Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 May 2017 17:26:34 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r318192 - in stable: 10/sys/kern 10/sys/sys 10/usr.bin/gcore 11/sys/kern 11/sys/sys 11/usr.bin/gcore
Message-ID:  <201705111726.v4BHQYcg043791@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Thu May 11 17:26:34 2017
New Revision: 318192
URL: https://svnweb.freebsd.org/changeset/base/318192

Log:
  MFC 313407,313449: Copy ELF machine/flags from binaries to core dumps.
  
  313407:
  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).
  
  313449:
  Trim trailing whitespace (mostly introduced in r313407).
  
  Sponsored by:	DARPA / AFRL

Modified:
  stable/10/sys/kern/imgact_elf.c
  stable/10/sys/kern/kern_fork.c
  stable/10/sys/sys/proc.h
  stable/10/usr.bin/gcore/elfcore.c
Directory Properties:
  stable/10/   (props changed)

Changes in other areas also in this revision:
Modified:
  stable/11/sys/kern/imgact_elf.c
  stable/11/sys/kern/kern_fork.c
  stable/11/sys/sys/proc.h
  stable/11/usr.bin/gcore/elfcore.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/10/sys/kern/imgact_elf.c
==============================================================================
--- stable/10/sys/kern/imgact_elf.c	Thu May 11 17:03:45 2017	(r318191)
+++ stable/10/sys/kern/imgact_elf.c	Thu May 11 17:26:34 2017	(r318192)
@@ -1041,6 +1041,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);
@@ -1616,15 +1618,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_phnum = numsegs + 1;

Modified: stable/10/sys/kern/kern_fork.c
==============================================================================
--- stable/10/sys/kern/kern_fork.c	Thu May 11 17:03:45 2017	(r318191)
+++ stable/10/sys/kern/kern_fork.c	Thu May 11 17:26:34 2017	(r318192)
@@ -404,6 +404,8 @@ do_fork(struct thread *td, int flags, st
 
 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
 	    __rangeof(struct proc, p_startcopy, p_endcopy));
+	p2->p_elf_machine = p1->p_elf_machine;
+	p2->p_elf_flags = p1->p_elf_flags;
 	pargs_hold(p2->p_args);
 	PROC_UNLOCK(p1);
 

Modified: stable/10/sys/sys/proc.h
==============================================================================
--- stable/10/sys/sys/proc.h	Thu May 11 17:03:45 2017	(r318191)
+++ stable/10/sys/sys/proc.h	Thu May 11 17:26:34 2017	(r318192)
@@ -580,6 +580,7 @@ struct proc {
 	rlim_t		p_cpulimit;	/* (c) Current CPU limit in seconds. */
 	signed char	p_nice;		/* (c) Process "nice" value. */
 	int		p_fibnum;	/* in this routing domain XXX MRT */
+
 /* End area that is copied on creation. */
 #define	p_endcopy	p_xstat
 
@@ -623,6 +624,8 @@ struct proc {
 	struct pgrp	*p_pgrp;	/* (c + e) Pointer to process group. */
 	struct filemon	*p_filemon;	/* (c) filemon-specific data. */
 	u_int		p_ptevents;	/* (c) ptrace() event mask. */
+	uint16_t	p_elf_machine;	/* (x) ELF machine type */
+	uint64_t	p_elf_flags;	/* (x) ELF flags */
 };
 
 #define	p_session	p_pgrp->pg_session

Modified: stable/10/usr.bin/gcore/elfcore.c
==============================================================================
--- stable/10/usr.bin/gcore/elfcore.c	Thu May 11 17:03:45 2017	(r318191)
+++ stable/10/usr.bin/gcore/elfcore.c	Thu May 11 17:26:34 2017	(r318192)
@@ -114,8 +114,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);
@@ -175,7 +175,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;
@@ -225,7 +225,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)
@@ -412,12 +412,19 @@ 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;
 	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;
 	phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
@@ -433,11 +440,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_phnum = numsegs + 1;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201705111726.v4BHQYcg043791>