Index: lld/trunk/ELF/SymbolTable.cpp =================================================================== --- lld/trunk/ELF/SymbolTable.cpp +++ lld/trunk/ELF/SymbolTable.cpp @@ -584,9 +584,14 @@ // Process undefined (-u) flags by loading lazy symbols named by those flags. template void SymbolTable::scanUndefinedFlags() { for (StringRef S : Config->Undefined) - if (auto *L = dyn_cast_or_null(find(S))) - if (InputFile *File = L->fetch()) - addFile(File); + if (SymbolBody *B = find(S)) { + // Mark the symbol not to be eliminated by LTO + // even if it is a bitcode symbol. + B->symbol()->IsUsedInRegularObj = true; + if (auto *L = dyn_cast_or_null(B)) + if (InputFile *File = L->fetch()) + addFile(File); + } } // This function takes care of the case in which shared libraries depend on Index: lld/trunk/test/ELF/lto/keep-undefined.ll =================================================================== --- lld/trunk/test/ELF/lto/keep-undefined.ll +++ lld/trunk/test/ELF/lto/keep-undefined.ll @@ -0,0 +1,20 @@ +; REQUIRES: x86 +; This test checks that symbols which are specified in "-u" switches +; are kept over LTO if we link an executable. +; RUN: llvm-as %s -o %t.o +; RUN: ld.lld -m elf_x86_64 %t.o -o %tout -u foo +; RUN: llvm-nm %tout | FileCheck %s + +; CHECK: T foo + +target triple = "x86_64-unknown-linux-gnu" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define void @foo() { + ret void +} + +define void @_start() { + call void @foo() + ret void +}