libdl.c 21 KB

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