]>
Commit | Line | Data |
---|---|---|
31e31b8a FB |
1 | /* This is the Linux kernel elf-loading code, ported into user space */ |
2 | ||
3 | #include <stdio.h> | |
4 | #include <sys/types.h> | |
5 | #include <fcntl.h> | |
6 | #include <sys/stat.h> | |
7 | #include <errno.h> | |
8 | #include <unistd.h> | |
9 | #include <sys/mman.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | ||
3ef693a0 | 13 | #include "qemu.h" |
31e31b8a | 14 | |
30ac07d4 FB |
15 | #ifdef TARGET_I386 |
16 | ||
17 | #define ELF_START_MMAP 0x80000000 | |
18 | ||
19 | typedef uint32_t elf_greg_t; | |
20 | ||
21 | #define ELF_NGREG (sizeof (struct target_pt_regs) / sizeof(elf_greg_t)) | |
22 | typedef elf_greg_t elf_gregset_t[ELF_NGREG]; | |
23 | ||
24 | typedef struct user_i387_struct elf_fpregset_t; | |
25 | ||
26 | /* | |
27 | * This is used to ensure we don't load something for the wrong architecture. | |
28 | */ | |
29 | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) | |
30 | ||
31 | /* | |
32 | * These are used to set parameters in the core dumps. | |
33 | */ | |
34 | #define ELF_CLASS ELFCLASS32 | |
35 | #define ELF_DATA ELFDATA2LSB | |
36 | #define ELF_ARCH EM_386 | |
37 | ||
38 | /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program | |
39 | starts %edx contains a pointer to a function which might be | |
40 | registered using `atexit'. This provides a mean for the | |
41 | dynamic linker to call DT_FINI functions for shared libraries | |
42 | that have been loaded before the code runs. | |
43 | ||
44 | A value of 0 tells we have no such handler. */ | |
45 | #define ELF_PLAT_INIT(_r) _r->edx = 0 | |
46 | ||
47 | #define USE_ELF_CORE_DUMP | |
48 | #define ELF_EXEC_PAGESIZE 4096 | |
49 | ||
50 | #endif | |
51 | ||
31e31b8a FB |
52 | #include "linux_bin.h" |
53 | #include "elf.h" | |
54 | #include "segment.h" | |
55 | ||
56 | /* Necessary parameters */ | |
57 | #define ALPHA_PAGE_SIZE 4096 | |
58 | #define X86_PAGE_SIZE 4096 | |
59 | ||
60 | #define ALPHA_PAGE_MASK (~(ALPHA_PAGE_SIZE-1)) | |
61 | #define X86_PAGE_MASK (~(X86_PAGE_SIZE-1)) | |
62 | ||
63 | #define ALPHA_PAGE_ALIGN(addr) ((((addr)+ALPHA_PAGE_SIZE)-1)&ALPHA_PAGE_MASK) | |
64 | #define X86_PAGE_ALIGN(addr) ((((addr)+X86_PAGE_SIZE)-1)&X86_PAGE_MASK) | |
65 | ||
66 | #define NGROUPS 32 | |
67 | ||
68 | #define X86_ELF_EXEC_PAGESIZE X86_PAGE_SIZE | |
69 | #define X86_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(X86_ELF_EXEC_PAGESIZE-1)) | |
70 | #define X86_ELF_PAGEOFFSET(_v) ((_v) & (X86_ELF_EXEC_PAGESIZE-1)) | |
71 | ||
72 | #define ALPHA_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ALPHA_PAGE_SIZE-1)) | |
73 | #define ALPHA_ELF_PAGEOFFSET(_v) ((_v) & (ALPHA_PAGE_SIZE-1)) | |
74 | ||
75 | #define INTERPRETER_NONE 0 | |
76 | #define INTERPRETER_AOUT 1 | |
77 | #define INTERPRETER_ELF 2 | |
78 | ||
79 | #define DLINFO_ITEMS 12 | |
80 | ||
81 | /* Where we find X86 libraries... */ | |
d691f669 | 82 | |
31e31b8a FB |
83 | |
84 | //extern void * mmap4k(); | |
85 | #define mmap4k(a, b, c, d, e, f) mmap((void *)(a), b, c, d, e, f) | |
86 | ||
87 | extern unsigned long x86_stack_size; | |
88 | ||
89 | static int load_aout_interp(void * exptr, int interp_fd); | |
90 | ||
91 | #ifdef BSWAP_NEEDED | |
92 | static void bswap_ehdr(Elf32_Ehdr *ehdr) | |
93 | { | |
94 | bswap16s(&ehdr->e_type); /* Object file type */ | |
95 | bswap16s(&ehdr->e_machine); /* Architecture */ | |
96 | bswap32s(&ehdr->e_version); /* Object file version */ | |
97 | bswap32s(&ehdr->e_entry); /* Entry point virtual address */ | |
98 | bswap32s(&ehdr->e_phoff); /* Program header table file offset */ | |
99 | bswap32s(&ehdr->e_shoff); /* Section header table file offset */ | |
100 | bswap32s(&ehdr->e_flags); /* Processor-specific flags */ | |
101 | bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ | |
102 | bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ | |
103 | bswap16s(&ehdr->e_phnum); /* Program header table entry count */ | |
104 | bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ | |
105 | bswap16s(&ehdr->e_shnum); /* Section header table entry count */ | |
106 | bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ | |
107 | } | |
108 | ||
109 | static void bswap_phdr(Elf32_Phdr *phdr) | |
110 | { | |
111 | bswap32s(&phdr->p_type); /* Segment type */ | |
112 | bswap32s(&phdr->p_offset); /* Segment file offset */ | |
113 | bswap32s(&phdr->p_vaddr); /* Segment virtual address */ | |
114 | bswap32s(&phdr->p_paddr); /* Segment physical address */ | |
115 | bswap32s(&phdr->p_filesz); /* Segment size in file */ | |
116 | bswap32s(&phdr->p_memsz); /* Segment size in memory */ | |
117 | bswap32s(&phdr->p_flags); /* Segment flags */ | |
118 | bswap32s(&phdr->p_align); /* Segment alignment */ | |
119 | } | |
120 | #endif | |
121 | ||
122 | static void * get_free_page(void) | |
123 | { | |
124 | void * retval; | |
125 | ||
126 | /* User-space version of kernel get_free_page. Returns a page-aligned | |
127 | * page-sized chunk of memory. | |
128 | */ | |
129 | retval = mmap4k(0, ALPHA_PAGE_SIZE, PROT_READ|PROT_WRITE, | |
130 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
131 | ||
132 | if((long)retval == -1) { | |
133 | perror("get_free_page"); | |
134 | exit(-1); | |
135 | } | |
136 | else { | |
137 | return(retval); | |
138 | } | |
139 | } | |
140 | ||
141 | static void free_page(void * pageaddr) | |
142 | { | |
143 | (void)munmap(pageaddr, ALPHA_PAGE_SIZE); | |
144 | } | |
145 | ||
146 | /* | |
147 | * 'copy_string()' copies argument/envelope strings from user | |
148 | * memory to free pages in kernel mem. These are in a format ready | |
149 | * to be put directly into the top of new user memory. | |
150 | * | |
151 | */ | |
152 | static unsigned long copy_strings(int argc,char ** argv,unsigned long *page, | |
153 | unsigned long p) | |
154 | { | |
155 | char *tmp, *tmp1, *pag = NULL; | |
156 | int len, offset = 0; | |
157 | ||
158 | if (!p) { | |
159 | return 0; /* bullet-proofing */ | |
160 | } | |
161 | while (argc-- > 0) { | |
162 | if (!(tmp1 = tmp = get_user(argv+argc))) { | |
163 | fprintf(stderr, "VFS: argc is wrong"); | |
164 | exit(-1); | |
165 | } | |
166 | while (get_user(tmp++)); | |
167 | len = tmp - tmp1; | |
168 | if (p < len) { /* this shouldn't happen - 128kB */ | |
169 | return 0; | |
170 | } | |
171 | while (len) { | |
172 | --p; --tmp; --len; | |
173 | if (--offset < 0) { | |
174 | offset = p % X86_PAGE_SIZE; | |
175 | if (!(pag = (char *) page[p/X86_PAGE_SIZE]) && | |
176 | !(pag = (char *) page[p/X86_PAGE_SIZE] = | |
177 | (unsigned long *) get_free_page())) { | |
178 | return 0; | |
179 | } | |
180 | } | |
181 | if (len == 0 || offset == 0) { | |
182 | *(pag + offset) = get_user(tmp); | |
183 | } | |
184 | else { | |
185 | int bytes_to_copy = (len > offset) ? offset : len; | |
186 | tmp -= bytes_to_copy; | |
187 | p -= bytes_to_copy; | |
188 | offset -= bytes_to_copy; | |
189 | len -= bytes_to_copy; | |
190 | memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1); | |
191 | } | |
192 | } | |
193 | } | |
194 | return p; | |
195 | } | |
196 | ||
197 | static int in_group_p(gid_t g) | |
198 | { | |
199 | /* return TRUE if we're in the specified group, FALSE otherwise */ | |
200 | int ngroup; | |
201 | int i; | |
202 | gid_t grouplist[NGROUPS]; | |
203 | ||
204 | ngroup = getgroups(NGROUPS, grouplist); | |
205 | for(i = 0; i < ngroup; i++) { | |
206 | if(grouplist[i] == g) { | |
207 | return 1; | |
208 | } | |
209 | } | |
210 | return 0; | |
211 | } | |
212 | ||
213 | static int count(char ** vec) | |
214 | { | |
215 | int i; | |
216 | ||
217 | for(i = 0; *vec; i++) { | |
218 | vec++; | |
219 | } | |
220 | ||
221 | return(i); | |
222 | } | |
223 | ||
224 | static int prepare_binprm(struct linux_binprm *bprm) | |
225 | { | |
226 | struct stat st; | |
227 | int mode; | |
228 | int retval, id_change; | |
229 | ||
230 | if(fstat(bprm->fd, &st) < 0) { | |
231 | return(-errno); | |
232 | } | |
233 | ||
234 | mode = st.st_mode; | |
235 | if(!S_ISREG(mode)) { /* Must be regular file */ | |
236 | return(-EACCES); | |
237 | } | |
238 | if(!(mode & 0111)) { /* Must have at least one execute bit set */ | |
239 | return(-EACCES); | |
240 | } | |
241 | ||
242 | bprm->e_uid = geteuid(); | |
243 | bprm->e_gid = getegid(); | |
244 | id_change = 0; | |
245 | ||
246 | /* Set-uid? */ | |
247 | if(mode & S_ISUID) { | |
248 | bprm->e_uid = st.st_uid; | |
249 | if(bprm->e_uid != geteuid()) { | |
250 | id_change = 1; | |
251 | } | |
252 | } | |
253 | ||
254 | /* Set-gid? */ | |
255 | /* | |
256 | * If setgid is set but no group execute bit then this | |
257 | * is a candidate for mandatory locking, not a setgid | |
258 | * executable. | |
259 | */ | |
260 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { | |
261 | bprm->e_gid = st.st_gid; | |
262 | if (!in_group_p(bprm->e_gid)) { | |
263 | id_change = 1; | |
264 | } | |
265 | } | |
266 | ||
267 | memset(bprm->buf, 0, sizeof(bprm->buf)); | |
268 | retval = lseek(bprm->fd, 0L, SEEK_SET); | |
269 | if(retval >= 0) { | |
270 | retval = read(bprm->fd, bprm->buf, 128); | |
271 | } | |
272 | if(retval < 0) { | |
273 | perror("prepare_binprm"); | |
274 | exit(-1); | |
275 | /* return(-errno); */ | |
276 | } | |
277 | else { | |
278 | return(retval); | |
279 | } | |
280 | } | |
281 | ||
282 | unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm, | |
283 | struct image_info * info) | |
284 | { | |
285 | unsigned long stack_base; | |
286 | int i; | |
287 | extern unsigned long stktop; | |
288 | ||
289 | stack_base = X86_STACK_TOP - MAX_ARG_PAGES*X86_PAGE_SIZE; | |
290 | ||
291 | p += stack_base; | |
292 | if (bprm->loader) { | |
293 | bprm->loader += stack_base; | |
294 | } | |
295 | bprm->exec += stack_base; | |
296 | ||
297 | /* Create enough stack to hold everything. If we don't use | |
298 | * it for args, we'll use it for something else... | |
299 | */ | |
9de5e440 FB |
300 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so |
301 | we allocate a bigger stack. Need a better solution, for example | |
302 | by remapping the process stack directly at the right place */ | |
31e31b8a FB |
303 | if(x86_stack_size > MAX_ARG_PAGES*X86_PAGE_SIZE) { |
304 | if((long)mmap4k((void *)(X86_STACK_TOP-x86_stack_size), x86_stack_size + X86_PAGE_SIZE, | |
305 | PROT_READ | PROT_WRITE, | |
306 | MAP_GROWSDOWN | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) { | |
307 | perror("stk mmap"); | |
308 | exit(-1); | |
309 | } | |
310 | } | |
311 | else { | |
312 | if((long)mmap4k((void *)stack_base, (MAX_ARG_PAGES+1)*X86_PAGE_SIZE, | |
313 | PROT_READ | PROT_WRITE, | |
314 | MAP_GROWSDOWN | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) { | |
315 | perror("stk mmap"); | |
316 | exit(-1); | |
317 | } | |
318 | } | |
319 | ||
320 | stktop = stack_base; | |
321 | ||
322 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { | |
323 | if (bprm->page[i]) { | |
324 | info->rss++; | |
325 | ||
326 | memcpy((void *)stack_base, (void *)bprm->page[i], X86_PAGE_SIZE); | |
327 | free_page((void *)bprm->page[i]); | |
328 | } | |
329 | stack_base += X86_PAGE_SIZE; | |
330 | } | |
331 | return p; | |
332 | } | |
333 | ||
334 | static void set_brk(unsigned long start, unsigned long end) | |
335 | { | |
336 | /* page-align the start and end addresses... */ | |
337 | start = ALPHA_PAGE_ALIGN(start); | |
338 | end = ALPHA_PAGE_ALIGN(end); | |
339 | if (end <= start) | |
340 | return; | |
341 | if((long)mmap4k(start, end - start, | |
342 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
343 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) { | |
344 | perror("cannot mmap brk"); | |
345 | exit(-1); | |
346 | } | |
347 | } | |
348 | ||
349 | ||
350 | /* We need to explicitly zero any fractional pages | |
351 | after the data section (i.e. bss). This would | |
352 | contain the junk from the file that should not | |
353 | be in memory */ | |
354 | ||
355 | ||
356 | static void padzero(unsigned long elf_bss) | |
357 | { | |
358 | unsigned long nbyte; | |
359 | char * fpnt; | |
360 | ||
361 | nbyte = elf_bss & (ALPHA_PAGE_SIZE-1); /* was X86_PAGE_SIZE - JRP */ | |
362 | if (nbyte) { | |
363 | nbyte = ALPHA_PAGE_SIZE - nbyte; | |
364 | fpnt = (char *) elf_bss; | |
365 | do { | |
366 | *fpnt++ = 0; | |
367 | } while (--nbyte); | |
368 | } | |
369 | } | |
370 | ||
371 | static unsigned int * create_elf_tables(char *p, int argc, int envc, | |
372 | struct elfhdr * exec, | |
373 | unsigned long load_addr, | |
374 | unsigned long interp_load_addr, int ibcs, | |
375 | struct image_info *info) | |
376 | { | |
b17780d5 FB |
377 | target_ulong *argv, *envp, *dlinfo; |
378 | target_ulong *sp; | |
31e31b8a FB |
379 | |
380 | /* | |
381 | * Force 16 byte alignment here for generality. | |
382 | */ | |
383 | sp = (unsigned int *) (~15UL & (unsigned long) p); | |
384 | sp -= exec ? DLINFO_ITEMS*2 : 2; | |
385 | dlinfo = sp; | |
386 | sp -= envc+1; | |
387 | envp = sp; | |
388 | sp -= argc+1; | |
389 | argv = sp; | |
390 | if (!ibcs) { | |
b17780d5 FB |
391 | put_user(tswapl((target_ulong)envp),--sp); |
392 | put_user(tswapl((target_ulong)argv),--sp); | |
31e31b8a | 393 | } |
31e31b8a FB |
394 | |
395 | #define NEW_AUX_ENT(id, val) \ | |
b17780d5 FB |
396 | put_user (tswapl(id), dlinfo++); \ |
397 | put_user (tswapl(val), dlinfo++) | |
31e31b8a FB |
398 | |
399 | if (exec) { /* Put this here for an ELF program interpreter */ | |
31e31b8a FB |
400 | NEW_AUX_ENT (AT_PHDR, (unsigned int)(load_addr + exec->e_phoff)); |
401 | NEW_AUX_ENT (AT_PHENT, (unsigned int)(sizeof (struct elf_phdr))); | |
402 | NEW_AUX_ENT (AT_PHNUM, (unsigned int)(exec->e_phnum)); | |
403 | NEW_AUX_ENT (AT_PAGESZ, (unsigned int)(ALPHA_PAGE_SIZE)); | |
404 | NEW_AUX_ENT (AT_BASE, (unsigned int)(interp_load_addr)); | |
405 | NEW_AUX_ENT (AT_FLAGS, (unsigned int)0); | |
406 | NEW_AUX_ENT (AT_ENTRY, (unsigned int) exec->e_entry); | |
407 | NEW_AUX_ENT (AT_UID, (unsigned int) getuid()); | |
408 | NEW_AUX_ENT (AT_EUID, (unsigned int) geteuid()); | |
409 | NEW_AUX_ENT (AT_GID, (unsigned int) getgid()); | |
410 | NEW_AUX_ENT (AT_EGID, (unsigned int) getegid()); | |
411 | } | |
412 | NEW_AUX_ENT (AT_NULL, 0); | |
413 | #undef NEW_AUX_ENT | |
b17780d5 | 414 | put_user(tswapl(argc),--sp); |
31e31b8a FB |
415 | info->arg_start = (unsigned int)((unsigned long)p & 0xffffffff); |
416 | while (argc-->0) { | |
b17780d5 | 417 | put_user(tswapl((target_ulong)p),argv++); |
31e31b8a FB |
418 | while (get_user(p++)) /* nothing */ ; |
419 | } | |
420 | put_user(0,argv); | |
421 | info->arg_end = info->env_start = (unsigned int)((unsigned long)p & 0xffffffff); | |
31e31b8a | 422 | while (envc-->0) { |
b17780d5 | 423 | put_user(tswapl((target_ulong)p),envp++); |
31e31b8a FB |
424 | while (get_user(p++)) /* nothing */ ; |
425 | } | |
426 | put_user(0,envp); | |
31e31b8a FB |
427 | info->env_end = (unsigned int)((unsigned long)p & 0xffffffff); |
428 | return sp; | |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex, | |
434 | int interpreter_fd, | |
435 | unsigned long *interp_load_addr) | |
436 | { | |
437 | struct elf_phdr *elf_phdata = NULL; | |
438 | struct elf_phdr *eppnt; | |
439 | unsigned long load_addr; | |
440 | int load_addr_set = 0; | |
441 | int retval; | |
442 | unsigned long last_bss, elf_bss; | |
443 | unsigned long error; | |
444 | int i; | |
445 | ||
446 | elf_bss = 0; | |
447 | last_bss = 0; | |
448 | error = 0; | |
449 | ||
450 | /* We put this here so that mmap will search for the *first* | |
451 | * available memory... | |
452 | */ | |
453 | load_addr = INTERP_LOADADDR; | |
454 | ||
644c433c FB |
455 | #ifdef BSWAP_NEEDED |
456 | bswap_ehdr(interp_elf_ex); | |
457 | #endif | |
31e31b8a FB |
458 | /* First of all, some simple consistency checks */ |
459 | if ((interp_elf_ex->e_type != ET_EXEC && | |
460 | interp_elf_ex->e_type != ET_DYN) || | |
461 | !elf_check_arch(interp_elf_ex->e_machine)) { | |
462 | return ~0UL; | |
463 | } | |
464 | ||
644c433c | 465 | |
31e31b8a FB |
466 | /* Now read in all of the header information */ |
467 | ||
468 | if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > X86_PAGE_SIZE) | |
469 | return ~0UL; | |
470 | ||
471 | elf_phdata = (struct elf_phdr *) | |
472 | malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); | |
473 | ||
474 | if (!elf_phdata) | |
475 | return ~0UL; | |
476 | ||
477 | /* | |
478 | * If the size of this structure has changed, then punt, since | |
479 | * we will be doing the wrong thing. | |
480 | */ | |
481 | if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) | |
482 | { | |
483 | free(elf_phdata); | |
484 | return ~0UL; | |
485 | } | |
486 | ||
487 | retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET); | |
488 | if(retval >= 0) { | |
489 | retval = read(interpreter_fd, | |
490 | (char *) elf_phdata, | |
491 | sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); | |
492 | } | |
31e31b8a FB |
493 | if (retval < 0) { |
494 | perror("load_elf_interp"); | |
495 | exit(-1); | |
496 | free (elf_phdata); | |
497 | return retval; | |
498 | } | |
499 | #ifdef BSWAP_NEEDED | |
500 | eppnt = elf_phdata; | |
501 | for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) { | |
502 | bswap_phdr(eppnt); | |
503 | } | |
504 | #endif | |
505 | eppnt = elf_phdata; | |
506 | for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) | |
507 | if (eppnt->p_type == PT_LOAD) { | |
508 | int elf_type = MAP_PRIVATE | MAP_DENYWRITE; | |
509 | int elf_prot = 0; | |
510 | unsigned long vaddr = 0; | |
511 | unsigned long k; | |
512 | ||
513 | if (eppnt->p_flags & PF_R) elf_prot = PROT_READ; | |
514 | if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; | |
515 | if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; | |
516 | if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) { | |
517 | elf_type |= MAP_FIXED; | |
518 | vaddr = eppnt->p_vaddr; | |
519 | } | |
520 | error = (unsigned long)mmap4k(load_addr+X86_ELF_PAGESTART(vaddr), | |
521 | eppnt->p_filesz + X86_ELF_PAGEOFFSET(eppnt->p_vaddr), | |
522 | elf_prot, | |
523 | elf_type, | |
524 | interpreter_fd, | |
525 | eppnt->p_offset - X86_ELF_PAGEOFFSET(eppnt->p_vaddr)); | |
526 | ||
527 | if (error > -1024UL) { | |
528 | /* Real error */ | |
529 | close(interpreter_fd); | |
530 | free(elf_phdata); | |
531 | return ~0UL; | |
532 | } | |
533 | ||
534 | if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) { | |
535 | load_addr = error; | |
536 | load_addr_set = 1; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Find the end of the file mapping for this phdr, and keep | |
541 | * track of the largest address we see for this. | |
542 | */ | |
543 | k = load_addr + eppnt->p_vaddr + eppnt->p_filesz; | |
544 | if (k > elf_bss) elf_bss = k; | |
545 | ||
546 | /* | |
547 | * Do the same thing for the memory mapping - between | |
548 | * elf_bss and last_bss is the bss section. | |
549 | */ | |
550 | k = load_addr + eppnt->p_memsz + eppnt->p_vaddr; | |
551 | if (k > last_bss) last_bss = k; | |
552 | } | |
553 | ||
554 | /* Now use mmap to map the library into memory. */ | |
555 | ||
556 | close(interpreter_fd); | |
557 | ||
558 | /* | |
559 | * Now fill out the bss section. First pad the last page up | |
560 | * to the page boundary, and then perform a mmap to make sure | |
561 | * that there are zeromapped pages up to and including the last | |
562 | * bss page. | |
563 | */ | |
564 | padzero(elf_bss); | |
565 | elf_bss = X86_ELF_PAGESTART(elf_bss + ALPHA_PAGE_SIZE - 1); /* What we have mapped so far */ | |
566 | ||
567 | /* Map the last of the bss segment */ | |
568 | if (last_bss > elf_bss) { | |
569 | mmap4k(elf_bss, last_bss-elf_bss, | |
570 | PROT_READ|PROT_WRITE|PROT_EXEC, | |
571 | MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
572 | } | |
573 | free(elf_phdata); | |
574 | ||
575 | *interp_load_addr = load_addr; | |
576 | return ((unsigned long) interp_elf_ex->e_entry) + load_addr; | |
577 | } | |
578 | ||
579 | ||
580 | ||
b17780d5 FB |
581 | static int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, |
582 | struct image_info * info) | |
31e31b8a FB |
583 | { |
584 | struct elfhdr elf_ex; | |
585 | struct elfhdr interp_elf_ex; | |
586 | struct exec interp_ex; | |
587 | int interpreter_fd = -1; /* avoid warning */ | |
588 | unsigned long load_addr; | |
589 | int load_addr_set = 0; | |
590 | unsigned int interpreter_type = INTERPRETER_NONE; | |
591 | unsigned char ibcs2_interpreter; | |
592 | int i; | |
593 | void * mapped_addr; | |
594 | struct elf_phdr * elf_ppnt; | |
595 | struct elf_phdr *elf_phdata; | |
596 | unsigned long elf_bss, k, elf_brk; | |
597 | int retval; | |
598 | char * elf_interpreter; | |
599 | unsigned long elf_entry, interp_load_addr = 0; | |
600 | int status; | |
601 | unsigned long start_code, end_code, end_data; | |
602 | unsigned long elf_stack; | |
603 | char passed_fileno[6]; | |
604 | ||
605 | ibcs2_interpreter = 0; | |
606 | status = 0; | |
607 | load_addr = 0; | |
608 | elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */ | |
609 | #ifdef BSWAP_NEEDED | |
610 | bswap_ehdr(&elf_ex); | |
611 | #endif | |
612 | ||
613 | if (elf_ex.e_ident[0] != 0x7f || | |
614 | strncmp(&elf_ex.e_ident[1], "ELF",3) != 0) { | |
615 | return -ENOEXEC; | |
616 | } | |
617 | ||
31e31b8a FB |
618 | /* First of all, some simple consistency checks */ |
619 | if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) || | |
620 | (! elf_check_arch(elf_ex.e_machine))) { | |
621 | return -ENOEXEC; | |
622 | } | |
623 | ||
624 | /* Now read in all of the header information */ | |
625 | ||
626 | elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum); | |
627 | if (elf_phdata == NULL) { | |
628 | return -ENOMEM; | |
629 | } | |
630 | ||
631 | retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET); | |
632 | if(retval > 0) { | |
633 | retval = read(bprm->fd, (char *) elf_phdata, | |
634 | elf_ex.e_phentsize * elf_ex.e_phnum); | |
635 | } | |
636 | ||
637 | if (retval < 0) { | |
638 | perror("load_elf_binary"); | |
639 | exit(-1); | |
640 | free (elf_phdata); | |
641 | return -errno; | |
642 | } | |
643 | ||
b17780d5 FB |
644 | #ifdef BSWAP_NEEDED |
645 | elf_ppnt = elf_phdata; | |
646 | for (i=0; i<elf_ex.e_phnum; i++, elf_ppnt++) { | |
647 | bswap_phdr(elf_ppnt); | |
648 | } | |
649 | #endif | |
31e31b8a FB |
650 | elf_ppnt = elf_phdata; |
651 | ||
652 | elf_bss = 0; | |
653 | elf_brk = 0; | |
654 | ||
655 | ||
656 | elf_stack = ~0UL; | |
657 | elf_interpreter = NULL; | |
658 | start_code = ~0UL; | |
659 | end_code = 0; | |
660 | end_data = 0; | |
661 | ||
662 | for(i=0;i < elf_ex.e_phnum; i++) { | |
663 | if (elf_ppnt->p_type == PT_INTERP) { | |
664 | if ( elf_interpreter != NULL ) | |
665 | { | |
666 | free (elf_phdata); | |
667 | free(elf_interpreter); | |
668 | close(bprm->fd); | |
669 | return -EINVAL; | |
670 | } | |
671 | ||
672 | /* This is the program interpreter used for | |
673 | * shared libraries - for now assume that this | |
674 | * is an a.out format binary | |
675 | */ | |
676 | ||
d691f669 FB |
677 | elf_interpreter = (char *)malloc(elf_ppnt->p_filesz+ |
678 | strlen(bprm->interp_prefix)); | |
31e31b8a FB |
679 | |
680 | if (elf_interpreter == NULL) { | |
681 | free (elf_phdata); | |
682 | close(bprm->fd); | |
683 | return -ENOMEM; | |
684 | } | |
685 | ||
d691f669 | 686 | strcpy(elf_interpreter, bprm->interp_prefix); |
31e31b8a FB |
687 | retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET); |
688 | if(retval >= 0) { | |
689 | retval = read(bprm->fd, | |
d691f669 | 690 | elf_interpreter+strlen(bprm->interp_prefix), |
31e31b8a FB |
691 | elf_ppnt->p_filesz); |
692 | } | |
693 | if(retval < 0) { | |
694 | perror("load_elf_binary2"); | |
695 | exit(-1); | |
696 | } | |
697 | ||
698 | /* If the program interpreter is one of these two, | |
699 | then assume an iBCS2 image. Otherwise assume | |
700 | a native linux image. */ | |
701 | ||
702 | /* JRP - Need to add X86 lib dir stuff here... */ | |
703 | ||
704 | if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 || | |
705 | strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) { | |
706 | ibcs2_interpreter = 1; | |
707 | } | |
708 | ||
709 | #if 0 | |
710 | printf("Using ELF interpreter %s\n", elf_interpreter); | |
711 | #endif | |
712 | if (retval >= 0) { | |
713 | retval = open(elf_interpreter, O_RDONLY); | |
714 | if(retval >= 0) { | |
715 | interpreter_fd = retval; | |
716 | } | |
717 | else { | |
718 | perror(elf_interpreter); | |
719 | exit(-1); | |
720 | /* retval = -errno; */ | |
721 | } | |
722 | } | |
723 | ||
724 | if (retval >= 0) { | |
725 | retval = lseek(interpreter_fd, 0, SEEK_SET); | |
726 | if(retval >= 0) { | |
727 | retval = read(interpreter_fd,bprm->buf,128); | |
728 | } | |
729 | } | |
730 | if (retval >= 0) { | |
731 | interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */ | |
732 | interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */ | |
733 | } | |
734 | if (retval < 0) { | |
735 | perror("load_elf_binary3"); | |
736 | exit(-1); | |
737 | free (elf_phdata); | |
738 | free(elf_interpreter); | |
739 | close(bprm->fd); | |
740 | return retval; | |
741 | } | |
742 | } | |
743 | elf_ppnt++; | |
744 | } | |
745 | ||
746 | /* Some simple consistency checks for the interpreter */ | |
747 | if (elf_interpreter){ | |
748 | interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT; | |
749 | ||
750 | /* Now figure out which format our binary is */ | |
751 | if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) && | |
752 | (N_MAGIC(interp_ex) != QMAGIC)) { | |
753 | interpreter_type = INTERPRETER_ELF; | |
754 | } | |
755 | ||
756 | if (interp_elf_ex.e_ident[0] != 0x7f || | |
757 | strncmp(&interp_elf_ex.e_ident[1], "ELF",3) != 0) { | |
758 | interpreter_type &= ~INTERPRETER_ELF; | |
759 | } | |
760 | ||
761 | if (!interpreter_type) { | |
762 | free(elf_interpreter); | |
763 | free(elf_phdata); | |
764 | close(bprm->fd); | |
765 | return -ELIBBAD; | |
766 | } | |
767 | } | |
768 | ||
769 | /* OK, we are done with that, now set up the arg stuff, | |
770 | and then start this sucker up */ | |
771 | ||
772 | if (!bprm->sh_bang) { | |
773 | char * passed_p; | |
774 | ||
775 | if (interpreter_type == INTERPRETER_AOUT) { | |
776 | sprintf(passed_fileno, "%d", bprm->fd); | |
777 | passed_p = passed_fileno; | |
778 | ||
779 | if (elf_interpreter) { | |
780 | bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p); | |
781 | bprm->argc++; | |
782 | } | |
783 | } | |
784 | if (!bprm->p) { | |
785 | if (elf_interpreter) { | |
786 | free(elf_interpreter); | |
787 | } | |
788 | free (elf_phdata); | |
789 | close(bprm->fd); | |
790 | return -E2BIG; | |
791 | } | |
792 | } | |
793 | ||
794 | /* OK, This is the point of no return */ | |
795 | info->end_data = 0; | |
796 | info->end_code = 0; | |
797 | info->start_mmap = (unsigned long)ELF_START_MMAP; | |
798 | info->mmap = 0; | |
799 | elf_entry = (unsigned long) elf_ex.e_entry; | |
800 | ||
801 | /* Do this so that we can load the interpreter, if need be. We will | |
802 | change some of these later */ | |
803 | info->rss = 0; | |
804 | bprm->p = setup_arg_pages(bprm->p, bprm, info); | |
805 | info->start_stack = bprm->p; | |
806 | ||
807 | /* Now we do a little grungy work by mmaping the ELF image into | |
808 | * the correct location in memory. At this point, we assume that | |
809 | * the image should be loaded at fixed address, not at a variable | |
810 | * address. | |
811 | */ | |
812 | ||
813 | ||
814 | ||
815 | for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) { | |
816 | if (elf_ppnt->p_type == PT_LOAD) { | |
817 | int elf_prot = 0; | |
818 | if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ; | |
819 | if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE; | |
820 | if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; | |
821 | ||
822 | mapped_addr = mmap4k(X86_ELF_PAGESTART(elf_ppnt->p_vaddr), | |
823 | (elf_ppnt->p_filesz + | |
824 | X86_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)), | |
825 | elf_prot, | |
826 | (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE), | |
827 | bprm->fd, | |
828 | (elf_ppnt->p_offset - | |
829 | X86_ELF_PAGEOFFSET(elf_ppnt->p_vaddr))); | |
830 | ||
831 | if((unsigned long)mapped_addr == 0xffffffffffffffff) { | |
832 | perror("mmap"); | |
833 | exit(-1); | |
834 | } | |
835 | ||
836 | ||
837 | ||
838 | #ifdef LOW_ELF_STACK | |
839 | if (X86_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack) | |
840 | elf_stack = X86_ELF_PAGESTART(elf_ppnt->p_vaddr); | |
841 | #endif | |
842 | ||
843 | if (!load_addr_set) { | |
844 | load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset; | |
845 | load_addr_set = 1; | |
846 | } | |
847 | k = elf_ppnt->p_vaddr; | |
848 | if (k < start_code) start_code = k; | |
849 | k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz; | |
850 | if (k > elf_bss) elf_bss = k; | |
851 | #if 1 | |
852 | if ((elf_ppnt->p_flags & PF_X) && end_code < k) | |
853 | #else | |
854 | if ( !(elf_ppnt->p_flags & PF_W) && end_code < k) | |
855 | #endif | |
856 | end_code = k; | |
857 | if (end_data < k) end_data = k; | |
858 | k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; | |
859 | if (k > elf_brk) elf_brk = k; | |
860 | } | |
861 | } | |
862 | ||
863 | if (elf_interpreter) { | |
864 | if (interpreter_type & 1) { | |
865 | elf_entry = load_aout_interp(&interp_ex, interpreter_fd); | |
866 | } | |
867 | else if (interpreter_type & 2) { | |
868 | elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd, | |
869 | &interp_load_addr); | |
870 | } | |
871 | ||
872 | close(interpreter_fd); | |
873 | free(elf_interpreter); | |
874 | ||
875 | if (elf_entry == ~0UL) { | |
876 | printf("Unable to load interpreter\n"); | |
877 | free(elf_phdata); | |
878 | exit(-1); | |
879 | return 0; | |
880 | } | |
881 | } | |
882 | ||
883 | free(elf_phdata); | |
884 | ||
885 | if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd); | |
886 | info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX); | |
887 | ||
888 | #ifdef LOW_ELF_STACK | |
889 | info->start_stack = bprm->p = elf_stack - 4; | |
890 | #endif | |
891 | bprm->p = (unsigned long) | |
892 | create_elf_tables((char *)bprm->p, | |
893 | bprm->argc, | |
894 | bprm->envc, | |
895 | (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL), | |
896 | load_addr, | |
897 | interp_load_addr, | |
898 | (interpreter_type == INTERPRETER_AOUT ? 0 : 1), | |
899 | info); | |
900 | if (interpreter_type == INTERPRETER_AOUT) | |
901 | info->arg_start += strlen(passed_fileno) + 1; | |
902 | info->start_brk = info->brk = elf_brk; | |
903 | info->end_code = end_code; | |
904 | info->start_code = start_code; | |
905 | info->end_data = end_data; | |
906 | info->start_stack = bprm->p; | |
907 | ||
908 | /* Calling set_brk effectively mmaps the pages that we need for the bss and break | |
909 | sections */ | |
910 | set_brk(elf_bss, elf_brk); | |
911 | ||
912 | padzero(elf_bss); | |
913 | ||
914 | #if 0 | |
915 | printf("(start_brk) %x\n" , info->start_brk); | |
916 | printf("(end_code) %x\n" , info->end_code); | |
917 | printf("(start_code) %x\n" , info->start_code); | |
918 | printf("(end_data) %x\n" , info->end_data); | |
919 | printf("(start_stack) %x\n" , info->start_stack); | |
920 | printf("(brk) %x\n" , info->brk); | |
921 | #endif | |
922 | ||
923 | if ( info->personality == PER_SVR4 ) | |
924 | { | |
925 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, | |
926 | and some applications "depend" upon this behavior. | |
927 | Since we do not have the power to recompile these, we | |
928 | emulate the SVr4 behavior. Sigh. */ | |
929 | mapped_addr = mmap4k(NULL, ALPHA_PAGE_SIZE, PROT_READ | PROT_EXEC, | |
930 | MAP_FIXED | MAP_PRIVATE, -1, 0); | |
931 | } | |
932 | ||
933 | #ifdef ELF_PLAT_INIT | |
934 | /* | |
935 | * The ABI may specify that certain registers be set up in special | |
936 | * ways (on i386 %edx is the address of a DT_FINI function, for | |
937 | * example. This macro performs whatever initialization to | |
938 | * the regs structure is required. | |
939 | */ | |
940 | ELF_PLAT_INIT(regs); | |
941 | #endif | |
942 | ||
943 | ||
944 | info->entry = elf_entry; | |
945 | ||
946 | return 0; | |
947 | } | |
948 | ||
949 | ||
950 | ||
d691f669 FB |
951 | int elf_exec(const char *interp_prefix, |
952 | const char * filename, char ** argv, char ** envp, | |
b17780d5 | 953 | struct target_pt_regs * regs, struct image_info *infop) |
31e31b8a FB |
954 | { |
955 | struct linux_binprm bprm; | |
956 | int retval; | |
957 | int i; | |
958 | ||
959 | bprm.p = X86_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); | |
960 | for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ | |
961 | bprm.page[i] = 0; | |
962 | retval = open(filename, O_RDONLY); | |
963 | if (retval == -1) { | |
964 | perror(filename); | |
965 | exit(-1); | |
966 | /* return retval; */ | |
967 | } | |
968 | else { | |
969 | bprm.fd = retval; | |
970 | } | |
d691f669 | 971 | bprm.interp_prefix = (char *)interp_prefix; |
31e31b8a FB |
972 | bprm.filename = (char *)filename; |
973 | bprm.sh_bang = 0; | |
974 | bprm.loader = 0; | |
975 | bprm.exec = 0; | |
976 | bprm.dont_iput = 0; | |
977 | bprm.argc = count(argv); | |
978 | bprm.envc = count(envp); | |
979 | ||
980 | retval = prepare_binprm(&bprm); | |
981 | ||
982 | if(retval>=0) { | |
983 | bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p); | |
984 | bprm.exec = bprm.p; | |
985 | bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p); | |
986 | bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p); | |
987 | if (!bprm.p) { | |
988 | retval = -E2BIG; | |
989 | } | |
990 | } | |
991 | ||
992 | if(retval>=0) { | |
993 | retval = load_elf_binary(&bprm,regs,infop); | |
994 | } | |
995 | if(retval>=0) { | |
996 | /* success. Initialize important registers */ | |
997 | regs->esp = infop->start_stack; | |
998 | regs->eip = infop->entry; | |
999 | return retval; | |
1000 | } | |
1001 | ||
1002 | /* Something went wrong, return the inode and free the argument pages*/ | |
1003 | for (i=0 ; i<MAX_ARG_PAGES ; i++) { | |
1004 | free_page((void *)bprm.page[i]); | |
1005 | } | |
1006 | return(retval); | |
1007 | } | |
1008 | ||
1009 | ||
1010 | static int load_aout_interp(void * exptr, int interp_fd) | |
1011 | { | |
1012 | printf("a.out interpreter not yet supported\n"); | |
1013 | return(0); | |
1014 | } | |
1015 |