diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2017-02-08 05:09:27 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2017-02-08 05:09:27 +0000 |
commit | dce59fe6e1e128ed03c16e493686ecd655007b83 (patch) | |
tree | 1a0af8032f920fef5e6b02bd94aa273279586f40 /sys/kern | |
parent | eebfc47f73667f8b137ff4b393a24084a2a944fc (diff) |
In exec_elf.c: expand ELFNAME(), ELFNAME2(), and ELFNAMEEND() except
leaving out the size, so that
ELFNAME2(exec,makecmds)
becomes
exec_elf_makecmds
instead of
exec_elf{32,64}_makecmds
and then delete the ELFNAME2() and ELFNAMEEND() macros.
Move the prototypes for functions local to exec_elf.c to there from
exec_elf.h.
Simplify the SMALL_KERNEL conditionals around the ELF coredump code.
Change exec_conf.c to use the size-generic names and macros
Remove exec_elf{32,64}.c and just build exec_elf.c; delete the
_KERN_DO_ELF and _KERN_DO_ELF64 #defines.
ok jca@, encouragement from deraadt@ and tom@
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/exec_conf.c | 12 | ||||
-rw-r--r-- | sys/kern/exec_elf.c | 155 | ||||
-rw-r--r-- | sys/kern/exec_elf32.c | 11 | ||||
-rw-r--r-- | sys/kern/exec_elf64.c | 10 |
4 files changed, 79 insertions, 109 deletions
diff --git a/sys/kern/exec_conf.c b/sys/kern/exec_conf.c index 209609062cc..20465504c31 100644 --- a/sys/kern/exec_conf.c +++ b/sys/kern/exec_conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exec_conf.c,v 1.34 2016/02/28 15:46:18 naddy Exp $ */ +/* $OpenBSD: exec_conf.c,v 1.35 2017/02/08 05:09:25 guenther Exp $ */ /* $NetBSD: exec_conf.c,v 1.16 1995/12/09 05:34:47 cgd Exp $ */ /* @@ -34,21 +34,13 @@ #include <sys/param.h> #include <sys/exec.h> #include <sys/exec_script.h> - -#if defined(_KERN_DO_ELF) || defined(_KERN_DO_ELF64) #include <sys/exec_elf.h> -#endif extern struct emul emul_native; struct execsw execsw[] = { { EXEC_SCRIPT_HDRSZ, exec_script_makecmds, &emul_native, }, /* shell scripts */ -#ifdef _KERN_DO_ELF - { sizeof(Elf32_Ehdr), exec_elf32_makecmds, &emul_native }, /* elf binaries */ -#endif -#ifdef _KERN_DO_ELF64 - { sizeof(Elf64_Ehdr), exec_elf64_makecmds, &emul_native }, /* elf binaries */ -#endif /* ELF64 */ + { sizeof(Elf_Ehdr), exec_elf_makecmds, &emul_native }, /* elf binaries */ }; int nexecs = (sizeof execsw / sizeof(*execsw)); int exec_maxhdrsz; diff --git a/sys/kern/exec_elf.c b/sys/kern/exec_elf.c index e2ece9d652b..25a03a3763e 100644 --- a/sys/kern/exec_elf.c +++ b/sys/kern/exec_elf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exec_elf.c,v 1.135 2017/02/08 05:02:05 guenther Exp $ */ +/* $OpenBSD: exec_elf.c,v 1.136 2017/02/08 05:09:25 guenther Exp $ */ /* * Copyright (c) 1996 Per Fogelstrom @@ -93,13 +93,18 @@ #include <machine/reg.h> #include <machine/exec.h> -int ELFNAME(load_file)(struct proc *, char *, struct exec_package *, - struct elf_args *, Elf_Addr *); -int ELFNAME(check_header)(Elf_Ehdr *); -int ELFNAME(read_from)(struct proc *, struct vnode *, u_long, void *, int); -void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, - Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int); -int ELFNAMEEND(coredump)(struct proc *, void *); +int elf_load_file(struct proc *, char *, struct exec_package *, + struct elf_args *, Elf_Addr *); +int elf_check_header(Elf_Ehdr *); +int elf_read_from(struct proc *, struct vnode *, u_long, void *, int); +void elf_load_psection(struct exec_vmcmd_set *, struct vnode *, + Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int); +int coredump_elf(struct proc *, void *); +void *elf_copyargs(struct exec_package *, struct ps_strings *, void *, + void *); +int exec_elf_fixup(struct proc *, struct exec_package *); +int elf_os_pt_note(struct proc *, struct exec_package *, Elf_Ehdr *, + char *, size_t, size_t); extern char sigcode[], esigcode[], sigcoderet[]; #ifdef SYSCALL_DEBUG @@ -124,7 +129,7 @@ extern char *syscallnames[]; /* * This is the OpenBSD ELF emul */ -struct emul ELFNAMEEND(emul) = { +struct emul emul_elf = { "native", NULL, sendsig, @@ -137,10 +142,10 @@ struct emul ELFNAMEEND(emul) = { NULL, #endif (sizeof(AuxInfo) * ELF_AUX_ENTRIES / sizeof(char *)), - ELFNAME(copyargs), + elf_copyargs, setregs, - ELFNAME2(exec,fixup), - ELFNAMEEND(coredump), + exec_elf_fixup, + coredump_elf, sigcode, esigcode, sigcoderet, @@ -152,7 +157,7 @@ struct emul ELFNAMEEND(emul) = { * space for extra information in case of dynamic binding. */ void * -ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo, +elf_copyargs(struct exec_package *pack, struct ps_strings *arginfo, void *stack, void *argp) { stack = copyargs(pack, arginfo, stack, argp); @@ -174,7 +179,7 @@ ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo, * Check header for validity; return 0 for ok, ENOEXEC if error */ int -ELFNAME(check_header)(Elf_Ehdr *ehdr) +elf_check_header(Elf_Ehdr *ehdr) { /* * We need to check magic, class size, endianess, and version before @@ -203,8 +208,8 @@ ELFNAME(check_header)(Elf_Ehdr *ehdr) * Load a psection at the appropriate address */ void -ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, - Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags) +elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, + Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags) { u_long msize, lsize, psize, rm, rf; long diff, offset, bdiff; @@ -284,8 +289,8 @@ ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, * Read from vnode into buffer at offset. */ int -ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, void *buf, - int size) +elf_read_from(struct proc *p, struct vnode *vp, u_long off, void *buf, + int size) { int error; size_t resid; @@ -306,8 +311,8 @@ ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, void *buf, * coff_load_shlib()]. Made slightly generic so it might be used externally. */ int -ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, - struct elf_args *ap, Elf_Addr *last) +elf_load_file(struct proc *p, char *path, struct exec_package *epp, + struct elf_args *ap, Elf_Addr *last) { int error, i; struct nameidata nd; @@ -345,10 +350,10 @@ ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, } if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0) goto bad1; - if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, &eh, sizeof(eh))) != 0) + if ((error = elf_read_from(p, nd.ni_vp, 0, &eh, sizeof(eh))) != 0) goto bad1; - if (ELFNAME(check_header)(&eh) || eh.e_type != ET_DYN) { + if (elf_check_header(&eh) || eh.e_type != ET_DYN) { error = ENOEXEC; goto bad1; } @@ -356,8 +361,7 @@ ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, ph = mallocarray(eh.e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); phsize = eh.e_phnum * sizeof(Elf_Phdr); - if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, ph, - phsize)) != 0) + if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff, ph, phsize)) != 0) goto bad1; for (i = 0; i < eh.e_phnum; i++) { @@ -454,7 +458,7 @@ ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, flags = VMCMD_RELATIVE; addr = ph[i].p_vaddr - base_ph->p_vaddr; } - ELFNAME(load_psection)(&epp->ep_vmcmds, nd.ni_vp, + elf_load_psection(&epp->ep_vmcmds, nd.ni_vp, &ph[i], &addr, &size, &prot, flags); /* If entry is within this section it must be text */ if (eh.e_entry >= ph[i].p_vaddr && @@ -508,7 +512,7 @@ bad: * stack segments. */ int -ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) +exec_elf_makecmds(struct proc *p, struct exec_package *epp) { Elf_Ehdr *eh = epp->ep_hdr; Elf_Phdr *ph, *pp, *base_ph = NULL; @@ -521,7 +525,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) return (ENOEXEC); - if (ELFNAME(check_header)(eh) || + if (elf_check_header(eh) || (eh->e_type != ET_EXEC && eh->e_type != ET_DYN)) return (ENOEXEC); @@ -543,7 +547,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) ph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); phsize = eh->e_phnum * sizeof(Elf_Phdr); - if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, ph, + if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) != 0) goto bad; @@ -555,7 +559,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) goto bad; interp = pool_get(&namei_pool, PR_WAITOK); - if ((error = ELFNAME(read_from)(p, epp->ep_vp, + if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset, interp, pp->p_filesz)) != 0) { goto bad; } @@ -587,7 +591,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) * OK, we want a slightly different twist of the * standard emulation package for "real" elf. */ - epp->ep_emul = &ELFNAMEEND(emul); + epp->ep_emul = &emul_elf; pos = ELF_NO_ADDR; /* @@ -595,7 +599,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) * via a PT_NOTE then also check for a PT_OPENBSD_WXNEEDED segment. */ if (eh->e_ident[EI_OSABI] != ELFOSABI_OPENBSD && (error = - ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "OpenBSD", 8, 4)) != 0) { + elf_os_pt_note(p, epp, epp->ep_hdr, "OpenBSD", 8, 4)) != 0) { goto bad; } @@ -627,7 +631,7 @@ ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) * this is correct for BSS_PLT, but may not be * for DATA_PLT, is fine for TEXT_PLT. */ - ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, + elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, pp, &addr, &size, &prot, flags); /* @@ -773,7 +777,7 @@ bad: * when loading the program is available for setup of the interpreter. */ int -ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp) +exec_elf_fixup(struct proc *p, struct exec_package *epp) { char *interp; int error = 0; @@ -789,7 +793,7 @@ ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp) ap = epp->ep_emul_arg; if (interp && - (error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) { + (error = elf_load_file(p, interp, epp, ap, &pos)) != 0) { free(ap, M_TEMP, epp->ep_emul_argsize); pool_put(&namei_pool, interp); kill_vmcmds(&epp->ep_vmcmds); @@ -848,8 +852,8 @@ ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp) } int -ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, - char *os_name, size_t name_size, size_t desc_size) +elf_os_pt_note(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, + char *os_name, size_t name_size, size_t desc_size) { char pathbuf[MAXPATHLEN]; Elf_Phdr *hph, *ph; @@ -859,7 +863,7 @@ ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, hph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); phsize = eh->e_phnum * sizeof(Elf_Phdr); - if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, + if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff, hph, phsize)) != 0) goto out1; @@ -889,7 +893,7 @@ ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, continue; np = malloc(ph->p_filesz, M_TEMP, M_WAITOK); - if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, + if ((error = elf_read_from(p, epp->ep_vp, ph->p_offset, np, ph->p_filesz)) != 0) goto out2; @@ -924,11 +928,24 @@ out1: return error; } +/* + * Start of routines related to dumping core + */ + +#ifdef SMALL_KERNEL +int +coredump_elf(struct proc *p, void *cookie) +{ + return EPERM; +} +#else /* !SMALL_KERNEL */ + + struct countsegs_state { int npsections; }; -int ELFNAMEEND(coredump_countsegs)(struct proc *, void *, +int coredump_countsegs_elf(struct proc *, void *, struct uvm_coredump_state *); struct writesegs_state { @@ -936,23 +953,20 @@ struct writesegs_state { off_t secoff; }; -int ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *, +int coredump_writeseghdrs_elf(struct proc *, void *, struct uvm_coredump_state *); -int ELFNAMEEND(coredump_notes)(struct proc *, void *, size_t *); -int ELFNAMEEND(coredump_note)(struct proc *, void *, size_t *); -int ELFNAMEEND(coredump_writenote)(struct proc *, void *, Elf_Note *, +int coredump_notes_elf(struct proc *, void *, size_t *); +int coredump_note_elf(struct proc *, void *, size_t *); +int coredump_writenote_elf(struct proc *, void *, Elf_Note *, const char *, void *); #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */ #define elfround(x) roundup((x), ELFROUNDSIZE) int -ELFNAMEEND(coredump)(struct proc *p, void *cookie) +coredump_elf(struct proc *p, void *cookie) { -#ifdef SMALL_KERNEL - return EPERM; -#else Elf_Ehdr ehdr; Elf_Phdr *psections = NULL; struct countsegs_state cs; @@ -974,8 +988,7 @@ ELFNAMEEND(coredump)(struct proc *p, void *cookie) /* Pass 1: count the entries. */ cs.npsections = 0; - error = uvm_coredump_walkmap(p, NULL, - ELFNAMEEND(coredump_countsegs), &cs); + error = uvm_coredump_walkmap(p, NULL, coredump_countsegs_elf, &cs); if (error) goto out; @@ -983,7 +996,7 @@ ELFNAMEEND(coredump)(struct proc *p, void *cookie) cs.npsections++; /* Get the size of the notes. */ - error = ELFNAMEEND(coredump_notes)(p, NULL, ¬esize); + error = coredump_notes_elf(p, NULL, ¬esize); if (error) goto out; @@ -1026,8 +1039,7 @@ ELFNAMEEND(coredump)(struct proc *p, void *cookie) /* Pass 2: now write the P-section headers. */ ws.secoff = secstart; ws.psections = psections; - error = uvm_coredump_walkmap(p, cookie, - ELFNAMEEND(coredump_writeseghdrs), &ws); + error = uvm_coredump_walkmap(p, cookie, coredump_writeseghdrs_elf, &ws); if (error) goto out; @@ -1053,7 +1065,7 @@ ELFNAMEEND(coredump)(struct proc *p, void *cookie) #endif /* Write out the notes. */ - error = ELFNAMEEND(coredump_notes)(p, cookie, ¬esize); + error = coredump_notes_elf(p, cookie, ¬esize); if (error) goto out; @@ -1093,26 +1105,22 @@ ELFNAMEEND(coredump)(struct proc *p, void *cookie) out: free(psections, M_TEMP, psectionslen); return (error); -#endif } int -ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie, +coredump_countsegs_elf(struct proc *p, void *iocookie, struct uvm_coredump_state *us) { -#ifndef SMALL_KERNEL struct countsegs_state *cs = us->cookie; cs->npsections++; -#endif return (0); } int -ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie, +coredump_writeseghdrs_elf(struct proc *p, void *iocookie, struct uvm_coredump_state *us) { -#ifndef SMALL_KERNEL struct writesegs_state *ws = us->cookie; Elf_Phdr phdr; vsize_t size, realsize; @@ -1137,15 +1145,13 @@ ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie, ws->secoff += phdr.p_filesz; *ws->psections++ = phdr; -#endif return (0); } int -ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) +coredump_notes_elf(struct proc *p, void *iocookie, size_t *sizep) { -#ifndef SMALL_KERNEL struct ps_strings pss; struct iovec iov; struct uio uio; @@ -1196,7 +1202,7 @@ ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) nhdr.descsz = sizeof(cpi); nhdr.type = NT_OPENBSD_PROCINFO; - error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, + error = coredump_writenote_elf(p, iocookie, &nhdr, "OpenBSD", &cpi); if (error) return (error); @@ -1256,7 +1262,7 @@ ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) nhdr.type = NT_OPENBSD_WCOOKIE; wcookie = process_get_wcookie(p); - error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, + error = coredump_writenote_elf(p, iocookie, &nhdr, "OpenBSD", &wcookie); if (error) return (error); @@ -1268,7 +1274,7 @@ ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) * Now write the register info for the thread that caused the * coredump. */ - error = ELFNAMEEND(coredump_note)(p, iocookie, ¬esize); + error = coredump_note_elf(p, iocookie, ¬esize); if (error) return (error); size += notesize; @@ -1282,21 +1288,19 @@ ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { if (q == p) /* we've taken care of this thread */ continue; - error = ELFNAMEEND(coredump_note)(q, iocookie, ¬esize); + error = coredump_note_elf(q, iocookie, ¬esize); if (error) return (error); size += notesize; } *sizep = size; -#endif return (0); } int -ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep) +coredump_note_elf(struct proc *p, void *iocookie, size_t *sizep) { -#ifndef SMALL_KERNEL Elf_Note nhdr; int size, notesize, error; int namesize; @@ -1323,7 +1327,7 @@ ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep) nhdr.descsz = sizeof(intreg); nhdr.type = NT_OPENBSD_REGS; - error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, + error = coredump_writenote_elf(p, iocookie, &nhdr, name, &intreg); if (error) return (error); @@ -1342,8 +1346,7 @@ ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep) nhdr.descsz = sizeof(freg); nhdr.type = NT_OPENBSD_FPREGS; - error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, - name, &freg); + error = coredump_writenote_elf(p, iocookie, &nhdr, name, &freg); if (error) return (error); } @@ -1352,17 +1355,13 @@ ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep) *sizep = size; /* XXX Add hook for machdep per-LWP notes. */ -#endif return (0); } int -ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Note *nhdr, +coredump_writenote_elf(struct proc *p, void *cookie, Elf_Note *nhdr, const char *name, void *data) { -#ifdef SMALL_KERNEL - return EPERM; -#else int error; error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr)); @@ -1375,5 +1374,5 @@ ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Note *nhdr, return error; return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->descsz); -#endif } +#endif /* !SMALL_KERNEL */ diff --git a/sys/kern/exec_elf32.c b/sys/kern/exec_elf32.c deleted file mode 100644 index 584667fa92e..00000000000 --- a/sys/kern/exec_elf32.c +++ /dev/null @@ -1,11 +0,0 @@ -/* $OpenBSD: exec_elf32.c,v 1.3 2015/01/20 04:41:01 krw Exp $ */ -/* - * Public domain. Author: Artur Grabowski <art@openbsd.org> - */ -#include <machine/exec.h> - -#ifdef _KERN_DO_ELF -#define ELFSIZE 32 -#include <kern/exec_elf.c> -#endif - diff --git a/sys/kern/exec_elf64.c b/sys/kern/exec_elf64.c deleted file mode 100644 index 517ee5d7a6b..00000000000 --- a/sys/kern/exec_elf64.c +++ /dev/null @@ -1,10 +0,0 @@ -/* $OpenBSD: exec_elf64.c,v 1.21 2015/01/20 04:41:01 krw Exp $ */ -/* - * Public domain. Author: Artur Grabowski <art@openbsd.org> - */ -#include <machine/exec.h> - -#ifdef _KERN_DO_ELF64 -#define ELFSIZE 64 -#include <kern/exec_elf.c> -#endif /* _KERN_DO_ELF64 */ |