Skip to contentSkip to conversation

correct core dump format

PATCH In review Laurent Vivier 2 participants 4 messages Feb 13, 2011 – Feb 16, 2011

lore
Share & tools
lore
#0 lore raw

v4 cover correct core dump format

This is the v4 of my patch correcting the core dump format. (3 versions for patch 2, 3 versions for patch 1 starting at version 2 of patch 2...)

v4 adds some long alignments for 64bit targets,
renames target_long_long to target_llong, and so on...

v3 introduces a new parameter of the target: the datatype alignment size.

Targets like i386, mips or ppc align (short, int, long, long long) on (2, 4, 4, 8), target like x86_64 aligns on (2, 4, 8, 8)

but arm aligns on (2, 4, 4, 4) and m68k (680x0) on (2, 2, 2, 2).

And this knowledge is needed to correctly generate a core dump.

For other targets, please update the patch with your favorite one.

V2 introduces target_elf_prstatus alignment (to manage arm and m68k)

v1 corrects core dump for m68k

[PATCH 1/2][v3] linux-user: Define target alignment size [PATCH 2/2][v3] linux-user: correct core dump format

#1 lore raw

PATCH v3 1/2 linux-user: Define target alignment size

Datatype alignment can be found using following application:

int main(void)

{
	printf("alignof(short) %ld\n", __alignof__(short));
	printf("alignof(int) %ld\n", __alignof__(int));
	printf("alignof(long) %ld\n", __alignof__(long));
	printf("alignof(long long) %ld\n", __alignof__(long long));
}

This patch includes following alignments:

i386

alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 8

x86_64

alignof(short) 2
alignof(int) 4
alignof(long) 8
alignof(long long) 8

arm

alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 4

m68k (680x0)

alignof(short) 2
alignof(int) 2
alignof(long) 2
alignof(long long) 2

mips

alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 8

ppc

alignof(short) 2
alignof(int) 4
alignof(long) 4
alignof(long long) 8

for other targets, use by default (2,4,4,8).

Please, update for your favorite target...

---
v2: compute align size for each basic datatype
v3: add alignments for some 64bit targets, renanme long_long to llong

 configure  |   17 +++++++++++++++++
 cpu-defs.h |   14 ++++++++++----
 2 files changed, 27 insertions(+), 4 deletions(-)
configure +17 -0
index 25381e5..c36c553 100755
@@ -2919,6 +2919,10 @@ target_nptl="no"
 interp_prefix1=`echo "$interp_prefix" | sed "s/%M/$target_arch2/g"`
 echo "CONFIG_QEMU_INTERP_PREFIX=\"$interp_prefix1\"" >> $config_target_mak
 gdb_xml_files=""
+target_short_alignment=2
+target_int_alignment=4
+target_long_alignment=4
+target_llong_alignment=8
 
 TARGET_ARCH="$target_arch2"
 TARGET_BASE_ARCH=""
@@ -2931,9 +2935,11 @@ case "$target_arch2" in
   x86_64)
     TARGET_BASE_ARCH=i386
     target_phys_bits=64
+    target_long_alignment=8
   ;;
   alpha)
     target_phys_bits=64
+    target_long_alignment=8
     target_nptl="yes"
   ;;
   arm|armeb)
@@ -2942,6 +2948,7 @@ case "$target_arch2" in
     target_nptl="yes"
     gdb_xml_files="arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
     target_phys_bits=32
+    target_llong_alignment=4
   ;;
   cris)
     target_nptl="yes"
@@ -2951,6 +2958,9 @@ case "$target_arch2" in
     bflt="yes"
     gdb_xml_files="cf-core.xml cf-fp.xml"
     target_phys_bits=32
+    target_int_alignment=2
+    target_long_alignment=2
+    target_llong_alignment=2
   ;;
   microblaze)
     bflt="yes"
@@ -2974,6 +2984,7 @@ case "$target_arch2" in
     TARGET_BASE_ARCH=mips
     echo "TARGET_ABI_MIPSN64=y" >> $config_target_mak
     target_phys_bits=64
+    target_long_alignment=8
   ;;
   ppc)
     gdb_xml_files="power-core.xml power-fpu.xml power-altivec.xml power-spe.xml"
@@ -2992,6 +3003,7 @@ case "$target_arch2" in
     TARGET_ABI_DIR=ppc
     gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml"
     target_phys_bits=64
+    target_long_alignment=8
   ;;
   ppc64abi32)
     TARGET_ARCH=ppc64
@@ -3013,6 +3025,7 @@ case "$target_arch2" in
   sparc64)
     TARGET_BASE_ARCH=sparc
     target_phys_bits=64
+    target_long_alignment=8
   ;;
   sparc32plus)
     TARGET_ARCH=sparc64
@@ -3029,6 +3042,10 @@ case "$target_arch2" in
     exit 1
   ;;
 esac
