// 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:
// 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.
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");
}
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
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
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
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