OSDN Git Service

322230860435f4c938b859ab319aa085d2cb1682
[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-2006 by Erik Andersen <andersen@uclibc.org>
7  * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8  *                              David Engel, Hongjiu Lu and Mitch D'Souza
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the above contributors may not be
16  *    used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32
33 #include <ldso.h>
34 #include <stdio.h>
35
36
37 #ifdef SHARED
38
39 /* When libdl is loaded as a shared library, we need to load in
40  * and use a pile of symbols from ldso... */
41
42 extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, int);
43 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
44         struct elf_resolve *, char *, int);
45 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy);
46 extern void _dl_protect_relro(struct elf_resolve * tpnt);
47 extern int _dl_errno;
48 extern struct dyn_elf *_dl_symbol_tables;
49 extern struct dyn_elf *_dl_handles;
50 extern struct elf_resolve *_dl_loaded_modules;
51 extern struct r_debug *_dl_debug_addr;
52 extern unsigned long _dl_error_number;
53 extern void *(*_dl_malloc_function)(size_t);
54 extern void _dl_run_init_array(struct elf_resolve *);
55 extern void _dl_run_fini_array(struct elf_resolve *);
56 #ifdef __LDSO_CACHE_SUPPORT__
57 int _dl_map_cache(void);
58 int _dl_unmap_cache(void);
59 #endif
60 #ifdef __mips__
61 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
62 #endif
63 #ifdef __SUPPORT_LD_DEBUG__
64 extern char *_dl_debug;
65 #endif
66
67
68 #else /* SHARED */
69
70 /* When libdl is linked as a static library, we need to replace all
71  * the symbols that otherwise would have been loaded in from ldso... */
72
73 #ifdef __SUPPORT_LD_DEBUG__
74 char *_dl_debug  = 0;
75 #endif
76 const char *_dl_progname       = "";        /* Program name */
77 char *_dl_library_path         = 0;         /* Where we look for libraries */
78 char *_dl_ldsopath             = 0;         /* Location of the shared lib loader */
79 int _dl_errno                  = 0;         /* We can't use the real errno in ldso */
80 size_t _dl_pagesize            = PAGE_SIZE; /* Store the page size for use later */
81 /* This global variable is also to communicate with debuggers such as gdb. */
82 struct r_debug *_dl_debug_addr = NULL;
83 #define _dl_malloc malloc
84 #include "../ldso/dl-debug.c"
85 #include LDSO_ELFINTERP
86 #include "../ldso/dl-hash.c"
87 #define _dl_trace_loaded_objects    0
88 #include "../ldso/dl-elf.c"
89 #endif /* SHARED */
90
91 #ifdef __SUPPORT_LD_DEBUG__
92 # define _dl_if_debug_print(fmt, args...) \
93         do { \
94         if (_dl_debug) \
95                 fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
96         } while (0)
97 #else
98 # define _dl_if_debug_print(fmt, args...)
99 #endif
100
101 static int do_dlclose(void *, int need_fini);
102
103
104 static const char *dl_error_names[] = {
105         "",
106         "File not found",
107         "Unable to open /dev/zero",
108         "Not an ELF file",
109 #if defined (__i386__)
110         "Not i386 binary",
111 #elif defined (__sparc__)
112         "Not sparc binary",
113 #elif defined (__mc68000__)
114         "Not m68k binary",
115 #else
116         "Unrecognized binary type",
117 #endif
118         "Not an ELF shared library",
119         "Unable to mmap file",
120         "No dynamic section",
121 #ifdef ELF_USES_RELOCA
122         "Unable to process REL relocs",
123 #else
124         "Unable to process RELA relocs",
125 #endif
126         "Bad handle",
127         "Unable to resolve symbol"
128 };
129
130 void dl_cleanup(void) __attribute__ ((destructor));
131 void dl_cleanup(void)
132 {
133         struct dyn_elf *d;
134         for (d = _dl_handles; d; d = d->next_handle) {
135                 do_dlclose(d, 1);
136         }
137 }
138
139 void *dlopen(const char *libname, int flag)
140 {
141         struct elf_resolve *tpnt, *tfrom;
142         struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
143         ElfW(Addr) from;
144         struct elf_resolve *tpnt1;
145         void (*dl_brk) (void);
146         int now_flag;
147         struct init_fini_list *tmp, *runp, *runp2, *dep_list;
148         unsigned int nlist, i;
149         struct elf_resolve **init_fini_list;
150
151         /* A bit of sanity checking... */
152         if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
153                 _dl_error_number = LD_BAD_HANDLE;
154                 return NULL;
155         }
156
157         from = (ElfW(Addr)) __builtin_return_address(0);
158
159         /* Cover the trivial case first */
160         if (!libname)
161                 return _dl_symbol_tables;
162
163         _dl_map_cache();
164
165         /*
166          * Try and locate the module we were called from - we
167          * need this so that we get the correct RPATH/RUNPATH.  Note that
168          * this is the current behavior under Solaris, but the
169          * ABI+ specifies that we should only use the RPATH from
170          * the application.  Thus this may go away at some time
171          * in the future.
172          */
173         {
174                 struct dyn_elf *dpnt;
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         }
183         for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
184
185         relro_ptr = rpnt;
186         now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
187         if (getenv("LD_BIND_NOW"))
188                 now_flag = RTLD_NOW;
189
190         /* Try to load the specified library */
191         _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
192                         (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
193         tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
194
195         if (tpnt == NULL) {
196                 _dl_unmap_cache();
197                 return NULL;
198         }
199         dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
200         _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
201         dyn_chain->dyn = tpnt;
202         tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
203
204         dyn_chain->next_handle = _dl_handles;
205         _dl_handles = dyn_ptr = dyn_chain;
206
207         if (tpnt->usage_count > 1) {
208                 _dl_if_debug_print("Lib: %s already opened\n", libname);
209                 /* see if there is a handle from a earlier dlopen */
210                 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
211                         if (handle->dyn == tpnt) {
212                                 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
213                                 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
214                                 for(i=0; i < dyn_chain->init_fini.nlist; i++)
215                                         dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
216                                 dyn_chain->next = handle->next;
217                                 break;
218                         }
219                 }
220                 return dyn_chain;
221         } else {
222                 tpnt->init_flag |= DL_OPENED;
223         }
224
225         _dl_if_debug_print("Looking for needed libraries\n");
226         nlist = 0;
227         runp = alloca(sizeof(*runp));
228         runp->tpnt = tpnt;
229         runp->next = NULL;
230         dep_list = runp2 = runp;
231         for (; runp; runp = runp->next)
232         {
233                 ElfW(Dyn) *dpnt;
234                 char *lpntstr;
235
236                 nlist++;
237                 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
238                 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
239                         if (dpnt->d_tag == DT_NEEDED) {
240                                 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
241                                                 dpnt->d_un.d_val);
242                                 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
243                                                 lpntstr, runp->tpnt->libname);
244                                 tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
245                                 if (!tpnt1)
246                                         goto oops;
247
248                                 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
249
250                                 if (tpnt1->usage_count == 1) {
251                                         tpnt1->init_flag |= DL_OPENED;
252                                         /* This list is for dlsym() and relocation */
253                                         dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
254                                         _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
255                                         dyn_ptr = dyn_ptr->next;
256                                         dyn_ptr->dyn = tpnt1;
257                                 }
258                                 if (tpnt1->init_flag & DL_OPENED) {
259                                         /* Used to record RTLD_LOCAL scope */
260                                         tmp = alloca(sizeof(struct init_fini_list));
261                                         tmp->tpnt = tpnt1;
262                                         tmp->next = runp->tpnt->init_fini;
263                                         runp->tpnt->init_fini = tmp;
264
265                                         for (tmp=dep_list; tmp; tmp = tmp->next) {
266                                                 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
267                                                         _dl_if_debug_print("Circular dependency, skipping '%s',\n",
268                                                                         tmp->tpnt->libname);
269                                                         tpnt1->usage_count--;
270                                                         break;
271                                                 }
272                                         }
273                                         if (!tmp) { /* Don't add if circular dependency detected */
274                                                 runp2->next = alloca(sizeof(*runp));
275                                                 runp2 = runp2->next;
276                                                 runp2->tpnt = tpnt1;
277                                                 runp2->next = NULL;
278                                         }
279                                 }
280                         }
281                 }
282         }
283         init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
284         dyn_chain->init_fini.init_fini = init_fini_list;
285         dyn_chain->init_fini.nlist = nlist;
286         i = 0;
287         for (runp2 = dep_list; runp2; runp2 = runp2->next) {
288                 init_fini_list[i++] = runp2->tpnt;
289                 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
290                         if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
291                                 tmp = malloc(sizeof(struct init_fini_list));
292                                 tmp->tpnt = runp->tpnt;
293                                 tmp->next = runp2->tpnt->rtld_local;
294                                 runp2->tpnt->rtld_local = tmp;
295                         }
296                 }
297
298         }
299         /* Sort the INIT/FINI list in dependency order. */
300         for (runp2 = dep_list; runp2; runp2 = runp2->next) {
301                 unsigned int j, k;
302                 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
303                         /* Empty */;
304                 for (k = j + 1; k < nlist; ++k) {
305                         struct init_fini_list *ele = init_fini_list[k]->init_fini;
306
307                         for (; ele; ele = ele->next) {
308                                 if (ele->tpnt == runp2->tpnt) {
309                                         struct elf_resolve *here = init_fini_list[k];
310                                         _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
311                                         for (i = (k - j); i; --i)
312                                                 init_fini_list[i+j] = init_fini_list[i+j-1];
313                                         init_fini_list[j] = here;
314                                         ++j;
315                                         break;
316                                 }
317                         }
318                 }
319         }
320 #ifdef __SUPPORT_LD_DEBUG__
321         if(_dl_debug) {
322                 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
323                 for (i=0;i < nlist;i++) {
324                         fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
325                         runp = init_fini_list[i]->init_fini;
326                         for (; runp; runp = runp->next)
327                                 printf(" %s ", runp->tpnt->libname);
328                         printf("\n");
329                 }
330         }
331 #endif
332
333         _dl_if_debug_print("Beginning dlopen relocation fixups\n");
334         /*
335          * OK, now all of the kids are tucked into bed in their proper addresses.
336          * Now we go through and look for REL and RELA records that indicate fixups
337          * to the GOT tables.  We need to do this in reverse order so that COPY
338          * directives work correctly */
339 #ifdef __mips__
340         /*
341          * Relocation of the GOT entries for MIPS have to be done
342          * after all the libraries have been loaded.
343          */
344         _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
345 #endif
346
347         if (_dl_fixup(dyn_chain, now_flag))
348                 goto oops;
349
350         if (relro_ptr) {
351                 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
352                         if (rpnt->dyn->relro_size)
353                                 _dl_protect_relro(rpnt->dyn);
354                 }
355         }
356         /* TODO:  Should we set the protections of all pages back to R/O now ? */
357
358
359         /* Notify the debugger we have added some objects. */
360         if (_dl_debug_addr) {
361                 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
362                 if (dl_brk != NULL) {
363                         _dl_debug_addr->r_state = RT_ADD;
364                         (*dl_brk) ();
365
366                         _dl_debug_addr->r_state = RT_CONSISTENT;
367                         (*dl_brk) ();
368                 }
369         }
370
371 #ifdef SHARED
372         /* Run the ctors and setup the dtors */
373         for (i = nlist; i; --i) {
374                 tpnt = init_fini_list[i-1];
375                 if (tpnt->init_flag & INIT_FUNCS_CALLED)
376                         continue;
377                 tpnt->init_flag |= INIT_FUNCS_CALLED;
378
379                 if (tpnt->dynamic_info[DT_INIT]) {
380                         void (*dl_elf_func) (void);
381                         dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
382                         if (dl_elf_func && *dl_elf_func != NULL) {
383                                 _dl_if_debug_print("running ctors for library %s at '%p'\n",
384                                                 tpnt->libname, dl_elf_func);
385                                 (*dl_elf_func) ();
386                         }
387                 }
388
389                 _dl_run_init_array(tpnt);
390         }
391 #endif /* SHARED */
392
393         _dl_unmap_cache();
394         return (void *) dyn_chain;
395
396 oops:
397         /* Something went wrong.  Clean up and return NULL. */
398         _dl_unmap_cache();
399         do_dlclose(dyn_chain, 0);
400         return NULL;
401 }
402
403 void *dlsym(void *vhandle, const char *name)
404 {
405         struct elf_resolve *tpnt, *tfrom;
406         struct dyn_elf *handle;
407         ElfW(Addr) from;
408         struct dyn_elf *rpnt;
409         void *ret;
410
411         handle = (struct dyn_elf *) vhandle;
412
413         /* First of all verify that we have a real handle
414            of some kind.  Return NULL if not a valid handle. */
415
416         if (handle == NULL)
417                 handle = _dl_symbol_tables;
418         else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
419                 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
420                         if (rpnt == handle)
421                                 break;
422                 if (!rpnt) {
423                         _dl_error_number = LD_BAD_HANDLE;
424                         return NULL;
425                 }
426         } else if (handle == RTLD_NEXT) {
427                 /*
428                  * Try and locate the module we were called from - we
429                  * need this so that we know where to start searching
430                  * from.  We never pass RTLD_NEXT down into the actual
431                  * dynamic loader itself, as it doesn't know
432                  * how to properly treat it.
433                  */
434                 from = (ElfW(Addr)) __builtin_return_address(0);
435
436                 tfrom = NULL;
437                 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
438                         tpnt = rpnt->dyn;
439                         if (tpnt->loadaddr < from
440                                         && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
441                                 tfrom = tpnt;
442                                 handle = rpnt->next;
443                         }
444                 }
445         }
446
447         ret = _dl_find_hash((char*)name, handle, NULL, 0);
448
449         /*
450          * Nothing found.
451          */
452         if (!ret)
453                 _dl_error_number = LD_NO_SYMBOL;
454         return ret;
455 }
456
457 #if 0
458 void *dlvsym(void *vhandle, const char *name, const char *version)
459 {
460         return dlsym(vhandle, name);
461 }
462 #endif
463
464 static int do_dlclose(void *vhandle, int need_fini)
465 {
466         struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
467         struct init_fini_list *runp, *tmp;
468         ElfW(Phdr) *ppnt;
469         struct elf_resolve *tpnt, *run_tpnt;
470         int (*dl_elf_fini) (void);
471         void (*dl_brk) (void);
472         struct dyn_elf *handle;
473         unsigned int end;
474         unsigned int i, j;
475
476         handle = (struct dyn_elf *) vhandle;
477         if (handle == _dl_symbol_tables)
478                 return 0;
479         rpnt1 = NULL;
480         for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
481                 if (rpnt == handle)
482                         break;
483                 rpnt1 = rpnt;
484         }
485
486         if (!rpnt) {
487                 _dl_error_number = LD_BAD_HANDLE;
488                 return 1;
489         }
490         if (rpnt1)
491                 rpnt1->next_handle = rpnt->next_handle;
492         else
493                 _dl_handles = rpnt->next_handle;
494         _dl_if_debug_print("%s: usage count: %d\n",
495                         handle->dyn->libname, handle->dyn->usage_count);
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]
506                              || tpnt->dynamic_info[DT_FINI_ARRAY])
507                             && need_fini &&
508                             !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
509                                 tpnt->init_flag |= FINI_FUNCS_CALLED;
510 #ifdef SHARED
511                                 _dl_run_fini_array(tpnt);
512 #endif
513
514                                 if (tpnt->dynamic_info[DT_FINI]) {
515                                         dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
516                                         _dl_if_debug_print("running dtors for library %s at '%p'\n",
517                                                         tpnt->libname, dl_elf_fini);
518                                         (*dl_elf_fini) ();
519                                 }
520                         }
521
522                         _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
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                                                 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
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) {
555                                 if (_dl_symbol_tables->dyn == tpnt) {
556                                         _dl_symbol_tables = _dl_symbol_tables->next;
557                                         if (_dl_symbol_tables)
558                                                 _dl_symbol_tables->prev = 0;
559                                 } else
560                                         for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
561                                                 if (rpnt1->next->dyn == tpnt) {
562                                                         _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
563                                                         rpnt1_tmp = rpnt1->next->next;
564                                                         free(rpnt1->next);
565                                                         rpnt1->next = rpnt1_tmp;
566                                                         if (rpnt1->next)
567                                                                 rpnt1->next->prev = rpnt1;
568                                                         break;
569                                                 }
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 stderr about the current loaded modules
612  */
613 #ifdef __USE_GNU
614 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
615
616 int dlinfo(void)
617 {
618         struct elf_resolve *tpnt;
619         struct dyn_elf *rpnt, *hpnt;
620
621         fprintf(stderr, "List of loaded modules\n");
622         /* First start with a complete list of all of the loaded files. */
623         for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
624                 fprintf(stderr, "\t%p %p %p %s %d %s\n",
625                         tpnt->loadaddr, tpnt, 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 (%p):\n", _dl_symbol_tables);
632         for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
633                 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
634
635         for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
636                 fprintf(stderr, "Modules for handle %p\n", hpnt);
637                 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
638                         fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
639         }
640         return 0;
641 }
642
643 int dladdr(const void *__address, Dl_info * __info)
644 {
645         struct elf_resolve *pelf;
646         struct elf_resolve *rpnt;
647
648         _dl_map_cache();
649
650         /*
651          * Try and locate the module address is in
652          */
653         pelf = NULL;
654
655 #if 0
656         fprintf(stderr, "dladdr( %p, %p )\n", __address, __info);
657 #endif
658
659         for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
660                 struct elf_resolve *tpnt;
661
662                 tpnt = rpnt;
663 #if 0
664                 fprintf(stderr, "Module \"%s\" at %p\n",
665                                 tpnt->libname, tpnt->loadaddr);
666 #endif
667                 if (tpnt->loadaddr < (ElfW(Addr)) __address
668                                 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
669                         pelf = tpnt;
670                 }
671         }
672
673         if (!pelf) {
674                 return 0;
675         }
676
677         /*
678          * Try and locate the symbol of address
679          */
680
681         {
682                 char *strtab;
683                 ElfW(Sym) *symtab;
684                 unsigned int hn, si, sn, sf;
685                 ElfW(Addr) sa;
686
687                 sa = 0;
688                 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
689                 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
690
691                 sf = sn = 0;
692                 for (hn = 0; hn < pelf->nbucket; hn++) {
693                         for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
694                                 ElfW(Addr) symbol_addr;
695
696                                 symbol_addr = pelf->loadaddr + symtab[si].st_value;
697                                 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
698                                         sa = symbol_addr;
699                                         sn = si;
700                                         sf = 1;
701                                 }
702 #if 0
703                                 fprintf(stderr, "Symbol \"%s\" at %p\n",
704                                                 strtab + symtab[si].st_name, symbol_addr);
705 #endif
706                         }
707                 }
708
709                 if (sf) {
710                         __info->dli_fname = pelf->libname;
711                         __info->dli_fbase = (void *)pelf->loadaddr;
712                         __info->dli_sname = strtab + symtab[sn].st_name;
713                         __info->dli_saddr = (void *)sa;
714                 }
715                 return 1;
716         }
717 }
718 #endif