[PATCH] D54946: [yaml2obj] [COFF] Subtract the image base for section virtual addresses

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 27 05:31:22 PST 2018


mstorsjo created this revision.
mstorsjo added reviewers: rnk, zturner, ruiu.
Herald added a subscriber: javed.absar.

Serialized yaml versions of COFF executables have section addresses as absolute addresses, while the section header itself stores the relative virtual address. When writing fields back into an executable, subtract the image base.

Alternatively, if we want the yaml representation to be a more exact dump of the source file, we could change obj2yaml instead.


Repository:
  rL LLVM

https://reviews.llvm.org/D54946

Files:
  test/tools/yaml2obj/coff-arm64.yaml
  tools/yaml2obj/yaml2coff.cpp


Index: tools/yaml2obj/yaml2coff.cpp
===================================================================
--- tools/yaml2obj/yaml2coff.cpp
+++ tools/yaml2obj/yaml2coff.cpp
@@ -458,16 +458,19 @@
        << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
        << binary_le(CP.Obj.Header.Characteristics);
   }
+  uint64_t ImageBase = 0;
   if (CP.isPE()) {
     if (CP.is64Bit()) {
       object::pe32plus_header PEH;
       initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH);
       OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
+      ImageBase = PEH.ImageBase;
     } else {
       object::pe32_header PEH;
       uint32_t BaseOfData = initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH);
       PEH.BaseOfData = BaseOfData;
       OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
+      ImageBase = PEH.ImageBase;
     }
     for (const Optional<COFF::DataDirectory> &DD :
          CP.Obj.OptionalHeader->DataDirectories) {
@@ -490,7 +493,7 @@
                                                 i != e; ++i) {
     OS.write(i->Header.Name, COFF::NameSize);
     OS << binary_le(i->Header.VirtualSize)
-       << binary_le(i->Header.VirtualAddress)
+       << binary_le(uint32_t(i->Header.VirtualAddress - ImageBase))
        << binary_le(i->Header.SizeOfRawData)
        << binary_le(i->Header.PointerToRawData)
        << binary_le(i->Header.PointerToRelocations)
Index: test/tools/yaml2obj/coff-arm64.yaml
===================================================================
--- test/tools/yaml2obj/coff-arm64.yaml
+++ test/tools/yaml2obj/coff-arm64.yaml
@@ -1,8 +1,13 @@
 # RUN: yaml2obj %s -o %t
 # RUN: llvm-readobj -file-headers %t | FileCheck %s
+# RUN: llvm-readobj -sections %t | FileCheck %s --check-prefix=SECTIONS
 
 # CHECK: OptionalHeaderSize: 240
 
+# SECTIONS:     VirtualAddress: 0x1000
+# SECTIONS:     VirtualAddress: 0x2000
+# SECTIONS:     VirtualAddress: 0x3000
+
 --- !COFF
 OptionalHeader:  
   AddressOfEntryPoint: 4096


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54946.175461.patch
Type: text/x-patch
Size: 1979 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181127/94d7141b/attachment.bin>


More information about the llvm-commits mailing list