libdl.c 21 KB

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