dl-startup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * dynamicly linked. This means that we cannot access any global variables or
  35. * 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 which is provided to us by
  58. * the kernel, and which includes information about the load address that the
  59. * program interpreter was loaded at, the number of sections, the address the
  60. * application was loaded at and so forth. Here this information is stored in
  61. * the array auxvt. For details see linux/fs/binfmt_elf.c where it calls
  62. * NEW_AUX_ENT() a bunch of time....
  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. /* Static declarations */
  94. int (*_dl_elf_main) (int, char **, char **);
  95. static void* __rtld_stack_end; /* Points to argc on stack, e.g *((long *)__rtld_stackend) == argc */
  96. strong_alias(__rtld_stack_end, __libc_stack_end) /* Exported version of __rtld_stack_end */
  97. /* When we enter this piece of code, the program stack looks like this:
  98. argc argument counter (integer)
  99. argv[0] program name (pointer)
  100. argv[1...N] program args (pointers)
  101. argv[argc-1] end of args (integer)
  102. NULL
  103. env[0...N] environment variables (pointers)
  104. NULL
  105. auxvt[0...N] Auxiliary Vector Table elements (mixed types)
  106. */
  107. DL_START(unsigned long args)
  108. {
  109. unsigned int argc;
  110. char **argv, **envp;
  111. DL_LOADADDR_TYPE load_addr;
  112. ElfW(Addr) got;
  113. unsigned long *aux_dat;
  114. ElfW(Ehdr) *header;
  115. struct elf_resolve tpnt_tmp;
  116. struct elf_resolve *tpnt = &tpnt_tmp;
  117. ElfW(auxv_t) auxvt[AT_EGID + 1];
  118. ElfW(Dyn) *dpnt;
  119. /* WARNING! -- we cannot make _any_ funtion calls until we have
  120. * taken care of fixing up our own relocations. Making static
  121. * inline calls is ok, but _no_ function calls. Not yet
  122. * anyways. */
  123. /* First obtain the information on the stack that tells us more about
  124. what binary is loaded, where it is loaded, etc, etc */
  125. GET_ARGV(aux_dat, args);
  126. argc = *(aux_dat - 1);
  127. argv = (char **) aux_dat;
  128. aux_dat += argc; /* Skip over the argv pointers */
  129. aux_dat++; /* Skip over NULL at end of argv */
  130. envp = (char **) aux_dat;
  131. #ifndef NO_EARLY_SEND_STDERR
  132. SEND_EARLY_STDERR_DEBUG("argc=");
  133. SEND_NUMBER_STDERR_DEBUG(argc, 0);
  134. SEND_EARLY_STDERR_DEBUG(" argv=");
  135. SEND_ADDRESS_STDERR_DEBUG(argv, 0);
  136. SEND_EARLY_STDERR_DEBUG(" envp=");
  137. SEND_ADDRESS_STDERR_DEBUG(envp, 1);
  138. #endif
  139. while (*aux_dat)
  140. aux_dat++; /* Skip over the envp pointers */
  141. aux_dat++; /* Skip over NULL at end of envp */
  142. /* Place -1 here as a checkpoint. We later check if it was changed
  143. * when we read in the auxvt */
  144. auxvt[AT_UID].a_type = -1;
  145. /* The junk on the stack immediately following the environment is
  146. * the Auxiliary Vector Table. Read out the elements of the auxvt,
  147. * sort and store them in auxvt for later use. */
  148. while (*aux_dat) {
  149. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  150. if (auxv_entry->a_type <= AT_EGID) {
  151. _dl_memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  152. }
  153. aux_dat += 2;
  154. }
  155. /* locate the ELF header. We need this done as soon as possible
  156. * (esp since SEND_STDERR() needs this on some platforms... */
  157. if (!auxvt[AT_BASE].a_un.a_val)
  158. auxvt[AT_BASE].a_un.a_val = elf_machine_load_address();
  159. DL_INIT_LOADADDR_BOOT(load_addr, auxvt[AT_BASE].a_un.a_val);
  160. header = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_val;
  161. /* Check the ELF header to make sure everything looks ok. */
  162. if (!header || header->e_ident[EI_CLASS] != ELF_CLASS ||
  163. header->e_ident[EI_VERSION] != EV_CURRENT
  164. /* Do not use an inline _dl_strncmp here or some arches
  165. * will blow chunks, i.e. those that need to relocate all
  166. * string constants... */
  167. || header->e_ident[EI_MAG0] != ELFMAG0
  168. || header->e_ident[EI_MAG1] != ELFMAG1
  169. || header->e_ident[EI_MAG2] != ELFMAG2
  170. || header->e_ident[EI_MAG3] != ELFMAG3)
  171. {
  172. SEND_EARLY_STDERR("Invalid ELF header\n");
  173. _dl_exit(0);
  174. }
  175. SEND_EARLY_STDERR_DEBUG("ELF header=");
  176. SEND_ADDRESS_STDERR_DEBUG(DL_LOADADDR_BASE(load_addr), 1);
  177. /* Locate the global offset table. Since this code must be PIC
  178. * we can take advantage of the magic offset register, if we
  179. * happen to know what that is for this architecture. If not,
  180. * we can always read stuff out of the ELF file to find it... */
  181. got = elf_machine_dynamic();
  182. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(load_addr, got);
  183. SEND_EARLY_STDERR_DEBUG("First Dynamic section entry=");
  184. SEND_ADDRESS_STDERR_DEBUG(dpnt, 1);
  185. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  186. tpnt->loadaddr = load_addr;
  187. /* OK, that was easy. Next scan the DYNAMIC section of the image.
  188. We are only doing ourself right now - we will have to do the rest later */
  189. SEND_EARLY_STDERR_DEBUG("Scanning DYNAMIC section\n");
  190. tpnt->dynamic_addr = dpnt;
  191. #if defined(NO_FUNCS_BEFORE_BOOTSTRAP)
  192. /* Some architectures cannot call functions here, must inline */
  193. __dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
  194. #else
  195. _dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
  196. #endif
  197. SEND_EARLY_STDERR_DEBUG("Done scanning DYNAMIC section\n");
  198. #if defined(PERFORM_BOOTSTRAP_GOT)
  199. SEND_EARLY_STDERR_DEBUG("About to do specific GOT bootstrap\n");
  200. /* some arches (like MIPS) we have to tweak the GOT before relocations */
  201. PERFORM_BOOTSTRAP_GOT(tpnt);
  202. #else
  203. /* OK, now do the relocations. We do not do a lazy binding here, so
  204. that once we are done, we have considerably more flexibility. */
  205. SEND_EARLY_STDERR_DEBUG("About to do library loader relocations\n");
  206. {
  207. int goof, indx;
  208. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  209. # define INDX_MAX 1
  210. #else
  211. # define INDX_MAX 2
  212. #endif
  213. goof = 0;
  214. for (indx = 0; indx < INDX_MAX; indx++) {
  215. unsigned int i;
  216. unsigned long *reloc_addr;
  217. unsigned long symbol_addr;
  218. int symtab_index;
  219. ElfW(Sym) *sym;
  220. ELF_RELOC *rpnt;
  221. unsigned long rel_addr, rel_size;
  222. ElfW(Word) relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  223. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] :
  224. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]);
  225. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] :
  226. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
  227. if (!rel_addr)
  228. continue;
  229. /* Now parse the relocation information */
  230. /* Since ldso is linked with -Bsymbolic, all relocs will be RELATIVE(for those archs that have
  231. RELATIVE relocs) which means that the for(..) loop below has nothing to do and can be deleted.
  232. Possibly one should add a HAVE_RELATIVE_RELOCS directive and #ifdef away some code. */
  233. if (!indx && relative_count) {
  234. rel_size -= relative_count * sizeof(ELF_RELOC);
  235. elf_machine_relative(load_addr, rel_addr, relative_count);
  236. rel_addr += relative_count * sizeof(ELF_RELOC);
  237. }
  238. rpnt = (ELF_RELOC *) rel_addr;
  239. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  240. reloc_addr = (unsigned long *) DL_RELOC_ADDR(load_addr, (unsigned long)rpnt->r_offset);
  241. symtab_index = ELF_R_SYM(rpnt->r_info);
  242. symbol_addr = 0;
  243. sym = NULL;
  244. if (symtab_index) {
  245. char *strtab;
  246. ElfW(Sym) *symtab;
  247. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  248. strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
  249. sym = &symtab[symtab_index];
  250. symbol_addr = (unsigned long) DL_RELOC_ADDR(load_addr, sym->st_value);
  251. #ifndef EARLY_STDERR_SPECIAL
  252. SEND_STDERR_DEBUG("relocating symbol: ");
  253. SEND_STDERR_DEBUG(strtab + sym->st_name);
  254. SEND_STDERR_DEBUG("\n");
  255. #endif
  256. } else
  257. SEND_STDERR_DEBUG("relocating unknown symbol\n");
  258. /* Use this machine-specific macro to perform the actual relocation. */
  259. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr, sym);
  260. }
  261. }
  262. if (goof) {
  263. _dl_exit(14);
  264. }
  265. }
  266. #endif
  267. /* Wahoo!!! */
  268. SEND_STDERR_DEBUG("Done relocating ldso; we can now use globals and make function calls!\n");
  269. /* Now we have done the mandatory linking of some things. We are now
  270. free to start using global variables, since these things have all been
  271. fixed up by now. Still no function calls outside of this library,
  272. since the dynamic resolver is not yet ready. */
  273. __rtld_stack_end = (void *)(argv - 1);
  274. _dl_get_ready_to_run(tpnt, load_addr, auxvt, envp, argv);
  275. /* Transfer control to the application. */
  276. SEND_STDERR_DEBUG("transfering control to application @ ");
  277. _dl_elf_main = (int (*)(int, char **, char **)) auxvt[AT_ENTRY].a_un.a_val;
  278. SEND_ADDRESS_STDERR_DEBUG(_dl_elf_main, 1);
  279. #ifndef START
  280. return _dl_elf_main;
  281. #else
  282. START();
  283. #endif
  284. }