git: 1b196c07b81d - stable/13 - linux(4): implement coredump support

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Fri, 17 Jun 2022 19:36:46 UTC
The branch stable/13 has been updated by dchagin:

URL: https://cgit.FreeBSD.org/src/commit/?id=1b196c07b81d1ec547a4e57567c352e93080c9e5

commit 1b196c07b81d1ec547a4e57567c352e93080c9e5
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-06-30 16:45:21 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2022-06-17 19:33:02 +0000

    linux(4): implement coredump support
    
    Implement dumping core for Linux binaries on amd64, for both
    32- and 64-bit executables.  Some bits are still missing.
    
    This is based on a prototype by chuck@.
    
    Reviewed By:    kib
    Sponsored By:   EPSRC
    Differential Revision:  https://reviews.freebsd.org/D30019
    
    (cherry picked from commit 447636e43c08d697664512a50f00f93f41c0a79f)
---
 sys/amd64/linux/linux_machdep.c    |  34 +++++
 sys/amd64/linux/linux_ptrace.c     |  33 -----
 sys/amd64/linux/linux_sysvec.c     |   4 +-
 sys/amd64/linux32/linux32_sysvec.c |   6 +-
 sys/arm64/linux/linux_sysvec.c     |   4 +-
 sys/compat/linux/linux_elf.c       | 294 +++++++++++++++++++++++++++++++++++++
 sys/compat/linux/linux_elf.h       |  56 +++++++
 sys/compat/linux/linux_elf32.c     |  38 +++++
 sys/compat/linux/linux_elf64.c     |  38 +++++
 sys/compat/linux/linux_emul.h      |   6 +
 sys/modules/linux/Makefile         |   1 +
 sys/modules/linux64/Makefile       |   1 +
 12 files changed, 475 insertions(+), 40 deletions(-)

diff --git a/sys/amd64/linux/linux_machdep.c b/sys/amd64/linux/linux_machdep.c
index c2f841707974..84663a79972d 100644
--- a/sys/amd64/linux/linux_machdep.c
+++ b/sys/amd64/linux/linux_machdep.c
@@ -79,6 +79,7 @@ __FBSDID("$FreeBSD$");
 #include <vm/vm_map.h>
 
 #include <x86/ifunc.h>
+#include <x86/reg.h>
 #include <x86/sysarch.h>
 
 #include <security/audit/audit.h>
@@ -326,3 +327,36 @@ DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
 	    futex_xorl_smap : futex_xorl_nosmap);
 }
+
+void
+bsd_to_linux_regset(struct reg *b_reg, struct linux_pt_regset *l_regset)
+{
+
+	l_regset->r15 = b_reg->r_r15;
+	l_regset->r14 = b_reg->r_r14;
+	l_regset->r13 = b_reg->r_r13;
+	l_regset->r12 = b_reg->r_r12;
+	l_regset->rbp = b_reg->r_rbp;
+	l_regset->rbx = b_reg->r_rbx;
+	l_regset->r11 = b_reg->r_r11;
+	l_regset->r10 = b_reg->r_r10;
+	l_regset->r9 = b_reg->r_r9;
+	l_regset->r8 = b_reg->r_r8;
+	l_regset->rax = b_reg->r_rax;
+	l_regset->rcx = b_reg->r_rcx;
+	l_regset->rdx = b_reg->r_rdx;
+	l_regset->rsi = b_reg->r_rsi;
+	l_regset->rdi = b_reg->r_rdi;
+	l_regset->orig_rax = b_reg->r_rax;
+	l_regset->rip = b_reg->r_rip;
+	l_regset->cs = b_reg->r_cs;
+	l_regset->eflags = b_reg->r_rflags;
+	l_regset->rsp = b_reg->r_rsp;
+	l_regset->ss = b_reg->r_ss;
+	l_regset->fs_base = 0;
+	l_regset->gs_base = 0;
+	l_regset->ds = b_reg->r_ds;
+	l_regset->es = b_reg->r_es;
+	l_regset->fs = b_reg->r_fs;
+	l_regset->gs = b_reg->r_gs;
+}
diff --git a/sys/amd64/linux/linux_ptrace.c b/sys/amd64/linux/linux_ptrace.c
index 13bc0edd7c98..770ebcf58364 100644
--- a/sys/amd64/linux/linux_ptrace.c
+++ b/sys/amd64/linux/linux_ptrace.c
@@ -246,39 +246,6 @@ map_regs_to_linux(struct reg *b_reg, struct linux_pt_reg *l_reg)
 	l_reg->ss = b_reg->r_ss;
 }
 
