Skip to content

Commit 1d16515

Browse files
committedMay 17, 2019
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow either the --dependent-library compiler option, or "comment lib" pragmas ( https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically add the specified library to the link when processing the input file generated by the compiler. Currently this extension is unique to LLVM and LLD. However, care has been taken to design this feature so that it could be supported by other ELF linkers. The design goals were to provide: - A simple linking model for developers to reason about. - The ability to to override autolinking from the linker command line. - Source code compatibility, where possible, with "comment lib" pragmas in other environments (MSVC in particular). Dependent library support is implemented differently for ELF platforms than on the other platforms. Primarily this difference is that 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 specifiers to the linker. In order to achieve a level of source code compatibility with other platforms we have restricted this feature to work with libraries that meet the following "reasonable" requirements: 1. There are no competing defined symbols in a given set of libraries, or if they exist, the program owner doesn't care which is linked to their program. 2. There may be circular dependencies between libraries. The binary representation is a mergeable string section (SHF_MERGE, SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES (0x6fff4c04). The compiler forms this section by concatenating the arguments of the "comment lib" pragmas and --dependent-library options in the order they are encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs sections with the normal mergeable string section rules. As an example, #pragma comment(lib, "foo") would result in: .section ".deplibs","MS",@llvm_dependent_libraries,1 .asciz "foo" For LTO, equivalent information to the contents of a the .deplibs section can be retrieved by the LLD for bitcode input files. LLD processes the dependent library specifiers in the following way: 1. Dependent libraries which are found from the specifiers in .deplibs sections of relocatable object files are added when the linker decides to include that file (which could itself be in a library) in the link. Dependent libraries behave as if they were appended to the command line after all other options. As a consequence the set of dependent libraries are searched last to resolve symbols. 2. It is an error if a file cannot be found for a given specifier. 3. Any command line options in effect at the end of the command line parsing apply to the dependent libraries, e.g. --whole-archive. 4. The linker tries to add a library or relocatable object file from each of the strings in a .deplibs section by; first, handling the string as if it was specified on the command line; second, by looking for the string in each of the library search paths in turn; third, by looking for a lib<string>.a or lib<string>.so (depending on the current mode of the linker) in each of the library search paths. 5. A new command line option --no-dependent-libraries tells LLD to ignore the dependent libraries. Rationale for the above points: 1. Adding the dependent libraries last makes the process simple to understand from a developers perspective. All linkers are able to implement this scheme. 2. Error-ing for libraries that are not found seems like better behavior than failing the link during symbol resolution. 3. It seems useful for the user to be able to apply command line options which will affect all of the dependent libraries. There is a potential problem of surprise for developers, who might not realize that these options would apply to these "invisible" input files; however, despite the potential for surprise, this is easy for developers to reason about and gives developers the control that they may require. 4. This algorithm takes into account all of the different ways that ELF linkers find input files. The different search methods are tried by the linker in most obvious to least obvious order. 5. I considered adding finer grained control over which dependent libraries were ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this is not necessary: if finer control is required developers can fall back to using the command line directly. RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html. Differential Revision: https://reviews.llvm.org/D60274 llvm-svn: 360984
1 parent 2463239 commit 1d16515

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+382
-76
lines changed
 

‎clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ void CodeGenModule::Release() {
446446
EmitModuleLinkOptions();
447447
}
448448

449+
// On ELF we pass the dependent library specifiers directly to the linker
450+
// without manipulating them. This is in contrast to other platforms where
451+
// they are mapped to a specific linker option by the compiler. This
452+
// difference is a result of the greater variety of ELF linkers and the fact
453+
// that ELF linkers tend to handle libraries in a more complicated fashion
454+
// than on other platforms. This forces us to defer handling the dependent
455+
// libs to the linker.
456+
if (!ELFDependentLibraries.empty()) {
457+
auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
458+
for (auto *MD : ELFDependentLibraries)
459+
NMD->addOperand(MD);
460+
}
461+
449462
// Record mregparm value now so it is visible through rest of codegen.
450463
if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
451464
getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
@@ -1903,17 +1916,18 @@ void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
19031916
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
19041917
}
19051918

1906-
void CodeGenModule::AddELFLibDirective(StringRef Lib) {
1919+
void CodeGenModule::AddDependentLib(StringRef Lib) {
19071920
auto &C = getLLVMContext();
1908-
LinkerOptionsMetadata.push_back(llvm::MDNode::get(
1909-
C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)}));
1910-
}
1921+
if (getTarget().getTriple().isOSBinFormatELF()) {
1922+
ELFDependentLibraries.push_back(
1923+
llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
1924+
return;
1925+
}
19111926

1912-
void CodeGenModule::AddDependentLib(StringRef Lib) {
19131927
llvm::SmallString<24> Opt;
19141928
getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
19151929
auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
1916-
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
1930+
LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
19171931
}
19181932

19191933
/// Add link options implied by the given module, including modules
@@ -1936,7 +1950,6 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
19361950
// described by this module.
19371951
llvm::LLVMContext &Context = CGM.getLLVMContext();
19381952
bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
1939-
bool IsPS4 = CGM.getTarget().getTriple().isPS4();
19401953

19411954
// For modules that use export_as for linking, use that module
19421955
// name instead.
@@ -1956,7 +1969,7 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
19561969
}
19571970

19581971
// Link against a library.
1959-
if (IsELF && !IsPS4) {
1972+
if (IsELF) {
19601973
llvm::Metadata *Args[2] = {
19611974
llvm::MDString::get(Context, "lib"),
19621975
llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library),
@@ -5197,10 +5210,6 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
51975210
AppendLinkerOptions(PCD->getArg());
51985211
break;
51995212
case PCK_Lib:
5200-
if (getTarget().getTriple().isOSBinFormatELF() &&
5201-
!getTarget().getTriple().isPS4())
5202-
AddELFLibDirective(PCD->getArg());
5203-
else
52045213
AddDependentLib(PCD->getArg());
52055214
break;
52065215
case PCK_Compiler:

‎clang/lib/CodeGen/CodeGenModule.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,12 @@ class CodeGenModule : public CodeGenTypeCache {
465465
/// have been emitted.
466466
llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
467467

468-
/// A vector of metadata strings.
468+
/// A vector of metadata strings for linker options.
469469
SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
470470

471+
/// A vector of metadata strings for dependent libraries for ELF.
472+
SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
473+
471474
/// @name Cache for Objective-C runtime types
472475
/// @{
473476

@@ -1152,11 +1155,9 @@ class CodeGenModule : public CodeGenTypeCache {
11521155
/// Appends a detect mismatch command to the linker options.
11531156
void AddDetectMismatch(StringRef Name, StringRef Value);
11541157

1155-
/// Appends a dependent lib to the "llvm.linker.options" metadata
1156-
/// value.
1158+
/// Appends a dependent lib to the appropriate metadata value.
11571159
void AddDependentLib(StringRef Lib);
11581160

1159-
void AddELFLibDirective(StringRef Lib);
11601161

11611162
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
11621163

0 commit comments

Comments
 (0)