1 /** $MirOS: src/sys/kern/exec_elf.c,v 1.4 2006/01/18 09:22:07 tg Exp $ */
2 /* $OpenBSD: exec_elf.c,v 1.49 2003/11/03 19:58:22 tedu Exp $ */
3
4 /*
5 * Copyright (c) 1996 Per Fogelstrom
6 * All rights reserved.
7 *
8 * Copyright (c) 1994 Christos Zoulas
9 * All rights reserved.
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. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/exec.h>
44 #include <sys/exec_elf.h>
45 #include <sys/exec_olf.h>
46 #include <sys/file.h>
47 #include <sys/syscall.h>
48 #include <sys/signalvar.h>
49 #include <sys/stat.h>
50
51 #include <sys/mman.h>
52 #include <uvm/uvm_extern.h>
53
54 #include <machine/cpu.h>
55 #include <machine/reg.h>
56 #include <machine/exec.h>
57
58 #ifdef COMPAT_LINUX
59 #include <compat/linux/linux_exec.h>
60 #endif
61
62 #ifdef COMPAT_OPENBSD
63 #include <compat/openbsd/compat_openbsd.h>
64 #endif
65
ELFNAME(probe_entry)66 struct ELFNAME(probe_entry) {
67 int (*func)(struct proc *, struct exec_package *, char *,
68 u_long *, u_int8_t *);
69 int os_mask;
70 } ELFNAME(probes)[] = {
71 /* XXX - bogus, shouldn't be size independent.. */
72 #ifdef COMPAT_OPENBSD
73 { openbsd_elf_probe, 1 << OOS_OPENBSD },
74 #endif
75 #ifdef COMPAT_LINUX
76 { linux_elf_probe, 1 << OOS_LINUX },
77 #endif
78 { 0, 1 << OOS_MIRBSD }
79 };
80
81 int ELFNAME(load_file)(struct proc *, char *, struct exec_package *,
82 struct elf_args *, Elf_Addr *);
83 int ELFNAME(check_header)(Elf_Ehdr *, int);
84 int ELFNAME(olf_check_header)(Elf_Ehdr *, int, u_int8_t *);
85 int ELFNAME(read_from)(struct proc *, struct vnode *, u_long, caddr_t, int);
86 void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
87 Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int);
88
89 extern char sigcode[], esigcode[];
90 #ifdef SYSCALL_DEBUG
91 extern char *syscallnames[];
92 #endif
93
94 /* round up and down to page boundaries. */
95 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
96 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
97
98 /*
99 * We limit the number of program headers to 32, this should
100 * be a reasonable limit for ELF, the most we have seen so far is 12
101 */
102 #define ELF_MAX_VALID_PHDR 32
103
104 /*
105 * This is the basic elf emul. elf_probe_funcs may change to other emuls.
106 */
107 struct emul ELFNAMEEND(emul) = {
108 "native",
109 NULL,
110 sendsig,
111 SYS_syscall,
112 SYS_MAXSYSCALL,
113 sysent,
114 #ifdef SYSCALL_DEBUG
115 syscallnames,
116 #else
117 NULL,
118 #endif
119 sizeof (AuxInfo) * ELF_AUX_ENTRIES,
120 ELFNAME(copyargs),
121 setregs,
122 ELFNAME2(exec,fixup),
123 sigcode,
124 esigcode,
125 EMUL_ENABLED | EMUL_NATIVE,
126 };
127
128 /*
129 * Copy arguments onto the stack in the normal way, but add some
130 * space for extra information in case of dynamic binding.
131 */
132 void *
ELFNAME(copyargs)133 ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
134 void *stack, void *argp)
135 {
136 stack = copyargs(pack, arginfo, stack, argp);
137 if (!stack)
138 return (NULL);
139
140 /*
141 * Push space for extra arguments on the stack needed by
142 * dynamically linked binaries.
143 */
144 if (pack->ep_interp != NULL) {
145 pack->ep_emul_argp = stack;
146 stack += ELF_AUX_ENTRIES * sizeof (AuxInfo);
147 }
148 return (stack);
149 }
150
151 #ifndef COMPAT_LINUX
152 #define CHECK_ET (ehdr->e_type != type)
153 #else
154 /* ld-linux.so.2 kludge */
155 #define CHECK_ET ((ehdr->e_type != type) && \
156 ((type != ET_EXEC) || (ehdr->e_type != ET_DYN)))
157 #endif
158
159 /*
160 * Check header for validity; return 0 for ok, ENOEXEC if error
161 */
162 int
ELFNAME(check_header)163 ELFNAME(check_header)(Elf_Ehdr *ehdr, int type)
164 {
165 /*
166 * We need to check magic, class size, endianess, and version before
167 * we look at the rest of the Elf_Ehdr structure. These few elements
168 * are represented in a machine independant fashion.
169 */
170 if (!IS_ELF(*ehdr) ||
171 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
172 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
173 ehdr->e_ident[EI_VERSION] != ELF_TARG_VER)
174 return (ENOEXEC);
175
176 /* Now check the machine dependant header */
177 if (ehdr->e_machine != ELF_TARG_MACH ||
178 ehdr->e_version != ELF_TARG_VER)
179 return (ENOEXEC);
180
181 /* Check the type */
182 if CHECK_ET
183 return (ENOEXEC);
184
185 /* Don't allow an insane amount of sections. */
186 if (ehdr->e_phnum > ELF_MAX_VALID_PHDR)
187 return (ENOEXEC);
188
189 return (0);
190 }
191
192 /*
193 * Check header for validity; return 0 for ok, ENOEXEC if error.
194 * Remember OS tag for callers sake.
195 */
196 int
ELFNAME(olf_check_header)197 ELFNAME(olf_check_header)(Elf_Ehdr *ehdr, int type, u_int8_t *os)
198 {
199 int i;
200
201 /*
202 * We need to check magic, class size, endianess, version, and OS
203 * before we look at the rest of the Elf_Ehdr structure. These few
204 * elements are represented in a machine independant fashion.
205 */
206 if (!IS_OLF(*ehdr) ||
207 ehdr->e_ident[OI_CLASS] != ELF_TARG_CLASS ||
208 ehdr->e_ident[OI_DATA] != ELF_TARG_DATA ||
209 ehdr->e_ident[OI_VERSION] != ELF_TARG_VER)
210 return (ENOEXEC);
211
212 for (i = 0;
213 i < sizeof(ELFNAME(probes)) / sizeof(ELFNAME(probes)[0]);
214 i++) {
215 if ((1 << ehdr->e_ident[OI_OS]) & ELFNAME(probes)[i].os_mask)
216 goto os_ok;
217 }
218 return (ENOEXEC);
219
220 os_ok:
221 /* Now check the machine dependant header */
222 if (ehdr->e_machine != ELF_TARG_MACH ||
223 ehdr->e_version != ELF_TARG_VER)
224 return (ENOEXEC);
225
226 /* Check the type */
227 if CHECK_ET
228 return (ENOEXEC);
229
230 /* Don't allow an insane amount of sections. */
231 if (ehdr->e_phnum > ELF_MAX_VALID_PHDR)
232 return (ENOEXEC);
233
234 *os = ehdr->e_ident[OI_OS];
235 return (0);
236 }
237 #undef CHECK_ET
238
239 /*
240 * Load a psection at the appropriate address
241 */
242 void
ELFNAME(load_psection)243 ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
244 Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags)
245 {
246 u_long uaddr, msize, lsize, psize, rm, rf;
247 long diff, offset, bdiff;
248 Elf_Addr base;
249
250 /*
251 * If the user specified an address, then we load there.
252 */
253 if (*addr != ELFDEFNNAME(NO_ADDR)) {
254 if (ph->p_align > 1) {
255 *addr = ELF_TRUNC(*addr, ph->p_align);
256 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
257 /* page align vaddr */
258 base = *addr + trunc_page(ph->p_vaddr)
259 - ELF_TRUNC(ph->p_vaddr, ph->p_align);
260
261 bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
262
263 } else
264 diff = 0;
265 } else {
266 *addr = uaddr = ph->p_vaddr;
267 if (ph->p_align > 1)
268 *addr = ELF_TRUNC(uaddr, ph->p_align);
269 base = trunc_page(uaddr);
270 bdiff = uaddr - base;
271 diff = uaddr - *addr;
272 }
273
274 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
275 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
276 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
277
278 msize = ph->p_memsz + diff;
279 offset = ph->p_offset - bdiff;
280 lsize = ph->p_filesz + bdiff;
281 psize = round_page(lsize);
282
283 /*
284 * Because the pagedvn pager can't handle zero fill of the last
285 * data page if it's not page aligned we map the last page readvn.
286 */
287 if (ph->p_flags & PF_W) {
288 psize = trunc_page(lsize);
289 if (psize > 0)
290 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp,
291 offset, *prot, flags);
292 if (psize != lsize) {
293 NEW_VMCMD2(vcset, vmcmd_map_readvn, lsize - psize,
294 base + psize, vp, offset + psize, *prot, flags);
295 }
296 } else {
297 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, offset,
298 *prot, flags);
299 }
300
301 /*
302 * Check if we need to extend the size of the segment
303 */
304 rm = round_page(*addr + ph->p_memsz + diff);
305 rf = round_page(*addr + ph->p_filesz + diff);
306
307 if (rm != rf) {
308 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0,
309 *prot, flags);
310 }
311 *size = msize;
312 }
313
314 /*
315 * Read from vnode into buffer at offset.
316 */
317 int
ELFNAME(read_from)318 ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, caddr_t buf,
319 int size)
320 {
321 int error;
322 size_t resid;
323
324 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
325 0, p->p_ucred, &resid, p)) != 0)
326 return error;
327 /*
328 * See if we got all of it
329 */
330 if (resid != 0)
331 return (ENOEXEC);
332 return (0);
333 }
334
335 /*
336 * Load a file (interpreter/library) pointed to by path [stolen from
337 * coff_load_shlib()]. Made slightly generic so it might be used externally.
338 */
339 int
ELFNAME(load_file)340 ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp,
341 struct elf_args *ap, Elf_Addr *last)
342 {
343 int error, i;
344 struct nameidata nd;
345 Elf_Ehdr eh;
346 Elf_Phdr *ph = NULL;
347 u_long phsize;
348 char *bp = NULL;
349 Elf_Addr addr;
350 struct vnode *vp;
351 u_int8_t os; /* Just a dummy in this routine */
352 Elf_Phdr *base_ph = NULL;
353 struct interp_ld_sec {
354 Elf_Addr vaddr;
355 u_long memsz;
356 } loadmap[ELF_MAX_VALID_PHDR];
357 int nload, idx = 0;
358 Elf_Addr pos = *last;
359 int file_align;
360
361 bp = path;
362 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
363 if ((error = namei(&nd)) != 0) {
364 return (error);
365 }
366 vp = nd.ni_vp;
367 if (vp->v_type != VREG) {
368 error = EACCES;
369 goto bad;
370 }
371 if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
372 goto bad;
373 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
374 error = EACCES;
375 goto bad;
376 }
377 if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0)
378 goto bad1;
379 if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0,
380 (caddr_t)&eh, sizeof(eh))) != 0)
381 goto bad1;
382
383 if (ELFNAME(check_header)(&eh, ET_DYN) &&
384 ELFNAME(olf_check_header)(&eh, ET_DYN, &os)) {
385 error = ENOEXEC;
386 goto bad1;
387 }
388
389 phsize = eh.e_phnum * sizeof(Elf_Phdr);
390 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
391
392 if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph,
393 phsize)) != 0)
394 goto bad1;
395
396 for (i = 0; i < eh.e_phnum; i++) {
397 if (ph[i].p_type == PT_LOAD) {
398 loadmap[idx].vaddr = trunc_page(ph[i].p_vaddr);
399 loadmap[idx].memsz = round_page (ph[i].p_vaddr +
400 ph[i].p_memsz - loadmap[idx].vaddr);
401 file_align = ph[i].p_align;
402 idx++;
403 }
404 }
405 nload = idx;
406
407 /*
408 * If no position to load the interpreter was set by a probe
409 * function, pick the same address that a non-fixed mmap(0, ..)
410 * would (i.e. something safely out of the way).
411 */
412 if (pos == ELFDEFNNAME(NO_ADDR)) {
413 pos = uvm_map_hint(p, VM_PROT_EXECUTE);
414 }
415
416 pos = ELF_ROUND(pos, file_align);
417 *last = epp->ep_interp_pos = pos;
418 for (i = 0; i < nload;/**/) {
419 vaddr_t addr;
420 struct uvm_object *uobj;
421 off_t uoff;
422 size_t size;
423
424 #ifdef this_needs_fixing
425 if (i == 0) {
426 uobj = &vp->v_uvm.u_obj;
427 /* need to fix uoff */
428 } else {
429 #endif
430 uobj = NULL;
431 uoff = 0;
432 #ifdef this_needs_fixing
433 }
434 #endif
435
436 addr = trunc_page(pos + loadmap[i].vaddr);
437 size = round_page(addr + loadmap[i].memsz) - addr;
438
439 /* CRAP - map_findspace does not avoid daddr+MAXDSIZ */
440 if ((addr + size > (vaddr_t)p->p_vmspace->vm_daddr) &&
441 (addr < (vaddr_t)p->p_vmspace->vm_daddr + MAXDSIZ))
442 addr = round_page((vaddr_t)p->p_vmspace->vm_daddr +
443 MAXDSIZ);
444
445 if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size,
446 &addr, uobj, uoff, 0, UVM_FLAG_FIXED) == NULL) {
447 if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size,
448 &addr, uobj, uoff, 0, 0) == NULL) {
449 error = ENOMEM; /* XXX */
450 goto bad1;
451 }
452 }
453 if (addr != pos + loadmap[i].vaddr) {
454 /* base changed. */
455 pos = addr - trunc_page(loadmap[i].vaddr);
456 pos = ELF_ROUND(pos,file_align);
457 epp->ep_interp_pos = *last = pos;
458 i = 0;
459 continue;
460 }
461
462 i++;
463 }
464
465 /*
466 * Load all the necessary sections
467 */
468 for (i = 0; i < eh.e_phnum; i++) {
469 Elf_Addr size = 0;
470 int prot = 0;
471 int flags;
472
473 switch (ph[i].p_type) {
474 case PT_LOAD:
475 if (base_ph == NULL) {
476 flags = VMCMD_BASE;
477 addr = *last;
478 base_ph = &ph[i];
479 } else {
480 flags = VMCMD_RELATIVE;
481 addr = ph[i].p_vaddr - base_ph->p_vaddr;
482 }
483 ELFNAME(load_psection)(&epp->ep_vmcmds, nd.ni_vp,
484 &ph[i], &addr, &size, &prot, flags);
485 /* If entry is within this section it must be text */
486 if (eh.e_entry >= ph[i].p_vaddr &&
487 eh.e_entry < (ph[i].p_vaddr + size)) {
488 epp->ep_entry = addr + eh.e_entry -
489 ELF_TRUNC(ph[i].p_vaddr,ph[i].p_align);
490 ap->arg_interp = addr;
491 }
492 addr += size;
493 break;
494
495 case PT_DYNAMIC:
496 case PT_PHDR:
497 case PT_NOTE:
498 break;
499
500 default:
501 break;
502 }
503 }
504
505 vn_marktext(nd.ni_vp);
506
507 bad1:
508 VOP_CLOSE(nd.ni_vp, FREAD, p->p_ucred, p);
509 bad:
510 if (ph != NULL)
511 free((char *)ph, M_TEMP);
512
513 *last = addr;
514 vput(nd.ni_vp);
515 return (error);
516 }
517
518 /*
519 * Prepare an Elf binary's exec package
520 *
521 * First, set of the various offsets/lengths in the exec package.
522 *
523 * Then, mark the text image busy (so it can be demand paged) or error out if
524 * this is not possible. Finally, set up vmcmds for the text, data, bss, and
525 * stack segments.
526 */
527 int
ELFNAME2(exec,makecmds)528 ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
529 {
530 Elf_Ehdr *eh = epp->ep_hdr;
531 Elf_Phdr *ph, *pp;
532 Elf_Addr phdr = 0;
533 int error, i;
534 char interp[MAXPATHLEN];
535 u_long pos = 0, phsize;
536 u_int8_t os = OOS_NULL;
537 extern struct emul emul_native;
538
539 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
540 return (ENOEXEC);
541
542 if (ELFNAME(check_header)(eh, ET_EXEC) &&
543 ELFNAME(olf_check_header)(eh, ET_EXEC, &os))
544 return (ENOEXEC);
545
546 /*
547 * check if vnode is in open for writing, because we want to demand-
548 * page out of it. if it is, don't do it, for various reasons.
549 */
550 if (epp->ep_vp->v_writecount != 0) {
551 #ifdef DIAGNOSTIC
552 if (epp->ep_vp->v_flag & VTEXT)
553 panic("exec: a VTEXT vnode has writecount != 0");
554 #endif
555 return (ETXTBSY);
556 }
557 /*
558 * Allocate space to hold all the program headers, and read them
559 * from the file
560 */
561 phsize = eh->e_phnum * sizeof(Elf_Phdr);
562 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
563
564 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
565 phsize)) != 0)
566 goto bad;
567
568 epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
569 epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
570
571 interp[0] = '\0';
572
573 for (i = 0; i < eh->e_phnum; i++) {
574 pp = &ph[i];
575 if (pp->p_type == PT_INTERP) {
576 if (pp->p_filesz >= sizeof(interp))
577 goto bad;
578 if ((error = ELFNAME(read_from)(p, epp->ep_vp,
579 pp->p_offset, (caddr_t)interp, pp->p_filesz)) != 0)
580 goto bad;
581 break;
582 }
583 }
584
585 /*
586 * OK, we want a slightly different twist of the
587 * standard emulation package for "real" elf.
588 */
589 epp->ep_emul = &ELFNAMEEND(emul);
590 pos = ELFDEFNNAME(NO_ADDR);
591
592 /*
593 * On the same architecture, we may be emulating different systems.
594 * See which one will accept this executable.
595 *
596 * Probe functions would normally see if the interpreter (if any)
597 * exists. Emulation packages may possibly replace the interpreter in
598 * interp[] with a changed path (/emul/xxx/<path>), and also
599 * set the ep_emul field in the exec package structure.
600 */
601 error = ENOEXEC;
602 p->p_os = OOS_MIRBSD;
603 #ifdef NATIVE_EXEC_ELF
604 /* recognise MirOS BSD executables */
605 if (!ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "MirOS BSD", 10, 4)) {
606 goto native;
607 }
608 /* currently, MirBSD(TM) is (nearly) the same as MirOS BSD */
609 if (!ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "MirBSD", 7, 4)) {
610 goto native;
611 }
612 #endif
613 for (i = 0;
614 i < sizeof(ELFNAME(probes)) / sizeof(ELFNAME(probes)[0]) && error;
615 i++) {
616 if (os == OOS_NULL || ((1 << os) & ELFNAME(probes)[i].os_mask))
617 error = ELFNAME(probes)[i].func ?
618 (*ELFNAME(probes)[i].func)(p, epp, interp, &pos, &os) :
619 0;
620 }
621 if (!error)
622 p->p_os = os;
623 #ifndef NATIVE_EXEC_ELF
624 else
625 goto bad;
626 #else
627 native:
628 #endif /* NATIVE_EXEC_ELF */
629 /*
630 * Load all the necessary sections
631 */
632 for (i = 0; i < eh->e_phnum; i++) {
633 Elf_Addr addr = ELFDEFNNAME(NO_ADDR), size = 0;
634 int prot = 0;
635
636 pp = &ph[i];
637
638 switch (ph[i].p_type) {
639 case PT_LOAD:
640 /*
641 * Calculates size of text and data segments
642 * by starting at first and going to end of last.
643 * 'rwx' sections are treated as data, except for
644 * executables running on emulation if the entry
645 * point is in this section (this is gross).
646 * this is correct for BSS_PLT, but may not be
647 * for DATA_PLT, is fine for TEXT_PLT.
648 */
649 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
650 &ph[i], &addr, &size, &prot, 0);
651 /*
652 * Decide whether it's text or data by looking
653 * at the protection of the section
654 */
655 if ((prot & VM_PROT_WRITE) &&
656 (epp->ep_emul == &emul_native ||
657 (eh->e_entry < addr ||
658 eh->e_entry >= (addr + size)))) {
659 /* data section */
660 if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) {
661 epp->ep_daddr = addr;
662 epp->ep_dsize = size;
663 } else {
664 if (addr < epp->ep_daddr) {
665 epp->ep_dsize =
666 epp->ep_dsize +
667 epp->ep_daddr -
668 addr;
669 epp->ep_daddr = addr;
670 } else
671 epp->ep_dsize = addr+size -
672 epp->ep_daddr;
673 }
674 } else if (prot & VM_PROT_EXECUTE) {
675 /* text section */
676 if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) {
677 epp->ep_taddr = addr;
678 epp->ep_tsize = size;
679 } else {
680 if (addr < epp->ep_taddr) {
681 epp->ep_tsize =
682 epp->ep_tsize +
683 epp->ep_taddr -
684 addr;
685 epp->ep_taddr = addr;
686 } else
687 epp->ep_tsize = addr+size -
688 epp->ep_taddr;
689 }
690 }
691 break;
692
693 case PT_SHLIB:
694 error = ENOEXEC;
695 goto bad;
696
697 case PT_INTERP:
698 /* Already did this one */
699 case PT_DYNAMIC:
700 case PT_NOTE:
701 break;
702
703 case PT_PHDR:
704 /* Note address of program headers (in text segment) */
705 phdr = pp->p_vaddr;
706 break;
707
708 default:
709 /*
710 * Not fatal, we don't need to understand everything
711 * :-)
712 */
713 break;
714 }
715 }
716
717 /*
718 * Check if we found a dynamically linked binary and arrange to load
719 * it's interpreter when the exec file is released.
720 */
721 if (interp[0]) {
722 char *ip;
723 struct elf_args *ap;
724
725 ip = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
726 ap = (struct elf_args *)
727 malloc(sizeof(struct elf_args), M_TEMP, M_WAITOK);
728
729 bcopy(interp, ip, MAXPATHLEN);
730 epp->ep_interp = ip;
731 epp->ep_interp_pos = pos;
732
733 ap->arg_phaddr = phdr;
734 ap->arg_phentsize = eh->e_phentsize;
735 ap->arg_phnum = eh->e_phnum;
736 ap->arg_entry = eh->e_entry;
737 ap->arg_os = os;
738
739 epp->ep_emul_arg = ap;
740 epp->ep_entry = eh->e_entry; /* keep check_exec() happy */
741 } else {
742 epp->ep_interp = NULL;
743 epp->ep_entry = eh->e_entry;
744 }
745
746 free((char *)ph, M_TEMP);
747 vn_marktext(epp->ep_vp);
748 return (exec_setup_stack(p, epp));
749
750 bad:
751 free((char *)ph, M_TEMP);
752 kill_vmcmds(&epp->ep_vmcmds);
753 return (ENOEXEC);
754 }
755
756 /*
757 * Phase II of load. It is now safe to load the interpreter. Info collected
758 * when loading the program is available for setup of the interpreter.
759 */
760 int
ELFNAME2(exec,fixup)761 ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp)
762 {
763 char *interp;
764 int error;
765 struct elf_args *ap;
766 AuxInfo ai[ELF_AUX_ENTRIES], *a;
767 Elf_Addr pos = epp->ep_interp_pos;
768
769 if (epp->ep_interp == NULL) {
770 return (0);
771 }
772
773 interp = (char *)epp->ep_interp;
774 ap = (struct elf_args *)epp->ep_emul_arg;
775
776 if ((error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) {
777 free((char *)ap, M_TEMP);
778 free((char *)interp, M_TEMP);
779 kill_vmcmds(&epp->ep_vmcmds);
780 return (error);
781 }
782 /*
783 * We have to do this ourselves...
784 */
785 error = exec_process_vmcmds(p, epp);
786
787 /*
788 * Push extra arguments on the stack needed by dynamically
789 * linked binaries
790 */
791 if (error == 0) {
792 a = ai;
793
794 a->au_id = AUX_phdr;
795 a->au_v = ap->arg_phaddr;
796 a++;
797
798 a->au_id = AUX_phent;
799 a->au_v = ap->arg_phentsize;
800 a++;
801
802 a->au_id = AUX_phnum;
803 a->au_v = ap->arg_phnum;
804 a++;
805
806 a->au_id = AUX_pagesz;
807 a->au_v = PAGE_SIZE;
808 a++;
809
810 a->au_id = AUX_base;
811 a->au_v = ap->arg_interp;
812 a++;
813
814 a->au_id = AUX_flags;
815 a->au_v = 0;
816 a++;
817
818 a->au_id = AUX_entry;
819 a->au_v = ap->arg_entry;
820 a++;
821
822 a->au_id = AUX_null;
823 a->au_v = 0;
824 a++;
825
826 error = copyout(ai, epp->ep_emul_argp, sizeof ai);
827 }
828 free((char *)ap, M_TEMP);
829 free((char *)interp, M_TEMP);
830 return (error);
831 }
832
833 /*
834 * Older ELF binaries use EI_ABIVERSION (formerly EI_BRAND) to brand
835 * executables. Newer ELF binaries use EI_OSABI instead.
836 */
837 char *
ELFNAME(check_brand)838 ELFNAME(check_brand)(Elf_Ehdr *eh)
839 {
840 if (eh->e_ident[EI_ABIVERSION] == '\0')
841 return (NULL);
842 return (&eh->e_ident[EI_ABIVERSION]);
843 }
844
845 int
ELFNAME(os_pt_note)846 ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh,
847 char *os_name, size_t name_size, size_t desc_size)
848 {
849 Elf_Phdr *hph, *ph;
850 Elf_Note *np = NULL;
851 size_t phsize;
852 int error;
853
854 phsize = eh->e_phnum * sizeof(Elf_Phdr);
855 hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
856 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
857 (caddr_t)hph, phsize)) != 0)
858 goto out1;
859
860 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) {
861 if (ph->p_type != PT_NOTE ||
862 ph->p_filesz > 1024 ||
863 ph->p_filesz < sizeof(Elf_Note) + name_size)
864 continue;
865
866 np = (Elf_Note *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
867 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
868 (caddr_t)np, ph->p_filesz)) != 0)
869 goto out2;
870
871 if (np->type != 1) {
872 free(np, M_TEMP);
873 np = NULL;
874 continue;
875 }
876
877 /* Check the name and description sizes. */
878 if (np->namesz != name_size ||
879 np->descsz != desc_size)
880 goto out3;
881
882 if (bcmp((np + 1), os_name, name_size))
883 goto out3;
884
885 /* XXX: We could check for the specific emulation here */
886 /* All checks succeeded. */
887 error = 0;
888 goto out2;
889 }
890
891 out3:
892 error = ENOEXEC;
893 out2:
894 if (np)
895 free(np, M_TEMP);
896 out1:
897 free(hph, M_TEMP);
898 return error;
899 }
900