+echo "TARGET_SHORT_ALIGNMENT=$target_short_alignment" >> $config_target_mak
+echo "TARGET_INT_ALIGNMENT=$target_int_alignment" >> $config_target_mak
+echo "TARGET_LONG_ALIGNMENT=$target_long_alignment" >> $config_target_mak
+echo "TARGET_LLONG_ALIGNMENT=$target_llong_alignment" >> $config_target_mak
 echo "TARGET_ARCH=$TARGET_ARCH" >> $config_target_mak
 target_arch_name="`echo $TARGET_ARCH | tr '[:lower:]' '[:upper:]'`"
 echo "TARGET_$target_arch_name=y" >> $config_target_mak
cpu-defs.h +10 -4
index 8d4bf86..37780e7 100644
@@ -37,16 +37,22 @@
 
 #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
 
+typedef int16_t target_short __attribute__ ((aligned(TARGET_SHORT_ALIGNMENT)));
+typedef uint16_t target_ushort __attribute__((aligned(TARGET_SHORT_ALIGNMENT)));
+typedef int32_t target_int __attribute__((aligned(TARGET_INT_ALIGNMENT)));
+typedef uint32_t target_uint __attribute__((aligned(TARGET_INT_ALIGNMENT)));
+typedef int64_t target_llong __attribute__((aligned(TARGET_LLONG_ALIGNMENT)));
+typedef uint64_t target_ullong __attribute__((aligned(TARGET_LLONG_ALIGNMENT)));
 /* target_ulong is the type of a virtual address */
 #if TARGET_LONG_SIZE == 4
-typedef int32_t target_long;
-typedef uint32_t target_ulong;
+typedef int32_t target_long __attribute__((aligned(TARGET_LONG_ALIGNMENT)));
+typedef uint32_t target_ulong __attribute__((aligned(TARGET_LONG_ALIGNMENT)));
 #define TARGET_FMT_lx "%08x"
 #define TARGET_FMT_ld "%d"
 #define TARGET_FMT_lu "%u"
 #elif TARGET_LONG_SIZE == 8
-typedef int64_t target_long;
-typedef uint64_t target_ulong;
+typedef int64_t target_long __attribute__((aligned(TARGET_LONG_ALIGNMENT)));
+typedef uint64_t target_ulong __attribute__((aligned(TARGET_LONG_ALIGNMENT)));
 #define TARGET_FMT_lx "%016" PRIx64
 #define TARGET_FMT_ld "%" PRId64
 #define TARGET_FMT_lu "%" PRIu64
Signature · 1 line
1.7.1
#2 lore raw

PATCH v3 2/2 linux-user: correct core dump format

In reply to Laurent Vivier

This patch allows to really use the core dumped by qemu with guest architecture tools.

- it adds a missing bswap_phdr() for the program headers of memory regions.

"objdump -x" sample:

BEFORE:

0x1000000 off 0x00200000 vaddr 0x00000400 paddr 0x00000000 align 2**21

         filesz 0x00000000 memsz 0x00100000 flags ---
0x1000000 off    0x00200000 vaddr 0x00100400 paddr 0x00000000 align 2**21
         filesz 0x00000000 memsz 0x00080000 flags --- 6000000

AFTER:

    LOAD off    0x00002000 vaddr 0x00040000 paddr 0x00000000 align 2**13
         filesz 0x00000000 memsz 0x00001000 flags ---
    LOAD off    0x00002000 vaddr 0x00041000 paddr 0x00000000 align 2**13
         filesz 0x00000000 memsz 0x00000800 flags rw-

- it doesn't pad the note size to sizeof(int32_t).
On m68k the NT_PRSTATUS note size is 154 and
must not be rounded up to 156, because this value is checked by objdump and gdb.

"gdb" symptoms:

"warning: Couldn't find general-purpose registers in core file."

"objdump -x" sample:

BEFORE:

Sections:

Idx Name          Size      VMA       LMA       File off  Algn
  0 note0         000001c4  00000000  00000000  000003b4  2**0

CONTENTS, READONLY
1 .auxv 00000070 00000000 00000000 00000508 2**2
CONTENTS
2 proc1 00100000 00000400 00000000 00200000 2**10
READONLY

AFTER:

Sections:

Idx Name          Size      VMA       LMA       File off  Algn
  0 note0         000001c4  00000000  00000000  000003b4  2**0

CONTENTS, READONLY
1 .reg/19022 00000050 00000000 00000000 0000040e 2**2
CONTENTS
2 .reg 00000050 00000000 00000000 0000040e 2**2
CONTENTS
3 .auxv 00000070 00000000 00000000 00000508 2**2
CONTENTS
4 load1 00000000 00040000 00000000 00002000 2**13
ALLOC, READONLY

---
v2: use a predefined alignment size for target_elf_prstatus
v3: use target_<type> aligned according target properties

 linux-user/elfload.c |   34 ++++++++++++++++++----------------
 1 files changed, 18 insertions(+), 16 deletions(-)
