Skip to contentSkip to conversation

contrib/elf2dmp: elf2dmp for Windows hosts

PATCH Superseded Viktor Prutyanov 3 participants 8 messages Nov 2, 2018 – Nov 22, 2018 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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 hosts.

Viktor Prutyanov (2):
contrib/elf2dmp: use GLib file mapping
contrib/elf2dmp: elf2dmp for Windows hosts

 Makefile                      |  4 ++--
 contrib/elf2dmp/Makefile.objs |  2 +-
 contrib/elf2dmp/addrspace.c   |  7 ++++---
 contrib/elf2dmp/file_map.c    | 30 ++++++++++++++++++++++++++++++
 contrib/elf2dmp/file_map.h    | 21 +++++++++++++++++++++
 contrib/elf2dmp/kdbg.h        | 12 ++++++++----
 contrib/elf2dmp/main.c        | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c         | 32 ++++++++------------------------
 contrib/elf2dmp/pdb.h         |  7 +++++--
 contrib/elf2dmp/pe.h          |  6 ++++--
 contrib/elf2dmp/qemu_elf.c    | 34 ++++++++++++----------------------
 contrib/elf2dmp/qemu_elf.h    | 15 ++++++++-------
 12 files changed, 118 insertions(+), 79 deletions(-)
 create mode 100644 contrib/elf2dmp/file_map.c
 create mode 100644 contrib/elf2dmp/file_map.h
Signature · 1 line
2.17.2
#1 lore raw

PATCH 1/2 contrib/elf2dmp: use GLib file mapping

Replace POSIX mmap with GLib g_mapped_file_new to make elf2dmp cross-paltform. After this patch there are no direct POSIX calls.

---
 Makefile                      |  2 +-
 contrib/elf2dmp/Makefile.objs |  2 +-
 contrib/elf2dmp/addrspace.c   |  7 ++++---
 contrib/elf2dmp/file_map.c    | 30 ++++++++++++++++++++++++++++++
 contrib/elf2dmp/file_map.h    | 21 +++++++++++++++++++++
 contrib/elf2dmp/pdb.c         | 28 +++++-----------------------
 contrib/elf2dmp/pdb.h         |  5 +++--
 contrib/elf2dmp/qemu_elf.c    | 34 ++++++++++++----------------------
 contrib/elf2dmp/qemu_elf.h    | 15 ++++++++-------
 9 files changed, 85 insertions(+), 59 deletions(-)
 create mode 100644 contrib/elf2dmp/file_map.c
 create mode 100644 contrib/elf2dmp/file_map.h
Makefile +1 -1
index f2947186a4..ef53dd2a97 100644
@@ -711,7 +711,7 @@ ifneq ($(EXESUF),)
 qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI)
 endif
 
-elf2dmp: LIBS = $(CURL_LIBS)
+elf2dmp: LIBS += $(CURL_LIBS)
 elf2dmp: $(elf2dmp-obj-y)
 	$(call LINK, $^)
 
