2020-10-10 03:10:06 +03:00
|
|
|
/* $NetBSD: core_elf32.c,v 1.65 2020/10/10 00:10:06 rin Exp $ */
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Wasabi Systems, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2020-10-10 03:10:06 +03:00
|
|
|
__KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.65 2020/10/10 00:10:06 rin Exp $");
|
2008-11-19 21:35:57 +03:00
|
|
|
|
|
|
|
#ifdef _KERNEL_OPT
|
2016-06-27 04:46:04 +03:00
|
|
|
#include "opt_compat_netbsd32.h"
|
2008-11-19 21:35:57 +03:00
|
|
|
#endif
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
2005-06-10 09:10:12 +04:00
|
|
|
#include <sys/exec.h>
|
2001-12-10 02:05:56 +03:00
|
|
|
#include <sys/exec_elf.h>
|
|
|
|
#include <sys/ptrace.h>
|
2012-01-27 23:48:38 +04:00
|
|
|
#include <sys/kmem.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2019-11-20 22:37:51 +03:00
|
|
|
#include <sys/compat_stub.h>
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2020-10-10 03:10:06 +03:00
|
|
|
#ifdef COMPAT_NETBSD32
|
|
|
|
#include <machine/netbsd32_machdep.h>
|
|
|
|
#endif
|
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
struct writesegs_state {
|
2005-06-02 21:01:43 +04:00
|
|
|
Elf_Phdr *psections;
|
2014-01-04 01:12:18 +04:00
|
|
|
proc_t *p;
|
|
|
|
off_t secoff;
|
|
|
|
size_t npsections;
|
2014-01-04 00:52:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to know how big the 'notes' are before we write the main header.
|
|
|
|
* To avoid problems with double-processing we save the data.
|
|
|
|
*/
|
|
|
|
struct note_buf {
|
|
|
|
struct note_buf *nb_next;
|
|
|
|
unsigned char nb_data[4096 - sizeof (void *)];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct note_state {
|
|
|
|
struct note_buf *ns_first;
|
|
|
|
struct note_buf *ns_last;
|
|
|
|
unsigned int ns_count; /* Of full buffers */
|
|
|
|
unsigned int ns_offset; /* Write point in last buffer */
|
2001-12-10 04:52:26 +03:00
|
|
|
};
|
|
|
|
|
2014-01-04 01:12:18 +04:00
|
|
|
static int ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
static int ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
|
|
|
|
static int ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/* The 'note' section names and data are always 4-byte aligned. */
|
2001-12-10 02:05:56 +03:00
|
|
|
#define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
|
|
|
|
|
2019-12-24 17:50:59 +03:00
|
|
|
#define elf_read_lwpstatus CONCAT(process_read_lwpstatus, ELFSIZE)
|
|
|
|
#define elf_lwpstatus CONCAT(process_lwpstatus, ELFSIZE)
|
|
|
|
|
2006-03-12 23:25:25 +03:00
|
|
|
#define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
|
|
|
|
#define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
|
|
|
|
#define elf_reg CONCAT(process_reg, ELFSIZE)
|
|
|
|
#define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
|
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
int
|
2014-01-01 22:57:15 +04:00
|
|
|
ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
|
|
|
Elf_Ehdr ehdr;
|
2014-04-02 21:19:49 +04:00
|
|
|
Elf_Shdr shdr;
|
2014-01-04 00:52:47 +04:00
|
|
|
Elf_Phdr *psections;
|
2012-01-27 23:48:38 +04:00
|
|
|
size_t psectionssize;
|
2014-01-03 19:15:02 +04:00
|
|
|
int npsections;
|
2001-12-10 04:52:26 +03:00
|
|
|
struct writesegs_state ws;
|
2014-01-04 00:52:47 +04:00
|
|
|
off_t notestart;
|
2003-05-09 00:26:40 +04:00
|
|
|
size_t notesize;
|
2005-06-02 21:01:43 +04:00
|
|
|
int error, i;
|
2019-11-22 18:57:49 +03:00
|
|
|
off_t offset __diagused;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
struct note_state ns;
|
|
|
|
struct note_buf *nb;
|
|
|
|
|
2005-06-02 21:01:43 +04:00
|
|
|
psections = NULL;
|
2014-01-04 00:52:47 +04:00
|
|
|
|
|
|
|
/* Get all of the notes (mostly all the registers). */
|
|
|
|
ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
|
|
|
|
ns.ns_last = ns.ns_first;
|
|
|
|
ns.ns_count = 0;
|
|
|
|
ns.ns_offset = 0;
|
|
|
|
error = ELFNAMEEND(coredump_notes)(l, &ns);
|
|
|
|
ns.ns_last->nb_next = NULL;
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
|
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
/*
|
|
|
|
* We have to make a total of 3 passes across the map:
|
|
|
|
*
|
|
|
|
* 1. Count the number of map entries (the number of
|
2014-01-03 19:15:02 +04:00
|
|
|
* PT_LOAD sections in the dump).
|
2001-12-10 02:05:56 +03:00
|
|
|
*
|
|
|
|
* 2. Write the P-section headers.
|
|
|
|
*
|
|
|
|
* 3. Write the P-sections.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pass 1: count the entries. */
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(uvm_coredump_count_segs_hook,
|
|
|
|
(l->l_proc), 0, npsections);
|
2014-01-04 00:52:47 +04:00
|
|
|
/* Allow for the PT_NOTE section. */
|
2014-01-03 19:15:02 +04:00
|
|
|
npsections++;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/* Build the main elf header */
|
2005-06-10 09:10:12 +04:00
|
|
|
memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
|
2001-12-10 02:05:56 +03:00
|
|
|
memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
|
|
|
|
#if ELFSIZE == 32
|
|
|
|
ehdr.e_ident[EI_CLASS] = ELFCLASS32;
|
|
|
|
#elif ELFSIZE == 64
|
|
|
|
ehdr.e_ident[EI_CLASS] = ELFCLASS64;
|
|
|
|
#endif
|
|
|
|
ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
|
|
|
|
ehdr.e_ident[EI_VERSION] = EV_CURRENT;
|
2017-05-04 14:12:23 +03:00
|
|
|
/*
|
|
|
|
* NetBSD sets generic SYSV OSABI and ABI version 0
|
|
|
|
* Native ELF files are distinguishable with NetBSD specific notes
|
|
|
|
*/
|
2001-12-10 02:05:56 +03:00
|
|
|
ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
|
2001-12-10 04:52:26 +03:00
|
|
|
ehdr.e_ident[EI_ABIVERSION] = 0;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
ehdr.e_type = ET_CORE;
|
|
|
|
/* XXX This should be the e_machine of the executable. */
|
|
|
|
ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
|
|
|
|
ehdr.e_version = EV_CURRENT;
|
|
|
|
ehdr.e_entry = 0;
|
|
|
|
ehdr.e_flags = 0;
|
|
|
|
ehdr.e_ehsize = sizeof(ehdr);
|
|
|
|
ehdr.e_phentsize = sizeof(Elf_Phdr);
|
2014-04-02 21:19:49 +04:00
|
|
|
if (npsections < PN_XNUM) {
|
|
|
|
ehdr.e_phnum = npsections;
|
|
|
|
ehdr.e_shentsize = 0;
|
|
|
|
ehdr.e_shnum = 0;
|
|
|
|
ehdr.e_shoff = 0;
|
|
|
|
ehdr.e_phoff = sizeof(ehdr);
|
|
|
|
} else {
|
|
|
|
ehdr.e_phnum = PN_XNUM;
|
|
|
|
ehdr.e_shentsize = sizeof(Elf_Shdr);
|
|
|
|
ehdr.e_shnum = 1;
|
|
|
|
ehdr.e_shoff = sizeof(ehdr);
|
|
|
|
ehdr.e_phoff = sizeof(ehdr) + sizeof(shdr);
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
ehdr.e_shstrndx = 0;
|
|
|
|
|
2009-12-14 03:47:10 +03:00
|
|
|
#ifdef ELF_MD_COREDUMP_SETUP
|
|
|
|
ELF_MD_COREDUMP_SETUP(l, &ehdr);
|
|
|
|
#endif
|
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
/* Write out the ELF header. */
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, &ehdr,
|
|
|
|
sizeof(ehdr)), ENOSYS, error);
|
2005-06-02 21:01:43 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
2014-04-02 21:19:49 +04:00
|
|
|
/* Write out sections, if needed */
|
|
|
|
if (npsections >= PN_XNUM) {
|
|
|
|
memset(&shdr, 0, sizeof(shdr));
|
|
|
|
shdr.sh_type = SHT_NULL;
|
|
|
|
shdr.sh_info = npsections;
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
|
|
|
|
&shdr, sizeof(shdr)), ENOSYS, error);
|
2014-04-02 21:19:49 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
psectionssize = npsections * sizeof(*psections);
|
2014-04-02 21:19:49 +04:00
|
|
|
notestart = ehdr.e_phoff + psectionssize;
|
2005-06-02 21:01:43 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
psections = kmem_zalloc(psectionssize, KM_SLEEP);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-03 19:15:02 +04:00
|
|
|
/* Pass 2: now find the P-section headers. */
|
2014-01-04 00:52:47 +04:00
|
|
|
ws.secoff = notestart + notesize;
|
2005-06-02 21:01:43 +04:00
|
|
|
ws.psections = psections;
|
2014-01-04 00:52:47 +04:00
|
|
|
ws.npsections = npsections - 1;
|
2014-01-04 01:34:40 +04:00
|
|
|
ws.p = l->l_proc;
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(uvm_coredump_walkmap_hook,
|
|
|
|
(l->l_proc, ELFNAMEEND(coredump_getseghdrs), &ws), ENOSYS, error);
|
2001-12-10 04:52:26 +03:00
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
2014-01-04 00:52:47 +04:00
|
|
|
if (ws.npsections != 0) {
|
|
|
|
/* A section went away */
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-03 19:15:02 +04:00
|
|
|
/* Add the PT_NOTE header after the P-section headers. */
|
2005-06-02 21:01:43 +04:00
|
|
|
ws.psections->p_type = PT_NOTE;
|
|
|
|
ws.psections->p_offset = notestart;
|
|
|
|
ws.psections->p_vaddr = 0;
|
|
|
|
ws.psections->p_paddr = 0;
|
|
|
|
ws.psections->p_filesz = notesize;
|
|
|
|
ws.psections->p_memsz = 0;
|
|
|
|
ws.psections->p_flags = PF_R;
|
|
|
|
ws.psections->p_align = ELFROUNDSIZE;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/* Write the P-section headers followed by the PT_NOTE header */
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE, psections,
|
|
|
|
psectionssize), ENOSYS, error);
|
2001-12-10 02:05:56 +03:00
|
|
|
if (error)
|
2005-06-02 21:01:43 +04:00
|
|
|
goto out;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
|
|
|
|
if (offset != notestart)
|
2001-12-10 04:52:26 +03:00
|
|
|
panic("coredump: offset %lld != notestart %lld",
|
2019-11-20 22:37:51 +03:00
|
|
|
(long long) offset,
|
2014-01-04 00:52:47 +04:00
|
|
|
(long long) notestart);
|
2001-12-10 04:52:26 +03:00
|
|
|
#endif
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* Write out the notes. */
|
2014-01-04 00:52:47 +04:00
|
|
|
for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_SYSSPACE,
|
|
|
|
nb->nb_data,
|
|
|
|
nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data),
|
|
|
|
ENOSYS, error);
|
2014-01-04 00:52:47 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
2001-12-10 04:52:26 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/* Finally, write the sections themselves. */
|
2014-01-03 19:15:02 +04:00
|
|
|
for (i = 0; i < npsections - 1; i++) {
|
2005-06-02 21:01:43 +04:00
|
|
|
if (psections[i].p_filesz == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_offset_hook, (cookie), 0, offset);
|
|
|
|
if (offset != psections[i].p_offset)
|
2005-06-02 21:01:43 +04:00
|
|
|
panic("coredump: offset %lld != p_offset[%d] %lld",
|
2019-11-20 22:37:51 +03:00
|
|
|
(long long) offset, i,
|
2005-06-02 21:01:43 +04:00
|
|
|
(long long) psections[i].p_filesz);
|
|
|
|
#endif
|
|
|
|
|
2019-11-20 22:37:51 +03:00
|
|
|
MODULE_HOOK_CALL(coredump_write_hook, (cookie, UIO_USERSPACE,
|
2005-06-10 09:10:12 +04:00
|
|
|
(void *)(vaddr_t)psections[i].p_vaddr,
|
2019-11-20 22:37:51 +03:00
|
|
|
psections[i].p_filesz), ENOSYS, error);
|
2005-06-02 21:01:43 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (psections)
|
2012-01-27 23:48:38 +04:00
|
|
|
kmem_free(psections, psectionssize);
|
2014-01-05 13:13:18 +04:00
|
|
|
while ((nb = ns.ns_first) != NULL) {
|
|
|
|
ns.ns_first = nb->nb_next;
|
2014-01-04 00:52:47 +04:00
|
|
|
kmem_free(nb, sizeof *nb);
|
2014-01-05 04:53:53 +04:00
|
|
|
}
|
2001-12-10 04:52:26 +03:00
|
|
|
return (error);
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2005-12-08 06:05:40 +03:00
|
|
|
static int
|
2014-01-04 01:12:18 +04:00
|
|
|
ELFNAMEEND(coredump_getseghdrs)(struct uvm_coredump_state *us)
|
2001-12-10 04:52:26 +03:00
|
|
|
{
|
|
|
|
struct writesegs_state *ws = us->cookie;
|
|
|
|
Elf_Phdr phdr;
|
2005-06-02 21:01:43 +04:00
|
|
|
vsize_t size, realsize;
|
|
|
|
vaddr_t end;
|
2001-12-10 04:52:26 +03:00
|
|
|
int error;
|
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/* Don't overrun if there are more sections */
|
|
|
|
if (ws->npsections == 0)
|
|
|
|
return ENOMEM;
|
|
|
|
ws->npsections--;
|
|
|
|
|
2001-12-10 04:52:26 +03:00
|
|
|
size = us->end - us->start;
|
2005-06-02 21:01:43 +04:00
|
|
|
realsize = us->realend - us->start;
|
|
|
|
end = us->realend;
|
|
|
|
|
2014-01-03 19:15:02 +04:00
|
|
|
/* Don't bother writing out trailing zeros */
|
2005-06-02 21:01:43 +04:00
|
|
|
while (realsize > 0) {
|
|
|
|
long buf[1024 / sizeof(long)];
|
|
|
|
size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
|
|
|
|
const long *ep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
end -= slen;
|
2019-01-22 06:44:44 +03:00
|
|
|
if ((error = copyin_proc(ws->p, (void *)end, buf, slen)) != 0) {
|
|
|
|
/*
|
|
|
|
* In case of any errors of scanning the segments reset
|
|
|
|
* their content to a default value with zeros. This is
|
|
|
|
* achieved with shortening the p_filesz parameter.
|
|
|
|
*
|
|
|
|
* This allows to emit core(5) files for a process
|
|
|
|
* regardless of its state of mappings, such as mapping
|
|
|
|
* pages after EOF in a file.
|
|
|
|
*/
|
|
|
|
realsize -= slen;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-02 21:01:43 +04:00
|
|
|
|
|
|
|
ep = (const long *) &buf[slen / sizeof(buf[0])];
|
|
|
|
for (i = 0, ep--; buf <= ep; ep--, i++) {
|
|
|
|
if (*ep)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
realsize -= i * sizeof(buf[0]);
|
|
|
|
if (i * sizeof(buf[0]) < slen)
|
|
|
|
break;
|
|
|
|
}
|
2001-12-10 04:52:26 +03:00
|
|
|
|
|
|
|
phdr.p_type = PT_LOAD;
|
|
|
|
phdr.p_offset = ws->secoff;
|
|
|
|
phdr.p_vaddr = us->start;
|
|
|
|
phdr.p_paddr = 0;
|
2005-06-02 21:01:43 +04:00
|
|
|
phdr.p_filesz = realsize;
|
2001-12-10 04:52:26 +03:00
|
|
|
phdr.p_memsz = size;
|
|
|
|
phdr.p_flags = 0;
|
|
|
|
if (us->prot & VM_PROT_READ)
|
|
|
|
phdr.p_flags |= PF_R;
|
|
|
|
if (us->prot & VM_PROT_WRITE)
|
|
|
|
phdr.p_flags |= PF_W;
|
|
|
|
if (us->prot & VM_PROT_EXECUTE)
|
|
|
|
phdr.p_flags |= PF_X;
|
|
|
|
phdr.p_align = PAGE_SIZE;
|
|
|
|
|
|
|
|
ws->secoff += phdr.p_filesz;
|
2005-06-02 21:01:43 +04:00
|
|
|
*ws->psections++ = phdr;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-05-24 03:49:55 +03:00
|
|
|
static void
|
|
|
|
coredump_note_procinfo(struct lwp *l, struct note_state *ns)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
2014-01-04 00:52:47 +04:00
|
|
|
struct proc *p;
|
2001-12-10 02:05:56 +03:00
|
|
|
struct netbsd_elfcore_procinfo cpi;
|
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
p = l->l_proc;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* First, write an elfcore_procinfo. */
|
2014-01-04 00:52:47 +04:00
|
|
|
cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
|
|
|
|
cpi.cpi_cpisize = sizeof(cpi);
|
2017-01-07 01:53:17 +03:00
|
|
|
cpi.cpi_signo = p->p_sigctx.ps_info._signo;
|
|
|
|
cpi.cpi_sigcode = p->p_sigctx.ps_info._code;
|
2014-01-04 00:52:47 +04:00
|
|
|
cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/*
|
2019-12-24 17:50:59 +03:00
|
|
|
* per-LWP pending signals are stored in PT_LWPSTATUS@nnn.
|
2014-01-04 00:52:47 +04:00
|
|
|
*/
|
2019-12-24 17:50:59 +03:00
|
|
|
memcpy(&cpi.cpi_sigpend, &p->p_sigpend.sp_set, sizeof(cpi.cpi_sigpend));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal mask is stored on a per-LWP basis in PT_LWPSTATUS@nnn.
|
|
|
|
* For compatibility purposes, cpi_sigmask is present, but zeroed.
|
|
|
|
*/
|
|
|
|
memset(&cpi.cpi_sigmask, 0, sizeof(cpi.cpi_sigmask));
|
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
|
|
|
|
sizeof(cpi.cpi_sigignore));
|
|
|
|
memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
|
|
|
|
sizeof(cpi.cpi_sigcatch));
|
|
|
|
|
|
|
|
cpi.cpi_pid = p->p_pid;
|
2020-05-24 02:42:41 +03:00
|
|
|
mutex_enter(&proc_lock);
|
2014-01-04 00:52:47 +04:00
|
|
|
cpi.cpi_ppid = p->p_pptr->p_pid;
|
|
|
|
cpi.cpi_pgrp = p->p_pgid;
|
|
|
|
cpi.cpi_sid = p->p_session->s_sid;
|
2020-05-24 02:42:41 +03:00
|
|
|
mutex_exit(&proc_lock);
|
2014-01-04 00:52:47 +04:00
|
|
|
|
|
|
|
cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
|
|
|
|
cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
|
|
|
|
cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
|
|
|
|
|
|
|
|
cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
|
|
|
|
cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
|
|
|
|
cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
|
|
|
|
|
|
|
|
cpi.cpi_nlwps = p->p_nlwps;
|
|
|
|
(void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
|
|
|
|
cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
|
|
|
|
|
2014-01-04 01:34:40 +04:00
|
|
|
ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
|
|
|
|
ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
|
2016-05-24 03:49:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
coredump_note_auxv(struct lwp *l, struct note_state *ns)
|
|
|
|
{
|
|
|
|
int error;
|
2017-03-30 23:17:11 +03:00
|
|
|
size_t len;
|
|
|
|
void *kauxv;
|
2016-05-24 03:49:55 +03:00
|
|
|
|
2017-03-30 23:17:11 +03:00
|
|
|
if ((error = proc_getauxv(l->l_proc, &kauxv, &len)) != 0)
|
2016-05-24 03:49:55 +03:00
|
|
|
return error;
|
|
|
|
|
2017-03-30 23:17:11 +03:00
|
|
|
ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_AUXV,
|
|
|
|
ELF_NOTE_NETBSD_CORE_NAME, kauxv, len);
|
2016-05-24 03:49:55 +03:00
|
|
|
|
|
|
|
kmem_free(kauxv, len);
|
2017-03-30 23:17:11 +03:00
|
|
|
return 0;
|
2016-05-24 03:49:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
|
|
|
/* XXX Add hook for machdep per-proc notes. */
|
|
|
|
|
2003-05-09 00:26:40 +04:00
|
|
|
/*
|
|
|
|
* Now write the register info for the thread that caused the
|
|
|
|
* coredump.
|
|
|
|
*/
|
2014-01-04 00:52:47 +04:00
|
|
|
error = ELFNAMEEND(coredump_note)(l, ns);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
2016-05-24 03:49:55 +03:00
|
|
|
return error;
|
2003-05-09 00:26:40 +04:00
|
|
|
|
2001-12-10 02:05:56 +03:00
|
|
|
/*
|
|
|
|
* Now, for each LWP, write the register info and any other
|
2014-01-04 01:12:18 +04:00
|
|
|
* per-LWP notes.
|
|
|
|
* Lock in case this is a gcore requested dump.
|
2001-12-10 02:05:56 +03:00
|
|
|
*/
|
2014-01-04 01:12:18 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2003-05-09 00:26:40 +04:00
|
|
|
LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
|
|
|
|
if (l0 == l) /* we've taken care of this thread */
|
|
|
|
continue;
|
2014-01-04 00:52:47 +04:00
|
|
|
error = ELFNAMEEND(coredump_note)(l0, ns);
|
2003-05-09 00:26:40 +04:00
|
|
|
if (error)
|
2014-01-04 01:12:18 +04:00
|
|
|
break;
|
2003-05-09 00:26:40 +04:00
|
|
|
}
|
2014-01-04 01:12:18 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 01:12:18 +04:00
|
|
|
return error;
|
2003-05-09 00:26:40 +04:00
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
struct elf_coredump_note_data {
|
2014-01-04 00:52:47 +04:00
|
|
|
char name[64];
|
2019-12-24 17:50:59 +03:00
|
|
|
elf_lwpstatus els;
|
2006-03-12 23:25:25 +03:00
|
|
|
elf_reg intreg;
|
2003-05-09 00:26:40 +04:00
|
|
|
#ifdef PT_GETFPREGS
|
2006-03-12 23:25:25 +03:00
|
|
|
elf_fpreg freg;
|
2020-05-30 19:12:56 +03:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
|
|
|
|
{
|
|
|
|
struct elf_coredump_note_data *d;
|
|
|
|
#ifdef PT_GETFPREGS
|
2014-01-04 00:52:47 +04:00
|
|
|
size_t freglen;
|
2001-12-10 02:05:56 +03:00
|
|
|
#endif
|
2020-05-30 19:12:56 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
d = kmem_alloc(sizeof(*d), KM_SLEEP);
|
2003-05-09 00:26:40 +04:00
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
snprintf(d->name, sizeof(d->name), "%s@%d",
|
2005-06-10 09:10:12 +04:00
|
|
|
ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
|
2003-05-09 00:26:40 +04:00
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
elf_read_lwpstatus(l, &d->els);
|
2019-12-24 17:50:59 +03:00
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
ELFNAMEEND(coredump_savenote)(ns, PT_LWPSTATUS, d->name, &d->els,
|
|
|
|
sizeof(d->els));
|
2019-12-24 17:50:59 +03:00
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
error = elf_process_read_regs(l, &d->intreg);
|
2014-01-04 00:52:47 +04:00
|
|
|
if (error)
|
2020-05-30 19:12:56 +03:00
|
|
|
goto out;
|
2003-05-09 00:26:40 +04:00
|
|
|
|
2020-10-10 03:10:06 +03:00
|
|
|
ELFNAMEEND(coredump_savenote)(ns,
|
|
|
|
#if ELFSIZE == 32 && defined(PT32_GETREGS)
|
|
|
|
PT32_GETREGS,
|
|
|
|
#else
|
|
|
|
PT_GETREGS,
|
|
|
|
#endif
|
|
|
|
d->name, &d->intreg, sizeof(d->intreg));
|
2003-05-09 00:26:40 +04:00
|
|
|
|
|
|
|
#ifdef PT_GETFPREGS
|
2020-05-30 19:12:56 +03:00
|
|
|
freglen = sizeof(d->freg);
|
|
|
|
error = elf_process_read_fpregs(l, &d->freg, &freglen);
|
2014-01-04 00:52:47 +04:00
|
|
|
if (error)
|
2020-05-30 19:12:56 +03:00
|
|
|
goto out;
|
2003-05-09 00:26:40 +04:00
|
|
|
|
2020-10-10 03:10:06 +03:00
|
|
|
ELFNAMEEND(coredump_savenote)(ns,
|
|
|
|
# if ELFSIZE == 32 && defined(PT32_GETFPREGS)
|
|
|
|
PT32_GETFPREGS,
|
|
|
|
# else
|
|
|
|
PT_GETFPREGS,
|
|
|
|
# endif
|
|
|
|
d->name, &d->freg, freglen);
|
2003-05-09 00:26:40 +04:00
|
|
|
#endif
|
2020-01-08 20:21:38 +03:00
|
|
|
|
|
|
|
#ifdef COREDUMP_MACHDEP_LWP_NOTES
|
2020-05-30 19:12:56 +03:00
|
|
|
COREDUMP_MACHDEP_LWP_NOTES(l, ns, d->name);
|
2020-01-08 20:21:38 +03:00
|
|
|
#endif
|
|
|
|
|
2020-05-30 19:12:56 +03:00
|
|
|
out:
|
|
|
|
kmem_free(d, sizeof(*d));
|
|
|
|
return (error);
|
2001-12-10 02:05:56 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
static void
|
|
|
|
save_note_bytes(struct note_state *ns, const void *data, size_t len)
|
2001-12-10 02:05:56 +03:00
|
|
|
{
|
2014-01-04 00:52:47 +04:00
|
|
|
struct note_buf *nb = ns->ns_last;
|
|
|
|
size_t copylen;
|
|
|
|
unsigned char *wp;
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
/*
|
|
|
|
* Just copy the data into a buffer list.
|
|
|
|
* All but the last buffer is full.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
Rename min/max -> uimin/uimax for better honesty.
These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.
HOWEVER! Some subsystems have
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.
To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.
I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:
cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))
It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.
Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
2018-09-03 19:29:22 +03:00
|
|
|
copylen = uimin(len, sizeof(nb->nb_data) - ns->ns_offset);
|
2014-01-04 00:52:47 +04:00
|
|
|
wp = nb->nb_data + ns->ns_offset;
|
|
|
|
memcpy(wp, data, copylen);
|
|
|
|
if (copylen == len)
|
|
|
|
break;
|
2018-01-21 20:22:55 +03:00
|
|
|
nb->nb_next = kmem_alloc(sizeof(*nb->nb_next), KM_SLEEP);
|
2014-01-04 00:52:47 +04:00
|
|
|
nb = nb->nb_next;
|
|
|
|
ns->ns_last = nb;
|
|
|
|
ns->ns_count++;
|
|
|
|
ns->ns_offset = 0;
|
|
|
|
len -= copylen;
|
|
|
|
data = (const unsigned char *)data + copylen;
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2018-01-21 20:22:55 +03:00
|
|
|
while ((copylen & (ELFROUNDSIZE - 1)) &&
|
|
|
|
wp + copylen < nb->nb_data + sizeof(nb->nb_data))
|
|
|
|
wp[copylen++] = 0;
|
2014-01-04 00:52:47 +04:00
|
|
|
|
|
|
|
ns->ns_offset += copylen;
|
|
|
|
}
|
2001-12-10 02:05:56 +03:00
|
|
|
|
2014-01-04 00:52:47 +04:00
|
|
|
void
|
2014-01-04 01:34:40 +04:00
|
|
|
ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
|
|
|
|
const char *name, void *data, size_t data_len)
|
2014-01-04 00:52:47 +04:00
|
|
|
{
|
2014-01-04 01:34:40 +04:00
|
|
|
Elf_Nhdr nhdr;
|
|
|
|
|
|
|
|
nhdr.n_namesz = strlen(name) + 1;
|
|
|
|
nhdr.n_descsz = data_len;
|
|
|
|
nhdr.n_type = type;
|
|
|
|
|
|
|
|
save_note_bytes(ns, &nhdr, sizeof (nhdr));
|
|
|
|
save_note_bytes(ns, name, nhdr.n_namesz);
|
|
|
|
save_note_bytes(ns, data, data_len);
|
2001-12-10 02:05:56 +03:00
|
|
|
}
|