Chris Lattner | 825937d | 2003-09-20 02:42:54 +0000 | [diff] [blame] | 1 | //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Misha Brukman | cf0c744 | 2003-09-15 18:34:34 +0000 | [diff] [blame] | 11 | // llvm-link a.bc b.bc c.bc -o x.bc |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Peter Collingbourne | c15d60b | 2017-05-01 20:42:32 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/BitcodeReader.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeWriter.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 18 | #include "llvm/IR/AutoUpgrade.h" |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
| 20 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/Module.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 23 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 25 | #include "llvm/IRReader/IRReader.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 26 | #include "llvm/Linker/Linker.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/InitLLVM.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 31 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 32 | #include "llvm/Support/SystemUtils.h" |
| 33 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 34 | #include "llvm/Support/WithColor.h" |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/IPO/FunctionImport.h" |
Jonas Devlieghere | 5eb9c81 | 2017-03-13 18:08:11 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/IPO/Internalize.h" |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 38 | |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 39 | #include <memory> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 40 | #include <utility> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 43 | static cl::list<std::string> |
| 44 | InputFilenames(cl::Positional, cl::OneOrMore, |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 45 | cl::desc("<input bitcode files>")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 46 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 47 | static cl::list<std::string> OverridingInputs( |
| 48 | "override", cl::ZeroOrMore, cl::value_desc("filename"), |
| 49 | cl::desc( |
| 50 | "input bitcode file which can override previously defined symbol(s)")); |
| 51 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 52 | // Option to simulate function importing for testing. This enables using |
| 53 | // llvm-link to simulate ThinLTO backend processes. |
| 54 | static cl::list<std::string> Imports( |
| 55 | "import", cl::ZeroOrMore, cl::value_desc("function:filename"), |
| 56 | cl::desc("Pair of function name and filename, where function should be " |
| 57 | "imported from bitcode in filename")); |
| 58 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 59 | // Option to support testing of function importing. The module summary |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 60 | // must be specified in the case were we request imports via the -import |
| 61 | // option, as well as when compiling any module with functions that may be |
| 62 | // exported (imported by a different llvm-link -import invocation), to ensure |
| 63 | // consistent promotion and renaming of locals. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 64 | static cl::opt<std::string> |
| 65 | SummaryIndex("summary-index", cl::desc("Module summary index filename"), |
| 66 | cl::init(""), cl::value_desc("filename")); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 67 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 68 | static cl::opt<std::string> |
| 69 | OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), |
| 70 | cl::value_desc("filename")); |
| 71 | |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 72 | static cl::opt<bool> |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 73 | Internalize("internalize", cl::desc("Internalize linked symbols")); |
| 74 | |
| 75 | static cl::opt<bool> |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame] | 76 | DisableDITypeMap("disable-debug-info-type-map", |
| 77 | cl::desc("Don't use a uniquing type map for debug info")); |
| 78 | |
| 79 | static cl::opt<bool> |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 80 | OnlyNeeded("only-needed", cl::desc("Link only needed symbols")); |
| 81 | |
| 82 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 83 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 84 | |
| 85 | static cl::opt<bool> |
Mehdi Amini | e470927 | 2016-09-14 22:29:59 +0000 | [diff] [blame] | 86 | DisableLazyLoad("disable-lazy-loading", |
Davide Italiano | aedafd4 | 2016-10-09 17:15:04 +0000 | [diff] [blame] | 87 | cl::desc("Disable lazy module loading")); |
Mehdi Amini | e470927 | 2016-09-14 22:29:59 +0000 | [diff] [blame] | 88 | |
| 89 | static cl::opt<bool> |
| 90 | OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden); |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 91 | |
| 92 | static cl::opt<bool> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 93 | Verbose("v", cl::desc("Print information about actions taken")); |
| 94 | |
| 95 | static cl::opt<bool> |
| 96 | DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); |
| 97 | |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 98 | static cl::opt<bool> |
| 99 | SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"), |
| 100 | cl::init(false)); |
| 101 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 102 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 103 | "preserve-bc-uselistorder", |
| 104 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 105 | cl::init(true), cl::Hidden); |
| 106 | |
| 107 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 108 | "preserve-ll-uselistorder", |
| 109 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 110 | cl::init(false), cl::Hidden); |
| 111 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 112 | static ExitOnError ExitOnErr; |
| 113 | |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 114 | // Read the specified bitcode file in and return it. This routine searches the |
| 115 | // link path for the specified file to try to find it... |
Reid Spencer | b956fc1 | 2004-09-12 23:39:42 +0000 | [diff] [blame] | 116 | // |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 117 | static std::unique_ptr<Module> loadFile(const char *argv0, |
| 118 | const std::string &FN, |
| 119 | LLVMContext &Context, |
| 120 | bool MaterializeMetadata = true) { |
Dan Gohman | 3d2c914 | 2009-09-12 21:55:12 +0000 | [diff] [blame] | 121 | SMDiagnostic Err; |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 122 | if (Verbose) |
| 123 | errs() << "Loading '" << FN << "'\n"; |
Mehdi Amini | e470927 | 2016-09-14 22:29:59 +0000 | [diff] [blame] | 124 | std::unique_ptr<Module> Result; |
| 125 | if (DisableLazyLoad) |
| 126 | Result = parseIRFile(FN, Err, Context); |
| 127 | else |
| 128 | Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata); |
| 129 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 130 | if (!Result) { |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 131 | Err.print(argv0, errs()); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 132 | return nullptr; |
| 133 | } |
Reid Spencer | fe020a3 | 2004-09-11 04:32:42 +0000 | [diff] [blame] | 134 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 135 | if (MaterializeMetadata) { |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 136 | ExitOnErr(Result->materializeMetadata()); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 137 | UpgradeDebugInfo(*Result); |
| 138 | } |
Rafael Espindola | 2fcfb5e | 2015-03-27 15:55:06 +0000 | [diff] [blame] | 139 | |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 140 | return Result; |
Chris Lattner | ae31f5b | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 142 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 143 | namespace { |
| 144 | |
| 145 | /// Helper to load on demand a Module from file and cache it for subsequent |
| 146 | /// queries during function importing. |
| 147 | class ModuleLazyLoaderCache { |
| 148 | /// Cache of lazily loaded module for import. |
| 149 | StringMap<std::unique_ptr<Module>> ModuleMap; |
| 150 | |
| 151 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 152 | std::function<std::unique_ptr<Module>(const char *argv0, |
| 153 | const std::string &FileName)> |
| 154 | createLazyModule; |
| 155 | |
| 156 | public: |
| 157 | /// Create the loader, Module will be initialized in \p Context. |
| 158 | ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>( |
| 159 | const char *argv0, const std::string &FileName)> |
| 160 | createLazyModule) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 161 | : createLazyModule(std::move(createLazyModule)) {} |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 162 | |
| 163 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 164 | Module &operator()(const char *argv0, const std::string &FileName); |
| 165 | |
| 166 | std::unique_ptr<Module> takeModule(const std::string &FileName) { |
| 167 | auto I = ModuleMap.find(FileName); |
| 168 | assert(I != ModuleMap.end()); |
| 169 | std::unique_ptr<Module> Ret = std::move(I->second); |
| 170 | ModuleMap.erase(I); |
| 171 | return Ret; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | // Get a Module for \p FileName from the cache, or load it lazily. |
| 176 | Module &ModuleLazyLoaderCache::operator()(const char *argv0, |
| 177 | const std::string &Identifier) { |
| 178 | auto &Module = ModuleMap[Identifier]; |
| 179 | if (!Module) |
| 180 | Module = createLazyModule(argv0, Identifier); |
| 181 | return *Module; |
| 182 | } |
| 183 | } // anonymous namespace |
| 184 | |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 185 | namespace { |
| 186 | struct LLVMLinkDiagnosticHandler : public DiagnosticHandler { |
| 187 | bool handleDiagnostics(const DiagnosticInfo &DI) override { |
| 188 | unsigned Severity = DI.getSeverity(); |
| 189 | switch (Severity) { |
| 190 | case DS_Error: |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 191 | WithColor::error(); |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 192 | break; |
| 193 | case DS_Warning: |
| 194 | if (SuppressWarnings) |
| 195 | return true; |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 196 | WithColor::warning(); |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 197 | break; |
| 198 | case DS_Remark: |
| 199 | case DS_Note: |
| 200 | llvm_unreachable("Only expecting warnings and errors"); |
| 201 | } |
Vivek Pandya | df8598d | 2017-09-15 19:53:54 +0000 | [diff] [blame] | 202 | |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 203 | DiagnosticPrinterRawOStream DP(errs()); |
| 204 | DI.print(DP); |
| 205 | errs() << '\n'; |
| 206 | return true; |
| 207 | } |
| 208 | }; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 211 | /// Import any functions requested via the -import option. |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 212 | static bool importFunctions(const char *argv0, Module &DestModule) { |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 213 | if (SummaryIndex.empty()) |
| 214 | return true; |
Peter Collingbourne | 6de481a | 2016-11-11 19:50:39 +0000 | [diff] [blame] | 215 | std::unique_ptr<ModuleSummaryIndex> Index = |
| 216 | ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 217 | |
| 218 | // Map of Module -> List of globals to import from the Module |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 219 | FunctionImporter::ImportMapTy ImportList; |
| 220 | |
| 221 | auto ModuleLoader = [&DestModule](const char *argv0, |
| 222 | const std::string &Identifier) { |
| 223 | return loadFile(argv0, Identifier, DestModule.getContext(), false); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 224 | }; |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 225 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 226 | ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 227 | for (const auto &Import : Imports) { |
| 228 | // Identify the requested function and its bitcode source file. |
| 229 | size_t Idx = Import.find(':'); |
| 230 | if (Idx == std::string::npos) { |
| 231 | errs() << "Import parameter bad format: " << Import << "\n"; |
| 232 | return false; |
| 233 | } |
| 234 | std::string FunctionName = Import.substr(0, Idx); |
| 235 | std::string FileName = Import.substr(Idx + 1, std::string::npos); |
| 236 | |
| 237 | // Load the specified source module. |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 238 | auto &SrcModule = ModuleLoaderCache(argv0, FileName); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 239 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 240 | if (verifyModule(SrcModule, &errs())) { |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 241 | errs() << argv0 << ": " << FileName; |
| 242 | WithColor::error() << "input module is broken!\n"; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 246 | Function *F = SrcModule.getFunction(FunctionName); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 247 | if (!F) { |
| 248 | errs() << "Ignoring import request for non-existent function " |
| 249 | << FunctionName << " from " << FileName << "\n"; |
| 250 | continue; |
| 251 | } |
| 252 | // We cannot import weak_any functions without possibly affecting the |
| 253 | // order they are seen and selected by the linker, changing program |
| 254 | // semantics. |
| 255 | if (F->hasWeakAnyLinkage()) { |
| 256 | errs() << "Ignoring import request for weak-any function " << FunctionName |
| 257 | << " from " << FileName << "\n"; |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | if (Verbose) |
| 262 | errs() << "Importing " << FunctionName << " from " << FileName << "\n"; |
| 263 | |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 264 | auto &Entry = ImportList[FileName]; |
Teresa Johnson | d68935c | 2018-07-16 15:30:27 +0000 | [diff] [blame^] | 265 | Entry.insert(F->getGUID()); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 266 | } |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 267 | auto CachedModuleLoader = [&](StringRef Identifier) { |
| 268 | return ModuleLoaderCache.takeModule(Identifier); |
| 269 | }; |
| 270 | FunctionImporter Importer(*Index, CachedModuleLoader); |
| 271 | ExitOnErr(Importer.importFunctions(DestModule, ImportList)); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 272 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 276 | static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L, |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 277 | const cl::list<std::string> &Files, |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 278 | unsigned Flags) { |
| 279 | // Filter out flags that don't apply to the first file we load. |
| 280 | unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc; |
Jonas Devlieghere | 5eb9c81 | 2017-03-13 18:08:11 +0000 | [diff] [blame] | 281 | // Similar to some flags, internalization doesn't apply to the first file. |
| 282 | bool InternalizeLinkedSymbols = false; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 283 | for (const auto &File : Files) { |
| 284 | std::unique_ptr<Module> M = loadFile(argv0, File, Context); |
| 285 | if (!M.get()) { |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 286 | errs() << argv0 << ": "; |
| 287 | WithColor::error() << " loading file '" << File << "'\n"; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
Rafael Espindola | 2211f01 | 2016-06-29 18:31:48 +0000 | [diff] [blame] | 291 | // Note that when ODR merging types cannot verify input files in here When |
| 292 | // doing that debug metadata in the src module might already be pointing to |
| 293 | // the destination. |
| 294 | if (DisableDITypeMap && verifyModule(*M, &errs())) { |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 295 | errs() << argv0 << ": " << File << ": "; |
| 296 | WithColor::error() << "input module is broken!\n"; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 300 | // If a module summary index is supplied, load it so linkInModule can treat |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 301 | // local functions/variables as exported and promote if necessary. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 302 | if (!SummaryIndex.empty()) { |
Peter Collingbourne | 6de481a | 2016-11-11 19:50:39 +0000 | [diff] [blame] | 303 | std::unique_ptr<ModuleSummaryIndex> Index = |
| 304 | ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 305 | |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 306 | // Conservatively mark all internal values as promoted, since this tool |
| 307 | // does not do the ThinLink that would normally determine what values to |
| 308 | // promote. |
| 309 | for (auto &I : *Index) { |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 310 | for (auto &S : I.second.SummaryList) { |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 311 | if (GlobalValue::isLocalLinkage(S->linkage())) |
| 312 | S->setLinkage(GlobalValue::ExternalLinkage); |
| 313 | } |
| 314 | } |
| 315 | |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 316 | // Promotion |
| 317 | if (renameModuleForThinLTO(*M, *Index)) |
| 318 | return true; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 321 | if (Verbose) |
| 322 | errs() << "Linking in '" << File << "'\n"; |
| 323 | |
Jonas Devlieghere | 5eb9c81 | 2017-03-13 18:08:11 +0000 | [diff] [blame] | 324 | bool Err = false; |
| 325 | if (InternalizeLinkedSymbols) { |
| 326 | Err = L.linkInModule( |
| 327 | std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) { |
David Blaikie | 427f426 | 2017-03-13 21:46:14 +0000 | [diff] [blame] | 328 | internalizeModule(M, [&GVS](const GlobalValue &GV) { |
Jonas Devlieghere | 5eb9c81 | 2017-03-13 18:08:11 +0000 | [diff] [blame] | 329 | return !GV.hasName() || (GVS.count(GV.getName()) == 0); |
| 330 | }); |
| 331 | }); |
| 332 | } else { |
| 333 | Err = L.linkInModule(std::move(M), ApplicableFlags); |
| 334 | } |
| 335 | |
| 336 | if (Err) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 337 | return false; |
Jonas Devlieghere | 5eb9c81 | 2017-03-13 18:08:11 +0000 | [diff] [blame] | 338 | |
| 339 | // Internalization applies to linking of subsequent files. |
| 340 | InternalizeLinkedSymbols = Internalize; |
| 341 | |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 342 | // All linker flags apply to linking of subsequent files. |
| 343 | ApplicableFlags = Flags; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 349 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 350 | InitLLVM X(argc, argv); |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 351 | ExitOnErr.setBanner(std::string(argv[0]) + ": "); |
| 352 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 353 | LLVMContext Context; |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 354 | Context.setDiagnosticHandler( |
| 355 | llvm::make_unique<LLVMLinkDiagnosticHandler>(), true); |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 356 | cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 357 | |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame] | 358 | if (!DisableDITypeMap) |
Duncan P. N. Exon Smith | ed8fdb2 | 2016-04-19 04:55:25 +0000 | [diff] [blame] | 359 | Context.enableDebugTypeODRUniquing(); |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | 9a2bf41 | 2018-02-21 20:12:18 +0000 | [diff] [blame] | 361 | auto Composite = make_unique<Module>("llvm-link", Context); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 362 | Linker L(*Composite); |
Rafael Espindola | 957eae2 | 2014-10-23 19:40:45 +0000 | [diff] [blame] | 363 | |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 364 | unsigned Flags = Linker::Flags::None; |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 365 | if (OnlyNeeded) |
| 366 | Flags |= Linker::Flags::LinkOnlyNeeded; |
| 367 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 368 | // First add all the regular input files |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 369 | if (!linkFiles(argv[0], Context, L, InputFilenames, Flags)) |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 370 | return 1; |
| 371 | |
| 372 | // Next the -override ones. |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 373 | if (!linkFiles(argv[0], Context, L, OverridingInputs, |
| 374 | Flags | Linker::Flags::OverrideFromSrc)) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 375 | return 1; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 376 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 377 | // Import any functions requested via -import |
Teresa Johnson | 0fca905 | 2017-01-04 14:27:31 +0000 | [diff] [blame] | 378 | if (!importFunctions(argv[0], *Composite)) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 379 | return 1; |
| 380 | |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 381 | if (DumpAsm) |
| 382 | errs() << "Here's the assembly:\n" << *Composite; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 383 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 384 | std::error_code EC; |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 385 | ToolOutputFile Out(OutputFilename, EC, sys::fs::F_None); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 386 | if (EC) { |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 387 | WithColor::error() << EC.message() << '\n'; |
Chris Lattner | abd1736 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 388 | return 1; |
| 389 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 390 | |
Duncan P. N. Exon Smith | 4628282 | 2015-03-31 03:07:23 +0000 | [diff] [blame] | 391 | if (verifyModule(*Composite, &errs())) { |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 392 | errs() << argv[0] << ": "; |
| 393 | WithColor::error() << "linked module is broken!\n"; |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 394 | return 1; |
| 395 | } |
| 396 | |
Jonas Devlieghere | fef7ada | 2018-04-18 14:41:47 +0000 | [diff] [blame] | 397 | if (Verbose) |
| 398 | errs() << "Writing bitcode...\n"; |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 399 | if (OutputAssembly) { |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 400 | Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 401 | } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 402 | WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder); |
Dan Gohman | 4cc73ba | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 403 | |
| 404 | // Declare success. |
| 405 | Out.keep(); |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 407 | return 0; |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 408 | } |