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 cl::opt<bool> NoVerify("disable-verify", 114 cl::desc("Do not run the verifier"), cl::Hidden); 115 116 static ExitOnError ExitOnErr; 117 118 // Read the specified bitcode file in and return it. This routine searches the 119 // link path for the specified file to try to find it... 120 // 121 static std::unique_ptr<Module> loadFile(const char *argv0, 122 std::unique_ptr<MemoryBuffer> Buffer, 123 LLVMContext &Context, 124 bool MaterializeMetadata = true) { 125 SMDiagnostic Err; 126 if (Verbose) 127 errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n"; 128 std::unique_ptr<Module> Result; 129 if (DisableLazyLoad) 130 Result = parseIR(*Buffer, Err, Context); 131 else 132 Result = 133 getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata); 134 135 if (!Result) { 136 Err.print(argv0, errs()); 137 return nullptr; 138 } 139 140 if (MaterializeMetadata) { 141 ExitOnErr(Result->materializeMetadata()); 142 UpgradeDebugInfo(*Result); 143 } 144 145 return Result; 146 } 147 148 static std::unique_ptr<Module> loadArFile(const char *Argv0, 149 std::unique_ptr<MemoryBuffer> Buffer, 150 LLVMContext &Context) { 151 std::unique_ptr<Module> Result(new Module("ArchiveModule", Context)); 152 StringRef ArchiveName = Buffer->getBufferIdentifier(); 153 if (Verbose) 154 errs() << "Reading library archive file '" << ArchiveName 155 << "' to memory\n"; 156 Error Err = Error::success(); 157 object::Archive Archive(*Buffer, Err); 158 ExitOnErr(std::move(Err)); 159 Linker L(*Result); 160 for (const object::Archive::Child &C : Archive.children(Err)) { 161 Expected<StringRef> Ename = C.getName(); 162 if (Error E = Ename.takeError()) { 163 errs() << Argv0 << ": "; 164 WithColor::error() 165 << " failed to read name of archive member" 166 << ArchiveName << "'\n"; 167 return nullptr; 168 } 169 std::string ChildName = Ename.get().str(); 170 if (Verbose) 171 errs() << "Parsing member '" << ChildName 172 << "' of archive library to module.\n"; 173 SMDiagnostic ParseErr; 174 Expected<MemoryBufferRef> MemBuf = C.getMemoryBufferRef(); 175 if (Error E = MemBuf.takeError()) { 176 errs() << Argv0 << ": "; 177 WithColor::error() << " loading memory for member '" << ChildName 178 << "' of archive library failed'" << ArchiveName 179 << "'\n"; 180 return nullptr; 181 }; 182 183 if (!isBitcode(reinterpret_cast<const unsigned char *> 184 (MemBuf.get().getBufferStart()), 185 reinterpret_cast<const unsigned char *> 186 (MemBuf.get().getBufferEnd()))) { 187 errs() << Argv0 << ": "; 188 WithColor::error() << " member of archive is not a bitcode file: '" 189 << ChildName << "'\n"; 190 return nullptr; 191 } 192 193 std::unique_ptr<Module> M; 194 if (DisableLazyLoad) 195 M = parseIR(MemBuf.get(), ParseErr, Context); 196 else 197 M = getLazyIRModule(MemoryBuffer::getMemBuffer(MemBuf.get(), false), 198 ParseErr, Context); 199 200 if (!M.get()) { 201 errs() << Argv0 << ": "; 202 WithColor::error() << " parsing member '" << ChildName 203 << "' of archive library failed'" << ArchiveName 204 << "'\n"; 205 return nullptr; 206 } 207 if (Verbose) 208 errs() << "Linking member '" << ChildName << "' of archive library.\n"; 209 if (L.linkInModule(std::move(M))) 210 return nullptr; 211 } // end for each child 212 ExitOnErr(std::move(Err)); 213 return Result; 214 } 215 216 namespace { 217 218 /// Helper to load on demand a Module from file and cache it for subsequent 219 /// queries during function importing. 220 class ModuleLazyLoaderCache { 221 /// Cache of lazily loaded module for import. 222 StringMap<std::unique_ptr<Module>> ModuleMap; 223 224 /// Retrieve a Module from the cache or lazily load it on demand. 225 std::function<std::unique_ptr<Module>(const char *argv0, 226 const std::string &FileName)> 227 createLazyModule; 228 229 public: 230 /// Create the loader, Module will be initialized in \p Context. 231 ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>( 232 const char *argv0, const std::string &FileName)> 233 createLazyModule) 234 : createLazyModule(std::move(createLazyModule)) {} 235 236 /// Retrieve a Module from the cache or lazily load it on demand. 237 Module &operator()(const char *argv0, const std::string &FileName); 238 239 std::unique_ptr<Module> takeModule(const std::string &FileName) { 240 auto I = ModuleMap.find(FileName); 241 assert(I != ModuleMap.end()); 242 std::unique_ptr<Module> Ret = std::move(I->second); 243 ModuleMap.erase(I); 244 return Ret; 245 } 246 }; 247 248 // Get a Module for \p FileName from the cache, or load it lazily. 249 Module &ModuleLazyLoaderCache::operator()(const char *argv0, 250 const std::string &Identifier) { 251 auto &Module = ModuleMap[Identifier]; 252 if (!Module) { 253 Module = createLazyModule(argv0, Identifier); 254 assert(Module && "Failed to create lazy module!"); 255 } 256 return *Module; 257 } 258 } // anonymous namespace 259 260 namespace { 261 struct LLVMLinkDiagnosticHandler : public DiagnosticHandler { 262 bool handleDiagnostics(const DiagnosticInfo &DI) override { 263 unsigned Severity = DI.getSeverity(); 264 switch (Severity) { 265 case DS_Error: 266 WithColor::error(); 267 break; 268 case DS_Warning: 269 if (SuppressWarnings) 270 return true; 271 WithColor::warning(); 272 break; 273 case DS_Remark: 274 case DS_Note: 275 llvm_unreachable("Only expecting warnings and errors"); 276 } 277 278 DiagnosticPrinterRawOStream DP(errs()); 279 DI.print(DP); 280 errs() << '\n'; 281 return true; 282 } 283 }; 284 } 285 286 /// Import any functions requested via the -import option. 287 static bool importFunctions(const char *argv0, Module &DestModule) { 288 if (SummaryIndex.empty()) 289 return true; 290 std::unique_ptr<ModuleSummaryIndex> Index = 291 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); 292 293 // Map of Module -> List of globals to import from the Module 294 FunctionImporter::ImportMapTy ImportList; 295 296 auto ModuleLoader = [&DestModule](const char *argv0, 297 const std::string &Identifier) { 298 std::unique_ptr<MemoryBuffer> Buffer = 299 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Identifier))); 300 return loadFile(argv0, std::move(Buffer), DestModule.getContext(), false); 301 }; 302 303 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); 304 for (const auto &Import : Imports) { 305 // Identify the requested function and its bitcode source file. 306 size_t Idx = Import.find(':'); 307 if (Idx == std::string::npos) { 308 errs() << "Import parameter bad format: " << Import << "\n"; 309 return false; 310 } 311 std::string FunctionName = Import.substr(0, Idx); 312 std::string FileName = Import.substr(Idx + 1, std::string::npos); 313 314 // Load the specified source module. 315 auto &SrcModule = ModuleLoaderCache(argv0, FileName); 316 317 if (!NoVerify && verifyModule(SrcModule, &errs())) { 318 errs() << argv0 << ": " << FileName; 319 WithColor::error() << "input module is broken!\n"; 320 return false; 321 } 322 323 Function *F = SrcModule.getFunction(FunctionName); 324 if (!F) { 325 errs() << "Ignoring import request for non-existent function " 326 << FunctionName << " from " << FileName << "\n"; 327 continue; 328 } 329 // We cannot import weak_any functions without possibly affecting the 330 // order they are seen and selected by the linker, changing program 331 // semantics. 332 if (F->hasWeakAnyLinkage()) { 333 errs() << "Ignoring import request for weak-any function " << FunctionName 334 << " from " << FileName << "\n"; 335 continue; 336 } 337 338 if (Verbose) 339 errs() << "Importing " << FunctionName << " from " << FileName << "\n"; 340 341 auto &Entry = ImportList[FileName]; 342 Entry.insert(F->getGUID()); 343 } 344 auto CachedModuleLoader = [&](StringRef Identifier) { 345 return ModuleLoaderCache.takeModule(std::string(Identifier)); 346 }; 347 FunctionImporter Importer(*Index, CachedModuleLoader, 348 /*ClearDSOLocalOnDeclarations=*/false); 349 ExitOnErr(Importer.importFunctions(DestModule, ImportList)); 350 351 return true; 352 } 353 354 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L, 355 const cl::list<std::string> &Files, 356 unsigned Flags) { 357 // Filter out flags that don't apply to the first file we load. 358 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc; 359 // Similar to some flags, internalization doesn't apply to the first file. 360 bool InternalizeLinkedSymbols = false; 361 for (const auto &File : Files) { 362 std::unique_ptr<MemoryBuffer> Buffer = 363 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(File))); 364 365 std::unique_ptr<Module> M = 366 identify_magic(Buffer->getBuffer()) == file_magic::archive 367 ? loadArFile(argv0, std::move(Buffer), Context) 368 : loadFile(argv0, std::move(Buffer), Context); 369 if (!M.get()) { 370 errs() << argv0 << ": "; 371 WithColor::error() << " loading file '" << File << "'\n"; 372 return false; 373 } 374 375 // Note that when ODR merging types cannot verify input files in here When 376 // doing that debug metadata in the src module might already be pointing to 377 // the destination. 378 if (DisableDITypeMap && !NoVerify && verifyModule(*M, &errs())) { 379 errs() << argv0 << ": " << File << ": "; 380 WithColor::error() << "input module is broken!\n"; 381 return false; 382 } 383 384 // If a module summary index is supplied, load it so linkInModule can treat 385 // local functions/variables as exported and promote if necessary. 386 if (!SummaryIndex.empty()) { 387 std::unique_ptr<ModuleSummaryIndex> Index = 388 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); 389 390 // Conservatively mark all internal values as promoted, since this tool 391 // does not do the ThinLink that would normally determine what values to 392 // promote. 393 for (auto &I : *Index) { 394 for (auto &S : I.second.SummaryList) { 395 if (GlobalValue::isLocalLinkage(S->linkage())) 396 S->setLinkage(GlobalValue::ExternalLinkage); 397 } 398 } 399 400 // Promotion 401 if (renameModuleForThinLTO(*M, *Index, 402 /*ClearDSOLocalOnDeclarations=*/false)) 403 return true; 404 } 405 406 if (Verbose) 407 errs() << "Linking in '" << File << "'\n"; 408 409 bool Err = false; 410 if (InternalizeLinkedSymbols) { 411 Err = L.linkInModule( 412 std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) { 413 internalizeModule(M, [&GVS](const GlobalValue &GV) { 414 return !GV.hasName() || (GVS.count(GV.getName()) == 0); 415 }); 416 }); 417 } else { 418 Err = L.linkInModule(std::move(M), ApplicableFlags); 419 } 420 421 if (Err) 422 return false; 423 424 // Internalization applies to linking of subsequent files. 425 InternalizeLinkedSymbols = Internalize; 426 427 // All linker flags apply to linking of subsequent files. 428 ApplicableFlags = Flags; 429 } 430 431 return true; 432 } 433 434 int main(int argc, char **argv) { 435 InitLLVM X(argc, argv); 436 ExitOnErr.setBanner(std::string(argv[0]) + ": "); 437 438 LLVMContext Context; 439 Context.setDiagnosticHandler( 440 std::make_unique<LLVMLinkDiagnosticHandler>(), true); 441 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); 442 443 if (!DisableDITypeMap) 444 Context.enableDebugTypeODRUniquing(); 445 446 auto Composite = std::make_unique<Module>("llvm-link", Context); 447 Linker L(*Composite); 448 449 unsigned Flags = Linker::Flags::None; 450 if (OnlyNeeded) 451 Flags |= Linker::Flags::LinkOnlyNeeded; 452 453 // First add all the regular input files 454 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags)) 455 return 1; 456 457 // Next the -override ones. 458 if (!linkFiles(argv[0], Context, L, OverridingInputs, 459 Flags | Linker::Flags::OverrideFromSrc)) 460 return 1; 461 462 // Import any functions requested via -import 463 if (!importFunctions(argv[0], *Composite)) 464 return 1; 465 466 if (DumpAsm) 467 errs() << "Here's the assembly:\n" << *Composite; 468 469 std::error_code EC; 470 ToolOutputFile Out(OutputFilename, EC, 471 OutputAssembly ? sys::fs::OF_Text : sys::fs::OF_None); 472 if (EC) { 473 WithColor::error() << EC.message() << '\n'; 474 return 1; 475 } 476 477 if (!NoVerify && verifyModule(*Composite, &errs())) { 478 errs() << argv[0] << ": "; 479 WithColor::error() << "linked module is broken!\n"; 480 return 1; 481 } 482 483 if (Verbose) 484 errs() << "Writing bitcode...\n"; 485 if (OutputAssembly) { 486 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder); 487 } else if (Force || !CheckBitcodeOutputToConsole(Out.os())) 488 WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder); 489 490 // Declare success. 491 Out.keep(); 492 493 return 0; 494 } 495