OSDN Git Service

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