libdl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Program to load an ELF binary on a linux system, and run it
  3. * after resolving ELF shared library symbols
  4. *
  5. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  6. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  7. * David Engel, Hongjiu Lu and Mitch D'Souza
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. The name of the above contributors may not be
  15. * used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* When libdl is linked in statically into libc.a, we need to replace
  31. * these symbols that otherwise would have been loaded in from ldso.
  32. * This must be before including ldso.h */
  33. #ifndef SHARED
  34. #define _dl_malloc malloc
  35. #define _dl_free free
  36. #endif
  37. #include <ldso.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdbool.h>
  41. #include <bits/uClibc_mutex.h>
  42. #ifdef __UCLIBC_HAS_TLS__
  43. #include <tls.h>
  44. #endif
  45. #if defined(USE_TLS) && USE_TLS
  46. #include <ldsodefs.h>
  47. #include <dl-tls.h>
  48. extern void _dl_add_to_slotinfo(struct link_map *l);
  49. #endif
  50. /* TODO: get rid of global lock and use more finegrained locking, or
  51. * perhaps RCU for the global structures */
  52. __UCLIBC_MUTEX_STATIC(_dl_mutex, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  53. #ifdef SHARED
  54. # if defined(USE_TLS) && USE_TLS
  55. extern struct link_map *_dl_update_slotinfo(unsigned long int req_modid);
  56. # endif
  57. /* When libdl is loaded as a shared library, we need to load in
  58. * and use a pile of symbols from ldso... */
  59. #include <dl-elf.h>
  60. #if 0
  61. extern int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int lazy);
  62. extern void _dl_protect_relro(struct elf_resolve * tpnt);
  63. #endif
  64. extern int _dl_errno;
  65. extern struct dyn_elf *_dl_symbol_tables;
  66. extern struct dyn_elf *_dl_handles;
  67. extern struct elf_resolve *_dl_loaded_modules;
  68. extern void _dl_free (void *__ptr);
  69. extern struct r_debug *_dl_debug_addr;
  70. extern unsigned long _dl_error_number;
  71. extern void *(*_dl_malloc_function)(size_t);
  72. extern void (*_dl_free_function) (void *p);
  73. extern void _dl_run_init_array(struct elf_resolve *);
  74. extern void _dl_run_fini_array(struct elf_resolve *);
  75. #ifdef __LDSO_CACHE_SUPPORT__
  76. int _dl_map_cache(void);
  77. int _dl_unmap_cache(void);
  78. #endif
  79. #ifdef __mips__
  80. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  81. #endif
  82. #ifdef __SUPPORT_LD_DEBUG__
  83. extern char *_dl_debug;
  84. #endif
  85. #else /* !SHARED */
  86. /* When libdl is linked as a static library, we need to replace all
  87. * the symbols that otherwise would have been loaded in from ldso... */
  88. #ifdef __SUPPORT_LD_DEBUG__
  89. char *_dl_debug = NULL;
  90. char *_dl_debug_symbols = NULL;
  91. char *_dl_debug_move = NULL;
  92. char *_dl_debug_reloc = NULL;
  93. char *_dl_debug_detail = NULL;
  94. char *_dl_debug_nofixups = NULL;
  95. char *_dl_debug_bindings = NULL;
  96. int _dl_debug_file = 2;
  97. #endif
  98. const char *_dl_progname = ""; /* Program name */
  99. void *(*_dl_malloc_function)(size_t);
  100. void (*_dl_free_function) (void *p);
  101. #ifdef __LDSO_LD_LIBRARY_PATH__
  102. char *_dl_library_path = NULL; /* Where we look for libraries */
  103. #endif
  104. int _dl_errno = 0; /* We can't use the real errno in ldso */
  105. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  106. /* This global variable is also to communicate with debuggers such as gdb. */
  107. struct r_debug *_dl_debug_addr = NULL;
  108. #include "../ldso/dl-array.c"
  109. #include "../ldso/dl-debug.c"
  110. # if defined(USE_TLS) && USE_TLS
  111. /*
  112. * Giving this initialized value preallocates some surplus bytes in the
  113. * static TLS area, see __libc_setup_tls (libc-tls.c).
  114. */
  115. size_t _dl_tls_static_size = 2048;
  116. # endif
  117. #include LDSO_ELFINTERP
  118. #include "../ldso/dl-hash.c"
  119. #define _dl_trace_loaded_objects 0
  120. #include "../ldso/dl-elf.c"
  121. #endif /* SHARED */
  122. #ifdef __SUPPORT_LD_DEBUG__
  123. # define _dl_if_debug_print(fmt, args...) \
  124. do { \
  125. if (_dl_debug) \
  126. fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
  127. } while (0)
  128. #else
  129. # define _dl_if_debug_print(fmt, args...)
  130. #endif
  131. static int do_dlclose(void *, int need_fini);
  132. static const char *const dl_error_names[] = {
  133. "",
  134. "File not found",
  135. "Unable to open /dev/zero",
  136. "Not an ELF file",
  137. #if defined (__i386__)
  138. "Not i386 binary",
  139. #elif defined (__sparc__)
  140. "Not sparc binary",
  141. #elif defined (__mc68000__)
  142. "Not m68k binary",
  143. #else
  144. "Unrecognized binary type",
  145. #endif
  146. "Not an ELF shared library",
  147. "Unable to mmap file",
  148. "No dynamic section",
  149. "Library contains unsupported TLS",
  150. #ifdef ELF_USES_RELOCA
  151. "Unable to process REL relocs",
  152. #else
  153. "Unable to process RELA relocs",
  154. #endif
  155. "Bad handle",
  156. "Unable to resolve symbol"
  157. };
  158. #if defined(USE_TLS) && USE_TLS
  159. #ifdef SHARED
  160. /*
  161. * Systems which do not have tls_index also probably have to define
  162. * DONT_USE_TLS_INDEX.
  163. */
  164. # ifndef __TLS_GET_ADDR
  165. # define __TLS_GET_ADDR __tls_get_addr
  166. # endif
  167. /*
  168. * Return the symbol address given the map of the module it is in and
  169. * the symbol record. This is used in dl-sym.c.
  170. */
  171. static void *
  172. internal_function
  173. _dl_tls_symaddr(struct link_map *map, const Elf32_Addr st_value)
  174. {
  175. # ifndef DONT_USE_TLS_INDEX
  176. tls_index tmp =
  177. {
  178. .ti_module = map->l_tls_modid,
  179. .ti_offset = st_value
  180. };
  181. return __TLS_GET_ADDR (&tmp);
  182. # else
  183. return __TLS_GET_ADDR (map->l_tls_modid, st_value);
  184. # endif
  185. }
  186. #endif
  187. /* Returns true when a non-empty entry was found. */
  188. static bool
  189. remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
  190. bool should_be_there)
  191. {
  192. if (idx - disp >= listp->len) {
  193. if (listp->next == NULL) {
  194. /*
  195. * The index is not actually valid in the slotinfo list,
  196. * because this object was closed before it was fully set
  197. * up due to some error.
  198. */
  199. _dl_assert(!should_be_there);
  200. } else {
  201. if (remove_slotinfo(idx, listp->next, disp + listp->len,
  202. should_be_there))
  203. return true;
  204. /*
  205. * No non-empty entry. Search from the end of this element's
  206. * slotinfo array.
  207. */
  208. idx = disp + listp->len;
  209. }
  210. } else {
  211. struct link_map *old_map = listp->slotinfo[idx - disp].map;
  212. /*
  213. * The entry might still be in its unused state if we are
  214. * closing an object that wasn't fully set up.
  215. */
  216. if (__builtin_expect(old_map != NULL, 1)) {
  217. _dl_assert(old_map->l_tls_modid == idx);
  218. /* Mark the entry as unused. */
  219. listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
  220. listp->slotinfo[idx - disp].map = NULL;
  221. }
  222. /*
  223. * If this is not the last currently used entry no need to
  224. * look further.
  225. */
  226. if (idx != _dl_tls_max_dtv_idx)
  227. return true;
  228. }
  229. while (idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0)) {
  230. --idx;
  231. if (listp->slotinfo[idx - disp].map != NULL) {
  232. /* Found a new last used index. */
  233. _dl_tls_max_dtv_idx = idx;
  234. return true;
  235. }
  236. }
  237. /* No non-entry in this list element. */
  238. return false;
  239. }
  240. #endif
  241. #ifndef __LDSO_NO_CLEANUP__
  242. void dl_cleanup(void) attribute_hidden __attribute__ ((destructor));
  243. void dl_cleanup(void)
  244. {
  245. struct dyn_elf *h, *n;
  246. for (h = _dl_handles; h; h = n) {
  247. n = h->next_handle;
  248. do_dlclose(h, 1);
  249. }
  250. }
  251. #endif
  252. static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
  253. struct elf_resolve *map)
  254. {
  255. struct elf_resolve **p = list;
  256. struct init_fini_list *q;
  257. *p++ = map;
  258. map->init_flag |= DL_RESERVED;
  259. if (map->init_fini)
  260. for (q = map->init_fini; q; q = q->next)
  261. if (! (q->tpnt->init_flag & DL_RESERVED))
  262. p += _dl_build_local_scope (p, q->tpnt);
  263. return p - list;
  264. }
  265. static void *do_dlopen(const char *libname, int flag, ElfW(Addr) from)
  266. {
  267. struct elf_resolve *tpnt, *tfrom;
  268. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
  269. struct elf_resolve *tpnt1;
  270. void (*dl_brk) (void);
  271. int now_flag;
  272. struct init_fini_list *tmp, *runp, *runp2, *dep_list;
  273. unsigned int nlist, i;
  274. struct elf_resolve **init_fini_list;
  275. static bool _dl_init;
  276. struct elf_resolve **local_scope;
  277. #ifdef SHARED
  278. struct r_scope_elem *ls;
  279. #endif
  280. #if defined(USE_TLS) && USE_TLS
  281. bool any_tls = false;
  282. #endif
  283. /* A bit of sanity checking... */
  284. if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
  285. _dl_error_number = LD_BAD_HANDLE;
  286. return NULL;
  287. }
  288. if (!_dl_init) {
  289. _dl_init = true;
  290. _dl_malloc_function = malloc;
  291. _dl_free_function = free;
  292. }
  293. /* Cover the trivial case first */
  294. if (!libname)
  295. return _dl_symbol_tables;
  296. #ifndef SHARED
  297. # ifdef __SUPPORT_LD_DEBUG__
  298. _dl_debug = getenv("LD_DEBUG");
  299. if (_dl_debug) {
  300. if (strstr(_dl_debug, "all")) {
  301. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  302. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  303. } else {
  304. _dl_debug_detail = strstr(_dl_debug, "detail");
  305. _dl_debug_move = strstr(_dl_debug, "move");
  306. _dl_debug_symbols = strstr(_dl_debug, "sym");
  307. _dl_debug_reloc = strstr(_dl_debug, "reloc");
  308. _dl_debug_nofixups = strstr(_dl_debug, "nofix");
  309. _dl_debug_bindings = strstr(_dl_debug, "bind");
  310. }
  311. }
  312. # endif
  313. #endif
  314. _dl_map_cache();
  315. /*
  316. * Try and locate the module we were called from - we
  317. * need this so that we get the correct RPATH/RUNPATH. Note that
  318. * this is the current behavior under Solaris, but the
  319. * ABI+ specifies that we should only use the RPATH from
  320. * the application. Thus this may go away at some time
  321. * in the future.
  322. */
  323. {
  324. struct dyn_elf *dpnt;
  325. tfrom = NULL;
  326. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  327. tpnt = dpnt->dyn;
  328. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
  329. tfrom = tpnt;
  330. }
  331. }
  332. for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
  333. continue;
  334. relro_ptr = rpnt;
  335. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  336. if (getenv("LD_BIND_NOW"))
  337. now_flag = RTLD_NOW;
  338. #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
  339. /* When statically linked, the _dl_library_path is not yet initialized */
  340. _dl_library_path = getenv("LD_LIBRARY_PATH");
  341. #endif
  342. /* Try to load the specified library */
  343. _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
  344. (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
  345. tpnt = _dl_load_shared_library(flag & (RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE),
  346. &rpnt, tfrom, (char*)libname, 0);
  347. if (tpnt == NULL) {
  348. _dl_unmap_cache();
  349. return NULL;
  350. }
  351. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  352. memset(dyn_chain, 0, sizeof(struct dyn_elf));
  353. dyn_chain->dyn = tpnt;
  354. dyn_chain->next_handle = _dl_handles;
  355. _dl_handles = dyn_ptr = dyn_chain;
  356. if (tpnt->init_flag & DL_OPENED2) {
  357. _dl_if_debug_print("Lib: %s already opened\n", libname);
  358. /* see if there is a handle from a earlier dlopen */
  359. for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
  360. if (handle->dyn == tpnt) {
  361. dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
  362. dyn_chain->init_fini.nlist = handle->init_fini.nlist;
  363. for (i = 0; i < dyn_chain->init_fini.nlist; i++)
  364. dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & (RTLD_GLOBAL|RTLD_NODELETE));
  365. dyn_chain->next = handle->next;
  366. break;
  367. }
  368. }
  369. return dyn_chain;
  370. }
  371. _dl_if_debug_print("Looking for needed libraries\n");
  372. nlist = 0;
  373. runp = alloca(sizeof(*runp));
  374. runp->tpnt = tpnt;
  375. runp->next = NULL;
  376. dep_list = runp2 = runp;
  377. for (; runp; runp = runp->next) {
  378. ElfW(Dyn) *dpnt;
  379. char *lpntstr;
  380. nlist++;
  381. runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
  382. for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
  383. if (dpnt->d_tag == DT_NEEDED) {
  384. lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
  385. dpnt->d_un.d_val);
  386. _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
  387. lpntstr, runp->tpnt->libname);
  388. tpnt1 = _dl_load_shared_library(flag & (RTLD_GLOBAL | RTLD_NODELETE),
  389. &rpnt, runp->tpnt, lpntstr, 0);
  390. if (!tpnt1)
  391. goto oops;
  392. /* This list is for dlsym() and relocation */
  393. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  394. memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  395. dyn_ptr = dyn_ptr->next;
  396. dyn_ptr->dyn = tpnt1;
  397. /* Used to record RTLD_LOCAL scope */
  398. tmp = alloca(sizeof(struct init_fini_list));
  399. tmp->tpnt = tpnt1;
  400. tmp->next = runp->tpnt->init_fini;
  401. runp->tpnt->init_fini = tmp;
  402. for (tmp=dep_list; tmp; tmp = tmp->next) {
  403. if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
  404. _dl_if_debug_print("Circular dependency, skipping '%s',\n",
  405. tmp->tpnt->libname);
  406. tpnt1->usage_count--;
  407. break;
  408. }
  409. }
  410. if (!tmp) { /* Don't add if circular dependency detected */
  411. runp2->next = alloca(sizeof(*runp));
  412. runp2 = runp2->next;
  413. runp2->tpnt = tpnt1;
  414. runp2->next = NULL;
  415. }
  416. }
  417. }
  418. }
  419. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  420. dyn_chain->init_fini.init_fini = init_fini_list;
  421. dyn_chain->init_fini.nlist = nlist;
  422. i = 0;
  423. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  424. init_fini_list[i++] = runp2->tpnt;
  425. for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
  426. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  427. tmp = malloc(sizeof(struct init_fini_list));
  428. tmp->tpnt = runp->tpnt;
  429. tmp->next = runp2->tpnt->rtld_local;
  430. runp2->tpnt->rtld_local = tmp;
  431. }
  432. }
  433. }
  434. /* Build the local scope for the dynamically loaded modules. */
  435. local_scope = _dl_malloc(nlist * sizeof(struct elf_resolve *)); /* Could it allocated on stack? */
  436. for (i = 0; i < nlist; i++)
  437. if (init_fini_list[i]->symbol_scope.r_nlist == 0) {
  438. int k, cnt;
  439. cnt = _dl_build_local_scope(local_scope, init_fini_list[i]);
  440. init_fini_list[i]->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  441. init_fini_list[i]->symbol_scope.r_nlist = cnt;
  442. _dl_memcpy (init_fini_list[i]->symbol_scope.r_list, local_scope,
  443. cnt * sizeof (struct elf_resolve *));
  444. /* Restoring the init_flag.*/
  445. for (k = 0; k < nlist; k++)
  446. init_fini_list[k]->init_flag &= ~DL_RESERVED;
  447. }
  448. _dl_free(local_scope);
  449. /* Sort the INIT/FINI list in dependency order. */
  450. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  451. unsigned int j, k;
  452. for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
  453. /* Empty */;
  454. for (k = j + 1; k < nlist; ++k) {
  455. struct init_fini_list *ele = init_fini_list[k]->init_fini;
  456. for (; ele; ele = ele->next) {
  457. if (ele->tpnt == runp2->tpnt) {
  458. struct elf_resolve *here = init_fini_list[k];
  459. _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  460. for (i = (k - j); i; --i)
  461. init_fini_list[i+j] = init_fini_list[i+j-1];
  462. init_fini_list[j] = here;
  463. ++j;
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. #ifdef __SUPPORT_LD_DEBUG__
  470. if (_dl_debug) {
  471. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  472. for (i = 0; i < nlist; i++) {
  473. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  474. runp = init_fini_list[i]->init_fini;
  475. for (; runp; runp = runp->next)
  476. fprintf(stderr, " %s ", runp->tpnt->libname);
  477. fprintf(stderr, "\n");
  478. }
  479. }
  480. #endif
  481. _dl_if_debug_print("Beginning dlopen relocation fixups\n");
  482. /*
  483. * OK, now all of the kids are tucked into bed in their proper addresses.
  484. * Now we go through and look for REL and RELA records that indicate fixups
  485. * to the GOT tables. We need to do this in reverse order so that COPY
  486. * directives work correctly */
  487. #ifdef SHARED
  488. /*
  489. * Get the tail of the list.
  490. * In the static case doesn't need to extend the global scope, it is
  491. * ready to be used as it is, because _dl_loaded_modules already points
  492. * to the dlopened library.
  493. */
  494. for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next);
  495. /* Extend the global scope by adding the local scope of the dlopened DSO. */
  496. ls->next = &dyn_chain->dyn->symbol_scope;
  497. #endif
  498. #ifdef __mips__
  499. /*
  500. * Relocation of the GOT entries for MIPS have to be done
  501. * after all the libraries have been loaded.
  502. */
  503. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  504. #endif
  505. if (_dl_fixup(dyn_chain, &_dl_loaded_modules->symbol_scope, now_flag))
  506. goto oops;
  507. if (relro_ptr) {
  508. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  509. if (rpnt->dyn->relro_size)
  510. _dl_protect_relro(rpnt->dyn);
  511. }
  512. }
  513. /* TODO: Should we set the protections of all pages back to R/O now ? */
  514. #if defined(USE_TLS) && USE_TLS
  515. for (i=0; i < nlist; i++) {
  516. struct elf_resolve *tmp_tpnt = init_fini_list[i];
  517. /* Only add TLS memory if this object is loaded now and
  518. therefore is not yet initialized. */
  519. if (!(tmp_tpnt->init_flag & INIT_FUNCS_CALLED)
  520. /* Only if the module defines thread local data. */
  521. && __builtin_expect (tmp_tpnt->l_tls_blocksize > 0, 0)) {
  522. /* Now that we know the object is loaded successfully add
  523. modules containing TLS data to the slot info table. We
  524. might have to increase its size. */
  525. _dl_add_to_slotinfo ((struct link_map*)tmp_tpnt);
  526. /* It is the case in which we couldn't perform TLS static
  527. initialization at relocation time, and we delayed it until
  528. the relocation has been completed. */
  529. if (tmp_tpnt->l_need_tls_init) {
  530. tmp_tpnt->l_need_tls_init = 0;
  531. # ifdef SHARED
  532. /* Update the slot information data for at least the
  533. generation of the DSO we are allocating data for. */
  534. _dl_update_slotinfo (tmp_tpnt->l_tls_modid);
  535. # endif
  536. _dl_init_static_tls((struct link_map*)tmp_tpnt);
  537. _dl_assert (tmp_tpnt->l_need_tls_init == 0);
  538. }
  539. /* We have to bump the generation counter. */
  540. any_tls = true;
  541. }
  542. }
  543. /* Bump the generation number if necessary. */
  544. if (any_tls && __builtin_expect (++_dl_tls_generation == 0, 0)) {
  545. _dl_debug_early("TLS generation counter wrapped! Please report this.");
  546. _dl_exit(30);
  547. }
  548. #endif
  549. /* Notify the debugger we have added some objects. */
  550. if (_dl_debug_addr) {
  551. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  552. if (dl_brk != NULL) {
  553. _dl_debug_addr->r_state = RT_ADD;
  554. (*dl_brk) ();
  555. _dl_debug_addr->r_state = RT_CONSISTENT;
  556. (*dl_brk) ();
  557. }
  558. }
  559. /* Run the ctors and setup the dtors */
  560. for (i = nlist; i; --i) {
  561. tpnt = init_fini_list[i-1];
  562. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  563. continue;
  564. tpnt->init_flag |= INIT_FUNCS_CALLED;
  565. if (tpnt->dynamic_info[DT_INIT]) {
  566. void (*dl_elf_func) (void);
  567. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  568. if (dl_elf_func) {
  569. _dl_if_debug_print("running ctors for library %s at '%p'\n",
  570. tpnt->libname, dl_elf_func);
  571. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  572. }
  573. }
  574. _dl_run_init_array(tpnt);
  575. }
  576. _dl_unmap_cache();
  577. return (void *) dyn_chain;
  578. oops:
  579. /* Something went wrong. Clean up and return NULL. */
  580. _dl_unmap_cache();
  581. do_dlclose(dyn_chain, 0);
  582. return NULL;
  583. }
  584. void *dlopen(const char *libname, int flag)
  585. {
  586. void *ret;
  587. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  588. ret = do_dlopen(libname, flag,
  589. (ElfW(Addr)) __builtin_return_address(0));
  590. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  591. return ret;
  592. }
  593. static void *do_dlsym(void *vhandle, const char *name, void *caller_address)
  594. {
  595. struct elf_resolve *tpnt, *tfrom;
  596. struct dyn_elf *handle;
  597. ElfW(Addr) from = 0;
  598. struct dyn_elf *rpnt;
  599. void *ret;
  600. struct symbol_ref sym_ref = { NULL, NULL };
  601. /* Nastiness to support underscore prefixes. */
  602. #ifdef __UCLIBC_UNDERSCORES__
  603. char tmp_buf[80];
  604. char *name2 = tmp_buf;
  605. size_t nlen = strlen (name) + 1;
  606. if (nlen + 1 > sizeof (tmp_buf))
  607. name2 = malloc (nlen + 1);
  608. if (name2 == 0) {
  609. _dl_error_number = LD_ERROR_MMAP_FAILED;
  610. return 0;
  611. }
  612. name2[0] = '_';
  613. memcpy (name2 + 1, name, nlen);
  614. #else
  615. const char *name2 = name;
  616. #endif
  617. handle = (struct dyn_elf *) vhandle;
  618. /* First of all verify that we have a real handle
  619. of some kind. Return NULL if not a valid handle. */
  620. if (handle == NULL)
  621. handle = _dl_symbol_tables;
  622. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  623. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  624. if (rpnt == handle)
  625. break;
  626. if (!rpnt) {
  627. _dl_error_number = LD_BAD_HANDLE;
  628. ret = NULL;
  629. goto out;
  630. }
  631. } else if (handle == RTLD_NEXT) {
  632. /*
  633. * Try and locate the module we were called from - we
  634. * need this so that we know where to start searching
  635. * from. We never pass RTLD_NEXT down into the actual
  636. * dynamic loader itself, as it doesn't know
  637. * how to properly treat it.
  638. */
  639. from = (ElfW(Addr)) caller_address;
  640. tfrom = NULL;
  641. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  642. tpnt = rpnt->dyn;
  643. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
  644. tfrom = tpnt;
  645. handle = rpnt->next;
  646. }
  647. }
  648. }
  649. tpnt = NULL;
  650. if (handle == _dl_symbol_tables)
  651. tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
  652. do {
  653. ret = _dl_find_hash(name2, &handle->dyn->symbol_scope, tpnt, ELF_RTYPE_CLASS_DLSYM, &sym_ref);
  654. if (ret != NULL)
  655. break;
  656. handle = handle->next;
  657. } while (from && handle);
  658. #if defined(USE_TLS) && USE_TLS && defined SHARED
  659. if (sym_ref.sym && (ELF_ST_TYPE(sym_ref.sym->st_info) == STT_TLS) && (sym_ref.tpnt)) {
  660. /* The found symbol is a thread-local storage variable.
  661. Return its address for the current thread. */
  662. ret = _dl_tls_symaddr ((struct link_map *)sym_ref.tpnt, (Elf32_Addr)ret);
  663. }
  664. #endif
  665. /*
  666. * Nothing found.
  667. */
  668. if (!ret)
  669. _dl_error_number = LD_NO_SYMBOL;
  670. out:
  671. #ifdef __UCLIBC_UNDERSCORES__
  672. if (name2 != tmp_buf)
  673. free (name2);
  674. #endif
  675. return ret;
  676. }
  677. void *dlsym(void *vhandle, const char *name)
  678. {
  679. void *ret;
  680. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  681. ret = do_dlsym(vhandle, name, __builtin_return_address(0));
  682. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  683. return ret;
  684. }
  685. #if 0
  686. void *dlvsym(void *vhandle, const char *name, const char *version)
  687. {
  688. return dlsym(vhandle, name);
  689. }
  690. #endif
  691. static int do_dlclose(void *vhandle, int need_fini)
  692. {
  693. struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
  694. struct init_fini_list *runp, *tmp;
  695. ElfW(Phdr) *ppnt;
  696. struct elf_resolve *tpnt, *run_tpnt;
  697. int (*dl_elf_fini) (void);
  698. void (*dl_brk) (void);
  699. struct dyn_elf *handle;
  700. unsigned int end = 0, start = 0xffffffff;
  701. unsigned int i, j;
  702. struct r_scope_elem *ls, *ls_next = NULL;
  703. struct elf_resolve **handle_rlist;
  704. #if defined(USE_TLS) && USE_TLS
  705. bool any_tls = false;
  706. size_t tls_free_start = NO_TLS_OFFSET;
  707. size_t tls_free_end = NO_TLS_OFFSET;
  708. struct link_map *tls_lmap;
  709. #endif
  710. handle = (struct dyn_elf *) vhandle;
  711. if (handle == _dl_symbol_tables)
  712. return 0;
  713. rpnt1 = NULL;
  714. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  715. if (rpnt == handle)
  716. break;
  717. rpnt1 = rpnt;
  718. }
  719. if (!rpnt) {
  720. _dl_error_number = LD_BAD_HANDLE;
  721. return 1;
  722. }
  723. if (rpnt1)
  724. rpnt1->next_handle = rpnt->next_handle;
  725. else
  726. _dl_handles = rpnt->next_handle;
  727. _dl_if_debug_print("%s: usage count: %d\n",
  728. handle->dyn->libname, handle->dyn->usage_count);
  729. if (handle->dyn->usage_count != 1) {
  730. handle->dyn->usage_count--;
  731. free(handle);
  732. return 0;
  733. }
  734. /* Store the handle's local scope array for later removal */
  735. handle_rlist = handle->dyn->symbol_scope.r_list;
  736. /* Store references to the local scope entries for later removal */
  737. for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next)
  738. if (ls->next->r_list[0] == handle->dyn) {
  739. break;
  740. }
  741. /* ls points to the previous local symbol scope */
  742. if(ls && ls->next)
  743. ls_next = ls->next->next;
  744. /* OK, this is a valid handle - now close out the file */
  745. for (j = 0; j < handle->init_fini.nlist; ++j) {
  746. tpnt = handle->init_fini.init_fini[j];
  747. tpnt->usage_count--;
  748. if (tpnt->usage_count == 0) {
  749. if ((tpnt->dynamic_info[DT_FINI]
  750. || tpnt->dynamic_info[DT_FINI_ARRAY])
  751. && need_fini
  752. && !(tpnt->init_flag & FINI_FUNCS_CALLED)
  753. ) {
  754. tpnt->init_flag |= FINI_FUNCS_CALLED;
  755. _dl_run_fini_array(tpnt);
  756. if (tpnt->dynamic_info[DT_FINI]) {
  757. dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  758. _dl_if_debug_print("running dtors for library %s at '%p'\n",
  759. tpnt->libname, dl_elf_fini);
  760. DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
  761. }
  762. }
  763. _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
  764. end = 0;
  765. for (i = 0, ppnt = tpnt->ppnt;
  766. i < tpnt->n_phent; ppnt++, i++) {
  767. if (ppnt->p_type != PT_LOAD)
  768. continue;
  769. if (ppnt->p_vaddr < start)
  770. start = ppnt->p_vaddr;
  771. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  772. end = ppnt->p_vaddr + ppnt->p_memsz;
  773. }
  774. #if defined(USE_TLS) && USE_TLS
  775. /* Do the cast to make things easy. */
  776. tls_lmap = (struct link_map *) tpnt;
  777. /* Remove the object from the dtv slotinfo array if it uses TLS. */
  778. if (__builtin_expect (tls_lmap->l_tls_blocksize > 0, 0)) {
  779. any_tls = true;
  780. if (_dl_tls_dtv_slotinfo_list != NULL
  781. && ! remove_slotinfo (tls_lmap->l_tls_modid,
  782. _dl_tls_dtv_slotinfo_list, 0,
  783. (tpnt->init_flag & INIT_FUNCS_CALLED)))
  784. /* All dynamically loaded modules with TLS are unloaded. */
  785. _dl_tls_max_dtv_idx = _dl_tls_static_nelem;
  786. if (tls_lmap->l_tls_offset != NO_TLS_OFFSET) {
  787. /*
  788. * Collect a contiguous chunk built from the objects in
  789. * this search list, going in either direction. When the
  790. * whole chunk is at the end of the used area then we can
  791. * reclaim it.
  792. */
  793. # if defined(TLS_TCB_AT_TP)
  794. if (tls_free_start == NO_TLS_OFFSET
  795. || (size_t) tls_lmap->l_tls_offset == tls_free_start) {
  796. /* Extend the contiguous chunk being reclaimed. */
  797. tls_free_start
  798. = tls_lmap->l_tls_offset -
  799. tls_lmap->l_tls_blocksize;
  800. if (tls_free_end == NO_TLS_OFFSET)
  801. tls_free_end = tls_lmap->l_tls_offset;
  802. } else if (tls_lmap->l_tls_offset - tls_lmap->l_tls_blocksize
  803. == tls_free_end)
  804. /* Extend the chunk backwards. */
  805. tls_free_end = tls_lmap->l_tls_offset;
  806. else {
  807. /*
  808. * This isn't contiguous with the last chunk freed.
  809. * One of them will be leaked unless we can free
  810. * one block right away.
  811. */
  812. if (tls_free_end == _dl_tls_static_used) {
  813. _dl_tls_static_used = tls_free_start;
  814. tls_free_end = tls_lmap->l_tls_offset;
  815. tls_free_start
  816. = tls_free_end - tls_lmap->l_tls_blocksize;
  817. } else if ((size_t) tls_lmap->l_tls_offset
  818. == _dl_tls_static_used)
  819. _dl_tls_static_used = tls_lmap->l_tls_offset -
  820. tls_lmap->l_tls_blocksize;
  821. else if (tls_free_end < (size_t) tls_lmap->l_tls_offset) {
  822. /*
  823. * We pick the later block. It has a chance
  824. * to be freed.
  825. */
  826. tls_free_end = tls_lmap->l_tls_offset;
  827. tls_free_start = tls_free_end -
  828. tls_lmap->l_tls_blocksize;
  829. }
  830. }
  831. # elif defined(TLS_DTV_AT_TP)
  832. if ((size_t) tls_lmap->l_tls_offset == tls_free_end)
  833. /* Extend the contiguous chunk being reclaimed. */
  834. tls_free_end -= tls_lmap->l_tls_blocksize;
  835. else if (tls_lmap->l_tls_offset + tls_lmap->l_tls_blocksize
  836. == tls_free_start)
  837. /* Extend the chunk backwards. */
  838. tls_free_start = tls_lmap->l_tls_offset;
  839. else {
  840. /*
  841. * This isn't contiguous with the last chunk
  842. * freed. One of them will be leaked.
  843. */
  844. if (tls_free_end == _dl_tls_static_used)
  845. _dl_tls_static_used = tls_free_start;
  846. tls_free_start = tls_lmap->l_tls_offset;
  847. tls_free_end = tls_free_start +
  848. tls_lmap->l_tls_blocksize;
  849. }
  850. # else
  851. # error Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined
  852. # endif
  853. } else {
  854. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  855. dtv_t *dtv = THREAD_DTV ();
  856. if (!(dtv[tls_lmap->l_tls_modid].pointer.is_static) &&
  857. dtv[tls_lmap->l_tls_modid].pointer.val != TLS_DTV_UNALLOCATED) {
  858. /* Note that free is called for NULL is well. We
  859. deallocate even if it is this dtv entry we are
  860. supposed to load. The reason is that we call
  861. memalign and not malloc. */
  862. _dl_free (dtv[tls_lmap->l_tls_modid].pointer.val);
  863. dtv[tls_lmap->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  864. }
  865. }
  866. }
  867. #endif
  868. end = (end + ADDR_ALIGN) & PAGE_ALIGN;
  869. start = start & ~ADDR_ALIGN;
  870. DL_LIB_UNMAP (tpnt, end - start);
  871. /* Free elements in RTLD_LOCAL scope list */
  872. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  873. tmp = runp->next;
  874. free(runp);
  875. }
  876. /* Next, remove tpnt from the loaded_module list */
  877. if (_dl_loaded_modules == tpnt) {
  878. _dl_loaded_modules = tpnt->next;
  879. if (_dl_loaded_modules)
  880. _dl_loaded_modules->prev = 0;
  881. } else {
  882. for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
  883. if (run_tpnt->next == tpnt) {
  884. _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
  885. run_tpnt->next = run_tpnt->next->next;
  886. if (run_tpnt->next)
  887. run_tpnt->next->prev = run_tpnt;
  888. break;
  889. }
  890. }
  891. }
  892. /* Next, remove tpnt from the global symbol table list */
  893. if (_dl_symbol_tables) {
  894. if (_dl_symbol_tables->dyn == tpnt) {
  895. _dl_symbol_tables = _dl_symbol_tables->next;
  896. if (_dl_symbol_tables)
  897. _dl_symbol_tables->prev = 0;
  898. } else {
  899. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  900. if (rpnt1->next->dyn == tpnt) {
  901. _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
  902. rpnt1_tmp = rpnt1->next->next;
  903. free(rpnt1->next);
  904. rpnt1->next = rpnt1_tmp;
  905. if (rpnt1->next)
  906. rpnt1->next->prev = rpnt1;
  907. break;
  908. }
  909. }
  910. }
  911. }
  912. free(tpnt->libname);
  913. if (handle->dyn != tpnt)
  914. free(tpnt->symbol_scope.r_list);
  915. free(tpnt);
  916. }
  917. }
  918. /* Unlink and release the handle's local scope from global one */
  919. if(ls)
  920. ls->next = ls_next;
  921. free(handle_rlist);
  922. for (rpnt1 = handle->next; rpnt1; rpnt1 = rpnt1_tmp) {
  923. rpnt1_tmp = rpnt1->next;
  924. free(rpnt1);
  925. }
  926. free(handle->init_fini.init_fini);
  927. free(handle);
  928. #if defined(USE_TLS) && USE_TLS
  929. /* If we removed any object which uses TLS bump the generation counter. */
  930. if (any_tls) {
  931. if (__builtin_expect(++_dl_tls_generation == 0, 0)) {
  932. _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
  933. _dl_exit(30);
  934. }
  935. if (tls_free_end == _dl_tls_static_used)
  936. _dl_tls_static_used = tls_free_start;
  937. }
  938. #endif
  939. if (_dl_debug_addr) {
  940. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  941. if (dl_brk != NULL) {
  942. _dl_debug_addr->r_state = RT_DELETE;
  943. (*dl_brk) ();
  944. _dl_debug_addr->r_state = RT_CONSISTENT;
  945. (*dl_brk) ();
  946. }
  947. }
  948. return 0;
  949. }
  950. int dlclose(void *vhandle)
  951. {
  952. int ret;
  953. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  954. ret = do_dlclose(vhandle, 1);
  955. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  956. return ret;
  957. }
  958. char *dlerror(void)
  959. {
  960. const char *retval;
  961. if (!_dl_error_number)
  962. return NULL;
  963. retval = dl_error_names[_dl_error_number];
  964. _dl_error_number = 0;
  965. return (char *)retval;
  966. }
  967. /*
  968. * Dump information to stderr about the current loaded modules
  969. */
  970. #ifdef __USE_GNU
  971. # if 0
  972. static const char type[][4] = { "Lib", "Exe", "Int", "Mod" };
  973. /* reimplement this, being a GNU extension it should be the same as on glibc */
  974. int dlinfo(void)
  975. {
  976. struct elf_resolve *tpnt;
  977. struct dyn_elf *rpnt, *hpnt;
  978. fprintf(stderr, "List of loaded modules\n");
  979. /* First start with a complete list of all of the loaded files. */
  980. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  981. fprintf(stderr, "\t%p %p %p %s %d %s\n",
  982. DL_LOADADDR_BASE(tpnt->loadaddr), tpnt, tpnt->symbol_scope,
  983. type[tpnt->libtype],
  984. tpnt->usage_count, tpnt->libname);
  985. }
  986. /* Next dump the module list for the application itself */
  987. fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
  988. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  989. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  990. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  991. fprintf(stderr, "Modules for handle %p\n", hpnt);
  992. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  993. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  994. }
  995. return 0;
  996. }
  997. #endif
  998. static int do_dladdr(const void *__address, Dl_info * __info)
  999. {
  1000. struct elf_resolve *pelf;
  1001. struct elf_resolve *rpnt;
  1002. _dl_map_cache();
  1003. /*
  1004. * Try and locate the module address is in
  1005. */
  1006. pelf = NULL;
  1007. _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
  1008. __address = DL_LOOKUP_ADDRESS (__address);
  1009. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  1010. struct elf_resolve *tpnt;
  1011. tpnt = rpnt;
  1012. _dl_if_debug_print("Module \"%s\" at %p\n",
  1013. tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
  1014. if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
  1015. pelf = tpnt;
  1016. }
  1017. if (!pelf) {
  1018. return 0;
  1019. }
  1020. /*
  1021. * Try and locate the symbol of address
  1022. */
  1023. {
  1024. char *strtab;
  1025. ElfW(Sym) *symtab;
  1026. unsigned int hn, si, sn, sf;
  1027. ElfW(Addr) sa = 0;
  1028. /* Set the info for the object the address lies in */
  1029. __info->dli_fname = pelf->libname;
  1030. __info->dli_fbase = (void *)pelf->mapaddr;
  1031. symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
  1032. strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
  1033. sf = sn = 0;
  1034. #ifdef __LDSO_GNU_HASH_SUPPORT__
  1035. if (pelf->l_gnu_bitmask) {
  1036. for (hn = 0; hn < pelf->nbucket; hn++) {
  1037. si = pelf->l_gnu_buckets[hn];
  1038. if (!si)
  1039. continue;
  1040. const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
  1041. do {
  1042. ElfW(Addr) symbol_addr;
  1043. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  1044. if ((symtab[si].st_shndx != SHN_UNDEF
  1045. || symtab[si].st_value != 0)
  1046. && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
  1047. && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
  1048. (ElfW(Addr)) __address)) {
  1049. sa = symbol_addr;
  1050. sn = si;
  1051. sf = 1;
  1052. }
  1053. _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
  1054. ++si;
  1055. } while ((*hasharr++ & 1u) == 0);
  1056. }
  1057. } else
  1058. #endif
  1059. for (hn = 0; hn < pelf->nbucket; hn++) {
  1060. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  1061. ElfW(Addr) symbol_addr;
  1062. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  1063. if ((symtab[si].st_shndx != SHN_UNDEF
  1064. || symtab[si].st_value != 0)
  1065. && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
  1066. && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
  1067. (ElfW(Addr)) __address)) {
  1068. sa = symbol_addr;
  1069. sn = si;
  1070. sf = 1;
  1071. }
  1072. _dl_if_debug_print("Symbol \"%s\" at %p\n",
  1073. strtab + symtab[si].st_name, symbol_addr);
  1074. }
  1075. }
  1076. if (sf) {
  1077. /* A nearest symbol has been found; fill the entries */
  1078. __info->dli_sname = strtab + symtab[sn].st_name;
  1079. __info->dli_saddr = (void *)sa;
  1080. } else {
  1081. /* No symbol found, fill entries with NULL value,
  1082. only the containing object will be returned. */
  1083. __info->dli_sname = NULL;
  1084. __info->dli_saddr = NULL;
  1085. }
  1086. return 1;
  1087. }
  1088. }
  1089. #endif
  1090. int dladdr(const void *__address, Dl_info * __info)
  1091. {
  1092. int ret;
  1093. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  1094. ret = do_dladdr(__address, __info);
  1095. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  1096. return ret;
  1097. }