OSDN Git Service

mkostemp: fix implementation
[uclinux-h8/uClibc.git] / ldso / ldso / dl-hash.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Program to load an ELF binary on a linux system, and run it
4  * after resolving ELF shared library symbols
5  *
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
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33
34 /* Various symbol table handling functions, including symbol lookup */
35 /*
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
38  * chains.
39  */
40 struct dyn_elf *_dl_symbol_tables = NULL;
41
42 /*
43  * This is the list of modules that are loaded via dlopen.  We may need
44  * to search these for RTLD_GLOBAL files.
45  */
46 struct dyn_elf *_dl_handles = NULL;
47
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)
53 {
54   unsigned long h = 5381;
55   unsigned char c;
56   for (c = *name; c != '\0'; c = *++name)
57     h = h * 33 + c;
58   return h & 0xffffffff;
59 }
60 #endif
61
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)
66 {
67         unsigned long hash=0;
68         unsigned long tmp;
69
70         while (*name) {
71                 hash = (hash << 4) + *name++;
72                 tmp = hash & 0xf0000000;
73                 /* The algorithm specified in the ELF ABI is as follows:
74                    if (tmp != 0)
75                        hash ^= tmp >> 24;
76                    hash &= ~tmp;
77                    But the following is equivalent and a lot
78                    faster, especially on modern processors. */
79                 hash ^= tmp;
80                 hash ^= tmp >> 24;
81         }
82         return hash;
83 }
84
85 /*
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
88  * externals properly.
89  */
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)
93 {
94         Elf_Symndx *hash_addr;
95         struct elf_resolve *tpnt;
96         int i;
97
98         tpnt = _dl_malloc(sizeof(struct elf_resolve));
99         _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
100
101         if (!_dl_loaded_modules)
102                 _dl_loaded_modules = tpnt;
103         else {
104                 struct elf_resolve *t = _dl_loaded_modules;
105                 while (t->next)
106                         t = t->next;
107                 t->next = tpnt;
108                 t->next->prev = t;
109                 tpnt = t->next;
110         }
111
112         tpnt->next = NULL;
113         tpnt->init_flag = 0;
114         tpnt->libname = _dl_strdup(libname);
115         tpnt->dynamic_addr = (ElfW(Dyn) *)dynamic_addr;
116         tpnt->libtype = loaded_file;
117
118 #ifdef __DSBT__
119         if (dynamic_info[DT_DSBT_BASE_IDX] != 0)
120                 tpnt->dsbt_table = (void *)dynamic_info[DT_DSBT_BASE_IDX];
121         if (dynamic_info[DT_DSBT_SIZE_IDX] != 0)
122                 tpnt->dsbt_size = dynamic_info[DT_DSBT_SIZE_IDX];
123         if (dynamic_info[DT_DSBT_INDEX_IDX] != 0)
124                 tpnt->dsbt_index = dynamic_info[DT_DSBT_INDEX_IDX];
125 #endif /* __DSBT__ */
126
127 #ifdef __LDSO_GNU_HASH_SUPPORT__
128         if (dynamic_info[DT_GNU_HASH_IDX] != 0) {
129                 Elf32_Word *hash32 = (Elf_Symndx*)dynamic_info[DT_GNU_HASH_IDX];
130
131                 tpnt->nbucket = *hash32++;
132                 Elf32_Word symbias = *hash32++;
133                 Elf32_Word bitmask_nwords = *hash32++;
134                 /* Must be a power of two.  */
135                 _dl_assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
136                 tpnt->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
137                 tpnt->l_gnu_shift = *hash32++;
138
139                 tpnt->l_gnu_bitmask = (ElfW(Addr) *) hash32;
140                 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
141
142                 tpnt->l_gnu_buckets = hash32;
143                 hash32 += tpnt->nbucket;
144                 tpnt->l_gnu_chain_zero = hash32 - symbias;
145         } else
146         /* Fall using old SysV hash table if GNU hash is not present */
147 #endif
148
149         if (dynamic_info[DT_HASH] != 0) {
150                 hash_addr = (Elf_Symndx*)dynamic_info[DT_HASH];
151                 tpnt->nbucket = *hash_addr++;
152                 tpnt->nchain = *hash_addr++;
153                 tpnt->elf_buckets = hash_addr;
154                 hash_addr += tpnt->nbucket;
155                 tpnt->chains = hash_addr;
156         }
157         tpnt->loadaddr = loadaddr;
158         for (i = 0; i < DYNAMIC_SIZE; i++)
159                 tpnt->dynamic_info[i] = dynamic_info[i];
160         return tpnt;
161 }
162
163
164 /* Routine to check whether the symbol matches.  */
165 static __attribute_noinline__ const ElfW(Sym) *
166 check_match (const ElfW(Sym) *sym, char *strtab, const char* undef_name, int type_class)
167 {
168
169 #if defined(USE_TLS) && USE_TLS
170         if ((sym->st_value == 0 && (ELF_ST_TYPE(sym->st_info) != STT_TLS))
171                       || (type_class & (sym->st_shndx == SHN_UNDEF)))
172                 /* No value or undefined symbol itself */
173                 return NULL;
174
175         if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
176                 && ELF_ST_TYPE(sym->st_info) != STT_COMMON
177                 && ELF_ST_TYPE(sym->st_info) != STT_TLS)
178                 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC and STT_COMMON
179                  * entries (and STT_TLS if TLS is supported) since these
180                  * are no code/data definitions.
181                  */
182                 return NULL;
183 #else
184         if (type_class & (sym->st_shndx == SHN_UNDEF))
185                 /* undefined symbol itself */
186                 return NULL;
187
188         if (sym->st_value == 0)
189                 /* No value */
190                 return NULL;
191
192         if (ELF_ST_TYPE(sym->st_info) > STT_FUNC
193                 && ELF_ST_TYPE(sym->st_info) != STT_COMMON)
194                 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC
195                  * and STT_COMMON entries since these are no
196                  * code/data definitions
197                  */
198                 return NULL;
199 #endif
200 #ifdef ARCH_SKIP_RELOC
201         if (ARCH_SKIP_RELOC(type_class, sym))
202                 return NULL;
203 #endif
204         if (_dl_strcmp(strtab + sym->st_name, undef_name) != 0)
205                 return NULL;
206
207         /* This is the matching symbol */
208         return sym;
209 }
210
211
212 #ifdef __LDSO_GNU_HASH_SUPPORT__
213
214 static __always_inline const ElfW(Sym) *
215 _dl_lookup_gnu_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash,
216                                         const char* undef_name, int type_class)
217 {
218         Elf_Symndx symidx;
219         const ElfW(Sym) *sym;
220         char *strtab;
221
222         const ElfW(Addr) *bitmask = tpnt->l_gnu_bitmask;
223
224         ElfW(Addr) bitmask_word = bitmask[(hash / __ELF_NATIVE_CLASS) & tpnt->l_gnu_bitmask_idxbits];
225
226         unsigned int hashbit1 = hash & (__ELF_NATIVE_CLASS - 1);
227         unsigned int hashbit2 = ((hash >> tpnt->l_gnu_shift) & (__ELF_NATIVE_CLASS - 1));
228         _dl_assert (bitmask != NULL);
229
230         if (unlikely((bitmask_word >> hashbit1) & (bitmask_word >> hashbit2) & 1)) {
231                 unsigned long rem;
232                 Elf32_Word bucket;
233
234                 do_rem (rem, hash, tpnt->nbucket);
235                 bucket = tpnt->l_gnu_buckets[rem];
236
237                 if (bucket != 0) {
238                         const Elf32_Word *hasharr = &tpnt->l_gnu_chain_zero[bucket];
239                         do {
240                                 if (((*hasharr ^ hash) >> 1) == 0) {
241                                         symidx = hasharr - tpnt->l_gnu_chain_zero;
242                                         strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
243                                         sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
244                                         if (sym != NULL)
245                                                 return sym;
246                                 }
247                         } while ((*hasharr++ & 1u) == 0);
248                 }
249         }
250         /* No symbol found.  */
251         return NULL;
252 }
253 #endif
254
255 static __always_inline const ElfW(Sym) *
256 _dl_lookup_sysv_hash(struct elf_resolve *tpnt, ElfW(Sym) *symtab, unsigned long hash,  const char* undef_name, int type_class)
257 {
258         unsigned long hn;
259         char *strtab;
260         const ElfW(Sym) *sym;
261         Elf_Symndx symidx;
262
263         /* Avoid calling .urem here. */
264         do_rem(hn, hash, tpnt->nbucket);
265         strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]);
266
267         _dl_assert(tpnt->elf_buckets != NULL);
268
269         for (symidx = tpnt->elf_buckets[hn]; symidx != STN_UNDEF; symidx = tpnt->chains[symidx]) {
270                 sym = check_match (&symtab[symidx], strtab, undef_name, type_class);
271                 if (sym != NULL)
272                         /* At this point the symbol is that we are looking for */
273                         return sym;
274         }
275         /* No symbol found into the current module*/
276         return NULL;
277 }
278
279 /*
280  * This function resolves externals, and this is either called when we process
281  * relocations or when we call an entry in the PLT table for the first time.
282  */
283 char *_dl_find_hash(const char *name, struct r_scope_elem *scope, struct elf_resolve *mytpnt,
284         int type_class, struct symbol_ref *sym_ref)
285 {
286         struct elf_resolve *tpnt = NULL;
287         ElfW(Sym) *symtab;
288         int i = 0;
289
290         unsigned long elf_hash_number = 0xffffffff;
291         const ElfW(Sym) *sym = NULL;
292
293         char *weak_result = NULL;
294         struct r_scope_elem *loop_scope;
295
296 #ifdef __LDSO_GNU_HASH_SUPPORT__
297         unsigned long gnu_hash_number = _dl_gnu_hash((const unsigned char *)name);
298 #endif
299
300         if ((sym_ref) && (sym_ref->sym) && (ELF32_ST_VISIBILITY(sym_ref->sym->st_other) == STV_PROTECTED)) {
301                         sym = sym_ref->sym;
302                 if (mytpnt)
303                         tpnt = mytpnt;
304         } else
305         for (loop_scope = scope; loop_scope && !sym; loop_scope = loop_scope->next) {
306                 for (i = 0; i < loop_scope->r_nlist; i++) {
307                         tpnt = loop_scope->r_list[i];
308
309                         if (!(tpnt->rtld_flags & RTLD_GLOBAL) && mytpnt) {
310                                 if (mytpnt == tpnt)
311                                         ;
312                                 else {
313                                         struct init_fini_list *tmp;
314
315                                         for (tmp = mytpnt->rtld_local; tmp; tmp = tmp->next) {
316                                                 if (tmp->tpnt == tpnt)
317                                                         break;
318                                         }
319                                         if (!tmp)
320                                                 continue;
321                                 }
322                         }
323                         /* Don't search the executable when resolving a copy reloc. */
324                         if ((type_class &  ELF_RTYPE_CLASS_COPY) && tpnt->libtype == elf_executable)
325                                 continue;
326
327                         /* If the hash table is empty there is nothing to do here.  */
328                         if (tpnt->nbucket == 0)
329                                 continue;
330
331                         symtab = (ElfW(Sym) *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB]);
332
333 #ifdef __LDSO_GNU_HASH_SUPPORT__
334                         /* Prefer GNU hash style, if any */
335                         if (tpnt->l_gnu_bitmask) {
336                                 sym = _dl_lookup_gnu_hash(tpnt, symtab, gnu_hash_number, name, type_class);
337                                 if (sym != NULL)
338                                         /* If sym has been found, do not search further */
339                                         break;
340                         } else {
341 #endif
342                                 /* Use the old SysV-style hash table */
343
344                                 /* Calculate the old sysv hash number only once */
345                                 if (elf_hash_number == 0xffffffff)
346                                         elf_hash_number = _dl_elf_hash((const unsigned char *)name);
347
348                                 sym = _dl_lookup_sysv_hash(tpnt, symtab, elf_hash_number, name, type_class);
349                                 if (sym != NULL)
350                                         /* If sym has been found, do not search further */
351                                         break;
352 #ifdef __LDSO_GNU_HASH_SUPPORT__
353                         }
354 #endif
355                 } /* End of inner for */
356         }
357
358         if (sym) {
359                 if (sym_ref) {
360                         sym_ref->sym = sym;
361                         sym_ref->tpnt = tpnt;
362                 }
363                 /* At this point we have found the requested symbol, do binding */
364 #if defined(USE_TLS) && USE_TLS
365                 if (ELF_ST_TYPE(sym->st_info) == STT_TLS) {
366                         _dl_assert(sym_ref != NULL);
367                         return (char *)sym->st_value;
368                 }
369 #endif
370
371                 switch (ELF_ST_BIND(sym->st_info)) {
372                         case STB_WEAK:
373 #if 0
374         /* Perhaps we should support old style weak symbol handling
375         * per what glibc does when you export LD_DYNAMIC_WEAK */
376                                 if (!weak_result)
377                                         weak_result = (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
378                                 break;
379 #endif
380                         case STB_GLOBAL:
381 #ifdef __FDPIC__
382                         if (sym_ref)
383                                 sym_ref->tpnt = tpnt;
384 #endif
385                                 return (char *)DL_FIND_HASH_VALUE(tpnt, type_class, sym);
386                         default:        /* Local symbols not handled here */
387                                 break;
388                 }
389         }
390 #ifdef __FDPIC__
391         if (sym_ref)
392                 sym_ref->tpnt = tpnt;
393 #endif
394         return weak_result;
395 }