libdl.c 33 KB

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