[lld] r241857 - COFF: Implement base relocations for x86.

Rui Ueyama ruiu at google.com
Thu Jul 9 13:36:59 PDT 2015


Author: ruiu
Date: Thu Jul  9 15:36:59 2015
New Revision: 241857

URL: http://llvm.org/viewvc/llvm-project?rev=241857&view=rev
Log:
COFF: Implement base relocations for x86.

With this patch, LLD is now able to self-link an .exe file for x86
that runs correctly, although I don't think some headers (particularly
SEH) are not correct. DLL support is coming soon.

Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/test/COFF/hello32.test

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=241857&r1=241856&r2=241857&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Thu Jul  9 15:36:59 2015
@@ -114,16 +114,26 @@ void SectionChunk::addAssociative(Sectio
   Child->Root = false;
 }
 
+static bool isAbs(const coff_relocation &Rel) {
+  switch (Config->MachineType) {
+  case IMAGE_FILE_MACHINE_AMD64:
+    return Rel.Type == IMAGE_REL_AMD64_ADDR64;
+  case IMAGE_FILE_MACHINE_I386:
+    return Rel.Type == IMAGE_REL_I386_DIR32;
+  default:
+    llvm_unreachable("unknown machine type");
+  }
+}
+
 // Windows-specific.
-// Collect all locations that contain absolute 64-bit addresses,
-// which need to be fixed by the loader if load-time relocation is needed.
+// Collect all locations that contain absolute addresses, which need to be
+// fixed by the loader if load-time relocation is needed.
 // Only called when base relocation is enabled.
 void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
   for (const coff_relocation &Rel : Relocs) {
-    // ADDR64 relocations contain absolute addresses.
     // Symbol __ImageBase is special -- it's an absolute symbol, but its
     // address never changes even if image is relocated.
-    if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
+    if (!isAbs(Rel))
       continue;
     SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
     if (Body == ImageBase)

Modified: lld/trunk/test/COFF/hello32.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/hello32.test?rev=241857&r1=241856&r2=241857&view=diff
==============================================================================
--- lld/trunk/test/COFF/hello32.test (original)
+++ lld/trunk/test/COFF/hello32.test Thu Jul  9 15:36:59 2015
@@ -3,13 +3,14 @@
 # RUN:   /entry:main at 0 /out:%t.exe
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=HEADER %s
 # RUN: llvm-readobj -coff-imports %t.exe | FileCheck -check-prefix=IMPORTS %s
+# RUN: llvm-readobj -coff-basereloc %t.exe | FileCheck -check-prefix=BASEREL %s
 
 HEADER:      Format: COFF-i386
 HEADER-NEXT: Arch: i386
 HEADER-NEXT: AddressSize: 32bit
 HEADER-NEXT: ImageFileHeader {
 HEADER-NEXT:   Machine: IMAGE_FILE_MACHINE_I386 (0x14C)
-HEADER-NEXT:   SectionCount: 3
+HEADER-NEXT:   SectionCount: 4
 HEADER-NEXT:   TimeDateStamp: 1970-01-01 00:00:00 (0x0)
 HEADER-NEXT:   PointerToSymbolTable: 0x0
 HEADER-NEXT:   SymbolCount: 0
@@ -23,7 +24,7 @@ HEADER-NEXT: ImageOptionalHeader {
 HEADER-NEXT:   MajorLinkerVersion: 0
 HEADER-NEXT:   MinorLinkerVersion: 0
 HEADER-NEXT:   SizeOfCode: 512
-HEADER-NEXT:   SizeOfInitializedData: 1024
+HEADER-NEXT:   SizeOfInitializedData: 1536
 HEADER-NEXT:   SizeOfUninitializedData: 0
 HEADER-NEXT:   AddressOfEntryPoint: 0x2000
 HEADER-NEXT:   BaseOfCode: 0x2000
@@ -37,7 +38,7 @@ HEADER-NEXT:   MajorImageVersion: 0
 HEADER-NEXT:   MinorImageVersion: 0
 HEADER-NEXT:   MajorSubsystemVersion: 6
 HEADER-NEXT:   MinorSubsystemVersion: 0
-HEADER-NEXT:   SizeOfImage: 16384
+HEADER-NEXT:   SizeOfImage: 20480
 HEADER-NEXT:   SizeOfHeaders: 4096
 HEADER-NEXT:   Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI (0x3)
 HEADER-NEXT:   Characteristics [ (0x8160)
@@ -62,8 +63,8 @@ HEADER-NEXT:     ExceptionTableRVA: 0x0
 HEADER-NEXT:     ExceptionTableSize: 0x0
 HEADER-NEXT:     CertificateTableRVA: 0x0
 HEADER-NEXT:     CertificateTableSize: 0x0
-HEADER-NEXT:     BaseRelocationTableRVA: 0x0
-HEADER-NEXT:     BaseRelocationTableSize: 0x0
+HEADER-NEXT:     BaseRelocationTableRVA: 0x4000
+HEADER-NEXT:     BaseRelocationTableSize: 0xC
 HEADER-NEXT:     DebugRVA: 0x0
 HEADER-NEXT:     DebugSize: 0x0
 HEADER-NEXT:     ArchitectureRVA: 0x0
@@ -116,3 +117,14 @@ IMPORTS:   ImportAddressTableRVA: 0x3034
 IMPORTS:   Symbol: ExitProcess (0)
 IMPORTS:   Symbol: MessageBoxA (1)
 IMPORTS: }
+
+BASEREL: BaseReloc [
+BASEREL:   Entry {
+BASEREL:     Type: DIR64
+BASEREL:     Address: 0x2005
+BASEREL:   }
+BASEREL:   Entry {
+BASEREL:     Type: DIR64
+BASEREL:     Address: 0x200C
+BASEREL:   }
+BASEREL: ]





More information about the llvm-commits mailing list