Skip to contentSkip to conversation

contrib/elf2dmp: elf2dmp for Windows hosts

PATCH Applied Viktor Prutyanov 2 participants 8 messages Dec 20, 2018 – Jan 15, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

v2 cover contrib/elf2dmp: elf2dmp for Windows hosts

In most cases, it is more convenient to convert a dump on the same machine on which the analysis is performed. Because of WinDbg, the analysis of guest Windows problems needs Windows host anyway, so it is useful to have dump convertion tool near the debugger.
After these patches elf2dmp can be built both for Linux and Windows (x86 and x86_64) hosts.

Viktor Prutyanov (6):
contrib/elf2dmp: fix elf.h including contrib/elf2dmp: use GLib in ELF processing contrib/elf2dmp: use GLib in PDB processing contrib/elf2dmp: fix structures definitions contrib/elf2dmp: fix printf format
configure: enable elf2dmp build for Windows hosts

 Makefile                   |  4 ++--
 configure                  |  4 ++--
 contrib/elf2dmp/kdbg.h     | 12 ++++++++----
 contrib/elf2dmp/main.c     | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c      | 33 +++++++++++----------------------
 contrib/elf2dmp/pdb.h      |  4 +++-
 contrib/elf2dmp/pe.h       |  6 ++++--
 contrib/elf2dmp/qemu_elf.c | 27 ++++++++-------------------
 contrib/elf2dmp/qemu_elf.h | 10 +++++-----
 9 files changed, 58 insertions(+), 69 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH v2 1/6 contrib/elf2dmp: fix elf.h including

Before this patch QEMU elf.h was not actually included.

---
 contrib/elf2dmp/qemu_elf.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
contrib/elf2dmp/qemu_elf.h +4 -4
index d85d6558fa..19d1299954 100644
@@ -5,11 +5,11 @@
  *
  */
 
-#ifndef QEMU_ELF_H
-#define QEMU_ELF_H
+#ifndef ELF2DMP_ELF_H
+#define ELF2DMP_ELF_H
 
 #include <stdint.h>
