[PECOFF][Writer] Support DIR32NB and REL32 relocation types.
authorRui Ueyama <ruiu@google.com>
Fri, 28 Jun 2013 04:40:52 +0000 (04:40 +0000)
committerRui Ueyama <ruiu@google.com>
Fri, 28 Jun 2013 04:40:52 +0000 (04:40 +0000)
llvm-svn: 185133

lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
lld/test/pecoff/importlib.test
lld/test/pecoff/lib.test
lld/test/pecoff/multi.test
lld/test/pecoff/reloc.test

index 6ed30fa..ec429bb 100644 (file)
@@ -56,6 +56,9 @@ const int PAGE_SIZE = 4096;
 // file.
 const int SECTOR_SIZE = 512;
 
+// The address of the executable when loaded into memory.
+const int32_t IMAGE_BASE = 0x400000;
+
 /// A Chunk is an abstrace contiguous range in an output file.
 class Chunk {
 public:
@@ -159,7 +162,7 @@ public:
 
     // The address of the executable when loaded into memory. The default for
     // DLLs is 0x10000000. The default for executables is 0x400000.
-    _peHeader.ImageBase = 0x400000;
+    _peHeader.ImageBase = IMAGE_BASE;
 
     // Sections should be page-aligned when loaded into memory, which is 4KB on
     // x86.
@@ -348,12 +351,24 @@ public:
           continue;
 
         switch (ref->kind()) {
+        case llvm::COFF::IMAGE_REL_I386_ABSOLUTE:
+          // This relocation is no-op.
+          break;
         case llvm::COFF::IMAGE_REL_I386_DIR32:
+          // Set target's 32-bit VA.
+          *relocSite = targetAddr + IMAGE_BASE;
+          break;
+        case llvm::COFF::IMAGE_REL_I386_DIR32NB:
+          // Set target's 32-bit RVA.
           *relocSite = targetAddr;
           break;
-        case llvm::COFF::IMAGE_REL_I386_REL32:
-          // TODO: Implement this relocation
+        case llvm::COFF::IMAGE_REL_I386_REL32: {
+          // Set 32-bit relative address of the target. This relocation is
+          // usually used for relative branch or call instruction.
+          uint32_t disp = atomToVirtualAddr[atom] + ref->offsetInAtom() + 4;
+          *relocSite = targetAddr - disp;
           break;
+        }
         default:
           llvm_unreachable("Unsupported relocation kind");
         }
index 419a700..84b4951 100644 (file)
@@ -7,6 +7,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000:       a1 00 00 00 00
-CHECK:     1005:       03 05 00 00 00 00
+CHECK:     1000:       a1 00 00 40 00
+CHECK:     1005:       03 05 00 00 40 00
 CHECK:     100b:       c3
index 9eb4b0b..9c0d9af 100644 (file)
@@ -6,6 +6,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000:       a1 00 20 00 00
-CHECK:     1005:       03 05 04 20 00 00
+CHECK:     1000:       a1 00 20 40 00
+CHECK:     1005:       03 05 04 20 40 00
 CHECK:     100b:       c3
index 7f7c124..458729d 100644 (file)
@@ -7,6 +7,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000:       a1 00 20 00 00
-CHECK:     1005:       03 05 04 20 00 00
+CHECK:     1000:       a1 00 20 40 00
+CHECK:     1005:       03 05 04 20 40 00
 CHECK:     100b:       c3
index 3afbc0a..974cbcd 100644 (file)
@@ -18,9 +18,9 @@ AFTER: Disassembly of section .text:
 AFTER: .text:
 AFTER:     1000:       b8 00 00 00 00
 AFTER:     1005:       50
-AFTER:     1006:       68 00 20 00 00
-AFTER:     100b:       68 06 20 00 00
+AFTER:     1006:       68 00 20 40 00
+AFTER:     100b:       68 06 20 40 00
 AFTER:     1010:       50
-AFTER:     1011:       e8 00 00 00 00
+AFTER:     1011:       e8 ea ef ff ff
 AFTER:     1016:       50
-AFTER:     1017:       e8 00 00 00 00
+AFTER:     1017:       e8 e4 ef ff ff