OSDN Git Service

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