Index: lld/ELF/InputSection.cpp =================================================================== --- lld/ELF/InputSection.cpp +++ lld/ELF/InputSection.cpp @@ -402,7 +402,7 @@ } if (RelTy::IsRela) - P->r_addend = Sym.getVA(Addend) - Section->getOutputSection()->Addr; + P->r_addend = Sym.getVA(Addend) - Section->Repl->getOutputSection()->Addr; else if (Config->Relocatable) Sec->Relocations.push_back({R_ABS, Type, Rel.r_offset, Addend, &Sym}); } Index: lld/test/ELF/icf18.s =================================================================== --- /dev/null +++ lld/test/ELF/icf18.s @@ -0,0 +1,49 @@ +# RUN: yaml2obj %s -o %t.o +# RUN: ld.lld --icf=all --emit-relocs %t.o -o /dev/null --print-icf-sections 2>&1 | FileCheck %s + +# Checks that ICF with --emit-relocs doesn't crash. + +# CHECK: removing identical section +# CHECK: (.text.bar) + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + OSABI: ELFOSABI_FREEBSD + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text.foo + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Content: "FFFFFFFFFFFFFFFF" + - Name: .text.bar + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Content: "FFFFFFFFFFFFFFFF" + - Name: .text.foo_test + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + - Name: .rela.text.foo_test + Type: SHT_RELA + Info: .text.foo_test + Relocations: + - Offset: 0x0000000000000000 + Symbol: .text.foo + Type: R_X86_64_PLT32 + - Offset: 0x0000000000000000 + Symbol: .text.bar + Type: R_X86_64_PLT32 +Symbols: + Local: + - Name: .text.foo + Type: STT_SECTION + Section: .text.foo + - Name: .text.bar + Type: STT_SECTION + Section: .text.bar + Global: + - Name: _start + Type: STT_FUNC + Section: .text.foo_test