ldso.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. #ifdef __FDPIC__
  99. /* We need to take the address of _start instead of its FUNCDESC:
  100. declare it as void* to control the relocation emitted. */
  101. extern void *_start;
  102. #else
  103. extern void _start(void);
  104. #endif
  105. #ifdef __UCLIBC_HAS_SSP__
  106. # include <dl-osinfo.h>
  107. static uintptr_t stack_chk_guard;
  108. # ifndef THREAD_SET_STACK_GUARD
  109. /* Only exported for architectures that don't store the stack guard canary
  110. * in local thread area. */
  111. uintptr_t __stack_chk_guard attribute_relro;
  112. # endif
  113. #endif
  114. #ifdef __LDSO_SEARCH_INTERP_PATH__
  115. const char *_dl_ldsopath = NULL; /* Location of the shared lib loader */
  116. static void _dl_ldsopath_init(struct elf_resolve *tpnt)
  117. {
  118. char *ldsopath, *ptmp;
  119. /*
  120. * Store the path where the shared lib loader was found for later use.
  121. * Note that this logic isn't bullet proof when it comes to relative
  122. * paths: if you use "./lib/ldso.so", and then the app does chdir()
  123. * followed by dlopen(), the old ldso path won't get searched. But
  124. * that is a fairly pathological use case, so if you don't like that,
  125. * then set a full path to your interp and be done :P.
  126. */
  127. ldsopath = _dl_strdup(tpnt->libname);
  128. ptmp = _dl_strrchr(ldsopath, '/');
  129. /*
  130. * If there is no "/", then set the path to "", and the code
  131. * later on will take this to implicitly mean "search $PWD".
  132. */
  133. if (!ptmp)
  134. ptmp = ldsopath;
  135. *ptmp = '\0';
  136. _dl_ldsopath = ldsopath;
  137. _dl_debug_early("Lib Loader: (%x) %s: using path: %s\n",
  138. (unsigned) DL_LOADADDR_BASE(tpnt->loadaddr), tpnt->libname,
  139. _dl_ldsopath);
  140. }
  141. #else
  142. #define _dl_ldsopath_init(tpnt)
  143. #endif
  144. char *_dl_getenv(const char *symbol, char **envp)
  145. {
  146. char *pnt;
  147. const char *pnt1;
  148. while ((pnt = *envp++)) {
  149. pnt1 = symbol;
  150. while (*pnt && *pnt == *pnt1)
  151. pnt1++, pnt++;
  152. if (!*pnt || *pnt != '=' || *pnt1)
  153. continue;
  154. return pnt + 1;
  155. }
  156. return 0;
  157. }
  158. void _dl_unsetenv(const char *symbol, char **envp)
  159. {
  160. char *pnt;
  161. const char *pnt1;
  162. char **newenvp = envp;
  163. for (pnt = *envp; pnt; pnt = *++envp) {
  164. pnt1 = symbol;
  165. while (*pnt && *pnt == *pnt1)
  166. pnt1++, pnt++;
  167. if (!*pnt || *pnt != '=' || *pnt1)
  168. *newenvp++ = *envp;
  169. }
  170. *newenvp++ = *envp;
  171. return;
  172. }
  173. static int _dl_suid_ok(void)
  174. {
  175. __kernel_uid_t uid, euid;
  176. __kernel_gid_t gid, egid;
  177. uid = _dl_getuid();
  178. euid = _dl_geteuid();
  179. gid = _dl_getgid();
  180. egid = _dl_getegid();
  181. if (uid == euid && gid == egid) {
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. void *_dl_malloc(size_t size)
  187. {
  188. void *retval;
  189. #if 0
  190. _dl_debug_early("request for %d bytes\n", size);
  191. #endif
  192. if (_dl_malloc_function)
  193. return (*_dl_malloc_function) (size);
  194. if (_dl_malloc_addr - _dl_mmap_zero + size > _dl_pagesize) {
  195. size_t rounded_size;
  196. /* Since the above assumes we get a full page even if
  197. we request less than that, make sure we request a
  198. full page, since uClinux may give us less than than
  199. a full page. We might round even
  200. larger-than-a-page sizes, but we end up never
  201. reusing _dl_mmap_zero/_dl_malloc_addr in that case,
  202. so we don't do it.
  203. The actual page size doesn't really matter; as long
  204. as we're self-consistent here, we're safe. */
  205. if (size < _dl_pagesize)
  206. rounded_size = (size + ADDR_ALIGN) & _dl_pagesize;
  207. else
  208. rounded_size = size;
  209. _dl_debug_early("mmapping more memory\n");
  210. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, rounded_size,
  211. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | _MAP_UNINITIALIZED, -1, 0);
  212. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  213. _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname);
  214. _dl_exit(20);
  215. }
  216. }
  217. retval = _dl_malloc_addr;
  218. _dl_malloc_addr += size;
  219. /*
  220. * Align memory to DL_MALLOC_ALIGN byte boundary. Some
  221. * platforms require this, others simply get better
  222. * performance.
  223. */
  224. _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1));
  225. return retval;
  226. }
  227. static void *_dl_zalloc(size_t size)
  228. {
  229. void *p = _dl_malloc(size);
  230. if (p)
  231. _dl_memset(p, 0, size);
  232. else
  233. _dl_exit(1);
  234. return p;
  235. }
  236. void _dl_free(void *p)
  237. {
  238. if (_dl_free_function)
  239. (*_dl_free_function) (p);
  240. }
  241. #if defined(USE_TLS) && USE_TLS
  242. void *_dl_memalign(size_t __boundary, size_t __size)
  243. {
  244. void *result;
  245. int i = 0;
  246. size_t delta;
  247. size_t rounded = 0;
  248. if (_dl_memalign_function)
  249. return (*_dl_memalign_function) (__boundary, __size);
  250. while (rounded < __boundary) {
  251. rounded = (1 << i++);
  252. }
  253. delta = (((size_t) _dl_malloc_addr + __size) & (rounded - 1));
  254. if ((result = _dl_malloc(rounded - delta)) == NULL)
  255. return result;
  256. result = _dl_malloc(__size);
  257. return result;
  258. }
  259. #endif
  260. static void __attribute__ ((destructor)) __attribute_used__ _dl_fini(void)
  261. {
  262. unsigned int i;
  263. struct elf_resolve * tpnt;
  264. for (i = 0; i < nlist; ++i) {
  265. tpnt = init_fini_list[i];
  266. if (tpnt->init_flag & FINI_FUNCS_CALLED)
  267. continue;
  268. tpnt->init_flag |= FINI_FUNCS_CALLED;
  269. _dl_run_fini_array(tpnt);
  270. if (tpnt->dynamic_info[DT_FINI]) {
  271. void (*dl_elf_func) (void);
  272. dl_elf_func = (void (*)(void)) (intptr_t) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  273. _dl_if_debug_dprint("\ncalling FINI: %s\n\n", tpnt->libname);
  274. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  275. }
  276. }
  277. }
  278. #ifdef __LDSO_PRELINK_SUPPORT__
  279. static void trace_objects(struct elf_resolve *tpnt, char *str_name)
  280. {
  281. if (_dl_strcmp(_dl_trace_prelink, tpnt->libname) == 0)
  282. _dl_trace_prelink_map = tpnt;
  283. if (tpnt->libtype == elf_executable) {
  284. /* Main executeble */
  285. _dl_dprintf(1, "\t%s => %s (%x, %x)", tpnt->libname, tpnt->libname,
  286. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  287. } else {
  288. /* Preloaded, Needed or interpreter */
  289. _dl_dprintf(1, "\t%s => %s (%x, %x)", str_name, tpnt->libname,
  290. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  291. }
  292. #if defined USE_TLS && USE_TLS
  293. if ((tpnt->libtype != program_interpreter) && (tpnt->l_tls_modid))
  294. _dl_dprintf (1, " TLS(%x, %x)\n", tpnt->l_tls_modid,
  295. (size_t) tpnt->l_tls_offset);
  296. else
  297. #endif
  298. _dl_dprintf (1, "\n");
  299. }
  300. #endif
  301. static struct elf_resolve * add_ldso(struct elf_resolve *tpnt,
  302. DL_LOADADDR_TYPE load_addr,
  303. ElfW(Addr) ldso_mapaddr,
  304. ElfW(auxv_t) auxvt[AT_EGID + 1],
  305. struct dyn_elf *rpnt)
  306. {
  307. ElfW(Ehdr) *epnt = (ElfW(Ehdr) *) 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. ElfW(auxv_t) auxvt[AT_EGID + 1], 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 = (auxvt[AT_PAGESZ].a_un.a_val) ? (size_t) 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 *) 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 ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && _dl_suid_ok()) ||
  441. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  442. auxvt[AT_UID].a_un.a_val == auxvt[AT_EUID].a_un.a_val &&
  443. auxvt[AT_GID].a_un.a_val == 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 *) 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) *) auxvt[AT_PHDR].a_un.a_val;
  567. for (idx = 0; idx < auxvt[AT_PHNUM].a_un.a_val; idx++, phdr++)
  568. if (phdr->p_type == PT_PHDR) {
  569. DL_INIT_LOADADDR_PROG(app_tpnt->loadaddr, 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) *) auxvt[AT_PHDR].a_un.a_val;
  582. for (i = 0; i < 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) *) auxvt[AT_PHDR].a_un.a_val;
  605. for (j = 0; j < 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) *) auxvt[AT_PHDR].a_un.a_val;
  632. _dl_loaded_modules->n_phent = 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 = (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. }
  711. }
  712. {
  713. const char *dl_debug_output;
  714. dl_debug_output = _dl_getenv("LD_DEBUG_OUTPUT", envp);
  715. if (dl_debug_output) {
  716. char tmp[22], *tmp1, *filename;
  717. int len1, len2;
  718. _dl_memset(tmp, 0, sizeof(tmp));
  719. tmp1 = _dl_simple_ltoa( tmp, (unsigned long)_dl_getpid());
  720. len1 = _dl_strlen(dl_debug_output);
  721. len2 = _dl_strlen(tmp1);
  722. filename = _dl_malloc(len1 + len2 + 2);
  723. if (filename) {
  724. _dl_strcpy (filename, dl_debug_output);
  725. filename[len1] = '.';
  726. _dl_strcpy (&filename[len1+1], tmp1);
  727. _dl_debug_file = _dl_open(filename, O_WRONLY|O_CREAT, 0644);
  728. if (_dl_debug_file < 0) {
  729. _dl_debug_file = 2;
  730. _dl_dprintf(_dl_debug_file, "can't open file: '%s'\n",filename);
  731. }
  732. }
  733. }
  734. }
  735. #endif
  736. #ifdef __LDSO_PRELINK_SUPPORT__
  737. {
  738. char *ld_warn = _dl_getenv ("LD_WARN", envp);
  739. if (ld_warn && *ld_warn == '\0')
  740. _dl_verbose = false;
  741. }
  742. _dl_trace_prelink = _dl_getenv("LD_TRACE_PRELINKING", envp);
  743. #endif
  744. if (_dl_getenv("LD_TRACE_LOADED_OBJECTS", envp) != NULL) {
  745. trace_loaded_objects++;
  746. }
  747. #ifndef __LDSO_LDD_SUPPORT__
  748. if (trace_loaded_objects) {
  749. _dl_dprintf(2, "Use the ldd provided by uClibc\n");
  750. _dl_exit(1);
  751. }
  752. #endif
  753. ldso_mapaddr = (ElfW(Addr)) auxvt[AT_BASE].a_un.a_val;
  754. /*
  755. * OK, fix one more thing - set up debug_addr so it will point
  756. * to our chain. Later we may need to fill in more fields, but this
  757. * should be enough for now.
  758. */
  759. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  760. debug_addr->r_version = 1;
  761. debug_addr->r_ldbase = (ElfW(Addr))
  762. DL_LOADADDR_BASE(DL_GET_RUN_ADDR(load_addr, ldso_mapaddr));
  763. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  764. _dl_debug_addr = debug_addr;
  765. /* Do not notify the debugger until the interpreter is in the list */
  766. /* OK, we now have the application in the list, and we have some
  767. * basic stuff in place. Now search through the list for other shared
  768. * libraries that should be loaded, and insert them on the list in the
  769. * correct order.
  770. */
  771. _dl_map_cache();
  772. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  773. if (_dl_preload) {
  774. char c, *str, *str2;
  775. str = _dl_preload;
  776. while (*str == ':' || *str == ' ' || *str == '\t')
  777. str++;
  778. while (*str) {
  779. str2 = str;
  780. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  781. str2++;
  782. c = *str2;
  783. *str2 = '\0';
  784. if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
  785. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", str, _dl_progname);
  786. tpnt1 = _dl_load_shared_library(
  787. _dl_secure ? __RTLD_SECURE : 0,
  788. &rpnt, NULL, str, trace_loaded_objects);
  789. if (!tpnt1) {
  790. #ifdef __LDSO_LDD_SUPPORT__
  791. if (trace_loaded_objects || _dl_trace_prelink)
  792. _dl_dprintf(1, "\t%s => not found\n", str);
  793. else
  794. #endif
  795. {
  796. _dl_dprintf(2, "%s: library '%s' "
  797. "from LD_PRELOAD can't be preloaded: ignored.\n",
  798. _dl_progname, str);
  799. }
  800. } else {
  801. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  802. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  803. #ifdef __LDSO_LDD_SUPPORT__
  804. if (trace_loaded_objects && !_dl_trace_prelink &&
  805. !(tpnt1->init_flag & DL_OPENED2)) {
  806. /* This is a real hack to make
  807. * ldd not print the library
  808. * itself when run on a
  809. * library.
  810. */
  811. if (_dl_strcmp(_dl_progname, str) != 0)
  812. _dl_dprintf(1, "\t%s => %s (%x)\n", str, tpnt1->libname,
  813. DL_LOADADDR_BASE(tpnt1->loadaddr));
  814. }
  815. #endif
  816. }
  817. }
  818. *str2 = c;
  819. str = str2;
  820. while (*str == ':' || *str == ' ' || *str == '\t')
  821. str++;
  822. }
  823. }
  824. #endif /* __LDSO_PRELOAD_ENV_SUPPORT__ */
  825. #ifdef __LDSO_PRELOAD_FILE_SUPPORT__
  826. do {
  827. char *preload;
  828. int fd;
  829. char c, *cp, *cp2;
  830. struct stat st;
  831. if (_dl_stat(LDSO_PRELOAD, &st) || st.st_size == 0) {
  832. break;
  833. }
  834. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY, 0)) < 0) {
  835. _dl_dprintf(2, "%s: can't open file '%s'\n",
  836. _dl_progname, LDSO_PRELOAD);
  837. break;
  838. }
  839. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  840. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  841. _dl_close(fd);
  842. if (preload == (caddr_t) -1) {
  843. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  844. _dl_progname, __LINE__, LDSO_PRELOAD);
  845. break;
  846. }
  847. /* convert all separators and comments to spaces */
  848. for (cp = preload; *cp; /*nada */ ) {
  849. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  850. *cp++ = ' ';
  851. } else if (*cp == '#') {
  852. do {
  853. *cp++ = ' ';
  854. } while (*cp != '\n' && *cp != '\0');
  855. } else {
  856. cp++;
  857. }
  858. }
  859. /* find start of first library */
  860. for (cp = preload; *cp && *cp == ' '; cp++)
  861. /*nada */ ;
  862. while (*cp) {
  863. /* find end of library */
  864. for (cp2 = cp; *cp && *cp != ' '; cp++)
  865. /*nada */ ;
  866. c = *cp;
  867. *cp = '\0';
  868. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", cp2, _dl_progname);
  869. tpnt1 = _dl_load_shared_library(0, &rpnt, NULL, cp2, trace_loaded_objects);
  870. if (!tpnt1) {
  871. # ifdef __LDSO_LDD_SUPPORT__
  872. if (trace_loaded_objects || _dl_trace_prelink)
  873. _dl_dprintf(1, "\t%s => not found\n", cp2);
  874. else
  875. # endif
  876. {
  877. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, cp2);
  878. _dl_exit(15);
  879. }
  880. } else {
  881. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  882. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  883. # ifdef __LDSO_LDD_SUPPORT__
  884. if (trace_loaded_objects && !_dl_trace_prelink &&
  885. !(tpnt1->init_flag & DL_OPENED2)) {
  886. _dl_dprintf(1, "\t%s => %s (%x)\n",
  887. cp2, tpnt1->libname,
  888. DL_LOADADDR_BASE(tpnt1->loadaddr));
  889. }
  890. # endif
  891. }
  892. /* find start of next library */
  893. *cp = c;
  894. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  895. /*nada */ ;
  896. }
  897. _dl_munmap(preload, st.st_size + 1);
  898. } while (0);
  899. #endif /* __LDSO_PRELOAD_FILE_SUPPORT__ */
  900. nlist = 0;
  901. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  902. ElfW(Dyn) *this_dpnt;
  903. nlist++;
  904. for (this_dpnt = (ElfW(Dyn) *) tcurr->dynamic_addr; this_dpnt->d_tag; this_dpnt++) {
  905. if (this_dpnt->d_tag == DT_NEEDED) {
  906. char *name;
  907. struct init_fini_list *tmp;
  908. lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] + this_dpnt->d_un.d_val);
  909. name = _dl_get_last_path_component(lpntstr);
  910. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", lpntstr, _dl_progname);
  911. if (_dl_strcmp(name, UCLIBC_LDSO) == 0) {
  912. if (!ldso_tpnt) {
  913. /* Insert the ld.so only once */
  914. ldso_tpnt = add_ldso(tpnt, load_addr,
  915. ldso_mapaddr, auxvt, rpnt);
  916. } else {
  917. ldso_tpnt->init_flag |= DL_OPENED2;
  918. }
  919. ldso_tpnt->usage_count++;
  920. tpnt1 = ldso_tpnt;
  921. } else
  922. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, trace_loaded_objects);
  923. if (!tpnt1) {
  924. #ifdef __LDSO_LDD_SUPPORT__
  925. if (trace_loaded_objects || _dl_trace_prelink) {
  926. _dl_dprintf(1, "\t%s => not found\n", lpntstr);
  927. continue;
  928. } else
  929. #endif
  930. {
  931. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, lpntstr);
  932. _dl_exit(16);
  933. }
  934. }
  935. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  936. tmp->tpnt = tpnt1;
  937. tmp->next = tcurr->init_fini;
  938. tcurr->init_fini = tmp;
  939. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  940. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  941. #ifdef __LDSO_LDD_SUPPORT__
  942. if (trace_loaded_objects && !_dl_trace_prelink &&
  943. !(tpnt1->init_flag & DL_OPENED2)) {
  944. _dl_dprintf(1, "\t%s => %s (%x)\n",
  945. lpntstr, tpnt1->libname,
  946. DL_LOADADDR_BASE(tpnt1->loadaddr));
  947. }
  948. #endif
  949. }
  950. }
  951. }
  952. _dl_unmap_cache();
  953. /* Keep track of the number of elements in the global scope */
  954. nscope_elem = nlist;
  955. if (_dl_loaded_modules->libtype == elf_executable) {
  956. --nlist; /* Exclude the application. */
  957. tcurr = _dl_loaded_modules->next;
  958. } else
  959. tcurr = _dl_loaded_modules;
  960. init_fini_list = _dl_malloc(nlist * sizeof(struct elf_resolve *));
  961. i = 0;
  962. for (; tcurr; tcurr = tcurr->next)
  963. init_fini_list[i++] = tcurr;
  964. /* Sort the INIT/FINI list in dependency order. */
  965. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  966. unsigned int j, k;
  967. for (j = 0; init_fini_list[j] != tcurr; ++j)
  968. /* Empty */;
  969. for (k = j + 1; k < nlist; ++k) {
  970. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  971. for (; runp; runp = runp->next) {
  972. if (runp->tpnt == tcurr) {
  973. struct elf_resolve *here = init_fini_list[k];
  974. _dl_if_debug_dprint("Move %s from pos %d to %d in INIT/FINI list\n", here->libname, k, j);
  975. for (i = (k - j); i; --i)
  976. init_fini_list[i+j] = init_fini_list[i+j-1];
  977. init_fini_list[j] = here;
  978. ++j;
  979. break;
  980. }
  981. }
  982. }
  983. }
  984. #ifdef __SUPPORT_LD_DEBUG__
  985. if (_dl_debug) {
  986. _dl_dprintf(_dl_debug_file, "\nINIT/FINI order and dependencies:\n");
  987. for (i = 0; i < nlist; i++) {
  988. struct init_fini_list *tmp;
  989. _dl_dprintf(_dl_debug_file, "lib: %s has deps:\n",
  990. init_fini_list[i]->libname);
  991. tmp = init_fini_list[i]->init_fini;
  992. for (; tmp; tmp = tmp->next)
  993. _dl_dprintf(_dl_debug_file, " %s ", tmp->tpnt->libname);
  994. _dl_dprintf(_dl_debug_file, "\n");
  995. }
  996. }
  997. #endif
  998. /*
  999. * If the program interpreter is not in the module chain, add it.
  1000. * This will be required for dlopen to be able to access the internal
  1001. * functions in the dynamic linker and to relocate the interpreter
  1002. * again once all libs are loaded.
  1003. */
  1004. if (!ldso_tpnt) {
  1005. tpnt = add_ldso(tpnt, load_addr, ldso_mapaddr, auxvt, rpnt);
  1006. tpnt->usage_count++;
  1007. nscope_elem++;
  1008. } else
  1009. tpnt = ldso_tpnt;
  1010. #ifdef RERELOCATE_LDSO
  1011. /* Only rerelocate functions for now. */
  1012. tpnt->init_flag = RELOCS_DONE;
  1013. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT]);
  1014. # ifdef ALLOW_ZERO_PLTGOT
  1015. if (tpnt->dynamic_info[DT_PLTGOT])
  1016. # endif
  1017. INIT_GOT(lpnt, tpnt);
  1018. #else
  1019. tpnt->init_flag = RELOCS_DONE | JMP_RELOCS_DONE;
  1020. #endif
  1021. tpnt = NULL;
  1022. /*
  1023. * Allocate the global scope array.
  1024. */
  1025. scope_elem_list = (struct elf_resolve **) _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1026. for (i = 0, tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next)
  1027. scope_elem_list[i++] = tcurr;
  1028. _dl_loaded_modules->symbol_scope.r_list = scope_elem_list;
  1029. _dl_loaded_modules->symbol_scope.r_nlist = nscope_elem;
  1030. /*
  1031. * The symbol scope of the application, that is the first entry of the
  1032. * _dl_loaded_modules list, is just the global scope to be used for the
  1033. * symbol lookup.
  1034. */
  1035. global_scope = &_dl_loaded_modules->symbol_scope;
  1036. /* Build the local scope for each loaded modules. */
  1037. local_scope = _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1038. i = 1;
  1039. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  1040. unsigned int k;
  1041. cnt = _dl_build_local_scope(local_scope, scope_elem_list[i++]);
  1042. tcurr->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  1043. tcurr->symbol_scope.r_nlist = cnt;
  1044. _dl_memcpy (tcurr->symbol_scope.r_list, local_scope, cnt * sizeof (struct elf_resolve *));
  1045. /* Restoring the init_flag.*/
  1046. for (k = 1; k < nscope_elem; k++)
  1047. scope_elem_list[k]->init_flag &= ~DL_RESERVED;
  1048. }
  1049. _dl_free(local_scope);
  1050. #ifdef __LDSO_LDD_SUPPORT__
  1051. /* Exit if LD_TRACE_LOADED_OBJECTS is on. */
  1052. if (trace_loaded_objects && !_dl_trace_prelink)
  1053. _dl_exit(0);
  1054. #endif
  1055. #if defined(USE_TLS) && USE_TLS
  1056. /* We do not initialize any of the TLS functionality unless any of the
  1057. * initial modules uses TLS. This makes dynamic loading of modules with
  1058. * TLS impossible, but to support it requires either eagerly doing setup
  1059. * now or lazily doing it later. Doing it now makes us incompatible with
  1060. * an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
  1061. * used. Trying to do it lazily is too hairy to try when there could be
  1062. * multiple threads (from a non-TLS-using libpthread). */
  1063. bool was_tls_init_tp_called = tls_init_tp_called;
  1064. if (tcbp == NULL) {
  1065. _dl_debug_early("Calling init_tls()!\n");
  1066. tcbp = init_tls ();
  1067. }
  1068. #endif
  1069. #ifdef __UCLIBC_HAS_SSP__
  1070. _dl_debug_early("Setting up SSP guards\n");
  1071. /* Set up the stack checker's canary. */
  1072. stack_chk_guard = _dl_setup_stack_chk_guard ();
  1073. # ifdef THREAD_SET_STACK_GUARD
  1074. THREAD_SET_STACK_GUARD (stack_chk_guard);
  1075. # else
  1076. __stack_chk_guard = stack_chk_guard;
  1077. # endif
  1078. #endif
  1079. #ifdef __LDSO_PRELINK_SUPPORT__
  1080. if (_dl_trace_prelink) {
  1081. unsigned int nscope_trace = ldso_tpnt ? nscope_elem : (nscope_elem - 1);
  1082. for (i = 0; i < nscope_trace; i++)
  1083. trace_objects(scope_elem_list[i],
  1084. _dl_get_last_path_component(scope_elem_list[i]->libname));
  1085. if (_dl_verbose)
  1086. /* Warn about undefined symbols. */
  1087. if (_dl_symbol_tables)
  1088. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1089. _dl_exit(-1);
  1090. _dl_exit(0);
  1091. }
  1092. if (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX]) {
  1093. ElfW(Lib) *liblist, *liblistend;
  1094. struct elf_resolve **r_list, **r_listend, *l;
  1095. const char *strtab = (const char *)_dl_loaded_modules->dynamic_info[DT_STRTAB];
  1096. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX] != 0);
  1097. liblist = (ElfW(Lib) *) _dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX];
  1098. liblistend = (ElfW(Lib) *)
  1099. ((char *) liblist + _dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX]);
  1100. r_list = _dl_loaded_modules->symbol_scope.r_list;
  1101. r_listend = r_list + nscope_elem;
  1102. for (; r_list < r_listend && liblist < liblistend; r_list++) {
  1103. l = *r_list;
  1104. if (l == _dl_loaded_modules)
  1105. continue;
  1106. /* If the library is not mapped where it should, fail. */
  1107. if (l->loadaddr)
  1108. break;
  1109. /* Next, check if checksum matches. */
  1110. if (l->dynamic_info[DT_CHECKSUM_IDX] == 0 ||
  1111. l->dynamic_info[DT_CHECKSUM_IDX] != liblist->l_checksum)
  1112. break;
  1113. if (l->dynamic_info[DT_GNU_PRELINKED_IDX] == 0 ||
  1114. (l->dynamic_info[DT_GNU_PRELINKED_IDX] != liblist->l_time_stamp))
  1115. break;
  1116. if (_dl_strcmp(strtab + liblist->l_name, _dl_get_last_path_component(l->libname)) != 0)
  1117. break;
  1118. ++liblist;
  1119. }
  1120. if (r_list == r_listend && liblist == liblistend)
  1121. prelinked = true;
  1122. }
  1123. _dl_debug_early ("prelink checking: %s\n", prelinked ? "ok" : "failed");
  1124. if (prelinked) {
  1125. if (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX]) {
  1126. ELF_RELOC *conflict;
  1127. unsigned long conflict_size;
  1128. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX] != 0);
  1129. conflict = (ELF_RELOC *) _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX];
  1130. conflict_size = _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX];
  1131. _dl_parse_relocation_information(_dl_symbol_tables, global_scope,
  1132. (unsigned long) conflict, conflict_size);
  1133. }
  1134. /* Mark all the objects so we know they have been already relocated. */
  1135. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1136. tpnt->init_flag |= RELOCS_DONE;
  1137. if (tpnt->relro_size)
  1138. _dl_protect_relro (tpnt);
  1139. }
  1140. } else
  1141. #endif
  1142. {
  1143. _dl_debug_early("Beginning relocation fixups\n");
  1144. #ifdef __mips__
  1145. /*
  1146. * Relocation of the GOT entries for MIPS have to be done
  1147. * after all the libraries have been loaded.
  1148. */
  1149. _dl_perform_mips_global_got_relocations(_dl_loaded_modules, !unlazy);
  1150. #endif
  1151. /*
  1152. * OK, now all of the kids are tucked into bed in their proper
  1153. * addresses. Now we go through and look for REL and RELA records that
  1154. * indicate fixups to the GOT tables. We need to do this in reverse
  1155. * order so that COPY directives work correctly.
  1156. */
  1157. if (_dl_symbol_tables)
  1158. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1159. _dl_exit(-1);
  1160. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1161. if (tpnt->relro_size)
  1162. _dl_protect_relro (tpnt);
  1163. }
  1164. } /* not prelinked */
  1165. #if defined(USE_TLS) && USE_TLS
  1166. if (!was_tls_init_tp_called && _dl_tls_max_dtv_idx > 0)
  1167. ++_dl_tls_generation;
  1168. _dl_debug_early("Calling _dl_allocate_tls_init()!\n");
  1169. /* Now that we have completed relocation, the initializer data
  1170. for the TLS blocks has its final values and we can copy them
  1171. into the main thread's TLS area, which we allocated above. */
  1172. _dl_allocate_tls_init (tcbp);
  1173. /* And finally install it for the main thread. If ld.so itself uses
  1174. TLS we know the thread pointer was initialized earlier. */
  1175. if (! tls_init_tp_called) {
  1176. const char *lossage = (char *) TLS_INIT_TP (tcbp, USE___THREAD);
  1177. if (__builtin_expect (lossage != NULL, 0)) {
  1178. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  1179. _dl_exit(30);
  1180. }
  1181. }
  1182. #endif /* USE_TLS */
  1183. /* OK, at this point things are pretty much ready to run. Now we need
  1184. * to touch up a few items that are required, and then we can let the
  1185. * user application have at it. Note that the dynamic linker itself
  1186. * is not guaranteed to be fully dynamicly linked if we are using
  1187. * ld.so.1, so we have to look up each symbol individually.
  1188. */
  1189. _dl_envp = (unsigned long *) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "__environ", global_scope, NULL, 0, NULL);
  1190. if (_dl_envp)
  1191. *_dl_envp = (unsigned long) envp;
  1192. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  1193. {
  1194. unsigned int j;
  1195. ElfW(Phdr) *myppnt;
  1196. /* We had to set the protections of all pages to R/W for
  1197. * dynamic linking. Set text pages back to R/O.
  1198. */
  1199. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1200. for (myppnt = tpnt->ppnt, j = 0; j < tpnt->n_phent; j++, myppnt++) {
  1201. if (myppnt->p_type == PT_LOAD && !(myppnt->p_flags & PF_W) && tpnt->dynamic_info[DT_TEXTREL]) {
  1202. _dl_mprotect((void *) (DL_RELOC_ADDR(tpnt->loadaddr, myppnt->p_vaddr) & PAGE_ALIGN),
  1203. (myppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) myppnt->p_filesz, LXFLAGS(myppnt->p_flags));
  1204. }
  1205. }
  1206. }
  1207. }
  1208. #endif
  1209. /* Notify the debugger we have added some objects. */
  1210. _dl_debug_addr->r_state = RT_ADD;
  1211. _dl_debug_state();
  1212. /* Run pre-initialization functions for the executable. */
  1213. _dl_run_array_forward(_dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAY],
  1214. _dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAYSZ],
  1215. _dl_loaded_modules->loadaddr);
  1216. /* Run initialization functions for loaded objects. For the
  1217. main executable, they will be run from __uClibc_main. */
  1218. for (i = nlist; i; --i) {
  1219. tpnt = init_fini_list[i-1];
  1220. tpnt->init_fini = NULL; /* Clear, since alloca was used */
  1221. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  1222. continue;
  1223. tpnt->init_flag |= INIT_FUNCS_CALLED;
  1224. if (tpnt->dynamic_info[DT_INIT]) {
  1225. void (*dl_elf_func) (void);
  1226. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  1227. _dl_if_debug_dprint("calling INIT: %s\n\n", tpnt->libname);
  1228. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  1229. }
  1230. _dl_run_init_array(tpnt);
  1231. }
  1232. /* Find the real malloc function and make ldso functions use that from now on */
  1233. _dl_malloc_function = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
  1234. global_scope, NULL, rtype_class, NULL);
  1235. #if defined(USE_TLS) && USE_TLS
  1236. /* Find the real functions and make ldso functions use them from now on */
  1237. _dl_calloc_function = (void* (*)(size_t, size_t)) (intptr_t)
  1238. _dl_find_hash(__C_SYMBOL_PREFIX__ "calloc", global_scope, NULL, rtype_class, NULL);
  1239. _dl_realloc_function = (void* (*)(void *, size_t)) (intptr_t)
  1240. _dl_find_hash(__C_SYMBOL_PREFIX__ "realloc", global_scope, NULL, rtype_class, NULL);
  1241. _dl_free_function = (void (*)(void *)) (intptr_t)
  1242. _dl_find_hash(__C_SYMBOL_PREFIX__ "free", global_scope, NULL, rtype_class, NULL);
  1243. _dl_memalign_function = (void* (*)(size_t, size_t)) (intptr_t)
  1244. _dl_find_hash(__C_SYMBOL_PREFIX__ "memalign", global_scope, NULL, rtype_class, NULL);
  1245. #endif
  1246. /* Notify the debugger that all objects are now mapped in. */
  1247. _dl_debug_addr->r_state = RT_CONSISTENT;
  1248. _dl_debug_state();
  1249. #ifdef __LDSO_STANDALONE_SUPPORT__
  1250. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val)
  1251. return (void *) app_tpnt->l_entry;
  1252. else
  1253. #endif
  1254. return (void *) auxvt[AT_ENTRY].a_un.a_val;
  1255. }
  1256. #include "dl-hash.c"
  1257. #include "dl-elf.c"