source: mainline/uspace/lib/c/generic/rtld/symbol.c@ a7bd30f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a7bd30f was 0f792c28, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Source for R_*_COPY relocations must be searched for everywhere except for the executable itself (Thanks Jakub)

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[1ea99cc]1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup rtld rtld
30 * @brief
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39
[bfdb5af1]40#include <elf/elf.h>
[8a1fb09]41#include <rtld/rtld.h>
42#include <rtld/rtld_debug.h>
43#include <rtld/symbol.h>
[1ea99cc]44
45/*
46 * Hash tables are 32-bit (elf_word) even for 64-bit ELF files.
47 */
48static elf_word elf_hash(const unsigned char *name)
49{
50 elf_word h = 0, g;
51
52 while (*name) {
53 h = (h << 4) + *name++;
54 g = h & 0xf0000000;
55 if (g != 0) h ^= g >> 24;
56 h &= ~g;
57 }
58
59 return h;
60}
61
[04803bf]62static elf_symbol_t *def_find_in_module(const char *name, module_t *m)
[1ea99cc]63{
64 elf_symbol_t *sym_table;
65 elf_symbol_t *s, *sym;
66 elf_word nbucket;
[e2098dd7]67 /*elf_word nchain;*/
[1ea99cc]68 elf_word i;
69 char *s_name;
70 elf_word bucket;
71
72 DPRINTF("def_find_in_module('%s', %s)\n", name, m->dyn.soname);
73
74 sym_table = m->dyn.sym_tab;
75 nbucket = m->dyn.hash[0];
[e2098dd7]76 /*nchain = m->dyn.hash[1]; XXX Use to check HT range*/
[1ea99cc]77
78 bucket = elf_hash((unsigned char *)name) % nbucket;
79 i = m->dyn.hash[2 + bucket];
80
81 sym = NULL;
82 while (i != STN_UNDEF) {
83 s = &sym_table[i];
84 s_name = m->dyn.str_tab + s->st_name;
85
86 if (str_cmp(name, s_name) == 0) {
87 sym = s;
88 break;
89 }
90
91 i = m->dyn.hash[2 + nbucket + i];
92 }
93
94 if (!sym)
95 return NULL; /* Not found */
96
97 if (sym->st_shndx == SHN_UNDEF) {
98 /* Not a definition */
99 return NULL;
100 }
101
102 return sym; /* Found */
103}
104
105/** Find the definition of a symbol in a module and its deps.
106 *
107 * Search the module dependency graph is breadth-first, beginning
108 * from the module @a start. Thus, @start and all its dependencies
109 * get searched.
110 *
111 * @param name Name of the symbol to search for.
112 * @param start Module in which to start the search..
[0f792c28]113 * @param flags @c ssf_none or @c ssf_noroot to not look for the symbol
114 * in @a start
[1ea99cc]115 * @param mod (output) Will be filled with a pointer to the module
116 * that contains the symbol.
117 */
[0f792c28]118elf_symbol_t *symbol_bfs_find(const char *name, module_t *start,
119 symbol_search_flags_t flags, module_t **mod)
[1ea99cc]120{
121 module_t *m, *dm;
122 elf_symbol_t *sym, *s;
[bfdb5af1]123 list_t queue;
[1ea99cc]124 size_t i;
125
126 /*
127 * Do a BFS using the queue_link and bfs_tag fields.
128 * Vertices (modules) are tagged the moment they are inserted
129 * into the queue. This prevents from visiting the same vertex
130 * more times in case of circular dependencies.
131 */
132
133 /* Mark all vertices (modules) as unvisited */
134 modules_untag();
135
136 /* Insert root (the program) into the queue and tag it */
[bfdb5af1]137 list_initialize(&queue);
[1ea99cc]138 start->bfs_tag = true;
[bfdb5af1]139 list_append(&start->queue_link, &queue);
[1ea99cc]140
141 /* If the symbol is found, it will be stored in 'sym' */
142 sym = NULL;
143
144 /* While queue is not empty */
[bfdb5af1]145 while (!list_empty(&queue)) {
[1ea99cc]146 /* Pop first element from the queue */
[bfdb5af1]147 m = list_get_instance(list_first(&queue), module_t, queue_link);
[1ea99cc]148 list_remove(&m->queue_link);
149
[0f792c28]150 /* If ssf_noroot is specified, do not look in start module */
151 if (m != start || (flags & ssf_noroot) == 0) {
152 s = def_find_in_module(name, m);
153 if (s != NULL) {
154 /* Symbol found */
155 sym = s;
156 *mod = m;
157 break;
158 }
[1ea99cc]159 }
160
161 /*
162 * Insert m's untagged dependencies into the queue
163 * and tag them.
164 */
165 for (i = 0; i < m->n_deps; ++i) {
166 dm = m->deps[i];
167
168 if (dm->bfs_tag == false) {
169 dm->bfs_tag = true;
[bfdb5af1]170 list_append(&dm->queue_link, &queue);
[1ea99cc]171 }
172 }
173 }
174
175 /* Empty the queue so that we leave it in a clean state */
[bfdb5af1]176 while (!list_empty(&queue))
177 list_remove(list_first(&queue));
[1ea99cc]178
179 if (!sym) {
180 return NULL; /* Not found */
181 }
182
183 return sym; /* Symbol found */
184}
185
186
[0f792c28]187/** Find the definition of a symbol.
[1ea99cc]188 *
189 * By definition in System V ABI, if module origin has the flag DT_SYMBOLIC,
190 * origin is searched first. Otherwise, or if the symbol hasn't been found,
191 * the module dependency graph is searched breadth-first, beginning
192 * from the executable program.
193 *
194 * @param name Name of the symbol to search for.
195 * @param origin Module in which the dependency originates.
[0f792c28]196 * @param flags @c ssf_none or @c ssf_noroot to not look for the symbol
197 * in the executable program.
[1ea99cc]198 * @param mod (output) Will be filled with a pointer to the module
199 * that contains the symbol.
200 */
[0f792c28]201elf_symbol_t *symbol_def_find(const char *name, module_t *origin,
202 symbol_search_flags_t flags, module_t **mod)
[1ea99cc]203{
[04803bf]204 elf_symbol_t *s;
[1ea99cc]205
206 if (origin->dyn.symbolic) {
207 /*
208 * Origin module has a DT_SYMBOLIC flag.
209 * Try this module first
210 */
211 s = def_find_in_module(name, origin);
212 if (s != NULL) {
213 /* Found */
214 *mod = origin;
215 return s;
216 }
217 }
218
219 /* Not DT_SYMBOLIC or no match. Now try other locations. */
220
221 if (runtime_env->program) {
222 /* Program is dynamic -- start with program as root. */
[0f792c28]223 return symbol_bfs_find(name, runtime_env->program, flags, mod);
[04803bf]224 } else {
[1ea99cc]225 /* Program is static -- start with @a origin as root. */
[0f792c28]226 return symbol_bfs_find(name, origin, ssf_none, mod);
[1ea99cc]227 }
228}
229
[04803bf]230void *symbol_get_addr(elf_symbol_t *sym, module_t *m)
[1ea99cc]231{
232 if (sym->st_shndx == SHN_ABS) {
233 /* Do not add bias to absolute symbols */
[04803bf]234 return (void *) sym->st_value;
[1ea99cc]235 } else {
[04803bf]236 return (void *) (sym->st_value + m->bias);
[1ea99cc]237 }
238}
239
240/** @}
241 */
Note: See TracBrowser for help on using the repository browser.