libdl.c 33 KB

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