Module Name:    src
Committed By:   christos
Date:           Tue May 24 00:49:56 UTC 2016

Modified Files:
        src/sys/kern: core_elf32.c
        src/sys/sys: exec_elf.h

Log Message:
Add a note for the auxv array so we can find our load location from a
core file of a PIE binary.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/kern/core_elf32.c
cvs rdiff -u -r1.153 -r1.154 src/sys/sys/exec_elf.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/core_elf32.c
diff -u src/sys/kern/core_elf32.c:1.45 src/sys/kern/core_elf32.c:1.46
--- src/sys/kern/core_elf32.c:1.45	Wed Apr  2 13:19:49 2014
+++ src/sys/kern/core_elf32.c	Mon May 23 20:49:56 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: core_elf32.c,v 1.45 2014/04/02 17:19:49 matt Exp $	*/
+/*	$NetBSD: core_elf32.c,v 1.46 2016/05/24 00:49:56 christos Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.45 2014/04/02 17:19:49 matt Exp $");
+__KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.46 2016/05/24 00:49:56 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_coredump.h"
@@ -343,12 +343,11 @@ ELFNAMEEND(coredump_getseghdrs)(struct u
 	return (0);
 }
 
-static int
-ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
+static void
+coredump_note_procinfo(struct lwp *l, struct note_state *ns)
 {
 	struct proc *p;
 	struct netbsd_elfcore_procinfo cpi;
-	int error;
 	struct lwp *l0;
 	sigset_t ss1, ss2;
 
@@ -398,6 +397,54 @@ ELFNAMEEND(coredump_notes)(struct lwp *l
 
 	ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
 	    ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
+}
+
+static int
+coredump_note_auxv(struct lwp *l, struct note_state *ns)
+{
+	struct ps_strings pss;
+	int error;
+	struct proc *p = l->l_proc;
+	struct vmspace *vm;
+	void *uauxv, *kauxv;
+	size_t len;
+
+	if ((error = copyin_psstrings(p, &pss)) != 0)
+		return error;
+
+	if (pss.ps_envstr == NULL)
+		return EIO;
+
+        vm = p->p_vmspace;
+	uvmspace_addref(vm);
+
+	len = p->p_execsw->es_arglen * sizeof(char *);
+	uauxv = (void *)(pss.ps_envstr + pss.ps_nenvstr + 1);
+	kauxv = kmem_alloc(len, KM_SLEEP);
+	error = copyin_vmspace(vm, uauxv, kauxv, len);
+
+	uvmspace_free(vm);
+
+	if (error == 0) {
+		ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
+		    ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
+	}
+	
+	kmem_free(kauxv, len);
+	return error;
+}
+
+static int
+ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
+{
+	int error;
+	struct lwp *l0;
+	struct proc *p = l->l_proc;
+
+	coredump_note_procinfo(l, ns);
+	error = coredump_note_auxv(l, ns);
+	if (error)
+		return error;
 
 	/* XXX Add hook for machdep per-proc notes. */
 
@@ -407,7 +454,7 @@ ELFNAMEEND(coredump_notes)(struct lwp *l
 	 */
 	error = ELFNAMEEND(coredump_note)(l, ns);
 	if (error)
-		return (error);
+		return error;
 
 	/*
 	 * Now, for each LWP, write the register info and any other

Index: src/sys/sys/exec_elf.h
diff -u src/sys/sys/exec_elf.h:1.153 src/sys/sys/exec_elf.h:1.154
--- src/sys/sys/exec_elf.h:1.153	Fri Feb 19 15:45:21 2016
+++ src/sys/sys/exec_elf.h	Mon May 23 20:49:55 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_elf.h,v 1.153 2016/02/19 20:45:21 christos Exp $	*/
+/*	$NetBSD: exec_elf.h,v 1.154 2016/05/24 00:49:55 christos Exp $	*/
 
 /*-
  * Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -975,6 +975,8 @@ typedef struct {
  *
  *	ELF_NOTE_NETBSD_CORE_PROCINFO
  *		Note is a "netbsd_elfcore_procinfo" structure.
+ *	ELF_NOTE_NETBSD_CORE_AUXV
+ *		Note is an array of AuxInfo structures.
  *
  * We also use ptrace(2) request numbers (the ones that exist in
  * machine-dependent space) to identify register info notes.  The
@@ -988,6 +990,7 @@ typedef struct {
 #define ELF_NOTE_NETBSD_CORE_NAME	"NetBSD-CORE"
 
 #define ELF_NOTE_NETBSD_CORE_PROCINFO	1
+#define ELF_NOTE_NETBSD_CORE_AUXV	2
 
 #define NETBSD_ELFCORE_PROCINFO_VERSION 1
 

Reply via email to