>From 19c7d40ed70f4199b362cae91f755a4c037eb8d4 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mjw@redhat.com>
Date: Thu, 10 Apr 2014 15:02:53 +0200
Subject: [PATCH] readelf: convert up to a target long in buf_read_ulong.

Add a i686 corefile test.

Reported-by: Markus Engel <m_engel@cs.uni-kl.de>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 src/ChangeLog                       |    4 ++
 src/readelf.c                       |    2 +-
 tests/ChangeLog                     |    6 +++
 tests/Makefile.am                   |    2 +-
 tests/run-readelf-mixed-corenote.sh |   67 ++++++++++++++++++++++++++++++++++-
 tests/testfile_i686_core.bz2        |  Bin 0 -> 15980 bytes
 6 files changed, 78 insertions(+), 3 deletions(-)
 create mode 100644 tests/testfile_i686_core.bz2

diff --git a/src/ChangeLog b/src/ChangeLog
index c166267..a6218e2 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2014-04-10  Mark Wielaard  <mjw@redhat.com>
+
+	* readelf.c (buf_read_ulong): Pass actual long size to convert.
+
 2014-03-05  Mark Wielaard  <mjw@redhat.com>
 
 	* readelf.c (attr_callback): Print DW_FORM_sdata values as signed
diff --git a/src/readelf.c b/src/readelf.c
index d2efbae..ff8048e 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -8736,7 +8736,7 @@ buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
     uint32_t u32;
   } u;
 
-  *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sizeof u);
+  *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
 
   if (sz == 4)
     *retp = u.u32;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 9b0e7ac..bdba1d9 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@
+2014-04-10  Mark Wielaard  <mjw@redhat.com>
+
+	* testfile_i686_core.bz2: New test file.
+	* run-readelf-mixed-corenote.sh: Add testfile_i686_core test.
+	* Makefile.am (EXTRA_DIST): Add testfile_i686_core.bz2
+
 2014-03-11  Josh Stone  <jistone@redhat.com>
 
 	* testfilebaxmin.bz2: New testfile.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5e64095..04be7f0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -249,7 +249,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
 	     testfile_parameter_ref.c testfile_parameter_ref.bz2 \
 	     testfile_entry_value.c testfile_entry_value.bz2 \
 	     testfile_implicit_value.c testfile_implicit_value.bz2 \
-	     testfile_aarch64_core.bz2 \
+	     testfile_aarch64_core.bz2 testfile_i686_core.bz2 \
 	     run-funcretval.sh funcretval_test.c funcretval_test_aarch64.bz2 \
 	     run-backtrace-data.sh run-backtrace-dwarf.sh cleanup-13.c \
 	     run-backtrace-native.sh run-backtrace-native-biarch.sh \
diff --git a/tests/run-readelf-mixed-corenote.sh b/tests/run-readelf-mixed-corenote.sh
index c176e28..01e4594 100755
--- a/tests/run-readelf-mixed-corenote.sh
+++ b/tests/run-readelf-mixed-corenote.sh
@@ -1,5 +1,5 @@
 #! /bin/sh
-# Copyright (C) 2012, 2013 Red Hat, Inc.
+# Copyright (C) 2012, 2013, 2014 Red Hat, Inc.
 # This file is part of elfutils.
 #
 # This file is free software; you can redistribute it and/or modify
@@ -425,4 +425,69 @@ Note segment of 2512 bytes at offset 0x270:
     DBGWVR15_EL1: 0x0000000000000000, DBGWCR15_EL1: 0x00000000
 EOF
 
+# To reproduce this core dump, do this on an i686 machine:
+# $ gcc -x c <(echo 'int main () { return *(int *)0x12345678; }')
+# $ ./a.out
+testfiles testfile_i686_core
+testrun_compare ${abs_top_builddir}/src/readelf -n testfile_i686_core <<\EOF
+
+Note segment of 1000 bytes at offset 0x214:
+  Owner          Data size  Type
+  CORE                 144  PRSTATUS
+    info.si_signo: 11, info.si_code: 0, info.si_errno: 0, cursig: 11
+    sigpend: <>
+    sighold: <>
+    pid: 27395, ppid: 1130, pgrp: 27395, sid: 1130
+    utime: 0.000000, stime: 0.001000, cutime: 0.000000, cstime: 0.000000
+    orig_eax: -1, fpvalid: 0
+    ebx:     1334976512  ecx:    -1239415396  edx:    -1079283900
+    esi:              0  edi:              0  ebp:     0xbfab6f18
+    eax:      305419896  eip:     0x08048408  eflags:  0x00010246
+    esp:     0xbfab6f18
+    ds: 0x007b  es: 0x007b  fs: 0x0000  gs: 0x0033  cs: 0x0073  ss: 0x007b
+  CORE                 124  PRPSINFO
+    state: 0, sname: R, zomb: 0, nice: 0, flag: 0x00000200
+    uid: 1000, gid: 1000, pid: 27395, ppid: 1130, pgrp: 27395, sid: 1130
+    fname: a.out, psargs: ./a.out 
+  CORE                 128  SIGINFO
+    si_signo: 11, si_errno: 0, si_code: 1
+    fault address: 0x12345678
+  CORE                 160  AUXV
+    SYSINFO: 0xb77fc414
+    SYSINFO_EHDR: 0xb77fc000
+    HWCAP: 0x780abfd  <fpu de pse tsc msr pae mce cx8 apic sep pge cmov mmx fxsr sse sse2>
+    PAGESZ: 4096
+    CLKTCK: 100
+    PHDR: 0x8048034
+    PHENT: 32
+    PHNUM: 9
+    BASE: 0
+    FLAGS: 0
+    ENTRY: 0x80482f0
+    UID: 1000
+    EUID: 1000
+    GID: 1000
+    EGID: 1000
+    SECURE: 0
+    RANDOM: 0xbfab70eb
+    EXECFN: 0xbfab7ff4
+    PLATFORM: 0xbfab70fb
+    NULL
+  CORE                 275  FILE
+    9 files:
+      08048000-08049000 00000000 4096                /tmp/a.out
+      08049000-0804a000 00000000 4096                /tmp/a.out
+      0804a000-0804b000 00001000 4096                /tmp/a.out
+      4f744000-4f763000 00000000 126976              /usr/lib/ld-2.18.so
+      4f764000-4f765000 0001f000 4096                /usr/lib/ld-2.18.so
+      4f765000-4f766000 00020000 4096                /usr/lib/ld-2.18.so
+      4f768000-4f920000 00000000 1802240             /usr/lib/libc-2.18.so
+      4f920000-4f922000 001b8000 8192                /usr/lib/libc-2.18.so
+      4f922000-4f923000 001ba000 4096                /usr/lib/libc-2.18.so
+  LINUX                 48  386_TLS
+    index: 6, base: 0xb77da700, limit: 0x000fffff, flags: 0x00000051
+    index: 7, base: 0x00000000, limit: 0x00000000, flags: 0x00000028
+    index: 8, base: 0x00000000, limit: 0x00000000, flags: 0x00000028
+EOF
+
 exit 0
diff --git a/tests/testfile_i686_core.bz2 b/tests/testfile_i686_core.bz2
new file mode 100644
index 0000000000000000000000000000000000000000..8412776ee99c8b305833af03efd983e903f4279f
GIT binary patch
-- 
1.7.1

