1 /* vi: set sw=4 ts=4: */
3 * Program to load an ELF binary on a linux system, and run it
4 * after resolving ELF shared library symbols
6 * Copyright (C) 2004 by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
7 * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
8 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
9 * David Engel, Hongjiu Lu and Mitch D'Souza
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the above contributors may not be
17 * used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Various symbol table handling functions, including symbol lookup */
36 * This is the list of modules that are loaded when the image is first
37 * started. As we add more via dlopen, they get added into other
40 struct dyn_elf *_dl_symbol_tables = NULL;
43 * This is the list of modules that are loaded via dlopen. We may need
44 * to search these for RTLD_GLOBAL files.
46 struct dyn_elf *_dl_handles = NULL;
48 #ifdef __LDSO_GNU_HASH_SUPPORT__
49 /* This is the new hash function that is used by the ELF linker to generate the
50 * GNU hash table that each executable and library will have if --hash-style=[gnu,both]
51 * is passed to the linker. We need it to decode the GNU hash table. */
52 static __inline__ Elf_Symndx _dl_gnu_hash (const unsigned char *name)
54 unsigned long h = 5381;
56 for (c = *name; c != '\0'; c = *++name)
58 return h & 0xffffffff;
62 /* This is the hash function that is used by the ELF linker to generate the
63 * hash table that each executable and library is required to have. We need
64 * it to decode the hash table. */
65 static __inline__ Elf_Symndx _dl_elf_hash(const unsigned char *name)
71 hash = (hash << 4) + *name++;
72 tmp = hash & 0xf0000000;
73 /* The algorithm specified in the ELF ABI is as follows:
77 But the following is equivalent and a lot
78 faster, especially on modern processors. */
86 * We call this function when we have just read an ELF library or executable.
87 * We add the relevant info to the symbol chain, so that we can resolve all
90 struct elf_resolve *_dl_add_elf_hash_table(const char *libname,
91 DL_LOADADDR_TYPE loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
92 attribute_unused unsigned long dynamic_size)
94 Elf_Symndx *hash_addr;
95 struct elf_resolve *tpnt;
98 tpnt = _dl_malloc(sizeof(struct elf_resolve));
99 _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
101 if (!_dl_loaded_modules)
102 _dl_loaded_modules = tpnt;
104 struct elf_resolve *t = _dl_loaded_modules;
114 tpnt->libname = _dl_strdup(libname);
115 tpnt->dynamic_addr = (ElfW(Dyn) *)dynamic_addr;
116 tpnt->libtype = loaded_file;
118 #ifdef __LDSO_GNU_HASH_SUPPORT__
119 if (dynamic_info[DT_GNU_HASH_IDX] != 0) {
120 Elf32_Word *hash32 = (Elf_Symndx*)dynamic_info[DT_GNU_HASH_IDX];
122 tpnt->nbucket = *hash32++;
123 Elf32_Word symbias = *hash32++;
124 Elf32_Word bitmask_nwords = *hash32++;
125 /* Must be a power of two. */
126 _dl_assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
127 tpnt->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
128 tpnt->l_gnu_shift = *hash32++;
130 tpnt->l_gnu_bitmask = (ElfW(Addr) *) hash32;
131 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
133 tpnt->l_gnu_buckets = hash32;
134 hash32 += tpnt->nbucket;
135 tpnt->l_gnu_chain_zero = hash32 - symbias;
137 /* Fall using old SysV hash table if GNU hash is not present */
140 if (dynamic_info[DT_HASH] != 0) {
141 hash_addr = (Elf_Symndx*)dynamic_info[DT_HASH];
142 tpnt->nbucket = *hash_addr++;
143 tpnt->nchain = *hash_addr++;
144 tpnt->elf_buckets = hash_addr;
145 hash_addr += tpnt->nbucket;
146 tpnt->chains = hash_addr;
148 tpnt->loadaddr = loadaddr;
149 for (i = 0; i < DYNAMIC_SIZE; i++)
150 tpnt->dynamic_info[i] = dynamic_info[i];
155 /* Routine to check whether the symbol matches. */
156 static __attribute_noinline__ const ElfW(Sym) *
157 check_match (const ElfW(Sym) *sym, char *strtab, const char* undef_name, int type_class)
160 #if defined(USE_TLS) && USE_TLS
161 if ((sym->st_value == 0 && (ELF_ST_TYPE(sym->st_info) != STT_TLS))
162 || (type_class & (sym->st_shndx == SHN_UNDEF)))
163 /* No value or undefined symbol itself */
166 if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
167 && ELF_ST_TYPE(sym->st_info) != STT_COMMON
168 && ELF_ST_TYPE(sym->st_info) != STT_TLS)
169 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC and STT_COMMON
170 * entries (and STT_TLS if TLS is supported) since these
171 * are no code/data definitions.
175 if (type_class & (sym->st_shndx == SHN_UNDEF))
176 /* undefined symbol itself */
179 if (sym->st_value == 0)
183 if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
184 && ELF_ST_TYPE(sym->st_info) != STT_COMMON)
185 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC
186 * and STT_COMMON entries since these are no
187 * code/data definitions
191 if (_dl_strcmp(strtab + sym->st_name, undef_name) != 0)
194 /* This is the matching symbol */
199 #ifdef __LDSO_GNU_HASH_SUPPORT__
201 static __always_inline const ElfW(Sym) *
202 _dl_lookup_gnu_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash,
203 const char* undef_name, int type_class)
206 const ElfW(Sym) *sym;
209 const ElfW(Addr) *bitmask = tpnt->l_gnu_bitmask;
211 ElfW(Addr) bitmask_word = bitmask[(hash / __ELF_NATIVE_CLASS) & tpnt->l_gnu_bitmask_idxbits];
213 unsigned int hashbit1 = hash & (__ELF_NATIVE_CLASS - 1);
214 unsigned int hashbit2 = ((hash >> tpnt->l_gnu_shift) & (__ELF_NATIVE_CLASS - 1));
215 _dl_assert (bitmask != NULL);
217 if (unlikely((bitmask_word >> hashbit1) & (bitmask_word >> hashbit2) & 1)) {
221 do_rem (rem, hash, tpnt->nbucket);
222 bucket = tpnt->l_gnu_buckets[rem];
225 const Elf32_Word *hasharr = &tpnt->l_gnu_chain_zero[bucket];
227 if (((*hasharr ^ hash) >> 1) == 0) {
228 symidx = hasharr - tpnt->l_gnu_chain_zero;
229 strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
230 sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
234 } while ((*hasharr++ & 1u) == 0);
237 /* No symbol found. */
242 static __always_inline const ElfW(Sym) *
243 _dl_lookup_sysv_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash, const char* undef_name, int type_class)
247 const ElfW(Sym) *sym;
250 /* Avoid calling .urem here. */
251 do_rem(hn, hash, tpnt->nbucket);
252 strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
254 _dl_assert(tpnt->elf_buckets != NULL);
256 for (symidx = tpnt->elf_buckets[hn]; symidx != STN_UNDEF; symidx = tpnt->chains[symidx]) {
257 sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
259 /* At this point the symbol is that we are looking for */
262 /* No symbol found into the current module*/
267 * This function resolves externals, and this is either called when we process
268 * relocations or when we call an entry in the PLT table for the first time.
270 char *_dl_find_hash(const char *name, struct r_scope_elem *scope, struct elf_resolve *mytpnt,
271 int type_class, struct symbol_ref *sym_ref)
273 struct elf_resolve *tpnt = NULL;
277 unsigned long elf_hash_number = 0xffffffff;
278 const ElfW(Sym) *sym = NULL;
280 char *weak_result = NULL;
281 struct r_scope_elem *loop_scope;
283 #ifdef __LDSO_GNU_HASH_SUPPORT__
284 unsigned long gnu_hash_number = _dl_gnu_hash((const unsigned char *)name);
287 if ((sym_ref) && (sym_ref->sym) && (ELF32_ST_VISIBILITY(sym_ref->sym->st_other) == STV_PROTECTED)) {
292 for (loop_scope = scope; loop_scope && !sym; loop_scope = loop_scope->next) {
293 for (i = 0; i < loop_scope->r_nlist; i++) {
294 tpnt = loop_scope->r_list[i];
296 if (!(tpnt->rtld_flags & RTLD_GLOBAL) && mytpnt) {
300 struct init_fini_list *tmp;
302 for (tmp = mytpnt->rtld_local; tmp; tmp = tmp->next) {
303 if (tmp->tpnt == tpnt)
310 /* Don't search the executable when resolving a copy reloc. */
311 if ((type_class & ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
314 /* If the hash table is empty there is nothing to do here. */
315 if (tpnt->nbucket == 0)
318 symtab = (ElfW(Sym) *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB]);
320 #ifdef __LDSO_GNU_HASH_SUPPORT__
321 /* Prefer GNU hash style, if any */
322 if (tpnt->l_gnu_bitmask) {
323 sym = _dl_lookup_gnu_hash(tpnt, symtab, gnu_hash_number, name, type_class);
325 /* If sym has been found, do not search further */
329 /* Use the old SysV-style hash table */
331 /* Calculate the old sysv hash number only once */
332 if (elf_hash_number == 0xffffffff)
333 elf_hash_number = _dl_elf_hash((const unsigned char *)name);
335 sym = _dl_lookup_sysv_hash(tpnt, symtab, elf_hash_number, name, type_class);
337 /* If sym has been found, do not search further */
339 #ifdef __LDSO_GNU_HASH_SUPPORT__
342 } /* End of inner for */
348 sym_ref->tpnt = tpnt;
350 /* At this point we have found the requested symbol, do binding */
351 #if defined(USE_TLS) && USE_TLS
352 if (ELF_ST_TYPE(sym->st_info) == STT_TLS) {
353 _dl_assert(sym_ref != NULL);
354 sym_ref->tpnt = tpnt;
355 return (char *)sym->st_value;
359 switch (ELF_ST_BIND(sym->st_info)) {
362 /* Perhaps we should support old style weak symbol handling
363 * per what glibc does when you export LD_DYNAMIC_WEAK */
365 weak_result = (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
371 sym_ref->tpnt = tpnt;
373 return (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
374 default: /* Local symbols not handled here */
380 sym_ref->tpnt = tpnt;