Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.19
      1  1.19    kleink /*	$NetBSD: core_elf32.c,v 1.19 2005/07/06 20:31:33 kleink Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 2001 Wasabi Systems, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8   1.1   thorpej  *
      9   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10   1.1   thorpej  * modification, are permitted provided that the following conditions
     11   1.1   thorpej  * are met:
     12   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18   1.1   thorpej  *    must display the following acknowledgement:
     19   1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20   1.1   thorpej  *	Wasabi Systems, Inc.
     21   1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1   thorpej  *    or promote products derived from this software without specific prior
     23   1.1   thorpej  *    written permission.
     24   1.1   thorpej  *
     25   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1   thorpej  */
     37   1.1   thorpej 
     38   1.1   thorpej /*
     39   1.1   thorpej  * core_elf32.c/core_elf64.c: Support for the Elf32/Elf64 core file format.
     40   1.1   thorpej  */
     41   1.1   thorpej 
     42   1.1   thorpej #include <sys/cdefs.h>
     43  1.19    kleink __KERNEL_RCSID(1, "$NetBSD: core_elf32.c,v 1.19 2005/07/06 20:31:33 kleink Exp $");
     44   1.1   thorpej 
     45   1.1   thorpej /* If not included by core_elf64.c, ELFSIZE won't be defined. */
     46   1.1   thorpej #ifndef ELFSIZE
     47   1.1   thorpej #define	ELFSIZE		32
     48   1.1   thorpej #endif
     49   1.1   thorpej 
     50   1.1   thorpej #include <sys/param.h>
     51   1.1   thorpej #include <sys/systm.h>
     52   1.1   thorpej #include <sys/proc.h>
     53   1.1   thorpej #include <sys/vnode.h>
     54  1.18      matt #include <sys/exec.h>
     55   1.1   thorpej #include <sys/exec_elf.h>
     56   1.1   thorpej #include <sys/ptrace.h>
     57  1.16      matt #include <sys/malloc.h>
     58   1.1   thorpej 
     59   1.1   thorpej #include <machine/reg.h>
     60   1.1   thorpej 
     61   1.2   thorpej #include <uvm/uvm_extern.h>
     62   1.2   thorpej 
     63   1.2   thorpej struct countsegs_state {
     64   1.2   thorpej 	int	npsections;
     65   1.2   thorpej };
     66   1.2   thorpej 
     67  1.18      matt int	ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
     68  1.18      matt 	    struct uvm_coredump_state *);
     69   1.2   thorpej 
     70   1.2   thorpej struct writesegs_state {
     71  1.16      matt 	Elf_Phdr *psections;
     72   1.2   thorpej 	off_t	secoff;
     73   1.2   thorpej };
     74   1.2   thorpej 
     75  1.18      matt int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
     76  1.18      matt 	    struct uvm_coredump_state *);
     77  1.18      matt int	ELFNAMEEND(coredump_writesegs)(struct proc *, void *,
     78  1.18      matt 	    struct uvm_coredump_state *);
     79  1.18      matt 
     80  1.18      matt int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
     81  1.18      matt 	    size_t *);
     82  1.18      matt int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
     83  1.18      matt 	    size_t *);
     84   1.1   thorpej 
     85   1.1   thorpej #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     86   1.1   thorpej #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     87   1.1   thorpej 
     88   1.1   thorpej int
     89  1.18      matt ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
     90   1.1   thorpej {
     91   1.4   thorpej 	struct proc *p;
     92   1.1   thorpej 	Elf_Ehdr ehdr;
     93  1.16      matt 	Elf_Phdr phdr, *psections;
     94   1.2   thorpej 	struct countsegs_state cs;
     95   1.2   thorpej 	struct writesegs_state ws;
     96  1.16      matt 	off_t notestart, secstart, offset;
     97   1.6      matt 	size_t notesize;
     98  1.16      matt 	int error, i;
     99   1.1   thorpej 
    100  1.16      matt 	psections = NULL;
    101   1.4   thorpej 	p = l->l_proc;
    102   1.1   thorpej 	/*
    103   1.1   thorpej 	 * We have to make a total of 3 passes across the map:
    104   1.1   thorpej 	 *
    105   1.1   thorpej 	 *	1. Count the number of map entries (the number of
    106   1.1   thorpej 	 *	   PT_LOAD sections).
    107   1.1   thorpej 	 *
    108   1.1   thorpej 	 *	2. Write the P-section headers.
    109   1.1   thorpej 	 *
    110   1.1   thorpej 	 *	3. Write the P-sections.
    111   1.1   thorpej 	 */
    112   1.1   thorpej 
    113   1.1   thorpej 	/* Pass 1: count the entries. */
    114   1.2   thorpej 	cs.npsections = 0;
    115  1.18      matt 	error = uvm_coredump_walkmap(p, NULL,
    116   1.2   thorpej 	    ELFNAMEEND(coredump_countsegs), &cs);
    117   1.2   thorpej 	if (error)
    118  1.16      matt 		goto out;
    119  1.16      matt 
    120  1.16      matt 	/* Count the PT_NOTE section. */
    121  1.16      matt 	cs.npsections++;
    122   1.1   thorpej 
    123   1.1   thorpej 	/* Get the size of the notes. */
    124  1.18      matt 	error = ELFNAMEEND(coredump_notes)(p, l, NULL, &notesize);
    125   1.1   thorpej 	if (error)
    126  1.16      matt 		goto out;
    127   1.1   thorpej 
    128  1.18      matt 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    129   1.1   thorpej 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    130   1.1   thorpej #if ELFSIZE == 32
    131   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    132   1.1   thorpej #elif ELFSIZE == 64
    133   1.1   thorpej 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    134   1.1   thorpej #endif
    135   1.1   thorpej 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    136   1.1   thorpej 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    137   1.1   thorpej 	/* XXX Should be the OSABI/ABI version of the executable. */
    138   1.1   thorpej 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    139   1.2   thorpej 	ehdr.e_ident[EI_ABIVERSION] = 0;
    140   1.1   thorpej 
    141   1.1   thorpej 	ehdr.e_type = ET_CORE;
    142   1.1   thorpej 	/* XXX This should be the e_machine of the executable. */
    143   1.1   thorpej 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    144   1.1   thorpej 	ehdr.e_version = EV_CURRENT;
    145   1.1   thorpej 	ehdr.e_entry = 0;
    146   1.1   thorpej 	ehdr.e_phoff = sizeof(ehdr);
    147   1.1   thorpej 	ehdr.e_shoff = 0;
    148   1.1   thorpej 	ehdr.e_flags = 0;
    149   1.1   thorpej 	ehdr.e_ehsize = sizeof(ehdr);
    150   1.1   thorpej 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    151   1.2   thorpej 	ehdr.e_phnum = cs.npsections;
    152   1.1   thorpej 	ehdr.e_shentsize = 0;
    153   1.1   thorpej 	ehdr.e_shnum = 0;
    154   1.1   thorpej 	ehdr.e_shstrndx = 0;
    155   1.1   thorpej 
    156   1.1   thorpej 	/* Write out the ELF header. */
    157  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    158  1.16      matt 	if (error)
    159  1.16      matt 		goto out;
    160  1.16      matt 
    161  1.18      matt 	offset = sizeof(ehdr);
    162   1.1   thorpej 
    163  1.16      matt 	notestart = offset + sizeof(phdr) * cs.npsections;
    164  1.16      matt 	secstart = notestart + notesize;
    165  1.16      matt 
    166  1.16      matt 	psections = malloc(cs.npsections * sizeof(Elf_Phdr),
    167  1.16      matt 	    M_TEMP, M_WAITOK|M_ZERO);
    168   1.1   thorpej 
    169   1.5    atatat 	/* Pass 2: now write the P-section headers. */
    170   1.2   thorpej 	ws.secoff = secstart;
    171  1.16      matt 	ws.psections = psections;
    172  1.18      matt 	error = uvm_coredump_walkmap(p, cookie,
    173   1.2   thorpej 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    174   1.2   thorpej 	if (error)
    175  1.16      matt 		goto out;
    176   1.1   thorpej 
    177   1.1   thorpej 	/* Write out the PT_NOTE header. */
    178  1.16      matt 	ws.psections->p_type = PT_NOTE;
    179  1.16      matt 	ws.psections->p_offset = notestart;
    180  1.16      matt 	ws.psections->p_vaddr = 0;
    181  1.16      matt 	ws.psections->p_paddr = 0;
    182  1.16      matt 	ws.psections->p_filesz = notesize;
    183  1.16      matt 	ws.psections->p_memsz = 0;
    184  1.16      matt 	ws.psections->p_flags = PF_R;
    185  1.16      matt 	ws.psections->p_align = ELFROUNDSIZE;
    186   1.1   thorpej 
    187  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, psections,
    188  1.18      matt 	    cs.npsections * sizeof(Elf_Phdr));
    189   1.1   thorpej 	if (error)
    190  1.16      matt 		goto out;
    191   1.1   thorpej 
    192  1.18      matt #ifdef DIAGNOSTIC
    193  1.16      matt 	offset += cs.npsections * sizeof(Elf_Phdr);
    194  1.16      matt 	if (offset != notestart)
    195   1.2   thorpej 		panic("coredump: offset %lld != notestart %lld",
    196  1.16      matt 		    (long long) offset, (long long) notestart);
    197   1.2   thorpej #endif
    198   1.1   thorpej 
    199   1.1   thorpej 	/* Write out the notes. */
    200  1.18      matt 	error = ELFNAMEEND(coredump_notes)(p, l, cookie, &notesize);
    201  1.16      matt 	if (error)
    202  1.16      matt 		goto out;
    203   1.2   thorpej 
    204   1.2   thorpej #ifdef DIAGNOSTIC
    205  1.16      matt 	offset += notesize;
    206  1.16      matt 	if (offset != secstart)
    207   1.2   thorpej 		panic("coredump: offset %lld != secstart %lld",
    208  1.16      matt 		    (long long) offset, (long long) secstart);
    209   1.2   thorpej #endif
    210   1.2   thorpej 
    211   1.5    atatat 	/* Pass 3: finally, write the sections themselves. */
    212  1.16      matt 	for (i = 0; i < cs.npsections - 1; i++) {
    213  1.16      matt 		if (psections[i].p_filesz == 0)
    214  1.16      matt 			continue;
    215  1.16      matt 
    216  1.16      matt #ifdef DIAGNOSTIC
    217  1.16      matt 		if (offset != psections[i].p_offset)
    218  1.16      matt 			panic("coredump: offset %lld != p_offset[%d] %lld",
    219  1.16      matt 			    (long long) offset, i,
    220  1.16      matt 			    (long long) psections[i].p_filesz);
    221  1.16      matt #endif
    222  1.16      matt 
    223  1.18      matt 		error = coredump_write(cookie, UIO_USERSPACE,
    224  1.18      matt 		    (void *)(vaddr_t)psections[i].p_vaddr,
    225  1.18      matt 		    psections[i].p_filesz);
    226  1.16      matt 		if (error)
    227  1.16      matt 			goto out;
    228  1.16      matt 
    229  1.16      matt #ifdef DIAGNOSTIC
    230  1.16      matt 		offset += psections[i].p_filesz;
    231  1.16      matt #endif
    232  1.16      matt 	}
    233   1.1   thorpej 
    234  1.16      matt   out:
    235  1.16      matt 	if (psections)
    236  1.16      matt 		free(psections, M_TEMP);
    237   1.2   thorpej 	return (error);
    238   1.2   thorpej }
    239   1.2   thorpej 
    240   1.2   thorpej int
    241  1.18      matt ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
    242  1.18      matt     struct uvm_coredump_state *us)
    243   1.2   thorpej {
    244   1.2   thorpej 	struct countsegs_state *cs = us->cookie;
    245   1.1   thorpej 
    246   1.2   thorpej 	cs->npsections++;
    247   1.2   thorpej 	return (0);
    248   1.2   thorpej }
    249   1.2   thorpej 
    250   1.2   thorpej int
    251  1.18      matt ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
    252  1.18      matt     struct uvm_coredump_state *us)
    253   1.2   thorpej {
    254   1.2   thorpej 	struct writesegs_state *ws = us->cookie;
    255   1.2   thorpej 	Elf_Phdr phdr;
    256  1.16      matt 	vsize_t size, realsize;
    257  1.16      matt 	vaddr_t end;
    258   1.2   thorpej 	int error;
    259   1.2   thorpej 
    260   1.2   thorpej 	size = us->end - us->start;
    261  1.16      matt 	realsize = us->realend - us->start;
    262  1.16      matt 	end = us->realend;
    263  1.16      matt 
    264  1.16      matt 	while (realsize > 0) {
    265  1.16      matt 		long buf[1024 / sizeof(long)];
    266  1.16      matt 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    267  1.16      matt 		const long *ep;
    268  1.16      matt 		int i;
    269  1.16      matt 
    270  1.16      matt 		end -= slen;
    271  1.19    kleink 		error = copyin_proc(p, (void *) end, buf, slen);
    272  1.16      matt 		if (error)
    273  1.16      matt 			return (error);
    274  1.16      matt 
    275  1.16      matt 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    276  1.16      matt 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    277  1.16      matt 			if (*ep)
    278  1.16      matt 				break;
    279  1.16      matt 		}
    280  1.16      matt 		realsize -= i * sizeof(buf[0]);
    281  1.16      matt 		if (i * sizeof(buf[0]) < slen)
    282  1.16      matt 			break;
    283  1.16      matt 	}
    284   1.2   thorpej 
    285   1.2   thorpej 	phdr.p_type = PT_LOAD;
    286   1.2   thorpej 	phdr.p_offset = ws->secoff;
    287   1.2   thorpej 	phdr.p_vaddr = us->start;
    288   1.2   thorpej 	phdr.p_paddr = 0;
    289  1.16      matt 	phdr.p_filesz = realsize;
    290   1.2   thorpej 	phdr.p_memsz = size;
    291   1.2   thorpej 	phdr.p_flags = 0;
    292   1.2   thorpej 	if (us->prot & VM_PROT_READ)
    293   1.2   thorpej 		phdr.p_flags |= PF_R;
    294   1.2   thorpej 	if (us->prot & VM_PROT_WRITE)
    295   1.2   thorpej 		phdr.p_flags |= PF_W;
    296   1.2   thorpej 	if (us->prot & VM_PROT_EXECUTE)
    297   1.2   thorpej 		phdr.p_flags |= PF_X;
    298   1.2   thorpej 	phdr.p_align = PAGE_SIZE;
    299   1.2   thorpej 
    300   1.2   thorpej 	ws->secoff += phdr.p_filesz;
    301  1.16      matt 	*ws->psections++ = phdr;
    302   1.1   thorpej 
    303   1.1   thorpej 	return (0);
    304   1.1   thorpej }
    305   1.1   thorpej 
    306   1.1   thorpej int
    307  1.18      matt ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
    308  1.18      matt     void *iocookie, size_t *sizep)
    309   1.1   thorpej {
    310   1.1   thorpej 	struct netbsd_elfcore_procinfo cpi;
    311   1.1   thorpej 	Elf_Nhdr nhdr;
    312   1.6      matt 	size_t size, notesize;
    313   1.6      matt 	int error;
    314   1.6      matt 	struct lwp *l0;
    315   1.1   thorpej 
    316   1.1   thorpej 	size = 0;
    317   1.1   thorpej 
    318   1.1   thorpej 	/* First, write an elfcore_procinfo. */
    319   1.1   thorpej 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    320   1.1   thorpej 	    elfround(sizeof(cpi));
    321  1.18      matt 	if (iocookie) {
    322   1.1   thorpej 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    323   1.1   thorpej 		cpi.cpi_cpisize = sizeof(cpi);
    324  1.12  christos 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    325  1.12  christos 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    326   1.8   nathanw 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    327   1.1   thorpej 
    328   1.1   thorpej 		memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
    329   1.1   thorpej 		    sizeof(cpi.cpi_sigpend));
    330   1.1   thorpej 		memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
    331   1.1   thorpej 		    sizeof(cpi.cpi_sigmask));
    332   1.1   thorpej 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    333   1.1   thorpej 		    sizeof(cpi.cpi_sigignore));
    334   1.1   thorpej 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    335   1.1   thorpej 		    sizeof(cpi.cpi_sigcatch));
    336   1.1   thorpej 
    337   1.1   thorpej 		cpi.cpi_pid = p->p_pid;
    338   1.1   thorpej 		cpi.cpi_ppid = p->p_pptr->p_pid;
    339   1.1   thorpej 		cpi.cpi_pgrp = p->p_pgid;
    340   1.1   thorpej 		cpi.cpi_sid = p->p_session->s_sid;
    341   1.1   thorpej 
    342   1.1   thorpej 		cpi.cpi_ruid = p->p_cred->p_ruid;
    343   1.1   thorpej 		cpi.cpi_euid = p->p_ucred->cr_uid;
    344   1.1   thorpej 		cpi.cpi_svuid = p->p_cred->p_svuid;
    345   1.1   thorpej 
    346   1.1   thorpej 		cpi.cpi_rgid = p->p_cred->p_rgid;
    347   1.1   thorpej 		cpi.cpi_egid = p->p_ucred->cr_gid;
    348   1.1   thorpej 		cpi.cpi_svgid = p->p_cred->p_svgid;
    349   1.1   thorpej 
    350   1.4   thorpej 		cpi.cpi_nlwps = p->p_nlwps;
    351   1.7    itojun 		strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    352   1.1   thorpej 
    353   1.1   thorpej 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    354   1.1   thorpej 		nhdr.n_descsz = sizeof(cpi);
    355   1.1   thorpej 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    356   1.1   thorpej 
    357  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    358  1.18      matt 		    ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
    359   1.1   thorpej 		if (error)
    360   1.1   thorpej 			return (error);
    361   1.1   thorpej 	}
    362   1.1   thorpej 
    363   1.1   thorpej 	size += notesize;
    364   1.1   thorpej 
    365   1.1   thorpej 	/* XXX Add hook for machdep per-proc notes. */
    366   1.1   thorpej 
    367   1.1   thorpej 	/*
    368   1.6      matt 	 * Now write the register info for the thread that caused the
    369   1.6      matt 	 * coredump.
    370   1.6      matt 	 */
    371  1.18      matt 	error = ELFNAMEEND(coredump_note)(p, l, iocookie, &notesize);
    372   1.6      matt 	if (error)
    373   1.6      matt 		return (error);
    374   1.6      matt 	size += notesize;
    375   1.6      matt 
    376   1.6      matt 	/*
    377   1.1   thorpej 	 * Now, for each LWP, write the register info and any other
    378   1.1   thorpej 	 * per-LWP notes.
    379   1.1   thorpej 	 */
    380   1.6      matt 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    381   1.6      matt 		if (l0 == l)		/* we've taken care of this thread */
    382   1.6      matt 			continue;
    383  1.18      matt 		error = ELFNAMEEND(coredump_note)(p, l0, iocookie, &notesize);
    384   1.6      matt 		if (error)
    385   1.6      matt 			return (error);
    386   1.1   thorpej 		size += notesize;
    387   1.6      matt 	}
    388   1.6      matt 
    389   1.6      matt 	*sizep = size;
    390   1.6      matt 	return (0);
    391   1.6      matt }
    392   1.1   thorpej 
    393   1.6      matt int
    394  1.18      matt ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
    395  1.18      matt     size_t *sizep)
    396   1.6      matt {
    397   1.6      matt 	Elf_Nhdr nhdr;
    398   1.6      matt 	int size, notesize, error;
    399   1.6      matt 	int namesize;
    400  1.18      matt 	char name[64+ELFROUNDSIZE];
    401   1.6      matt 	struct reg intreg;
    402   1.1   thorpej #ifdef PT_GETFPREGS
    403   1.6      matt 	struct fpreg freg;
    404   1.6      matt #endif
    405   1.6      matt 
    406   1.6      matt 	size = 0;
    407   1.6      matt 
    408  1.18      matt 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
    409  1.18      matt 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    410   1.6      matt 	namesize = strlen(name) + 1;
    411  1.18      matt 	memset(name + namesize, 0, elfround(namesize) - namesize);
    412   1.6      matt 
    413   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    414  1.18      matt 	if (iocookie) {
    415   1.6      matt 		PHOLD(l);
    416   1.6      matt 		error = process_read_regs(l, &intreg);
    417   1.6      matt 		PRELE(l);
    418   1.6      matt 		if (error)
    419   1.6      matt 			return (error);
    420   1.6      matt 
    421   1.6      matt 		nhdr.n_namesz = namesize;
    422   1.6      matt 		nhdr.n_descsz = sizeof(intreg);
    423   1.6      matt 		nhdr.n_type = PT_GETREGS;
    424   1.6      matt 
    425  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    426  1.18      matt 		    name, &intreg);
    427   1.6      matt 		if (error)
    428   1.6      matt 			return (error);
    429   1.1   thorpej 
    430   1.4   thorpej 	}
    431   1.6      matt 	size += notesize;
    432   1.6      matt 
    433   1.6      matt #ifdef PT_GETFPREGS
    434   1.6      matt 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    435  1.18      matt 	if (iocookie) {
    436   1.6      matt 		PHOLD(l);
    437   1.6      matt 		error = process_read_fpregs(l, &freg);
    438   1.6      matt 		PRELE(l);
    439   1.6      matt 		if (error)
    440   1.6      matt 			return (error);
    441   1.6      matt 
    442   1.6      matt 		nhdr.n_namesz = namesize;
    443   1.6      matt 		nhdr.n_descsz = sizeof(freg);
    444   1.6      matt 		nhdr.n_type = PT_GETFPREGS;
    445   1.1   thorpej 
    446  1.18      matt 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    447  1.18      matt 		    name, &freg);
    448   1.6      matt 		if (error)
    449   1.6      matt 			return (error);
    450   1.6      matt 	}
    451   1.6      matt 	size += notesize;
    452   1.6      matt #endif
    453   1.1   thorpej 	*sizep = size;
    454   1.6      matt 	/* XXX Add hook for machdep per-LWP notes. */
    455   1.1   thorpej 	return (0);
    456   1.1   thorpej }
    457   1.1   thorpej 
    458   1.1   thorpej int
    459  1.18      matt ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
    460  1.18      matt     const char *name, void *data)
    461   1.1   thorpej {
    462   1.1   thorpej 	int error;
    463   1.1   thorpej 
    464  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
    465   1.1   thorpej 	if (error)
    466  1.18      matt 		return error;
    467   1.1   thorpej 
    468  1.18      matt 	error = coredump_write(cookie, UIO_SYSSPACE, name,
    469  1.18      matt 	    elfround(nhdr->n_namesz));
    470   1.1   thorpej 	if (error)
    471  1.18      matt 		return error;
    472   1.1   thorpej 
    473  1.18      matt 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
    474   1.1   thorpej }
    475