blob: f90e3916ff73e655f6d3284313f0131f9cf95e5b [file] [log] [blame]
Chris Lattner825937d2003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner9d810e02001-10-13 07:06:23 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmancf0c7442003-09-15 18:34:34 +000011// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner9d810e02001-10-13 07:06:23 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000016#include "llvm/Bitcode/BitcodeWriter.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000017#include "llvm/IR/AutoUpgrade.h"
Rafael Espindolad12b4a32014-10-25 04:06:10 +000018#include "llvm/IR/DiagnosticInfo.h"
19#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000022#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000023#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000024#include "llvm/IRReader/IRReader.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000025#include "llvm/Linker/Linker.h"
26#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000028#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000030#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/PrettyStackTrace.h"
32#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000033#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Support/SystemUtils.h"
35#include "llvm/Support/ToolOutputFile.h"
Mehdi Amini8d051852016-03-19 00:40:31 +000036#include "llvm/Transforms/Utils/FunctionImportUtils.h"
37
Chris Lattner9d810e02001-10-13 07:06:23 +000038#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000039#include <utility>
Brian Gaeke960707c2003-11-11 22:41:34 +000040using namespace llvm;
41
Chris Lattnerf5cad152002-07-22 02:10:13 +000042static cl::list<std::string>
43InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greife16561c2007-07-05 17:07:56 +000044 cl::desc("<input bitcode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000045
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +000046static cl::list<std::string> OverridingInputs(
47 "override", cl::ZeroOrMore, cl::value_desc("filename"),
48 cl::desc(
49 "input bitcode file which can override previously defined symbol(s)"));
50
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000051// Option to simulate function importing for testing. This enables using
52// llvm-link to simulate ThinLTO backend processes.
53static cl::list<std::string> Imports(
54 "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
55 cl::desc("Pair of function name and filename, where function should be "
56 "imported from bitcode in filename"));
57
Teresa Johnson26ab5772016-03-15 00:04:37 +000058// Option to support testing of function importing. The module summary
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000059// must be specified in the case were we request imports via the -import
60// option, as well as when compiling any module with functions that may be
61// exported (imported by a different llvm-link -import invocation), to ensure
62// consistent promotion and renaming of locals.
Teresa Johnson26ab5772016-03-15 00:04:37 +000063static cl::opt<std::string>
64 SummaryIndex("summary-index", cl::desc("Module summary index filename"),
65 cl::init(""), cl::value_desc("filename"));
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000066
Chris Lattnerf5cad152002-07-22 02:10:13 +000067static cl::opt<std::string>
68OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
69 cl::value_desc("filename"));
70
Dan Gohman61a87962009-08-25 15:34:52 +000071static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000072Internalize("internalize", cl::desc("Internalize linked symbols"));
73
74static cl::opt<bool>
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +000075 DisableDITypeMap("disable-debug-info-type-map",
76 cl::desc("Don't use a uniquing type map for debug info"));
77
78static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000079OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
80
81static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000082Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000083
84static cl::opt<bool>
Mehdi Aminie4709272016-09-14 22:29:59 +000085 DisableLazyLoad("disable-lazy-loading",
Davide Italianoaedafd42016-10-09 17:15:04 +000086 cl::desc("Disable lazy module loading"));
Mehdi Aminie4709272016-09-14 22:29:59 +000087
88static cl::opt<bool>
89 OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
Dan Gohman2b09de92009-09-15 15:35:07 +000090
91static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000092Verbose("v", cl::desc("Print information about actions taken"));
93
94static cl::opt<bool>
95DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
96
Eli Bendersky7da92ed2014-02-20 22:19:24 +000097static cl::opt<bool>
98SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
99 cl::init(false));
100
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000101static cl::opt<bool> PreserveBitcodeUseListOrder(
102 "preserve-bc-uselistorder",
103 cl::desc("Preserve use-list order when writing LLVM bitcode."),
104 cl::init(true), cl::Hidden);
105
106static cl::opt<bool> PreserveAssemblyUseListOrder(
107 "preserve-ll-uselistorder",
108 cl::desc("Preserve use-list order when writing LLVM assembly."),
109 cl::init(false), cl::Hidden);
110
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000111static ExitOnError ExitOnErr;
112
Rafael Espindolad233b062014-08-26 17:29:46 +0000113// Read the specified bitcode file in and return it. This routine searches the
114// link path for the specified file to try to find it...
Reid Spencerb956fc12004-09-12 23:39:42 +0000115//
Teresa Johnsone5a61912015-12-17 17:14:09 +0000116static std::unique_ptr<Module> loadFile(const char *argv0,
117 const std::string &FN,
118 LLVMContext &Context,
119 bool MaterializeMetadata = true) {
Dan Gohman3d2c9142009-09-12 21:55:12 +0000120 SMDiagnostic Err;
Rafael Espindolaf1f12732013-06-17 17:32:19 +0000121 if (Verbose) errs() << "Loading '" << FN << "'\n";
Mehdi Aminie4709272016-09-14 22:29:59 +0000122 std::unique_ptr<Module> Result;
123 if (DisableLazyLoad)
124 Result = parseIRFile(FN, Err, Context);
125 else
126 Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
127
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000128 if (!Result) {
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000129 Err.print(argv0, errs());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000130 return nullptr;
131 }
Reid Spencerfe020a32004-09-11 04:32:42 +0000132
Teresa Johnsone5a61912015-12-17 17:14:09 +0000133 if (MaterializeMetadata) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000134 ExitOnErr(Result->materializeMetadata());
Teresa Johnsone5a61912015-12-17 17:14:09 +0000135 UpgradeDebugInfo(*Result);
136 }
Rafael Espindola2fcfb5e2015-03-27 15:55:06 +0000137
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000138 return Result;
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000139}
Chris Lattner9d810e02001-10-13 07:06:23 +0000140
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000141namespace {
142
143/// Helper to load on demand a Module from file and cache it for subsequent
144/// queries during function importing.
145class ModuleLazyLoaderCache {
146 /// Cache of lazily loaded module for import.
147 StringMap<std::unique_ptr<Module>> ModuleMap;
148
149 /// Retrieve a Module from the cache or lazily load it on demand.
150 std::function<std::unique_ptr<Module>(const char *argv0,
151 const std::string &FileName)>
152 createLazyModule;
153
154public:
155 /// Create the loader, Module will be initialized in \p Context.
156 ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
157 const char *argv0, const std::string &FileName)>
158 createLazyModule)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000159 : createLazyModule(std::move(createLazyModule)) {}
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000160
161 /// Retrieve a Module from the cache or lazily load it on demand.
162 Module &operator()(const char *argv0, const std::string &FileName);
163
164 std::unique_ptr<Module> takeModule(const std::string &FileName) {
165 auto I = ModuleMap.find(FileName);
166 assert(I != ModuleMap.end());
167 std::unique_ptr<Module> Ret = std::move(I->second);
168 ModuleMap.erase(I);
169 return Ret;
170 }
171};
172
173// Get a Module for \p FileName from the cache, or load it lazily.
174Module &ModuleLazyLoaderCache::operator()(const char *argv0,
175 const std::string &Identifier) {
176 auto &Module = ModuleMap[Identifier];
177 if (!Module)
178 Module = createLazyModule(argv0, Identifier);
179 return *Module;
180}
181} // anonymous namespace
182
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000183static void diagnosticHandler(const DiagnosticInfo &DI) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000184 unsigned Severity = DI.getSeverity();
185 switch (Severity) {
186 case DS_Error:
187 errs() << "ERROR: ";
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000188 break;
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000189 case DS_Warning:
190 if (SuppressWarnings)
191 return;
192 errs() << "WARNING: ";
193 break;
194 case DS_Remark:
195 case DS_Note:
196 llvm_unreachable("Only expecting warnings and errors");
197 }
198
199 DiagnosticPrinterRawOStream DP(errs());
200 DI.print(DP);
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000201 errs() << '\n';
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000202}
203
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000204static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) {
205 diagnosticHandler(DI);
206}
207
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000208/// Import any functions requested via the -import option.
209static bool importFunctions(const char *argv0, LLVMContext &Context,
210 Linker &L) {
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000211 if (SummaryIndex.empty())
212 return true;
213 ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
214 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
215 std::error_code EC = IndexOrErr.getError();
216 if (EC) {
217 errs() << EC.message() << '\n';
218 return false;
219 }
220 auto Index = std::move(IndexOrErr.get());
221
222 // Map of Module -> List of globals to import from the Module
223 std::map<StringRef, DenseSet<const GlobalValue *>> ModuleToGlobalsToImportMap;
224 auto ModuleLoader = [&Context](const char *argv0,
225 const std::string &Identifier) {
226 return loadFile(argv0, Identifier, Context, false);
227 };
228 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000229 for (const auto &Import : Imports) {
230 // Identify the requested function and its bitcode source file.
231 size_t Idx = Import.find(':');
232 if (Idx == std::string::npos) {
233 errs() << "Import parameter bad format: " << Import << "\n";
234 return false;
235 }
236 std::string FunctionName = Import.substr(0, Idx);
237 std::string FileName = Import.substr(Idx + 1, std::string::npos);
238
239 // Load the specified source module.
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000240 auto &SrcModule = ModuleLoaderCache(argv0, FileName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000241
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000242 if (verifyModule(SrcModule, &errs())) {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000243 errs() << argv0 << ": " << FileName
244 << ": error: input module is broken!\n";
245 return false;
246 }
247
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000248 Function *F = SrcModule.getFunction(FunctionName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000249 if (!F) {
250 errs() << "Ignoring import request for non-existent function "
251 << FunctionName << " from " << FileName << "\n";
252 continue;
253 }
254 // We cannot import weak_any functions without possibly affecting the
255 // order they are seen and selected by the linker, changing program
256 // semantics.
257 if (F->hasWeakAnyLinkage()) {
258 errs() << "Ignoring import request for weak-any function " << FunctionName
259 << " from " << FileName << "\n";
260 continue;
261 }
262
263 if (Verbose)
264 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
265
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000266 auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()];
267 Entry.insert(F);
Mehdi Amini8d051852016-03-19 00:40:31 +0000268
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000269 ExitOnErr(F->materialize());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000270 }
Mehdi Amini8d051852016-03-19 00:40:31 +0000271
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000272 // Do the actual import of globals now, one Module at a time
273 for (auto &GlobalsToImportPerModule : ModuleToGlobalsToImportMap) {
274 // Get the module for the import
275 auto &GlobalsToImport = GlobalsToImportPerModule.second;
276 std::unique_ptr<Module> SrcModule =
277 ModuleLoaderCache.takeModule(GlobalsToImportPerModule.first);
278 assert(&Context == &SrcModule->getContext() && "Context mismatch");
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000279
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000280 // If modules were created with lazy metadata loading, materialize it
281 // now, before linking it (otherwise this will be a noop).
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000282 ExitOnErr(SrcModule->materializeMetadata());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000283 UpgradeDebugInfo(*SrcModule);
Teresa Johnsone5a61912015-12-17 17:14:09 +0000284
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000285 // Linkage Promotion and renaming
286 if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport))
287 return true;
288
Mehdi Aminibda3c972016-04-21 01:59:39 +0000289 // Instruct the linker to not automatically import linkonce defintion.
290 unsigned Flags = Linker::Flags::DontForceLinkLinkonceODR;
291
292 if (L.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
Teresa Johnsone5a61912015-12-17 17:14:09 +0000293 return false;
294 }
295
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000296 return true;
297}
298
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000299static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000300 const cl::list<std::string> &Files,
Artem Belevich020d4fb2015-09-01 17:55:55 +0000301 unsigned Flags) {
302 // Filter out flags that don't apply to the first file we load.
303 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000304 for (const auto &File : Files) {
305 std::unique_ptr<Module> M = loadFile(argv0, File, Context);
306 if (!M.get()) {
307 errs() << argv0 << ": error loading file '" << File << "'\n";
308 return false;
309 }
310
Rafael Espindola2211f012016-06-29 18:31:48 +0000311 // Note that when ODR merging types cannot verify input files in here When
312 // doing that debug metadata in the src module might already be pointing to
313 // the destination.
314 if (DisableDITypeMap && verifyModule(*M, &errs())) {
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000315 errs() << argv0 << ": " << File << ": error: input module is broken!\n";
316 return false;
317 }
318
Teresa Johnson26ab5772016-03-15 00:04:37 +0000319 // If a module summary index is supplied, load it so linkInModule can treat
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000320 // local functions/variables as exported and promote if necessary.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000321 if (!SummaryIndex.empty()) {
322 ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
323 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000324 std::error_code EC = IndexOrErr.getError();
325 if (EC) {
326 errs() << EC.message() << '\n';
327 return false;
328 }
Mehdi Amini8d051852016-03-19 00:40:31 +0000329 auto Index = std::move(IndexOrErr.get());
330
331 // Promotion
332 if (renameModuleForThinLTO(*M, *Index))
333 return true;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000334 }
335
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000336 if (Verbose)
337 errs() << "Linking in '" << File << "'\n";
338
Mehdi Amini8d051852016-03-19 00:40:31 +0000339 if (L.linkInModule(std::move(M), ApplicableFlags))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000340 return false;
Artem Belevich020d4fb2015-09-01 17:55:55 +0000341 // All linker flags apply to linking of subsequent files.
342 ApplicableFlags = Flags;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000343 }
344
345 return true;
346}
347
Chris Lattner9d810e02001-10-13 07:06:23 +0000348int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000349 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000350 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000351 PrettyStackTraceProgram X(argc, argv);
Andrew Trickdc073ad2013-09-18 23:31:10 +0000352
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000353 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
354
Mehdi Amini03b42e42016-04-14 21:59:01 +0000355 LLVMContext Context;
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000356 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
357
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000358 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
359 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +0000360
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000361 if (!DisableDITypeMap)
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000362 Context.enableDebugTypeODRUniquing();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000363
Rafael Espindola957eae22014-10-23 19:40:45 +0000364 auto Composite = make_unique<Module>("llvm-link", Context);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000365 Linker L(*Composite);
Rafael Espindola957eae22014-10-23 19:40:45 +0000366
Artem Belevich020d4fb2015-09-01 17:55:55 +0000367 unsigned Flags = Linker::Flags::None;
368 if (Internalize)
369 Flags |= Linker::Flags::InternalizeLinkedSymbols;
370 if (OnlyNeeded)
371 Flags |= Linker::Flags::LinkOnlyNeeded;
372
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000373 // First add all the regular input files
Artem Belevich020d4fb2015-09-01 17:55:55 +0000374 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000375 return 1;
376
377 // Next the -override ones.
Artem Belevich020d4fb2015-09-01 17:55:55 +0000378 if (!linkFiles(argv[0], Context, L, OverridingInputs,
379 Flags | Linker::Flags::OverrideFromSrc))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000380 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000381
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000382 // Import any functions requested via -import
383 if (!importFunctions(argv[0], Context, L))
384 return 1;
385
Dan Gohman2b09de92009-09-15 15:35:07 +0000386 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000387
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000388 std::error_code EC;
389 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
390 if (EC) {
391 errs() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000392 return 1;
393 }
Reid Spencer996ec722004-12-30 05:36:08 +0000394
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000395 if (verifyModule(*Composite, &errs())) {
396 errs() << argv[0] << ": error: linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000397 return 1;
398 }
399
Dan Gohmanee051522009-07-16 15:30:09 +0000400 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000401 if (OutputAssembly) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000402 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000403 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000404 WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000405
406 // Declare success.
407 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000408
Chris Lattner27936992007-05-06 05:13:17 +0000409 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000410}