OSDN Git Service

Moved the addition of load address from the fast path
[uclinux-h8/uClibc.git] / ldso / libdl / libdl.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) 2000-2004 by Erik Andersen <andersen@codepoet.org>
7  * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8  *                              David Engel, Hongjiu Lu and Mitch D'Souza
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the above contributors may not be
16  *    used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32
33 #define _GNU_SOURCE
34 #include <ldso.h>
35 #include <stdio.h>
36
37
38 #if defined (__LIBDL_SHARED__)
39
40 /* When libdl is loaded as a shared library, we need to load in
41  * and use a pile of symbols from ldso... */
42
43 extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, int)
44         __attribute__ ((__weak__));
45 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
46         struct elf_resolve *, char *, int) __attribute__ ((__weak__));
47 extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
48         __attribute__ ((__weak__));
49 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
50          __attribute__ ((__weak__));
51 extern void _dl_protect_relro(struct elf_resolve * tpnt)
52         __attribute__ ((__weak__));
53 extern int _dl_errno __attribute__ ((__weak__));
54 extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
55 extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
56 extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
57 extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
58 extern unsigned long _dl_error_number __attribute__ ((__weak__));
59 extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
60 #ifdef __LDSO_CACHE_SUPPORT__
61 int _dl_map_cache(void) __attribute__ ((__weak__));
62 int _dl_unmap_cache(void) __attribute__ ((__weak__));
63 #endif
64 #ifdef __mips__
65 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
66         __attribute__ ((__weak__));
67 #endif
68 #ifdef __SUPPORT_LD_DEBUG__
69 extern char *_dl_debug __attribute__ ((__weak__));
70 #endif
71
72
73 #else /* __LIBDL_SHARED__ */
74
75 /* When libdl is linked as a static library, we need to replace all
76  * the symbols that otherwise would have been loaded in from ldso... */
77
78 #ifdef __SUPPORT_LD_DEBUG__
79 char *_dl_debug  = 0;
80 #endif
81 char *_dl_library_path         = 0;                 /* Where we look for libraries */
82 char *_dl_ldsopath             = 0;                 /* Location of the shared lib loader */
83 int _dl_errno                  = 0;         /* We can't use the real errno in ldso */
84 size_t _dl_pagesize            = PAGE_SIZE; /* Store the page size for use later */
85 /* This global variable is also to communicate with debuggers such as gdb. */
86 struct r_debug *_dl_debug_addr = NULL;
87 #define _dl_malloc malloc
88 #include "dl-progname.h"
89 #include "../ldso/dl-hash.c"
90 #define _dl_trace_loaded_objects    0
91 #include "../ldso/dl-elf.c"
92 #endif
93
94 static int do_dlclose(void *, int need_fini);
95
96
97 static const char *dl_error_names[] = {
98         "",
99         "File not found",
100         "Unable to open /dev/zero",
101         "Not an ELF file",
102 #if defined (__i386__)
103         "Not i386 binary",
104 #elif defined (__sparc__)
105         "Not sparc binary",
106 #elif defined (__mc68000__)
107         "Not m68k binary",
108 #else
109         "Unrecognized binary type",
110 #endif
111         "Not an ELF shared library",
112         "Unable to mmap file",
113         "No dynamic section",
114 #ifdef ELF_USES_RELOCA
115         "Unable to process REL relocs",
116 #else
117         "Unable to process RELA relocs",
118 #endif
119         "Bad handle",
120         "Unable to resolve symbol"
121 };
122
123 void __attribute__ ((destructor)) dl_cleanup(void)
124 {
125         struct dyn_elf *d;
126         for (d = _dl_handles; d; d = d->next_handle) {
127                 do_dlclose(d, 1);
128         }
129 }
130
131 void *dlopen(const char *libname, int flag)
132 {
133         struct elf_resolve *tpnt, *tfrom, *tcurr;
134         struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr;
135         struct dyn_elf *dpnt;
136         ElfW(Addr) from;
137         struct elf_resolve *tpnt1;
138         void (*dl_brk) (void);
139         int now_flag;
140         struct init_fini_list *tmp, *runp;
141         int nlist, i;
142         struct elf_resolve **init_fini_list;
143
144         /* A bit of sanity checking... */
145         if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
146                 _dl_error_number = LD_BAD_HANDLE;
147                 return NULL;
148         }
149
150         from = (ElfW(Addr)) __builtin_return_address(0);
151
152         /* Cover the trivial case first */
153         if (!libname)
154                 return _dl_symbol_tables;
155
156         _dl_map_cache();
157
158         /*
159          * Try and locate the module we were called from - we
160          * need this so that we get the correct RPATH.  Note that
161          * this is the current behavior under Solaris, but the
162          * ABI+ specifies that we should only use the RPATH from
163          * the application.  Thus this may go away at some time
164          * in the future.
165          */
166         tfrom = NULL;
167         for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
168                 tpnt = dpnt->dyn;
169                 if (tpnt->loadaddr < from
170                                 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
171                         tfrom = tpnt;
172         }
173         for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
174
175         relro_ptr = rpnt;
176         /* Try to load the specified library */
177 #ifdef __SUPPORT_LD_DEBUG__
178         if(_dl_debug)
179                 fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
180 #endif
181         tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0);
182         if (!(tpnt))
183                 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
184         else
185                 tpnt->usage_count++;
186         if (tpnt == NULL) {
187                 _dl_unmap_cache();
188                 return NULL;
189         }
190
191         dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
192         _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
193         dyn_chain->dyn = tpnt;
194         tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
195
196         dyn_chain->next_handle = _dl_handles;
197         _dl_handles = dyn_ptr = dyn_chain;
198
199 #ifdef __SUPPORT_LD_DEBUG__
200         if(_dl_debug)
201                 fprintf(stderr, "Looking for needed libraries\n");
202 #endif
203         nlist = 0;
204         for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
205         {
206                 Elf32_Dyn *dpnt;
207                 char *lpntstr;
208
209                 nlist++;
210                 tcurr->init_fini = NULL; /* clear any previous dependcies */
211                 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
212                         if (dpnt->d_tag == DT_NEEDED) {
213                                 char *name;
214
215                                 lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] +
216                                                 dpnt->d_un.d_val);
217                                 name = _dl_get_last_path_component(lpntstr);
218                                 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
219 #ifdef __SUPPORT_LD_DEBUG__
220                                 if(_dl_debug)
221                                         fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
222                                                         lpntstr, tcurr->libname);
223 #endif
224                                 if (tpnt1) {
225                                         tpnt1->usage_count++;
226                                 } else {
227                                         tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
228                                         if (!tpnt1)
229                                                 goto oops;
230                                 }
231                                 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
232                                 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
233                                 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
234                                 dyn_ptr = dyn_ptr->next;
235                                 dyn_ptr->dyn = tpnt1;
236
237                                 tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
238                                 tmp->tpnt = tpnt1;
239                                 tmp->next = tcurr->init_fini;
240                                 tcurr->init_fini = tmp;
241                         }
242                 }
243         }
244         init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
245         dyn_chain->init_fini.init_fini = init_fini_list;
246         dyn_chain->init_fini.nlist = nlist;
247         i = 0;
248         for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
249                 init_fini_list[i++] = tcurr;
250                 for(runp = tcurr->init_fini; runp; runp = runp->next){
251                         if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
252                                 tmp = malloc(sizeof(struct init_fini_list));
253                                 tmp->tpnt = runp->tpnt;
254                                 tmp->next = tcurr->rtld_local;
255                                 tcurr->rtld_local = tmp;
256                         }
257                 }
258
259         }
260         /* Sort the INIT/FINI list in dependency order. */
261         for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
262                 int j, k;
263                 for (j = 0; init_fini_list[j] != tcurr; ++j)
264                         /* Empty */;
265                 for (k = j + 1; k < nlist; ++k) {
266                         struct init_fini_list *runp = init_fini_list[k]->init_fini;
267
268                         for (; runp; runp = runp->next) {
269                                 if (runp->tpnt == tcurr) {
270                                         struct elf_resolve *here = init_fini_list[k];
271 #ifdef __SUPPORT_LD_DEBUG__
272                                         if(_dl_debug)
273                                                 fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
274 #endif
275                                         for (i = (k - j); i; --i)
276                                                 init_fini_list[i+j] = init_fini_list[i+j-1];
277                                         init_fini_list[j] = here;
278                                         ++j;
279                                         break;
280                                 }
281                         }
282                 }
283         }
284 #ifdef __SUPPORT_LD_DEBUG__
285         if(_dl_debug) {
286                 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
287                 for (i=0;i < nlist;i++) {
288                         fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
289                         runp = init_fini_list[i]->init_fini;
290                         for ( ;runp; runp = runp->next)
291                                 printf(" %s ", runp->tpnt->libname);
292                         printf("\n");
293                 }
294         }
295 #endif
296
297         if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
298                 /* If the init and fini stuff has already been run, that means
299                  * the dlopen'd library has already been loaded, and nothing
300                  * further needs to be done. */
301                 return (void *) dyn_chain;
302         }
303
304 #ifdef __SUPPORT_LD_DEBUG__
305         if(_dl_debug)
306                 fprintf(stderr, "Beginning dlopen relocation fixups\n");
307 #endif
308         /*
309          * OK, now all of the kids are tucked into bed in their proper addresses.
310          * Now we go through and look for REL and RELA records that indicate fixups
311          * to the GOT tables.  We need to do this in reverse order so that COPY
312          * directives work correctly */
313         now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
314         if (getenv("LD_BIND_NOW"))
315                 now_flag = RTLD_NOW;
316
317 #ifdef __mips__
318         /*
319          * Relocation of the GOT entries for MIPS have to be done
320          * after all the libraries have been loaded.
321          */
322         _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
323 #endif
324
325         if (_dl_fixup(dyn_chain, now_flag))
326                 goto oops;
327
328         for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
329                 if (rpnt->dyn->relro_size)
330                         _dl_protect_relro(rpnt->dyn);
331         }
332         /* TODO:  Should we set the protections of all pages back to R/O now ? */
333
334
335         /* Notify the debugger we have added some objects. */
336         if (_dl_debug_addr) {
337                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
338                 if (dl_brk != NULL) {
339                         _dl_debug_addr->r_state = RT_ADD;
340                         (*dl_brk) ();
341
342                         _dl_debug_addr->r_state = RT_CONSISTENT;
343                         (*dl_brk) ();
344                 }
345         }
346
347 #if defined (__LIBDL_SHARED__)
348         /* Run the ctors and setup the dtors */
349         for (i = nlist; i; --i) {
350                 tpnt = init_fini_list[i-1];
351                 if (tpnt->init_flag & INIT_FUNCS_CALLED)
352                         continue;
353                 tpnt->init_flag |= INIT_FUNCS_CALLED;
354
355                 if (tpnt->dynamic_info[DT_INIT]) {
356                         void (*dl_elf_func) (void);
357                         dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
358                         if (dl_elf_func && *dl_elf_func != NULL) {
359 #ifdef __SUPPORT_LD_DEBUG__
360                                 if(_dl_debug)
361                                         fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
362 #endif
363                                 (*dl_elf_func) ();
364                         }
365                 }
366         }
367 #endif
368         _dl_unmap_cache();
369         return (void *) dyn_chain;
370
371 oops:
372         /* Something went wrong.  Clean up and return NULL. */
373         _dl_unmap_cache();
374         do_dlclose(dyn_chain, 0);
375         return NULL;
376 }
377
378 void *dlsym(void *vhandle, const char *name)
379 {
380         struct elf_resolve *tpnt, *tfrom;
381         struct dyn_elf *handle;
382         ElfW(Addr) from;
383         struct dyn_elf *rpnt;
384         void *ret;
385
386         handle = (struct dyn_elf *) vhandle;
387
388         /* First of all verify that we have a real handle
389            of some kind.  Return NULL if not a valid handle. */
390
391         if (handle == NULL)
392                 handle = _dl_symbol_tables;
393         else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
394                 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
395                         if (rpnt == handle)
396                                 break;
397                 if (!rpnt) {
398                         _dl_error_number = LD_BAD_HANDLE;
399                         return NULL;
400                 }
401         } else if (handle == RTLD_NEXT) {
402                 /*
403                  * Try and locate the module we were called from - we
404                  * need this so that we know where to start searching
405                  * from.  We never pass RTLD_NEXT down into the actual
406                  * dynamic loader itself, as it doesn't know
407                  * how to properly treat it.
408                  */
409                 from = (ElfW(Addr)) __builtin_return_address(0);
410
411                 tfrom = NULL;
412                 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
413                         tpnt = rpnt->dyn;
414                         if (tpnt->loadaddr < from
415                                         && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
416                                 tfrom = tpnt;
417                                 handle = rpnt->next;
418                         }
419                 }
420         }
421
422         ret = _dl_find_hash((char*)name, handle, NULL, 0);
423
424         /*
425          * Nothing found.
426          */
427         if (!ret)
428                 _dl_error_number = LD_NO_SYMBOL;
429         return ret;
430 }
431
432 static int do_dlclose(void *vhandle, int need_fini)
433 {
434         struct dyn_elf *rpnt, *rpnt1;
435         struct init_fini_list *runp, *tmp;
436         ElfW(Phdr) *ppnt;
437         struct elf_resolve *tpnt;
438         int (*dl_elf_fini) (void);
439         void (*dl_brk) (void);
440         struct dyn_elf *handle;
441         unsigned int end;
442         int i = 0;
443
444         handle = (struct dyn_elf *) vhandle;
445         rpnt1 = NULL;
446         for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
447                 if (rpnt == handle)
448                         break;
449                 rpnt1 = rpnt;
450         }
451
452         if (!rpnt) {
453                 _dl_error_number = LD_BAD_HANDLE;
454                 return 1;
455         }
456         if (rpnt1)
457                 rpnt1->next_handle = rpnt->next_handle;
458         else
459                 _dl_handles = rpnt->next_handle;
460         if (need_fini) {
461                 for (i = 0; i < handle->init_fini.nlist; ++i) {
462                         tpnt = handle->init_fini.init_fini[i];
463                         if (tpnt->dynamic_info[DT_FINI] && tpnt->usage_count == 1 &&
464                             !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
465                                 tpnt->init_flag |= FINI_FUNCS_CALLED;
466                                 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
467 #ifdef __SUPPORT_LD_DEBUG__
468                                 if(_dl_debug)
469                                         fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
470 #endif
471                                 (*dl_elf_fini) ();
472                         }
473                 }
474         }
475         if (handle->dyn->usage_count == 1)
476                 free(handle->init_fini.init_fini);
477         /* OK, this is a valid handle - now close out the file */
478         for (rpnt = handle; rpnt; rpnt = rpnt->next) {
479                 tpnt = rpnt->dyn;
480                 if (--tpnt->usage_count == 0) {
481                         end = 0;
482                         for (i = 0, ppnt = tpnt->ppnt;
483                                         i < tpnt->n_phent; ppnt++, i++) {
484                                 if (ppnt->p_type != PT_LOAD)
485                                         continue;
486                                 if (end < ppnt->p_vaddr + ppnt->p_memsz)
487                                         end = ppnt->p_vaddr + ppnt->p_memsz;
488                         }
489                         _dl_munmap((void*)tpnt->loadaddr, end);
490                         /* Free elements in RTLD_LOCAL scope list */ 
491                         for (runp = tpnt->rtld_local; runp; runp = tmp) {
492                                 tmp = runp->next;
493                                 free(runp);
494                         }
495                         /* Next, remove tpnt from the loaded_module list */
496                         if (_dl_loaded_modules == tpnt) {
497                                 _dl_loaded_modules = tpnt->next;
498                                 if (_dl_loaded_modules)
499                                         _dl_loaded_modules->prev = 0;
500                         } else
501                                 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
502                                         if (tpnt->next == rpnt->dyn) {
503                                                 tpnt->next = tpnt->next->next;
504                                                 if (tpnt->next)
505                                                         tpnt->next->prev = tpnt;
506                                                 break;
507                                         }
508
509                         /* Next, remove tpnt from the global symbol table list */
510                         if (_dl_symbol_tables->dyn == rpnt->dyn) {
511                                 _dl_symbol_tables = rpnt->next;
512                                 if (_dl_symbol_tables)
513                                         _dl_symbol_tables->prev = 0;
514                         } else
515                                 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
516                                         if (rpnt1->next->dyn == rpnt->dyn) {
517                                                 free(rpnt1->next);
518                                                 rpnt1->next = rpnt1->next->next;
519                                                 if (rpnt1->next)
520                                                         rpnt1->next->prev = rpnt1;
521                                                 break;
522                                         }
523                                 }
524                         free(rpnt->dyn->libname);
525                         free(rpnt->dyn);
526                 }
527                 free(rpnt);
528         }
529
530
531         if (_dl_debug_addr) {
532                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
533                 if (dl_brk != NULL) {
534                         _dl_debug_addr->r_state = RT_DELETE;
535                         (*dl_brk) ();
536
537                         _dl_debug_addr->r_state = RT_CONSISTENT;
538                         (*dl_brk) ();
539                 }
540         }
541
542         return 0;
543 }
544
545 int dlclose(void *vhandle)
546 {
547         return do_dlclose(vhandle, 1);
548 }
549
550 char *dlerror(void)
551 {
552         const char *retval;
553
554         if (!_dl_error_number)
555                 return NULL;
556         retval = dl_error_names[_dl_error_number];
557         _dl_error_number = 0;
558         return (char *)retval;
559 }
560
561 /*
562  * Dump information to stderrr about the current loaded modules
563  */
564 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
565
566 int dlinfo(void)
567 {
568         struct elf_resolve *tpnt;
569         struct dyn_elf *rpnt, *hpnt;
570
571         fprintf(stderr, "List of loaded modules\n");
572         /* First start with a complete list of all of the loaded files. */
573         for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
574                 fprintf(stderr, "\t%x %x %x %s %d %s\n",
575                                 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
576                                 (unsigned) tpnt->symbol_scope,
577                                 type[tpnt->libtype],
578                                 tpnt->usage_count, tpnt->libname);
579         }
580
581         /* Next dump the module list for the application itself */
582         fprintf(stderr, "\nModules for application (%x):\n",
583                         (unsigned) _dl_symbol_tables);
584         for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
585                 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
586
587         for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
588                 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
589                 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
590                         fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
591                                         rpnt->dyn->libname);
592         }
593         return 0;
594 }
595
596 int dladdr(const void *__address, Dl_info * __info)
597 {
598         struct elf_resolve *pelf;
599         struct elf_resolve *rpnt;
600
601         _dl_map_cache();
602
603         /*
604          * Try and locate the module address is in
605          */
606         pelf = NULL;
607
608 #if 0
609         fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
610 #endif
611
612         for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
613                 struct elf_resolve *tpnt;
614
615                 tpnt = rpnt;
616 #if 0
617                 fprintf(stderr, "Module \"%s\" at %x\n",
618                                 tpnt->libname, tpnt->loadaddr);
619 #endif
620                 if (tpnt->loadaddr < (ElfW(Addr)) __address
621                                 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
622                         pelf = tpnt;
623                 }
624         }
625
626         if (!pelf) {
627                 return 0;
628         }
629
630         /*
631          * Try and locate the symbol of address
632          */
633
634         {
635                 char *strtab;
636                 Elf32_Sym *symtab;
637                 int hn, si;
638                 int sf;
639                 int sn = 0;
640                 ElfW(Addr) sa;
641
642                 sa = 0;
643                 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
644                 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
645
646                 sf = 0;
647                 for (hn = 0; hn < pelf->nbucket; hn++) {
648                         for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
649                                 ElfW(Addr) symbol_addr;
650
651                                 symbol_addr = pelf->loadaddr + symtab[si].st_value;
652                                 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
653                                         sa = symbol_addr;
654                                         sn = si;
655                                         sf = 1;
656                                 }
657 #if 0
658                                 fprintf(stderr, "Symbol \"%s\" at %x\n",
659                                                 strtab + symtab[si].st_name, symbol_addr);
660 #endif
661                         }
662                 }
663
664                 if (sf) {
665                         __info->dli_fname = pelf->libname;
666                         __info->dli_fbase = (void *)pelf->loadaddr;
667                         __info->dli_sname = strtab + symtab[sn].st_name;
668                         __info->dli_saddr = (void *)sa;
669                 }
670                 return 1;
671         }
672 }