dl-startup.c 14 KB

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