-void
-bsd_to_linux_regset(struct reg *b_reg, struct linux_pt_regset *l_regset)
-{
-
-	l_regset->r15 = b_reg->r_r15;
-	l_regset->r14 = b_reg->r_r14;
-	l_regset->r13 = b_reg->r_r13;
-	l_regset->r12 = b_reg->r_r12;
-	l_regset->rbp = b_reg->r_rbp;
-	l_regset->rbx = b_reg->r_rbx;
-	l_regset->r11 = b_reg->r_r11;
-	l_regset->r10 = b_reg->r_r10;
-	l_regset->r9 = b_reg->r_r9;
-	l_regset->r8 = b_reg->r_r8;
-	l_regset->rax = b_reg->r_rax;
-	l_regset->rcx = b_reg->r_rcx;
-	l_regset->rdx = b_reg->r_rdx;
-	l_regset->rsi = b_reg->r_rsi;
-	l_regset->rdi = b_reg->r_rdi;
-	l_regset->orig_rax = b_reg->r_rax;
-	l_regset->rip = b_reg->r_rip;
-	l_regset->cs = b_reg->r_cs;
-	l_regset->eflags = b_reg->r_rflags;
-	l_regset->rsp = b_reg->r_rsp;
-	l_regset->ss = b_reg->r_ss;
-	l_regset->fs_base = 0;
-	l_regset->gs_base = 0;
-	l_regset->ds = b_reg->r_ds;
-	l_regset->es = b_reg->r_es;
-	l_regset->fs = b_reg->r_fs;
-	l_regset->gs = b_reg->r_gs;
-}
-
 static void
 map_regs_from_linux(struct reg *b_reg, struct linux_pt_reg *l_reg)
 {
diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c
index 54870b0153c9..5237e32b614d 100644
--- a/sys/amd64/linux/linux_sysvec.c
+++ b/sys/amd64/linux/linux_sysvec.c
@@ -735,8 +735,8 @@ struct sysentvec elf_linux_sysvec = {
 	.sv_name	= "Linux ELF64",
 	.sv_coredump	= elf64_coredump,
 	.sv_elf_core_osabi = ELFOSABI_NONE,
-	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
-	.sv_elf_core_prepare_notes = elf64_prepare_notes,
+	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
+	.sv_elf_core_prepare_notes = linux64_prepare_notes,
 	.sv_imgact_try	= linux_exec_imgact_try,
 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
diff --git a/sys/amd64/linux32/linux32_sysvec.c b/sys/amd64/linux32/linux32_sysvec.c
index 13ba0c883c42..398ac51d4203 100644
--- a/sys/amd64/linux32/linux32_sysvec.c
+++ b/sys/amd64/linux32/linux32_sysvec.c
@@ -903,9 +903,9 @@ struct sysentvec elf_linux_sysvec = {
 	.sv_szsigcode	= &linux_szsigcode,
 	.sv_name	= "Linux ELF32",
 	.sv_coredump	= elf32_coredump,
-	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
-	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
-	.sv_elf_core_prepare_notes = elf32_prepare_notes,
+	.sv_elf_core_osabi = ELFOSABI_NONE,
+	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
+	.sv_elf_core_prepare_notes = linux32_prepare_notes,
 	.sv_imgact_try	= linux_exec_imgact_try,
 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c
index 5a2edcefac2c..1b815b8ac0f2 100644
--- a/sys/arm64/linux/linux_sysvec.c
+++ b/sys/arm64/linux/linux_sysvec.c
@@ -404,8 +404,8 @@ struct sysentvec elf_linux_sysvec = {
 	.sv_name	= "Linux ELF64",
 	.sv_coredump	= elf64_coredump,
 	.sv_elf_core_osabi = ELFOSABI_NONE,
-	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
-	.sv_elf_core_prepare_notes = elf64_prepare_notes,
+	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
+	.sv_elf_core_prepare_notes = linux64_prepare_notes,
 	.sv_imgact_try	= linux_exec_imgact_try,
 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
diff --git a/sys/compat/linux/linux_elf.c b/sys/compat/linux/linux_elf.c
new file mode 100644
index 000000000000..077e0c93c884
--- /dev/null
+++ b/sys/compat/linux/linux_elf.c
@@ -0,0 +1,294 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 2021 Edward Tomasz Napierala <trasz@FreeBSD.org>
+ * Copyright (c) 2018 Chuck Tuffli
+ * Copyright (c) 2017 Dell EMC
+ * Copyright (c) 2000 David O'Brien
+ * Copyright (c) 1995-1996 Søren Schmidt
+ * Copyright (c) 1996 Peter Wemm
+ * All rights reserved.
+ *
+ * This software was developed by the University of Cambridge Computer
+ * Laboratory as part of the CHERI for Hypervisors and Operating Systems
+ * (CHaOS) project, funded by EPSRC grant EP/V000292/1.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/exec.h>
+#include <sys/imgact.h>
+#include <sys/imgact_elf.h>
+#include <sys/kernel.h>
+#include <sys/proc.h>
+#include <sys/procfs.h>
+#include <sys/ptrace.h>
+#include <sys/sbuf.h>
+#include <sys/sysent.h>
+#include <sys/user.h>
+#include <sys/vnode.h>
+
+#include <machine/elf.h>
+
+#if __ELF_WORD_SIZE == 32
+#define linux_pt_regset linux_pt_regset32
+#define bsd_to_linux_regset bsd_to_linux_regset32
+#include <machine/../linux32/linux.h>
+#else
+#include <machine/../linux/linux.h>
+#endif
+#include <compat/linux/linux_elf.h>
+#include <compat/linux/linux_emul.h>
+#include <compat/linux/linux_misc.h>
+
+/* This adds "linux32_" and "linux64_" prefixes. */
+#define	__linuxN(x)	__CONCAT(__CONCAT(__CONCAT(linux,__ELF_WORD_SIZE),_),x)
+
+#define	LINUX_NT_AUXV	6
+
+static void __linuxN(note_fpregset)(void *, struct sbuf *, size_t *);
+static void __linuxN(note_prpsinfo)(void *, struct sbuf *, size_t *);
+static void __linuxN(note_prstatus)(void *, struct sbuf *, size_t *);
+static void __linuxN(note_threadmd)(void *, struct sbuf *, size_t *);
+static void __linuxN(note_nt_auxv)(void *, struct sbuf *, size_t *);
+
+void
+__linuxN(prepare_notes)(struct thread *td, struct note_info_list *list,
+    size_t *sizep)
+{
+	struct proc *p;
+	struct thread *thr;
+	size_t size;
+
+	p = td->td_proc;
+	size = 0;
+
+	/*
+	 * To have the debugger select the right thread (LWP) as the initial
+	 * thread, we dump the state of the thread passed to us in td first.
+	 * This is the thread that causes the core dump and thus likely to
+	 * be the right thread one wants to have selected in the debugger.
+	 */
+	thr = td;
+	while (thr != NULL) {
+		size += __elfN(register_note)(td, list,
+		    NT_PRSTATUS, __linuxN(note_prstatus), thr);
+		size += __elfN(register_note)(td, list,
+		    NT_PRPSINFO, __linuxN(note_prpsinfo), p);
+		size += __elfN(register_note)(td, list,
+		    LINUX_NT_AUXV, __linuxN(note_nt_auxv), p);
+		size += __elfN(register_note)(td, list,
+		    NT_FPREGSET, __linuxN(note_fpregset), thr);
+		size += __elfN(register_note)(td, list,
+		    -1, __linuxN(note_threadmd), thr);
+
+		thr = thr == td ? TAILQ_FIRST(&p->p_threads) :
+		    TAILQ_NEXT(thr, td_plist);
+		if (thr == td)
+			thr = TAILQ_NEXT(thr, td_plist);
+	}
+
+	*sizep = size;
+}
+
+typedef struct linux_elf_prstatus linux_elf_prstatus_t;
+#if __ELF_WORD_SIZE == 32
+typedef struct prpsinfo32 linux_elf_prpsinfo_t;
+typedef struct fpreg32 linux_elf_prfpregset_t;
+#else
+typedef prpsinfo_t linux_elf_prpsinfo_t;
+typedef prfpregset_t linux_elf_prfpregset_t;
+#endif
+
+static void
+__linuxN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
+{
+	struct sbuf sbarg;
+	size_t len;
+	char *cp, *end;
+	struct proc *p;
+	linux_elf_prpsinfo_t *psinfo;
+	int error;
+
+	p = arg;
+	if (sb != NULL) {
+		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
+		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
+		psinfo->pr_version = PRPSINFO_VERSION;
+		psinfo->pr_psinfosz = sizeof(linux_elf_prpsinfo_t);
+		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
+		PROC_LOCK(p);
+		if (p->p_args != NULL) {
+			len = sizeof(psinfo->pr_psargs) - 1;
+			if (len > p->p_args->ar_length)
+				len = p->p_args->ar_length;
+			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
+			PROC_UNLOCK(p);
+			error = 0;
+		} else {
+			_PHOLD(p);
+			PROC_UNLOCK(p);
+			sbuf_new(&sbarg, psinfo->pr_psargs,
+			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
+			error = proc_getargv(curthread, p, &sbarg);
+			PRELE(p);
+			if (sbuf_finish(&sbarg) == 0)
+				len = sbuf_len(&sbarg) - 1;
+			else
+				len = sizeof(psinfo->pr_psargs) - 1;
+			sbuf_delete(&sbarg);
+		}
+		if (error || len == 0)
+			strlcpy(psinfo->pr_psargs, p->p_comm,
+			    sizeof(psinfo->pr_psargs));
+		else {
+			KASSERT(len < sizeof(psinfo->pr_psargs),
+			    ("len is too long: %zu vs %zu", len,
+			    sizeof(psinfo->pr_psargs)));
+			cp = psinfo->pr_psargs;
+			end = cp + len - 1;
+			for (;;) {
+				cp = memchr(cp, '\0', end - cp);
+				if (cp == NULL)
+					break;
+				*cp = ' ';
+			}
+		}
+		psinfo->pr_pid = p->p_pid;
+		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
+		free(psinfo, M_TEMP);
+	}
+	*sizep = sizeof(*psinfo);
+}
+
+static void
+__linuxN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
+{
+	struct thread *td;
+	linux_elf_prstatus_t *status;
+#if __ELF_WORD_SIZE == 32
+	struct reg32 pr_reg;
+#else
+	struct reg pr_reg;
+#endif
+
+	td = arg;
+	if (sb != NULL) {
+		KASSERT(*sizep == sizeof(*status), ("invalid size"));
+		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
+
+		/*
+		 * XXX: Some fields missing.
+		 */
+		status->pr_cursig = td->td_proc->p_sig;
+		status->pr_pid = td->td_tid;
+
+#if __ELF_WORD_SIZE == 32
+		fill_regs32(td, &pr_reg);
+#else
+		fill_regs(td, &pr_reg);
+#endif
+		bsd_to_linux_regset(&pr_reg, &status->pr_reg);
+		sbuf_bcat(sb, status, sizeof(*status));
+		free(status, M_TEMP);
+	}
+	*sizep = sizeof(*status);
+}
+
+static void
+__linuxN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
+{
+	struct thread *td;
+	linux_elf_prfpregset_t *fpregset;
+
+	td = arg;
+	if (sb != NULL) {
+		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
+		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
+#if __ELF_WORD_SIZE == 32
+		fill_fpregs32(td, fpregset);
+#else
+		fill_fpregs(td, fpregset);
+#endif
+		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
+		free(fpregset, M_TEMP);
+	}
+	*sizep = sizeof(*fpregset);
+}
+
+/*
+ * Allow for MD specific notes, as well as any MD
+ * specific preparations for writing MI notes.
+ */
+static void
+__linuxN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
+{
+	struct thread *td;
+	void *buf;
+	size_t size;
+
+	td = arg;
+	size = *sizep;
+	if (size != 0 && sb != NULL)
+		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
+	else
+		buf = NULL;
+	size = 0;
+	__elfN(dump_thread)(td, buf, &size);
+	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
+	if (size != 0 && sb != NULL)
+		sbuf_bcat(sb, buf, size);
+	free(buf, M_TEMP);
+	*sizep = size;
+}
+
+static void
+__linuxN(note_nt_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
+{
+	struct proc *p;
+	size_t size;
+
+	p = arg;
+	if (sb == NULL) {
+		size = 0;
+		sb = sbuf_new(NULL, NULL, LINUX_AT_COUNT * sizeof(Elf_Auxinfo),
+		    SBUF_FIXEDLEN);
+		sbuf_set_drain(sb, sbuf_count_drain, &size);
+		PHOLD(p);
+		proc_getauxv(curthread, p, sb);
+		PRELE(p);
+		sbuf_finish(sb);
+		sbuf_delete(sb);
+		*sizep = size;
+	} else {
+		PHOLD(p);
+		proc_getauxv(curthread, p, sb);
+		PRELE(p);
+	}
+}
diff --git a/sys/compat/linux/linux_elf.h b/sys/compat/linux/linux_elf.h
new file mode 100644
index 000000000000..4bb9318e360b
--- /dev/null
+++ b/sys/compat/linux/linux_elf.h
@@ -0,0 +1,56 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2018 Chuck Tuffli
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _COMPAT_LINUX_ELF_H_
+#define _COMPAT_LINUX_ELF_H_
+
+struct l_elf_siginfo {
+	l_int		si_signo;
+	l_int		si_code;
+	l_int		si_errno;
+};
+
+typedef struct linux_pt_regset l_elf_gregset_t;
+
+struct linux_elf_prstatus {
+	struct l_elf_siginfo pr_info;
+	l_short		pr_cursig;
+	l_ulong		pr_sigpend;
+	l_ulong		pr_sighold;
+	l_pid_t		pr_pid;
+	l_pid_t		pr_ppid;
+	l_pid_t		pr_pgrp;
+	l_pid_t		pr_sid;
+	l_timeval	pr_utime;
+	l_timeval	pr_stime;
+	l_timeval	pr_cutime;
+	l_timeval	pr_cstime;
+	l_elf_gregset_t	pr_reg;
+	l_int		pr_fpvalid;
+};
+
+#endif
diff --git a/sys/compat/linux/linux_elf32.c b/sys/compat/linux/linux_elf32.c
new file mode 100644
index 000000000000..17ee4b6baa0a
--- /dev/null
+++ b/sys/compat/linux/linux_elf32.c
@@ -0,0 +1,38 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2021 Edward Tomasz Napierala <trasz@FreeBSD.org>
+ * Copyright (c) 2002 Doug Rabson
+ * All rights reserved.
+ *
+ * This software was developed by the University of Cambridge Computer
+ * Laboratory as part of the CHERI for Hypervisors and Operating Systems
+ * (CHaOS) project, funded by EPSRC grant EP/V000292/1.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#define __ELF_WORD_SIZE 32
+#include <compat/linux/linux_elf.c>
diff --git a/sys/compat/linux/linux_elf64.c b/sys/compat/linux/linux_elf64.c
new file mode 100644
index 000000000000..2e283d763bbc
--- /dev/null
+++ b/sys/compat/linux/linux_elf64.c
@@ -0,0 +1,38 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2021 Edward Tomasz Napierala <trasz@FreeBSD.org>
+ * Copyright (c) 2002 Doug Rabson
+ * All rights reserved.
+ *
+ * This software was developed by the University of Cambridge Computer
+ * Laboratory as part of the CHERI for Hypervisors and Operating Systems
+ * (CHaOS) project, funded by EPSRC grant EP/V000292/1.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#define __ELF_WORD_SIZE 64
+#include <compat/linux/linux_elf.c>
diff --git a/sys/compat/linux/linux_emul.h b/sys/compat/linux/linux_emul.h
index de66a7a4c82a..d5f37c918b6c 100644
--- a/sys/compat/linux/linux_emul.h
+++ b/sys/compat/linux/linux_emul.h
@@ -33,6 +33,10 @@
 #define	_LINUX_EMUL_H_
 
 struct image_params;
+struct note_info_list;
+
+/* Linux core notes are labeled "CORE" */
+#define	LINUX_ABI_VENDOR	"CORE"
 
 /*
  * modeled after similar structure in NetBSD
@@ -58,6 +62,8 @@ void	linux_on_exec(struct proc *, struct image_params *);
 void	linux_thread_dtor(struct thread *);
 void	linux_thread_detach(struct thread *);
 int	linux_common_execve(struct thread *, struct image_args *);
+void 	linux32_prepare_notes(struct thread *, struct note_info_list *, size_t *);
+void 	linux64_prepare_notes(struct thread *, struct note_info_list *, size_t *);
 
 /* process emuldata flags */
 #define	LINUX_XDEPR_REQUEUEOP	0x00000001	/* uses deprecated
diff --git a/sys/modules/linux/Makefile b/sys/modules/linux/Makefile
index fd027e2118df..4f12c4dc6913 100644
--- a/sys/modules/linux/Makefile
+++ b/sys/modules/linux/Makefile
@@ -26,6 +26,7 @@ SRCS+=	linux_dummy_x86.c
 .endif
 .if ${MACHINE_CPUARCH} == "amd64"
 SRCS+=	linux${SFX}_support.s
+SRCS+=	linux_elf32.c
 .else
 SRCS+=	linux_copyout.c
 .endif
diff --git a/sys/modules/linux64/Makefile b/sys/modules/linux64/Makefile
index 994f4aff1dac..c269a2251b76 100644
--- a/sys/modules/linux64/Makefile
+++ b/sys/modules/linux64/Makefile
@@ -18,6 +18,7 @@ SRCS=	linux_fork.c linux_dummy_machdep.c linux_file.c linux_event.c \
 	linux_support.s
 .if ${MACHINE_CPUARCH} == "amd64"
 SRCS+=	linux_dummy_x86.c
+SRCS+=	linux_elf64.c
 .endif
 DPSRCS=	assym.inc linux_genassym.c