OSDN Git Service

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