-#include <elf.h>
+#include "elf.h"
 
 typedef struct QEMUCPUSegment {
     uint32_t selector;
@@ -48,4 +48,4 @@ void QEMU_Elf_exit(QEMU_Elf *qe);
 Elf64_Phdr *elf64_getphdr(void *map);
 Elf64_Half elf_getphdrnum(void *map);
 
-#endif /* QEMU_ELF_H */
+#endif /* ELF2DMP_ELF_H */
Signature · 1 line
2.17.2
#2 lore raw

PATCH v2 2/6 contrib/elf2dmp: use GLib in ELF processing

In reply to Viktor Prutyanov

Replace POSIX mmap with GLib g_mapped_file_new in ELF processing module to make elf2dmp cross-platform.

---
 contrib/elf2dmp/qemu_elf.c | 27 ++++++++-------------------
 contrib/elf2dmp/qemu_elf.h |  2 +-
 2 files changed, 9 insertions(+), 20 deletions(-)
contrib/elf2dmp/qemu_elf.c +8 -19
index e9c0d2534a..0db7816586 100644
@@ -120,25 +120,17 @@ static void exit_states(QEMU_Elf *qe)
 
 int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
 {
+    GError *gerr = NULL;
     int err = 0;
-    struct stat st;
 
-    qe->fd = open(filename, O_RDONLY, 0);
-    if (qe->fd == -1) {
-        eprintf("Failed to open ELF dump file \'%s\'\n", filename);
+    qe->gmf = g_mapped_file_new(filename, TRUE, &gerr);
+    if (gerr) {
+        eprintf("Failed to map ELF dump file \'%s\'\n", filename);
         return 1;
     }
 
-    fstat(qe->fd, &st);
-    qe->size = st.st_size;
-
-    qe->map = mmap(NULL, qe->size, PROT_READ | PROT_WRITE,
-            MAP_PRIVATE, qe->fd, 0);
-    if (qe->map == MAP_FAILED) {
-        eprintf("Failed to map ELF file\n");
-        err = 1;
-        goto out_fd;
-    }
+    qe->map = g_mapped_file_get_contents(qe->gmf);
+    qe->size = g_mapped_file_get_length(qe->gmf);
 
     if (init_states(qe)) {
         eprintf("Failed to extract QEMU CPU states\n");
@@ -149,9 +141,7 @@ int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
     return 0;
 
 out_unmap:
-    munmap(qe->map, qe->size);
-out_fd:
-    close(qe->fd);
+    g_mapped_file_unref(qe->gmf);
 
     return err;
 }
@@ -159,6 +149,5 @@ out_fd:
 void QEMU_Elf_exit(QEMU_Elf *qe)
 {
     exit_states(qe);
-    munmap(qe->map, qe->size);
-    close(qe->fd);
+    g_mapped_file_unref(qe->gmf);
 }
contrib/elf2dmp/qemu_elf.h +1 -1
index 19d1299954..fc69606d00 100644
@@ -34,7 +34,7 @@ typedef struct QEMUCPUState {
 int is_system(QEMUCPUState *s);
 
 typedef struct QEMU_Elf {
-    int fd;
+    GMappedFile *gmf;
     size_t size;
     void *map;
     QEMUCPUState **state;
Signature · 1 line
2.17.2
#3 lore raw

PATCH v2 3/6 contrib/elf2dmp: use GLib in PDB processing

In reply to Viktor Prutyanov

Replace POSIX mmap with GLib g_mapped_file_new in PDB processing stage to make elf2dmp cross-platform. There are no direct POSIX in elf2dmp after this patch.

---
 contrib/elf2dmp/pdb.c | 29 ++++++++---------------------
 contrib/elf2dmp/pdb.h |  2 +-
 2 files changed, 9 insertions(+), 22 deletions(-)
contrib/elf2dmp/pdb.c +8 -21
index bcb01b414f..52e352df79 100644
@@ -277,28 +277,18 @@ static void pdb_reader_exit(struct pdb_reader *r)
 
 int pdb_init_from_file(const char *name, struct pdb_reader *reader)
 {
+    GError *gerr = NULL;
     int err = 0;
-    int fd;
     void *map;
-    struct stat st;
 
-    fd = open(name, O_RDONLY, 0);
-    if (fd == -1) {
-        eprintf("Failed to open PDB file \'%s\'\n", name);
+    reader->gmf = g_mapped_file_new(name, TRUE, &gerr);
+    if (gerr) {
+        eprintf("Failed to map PDB file \'%s\'\n", name);
         return 1;
     }
-    reader->fd = fd;
-
-    fstat(fd, &st);
-    reader->file_size = st.st_size;
-
-    map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-    if (map == MAP_FAILED) {
-        eprintf("Failed to map PDB file\n");
-        err = 1;
-        goto out_fd;
-    }
 
+    reader->file_size = g_mapped_file_get_length(reader->gmf);
+    map = g_mapped_file_get_contents(reader->gmf);
     if (pdb_reader_init(reader, map)) {
         err = 1;
         goto out_unmap;
@@ -307,16 +297,13 @@ int pdb_init_from_file(const char *name, struct pdb_reader *reader)
     return 0;
 
 out_unmap:
-    munmap(map, st.st_size);
-out_fd:
-    close(fd);
+    g_mapped_file_unref(reader->gmf);
 
     return err;
 }
 
 void pdb_exit(struct pdb_reader *reader)
 {
-    munmap(reader->ds.header, reader->file_size);
-    close(reader->fd);
+    g_mapped_file_unref(reader->gmf);
     pdb_reader_exit(reader);
 }
contrib/elf2dmp/pdb.h +1 -1
index 4351a2dd61..8e395119d1 100644
@@ -218,7 +218,7 @@ typedef struct pdb_seg {
 #define IMAGE_FILE_MACHINE_AMD64 0x8664
 
 struct pdb_reader {
-    int fd;
+    GMappedFile *gmf;
     size_t file_size;
     struct {
         PDB_DS_HEADER *header;
Signature · 1 line
2.17.2
#4 lore raw

PATCH v2 4/6 contrib/elf2dmp: fix structures definitions

In reply to Viktor Prutyanov

Remove duplicate structures definitions in case of build for Windows hosts.

---
 contrib/elf2dmp/kdbg.h | 12 ++++++++----
 contrib/elf2dmp/pdb.h  |  2 ++
 contrib/elf2dmp/pe.h   |  6 ++++--
 3 files changed, 14 insertions(+), 6 deletions(-)
contrib/elf2dmp/kdbg.h +8 -4
index 851b57c321..002e3d0cd5 100644
@@ -25,11 +25,15 @@ typedef struct DBGKD_GET_VERSION64 {
     uint64_t DebuggerDataList;
 } DBGKD_GET_VERSION64;
 
+#ifndef _WIN32
+typedef struct LIST_ENTRY64 {
+    struct LIST_ENTRY64 *Flink;
+    struct LIST_ENTRY64 *Blink;
+} LIST_ENTRY64;
+#endif
+
 typedef struct DBGKD_DEBUG_DATA_HEADER64 {
-    struct LIST_ENTRY64 {
-       struct LIST_ENTRY64 *Flink;
-       struct LIST_ENTRY64 *Blink;
-    } List;
+    LIST_ENTRY64    List;
     uint32_t           OwnerTag;
     uint32_t           Size;
 } DBGKD_DEBUG_DATA_HEADER64;
contrib/elf2dmp/pdb.h +2 -0
index 8e395119d1..9a848f75e2 100644
@@ -11,12 +11,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#ifndef _WIN32
 typedef struct GUID {
     unsigned int Data1;
     unsigned short Data2;
     unsigned short Data3;
     unsigned char Data4[8];
 } GUID;
+#endif
 
 struct PDB_FILE {
     uint32_t size;
contrib/elf2dmp/pe.h +4 -2
index 374e06a9c5..0fd5c23c7f 100644
@@ -10,6 +10,7 @@
 
 #include <stdint.h>
 
+#ifndef _WIN32
 typedef struct IMAGE_DOS_HEADER {
     uint16_t  e_magic;      /* 0x00: MZ Header signature */
     uint16_t  e_cblp;       /* 0x02: Bytes on last page of file */
@@ -88,8 +89,6 @@ typedef struct IMAGE_NT_HEADERS64 {
   IMAGE_OPTIONAL_HEADER64 OptionalHeader;
 } __attribute__ ((packed)) IMAGE_NT_HEADERS64;
 
-#define IMAGE_FILE_DEBUG_DIRECTORY  6
-
 typedef struct IMAGE_DEBUG_DIRECTORY {
   uint32_t Characteristics;
   uint32_t TimeDateStamp;
@@ -102,6 +101,9 @@ typedef struct IMAGE_DEBUG_DIRECTORY {
 } __attribute__ ((packed)) IMAGE_DEBUG_DIRECTORY;
 
 #define IMAGE_DEBUG_TYPE_CODEVIEW   2
+#endif
+
+#define IMAGE_FILE_DEBUG_DIRECTORY  6
 
 typedef struct guid_t {
     uint32_t a;
Signature · 1 line
2.17.2
#5 lore raw

PATCH v2 5/6 contrib/elf2dmp: fix printf format

In reply to Viktor Prutyanov

Format strings for printf are changed for successful build for Windows hosts.

---
 contrib/elf2dmp/main.c | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c  |  4 +++-
 2 files changed, 18 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c +15 -12
index 9b93dab662..fdafb54900 100644
@@ -5,6 +5,8 @@
  *
  */
 
+#include <inttypes.h>
+
 #include "qemu/osdep.h"
 #include "err.h"
 #include "addrspace.h"
@@ -41,7 +43,8 @@ static const uint64_t SharedUserData = 0xfffff78000000000;
 #define KUSD_OFFSET_PRODUCT_TYPE 0x264
 
 #define SYM_RESOLVE(base, r, s) ((s = pdb_resolve(base, r, #s)),\
-    s ? printf(#s" = 0x%016lx\n", s) : eprintf("Failed to resolve "#s"\n"), s)
+    s ? printf(#s" = 0x%016"PRIx64"\n", s) :\
+    eprintf("Failed to resolve "#s"\n"), s)
 
 static uint64_t rol(uint64_t x, uint64_t y)
 {
@@ -98,8 +101,8 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb,
             return NULL;
         }
 
-        printf("[KiWaitNever] = 0x%016lx\n", kwn);
-        printf("[KiWaitAlways] = 0x%016lx\n", kwa);
+        printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
+        printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);
 
         /*
          * If KDBG header can be decoded, KDBG size is available
@@ -202,7 +205,7 @@ static int fix_dtb(struct va_space *vs, QEMU_Elf *qe)
 
         if (is_system(s)) {
             va_space_set_dtb(vs, s->cr[3]);
-            printf("DTB 0x%016lx has been found from CPU #%zu"
+            printf("DTB 0x%016"PRIx64" has been found from CPU #%zu"
                     " as system task CR3\n", vs->dtb, i);
             return !(va_space_resolve(vs, SharedUserData));
         }
@@ -222,7 +225,7 @@ static int fix_dtb(struct va_space *vs, QEMU_Elf *qe)
         }
 
         va_space_set_dtb(vs, *cr3);
-        printf("DirectoryTableBase = 0x%016lx has been found from CPU #0"
+        printf("DirectoryTableBase = 0x%016"PRIx64" has been found from CPU #0"
                 " as interrupt handling CR3\n", vs->dtb);
         return !(va_space_resolve(vs, SharedUserData));
     }
@@ -393,8 +396,8 @@ static int pe_get_pdb_symstore_hash(uint64_t base, void *start_addr,
         return 1;
     }
 
-    printf("Debug Directory RVA = 0x%016x\n",
-            data_dir[IMAGE_FILE_DEBUG_DIRECTORY].VirtualAddress);
+    printf("Debug Directory RVA = 0x%08"PRIx32"\n",
+            (uint32_t)data_dir[IMAGE_FILE_DEBUG_DIRECTORY].VirtualAddress);
 
     if (va_space_rw(vs,
                 base + data_dir[IMAGE_FILE_DEBUG_DIRECTORY].VirtualAddress,
@@ -488,7 +491,7 @@ int main(int argc, char *argv[])
     }
 
     state = qemu_elf.state[0];
-    printf("CPU #0 CR3 is 0x%016lx\n", state->cr[3]);
+    printf("CPU #0 CR3 is 0x%016"PRIx64"\n", state->cr[3]);
 
     va_space_create(&vs, &ps, state->cr[3]);
     if (fix_dtb(&vs, &qemu_elf)) {
@@ -497,7 +500,7 @@ int main(int argc, char *argv[])
         goto out_elf;
     }
 
-    printf("CPU #0 IDT is at 0x%016lx\n", state->idt.base);
+    printf("CPU #0 IDT is at 0x%016"PRIx64"\n", state->idt.base);
 
     if (va_space_rw(&vs, state->idt.base,
                 &first_idt_desc, sizeof(first_idt_desc), 0)) {
@@ -505,10 +508,10 @@ int main(int argc, char *argv[])
         err = 1;
         goto out_ps;
     }
-    printf("CPU #0 IDT[0] -> 0x%016lx\n", idt_desc_addr(first_idt_desc));
+    printf("CPU #0 IDT[0] -> 0x%016"PRIx64"\n", idt_desc_addr(first_idt_desc));
 
     KernBase = idt_desc_addr(first_idt_desc) & ~(PAGE_SIZE - 1);
-    printf("Searching kernel downwards from 0x%16lx...\n", KernBase);
+    printf("Searching kernel downwards from 0x%016"PRIx64"...\n", KernBase);
 
     for (; KernBase >= 0xfffff78000000000; KernBase -= PAGE_SIZE) {
         nt_start_addr = va_space_resolve(&vs, KernBase);
@@ -521,7 +524,7 @@ int main(int argc, char *argv[])
         }
     }
 
-    printf("KernBase = 0x%16lx, signature is \'%.2s\'\n", KernBase,
+    printf("KernBase = 0x%016"PRIx64", signature is \'%.2s\'\n", KernBase,
             (char *)nt_start_addr);
 
     if (pe_get_pdb_symstore_hash(KernBase, nt_start_addr, pdb_hash, &vs)) {
contrib/elf2dmp/pdb.c +3 -1
index 52e352df79..64af20f584 100644
@@ -18,6 +18,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include <inttypes.h>
+
 #include "qemu/osdep.h"
 #include "pdb.h"
 #include "err.h"
@@ -66,7 +68,7 @@ uint64_t pdb_find_public_v3_symbol(struct pdb_reader *r, const char *name)
             uint32_t sect_rva = segment->dword[1];
             uint64_t rva = sect_rva + sym->public_v3.offset;
 
-            printf("%s: 0x%016x(%d:\'%.8s\') + 0x%08x = 0x%09lx\n", name,
+            printf("%s: 0x%016x(%d:\'%.8s\') + 0x%08x = 0x%09"PRIx64"\n", name,
                     sect_rva, sym->public_v3.segment,
                     ((char *)segment - 8), sym->public_v3.offset, rva);
             return rva;
Signature · 1 line
2.17.2
#6 lore raw

PATCH v2 6/6 configure: enable elf2dmp build for Windows hosts

In reply to Viktor Prutyanov

After this patch contrib/elf2dmp can be built for Windows x86 and x86_64 hosts by mingw.

---
 Makefile  | 4 ++--
 configure | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Makefile +2 -2
index c8b9efdad4..25acb94aa6 100644
@@ -565,8 +565,8 @@ ifneq ($(EXESUF),)
 qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI)
 endif
 
-elf2dmp: LIBS = $(CURL_LIBS)
-elf2dmp: $(elf2dmp-obj-y)
+elf2dmp$(EXESUF): LIBS += $(CURL_LIBS)
+elf2dmp$(EXESUF): $(elf2dmp-obj-y)
 	$(call LINK, $^)
 
 ifdef CONFIG_IVSHMEM
configure +2 -2
index 224d3071ac..686ae2e093 100755
@@ -5753,8 +5753,8 @@ if test "$want_tools" = "yes" ; then
   if [ "$ivshmem" = "yes" ]; then
     tools="ivshmem-client\$(EXESUF) ivshmem-server\$(EXESUF) $tools"
   fi
-  if [ "$posix" = "yes" ] && [ "$curl" = "yes" ]; then
-    tools="elf2dmp $tools"
+  if [ "$curl" = "yes" ]; then
+      tools="elf2dmp\$(EXESUF) $tools"
   fi
 fi
 if test "$softmmu" = yes ; then
Signature · 1 line
2.17.2
#7 lore raw
In reply to Viktor Prutyanov
Quoted 15 lines · Viktor Prutyanov

In most cases, it is more convenient to convert a dump on the same machine on which the analysis is performed. Because of WinDbg, the analysis of guest Windows problems needs Windows host anyway, so it is useful to have dump convertion tool near the debugger.
After these patches elf2dmp can be built both for Linux and Windows (x86 and x86_64) hosts.

Viktor Prutyanov (6):
contrib/elf2dmp: fix elf.h including contrib/elf2dmp: use GLib in ELF processing contrib/elf2dmp: use GLib in PDB processing contrib/elf2dmp: fix structures definitions contrib/elf2dmp: fix printf format
configure: enable elf2dmp build for Windows hosts

Quoted 11 lines
 Makefile                   |  4 ++--
 configure                  |  4 ++--
 contrib/elf2dmp/kdbg.h     | 12 ++++++++----
 contrib/elf2dmp/main.c     | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c      | 33 +++++++++++----------------------
 contrib/elf2dmp/pdb.h      |  4 +++-
 contrib/elf2dmp/pe.h       |  6 ++++--
 contrib/elf2dmp/qemu_elf.c | 27 ++++++++-------------------
 contrib/elf2dmp/qemu_elf.h | 10 +++++-----
 9 files changed, 58 insertions(+), 69 deletions(-)

Queued, thanks.

Paolo