libdl.c 20 KB

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