From patchwork Fri Nov 2 00:28:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 10664849 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 920A114E2 for ; Fri, 2 Nov 2018 00:29:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 77D7D2C144 for ; Fri, 2 Nov 2018 00:29:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6ACF42C1A2; Fri, 2 Nov 2018 00:29:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 55D9D2C144 for ; Fri, 2 Nov 2018 00:29:46 +0000 (UTC) Received: from localhost ([::1]:47988 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gINLN-00073O-2K for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Nov 2018 20:29:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34841) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gINKU-000637-Hd for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gINKQ-0003iA-AB for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:50 -0400 Received: from mail-lf1-x143.google.com ([2a00:1450:4864:20::143]:46867) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gINKP-0003ha-V5 for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:46 -0400 Received: by mail-lf1-x143.google.com with SMTP id o2-v6so160725lfl.13 for ; Thu, 01 Nov 2018 17:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=phystech-edu.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=IPgDbyEmpgk4VO2y10xEGdYThfqlh8PEr6yAiySoPyU=; b=zFq9bo7nlGxSEsKyE1ZnJu55qh/GC/ai5AcG8f+kI9Qyrr6jYQ3KtqsNPFtIR5C+Pa ahY51QDZJKQQ20a4uvAOBNxkQPXeK0MJT6YtoBnECkNugMTnDYjDpy1oiF47GF7TFGBJ /d9wwdXMTdRl51OTuuEGpUcKO/ypwl/5fLEzehbaglaozkPyDuBPQofeXzaotrToqeuc 86F7LopFmb8/LV78lj4UBCAbTnqgyJX+jgkDAcg2nKcAV55v61kVk7upde2DnAVpS11Y BBqiR/3bS9M5GCD3WDj48ppUnAlk3381u0+LsnRckk3ZeWveEZJllTEGzphrCRu2N8Mz PR6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=IPgDbyEmpgk4VO2y10xEGdYThfqlh8PEr6yAiySoPyU=; b=FDeAgq8KFpFB0Z6HjjV8lQol2HpL1fnldkvX4s53QslKTrdIiHLSwsj5tMpfq/1a6x HksDKUfX9hAkthx0rULCW8+NrTqKzm2s0o3E9+gkb1uKa4dIbZAPZz94X1cxrMQn0aa3 OTAHzg8UnnxS5CSEuoUlFM5/CTuEmKTUMiOVfQaSGlSQKbnGC/c1B1riciT8mbuhpC8m rY1bU2EF2kZe3LXJSvbx9+gvA/fcKFUTw1giPirbpVy6bKL3tqa3ozo3V8cKXf9r+XPc dm5JwFopltX6spCNhkp44AGI792y0L/CtzKlF0dL4P7+Wg40MMZRtZHnzeHBtMvETpRE k9mQ== X-Gm-Message-State: AGRZ1gISTek+ueevXkaBDzShIHde0/otbBdte2/Jvq89ejgsHN4ad39q V3PMrioiIek6tXmYdvas/PZ/FQ== X-Google-Smtp-Source: AJdET5eGUrCpAmmCfcnFo+xPunL3PZZed5/QRqXraYWs+1dyd95z1+0kGoRzw0hlDHWhaflwO3hr1w== X-Received: by 2002:a19:7019:: with SMTP id h25-v6mr5881001lfc.147.1541118524476; Thu, 01 Nov 2018 17:28:44 -0700 (PDT) Received: from localhost.localdomain ([93.175.11.132]) by smtp.gmail.com with ESMTPSA id r85-v6sm2019900lfd.18.2018.11.01.17.28.43 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Nov 2018 17:28:43 -0700 (PDT) From: Viktor Prutyanov To: pbonzini@redhat.com, rkagan@virtuozzo.com Date: Fri, 2 Nov 2018 03:28:17 +0300 Message-Id: <20181102002818.8568-2-viktor.prutyanov@phystech.edu> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181102002818.8568-1-viktor.prutyanov@phystech.edu> References: <20181102002818.8568-1-viktor.prutyanov@phystech.edu> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::143 Subject: [Qemu-devel] [PATCH 1/2] contrib/elf2dmp: use GLib file mapping X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Viktor Prutyanov Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Replace POSIX mmap with GLib g_mapped_file_new to make elf2dmp cross-paltform. After this patch there are no direct POSIX calls. Signed-off-by: Viktor Prutyanov --- 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 diff --git a/Makefile b/Makefile index f2947186a4..ef53dd2a97 100644 --- a/Makefile +++ b/Makefile @@ -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, $^) diff --git a/contrib/elf2dmp/Makefile.objs b/contrib/elf2dmp/Makefile.objs index e3140f58cf..04d4bbb189 100644 --- a/contrib/elf2dmp/Makefile.objs +++ b/contrib/elf2dmp/Makefile.objs @@ -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 diff --git a/contrib/elf2dmp/addrspace.c b/contrib/elf2dmp/addrspace.c index 8a76069cb5..851373b7b7 100644 --- a/contrib/elf2dmp/addrspace.c +++ b/contrib/elf2dmp/addrspace.c @@ -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, }; diff --git a/contrib/elf2dmp/file_map.c b/contrib/elf2dmp/file_map.c new file mode 100644 index 0000000000..08152044d6 --- /dev/null +++ b/contrib/elf2dmp/file_map.c @@ -0,0 +1,30 @@ +/* + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * + */ + +#include +#include + +#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); +} diff --git a/contrib/elf2dmp/file_map.h b/contrib/elf2dmp/file_map.h new file mode 100644 index 0000000000..1a0ea120e9 --- /dev/null +++ b/contrib/elf2dmp/file_map.h @@ -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 +#include + +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 */ diff --git a/contrib/elf2dmp/pdb.c b/contrib/elf2dmp/pdb.c index bcb01b414f..8fa5d71c66 100644 --- a/contrib/elf2dmp/pdb.c +++ b/contrib/elf2dmp/pdb.c @@ -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); } diff --git a/contrib/elf2dmp/pdb.h b/contrib/elf2dmp/pdb.h index 4351a2dd61..21c0a0e833 100644 --- a/contrib/elf2dmp/pdb.h +++ b/contrib/elf2dmp/pdb.h @@ -11,6 +11,8 @@ #include #include +#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; diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c index e9c0d2534a..8a6246fb4e 100644 --- a/contrib/elf2dmp/qemu_elf.c +++ b/contrib/elf2dmp/qemu_elf.c @@ -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); } diff --git a/contrib/elf2dmp/qemu_elf.h b/contrib/elf2dmp/qemu_elf.h index d85d6558fa..565f6f810c 100644 --- a/contrib/elf2dmp/qemu_elf.h +++ b/contrib/elf2dmp/qemu_elf.h @@ -5,11 +5,13 @@ * */ -#ifndef QEMU_ELF_H -#define QEMU_ELF_H +#ifndef ELF2DMP_ELF_H +#define ELF2DMP_ELF_H #include -#include +#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 */ From patchwork Fri Nov 2 00:28:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 10664853 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3589F13BF for ; Fri, 2 Nov 2018 00:31:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F88F2C19A for ; Fri, 2 Nov 2018 00:31:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D5592C1AB; Fri, 2 Nov 2018 00:31:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AD1A92C19A for ; Fri, 2 Nov 2018 00:31:32 +0000 (UTC) Received: from localhost ([::1]:48024 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gINN6-000865-1K for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Nov 2018 20:31:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gINKU-000638-Ih for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gINKR-0003jf-Vj for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:50 -0400 Received: from mail-lf1-x142.google.com ([2a00:1450:4864:20::142]:35466) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gINKR-0003iI-9I for qemu-devel@nongnu.org; Thu, 01 Nov 2018 20:28:47 -0400 Received: by mail-lf1-x142.google.com with SMTP id d7-v6so196169lfi.2 for ; Thu, 01 Nov 2018 17:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=phystech-edu.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=akV4ufcOmA7aYQGPKL8g7KbBTKKtxO1zDBaDPgt73wI=; b=uLeTsNEmx/OT/BYxXYLM7H6Hvd4RNCSAtG+KIyLL59YeRt/NsexrSs4c9sKcAT589w UETEQVxy+Tl9VYecBtn2hIMpJGGd2nRYGzkL3yZY4MSvEDEflECOBTCAgMnuBi6CUVst ak/cq63UJflyRp59FbDEEFDn0Rc5+dhSrKstWIUKrM8jMa7UFdry9QN/wxRSfrJxJGEj UoU3BCQTOaCj9pnKHYNtPrB9s2AZ59/g5FnFLTjtbinWq98zAN3Mk0a0l47jSEOO5aLs VrjrtKqtmUmhG/iYLjY0O8vA4mkbPVbEVVYr4BRE5bXShJTWb89WC0BwY9+G4KqFjW6d EQsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=akV4ufcOmA7aYQGPKL8g7KbBTKKtxO1zDBaDPgt73wI=; b=MkKO3XSN9ZphzTbGZove9RGfeIAtsAYUrwK9P8dLyW1/2NOZH8z/vXEVvmr+o4Jm0D 9NzsXMx8OVSLnsZ7P/EUhlvW0+ZC4Wl5UHTzsPvfXiSr2el3VXw2XQC1UOZP1mAP6c7X RncqvxirM8e9cCEfHaL3+PWAkTSKhQx0oh1Ju0ywpiyPu0pZwOUlbpmhIgtq3A3ms5CQ dvitnNKd8buz2D5Iv7ORJEd/HkmqH1OESScRDerU591fE5HM+bEXHSF4NcYXK13cNRWg sZ/DqarEzZxbYnOmhUdJOgN993dBCqL8/RGOQLi/fPWbXjgULR52z4hXp/dEI28XIzIa pr1A== X-Gm-Message-State: AGRZ1gLWeVr4Q8s3fwBUJ0FV8l0KNJM4A3CRhn7byIo3fHavieBh64hy Q84EggXWY6PrCS6Ye1NdvD39SQ== X-Google-Smtp-Source: AJdET5fewPW8FLEZrRMQMtUnpLQ5LCw4F6eheb7Fjx0qLpgO/zY19guscyvgq2LGZtRzTAK6uLmY8g== X-Received: by 2002:a19:c203:: with SMTP id l3mr1060094lfc.113.1541118525867; Thu, 01 Nov 2018 17:28:45 -0700 (PDT) Received: from localhost.localdomain ([93.175.11.132]) by smtp.gmail.com with ESMTPSA id r85-v6sm2019900lfd.18.2018.11.01.17.28.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 01 Nov 2018 17:28:44 -0700 (PDT) From: Viktor Prutyanov To: pbonzini@redhat.com, rkagan@virtuozzo.com Date: Fri, 2 Nov 2018 03:28:18 +0300 Message-Id: <20181102002818.8568-3-viktor.prutyanov@phystech.edu> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181102002818.8568-1-viktor.prutyanov@phystech.edu> References: <20181102002818.8568-1-viktor.prutyanov@phystech.edu> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::142 Subject: [Qemu-devel] [PATCH 2/2] contrib/elf2dmp: elf2dmp for Windows hosts X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Viktor Prutyanov Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP After this patch elf2dmp can be built by mingw64 for Windows hosts. Signed-off-by: Viktor Prutyanov --- 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(-) diff --git a/Makefile b/Makefile index ef53dd2a97..73adb02b41 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/contrib/elf2dmp/kdbg.h b/contrib/elf2dmp/kdbg.h index 851b57c321..d3936994a0 100644 --- a/contrib/elf2dmp/kdbg.h +++ b/contrib/elf2dmp/kdbg.h @@ -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; diff --git a/contrib/elf2dmp/main.c b/contrib/elf2dmp/main.c index 9b93dab662..fdafb54900 100644 --- a/contrib/elf2dmp/main.c +++ b/contrib/elf2dmp/main.c @@ -5,6 +5,8 @@ * */ +#include + #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)) { diff --git a/contrib/elf2dmp/pdb.c b/contrib/elf2dmp/pdb.c index 8fa5d71c66..0c5a6fbb9e 100644 --- a/contrib/elf2dmp/pdb.c +++ b/contrib/elf2dmp/pdb.c @@ -18,6 +18,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include + #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; diff --git a/contrib/elf2dmp/pdb.h b/contrib/elf2dmp/pdb.h index 21c0a0e833..fe189f0ecc 100644 --- a/contrib/elf2dmp/pdb.h +++ b/contrib/elf2dmp/pdb.h @@ -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; diff --git a/contrib/elf2dmp/pe.h b/contrib/elf2dmp/pe.h index 374e06a9c5..7b60849713 100644 --- a/contrib/elf2dmp/pe.h +++ b/contrib/elf2dmp/pe.h @@ -10,6 +10,7 @@ #include +#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;