core_elf32.c revision 1.1 1 /* $NetBSD: core_elf32.c,v 1.1 2001/12/09 23:05:59 thorpej 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.1 2001/12/09 23:05:59 thorpej 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
57 #include <machine/reg.h>
58
59 #include <uvm/uvm.h>
60
61 int ELFNAMEEND(coredump_notes)(struct proc *, struct vnode *,
62 struct ucred *, int *, off_t);
63
64 #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */
65 #define elfround(x) roundup((x), ELFROUNDSIZE)
66
67 int
68 ELFNAMEEND(coredump)(struct proc *p, struct vnode *vp, struct ucred *cred)
69 {
70 Elf_Ehdr ehdr;
71 Elf_Phdr phdr;
72 struct vmspace *vm = p->p_vmspace;
73 struct vm_map *map = &vm->vm_map;
74 struct vm_map_entry *entry;
75 vaddr_t start, end, maxstack;
76 vsize_t size;
77 off_t offset, secoff, notestart, secstart;
78 int npsections, notesize, error;
79
80 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
81
82 /*
83 * We have to make a total of 3 passes across the map:
84 *
85 * 1. Count the number of map entries (the number of
86 * PT_LOAD sections).
87 *
88 * 2. Write the P-section headers.
89 *
90 * 3. Write the P-sections.
91 */
92
93 /* Pass 1: count the entries. */
94 npsections = 0;
95 for (entry = map->header.next; entry != &map->header;
96 entry = entry->next) {
97 /* Should never happen for a user process. */
98 if (UVM_ET_ISSUBMAP(entry))
99 panic("coredump_elf: user process with submap?");
100
101 if (entry->start >= VM_MAXUSER_ADDRESS)
102 continue;
103
104 if (entry->start >= (vaddr_t)vm->vm_maxsaddr &&
105 entry->end <= maxstack)
106 continue;
107
108 npsections++;
109 }
110
111 /* Get the size of the notes. */
112 error = ELFNAMEEND(coredump_notes)(p, vp, cred, ¬esize, 0);
113 if (error)
114 return (error);
115
116 /* Count the PT_NOTE section. */
117 npsections++;
118
119 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
120 #if ELFSIZE == 32
121 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
122 #elif ELFSIZE == 64
123 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
124 #endif
125 ehdr.e_ident[EI_DATA] = ELFDEFNNAME(MACHDEP_ENDIANNESS);
126 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
127 /* XXX Should be the OSABI/ABI version of the executable. */
128 ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
129 ehdr.e_ident[EI_ABIVERSION] = 1;
130
131 ehdr.e_type = ET_CORE;
132 /* XXX This should be the e_machine of the executable. */
133 ehdr.e_machine = ELFDEFNNAME(MACHDEP_ID);
134 ehdr.e_version = EV_CURRENT;
135 ehdr.e_entry = 0;
136 ehdr.e_phoff = sizeof(ehdr);
137 ehdr.e_shoff = 0;
138 ehdr.e_flags = 0;
139 ehdr.e_ehsize = sizeof(ehdr);
140 ehdr.e_phentsize = sizeof(Elf_Phdr);
141 ehdr.e_phnum = npsections;
142 ehdr.e_shentsize = 0;
143 ehdr.e_shnum = 0;
144 ehdr.e_shstrndx = 0;
145
146 /* Write out the ELF header. */
147 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ehdr,
148 (int)sizeof(ehdr), (off_t)0,
149 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
150
151 offset = ehdr.e_phoff;
152 notestart = offset + (sizeof(phdr) * npsections);
153 secstart = round_page(notestart + notesize);
154
155 /*
156 * Now write the P-section headers.
157 */
158 secoff = secstart;
159 for (entry = map->header.next; entry != &map->header;
160 entry = entry->next) {
161 start = entry->start;
162 end = entry->end;
163
164 if (start >= VM_MAXUSER_ADDRESS)
165 continue;
166
167 if (end > VM_MAXUSER_ADDRESS)
168 end = VM_MAXUSER_ADDRESS;
169
170 if (start >= (vaddr_t)vm->vm_maxsaddr) {
171 if (end <= maxstack)
172 continue;
173 if (start < maxstack)
174 start = maxstack;
175 }
176
177 size = end - start;
178
179 phdr.p_type = PT_LOAD;
180 phdr.p_offset = secoff;
181 phdr.p_vaddr = start;
182 phdr.p_paddr = 0;
183 phdr.p_filesz = (entry->protection & VM_PROT_WRITE) ? size : 0;
184 phdr.p_memsz = size;
185 phdr.p_flags = 0;
186 if (entry->protection & VM_PROT_READ)
187 phdr.p_flags |= PF_R;
188 if (entry->protection & VM_PROT_WRITE)
189 phdr.p_flags |= PF_W;
190 if (entry->protection & VM_PROT_EXECUTE)
191 phdr.p_flags |= PF_X;
192 phdr.p_align = PAGE_SIZE;
193
194 error = vn_rdwr(UIO_WRITE, vp,
195 (caddr_t)&phdr, sizeof(phdr),
196 offset, UIO_SYSSPACE,
197 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
198 if (error)
199 return (error);
200
201 offset += sizeof(phdr);
202 secoff += phdr.p_filesz;
203 }
204
205 /* Write out the PT_NOTE header. */
206 phdr.p_type = PT_NOTE;
207 phdr.p_offset = notestart;
208 phdr.p_vaddr = 0;
209 phdr.p_paddr = 0;
210 phdr.p_filesz = notesize;
211 phdr.p_memsz = 0;
212 phdr.p_flags = PF_R;
213 phdr.p_align = ELFROUNDSIZE;
214
215 error = vn_rdwr(UIO_WRITE, vp,
216 (caddr_t)&phdr, sizeof(phdr),
217 offset, UIO_SYSSPACE,
218 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
219 if (error)
220 return (error);
221
222 offset += sizeof(phdr);
223
224 KASSERT(offset == notestart);
225
226 /* Write out the notes. */
227 error = ELFNAMEEND(coredump_notes)(p, vp, cred, ¬esize, offset);
228 if (error)
229 return (error);
230
231 offset += notesize;
232
233 /* ...and write out the sections. */
234 secoff = secstart;
235 for (entry = map->header.next; entry != &map->header;
236 entry = entry->next) {
237 start = entry->start;
238 end = entry->end;
239
240 if (start >= VM_MAXUSER_ADDRESS)
241 continue;
242
243 if (end > VM_MAXUSER_ADDRESS)
244 end = VM_MAXUSER_ADDRESS;
245
246 if (start >= (vaddr_t)vm->vm_maxsaddr) {
247 if (end <= maxstack)
248 continue;
249 if (start < maxstack)
250 start = maxstack;
251 }
252
253 size = end - start;
254
255 if ((entry->protection & VM_PROT_WRITE) == 0) {
256 /* Not actually written out. */
257 continue;
258 }
259
260 error = vn_rdwr(UIO_WRITE, vp,
261 (caddr_t)start, size,
262 secoff, UIO_USERSPACE,
263 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
264 if (error)
265 return (error);
266
267 secoff += size;
268 }
269
270 return (0);
271 }
272
273 int
274 ELFNAMEEND(coredump_notes)(struct proc *p, struct vnode *vp,
275 struct ucred *cred, int *sizep, off_t offset)
276 {
277 struct netbsd_elfcore_procinfo cpi;
278 Elf_Nhdr nhdr;
279 int size, notesize, error;
280 char name[64];
281 int namesize;
282 struct reg intreg;
283 #ifdef PT_GETFPREGS
284 struct fpreg freg;
285 #endif
286
287 size = 0;
288
289 /* First, write an elfcore_procinfo. */
290 notesize = sizeof(nhdr) + elfround(sizeof(ELF_NOTE_NETBSD_CORE_NAME)) +
291 elfround(sizeof(cpi));
292 if (offset) {
293 cpi.cpi_version = NETBSD_ELFCORE_PROCINFO_VERSION;
294 cpi.cpi_cpisize = sizeof(cpi);
295 cpi.cpi_signo = p->p_sigctx.ps_sig;
296 cpi.cpi_sigcode = p->p_sigctx.ps_code;
297
298 memcpy(&cpi.cpi_sigpend, &p->p_sigctx.ps_siglist,
299 sizeof(cpi.cpi_sigpend));
300 memcpy(&cpi.cpi_sigmask, &p->p_sigctx.ps_sigmask,
301 sizeof(cpi.cpi_sigmask));
302 memcpy(&cpi.cpi_sigignore, &p->p_sigctx.ps_sigignore,
303 sizeof(cpi.cpi_sigignore));
304 memcpy(&cpi.cpi_sigcatch, &p->p_sigctx.ps_sigcatch,
305 sizeof(cpi.cpi_sigcatch));
306
307 cpi.cpi_pid = p->p_pid;
308 cpi.cpi_ppid = p->p_pptr->p_pid;
309 cpi.cpi_pgrp = p->p_pgid;
310 cpi.cpi_sid = p->p_session->s_sid;
311
312 cpi.cpi_ruid = p->p_cred->p_ruid;
313 cpi.cpi_euid = p->p_ucred->cr_uid;
314 cpi.cpi_svuid = p->p_cred->p_svuid;
315
316 cpi.cpi_rgid = p->p_cred->p_rgid;
317 cpi.cpi_egid = p->p_ucred->cr_gid;
318 cpi.cpi_svgid = p->p_cred->p_svgid;
319
320 cpi.cpi_nlwps = 1; /* XXX for now */
321 strcpy(cpi.cpi_name, p->p_comm);
322
323 nhdr.n_namesz = sizeof(ELF_NOTE_NETBSD_CORE_NAME);
324 nhdr.n_descsz = sizeof(cpi);
325 nhdr.n_type = ELF_NOTE_NETBSD_CORE_PROCINFO;
326
327 error = ELFNAMEEND(coredump_writenote)(p, vp, cred, offset,
328 &nhdr, ELF_NOTE_NETBSD_CORE_NAME, &cpi);
329 if (error)
330 return (error);
331
332 offset += notesize;
333 }
334
335 size += notesize;
336
337 /* XXX Add hook for machdep per-proc notes. */
338
339 /*
340 * Now, for each LWP, write the register info and any other
341 * per-LWP notes.
342 */
343 do {
344 /* XXX Only one LWP for now. */
345 sprintf(name, "%s@%d", ELF_NOTE_NETBSD_CORE_NAME, 1);
346 namesize = strlen(name) + 1;
347
348 notesize = sizeof(nhdr) + elfround(namesize) +
349 elfround(sizeof(intreg));
350 if (offset) {
351 error = process_read_regs(p, &intreg);
352 if (error)
353 return (error);
354
355 nhdr.n_namesz = namesize;
356 nhdr.n_descsz = sizeof(intreg);
357 nhdr.n_type = PT_GETREGS;
358
359 error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
360 offset, &nhdr, name, &intreg);
361 if (error)
362 return (error);
363
364 offset += notesize;
365 }
366 size += notesize;
367
368 #ifdef PT_GETFPREGS
369 notesize = sizeof(nhdr) + elfround(namesize) +
370 elfround(sizeof(freg));
371 if (offset) {
372 error = process_read_fpregs(p, &freg);
373 if (error)
374 return (error);
375
376 nhdr.n_namesz = namesize;
377 nhdr.n_descsz = sizeof(freg);
378 nhdr.n_type = PT_GETFPREGS;
379
380 error = ELFNAMEEND(coredump_writenote)(p, vp, cred,
381 offset, &nhdr, name, &freg);
382 if (error)
383 return (error);
384
385 offset += notesize;
386 }
387 size += notesize;
388 #endif
389 /* XXX Add hook for machdep per-LWP notes. */
390 } while (/*CONSTCOND*/0);
391
392 *sizep = size;
393 return (0);
394 }
395
396 int
397 ELFNAMEEND(coredump_writenote)(struct proc *p, struct vnode *vp,
398 struct ucred *cred, off_t offset, Elf_Nhdr *nhdr, const char *name,
399 void *data)
400 {
401 int error;
402
403 error = vn_rdwr(UIO_WRITE, vp,
404 (caddr_t) nhdr, sizeof(*nhdr),
405 offset, UIO_SYSSPACE,
406 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
407 if (error)
408 return (error);
409
410 offset += sizeof(*nhdr);
411
412 error = vn_rdwr(UIO_WRITE, vp,
413 (caddr_t)name, nhdr->n_namesz,
414 offset, UIO_SYSSPACE,
415 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
416 if (error)
417 return (error);
418
419 offset += elfround(nhdr->n_namesz);
420
421 error = vn_rdwr(UIO_WRITE, vp,
422 data, nhdr->n_descsz,
423 offset, UIO_SYSSPACE,
424 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
425
426 return (error);
427 }
428