ldso.c 44 KB

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