diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index 3710bef345ee..098e842597dd 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -154,8 +154,14 @@ void BitcodeCompiler::add(BitcodeFile &F) { R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym()) || UsedStartStop.count(ObjSym.getSectionName()); + const auto *DR = dyn_cast(Sym); R.FinalDefinitionInLinkageUnit = - Sym->isDefined() && (IsExecutable || Sym->Visibility != STV_DEFAULT); + (IsExecutable || Sym->Visibility != STV_DEFAULT) && DR && + // Skip absolute symbols from ELF objects, otherwise PC-rel relocations + // will be generated by for them, triggering linker errors. + // Symbol section is always null for bitcode symbols, hence the check + // for isElf(). + !(Sym->File && Sym->File->isElf() && DR->Section == nullptr); if (R.Prevailing) undefine(Sym); diff --git a/lld/test/ELF/lto/Inputs/absolute.s b/lld/test/ELF/lto/Inputs/absolute.s new file mode 100644 index 000000000000..63fbc9f9234c --- /dev/null +++ b/lld/test/ELF/lto/Inputs/absolute.s @@ -0,0 +1,2 @@ +.globl blah + blah = 0xdeadbeef diff --git a/lld/test/ELF/lto/abs-resol.ll b/lld/test/ELF/lto/abs-resol.ll new file mode 100644 index 000000000000..eddf49988a95 --- /dev/null +++ b/lld/test/ELF/lto/abs-resol.ll @@ -0,0 +1,14 @@ +; REQUIRES: x86 + +; RUN: llvm-as %s -o %t.o +; RUN: llvm-mc -triple=x86_64-pc-linux %p/Inputs/absolute.s -o %t2.o -filetype=obj +; RUN: ld.lld %t.o %t2.o -o %t.out -pie + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@blah = external global i8, align 1 + +define i8* @_start() { + ret i8* @blah +}