contrib/elf2dmp/Makefile.objs +1 -1
index e3140f58cf..04d4bbb189 100644
@@ -1 +1 @@
-elf2dmp-obj-y = main.o addrspace.o download.o pdb.o qemu_elf.o
+elf2dmp-obj-y = main.o addrspace.o download.o pdb.o qemu_elf.o file_map.o
contrib/elf2dmp/addrspace.c +4 -3
index 8a76069cb5..851373b7b7 100644
@@ -34,8 +34,9 @@ static uint8_t *pa_space_resolve(struct pa_space *ps, uint64_t pa)
 
 int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
 {
-    Elf64_Half phdr_nr = elf_getphdrnum(qemu_elf->map);
-    Elf64_Phdr *phdr = elf64_getphdr(qemu_elf->map);
+    void *map = QEMU_Elf_get_map(qemu_elf);
+    Elf64_Half phdr_nr = elf_getphdrnum(map);
+    Elf64_Phdr *phdr = elf64_getphdr(map);
     size_t block_i = 0;
     size_t i;
 
@@ -55,7 +56,7 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
     for (i = 0; i < phdr_nr; i++) {
         if (phdr[i].p_type == PT_LOAD) {
             ps->block[block_i] = (struct pa_block) {
-                .addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
+                .addr = (uint8_t *)map + phdr[i].p_offset,
                 .paddr = phdr[i].p_paddr,
                 .size = phdr[i].p_filesz,
             };
contrib/elf2dmp/file_map.c new file +30 -0
new file mode 100644
index 0000000000..08152044d6
@@ -0,0 +1,30 @@
+/*
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ *
+ */
+
+#include <stdio.h>
+#include <glib.h>
+
+#include "err.h"
+#include "file_map.h"
+
+int file_map(const char *name, mapped_file *mf)
+{
+    GError *err = NULL;
+
+    mf->gmf = g_mapped_file_new(name, TRUE, &err);
+    if (err) {
+        eprintf("Failed to map file \'%s\'\n", name);
+        return 1;
+    }
+    mf->map = g_mapped_file_get_contents(mf->gmf);
+    mf->size = g_mapped_file_get_length(mf->gmf);
+
+    return 0;
+}
+
+void file_unmap(mapped_file *mf)
+{
+    g_mapped_file_unref(mf->gmf);
+}
contrib/elf2dmp/file_map.h new file +21 -0
new file mode 100644
index 0000000000..1a0ea120e9
@@ -0,0 +1,21 @@
+/*
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ *
+ */
+
+#ifndef FILE_MAP_H
+#define FILE_MAP_H
+
+#include <stdio.h>
+#include <glib.h>
+
+typedef struct mapped_file {
+    GMappedFile *gmf;
+    void *map;
+    size_t size;
+} mapped_file;
+
+int file_map(const char *name, mapped_file *mf);
+void file_unmap(mapped_file *mf);
+
+#endif /* FILE_MAP_H */
contrib/elf2dmp/pdb.c +5 -23
index bcb01b414f..8fa5d71c66 100644
@@ -278,28 +278,13 @@ static void pdb_reader_exit(struct pdb_reader *r)
 int pdb_init_from_file(const char *name, struct pdb_reader *reader)
 {
     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);
-        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) {
+    if (file_map(name, &reader->mf)) {
         eprintf("Failed to map PDB file\n");
-        err = 1;
-        goto out_fd;
+        return 1;
     }
 
-    if (pdb_reader_init(reader, map)) {
+    if (pdb_reader_init(reader, reader->mf.map)) {
         err = 1;
         goto out_unmap;
     }
@@ -307,16 +292,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);
+    file_unmap(&reader->mf);
 
     return err;
 }
 
 void pdb_exit(struct pdb_reader *reader)
 {
-    munmap(reader->ds.header, reader->file_size);
-    close(reader->fd);
+    file_unmap(&reader->mf);
     pdb_reader_exit(reader);
 }
contrib/elf2dmp/pdb.h +3 -2
index 4351a2dd61..21c0a0e833 100644
@@ -11,6 +11,8 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#include "file_map.h"
+
 typedef struct GUID {
     unsigned int Data1;
     unsigned short Data2;
@@ -218,8 +220,7 @@ typedef struct pdb_seg {
 #define IMAGE_FILE_MACHINE_AMD64 0x8664
 
 struct pdb_reader {
-    int fd;
-    size_t file_size;
+    mapped_file mf;
     struct {
         PDB_DS_HEADER *header;
         PDB_DS_TOC *toc;
contrib/elf2dmp/qemu_elf.c +12 -22
index e9c0d2534a..8a6246fb4e 100644
@@ -60,10 +60,16 @@ Elf64_Half elf_getphdrnum(void *map)
     return ehdr->e_phnum;
 }
 
+void *QEMU_Elf_get_map(QEMU_Elf *qe)
+{
+    return qe->mf.map;
+}
+
 static int init_states(QEMU_Elf *qe)
 {
-    Elf64_Phdr *phdr = elf64_getphdr(qe->map);
-    Elf64_Nhdr *start = (void *)((uint8_t *)qe->map + phdr[0].p_offset);
+    void *map = QEMU_Elf_get_map(qe);
+    Elf64_Phdr *phdr = elf64_getphdr(map);
+    Elf64_Nhdr *start = (void *)((uint8_t *)map + phdr[0].p_offset);
     Elf64_Nhdr *end = (void *)((uint8_t *)start + phdr[0].p_memsz);
     Elf64_Nhdr *nhdr;
     size_t cpu_nr = 0;
@@ -121,23 +127,10 @@ static void exit_states(QEMU_Elf *qe)
 int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
 {
     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);
-        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) {
+    if (file_map(filename, &qe->mf)) {
         eprintf("Failed to map ELF file\n");
-        err = 1;
-        goto out_fd;
+        return 1;
     }
 
     if (init_states(qe)) {
@@ -149,9 +142,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);
+    file_unmap(&qe->mf);
 
     return err;
 }
@@ -159,6 +150,5 @@ out_fd:
 void QEMU_Elf_exit(QEMU_Elf *qe)
 {
     exit_states(qe);
-    munmap(qe->map, qe->size);
-    close(qe->fd);
+    file_unmap(&qe->mf);
 }
contrib/elf2dmp/qemu_elf.h +8 -7
index d85d6558fa..565f6f810c 100644
@@ -5,11 +5,13 @@
  *
  */
 
-#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"
+
+#include "file_map.h"
 
 typedef struct QEMUCPUSegment {
     uint32_t selector;
@@ -34,9 +36,7 @@ typedef struct QEMUCPUState {
 int is_system(QEMUCPUState *s);
 
 typedef struct QEMU_Elf {
-    int fd;
-    size_t size;
-    void *map;
+    mapped_file mf;
     QEMUCPUState **state;
     size_t state_nr;
     int has_kernel_gs_base;
@@ -47,5 +47,6 @@ void QEMU_Elf_exit(QEMU_Elf *qe);
 
 Elf64_Phdr *elf64_getphdr(void *map);
 Elf64_Half elf_getphdrnum(void *map);
+void *QEMU_Elf_get_map(QEMU_Elf *qe);
 
-#endif /* QEMU_ELF_H */
+#endif /* ELF2DMP_ELF_H */
Signature · 1 line
2.17.2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] contrib/elf2dmp: use GLib file mapping

Quoted 2 lines · Viktor Prutyanov

Replace POSIX mmap with GLib g_mapped_file_new to make elf2dmp cross-paltform. After this patch there are no direct POSIX calls.

s/paltform/platform/

Quoted 4 lines
---
  Makefile                      |  2 +-
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] contrib/elf2dmp: use GLib file mapping

In reply to Viktor Prutyanov
Quoted 114 lines · Viktor Prutyanov

Replace POSIX mmap with GLib g_mapped_file_new to make elf2dmp cross-paltform. After this patch there are no direct POSIX calls.

---
 Makefile                      |  2 +-
 contrib/elf2dmp/Makefile.objs |  2 +-
 contrib/elf2dmp/addrspace.c   |  7 ++++---
 contrib/elf2dmp/file_map.c    | 30 ++++++++++++++++++++++++++++++
 contrib/elf2dmp/file_map.h    | 21 +++++++++++++++++++++
 contrib/elf2dmp/pdb.c         | 28 +++++-----------------------
 contrib/elf2dmp/pdb.h         |  5 +++--
 contrib/elf2dmp/qemu_elf.c    | 34 ++++++++++++----------------------
 contrib/elf2dmp/qemu_elf.h    | 15 ++++++++-------
 9 files changed, 85 insertions(+), 59 deletions(-)
 create mode 100644 contrib/elf2dmp/file_map.c
 create mode 100644 contrib/elf2dmp/file_map.h
Makefile +1 -1
index f2947186a4..ef53dd2a97 100644
@@ -711,7 +711,7 @@ ifneq ($(EXESUF),)
 qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI)
 endif
 
-elf2dmp: LIBS = $(CURL_LIBS)
+elf2dmp: LIBS += $(CURL_LIBS)
 elf2dmp: $(elf2dmp-obj-y)
 	$(call LINK, $^)
 
contrib/elf2dmp/Makefile.objs +1 -1
index e3140f58cf..04d4bbb189 100644
@@ -1 +1 @@
-elf2dmp-obj-y = main.o addrspace.o download.o pdb.o qemu_elf.o
+elf2dmp-obj-y = main.o addrspace.o download.o pdb.o qemu_elf.o file_map.o
contrib/elf2dmp/addrspace.c +4 -3
index 8a76069cb5..851373b7b7 100644
@@ -34,8 +34,9 @@ static uint8_t *pa_space_resolve(struct pa_space *ps, uint64_t pa)
 
 int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
 {
-    Elf64_Half phdr_nr = elf_getphdrnum(qemu_elf->map);
-    Elf64_Phdr *phdr = elf64_getphdr(qemu_elf->map);
+    void *map = QEMU_Elf_get_map(qemu_elf);
+    Elf64_Half phdr_nr = elf_getphdrnum(map);
+    Elf64_Phdr *phdr = elf64_getphdr(map);
     size_t block_i = 0;
     size_t i;
 
@@ -55,7 +56,7 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
     for (i = 0; i < phdr_nr; i++) {
         if (phdr[i].p_type == PT_LOAD) {
             ps->block[block_i] = (struct pa_block) {
-                .addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
+                .addr = (uint8_t *)map + phdr[i].p_offset,
                 .paddr = phdr[i].p_paddr,
                 .size = phdr[i].p_filesz,
             };
contrib/elf2dmp/file_map.c new file +30 -0
new file mode 100644
index 0000000000..08152044d6
@@ -0,0 +1,30 @@
+/*
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ *
+ */
+
+#include <stdio.h>
+#include <glib.h>
+
+#include "err.h"
+#include "file_map.h"
+
+int file_map(const char *name, mapped_file *mf)
+{
+    GError *err = NULL;
+
+    mf->gmf = g_mapped_file_new(name, TRUE, &err);
+    if (err) {
+        eprintf("Failed to map file \'%s\'\n", name);
+        return 1;
+    }
+    mf->map = g_mapped_file_get_contents(mf->gmf);
+    mf->size = g_mapped_file_get_length(mf->gmf);
+
+    return 0;
+}
+
+void file_unmap(mapped_file *mf)
+{
+    g_mapped_file_unref(mf->gmf);
+}
contrib/elf2dmp/file_map.h new file +9 -0
new file mode 100644
index 0000000000..1a0ea120e9
@@ -0,0 +1,21 @@
+/*
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ *
+ */
+
+#ifndef FILE_MAP_H
+#define FILE_MAP_H
+
+#include <stdio.h>

contrib/elf2dmp/file_map.h line 19

Quoted 10 lines
contrib/elf2dmp/file_map.h new file +10 -0
+#include <glib.h>
+
+typedef struct mapped_file {
+    GMappedFile *gmf;
+    void *map;
+    size_t size;
+} mapped_file;
+
+int file_map(const char *name, mapped_file *mf);
+void file_unmap(mapped_file *mf);

What does this extra wrapping layer buy you? Can't you use GMappedFile directly?

Thanks,
Roman.

Quoted 191 lines
contrib/elf2dmp/file_map.h new file +2 -0
+
+#endif /* FILE_MAP_H */
contrib/elf2dmp/pdb.c +5 -23
index bcb01b414f..8fa5d71c66 100644
@@ -278,28 +278,13 @@ static void pdb_reader_exit(struct pdb_reader *r)
 int pdb_init_from_file(const char *name, struct pdb_reader *reader)
 {
     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);
-        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) {
+    if (file_map(name, &reader->mf)) {
         eprintf("Failed to map PDB file\n");
-        err = 1;
-        goto out_fd;
+        return 1;
     }
 
-    if (pdb_reader_init(reader, map)) {
+    if (pdb_reader_init(reader, reader->mf.map)) {
         err = 1;
         goto out_unmap;
     }
@@ -307,16 +292,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);
+    file_unmap(&reader->mf);
 
     return err;
 }
 
 void pdb_exit(struct pdb_reader *reader)
 {
-    munmap(reader->ds.header, reader->file_size);
-    close(reader->fd);
+    file_unmap(&reader->mf);
     pdb_reader_exit(reader);
 }
contrib/elf2dmp/pdb.h +3 -2
index 4351a2dd61..21c0a0e833 100644
@@ -11,6 +11,8 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#include "file_map.h"
+
 typedef struct GUID {
     unsigned int Data1;
     unsigned short Data2;
@@ -218,8 +220,7 @@ typedef struct pdb_seg {
 #define IMAGE_FILE_MACHINE_AMD64 0x8664
 
 struct pdb_reader {
-    int fd;
-    size_t file_size;
+    mapped_file mf;
     struct {
         PDB_DS_HEADER *header;
         PDB_DS_TOC *toc;
contrib/elf2dmp/qemu_elf.c +12 -22
index e9c0d2534a..8a6246fb4e 100644
@@ -60,10 +60,16 @@ Elf64_Half elf_getphdrnum(void *map)
     return ehdr->e_phnum;
 }
 
+void *QEMU_Elf_get_map(QEMU_Elf *qe)
+{
+    return qe->mf.map;
+}
+
 static int init_states(QEMU_Elf *qe)
 {
-    Elf64_Phdr *phdr = elf64_getphdr(qe->map);
-    Elf64_Nhdr *start = (void *)((uint8_t *)qe->map + phdr[0].p_offset);
+    void *map = QEMU_Elf_get_map(qe);
+    Elf64_Phdr *phdr = elf64_getphdr(map);
+    Elf64_Nhdr *start = (void *)((uint8_t *)map + phdr[0].p_offset);
     Elf64_Nhdr *end = (void *)((uint8_t *)start + phdr[0].p_memsz);
     Elf64_Nhdr *nhdr;
     size_t cpu_nr = 0;
@@ -121,23 +127,10 @@ static void exit_states(QEMU_Elf *qe)
 int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
 {
     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);
-        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) {
+    if (file_map(filename, &qe->mf)) {
         eprintf("Failed to map ELF file\n");
-        err = 1;
-        goto out_fd;
+        return 1;
     }
 
     if (init_states(qe)) {
@@ -149,9 +142,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);
+    file_unmap(&qe->mf);
 
     return err;
 }
@@ -159,6 +150,5 @@ out_fd:
 void QEMU_Elf_exit(QEMU_Elf *qe)
 {
     exit_states(qe);
-    munmap(qe->map, qe->size);
-    close(qe->fd);
+    file_unmap(&qe->mf);
 }
contrib/elf2dmp/qemu_elf.h +8 -7
index d85d6558fa..565f6f810c 100644
@@ -5,11 +5,13 @@
  *
  */
 
-#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"
+
+#include "file_map.h"
 
 typedef struct QEMUCPUSegment {
     uint32_t selector;
@@ -34,9 +36,7 @@ typedef struct QEMUCPUState {
 int is_system(QEMUCPUState *s);
 
 typedef struct QEMU_Elf {
-    int fd;
-    size_t size;
-    void *map;
+    mapped_file mf;
     QEMUCPUState **state;
     size_t state_nr;
     int has_kernel_gs_base;
@@ -47,5 +47,6 @@ void QEMU_Elf_exit(QEMU_Elf *qe);
 
 Elf64_Phdr *elf64_getphdr(void *map);
 Elf64_Half elf_getphdrnum(void *map);
+void *QEMU_Elf_get_map(QEMU_Elf *qe);
 
-#endif /* QEMU_ELF_H */
+#endif /* ELF2DMP_ELF_H */
Signature · 1 line
2.17.2
#4 lore raw

PATCH 2/2 contrib/elf2dmp: elf2dmp for Windows hosts

In reply to Viktor Prutyanov

After this patch elf2dmp can be built by mingw64 for Windows hosts.

---
 Makefile               |  4 ++--
 contrib/elf2dmp/kdbg.h | 12 ++++++++----
 contrib/elf2dmp/main.c | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c  |  4 +++-
 contrib/elf2dmp/pdb.h  |  2 ++
 contrib/elf2dmp/pe.h   |  6 ++++--
 6 files changed, 34 insertions(+), 21 deletions(-)
Makefile +2 -2
index ef53dd2a97..73adb02b41 100644
@@ -711,8 +711,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
contrib/elf2dmp/kdbg.h +8 -4
index 851b57c321..d3936994a0 100644
@@ -25,11 +25,15 @@ typedef struct DBGKD_GET_VERSION64 {
     uint64_t DebuggerDataList;
 } DBGKD_GET_VERSION64;
 
+#ifndef _WIN64
+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/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 8fa5d71c66..0c5a6fbb9e 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;
contrib/elf2dmp/pdb.h +2 -0
index 21c0a0e833..fe189f0ecc 100644
@@ -13,12 +13,14 @@
 
 #include "file_map.h"
 
+#ifndef _WIN64
 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..7b60849713 100644
@@ -10,6 +10,7 @@
 
 #include <stdint.h>
 
+#ifndef _WIN64
 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
Quoted 1 line · Viktor Prutyanov

After this patch elf2dmp can be built by mingw64 for Windows hosts.

What prevents building it with mingw32? (I realize that since the tool uses mmaped access to the dump data it's limited to dumps under a couple of gigs but it's still not totally useless).

Quoted 9 lines
---
 Makefile               |  4 ++--
 contrib/elf2dmp/kdbg.h | 12 ++++++++----
 contrib/elf2dmp/main.c | 27 +++++++++++++++------------
 contrib/elf2dmp/pdb.c  |  4 +++-
 contrib/elf2dmp/pdb.h  |  2 ++
 contrib/elf2dmp/pe.h   |  6 ++++--
 6 files changed, 34 insertions(+), 21 deletions(-)

Isn't there a configure check that needs to be removed?

Quoted 21 lines
Makefile +2 -2
index ef53dd2a97..73adb02b41 100644
@@ -711,8 +711,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
contrib/elf2dmp/kdbg.h +0 -0
index 851b57c321..d3936994a0 100644

contrib/elf2dmp/kdbg.h typedef struct DBGKD_GET_VERSION64 { · line 28

Quoted 4 lines
contrib/elf2dmp/kdbg.h +1 -0
@@ -25,11 +25,15 @@ typedef struct DBGKD_GET_VERSION64 {
     uint64_t DebuggerDataList;
 } DBGKD_GET_VERSION64;
 
+#ifndef _WIN64

So this may be better _WIN32

Quoted 29 lines
contrib/elf2dmp/kdbg.h +7 -4
+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/main.c +2 -0
index 9b93dab662..fdafb54900 100644
@@ -5,6 +5,8 @@
  *
  */
 
+#include <inttypes.h>
+
 #include "qemu/osdep.h"
 #include "err.h"
 #include "addrspace.h"

contrib/elf2dmp/main.c static const uint64_t SharedUserData = 0xfffff78000000000; · line 46

Quoted 5 lines
contrib/elf2dmp/main.c +1 -1
@@ -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) :\

I'd rather split the fixes to printf specifiers into a separate patch, as they have nothing to do with Windows support.

Thanks,
Roman.

Quoted 159 lines
contrib/elf2dmp/main.c +12 -11
+    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 8fa5d71c66..0c5a6fbb9e 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;
contrib/elf2dmp/pdb.h +2 -0
index 21c0a0e833..fe189f0ecc 100644
@@ -13,12 +13,14 @@
 
 #include "file_map.h"
 
+#ifndef _WIN64
 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..7b60849713 100644
@@ -10,6 +10,7 @@
 
 #include <stdint.h>
 
+#ifndef _WIN64
 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
#6 lore raw
Quoted 6 lines · Roman Kagan
Quoted 1 line · Viktor Prutyanov

After this patch elf2dmp can be built by mingw64 for Windows hosts.

What prevents building it with mingw32? (I realize that since the tool uses mmaped access to the dump data it's limited to dumps under a couple of gigs but it's still not totally useless).

Careful. The original (32-bit) mingw project is obsolete, and its replacement, the mingw64 project, provides both 32- and 64-bit environments. Even more confusingly, Fedora ships the mingw64 cross-compilers under the names i686-w64-mingw32-gcc (build for a 32-bit environment) and x86_64-w64-mingw32-gcc (build for a 64-bit environment). Historical naming causes confusion.

So your question may not be relevant, especially given that tests/docker/test-mingw builds both 32- and 64-bit mingw builds.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw
Quoted 8 lines · Eric Blake
Quoted 6 lines · Roman Kagan
Quoted 1 line · Viktor Prutyanov

After this patch elf2dmp can be built by mingw64 for Windows hosts.

What prevents building it with mingw32? (I realize that since the tool uses mmaped access to the dump data it's limited to dumps under a couple of gigs but it's still not totally useless).

Quoted 6 lines

Careful. The original (32-bit) mingw project is obsolete, and its replacement, the mingw64 project, provides both 32- and 64-bit environments. Even more confusingly, Fedora ships the mingw64 cross-compilers under the names i686-w64-mingw32-gcc (build for a 32-bit environment) and x86_64-w64-mingw32-gcc (build for a 64-bit environment). Historical naming causes confusion.

Indeed.

Quoted 2 lines

So your question may not be relevant, especially given that tests/docker/test-mingw builds both 32- and 64-bit mingw builds.

I think it still is :)

The patch protects certain parts with #ifdef _WIN64, so it looks like it won't build against 32-bit mingw. Hence my question.

Roman.