core_elf32.c revision 1.43 1 /* $NetBSD: core_elf32.c,v 1.43 2014/01/05 00:53:53 mrg 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.43 2014/01/05 00:53:53 mrg 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 proc_t *p;
72 off_t secoff;
73 size_t npsections;
74 };
75
76 /*
77 * We need to know how big the 'notes' are before we write the main header.
78 * To avoid problems with double-processing we save the data.
79 */
80 struct note_buf {
81 struct note_buf *nb_next;
82 unsigned char nb_data[4096 - sizeof (void *)];
83 };
84
85 struct note_state {
86 struct note_buf *ns_first;
87 struct note_buf *ns_last;
88 unsigned int ns_count; /* Of full buffers */
89 unsigned int ns_offset; /* Write point in last buffer */
90 };
91
92 static int ELFNAMEEND(coredump_getseghdrs)(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 #define elf_process_read_fpregs CONCAT(process_read_fpregs, ELFSIZE)
102 #define elf_reg CONCAT(process_reg, ELFSIZE)
103 #define elf_fpreg CONCAT(process_fpreg, ELFSIZE)
104
105 int
106 ELFNAMEEND(coredump)(struct lwp *l, struct coredump_iostate *cookie)
107 {
108 Elf_Ehdr ehdr;
109 Elf_Phdr *psections;
110 size_t psectionssize;
111 int npsections;
112 struct writesegs_state ws;
113 off_t notestart;
114 size_t notesize;
115 int error, i;
116
117 struct note_state ns;
118 struct note_buf *nb;
119 struct note_buf *nb_next;
120
121 psections = NULL;
122
123 /* Get all of the notes (mostly all the registers). */
124 ns.ns_first = kmem_alloc(sizeof *ns.ns_first, KM_SLEEP);
125 ns.ns_last = ns.ns_first;
126 ns.ns_count = 0;
127 ns.ns_offset = 0;
128 error = ELFNAMEEND(coredump_notes)(l, &ns);
129 ns.ns_last->nb_next = NULL;
130 if (error)
131 goto out;
132 notesize = ns.ns_count * sizeof nb->nb_data + ns.ns_offset;
133
134 /*
135 * We have to make a total of 3 passes across the map:
136 *
137 * 1. Count the number of map entries (the number of
138 * PT_LOAD sections in the dump).
139 *
140 * 2. Write the P-section headers.
141 *
142 * 3. Write the P-sections.
143 */
144
145 /* Pass 1: count the entries. */
146 npsections = uvm_coredump_count_segs(l->l_proc);
147 /* Allow for the PT_NOTE section. */
148 npsections++;
149
150 /* Build the main elf header */
151 memset(&ehdr.e_ident[EI_PAD], 0, sizeof(ehdr.e_ident) - EI_PAD);
152 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
153 #if ELFSIZE == 32
154 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
155 #elif ELFSIZE == 64
156 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
157 #endif
158 ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
159 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
160 /* XXX Should be the OSABI/ABI version of the executable. */
161 ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
162 ehdr.e_ident[EI_ABIVERSION] = 0;
163
164 ehdr.e_type = ET_CORE;
165 /* XXX This should be the e_machine of the executable. */
166 ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
167 ehdr.e_version = EV_CURRENT;
168 ehdr.e_entry = 0;
169 ehdr.e_phoff = sizeof(ehdr);
170 ehdr.e_shoff = 0;
171 ehdr.e_flags = 0;
172 ehdr.e_ehsize = sizeof(ehdr);
173 ehdr.e_phentsize = sizeof(Elf_Phdr);
174 ehdr.e_phnum = npsections;
175 ehdr.e_shentsize = 0;
176 ehdr.e_shnum = 0;
177 ehdr.e_shstrndx = 0;
178
179 #ifdef ELF_MD_COREDUMP_SETUP
180 ELF_MD_COREDUMP_SETUP(l, &ehdr);
181 #endif
182
183 /* Write out the ELF header. */
184 error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
185 if (error)
186 goto out;
187
188 psectionssize = npsections * sizeof(*psections);
189 notestart = sizeof(ehdr) + psectionssize;
190
191 psections = kmem_zalloc(psectionssize, KM_SLEEP);
192
193 /* Pass 2: now find the P-section headers. */
194 ws.secoff = notestart + notesize;
195 ws.psections = psections;
196 ws.npsections = npsections - 1;
197 ws.p = l->l_proc;
198 error = uvm_coredump_walkmap(l->l_proc, ELFNAMEEND(coredump_getseghdrs),
199 &ws);
200 if (error)
201 goto out;
202 if (ws.npsections != 0) {
203 /* A section went away */
204 error = ENOMEM;
205 goto out;
206 }
207
208 /* Add the PT_NOTE header after the P-section headers. */
209 ws.psections->p_type = PT_NOTE;
210 ws.psections->p_offset = notestart;
211 ws.psections->p_vaddr = 0;
212 ws.psections->p_paddr = 0;
213 ws.psections->p_filesz = notesize;
214 ws.psections->p_memsz = 0;
215 ws.psections->p_flags = PF_R;
216 ws.psections->p_align = ELFROUNDSIZE;
217
218 /* Write the P-section headers followed by the PT_NOTE header */
219 error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionssize);
220 if (error)
221 goto out;
222
223 #ifdef DIAGNOSTIC
224 if (coredump_offset(cookie) != notestart)
225 panic("coredump: offset %lld != notestart %lld",
226 (long long) coredump_offset(cookie),
227 (long long) notestart);
228 #endif
229
230 /* Write out the notes. */
231 for (nb = ns.ns_first; nb != NULL; nb = nb->nb_next) {
232 error = coredump_write(cookie, UIO_SYSSPACE, nb->nb_data,
233 nb->nb_next == NULL ? ns.ns_offset : sizeof nb->nb_data);
234 if (error)
235 goto out;
236 }
237
238 /* Finally, write the sections themselves. */
239 for (i = 0; i < npsections - 1; i++) {
240 if (psections[i].p_filesz == 0)
241 continue;
242
243 #ifdef DIAGNOSTIC
244 if (coredump_offset(cookie) != psections[i].p_offset)
245 panic("coredump: offset %lld != p_offset[%d] %lld",
246 (long long) coredump_offset(cookie), i,
247 (long long) psections[i].p_filesz);
248 #endif
249
250 error = coredump_write(cookie, UIO_USERSPACE,
251 (void *)(vaddr_t)psections[i].p_vaddr,
252 psections[i].p_filesz);
253 if (error)
254 goto out;
255 }
256
257 out:
258 if (psections)
259 kmem_free(psections, psectionssize);
260 for (; (nb = ns.ns_first) != NULL; ns.ns_first = nb_next) {
261 nb_next = nb->nb_next;
262 kmem_free(nb, sizeof *nb);
263 }
264 return (error);
265 }
266
267 static int
268 ELFNAMEEND(coredump_getseghdrs)(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(ws->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 int error;
333 struct lwp *l0;
334 sigset_t ss1, ss2;
335
336 p = l->l_proc;
337
338 /* First, write an elfcore_procinfo. */
339 cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
340 cpi.cpi_cpisize = sizeof(cpi);
341 cpi.cpi_signo = p->p_sigctx.ps_signo;
342 cpi.cpi_sigcode = p->p_sigctx.ps_code;
343 cpi.cpi_siglwp = p->p_sigctx.ps_lwp;
344
345 /*
346 * XXX This should be per-LWP.
347 */
348 ss1 = p->p_sigpend.sp_set;
349 sigemptyset(&ss2);
350 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
351 sigplusset(&l0->l_sigpend.sp_set, &ss1);
352 sigplusset(&l0->l_sigmask, &ss2);
353 }
354 memcpy(&cpi.cpi_sigpend, &ss1, sizeof(cpi.cpi_sigpend));
355 memcpy(&cpi.cpi_sigmask, &ss2, sizeof(cpi.cpi_sigmask));
356 memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
357 sizeof(cpi.cpi_sigignore));
358 memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
359 sizeof(cpi.cpi_sigcatch));
360
361 cpi.cpi_pid = p->p_pid;
362 mutex_enter(proc_lock);
363 cpi.cpi_ppid = p->p_pptr->p_pid;
364 cpi.cpi_pgrp = p->p_pgid;
365 cpi.cpi_sid = p->p_session->s_sid;
366 mutex_exit(proc_lock);
367
368 cpi.cpi_ruid = kauth_cred_getuid(l->l_cred);
369 cpi.cpi_euid = kauth_cred_geteuid(l->l_cred);
370 cpi.cpi_svuid = kauth_cred_getsvuid(l->l_cred);
371
372 cpi.cpi_rgid = kauth_cred_getgid(l->l_cred);
373 cpi.cpi_egid = kauth_cred_getegid(l->l_cred);
374 cpi.cpi_svgid = kauth_cred_getsvgid(l->l_cred);
375
376 cpi.cpi_nlwps = p->p_nlwps;
377 (void)strncpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
378 cpi.cpi_name[sizeof(cpi.cpi_name) - 1] = '\0';
379
380 ELFNAMEEND(coredump_savenote)(ns, ELF_NOTE_NETBSD_CORE_PROCINFO,
381 ELF_NOTE_NETBSD_CORE_NAME, &cpi, sizeof(cpi));
382
383 /* XXX Add hook for machdep per-proc notes. */
384
385 /*
386 * Now write the register info for the thread that caused the
387 * coredump.
388 */
389 error = ELFNAMEEND(coredump_note)(l, ns);
390 if (error)
391 return (error);
392
393 /*
394 * Now, for each LWP, write the register info and any other
395 * per-LWP notes.
396 * Lock in case this is a gcore requested dump.
397 */
398 mutex_enter(p->p_lock);
399 LIST_FOREACH(l0, &p->p_lwps, l_sibling) {
400 if (l0 == l) /* we've taken care of this thread */
401 continue;
402 error = ELFNAMEEND(coredump_note)(l0, ns);
403 if (error)
404 break;
405 }
406 mutex_exit(p->p_lock);
407
408 return error;
409 }
410
411 static int
412 ELFNAMEEND(coredump_note)(struct lwp *l, struct note_state *ns)
413 {
414 int error;
415 char name[64];
416 elf_reg intreg;
417 #ifdef PT_GETFPREGS
418 elf_fpreg freg;
419 size_t freglen;
420 #endif
421
422 snprintf(name, sizeof(name), "%s@%d",
423 ELF_NOTE_NETBSD_CORE_NAME, l->l_lid);
424
425 error = elf_process_read_regs(l, &intreg);
426 if (error)
427 return (error);
428
429 ELFNAMEEND(coredump_savenote)(ns, PT_GETREGS, name, &intreg,
430 sizeof(intreg));
431
432 #ifdef PT_GETFPREGS
433 freglen = sizeof(freg);
434 error = elf_process_read_fpregs(l, &freg, &freglen);
435 if (error)
436 return (error);
437
438 ELFNAMEEND(coredump_savenote)(ns, PT_GETFPREGS, name, &freg, freglen);
439 #endif
440 /* XXX Add hook for machdep per-LWP notes. */
441 return (0);
442 }
443
444 static void
445 save_note_bytes(struct note_state *ns, const void *data, size_t len)
446 {
447 struct note_buf *nb = ns->ns_last;
448 size_t copylen;
449 unsigned char *wp;
450
451 /*
452 * Just copy the data into a buffer list.
453 * All but the last buffer is full.
454 */
455 for (;;) {
456 copylen = min(len, sizeof nb->nb_data - ns->ns_offset);
457 wp = nb->nb_data + ns->ns_offset;
458 memcpy(wp, data, copylen);
459 if (copylen == len)
460 break;
461 nb->nb_next = kmem_alloc(sizeof *nb->nb_next, KM_SLEEP);
462 nb = nb->nb_next;
463 ns->ns_last = nb;
464 ns->ns_count++;
465 ns->ns_offset = 0;
466 len -= copylen;
467 data = (const unsigned char *)data + copylen;
468 }
469
470 while (copylen & (ELFROUNDSIZE - 1))
471 wp[copylen++] = 0;
472
473 ns->ns_offset += copylen;
474 }
475
476 void
477 ELFNAMEEND(coredump_savenote)(struct note_state *ns, unsigned int type,
478 const char *name, void *data, size_t data_len)
479 {
480 Elf_Nhdr nhdr;
481
482 nhdr.n_namesz = strlen(name) + 1;
483 nhdr.n_descsz = data_len;
484 nhdr.n_type = type;
485
486 save_note_bytes(ns, &nhdr, sizeof (nhdr));
487 save_note_bytes(ns, name, nhdr.n_namesz);
488 save_note_bytes(ns, data, data_len);
489 }
490
491 #else /* COREDUMP */
492
493 int
494 ELFNAMEEND(coredump)(struct lwp *l, void *cookie)
495 {
496
497 return ENOSYS;
498 }
499
500 #endif /* COREDUMP */
501