OSDN Git Service

Fix bug 214, reported by aurel. This is a major update that
[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=NULL;
134         struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
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, *runp2, *dep_list;
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         dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
191         _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
192         dyn_chain->dyn = tpnt;
193         tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
194
195         dyn_chain->next_handle = _dl_handles;
196         _dl_handles = dyn_ptr = dyn_chain;
197
198         if (tpnt->usage_count > 1) {
199 #ifdef __SUPPORT_LD_DEBUG__
200                 if(_dl_debug)
201                         fprintf(stderr, "Lib: % already opened\n", libname);
202 #endif
203                 /* see if there is a handle from a earlier dlopen */
204                 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
205                         if (handle->dyn == tpnt) {
206                                 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
207                                 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
208                                 for(i=0; i < dyn_chain->init_fini.nlist; i++)
209                                         dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
210                                 dyn_chain->next = handle->next;
211                                 break;
212                         }
213                 }
214                 return dyn_chain;
215         } else {
216                 tpnt->init_flag |= DL_OPENED;
217         }
218
219 #ifdef __SUPPORT_LD_DEBUG__
220         if(_dl_debug)
221                 fprintf(stderr, "Looking for needed libraries\n");
222 #endif
223         nlist = 0;
224         runp = alloca(sizeof(*runp));
225         runp->tpnt = tpnt;
226         runp->next = NULL;
227         dep_list = runp2 = runp;
228         for (; runp; runp = runp->next)
229         {
230                 Elf32_Dyn *dpnt;
231                 char *lpntstr;
232
233                 nlist++;
234                 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
235                 for (dpnt = (Elf32_Dyn *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
236                         if (dpnt->d_tag == DT_NEEDED) {
237                                 char *name;
238
239                                 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
240                                                 dpnt->d_un.d_val);
241                                 name = _dl_get_last_path_component(lpntstr);
242                                 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
243 #ifdef __SUPPORT_LD_DEBUG__
244                                 if(_dl_debug)
245                                         fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
246                                                         lpntstr, runp->tpnt->libname);
247 #endif
248                                 if (tpnt1) {
249                                         tpnt1->usage_count++;
250                                 } else {
251                                         tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
252                                         if (!tpnt1)
253                                                 goto oops;
254                                         tpnt1->init_flag |= DL_OPENED;
255
256                                 }
257                                 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
258
259                                 if (tpnt1->usage_count == 1) {
260                                         /* This list is for dlsym() and relocation */
261                                         dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
262                                         _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
263                                         dyn_ptr = dyn_ptr->next;
264                                         dyn_ptr->dyn = tpnt1;
265                                 }
266                                 if (tpnt1->init_flag & DL_OPENED) {
267                                         /* Used to record RTLD_LOCAL scope */
268                                         tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
269                                         tmp->tpnt = tpnt1;
270                                         tmp->next = runp->tpnt->init_fini;
271                                         runp->tpnt->init_fini = tmp;
272
273                                         runp2->next = alloca(sizeof(*runp)); /* Allocates on stack, no need to free this memory */
274                                         runp2 = runp2->next;
275                                         runp2->tpnt = tpnt1;
276                                         runp2->next = NULL;
277                                 }
278                         }
279                 }
280         }
281         init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
282         dyn_chain->init_fini.init_fini = init_fini_list;
283         dyn_chain->init_fini.nlist = nlist;
284         i = 0;
285         for (runp2 = dep_list; runp2; runp2 = runp2->next) {
286                 init_fini_list[i++] = runp2->tpnt;
287                 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
288                         if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
289                                 tmp = malloc(sizeof(struct init_fini_list));
290                                 tmp->tpnt = runp->tpnt;
291                                 tmp->next = runp2->tpnt->rtld_local;
292                                 runp2->tpnt->rtld_local = tmp;
293                         }
294                 }
295
296         }
297         /* Sort the INIT/FINI list in dependency order. */
298         for (runp2 = dep_list; runp2; runp2 = runp2->next) {
299                 int j, k;
300                 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
301                         /* Empty */;
302                 for (k = j + 1; k < nlist; ++k) {
303                         struct init_fini_list *runp = init_fini_list[k]->init_fini;
304
305                         for (; runp; runp = runp->next) {
306                                 if (runp->tpnt == runp2->tpnt) {
307                                         struct elf_resolve *here = init_fini_list[k];
308 #ifdef __SUPPORT_LD_DEBUG__
309                                         if(_dl_debug)
310                                                 fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
311 #endif
312                                         for (i = (k - j); i; --i)
313                                                 init_fini_list[i+j] = init_fini_list[i+j-1];
314                                         init_fini_list[j] = here;
315                                         ++j;
316                                         break;
317                                 }
318                         }
319                 }
320         }
321 #ifdef __SUPPORT_LD_DEBUG__
322         if(_dl_debug) {
323                 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
324                 for (i=0;i < nlist;i++) {
325                         fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
326                         runp = init_fini_list[i]->init_fini;
327                         for ( ;runp; runp = runp->next)
328                                 printf(" %s ", runp->tpnt->libname);
329                         printf("\n");
330                 }
331         }
332 #endif
333
334 #ifdef __SUPPORT_LD_DEBUG__
335         if(_dl_debug)
336                 fprintf(stderr, "Beginning dlopen relocation fixups\n");
337 #endif
338         /*
339          * OK, now all of the kids are tucked into bed in their proper addresses.
340          * Now we go through and look for REL and RELA records that indicate fixups
341          * to the GOT tables.  We need to do this in reverse order so that COPY
342          * directives work correctly */
343         now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
344         if (getenv("LD_BIND_NOW"))
345                 now_flag = RTLD_NOW;
346
347 #ifdef __mips__
348         /*
349          * Relocation of the GOT entries for MIPS have to be done
350          * after all the libraries have been loaded.
351          */
352         _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
353 #endif
354
355         if (_dl_fixup(dyn_chain, now_flag))
356                 goto oops;
357
358         for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
359                 if (rpnt->dyn->relro_size)
360                         _dl_protect_relro(rpnt->dyn);
361         }
362         /* TODO:  Should we set the protections of all pages back to R/O now ? */
363
364
365         /* Notify the debugger we have added some objects. */
366         if (_dl_debug_addr) {
367                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
368                 if (dl_brk != NULL) {
369                         _dl_debug_addr->r_state = RT_ADD;
370                         (*dl_brk) ();
371
372                         _dl_debug_addr->r_state = RT_CONSISTENT;
373                         (*dl_brk) ();
374                 }
375         }
376
377 #if defined (__LIBDL_SHARED__)
378         /* Run the ctors and setup the dtors */
379         for (i = nlist; i; --i) {
380                 tpnt = init_fini_list[i-1];
381                 if (tpnt->init_flag & INIT_FUNCS_CALLED)
382                         continue;
383                 tpnt->init_flag |= INIT_FUNCS_CALLED;
384
385                 if (tpnt->dynamic_info[DT_INIT]) {
386                         void (*dl_elf_func) (void);
387                         dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
388                         if (dl_elf_func && *dl_elf_func != NULL) {
389 #ifdef __SUPPORT_LD_DEBUG__
390                                 if(_dl_debug)
391                                         fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
392 #endif
393                                 (*dl_elf_func) ();
394                         }
395                 }
396         }
397 #endif
398         _dl_unmap_cache();
399         return (void *) dyn_chain;
400
401 oops:
402         /* Something went wrong.  Clean up and return NULL. */
403         _dl_unmap_cache();
404         do_dlclose(dyn_chain, 0);
405         return NULL;
406 }
407
408 void *dlsym(void *vhandle, const char *name)
409 {
410         struct elf_resolve *tpnt, *tfrom;
411         struct dyn_elf *handle;
412         ElfW(Addr) from;
413         struct dyn_elf *rpnt;
414         void *ret;
415
416         handle = (struct dyn_elf *) vhandle;
417
418         /* First of all verify that we have a real handle
419            of some kind.  Return NULL if not a valid handle. */
420
421         if (handle == NULL)
422                 handle = _dl_symbol_tables;
423         else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
424                 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
425                         if (rpnt == handle)
426                                 break;
427                 if (!rpnt) {
428                         _dl_error_number = LD_BAD_HANDLE;
429                         return NULL;
430                 }
431         } else if (handle == RTLD_NEXT) {
432                 /*
433                  * Try and locate the module we were called from - we
434                  * need this so that we know where to start searching
435                  * from.  We never pass RTLD_NEXT down into the actual
436                  * dynamic loader itself, as it doesn't know
437                  * how to properly treat it.
438                  */
439                 from = (ElfW(Addr)) __builtin_return_address(0);
440
441                 tfrom = NULL;
442                 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
443                         tpnt = rpnt->dyn;
444                         if (tpnt->loadaddr < from
445                                         && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
446                                 tfrom = tpnt;
447                                 handle = rpnt->next;
448                         }
449                 }
450         }
451
452         ret = _dl_find_hash((char*)name, handle, NULL, 0);
453
454         /*
455          * Nothing found.
456          */
457         if (!ret)
458                 _dl_error_number = LD_NO_SYMBOL;
459         return ret;
460 }
461
462 static int do_dlclose(void *vhandle, int need_fini)
463 {
464         struct dyn_elf *rpnt, *rpnt1;
465         struct init_fini_list *runp, *tmp;
466         ElfW(Phdr) *ppnt;
467         struct elf_resolve *tpnt, *run_tpnt;
468         int (*dl_elf_fini) (void);
469         void (*dl_brk) (void);
470         struct dyn_elf *handle;
471         unsigned int end;
472         int i = 0, j;
473
474         handle = (struct dyn_elf *) vhandle;
475         if (handle == _dl_symbol_tables)
476                 return 0;
477         rpnt1 = NULL;
478         for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
479                 if (rpnt == handle)
480                         break;
481                 rpnt1 = rpnt;
482         }
483
484         if (!rpnt) {
485                 _dl_error_number = LD_BAD_HANDLE;
486                 return 1;
487         }
488         if (rpnt1)
489                 rpnt1->next_handle = rpnt->next_handle;
490         else
491                 _dl_handles = rpnt->next_handle;
492 #ifdef __SUPPORT_LD_DEBUG__
493         if(_dl_debug)
494                 fprintf(stderr, "dlclose: %s, usage count: %d\n", handle->dyn->libname, handle->dyn->usage_count);
495 #endif
496         if (handle->dyn->usage_count != 1) {
497                 handle->dyn->usage_count--;
498                 free(handle);
499                 return 0;
500         }
501         /* OK, this is a valid handle - now close out the file */
502         for (j = 0; j < handle->init_fini.nlist; ++j) {
503                 tpnt = handle->init_fini.init_fini[j];
504                 if (--tpnt->usage_count == 0) {
505                         if (tpnt->dynamic_info[DT_FINI] && need_fini &&
506                             !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
507                                 tpnt->init_flag |= FINI_FUNCS_CALLED;
508                                 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
509 #ifdef __SUPPORT_LD_DEBUG__
510                                 if(_dl_debug)
511                                         fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
512 #endif
513                                 (*dl_elf_fini) ();
514                         }
515
516 #ifdef __SUPPORT_LD_DEBUG__
517                         if(_dl_debug)
518                                 fprintf(stderr, "dlclose unmapping: %s\n", tpnt->libname);
519 #endif
520                         end = 0;
521                         for (i = 0, ppnt = tpnt->ppnt;
522                                         i < tpnt->n_phent; ppnt++, i++) {
523                                 if (ppnt->p_type != PT_LOAD)
524                                         continue;
525                                 if (end < ppnt->p_vaddr + ppnt->p_memsz)
526                                         end = ppnt->p_vaddr + ppnt->p_memsz;
527                         }
528                         _dl_munmap((void*)tpnt->loadaddr, end);
529                         /* Free elements in RTLD_LOCAL scope list */ 
530                         for (runp = tpnt->rtld_local; runp; runp = tmp) {
531                                 tmp = runp->next;
532                                 free(runp);
533                         }
534
535                         /* Next, remove tpnt from the loaded_module list */
536                         if (_dl_loaded_modules == tpnt) {
537                                 _dl_loaded_modules = tpnt->next;
538                                 if (_dl_loaded_modules)
539                                         _dl_loaded_modules->prev = 0;
540                         } else
541                                 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
542                                         if (run_tpnt->next == tpnt) {
543 #ifdef __SUPPORT_LD_DEBUG__
544                                                 if(_dl_debug)
545                                                         fprintf(stderr, "dlclose removing loaded_modules: %s\n", tpnt->libname);
546 #endif
547                                                 run_tpnt->next = run_tpnt->next->next;
548                                                 if (run_tpnt->next)
549                                                         run_tpnt->next->prev = run_tpnt;
550                                                 break;
551                                         }
552
553                         /* Next, remove tpnt from the global symbol table list */
554                         if (_dl_symbol_tables->dyn == tpnt) {
555                                 _dl_symbol_tables = _dl_symbol_tables->next;
556                                 if (_dl_symbol_tables)
557                                         _dl_symbol_tables->prev = 0;
558                         } else
559                                 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
560                                         if (rpnt1->next->dyn == tpnt) {
561 #ifdef __SUPPORT_LD_DEBUG__
562                                                 if(_dl_debug)
563                                                         fprintf(stderr, "dlclose removing symbol_tables: %s\n", tpnt->libname);
564 #endif
565                                                 free(rpnt1->next);
566                                                 rpnt1->next = rpnt1->next->next;
567                                                 if (rpnt1->next)
568                                                         rpnt1->next->prev = rpnt1;
569                                                 break;
570                                         }
571                                 }
572                         free(tpnt->libname);
573                         free(tpnt);
574                 }
575         }
576         free(handle->init_fini.init_fini);
577         free(handle);
578
579
580         if (_dl_debug_addr) {
581                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
582                 if (dl_brk != NULL) {
583                         _dl_debug_addr->r_state = RT_DELETE;
584                         (*dl_brk) ();
585
586                         _dl_debug_addr->r_state = RT_CONSISTENT;
587                         (*dl_brk) ();
588                 }
589         }
590
591         return 0;
592 }
593
594 int dlclose(void *vhandle)
595 {
596         return do_dlclose(vhandle, 1);
597 }
598
599 char *dlerror(void)
600 {
601         const char *retval;
602
603         if (!_dl_error_number)
604                 return NULL;
605         retval = dl_error_names[_dl_error_number];
606         _dl_error_number = 0;
607         return (char *)retval;
608 }
609
610 /*
611  * Dump information to stderrr about the current loaded modules
612  */
613 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
614
615 int dlinfo(void)
616 {
617         struct elf_resolve *tpnt;
618         struct dyn_elf *rpnt, *hpnt;
619
620         fprintf(stderr, "List of loaded modules\n");
621         /* First start with a complete list of all of the loaded files. */
622         for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
623                 fprintf(stderr, "\t%x %x %x %s %d %s\n",
624                                 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
625                                 (unsigned) tpnt->symbol_scope,
626                                 type[tpnt->libtype],
627                                 tpnt->usage_count, tpnt->libname);
628         }
629
630         /* Next dump the module list for the application itself */
631         fprintf(stderr, "\nModules for application (%x):\n",
632                         (unsigned) _dl_symbol_tables);
633         for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
634                 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
635
636         for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
637                 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
638                 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
639                         fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
640                                         rpnt->dyn->libname);
641         }
642         return 0;
643 }
644
645 int dladdr(const void *__address, Dl_info * __info)
646 {
647         struct elf_resolve *pelf;
648         struct elf_resolve *rpnt;
649
650         _dl_map_cache();
651
652         /*
653          * Try and locate the module address is in
654          */
655         pelf = NULL;
656
657 #if 0
658         fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
659 #endif
660
661         for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
662                 struct elf_resolve *tpnt;
663
664                 tpnt = rpnt;
665 #if 0
666                 fprintf(stderr, "Module \"%s\" at %x\n",
667                                 tpnt->libname, tpnt->loadaddr);
668 #endif
669                 if (tpnt->loadaddr < (ElfW(Addr)) __address
670                                 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
671                         pelf = tpnt;
672                 }
673         }
674
675         if (!pelf) {
676                 return 0;
677         }
678
679         /*
680          * Try and locate the symbol of address
681          */
682
683         {
684                 char *strtab;
685                 Elf32_Sym *symtab;
686                 int hn, si;
687                 int sf;
688                 int sn = 0;
689                 ElfW(Addr) sa;
690
691                 sa = 0;
692                 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
693                 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
694
695                 sf = 0;
696                 for (hn = 0; hn < pelf->nbucket; hn++) {
697                         for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
698                                 ElfW(Addr) symbol_addr;
699
700                                 symbol_addr = pelf->loadaddr + symtab[si].st_value;
701                                 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
702                                         sa = symbol_addr;
703                                         sn = si;
704                                         sf = 1;
705                                 }
706 #if 0
707                                 fprintf(stderr, "Symbol \"%s\" at %x\n",
708                                                 strtab + symtab[si].st_name, symbol_addr);
709 #endif
710                         }
711                 }
712
713                 if (sf) {
714                         __info->dli_fname = pelf->libname;
715                         __info->dli_fbase = (void *)pelf->loadaddr;
716                         __info->dli_sname = strtab + symtab[sn].st_name;
717                         __info->dli_saddr = (void *)sa;
718                 }
719                 return 1;
720         }
721 }