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