libdl.c 20 KB

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