Home | History | Annotate | Line # | Download | only in kern
core_elf32.c revision 1.21
      1 /*	$NetBSD: core_elf32.c,v 1.21 2005/07/06 22:30:42 christos 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.21 2005/07/06 22:30:42 christos 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/proc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/exec.h>
     55 #include <sys/exec_elf.h>
     56 #include <sys/ptrace.h>
     57 #include <sys/malloc.h>
     58 
     59 #include <machine/reg.h>
     60 
     61 #include <uvm/uvm_extern.h>
     62 
     63 struct countsegs_state {
     64 	int	npsections;
     65 };
     66 
     67 int	ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
     68 	    struct uvm_coredump_state *);
     69 
     70 struct writesegs_state {
     71 	Elf_Phdr *psections;
     72 	off_t	secoff;
     73 };
     74 
     75 int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
     76 	    struct uvm_coredump_state *);
     77 int	ELFNAMEEND(coredump_writesegs)(struct proc *, void *,
     78 	    struct uvm_coredump_state *);
     79 
     80 int	ELFNAMEEND(coredump_notes)(struct proc *, struct lwp *, void *,
     81 	    size_t *);
     82 int	ELFNAMEEND(coredump_note)(struct proc *, struct lwp *, void *,
     83 	    size_t *);
     84 
     85 #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
     86 #define	elfround(x)	roundup((x), ELFROUNDSIZE)
     87 
     88 int
     89 ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
     90 {
     91 	struct proc *p;
     92 	Elf_Ehdr ehdr;
     93 	Elf_Phdr phdr, *psections;
     94 	struct countsegs_state cs;
     95 	struct writesegs_state ws;
     96 	off_t notestart, secstart, offset;
     97 	size_t notesize;
     98 	int error, i;
     99 
    100 	psections = NULL;
    101 	p = l->l_proc;
    102 	/*
    103 	 * We have to make a total of 3 passes across the map:
    104 	 *
    105 	 *	1. Count the number of map entries (the number of
    106 	 *	   PT_LOAD sections).
    107 	 *
    108 	 *	2. Write the P-section headers.
    109 	 *
    110 	 *	3. Write the P-sections.
    111 	 */
    112 
    113 	/* Pass 1: count the entries. */
    114 	cs.npsections = 0;
    115 	error = uvm_coredump_walkmap(p, NULL,
    116 	    ELFNAMEEND(coredump_countsegs), &cs);
    117 	if (error)
    118 		goto out;
    119 
    120 	/* Count the PT_NOTE section. */
    121 	cs.npsections++;
    122 
    123 	/* Get the size of the notes. */
    124 	error = ELFNAMEEND(coredump_notes)(p, l, NULL, &notesize);
    125 	if (error)
    126 		goto out;
    127 
    128 	memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
    129 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
    130 #if ELFSIZE == 32
    131 	ehdr.e_ident[EI_CLASS] = ELFCLASS32;
    132 #elif ELFSIZE == 64
    133 	ehdr.e_ident[EI_CLASS] = ELFCLASS64;
    134 #endif
    135 	ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
    136 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
    137 	/* XXX Should be the OSABI/ABI version of the executable. */
    138 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
    139 	ehdr.e_ident[EI_ABIVERSION] = 0;
    140 
    141 	ehdr.e_type = ET_CORE;
    142 	/* XXX This should be the e_machine of the executable. */
    143 	ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
    144 	ehdr.e_version = EV_CURRENT;
    145 	ehdr.e_entry = 0;
    146 	ehdr.e_phoff = sizeof(ehdr);
    147 	ehdr.e_shoff = 0;
    148 	ehdr.e_flags = 0;
    149 	ehdr.e_ehsize = sizeof(ehdr);
    150 	ehdr.e_phentsize = sizeof(Elf_Phdr);
    151 	ehdr.e_phnum = cs.npsections;
    152 	ehdr.e_shentsize = 0;
    153 	ehdr.e_shnum = 0;
    154 	ehdr.e_shstrndx = 0;
    155 
    156 	/* Write out the ELF header. */
    157 	error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
    158 	if (error)
    159 		goto out;
    160 
    161 	offset = sizeof(ehdr);
    162 
    163 	notestart = offset + sizeof(phdr) * cs.npsections;
    164 	secstart = notestart + notesize;
    165 
    166 	psections = malloc(cs.npsections * sizeof(Elf_Phdr),
    167 	    M_TEMP, M_WAITOK|M_ZERO);
    168 
    169 	/* Pass 2: now write the P-section headers. */
    170 	ws.secoff = secstart;
    171 	ws.psections = psections;
    172 	error = uvm_coredump_walkmap(p, cookie,
    173 	    ELFNAMEEND(coredump_writeseghdrs), &ws);
    174 	if (error)
    175 		goto out;
    176 
    177 	/* Write out the PT_NOTE header. */
    178 	ws.psections->p_type = PT_NOTE;
    179 	ws.psections->p_offset = notestart;
    180 	ws.psections->p_vaddr = 0;
    181 	ws.psections->p_paddr = 0;
    182 	ws.psections->p_filesz = notesize;
    183 	ws.psections->p_memsz = 0;
    184 	ws.psections->p_flags = PF_R;
    185 	ws.psections->p_align = ELFROUNDSIZE;
    186 
    187 	error = coredump_write(cookie, UIO_SYSSPACE, psections,
    188 	    cs.npsections * sizeof(Elf_Phdr));
    189 	if (error)
    190 		goto out;
    191 
    192 #ifdef DIAGNOSTIC
    193 	offset += cs.npsections * sizeof(Elf_Phdr);
    194 	if (offset != notestart)
    195 		panic("coredump: offset %lld != notestart %lld",
    196 		    (long long) offset, (long long) notestart);
    197 #endif
    198 
    199 	/* Write out the notes. */
    200 	error = ELFNAMEEND(coredump_notes)(p, l, cookie, &notesize);
    201 	if (error)
    202 		goto out;
    203 
    204 #ifdef DIAGNOSTIC
    205 	offset += notesize;
    206 	if (offset != secstart)
    207 		panic("coredump: offset %lld != secstart %lld",
    208 		    (long long) offset, (long long) secstart);
    209 #endif
    210 
    211 	/* Pass 3: finally, write the sections themselves. */
    212 	for (i = 0; i < cs.npsections - 1; i++) {
    213 		if (psections[i].p_filesz == 0)
    214 			continue;
    215 
    216 #ifdef DIAGNOSTIC
    217 		if (offset != psections[i].p_offset)
    218 			panic("coredump: offset %lld != p_offset[%d] %lld",
    219 			    (long long) offset, i,
    220 			    (long long) psections[i].p_filesz);
    221 #endif
    222 
    223 		error = coredump_write(cookie, UIO_USERSPACE,
    224 		    (void *)(vaddr_t)psections[i].p_vaddr,
    225 		    psections[i].p_filesz);
    226 		if (error)
    227 			goto out;
    228 
    229 #ifdef DIAGNOSTIC
    230 		offset += psections[i].p_filesz;
    231 #endif
    232 	}
    233 
    234   out:
    235 	if (psections)
    236 		free(psections, M_TEMP);
    237 	return (error);
    238 }
    239 
    240 int
    241 ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
    242     struct uvm_coredump_state *us)
    243 {
    244 	struct countsegs_state *cs = us->cookie;
    245 
    246 	cs->npsections++;
    247 	return (0);
    248 }
    249 
    250 int
    251 ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
    252     struct uvm_coredump_state *us)
    253 {
    254 	struct writesegs_state *ws = us->cookie;
    255 	Elf_Phdr phdr;
    256 	vsize_t size, realsize;
    257 	vaddr_t end;
    258 	int error;
    259 
    260 	size = us->end - us->start;
    261 	realsize = us->realend - us->start;
    262 	end = us->realend;
    263 
    264 	while (realsize > 0) {
    265 		long buf[1024 / sizeof(long)];
    266 		size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
    267 		const long *ep;
    268 		int i;
    269 
    270 		end -= slen;
    271 		if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
    272 			return error;
    273 
    274 		ep = (const long *) &buf[slen / sizeof(buf[0])];
    275 		for (i = 0, ep--; buf <= ep; ep--, i++) {
    276 			if (*ep)
    277 				break;
    278 		}
    279 		realsize -= i * sizeof(buf[0]);
    280 		if (i * sizeof(buf[0]) < slen)
    281 			break;
    282 	}
    283 
    284 	phdr.p_type = PT_LOAD;
    285 	phdr.p_offset = ws->secoff;
    286 	phdr.p_vaddr = us->start;
    287 	phdr.p_paddr = 0;
    288 	phdr.p_filesz = realsize;
    289 	phdr.p_memsz = size;
    290 	phdr.p_flags = 0;
    291 	if (us->prot & VM_PROT_READ)
    292 		phdr.p_flags |= PF_R;
    293 	if (us->prot & VM_PROT_WRITE)
    294 		phdr.p_flags |= PF_W;
    295 	if (us->prot & VM_PROT_EXECUTE)
    296 		phdr.p_flags |= PF_X;
    297 	phdr.p_align = PAGE_SIZE;
    298 
    299 	ws->secoff += phdr.p_filesz;
    300 	*ws->psections++ = phdr;
    301 
    302 	return (0);
    303 }
    304 
    305 int
    306 ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l,
    307     void *iocookie, size_t *sizep)
    308 {
    309 	struct netbsd_elfcore_procinfo cpi;
    310 	Elf_Nhdr nhdr;
    311 	size_t size, notesize;
    312 	int error;
    313 	struct lwp *l0;
    314 
    315 	size = 0;
    316 
    317 	/* First, write an elfcore_procinfo. */
    318 	notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
    319 	    elfround(sizeof(cpi));
    320 	if (iocookie) {
    321 		cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
    322 		cpi.cpi_cpisize = sizeof(cpi);
    323 		cpi.cpi_signo = p->p_sigctx.ps_signo;
    324 		cpi.cpi_sigcode = p->p_sigctx.ps_code;
    325 		cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
    326 
    327 		memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
    328 		    sizeof(cpi.cpi_sigpend));
    329 		memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
    330 		    sizeof(cpi.cpi_sigmask));
    331 		memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
    332 		    sizeof(cpi.cpi_sigignore));
    333 		memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
    334 		    sizeof(cpi.cpi_sigcatch));
    335 
    336 		cpi.cpi_pid = p->p_pid;
    337 		cpi.cpi_ppid = p->p_pptr->p_pid;
    338 		cpi.cpi_pgrp = p->p_pgid;
    339 		cpi.cpi_sid = p->p_session->s_sid;
    340 
    341 		cpi.cpi_ruid = p->p_cred->p_ruid;
    342 		cpi.cpi_euid = p->p_ucred->cr_uid;
    343 		cpi.cpi_svuid = p->p_cred->p_svuid;
    344 
    345 		cpi.cpi_rgid = p->p_cred->p_rgid;
    346 		cpi.cpi_egid = p->p_ucred->cr_gid;
    347 		cpi.cpi_svgid = p->p_cred->p_svgid;
    348 
    349 		cpi.cpi_nlwps = p->p_nlwps;
    350 		strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
    351 
    352 		nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
    353 		nhdr.n_descsz = sizeof(cpi);
    354 		nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
    355 
    356 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    357 		    ELF_NOTE_NETBSD_CORE_NAME "\0\0\0", &cpi);
    358 		if (error)
    359 			return (error);
    360 	}
    361 
    362 	size += notesize;
    363 
    364 	/* XXX Add hook for machdep per-proc notes. */
    365 
    366 	/*
    367 	 * Now write the register info for the thread that caused the
    368 	 * coredump.
    369 	 */
    370 	error = ELFNAMEEND(coredump_note)(p, l, iocookie, &notesize);
    371 	if (error)
    372 		return (error);
    373 	size += notesize;
    374 
    375 	/*
    376 	 * Now, for each LWP, write the register info and any other
    377 	 * per-LWP notes.
    378 	 */
    379 	LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
    380 		if (l0 == l)		/* we've taken care of this thread */
    381 			continue;
    382 		error = ELFNAMEEND(coredump_note)(p, l0, iocookie, &notesize);
    383 		if (error)
    384 			return (error);
    385 		size += notesize;
    386 	}
    387 
    388 	*sizep = size;
    389 	return (0);
    390 }
    391 
    392 int
    393 ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, void *iocookie,
    394     size_t *sizep)
    395 {
    396 	Elf_Nhdr nhdr;
    397 	int size, notesize, error;
    398 	int namesize;
    399 	char name[64+ELFROUNDSIZE];
    400 	struct reg intreg;
    401 #ifdef PT_GETFPREGS
    402 	struct fpreg freg;
    403 #endif
    404 
    405 	size = 0;
    406 
    407 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
    408 	    ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
    409 	namesize = strlen(name) + 1;
    410 	memset(name + namesize, 0, elfround(namesize) - namesize);
    411 
    412 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
    413 	if (iocookie) {
    414 		PHOLD(l);
    415 		error = process_read_regs(l, &intreg);
    416 		PRELE(l);
    417 		if (error)
    418 			return (error);
    419 
    420 		nhdr.n_namesz = namesize;
    421 		nhdr.n_descsz = sizeof(intreg);
    422 		nhdr.n_type = PT_GETREGS;
    423 
    424 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    425 		    name, &intreg);
    426 		if (error)
    427 			return (error);
    428 
    429 	}
    430 	size += notesize;
    431 
    432 #ifdef PT_GETFPREGS
    433 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
    434 	if (iocookie) {
    435 		PHOLD(l);
    436 		error = process_read_fpregs(l, &freg);
    437 		PRELE(l);
    438 		if (error)
    439 			return (error);
    440 
    441 		nhdr.n_namesz = namesize;
    442 		nhdr.n_descsz = sizeof(freg);
    443 		nhdr.n_type = PT_GETFPREGS;
    444 
    445 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
    446 		    name, &freg);
    447 		if (error)
    448 			return (error);
    449 	}
    450 	size += notesize;
    451 #endif
    452 	*sizep = size;
    453 	/* XXX Add hook for machdep per-LWP notes. */
    454 	return (0);
    455 }
    456 
    457 int
    458 ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Nhdr *nhdr,
    459     const char *name, void *data)
    460 {
    461 	int error;
    462 
    463 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
    464 	if (error)
    465 		return error;
    466 
    467 	error = coredump_write(cookie, UIO_SYSSPACE, name,
    468 	    elfround(nhdr->n_namesz));
    469 	if (error)
    470 		return error;
    471 
    472 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->n_descsz);
    473 }
    474