OSDN Git Service

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