linux-user/elfload.c +18 -16
index 2de83e4..fe5410e 100644
@@ -103,13 +103,13 @@ enum {
 
 typedef target_ulong    target_elf_greg_t;
 #ifdef USE_UID16
-typedef uint16_t        target_uid_t;
-typedef uint16_t        target_gid_t;
+typedef target_ushort   target_uid_t;
+typedef target_ushort   target_gid_t;
 #else
-typedef uint32_t        target_uid_t;
-typedef uint32_t        target_gid_t;
+typedef target_uint     target_uid_t;
+typedef target_uint     target_gid_t;
 #endif
-typedef int32_t         target_pid_t;
+typedef target_int      target_pid_t;
 
 #ifdef TARGET_I386
 
@@ -1761,19 +1761,20 @@ struct memelfnote {
     size_t     namesz_rounded;
     int        type;
     size_t     datasz;
+    size_t     datasz_rounded;
     void       *data;
     size_t     notesz;
 };
 
 struct target_elf_siginfo {
-    int  si_signo; /* signal number */
-    int  si_code;  /* extra code */
-    int  si_errno; /* errno */
+    target_int  si_signo; /* signal number */
+    target_int  si_code;  /* extra code */
+    target_int  si_errno; /* errno */
 };
 
 struct target_elf_prstatus {
     struct target_elf_siginfo pr_info;      /* Info associated with signal */
-    short              pr_cursig;    /* Current signal */
+    target_short       pr_cursig;    /* Current signal */
     target_ulong       pr_sigpend;   /* XXX */
     target_ulong       pr_sighold;   /* XXX */
     target_pid_t       pr_pid;
@@ -1785,7 +1786,7 @@ struct target_elf_prstatus {
     struct target_timeval pr_cutime; /* XXX Cumulative user time */
     struct target_timeval pr_cstime; /* XXX Cumulative system time */
     target_elf_gregset_t      pr_reg;       /* GP registers */
-    int                pr_fpvalid;   /* XXX */
+    target_int         pr_fpvalid;   /* XXX */
 };
 
 #define ELF_PRARGSZ     (80) /* Number of chars for args */
@@ -2036,7 +2037,9 @@ static void fill_note(struct memelfnote *note, const char *name, int type,
     note->namesz = namesz;
     note->namesz_rounded = roundup(namesz, sizeof (int32_t));
     note->type = type;
-    note->datasz = roundup(sz, sizeof (int32_t));;
+    note->datasz = sz;
+    note->datasz_rounded = roundup(sz, sizeof (int32_t));
+
     note->data = data;
 
     /*
@@ -2044,7 +2047,7 @@ static void fill_note(struct memelfnote *note, const char *name, int type,
      * ELF document.
      */
     note->notesz = sizeof (struct elf_note) +
-        note->namesz_rounded + note->datasz;
+        note->namesz_rounded + note->datasz_rounded;
 }
 
 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
@@ -2264,7 +2267,7 @@ static int write_note(struct memelfnote *men, int fd)
         return (-1);
     if (dump_write(fd, men->name, men->namesz_rounded) != 0)
         return (-1);
-    if (dump_write(fd, men->data, men->datasz) != 0)
+    if (dump_write(fd, men->data, men->datasz_rounded) != 0)
         return (-1);
 
     return (0);
@@ -2480,7 +2483,7 @@ static int elf_core_dump(int signr, const CPUState *env)
      * ELF specification wants data to start at page boundary so
      * we align it here.
      */
-    offset = roundup(offset, ELF_EXEC_PAGESIZE);
+    data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
 
     /*
      * Write program headers for memory regions mapped in
@@ -2503,6 +2506,7 @@ static int elf_core_dump(int signr, const CPUState *env)
             phdr.p_flags |= PF_X;
         phdr.p_align = ELF_EXEC_PAGESIZE;
 
+        bswap_phdr(&phdr, 1);
         dump_write(fd, &phdr, sizeof (phdr));
     }
 
@@ -2514,8 +2518,6 @@ static int elf_core_dump(int signr, const CPUState *env)
         goto out;
 
     /* align data to page boundary */
-    data_offset = lseek(fd, 0, SEEK_CUR);
-    data_offset = TARGET_PAGE_ALIGN(data_offset);
     if (lseek(fd, data_offset, SEEK_SET) != data_offset)
         goto out;
 
Signature · 1 line
1.7.1
#3 lore raw
In reply to Laurent Vivier
Quoted 3 lines · Laurent Vivier

This is the v4 of my patch correcting the core dump format. (3 versions for patch 2, 3 versions for patch 1 starting at version 2 of patch 2...)

Thanks, tested to work and added to que.

Quoted 20 lines

v4 adds some long alignments for 64bit targets,
renames target_long_long to target_llong, and so on...

v3 introduces a new parameter of the target: the datatype alignment size.

Targets like i386, mips or ppc align (short, int, long, long long) on (2, 4, 4, 8), target like x86_64 aligns on (2, 4, 8, 8)

but arm aligns on (2, 4, 4, 4) and m68k (680x0) on (2, 2, 2, 2).

And this knowledge is needed to correctly generate a core dump.

For other targets, please update the patch with your favorite one.

V2 introduces target_elf_prstatus alignment (to manage arm and m68k)

v1 corrects core dump for m68k

[PATCH 1/2][v3] linux-user: Define target alignment size [PATCH 2/2][v3] linux-user: correct core dump format