diff options
41 files changed, 382 insertions, 76 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 3a84bdf94c8..e592442e6bd 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -446,6 +446,19 @@ void CodeGenModule::Release() { EmitModuleLinkOptions(); } + // On ELF we pass the dependent library specifiers directly to the linker + // without manipulating them. This is in contrast to other platforms where + // they are mapped to a specific linker option by the compiler. This + // difference is a result of the greater variety of ELF linkers and the fact + // that ELF linkers tend to handle libraries in a more complicated fashion + // than on other platforms. This forces us to defer handling the dependent + // libs to the linker. + if (!ELFDependentLibraries.empty()) { + auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries"); + for (auto *MD : ELFDependentLibraries) + NMD->addOperand(MD); + } + // Record mregparm value now so it is visible through rest of codegen. if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", @@ -1903,17 +1916,18 @@ void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); } -void CodeGenModule::AddELFLibDirective(StringRef Lib) { +void CodeGenModule::AddDependentLib(StringRef Lib) { auto &C = getLLVMContext(); - LinkerOptionsMetadata.push_back(llvm::MDNode::get( - C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)})); -} + if (getTarget().getTriple().isOSBinFormatELF()) { + ELFDependentLibraries.push_back( + llvm::MDNode::get(C, llvm::MDString::get(C, Lib))); + return; + } -void CodeGenModule::AddDependentLib(StringRef Lib) { llvm::SmallString<24> Opt; getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); - LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); + LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts)); } /// Add link options implied by the given module, including modules @@ -1936,7 +1950,6 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, // described by this module. llvm::LLVMContext &Context = CGM.getLLVMContext(); bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF(); - bool IsPS4 = CGM.getTarget().getTriple().isPS4(); // For modules that use export_as for linking, use that module // name instead. @@ -1956,7 +1969,7 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, } // Link against a library. - if (IsELF && !IsPS4) { + if (IsELF) { llvm::Metadata *Args[2] = { llvm::MDString::get(Context, "lib"), llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library), @@ -5197,10 +5210,6 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) { AppendLinkerOptions(PCD->getArg()); break; case PCK_Lib: - if (getTarget().getTriple().isOSBinFormatELF() && - !getTarget().getTriple().isPS4()) - AddELFLibDirective(PCD->getArg()); - else AddDependentLib(PCD->getArg()); break; case PCK_Compiler: diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 61ce3ab2275..332177f6642 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -465,9 +465,12 @@ private: /// have been emitted. llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers; - /// A vector of metadata strings. + /// A vector of metadata strings for linker options. SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata; + /// A vector of metadata strings for dependent libraries for ELF. + SmallVector<llvm::MDNode *, 16> ELFDependentLibraries; + /// @name Cache for Objective-C runtime types /// @{ @@ -1152,11 +1155,9 @@ public: /// Appends a detect mismatch command to the linker options. void AddDetectMismatch(StringRef Name, StringRef Value); - /// Appends a dependent lib to the "llvm.linker.options" metadata - /// value. + /// Appends a dependent lib to the appropriate metadata value. void AddDependentLib(StringRef Lib); - void AddELFLibDirective(StringRef Lib); llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD); diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index 432e55da411..abd630411d2 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -2348,22 +2348,6 @@ public: } }; -class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { -public: - PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) - : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} - - void getDependentLibraryOption(llvm::StringRef Lib, - llvm::SmallString<24> &Opt) const override { - Opt = "\01"; - // If the argument contains a space, enclose it in quotes. - if (Lib.find(" ") != StringRef::npos) - Opt += "\"" + Lib.str() + "\""; - else - Opt += Lib; - } -}; - static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { // If the argument does not end in .lib, automatically add the suffix. // If the argument contains a space, enclose it in quotes. @@ -9493,8 +9477,6 @@ const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { switch (Triple.getOS()) { case llvm::Triple::Win32: return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); - case llvm::Triple::PS4: - return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); default: return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); } diff --git a/clang/test/CodeGen/dependent-lib.c b/clang/test/CodeGen/dependent-lib.c index 7178a29692a..fdd2ff3957b 100644 --- a/clang/test/CodeGen/dependent-lib.c +++ b/clang/test/CodeGen/dependent-lib.c @@ -1,12 +1,12 @@ // RUN: %clang_cc1 %s --dependent-lib=msvcrt -triple thumbv7-windows -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 %s --dependent-lib=msvcrt -triple i686-pc-win32 -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 %s --dependent-lib=msvcrt -triple x86_64-pc-win32 -emit-llvm -o - | FileCheck %s -// RUN: %clang_cc1 %s --dependent-lib=msvcrt -triple i686-pc-linux -emit-llvm -o - | FileCheck -check-prefix LINUX %s +// RUN: %clang_cc1 %s --dependent-lib=msvcrt -triple i686-pc-linux -emit-llvm -o - | FileCheck -check-prefix LINUX %s --implicit-check-not llvm.linker.options // CHECK: !llvm.linker.options = !{![[msvcrt:[0-9]+]]} // CHECK: ![[msvcrt]] = !{!"/DEFAULTLIB:msvcrt.lib"} -// LINUX: !llvm.linker.options = !{![[msvcrt:[0-9]+]]} -// LINUX: ![[msvcrt]] = !{!"-lmsvcrt"} +// LINUX: !llvm.dependent-libraries = !{![[msvcrt:[0-9]+]]} +// LINUX: ![[msvcrt]] = !{!"msvcrt"} int f(); diff --git a/clang/test/CodeGen/elf-linker-options.c b/clang/test/CodeGen/elf-linker-options.c deleted file mode 100644 index cf2d1b92b99..00000000000 --- a/clang/test/CodeGen/elf-linker-options.c +++ /dev/null @@ -1,7 +0,0 @@ -// RUN: %clang_cc1 -triple i686---elf -emit-llvm %s -o - | FileCheck %s - -#pragma comment(lib, "alpha") - -// CHECK: !llvm.linker.options = !{[[NODE:![0-9]+]]} -// CHECK: [[NODE]] = !{!"lib", !"alpha"} - diff --git a/clang/test/CodeGen/pragma-comment.c b/clang/test/CodeGen/pragma-comment.c index 1896e5c0fb9..25675d94c5a 100644 --- a/clang/test/CodeGen/pragma-comment.c +++ b/clang/test/CodeGen/pragma-comment.c @@ -1,9 +1,9 @@ // RUN: %clang_cc1 %s -triple i686-pc-win32 -fms-extensions -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 %s -triple thumbv7-windows -fms-extensions -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 %s -triple x86_64-pc-win32 -fms-extensions -emit-llvm -o - | FileCheck %s -// RUN: %clang_cc1 %s -triple thumbv7-linux-gnueabihf -fms-extensions -emit-llvm -o - | FileCheck -check-prefix LINUX %s -// RUN: %clang_cc1 %s -triple i686-pc-linux -fms-extensions -emit-llvm -o - | FileCheck -check-prefix LINUX %s -// RUN: %clang_cc1 %s -triple x86_64-scei-ps4 -fms-extensions -emit-llvm -o - | FileCheck -check-prefix PS4 %s +// RUN: %clang_cc1 %s -triple thumbv7-linux-gnueabihf -fms-extensions -emit-llvm -o - | FileCheck -check-prefix ELF %s --implicit-check-not llvm.linker.options +// RUN: %clang_cc1 %s -triple i686-pc-linux -fms-extensions -emit-llvm -o - | FileCheck -check-prefix ELF %s --implicit-check-not llvm.linker.options +// RUN: %clang_cc1 %s -triple x86_64-scei-ps4 -fms-extensions -emit-llvm -o - | FileCheck -check-prefix ELF %s --implicit-check-not llvm.linker.options // RUN: %clang_cc1 %s -triple aarch64-windows-msvc -fms-extensions -emit-llvm -o - | FileCheck %s #pragma comment(lib, "msvcrt.lib") @@ -23,11 +23,10 @@ // CHECK: ![[bar]] = !{!" /bar=2"} // CHECK: ![[foo]] = !{!" /foo=\22foo bar\22"} -// LINUX: !{!"lib", !"msvcrt.lib"} -// LINUX: !{!"lib", !"kernel32"} -// LINUX: !{!"lib", !"USER32.LIB"} - -// PS4: !{!"\01msvcrt.lib"} -// PS4: !{!"\01kernel32"} -// PS4: !{!"\01USER32.LIB"} -// PS4: !{!"\01\22with space\22"} +// ELF: !llvm.dependent-libraries = !{![[msvcrt:[0-9]+]], ![[kernel32:[0-9]+]], ![[USER32:[0-9]+]], ![[space:[0-9]+]] +// ELF: ![[msvcrt]] = !{!"msvcrt.lib"} +// ELF: ![[kernel32]] = !{!"kernel32"} +// ELF: ![[USER32]] = !{!"USER32.LIB"} +// ELF: ![[space]] = !{!"with space"} +// ELF-NOT: bar +// ELF-NOT: foo diff --git a/clang/test/Modules/autolink.m b/clang/test/Modules/autolink.m index f15472692dd..f180f6910b5 100644 --- a/clang/test/Modules/autolink.m +++ b/clang/test/Modules/autolink.m @@ -37,9 +37,9 @@ int use_autolink_sub3() { // NOTE: "autolink_sub" is intentionally not linked. // CHECK: !llvm.linker.options = !{![[AUTOLINK_PCH:[0-9]+]], ![[AUTOLINK_FRAMEWORK:[0-9]+]], ![[AUTOLINK:[0-9]+]], ![[DEPENDSONMODULE:[0-9]+]], ![[MODULE:[0-9]+]], ![[NOUMBRELLA:[0-9]+]]} -// CHECK: ![[AUTOLINK_PCH]] = !{!"{{(\\01|-l|/DEFAULTLIB:|lib", !")}}autolink_from_pch{{(\.lib)?}}"} +// CHECK: ![[AUTOLINK_PCH]] = !{!"{{(-l|/DEFAULTLIB:|lib", !")}}autolink_from_pch{{(\.lib)?}}"} // CHECK: ![[AUTOLINK_FRAMEWORK]] = !{!"-framework", !"autolink_framework"} -// CHECK: ![[AUTOLINK]] = !{!"{{(\\01|-l|/DEFAULTLIB:|lib", !")}}autolink{{(\.lib)?}}"} +// CHECK: ![[AUTOLINK]] = !{!"{{(-l|/DEFAULTLIB:|lib", !")}}autolink{{(\.lib)?}}"} // CHECK: ![[DEPENDSONMODULE]] = !{!"-framework", !"DependsOnModule"} // CHECK: ![[MODULE]] = !{!"-framework", !"Module"} // CHECK: ![[NOUMBRELLA]] = !{!"-framework", !"NoUmbrella"} diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index e138da4748c..ff48fd5f264 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -137,6 +137,7 @@ struct Configuration { bool Cref; bool DefineCommon; bool Demangle = true; + bool DependentLibraries; bool DisableVerify; bool EhFrameHdr; bool EmitLLVM; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 5bd4fbfc9bf..02f5ab3b0f0 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -790,6 +790,7 @@ static void readConfigs(opt::InputArgList &Args) { Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common, !Args.hasArg(OPT_relocatable)); Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true); + Config->DependentLibraries = Args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true); Config->DisableVerify = Args.hasArg(OPT_disable_verify); Config->Discard = getDiscard(Args); Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq); @@ -1548,9 +1549,11 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { Symtab->trace(Arg->getValue()); // Add all files to the symbol table. This will add almost all - // symbols that we need to the symbol table. - for (InputFile *F : Files) - parseFile(F); + // symbols that we need to the symbol table. This process might + // add files to the link, via autolinking, these files are always + // appended to the Files vector. + for (size_t I = 0; I < Files.size(); ++I) + parseFile(Files[I]); // Now that we have every file, we can decide if we will need a // dynamic symbol table. diff --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h index 050bb611fc6..91d52c63723 100644 --- a/lld/ELF/Driver.h +++ b/lld/ELF/Driver.h @@ -63,6 +63,7 @@ std::string createResponseFile(const llvm::opt::InputArgList &Args); llvm::Optional<std::string> findFromSearchPaths(StringRef Path); llvm::Optional<std::string> searchScript(StringRef Path); +llvm::Optional<std::string> searchLibraryBaseName(StringRef Path); llvm::Optional<std::string> searchLibrary(StringRef Path); } // namespace elf diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp index 0fb3aebc61f..e6a482b4969 100644 --- a/lld/ELF/DriverUtils.cpp +++ b/lld/ELF/DriverUtils.cpp @@ -223,12 +223,9 @@ Optional<std::string> elf::findFromSearchPaths(StringRef Path) { return None; } -// This is for -lfoo. We'll look for libfoo.so or libfoo.a from +// This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from // search paths. -Optional<std::string> elf::searchLibrary(StringRef Name) { - if (Name.startswith(":")) - return findFromSearchPaths(Name.substr(1)); - +Optional<std::string> elf::searchLibraryBaseName(StringRef Name) { for (StringRef Dir : Config->SearchPaths) { if (!Config->Static) if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".so")) @@ -239,6 +236,13 @@ Optional<std::string> elf::searchLibrary(StringRef Name) { return None; } +// This is for -l<namespec>. +Optional<std::string> elf::searchLibrary(StringRef Name) { + if (Name.startswith(":")) + return findFromSearchPaths(Name.substr(1)); + return searchLibraryBaseName (Name); +} + // If a linker/version script doesn't exist in the current directory, we also // look for the script in the '-L' search paths. This matches the behaviour of // '-T', --version-script=, and linker script INPUT() command in ld.bfd. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 8fbd7d27786..1ab8f7f6a8c 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "InputFiles.h" +#include "Driver.h" #include "InputSection.h" #include "LinkerScript.h" #include "SymbolTable.h" @@ -499,6 +500,27 @@ template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { } } +// An ELF object file may contain a `.deplibs` section. If it exists, the +// section contains a list of library specifiers such as `m` for libm. This +// function resolves a given name by finding the first matching library checking +// the various ways that a library can be specified to LLD. This ELF extension +// is a form of autolinking and is called `dependent libraries`. It is currently +// unique to LLVM and lld. +static void addDependentLibrary(StringRef Specifier, const InputFile *F) { + if (!Config->DependentLibraries) + return; + if (fs::exists(Specifier)) + Driver->addFile(Specifier, /*WithLOption=*/false); + else if (Optional<std::string> S = findFromSearchPaths(Specifier)) + Driver->addFile(*S, /*WithLOption=*/true); + else if (Optional<std::string> S = searchLibraryBaseName(Specifier)) + Driver->addFile(*S, /*WithLOption=*/true); + else + error(toString(F) + + ": unable to find library from dependent library specifier: " + + Specifier); +} + template <class ELFT> void ObjFile<ELFT>::initializeSections( DenseSet<CachedHashStringRef> &ComdatGroups) { @@ -740,6 +762,24 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { } return &InputSection::Discarded; } + case SHT_LLVM_DEPENDENT_LIBRARIES: { + if (Config->Relocatable) + break; + ArrayRef<char> Data = + CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this); + if (!Data.empty() && Data.back() != '\0') { + error(toString(this) + + ": corrupted dependent libraries section (unterminated string): " + + Name); + return &InputSection::Discarded; + } + for (const char *D = Data.begin(), *E = Data.end(); D < E;) { + StringRef S(D); + addDependentLibrary(S, this); + D += S.size() + 1; + } + return &InputSection::Discarded; + } case SHT_RELA: case SHT_REL: { // Find a relocation target section and associate this section with that. @@ -1302,6 +1342,9 @@ void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this)); + + for (auto L : Obj->getDependentLibraries()) + addDependentLibrary(L, this); } static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) { diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index a96bf3cc0a3..6db47e64cb9 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -71,6 +71,10 @@ defm apply_dynamic_relocs: B<"apply-dynamic-relocs", "Apply link-time values for dynamic relocations", "Do not apply link-time values for dynamic relocations (default)">; +defm dependent_libraries: B<"dependent-libraries", + "Process dependent library specifiers from input files (default)", + "Ignore dependent library specifiers from input files">; + defm as_needed: B<"as-needed", "Only set DT_NEEDED for shared libraries if used", "Always set DT_NEEDED for shared libraries (default)">; diff --git a/lld/test/ELF/Inputs/deplibs-lib_bar.s b/lld/test/ELF/Inputs/deplibs-lib_bar.s new file mode 100644 index 00000000000..b092459eef3 --- /dev/null +++ b/lld/test/ELF/Inputs/deplibs-lib_bar.s @@ -0,0 +1,2 @@ + .global bar +bar: diff --git a/lld/test/ELF/Inputs/deplibs-lib_foo.s b/lld/test/ELF/Inputs/deplibs-lib_foo.s new file mode 100644 index 00000000000..2f0ccf81e39 --- /dev/null +++ b/lld/test/ELF/Inputs/deplibs-lib_foo.s @@ -0,0 +1,2 @@ + .global foo +foo: diff --git a/lld/test/ELF/deplibs-colon-prefix.s b/lld/test/ELF/deplibs-colon-prefix.s new file mode 100644 index 00000000000..6f480717b5b --- /dev/null +++ b/lld/test/ELF/deplibs-colon-prefix.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/deplibs-lib_foo.s -o %tfoo.o +# RUN: rm -rf %t.dir +# RUN: mkdir -p %t.dir +# RUN: llvm-ar rc %t.dir/foo.a %tfoo.o +# RUN: not ld.lld %t.o -o /dev/null -L %t.dir 2>&1 | FileCheck %s -DOBJ=%t.o +# CHECK: error: [[OBJ]]: unable to find library from dependent library specifier: :foo.a + + .global _start +_start: + call foo + .section ".deplibs","MS",@llvm_dependent_libraries,1 + .asciz ":foo.a" diff --git a/lld/test/ELF/deplibs-corrupt.s b/lld/test/ELF/deplibs-corrupt.s new file mode 100644 index 00000000000..b17987f1479 --- /dev/null +++ b/lld/test/ELF/deplibs-corrupt.s @@ -0,0 +1,8 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: not ld.lld %t.o -o /dev/null -L %t.dir 2>&1 | FileCheck %s -DOBJ=%t.o +# CHECK: error: [[OBJ]]: corrupted dependent libraries section (unterminated string): .deplibs + +.section ".deplibs","MS",@llvm_dependent_libraries,1 + .ascii ":foo.a" diff --git a/lld/test/ELF/deplibs.s b/lld/test/ELF/deplibs.s new file mode 100644 index 00000000000..a0023163e82 --- /dev/null +++ b/lld/test/ELF/deplibs.s @@ -0,0 +1,56 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/deplibs-lib_foo.s -o %tfoo.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/deplibs-lib_bar.s -o %tbar.o +# RUN: rm -rf %t.dir %t.cwd +# RUN: mkdir -p %t.dir + +# error if dependent libraries cannot be found +# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s -DOBJ=%t.o --check-prefix MISSING +# MISSING: error: [[OBJ]]: unable to find library from dependent library specifier: foo.a +# MISSING-NEXT: error: [[OBJ]]: unable to find library from dependent library specifier: bar + +# can ignore dependent libraries +# RUN: not ld.lld %t.o -o /dev/null --no-dependent-libraries 2>&1 | FileCheck %s --check-prefix IGNORE +# IGNORE: error: undefined symbol: foo +# IGNORE: error: undefined symbol: bar + +# -r links preserve dependent libraries +# RUN: ld.lld %t.o %t.o -r -o %t-r.o +# RUN: not ld.lld %t-r.o -o /dev/null 2>&1 | sort | FileCheck %s -DOBJ=%t-r.o --check-prefixes MINUSR +# MINUSR: error: [[OBJ]]: unable to find library from dependent library specifier: bar +# MINUSR-NEXT: error: [[OBJ]]: unable to find library from dependent library specifier: foo.a +# MINUSR-NOT: unable to find library from dependent library specifier + +# static archives located relative to library search paths +# RUN: llvm-ar rc %t.dir/foo.a %tfoo.o +# RUN: llvm-ar rc %t.dir/libbar.a %tbar.o +# RUN: ld.lld %t.o -o /dev/null -L %t.dir + +# shared objects located relative to library search paths +# RUN: rm %t.dir/libbar.a +# RUN: ld.lld -shared -o %t.dir/libbar.so %tbar.o +# RUN: ld.lld -Bdynamic %t.o -o /dev/null -L %t.dir + +# dependent libraries searched for symbols after libraries on the command line +# RUN: mkdir -p %t.cwd +# RUN: cd %t.cwd +# RUN: cp %t.dir/foo.a %t.cwd/libcmdline.a +# RUN: ld.lld %t.o libcmdline.a -o /dev/null -L %t.dir --trace 2>&1 | FileCheck %s -DOBJ=%t.o -DSO=%t.dir --check-prefix CMDLINE --implicit-check-not foo.a +# CMDLINE: [[OBJ]] +# CMDLINE-NEXT: {{^libcmdline\.a}} +# CMDLINE-NEXT: [[SO]]{{[\\/]}}libbar.so + +# libraries can be found from specifiers as if the specifiers were listed on on the command-line. +# RUN: cp %t.dir/foo.a %t.cwd/foo.a +# RUN: ld.lld %t.o -o /dev/null -L %t.dir --trace 2>&1 | FileCheck %s -DOBJ=%t.o -DSO=%t.dir --check-prefix ASIFCMDLINE --implicit-check-not foo.a +# ASIFCMDLINE: [[OBJ]] +# ASIFCMDLINE-NEXT: {{^foo\.a}} +# ASIFCMDLINE-NEXT: [[SO]]{{[\\/]}}libbar.so + + call foo + call bar +.section ".deplibs","MS",@llvm_dependent_libraries,1 + .asciz "foo.a" + .asciz "bar" diff --git a/lld/test/ELF/lto/deplibs.s b/lld/test/ELF/lto/deplibs.s new file mode 100644 index 00000000000..cc278698463 --- /dev/null +++ b/lld/test/ELF/lto/deplibs.s @@ -0,0 +1,15 @@ +; REQUIRES: x86 + +; RUN: llvm-as %s -o %t.o +; RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s -DOBJ=%t.o + +; CHECK: error: [[OBJ]]: unable to find library from dependent library specifier: foo +; CHECK: error: [[OBJ]]: unable to find library from dependent library specifier: bar + +target triple = "x86_64-unknown-linux-gnu" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +!llvm.dependent-libraries = !{!0, !1} + +!0 = !{!"foo"} +!1 = !{!"bar"} diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst index fac2289921e..8543ac61185 100644 --- a/llvm/docs/Extensions.rst +++ b/llvm/docs/Extensions.rst @@ -285,6 +285,26 @@ The following directives are specified: The paramter identifies an additional library search path to be considered when looking up libraries after the inclusion of this option. +``SHT_LLVM_DEPENDENT_LIBRARIES`` Section (Dependent Libraries) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +This section contains strings specifying libraries to be added to the link by +the linker. + +The section should be consumed by the linker and not written to the output. + +The strings are encoded as standard null-terminated UTF-8 strings. + +For example: + +.. code-block:: gas + + .section ".deplibs","MS",@llvm_dependent_libraries,1 + .asciz "library specifier 1" + .asciz "library specifier 2" + +The interpretation of the library specifiers is defined by the consuming linker. + ``SHT_LLVM_CALL_GRAPH_PROFILE`` Section (Call Graph Profile) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst index af6e484c4b8..bfe0a0ab9fe 100644 --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -6081,10 +6081,10 @@ enum is the smallest type which can represent all of its values:: Automatic Linker Flags Named Metadata ===================================== -Some targets support embedding flags to the linker inside individual object +Some targets support embedding of flags to the linker inside individual object files. Typically this is used in conjunction with language extensions which -allow source files to explicitly declare the libraries they depend on, and have -these automatically be transmitted to the linker via object files. +allow source files to contain linker command line options, and have these +automatically be transmitted to the linker via object files. These flags are encoded in the IR using named metadata with the name ``!llvm.linker.options``. Each operand is expected to be a metadata node @@ -6095,8 +6095,8 @@ For example, the following metadata section specifies two separate sets of linker options, presumably to link against ``libz`` and the ``Cocoa`` framework:: - !0 = !{ !"-lz" }, - !1 = !{ !"-framework", !"Cocoa" } } } + !0 = !{ !"-lz" } + !1 = !{ !"-framework", !"Cocoa" } !llvm.linker.options = !{ !0, !1 } The metadata encoding as lists of lists of options, as opposed to a collapsed @@ -6109,6 +6109,28 @@ Each individual option is required to be either a valid option for the target's linker, or an option that is reserved by the target specific assembly writer or object file emitter. No other aspect of these options is defined by the IR. +Dependent Libs Named Metadata +============================= + +Some targets support embedding of strings into object files to indicate +a set of libraries to add to the link. Typically this is used in conjunction +with language extensions which allow source files to explicitly declare the +libraries they depend on, and have these automatically be transmitted to the +linker via object files. + +The list is encoded in the IR using named metadata with the name +``!llvm.dependent-libraries``. Each operand is expected to be a metadata node +which should contain a single string operand. + +For example, the following metadata section contains two library specfiers:: + + !0 = !{!"a library specifier"} + !1 = !{!"another library specifier"} + !llvm.dependent-libraries = !{ !0, !1 } + +Each library specifier will be handled independently by the consuming linker. +The effect of the library specifiers are defined by the consuming linker. + .. _summary: ThinLTO Summary diff --git a/llvm/include/llvm-c/lto.h b/llvm/include/llvm-c/lto.h index a22f0bbade4..5a33898d0f8 100644 --- a/llvm/include/llvm-c/lto.h +++ b/llvm/include/llvm-c/lto.h @@ -44,7 +44,7 @@ typedef bool lto_bool_t; * @{ */ -#define LTO_API_VERSION 23 +#define LTO_API_VERSION 24 /** * \since prior to LTO_API_VERSION=3 @@ -298,6 +298,14 @@ extern const char* lto_module_get_linkeropts(lto_module_t mod); /** +* Returns the module's dependent library specifiers. +* +* \since LTO_API_VERSION=24 +*/ +extern const char* +lto_module_get_dependent_libraries(lto_module_t mod); + +/** * Diagnostic severity. * * \since LTO_API_VERSION=7 diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h index 84f69666bb9..6ec924d08d1 100644 --- a/llvm/include/llvm/BinaryFormat/ELF.h +++ b/llvm/include/llvm/BinaryFormat/ELF.h @@ -841,6 +841,7 @@ enum : unsigned { SHT_LLVM_CALL_GRAPH_PROFILE = 0x6fff4c02, // LLVM Call Graph Profile. SHT_LLVM_ADDRSIG = 0x6fff4c03, // List of address-significant symbols // for safe ICF. + SHT_LLVM_DEPENDENT_LIBRARIES = 0x6fff4c04, // LLVM Dependent Library Specifiers. // Android's experimental support for SHT_RELR sections. // https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512 SHT_ANDROID_RELR = 0x6fffff00, // Relocation entries; only offsets. diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h index 76fa4f4dd62..d3a0d3c37dc 100644 --- a/llvm/include/llvm/LTO/LTO.h +++ b/llvm/include/llvm/LTO/LTO.h @@ -115,6 +115,7 @@ private: std::vector<std::pair<size_t, size_t>> ModuleSymIndices; StringRef TargetTriple, SourceFileName, COFFLinkerOpts; + std::vector<StringRef> DependentLibraries; std::vector<StringRef> ComdatTable; public: @@ -155,6 +156,9 @@ public: /// Returns linker options specified in the input file. StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; } + /// Returns dependent library specifiers from the input file. + ArrayRef<StringRef> getDependentLibraries() const { return DependentLibraries; } + /// Returns the path to the InputFile. StringRef getName() const; diff --git a/llvm/include/llvm/LTO/legacy/LTOModule.h b/llvm/include/llvm/LTO/legacy/LTOModule.h index 006521bf397..89d8682a207 100644 --- a/llvm/include/llvm/LTO/legacy/LTOModule.h +++ b/llvm/include/llvm/LTO/legacy/LTOModule.h @@ -47,6 +47,8 @@ private: std::string LinkerOpts; + std::string DependentLibraries; + std::unique_ptr<Module> Mod; MemoryBufferRef MBRef; ModuleSymbolTable SymTab; @@ -153,6 +155,8 @@ public: StringRef getLinkerOpts() { return LinkerOpts; } + StringRef getDependentLibraries() { return DependentLibraries; } + const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; } private: diff --git a/llvm/include/llvm/Object/IRSymtab.h b/llvm/include/llvm/Object/IRSymtab.h index 0601d995fd1..0bbfc932493 100644 --- a/llvm/include/llvm/Object/IRSymtab.h +++ b/llvm/include/llvm/Object/IRSymtab.h @@ -125,12 +125,13 @@ struct Uncommon { Str SectionName; }; + struct Header { /// Version number of the symtab format. This number should be incremented /// when the format changes, but it does not need to be incremented if a /// change to LLVM would cause it to create a different symbol table. Word Version; - enum { kCurrentVersion = 1 }; + enum { kCurrentVersion = 2 }; /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION). /// Consumers should rebuild the symbol table from IR if the producer's @@ -147,6 +148,9 @@ struct Header { /// COFF-specific: linker directives. Str COFFLinkerOpts; + + /// Dependent Library Specifiers + Range<Str> DependentLibraries; }; } // end namespace storage @@ -231,6 +235,7 @@ class Reader { ArrayRef<storage::Comdat> Comdats; ArrayRef<storage::Symbol> Symbols; ArrayRef<storage::Uncommon> Uncommons; + ArrayRef<storage::Str> DependentLibraries; StringRef str(storage::Str S) const { return S.get(Strtab); } @@ -251,6 +256,7 @@ public: Comdats = range(header().Comdats); Symbols = range(header().Symbols); Uncommons = range(header().Uncommons); + DependentLibraries = range(header().DependentLibraries); } using symbol_range = iterator_range<object::content_iterator<SymbolRef>>; @@ -283,6 +289,16 @@ public: /// COFF-specific: returns linker options specified in the input file. StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); } + + /// Returns dependent library specifiers + std::vector<StringRef> getDependentLibraries() const { + std::vector<StringRef> Specifiers; + Specifiers.reserve(DependentLibraries.size()); + for (auto S : DependentLibraries) { + Specifiers.push_back(str(S)); + } + return Specifiers; + } }; /// Ephemeral symbols produced by Reader::symbols() and diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 436076f33b9..b3aa8c6e634 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -271,6 +271,19 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, } } + if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) { + auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES, + ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); + + Streamer.SwitchSection(S); + + for (const auto &Operand : DependentLibraries->operands()) { + Streamer.EmitBytes( + cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString()); + Streamer.EmitIntValue(0, 1); + } + } + unsigned Version = 0; unsigned Flags = 0; StringRef Section; diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 442c0853263..ce1b0a1e4f6 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -425,6 +425,7 @@ Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { File->TargetTriple = FOrErr->TheReader.getTargetTriple(); File->SourceFileName = FOrErr->TheReader.getSourceFileName(); File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts(); + File->DependentLibraries = FOrErr->TheReader.getDependentLibraries(); File->ComdatTable = FOrErr->TheReader.getComdatTable(); for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) { diff --git a/llvm/lib/LTO/LTOModule.cpp b/llvm/lib/LTO/LTOModule.cpp index eb46b31d211..83e9a09854c 100644 --- a/llvm/lib/LTO/LTOModule.cpp +++ b/llvm/lib/LTO/LTOModule.cpp @@ -646,5 +646,9 @@ void LTOModule::parseMetadata() { emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M); } - // Add other interesting metadata here. + // Dependent Libraries + raw_string_ostream OSD(DependentLibraries); + if (NamedMDNode *DependentLibraries = getModule().getNamedMetadata("llvm.dependent-libraries")) + for (MDNode *N : DependentLibraries->operands()) + OSD << " " << cast<MDString>(N->getOperand(0))->getString(); } diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp index 9e5b54a6881..48ced8d3dfa 100644 --- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp +++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp @@ -615,6 +615,8 @@ EndStmt: Type = ELF::SHT_LLVM_LINKER_OPTIONS; else if (TypeName == "llvm_call_graph_profile") Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE; + else if (TypeName == "llvm_dependent_libraries") + Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES; else if (TypeName.getAsInteger(0, Type)) return TokError("unknown section type"); } diff --git a/llvm/lib/MC/MCSectionELF.cpp b/llvm/lib/MC/MCSectionELF.cpp index e3c24efa93f..569b6ba0997 100644 --- a/llvm/lib/MC/MCSectionELF.cpp +++ b/llvm/lib/MC/MCSectionELF.cpp @@ -152,6 +152,8 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, OS << "llvm_linker_options"; else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) OS << "llvm_call_graph_profile"; + else if (Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES) + OS << "llvm_dependent_libraries"; else report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) + " for section " + getSectionName()); diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp index d5d98126343..951f4ae8f7e 100644 --- a/llvm/lib/Object/ELF.cpp +++ b/llvm/lib/Object/ELF.cpp @@ -253,6 +253,7 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS); STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE); STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG); + STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); diff --git a/llvm/lib/Object/IRSymtab.cpp b/llvm/lib/Object/IRSymtab.cpp index e80eaf1db32..e4282b9d6bd 100644 --- a/llvm/lib/Object/IRSymtab.cpp +++ b/llvm/lib/Object/IRSymtab.cpp @@ -89,6 +89,8 @@ struct Builder { std::string COFFLinkerOpts; raw_string_ostream COFFLinkerOptsOS{COFFLinkerOpts}; + std::vector<storage::Str> DependentLibraries; + void setStr(storage::Str &S, StringRef Value) { S.Offset = StrtabBuilder.add(Value); S.Size = Value.size(); @@ -140,6 +142,20 @@ Error Builder::addModule(Module *M) { } } + if (TT.isOSBinFormatELF()) { + if (auto E = M->materializeMetadata()) + return E; + if (NamedMDNode *N = M->getNamedMetadata("llvm.dependent-libraries")) { + for (MDNode *MDOptions : N->operands()) { + const auto OperandStr = + cast<MDString>(cast<MDNode>(MDOptions)->getOperand(0))->getString(); + storage::Str Specifier; + setStr(Specifier, OperandStr); + DependentLibraries.emplace_back(Specifier); + } + } + } + for (ModuleSymbolTable::Symbol Msym : Msymtab.symbols()) if (Error Err = addSymbol(Msymtab, Used, Msym)) return Err; @@ -312,7 +328,7 @@ Error Builder::build(ArrayRef<Module *> IRMods) { writeRange(Hdr.Comdats, Comdats); writeRange(Hdr.Symbols, Syms); writeRange(Hdr.Uncommons, Uncommons); - + writeRange(Hdr.DependentLibraries, DependentLibraries); *reinterpret_cast<storage::Header *>(Symtab.data()) = Hdr; return Error::success(); } diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp index 1ac624fe958..39e59efe00f 100644 --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -455,6 +455,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration( ECase(SHT_LLVM_LINKER_OPTIONS); ECase(SHT_LLVM_CALL_GRAPH_PROFILE); ECase(SHT_LLVM_ADDRSIG); + ECase(SHT_LLVM_DEPENDENT_LIBRARIES); ECase(SHT_GNU_ATTRIBUTES); ECase(SHT_GNU_HASH); ECase(SHT_GNU_verdef); diff --git a/llvm/test/Feature/elf-deplibs.ll b/llvm/test/Feature/elf-deplibs.ll new file mode 100644 index 00000000000..c44740ef961 --- /dev/null +++ b/llvm/test/Feature/elf-deplibs.ll @@ -0,0 +1,15 @@ +; RUN: llc -mtriple x86_64-elf -filetype asm -o - %s | FileCheck %s +; REQUIRES: x86-registered-target + +!llvm.dependent-libraries = !{!0, !1, !0} + +!0 = !{!"foo"} +!1 = !{!"b a r"} + +; CHECK: .section .deplibs,"MS",@llvm_dependent_libraries,1 +; CHECK-NEXT: .ascii "foo" +; CHECK-NEXT: .byte 0 +; CHECK-NEXT: .ascii "b a r" +; CHECK-NEXT: .byte 0 +; CHECK-NEXT: .ascii "foo" +; CHECK-NEXT: .byte 0 diff --git a/llvm/test/LTO/Resolution/X86/symtab-elf.ll b/llvm/test/LTO/Resolution/X86/symtab-elf.ll index d5f0fbe3700..40864a82dd0 100644 --- a/llvm/test/LTO/Resolution/X86/symtab-elf.ll +++ b/llvm/test/LTO/Resolution/X86/symtab-elf.ll @@ -9,6 +9,12 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" !0 = !{!"/include:foo"} !llvm.linker.options = !{ !0 } +; CHECK: {{^dependent libraries: \"foo\" \"b a r\" \"baz\"$}} +!1 = !{!"foo"} +!2 = !{!"b a r"} +!3 = !{!"baz"} +!llvm.dependent-libraries = !{!1, !2, !3} + @g1 = global i32 0 ; CHECK-NOT: fallback g1 diff --git a/llvm/test/MC/ELF/section.s b/llvm/test/MC/ELF/section.s index d31909f5f42..7c9bb7915d1 100644 --- a/llvm/test/MC/ELF/section.s +++ b/llvm/test/MC/ELF/section.s @@ -293,3 +293,16 @@ bar: // CHECK-NEXT: ] // CHECK: } +// Test SHT_LLVM_DEPENDENT_LIBRARIES + +.section .deplibs,"MS",@llvm_dependent_libraries,1 +// ASM: .section .deplibs,"MS",@llvm_dependent_libraries,1 + +// CHECK: Section { +// CHECK: Name: .deplibs +// CHECK-NEXT: Type: SHT_LLVM_DEPENDENT_LIBRARIES +// CHECK-NEXT: Flags [ +// CHECK-NEXT: SHF_MERGE +// CHECK-NEXT: SHF_STRINGS +// CHECK-NEXT: ] +// CHECK: } diff --git a/llvm/test/Object/X86/irsymtab.ll b/llvm/test/Object/X86/irsymtab.ll index 78e474df9a5..1b9915a2b74 100644 --- a/llvm/test/Object/X86/irsymtab.ll +++ b/llvm/test/Object/X86/irsymtab.ll @@ -9,16 +9,17 @@ ; BCA: <SYMTAB_BLOCK ; Version stored at offset 0. -; BCA-NEXT: <BLOB abbrevid=4/> blob data = '\x01\x00\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00D\x00\x00\x00\x01\x00\x00\x00P\x00\x00\x00\x00\x00\x00\x00P\x00\x00\x00\x02\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x0E\x00\x00\x00\x18\x00\x00\x00&\x00\x00\x00\x0B\x00\x00\x001\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\xFF\xFF\xFF\xFF\x00$\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\xFF\xFF\xFF\xFF\x08$\x00\x00' +; BCA-NEXT: <BLOB abbrevid=4/> blob data = '\x02\x00\x00\x00\x06\x00\x00\x00\x08\x00\x00\x00L\x00\x00\x00\x01\x00\x00\x00X\x00\x00\x00\x00\x00\x00\x00X\x00\x00\x00\x02\x00\x00\x00\x88\x00\x00\x00\x00\x00\x00\x00\x0E\x00\x00\x00\x18\x00\x00\x00&\x00\x00\x00\x0B\x00\x00\x001\x00\x00\x00\x00\x00\x00\x00\x88\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\xFF\xFF\xFF\xFF\x00$\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\xFF\xFF\xFF\xFF\x08$\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00' ; BCA-NEXT: </SYMTAB_BLOCK> ; BCA-NEXT: <STRTAB_BLOCK ; BCA-NEXT: <BLOB abbrevid=4/> blob data = 'foobarproducerx86_64-unknown-linux-gnuirsymtab.ll' ; BCA-NEXT: </STRTAB_BLOCK> -; SYMTAB: version: 1 +; SYMTAB: version: 2 ; SYMTAB-NEXT: producer: producer ; SYMTAB-NEXT: target triple: x86_64-unknown-linux-gnu ; SYMTAB-NEXT: source filename: irsymtab.ll +; SYMTAB-NEXT: {{^dependent libraries: \"foo\" \"bar\"$}} ; SYMTAB-NEXT: D------X foo ; SYMTAB-NEXT: DU-----X bar @@ -31,3 +32,8 @@ define void @foo() { } declare void @bar() + +!llvm.dependent-libraries = !{!0, !1} + +!0 = !{!"foo"} +!1 = !{!"bar"} diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp index df51921396a..72d80f64a96 100644 --- a/llvm/tools/llvm-lto2/llvm-lto2.cpp +++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -360,6 +360,13 @@ static int dumpSymtab(int argc, char **argv) { if (TT.isOSBinFormatCOFF()) outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n'; + if (TT.isOSBinFormatELF()) { + outs() << "dependent libraries:"; + for (auto L : Input->getDependentLibraries()) + outs() << " \"" << L << "\""; + outs() << '\n'; + } + std::vector<StringRef> ComdatTable = Input->getComdatTable(); for (const InputFile::Symbol &Sym : Input->symbols()) { switch (Sym.getVisibility()) { diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index ba6622686b9..75579879762 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -2857,6 +2857,8 @@ static std::string getSectionTypeString(unsigned Arch, unsigned Type) { return "LLVM_CALL_GRAPH_PROFILE"; case SHT_LLVM_ADDRSIG: return "LLVM_ADDRSIG"; + case SHT_LLVM_DEPENDENT_LIBRARIES: + return "LLVM_DEPENDENT_LIBRARIES"; // FIXME: Parse processor specific GNU attributes case SHT_GNU_ATTRIBUTES: return "ATTRIBUTES"; diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp index 379b5b89714..01dc141ffd0 100644 --- a/llvm/tools/lto/lto.cpp +++ b/llvm/tools/lto/lto.cpp @@ -325,6 +325,10 @@ const char* lto_module_get_linkeropts(lto_module_t mod) { return unwrap(mod)->getLinkerOpts().data(); } +const char* lto_module_get_dependent_libraries(lto_module_t mod) { + return unwrap(mod)->getDependentLibraries().data(); +} + void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, lto_diagnostic_handler_t diag_handler, void *ctxt) { |