Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: chrome/nacl/nacl_helper_bootstrap_linux.c

Issue 8524015: Make nacl_helper_bootstrap load a PIE normally, not the dynamic linker directly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be 2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. 3 * found in the LICENSE file.
4 * 4 *
5 * This is a standalone program that loads and runs the dynamic linker. 5 * This is a standalone program that loads and runs the dynamic linker.
6 * This program itself must be linked statically. To keep it small, it's 6 * This program itself must be linked statically. To keep it small, it's
7 * written to avoid all dependencies on libc and standard startup code. 7 * written to avoid all dependencies on libc and standard startup code.
8 * Hence, this should be linked using -nostartfiles. It must be compiled 8 * Hence, this should be linked using -nostartfiles. It must be compiled
9 * with -fno-stack-protector to ensure the compiler won't emit code that 9 * with -fno-stack-protector to ensure the compiler won't emit code that
10 * presumes some special setup has been done. 10 * presumes some special setup has been done.
11 * 11 *
12 * On ARM, the compiler will emit calls to some libc functions, so we 12 * On ARM, the compiler will emit calls to some libc functions, so we
13 * cannot link with -nostdlib. The functions it does use (memset and 13 * cannot link with -nostdlib. The functions it does use (memset and
14 * __aeabi_* functions for integer division) are sufficiently small and 14 * __aeabi_* functions for integer division) are sufficiently small and
15 * self-contained in ARM's libc.a that we don't have any problem using 15 * self-contained in ARM's libc.a that we don't have any problem using
16 * the libc definitions though we aren't using the rest of libc or doing 16 * the libc definitions though we aren't using the rest of libc or doing
17 * any of the setup it might expect. 17 * any of the setup it might expect.
18 */ 18 */
19 19
20 #include <elf.h> 20 #include <elf.h>
21 #include <fcntl.h> 21 #include <fcntl.h>
22 #include <link.h> 22 #include <link.h>
23 #include <stddef.h> 23 #include <stddef.h>
24 #include <stdint.h> 24 #include <stdint.h>
25 #include <sys/mman.h> 25 #include <sys/mman.h>
26 26
27 #define MAX_PHNUM 12 27 #define MAX_PHNUM 12
28 28
29 #if defined(__i386__)
30 # define DYNAMIC_LINKER "/lib/ld-linux.so.2"
31 #elif defined(__x86_64__)
32 # define DYNAMIC_LINKER "/lib64/ld-linux-x86-64.so.2"
33 #elif defined(__ARM_EABI__)
34 # define DYNAMIC_LINKER "/lib/ld-linux.so.3"
35 #else
36 # error "Don't know the dynamic linker file name for this architecture!"
37 #endif
38
39 29
40 /* 30 /*
41 * We're not using <string.h> functions here, to avoid dependencies. 31 * We're not using <string.h> functions here, to avoid dependencies.
42 * In the x86 libc, even "simple" functions like memset and strlen can 32 * In the x86 libc, even "simple" functions like memset and strlen can
43 * depend on complex startup code, because in newer libc 33 * depend on complex startup code, because in newer libc
44 * implementations they are defined using STT_GNU_IFUNC. 34 * implementations they are defined using STT_GNU_IFUNC.
45 */ 35 */
46 36
47 static void my_bzero(void *buf, size_t n) { 37 static void my_bzero(void *buf, size_t n) {
48 char *p = buf; 38 char *p = buf;
(...skipping 29 matching lines...) Expand all
78 *p = "0123456789"[value % 10]; 68 *p = "0123456789"[value % 10];
79 value /= 10; 69 value /= 10;
80 } while (value != 0); 70 } while (value != 0);
81 iov->iov_base = p; 71 iov->iov_base = p;
82 iov->iov_len = &buf[bufsz] - p; 72 iov->iov_len = &buf[bufsz] - p;
83 } 73 }
84 74
85 #define STRING_IOV(string_constant, cond) \ 75 #define STRING_IOV(string_constant, cond) \
86 { (void *) string_constant, cond ? (sizeof(string_constant) - 1) : 0 } 76 { (void *) string_constant, cond ? (sizeof(string_constant) - 1) : 0 }
87 77
88 __attribute__((noreturn)) static void fail(const char *message, 78 __attribute__((noreturn)) static void fail(const char *filename,
79 const char *message,
89 const char *item1, int value1, 80 const char *item1, int value1,
90 const char *item2, int value2) { 81 const char *item2, int value2) {
91 char valbuf1[32]; 82 char valbuf1[32];
92 char valbuf2[32]; 83 char valbuf2[32];
93 struct kernel_iovec iov[] = { 84 struct kernel_iovec iov[] = {
94 STRING_IOV("bootstrap_helper", 1), 85 STRING_IOV("bootstrap_helper: ", 1),
95 STRING_IOV(DYNAMIC_LINKER, 1), 86 { (void *) filename, my_strlen(filename) },
96 STRING_IOV(": ", 1), 87 STRING_IOV(": ", 1),
97 { (void *) message, my_strlen(message) }, 88 { (void *) message, my_strlen(message) },
98 { (void *) item1, item1 == NULL ? 0 : my_strlen(item1) }, 89 { (void *) item1, item1 == NULL ? 0 : my_strlen(item1) },
99 STRING_IOV("=", item1 != NULL), 90 STRING_IOV("=", item1 != NULL),
100 {}, 91 {},
101 STRING_IOV(", ", item1 != NULL && item2 != NULL), 92 STRING_IOV(", ", item1 != NULL && item2 != NULL),
102 { (void *) item2, item2 == NULL ? 0 : my_strlen(item2) }, 93 { (void *) item2, item2 == NULL ? 0 : my_strlen(item2) },
103 STRING_IOV("=", item2 != NULL), 94 STRING_IOV("=", item2 != NULL),
104 {}, 95 {},
105 { "\n", 1 }, 96 { "\n", 1 },
106 }; 97 };
107 const int niov = sizeof(iov) / sizeof(iov[0]); 98 const int niov = sizeof(iov) / sizeof(iov[0]);
108 99
109 if (item1 != NULL) 100 if (item1 != NULL)
110 iov_int_string(value1, &iov[6], valbuf1, sizeof(valbuf1)); 101 iov_int_string(value1, &iov[6], valbuf1, sizeof(valbuf1));
111 if (item2 != NULL) 102 if (item2 != NULL)
112 iov_int_string(value1, &iov[10], valbuf2, sizeof(valbuf2)); 103 iov_int_string(value1, &iov[10], valbuf2, sizeof(valbuf2));
113 104
114 sys_writev(2, iov, niov); 105 sys_writev(2, iov, niov);
115 sys_exit_group(2); 106 sys_exit_group(2);
116 while (1) *(volatile int *) 0 = 0; /* Crash. */ 107 while (1) *(volatile int *) 0 = 0; /* Crash. */
117 } 108 }
118 109
119 110
120 static int my_open(const char *file, int oflag) { 111 static int my_open(const char *file, int oflag) {
121 int result = sys_open(file, oflag, 0); 112 int result = sys_open(file, oflag, 0);
122 if (result < 0) 113 if (result < 0)
123 fail("Cannot open dynamic linker! ", "errno", my_errno, NULL, 0); 114 fail(file, "Cannot open ELF file! ", "errno", my_errno, NULL, 0);
124 return result; 115 return result;
125 } 116 }
126 117
127 static void my_pread(const char *fail_message, 118 static void my_pread(const char *file, const char *fail_message,
128 int fd, void *buf, size_t bufsz, uintptr_t pos) { 119 int fd, void *buf, size_t bufsz, uintptr_t pos) {
129 ssize_t result = sys_pread64(fd, buf, bufsz, pos); 120 ssize_t result = sys_pread64(fd, buf, bufsz, pos);
130 if (result < 0) 121 if (result < 0)
131 fail(fail_message, "errno", my_errno, NULL, 0); 122 fail(file, fail_message, "errno", my_errno, NULL, 0);
132 if ((size_t) result != bufsz) 123 if ((size_t) result != bufsz)
133 fail(fail_message, "read count", result, NULL, 0); 124 fail(file, fail_message, "read count", result, NULL, 0);
134 } 125 }
135 126
136 static uintptr_t my_mmap(const char *segment_type, unsigned int segnum, 127 static uintptr_t my_mmap(const char *file,
128 const char *segment_type, unsigned int segnum,
137 uintptr_t address, size_t size, 129 uintptr_t address, size_t size,
138 int prot, int flags, int fd, uintptr_t pos) { 130 int prot, int flags, int fd, uintptr_t pos) {
139 #if defined(__NR_mmap2) 131 #if defined(__NR_mmap2)
140 void *result = sys_mmap2((void *) address, size, prot, flags, fd, pos >> 12); 132 void *result = sys_mmap2((void *) address, size, prot, flags, fd, pos >> 12);
141 #else 133 #else
142 void *result = sys_mmap((void *) address, size, prot, flags, fd, pos); 134 void *result = sys_mmap((void *) address, size, prot, flags, fd, pos);
143 #endif 135 #endif
144 if (result == MAP_FAILED) 136 if (result == MAP_FAILED)
145 fail("Failed to map from dynamic linker! ", 137 fail(file, "Failed to map segment! ",
146 segment_type, segnum, "errno", my_errno); 138 segment_type, segnum, "errno", my_errno);
147 return (uintptr_t) result; 139 return (uintptr_t) result;
148 } 140 }
149 141
150 static void my_mprotect(unsigned int segnum, 142 static void my_mprotect(const char *file, unsigned int segnum,
151 uintptr_t address, size_t size, int prot) { 143 uintptr_t address, size_t size, int prot) {
152 if (sys_mprotect((void *) address, size, prot) < 0) 144 if (sys_mprotect((void *) address, size, prot) < 0)
153 fail("Failed to mprotect hole in dynamic linker! ", 145 fail(file, "Failed to mprotect segment hole! ",
154 "segment", segnum, "errno", my_errno); 146 "segment", segnum, "errno", my_errno);
155 } 147 }
156 148
157 149
158 static int prot_from_phdr(const ElfW(Phdr) *phdr) { 150 static int prot_from_phdr(const ElfW(Phdr) *phdr) {
159 int prot = 0; 151 int prot = 0;
160 if (phdr->p_flags & PF_R) 152 if (phdr->p_flags & PF_R)
161 prot |= PROT_READ; 153 prot |= PROT_READ;
162 if (phdr->p_flags & PF_W) 154 if (phdr->p_flags & PF_W)
163 prot |= PROT_WRITE; 155 prot |= PROT_WRITE;
164 if (phdr->p_flags & PF_X) 156 if (phdr->p_flags & PF_X)
165 prot |= PROT_EXEC; 157 prot |= PROT_EXEC;
166 return prot; 158 return prot;
167 } 159 }
168 160
169 static uintptr_t round_up(uintptr_t value, uintptr_t size) { 161 static uintptr_t round_up(uintptr_t value, uintptr_t size) {
170 return (value + size - 1) & -size; 162 return (value + size - 1) & -size;
171 } 163 }
172 164
173 static uintptr_t round_down(uintptr_t value, uintptr_t size) { 165 static uintptr_t round_down(uintptr_t value, uintptr_t size) {
174 return value & -size; 166 return value & -size;
175 } 167 }
176 168
177 /* 169 /*
178 * Handle the "bss" portion of a segment, where the memory size 170 * Handle the "bss" portion of a segment, where the memory size
179 * exceeds the file size and we zero-fill the difference. For any 171 * exceeds the file size and we zero-fill the difference. For any
180 * whole pages in this region, we over-map anonymous pages. For the 172 * whole pages in this region, we over-map anonymous pages. For the
181 * sub-page remainder, we zero-fill bytes directly. 173 * sub-page remainder, we zero-fill bytes directly.
182 */ 174 */
183 static void handle_bss(unsigned int segnum, const ElfW(Phdr) *ph, 175 static void handle_bss(const char *file,
176 unsigned int segnum, const ElfW(Phdr) *ph,
184 ElfW(Addr) load_bias, size_t pagesize) { 177 ElfW(Addr) load_bias, size_t pagesize) {
185 if (ph->p_memsz > ph->p_filesz) { 178 if (ph->p_memsz > ph->p_filesz) {
186 ElfW(Addr) file_end = ph->p_vaddr + load_bias + ph->p_filesz; 179 ElfW(Addr) file_end = ph->p_vaddr + load_bias + ph->p_filesz;
187 ElfW(Addr) file_page_end = round_up(file_end, pagesize); 180 ElfW(Addr) file_page_end = round_up(file_end, pagesize);
188 ElfW(Addr) page_end = round_up(ph->p_vaddr + load_bias + 181 ElfW(Addr) page_end = round_up(ph->p_vaddr + load_bias +
189 ph->p_memsz, pagesize); 182 ph->p_memsz, pagesize);
190 if (page_end > file_page_end) 183 if (page_end > file_page_end)
191 my_mmap("bss segment", segnum, 184 my_mmap(file, "bss segment", segnum,
192 file_page_end, page_end - file_page_end, 185 file_page_end, page_end - file_page_end,
193 prot_from_phdr(ph), MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); 186 prot_from_phdr(ph), MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
194 if (file_page_end > file_end && (ph->p_flags & PF_W)) 187 if (file_page_end > file_end && (ph->p_flags & PF_W))
195 my_bzero((void *) file_end, file_page_end - file_end); 188 my_bzero((void *) file_end, file_page_end - file_end);
196 } 189 }
197 } 190 }
198 191
199 /* 192 /*
200 * This is the main loading code. It's called with the address of the 193 * Open an ELF file and load it into memory.
201 * auxiliary vector on the stack, which we need to examine and modify.
202 * It returns the dynamic linker's runtime entry point address, where
203 * we should jump to. This is called by the machine-dependent _start
204 * code (below). On return, it restores the original stack pointer
205 * and jumps to this entry point.
206 */ 194 */
207 ElfW(Addr) do_load(ElfW(auxv_t) *auxv) { 195 static ElfW(Addr) load_elf_file(const char *filename,
208 /* 196 size_t pagesize,
209 * Record the auxv entries that are specific to the file loaded. 197 ElfW(Addr) *out_phdr,
210 * The incoming entries point to our own static executable. 198 ElfW(Addr) *out_phnum,
211 */ 199 const char **out_interp) {
212 ElfW(auxv_t) *av_entry = NULL; 200 int fd = my_open(filename, O_RDONLY);
213 ElfW(auxv_t) *av_phdr = NULL;
214 ElfW(auxv_t) *av_phnum = NULL;
215 size_t pagesize = 0;
216
217 ElfW(auxv_t) *av;
218 for (av = auxv;
219 av_entry == NULL || av_phdr == NULL || av_phnum == NULL || pagesize == 0;
220 ++av) {
221 switch (av->a_type) {
222 case AT_NULL:
223 fail("Failed to find AT_ENTRY, AT_PHDR, AT_PHNUM, or AT_PAGESZ!",
224 NULL, 0, NULL, 0);
225 /*NOTREACHED*/
226 break;
227 case AT_ENTRY:
228 av_entry = av;
229 break;
230 case AT_PAGESZ:
231 pagesize = av->a_un.a_val;
232 break;
233 case AT_PHDR:
234 av_phdr = av;
235 break;
236 case AT_PHNUM:
237 av_phnum = av;
238 break;
239 }
240 }
241
242 int fd = my_open(DYNAMIC_LINKER, O_RDONLY);
243 201
244 ElfW(Ehdr) ehdr; 202 ElfW(Ehdr) ehdr;
245 my_pread("Failed to read ELF header from dynamic linker! ", 203 my_pread(filename, "Failed to read ELF header from file! ",
246 fd, &ehdr, sizeof(ehdr), 0); 204 fd, &ehdr, sizeof(ehdr), 0);
247 205
248 if (ehdr.e_ident[EI_MAG0] != ELFMAG0 || 206 if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
249 ehdr.e_ident[EI_MAG1] != ELFMAG1 || 207 ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
250 ehdr.e_ident[EI_MAG2] != ELFMAG2 || 208 ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
251 ehdr.e_ident[EI_MAG3] != ELFMAG3 || 209 ehdr.e_ident[EI_MAG3] != ELFMAG3 ||
252 ehdr.e_version != EV_CURRENT || 210 ehdr.e_version != EV_CURRENT ||
253 ehdr.e_ehsize != sizeof(ehdr) || 211 ehdr.e_ehsize != sizeof(ehdr) ||
254 ehdr.e_phentsize != sizeof(ElfW(Phdr))) 212 ehdr.e_phentsize != sizeof(ElfW(Phdr)))
255 fail("Dynamic linker has no valid ELF header!", NULL, 0, NULL, 0); 213 fail(filename, "File has no valid ELF header!", NULL, 0, NULL, 0);
256 214
257 switch (ehdr.e_machine) { 215 switch (ehdr.e_machine) {
258 #if defined(__i386__) 216 #if defined(__i386__)
259 case EM_386: 217 case EM_386:
260 #elif defined(__x86_64__) 218 #elif defined(__x86_64__)
261 case EM_X86_64: 219 case EM_X86_64:
262 #elif defined(__arm__) 220 #elif defined(__arm__)
263 case EM_ARM: 221 case EM_ARM:
264 #else 222 #else
265 # error "Don't know the e_machine value for this architecture!" 223 # error "Don't know the e_machine value for this architecture!"
266 #endif 224 #endif
267 break; 225 break;
268 default: 226 default:
269 fail("Dynamic linker has wrong architecture! ", 227 fail(filename, "ELF file has wrong architecture! ",
270 "e_machine", ehdr.e_machine, NULL, 0); 228 "e_machine", ehdr.e_machine, NULL, 0);
271 break; 229 break;
272 } 230 }
273 231
274 ElfW(Phdr) phdr[MAX_PHNUM]; 232 ElfW(Phdr) phdr[MAX_PHNUM];
275 if (ehdr.e_phnum > sizeof(phdr) / sizeof(phdr[0]) || ehdr.e_phnum < 1) 233 if (ehdr.e_phnum > sizeof(phdr) / sizeof(phdr[0]) || ehdr.e_phnum < 1)
276 fail("Dynamic linker has unreasonable ", 234 fail(filename, "ELF file has unreasonable ",
277 "e_phnum", ehdr.e_phnum, NULL, 0); 235 "e_phnum", ehdr.e_phnum, NULL, 0);
278 236
279 if (ehdr.e_type != ET_DYN) 237 if (ehdr.e_type != ET_DYN)
280 fail("Dynamic linker not ET_DYN! ", 238 fail(filename, "ELF file not ET_DYN! ",
281 "e_type", ehdr.e_type, NULL, 0); 239 "e_type", ehdr.e_type, NULL, 0);
282 240
283 my_pread("Failed to read program headers from dynamic linker! ", 241 my_pread(filename, "Failed to read program headers from ELF file! ",
284 fd, phdr, sizeof(phdr[0]) * ehdr.e_phnum, ehdr.e_phoff); 242 fd, phdr, sizeof(phdr[0]) * ehdr.e_phnum, ehdr.e_phoff);
285 243
244 const ElfW(Phdr) *interp = NULL;
286 size_t i = 0; 245 size_t i = 0;
287 while (i < ehdr.e_phnum && phdr[i].p_type != PT_LOAD) 246 while (i < ehdr.e_phnum && phdr[i].p_type != PT_LOAD) {
247 if (phdr[i].p_type == PT_INTERP)
248 interp = &phdr[i];
288 ++i; 249 ++i;
250 }
289 if (i == ehdr.e_phnum) 251 if (i == ehdr.e_phnum)
290 fail("Dynamic linker has no PT_LOAD header!", 252 fail(filename, "ELF file has no PT_LOAD header!",
291 NULL, 0, NULL, 0); 253 NULL, 0, NULL, 0);
292 254
293 /* 255 /*
294 * ELF requires that PT_LOAD segments be in ascending order of p_vaddr. 256 * ELF requires that PT_LOAD segments be in ascending order of p_vaddr.
295 * Find the last one to calculate the whole address span of the image. 257 * Find the last one to calculate the whole address span of the image.
296 */ 258 */
297 const ElfW(Phdr) *first_load = &phdr[i]; 259 const ElfW(Phdr) *first_load = &phdr[i];
298 const ElfW(Phdr) *last_load = &phdr[ehdr.e_phnum - 1]; 260 const ElfW(Phdr) *last_load = &phdr[ehdr.e_phnum - 1];
299 while (last_load > first_load && last_load->p_type != PT_LOAD) 261 while (last_load > first_load && last_load->p_type != PT_LOAD)
300 --last_load; 262 --last_load;
301 263
264 if (interp == NULL && out_interp != NULL) {
265 /*
266 * Usually PT_INTERP is before the first PT_LOAD.
Mark Seaborn 2011/11/11 19:28:25 Can you just have one loop that looks for PT_INTER
267 * But nothing says it has to be. So look for it later too.
268 */
269 for (; i < ehdr.e_phnum; ++i) {
270 if (phdr[i].p_type == PT_INTERP) {
271 interp = &phdr[i];
272 break;
273 }
274 }
275 }
276
302 size_t span = last_load->p_vaddr + last_load->p_memsz - first_load->p_vaddr; 277 size_t span = last_load->p_vaddr + last_load->p_memsz - first_load->p_vaddr;
303 278
304 /* 279 /*
305 * Map the first segment and reserve the space used for the rest and 280 * Map the first segment and reserve the space used for the rest and
306 * for holes between segments. 281 * for holes between segments.
307 */ 282 */
308 const uintptr_t mapping = my_mmap("segment", first_load - phdr, 283 const uintptr_t mapping = my_mmap(filename, "segment", first_load - phdr,
309 round_down(first_load->p_vaddr, pagesize), 284 round_down(first_load->p_vaddr, pagesize),
310 span, prot_from_phdr(first_load), 285 span, prot_from_phdr(first_load),
311 MAP_PRIVATE, fd, 286 MAP_PRIVATE, fd,
312 round_down(first_load->p_offset, pagesize)); 287 round_down(first_load->p_offset, pagesize));
313 288
314 const ElfW(Addr) load_bias = mapping - round_down(first_load->p_vaddr, 289 const ElfW(Addr) load_bias = mapping - round_down(first_load->p_vaddr,
315 pagesize); 290 pagesize);
316 291
317 if (first_load->p_offset > ehdr.e_phoff || 292 if (first_load->p_offset > ehdr.e_phoff ||
318 first_load->p_filesz < ehdr.e_phoff + (ehdr.e_phnum * sizeof(ElfW(Phdr)))) 293 first_load->p_filesz < ehdr.e_phoff + (ehdr.e_phnum * sizeof(ElfW(Phdr))))
319 fail("First load segment of dynamic linker does not contain phdrs!", 294 fail(filename, "First load segment of ELF file does not contain phdrs!",
320 NULL, 0, NULL, 0); 295 NULL, 0, NULL, 0);
321 296
322 /* Point the auxv elements at the dynamic linker's phdrs and entry. */ 297 handle_bss(filename, first_load - phdr, first_load, load_bias, pagesize);
323 av_phdr->a_un.a_val = (ehdr.e_phoff - first_load->p_offset +
324 first_load->p_vaddr + load_bias);
325 av_phnum->a_un.a_val = ehdr.e_phnum;
326 av_entry->a_un.a_val = ehdr.e_entry + load_bias;
327
328 handle_bss(first_load - phdr, first_load, load_bias, pagesize);
329 298
330 ElfW(Addr) last_end = first_load->p_vaddr + load_bias + first_load->p_memsz; 299 ElfW(Addr) last_end = first_load->p_vaddr + load_bias + first_load->p_memsz;
331 300
332 /* 301 /*
333 * Map the remaining segments, and protect any holes between them. 302 * Map the remaining segments, and protect any holes between them.
334 */ 303 */
335 const ElfW(Phdr) *ph; 304 const ElfW(Phdr) *ph;
336 for (ph = first_load + 1; ph <= last_load; ++ph) { 305 for (ph = first_load + 1; ph <= last_load; ++ph) {
337 if (ph->p_type == PT_LOAD) { 306 if (ph->p_type == PT_LOAD) {
338 ElfW(Addr) last_page_end = round_up(last_end, pagesize); 307 ElfW(Addr) last_page_end = round_up(last_end, pagesize);
339 308
340 last_end = ph->p_vaddr + load_bias + ph->p_memsz; 309 last_end = ph->p_vaddr + load_bias + ph->p_memsz;
341 ElfW(Addr) start = round_down(ph->p_vaddr + load_bias, pagesize); 310 ElfW(Addr) start = round_down(ph->p_vaddr + load_bias, pagesize);
342 ElfW(Addr) end = round_up(last_end, pagesize); 311 ElfW(Addr) end = round_up(last_end, pagesize);
343 312
344 if (start > last_page_end) 313 if (start > last_page_end)
345 my_mprotect(ph - phdr, last_page_end, start - last_page_end, PROT_NONE); 314 my_mprotect(filename,
315 ph - phdr, last_page_end, start - last_page_end, PROT_NONE);
346 316
347 my_mmap("segment", ph - phdr, 317 my_mmap(filename, "segment", ph - phdr,
348 start, end - start, 318 start, end - start,
349 prot_from_phdr(ph), MAP_PRIVATE | MAP_FIXED, fd, 319 prot_from_phdr(ph), MAP_PRIVATE | MAP_FIXED, fd,
350 round_down(ph->p_offset, pagesize)); 320 round_down(ph->p_offset, pagesize));
351 321
352 handle_bss(ph - phdr, ph, load_bias, pagesize); 322 handle_bss(filename, ph - phdr, ph, load_bias, pagesize);
353 } 323 }
354 } 324 }
355 325
356 sys_close(fd); 326 sys_close(fd);
357 327
328 if (out_phdr != NULL)
329 *out_phdr = (ehdr.e_phoff - first_load->p_offset +
330 first_load->p_vaddr + load_bias);
331 if (out_phnum != NULL)
332 *out_phnum = ehdr.e_phnum;
333 if (out_interp != NULL && interp != NULL)
334 *out_interp = (const char *) (interp->p_vaddr + load_bias);
Mark Seaborn 2011/11/11 19:28:25 It's OK to assume that PT_INTERP is covered by a P
335
358 return ehdr.e_entry + load_bias; 336 return ehdr.e_entry + load_bias;
359 } 337 }
360 338
361 /* 339 /*
362 * We have to define the actual entry point code (_start) in assembly 340 * This is the main loading code. It's called with the starting stack pointer.
363 * for each machine. The kernel startup protocol is not compatible 341 * This points to a sequence of pointer-size words:
364 * with the normal C function calling convention. Here, we calculate 342 * [0] argc
365 * the address of the auxiliary vector on the stack; call do_load 343 * [1..argc] argv[0..argc-1]
366 * (above) using the normal C convention as per the ABI; restore the 344 * [1+argc] NULL
367 * original starting stack; and finally, jump to the dynamic linker's 345 * [2+argc..] envp[0..]
368 * entry point address. 346 * NULL
347 * auxv[0].a_type
348 * auxv[1].a_un.a_val
349 * ...
350 * It returns the dynamic linker's runtime entry point address, where
351 * we should jump to. This is called by the machine-dependent _start
352 * code (below). On return, it restores the original stack pointer
353 * and jumps to this entry point.
354 *
355 * argv[0] is the uninteresting name of this bootstrap program. argv[1] is
356 * the real program file name we'll open, and also the argv[0] for that
357 * program. We need to modify argc, move argv[1..] back to the argv[0..]
358 * position, and also examine and modify the auxiliary vector on the stack.
359 */
360 ElfW(Addr) do_load(uintptr_t *stack) {
361 size_t i;
362
363 /*
364 * First find the end of the auxiliary vector.
365 */
366 int argc = stack[0];
367 char **argv = (char **) &stack[1];
368 const char *program = argv[1];
369 char **envp = &argv[argc + 1];
370 char **ep = envp;
371 while (*ep != NULL)
372 ++ep;
373 ElfW(auxv_t) *auxv = (ElfW(auxv_t) *) (ep + 1);
374 ElfW(auxv_t) *av = auxv;
375 while (av->a_type != AT_NULL)
376 ++av;
377 size_t stack_words = (uintptr_t *) (av + 1) - &stack[1];
378
379 if (argc < 2)
380 fail("Usage", "PROGRAM ARGS...", NULL, 0, NULL, 0);
381
382 /*
383 * Now move everything back to eat our original argv[0]. When we've done
Mark Seaborn 2011/11/11 19:28:25 Is there a reason why you don't change the stack t
384 * that, envp and auxv will start one word back from where they were.
385 */
386 --argc;
387 --envp;
388 auxv = (ElfW(auxv_t) *) ep;
389 stack[0] = argc;
390 for (i = 1; i < stack_words; ++i)
391 stack[i] = stack[i + 1];
392
393 /*
394 * Record the auxv entries that are specific to the file loaded.
395 * The incoming entries point to our own static executable.
396 */
397 ElfW(auxv_t) *av_entry = NULL;
398 ElfW(auxv_t) *av_phdr = NULL;
399 ElfW(auxv_t) *av_phnum = NULL;
400 size_t pagesize = 0;
401
402 for (av = auxv;
403 av_entry == NULL || av_phdr == NULL || av_phnum == NULL || pagesize == 0;
404 ++av) {
405 switch (av->a_type) {
406 case AT_NULL:
407 fail("startup",
408 "Failed to find AT_ENTRY, AT_PHDR, AT_PHNUM, or AT_PAGESZ!",
409 NULL, 0, NULL, 0);
410 /*NOTREACHED*/
411 break;
412 case AT_ENTRY:
413 av_entry = av;
414 break;
415 case AT_PAGESZ:
416 pagesize = av->a_un.a_val;
417 break;
418 case AT_PHDR:
419 av_phdr = av;
420 break;
421 case AT_PHNUM:
422 av_phnum = av;
423 break;
424 }
425 }
426
427 /* Load the program and point the auxv elements at its phdrs and entry. */
428 const char *interp = NULL;
429 av_entry->a_un.a_val = load_elf_file(program,
430 pagesize,
431 &av_phdr->a_un.a_val,
432 &av_phnum->a_un.a_val,
433 &interp);
434
435 ElfW(Addr) entry = av_entry->a_un.a_val;
436
437 if (interp != NULL) {
438 /*
439 * There was a PT_INTERP, so we have a dynamic linker to load.
440 */
441 entry = load_elf_file(interp, pagesize, NULL, NULL, NULL);
442 }
443
444 return entry;
445 }
446
447 /*
448 * We have to define the actual entry point code (_start) in assembly for
449 * each machine. The kernel startup protocol is not compatible with the
450 * normal C function calling convention. Here, we call do_load (above)
451 * using the normal C convention as per the ABI, with the starting stack
452 * pointer as its argument; restore the original starting stack; and
453 * finally, jump to the dynamic linker's entry point address.
369 */ 454 */
370 #if defined(__i386__) 455 #if defined(__i386__)
371 asm(".globl _start\n" 456 asm(".text\n"
Mark Seaborn 2011/11/11 19:28:25 Was it wrong without .text before? Would .pushsec
457 ".globl _start\n"
372 ".type _start,@function\n" 458 ".type _start,@function\n"
373 "_start:\n" 459 "_start:\n"
374 "xorl %ebp, %ebp\n" 460 "xorl %ebp, %ebp\n"
375 "movl %esp, %ebx\n" /* Save starting SP in %ebx. */ 461 "movl %esp, %ebx\n" /* Save starting SP in %ebx. */
376 "andl $-16, %esp\n" /* Align the stack as per ABI. */ 462 "andl $-16, %esp\n" /* Align the stack as per ABI. */
377 "movl (%ebx), %eax\n" /* argc */ 463 "pushl %ebx\n" /* Argument: stack block. */
378 "leal 8(%ebx,%eax,4), %ecx\n" /* envp */
379 /* Find the envp element that is NULL, and auxv is past there. */
380 "0: addl $4, %ecx\n"
381 "cmpl $0, -4(%ecx)\n"
382 "jne 0b\n"
383 "pushl %ecx\n" /* Argument: auxv. */
384 "call do_load\n" 464 "call do_load\n"
385 "movl %ebx, %esp\n" /* Restore the saved SP. */ 465 "movl %ebx, %esp\n" /* Restore the saved SP. */
386 "jmp *%eax\n" /* Jump to the entry point. */ 466 "jmp *%eax\n" /* Jump to the entry point. */
387 ); 467 );
388 #elif defined(__x86_64__) 468 #elif defined(__x86_64__)
389 asm(".globl _start\n" 469 asm(".text\n"
470 ".globl _start\n"
390 ".type _start,@function\n" 471 ".type _start,@function\n"
391 "_start:\n" 472 "_start:\n"
392 "xorq %rbp, %rbp\n" 473 "xorq %rbp, %rbp\n"
393 "movq %rsp, %rbx\n" /* Save starting SP in %rbx. */ 474 "movq %rsp, %rbx\n" /* Save starting SP in %rbx. */
394 "andq $-16, %rsp\n" /* Align the stack as per ABI. */ 475 "andq $-16, %rsp\n" /* Align the stack as per ABI. */
395 "movq (%rbx), %rax\n" /* argc */ 476 "movq %rbx, %rdi\n" /* Argument: stack block. */
396 "leaq 16(%rbx,%rax,8), %rdi\n" /* envp */ 477 "call do_load\n"
397 /* Find the envp element that is NULL, and auxv is past there. */
398 "0: addq $8, %rdi\n"
399 "cmpq $0, -8(%rdi)\n"
400 "jne 0b\n"
401 "call do_load\n" /* Argument already in %rdi: auxv */
402 "movq %rbx, %rsp\n" /* Restore the saved SP. */ 478 "movq %rbx, %rsp\n" /* Restore the saved SP. */
403 "jmp *%rax\n" /* Jump to the entry point. */ 479 "jmp *%rax\n" /* Jump to the entry point. */
404 ); 480 );
405 #elif defined(__arm__) 481 #elif defined(__arm__)
406 asm(".globl _start\n" 482 asm(".text\n"
483 ".globl _start\n"
407 ".type _start,#function\n" 484 ".type _start,#function\n"
408 "_start:\n" 485 "_start:\n"
409 #if defined(__thumb2__) 486 #if defined(__thumb2__)
410 ".thumb\n" 487 ".thumb\n"
411 ".syntax unified\n" 488 ".syntax unified\n"
412 #endif 489 #endif
413 "mov fp, #0\n" 490 "mov fp, #0\n"
414 "mov lr, #0\n" 491 "mov lr, #0\n"
415 "mov r4, sp\n" /* Save starting SP in r4. */ 492 "mov r4, sp\n" /* Save starting SP in r4. */
416 "ldr r1, [r4]\n" /* argc */ 493 "mov r0, sp\n" /* Argument: stack block. */
417 "add r1, r1, #2\n"
418 "add r0, r4, r1, asl #2\n" /* envp */
419 /* Find the envp element that is NULL, and auxv is past there. */
420 "0: ldr r1, [r0], #4\n"
421 "cmp r1, #0\n"
422 "bne 0b\n"
423 "bl do_load\n" 494 "bl do_load\n"
424 "mov sp, r4\n" /* Restore the saved SP. */ 495 "mov sp, r4\n" /* Restore the saved SP. */
425 "blx r0\n" /* Jump to the entry point. */ 496 "blx r0\n" /* Jump to the entry point. */
426 ); 497 );
427 #else 498 #else
428 # error "Need stack-preserving _start code for this architecture!" 499 # error "Need stack-preserving _start code for this architecture!"
429 #endif 500 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698