blob: 27199d53538ec47d9833eca6e4a116280cc74bc5 [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"
Peter Collingbournec15d60b2017-05-01 20:42:32 +000016#include "llvm/Bitcode/BitcodeReader.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000017#include "llvm/Bitcode/BitcodeWriter.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000018#include "llvm/IR/AutoUpgrade.h"
Rafael Espindolad12b4a32014-10-25 04:06:10 +000019#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000023#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000025#include "llvm/IRReader/IRReader.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000026#include "llvm/Linker/Linker.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"
Teresa Johnson0fca9052017-01-04 14:27:31 +000036#include "llvm/Transforms/IPO/FunctionImport.h"
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +000037#include "llvm/Transforms/IPO/Internalize.h"
Mehdi Amini8d051852016-03-19 00:40:31 +000038#include "llvm/Transforms/Utils/FunctionImportUtils.h"
39
Chris Lattner9d810e02001-10-13 07:06:23 +000040#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000041#include <utility>
Brian Gaeke960707c2003-11-11 22:41:34 +000042using namespace llvm;
43
Chris Lattnerf5cad152002-07-22 02:10:13 +000044static cl::list<std::string>
45InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greife16561c2007-07-05 17:07:56 +000046 cl::desc("<input bitcode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000047
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +000048static cl::list<std::string> OverridingInputs(
49 "override", cl::ZeroOrMore, cl::value_desc("filename"),
50 cl::desc(
51 "input bitcode file which can override previously defined symbol(s)"));
52
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000053// Option to simulate function importing for testing. This enables using
54// llvm-link to simulate ThinLTO backend processes.
55static cl::list<std::string> Imports(
56 "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
57 cl::desc("Pair of function name and filename, where function should be "
58 "imported from bitcode in filename"));
59
Teresa Johnson26ab5772016-03-15 00:04:37 +000060// Option to support testing of function importing. The module summary
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000061// must be specified in the case were we request imports via the -import
62// option, as well as when compiling any module with functions that may be
63// exported (imported by a different llvm-link -import invocation), to ensure
64// consistent promotion and renaming of locals.
Teresa Johnson26ab5772016-03-15 00:04:37 +000065static cl::opt<std::string>
66 SummaryIndex("summary-index", cl::desc("Module summary index filename"),
67 cl::init(""), cl::value_desc("filename"));
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000068
Chris Lattnerf5cad152002-07-22 02:10:13 +000069static cl::opt<std::string>
70OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
71 cl::value_desc("filename"));
72
Dan Gohman61a87962009-08-25 15:34:52 +000073static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000074Internalize("internalize", cl::desc("Internalize linked symbols"));
75
76static cl::opt<bool>
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +000077 DisableDITypeMap("disable-debug-info-type-map",
78 cl::desc("Don't use a uniquing type map for debug info"));
79
80static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000081OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
82
83static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000084Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000085
86static cl::opt<bool>
Mehdi Aminie4709272016-09-14 22:29:59 +000087 DisableLazyLoad("disable-lazy-loading",
Davide Italianoaedafd42016-10-09 17:15:04 +000088 cl::desc("Disable lazy module loading"));
Mehdi Aminie4709272016-09-14 22:29:59 +000089
90static cl::opt<bool>
91 OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
Dan Gohman2b09de92009-09-15 15:35:07 +000092
93static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000094Verbose("v", cl::desc("Print information about actions taken"));
95
96static cl::opt<bool>
97DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
98
Eli Bendersky7da92ed2014-02-20 22:19:24 +000099static cl::opt<bool>
100SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
101 cl::init(false));
102
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000103static cl::opt<bool> PreserveBitcodeUseListOrder(
104 "preserve-bc-uselistorder",
105 cl::desc("Preserve use-list order when writing LLVM bitcode."),
106 cl::init(true), cl::Hidden);
107
108static cl::opt<bool> PreserveAssemblyUseListOrder(
109 "preserve-ll-uselistorder",
110 cl::desc("Preserve use-list order when writing LLVM assembly."),
111 cl::init(false), cl::Hidden);
112
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000113static ExitOnError ExitOnErr;
114
Rafael Espindolad233b062014-08-26 17:29:46 +0000115// Read the specified bitcode file in and return it. This routine searches the
116// link path for the specified file to try to find it...
Reid Spencerb956fc12004-09-12 23:39:42 +0000117//
Teresa Johnsone5a61912015-12-17 17:14:09 +0000118static std::unique_ptr<Module> loadFile(const char *argv0,
119 const std::string &FN,
120 LLVMContext &Context,
121 bool MaterializeMetadata = true) {
Dan Gohman3d2c9142009-09-12 21:55:12 +0000122 SMDiagnostic Err;
Rafael Espindolaf1f12732013-06-17 17:32:19 +0000123 if (Verbose) errs() << "Loading '" << FN << "'\n";
Mehdi Aminie4709272016-09-14 22:29:59 +0000124 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 Johnsonf4cc7522016-03-24 19:52:20 +0000130 if (!Result) {
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000131 Err.print(argv0, errs());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000132 return nullptr;
133 }
Reid Spencerfe020a32004-09-11 04:32:42 +0000134
Teresa Johnsone5a61912015-12-17 17:14:09 +0000135 if (MaterializeMetadata) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000136 ExitOnErr(Result->materializeMetadata());
Teresa Johnsone5a61912015-12-17 17:14:09 +0000137 UpgradeDebugInfo(*Result);
138 }
Rafael Espindola2fcfb5e2015-03-27 15:55:06 +0000139
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000140 return Result;
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000141}
Chris Lattner9d810e02001-10-13 07:06:23 +0000142
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000143namespace {
144
145/// Helper to load on demand a Module from file and cache it for subsequent
146/// queries during function importing.
147class 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
156public:
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 Kramer82de7d32016-05-27 14:27:24 +0000161 : createLazyModule(std::move(createLazyModule)) {}
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000162
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.
176Module &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
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000185static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000186 unsigned Severity = DI.getSeverity();
187 switch (Severity) {
188 case DS_Error:
189 errs() << "ERROR: ";
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000190 break;
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000191 case DS_Warning:
192 if (SuppressWarnings)
193 return;
194 errs() << "WARNING: ";
195 break;
196 case DS_Remark:
197 case DS_Note:
198 llvm_unreachable("Only expecting warnings and errors");
199 }
200
201 DiagnosticPrinterRawOStream DP(errs());
202 DI.print(DP);
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000203 errs() << '\n';
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000204}
205
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000206/// Import any functions requested via the -import option.
Teresa Johnson0fca9052017-01-04 14:27:31 +0000207static bool importFunctions(const char *argv0, Module &DestModule) {
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000208 if (SummaryIndex.empty())
209 return true;
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000210 std::unique_ptr<ModuleSummaryIndex> Index =
211 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000212
213 // Map of Module -> List of globals to import from the Module
Teresa Johnson0fca9052017-01-04 14:27:31 +0000214 FunctionImporter::ImportMapTy ImportList;
215
216 auto ModuleLoader = [&DestModule](const char *argv0,
217 const std::string &Identifier) {
218 return loadFile(argv0, Identifier, DestModule.getContext(), false);
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000219 };
Teresa Johnson0fca9052017-01-04 14:27:31 +0000220
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000221 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000222 for (const auto &Import : Imports) {
223 // Identify the requested function and its bitcode source file.
224 size_t Idx = Import.find(':');
225 if (Idx == std::string::npos) {
226 errs() << "Import parameter bad format: " << Import << "\n";
227 return false;
228 }
229 std::string FunctionName = Import.substr(0, Idx);
230 std::string FileName = Import.substr(Idx + 1, std::string::npos);
231
232 // Load the specified source module.
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000233 auto &SrcModule = ModuleLoaderCache(argv0, FileName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000234
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000235 if (verifyModule(SrcModule, &errs())) {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000236 errs() << argv0 << ": " << FileName
237 << ": error: input module is broken!\n";
238 return false;
239 }
240
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000241 Function *F = SrcModule.getFunction(FunctionName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000242 if (!F) {
243 errs() << "Ignoring import request for non-existent function "
244 << FunctionName << " from " << FileName << "\n";
245 continue;
246 }
247 // We cannot import weak_any functions without possibly affecting the
248 // order they are seen and selected by the linker, changing program
249 // semantics.
250 if (F->hasWeakAnyLinkage()) {
251 errs() << "Ignoring import request for weak-any function " << FunctionName
252 << " from " << FileName << "\n";
253 continue;
254 }
255
256 if (Verbose)
257 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
258
Teresa Johnson0fca9052017-01-04 14:27:31 +0000259 auto &Entry = ImportList[FileName];
260 Entry.insert(std::make_pair(F->getGUID(), /* (Unused) threshold */ 1.0));
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000261 }
Teresa Johnson0fca9052017-01-04 14:27:31 +0000262 auto CachedModuleLoader = [&](StringRef Identifier) {
263 return ModuleLoaderCache.takeModule(Identifier);
264 };
265 FunctionImporter Importer(*Index, CachedModuleLoader);
266 ExitOnErr(Importer.importFunctions(DestModule, ImportList));
Teresa Johnsone5a61912015-12-17 17:14:09 +0000267
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000268 return true;
269}
270
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000271static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000272 const cl::list<std::string> &Files,
Artem Belevich020d4fb2015-09-01 17:55:55 +0000273 unsigned Flags) {
274 // Filter out flags that don't apply to the first file we load.
275 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000276 // Similar to some flags, internalization doesn't apply to the first file.
277 bool InternalizeLinkedSymbols = false;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000278 for (const auto &File : Files) {
279 std::unique_ptr<Module> M = loadFile(argv0, File, Context);
280 if (!M.get()) {
281 errs() << argv0 << ": error loading file '" << File << "'\n";
282 return false;
283 }
284
Rafael Espindola2211f012016-06-29 18:31:48 +0000285 // Note that when ODR merging types cannot verify input files in here When
286 // doing that debug metadata in the src module might already be pointing to
287 // the destination.
288 if (DisableDITypeMap && verifyModule(*M, &errs())) {
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000289 errs() << argv0 << ": " << File << ": error: input module is broken!\n";
290 return false;
291 }
292
Teresa Johnson26ab5772016-03-15 00:04:37 +0000293 // If a module summary index is supplied, load it so linkInModule can treat
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000294 // local functions/variables as exported and promote if necessary.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000295 if (!SummaryIndex.empty()) {
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000296 std::unique_ptr<ModuleSummaryIndex> Index =
297 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Mehdi Amini8d051852016-03-19 00:40:31 +0000298
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000299 // Conservatively mark all internal values as promoted, since this tool
300 // does not do the ThinLink that would normally determine what values to
301 // promote.
302 for (auto &I : *Index) {
303 for (auto &S : I.second) {
304 if (GlobalValue::isLocalLinkage(S->linkage()))
305 S->setLinkage(GlobalValue::ExternalLinkage);
306 }
307 }
308
Mehdi Amini8d051852016-03-19 00:40:31 +0000309 // Promotion
310 if (renameModuleForThinLTO(*M, *Index))
311 return true;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000312 }
313
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000314 if (Verbose)
315 errs() << "Linking in '" << File << "'\n";
316
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000317 bool Err = false;
318 if (InternalizeLinkedSymbols) {
319 Err = L.linkInModule(
320 std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
David Blaikie427f4262017-03-13 21:46:14 +0000321 internalizeModule(M, [&GVS](const GlobalValue &GV) {
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000322 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
323 });
324 });
325 } else {
326 Err = L.linkInModule(std::move(M), ApplicableFlags);
327 }
328
329 if (Err)
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000330 return false;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000331
332 // Internalization applies to linking of subsequent files.
333 InternalizeLinkedSymbols = Internalize;
334
Artem Belevich020d4fb2015-09-01 17:55:55 +0000335 // All linker flags apply to linking of subsequent files.
336 ApplicableFlags = Flags;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000337 }
338
339 return true;
340}
341
Chris Lattner9d810e02001-10-13 07:06:23 +0000342int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000343 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000344 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000345 PrettyStackTraceProgram X(argc, argv);
Andrew Trickdc073ad2013-09-18 23:31:10 +0000346
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000347 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
348
Mehdi Amini03b42e42016-04-14 21:59:01 +0000349 LLVMContext Context;
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000350 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000351
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000352 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
353 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +0000354
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000355 if (!DisableDITypeMap)
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000356 Context.enableDebugTypeODRUniquing();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000357
Rafael Espindola957eae22014-10-23 19:40:45 +0000358 auto Composite = make_unique<Module>("llvm-link", Context);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000359 Linker L(*Composite);
Rafael Espindola957eae22014-10-23 19:40:45 +0000360
Artem Belevich020d4fb2015-09-01 17:55:55 +0000361 unsigned Flags = Linker::Flags::None;
Artem Belevich020d4fb2015-09-01 17:55:55 +0000362 if (OnlyNeeded)
363 Flags |= Linker::Flags::LinkOnlyNeeded;
364
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000365 // First add all the regular input files
Artem Belevich020d4fb2015-09-01 17:55:55 +0000366 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000367 return 1;
368
369 // Next the -override ones.
Artem Belevich020d4fb2015-09-01 17:55:55 +0000370 if (!linkFiles(argv[0], Context, L, OverridingInputs,
371 Flags | Linker::Flags::OverrideFromSrc))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000372 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000373
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000374 // Import any functions requested via -import
Teresa Johnson0fca9052017-01-04 14:27:31 +0000375 if (!importFunctions(argv[0], *Composite))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000376 return 1;
377
Dan Gohman2b09de92009-09-15 15:35:07 +0000378 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000379
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000380 std::error_code EC;
381 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
382 if (EC) {
383 errs() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000384 return 1;
385 }
Reid Spencer996ec722004-12-30 05:36:08 +0000386
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000387 if (verifyModule(*Composite, &errs())) {
388 errs() << argv[0] << ": error: linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000389 return 1;
390 }
391
Dan Gohmanee051522009-07-16 15:30:09 +0000392 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000393 if (OutputAssembly) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000394 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000395 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000396 WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000397
398 // Declare success.
399 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000400
Chris Lattner27936992007-05-06 05:13:17 +0000401 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000402}