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 | |
Chandler Carruth | 6cc07df | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 15 | #include "llvm/Linker/Linker.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/ReaderWriter.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" |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 21 | #include "llvm/IR/FunctionInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/Module.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 | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 26 | #include "llvm/Object/FunctionIndexObjectFile.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" |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 36 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 39 | static cl::list<std::string> |
| 40 | InputFilenames(cl::Positional, cl::OneOrMore, |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 41 | cl::desc("<input bitcode files>")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 43 | static cl::list<std::string> OverridingInputs( |
| 44 | "override", cl::ZeroOrMore, cl::value_desc("filename"), |
| 45 | cl::desc( |
| 46 | "input bitcode file which can override previously defined symbol(s)")); |
| 47 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 48 | // Option to simulate function importing for testing. This enables using |
| 49 | // llvm-link to simulate ThinLTO backend processes. |
| 50 | static cl::list<std::string> Imports( |
| 51 | "import", cl::ZeroOrMore, cl::value_desc("function:filename"), |
| 52 | cl::desc("Pair of function name and filename, where function should be " |
| 53 | "imported from bitcode in filename")); |
| 54 | |
| 55 | // Option to support testing of function importing. The function index |
| 56 | // must be specified in the case were we request imports via the -import |
| 57 | // option, as well as when compiling any module with functions that may be |
| 58 | // exported (imported by a different llvm-link -import invocation), to ensure |
| 59 | // consistent promotion and renaming of locals. |
| 60 | static cl::opt<std::string> FunctionIndex("functionindex", |
| 61 | cl::desc("Function index filename"), |
| 62 | cl::init(""), |
| 63 | cl::value_desc("filename")); |
| 64 | |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 65 | static cl::opt<std::string> |
| 66 | OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), |
| 67 | cl::value_desc("filename")); |
| 68 | |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 69 | static cl::opt<bool> |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 70 | Internalize("internalize", cl::desc("Internalize linked symbols")); |
| 71 | |
| 72 | static cl::opt<bool> |
| 73 | OnlyNeeded("only-needed", cl::desc("Link only needed symbols")); |
| 74 | |
| 75 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 76 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 77 | |
| 78 | static cl::opt<bool> |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 79 | OutputAssembly("S", |
| 80 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 81 | |
| 82 | static cl::opt<bool> |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 83 | Verbose("v", cl::desc("Print information about actions taken")); |
| 84 | |
| 85 | static cl::opt<bool> |
| 86 | DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden); |
| 87 | |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 88 | static cl::opt<bool> |
| 89 | SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"), |
| 90 | cl::init(false)); |
| 91 | |
Teresa Johnson | 5bc88be | 2015-11-21 00:35:38 +0000 | [diff] [blame] | 92 | static cl::opt<bool> |
| 93 | PreserveModules("preserve-modules", |
| 94 | cl::desc("Preserve linked modules for testing")); |
| 95 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 96 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 97 | "preserve-bc-uselistorder", |
| 98 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 99 | cl::init(true), cl::Hidden); |
| 100 | |
| 101 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 102 | "preserve-ll-uselistorder", |
| 103 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 104 | cl::init(false), cl::Hidden); |
| 105 | |
Rafael Espindola | d233b06 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 106 | // Read the specified bitcode file in and return it. This routine searches the |
| 107 | // link path for the specified file to try to find it... |
Reid Spencer | b956fc1 | 2004-09-12 23:39:42 +0000 | [diff] [blame] | 108 | // |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 109 | static std::unique_ptr<Module> loadFile(const char *argv0, |
| 110 | const std::string &FN, |
| 111 | LLVMContext &Context, |
| 112 | bool MaterializeMetadata = true) { |
Dan Gohman | 3d2c914 | 2009-09-12 21:55:12 +0000 | [diff] [blame] | 113 | SMDiagnostic Err; |
Rafael Espindola | f1f1273 | 2013-06-17 17:32:19 +0000 | [diff] [blame] | 114 | if (Verbose) errs() << "Loading '" << FN << "'\n"; |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 115 | std::unique_ptr<Module> Result = |
| 116 | getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata); |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 117 | if (!Result) |
| 118 | Err.print(argv0, errs()); |
Reid Spencer | fe020a3 | 2004-09-11 04:32:42 +0000 | [diff] [blame] | 119 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 120 | if (MaterializeMetadata) { |
| 121 | Result->materializeMetadata(); |
| 122 | UpgradeDebugInfo(*Result); |
| 123 | } |
Rafael Espindola | 2fcfb5e | 2015-03-27 15:55:06 +0000 | [diff] [blame] | 124 | |
Rafael Espindola | 5c4f4a6 | 2014-08-26 18:03:35 +0000 | [diff] [blame] | 125 | return Result; |
Chris Lattner | ae31f5b | 2001-10-24 06:23:00 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 127 | |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 128 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 129 | unsigned Severity = DI.getSeverity(); |
| 130 | switch (Severity) { |
| 131 | case DS_Error: |
| 132 | errs() << "ERROR: "; |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 133 | break; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 134 | case DS_Warning: |
| 135 | if (SuppressWarnings) |
| 136 | return; |
| 137 | errs() << "WARNING: "; |
| 138 | break; |
| 139 | case DS_Remark: |
| 140 | case DS_Note: |
| 141 | llvm_unreachable("Only expecting warnings and errors"); |
| 142 | } |
| 143 | |
| 144 | DiagnosticPrinterRawOStream DP(errs()); |
| 145 | DI.print(DP); |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 146 | errs() << '\n'; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 149 | static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) { |
| 150 | diagnosticHandler(DI); |
| 151 | } |
| 152 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 153 | /// Import any functions requested via the -import option. |
| 154 | static bool importFunctions(const char *argv0, LLVMContext &Context, |
| 155 | Linker &L) { |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 156 | StringMap<std::unique_ptr<DenseMap<unsigned, MDNode *>>> |
| 157 | ModuleToTempMDValsMap; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 158 | for (const auto &Import : Imports) { |
| 159 | // Identify the requested function and its bitcode source file. |
| 160 | size_t Idx = Import.find(':'); |
| 161 | if (Idx == std::string::npos) { |
| 162 | errs() << "Import parameter bad format: " << Import << "\n"; |
| 163 | return false; |
| 164 | } |
| 165 | std::string FunctionName = Import.substr(0, Idx); |
| 166 | std::string FileName = Import.substr(Idx + 1, std::string::npos); |
| 167 | |
| 168 | // Load the specified source module. |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 169 | std::unique_ptr<Module> M = loadFile(argv0, FileName, Context, false); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 170 | if (!M.get()) { |
| 171 | errs() << argv0 << ": error loading file '" << FileName << "'\n"; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | if (verifyModule(*M, &errs())) { |
| 176 | errs() << argv0 << ": " << FileName |
| 177 | << ": error: input module is broken!\n"; |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | Function *F = M->getFunction(FunctionName); |
| 182 | if (!F) { |
| 183 | errs() << "Ignoring import request for non-existent function " |
| 184 | << FunctionName << " from " << FileName << "\n"; |
| 185 | continue; |
| 186 | } |
| 187 | // We cannot import weak_any functions without possibly affecting the |
| 188 | // order they are seen and selected by the linker, changing program |
| 189 | // semantics. |
| 190 | if (F->hasWeakAnyLinkage()) { |
| 191 | errs() << "Ignoring import request for weak-any function " << FunctionName |
| 192 | << " from " << FileName << "\n"; |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | if (Verbose) |
| 197 | errs() << "Importing " << FunctionName << " from " << FileName << "\n"; |
| 198 | |
| 199 | std::unique_ptr<FunctionInfoIndex> Index; |
| 200 | if (!FunctionIndex.empty()) { |
Teresa Johnson | 6b92316 | 2015-11-23 19:19:11 +0000 | [diff] [blame] | 201 | ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = |
| 202 | llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 203 | std::error_code EC = IndexOrErr.getError(); |
| 204 | if (EC) { |
| 205 | errs() << EC.message() << '\n'; |
| 206 | return false; |
| 207 | } |
| 208 | Index = std::move(IndexOrErr.get()); |
| 209 | } |
| 210 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 211 | // Save the mapping of value ids to temporary metadata created when |
| 212 | // importing this function. If we have already imported from this module, |
| 213 | // add new temporary metadata to the existing mapping. |
| 214 | auto &TempMDVals = ModuleToTempMDValsMap[FileName]; |
| 215 | if (!TempMDVals) |
| 216 | TempMDVals = llvm::make_unique<DenseMap<unsigned, MDNode *>>(); |
| 217 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 218 | // Link in the specified function. |
Mehdi Amini | 7d11004 | 2015-12-03 02:40:39 +0000 | [diff] [blame] | 219 | DenseSet<const GlobalValue *> FunctionsToImport; |
| 220 | FunctionsToImport.insert(F); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 221 | if (L.linkInModule(std::move(M), Linker::Flags::None, Index.get(), |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame^] | 222 | &FunctionsToImport, TempMDVals.get())) |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // Now link in metadata for all modules from which we imported functions. |
| 227 | for (StringMapEntry<std::unique_ptr<DenseMap<unsigned, MDNode *>>> &SME : |
| 228 | ModuleToTempMDValsMap) { |
| 229 | // Load the specified source module. |
| 230 | std::unique_ptr<Module> M = loadFile(argv0, SME.getKey(), Context, true); |
| 231 | if (!M.get()) { |
| 232 | errs() << argv0 << ": error loading file '" << SME.getKey() << "'\n"; |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | if (verifyModule(*M, &errs())) { |
| 237 | errs() << argv0 << ": " << SME.getKey() |
| 238 | << ": error: input module is broken!\n"; |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // Link in all necessary metadata from this module. |
| 243 | if (L.linkInMetadata(*M, SME.getValue().get())) |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 249 | 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] | 250 | const cl::list<std::string> &Files, |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 251 | unsigned Flags) { |
| 252 | // Filter out flags that don't apply to the first file we load. |
| 253 | unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc; |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 254 | for (const auto &File : Files) { |
| 255 | std::unique_ptr<Module> M = loadFile(argv0, File, Context); |
| 256 | if (!M.get()) { |
| 257 | errs() << argv0 << ": error loading file '" << File << "'\n"; |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if (verifyModule(*M, &errs())) { |
| 262 | errs() << argv0 << ": " << File << ": error: input module is broken!\n"; |
| 263 | return false; |
| 264 | } |
| 265 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 266 | // If a function index is supplied, load it so linkInModule can treat |
| 267 | // local functions/variables as exported and promote if necessary. |
| 268 | std::unique_ptr<FunctionInfoIndex> Index; |
| 269 | if (!FunctionIndex.empty()) { |
Teresa Johnson | 6b92316 | 2015-11-23 19:19:11 +0000 | [diff] [blame] | 270 | ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = |
Mehdi Amini | 9abe108 | 2015-12-03 02:37:23 +0000 | [diff] [blame] | 271 | llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 272 | std::error_code EC = IndexOrErr.getError(); |
| 273 | if (EC) { |
| 274 | errs() << EC.message() << '\n'; |
| 275 | return false; |
| 276 | } |
| 277 | Index = std::move(IndexOrErr.get()); |
| 278 | } |
| 279 | |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 280 | if (Verbose) |
| 281 | errs() << "Linking in '" << File << "'\n"; |
| 282 | |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 283 | if (L.linkInModule(std::move(M), ApplicableFlags, Index.get())) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 284 | return false; |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 285 | // All linker flags apply to linking of subsequent files. |
| 286 | ApplicableFlags = Flags; |
Teresa Johnson | 5bc88be | 2015-11-21 00:35:38 +0000 | [diff] [blame] | 287 | |
| 288 | // If requested for testing, preserve modules by releasing them from |
| 289 | // the unique_ptr before the are freed. This can help catch any |
| 290 | // cross-module references from e.g. unneeded metadata references |
| 291 | // that aren't properly set to null but instead mapped to the source |
| 292 | // module version. The bitcode writer will assert if it finds any such |
| 293 | // cross-module references. |
| 294 | if (PreserveModules) |
| 295 | M.release(); |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return true; |
| 299 | } |
| 300 | |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 301 | int main(int argc, char **argv) { |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 302 | // Print a stack trace if we signal out. |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 303 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 304 | PrettyStackTraceProgram X(argc, argv); |
Andrew Trick | dc073ad | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 305 | |
Owen Anderson | 19251ec | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 306 | LLVMContext &Context = getGlobalContext(); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 307 | Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
| 308 | |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 309 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 310 | cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 311 | |
Rafael Espindola | 957eae2 | 2014-10-23 19:40:45 +0000 | [diff] [blame] | 312 | auto Composite = make_unique<Module>("llvm-link", Context); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 313 | Linker L(*Composite); |
Rafael Espindola | 957eae2 | 2014-10-23 19:40:45 +0000 | [diff] [blame] | 314 | |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 315 | unsigned Flags = Linker::Flags::None; |
| 316 | if (Internalize) |
| 317 | Flags |= Linker::Flags::InternalizeLinkedSymbols; |
| 318 | if (OnlyNeeded) |
| 319 | Flags |= Linker::Flags::LinkOnlyNeeded; |
| 320 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 321 | // First add all the regular input files |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 322 | if (!linkFiles(argv[0], Context, L, InputFilenames, Flags)) |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 323 | return 1; |
| 324 | |
| 325 | // Next the -override ones. |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 326 | if (!linkFiles(argv[0], Context, L, OverridingInputs, |
| 327 | Flags | Linker::Flags::OverrideFromSrc)) |
Duncan P. N. Exon Smith | 0de129d | 2015-04-22 04:08:22 +0000 | [diff] [blame] | 328 | return 1; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 329 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 330 | // Import any functions requested via -import |
| 331 | if (!importFunctions(argv[0], Context, L)) |
| 332 | return 1; |
| 333 | |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 334 | if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite; |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 335 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 336 | std::error_code EC; |
| 337 | tool_output_file Out(OutputFilename, EC, sys::fs::F_None); |
| 338 | if (EC) { |
| 339 | errs() << EC.message() << '\n'; |
Chris Lattner | abd1736 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 340 | return 1; |
| 341 | } |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 342 | |
Duncan P. N. Exon Smith | 4628282 | 2015-03-31 03:07:23 +0000 | [diff] [blame] | 343 | if (verifyModule(*Composite, &errs())) { |
| 344 | errs() << argv[0] << ": error: linked module is broken!\n"; |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 348 | if (Verbose) errs() << "Writing bitcode...\n"; |
Dan Gohman | 2b09de9 | 2009-09-15 15:35:07 +0000 | [diff] [blame] | 349 | if (OutputAssembly) { |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 350 | Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 351 | } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 352 | WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder); |
Dan Gohman | 4cc73ba | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 353 | |
| 354 | // Declare success. |
| 355 | Out.keep(); |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 2793699 | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 357 | return 0; |
Chris Lattner | 9d810e0 | 2001-10-13 07:06:23 +0000 | [diff] [blame] | 358 | } |