libdl.c 35 KB

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