diff --git a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp index 8dcfcc142a9c..6459084d9e54 100644 --- a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp +++ b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp @@ -784,7 +784,13 @@ void AtomChunk::addBaseRelocations(BaseRelocationList &relocSites) const { for (const Reference *ref : *atom) { if (ref->kindNamespace() != Reference::KindNamespace::COFF) continue; - if (isa(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(ref->target())) + if (!abs->name().equals("__ImageBase") && + !abs->name().equals("___ImageBase")) continue; uint64_t address = layout->_virtualAddr + ref->offsetInAtom(); diff --git a/lld/test/pecoff/Inputs/basereloc.obj.yaml b/lld/test/pecoff/Inputs/basereloc.obj.yaml index 088e9c83878f..34312e5d75b5 100644 --- a/lld/test/pecoff/Inputs/basereloc.obj.yaml +++ b/lld/test/pecoff/Inputs/basereloc.obj.yaml @@ -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@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 ... diff --git a/lld/test/pecoff/base-reloc.test b/lld/test/pecoff/base-reloc.test index 867cbcb38bf9..54c47a1b59cc 100644 --- a/lld/test/pecoff/base-reloc.test +++ b/lld/test/pecoff/base-reloc.test @@ -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_STRIPPED 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