libdl.c 19 KB

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