llvm-link.cpp 18 KB

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