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