[PATCH] D70464: [COFFYAML] Use PE32PlusHeader for storing the optional header

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 19 14:24:23 PST 2019


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
Herald added a project: LLVM.

This avoids truncating the 64 bit ImageBase for 64 bit executables.

PE32Header and PE32PlusHeader have the same set of fields, except for BaseOfData which is missing in PE32PlusHeader.

In COFFYAML, BaseOfData is never explicitly stored and calculated when written back to an executable, so we can simply use PE32PlusHeader as the intermediate storage without any further changes.

This requires adding PE32PlusHeader to BinaryFormat/COFF.h, as it previously only had the PE32Header variant. (Only Object/COFF.h had pe32plus_header.)

Alternatively, COFFYAML.h could be made to use Object/COFF.h and the pe32plus_header definition from there (which stores structs with on-disk endianness instead of the native ones).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70464

Files:
  llvm/include/llvm/BinaryFormat/COFF.h
  llvm/include/llvm/ObjectYAML/COFFYAML.h
  llvm/test/tools/yaml2obj/coff-arm64.yaml


Index: llvm/test/tools/yaml2obj/coff-arm64.yaml
===================================================================
--- llvm/test/tools/yaml2obj/coff-arm64.yaml
+++ llvm/test/tools/yaml2obj/coff-arm64.yaml
@@ -3,7 +3,9 @@
 # RUN: obj2yaml %t | FileCheck %s --check-prefix=ROUNDTRIP
 
 # CHECK: OptionalHeaderSize: 240
+# CHECK: ImageBase: 0x140000000
 
+# ROUNDTRIP: ImageBase: 5368709120
 # ROUNDTRIP: VirtualAddress:  4096
 # ROUNDTRIP: VirtualAddress:  8192
 # ROUNDTRIP: VirtualAddress:  12288
@@ -11,7 +13,7 @@
 --- !COFF
 OptionalHeader:  
   AddressOfEntryPoint: 4096
-  ImageBase:       1073741824
+  ImageBase:       5368709120
   SectionAlignment: 4096
   FileAlignment:   512
   MajorOperatingSystemVersion: 6
Index: llvm/include/llvm/ObjectYAML/COFFYAML.h
===================================================================
--- llvm/include/llvm/ObjectYAML/COFFYAML.h
+++ llvm/include/llvm/ObjectYAML/COFFYAML.h
@@ -96,7 +96,7 @@
 };
 
 struct PEHeader {
-  COFF::PE32Header Header;
+  COFF::PE32PlusHeader Header;
   Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
 };
 
Index: llvm/include/llvm/BinaryFormat/COFF.h
===================================================================
--- llvm/include/llvm/BinaryFormat/COFF.h
+++ llvm/include/llvm/BinaryFormat/COFF.h
@@ -572,6 +572,40 @@
   uint32_t NumberOfRvaAndSize;
 };
 
+struct PE32PlusHeader {
+  uint16_t Magic;
+  uint8_t MajorLinkerVersion;
+  uint8_t MinorLinkerVersion;
+  uint32_t SizeOfCode;
+  uint32_t SizeOfInitializedData;
+  uint32_t SizeOfUninitializedData;
+  uint32_t AddressOfEntryPoint; // RVA
+  uint32_t BaseOfCode;          // RVA
+  uint64_t ImageBase;
+  uint32_t SectionAlignment;
+  uint32_t FileAlignment;
+  uint16_t MajorOperatingSystemVersion;
+  uint16_t MinorOperatingSystemVersion;
+  uint16_t MajorImageVersion;
+  uint16_t MinorImageVersion;
+  uint16_t MajorSubsystemVersion;
+  uint16_t MinorSubsystemVersion;
+  uint32_t Win32VersionValue;
+  uint32_t SizeOfImage;
+  uint32_t SizeOfHeaders;
+  uint32_t CheckSum;
+  uint16_t Subsystem;
+  // FIXME: This should be DllCharacteristics to match the COFF spec.
+  uint16_t DLLCharacteristics;
+  uint64_t SizeOfStackReserve;
+  uint64_t SizeOfStackCommit;
+  uint64_t SizeOfHeapReserve;
+  uint64_t SizeOfHeapCommit;
+  uint32_t LoaderFlags;
+  // FIXME: This should be NumberOfRvaAndSizes to match the COFF spec.
+  uint32_t NumberOfRvaAndSize;
+};
+
 struct DataDirectory {
   uint32_t RelativeVirtualAddress;
   uint32_t Size;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70464.230150.patch
Type: text/x-patch
Size: 2511 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191119/a77d50dd/attachment.bin>


More information about the llvm-commits mailing list