libdl.c 33 KB

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