OSDN Git Service

update libdl so it also groks the new _dl_errno
[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@codpoet.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 #include <ldso.h>
34
35
36 #if defined (__LIBDL_SHARED__)
37
38 /* When libdl is loaded as a shared library, we need to load in
39  * and use a pile of symbols from ldso... */
40
41 extern void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__));
42 extern char *_dl_find_hash(const char *, struct dyn_elf *, int)
43         __attribute__ ((__weak__));
44 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
45         struct elf_resolve *, char *, int) __attribute__ ((__weak__));
46 extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
47         __attribute__ ((__weak__));
48 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
49          __attribute__ ((__weak__));
50 extern int _dl_errno __attribute__ ((__weak__));
51 extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
52 extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
53 extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
54 extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
55 extern unsigned long _dl_error_number __attribute__ ((__weak__));
56 extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
57 #ifdef USE_CACHE
58 int _dl_map_cache(void) __attribute__ ((__weak__));
59 int _dl_unmap_cache(void) __attribute__ ((__weak__));
60 #endif
61 #ifdef __mips__
62 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt)
63         __attribute__ ((__weak__));
64 #endif
65 #ifdef __SUPPORT_LD_DEBUG__
66 extern char *_dl_debug __attribute__ ((__weak__));
67 extern char *_dl_debug_symbols __attribute__ ((__weak__));
68 extern char *_dl_debug_move __attribute__ ((__weak__));
69 extern char *_dl_debug_reloc __attribute__ ((__weak__));
70 extern char *_dl_debug_detail __attribute__ ((__weak__));
71 extern char *_dl_debug_nofixups __attribute__ ((__weak__));
72 extern char *_dl_debug_bindings __attribute__ ((__weak__));
73 extern int   _dl_debug_file __attribute__ ((__weak__));
74 #endif
75
76
77 #else /* __LIBDL_SHARED__ */
78
79 /* When libdl is linked as a static library, we need to replace all
80  * the symbols that otherwise would have been loaded in from ldso... */
81
82 #ifdef __SUPPORT_LD_DEBUG__
83 char *_dl_debug  = 0;
84 char *_dl_debug_symbols = 0;
85 char *_dl_debug_move    = 0;
86 char *_dl_debug_reloc   = 0;
87 char *_dl_debug_detail  = 0;
88 char *_dl_debug_nofixups  = 0;
89 char *_dl_debug_bindings  = 0;
90 int   _dl_debug_file = 2;
91 #endif
92 char *_dl_library_path = 0;
93 char *_dl_ldsopath = 0;
94 struct r_debug *_dl_debug_addr = NULL;
95 static unsigned char *_dl_malloc_addr, *_dl_mmap_zero;
96 void *(*_dl_malloc_function) (size_t size);
97 int _dl_errno = 0;
98 int _dl_fixup(struct dyn_elf *rpnt, int lazy);
99 #include "../ldso/dl-progname.h"               /* Pull in the name of ld.so */
100 #include "../ldso/dl-hash.c"
101 #define _dl_trace_loaded_objects    0
102 #include "../ldso/dl-elf.c"
103 #endif
104
105 static int do_dlclose(void *, int need_fini);
106
107
108 static const char *dl_error_names[] = {
109         "",
110         "File not found",
111         "Unable to open /dev/zero",
112         "Not an ELF file",
113 #if defined (__i386__)
114         "Not i386 binary",
115 #elif defined (__sparc__)
116         "Not sparc binary",
117 #elif defined (__mc68000__)
118         "Not m68k binary",
119 #else
120         "Unrecognized binary type",
121 #endif
122         "Not an ELF shared library",
123         "Unable to mmap file",
124         "No dynamic section",
125 #ifdef ELF_USES_RELOCA
126         "Unable to process REL relocs",
127 #else
128         "Unable to process RELA relocs",
129 #endif
130         "Bad handle",
131         "Unable to resolve symbol"
132 };
133
134 static void __attribute__ ((destructor)) dl_cleanup(void)
135 {
136         struct dyn_elf *d;
137
138         for (d = _dl_handles; d; d = d->next_handle)
139                 if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
140                         (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
141                         d->dyn->dynamic_info[DT_FINI] = 0;
142                 }
143 }
144
145 void *_dlopen(const char *libname, int flag)
146 {
147         struct elf_resolve *tpnt, *tfrom, *tcurr;
148         struct dyn_elf *dyn_chain, *rpnt = NULL;
149         struct dyn_elf *dpnt;
150         static int dl_init = 0;
151         ElfW(Addr) from;
152         struct elf_resolve *tpnt1;
153         void (*dl_brk) (void);
154
155         /* A bit of sanity checking... */
156         if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
157                 _dl_error_number = LD_BAD_HANDLE;
158                 return NULL;
159         }
160
161         from = (ElfW(Addr)) __builtin_return_address(0);
162
163         /* Have the dynamic linker use the regular malloc function now */
164         if (!dl_init) {
165                 dl_init++;
166                 _dl_malloc_function = malloc;
167         }
168
169         /* Cover the trivial case first */
170         if (!libname)
171                 return _dl_symbol_tables;
172
173         _dl_map_cache();
174
175         /*
176          * Try and locate the module we were called from - we
177          * need this so that we get the correct RPATH.  Note that
178          * this is the current behavior under Solaris, but the
179          * ABI+ specifies that we should only use the RPATH from
180          * the application.  Thus this may go away at some time
181          * in the future.
182          */
183         tfrom = NULL;
184         for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
185                 tpnt = dpnt->dyn;
186                 if (tpnt->loadaddr < from
187                                 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
188                         tfrom = tpnt;
189         }
190
191         /* Try to load the specified library */
192 #ifdef __SUPPORT_LD_DEBUG__
193         if(_dl_debug)
194         _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname);
195 #endif
196         if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
197         tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
198         if (tpnt == NULL) {
199                 _dl_unmap_cache();
200                 return NULL;
201         }
202
203         dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
204         _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
205         dyn_chain->dyn = tpnt;
206         dyn_chain->flags = flag;
207         if (!tpnt->symbol_scope)
208                 tpnt->symbol_scope = dyn_chain;
209
210         dyn_chain->next_handle = _dl_handles;
211         _dl_handles = rpnt = dyn_chain;
212
213         if (tpnt->init_flag & INIT_FUNCS_CALLED) {
214             /* If the init and fini stuff has already been run, that means
215              * the dlopen'd library has already been loaded, and nothing
216              * further needs to be done. */
217             return (void *) dyn_chain;
218         }
219
220
221 #ifdef __SUPPORT_LD_DEBUG__
222         if(_dl_debug)
223         _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n");
224 #endif
225
226         for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
227         {
228                 Elf32_Dyn *dpnt;
229                 char *lpntstr;
230                 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
231                         if (dpnt->d_tag == DT_NEEDED) {
232
233                                 char *name;
234                                 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
235                                         dpnt->d_un.d_val);
236                                 name = _dl_get_last_path_component(lpntstr);
237
238                                 if ((tpnt1 = _dl_check_if_named_library_is_loaded(name, 0)))
239                                         continue;
240
241 #ifdef __SUPPORT_LD_DEBUG__
242                                 if(_dl_debug)
243                                 _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n",
244                                                 lpntstr, tcurr->libname);
245 #endif
246
247                                 if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0))) {
248                                         goto oops;
249                                 }
250
251                                 rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
252                                 _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
253                                 rpnt = rpnt->next;
254                                 if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
255                                 rpnt->dyn = tpnt1;
256
257                         }
258                 }
259         }
260
261         /*
262          * OK, now attach the entire chain at the end
263          */
264         rpnt->next = _dl_symbol_tables;
265
266 #ifdef __mips__
267         /*
268          * Relocation of the GOT entries for MIPS have to be done
269          * after all the libraries have been loaded.
270          */
271         _dl_perform_mips_global_got_relocations(tpnt);
272 #endif
273
274 #ifdef __SUPPORT_LD_DEBUG__
275         if(_dl_debug)
276         _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
277 #endif
278         /*
279          * OK, now all of the kids are tucked into bed in their proper addresses.
280          * Now we go through and look for REL and RELA records that indicate fixups
281          * to the GOT tables.  We need to do this in reverse order so that COPY
282          * directives work correctly */
283         if (_dl_fixup(dyn_chain, dyn_chain->flags))
284                 goto oops;
285
286         /* TODO:  Should we set the protections of all pages back to R/O now ? */
287
288
289         /* Notify the debugger we have added some objects. */
290         if (_dl_debug_addr) {
291                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
292                 if (dl_brk != NULL) {
293                         _dl_debug_addr->r_state = RT_ADD;
294                         (*dl_brk) ();
295
296                         _dl_debug_addr->r_state = RT_CONSISTENT;
297                         (*dl_brk) ();
298                 }
299         }
300
301 #if 0 //def __SUPPORT_LD_DEBUG__
302         if(_dl_debug)
303         _dlinfo();
304 #endif
305
306 #if defined (__LIBDL_SHARED__)
307         /* Find the last library so we can run things in the right order */
308         for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
309             ;
310
311         /* Run the ctors and set up the dtors */
312         for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
313         {
314                 /* Apparently crt1 for the application is responsible for handling this.
315                  * We only need to run the init/fini for shared libraries
316                  */
317                 if (tpnt->libtype == program_interpreter)
318                         continue;
319                 if (tpnt->libtype == elf_executable)
320                         continue;
321                 if (tpnt->init_flag & INIT_FUNCS_CALLED)
322                         continue;
323                 tpnt->init_flag |= INIT_FUNCS_CALLED;
324
325                 if (tpnt->dynamic_info[DT_INIT]) {
326                     void (*dl_elf_func) (void);
327                     dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
328                     if (dl_elf_func && *dl_elf_func != NULL) {
329 #ifdef __SUPPORT_LD_DEBUG__
330                         if(_dl_debug)
331                         _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
332 #endif
333                         (*dl_elf_func) ();
334                     }
335                 }
336                 if (tpnt->dynamic_info[DT_FINI]) {
337                     void (*dl_elf_func) (void);
338                     dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
339                     if (dl_elf_func && *dl_elf_func != NULL) {
340 #ifdef __SUPPORT_LD_DEBUG__
341                         if(_dl_debug)
342                         _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
343 #endif
344                         atexit(dl_elf_func);
345                     }
346                 }
347         }
348 #endif
349         return (void *) dyn_chain;
350
351 oops:
352         /* Something went wrong.  Clean up and return NULL. */
353         _dl_unmap_cache();
354         do_dlclose(dyn_chain, 0);
355         return NULL;
356 }
357 weak_alias(_dlopen, dlopen);
358
359 void *_dlsym(void *vhandle, const char *name)
360 {
361         struct elf_resolve *tpnt, *tfrom;
362         struct dyn_elf *handle;
363         ElfW(Addr) from;
364         struct dyn_elf *rpnt;
365         void *ret;
366
367         handle = (struct dyn_elf *) vhandle;
368
369         /* First of all verify that we have a real handle
370            of some kind.  Return NULL if not a valid handle. */
371
372         if (handle == NULL)
373                 handle = _dl_symbol_tables;
374         else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
375                 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
376                         if (rpnt == handle)
377                                 break;
378                 if (!rpnt) {
379                         _dl_error_number = LD_BAD_HANDLE;
380                         return NULL;
381                 }
382         } else if (handle == RTLD_NEXT) {
383                 /*
384                  * Try and locate the module we were called from - we
385                  * need this so that we know where to start searching
386                  * from.  We never pass RTLD_NEXT down into the actual
387                  * dynamic loader itself, as it doesn't know
388                  * how to properly treat it.
389                  */
390                 from = (ElfW(Addr)) __builtin_return_address(0);
391
392                 tfrom = NULL;
393                 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
394                         tpnt = rpnt->dyn;
395                         if (tpnt->loadaddr < from
396                                 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
397                                 tfrom = tpnt;
398                                 handle = rpnt->next;
399                         }
400                 }
401         }
402
403         ret = _dl_find_hash((char*)name, handle, 0);
404
405         /*
406          * Nothing found.
407          */
408         if (!ret)
409                 _dl_error_number = LD_NO_SYMBOL;
410         return ret;
411 }
412 weak_alias(_dlsym, dlsym);
413
414 static int do_dlclose(void *vhandle, int need_fini)
415 {
416         struct dyn_elf *rpnt, *rpnt1;
417         struct dyn_elf *spnt, *spnt1;
418         ElfW(Phdr) *ppnt;
419         struct elf_resolve *tpnt;
420         int (*dl_elf_fini) (void);
421         void (*dl_brk) (void);
422         struct dyn_elf *handle;
423         unsigned int end;
424         int i = 0;
425
426         handle = (struct dyn_elf *) vhandle;
427         rpnt1 = NULL;
428         for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
429                 if (rpnt == handle) {
430                         break;
431                 }
432                 rpnt1 = rpnt;
433         }
434
435         if (!rpnt) {
436                 _dl_error_number = LD_BAD_HANDLE;
437                 return 1;
438         }
439
440         /* OK, this is a valid handle - now close out the file.
441          * We check if we need to call fini () on the handle. */
442         spnt = need_fini ? handle : handle->next;
443         for (; spnt; spnt = spnt1) {
444                 spnt1 = spnt->next;
445
446                 /* We appended the module list to the end - when we get back here,
447                    quit. The access counts were not adjusted to account for being here. */
448                 if (spnt == _dl_symbol_tables)
449                         break;
450                 if (spnt->dyn->usage_count == 1
451                         && spnt->dyn->libtype == loaded_file) {
452                         tpnt = spnt->dyn;
453                         /* Apparently crt1 for the application is responsible for handling this.
454                          * We only need to run the init/fini for shared libraries
455                          */
456
457                         if (tpnt->dynamic_info[DT_FINI]) {
458                                 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
459                                         tpnt->dynamic_info[DT_FINI]);
460                                 (*dl_elf_fini) ();
461                         }
462                 }
463         }
464         if (rpnt1)
465                 rpnt1->next_handle = rpnt->next_handle;
466         else
467                 _dl_handles = rpnt->next_handle;
468
469         /* OK, this is a valid handle - now close out the file */
470         for (rpnt = handle; rpnt; rpnt = rpnt1) {
471                 rpnt1 = rpnt->next;
472
473                 /* We appended the module list to the end - when we get back here,
474                    quit. The access counts were not adjusted to account for being here. */
475                 if (rpnt == _dl_symbol_tables)
476                         break;
477
478                 rpnt->dyn->usage_count--;
479                 if (rpnt->dyn->usage_count == 0
480                         && rpnt->dyn->libtype == loaded_file) {
481                         tpnt = rpnt->dyn;
482                         /* Apparently crt1 for the application is responsible for handling this.
483                          * We only need to run the init/fini for shared libraries
484                          */
485 #if 0
486
487                         /* We have to do this above, before we start closing objects.
488                          * Otherwise when the needed symbols for _fini handling are
489                          * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
490                         if (tpnt->dynamic_info[DT_FINI]) {
491                             dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
492                                 (*dl_elf_fini) ();
493                         }
494 #endif
495                         end = 0;
496                         for (i = 0, ppnt = rpnt->dyn->ppnt;
497                                  i < rpnt->dyn->n_phent; ppnt++, i++) {
498                                 if (ppnt->p_type != PT_LOAD)
499                                         continue;
500                                 if (end < ppnt->p_vaddr + ppnt->p_memsz)
501                                         end = ppnt->p_vaddr + ppnt->p_memsz;
502                         }
503                         _dl_munmap((void*)rpnt->dyn->loadaddr, end);
504                         /* Next, remove rpnt->dyn from the loaded_module list */
505                         if (_dl_loaded_modules == rpnt->dyn) {
506                                 _dl_loaded_modules = rpnt->dyn->next;
507                                 if (_dl_loaded_modules)
508                                         _dl_loaded_modules->prev = 0;
509                         } else
510                                 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
511                                         if (tpnt->next == rpnt->dyn) {
512                                                 tpnt->next = tpnt->next->next;
513                                                 if (tpnt->next)
514                                                         tpnt->next->prev = tpnt;
515                                                 break;
516                                         }
517                         free(rpnt->dyn->libname);
518                         free(rpnt->dyn);
519                 }
520                 free(rpnt);
521         }
522
523
524         if (_dl_debug_addr) {
525             dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
526             if (dl_brk != NULL) {
527                 _dl_debug_addr->r_state = RT_DELETE;
528                 (*dl_brk) ();
529
530                 _dl_debug_addr->r_state = RT_CONSISTENT;
531                 (*dl_brk) ();
532             }
533         }
534
535         return 0;
536 }
537
538 int _dlclose(void *vhandle)
539 {
540         return do_dlclose(vhandle, 1);
541 }
542 weak_alias(_dlclose, dlclose);
543
544 const char *_dlerror(void)
545 {
546         const char *retval;
547
548         if (!_dl_error_number)
549                 return NULL;
550         retval = dl_error_names[_dl_error_number];
551         _dl_error_number = 0;
552         return retval;
553 }
554 weak_alias(_dlerror, dlerror);
555
556 /*
557  * Dump information to stderrr about the current loaded modules
558  */
559 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
560
561 void _dlinfo(void)
562 {
563         struct elf_resolve *tpnt;
564         struct dyn_elf *rpnt, *hpnt;
565
566         _dl_dprintf(2, "List of loaded modules\n");
567         /* First start with a complete list of all of the loaded files. */
568         for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
569                 _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
570                         (unsigned) tpnt->loadaddr, (unsigned) tpnt,
571                         (unsigned) tpnt->symbol_scope,
572                         type[tpnt->libtype],
573                         tpnt->usage_count, tpnt->libname);
574         }
575
576         /* Next dump the module list for the application itself */
577         _dl_dprintf(2, "\nModules for application (%x):\n",
578                                  (unsigned) _dl_symbol_tables);
579         for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
580                 _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
581
582         for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
583                 _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
584                 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
585                         _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
586                                 rpnt->dyn->libname);
587         }
588 }
589 weak_alias(_dlinfo, dlinfo);
590
591 int _dladdr(void *__address, Dl_info * __dlip)
592 {
593         struct elf_resolve *pelf;
594         struct elf_resolve *rpnt;
595
596         _dl_map_cache();
597
598         /*
599          * Try and locate the module address is in
600          */
601         pelf = NULL;
602
603 #if 0
604         _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip);
605 #endif
606
607         for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
608                 struct elf_resolve *tpnt;
609
610                 tpnt = rpnt;
611 #if 0
612                 _dl_dprintf(2, "Module \"%s\" at %x\n",
613                         tpnt->libname, tpnt->loadaddr);
614 #endif
615                 if (tpnt->loadaddr < (ElfW(Addr)) __address
616                         && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
617                     pelf = tpnt;
618                 }
619         }
620
621         if (!pelf) {
622                 return 0;
623         }
624
625         /*
626          * Try and locate the symbol of address
627          */
628
629         {
630                 char *strtab;
631                 Elf32_Sym *symtab;
632                 int hn, si;
633                 int sf;
634                 int sn = 0;
635                 ElfW(Addr) sa;
636
637                 sa = 0;
638                 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
639                 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
640
641                 sf = 0;
642                 for (hn = 0; hn < pelf->nbucket; hn++) {
643                         for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
644                                 ElfW(Addr) symbol_addr;
645
646                                 symbol_addr = pelf->loadaddr + symtab[si].st_value;
647                                 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
648                                         sa = symbol_addr;
649                                         sn = si;
650                                         sf = 1;
651                                 }
652 #if 0
653                                 _dl_dprintf(2, "Symbol \"%s\" at %x\n",
654                                         strtab + symtab[si].st_name, symbol_addr);
655 #endif
656                         }
657                 }
658
659                 if (sf) {
660                         __dlip->dli_fname = pelf->libname;
661                         __dlip->dli_fbase = (void *)pelf->loadaddr;
662                         __dlip->dli_sname = strtab + symtab[sn].st_name;
663                         __dlip->dli_saddr = (void *)sa;
664                 }
665                 return 1;
666         }
667 }
668 weak_alias(_dladdr, dladdr);