libdl.c 22 KB

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