OSDN Git Service

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