ldso.c 44 KB

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