dl-startup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /*
  32. * The main trick with this program is that initially, we ourselves are not
  33. * dynamically linked. This means that we cannot access any global variables
  34. * or call any functions. No globals initially, since the Global Offset Table
  35. * (GOT) is initialized by the linker assuming a virtual address of 0, and no
  36. * function calls initially since the Procedure Linkage Table (PLT) is not yet
  37. * initialized.
  38. *
  39. * There are additional initial restrictions - we cannot use large switch
  40. * statements, since the compiler generates tables of addresses and jumps
  41. * through them. We cannot use normal syscall stubs, because these all
  42. * reference the errno global variable which is not yet initialized. We _can_
  43. * use all of the local stack variables that we want. We _can_ use inline
  44. * functions, because these do not transfer control to a new address, but they
  45. * must be static so that they are not exported from the modules.
  46. *
  47. * Life is further complicated by the fact that initially we do not want to do
  48. * a complete dynamic linking. We want to allow the user to supply new
  49. * functions to override symbols (i.e. weak symbols and/or LD_PRELOAD). So
  50. * initially, we only perform relocations for variables that start with "_dl_"
  51. * since ANSI specifies that the user is not supposed to redefine any of these
  52. * variables.
  53. *
  54. * Fortunately, the linker itself leaves a few clues lying around, and when the
  55. * kernel starts the image, there are a few further clues. First of all, there
  56. * is Auxiliary Vector Table information sitting on the stack which is provided
  57. * to us by the kernel, and which includes information about the address
  58. * that the program interpreter was loaded at, the number of sections, the
  59. * address the application was loaded at, and so forth. Here this information
  60. * is stored in the array auxvt. For details see linux/fs/binfmt_elf.c where
  61. * it calls NEW_AUX_ENT() a bunch of times....
  62. *
  63. * Next, we need to find the GOT. On most arches there is a register pointing
  64. * to the GOT, but just in case (and for new ports) I've added some (slow) C
  65. * code to locate the GOT for you.
  66. *
  67. * This code was originally written for SVr4, and there the kernel would load
  68. * all text pages R/O, so they needed to call mprotect a zillion times to mark
  69. * all text pages as writable so dynamic linking would succeed. Then when they
  70. * were done, they would change the protections for all the pages back again.
  71. * Well, under Linux everything is loaded writable (since Linux does copy on
  72. * write anyways) so all the mprotect stuff has been disabled.
  73. *
  74. * Initially, we do not have access to _dl_malloc since we can't yet make
  75. * function calls, so we mmap one page to use as scratch space. Later on, when
  76. * we can call _dl_malloc we reuse this this memory. This is also beneficial,
  77. * since we do not want to use the same memory pool as malloc anyway - esp if
  78. * the user redefines malloc to do something funky.
  79. *
  80. * Our first task is to perform a minimal linking so that we can call other
  81. * portions of the dynamic linker. Once we have done this, we then build the
  82. * list of modules that the application requires, using LD_LIBRARY_PATH if this
  83. * is not a suid program (/usr/lib otherwise). Once this is done, we can do
  84. * the dynamic linking as required, and we must omit the things we did to get
  85. * the dynamic linker up and running in the first place. After we have done
  86. * this, we just have a few housekeeping chores and we can transfer control to
  87. * the user's application.
  88. */
  89. #include "ldso.h"
  90. /* Pull in all the arch specific stuff */
  91. #include "dl-startup.h"
  92. #ifdef __LDSO_PRELINK_SUPPORT__
  93. /* This is defined by the linker script. */
  94. extern ElfW(Addr) _begin[] attribute_hidden;
  95. #endif
  96. #ifdef LDSO_NEED_DPNT
  97. ElfW(Dyn) *_dl_saved_dpnt = 0;
  98. #endif
  99. /* Static declarations */
  100. static int (*_dl_elf_main) (int, char **, char **);
  101. static void* __rtld_stack_end; /* Points to argc on stack, e.g *((long *)__rtld_stackend) == argc */
  102. strong_alias(__rtld_stack_end, __libc_stack_end) /* Exported version of __rtld_stack_end */
  103. /* When we enter this piece of code, the program stack looks like this:
  104. argc argument counter (integer)
  105. argv[0] program name (pointer)
  106. argv[1..argc-1] program args (pointers)
  107. NULL
  108. env[0...N] environment variables (pointers)
  109. NULL
  110. auxvt[0...N] Auxiliary Vector Table elements (mixed types)
  111. */
  112. DL_START(unsigned long args)
  113. {
  114. unsigned int argc;
  115. char **argv, **envp;
  116. DL_LOADADDR_TYPE load_addr;
  117. ElfW(Addr) got;
  118. unsigned long *aux_dat;
  119. ElfW(Ehdr) *header;
  120. struct elf_resolve tpnt_tmp;
  121. struct elf_resolve *tpnt = &tpnt_tmp;
  122. ElfW(auxv_t) auxvt[AT_EGID + 1];
  123. ElfW(Dyn) *dpnt;
  124. uint32_t *p32;
  125. /* WARNING! -- we cannot make _any_ function calls until we have
  126. * taken care of fixing up our own relocations. Making static
  127. * inline calls is ok, but _no_ function calls. Not yet
  128. * anyways. */
  129. /* First obtain the information on the stack that tells us more about
  130. what binary is loaded, where it is loaded, etc, etc */
  131. GET_ARGV(aux_dat, args);
  132. argc = aux_dat[-1];
  133. argv = (char **) aux_dat;
  134. aux_dat += argc; /* Skip over the argv pointers */
  135. aux_dat++; /* Skip over NULL at end of argv */
  136. envp = (char **) aux_dat;
  137. #if !defined(NO_EARLY_SEND_STDERR)
  138. SEND_EARLY_STDERR_DEBUG("argc=");
  139. SEND_NUMBER_STDERR_DEBUG(argc, 0);
  140. SEND_EARLY_STDERR_DEBUG(" argv=");
  141. SEND_ADDRESS_STDERR_DEBUG(argv, 0);
  142. SEND_EARLY_STDERR_DEBUG(" envp=");
  143. SEND_ADDRESS_STDERR_DEBUG(envp, 1);
  144. #endif
  145. while (*aux_dat)
  146. aux_dat++; /* Skip over the envp pointers */
  147. aux_dat++; /* Skip over NULL at end of envp */
  148. /* Place -1 here as a checkpoint. We later check if it was changed
  149. * when we read in the auxvt */
  150. auxvt[AT_UID].a_type = -1;
  151. /* The junk on the stack immediately following the environment is
  152. * the Auxiliary Vector Table. Read out the elements of the auxvt,
  153. * sort and store them in auxvt for later use. */
  154. while (*aux_dat) {
  155. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  156. if (auxv_entry->a_type <= AT_EGID) {
  157. _dl_memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  158. }
  159. aux_dat += 2;
  160. }
  161. /*
  162. * Locate the dynamic linker ELF header. We need this done as soon as
  163. * possible (esp since SEND_STDERR() needs this on some platforms...
  164. */
  165. #ifdef __LDSO_PRELINK_SUPPORT__
  166. /*
  167. * The `_begin' symbol created by the linker script points to ld.so ELF
  168. * We use it if the kernel is not passing a valid address through the auxvt.
  169. */
  170. if (!auxvt[AT_BASE].a_un.a_val)
  171. auxvt[AT_BASE].a_un.a_val = (ElfW(Addr)) &_begin;
  172. /* Note: if the dynamic linker itself is prelinked, the load_addr is 0 */
  173. DL_INIT_LOADADDR_BOOT(load_addr, elf_machine_load_address());
  174. #else
  175. if (!auxvt[AT_BASE].a_un.a_val)
  176. auxvt[AT_BASE].a_un.a_val = elf_machine_load_address();
  177. DL_INIT_LOADADDR_BOOT(load_addr, auxvt[AT_BASE].a_un.a_val);
  178. #endif
  179. header = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_val;
  180. /* Check the ELF header to make sure everything looks ok. */
  181. if (!header || header->e_ident[EI_CLASS] != ELF_CLASS ||
  182. header->e_ident[EI_VERSION] != EV_CURRENT
  183. /* Do not use an inline _dl_strncmp here or some arches
  184. * will blow chunks, i.e. those that need to relocate all
  185. * string constants... */
  186. || *(p32 = (uint32_t*)&header->e_ident) != ELFMAG_U32
  187. ) {
  188. SEND_EARLY_STDERR("Invalid ELF header\n");
  189. _dl_exit(0);
  190. }
  191. SEND_EARLY_STDERR_DEBUG("ELF header=");
  192. SEND_ADDRESS_STDERR_DEBUG(
  193. DL_LOADADDR_BASE(DL_GET_RUN_ADDR(load_addr, header)), 1);
  194. /* Locate the global offset table. Since this code must be PIC
  195. * we can take advantage of the magic offset register, if we
  196. * happen to know what that is for this architecture. If not,
  197. * we can always read stuff out of the ELF file to find it... */
  198. DL_BOOT_COMPUTE_GOT(got);
  199. /* Now, finally, fix up the location of the dynamic stuff */
  200. DL_BOOT_COMPUTE_DYN(dpnt, got, (DL_LOADADDR_TYPE)header);
  201. SEND_EARLY_STDERR_DEBUG("First Dynamic section entry=");
  202. SEND_ADDRESS_STDERR_DEBUG(dpnt, 1);
  203. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  204. tpnt->loadaddr = load_addr;
  205. /* OK, that was easy. Next scan the DYNAMIC section of the image.
  206. We are only doing ourself right now - we will have to do the rest later */
  207. SEND_EARLY_STDERR_DEBUG("Scanning DYNAMIC section\n");
  208. tpnt->dynamic_addr = dpnt;
  209. #if defined(NO_FUNCS_BEFORE_BOOTSTRAP)
  210. /* Some architectures cannot call functions here, must inline */
  211. __dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
  212. #else
  213. _dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
  214. #endif
  215. /*
  216. * BIG ASSUMPTION: We assume that the dynamic loader does not
  217. * have any TLS data itself. If this ever occurs
  218. * more work than what is done below for the
  219. * loader will have to happen.
  220. */
  221. #if defined(USE_TLS) && USE_TLS
  222. /* This was done by _dl_memset above. */
  223. /* tpnt->l_tls_modid = 0; */
  224. # if NO_TLS_OFFSET != 0
  225. tpnt->l_tls_offset = NO_TLS_OFFSET;
  226. # endif
  227. #endif
  228. SEND_EARLY_STDERR_DEBUG("Done scanning DYNAMIC section\n");
  229. #if defined(PERFORM_BOOTSTRAP_GOT)
  230. SEND_EARLY_STDERR_DEBUG("About to do specific GOT bootstrap\n");
  231. /* some arches (like MIPS) we have to tweak the GOT before relocations */
  232. PERFORM_BOOTSTRAP_GOT(tpnt);
  233. #endif
  234. #if !defined(PERFORM_BOOTSTRAP_GOT) || defined(__avr32__) || defined(__mips__)
  235. /* OK, now do the relocations. We do not do a lazy binding here, so
  236. that once we are done, we have considerably more flexibility. */
  237. SEND_EARLY_STDERR_DEBUG("About to do library loader relocations\n");
  238. {
  239. int indx;
  240. #if defined(ELF_MACHINE_PLTREL_OVERLAP)
  241. # define INDX_MAX 1
  242. #else
  243. # define INDX_MAX 2
  244. #endif
  245. for (indx = 0; indx < INDX_MAX; indx++) {
  246. unsigned long rel_addr, rel_size;
  247. ElfW(Word) relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  248. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] :
  249. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]);
  250. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] :
  251. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
  252. if (!rel_addr)
  253. continue;
  254. if (!indx && relative_count) {
  255. rel_size -= relative_count * sizeof(ELF_RELOC);
  256. #ifdef __LDSO_PRELINK_SUPPORT__
  257. if (load_addr || !tpnt->dynamic_info[DT_GNU_PRELINKED_IDX])
  258. #endif
  259. elf_machine_relative(load_addr, rel_addr, relative_count);
  260. rel_addr += relative_count * sizeof(ELF_RELOC);
  261. }
  262. /*
  263. * Since ldso is linked with -Bsymbolic, all relocs should be RELATIVE. All archs
  264. * that need bootstrap relocations need to define ARCH_NEEDS_BOOTSTRAP_RELOCS.
  265. */
  266. #ifdef ARCH_NEEDS_BOOTSTRAP_RELOCS
  267. {
  268. ELF_RELOC *rpnt;
  269. unsigned int i;
  270. ElfW(Sym) *sym;
  271. unsigned long symbol_addr;
  272. int symtab_index;
  273. unsigned long *reloc_addr;
  274. /* Now parse the relocation information */
  275. rpnt = (ELF_RELOC *) rel_addr;
  276. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  277. reloc_addr = (unsigned long *) DL_RELOC_ADDR(load_addr, (unsigned long)rpnt->r_offset);
  278. symtab_index = ELF_R_SYM(rpnt->r_info);
  279. symbol_addr = 0;
  280. sym = NULL;
  281. if (symtab_index) {
  282. char *strtab;
  283. ElfW(Sym) *symtab;
  284. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  285. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  286. sym = &symtab[symtab_index];
  287. symbol_addr = (unsigned long) DL_RELOC_ADDR(load_addr, sym->st_value);
  288. #if !defined(EARLY_STDERR_SPECIAL)
  289. SEND_STDERR_DEBUG("relocating symbol: ");
  290. SEND_STDERR_DEBUG(strtab + sym->st_name);
  291. SEND_STDERR_DEBUG("\n");
  292. #endif
  293. }
  294. /* Use this machine-specific macro to perform the actual relocation. */
  295. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr, sym);
  296. }
  297. }
  298. #else /* ARCH_NEEDS_BOOTSTRAP_RELOCS */
  299. if (rel_size) {
  300. SEND_EARLY_STDERR("Cannot continue, found non relative relocs during the bootstrap.\n");
  301. _dl_exit(14);
  302. }
  303. #endif
  304. }
  305. }
  306. #endif
  307. SEND_STDERR_DEBUG("Done relocating ldso; we can now use globals and make function calls!\n");
  308. /* Now we have done the mandatory linking of some things. We are now
  309. free to start using global variables, since these things have all been
  310. fixed up by now. Still no function calls outside of this library,
  311. since the dynamic resolver is not yet ready. */
  312. #ifdef LDSO_NEED_DPNT
  313. /*XXX TODO this crashes on nios2: it translates to
  314. * [r5] := (value of the local variable dpnt)
  315. * but r5 is a NULL pointer at this place, which was
  316. * retrieved from the GOT a few instructions further above.
  317. */
  318. _dl_saved_dpnt = dpnt;
  319. #endif
  320. __rtld_stack_end = (void *)(argv - 1);
  321. _dl_elf_main = (int (*)(int, char **, char **))
  322. _dl_get_ready_to_run(tpnt, load_addr, auxvt, envp, argv
  323. DL_GET_READY_TO_RUN_EXTRA_ARGS);
  324. /* Transfer control to the application. */
  325. SEND_STDERR_DEBUG("transfering control to application @ ");
  326. SEND_ADDRESS_STDERR_DEBUG(_dl_elf_main, 1);
  327. #if !defined(START)
  328. return _dl_elf_main;
  329. #else
  330. START();
  331. #endif
  332. }