libdl.c 35 KB

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