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-2012 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 */
71static 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 */
83static Bool should_dump(const NSegment *seg)
84{
85   return may_dump(seg); // && seg->hasW;
86}
87
88static 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
113static 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
139struct note {
140   struct note *next;
141   ESZ(Nhdr) note;
142   Char name[0];
143};
144
145static UInt note_size(const struct note *n)
146{
147   return sizeof(ESZ(Nhdr)) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4)
148                            + VG_ROUNDUP(n->note.n_descsz, 4);
149}
150
151#if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
152static void add_note(struct note **list, const Char *name, UInt type,
153                     const void *data, UInt datasz)
154{
155   Int namelen = VG_(strlen)(name)+1;
156   Int notelen = sizeof(struct note) +
157      VG_ROUNDUP(namelen, 4) +
158      VG_ROUNDUP(datasz, 4);
159   struct note *n = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.an.1", notelen);
160
161   VG_(memset)(n, 0, notelen);
162
163   n->next = *list;
164   *list = n;
165
166   n->note.n_type = type;
167   n->note.n_namesz = namelen;
168   n->note.n_descsz = datasz;
169
170   VG_(memcpy)(n->name, name, namelen);
171   VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
172}
173#endif /* !defined(VGPV_*_linux_android) */
174
175static void write_note(Int fd, const struct note *n)
176{
177   VG_(write)(fd, &n->note, note_size(n));
178}
179
180static void fill_prpsinfo(const ThreadState *tst,
181                          struct vki_elf_prpsinfo *prpsinfo)
182{
183   static Char name[VKI_PATH_MAX];
184
185   VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
186
187   switch(tst->status) {
188   case VgTs_Runnable:
189   case VgTs_Yielding:
190      prpsinfo->pr_sname = 'R';
191      break;
192
193   case VgTs_WaitSys:
194      prpsinfo->pr_sname = 'S';
195      break;
196
197   case VgTs_Zombie:
198      prpsinfo->pr_sname = 'Z';
199      break;
200
201   case VgTs_Empty:
202   case VgTs_Init:
203      prpsinfo->pr_sname = '?';
204      break;
205   }
206
207   prpsinfo->pr_uid = 0;
208   prpsinfo->pr_gid = 0;
209
210   if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) {
211      Char *n = name+VG_(strlen)(name)-1;
212
213      while (n > name && *n != '/')
214	 n--;
215      if (n != name)
216	 n++;
217
218      VG_(strncpy)(prpsinfo->pr_fname, n, sizeof(prpsinfo->pr_fname));
219   }
220}
221
222static void fill_prstatus(const ThreadState *tst,
223			  struct vki_elf_prstatus *prs,
224			  const vki_siginfo_t *si)
225{
226   struct vki_user_regs_struct *regs;
227   ThreadArchState* arch = (ThreadArchState*)&tst->arch;
228
229   VG_(memset)(prs, 0, sizeof(*prs));
230
231   prs->pr_info.si_signo = si->si_signo;
232   prs->pr_info.si_code = si->si_code;
233   prs->pr_info.si_errno = 0;
234
235   prs->pr_cursig = si->si_signo;
236
237   prs->pr_pid = tst->os_state.lwpid;
238   prs->pr_ppid = 0;
239   prs->pr_pgrp = VG_(getpgrp)();
240   prs->pr_sid = VG_(getpgrp)();
241
242#ifdef VGP_s390x_linux
243   /* prs->pr_reg has struct type. Need to take address. */
244   regs = (struct vki_user_regs_struct *)&(prs->pr_reg);
245#else
246   regs = (struct vki_user_regs_struct *)prs->pr_reg;
247
248   vg_assert(sizeof(*regs) == sizeof(prs->pr_reg));
249#endif
250
251#if defined(VGP_x86_linux)
252   regs->eflags = LibVEX_GuestX86_get_eflags( &arch->vex );
253   regs->esp    = arch->vex.guest_ESP;
254   regs->eip    = arch->vex.guest_EIP;
255
256   regs->ebx    = arch->vex.guest_EBX;
257   regs->ecx    = arch->vex.guest_ECX;
258   regs->edx    = arch->vex.guest_EDX;
259   regs->esi    = arch->vex.guest_ESI;
260   regs->edi    = arch->vex.guest_EDI;
261   regs->ebp    = arch->vex.guest_EBP;
262   regs->eax    = arch->vex.guest_EAX;
263
264   regs->cs     = arch->vex.guest_CS;
265   regs->ds     = arch->vex.guest_DS;
266   regs->ss     = arch->vex.guest_SS;
267   regs->es     = arch->vex.guest_ES;
268   regs->fs     = arch->vex.guest_FS;
269   regs->gs     = arch->vex.guest_GS;
270
271#elif defined(VGP_amd64_linux)
272   regs->eflags = LibVEX_GuestAMD64_get_rflags( &((ThreadArchState*)arch)->vex );
273   regs->rsp    = arch->vex.guest_RSP;
274   regs->rip    = arch->vex.guest_RIP;
275
276   regs->rbx    = arch->vex.guest_RBX;
277   regs->rcx    = arch->vex.guest_RCX;
278   regs->rdx    = arch->vex.guest_RDX;
279   regs->rsi    = arch->vex.guest_RSI;
280   regs->rdi    = arch->vex.guest_RDI;
281   regs->rbp    = arch->vex.guest_RBP;
282   regs->rax    = arch->vex.guest_RAX;
283   regs->r8     = arch->vex.guest_R8;
284   regs->r9     = arch->vex.guest_R9;
285   regs->r10    = arch->vex.guest_R10;
286   regs->r11    = arch->vex.guest_R11;
287   regs->r12    = arch->vex.guest_R12;
288   regs->r13    = arch->vex.guest_R13;
289   regs->r14    = arch->vex.guest_R14;
290   regs->r15    = arch->vex.guest_R15;
291
292//::    regs->cs     = arch->vex.guest_CS;
293//::    regs->fs     = arch->vex.guest_FS;
294//::    regs->gs     = arch->vex.guest_GS;
295
296#elif defined(VGP_ppc32_linux)
297#  define DO(n)  regs->gpr[n] = arch->vex.guest_GPR##n
298   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
299   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
300   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
301   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
302#  undef DO
303
304   regs->nip = arch->vex.guest_CIA;
305   regs->msr = 0xf032;   /* pretty arbitrary */
306   regs->orig_gpr3 = arch->vex.guest_GPR3;
307   regs->ctr = arch->vex.guest_CTR;
308   regs->link = arch->vex.guest_LR;
309   regs->xer = LibVEX_GuestPPC32_get_XER( &((ThreadArchState*)arch)->vex );
310   regs->ccr = LibVEX_GuestPPC32_get_CR( &((ThreadArchState*)arch)->vex );
311   regs->mq = 0;
312   regs->trap = 0;
313   regs->dar = 0; /* should be fault address? */
314   regs->dsisr = 0;
315   regs->result = 0;
316
317#elif defined(VGP_ppc64_linux)
318#  define DO(n)  regs->gpr[n] = arch->vex.guest_GPR##n
319   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
320   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
321   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
322   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
323#  undef DO
324
325   regs->nip = arch->vex.guest_CIA;
326   regs->msr = 0xf032;   /* pretty arbitrary */
327   regs->orig_gpr3 = arch->vex.guest_GPR3;
328   regs->ctr = arch->vex.guest_CTR;
329   regs->link = arch->vex.guest_LR;
330   regs->xer = LibVEX_GuestPPC64_get_XER( &((ThreadArchState*)arch)->vex );
331   regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)arch)->vex );
332   /* regs->mq = 0; */
333   regs->trap = 0;
334   regs->dar = 0; /* should be fault address? */
335   regs->dsisr = 0;
336   regs->result = 0;
337
338#elif defined(VGP_arm_linux)
339   regs->ARM_r0   = arch->vex.guest_R0;
340   regs->ARM_r1   = arch->vex.guest_R1;
341   regs->ARM_r2   = arch->vex.guest_R2;
342   regs->ARM_r3   = arch->vex.guest_R3;
343   regs->ARM_r4   = arch->vex.guest_R4;
344   regs->ARM_r5   = arch->vex.guest_R5;
345   regs->ARM_r6   = arch->vex.guest_R6;
346   regs->ARM_r7   = arch->vex.guest_R7;
347   regs->ARM_r8   = arch->vex.guest_R8;
348   regs->ARM_r9   = arch->vex.guest_R9;
349   regs->ARM_r10  = arch->vex.guest_R10;
350   regs->ARM_fp   = arch->vex.guest_R11;
351   regs->ARM_ip   = arch->vex.guest_R12;
352   regs->ARM_sp   = arch->vex.guest_R13;
353   regs->ARM_lr   = arch->vex.guest_R14;
354   regs->ARM_pc   = arch->vex.guest_R15T;
355   regs->ARM_cpsr = LibVEX_GuestARM_get_cpsr( &((ThreadArchState*)arch)->vex );
356
357#elif defined(VGP_s390x_linux)
358#  define DO(n)  regs->gprs[n] = arch->vex.guest_r##n
359   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
360   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
361#  undef DO
362#  define DO(n)  regs->acrs[n] = arch->vex.guest_a##n
363   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
364   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
365#  undef DO
366   regs->orig_gpr2 = arch->vex.guest_r2;
367#elif defined(VGP_mips32_linux)
368#  define DO(n)  regs->MIPS_r##n = arch->vex.guest_r##n
369   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
370   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
371   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
372   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
373#  undef DO
374   regs->MIPS_hi   = arch->vex.guest_HI;
375   regs->MIPS_lo   = arch->vex.guest_LO;
376#else
377#  error Unknown ELF platform
378#endif
379}
380
381static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
382{
383   __attribute__((unused))
384   ThreadArchState* arch = (ThreadArchState*)&tst->arch;
385
386#if defined(VGP_x86_linux)
387//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const Char *from)
388//:: {
389//::    if (VG_(have_ssestate)) {
390//::       UShort *to;
391//::       Int i;
392//::
393//::       /* This is what the kernel does */
394//::       VG_(memcpy)(fpu, from, 7*sizeof(long));
395//::
396//::       to = (UShort *)&fpu->st_space[0];
397//::       from += 18 * sizeof(UShort);
398//::
399//::       for (i = 0; i < 8; i++, to += 5, from += 8)
400//:: 	 VG_(memcpy)(to, from, 5*sizeof(UShort));
401//::    } else
402//::       VG_(memcpy)(fpu, from, sizeof(*fpu));
403//:: }
404
405//::    fill_fpu(fpu, (const Char *)&arch->m_sse);
406
407#elif defined(VGP_amd64_linux)
408//::    fpu->cwd = ?;
409//::    fpu->swd = ?;
410//::    fpu->twd = ?;
411//::    fpu->fop = ?;
412//::    fpu->rip = ?;
413//::    fpu->rdp = ?;
414//::    fpu->mxcsr = ?;
415//::    fpu->mxcsr_mask = ?;
416//::    fpu->st_space = ?;
417
418#  define DO(n)  VG_(memcpy)(fpu->xmm_space + n * 4, \
419                             &arch->vex.guest_YMM##n[0], 16)
420   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
421   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
422#  undef DO
423
424   VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
425
426#elif defined(VGP_ppc32_linux)
427   /* The guest state has the FPR fields declared as ULongs, so need
428      to fish out the values without converting them.
429      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
430#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
431   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
432   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
433   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
434   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
435#  undef DO
436
437#elif defined(VGP_ppc64_linux)
438   /* The guest state has the FPR fields declared as ULongs, so need
439      to fish out the values without converting them.
440      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
441#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
442   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
443   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
444   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
445   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
446#  undef DO
447
448#elif defined(VGP_arm_linux)
449   // umm ...
450
451#elif defined(VGP_s390x_linux)
452#  define DO(n)  fpu->fprs[n].ui = arch->vex.guest_f##n
453   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
454   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
455# undef DO
456#elif defined(VGP_mips32_linux)
457#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_f##n)
458   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
459   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
460   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
461   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
462#  undef DO
463#else
464#  error Unknown ELF platform
465#endif
466}
467
468#if defined(VGP_x86_linux) && !defined(VGPV_x86_linux_android)
469static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
470{
471   ThreadArchState* arch = (ThreadArchState*)&tst->arch;
472
473//::    xfpu->cwd = ?;
474//::    xfpu->swd = ?;
475//::    xfpu->twd = ?;
476//::    xfpu->fop = ?;
477//::    xfpu->fip = ?;
478//::    xfpu->fcs = ?;
479//::    xfpu->foo = ?;
480//::    xfpu->fos = ?;
481//::    xfpu->mxcsr = ?;
482   xfpu->reserved = 0;
483//::    xfpu->st_space = ?;
484
485#  define DO(n)  VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
486   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
487#  undef DO
488
489   VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
490}
491#endif
492
493static
494void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
495{
496   Char* buf = NULL;
497   Char *basename = "vgcore";
498   Char *coreext = "";
499   Int seq = 0;
500   Int core_fd;
501   NSegment const * seg;
502   ESZ(Ehdr) ehdr;
503   ESZ(Phdr) *phdrs;
504   Int num_phdrs;
505   Int i, idx;
506   UInt off;
507   struct note *notelist, *note;
508   UInt notesz;
509   struct vki_elf_prpsinfo prpsinfo;
510   struct vki_elf_prstatus prstatus;
511   Addr *seg_starts;
512   Int n_seg_starts;
513
514   if (VG_(clo_log_fname_expanded) != NULL) {
515      coreext = ".core";
516      basename = VG_(expand_file_name)(
517                    "--log-file (while creating core filename)",
518                    VG_(clo_log_fname_expanded));
519   }
520
521   vg_assert(coreext);
522   vg_assert(basename);
523   buf = VG_(malloc)( "coredump-elf.mec.1",
524                      VG_(strlen)(coreext) + VG_(strlen)(basename)
525                         + 100/*for the two %ds. */ );
526   vg_assert(buf);
527
528   for(;;) {
529      Int oflags = VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC;
530      SysRes sres;
531
532      if (seq == 0)
533	 VG_(sprintf)(buf, "%s%s.%d",
534		      basename, coreext, VG_(getpid)());
535      else
536	 VG_(sprintf)(buf, "%s%s.%d.%d",
537		      basename, coreext, VG_(getpid)(), seq);
538      seq++;
539
540#     if defined(VKI_O_LARGEFILE)
541      oflags |= VKI_O_LARGEFILE;
542#     endif
543
544      sres = VG_(open)(buf, oflags, VKI_S_IRUSR|VKI_S_IWUSR);
545      if (!sr_isError(sres)) {
546         core_fd = sr_Res(sres);
547	 break;
548      }
549
550      if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
551	 return;		/* can't create file */
552   }
553
554   /* Get the segments */
555   seg_starts = VG_(get_segment_starts)(&n_seg_starts);
556
557   /* First, count how many memory segments to dump */
558   num_phdrs = 1;		/* start with notes */
559   for(i = 0; i < n_seg_starts; i++) {
560      if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
561	 continue;
562
563      num_phdrs++;
564   }
565
566   fill_ehdr(&ehdr, num_phdrs);
567
568   notelist = NULL;
569
570   /* Second, work out their layout */
571   phdrs = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.mec.1",
572                             sizeof(*phdrs) * num_phdrs);
573
574   for(i = 1; i < VG_N_THREADS; i++) {
575      vki_elf_fpregset_t  fpu;
576
577      if (VG_(threads)[i].status == VgTs_Empty)
578	 continue;
579
580#     if defined(VGP_x86_linux)
581#     if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
582      {
583         vki_elf_fpxregset_t xfpu;
584         fill_xfpu(&VG_(threads)[i], &xfpu);
585         add_note(&notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
586      }
587#     endif
588#     endif
589
590      fill_fpu(&VG_(threads)[i], &fpu);
591#     if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
592      add_note(&notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
593#     endif
594
595      fill_prstatus(&VG_(threads)[i], &prstatus, si);
596#     if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
597      add_note(&notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
598#     endif
599   }
600
601   fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
602#  if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
603   add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
604#  endif
605
606   for (note = notelist, notesz = 0; note != NULL; note = note->next)
607      notesz += note_size(note);
608
609   off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
610
611   phdrs[0].p_type = PT_NOTE;
612   phdrs[0].p_offset = off;
613   phdrs[0].p_vaddr = 0;
614   phdrs[0].p_paddr = 0;
615   phdrs[0].p_filesz = notesz;
616   phdrs[0].p_memsz = 0;
617   phdrs[0].p_flags = 0;
618   phdrs[0].p_align = 0;
619
620   off += notesz;
621
622   off = VG_PGROUNDUP(off);
623
624   for(i = 0, idx = 1; i < n_seg_starts; i++) {
625      seg = VG_(am_find_nsegment(seg_starts[i]));
626
627      if (!may_dump(seg))
628	 continue;
629
630      fill_phdr(&phdrs[idx], seg, off,
631                (seg->end - seg->start + off) < max_size);
632
633      off += phdrs[idx].p_filesz;
634
635      idx++;
636   }
637
638   /* write everything out */
639   VG_(write)(core_fd, &ehdr, sizeof(ehdr));
640   VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
641
642   for(note = notelist; note != NULL; note = note->next)
643      write_note(core_fd, note);
644
645   VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
646
647   for(i = 0, idx = 1; i < n_seg_starts; i++) {
648      seg = VG_(am_find_nsegment(seg_starts[i]));
649
650      if (!should_dump(seg))
651	 continue;
652
653      if (phdrs[idx].p_filesz > 0) {
654	 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET)
655                   == phdrs[idx].p_offset);
656	 vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz);
657
658	 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
659      }
660      idx++;
661   }
662
663   VG_(free)(seg_starts);
664
665   VG_(close)(core_fd);
666}
667
668void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
669{
670   make_elf_coredump(tid, si, max_size);
671}
672
673#endif // defined(VGO_linux)
674
675/*--------------------------------------------------------------------*/
676/*--- end                                                          ---*/
677/*--------------------------------------------------------------------*/
678