From 761aca1e2e393da62ddaf7c42b61196be2466571 Mon Sep 17 00:00:00 2001 From: Sergey Dmitriev Date: Tue, 5 Jan 2021 09:46:55 -0800 Subject: [PATCH] [llvm-link] fix linker behavior when linking archives with --only-needed option This patch fixes linker behavior when archive is linked with other inputs as a library (i.e. when --only-needed option is specified). In this case library is expected to be normally linked first into a separate module and only after that linker should import required symbols from the linked library module. Reviewed By: tra Differential Revision: https://reviews.llvm.org/D92535 --- llvm/test/tools/llvm-link/Inputs/i.ll | 8 ++++++++ llvm/test/tools/llvm-link/archive-only-needed.ll | 15 +++++++++++++++ llvm/tools/llvm-link/llvm-link.cpp | 12 +++++------- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 llvm/test/tools/llvm-link/Inputs/i.ll create mode 100644 llvm/test/tools/llvm-link/archive-only-needed.ll diff --git a/llvm/test/tools/llvm-link/Inputs/i.ll b/llvm/test/tools/llvm-link/Inputs/i.ll new file mode 100644 index 000000000000..d23df3792a60 --- /dev/null +++ b/llvm/test/tools/llvm-link/Inputs/i.ll @@ -0,0 +1,8 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define void @i() { + call void @f() + ret void +} + +declare void @f() diff --git a/llvm/test/tools/llvm-link/archive-only-needed.ll b/llvm/test/tools/llvm-link/archive-only-needed.ll new file mode 100644 index 000000000000..d997e6c71b9c --- /dev/null +++ b/llvm/test/tools/llvm-link/archive-only-needed.ll @@ -0,0 +1,15 @@ +; RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc +; RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc +; RUN: llvm-as %S/Inputs/i.ll -o %t.i.bc +; RUN: rm -f %t.lib +; RUN: llvm-ar cr %t.lib %t.f.bc %t.g.bc %t.i.bc +; RUN: llvm-link %s %t.lib -o %t.linked.bc --only-needed +; RUN: llvm-nm %t.linked.bc | FileCheck %s + +; CHECK: -------- T f +; CHECK: -------- T i + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +@i = external global i8* +@llvm.used = appending global [1 x i8*] [i8* bitcast (i8** @i to i8*)], section "llvm.metadata" diff --git a/llvm/tools/llvm-link/llvm-link.cpp b/llvm/tools/llvm-link/llvm-link.cpp index 6996c5bfbe76..61d0c1561054 100644 --- a/llvm/tools/llvm-link/llvm-link.cpp +++ b/llvm/tools/llvm-link/llvm-link.cpp @@ -142,9 +142,9 @@ static std::unique_ptr loadFile(const char *argv0, return Result; } -static std::unique_ptr -loadArFile(const char *Argv0, std::unique_ptr Buffer, - LLVMContext &Context, unsigned OrigFlags, unsigned ApplicableFlags) { +static std::unique_ptr loadArFile(const char *Argv0, + std::unique_ptr Buffer, + LLVMContext &Context) { std::unique_ptr Result(new Module("ArchiveModule", Context)); StringRef ArchiveName = Buffer->getBufferIdentifier(); if (Verbose) @@ -197,9 +197,8 @@ loadArFile(const char *Argv0, std::unique_ptr Buffer, } if (Verbose) errs() << "Linking member '" << ChildName << "' of archive library.\n"; - if (Linker::linkModules(*Result, std::move(M), ApplicableFlags)) + if (Linker::linkModules(*Result, std::move(M))) return nullptr; - ApplicableFlags = OrigFlags; } // end for each child ExitOnErr(std::move(Err)); return Result; @@ -354,8 +353,7 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L, std::unique_ptr M = identify_magic(Buffer->getBuffer()) == file_magic::archive - ? loadArFile(argv0, std::move(Buffer), Context, Flags, - ApplicableFlags) + ? loadArFile(argv0, std::move(Buffer), Context) : loadFile(argv0, std::move(Buffer), Context); if (!M.get()) { errs() << argv0 << ": ";