libdl.c 20 KB

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