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