1 //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This utility may be invoked in the following manner: 10 // llvm-link a.bc b.bc c.bc -o x.bc 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/BinaryFormat/Magic.h" 16 #include "llvm/Bitcode/BitcodeReader.h" 17 #include "llvm/Bitcode/BitcodeWriter.h" 18 #include "llvm/IR/AutoUpgrade.h" 19 #include "llvm/IR/DiagnosticInfo.h" 20 #include "llvm/IR/DiagnosticPrinter.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/IR/ModuleSummaryIndex.h" 24 #include "llvm/IR/Verifier.h" 25 #include "llvm/IRReader/IRReader.h" 26 #include "llvm/Linker/Linker.h" 27 #include "llvm/Object/Archive.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/FileSystem.h" 30 #include "llvm/Support/InitLLVM.h" 31 #include "llvm/Support/Path.h" 32 #include "llvm/Support/SourceMgr.h" 33 #include "llvm/Support/SystemUtils.h" 34 #include "llvm/Support/ToolOutputFile.h" 35 #include "llvm/Support/WithColor.h" 36 #include "llvm/Transforms/IPO/FunctionImport.h" 37 #include "llvm/Transforms/IPO/Internalize.h" 38 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 39 40 #include <memory> 41 #include <utility> 42 using namespace llvm; 43 44 static cl::list<std::string> 45 InputFilenames(cl::Positional, cl::OneOrMore, 46 cl::desc("<input bitcode files>")); 47 48 static cl::list<std::string> OverridingInputs( 49 "override", cl::ZeroOrMore, cl::value_desc("filename"), 50 cl::desc( 51 "input bitcode file which can override previously defined symbol(s)")); 52 53 // Option to simulate function importing for testing. This enables using 54 // llvm-link to simulate ThinLTO backend processes. 55 static cl::list<std::string> Imports( 56 "import", cl::ZeroOrMore, cl::value_desc("function:filename"), 57 cl::desc("Pair of function name and filename, where function should be " 58 "imported from bitcode in filename")); 59 60 // Option to support testing of function importing. The module summary 61 // must be specified in the case were we request imports via the -import 62 // option, as well as when compiling any module with functions that may be 63 // exported (imported by a different llvm-link -import invocation), to ensure 64 // consistent promotion and renaming of locals. 65 static cl::opt<std::string> 66 SummaryIndex("summary-index", cl::desc("Module summary index filename"), 67 cl::init(""), cl::value_desc("filename")); 68 69 static cl::opt<std::string> 70 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), 71 cl::value_desc("filename")); 72 73 static cl::opt<bool> 74 Internalize("internalize", cl::desc("Internalize linked symbols")); 75 76 static cl::opt<bool> 77 DisableDITypeMap("disable-debug-info-type-map", 78 cl::desc("Don't use a uniquing type map for debug info")); 79 80 static cl::opt<bool> 81 OnlyNeeded("only-needed", cl::desc("Link only needed symbols")); 82 83 static cl::opt<bool> 84 Force("f", cl::desc("Enable binary output on terminals")); 85 86 static cl::opt<bool> 87 DisableLazyLoad("disable-lazy-loading", 88 cl::desc("Disable lazy module loading")); 89 90 static cl::opt<bool> 91 OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden); 92 93 static cl::opt<bool> 94 Verbose("v", cl::desc("Print information about actions taken")); 95 96 static cl::opt<bool> 97 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); 98 99 static cl::opt<bool> 100 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"), 101 cl::init(false)); 102 103 static cl::opt<bool> PreserveBitcodeUseListOrder( 104 "preserve-bc-uselistorder", 105 cl::desc("Preserve use-list order when writing LLVM bitcode."), 106 cl::init(true), cl::Hidden); 107 108 static cl::opt<bool> PreserveAssemblyUseListOrder( 109 "preserve-ll-uselistorder", 110 cl::desc("Preserve use-list order when writing LLVM assembly."), 111 cl::init(false), cl::Hidden); 112 113 static ExitOnError ExitOnErr; 114 115 // Read the specified bitcode file in and return it. This routine searches the 116 // link path for the specified file to try to find it... 117 // 118 static std::unique_ptr<Module> loadFile(const char *argv0, 119 std::unique_ptr<MemoryBuffer> Buffer, 120 LLVMContext &Context, 121 bool MaterializeMetadata = true) { 122 SMDiagnostic Err; 123 if (Verbose) 124 errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n"; 125 std::unique_ptr<Module> Result; 126 if (DisableLazyLoad) 127 Result = parseIR(*Buffer, Err, Context); 128 else 129 Result = 130 getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata); 131 132 if (!Result) { 133 Err.print(argv0, errs()); 134 return nullptr; 135 } 136 137 if (MaterializeMetadata) { 138 ExitOnErr(Result->materializeMetadata()); 139 UpgradeDebugInfo(*Result); 140 } 141 142 return Result; 143 } 144 145 static std::unique_ptr<Module> loadArFile(const char *Argv0, 146 std::unique_ptr<MemoryBuffer> Buffer, 147 LLVMContext &Context) { 148 std::unique_ptr<Module> Result(new Module("ArchiveModule", Context)); 149 StringRef ArchiveName = Buffer->getBufferIdentifier(); 150 if (Verbose) 151 errs() << "Reading library archive file '" << ArchiveName 152 << "' to memory\n"; 153 Error Err = Error::success(); 154 object::Archive Archive(*Buffer, Err); 155 ExitOnErr(std::move(Err)); 156 Linker L(*Result); 157 for (const object::Archive::Child &C : Archive.children(Err)) { 158 Expected<StringRef> Ename = C.getName(); 159 if (Error E = Ename.takeError()) { 160 errs() << Argv0 << ": "; 161 WithColor::error() 162 << " failed to read name of archive member" 163 << ArchiveName << "'\n"; 164 return nullptr; 165 } 166 std::string ChildName = Ename.get().str(); 167 if (Verbose) 168 errs() << "Parsing member '" << ChildName 169 << "' of archive library to module.\n"; 170 SMDiagnostic ParseErr; 171 Expected<MemoryBufferRef> MemBuf = C.getMemoryBufferRef(); 172 if (Error E = MemBuf.takeError()) { 173 errs() << Argv0 << ": "; 174 WithColor::error() << " loading memory for member '" << ChildName 175 << "' of archive library failed'" << ArchiveName 176 << "'\n"; 177 return nullptr; 178 }; 179 180 if (!isBitcode(reinterpret_cast<const unsigned char *> 181 (MemBuf.get().getBufferStart()), 182 reinterpret_cast<const unsigned char *> 183 (MemBuf.get().getBufferEnd()))) { 184 errs() << Argv0 << ": "; 185 WithColor::error() << " member of archive is not a bitcode file: '" 186 << ChildName << "'\n"; 187 return nullptr; 188 } 189 190 std::unique_ptr<Module> M; 191 if (DisableLazyLoad) 192 M = parseIR(MemBuf.get(), ParseErr, Context); 193 else 194 M = getLazyIRModule(MemoryBuffer::getMemBuffer(MemBuf.get(), false), 195 ParseErr, Context); 196 197 if (!M.get()) { 198 errs() << Argv0 << ": "; 199 WithColor::error() << " parsing member '" << ChildName 200 << "' of archive library failed'" << ArchiveName 201 << "'\n"; 202 return nullptr; 203 } 204 if (Verbose) 205 errs() << "Linking member '" << ChildName << "' of archive library.\n"; 206 if (L.linkInModule(std::move(M))) 207 return nullptr; 208 } // end for each child 209 ExitOnErr(std::move(Err)); 210 return Result; 211 } 212 213 namespace { 214 215 /// Helper to load on demand a Module from file and cache it for subsequent 216 /// queries during function importing. 217 class ModuleLazyLoaderCache { 218 /// Cache of lazily loaded module for import. 219 StringMap<std::unique_ptr<Module>> ModuleMap; 220 221 /// Retrieve a Module from the cache or lazily load it on demand. 222 std::function<std::unique_ptr<Module>(const char *argv0, 223 const std::string &FileName)> 224 createLazyModule; 225 226 public: 227 /// Create the loader, Module will be initialized in \p Context. 228 ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>( 229 const char *argv0, const std::string &FileName)> 230 createLazyModule) 231 : createLazyModule(std::move(createLazyModule)) {} 232 233 /// Retrieve a Module from the cache or lazily load it on demand. 234 Module &operator()(const char *argv0, const std::string &FileName); 235 236 std::unique_ptr<Module> takeModule(const std::string &FileName) { 237 auto I = ModuleMap.find(FileName); 238 assert(I != ModuleMap.end()); 239 std::unique_ptr<Module> Ret = std::move(I->second); 240 ModuleMap.erase(I); 241 return Ret; 242 } 243 }; 244 245 // Get a Module for \p FileName from the cache, or load it lazily. 246 Module &ModuleLazyLoaderCache::operator()(const char *argv0, 247 const std::string &Identifier) { 248 auto &Module = ModuleMap[Identifier]; 249 if (!Module) 250 Module = createLazyModule(argv0, Identifier); 251 return *Module; 252 } 253 } // anonymous namespace 254 255 namespace { 256 struct LLVMLinkDiagnosticHandler : public DiagnosticHandler { 257 bool handleDiagnostics(const DiagnosticInfo &DI) override { 258 unsigned Severity = DI.getSeverity(); 259 switch (Severity) { 260 case DS_Error: 261 WithColor::error(); 262 break; 263 case DS_Warning: 264 if (SuppressWarnings) 265 return true; 266 WithColor::warning(); 267 break; 268 case DS_Remark: 269 case DS_Note: 270 llvm_unreachable("Only expecting warnings and errors"); 271 } 272 273 DiagnosticPrinterRawOStream DP(errs()); 274 DI.print(DP); 275 errs() << '\n'; 276 return true; 277 } 278 }; 279 } 280 281 /// Import any functions requested via the -import option. 282 static bool importFunctions(const char *argv0, Module &DestModule) { 283 if (SummaryIndex.empty()) 284 return true; 285 std::unique_ptr<ModuleSummaryIndex> Index = 286 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); 287 288 // Map of Module -> List of globals to import from the Module 289 FunctionImporter::ImportMapTy ImportList; 290 291 auto ModuleLoader = [&DestModule](const char *argv0, 292 const std::string &Identifier) { 293 std::unique_ptr<MemoryBuffer> Buffer = 294 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Identifier))); 295 return loadFile(argv0, std::move(Buffer), DestModule.getContext(), false); 296 }; 297 298 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); 299 for (const auto &Import : Imports) { 300 // Identify the requested function and its bitcode source file. 301 size_t Idx = Import.find(':'); 302 if (Idx == std::string::npos) { 303 errs() << "Import parameter bad format: " << Import << "\n"; 304 return false; 305 } 306 std::string FunctionName = Import.substr(0, Idx); 307 std::string FileName = Import.substr(Idx + 1, std::string::npos); 308 309 // Load the specified source module. 310 auto &SrcModule = ModuleLoaderCache(argv0, FileName); 311 312 if (verifyModule(SrcModule, &errs())) { 313 errs() << argv0 << ": " << FileName; 314 WithColor::error() << "input module is broken!\n"; 315 return false; 316 } 317 318 Function *F = SrcModule.getFunction(FunctionName); 319 if (!F) { 320 errs() << "Ignoring import request for non-existent function " 321 << FunctionName << " from " << FileName << "\n"; 322 continue; 323 } 324 // We cannot import weak_any functions without possibly affecting the 325 // order they are seen and selected by the linker, changing program 326 // semantics. 327 if (F->hasWeakAnyLinkage()) { 328 errs() << "Ignoring import request for weak-any function " << FunctionName 329 << " from " << FileName << "\n"; 330 continue; 331 } 332 333 if (Verbose) 334 errs() << "Importing " << FunctionName << " from " << FileName << "\n"; 335 336 auto &Entry = ImportList[FileName]; 337 Entry.insert(F->getGUID()); 338 } 339 auto CachedModuleLoader = [&](StringRef Identifier) { 340 return ModuleLoaderCache.takeModule(std::string(Identifier)); 341 }; 342 FunctionImporter Importer(*Index, CachedModuleLoader, 343 /*ClearDSOLocalOnDeclarations=*/false); 344 ExitOnErr(Importer.importFunctions(DestModule, ImportList)); 345 346 return true; 347 } 348 349 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L, 350 const cl::list<std::string> &Files, 351 unsigned Flags) { 352 // Filter out flags that don't apply to the first file we load. 353 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc; 354 // Similar to some flags, internalization doesn't apply to the first file. 355 bool InternalizeLinkedSymbols = false; 356 for (const auto &File : Files) { 357 std::unique_ptr<MemoryBuffer> Buffer = 358 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(File))); 359 360 std::unique_ptr<Module> M = 361 identify_magic(Buffer->getBuffer()) == file_magic::archive 362 ? loadArFile(argv0, std::move(Buffer), Context) 363 : loadFile(argv0, std::move(Buffer), Context); 364 if (!M.get()) { 365 errs() << argv0 << ": "; 366 WithColor::error() << " loading file '" << File << "'\n"; 367 return false; 368 } 369 370 // Note that when ODR merging types cannot verify input files in here When 371 // doing that debug metadata in the src module might already be pointing to 372 // the destination. 373 if (DisableDITypeMap && verifyModule(*M, &errs())) { 374 errs() << argv0 << ": " << File << ": "; 375 WithColor::error() << "input module is broken!\n"; 376 return false; 377 } 378 379 // If a module summary index is supplied, load it so linkInModule can treat 380 // local functions/variables as exported and promote if necessary. 381 if (!SummaryIndex.empty()) { 382 std::unique_ptr<ModuleSummaryIndex> Index = 383 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); 384 385 // Conservatively mark all internal values as promoted, since this tool 386 // does not do the ThinLink that would normally determine what values to 387 // promote. 388 for (auto &I : *Index) { 389 for (auto &S : I.second.SummaryList) { 390 if (GlobalValue::isLocalLinkage(S->linkage())) 391 S->setLinkage(GlobalValue::ExternalLinkage); 392 } 393 } 394 395 // Promotion 396 if (renameModuleForThinLTO(*M, *Index, 397 /*ClearDSOLocalOnDeclarations=*/false)) 398 return true; 399 } 400 401 if (Verbose) 402 errs() << "Linking in '" << File << "'\n"; 403 404 bool Err = false; 405 if (InternalizeLinkedSymbols) { 406 Err = L.linkInModule( 407 std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) { 408 internalizeModule(M, [&GVS](const GlobalValue &GV) { 409 return !GV.hasName() || (GVS.count(GV.getName()) == 0); 410 }); 411 }); 412 } else { 413 Err = L.linkInModule(std::move(M), ApplicableFlags); 414 } 415 416 if (Err) 417 return false; 418 419 // Internalization applies to linking of subsequent files. 420 InternalizeLinkedSymbols = Internalize; 421 422 // All linker flags apply to linking of subsequent files. 423 ApplicableFlags = Flags; 424 } 425 426 return true; 427 } 428 429 int main(int argc, char **argv) { 430 InitLLVM X(argc, argv); 431 ExitOnErr.setBanner(std::string(argv[0]) + ": "); 432 433 LLVMContext Context; 434 Context.setDiagnosticHandler( 435 std::make_unique<LLVMLinkDiagnosticHandler>(), true); 436 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); 437 438 if (!DisableDITypeMap) 439 Context.enableDebugTypeODRUniquing(); 440 441 auto Composite = std::make_unique<Module>("llvm-link", Context); 442 Linker L(*Composite); 443 444 unsigned Flags = Linker::Flags::None; 445 if (OnlyNeeded) 446 Flags |= Linker::Flags::LinkOnlyNeeded; 447 448 // First add all the regular input files 449 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags)) 450 return 1; 451 452 // Next the -override ones. 453 if (!linkFiles(argv[0], Context, L, OverridingInputs, 454 Flags | Linker::Flags::OverrideFromSrc)) 455 return 1; 456 457 // Import any functions requested via -import 458 if (!importFunctions(argv[0], *Composite)) 459 return 1; 460 461 if (DumpAsm) 462 errs() << "Here's the assembly:\n" << *Composite; 463 464 std::error_code EC; 465 ToolOutputFile Out(OutputFilename, EC, 466 OutputAssembly ? sys::fs::OF_Text : sys::fs::OF_None); 467 if (EC) { 468 WithColor::error() << EC.message() << '\n'; 469 return 1; 470 } 471 472 if (verifyModule(*Composite, &errs())) { 473 errs() << argv[0] << ": "; 474 WithColor::error() << "linked module is broken!\n"; 475 return 1; 476 } 477 478 if (Verbose) 479 errs() << "Writing bitcode...\n"; 480 if (OutputAssembly) { 481 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder); 482 } else if (Force || !CheckBitcodeOutputToConsole(Out.os())) 483 WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder); 484 485 // Declare success. 486 Out.keep(); 487 488 return 0; 489 } 490