ldso.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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) 2005 by Joakim Tjernlund
  7. * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
  8. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  9. * David Engel, Hongjiu Lu and Mitch D'Souza
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. The name of the above contributors may not be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include "ldso.h"
  33. #include "unsecvars.h"
  34. /* Pull in common debug code */
  35. #include "dl-debug.c"
  36. #define ALLOW_ZERO_PLTGOT
  37. #if defined(USE_TLS) && USE_TLS
  38. #include "dl-tls.c"
  39. #endif
  40. /* Pull in the value of _dl_progname */
  41. #include LDSO_ELFINTERP
  42. /* Global variables used within the shared library loader */
  43. #ifdef __LDSO_LD_LIBRARY_PATH__
  44. char *_dl_library_path = NULL; /* Where we look for libraries */
  45. #endif
  46. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  47. char *_dl_preload = NULL; /* Things to be loaded before the libs */
  48. #endif
  49. int _dl_errno = 0; /* We can't use the real errno in ldso */
  50. size_t _dl_pagesize = 0; /* Store the page size for use later */
  51. struct r_debug *_dl_debug_addr = NULL; /* Used to communicate with the gdb debugger */
  52. void *(*_dl_malloc_function) (size_t size) = NULL;
  53. void (*_dl_free_function) (void *p) = NULL;
  54. #ifdef __LDSO_PRELINK_SUPPORT__
  55. char *_dl_trace_prelink = NULL; /* Library for prelinking trace */
  56. struct elf_resolve *_dl_trace_prelink_map = NULL; /* Library module for prelinking trace */
  57. bool _dl_verbose = true; /* On by default */
  58. bool prelinked = false;
  59. #endif
  60. static int _dl_secure = 1; /* Are we dealing with setuid stuff? */
  61. #ifdef __SUPPORT_LD_DEBUG__
  62. char *_dl_debug = NULL;
  63. char *_dl_debug_symbols = NULL;
  64. char *_dl_debug_move = NULL;
  65. char *_dl_debug_reloc = NULL;
  66. char *_dl_debug_detail = NULL;
  67. char *_dl_debug_nofixups = NULL;
  68. char *_dl_debug_bindings = NULL;
  69. int _dl_debug_file = 2;
  70. #endif
  71. #if defined (__LDSO_STANDALONE_SUPPORT__) && defined (__sh__)
  72. /* Not hidden, needed for standalone execution. */
  73. /*
  74. * FIXME: align dl_start for SH to other archs so that we can keep this symbol
  75. * hidden and we don't need to handle in __uClibc_main
  76. */
  77. unsigned long _dl_skip_args = 0;
  78. #else
  79. unsigned long attribute_hidden _dl_skip_args = 0;
  80. #endif
  81. const char *_dl_progname = UCLIBC_LDSO; /* The name of the executable being run */
  82. #include "dl-startup.c"
  83. #include "dl-symbols.c"
  84. #include "dl-array.c"
  85. /*
  86. * This stub function is used by some debuggers. The idea is that they
  87. * can set an internal breakpoint on it, so that we are notified when the
  88. * address mapping is changed in some way.
  89. */
  90. void _dl_debug_state(void);
  91. rtld_hidden_proto(_dl_debug_state, noinline);
  92. void _dl_debug_state(void)
  93. {
  94. /* Make sure GCC doesn't recognize this function as pure, to avoid
  95. * having the calls optimized away.
  96. */
  97. __asm__("");
  98. }
  99. rtld_hidden_def(_dl_debug_state);
  100. static unsigned char *_dl_malloc_addr = NULL; /* Lets _dl_malloc use the already allocated memory page */
  101. static unsigned char *_dl_mmap_zero = NULL; /* Also used by _dl_malloc */
  102. static struct elf_resolve **init_fini_list;
  103. static struct elf_resolve **scope_elem_list;
  104. static unsigned int nlist; /* # items in init_fini_list */
  105. extern void _start(void);
  106. #ifdef __UCLIBC_HAS_SSP__
  107. # include <dl-osinfo.h>
  108. static uintptr_t stack_chk_guard;
  109. # ifndef THREAD_SET_STACK_GUARD
  110. /* Only exported for architectures that don't store the stack guard canary
  111. * in local thread area. */
  112. uintptr_t __stack_chk_guard attribute_relro;
  113. # endif
  114. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  115. uintptr_t __guard attribute_relro;
  116. # endif
  117. #endif
  118. #ifdef __LDSO_SEARCH_INTERP_PATH__
  119. const char *_dl_ldsopath = NULL; /* Location of the shared lib loader */
  120. static void _dl_ldsopath_init(struct elf_resolve *tpnt)
  121. {
  122. char *ldsopath, *ptmp;
  123. /*
  124. * Store the path where the shared lib loader was found for later use.
  125. * Note that this logic isn't bullet proof when it comes to relative
  126. * paths: if you use "./lib/ldso.so", and then the app does chdir()
  127. * followed by dlopen(), the old ldso path won't get searched. But
  128. * that is a fairly pathological use case, so if you don't like that,
  129. * then set a full path to your interp and be done :P.
  130. */
  131. ldsopath = _dl_strdup(tpnt->libname);
  132. ptmp = _dl_strrchr(ldsopath, '/');
  133. /*
  134. * If there is no "/", then set the path to "", and the code
  135. * later on will take this to implicitly mean "search $PWD".
  136. */
  137. if (!ptmp)
  138. ptmp = ldsopath;
  139. *ptmp = '\0';
  140. _dl_ldsopath = ldsopath;
  141. _dl_debug_early("Lib Loader: (%x) %s: using path: %s\n",
  142. (unsigned) DL_LOADADDR_BASE(tpnt->loadaddr), tpnt->libname,
  143. _dl_ldsopath);
  144. }
  145. #else
  146. #define _dl_ldsopath_init(tpnt)
  147. #endif
  148. char *_dl_getenv(const char *symbol, char **envp)
  149. {
  150. char *pnt;
  151. const char *pnt1;
  152. while ((pnt = *envp++)) {
  153. pnt1 = symbol;
  154. while (*pnt && *pnt == *pnt1)
  155. pnt1++, pnt++;
  156. if (!*pnt || *pnt != '=' || *pnt1)
  157. continue;
  158. return pnt + 1;
  159. }
  160. return 0;
  161. }
  162. void _dl_unsetenv(const char *symbol, char **envp)
  163. {
  164. char *pnt;
  165. const char *pnt1;
  166. char **newenvp = envp;
  167. for (pnt = *envp; pnt; pnt = *++envp) {
  168. pnt1 = symbol;
  169. while (*pnt && *pnt == *pnt1)
  170. pnt1++, pnt++;
  171. if (!*pnt || *pnt != '=' || *pnt1)
  172. *newenvp++ = *envp;
  173. }
  174. *newenvp++ = *envp;
  175. return;
  176. }
  177. static int _dl_suid_ok(void)
  178. {
  179. __kernel_uid_t uid, euid;
  180. __kernel_gid_t gid, egid;
  181. uid = _dl_getuid();
  182. euid = _dl_geteuid();
  183. gid = _dl_getgid();
  184. egid = _dl_getegid();
  185. if (uid == euid && gid == egid) {
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. void *_dl_malloc(size_t size)
  191. {
  192. void *retval;
  193. #if 0
  194. _dl_debug_early("request for %d bytes\n", size);
  195. #endif
  196. if (_dl_malloc_function)
  197. return (*_dl_malloc_function) (size);
  198. if (_dl_malloc_addr - _dl_mmap_zero + size > _dl_pagesize) {
  199. size_t rounded_size;
  200. /* Since the above assumes we get a full page even if
  201. we request less than that, make sure we request a
  202. full page, since uClinux may give us less than than
  203. a full page. We might round even
  204. larger-than-a-page sizes, but we end up never
  205. reusing _dl_mmap_zero/_dl_malloc_addr in that case,
  206. so we don't do it.
  207. The actual page size doesn't really matter; as long
  208. as we're self-consistent here, we're safe. */
  209. if (size < _dl_pagesize)
  210. rounded_size = (size + ADDR_ALIGN) & _dl_pagesize;
  211. else
  212. rounded_size = size;
  213. _dl_debug_early("mmapping more memory\n");
  214. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, rounded_size,
  215. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  216. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  217. _dl_dprintf(_dl_debug_file, "%s: mmap of a spare page failed!\n", _dl_progname);
  218. _dl_exit(20);
  219. }
  220. }
  221. retval = _dl_malloc_addr;
  222. _dl_malloc_addr += size;
  223. /*
  224. * Align memory to DL_MALLOC_ALIGN byte boundary. Some
  225. * platforms require this, others simply get better
  226. * performance.
  227. */
  228. _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1));
  229. return retval;
  230. }
  231. static void *_dl_zalloc(size_t size)
  232. {
  233. void *p = _dl_malloc(size);
  234. if (p)
  235. _dl_memset(p, 0, size);
  236. return p;
  237. }
  238. void _dl_free(void *p)
  239. {
  240. if (_dl_free_function)
  241. (*_dl_free_function) (p);
  242. }
  243. #if defined(USE_TLS) && USE_TLS
  244. void *_dl_memalign(size_t __boundary, size_t __size)
  245. {
  246. void *result;
  247. int i = 0;
  248. size_t delta;
  249. size_t rounded = 0;
  250. if (_dl_memalign_function)
  251. return (*_dl_memalign_function) (__boundary, __size);
  252. while (rounded < __boundary) {
  253. rounded = (1 << i++);
  254. }
  255. delta = (((size_t) _dl_malloc_addr + __size) & (rounded - 1));
  256. if ((result = _dl_malloc(rounded - delta)) == NULL)
  257. return result;
  258. result = _dl_malloc(__size);
  259. return result;
  260. }
  261. #endif
  262. static void __attribute__ ((destructor)) __attribute_used__ _dl_fini(void)
  263. {
  264. unsigned int i;
  265. struct elf_resolve * tpnt;
  266. for (i = 0; i < nlist; ++i) {
  267. tpnt = init_fini_list[i];
  268. if (tpnt->init_flag & FINI_FUNCS_CALLED)
  269. continue;
  270. tpnt->init_flag |= FINI_FUNCS_CALLED;
  271. _dl_run_fini_array(tpnt);
  272. if (tpnt->dynamic_info[DT_FINI]) {
  273. void (*dl_elf_func) (void);
  274. dl_elf_func = (void (*)(void)) (intptr_t) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  275. _dl_if_debug_dprint("\ncalling FINI: %s\n\n", tpnt->libname);
  276. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  277. }
  278. }
  279. }
  280. #ifdef __LDSO_PRELINK_SUPPORT__
  281. static void trace_objects(struct elf_resolve *tpnt, char *str_name)
  282. {
  283. if (_dl_strcmp(_dl_trace_prelink, tpnt->libname) == 0)
  284. _dl_trace_prelink_map = tpnt;
  285. if (tpnt->libtype == elf_executable) {
  286. /* Main executeble */
  287. _dl_dprintf(1, "\t%s => %s (%x, %x)", tpnt->libname, tpnt->libname,
  288. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  289. } else {
  290. /* Preloaded, Needed or interpreter */
  291. _dl_dprintf(1, "\t%s => %s (%x, %x)", str_name, tpnt->libname,
  292. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  293. }
  294. #if defined USE_TLS && USE_TLS
  295. if ((tpnt->libtype != program_interpreter) && (tpnt->l_tls_modid))
  296. _dl_dprintf (1, " TLS(%x, %x)\n", tpnt->l_tls_modid,
  297. (size_t) tpnt->l_tls_offset);
  298. else
  299. #endif
  300. _dl_dprintf (1, "\n");
  301. }
  302. #endif
  303. static struct elf_resolve * add_ldso(struct elf_resolve *tpnt,
  304. DL_LOADADDR_TYPE load_addr,
  305. ElfW(Addr) ldso_mapaddr,
  306. ElfW(auxv_t) auxvt[AT_EGID + 1],
  307. struct dyn_elf *rpnt)
  308. {
  309. ElfW(Ehdr) *epnt = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_val;
  310. ElfW(Phdr) *myppnt = (ElfW(Phdr) *)
  311. DL_RELOC_ADDR(DL_GET_RUN_ADDR(load_addr, ldso_mapaddr),
  312. epnt->e_phoff);
  313. int j;
  314. struct stat st;
  315. tpnt = _dl_add_elf_hash_table(tpnt->libname, tpnt->loadaddr,
  316. tpnt->dynamic_info, (unsigned long)tpnt->dynamic_addr,
  317. 0);
  318. tpnt->mapaddr = ldso_mapaddr;
  319. if (_dl_stat(tpnt->libname, &st) >= 0) {
  320. tpnt->st_dev = st.st_dev;
  321. tpnt->st_ino = st.st_ino;
  322. }
  323. tpnt->n_phent = epnt->e_phnum;
  324. tpnt->ppnt = myppnt;
  325. for (j = 0; j < epnt->e_phnum; j++, myppnt++) {
  326. if (myppnt->p_type == PT_GNU_RELRO) {
  327. tpnt->relro_addr = myppnt->p_vaddr;
  328. tpnt->relro_size = myppnt->p_memsz;
  329. break;
  330. }
  331. }
  332. tpnt->libtype = program_interpreter;
  333. if (rpnt) {
  334. rpnt->next = _dl_zalloc(sizeof(struct dyn_elf));
  335. rpnt->next->prev = rpnt;
  336. rpnt = rpnt->next;
  337. } else {
  338. rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  339. }
  340. rpnt->dyn = tpnt;
  341. tpnt->rtld_flags = RTLD_NOW | RTLD_GLOBAL; /* Must not be LAZY */
  342. return tpnt;
  343. }
  344. static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
  345. struct elf_resolve *map)
  346. {
  347. struct elf_resolve **p = list;
  348. struct init_fini_list *q;
  349. *p++ = map;
  350. map->init_flag |= DL_RESERVED;
  351. if (map->init_fini)
  352. for (q = map->init_fini; q; q = q->next)
  353. if (! (q->tpnt->init_flag & DL_RESERVED))
  354. p += _dl_build_local_scope (p, q->tpnt);
  355. return p - list;
  356. }
  357. void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE load_addr,
  358. ElfW(auxv_t) auxvt[AT_EGID + 1], char **envp, char **argv
  359. DL_GET_READY_TO_RUN_EXTRA_PARMS)
  360. {
  361. ElfW(Addr) app_mapaddr = 0, ldso_mapaddr = 0;
  362. ElfW(Phdr) *ppnt;
  363. ElfW(Dyn) *dpnt;
  364. char *lpntstr;
  365. unsigned int i, cnt, nscope_elem;
  366. int unlazy = 0, trace_loaded_objects = 0;
  367. struct dyn_elf *rpnt;
  368. struct elf_resolve *tcurr;
  369. struct elf_resolve *tpnt1;
  370. struct elf_resolve *ldso_tpnt = NULL;
  371. struct elf_resolve app_tpnt_tmp;
  372. struct elf_resolve *app_tpnt = &app_tpnt_tmp;
  373. struct r_debug *debug_addr;
  374. unsigned long *lpnt;
  375. unsigned long *_dl_envp; /* The environment address */
  376. ElfW(Addr) relro_addr = 0;
  377. size_t relro_size = 0;
  378. struct r_scope_elem *global_scope;
  379. struct elf_resolve **local_scope;
  380. #if defined(USE_TLS) && USE_TLS
  381. void *tcbp = NULL;
  382. #endif
  383. /* Wahoo!!! We managed to make a function call! Get malloc
  384. * setup so we can use _dl_dprintf() to print debug noise
  385. * instead of the SEND_STDERR macros used in dl-startup.c */
  386. _dl_memset(app_tpnt, 0, sizeof(*app_tpnt));
  387. /* Store the page size for later use */
  388. _dl_pagesize = (auxvt[AT_PAGESZ].a_un.a_val) ? (size_t) auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  389. /* Make it so _dl_malloc can use the page of memory we have already
  390. * allocated. We shouldn't need to grab any more memory. This must
  391. * be first since things like _dl_dprintf() use _dl_malloc()...
  392. */
  393. _dl_malloc_addr = (unsigned char *)_dl_pagesize;
  394. _dl_mmap_zero = 0;
  395. /* Wahoo!!! */
  396. _dl_debug_early("Cool, ldso survived making function calls\n");
  397. /* Now we have done the mandatory linking of some things. We are now
  398. * free to start using global variables, since these things have all
  399. * been fixed up by now. Still no function calls outside of this
  400. * library, since the dynamic resolver is not yet ready.
  401. */
  402. if (argv[0]) {
  403. _dl_progname = argv[0];
  404. }
  405. #ifndef __LDSO_STANDALONE_SUPPORT__
  406. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val) {
  407. _dl_dprintf(_dl_debug_file, "Standalone execution is not enabled\n");
  408. _dl_exit(1);
  409. }
  410. #endif
  411. /* Start to build the tables of the modules that are required for
  412. * this beast to run. We start with the basic executable, and then
  413. * go from there. Eventually we will run across ourself, and we
  414. * will need to properly deal with that as well.
  415. */
  416. rpnt = NULL;
  417. if (_dl_getenv("LD_BIND_NOW", envp))
  418. unlazy = RTLD_NOW;
  419. /* Now we need to figure out what kind of options are selected.
  420. * Note that for SUID programs we ignore the settings in
  421. * LD_LIBRARY_PATH.
  422. */
  423. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && _dl_suid_ok()) ||
  424. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  425. auxvt[AT_UID].a_un.a_val == auxvt[AT_EUID].a_un.a_val &&
  426. auxvt[AT_GID].a_un.a_val == auxvt[AT_EGID].a_un.a_val)) {
  427. _dl_secure = 0;
  428. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  429. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  430. #endif
  431. #ifdef __LDSO_LD_LIBRARY_PATH__
  432. _dl_library_path = _dl_getenv("LD_LIBRARY_PATH", envp);
  433. #endif
  434. } else {
  435. static const char unsecure_envvars[] =
  436. #ifdef EXTRA_UNSECURE_ENVVARS
  437. EXTRA_UNSECURE_ENVVARS
  438. #endif
  439. UNSECURE_ENVVARS;
  440. const char *nextp;
  441. _dl_secure = 1;
  442. nextp = unsecure_envvars;
  443. do {
  444. _dl_unsetenv (nextp, envp);
  445. /* We could use rawmemchr but this need not be fast. */
  446. nextp = _dl_strchr(nextp, '\0') + 1;
  447. } while (*nextp != '\0');
  448. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  449. _dl_preload = NULL;
  450. #endif
  451. #ifdef __LDSO_LD_LIBRARY_PATH__
  452. _dl_library_path = NULL;
  453. #endif
  454. /* SUID binaries can be exploited if they do LAZY relocation. */
  455. unlazy = RTLD_NOW;
  456. }
  457. #if defined(USE_TLS) && USE_TLS
  458. _dl_error_catch_tsd = &_dl_initial_error_catch_tsd;
  459. _dl_init_static_tls = &_dl_nothread_init_static_tls;
  460. #endif
  461. #ifdef __LDSO_STANDALONE_SUPPORT__
  462. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val) {
  463. unsigned int *aux_dat = (unsigned int *) argv;
  464. int argc = aux_dat[-1];
  465. tpnt->libname = argv[0];
  466. while (argc > 1)
  467. if (! _dl_strcmp (argv[1], "--library-path") && argc > 2) {
  468. #ifdef __LDSO_LD_LIBRARY_PATH__
  469. _dl_library_path = argv[2];
  470. #endif
  471. _dl_skip_args += 2;
  472. argc -= 2;
  473. argv += 2;
  474. } else
  475. break;
  476. /*
  477. * If we have no further argument the program was called incorrectly.
  478. * Grant the user some education.
  479. */
  480. if (argc < 2) {
  481. _dl_dprintf(1, "\
  482. Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
  483. You have invoked `ld.so', the helper program for shared library executables.\n\
  484. This program usually lives in the file `/lib/ld.so', and special directives\n\
  485. in executable files using ELF shared libraries tell the system's program\n\
  486. loader to load the helper program from this file. This helper program loads\n\
  487. the shared libraries needed by the program executable, prepares the program\n\
  488. to run, and runs it. You may invoke this helper program directly from the\n\
  489. command line to load and run an ELF executable file; this is like executing\n\
  490. that file itself, but always uses this helper program from the file you\n\
  491. specified, instead of the helper program file specified in the executable\n\
  492. file you run. This is mostly of use for maintainers to test new versions\n\
  493. of this helper program; chances are you did not intend to run this program.\n\
  494. \n\
  495. --library-path PATH use given PATH instead of content of the environment\n\
  496. variable LD_LIBRARY_PATH\n");
  497. _dl_exit(1);
  498. }
  499. ++_dl_skip_args;
  500. ++argv;
  501. _dl_progname = argv[0];
  502. _dl_symbol_tables = rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  503. /*
  504. * It needs to load the _dl_progname and to map it
  505. * Usually it is the main application launched by means of the ld.so
  506. * but it could be also a shared object (when ld.so used for tracing)
  507. * We keep the misleading app_tpnt name to avoid variable pollution
  508. */
  509. app_tpnt = _dl_load_elf_shared_library(_dl_secure, &rpnt, _dl_progname);
  510. if (!app_tpnt) {
  511. _dl_dprintf(_dl_debug_file, "can't load '%s'\n", _dl_progname);
  512. _dl_exit(16);
  513. }
  514. /*
  515. * FIXME: it needs to properly handle a PIE executable
  516. * Usually for a main application, loadaddr is computed as difference
  517. * between auxvt entry points and phdr, so if it is not 0, that it is a
  518. * PIE executable. In this case instead we need to set the loadaddr to 0
  519. * because we are actually mapping the ELF for the main application by
  520. * ourselves. So the PIE case must be checked.
  521. */
  522. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  523. /*
  524. * This is used by gdb to locate the chain of shared libraries that are
  525. * currently loaded.
  526. */
  527. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  528. ppnt = (ElfW(Phdr) *)app_tpnt->ppnt;
  529. for (i = 0; i < app_tpnt->n_phent; i++, ppnt++) {
  530. if (ppnt->p_type == PT_DYNAMIC) {
  531. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  532. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  533. }
  534. }
  535. _dl_ldsopath_init(tpnt);
  536. } else {
  537. #endif
  538. /* At this point we are now free to examine the user application,
  539. * and figure out which libraries are supposed to be called. Until
  540. * we have this list, we will not be completely ready for dynamic
  541. * linking.
  542. */
  543. /* Find the runtime load address of the main executable. This may be
  544. * different from what the ELF header says for ET_DYN/PIE executables.
  545. */
  546. {
  547. unsigned int idx;
  548. ElfW(Phdr) *phdr = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  549. for (idx = 0; idx < auxvt[AT_PHNUM].a_un.a_val; idx++, phdr++)
  550. if (phdr->p_type == PT_PHDR) {
  551. DL_INIT_LOADADDR_PROG(app_tpnt->loadaddr, auxvt[AT_PHDR].a_un.a_val - phdr->p_vaddr);
  552. break;
  553. }
  554. if (DL_LOADADDR_BASE(app_tpnt->loadaddr))
  555. _dl_debug_early("Position Independent Executable: "
  556. "app_tpnt->loadaddr=%x\n", DL_LOADADDR_BASE(app_tpnt->loadaddr));
  557. }
  558. /*
  559. * This is used by gdb to locate the chain of shared libraries that are
  560. * currently loaded.
  561. */
  562. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  563. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  564. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  565. if (ppnt->p_type == PT_GNU_RELRO) {
  566. relro_addr = ppnt->p_vaddr;
  567. relro_size = ppnt->p_memsz;
  568. }
  569. if (!app_mapaddr && (ppnt->p_type == PT_LOAD)) {
  570. app_mapaddr = DL_RELOC_ADDR (app_tpnt->loadaddr, ppnt->p_vaddr);
  571. }
  572. if (ppnt->p_type == PT_DYNAMIC) {
  573. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  574. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  575. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  576. /* Ugly, ugly. We need to call mprotect to change the
  577. * protection of the text pages so that we can do the
  578. * dynamic linking. We can set the protection back
  579. * again once we are done.
  580. */
  581. _dl_debug_early("calling mprotect on the application program\n");
  582. /* Now cover the application program. */
  583. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  584. ElfW(Phdr) *ppnt_outer = ppnt;
  585. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  586. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  587. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  588. _dl_mprotect((void *) (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & PAGE_ALIGN),
  589. (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & ADDR_ALIGN) +
  590. (unsigned long) ppnt->p_filesz,
  591. PROT_READ | PROT_WRITE | PROT_EXEC);
  592. }
  593. ppnt = ppnt_outer;
  594. }
  595. #else
  596. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  597. _dl_dprintf(_dl_debug_file, "Can't modify application's text section; use the GCC option -fPIE for position-independent executables.\n");
  598. _dl_exit(1);
  599. }
  600. #endif
  601. #ifndef ALLOW_ZERO_PLTGOT
  602. /* make sure it's really there. */
  603. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0)
  604. continue;
  605. #endif
  606. /* OK, we have what we need - slip this one into the list. */
  607. app_tpnt = _dl_add_elf_hash_table(_dl_progname, app_tpnt->loadaddr,
  608. app_tpnt->dynamic_info,
  609. (unsigned long) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr),
  610. ppnt->p_filesz);
  611. _dl_loaded_modules->libtype = elf_executable;
  612. _dl_loaded_modules->ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  613. _dl_loaded_modules->n_phent = auxvt[AT_PHNUM].a_un.a_val;
  614. _dl_symbol_tables = rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  615. rpnt->dyn = _dl_loaded_modules;
  616. app_tpnt->mapaddr = app_mapaddr;
  617. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  618. app_tpnt->usage_count++;
  619. lpnt = (unsigned long *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  620. #ifdef ALLOW_ZERO_PLTGOT
  621. if (lpnt)
  622. #endif
  623. INIT_GOT(lpnt, _dl_loaded_modules);
  624. }
  625. /* OK, fill this in - we did not have this before */
  626. if (ppnt->p_type == PT_INTERP) {
  627. tpnt->libname = (char *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  628. _dl_ldsopath_init(tpnt);
  629. }
  630. /* Discover any TLS sections if the target supports them. */
  631. if (ppnt->p_type == PT_TLS) {
  632. #if defined(USE_TLS) && USE_TLS
  633. if (ppnt->p_memsz > 0) {
  634. app_tpnt->l_tls_blocksize = ppnt->p_memsz;
  635. app_tpnt->l_tls_align = ppnt->p_align;
  636. if (ppnt->p_align == 0)
  637. app_tpnt->l_tls_firstbyte_offset = 0;
  638. else
  639. app_tpnt->l_tls_firstbyte_offset =
  640. (ppnt->p_vaddr & (ppnt->p_align - 1));
  641. app_tpnt->l_tls_initimage_size = ppnt->p_filesz;
  642. app_tpnt->l_tls_initimage = (void *) ppnt->p_vaddr;
  643. /* This image gets the ID one. */
  644. _dl_tls_max_dtv_idx = app_tpnt->l_tls_modid = 1;
  645. }
  646. _dl_debug_early("Found TLS header for application program\n");
  647. break;
  648. #else
  649. _dl_dprintf(_dl_debug_file, "Program uses unsupported TLS data!\n");
  650. _dl_exit(1);
  651. #endif
  652. }
  653. }
  654. app_tpnt->relro_addr = relro_addr;
  655. app_tpnt->relro_size = relro_size;
  656. #if defined(USE_TLS) && USE_TLS
  657. /*
  658. * Adjust the address of the TLS initialization image in
  659. * case the executable is actually an ET_DYN object.
  660. */
  661. if (app_tpnt->l_tls_initimage != NULL) {
  662. unsigned int tmp = (unsigned int) app_tpnt->l_tls_initimage;
  663. app_tpnt->l_tls_initimage =
  664. (char *) app_tpnt->l_tls_initimage + app_tpnt->loadaddr;
  665. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n",
  666. tmp, app_tpnt->l_tls_initimage, app_tpnt->l_tls_initimage_size);
  667. }
  668. #endif
  669. #ifdef __LDSO_STANDALONE_SUPPORT__
  670. } /* ! ldso standalone mode */
  671. #endif
  672. #ifdef __SUPPORT_LD_DEBUG__
  673. _dl_debug = _dl_getenv("LD_DEBUG", envp);
  674. if (_dl_debug) {
  675. if (_dl_strstr(_dl_debug, "all")) {
  676. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  677. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  678. } else {
  679. _dl_debug_detail = _dl_strstr(_dl_debug, "detail");
  680. _dl_debug_move = _dl_strstr(_dl_debug, "move");
  681. _dl_debug_symbols = _dl_strstr(_dl_debug, "sym");
  682. _dl_debug_reloc = _dl_strstr(_dl_debug, "reloc");
  683. _dl_debug_nofixups = _dl_strstr(_dl_debug, "nofix");
  684. _dl_debug_bindings = _dl_strstr(_dl_debug, "bind");
  685. }
  686. }
  687. {
  688. const char *dl_debug_output;
  689. dl_debug_output = _dl_getenv("LD_DEBUG_OUTPUT", envp);
  690. if (dl_debug_output) {
  691. char tmp[22], *tmp1, *filename;
  692. int len1, len2;
  693. _dl_memset(tmp, 0, sizeof(tmp));
  694. tmp1 = _dl_simple_ltoa( tmp, (unsigned long)_dl_getpid());
  695. len1 = _dl_strlen(dl_debug_output);
  696. len2 = _dl_strlen(tmp1);
  697. filename = _dl_malloc(len1 + len2 + 2);
  698. if (filename) {
  699. _dl_strcpy (filename, dl_debug_output);
  700. filename[len1] = '.';
  701. _dl_strcpy (&filename[len1+1], tmp1);
  702. _dl_debug_file = _dl_open(filename, O_WRONLY|O_CREAT, 0644);
  703. if (_dl_debug_file < 0) {
  704. _dl_debug_file = 2;
  705. _dl_dprintf(_dl_debug_file, "can't open file: '%s'\n",filename);
  706. }
  707. }
  708. }
  709. }
  710. #endif
  711. #ifdef __LDSO_PRELINK_SUPPORT__
  712. {
  713. char *ld_warn = _dl_getenv ("LD_WARN", envp);
  714. if (ld_warn && *ld_warn == '\0')
  715. _dl_verbose = false;
  716. }
  717. _dl_trace_prelink = _dl_getenv("LD_TRACE_PRELINKING", envp);
  718. #endif
  719. if (_dl_getenv("LD_TRACE_LOADED_OBJECTS", envp) != NULL) {
  720. trace_loaded_objects++;
  721. }
  722. #ifndef __LDSO_LDD_SUPPORT__
  723. if (trace_loaded_objects) {
  724. _dl_dprintf(_dl_debug_file, "Use the ldd provided by uClibc\n");
  725. _dl_exit(1);
  726. }
  727. #endif
  728. ldso_mapaddr = (ElfW(Addr)) auxvt[AT_BASE].a_un.a_val;
  729. /*
  730. * OK, fix one more thing - set up debug_addr so it will point
  731. * to our chain. Later we may need to fill in more fields, but this
  732. * should be enough for now.
  733. */
  734. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  735. debug_addr->r_version = 1;
  736. debug_addr->r_ldbase = (ElfW(Addr))
  737. DL_LOADADDR_BASE(DL_GET_RUN_ADDR(load_addr, ldso_mapaddr));
  738. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  739. _dl_debug_addr = debug_addr;
  740. /* Do not notify the debugger until the interpreter is in the list */
  741. /* OK, we now have the application in the list, and we have some
  742. * basic stuff in place. Now search through the list for other shared
  743. * libraries that should be loaded, and insert them on the list in the
  744. * correct order.
  745. */
  746. _dl_map_cache();
  747. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  748. if (_dl_preload) {
  749. char c, *str, *str2;
  750. str = _dl_preload;
  751. while (*str == ':' || *str == ' ' || *str == '\t')
  752. str++;
  753. while (*str) {
  754. str2 = str;
  755. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  756. str2++;
  757. c = *str2;
  758. *str2 = '\0';
  759. if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
  760. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", str, _dl_progname);
  761. tpnt1 = _dl_load_shared_library(
  762. _dl_secure ? DL_RESOLVE_SECURE : 0,
  763. &rpnt, NULL, str, trace_loaded_objects);
  764. if (!tpnt1) {
  765. #ifdef __LDSO_LDD_SUPPORT__
  766. if (trace_loaded_objects || _dl_trace_prelink)
  767. _dl_dprintf(1, "\t%s => not found\n", str);
  768. else
  769. #endif
  770. {
  771. _dl_dprintf(_dl_debug_file, "%s: can't load " "library '%s'\n", _dl_progname, str);
  772. _dl_exit(15);
  773. }
  774. } else {
  775. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  776. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  777. #ifdef __LDSO_LDD_SUPPORT__
  778. if (trace_loaded_objects && !_dl_trace_prelink &&
  779. tpnt1->usage_count == 1) {
  780. /* This is a real hack to make
  781. * ldd not print the library
  782. * itself when run on a
  783. * library.
  784. */
  785. if (_dl_strcmp(_dl_progname, str) != 0)
  786. _dl_dprintf(1, "\t%s => %s (%x)\n", str, tpnt1->libname,
  787. DL_LOADADDR_BASE(tpnt1->loadaddr));
  788. }
  789. #endif
  790. }
  791. }
  792. *str2 = c;
  793. str = str2;
  794. while (*str == ':' || *str == ' ' || *str == '\t')
  795. str++;
  796. }
  797. }
  798. #endif /* __LDSO_PRELOAD_ENV_SUPPORT__ */
  799. #ifdef __LDSO_PRELOAD_FILE_SUPPORT__
  800. do {
  801. char *preload;
  802. int fd;
  803. char c, *cp, *cp2;
  804. struct stat st;
  805. if (_dl_stat(LDSO_PRELOAD, &st) || st.st_size == 0) {
  806. break;
  807. }
  808. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY, 0)) < 0) {
  809. _dl_dprintf(_dl_debug_file, "%s: can't open file '%s'\n",
  810. _dl_progname, LDSO_PRELOAD);
  811. break;
  812. }
  813. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  814. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  815. _dl_close(fd);
  816. if (preload == (caddr_t) -1) {
  817. _dl_dprintf(_dl_debug_file, "%s:%i: can't map '%s'\n",
  818. _dl_progname, __LINE__, LDSO_PRELOAD);
  819. break;
  820. }
  821. /* convert all separators and comments to spaces */
  822. for (cp = preload; *cp; /*nada */ ) {
  823. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  824. *cp++ = ' ';
  825. } else if (*cp == '#') {
  826. do {
  827. *cp++ = ' ';
  828. } while (*cp != '\n' && *cp != '\0');
  829. } else {
  830. cp++;
  831. }
  832. }
  833. /* find start of first library */
  834. for (cp = preload; *cp && *cp == ' '; cp++)
  835. /*nada */ ;
  836. while (*cp) {
  837. /* find end of library */
  838. for (cp2 = cp; *cp && *cp != ' '; cp++)
  839. /*nada */ ;
  840. c = *cp;
  841. *cp = '\0';
  842. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", cp2, _dl_progname);
  843. tpnt1 = _dl_load_shared_library(0, &rpnt, NULL, cp2, trace_loaded_objects);
  844. if (!tpnt1) {
  845. # ifdef __LDSO_LDD_SUPPORT__
  846. if (trace_loaded_objects || _dl_trace_prelink)
  847. _dl_dprintf(1, "\t%s => not found\n", cp2);
  848. else
  849. # endif
  850. {
  851. _dl_dprintf(_dl_debug_file, "%s: can't load library '%s'\n", _dl_progname, cp2);
  852. _dl_exit(15);
  853. }
  854. } else {
  855. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  856. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  857. # ifdef __LDSO_LDD_SUPPORT__
  858. if (trace_loaded_objects && !_dl_trace_prelink &&
  859. tpnt1->usage_count == 1) {
  860. _dl_dprintf(1, "\t%s => %s (%x)\n",
  861. cp2, tpnt1->libname,
  862. DL_LOADADDR_BASE(tpnt1->loadaddr));
  863. }
  864. # endif
  865. }
  866. /* find start of next library */
  867. *cp = c;
  868. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  869. /*nada */ ;
  870. }
  871. _dl_munmap(preload, st.st_size + 1);
  872. } while (0);
  873. #endif /* __LDSO_PRELOAD_FILE_SUPPORT__ */
  874. nlist = 0;
  875. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  876. ElfW(Dyn) *this_dpnt;
  877. nlist++;
  878. for (this_dpnt = (ElfW(Dyn) *) tcurr->dynamic_addr; this_dpnt->d_tag; this_dpnt++) {
  879. if (this_dpnt->d_tag == DT_NEEDED) {
  880. char *name;
  881. struct init_fini_list *tmp;
  882. lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] + this_dpnt->d_un.d_val);
  883. name = _dl_get_last_path_component(lpntstr);
  884. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", lpntstr, _dl_progname);
  885. if (_dl_strcmp(name, UCLIBC_LDSO) == 0) {
  886. if (!ldso_tpnt) {
  887. /* Insert the ld.so only once */
  888. ldso_tpnt = add_ldso(tpnt, load_addr,
  889. ldso_mapaddr, auxvt, rpnt);
  890. }
  891. ldso_tpnt->usage_count++;
  892. tpnt1 = ldso_tpnt;
  893. } else
  894. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, trace_loaded_objects);
  895. if (!tpnt1) {
  896. #ifdef __LDSO_LDD_SUPPORT__
  897. if (trace_loaded_objects || _dl_trace_prelink) {
  898. _dl_dprintf(1, "\t%s => not found\n", lpntstr);
  899. continue;
  900. } else
  901. #endif
  902. {
  903. _dl_dprintf(_dl_debug_file, "%s: can't load library '%s'\n", _dl_progname, lpntstr);
  904. _dl_exit(16);
  905. }
  906. }
  907. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  908. tmp->tpnt = tpnt1;
  909. tmp->next = tcurr->init_fini;
  910. tcurr->init_fini = tmp;
  911. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  912. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  913. #ifdef __LDSO_LDD_SUPPORT__
  914. if (trace_loaded_objects && !_dl_trace_prelink &&
  915. tpnt1->usage_count == 1) {
  916. _dl_dprintf(1, "\t%s => %s (%x)\n",
  917. lpntstr, tpnt1->libname,
  918. DL_LOADADDR_BASE(tpnt1->loadaddr));
  919. }
  920. #endif
  921. }
  922. }
  923. }
  924. _dl_unmap_cache();
  925. /* Keep track of the number of elements in the global scope */
  926. nscope_elem = nlist;
  927. if (_dl_loaded_modules->libtype == elf_executable) {
  928. --nlist; /* Exclude the application. */
  929. tcurr = _dl_loaded_modules->next;
  930. } else
  931. tcurr = _dl_loaded_modules;
  932. init_fini_list = _dl_malloc(nlist * sizeof(struct elf_resolve *));
  933. i = 0;
  934. for (; tcurr; tcurr = tcurr->next)
  935. init_fini_list[i++] = tcurr;
  936. /* Sort the INIT/FINI list in dependency order. */
  937. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  938. unsigned int j, k;
  939. for (j = 0; init_fini_list[j] != tcurr; ++j)
  940. /* Empty */;
  941. for (k = j + 1; k < nlist; ++k) {
  942. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  943. for (; runp; runp = runp->next) {
  944. if (runp->tpnt == tcurr) {
  945. struct elf_resolve *here = init_fini_list[k];
  946. _dl_if_debug_dprint("Move %s from pos %d to %d in INIT/FINI list\n", here->libname, k, j);
  947. for (i = (k - j); i; --i)
  948. init_fini_list[i+j] = init_fini_list[i+j-1];
  949. init_fini_list[j] = here;
  950. ++j;
  951. break;
  952. }
  953. }
  954. }
  955. }
  956. #ifdef __SUPPORT_LD_DEBUG__
  957. if (_dl_debug) {
  958. _dl_dprintf(_dl_debug_file, "\nINIT/FINI order and dependencies:\n");
  959. for (i = 0; i < nlist; i++) {
  960. struct init_fini_list *tmp;
  961. _dl_dprintf(_dl_debug_file, "lib: %s has deps:\n",
  962. init_fini_list[i]->libname);
  963. tmp = init_fini_list[i]->init_fini;
  964. for (; tmp; tmp = tmp->next)
  965. _dl_dprintf(_dl_debug_file, " %s ", tmp->tpnt->libname);
  966. _dl_dprintf(_dl_debug_file, "\n");
  967. }
  968. }
  969. #endif
  970. /*
  971. * If the program interpreter is not in the module chain, add it.
  972. * This will be required for dlopen to be able to access the internal
  973. * functions in the dynamic linker and to relocate the interpreter
  974. * again once all libs are loaded.
  975. */
  976. if (!ldso_tpnt) {
  977. tpnt = add_ldso(tpnt, load_addr, ldso_mapaddr, auxvt, rpnt);
  978. tpnt->usage_count++;
  979. nscope_elem++;
  980. } else
  981. tpnt = ldso_tpnt;
  982. #ifdef RERELOCATE_LDSO
  983. /* Only rerelocate functions for now. */
  984. tpnt->init_flag = RELOCS_DONE;
  985. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT]);
  986. # ifdef ALLOW_ZERO_PLTGOT
  987. if (tpnt->dynamic_info[DT_PLTGOT])
  988. # endif
  989. INIT_GOT(lpnt, tpnt);
  990. #else
  991. tpnt->init_flag = RELOCS_DONE | JMP_RELOCS_DONE;
  992. #endif
  993. tpnt = NULL;
  994. /*
  995. * Allocate the global scope array.
  996. */
  997. scope_elem_list = (struct elf_resolve **) _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  998. for (i = 0, tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next)
  999. scope_elem_list[i++] = tcurr;
  1000. _dl_loaded_modules->symbol_scope.r_list = scope_elem_list;
  1001. _dl_loaded_modules->symbol_scope.r_nlist = nscope_elem;
  1002. /*
  1003. * The symbol scope of the application, that is the first entry of the
  1004. * _dl_loaded_modules list, is just the global scope to be used for the
  1005. * symbol lookup.
  1006. */
  1007. global_scope = &_dl_loaded_modules->symbol_scope;
  1008. /* Build the local scope for each loaded modules. */
  1009. local_scope = _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1010. i = 1;
  1011. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  1012. unsigned int k;
  1013. cnt = _dl_build_local_scope(local_scope, scope_elem_list[i++]);
  1014. tcurr->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  1015. tcurr->symbol_scope.r_nlist = cnt;
  1016. _dl_memcpy (tcurr->symbol_scope.r_list, local_scope, cnt * sizeof (struct elf_resolve *));
  1017. /* Restoring the init_flag.*/
  1018. for (k = 1; k < nscope_elem; k++)
  1019. scope_elem_list[k]->init_flag &= ~DL_RESERVED;
  1020. }
  1021. _dl_free(local_scope);
  1022. #ifdef __LDSO_LDD_SUPPORT__
  1023. /* Exit if LD_TRACE_LOADED_OBJECTS is on. */
  1024. if (trace_loaded_objects && !_dl_trace_prelink)
  1025. _dl_exit(0);
  1026. #endif
  1027. #if defined(USE_TLS) && USE_TLS
  1028. /* We do not initialize any of the TLS functionality unless any of the
  1029. * initial modules uses TLS. This makes dynamic loading of modules with
  1030. * TLS impossible, but to support it requires either eagerly doing setup
  1031. * now or lazily doing it later. Doing it now makes us incompatible with
  1032. * an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
  1033. * used. Trying to do it lazily is too hairy to try when there could be
  1034. * multiple threads (from a non-TLS-using libpthread). */
  1035. bool was_tls_init_tp_called = tls_init_tp_called;
  1036. if (tcbp == NULL) {
  1037. _dl_debug_early("Calling init_tls()!\n");
  1038. tcbp = init_tls ();
  1039. }
  1040. #endif
  1041. #ifdef __UCLIBC_HAS_SSP__
  1042. /* Set up the stack checker's canary. */
  1043. stack_chk_guard = _dl_setup_stack_chk_guard ();
  1044. # ifdef THREAD_SET_STACK_GUARD
  1045. THREAD_SET_STACK_GUARD (stack_chk_guard);
  1046. # else
  1047. __stack_chk_guard = stack_chk_guard;
  1048. # endif
  1049. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  1050. __guard = stack_chk_guard;
  1051. # endif
  1052. #endif
  1053. #ifdef __LDSO_PRELINK_SUPPORT__
  1054. if (_dl_trace_prelink) {
  1055. unsigned int nscope_trace = ldso_tpnt ? nscope_elem : (nscope_elem - 1);
  1056. for (i = 0; i < nscope_trace; i++)
  1057. trace_objects(scope_elem_list[i],
  1058. _dl_get_last_path_component(scope_elem_list[i]->libname));
  1059. if (_dl_verbose)
  1060. /* Warn about undefined symbols. */
  1061. if (_dl_symbol_tables)
  1062. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1063. _dl_exit(-1);
  1064. _dl_exit(0);
  1065. }
  1066. if (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX]) {
  1067. ElfW(Lib) *liblist, *liblistend;
  1068. struct elf_resolve **r_list, **r_listend, *l;
  1069. const char *strtab = (const char *)_dl_loaded_modules->dynamic_info[DT_STRTAB];
  1070. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX] != 0);
  1071. liblist = (ElfW(Lib) *) _dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX];
  1072. liblistend = (ElfW(Lib) *)
  1073. ((char *) liblist + _dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX]);
  1074. r_list = _dl_loaded_modules->symbol_scope.r_list;
  1075. r_listend = r_list + nscope_elem;
  1076. for (; r_list < r_listend && liblist < liblistend; r_list++) {
  1077. l = *r_list;
  1078. if (l == _dl_loaded_modules)
  1079. continue;
  1080. /* If the library is not mapped where it should, fail. */
  1081. if (l->loadaddr)
  1082. break;
  1083. /* Next, check if checksum matches. */
  1084. if (l->dynamic_info[DT_CHECKSUM_IDX] == 0 ||
  1085. l->dynamic_info[DT_CHECKSUM_IDX] != liblist->l_checksum)
  1086. break;
  1087. if (l->dynamic_info[DT_GNU_PRELINKED_IDX] == 0 ||
  1088. (l->dynamic_info[DT_GNU_PRELINKED_IDX] != liblist->l_time_stamp))
  1089. break;
  1090. if (_dl_strcmp(strtab + liblist->l_name, _dl_get_last_path_component(l->libname)) != 0)
  1091. break;
  1092. ++liblist;
  1093. }
  1094. if (r_list == r_listend && liblist == liblistend)
  1095. prelinked = true;
  1096. }
  1097. _dl_debug_early ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
  1098. if (prelinked) {
  1099. if (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX]) {
  1100. ELF_RELOC *conflict;
  1101. unsigned long conflict_size;
  1102. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX] != 0);
  1103. conflict = (ELF_RELOC *) _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX];
  1104. conflict_size = _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX];
  1105. _dl_parse_relocation_information(_dl_symbol_tables, global_scope,
  1106. (unsigned long) conflict, conflict_size);
  1107. }
  1108. /* Mark all the objects so we know they have been already relocated. */
  1109. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1110. tpnt->init_flag |= RELOCS_DONE;
  1111. if (tpnt->relro_size)
  1112. _dl_protect_relro (tpnt);
  1113. }
  1114. } else
  1115. #endif
  1116. {
  1117. _dl_debug_early("Beginning relocation fixups\n");
  1118. #ifdef __mips__
  1119. /*
  1120. * Relocation of the GOT entries for MIPS have to be done
  1121. * after all the libraries have been loaded.
  1122. */
  1123. _dl_perform_mips_global_got_relocations(_dl_loaded_modules, !unlazy);
  1124. #endif
  1125. /*
  1126. * OK, now all of the kids are tucked into bed in their proper
  1127. * addresses. Now we go through and look for REL and RELA records that
  1128. * indicate fixups to the GOT tables. We need to do this in reverse
  1129. * order so that COPY directives work correctly.
  1130. */
  1131. if (_dl_symbol_tables)
  1132. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1133. _dl_exit(-1);
  1134. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1135. if (tpnt->relro_size)
  1136. _dl_protect_relro (tpnt);
  1137. }
  1138. } /* not prelinked */
  1139. #if defined(USE_TLS) && USE_TLS
  1140. if (!was_tls_init_tp_called && _dl_tls_max_dtv_idx > 0)
  1141. ++_dl_tls_generation;
  1142. _dl_debug_early("Calling _dl_allocate_tls_init()!\n");
  1143. /* Now that we have completed relocation, the initializer data
  1144. for the TLS blocks has its final values and we can copy them
  1145. into the main thread's TLS area, which we allocated above. */
  1146. _dl_allocate_tls_init (tcbp);
  1147. /* And finally install it for the main thread. If ld.so itself uses
  1148. TLS we know the thread pointer was initialized earlier. */
  1149. if (! tls_init_tp_called) {
  1150. const char *lossage = (char *) TLS_INIT_TP (tcbp, USE___THREAD);
  1151. if (__builtin_expect (lossage != NULL, 0)) {
  1152. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  1153. _dl_exit(30);
  1154. }
  1155. }
  1156. #endif /* USE_TLS */
  1157. /* OK, at this point things are pretty much ready to run. Now we need
  1158. * to touch up a few items that are required, and then we can let the
  1159. * user application have at it. Note that the dynamic linker itself
  1160. * is not guaranteed to be fully dynamicly linked if we are using
  1161. * ld.so.1, so we have to look up each symbol individually.
  1162. */
  1163. _dl_envp = (unsigned long *) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "__environ", global_scope, NULL, 0, NULL);
  1164. if (_dl_envp)
  1165. *_dl_envp = (unsigned long) envp;
  1166. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  1167. {
  1168. unsigned int j;
  1169. ElfW(Phdr) *myppnt;
  1170. /* We had to set the protections of all pages to R/W for
  1171. * dynamic linking. Set text pages back to R/O.
  1172. */
  1173. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1174. for (myppnt = tpnt->ppnt, j = 0; j < tpnt->n_phent; j++, myppnt++) {
  1175. if (myppnt->p_type == PT_LOAD && !(myppnt->p_flags & PF_W) && tpnt->dynamic_info[DT_TEXTREL]) {
  1176. _dl_mprotect((void *) (DL_RELOC_ADDR(tpnt->loadaddr, myppnt->p_vaddr) & PAGE_ALIGN),
  1177. (myppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) myppnt->p_filesz, LXFLAGS(myppnt->p_flags));
  1178. }
  1179. }
  1180. }
  1181. }
  1182. #endif
  1183. /* Notify the debugger we have added some objects. */
  1184. _dl_debug_addr->r_state = RT_ADD;
  1185. _dl_debug_state();
  1186. /* Run pre-initialization functions for the executable. */
  1187. _dl_run_array_forward(_dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAY],
  1188. _dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAYSZ],
  1189. _dl_loaded_modules->loadaddr);
  1190. /* Run initialization functions for loaded objects. For the
  1191. main executable, they will be run from __uClibc_main. */
  1192. for (i = nlist; i; --i) {
  1193. tpnt = init_fini_list[i-1];
  1194. tpnt->init_fini = NULL; /* Clear, since alloca was used */
  1195. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  1196. continue;
  1197. tpnt->init_flag |= INIT_FUNCS_CALLED;
  1198. if (tpnt->dynamic_info[DT_INIT]) {
  1199. void (*dl_elf_func) (void);
  1200. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  1201. _dl_if_debug_dprint("calling INIT: %s\n\n", tpnt->libname);
  1202. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  1203. }
  1204. _dl_run_init_array(tpnt);
  1205. }
  1206. /* Find the real malloc function and make ldso functions use that from now on */
  1207. _dl_malloc_function = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
  1208. global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1209. #if defined(USE_TLS) && USE_TLS
  1210. /* Find the real functions and make ldso functions use them from now on */
  1211. _dl_calloc_function = (void* (*)(size_t, size_t)) (intptr_t)
  1212. _dl_find_hash(__C_SYMBOL_PREFIX__ "calloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1213. _dl_realloc_function = (void* (*)(void *, size_t)) (intptr_t)
  1214. _dl_find_hash(__C_SYMBOL_PREFIX__ "realloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1215. _dl_free_function = (void (*)(void *)) (intptr_t)
  1216. _dl_find_hash(__C_SYMBOL_PREFIX__ "free", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1217. _dl_memalign_function = (void* (*)(size_t, size_t)) (intptr_t)
  1218. _dl_find_hash(__C_SYMBOL_PREFIX__ "memalign", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1219. #endif
  1220. /* Notify the debugger that all objects are now mapped in. */
  1221. _dl_debug_addr->r_state = RT_CONSISTENT;
  1222. _dl_debug_state();
  1223. #ifdef __LDSO_STANDALONE_SUPPORT__
  1224. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val)
  1225. return (void *) app_tpnt->l_entry;
  1226. else
  1227. #endif
  1228. return (void *) auxvt[AT_ENTRY].a_un.a_val;
  1229. }
  1230. #include "dl-hash.c"
  1231. #include "dl-elf.c"