libdl.c 34 KB

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