OSDN Git Service

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