libdl.c 20 KB

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