]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/coregrind/m_coredump/coredump-elf.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / coregrind / m_coredump / coredump-elf.c
1
2 /*--------------------------------------------------------------------*/
3 /*--- Dumping core.                                 coredump-elf.c ---*/
4 /*--------------------------------------------------------------------*/
5  
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9
10    Copyright (C) 2000-2010 Julian Seward 
11       jseward@acm.org
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of the
16    License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26    02111-1307, USA.
27
28    The GNU General Public License is contained in the file COPYING.
29 */
30
31 #if defined(VGO_linux)
32
33 #include "pub_core_basics.h"
34 #include "pub_core_vki.h"
35 #include "pub_core_aspacehl.h"
36 #include "pub_core_aspacemgr.h"
37 #include "pub_core_libcbase.h"
38 #include "pub_core_machine.h"
39 #include "pub_core_coredump.h"
40 #include "pub_core_libcprint.h"
41 #include "pub_core_libcfile.h"    // VG_(close) et al
42 #include "pub_core_libcproc.h"    // VG_(geteuid), VG_(getegid)
43 #include "pub_core_libcassert.h"  // VG_(exit), vg_assert
44 #include "pub_core_mallocfree.h"  // VG_(malloc), VG_(free)
45 #include "pub_core_libcsetjmp.h"  // to keep _threadstate.h happy
46 #include "pub_core_threadstate.h"
47 #include "pub_core_xarray.h"
48 #include "pub_core_clientstate.h"
49 #include "pub_core_options.h"
50
51 /*
52   Dump core
53    
54   Generate a standard ELF core file corresponding to the client state
55   at the time of a crash.
56  */
57 #include <elf.h>
58 #ifndef NT_PRXFPREG
59 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
60 #endif /* NT_PRXFPREG */
61
62 #if     VG_WORDSIZE == 8
63 #define ESZ(x)  Elf64_##x
64 #elif   VG_WORDSIZE == 4
65 #define ESZ(x)  Elf32_##x
66 #else
67 #error VG_WORDSIZE needs to ==4 or ==8
68 #endif
69
70 /* If true, then this Segment may be mentioned in the core */
71 static Bool may_dump(const NSegment *seg)
72 {
73    if (seg->kind == SkAnonC ||
74        seg->kind == SkShmC ||
75        (seg->kind == SkFileC &&
76         !VKI_S_ISCHR(seg->mode) && !VKI_S_ISBLK(seg->mode)))
77       return True;
78
79    return False;
80 }
81
82 /* If true, then this Segment's contents will be in the core */
83 static Bool should_dump(const NSegment *seg)
84 {
85    return may_dump(seg); // && seg->hasW;
86 }
87
88 static void fill_ehdr(ESZ(Ehdr) *ehdr, Int num_phdrs)
89 {
90    VG_(memset)(ehdr, 0, sizeof(*ehdr));
91
92    VG_(memcpy)(ehdr->e_ident, ELFMAG, SELFMAG);
93    ehdr->e_ident[EI_CLASS]   = VG_ELF_CLASS;
94    ehdr->e_ident[EI_DATA]    = VG_ELF_DATA2XXX;
95    ehdr->e_ident[EI_VERSION] = EV_CURRENT;
96
97    ehdr->e_type = ET_CORE;
98    ehdr->e_machine = VG_ELF_MACHINE;
99    ehdr->e_version = EV_CURRENT;
100    ehdr->e_entry = 0;
101    ehdr->e_phoff = sizeof(ESZ(Ehdr));
102    ehdr->e_shoff = 0;
103    ehdr->e_flags = 0;
104    ehdr->e_ehsize = sizeof(ESZ(Ehdr));
105    ehdr->e_phentsize = sizeof(ESZ(Phdr));
106    ehdr->e_phnum = num_phdrs;
107    ehdr->e_shentsize = 0;
108    ehdr->e_shnum = 0;
109    ehdr->e_shstrndx = 0;
110
111 }
112
113 static void fill_phdr(ESZ(Phdr) *phdr, const NSegment *seg, UInt off, Bool write)
114 {
115    SizeT len = seg->end - seg->start;
116
117    write = write && should_dump(seg);
118
119    VG_(memset)(phdr, 0, sizeof(*phdr));
120
121    phdr->p_type = PT_LOAD;
122    phdr->p_offset = off;
123    phdr->p_vaddr = seg->start;
124    phdr->p_paddr = 0;
125    phdr->p_filesz = write ? len : 0;
126    phdr->p_memsz = len;
127    phdr->p_flags = 0;
128
129    if (seg->hasR)
130       phdr->p_flags |= PF_R;
131    if (seg->hasW)
132       phdr->p_flags |= PF_W;
133    if (seg->hasX)
134       phdr->p_flags |= PF_X;
135
136    phdr->p_align = VKI_PAGE_SIZE;
137 }
138
139 struct note {
140    struct note *next;
141    ESZ(Nhdr) note;
142    Char name[0];
143 };
144
145 static UInt note_size(const struct note *n)
146 {
147    return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) + VG_ROUNDUP(n->note.n_descsz, 4);
148 }
149
150 static void add_note(struct note **list, const Char *name, UInt type, const void *data, UInt datasz)
151 {
152    Int namelen = VG_(strlen)(name)+1;
153    Int notelen = sizeof(struct note) + 
154       VG_ROUNDUP(namelen, 4) + 
155       VG_ROUNDUP(datasz, 4);
156    struct note *n = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.an.1", notelen);
157
158    VG_(memset)(n, 0, notelen);
159
160    n->next = *list;
161    *list = n;
162
163    n->note.n_type = type;
164    n->note.n_namesz = namelen;
165    n->note.n_descsz = datasz;
166
167    VG_(memcpy)(n->name, name, namelen);
168    VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
169 }
170
171 static void write_note(Int fd, const struct note *n)
172 {
173    VG_(write)(fd, &n->note, note_size(n));
174 }
175
176 static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo)
177 {
178    static Char name[VKI_PATH_MAX];
179
180    VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
181
182    switch(tst->status) {
183    case VgTs_Runnable:
184    case VgTs_Yielding:
185       prpsinfo->pr_sname = 'R';
186       break;
187
188    case VgTs_WaitSys:
189       prpsinfo->pr_sname = 'S';
190       break;
191
192    case VgTs_Zombie:
193       prpsinfo->pr_sname = 'Z';
194       break;
195
196    case VgTs_Empty:
197    case VgTs_Init:
198       prpsinfo->pr_sname = '?';
199       break;
200    }
201
202    prpsinfo->pr_uid = 0;
203    prpsinfo->pr_gid = 0;
204    
205    if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) {
206       Char *n = name+VG_(strlen)(name)-1;
207
208       while (n > name && *n != '/')
209          n--;
210       if (n != name)
211          n++;
212
213       VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname));
214    }
215 }
216
217 static void fill_prstatus(const ThreadState *tst, 
218                           struct vki_elf_prstatus *prs, 
219                           const vki_siginfo_t *si)
220 {
221    struct vki_user_regs_struct *regs;
222    ThreadArchState* arch = (ThreadArchState*)&tst->arch;
223
224    VG_(memset)(prs, 0, sizeof(*prs));
225
226    prs->pr_info.si_signo = si->si_signo;
227    prs->pr_info.si_code = si->si_code;
228    prs->pr_info.si_errno = 0;
229
230    prs->pr_cursig = si->si_signo;
231
232    prs->pr_pid = tst->os_state.lwpid;
233    prs->pr_ppid = 0;
234    prs->pr_pgrp = VG_(getpgrp)();
235    prs->pr_sid = VG_(getpgrp)();
236    
237 #ifdef VGP_s390x_linux
238    /* prs->pr_reg has struct type. Need to take address. */
239    regs = (struct vki_user_regs_struct *)&(prs->pr_reg);
240 #else
241    regs = (struct vki_user_regs_struct *)prs->pr_reg;
242
243    vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
244 #endif
245
246 #if defined(VGP_x86_linux)
247    regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
248    regs->esp    = arch->vex.guest_ESP;
249    regs->eip    = arch->vex.guest_EIP;
250
251    regs->ebx    = arch->vex.guest_EBX;
252    regs->ecx    = arch->vex.guest_ECX;
253    regs->edx    = arch->vex.guest_EDX;
254    regs->esi    = arch->vex.guest_ESI;
255    regs->edi    = arch->vex.guest_EDI;
256    regs->ebp    = arch->vex.guest_EBP;
257    regs->eax    = arch->vex.guest_EAX;
258
259    regs->cs     = arch->vex.guest_CS;
260    regs->ds     = arch->vex.guest_DS;
261    regs->ss     = arch->vex.guest_SS;
262    regs->es     = arch->vex.guest_ES;
263    regs->fs     = arch->vex.guest_FS;
264    regs->gs     = arch->vex.guest_GS;
265
266 #elif defined(VGP_amd64_linux)
267    regs->eflags = LibVEX_GuestAMD64_get_rflags( &((ThreadArchState*)arch)->vex );
268    regs->rsp    = arch->vex.guest_RSP;
269    regs->rip    = arch->vex.guest_RIP;
270
271    regs->rbx    = arch->vex.guest_RBX;
272    regs->rcx    = arch->vex.guest_RCX;
273    regs->rdx    = arch->vex.guest_RDX;
274    regs->rsi    = arch->vex.guest_RSI;
275    regs->rdi    = arch->vex.guest_RDI;
276    regs->rbp    = arch->vex.guest_RBP;
277    regs->rax    = arch->vex.guest_RAX;
278    regs->r8     = arch->vex.guest_R8;
279    regs->r9     = arch->vex.guest_R9;
280    regs->r10    = arch->vex.guest_R10;
281    regs->r11    = arch->vex.guest_R11;
282    regs->r12    = arch->vex.guest_R12;
283    regs->r13    = arch->vex.guest_R13;
284    regs->r14    = arch->vex.guest_R14;
285    regs->r15    = arch->vex.guest_R15;
286
287 //::    regs->cs     = arch->vex.guest_CS;
288 //::    regs->fs     = arch->vex.guest_FS;
289 //::    regs->gs     = arch->vex.guest_GS;
290
291 #elif defined(VGP_ppc32_linux)
292 #  define DO(n)  regs->gpr[n] = arch->vex.guest_GPR##n
293    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
294    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
295    DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
296    DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
297 #  undef DO
298
299    regs->nip = arch->vex.guest_CIA;
300    regs->msr = 0xf032;   /* pretty arbitrary */
301    regs->orig_gpr3 = arch->vex.guest_GPR3;
302    regs->ctr = arch->vex.guest_CTR;
303    regs->link = arch->vex.guest_LR;
304    regs->xer = LibVEX_GuestPPC32_get_XER( &((ThreadArchState*)arch)->vex );
305    regs->ccr = LibVEX_GuestPPC32_get_CR( &((ThreadArchState*)arch)->vex );
306    regs->mq = 0;
307    regs->trap = 0;
308    regs->dar = 0; /* should be fault address? */
309    regs->dsisr = 0;
310    regs->result = 0;
311
312 #elif defined(VGP_ppc64_linux)
313 #  define DO(n)  regs->gpr[n] = arch->vex.guest_GPR##n
314    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
315    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
316    DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
317    DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
318 #  undef DO
319
320    regs->nip = arch->vex.guest_CIA;
321    regs->msr = 0xf032;   /* pretty arbitrary */
322    regs->orig_gpr3 = arch->vex.guest_GPR3;
323    regs->ctr = arch->vex.guest_CTR;
324    regs->link = arch->vex.guest_LR;
325    regs->xer = LibVEX_GuestPPC64_get_XER( &((ThreadArchState*)arch)->vex );
326    regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)arch)->vex );
327    /* regs->mq = 0; */
328    regs->trap = 0;
329    regs->dar = 0; /* should be fault address? */
330    regs->dsisr = 0;
331    regs->result = 0;
332
333 #elif defined(VGP_arm_linux)
334    regs->ARM_r0   = arch->vex.guest_R0;
335    regs->ARM_r1   = arch->vex.guest_R1;
336    regs->ARM_r2   = arch->vex.guest_R2;
337    regs->ARM_r3   = arch->vex.guest_R3;
338    regs->ARM_r4   = arch->vex.guest_R4;
339    regs->ARM_r5   = arch->vex.guest_R5;
340    regs->ARM_r6   = arch->vex.guest_R6;
341    regs->ARM_r7   = arch->vex.guest_R7;
342    regs->ARM_r8   = arch->vex.guest_R8;
343    regs->ARM_r9   = arch->vex.guest_R9;
344    regs->ARM_r10  = arch->vex.guest_R10;
345    regs->ARM_fp   = arch->vex.guest_R11;
346    regs->ARM_ip   = arch->vex.guest_R12;
347    regs->ARM_sp   = arch->vex.guest_R13;
348    regs->ARM_lr   = arch->vex.guest_R14;
349    regs->ARM_pc   = arch->vex.guest_R15T;
350    regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &((ThreadArchState*)arch)->vex );
351
352 #elif defined(VGP_s390x_linux)
353 #  define DO(n)  regs->gprs[n] = arch->vex.guest_r##n
354    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
355    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
356 #  undef DO
357 #  define DO(n)  regs->acrs[n] = arch->vex.guest_a##n
358    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
359    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
360 #  undef DO
361    regs->orig_gpr2 = arch->vex.guest_r2;
362 #else
363 #  error Unknown ELF platform
364 #endif
365 }
366
367 static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
368 {
369    __attribute__((unused))
370    ThreadArchState* arch = (ThreadArchState*)&tst->arch;
371
372 #if defined(VGP_x86_linux)
373 //:: static void fill_fpu(vki_elf_fpregset_t *fpu, const Char *from)
374 //:: {
375 //::    if (VG_(have_ssestate)) {
376 //::       UShort *to;
377 //::       Int i;
378 //:: 
379 //::       /* This is what the kernel does */
380 //::       VG_(memcpy)(fpu, from, 7*sizeof(long));
381 //::    
382 //::       to = (UShort *)&fpu->st_space[0];
383 //::       from += 18 * sizeof(UShort);
384 //:: 
385 //::       for (i = 0; i < 8; i++, to += 5, from += 8) 
386 //::     VG_(memcpy)(to, from, 5*sizeof(UShort));
387 //::    } else
388 //::       VG_(memcpy)(fpu, from, sizeof(*fpu));
389 //:: }
390
391 //::    fill_fpu(fpu, (const Char *)&arch->m_sse);
392
393 #elif defined(VGP_amd64_linux)
394 //::    fpu->cwd = ?;
395 //::    fpu->swd = ?;
396 //::    fpu->twd = ?;
397 //::    fpu->fop = ?;
398 //::    fpu->rip = ?;
399 //::    fpu->rdp = ?;
400 //::    fpu->mxcsr = ?;
401 //::    fpu->mxcsr_mask = ?;
402 //::    fpu->st_space = ?;
403
404 #  define DO(n)  VG_(memcpy)(fpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
405    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
406    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
407 #  undef DO
408
409    VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
410
411 #elif defined(VGP_ppc32_linux)
412    /* The guest state has the FPR fields declared as ULongs, so need
413       to fish out the values without converting them.
414       NOTE: The 32 FP registers map to the first 32 VSX registers.*/
415 #  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
416    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
417    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
418    DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
419    DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
420 #  undef DO
421
422 #elif defined(VGP_ppc64_linux)
423    /* The guest state has the FPR fields declared as ULongs, so need
424       to fish out the values without converting them.
425       NOTE: The 32 FP registers map to the first 32 VSX registers.*/
426 #  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
427    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
428    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
429    DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
430    DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
431 #  undef DO
432
433 #elif defined(VGP_arm_linux)
434    // umm ...
435
436 #elif defined(VGP_s390x_linux)
437 #  define DO(n)  fpu->fprs[n].ui = arch->vex.guest_f##n
438    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
439    DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
440 # undef DO
441 #else
442 #  error Unknown ELF platform
443 #endif
444 }
445
446 #if defined(VGP_x86_linux)
447 static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
448 {
449    ThreadArchState* arch = (ThreadArchState*)&tst->arch;
450
451 //::    xfpu->cwd = ?;
452 //::    xfpu->swd = ?;
453 //::    xfpu->twd = ?;
454 //::    xfpu->fop = ?;
455 //::    xfpu->fip = ?;
456 //::    xfpu->fcs = ?;
457 //::    xfpu->foo = ?;
458 //::    xfpu->fos = ?;
459 //::    xfpu->mxcsr = ?;
460    xfpu->reserved = 0;
461 //::    xfpu->st_space = ?;
462
463 #  define DO(n)  VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
464    DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
465 #  undef DO
466
467    VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
468 }
469 #endif
470
471 static
472 void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
473 {
474    Char* buf = NULL;
475    Char *basename = "vgcore";
476    Char *coreext = "";
477    Int seq = 0;
478    Int core_fd;
479    NSegment const * seg;
480    ESZ(Ehdr) ehdr;
481    ESZ(Phdr) *phdrs;
482    Int num_phdrs;
483    Int i, idx;
484    UInt off;
485    struct note *notelist, *note;
486    UInt notesz;
487    struct vki_elf_prpsinfo prpsinfo;
488    struct vki_elf_prstatus prstatus;
489    Addr *seg_starts;
490    Int n_seg_starts;
491
492    if (VG_(clo_log_fname_expanded) != NULL) {
493       coreext = ".core";
494       basename = VG_(expand_file_name)(
495                     "--log-file (while creating core filename)",
496                     VG_(clo_log_fname_expanded));
497    }
498
499    vg_assert(coreext);
500    vg_assert(basename);
501    buf = VG_(malloc)( "coredump-elf.mec.1", 
502                       VG_(strlen)(coreext) + VG_(strlen)(basename)
503                          + 100/*for the two %ds. */ );
504    vg_assert(buf);
505
506    for(;;) {
507       SysRes sres;
508
509       if (seq == 0)
510          VG_(sprintf)(buf, "%s%s.%d",
511                       basename, coreext, VG_(getpid)());
512       else
513          VG_(sprintf)(buf, "%s%s.%d.%d",
514                       basename, coreext, VG_(getpid)(), seq);
515       seq++;
516
517       sres = VG_(open)(buf,                        
518                        VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, 
519                        VKI_S_IRUSR|VKI_S_IWUSR);
520       if (!sr_isError(sres)) {
521          core_fd = sr_Res(sres);
522          break;
523       }
524
525       if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
526          return;                /* can't create file */
527    }
528
529    /* Get the segments */
530    seg_starts = VG_(get_segment_starts)(&n_seg_starts);
531
532    /* First, count how many memory segments to dump */
533    num_phdrs = 1;               /* start with notes */
534    for(i = 0; i < n_seg_starts; i++) {
535       if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
536          continue;
537
538       num_phdrs++;
539    }
540
541    fill_ehdr(&ehdr, num_phdrs);
542
543    notelist = NULL;
544
545    /* Second, work out their layout */
546    phdrs = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.mec.1", 
547                              sizeof(*phdrs) * num_phdrs);
548
549    for(i = 1; i < VG_N_THREADS; i++) {
550       vki_elf_fpregset_t  fpu;
551
552       if (VG_(threads)[i].status == VgTs_Empty)
553          continue;
554
555 #if defined(VGP_x86_linux)
556       {
557          vki_elf_fpxregset_t xfpu;
558          fill_xfpu(&VG_(threads)[i], &xfpu);
559          add_note(&notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
560       }
561 #endif
562
563       fill_fpu(&VG_(threads)[i], &fpu);
564       add_note(&notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
565
566       fill_prstatus(&VG_(threads)[i], &prstatus, si);
567       add_note(&notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
568    }
569
570    fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
571    add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
572
573    for(note = notelist, notesz = 0; note != NULL; note = note->next)
574       notesz += note_size(note);
575
576    off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
577
578    phdrs[0].p_type = PT_NOTE;
579    phdrs[0].p_offset = off;
580    phdrs[0].p_vaddr = 0;
581    phdrs[0].p_paddr = 0;
582    phdrs[0].p_filesz = notesz;
583    phdrs[0].p_memsz = 0;
584    phdrs[0].p_flags = 0;
585    phdrs[0].p_align = 0;
586
587    off += notesz;
588
589    off = VG_PGROUNDUP(off);
590
591    for(i = 0, idx = 1; i < n_seg_starts; i++) {
592       seg = VG_(am_find_nsegment(seg_starts[i]));
593
594       if (!may_dump(seg))
595          continue;
596
597       fill_phdr(&phdrs[idx], seg, off, (seg->end - seg->start + off) < max_size);
598       
599       off += phdrs[idx].p_filesz;
600
601       idx++;
602    }
603
604    /* write everything out */
605    VG_(write)(core_fd, &ehdr, sizeof(ehdr));
606    VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
607
608    for(note = notelist; note != NULL; note = note->next)
609       write_note(core_fd, note);
610    
611    VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
612
613    for(i = 0, idx = 1; i < n_seg_starts; i++) {
614       seg = VG_(am_find_nsegment(seg_starts[i]));
615
616       if (!should_dump(seg))
617          continue;
618
619       if (phdrs[idx].p_filesz > 0) {
620          vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET) == phdrs[idx].p_offset);
621          vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz);
622
623          (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
624       }
625       idx++;
626    }
627
628    VG_(free)(seg_starts);
629
630    VG_(close)(core_fd);
631 }
632
633 void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
634 {
635    make_elf_coredump(tid, si, max_size);
636 }
637
638 #endif // defined(VGO_linux)
639
640 /*--------------------------------------------------------------------*/
641 /*--- end                                                          ---*/
642 /*--------------------------------------------------------------------*/