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

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

Default symbol search vs. dlsym's BFS search.

  • Property mode set to 100644
File size: 6.7 KB
Line 
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
40#include <elf/elf.h>
41#include <rtld/module.h>
42#include <rtld/rtld.h>
43#include <rtld/rtld_debug.h>
44#include <rtld/symbol.h>
45
46/*
47 * Hash tables are 32-bit (elf_word) even for 64-bit ELF files.
48 */
49static elf_word elf_hash(const unsigned char *name)
50{
51 elf_word h = 0, g;
52
53 while (*name) {
54 h = (h << 4) + *name++;
55 g = h & 0xf0000000;
56 if (g != 0) h ^= g >> 24;
57 h &= ~g;
58 }
59
60 return h;
61}
62
63static elf_symbol_t *def_find_in_module(const char *name, module_t *m)
64{
65 elf_symbol_t *sym_table;
66 elf_symbol_t *s, *sym;
67 elf_word nbucket;
68 /*elf_word nchain;*/
69 elf_word i;
70 char *s_name;
71 elf_word bucket;
72
73 DPRINTF("def_find_in_module('%s', %s)\n", name, m->dyn.soname);
74
75 sym_table = m->dyn.sym_tab;
76 nbucket = m->dyn.hash[0];
77 /*nchain = m->dyn.hash[1]; XXX Use to check HT range*/
78
79 bucket = elf_hash((unsigned char *)name) % nbucket;
80 i = m->dyn.hash[2 + bucket];
81
82 sym = NULL;
83 while (i != STN_UNDEF) {
84 s = &sym_table[i];
85 s_name = m->dyn.str_tab + s->st_name;
86
87 if (str_cmp(name, s_name) == 0) {
88 sym = s;
89 break;
90 }
91
92 i = m->dyn.hash[2 + nbucket + i];
93 }
94
95 if (!sym)
96 return NULL; /* Not found */
97
98 if (sym->st_shndx == SHN_UNDEF) {
99 /* Not a definition */
100 return NULL;
101 }
102
103 return sym; /* Found */
104}
105
106/** Find the definition of a symbol in a module and its deps.
107 *
108 * Search the module dependency graph is breadth-first, beginning
109 * from the module @a start. Thus, @start and all its dependencies
110 * get searched.
111 *
112 * @param name Name of the symbol to search for.
113 * @param start Module in which to start the search..
114 * @param mod (output) Will be filled with a pointer to the module
115 * that contains the symbol.
116 */
117elf_symbol_t *symbol_bfs_find(const char *name, module_t *start,
118 module_t **mod)
119{
120 module_t *m, *dm;
121 elf_symbol_t *sym, *s;
122 list_t queue;
123 size_t i;
124
125 /*
126 * Do a BFS using the queue_link and bfs_tag fields.
127 * Vertices (modules) are tagged the moment they are inserted
128 * into the queue. This prevents from visiting the same vertex
129 * more times in case of circular dependencies.
130 */
131
132 /* Mark all vertices (modules) as unvisited */
133 modules_untag(start->rtld);
134
135 /* Insert root (the program) into the queue and tag it */
136 list_initialize(&queue);
137 start->bfs_tag = true;
138 list_append(&start->queue_link, &queue);
139
140 /* If the symbol is found, it will be stored in 'sym' */
141 sym = NULL;
142
143 /* While queue is not empty */
144 while (!list_empty(&queue)) {
145 /* Pop first element from the queue */
146 m = list_get_instance(list_first(&queue), module_t, queue_link);
147 list_remove(&m->queue_link);
148
149 /* If ssf_noroot is specified, do not look in start module */
150 s = def_find_in_module(name, m);
151 if (s != NULL) {
152 /* Symbol found */
153 sym = s;
154 *mod = m;
155 break;
156 }
157
158 /*
159 * Insert m's untagged dependencies into the queue
160 * and tag them.
161 */
162 for (i = 0; i < m->n_deps; ++i) {
163 dm = m->deps[i];
164
165 if (dm->bfs_tag == false) {
166 dm->bfs_tag = true;
167 list_append(&dm->queue_link, &queue);
168 }
169 }
170 }
171
172 /* Empty the queue so that we leave it in a clean state */
173 while (!list_empty(&queue))
174 list_remove(list_first(&queue));
175
176 if (!sym) {
177 return NULL; /* Not found */
178 }
179
180 return sym; /* Symbol found */
181}
182
183
184/** Find the definition of a symbol.
185 *
186 * By definition in System V ABI, if module origin has the flag DT_SYMBOLIC,
187 * origin is searched first. Otherwise, search global modules in the default
188 * order.
189 *
190 * @param name Name of the symbol to search for.
191 * @param origin Module in which the dependency originates.
192 * @param flags @c ssf_none or @c ssf_noexec to not look for the symbol
193 * in the executable program.
194 * @param mod (output) Will be filled with a pointer to the module
195 * that contains the symbol.
196 */
197elf_symbol_t *symbol_def_find(const char *name, module_t *origin,
198 symbol_search_flags_t flags, module_t **mod)
199{
200 elf_symbol_t *s;
201
202 DPRINTF("symbol_def_find('%s', origin='%s'\n",
203 name, origin->dyn.soname);
204 if (origin->dyn.symbolic && (!origin->exec || (flags & ssf_noexec) == 0)) {
205 DPRINTF("symbolic->find '%s' in module '%s'\n", name, origin->dyn.soname);
206 /*
207 * Origin module has a DT_SYMBOLIC flag.
208 * Try this module first
209 */
210 s = def_find_in_module(name, origin);
211 if (s != NULL) {
212 /* Found */
213 *mod = origin;
214 return s;
215 }
216 }
217
218 /* Not DT_SYMBOLIC or no match. Now try other locations. */
219
220 list_foreach(origin->rtld->modules, modules_link, module_t, m) {
221 DPRINTF("module '%s' local?\n", m->dyn.soname);
222 if (!m->local && (!m->exec || (flags & ssf_noexec) == 0)) {
223 DPRINTF("!local->find '%s' in module '%s'\n", name, m->dyn.soname);
224 s = def_find_in_module(name, m);
225 if (s != NULL) {
226 /* Found */
227 *mod = m;
228 return s;
229 }
230 }
231 }
232
233 /* Finally, try origin. */
234
235 DPRINTF("try finding '%s' in origin '%s'\n", name,
236 origin->dyn.soname);
237
238 if (!origin->exec || (flags & ssf_noexec) == 0) {
239 s = def_find_in_module(name, origin);
240 if (s != NULL) {
241 /* Found */
242 *mod = origin;
243 return s;
244 }
245 }
246
247 DPRINTF("'%s' not found\n", name);
248 return NULL;
249}
250
251void *symbol_get_addr(elf_symbol_t *sym, module_t *m)
252{
253 if (sym->st_shndx == SHN_ABS) {
254 /* Do not add bias to absolute symbols */
255 return (void *) sym->st_value;
256 } else {
257 return (void *) (sym->st_value + m->bias);
258 }
259}
260
261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.