From nobody Thu Nov 21 17:57:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1727113942; cv=none; d=zohomail.com; s=zohoarc; b=aNUal/10DN6aI5NiwYO2N1XRaCUYQwsO5I4dFTr6PoTLvbhnu8ulILNlJfl0JQPgvBJb9Q4PsFd9SCEUYLvruwaMQLPlca5EjMRyYfB1O2uDG5Y6ApCcC1psFkBUM8/1wymDSJY2baqgUNpOpEcDN2Y+cI1uFRKAVTkkWeG+MSs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727113942; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tsXlhjU+4Hce0iMK21m8sEq36yKNuqhh64j/nnc7Ed0=; b=hfitYBAH+vbpq3vAzTj3vuTrKhk+XFg+jlYo5wOntKHZRARSfT5wdzNh+hmDOYNqeLA5au3X9/+z05Sw82ga/OYs2C2bg5AfI3NP3SGLUaC0af782mBK9+/xHmLQevB5B15RGCer/alBKBe9OVW9W+yDpe5HpmeNOHkMEaZIo0s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1727113942206187.5723946973767; Mon, 23 Sep 2024 10:52:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.802262.1212478 (Exim 4.92) (envelope-from ) id 1ssnDt-0004kQ-Uo; Mon, 23 Sep 2024 17:51:45 +0000 Received: by outflank-mailman (output) from mailman id 802262.1212478; Mon, 23 Sep 2024 17:51:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ssnDt-0004kJ-SD; Mon, 23 Sep 2024 17:51:45 +0000 Received: by outflank-mailman (input) for mailman id 802262; Mon, 23 Sep 2024 17:51:44 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ssnDs-0004kD-0G for xen-devel@lists.xenproject.org; Mon, 23 Sep 2024 17:51:44 +0000 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [2a00:1450:4864:20::331]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7debb688-79d4-11ef-a0b9-8be0dac302b0; Mon, 23 Sep 2024 19:51:42 +0200 (CEST) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-42cb5b3c57eso46240925e9.2 for ; Mon, 23 Sep 2024 10:51:42 -0700 (PDT) Received: from Xen-host.domain.local ([89.216.37.146]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e73e80d0sm24848887f8f.25.2024.09.23.10.51.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 10:51:41 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7debb688-79d4-11ef-a0b9-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727113902; x=1727718702; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tsXlhjU+4Hce0iMK21m8sEq36yKNuqhh64j/nnc7Ed0=; b=SY8XpreedG+pNXI7sOb5Rjjp9Az13seyiJTD5MJKt1J6IBaOK/vrmweVtbVlOIHz/L LkbpfbDRib9aKizEbWD2y4lvHgHxh6YTELKh7UwhDwmnEZnvioTUwDzH5pQCjE3iAlrc t9QIsegeaOREQMhgbSQaGJr3LrRNxiHbbc4mxzyFtbBIPiMqTqQi8UyffbJuuvnXSwIn 2K7uY627fNKlNCFilJ+V377s0W9GOMsRAf/PsPFTZz08snusLvUAqqbeQ2ngtDavpYeI tdtplqwWRH70h7WlX20OxXXmYDw8kgewxIALiqGC8T6Z+5fZNvpiICI243Chsc7In1aE EC6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727113902; x=1727718702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tsXlhjU+4Hce0iMK21m8sEq36yKNuqhh64j/nnc7Ed0=; b=AsLaBl/7re0dxJrkqx4RpTPH+8sBOTo3ZN3iaXuTP/nz+MSxwNyoK8b+vipCPBIu/Q CpCV7Mkyz1xBCRfTFTan27vhZXi39VnPJ5TP92yR3fVkEqOyA6JXiCcn5LT+4m/K6XuR AJxC3qelfbdsukf23ECCi4FXLmbis4KK5tKc1B+nGhSetZzqAmoxZHWI9wAYXvFVO9d6 BRwi1KxfXHcW8l1gYOT7qrI6wj396U5AwcHTU6ZVETNp/3Q2kCDY9+x2g42HS5YnKdTn QdqurNql9COkYzbBzAxLKDdxelRGQcbGGL69FiR3TuAtuy3hTqAg3/tcUYtfm19T86w1 S6gw== X-Gm-Message-State: AOJu0YwrR8OC0mSJpoaefWYgFvRecOMxfQZ2vEMUviZcEAxeW/cIYS8E w1ck8iidbDCAHql3p8tySrUTVIGBz2I0mV9LXzwkFntJM+r3Cuu7el6AWDRx X-Google-Smtp-Source: AGHT+IFxbYux5kQsQOX+BwxSBKBGCQonCJGMSVhz6QewS99TOicmH0nDDsCduCXTH2Ka6vs6xshfEw== X-Received: by 2002:a05:600c:1c87:b0:42c:b7f9:4bbd with SMTP id 5b1f17b1804b1-42e7c19bb93mr88095665e9.26.1727113901349; Mon, 23 Sep 2024 10:51:41 -0700 (PDT) From: Milan Djokic To: xen-devel@lists.xenproject.org Cc: frediano.ziglio@cloud.com, Nikola Jelic , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , "Daniel P. Smith" , =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= , Milan Djokic Subject: [PATCH v3] x86/efi: Use generic PE/COFF structures Date: Mon, 23 Sep 2024 19:50:08 +0200 Message-Id: <5dfa21dd5f8e968bf38fda5690964e0f1d957ccc.1727113391.git.milandjokic1995@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <367c5d95fb89e7dd3b4260f136d194a4cd0b854a.1724765763.git.milandjokic1995@gmail.com> References: <367c5d95fb89e7dd3b4260f136d194a4cd0b854a.1724765763.git.milandjokic1995@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1727113943574116600 Content-Type: text/plain; charset="utf-8" From: Nikola Jelic Adapted x86 efi parser and mkreloc utility to use generic PE header (efi/pe.h), instead of locally defined structures for each component. Signed-off-by: Nikola Jelic Signed-off-by: Milan Djokic Acked-by: Andrew Cooper Acked-by: Daniel P. Smith --- Changes in V3: - Add xen/include among include dirs for mkreloc, instead of using relative path in code to include pe.h Changes in V2: - Using pe header constants instead of hardcoded values (magic, machine) --- xen/arch/x86/Makefile | 2 + xen/arch/x86/efi/mkreloc.c | 134 +++++++++++-------------------------- xen/common/efi/pe.c | 92 ++++++------------------- 3 files changed, 61 insertions(+), 167 deletions(-) diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index 286c003ec3..b1c8afbf03 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -88,6 +88,8 @@ extra-y +=3D xen.lds hostprogs-y +=3D boot/mkelf32 hostprogs-y +=3D efi/mkreloc =20 +$(obj)/efi/mkreloc:HOSTCFLAGS +=3D -I$(srctree)/include + # Allows usercopy.c to include itself $(obj)/usercopy.o: CFLAGS-y +=3D -iquote . =20 diff --git a/xen/arch/x86/efi/mkreloc.c b/xen/arch/x86/efi/mkreloc.c index 083740ab8a..7c9aac49ed 100644 --- a/xen/arch/x86/efi/mkreloc.c +++ b/xen/arch/x86/efi/mkreloc.c @@ -9,45 +9,7 @@ #include #include =20 -struct mz_hdr { - uint16_t signature; -#define MZ_SIGNATURE 0x5a4d - uint16_t last_page_size; - uint16_t page_count; - uint16_t relocation_count; - uint16_t header_paras; - uint16_t min_paras; - uint16_t max_paras; - uint16_t entry_ss; - uint16_t entry_sp; - uint16_t checksum; - uint16_t entry_ip; - uint16_t entry_cs; - uint16_t relocations; - uint16_t overlay; - uint8_t reserved[32]; - uint32_t extended_header_base; -}; - -struct pe_hdr { - uint32_t signature; -#define PE_SIGNATURE 0x00004550 - uint16_t cpu; - uint16_t section_count; - int32_t timestamp; - uint32_t symbols_file_offset; - uint32_t symbol_count; - uint16_t opt_hdr_size; - uint16_t flags; - struct { - uint16_t magic; -#define PE_MAGIC_EXE32 0x010b -#define PE_MAGIC_EXE32PLUS 0x020b - uint8_t linker_major, linker_minor; - uint32_t code_size, data_size, bss_size; - uint32_t entry_rva, code_rva, data_rva; - } opt_hdr; -}; +#include =20 #define PE_PAGE_SIZE 0x1000 =20 @@ -55,22 +17,6 @@ struct pe_hdr { #define PE_BASE_RELOC_HIGHLOW 3 #define PE_BASE_RELOC_DIR64 10 =20 -struct coff_section { - char name[8]; - uint32_t size; - uint32_t rva; - uint32_t file_size; - uint32_t file_offset; - uint32_t relocation_file_offset; - uint32_t line_number_file_offset; - uint16_t relocation_count; - uint16_t line_number_count; - uint32_t flags; -#define COFF_SECTION_BSS 0x00000080U -#define COFF_SECTION_DISCARDABLE 0x02000000U -#define COFF_SECTION_WRITEABLE 0x80000000U -}; - static void usage(const char *cmd, int rc) { fprintf(rc ? stderr : stdout, @@ -80,7 +26,7 @@ static void usage(const char *cmd, int rc) } =20 static unsigned int load(const char *name, int *handle, - struct coff_section **sections, + struct section_header **sections, uint_fast64_t *image_base, uint32_t *image_size, unsigned int *width) @@ -88,6 +34,7 @@ static unsigned int load(const char *name, int *handle, int in =3D open(name, O_RDONLY); struct mz_hdr mz_hdr; struct pe_hdr pe_hdr; + struct pe32_opt_hdr pe32_opt_hdr; uint32_t base; =20 if ( in < 0 || @@ -96,16 +43,17 @@ static unsigned int load(const char *name, int *handle, perror(name); exit(2); } - if ( mz_hdr.signature !=3D MZ_SIGNATURE || - mz_hdr.relocations < sizeof(mz_hdr) || - !mz_hdr.extended_header_base ) + if ( mz_hdr.magic !=3D MZ_MAGIC || + mz_hdr.reloc_table_offset < sizeof(mz_hdr) || + !mz_hdr.peaddr ) { fprintf(stderr, "%s: Wrong DOS file format\n", name); exit(2); } =20 - if ( lseek(in, mz_hdr.extended_header_base, SEEK_SET) < 0 || + if ( lseek(in, mz_hdr.peaddr, SEEK_SET) < 0 || read(in, &pe_hdr, sizeof(pe_hdr)) !=3D sizeof(pe_hdr) || + read(in, &pe32_opt_hdr, sizeof(pe32_opt_hdr)) !=3D sizeof(pe32_op= t_hdr) || read(in, &base, sizeof(base)) !=3D sizeof(base) || /* * Luckily the image size field lives at the @@ -117,35 +65,33 @@ static unsigned int load(const char *name, int *handle, perror(name); exit(3); } - switch ( (pe_hdr.signature =3D=3D PE_SIGNATURE && - pe_hdr.opt_hdr_size > sizeof(pe_hdr.opt_hdr)) * - pe_hdr.opt_hdr.magic ) + switch ( (pe_hdr.magic =3D=3D PE_MAGIC && + pe_hdr.opt_hdr_size > sizeof(pe32_opt_hdr)) * + pe32_opt_hdr.magic ) { - case PE_MAGIC_EXE32: + case PE_OPT_MAGIC_PE32: *width =3D 32; *image_base =3D base; break; - case PE_MAGIC_EXE32PLUS: + case PE_OPT_MAGIC_PE32PLUS: *width =3D 64; - *image_base =3D ((uint64_t)base << 32) | pe_hdr.opt_hdr.data_rva; + *image_base =3D ((uint64_t)base << 32) | pe32_opt_hdr.data_base; break; default: fprintf(stderr, "%s: Wrong PE file format\n", name); exit(3); } =20 - *sections =3D malloc(pe_hdr.section_count * sizeof(**sections)); + *sections =3D malloc(pe_hdr.sections * sizeof(**sections)); if ( !*sections ) { perror(NULL); exit(4); } - if ( lseek(in, - mz_hdr.extended_header_base + offsetof(struct pe_hdr, opt_h= dr) + - pe_hdr.opt_hdr_size, + if ( lseek(in, mz_hdr.peaddr + sizeof(pe_hdr) + pe_hdr.opt_hdr_size, SEEK_SET) < 0 || - read(in, *sections, pe_hdr.section_count * sizeof(**sections)) != =3D - pe_hdr.section_count * sizeof(**sections) ) + read(in, *sections, pe_hdr.sections * sizeof(**sections)) !=3D + pe_hdr.sections * sizeof(**sections) ) { perror(name); exit(4); @@ -153,12 +99,12 @@ static unsigned int load(const char *name, int *handle, =20 *handle =3D in; =20 - return pe_hdr.section_count; + return pe_hdr.sections; } =20 static long page_size; =20 -static const void *map_section(const struct coff_section *sec, int in, +static const void *map_section(const struct section_header *sec, int in, const char *name) { const char *ptr; @@ -166,10 +112,10 @@ static const void *map_section(const struct coff_sect= ion *sec, int in, =20 if ( !page_size ) page_size =3D sysconf(_SC_PAGESIZE); - offs =3D sec->file_offset & (page_size - 1); + offs =3D sec->data_addr & (page_size - 1); =20 - ptr =3D mmap(0, offs + sec->file_size, PROT_READ, MAP_PRIVATE, in, - sec->file_offset - offs); + ptr =3D mmap(0, offs + sec->raw_data_size, PROT_READ, MAP_PRIVATE, in, + sec->data_addr - offs); if ( ptr =3D=3D MAP_FAILED ) { perror(name); @@ -179,15 +125,15 @@ static const void *map_section(const struct coff_sect= ion *sec, int in, return ptr + offs; } =20 -static void unmap_section(const void *ptr, const struct coff_section *sec) +static void unmap_section(const void *ptr, const struct section_header *se= c) { - unsigned long offs =3D sec->file_offset & (page_size - 1); + unsigned long offs =3D sec->data_addr & (page_size - 1); =20 - munmap((char *)ptr - offs, offs + sec->file_size); + munmap((char *)ptr - offs, offs + sec->raw_data_size); } =20 static void diff_sections(const unsigned char *ptr1, const unsigned char *= ptr2, - const struct coff_section *sec, + const struct section_header *sec, int_fast64_t diff, unsigned int width, uint_fast64_t base, uint_fast64_t end) { @@ -208,7 +154,7 @@ static void diff_sections(const unsigned char *ptr1, co= nst unsigned char *ptr2, while ( !(diff & (((int_fast64_t)1 << ((disp + 1) * CHAR_BIT)) - 1)) ) ++disp; =20 - for ( i =3D 0; i < sec->file_size; ++i ) + for ( i =3D 0; i < sec->raw_data_size; ++i ) { uint_fast32_t rva; union { @@ -222,7 +168,7 @@ static void diff_sections(const unsigned char *ptr1, co= nst unsigned char *ptr2, if ( ptr1[i] =3D=3D ptr2[i] ) continue; =20 - if ( i < disp || i + width - disp > sec->file_size ) + if ( i < disp || i + width - disp > sec->raw_data_size ) { fprintf(stderr, "Bogus difference at %.8s:%08" PRIxFAST32 "\n", @@ -250,11 +196,11 @@ static void diff_sections(const unsigned char *ptr1, = const unsigned char *ptr2, reloc_size +=3D reloc_size & 2; if ( reloc_size ) printf("\t.equ rva_%08" PRIxFAST32 "_relocs," - " %#08" PRIxFAST32 "\n", + " %#08" PRIxFAST32 "\n", cur_rva, reloc_size); printf("\t.balign 4\n" "\t.long %#08" PRIxFAST32 "," - " rva_%08" PRIxFAST32 "_relocs\n", + " rva_%08" PRIxFAST32 "_relocs\n", rva, rva); cur_rva =3D rva; reloc_size =3D 8; @@ -267,7 +213,7 @@ static void diff_sections(const unsigned char *ptr1, co= nst unsigned char *ptr2, exit(3); } =20 - if ( !(sec->flags & COFF_SECTION_WRITEABLE) ) + if ( !(sec->flags & IMAGE_SCN_MEM_WRITE) ) fprintf(stderr, "Warning: relocation to r/o section %.8s:%08" PRIxFAST= 32 "\n", sec->name, i - disp); @@ -285,7 +231,7 @@ int main(int argc, char *argv[]) unsigned int i, nsec, width1, width2; uint_fast64_t base1, base2; uint32_t size1, size2; - struct coff_section *sec1, *sec2; + struct section_header *sec1, *sec2; =20 if ( argc =3D=3D 1 || !strcmp(argv[1], "-?") || @@ -328,16 +274,16 @@ int main(int argc, char *argv[]) =20 if ( memcmp(sec1[i].name, sec2[i].name, sizeof(sec1[i].name)) || sec1[i].rva !=3D sec2[i].rva || - sec1[i].size !=3D sec2[i].size || - sec1[i].file_size !=3D sec2[i].file_size || + sec1[i].virtual_size !=3D sec2[i].virtual_size || + sec1[i].raw_data_size !=3D sec2[i].raw_data_size || sec1[i].flags !=3D sec2[i].flags ) { fprintf(stderr, "Mismatched section %u parameters\n", i); return 5; } =20 - if ( !sec1[i].size || - (sec1[i].flags & (COFF_SECTION_DISCARDABLE|COFF_SECTION_BSS))= ) + if ( !sec1[i].virtual_size || + (sec1[i].flags & (IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_CNT_U= NINITIALIZED_DATA)) ) continue; =20 /* @@ -354,10 +300,10 @@ int main(int argc, char *argv[]) return 3; } =20 - if ( sec1[i].file_size > sec1[i].size ) + if ( sec1[i].raw_data_size > sec1[i].virtual_size ) { - sec1[i].file_size =3D sec1[i].size; - sec2[i].file_size =3D sec2[i].size; + sec1[i].raw_data_size =3D sec1[i].virtual_size; + sec2[i].raw_data_size =3D sec2[i].virtual_size; } ptr1 =3D map_section(sec1 + i, in1, argv[1]); ptr2 =3D map_section(sec2 + i, in2, argv[2]); diff --git a/xen/common/efi/pe.c b/xen/common/efi/pe.c index ef8a2543e0..edaa37006d 100644 --- a/xen/common/efi/pe.c +++ b/xen/common/efi/pe.c @@ -20,32 +20,10 @@ * Lesser General Public License for more details. */ =20 - #include "efi.h" +#include "efi/pe.h" =20 -struct DosFileHeader { - UINT8 Magic[2]; - UINT16 LastSize; - UINT16 nBlocks; - UINT16 nReloc; - UINT16 HdrSize; - UINT16 MinAlloc; - UINT16 MaxAlloc; - UINT16 ss; - UINT16 sp; - UINT16 Checksum; - UINT16 ip; - UINT16 cs; - UINT16 RelocPos; - UINT16 nOverlay; - UINT16 reserved[4]; - UINT16 OEMId; - UINT16 OEMInfo; - UINT16 reserved2[10]; - UINT32 ExeHeader; -}; - -#if defined(__arm__) || defined (__aarch64__) +#if defined(__arm__) || defined(__aarch64__) #define PE_HEADER_MACHINE 0xaa64 #elif defined(__x86_64__) #define PE_HEADER_MACHINE 0x8664 @@ -53,45 +31,17 @@ struct DosFileHeader { #error "Unknown architecture" #endif =20 -struct PeFileHeader { - UINT16 Machine; - UINT16 NumberOfSections; - UINT32 TimeDateStamp; - UINT32 PointerToSymbolTable; - UINT32 NumberOfSymbols; - UINT16 SizeOfOptionalHeader; - UINT16 Characteristics; -}; - -struct PeHeader { - UINT8 Magic[4]; - struct PeFileHeader FileHeader; -}; - -struct PeSectionHeader { - CHAR8 Name[8]; - UINT32 VirtualSize; - UINT32 VirtualAddress; - UINT32 SizeOfRawData; - UINT32 PointerToRawData; - UINT32 PointerToRelocations; - UINT32 PointerToLinenumbers; - UINT16 NumberOfRelocations; - UINT16 NumberOfLinenumbers; - UINT32 Characteristics; -}; - -static bool __init pe_name_compare(const struct PeSectionHeader *sect, +static bool __init pe_name_compare(const struct section_header *sect, const CHAR16 *name) { size_t i; =20 - if ( sect->Name[0] !=3D '.' ) + if ( sect->name[0] !=3D '.' ) return false; =20 - for ( i =3D 1; i < sizeof(sect->Name); i++ ) + for ( i =3D 1; i < sizeof(sect->name); i++ ) { - const char c =3D sect->Name[i]; + const char c =3D sect->name[i]; =20 if ( c !=3D name[i - 1] ) return false; @@ -105,33 +55,29 @@ static bool __init pe_name_compare(const struct PeSect= ionHeader *sect, const void *__init pe_find_section(const void *image, const UINTN image_si= ze, const CHAR16 *section_name, UINTN *size= _out) { - const struct DosFileHeader *dos =3D image; - const struct PeHeader *pe; - const struct PeSectionHeader *sect; + const struct mz_hdr *mz =3D image; + const struct pe_hdr *pe; + const struct section_header *sect; UINTN offset, i; =20 - if ( image_size < sizeof(*dos) || - dos->Magic[0] !=3D 'M' || - dos->Magic[1] !=3D 'Z' ) + if ( image_size < sizeof(*mz) || + mz->magic !=3D MZ_MAGIC ) return NULL; =20 - offset =3D dos->ExeHeader; + offset =3D mz->peaddr; pe =3D image + offset; =20 offset +=3D sizeof(*pe); if ( image_size < offset || - pe->Magic[0] !=3D 'P' || - pe->Magic[1] !=3D 'E' || - pe->Magic[2] !=3D '\0' || - pe->Magic[3] !=3D '\0' ) + pe->magic !=3D PE_MAGIC ) return NULL; =20 - if ( pe->FileHeader.Machine !=3D PE_HEADER_MACHINE ) + if ( pe->machine !=3D PE_HEADER_MACHINE ) return NULL; =20 - offset +=3D pe->FileHeader.SizeOfOptionalHeader; + offset +=3D pe->opt_hdr_size; =20 - for ( i =3D 0; i < pe->FileHeader.NumberOfSections; i++ ) + for ( i =3D 0; i < pe->sections; i++ ) { sect =3D image + offset; if ( image_size < offset + sizeof(*sect) ) @@ -143,13 +89,13 @@ const void *__init pe_find_section(const void *image, = const UINTN image_size, continue; } =20 - if ( image_size < sect->VirtualSize + sect->VirtualAddress ) + if ( image_size < sect->virtual_size + sect->rva ) blexit(L"PE invalid section size + address"); =20 if ( size_out ) - *size_out =3D sect->VirtualSize; + *size_out =3D sect->virtual_size; =20 - return image + sect->VirtualAddress; + return image + sect->rva; } =20 return NULL; --=20 2.25.1