[lld] r229961 - PECOFF: Fix base relocation for ImageBase.

Rui Ueyama ruiu at google.com
Thu Feb 19 19:36:00 PST 2015


Author: ruiu
Date: Thu Feb 19 21:35:59 2015
New Revision: 229961

URL: http://llvm.org/viewvc/llvm-project?rev=229961&view=rev
Log:
PECOFF: Fix base relocation for ImageBase.

This is yet another edge case of base relocation for symbols. Absolute
symbols are in general not target of base relocation because absolute
atom is a way to point to a specific memory location. In r229816, I
removed entries for absolute atoms from the base relocation table
(so that they won't be fixed by the loader).

However, there was one exception -- ImageBase. ImageBase points to the
start address of the current image in memory. That needs to be fixed up
at load time. This patch is to treat the symbol in a special manner.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
    lld/trunk/test/pecoff/Inputs/basereloc.obj.yaml
    lld/trunk/test/pecoff/base-reloc.test

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=229961&r1=229960&r2=229961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Feb 19 21:35:59 2015
@@ -784,7 +784,13 @@ void AtomChunk::addBaseRelocations(BaseR
     for (const Reference *ref : *atom) {
       if (ref->kindNamespace() != Reference::KindNamespace::COFF)
         continue;
-      if (isa<AbsoluteAtom>(ref->target()))
+
+      // An absolute symbol points to a fixed location in memory. Their
+      // address should not be fixed at load time. One exception is ImageBase
+      // because that's relative to run-time image base address.
+      if (auto *abs = dyn_cast<AbsoluteAtom>(ref->target()))
+        if (!abs->name().equals("__ImageBase") &&
+            !abs->name().equals("___ImageBase"))
           continue;
 
       uint64_t address = layout->_virtualAddr + ref->offsetInAtom();

Modified: lld/trunk/test/pecoff/Inputs/basereloc.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/basereloc.obj.yaml?rev=229961&r1=229960&r2=229961&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/basereloc.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/basereloc.obj.yaml Thu Feb 19 21:35:59 2015
@@ -6,7 +6,7 @@ sections:
   - Name:            .text
     Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
     Alignment:       4
-    SectionData:     B800000000506800000000680000000050E80000000050E800000000
+    SectionData:     B800000000506800000000680000000050E80000000050E80000000050E800000000
     Relocations:
       - VirtualAddress:  0
         SymbolName:      abs_symbol
@@ -23,6 +23,9 @@ sections:
       - VirtualAddress:  24
         SymbolName:      _ExitProcess at 4
         Type:            IMAGE_REL_I386_REL32
+      - VirtualAddress:  30
+        SymbolName:      ___ImageBase
+        Type:            IMAGE_REL_I386_DIR32
   - Name:            .data
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
     Alignment:       4
@@ -117,4 +120,10 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            ___ImageBase
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
 ...

Modified: lld/trunk/test/pecoff/base-reloc.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/base-reloc.test?rev=229961&r1=229960&r2=229961&view=diff
==============================================================================
--- lld/trunk/test/pecoff/base-reloc.test (original)
+++ lld/trunk/test/pecoff/base-reloc.test Thu Feb 19 21:35:59 2015
@@ -17,6 +17,14 @@ BASEREL-NEXT:   Entry {
 BASEREL-NEXT:     Type: HIGHLOW
 BASEREL-NEXT:     Address: 0x200C
 BASEREL-NEXT:   }
+BASEREL-NEXT:   Entry {
+BASEREL-NEXT:     Type: HIGHLOW
+BASEREL-NEXT:     Address: 0x201E
+BASEREL-NEXT:   }
+BASEREL-NEXT:   Entry {
+BASEREL-NEXT:     Type: ABSOLUTE
+BASEREL-NEXT:     Address: 0x2000
+BASEREL-NEXT:   }
 BASEREL-NEXT: ]
 
 NOBASEREL:      BaseReloc [
@@ -37,9 +45,9 @@ BASEREL-HEADER-NOT: IMAGE_FILE_RELOCS_ST
 NOBASEREL-HEADER: IMAGE_FILE_RELOCS_STRIPPED
 
 BASEREL-HEADER:     BaseRelocationTableRVA: 0x3000
-BASEREL-HEADER:     BaseRelocationTableSize: 0xC
+BASEREL-HEADER:     BaseRelocationTableSize: 0x10
 BASEREL-HEADER:     Name: .reloc (2E 72 65 6C 6F 63 00 00)
-BASEREL-HEADER-NEXT:     VirtualSize: 0xC
+BASEREL-HEADER-NEXT:     VirtualSize: 0x10
 BASEREL-HEADER-NEXT:     VirtualAddress: 0x3000
 BASEREL-HEADER-NEXT:     RawDataSize: 512
 BASEREL-HEADER-NEXT:     PointerToRawData: 0x600





More information about the llvm-commits mailing list