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

Last change on this file since a64970e1 was dd7df1c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 21 months ago

Silence spurious/unwanted GCC warnings using pragmas

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