libdl.c 22 KB

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