diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2579,9 +2579,21 @@ // // With this the symbol table should be complete. After this, no new names // except a few linker-synthesized ones will be added to the symbol table. + const size_t numFilesBeforeLTO = files.size(); const size_t numObjsBeforeLTO = objectFiles.size(); invokeELFT(compileBitcodeFiles, skipLinkedOutput); + // Adding the LTO object file to the link may have added additional input + // files to the link, e.g. if a builtin libcall was resolved using an object + // file with deplibs. + { + llvm::TimeTraceScope timeScope("Parse input files"); + for (size_t i = numFilesBeforeLTO; i < files.size(); ++i) { + llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName()); + parseFile(files[i]); + } + } + // Symbol resolution finished. Report backward reference problems, // --print-archive-stats=, and --why-extract=. reportBackrefs(); diff --git a/lld/test/ELF/deplibs-lto.s b/lld/test/ELF/deplibs-lto.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/deplibs-lto.s @@ -0,0 +1,42 @@ +# REQUIRES: aarch64 + +# RUN: rm -rf %t.inputs %t.dir +# RUN: mkdir -p %t.inputs %t.dir +# RUN: split-file %s %t.inputs +# RUN: llvm-mc -filetype=obj -triple=aarch64 %t.inputs/deplibs.s -o %tdeplibs.o +# RUN: llvm-mc -filetype=obj -triple=aarch64 %t.inputs/foo.s -o %tfoo.o +# RUN: llvm-as %t.inputs/lto.ll -o %t.o +# RUN: llvm-ar rc %t.dir/libdeplibs.a %tdeplibs.o +# RUN: llvm-ar rc %t.dir/libfoo.a %tfoo.o + +# LTO can emit libcalls that are resolved using libraries that contain .deplibs. +# Such libraries must be handled as if they were input files, and any referenced +# deplibs must be added to the link. Otherwise, symbols in the library may go +# undefined. + +# RUN: ld.lld %t.o -u a -o /dev/null -L %t.dir -ldeplibs --trace 2>&1 | \ +# RUN: FileCheck %s -DOBJ=%t.o -DDIR=%t.dir --check-prefix=LIBA-DIR + +# LIBA-DIR: [[OBJ]] +# LIBA-DIR-NEXT: [[DIR]]{{[\\/]}}libdeplibs.a +# LIBA-DIR-NEXT: [[DIR]]{{[\\/]}}libfoo.a + +#--- foo.s +.global foo +foo: +#--- deplibs.s +.global __aarch64_ldadd4_relax +__aarch64_ldadd4_relax: + b foo +.section ".deplibs","MS",@llvm_dependent_libraries,1 + .asciz "foo" +#--- lto.ll +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +target triple = "aarch64" + +define void @a(i32* nocapture %0) #0 { + %2 = atomicrmw add i32* %0, i32 1 monotonic, align 4 + ret void +} + +attributes #0 = { "target-features"="+outline-atomics" }