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