libdl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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;
  119. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr;
  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;
  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. #ifdef __SUPPORT_LD_DEBUG__
  176. if(_dl_debug)
  177. fprintf(stderr, "Looking for needed libraries\n");
  178. #endif
  179. nlist = 0;
  180. for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
  181. {
  182. Elf32_Dyn *dpnt;
  183. char *lpntstr;
  184. nlist++;
  185. tcurr->init_fini = NULL; /* clear any previous dependcies */
  186. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
  187. if (dpnt->d_tag == DT_NEEDED) {
  188. char *name;
  189. lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  190. dpnt->d_un.d_val);
  191. name = _dl_get_last_path_component(lpntstr);
  192. tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
  193. #ifdef __SUPPORT_LD_DEBUG__
  194. if(_dl_debug)
  195. fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
  196. lpntstr, tcurr->libname);
  197. #endif
  198. if (tpnt1) {
  199. tpnt1->usage_count++;
  200. } else {
  201. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
  202. if (!tpnt1)
  203. goto oops;
  204. }
  205. tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
  206. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  207. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  208. dyn_ptr = dyn_ptr->next;
  209. dyn_ptr->dyn = tpnt1;
  210. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  211. tmp->tpnt = tpnt1;
  212. tmp->next = tcurr->init_fini;
  213. tcurr->init_fini = tmp;
  214. }
  215. }
  216. }
  217. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  218. dyn_chain->init_fini.init_fini = init_fini_list;
  219. dyn_chain->init_fini.nlist = nlist;
  220. i = 0;
  221. for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
  222. init_fini_list[i++] = tcurr;
  223. for(runp = tcurr->init_fini; runp; runp = runp->next){
  224. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  225. tmp = malloc(sizeof(struct init_fini_list));
  226. tmp->tpnt = runp->tpnt;
  227. tmp->next = tcurr->rtld_local;
  228. tcurr->rtld_local = tmp;
  229. }
  230. }
  231. }
  232. /* Sort the INIT/FINI list in dependency order. */
  233. for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
  234. int j, k;
  235. for (j = 0; init_fini_list[j] != tcurr; ++j)
  236. /* Empty */;
  237. for (k = j + 1; k < nlist; ++k) {
  238. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  239. for (; runp; runp = runp->next) {
  240. if (runp->tpnt == tcurr) {
  241. struct elf_resolve *here = init_fini_list[k];
  242. #ifdef __SUPPORT_LD_DEBUG__
  243. if(_dl_debug)
  244. fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  245. #endif
  246. for (i = (k - j); i; --i)
  247. init_fini_list[i+j] = init_fini_list[i+j-1];
  248. init_fini_list[j] = here;
  249. ++j;
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. #ifdef __SUPPORT_LD_DEBUG__
  256. if(_dl_debug) {
  257. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  258. for (i=0;i < nlist;i++) {
  259. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  260. runp = init_fini_list[i]->init_fini;
  261. for ( ;runp; runp = runp->next)
  262. printf(" %s ", runp->tpnt->libname);
  263. printf("\n");
  264. }
  265. }
  266. #endif
  267. if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
  268. /* If the init and fini stuff has already been run, that means
  269. * the dlopen'd library has already been loaded, and nothing
  270. * further needs to be done. */
  271. return (void *) dyn_chain;
  272. }
  273. #ifdef __SUPPORT_LD_DEBUG__
  274. if(_dl_debug)
  275. fprintf(stderr, "Beginning dlopen relocation fixups\n");
  276. #endif
  277. /*
  278. * OK, now all of the kids are tucked into bed in their proper addresses.
  279. * Now we go through and look for REL and RELA records that indicate fixups
  280. * to the GOT tables. We need to do this in reverse order so that COPY
  281. * directives work correctly */
  282. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  283. if (getenv("LD_BIND_NOW"))
  284. now_flag = RTLD_NOW;
  285. #ifdef __mips__
  286. /*
  287. * Relocation of the GOT entries for MIPS have to be done
  288. * after all the libraries have been loaded.
  289. */
  290. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  291. #endif
  292. if (_dl_fixup(dyn_chain, now_flag))
  293. goto oops;
  294. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  295. if (rpnt->dyn->relro_size)
  296. _dl_protect_relro(rpnt->dyn);
  297. }
  298. /* TODO: Should we set the protections of all pages back to R/O now ? */
  299. /* Notify the debugger we have added some objects. */
  300. if (_dl_debug_addr) {
  301. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  302. if (dl_brk != NULL) {
  303. _dl_debug_addr->r_state = RT_ADD;
  304. (*dl_brk) ();
  305. _dl_debug_addr->r_state = RT_CONSISTENT;
  306. (*dl_brk) ();
  307. }
  308. }
  309. #if defined (__LIBDL_SHARED__)
  310. /* Run the ctors and setup the dtors */
  311. for (i = nlist; i; --i) {
  312. tpnt = init_fini_list[i-1];
  313. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  314. continue;
  315. tpnt->init_flag |= INIT_FUNCS_CALLED;
  316. if (tpnt->dynamic_info[DT_INIT]) {
  317. void (*dl_elf_func) (void);
  318. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  319. if (dl_elf_func && *dl_elf_func != NULL) {
  320. #ifdef __SUPPORT_LD_DEBUG__
  321. if(_dl_debug)
  322. fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
  323. #endif
  324. (*dl_elf_func) ();
  325. }
  326. }
  327. }
  328. #endif
  329. _dl_unmap_cache();
  330. return (void *) dyn_chain;
  331. oops:
  332. /* Something went wrong. Clean up and return NULL. */
  333. _dl_unmap_cache();
  334. do_dlclose(dyn_chain, 0);
  335. return NULL;
  336. }
  337. void *dlsym(void *vhandle, const char *name)
  338. {
  339. struct elf_resolve *tpnt, *tfrom;
  340. struct dyn_elf *handle;
  341. ElfW(Addr) from;
  342. struct dyn_elf *rpnt;
  343. void *ret;
  344. handle = (struct dyn_elf *) vhandle;
  345. /* First of all verify that we have a real handle
  346. of some kind. Return NULL if not a valid handle. */
  347. if (handle == NULL)
  348. handle = _dl_symbol_tables;
  349. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  350. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  351. if (rpnt == handle)
  352. break;
  353. if (!rpnt) {
  354. _dl_error_number = LD_BAD_HANDLE;
  355. return NULL;
  356. }
  357. } else if (handle == RTLD_NEXT) {
  358. /*
  359. * Try and locate the module we were called from - we
  360. * need this so that we know where to start searching
  361. * from. We never pass RTLD_NEXT down into the actual
  362. * dynamic loader itself, as it doesn't know
  363. * how to properly treat it.
  364. */
  365. from = (ElfW(Addr)) __builtin_return_address(0);
  366. tfrom = NULL;
  367. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  368. tpnt = rpnt->dyn;
  369. if (tpnt->loadaddr < from
  370. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  371. tfrom = tpnt;
  372. handle = rpnt->next;
  373. }
  374. }
  375. }
  376. ret = _dl_find_hash((char*)name, handle, NULL, 0);
  377. /*
  378. * Nothing found.
  379. */
  380. if (!ret)
  381. _dl_error_number = LD_NO_SYMBOL;
  382. return ret;
  383. }
  384. static int do_dlclose(void *vhandle, int need_fini)
  385. {
  386. struct dyn_elf *rpnt, *rpnt1;
  387. struct init_fini_list *runp, *tmp;
  388. ElfW(Phdr) *ppnt;
  389. struct elf_resolve *tpnt;
  390. int (*dl_elf_fini) (void);
  391. void (*dl_brk) (void);
  392. struct dyn_elf *handle;
  393. unsigned int end;
  394. int i = 0;
  395. handle = (struct dyn_elf *) vhandle;
  396. rpnt1 = NULL;
  397. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  398. if (rpnt == handle)
  399. break;
  400. rpnt1 = rpnt;
  401. }
  402. if (!rpnt) {
  403. _dl_error_number = LD_BAD_HANDLE;
  404. return 1;
  405. }
  406. if (rpnt1)
  407. rpnt1->next_handle = rpnt->next_handle;
  408. else
  409. _dl_handles = rpnt->next_handle;
  410. if (need_fini) {
  411. for (i = 0; i < handle->init_fini.nlist; ++i) {
  412. tpnt = handle->init_fini.init_fini[i];
  413. if (tpnt->dynamic_info[DT_FINI] && tpnt->usage_count == 1 &&
  414. !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
  415. tpnt->init_flag |= FINI_FUNCS_CALLED;
  416. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  417. #ifdef __SUPPORT_LD_DEBUG__
  418. if(_dl_debug)
  419. fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
  420. #endif
  421. (*dl_elf_fini) ();
  422. }
  423. }
  424. }
  425. if (handle->dyn->usage_count == 1)
  426. free(handle->init_fini.init_fini);
  427. /* OK, this is a valid handle - now close out the file */
  428. for (rpnt = handle; rpnt; rpnt = rpnt->next) {
  429. tpnt = rpnt->dyn;
  430. if (--tpnt->usage_count == 0) {
  431. end = 0;
  432. for (i = 0, ppnt = tpnt->ppnt;
  433. i < tpnt->n_phent; ppnt++, i++) {
  434. if (ppnt->p_type != PT_LOAD)
  435. continue;
  436. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  437. end = ppnt->p_vaddr + ppnt->p_memsz;
  438. }
  439. _dl_munmap((void*)tpnt->loadaddr, end);
  440. /* Free elements in RTLD_LOCAL scope list */
  441. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  442. tmp = runp->next;
  443. free(runp);
  444. }
  445. /* Next, remove tpnt from the loaded_module list */
  446. if (_dl_loaded_modules == tpnt) {
  447. _dl_loaded_modules = tpnt->next;
  448. if (_dl_loaded_modules)
  449. _dl_loaded_modules->prev = 0;
  450. } else
  451. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  452. if (tpnt->next == rpnt->dyn) {
  453. tpnt->next = tpnt->next->next;
  454. if (tpnt->next)
  455. tpnt->next->prev = tpnt;
  456. break;
  457. }
  458. /* Next, remove tpnt from the global symbol table list */
  459. if (_dl_symbol_tables->dyn == rpnt->dyn) {
  460. _dl_symbol_tables = rpnt->next;
  461. if (_dl_symbol_tables)
  462. _dl_symbol_tables->prev = 0;
  463. } else
  464. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  465. if (rpnt1->next->dyn == rpnt->dyn) {
  466. free(rpnt1->next);
  467. rpnt1->next = rpnt1->next->next;
  468. if (rpnt1->next)
  469. rpnt1->next->prev = rpnt1;
  470. break;
  471. }
  472. }
  473. free(rpnt->dyn->libname);
  474. free(rpnt->dyn);
  475. }
  476. free(rpnt);
  477. }
  478. if (_dl_debug_addr) {
  479. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  480. if (dl_brk != NULL) {
  481. _dl_debug_addr->r_state = RT_DELETE;
  482. (*dl_brk) ();
  483. _dl_debug_addr->r_state = RT_CONSISTENT;
  484. (*dl_brk) ();
  485. }
  486. }
  487. return 0;
  488. }
  489. int dlclose(void *vhandle)
  490. {
  491. return do_dlclose(vhandle, 1);
  492. }
  493. char *dlerror(void)
  494. {
  495. const char *retval;
  496. if (!_dl_error_number)
  497. return NULL;
  498. retval = dl_error_names[_dl_error_number];
  499. _dl_error_number = 0;
  500. return (char *)retval;
  501. }
  502. /*
  503. * Dump information to stderrr about the current loaded modules
  504. */
  505. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  506. int dlinfo(void)
  507. {
  508. struct elf_resolve *tpnt;
  509. struct dyn_elf *rpnt, *hpnt;
  510. fprintf(stderr, "List of loaded modules\n");
  511. /* First start with a complete list of all of the loaded files. */
  512. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  513. fprintf(stderr, "\t%x %x %x %s %d %s\n",
  514. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  515. (unsigned) tpnt->symbol_scope,
  516. type[tpnt->libtype],
  517. tpnt->usage_count, tpnt->libname);
  518. }
  519. /* Next dump the module list for the application itself */
  520. fprintf(stderr, "\nModules for application (%x):\n",
  521. (unsigned) _dl_symbol_tables);
  522. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  523. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  524. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  525. fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
  526. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  527. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
  528. rpnt->dyn->libname);
  529. }
  530. return 0;
  531. }
  532. int dladdr(const void *__address, Dl_info * __info)
  533. {
  534. struct elf_resolve *pelf;
  535. struct elf_resolve *rpnt;
  536. _dl_map_cache();
  537. /*
  538. * Try and locate the module address is in
  539. */
  540. pelf = NULL;
  541. #if 0
  542. fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
  543. #endif
  544. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  545. struct elf_resolve *tpnt;
  546. tpnt = rpnt;
  547. #if 0
  548. fprintf(stderr, "Module \"%s\" at %x\n",
  549. tpnt->libname, tpnt->loadaddr);
  550. #endif
  551. if (tpnt->loadaddr < (ElfW(Addr)) __address
  552. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  553. pelf = tpnt;
  554. }
  555. }
  556. if (!pelf) {
  557. return 0;
  558. }
  559. /*
  560. * Try and locate the symbol of address
  561. */
  562. {
  563. char *strtab;
  564. Elf32_Sym *symtab;
  565. int hn, si;
  566. int sf;
  567. int sn = 0;
  568. ElfW(Addr) sa;
  569. sa = 0;
  570. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  571. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  572. sf = 0;
  573. for (hn = 0; hn < pelf->nbucket; hn++) {
  574. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  575. ElfW(Addr) symbol_addr;
  576. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  577. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  578. sa = symbol_addr;
  579. sn = si;
  580. sf = 1;
  581. }
  582. #if 0
  583. fprintf(stderr, "Symbol \"%s\" at %x\n",
  584. strtab + symtab[si].st_name, symbol_addr);
  585. #endif
  586. }
  587. }
  588. if (sf) {
  589. __info->dli_fname = pelf->libname;
  590. __info->dli_fbase = (void *)pelf->loadaddr;
  591. __info->dli_sname = strtab + symtab[sn].st_name;
  592. __info->dli_saddr = (void *)sa;
  593. }
  594. return 1;
  595. }
  596. }