From owner-svn-src-all@freebsd.org Tue Feb 7 20:34:05 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 3E2C8CD599F; Tue, 7 Feb 2017 20:34:05 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id F043FF68; Tue, 7 Feb 2017 20:34:04 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v17KY459062947; Tue, 7 Feb 2017 20:34:04 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v17KY3is062944; Tue, 7 Feb 2017 20:34:03 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <201702072034.v17KY3is062944@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Tue, 7 Feb 2017 20:34:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r313407 - in head: sys/kern sys/sys usr.bin/gcore X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Feb 2017 20:34:05 -0000 Author: jhb Date: Tue Feb 7 20:34:03 2017 New Revision: 313407 URL: https://svnweb.freebsd.org/changeset/base/313407 Log: Copy the e_machine and e_flags fields from the binary into an ELF core dump. In the kernel, cache the machine and flags fields from ELF header to use in the ELF header of a core dump. For gcore, the copy these fields over from the ELF header in the binary. This matters for platforms which encode ABI information in the flags field (such as o32 vs n32 on MIPS). Reviewed by: kib Sponsored by: DARPA / AFRL Differential Revision: https://reviews.freebsd.org/D9392 Modified: head/sys/kern/imgact_elf.c head/sys/sys/proc.h head/usr.bin/gcore/elfcore.c Modified: head/sys/kern/imgact_elf.c ============================================================================== --- head/sys/kern/imgact_elf.c Tue Feb 7 19:47:30 2017 (r313406) +++ head/sys/kern/imgact_elf.c Tue Feb 7 20:34:03 2017 (r313407) @@ -1055,6 +1055,8 @@ __CONCAT(exec_, __elfN(imgact))(struct i imgp->interpreted = 0; imgp->reloc_base = addr; imgp->proc->p_osrel = osrel; + imgp->proc->p_elf_machine = hdr->e_machine; + imgp->proc->p_elf_flags = hdr->e_flags; ret: free(interp_buf, M_TEMP); @@ -1655,15 +1657,11 @@ __elfN(puthdr)(struct thread *td, void * ehdr->e_ident[EI_ABIVERSION] = 0; ehdr->e_ident[EI_PAD] = 0; ehdr->e_type = ET_CORE; -#if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 - ehdr->e_machine = ELF_ARCH32; -#else - ehdr->e_machine = ELF_ARCH; -#endif + ehdr->e_machine = td->td_proc->p_elf_machine; ehdr->e_version = EV_CURRENT; ehdr->e_entry = 0; ehdr->e_phoff = sizeof(Elf_Ehdr); - ehdr->e_flags = 0; + ehdr->e_flags = td->td_proc->p_elf_flags; ehdr->e_ehsize = sizeof(Elf_Ehdr); ehdr->e_phentsize = sizeof(Elf_Phdr); ehdr->e_shentsize = sizeof(Elf_Shdr); Modified: head/sys/sys/proc.h ============================================================================== --- head/sys/sys/proc.h Tue Feb 7 19:47:30 2017 (r313406) +++ head/sys/sys/proc.h Tue Feb 7 20:34:03 2017 (r313407) @@ -617,8 +617,11 @@ struct proc { our subtree. */ u_int p_xexit; /* (c) Exit code. */ u_int p_xsig; /* (c) Stop/kill sig. */ + uint16_t p_elf_machine; /* (x) ELF machine type */ + uint64_t p_elf_flags; /* (x) ELF flags */ + /* End area that is copied on creation. */ -#define p_endcopy p_xsig +#define p_endcopy p_elf_flags struct pgrp *p_pgrp; /* (c + e) Pointer to process group. */ struct knlist *p_klist; /* (c) Knotes attached to this proc. */ int p_numthreads; /* (c) Number of threads. */ Modified: head/usr.bin/gcore/elfcore.c ============================================================================== --- head/usr.bin/gcore/elfcore.c Tue Feb 7 19:47:30 2017 (r313406) +++ head/usr.bin/gcore/elfcore.c Tue Feb 7 20:34:03 2017 (r313407) @@ -117,8 +117,8 @@ static void *elf_note_procstat_psstrings static void *elf_note_procstat_rlimit(void *, size_t *); static void *elf_note_procstat_umask(void *, size_t *); static void *elf_note_procstat_vmmap(void *, size_t *); -static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t, - int); +static void elf_puthdr(int, pid_t, vm_map_entry_t, void *, size_t, size_t, + size_t, int); static void elf_putnote(int, notefunc_t, void *, struct sbuf *); static void elf_putnotes(pid_t, struct sbuf *, size_t *); static void freemap(vm_map_entry_t); @@ -178,7 +178,7 @@ elf_detach(void) * Write an ELF coredump for the given pid to the given fd. */ static void -elf_coredump(int efd __unused, int fd, pid_t pid) +elf_coredump(int efd, int fd, pid_t pid) { vm_map_entry_t map; struct sseg_closure seginfo; @@ -230,7 +230,7 @@ elf_coredump(int efd __unused, int fd, p hdr = sbuf_data(sb); segoff = sbuf_len(sb); /* Fill in the header. */ - elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count); + elf_puthdr(efd, pid, map, hdr, hdrsize, notesz, segoff, seginfo.count); n = write(fd, hdr, segoff); if (n == -1) @@ -420,14 +420,21 @@ elf_putnote(int type, notefunc_t notefun * Generate the ELF coredump header. */ static void -elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize, +elf_puthdr(int efd, pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize, size_t notesz, size_t segoff, int numsegs) { - Elf_Ehdr *ehdr; + Elf_Ehdr *ehdr, binhdr; Elf_Phdr *phdr; Elf_Shdr *shdr; struct phdr_closure phc; + ssize_t cnt; + cnt = read(efd, &binhdr, sizeof(binhdr)); + if (cnt < 0) + err(1, "Failed to re-read ELF header"); + else if (cnt != sizeof(binhdr)) + errx(1, "Failed to re-read ELF header"); + ehdr = (Elf_Ehdr *)hdr; ehdr->e_ident[EI_MAG0] = ELFMAG0; @@ -441,11 +448,11 @@ elf_puthdr(pid_t pid, vm_map_entry_t map ehdr->e_ident[EI_ABIVERSION] = 0; ehdr->e_ident[EI_PAD] = 0; ehdr->e_type = ET_CORE; - ehdr->e_machine = ELF_ARCH; + ehdr->e_machine = binhdr.e_machine; ehdr->e_version = EV_CURRENT; ehdr->e_entry = 0; ehdr->e_phoff = sizeof(Elf_Ehdr); - ehdr->e_flags = 0; + ehdr->e_flags = binhdr.e_flags; ehdr->e_ehsize = sizeof(Elf_Ehdr); ehdr->e_phentsize = sizeof(Elf_Phdr); ehdr->e_shentsize = sizeof(Elf_Shdr);