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