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" |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 17 | #include "llvm/IR/AutoUpgrade.h" |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticInfo.h" |
| 19 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/Module.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 24 | #include "llvm/IRReader/IRReader.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 25 | #include "llvm/Linker/Linker.h" |
| 26 | #include "llvm/Object/ModuleSummaryIndexObjectFile.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" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 31 | #include "llvm/Support/PrettyStackTrace.h" |
| 32 | #include "llvm/Support/Signals.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SystemUtils.h" |
| 35 | #include "llvm/Support/ToolOutputFile.h" |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 37 | |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 38 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 41 | static cl::list<std::string> |
| 42 | InputFilenames(cl::Positional, cl::OneOrMore, |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 43 | cl::desc("<input bitcode files>")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 44 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 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 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 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 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 57 | // Option to support testing of function importing. The module summary |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 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. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 62 | static cl::opt<std::string> |
| 63 | SummaryIndex("summary-index", cl::desc("Module summary index filename"), |
| 64 | cl::init(""), cl::value_desc("filename")); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 66 | static cl::opt<std::string> |
| 67 | OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), |
| 68 | cl::value_desc("filename")); |
| 69 | |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 70 | static cl::opt<bool> |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 71 | Internalize("internalize", cl::desc("Internalize linked symbols")); |
| 72 | |
| 73 | static cl::opt<bool> |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame^] | 74 | DisableDITypeMap("disable-debug-info-type-map", |
| 75 | cl::desc("Don't use a uniquing type map for debug info")); |
| 76 | |
| 77 | static cl::opt<bool> |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 78 | OnlyNeeded("only-needed", cl::desc("Link only needed symbols")); |
| 79 | |
| 80 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 81 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 82 | |
| 83 | static cl::opt<bool> |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 84 | OutputAssembly("S", |
| 85 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 86 | |
| 87 | static cl::opt<bool> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 88 | Verbose("v", cl::desc("Print information about actions taken")); |
| 89 | |
| 90 | static cl::opt<bool> |
| 91 | DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); |
| 92 | |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 93 | static cl::opt<bool> |
| 94 | SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"), |
| 95 | cl::init(false)); |
| 96 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 97 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 98 | "preserve-bc-uselistorder", |
| 99 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 100 | cl::init(true), cl::Hidden); |
| 101 | |
| 102 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 103 | "preserve-ll-uselistorder", |
| 104 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 105 | cl::init(false), cl::Hidden); |
| 106 | |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 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... |
Reid Spencer | b956fc1 | 2004-09-12 23:39:42 +0000 | [diff] [blame] | 109 | // |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 110 | static std::unique_ptr<Module> loadFile(const char *argv0, |
| 111 | const std::string &FN, |
| 112 | LLVMContext &Context, |
| 113 | bool MaterializeMetadata = true) { |
Dan Gohman | 3d2c914 | 2009-09-12 21:55:12 +0000 | [diff] [blame] | 114 | SMDiagnostic Err; |
Rafael Espindola | f1f1273 | 2013-06-17 17:32:19 +0000 | [diff] [blame] | 115 | if (Verbose) errs() << "Loading '" << FN << "'\n"; |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 116 | std::unique_ptr<Module> Result = |
| 117 | getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 118 | if (!Result) { |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 119 | Err.print(argv0, errs()); |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 120 | return nullptr; |
| 121 | } |
Reid Spencer | fe020a3 | 2004-09-11 04:32:42 +0000 | [diff] [blame] | 122 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 123 | if (MaterializeMetadata) { |
| 124 | Result->materializeMetadata(); |
| 125 | UpgradeDebugInfo(*Result); |
| 126 | } |
Rafael Espindola | 2fcfb5e | 2015-03-27 15:55:06 +0000 | [diff] [blame] | 127 | |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 128 | return Result; |
Chris Lattner | ae31f5b | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 130 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 131 | namespace { |
| 132 | |
| 133 | /// Helper to load on demand a Module from file and cache it for subsequent |
| 134 | /// queries during function importing. |
| 135 | class ModuleLazyLoaderCache { |
| 136 | /// Cache of lazily loaded module for import. |
| 137 | StringMap<std::unique_ptr<Module>> ModuleMap; |
| 138 | |
| 139 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 140 | std::function<std::unique_ptr<Module>(const char *argv0, |
| 141 | const std::string &FileName)> |
| 142 | createLazyModule; |
| 143 | |
| 144 | public: |
| 145 | /// Create the loader, Module will be initialized in \p Context. |
| 146 | ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>( |
| 147 | const char *argv0, const std::string &FileName)> |
| 148 | createLazyModule) |
| 149 | : createLazyModule(createLazyModule) {} |
| 150 | |
| 151 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 152 | Module &operator()(const char *argv0, const std::string &FileName); |
| 153 | |
| 154 | std::unique_ptr<Module> takeModule(const std::string &FileName) { |
| 155 | auto I = ModuleMap.find(FileName); |
| 156 | assert(I != ModuleMap.end()); |
| 157 | std::unique_ptr<Module> Ret = std::move(I->second); |
| 158 | ModuleMap.erase(I); |
| 159 | return Ret; |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | // Get a Module for \p FileName from the cache, or load it lazily. |
| 164 | Module &ModuleLazyLoaderCache::operator()(const char *argv0, |
| 165 | const std::string &Identifier) { |
| 166 | auto &Module = ModuleMap[Identifier]; |
| 167 | if (!Module) |
| 168 | Module = createLazyModule(argv0, Identifier); |
| 169 | return *Module; |
| 170 | } |
| 171 | } // anonymous namespace |
| 172 | |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 173 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 174 | unsigned Severity = DI.getSeverity(); |
| 175 | switch (Severity) { |
| 176 | case DS_Error: |
| 177 | errs() << "ERROR: "; |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 178 | break; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 179 | case DS_Warning: |
| 180 | if (SuppressWarnings) |
| 181 | return; |
| 182 | errs() << "WARNING: "; |
| 183 | break; |
| 184 | case DS_Remark: |
| 185 | case DS_Note: |
| 186 | llvm_unreachable("Only expecting warnings and errors"); |
| 187 | } |
| 188 | |
| 189 | DiagnosticPrinterRawOStream DP(errs()); |
| 190 | DI.print(DP); |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 191 | errs() << '\n'; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 194 | static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) { |
| 195 | diagnosticHandler(DI); |
| 196 | } |
| 197 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 198 | /// Import any functions requested via the -import option. |
| 199 | static bool importFunctions(const char *argv0, LLVMContext &Context, |
| 200 | Linker &L) { |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 201 | if (SummaryIndex.empty()) |
| 202 | return true; |
| 203 | ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = |
| 204 | llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler); |
| 205 | std::error_code EC = IndexOrErr.getError(); |
| 206 | if (EC) { |
| 207 | errs() << EC.message() << '\n'; |
| 208 | return false; |
| 209 | } |
| 210 | auto Index = std::move(IndexOrErr.get()); |
| 211 | |
| 212 | // Map of Module -> List of globals to import from the Module |
| 213 | std::map<StringRef, DenseSet<const GlobalValue *>> ModuleToGlobalsToImportMap; |
| 214 | auto ModuleLoader = [&Context](const char *argv0, |
| 215 | const std::string &Identifier) { |
| 216 | return loadFile(argv0, Identifier, Context, false); |
| 217 | }; |
| 218 | ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 219 | for (const auto &Import : Imports) { |
| 220 | // Identify the requested function and its bitcode source file. |
| 221 | size_t Idx = Import.find(':'); |
| 222 | if (Idx == std::string::npos) { |
| 223 | errs() << "Import parameter bad format: " << Import << "\n"; |
| 224 | return false; |
| 225 | } |
| 226 | std::string FunctionName = Import.substr(0, Idx); |
| 227 | std::string FileName = Import.substr(Idx + 1, std::string::npos); |
| 228 | |
| 229 | // Load the specified source module. |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 230 | auto &SrcModule = ModuleLoaderCache(argv0, FileName); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 231 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 232 | if (verifyModule(SrcModule, &errs())) { |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 233 | errs() << argv0 << ": " << FileName |
| 234 | << ": error: input module is broken!\n"; |
| 235 | return false; |
| 236 | } |
| 237 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 238 | Function *F = SrcModule.getFunction(FunctionName); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 239 | if (!F) { |
| 240 | errs() << "Ignoring import request for non-existent function " |
| 241 | << FunctionName << " from " << FileName << "\n"; |
| 242 | continue; |
| 243 | } |
| 244 | // We cannot import weak_any functions without possibly affecting the |
| 245 | // order they are seen and selected by the linker, changing program |
| 246 | // semantics. |
| 247 | if (F->hasWeakAnyLinkage()) { |
| 248 | errs() << "Ignoring import request for weak-any function " << FunctionName |
| 249 | << " from " << FileName << "\n"; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if (Verbose) |
| 254 | errs() << "Importing " << FunctionName << " from " << FileName << "\n"; |
| 255 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 256 | auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()]; |
| 257 | Entry.insert(F); |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 258 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 259 | F->materialize(); |
| 260 | } |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 261 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 262 | // Do the actual import of globals now, one Module at a time |
| 263 | for (auto &GlobalsToImportPerModule : ModuleToGlobalsToImportMap) { |
| 264 | // Get the module for the import |
| 265 | auto &GlobalsToImport = GlobalsToImportPerModule.second; |
| 266 | std::unique_ptr<Module> SrcModule = |
| 267 | ModuleLoaderCache.takeModule(GlobalsToImportPerModule.first); |
| 268 | assert(&Context == &SrcModule->getContext() && "Context mismatch"); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 269 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 270 | // If modules were created with lazy metadata loading, materialize it |
| 271 | // now, before linking it (otherwise this will be a noop). |
| 272 | SrcModule->materializeMetadata(); |
| 273 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 274 | |
Teresa Johnson | f4cc752 | 2016-03-24 19:52:20 +0000 | [diff] [blame] | 275 | // Linkage Promotion and renaming |
| 276 | if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport)) |
| 277 | return true; |
| 278 | |
| 279 | if (L.linkInModule(std::move(SrcModule), Linker::Flags::None, |
| 280 | &GlobalsToImport)) |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 287 | 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] | 288 | const cl::list<std::string> &Files, |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 289 | unsigned Flags) { |
| 290 | // Filter out flags that don't apply to the first file we load. |
| 291 | unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 292 | for (const auto &File : Files) { |
| 293 | std::unique_ptr<Module> M = loadFile(argv0, File, Context); |
| 294 | if (!M.get()) { |
| 295 | errs() << argv0 << ": error loading file '" << File << "'\n"; |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | if (verifyModule(*M, &errs())) { |
| 300 | errs() << argv0 << ": " << File << ": error: input module is broken!\n"; |
| 301 | return false; |
| 302 | } |
| 303 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 304 | // 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] | 305 | // local functions/variables as exported and promote if necessary. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 306 | if (!SummaryIndex.empty()) { |
| 307 | ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = |
| 308 | llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 309 | std::error_code EC = IndexOrErr.getError(); |
| 310 | if (EC) { |
| 311 | errs() << EC.message() << '\n'; |
| 312 | return false; |
| 313 | } |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 314 | auto Index = std::move(IndexOrErr.get()); |
| 315 | |
| 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 | |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 324 | if (L.linkInModule(std::move(M), ApplicableFlags)) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 325 | return false; |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 326 | // All linker flags apply to linking of subsequent files. |
| 327 | ApplicableFlags = Flags; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 333 | int main(int argc, char **argv) { |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 334 | // Print a stack trace if we signal out. |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 335 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 336 | PrettyStackTraceProgram X(argc, argv); |
Andrew Trick | dc073ad | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 337 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 338 | LLVMContext Context; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 339 | Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
| 340 | |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 341 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 342 | cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 343 | |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame^] | 344 | if (!DisableDITypeMap) |
| 345 | Context.ensureDITypeMap(); |
| 346 | |
Rafael Espindola | 957eae2 | 2014-10-23 19:40:45 +0000 | [diff] [blame] | 347 | auto Composite = make_unique<Module>("llvm-link", Context); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 348 | Linker L(*Composite); |
Rafael Espindola | 957eae2 | 2014-10-23 19:40:45 +0000 | [diff] [blame] | 349 | |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 350 | unsigned Flags = Linker::Flags::None; |
| 351 | if (Internalize) |
| 352 | Flags |= Linker::Flags::InternalizeLinkedSymbols; |
| 353 | if (OnlyNeeded) |
| 354 | Flags |= Linker::Flags::LinkOnlyNeeded; |
| 355 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 356 | // First add all the regular input files |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 357 | if (!linkFiles(argv[0], Context, L, InputFilenames, Flags)) |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 358 | return 1; |
| 359 | |
| 360 | // Next the -override ones. |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 361 | if (!linkFiles(argv[0], Context, L, OverridingInputs, |
| 362 | Flags | Linker::Flags::OverrideFromSrc)) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 363 | return 1; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 364 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 365 | // Import any functions requested via -import |
| 366 | if (!importFunctions(argv[0], Context, L)) |
| 367 | return 1; |
| 368 | |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 369 | if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 370 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 371 | std::error_code EC; |
| 372 | tool_output_file Out(OutputFilename, EC, sys::fs::F_None); |
| 373 | if (EC) { |
| 374 | errs() << EC.message() << '\n'; |
Chris Lattner | abd1736 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 375 | return 1; |
| 376 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 377 | |
Duncan P. N. Exon Smith | 4628282 | 2015-03-31 03:07:23 +0000 | [diff] [blame] | 378 | if (verifyModule(*Composite, &errs())) { |
| 379 | errs() << argv[0] << ": error: linked module is broken!\n"; |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 380 | return 1; |
| 381 | } |
| 382 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 383 | if (Verbose) errs() << "Writing bitcode...\n"; |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 384 | if (OutputAssembly) { |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 385 | Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 386 | } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 387 | WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder); |
Dan Gohman | 4cc73ba | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 388 | |
| 389 | // Declare success. |
| 390 | Out.keep(); |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 392 | return 0; |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 393 | } |