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

Last change on this file since df2b4ce7 was df2b4ce7, checked in by Matěj Volf <git@…>, 4 months ago

check for hash section presence in rtld

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