llvm-link.cpp 18 KB

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