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