coredump-elf.c revision 5d289698a0d8942d852eccb6b26823a2ab029858
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 */
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)
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   Char 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)
163static void add_note(struct note **list, const Char *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_arm_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 Char 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      Char *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   ThreadArchState* arch = (ThreadArchState*)&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( &((ThreadArchState*)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( &((ThreadArchState*)arch)->vex );
321   regs->ccr = LibVEX_GuestPPC32_get_CR( &((ThreadArchState*)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( &((ThreadArchState*)arch)->vex );
342   regs->ccr = LibVEX_GuestPPC64_get_CR( &((ThreadArchState*)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( &((ThreadArchState*)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#else
379#  error Unknown ELF platform
380#endif
381}
382
383static void fill_fpu(const ThreadState *tst, vki_elf_fpregset_t *fpu)
384{
385   __attribute__((unused))
386   ThreadArchState* arch = (ThreadArchState*)&tst->arch;
387
388#if defined(VGP_x86_linux)
389//:: static void fill_fpu(vki_elf_fpregset_t *fpu, const Char *from)
390//:: {
391//::    if (VG_(have_ssestate)) {
392//::       UShort *to;
393//::       Int i;
394//::
395//::       /* This is what the kernel does */
396//::       VG_(memcpy)(fpu, from, 7*sizeof(long));
397//::
398//::       to = (UShort *)&fpu->st_space[0];
399//::       from += 18 * sizeof(UShort);
400//::
401//::       for (i = 0; i < 8; i++, to += 5, from += 8)
402//:: 	 VG_(memcpy)(to, from, 5*sizeof(UShort));
403//::    } else
404//::       VG_(memcpy)(fpu, from, sizeof(*fpu));
405//:: }
406
407//::    fill_fpu(fpu, (const Char *)&arch->m_sse);
408
409#elif defined(VGP_amd64_linux)
410//::    fpu->cwd = ?;
411//::    fpu->swd = ?;
412//::    fpu->twd = ?;
413//::    fpu->fop = ?;
414//::    fpu->rip = ?;
415//::    fpu->rdp = ?;
416//::    fpu->mxcsr = ?;
417//::    fpu->mxcsr_mask = ?;
418//::    fpu->st_space = ?;
419
420#  define DO(n)  VG_(memcpy)(fpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
421   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
422   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
423#  undef DO
424
425   VG_(memset)(fpu->padding, 0, sizeof(fpu->padding));
426
427#elif defined(VGP_ppc32_linux)
428   /* The guest state has the FPR fields declared as ULongs, so need
429      to fish out the values without converting them.
430      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
431#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
432   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
433   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
434   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
435   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
436#  undef DO
437
438#elif defined(VGP_ppc64_linux)
439   /* The guest state has the FPR fields declared as ULongs, so need
440      to fish out the values without converting them.
441      NOTE: The 32 FP registers map to the first 32 VSX registers.*/
442#  define DO(n)  (*fpu)[n] = *(double*)(&arch->vex.guest_VSR##n)
443   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
444   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
445   DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23);
446   DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31);
447#  undef DO
448
449#elif defined(VGP_arm_linux)
450   // umm ...
451
452#elif defined(VGP_s390x_linux)
453#  define DO(n)  fpu->fprs[n].ui = arch->vex.guest_f##n
454   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
455   DO(8);  DO(9);  DO(10); DO(11); DO(12); DO(13); DO(14); DO(15);
456# undef DO
457#else
458#  error Unknown ELF platform
459#endif
460}
461
462#if defined(VGP_x86_linux)
463static void fill_xfpu(const ThreadState *tst, vki_elf_fpxregset_t *xfpu)
464{
465   ThreadArchState* arch = (ThreadArchState*)&tst->arch;
466
467//::    xfpu->cwd = ?;
468//::    xfpu->swd = ?;
469//::    xfpu->twd = ?;
470//::    xfpu->fop = ?;
471//::    xfpu->fip = ?;
472//::    xfpu->fcs = ?;
473//::    xfpu->foo = ?;
474//::    xfpu->fos = ?;
475//::    xfpu->mxcsr = ?;
476   xfpu->reserved = 0;
477//::    xfpu->st_space = ?;
478
479#  define DO(n)  VG_(memcpy)(xfpu->xmm_space + n * 4, &arch->vex.guest_XMM##n, sizeof(arch->vex.guest_XMM##n))
480   DO(0);  DO(1);  DO(2);  DO(3);  DO(4);  DO(5);  DO(6);  DO(7);
481#  undef DO
482
483   VG_(memset)(xfpu->padding, 0, sizeof(xfpu->padding));
484}
485#endif
486
487static
488void make_elf_coredump(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
489{
490   Char* buf = NULL;
491   Char *basename = "vgcore";
492   Char *coreext = "";
493   Int seq = 0;
494   Int core_fd;
495   NSegment const * seg;
496   ESZ(Ehdr) ehdr;
497   ESZ(Phdr) *phdrs;
498   Int num_phdrs;
499   Int i, idx;
500   UInt off;
501   struct note *notelist, *note;
502   UInt notesz;
503   struct vki_elf_prpsinfo prpsinfo;
504   struct vki_elf_prstatus prstatus;
505   Addr *seg_starts;
506   Int n_seg_starts;
507
508   if (VG_(clo_log_fname_expanded) != NULL) {
509      coreext = ".core";
510      basename = VG_(expand_file_name)(
511                    "--log-file (while creating core filename)",
512                    VG_(clo_log_fname_expanded));
513   }
514
515   vg_assert(coreext);
516   vg_assert(basename);
517   buf = VG_(malloc)( "coredump-elf.mec.1",
518                      VG_(strlen)(coreext) + VG_(strlen)(basename)
519                         + 100/*for the two %ds. */ );
520   vg_assert(buf);
521
522   for(;;) {
523      SysRes sres;
524
525      if (seq == 0)
526	 VG_(sprintf)(buf, "%s%s.%d",
527		      basename, coreext, VG_(getpid)());
528      else
529	 VG_(sprintf)(buf, "%s%s.%d.%d",
530		      basename, coreext, VG_(getpid)(), seq);
531      seq++;
532
533      sres = VG_(open)(buf,
534                       VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC,
535                       VKI_S_IRUSR|VKI_S_IWUSR);
536      if (!sr_isError(sres)) {
537         core_fd = sr_Res(sres);
538	 break;
539      }
540
541      if (sr_isError(sres) && sr_Err(sres) != VKI_EEXIST)
542	 return;		/* can't create file */
543   }
544
545   /* Get the segments */
546   seg_starts = VG_(get_segment_starts)(&n_seg_starts);
547
548   /* First, count how many memory segments to dump */
549   num_phdrs = 1;		/* start with notes */
550   for(i = 0; i < n_seg_starts; i++) {
551      if (!may_dump(VG_(am_find_nsegment(seg_starts[i]))))
552	 continue;
553
554      num_phdrs++;
555   }
556
557   fill_ehdr(&ehdr, num_phdrs);
558
559   notelist = NULL;
560
561   /* Second, work out their layout */
562   phdrs = VG_(arena_malloc)(VG_AR_CORE, "coredump-elf.mec.1",
563                             sizeof(*phdrs) * num_phdrs);
564
565   for(i = 1; i < VG_N_THREADS; i++) {
566      vki_elf_fpregset_t  fpu;
567
568      if (VG_(threads)[i].status == VgTs_Empty)
569	 continue;
570
571#     if defined(VGP_x86_linux)
572      {
573         vki_elf_fpxregset_t xfpu;
574         fill_xfpu(&VG_(threads)[i], &xfpu);
575         add_note(&notelist, "LINUX", NT_PRXFPREG, &xfpu, sizeof(xfpu));
576      }
577#     endif
578
579      fill_fpu(&VG_(threads)[i], &fpu);
580#     if !defined(VGPV_arm_linux_android)
581      add_note(&notelist, "CORE", NT_FPREGSET, &fpu, sizeof(fpu));
582#     endif
583
584      fill_prstatus(&VG_(threads)[i], &prstatus, si);
585#     if !defined(VGPV_arm_linux_android)
586      add_note(&notelist, "CORE", NT_PRSTATUS, &prstatus, sizeof(prstatus));
587#     endif
588   }
589
590   fill_prpsinfo(&VG_(threads)[tid], &prpsinfo);
591#  if !defined(VGPV_arm_linux_android)
592   add_note(&notelist, "CORE", NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
593#  endif
594
595   for (note = notelist, notesz = 0; note != NULL; note = note->next)
596      notesz += note_size(note);
597
598   off = sizeof(ehdr) + sizeof(*phdrs) * num_phdrs;
599
600   phdrs[0].p_type = PT_NOTE;
601   phdrs[0].p_offset = off;
602   phdrs[0].p_vaddr = 0;
603   phdrs[0].p_paddr = 0;
604   phdrs[0].p_filesz = notesz;
605   phdrs[0].p_memsz = 0;
606   phdrs[0].p_flags = 0;
607   phdrs[0].p_align = 0;
608
609   off += notesz;
610
611   off = VG_PGROUNDUP(off);
612
613   for(i = 0, idx = 1; i < n_seg_starts; i++) {
614      seg = VG_(am_find_nsegment(seg_starts[i]));
615
616      if (!may_dump(seg))
617	 continue;
618
619      fill_phdr(&phdrs[idx], seg, off,
620                (seg->end - seg->start + off) < max_size);
621
622      off += phdrs[idx].p_filesz;
623
624      idx++;
625   }
626
627   /* write everything out */
628   VG_(write)(core_fd, &ehdr, sizeof(ehdr));
629   VG_(write)(core_fd, phdrs, sizeof(*phdrs) * num_phdrs);
630
631   for(note = notelist; note != NULL; note = note->next)
632      write_note(core_fd, note);
633
634   VG_(lseek)(core_fd, phdrs[1].p_offset, VKI_SEEK_SET);
635
636   for(i = 0, idx = 1; i < n_seg_starts; i++) {
637      seg = VG_(am_find_nsegment(seg_starts[i]));
638
639      if (!should_dump(seg))
640	 continue;
641
642      if (phdrs[idx].p_filesz > 0) {
643	 vg_assert(VG_(lseek)(core_fd, phdrs[idx].p_offset, VKI_SEEK_SET)
644                   == phdrs[idx].p_offset);
645	 vg_assert(seg->end - seg->start >= phdrs[idx].p_filesz);
646
647	 (void)VG_(write)(core_fd, (void *)seg->start, phdrs[idx].p_filesz);
648      }
649      idx++;
650   }
651
652   VG_(free)(seg_starts);
653
654   VG_(close)(core_fd);
655}
656
657void VG_(make_coredump)(ThreadId tid, const vki_siginfo_t *si, UInt max_size)
658{
659   make_elf_coredump(tid, si, max_size);
660}
661
662#endif // defined(VGO_linux)
663
664/*--------------------------------------------------------------------*/
665/*--- end                                                          ---*/
666/*--------------------------------------------------------------------*/
667