core_elf32.c revision 1.39 1 /* $NetBSD: core_elf32.c,v 1.39 2014/01/03 20:52:47 dsl 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.39 2014/01/03 20:52:47 dsl Exp $");
44
45 #ifdef _KERNEL_OPT
46 #include "opt_coredump.h"
47 #endif
48
49 #ifndef ELFSIZE
50 #define ELFSIZE 32
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/exec.h>
58 #include <sys/exec_elf.h>
59 #include <sys/ptrace.h>
60 #include <sys/kmem.h>
61 #include <sys/kauth.h>
62
63 #include <machine/reg.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #ifdef COREDUMP
68
69 struct writesegs_state {
70 Elf_Phdr *psections;
71 off_t secoff;
72 size_t npsections;
73 };
74
75 /*
76 * We need to know how big the 'notes' are before we write the main header.
77 * To avoid problems with double-processing we save the data.
78 */
79 struct note_buf {
80 struct note_buf *nb_next;
81 unsigned char nb_data[4096 - sizeof (void *)];
82 };
83
84 struct note_state {
85 struct note_buf *ns_first;
86 struct note_buf *ns_last;
87 unsigned int ns_count; /* Of full buffers */
88 unsigned int ns_offset; /* Write point in last buffer */
89 };
90
91 static int ELFNAMEEND(coredump_getseghdrs)(struct proc *,
92 struct uvm_coredump_state *);
93
94 static int ELFNAMEEND(coredump_notes)(struct lwp *, struct note_state *);
95 static int ELFNAMEEND(coredump_note)(struct lwp *, struct note_state *);
96
97 /* The 'note' section names and data are always 4-byte aligned. */
98 #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
99
100 #define elf_process_read_regs CONCAT(process_read_regs, ELFSIZE)
101 #ifdef __HAVE_PROCESS_XFPREGS
102 #define elf_process_read_xfpregs CONCAT(process_read_xfpregs, ELFSIZE)
103 #else
104 #define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
105 #endif
106 #define elf_reg CONCAT(process_reg, ELFSIZE)
107 #define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
108
109 int
110 ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
111 {
112 Elf_Ehdr ehdr;
113 Elf_Phdr *psections;
114 size_t psectionssize;
115 int npsections;
116 struct writesegs_state ws;
117 off_t notestart;
118 size_t notesize;
119 int error, i;
120
121 struct note_state ns;
122 struct note_buf *nb;
123
124 psections = NULL;
125
126 /* Get all of the notes (mostly all the registers). */
127 ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
128 ns.ns_last = ns.ns_first;
129 ns.ns_count = 0;
130 ns.ns_offset = 0;
131 error = ELFNAMEEND(coredump_notes)(l, &ns);
132 ns.ns_last->nb_next = NULL;
133 if (error)
134 goto out;
135 notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
136
137 /*
138 * We have to make a total of 3 passes across the map:
139 *
140 * 1. Count the number of map entries (the number of
141 * PT_LOAD sections in the dump).
142 *
143 * 2. Write the P-section headers.
144 *
145 * 3. Write the P-sections.
146 */
147
148 /* Pass 1: count the entries. */
149 npsections = uvm_coredump_count_segs(l->l_proc);
150 /* Allow for the PT_NOTE section. */
151 npsections++;
152
153 /* Build the main elf header */
154 memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
155 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
156 #if ELFSIZE == 32
157 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
158 #elif ELFSIZE == 64
159 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
160 #endif
161 ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
162 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
163 /* XXX Should be the OSABI/ABI version of the executable. */
164 ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
165 ehdr.e_ident[EI_ABIVERSION] = 0;
166
167 ehdr.e_type = ET_CORE;
168 /* XXX This should be the e_machine of the executable. */
169 ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
170 ehdr.e_version = EV_CURRENT;
171 ehdr.e_entry = 0;
172 ehdr.e_phoff = sizeof(ehdr);
173 ehdr.e_shoff = 0;
174 ehdr.e_flags = 0;
175 ehdr.e_ehsize = sizeof(ehdr);
176 ehdr.e_phentsize = sizeof(Elf_Phdr);
177 ehdr.e_phnum = npsections;
178 ehdr.e_shentsize = 0;
179 ehdr.e_shnum = 0;
180 ehdr.e_shstrndx = 0;
181
182 #ifdef ELF_MD_COREDUMP_SETUP
183 ELF_MD_COREDUMP_SETUP(l, &ehdr);
184 #endif
185
186 /* Write out the ELF header. */
187 error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
188 if (error)
189 goto out;
190
191 psectionssize = npsections * sizeof(*psections);
192 notestart = sizeof(ehdr) + psectionssize;
193
194 psections = kmem_zalloc(psectionssize, KM_SLEEP);
195
196 /* Pass 2: now find the P-section headers. */
197 ws.secoff = notestart + notesize;
198 ws.psections = psections;
199 ws.npsections = npsections - 1;
200 error = uvm_coredump_walkmap(l->l_proc, ELFNAMEEND(coredump_getseghdrs),
201 &ws);
202 if (error)
203 goto out;
204 if (ws.npsections != 0) {
205 /* A section went away */
206 error = ENOMEM;
207 goto out;
208 }
209
210 /* Add the PT_NOTE header after the P-section headers. */
211 ws.psections->p_type = PT_NOTE;
212 ws.psections->p_offset = notestart;
213 ws.psections->p_vaddr = 0;
214 ws.psections->p_paddr = 0;
215 ws.psections->p_filesz = notesize;
216 ws.psections->p_memsz = 0;
217 ws.psections->p_flags = PF_R;
218 ws.psections->p_align = ELFROUNDSIZE;
219
220 /* Write the P-section headers followed by the PT_NOTE header */
221 error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionssize);
222 if (error)
223 goto out;
224
225 #ifdef DIAGNOSTIC
226 if (coredump_offset(cookie) != notestart)
227 panic("coredump: offset %lld != notestart %lld",
228 (long long) coredump_offset(cookie),
229 (long long) notestart);
230 #endif
231
232 /* Write out the notes. */
233 for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
234 error = coredump_write(cookie, UIO_SYSSPACE, nb->nb_data,
235 nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data);
236 if (error)
237 goto out;
238 }
239
240 /* Finally, write the sections themselves. */
241 for (i = 0; i < npsections - 1; i++) {
242 if (psections[i].p_filesz == 0)
243 continue;
244
245 #ifdef DIAGNOSTIC
246 if (coredump_offset(cookie) != psections[i].p_offset)
247 panic("coredump: offset %lld != p_offset[%d] %lld",
248 (long long) coredump_offset(cookie), i,
249 (long long) psections[i].p_filesz);
250 #endif
251
252 error = coredump_write(cookie, UIO_USERSPACE,
253 (void *)(vaddr_t)psections[i].p_vaddr,
254 psections[i].p_filesz);
255 if (error)
256 goto out;
257 }
258
259 out:
260 if (psections)
261 kmem_free(psections, psectionssize);
262 for (; (nb = ns.ns_first) != NULL; ns.ns_first = nb->nb_next)
263 kmem_free(nb, sizeof *nb);
264 return (error);
265 }
266
267 static int
268 ELFNAMEEND(coredump_getseghdrs)(struct proc *p, struct uvm_coredump_state *us)
269 {
270 struct writesegs_state *ws = us->cookie;
271 Elf_Phdr phdr;
272 vsize_t size, realsize;
273 vaddr_t end;
274 int error;
275
276 /* Don't overrun if there are more sections */
277 if (ws->npsections == 0)
278 return ENOMEM;
279 ws->npsections--;
280
281 size = us->end - us->start;
282 realsize = us->realend - us->start;
283 end = us->realend;
284
285 /* Don't bother writing out trailing zeros */
286 while (realsize > 0) {
287 long buf[1024 / sizeof(long)];
288 size_t slen = realsize > sizeof(buf) ? sizeof(buf) : realsize;
289 const long *ep;
290 int i;
291
292 end -= slen;
293 if ((error = copyin_proc(p, (void *)end, buf, slen)) != 0)
294 return error;
295
296 ep = (const long *) &buf[slen / sizeof(buf[0])];
297 for (i = 0, ep--; buf <= ep; ep--, i++) {
298 if (*ep)
299 break;
300 }
301 realsize -= i * sizeof(buf[0]);
302 if (i * sizeof(buf[0]) < slen)
303 break;
304 }
305
306 phdr.p_type = PT_LOAD;
307 phdr.p_offset = ws->secoff;
308 phdr.p_vaddr = us->start;
309 phdr.p_paddr = 0;
310 phdr.p_filesz = realsize;
311 phdr.p_memsz = size;
312 phdr.p_flags = 0;
313 if (us->prot & VM_PROT_READ)
314 phdr.p_flags |= PF_R;
315 if (us->prot & VM_PROT_WRITE)
316 phdr.p_flags |= PF_W;
317 if (us->prot & VM_PROT_EXECUTE)
318 phdr.p_flags |= PF_X;
319 phdr.p_align = PAGE_SIZE;
320
321 ws->secoff += phdr.p_filesz;
322 *ws->psections++ = phdr;
323
324 return (0);
325 }
326
327 static int
328 ELFNAMEEND(coredump_notes)(struct lwp *l, struct note_state *ns)
329 {
330 struct proc *p;
331 struct netbsd_elfcore_procinfo cpi;
332 Elf_Nhdr nhdr;
333 int error;
334 struct lwp *l0;
335 sigset_t ss1, ss2;
336
337 p = l->l_proc;
338
339 /* First, write an elfcore_procinfo. */
340 cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
341 cpi.cpi_cpisize = sizeof(cpi);
342 cpi.cpi_signo = p->p_sigctx.ps_signo;
343 cpi.cpi_sigcode = p->p_sigctx.ps_code;
344 cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
345
346 /*
347 * XXX This should be per-LWP.
348 */
349 ss1 = p->p_sigpend.sp_set;
350 sigemptyset(&ss2);
351 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
352 sigplusset(&l0->l_sigpend.sp_set, &ss1);
353 sigplusset(&l0->l_sigmask, &ss2);
354 }
355 memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
356 memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
357 memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
358 sizeof(cpi.cpi_sigignore));
359 memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
360 sizeof(cpi.cpi_sigcatch));
361
362 cpi.cpi_pid = p->p_pid;
363 mutex_enter(proc_lock);
364 cpi.cpi_ppid = p->p_pptr->p_pid;
365 cpi.cpi_pgrp = p->p_pgid;
366 cpi.cpi_sid = p->p_session->s_sid;
367 mutex_exit(proc_lock);
368
369 cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
370 cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
371 cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
372
373 cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
374 cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
375 cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
376
377 cpi.cpi_nlwps = p->p_nlwps;
378 (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
379 cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
380
381 nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
382 nhdr.n_descsz = sizeof(cpi);
383 nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
384
385 ELFNAMEEND(coredump_savenote)(ns, &nhdr, ELF_NOTE_NETBSD_CORE_NAME,
386 &cpi);
387
388 /* XXX Add hook for machdep per-proc notes. */
389
390 /*
391 * Now write the register info for the thread that caused the
392 * coredump.
393 */
394 error = ELFNAMEEND(coredump_note)(l, ns);
395 if (error)
396 return (error);
397
398 /*
399 * Now, for each LWP, write the register info and any other
400 * per-LWP notes. Since we're dumping core, we don't bother
401 * locking.
402 */
403 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
404 if (l0 == l) /* we've taken care of this thread */
405 continue;
406 error = ELFNAMEEND(coredump_note)(l0, ns);
407 if (error)
408 return (error);
409 }
410
411 return (0);
412 }
413
414 static int
415 ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
416 {
417 Elf_Nhdr nhdr;
418 int error;
419 int namesize;
420 char name[64];
421 elf_reg intreg;
422 #ifdef PT_GETFPREGS
423 elf_fpreg freg;
424 size_t freglen;
425 #endif
426
427 snprintf(name, sizeof(name), "%s@%d",
428 ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
429 namesize = strlen(name) + 1;
430
431 error = elf_process_read_regs(l, &intreg);
432 if (error)
433 return (error);
434
435 nhdr.n_namesz = namesize;
436 nhdr.n_descsz = sizeof(intreg);
437 nhdr.n_type = PT_GETREGS;
438
439 ELFNAMEEND(coredump_savenote)(ns, &nhdr, name, &intreg);
440
441 #ifdef PT_GETFPREGS
442 freglen = sizeof(freg);
443 #ifdef __HAVE_PROCESS_XFPREGS
444 error = elf_process_read_xfpregs(l, &freg, &freglen);
445 #else
446 error = elf_process_read_fpregs(l, &freg);
447 #endif
448 if (error)
449 return (error);
450
451 nhdr.n_namesz = namesize;
452 nhdr.n_descsz = freglen;
453 nhdr.n_type = PT_GETFPREGS;
454
455 ELFNAMEEND(coredump_savenote)(ns, &nhdr, name, &freg);
456 #endif
457 /* XXX Add hook for machdep per-LWP notes. */
458 return (0);
459 }
460
461 static void
462 save_note_bytes(struct note_state *ns, const void *data, size_t len)
463 {
464 struct note_buf *nb = ns->ns_last;
465 size_t copylen;
466 unsigned char *wp;
467
468 /*
469 * Just copy the data into a buffer list.
470 * All but the last buffer is full.
471 */
472 for (;;) {
473 copylen = min(len, sizeof nb->nb_data - ns->ns_offset);
474 wp = nb->nb_data + ns->ns_offset;
475 memcpy(wp, data, copylen);
476 if (copylen == len)
477 break;
478 nb->nb_next = kmem_alloc(sizeof *nb->nb_next, KM_SLEEP);
479 nb = nb->nb_next;
480 ns->ns_last = nb;
481 ns->ns_count++;
482 ns->ns_offset = 0;
483 len -= copylen;
484 data = (const unsigned char *)data + copylen;
485 }
486
487 while (copylen & (ELFROUNDSIZE - 1))
488 wp[copylen++] = 0;
489
490 ns->ns_offset += copylen;
491 }
492
493 void
494 ELFNAMEEND(coredump_savenote)(struct note_state *ns, Elf_Nhdr *nhdr,
495 const char *name, void *data)
496 {
497 save_note_bytes(ns, nhdr, sizeof (*nhdr));
498 save_note_bytes(ns, name, nhdr->n_namesz);
499 save_note_bytes(ns, data, nhdr->n_descsz);
500 }
501
502 #else /* COREDUMP */
503
504 int
505 ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
506 {
507
508 return ENOSYS;
509 }
510
511 #endif /* COREDUMP */
512