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.

llvm-svn: 241857
This commit is contained in:
Rui Ueyama 2015-07-09 20:36:59 +00:00
parent a841bb0f5d
commit 93b4571187
2 changed files with 31 additions and 9 deletions

View File

@ -114,16 +114,26 @@ void SectionChunk::addAssociative(SectionChunk *Child) {
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)

View File

@ -3,13 +3,14 @@
# RUN: /entry:main@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: ]