core_elf32.c revision 1.17 1 /* $NetBSD: core_elf32.c,v 1.17 2005/06/03 13:30:10 he 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.17 2005/06/03 13:30:10 he 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)((intptr_t) psections[i].p_vaddr),
230 psections[i].p_filesz,
231 psections[i].p_offset, UIO_USERSPACE,
232 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
233 if (error)
234 goto out;
235
236 #ifdef DIAGNOSTIC
237 offset += psections[i].p_filesz;
238 #endif
239 }
240
241 out:
242 if (psections)
243 free(psections, M_TEMP);
244 return (error);
245 }
246
247 int
248 ELFNAMEEND(coredump_countsegs)(struct proc *p, struct vnode *vp,
249 struct ucred *cred, struct uvm_coredump_state *us)
250 {
251 struct countsegs_state *cs = us->cookie;
252
253 cs->npsections++;
254 return (0);
255 }
256
257 int
258 ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, struct vnode *vp,
259 struct ucred *cred, struct uvm_coredump_state *us)
260 {
261 struct writesegs_state *ws = us->cookie;
262 Elf_Phdr phdr;
263 vsize_t size, realsize;
264 vaddr_t end;
265 int error;
266
267 size = us->end - us->start;
268 realsize = us->realend - us->start;
269 end = us->realend;
270
271 while (realsize > 0) {
272 long buf[1024 / sizeof(long)];
273 size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
274 const long *ep;
275 int i;
276
277 end -= slen;
278 error = copyin((void *) end, buf, slen);
279 if (error)
280 return (error);
281
282 ep = (const long *) &buf[slen / sizeof(buf[0])];
283 for (i = 0, ep--; buf <= ep; ep--, i++) {
284 if (*ep)
285 break;
286 }
287 realsize -= i * sizeof(buf[0]);
288 if (i * sizeof(buf[0]) < slen)
289 break;
290 }
291
292 phdr.p_type = PT_LOAD;
293 phdr.p_offset = ws->secoff;
294 phdr.p_vaddr = us->start;
295 phdr.p_paddr = 0;
296 phdr.p_filesz = realsize;
297 phdr.p_memsz = size;
298 phdr.p_flags = 0;
299 if (us->prot & VM_PROT_READ)
300 phdr.p_flags |= PF_R;
301 if (us->prot & VM_PROT_WRITE)
302 phdr.p_flags |= PF_W;
303 if (us->prot & VM_PROT_EXECUTE)
304 phdr.p_flags |= PF_X;
305 phdr.p_align = PAGE_SIZE;
306
307 ws->secoff += phdr.p_filesz;
308 *ws->psections++ = phdr;
309
310 return (0);
311 }
312
313 int
314 ELFNAMEEND(coredump_notes)(struct proc *p, struct lwp *l, struct vnode *vp,
315 struct ucred *cred, size_t *sizep, off_t offset)
316 {
317 struct netbsd_elfcore_procinfo cpi;
318 Elf_Nhdr nhdr;
319 size_t size, notesize;
320 int error;
321 struct lwp *l0;
322
323 size = 0;
324
325 /* First, write an elfcore_procinfo. */
326 notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
327 elfround(sizeof(cpi));
328 if (offset) {
329 cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
330 cpi.cpi_cpisize = sizeof(cpi);
331 cpi.cpi_signo = p->p_sigctx.ps_signo;
332 cpi.cpi_sigcode = p->p_sigctx.ps_code;
333 cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
334
335 memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
336 sizeof(cpi.cpi_sigpend));
337 memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
338 sizeof(cpi.cpi_sigmask));
339 memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
340 sizeof(cpi.cpi_sigignore));
341 memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
342 sizeof(cpi.cpi_sigcatch));
343
344 cpi.cpi_pid = p->p_pid;
345 cpi.cpi_ppid = p->p_pptr->p_pid;
346 cpi.cpi_pgrp = p->p_pgid;
347 cpi.cpi_sid = p->p_session->s_sid;
348
349 cpi.cpi_ruid = p->p_cred->p_ruid;
350 cpi.cpi_euid = p->p_ucred->cr_uid;
351 cpi.cpi_svuid = p->p_cred->p_svuid;
352
353 cpi.cpi_rgid = p->p_cred->p_rgid;
354 cpi.cpi_egid = p->p_ucred->cr_gid;
355 cpi.cpi_svgid = p->p_cred->p_svgid;
356
357 cpi.cpi_nlwps = p->p_nlwps;
358 strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
359
360 nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
361 nhdr.n_descsz = sizeof(cpi);
362 nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
363
364 error = ELFNAMEEND(coredump_writenote)(p, vp, cred, offset,
365 &nhdr, ELF_NOTE_NETBSD_CORE_NAME, &cpi);
366 if (error)
367 return (error);
368
369 offset += notesize;
370 }
371
372 size += notesize;
373
374 /* XXX Add hook for machdep per-proc notes. */
375
376 /*
377 * Now write the register info for the thread that caused the
378 * coredump.
379 */
380 error = ELFNAMEEND(coredump_note)(p, l, vp, cred, ¬esize, offset);
381 if (error)
382 return (error);
383 if (offset)
384 offset += notesize;
385 size += notesize;
386
387 /*
388 * Now, for each LWP, write the register info and any other
389 * per-LWP notes.
390 */
391 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
392 if (l0 == l) /* we've taken care of this thread */
393 continue;
394 error = ELFNAMEEND(coredump_note)(p, l0, vp, cred,
395 ¬esize, offset);
396 if (error)
397 return (error);
398 if (offset)
399 offset += notesize;
400 size += notesize;
401 }
402
403 *sizep = size;
404 return (0);
405 }
406
407 int
408 ELFNAMEEND(coredump_note)(struct proc *p, struct lwp *l, struct vnode *vp,
409 struct ucred *cred, size_t *sizep, off_t offset)
410 {
411 Elf_Nhdr nhdr;
412 int size, notesize, error;
413 int namesize;
414 char name[64];
415 struct reg intreg;
416 #ifdef PT_GETFPREGS
417 struct fpreg freg;
418 #endif
419
420 size = 0;
421
422 snprintf(name, sizeof(name), "%s@%d", ELF_NOTE_NETBSD_CORE_NAME,
423 l->l_lid);
424 namesize = strlen(name) + 1;
425
426 notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
427 if (offset) {
428 PHOLD(l);
429 error = process_read_regs(l, &intreg);
430 PRELE(l);
431 if (error)
432 return (error);
433
434 nhdr.n_namesz = namesize;
435 nhdr.n_descsz = sizeof(intreg);
436 nhdr.n_type = PT_GETREGS;
437
438 error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
439 offset, &nhdr, name, &intreg);
440 if (error)
441 return (error);
442
443 offset += notesize;
444 }
445 size += notesize;
446
447 #ifdef PT_GETFPREGS
448 notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
449 if (offset) {
450 PHOLD(l);
451 error = process_read_fpregs(l, &freg);
452 PRELE(l);
453 if (error)
454 return (error);
455
456 nhdr.n_namesz = namesize;
457 nhdr.n_descsz = sizeof(freg);
458 nhdr.n_type = PT_GETFPREGS;
459
460 error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
461 offset, &nhdr, name, &freg);
462 if (error)
463 return (error);
464
465 offset += notesize;
466 }
467 size += notesize;
468 #endif
469 *sizep = size;
470 /* XXX Add hook for machdep per-LWP notes. */
471 return (0);
472 }
473
474 int
475 ELFNAMEEND(coredump_writenote)(struct proc *p, struct vnode *vp,
476 struct ucred *cred, off_t offset, Elf_Nhdr *nhdr, const char *name,
477 void *data)
478 {
479 int error;
480
481 error = vn_rdwr(UIO_WRITE, vp,
482 (caddr_t) nhdr, sizeof(*nhdr),
483 offset, UIO_SYSSPACE,
484 IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
485 if (error)
486 return (error);
487
488 offset += sizeof(*nhdr);
489
490 error = vn_rdwr(UIO_WRITE, vp,
491 /*XXXUNCONST*/
492 __UNCONST(name), nhdr->n_namesz,
493 offset, UIO_SYSSPACE,
494 IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
495 if (error)
496 return (error);
497
498 offset += elfround(nhdr->n_namesz);
499
500 error = vn_rdwr(UIO_WRITE, vp,
501 data, nhdr->n_descsz,
502 offset, UIO_SYSSPACE,
503 IO_NODELOCKED|IO_UNIT, cred, NULL, NULL);
504
505 return (error);
506 }
507