libdl.c 20 KB

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