blob: fa36e083b6f8c4246b74d81fc08b7a0610bbb6c4 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner9d810e02001-10-13 07:06:23 +00008//
9// This utility may be invoked in the following manner:
Misha Brukmancf0c7442003-09-15 18:34:34 +000010// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner9d810e02001-10-13 07:06:23 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000014#include "llvm/ADT/STLExtras.h"
Peter Collingbournec15d60b2017-05-01 20:42:32 +000015#include "llvm/Bitcode/BitcodeReader.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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000027#include "llvm/Support/FileSystem.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000028#include "llvm/Support/InitLLVM.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000029#include "llvm/Support/Path.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000030#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/SystemUtils.h"
32#include "llvm/Support/ToolOutputFile.h"
Jonas Devliegherefef7ada2018-04-18 14:41:47 +000033#include "llvm/Support/WithColor.h"
Teresa Johnson0fca9052017-01-04 14:27:31 +000034#include "llvm/Transforms/IPO/FunctionImport.h"
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +000035#include "llvm/Transforms/IPO/Internalize.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;
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000121 if (Verbose)
122 errs() << "Loading '" << FN << "'\n";
Mehdi Aminie4709272016-09-14 22:29:59 +0000123 std::unique_ptr<Module> Result;
124 if (DisableLazyLoad)
125 Result = parseIRFile(FN, Err, Context);
126 else
127 Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
128
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000129 if (!Result) {
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000130 Err.print(argv0, errs());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000131 return nullptr;
132 }
Reid Spencerfe020a32004-09-11 04:32:42 +0000133
Teresa Johnsone5a61912015-12-17 17:14:09 +0000134 if (MaterializeMetadata) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000135 ExitOnErr(Result->materializeMetadata());
Teresa Johnsone5a61912015-12-17 17:14:09 +0000136 UpgradeDebugInfo(*Result);
137 }
Rafael Espindola2fcfb5e2015-03-27 15:55:06 +0000138
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000139 return Result;
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000140}
Chris Lattner9d810e02001-10-13 07:06:23 +0000141
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000142namespace {
143
144/// Helper to load on demand a Module from file and cache it for subsequent
145/// queries during function importing.
146class ModuleLazyLoaderCache {
147 /// Cache of lazily loaded module for import.
148 StringMap<std::unique_ptr<Module>> ModuleMap;
149
150 /// Retrieve a Module from the cache or lazily load it on demand.
151 std::function<std::unique_ptr<Module>(const char *argv0,
152 const std::string &FileName)>
153 createLazyModule;
154
155public:
156 /// Create the loader, Module will be initialized in \p Context.
157 ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
158 const char *argv0, const std::string &FileName)>
159 createLazyModule)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000160 : createLazyModule(std::move(createLazyModule)) {}
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000161
162 /// Retrieve a Module from the cache or lazily load it on demand.
163 Module &operator()(const char *argv0, const std::string &FileName);
164
165 std::unique_ptr<Module> takeModule(const std::string &FileName) {
166 auto I = ModuleMap.find(FileName);
167 assert(I != ModuleMap.end());
168 std::unique_ptr<Module> Ret = std::move(I->second);
169 ModuleMap.erase(I);
170 return Ret;
171 }
172};
173
174// Get a Module for \p FileName from the cache, or load it lazily.
175Module &ModuleLazyLoaderCache::operator()(const char *argv0,
176 const std::string &Identifier) {
177 auto &Module = ModuleMap[Identifier];
178 if (!Module)
179 Module = createLazyModule(argv0, Identifier);
180 return *Module;
181}
182} // anonymous namespace
183
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000184namespace {
185struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
186 bool handleDiagnostics(const DiagnosticInfo &DI) override {
187 unsigned Severity = DI.getSeverity();
188 switch (Severity) {
189 case DS_Error:
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000190 WithColor::error();
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000191 break;
192 case DS_Warning:
193 if (SuppressWarnings)
194 return true;
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000195 WithColor::warning();
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000196 break;
197 case DS_Remark:
198 case DS_Note:
199 llvm_unreachable("Only expecting warnings and errors");
200 }
Vivek Pandyadf8598d2017-09-15 19:53:54 +0000201
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000202 DiagnosticPrinterRawOStream DP(errs());
203 DI.print(DP);
204 errs() << '\n';
205 return true;
206 }
207};
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000208}
209
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000210/// Import any functions requested via the -import option.
Teresa Johnson0fca9052017-01-04 14:27:31 +0000211static bool importFunctions(const char *argv0, Module &DestModule) {
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000212 if (SummaryIndex.empty())
213 return true;
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000214 std::unique_ptr<ModuleSummaryIndex> Index =
215 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000216
217 // Map of Module -> List of globals to import from the Module
Teresa Johnson0fca9052017-01-04 14:27:31 +0000218 FunctionImporter::ImportMapTy ImportList;
219
220 auto ModuleLoader = [&DestModule](const char *argv0,
221 const std::string &Identifier) {
222 return loadFile(argv0, Identifier, DestModule.getContext(), false);
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000223 };
Teresa Johnson0fca9052017-01-04 14:27:31 +0000224
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000225 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000226 for (const auto &Import : Imports) {
227 // Identify the requested function and its bitcode source file.
228 size_t Idx = Import.find(':');
229 if (Idx == std::string::npos) {
230 errs() << "Import parameter bad format: " << Import << "\n";
231 return false;
232 }
233 std::string FunctionName = Import.substr(0, Idx);
234 std::string FileName = Import.substr(Idx + 1, std::string::npos);
235
236 // Load the specified source module.
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000237 auto &SrcModule = ModuleLoaderCache(argv0, FileName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000238
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000239 if (verifyModule(SrcModule, &errs())) {
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000240 errs() << argv0 << ": " << FileName;
241 WithColor::error() << "input module is broken!\n";
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000242 return false;
243 }
244
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000245 Function *F = SrcModule.getFunction(FunctionName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000246 if (!F) {
247 errs() << "Ignoring import request for non-existent function "
248 << FunctionName << " from " << FileName << "\n";
249 continue;
250 }
251 // We cannot import weak_any functions without possibly affecting the
252 // order they are seen and selected by the linker, changing program
253 // semantics.
254 if (F->hasWeakAnyLinkage()) {
255 errs() << "Ignoring import request for weak-any function " << FunctionName
256 << " from " << FileName << "\n";
257 continue;
258 }
259
260 if (Verbose)
261 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
262
Teresa Johnson0fca9052017-01-04 14:27:31 +0000263 auto &Entry = ImportList[FileName];
Teresa Johnsond68935c2018-07-16 15:30:27 +0000264 Entry.insert(F->getGUID());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000265 }
Teresa Johnson0fca9052017-01-04 14:27:31 +0000266 auto CachedModuleLoader = [&](StringRef Identifier) {
267 return ModuleLoaderCache.takeModule(Identifier);
268 };
269 FunctionImporter Importer(*Index, CachedModuleLoader);
270 ExitOnErr(Importer.importFunctions(DestModule, ImportList));
Teresa Johnsone5a61912015-12-17 17:14:09 +0000271
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000272 return true;
273}
274
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000275static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000276 const cl::list<std::string> &Files,
Artem Belevich020d4fb2015-09-01 17:55:55 +0000277 unsigned Flags) {
278 // Filter out flags that don't apply to the first file we load.
279 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000280 // Similar to some flags, internalization doesn't apply to the first file.
281 bool InternalizeLinkedSymbols = false;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000282 for (const auto &File : Files) {
283 std::unique_ptr<Module> M = loadFile(argv0, File, Context);
284 if (!M.get()) {
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000285 errs() << argv0 << ": ";
286 WithColor::error() << " loading file '" << File << "'\n";
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000287 return false;
288 }
289
Rafael Espindola2211f012016-06-29 18:31:48 +0000290 // Note that when ODR merging types cannot verify input files in here When
291 // doing that debug metadata in the src module might already be pointing to
292 // the destination.
293 if (DisableDITypeMap && verifyModule(*M, &errs())) {
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000294 errs() << argv0 << ": " << File << ": ";
295 WithColor::error() << "input module is broken!\n";
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000296 return false;
297 }
298
Teresa Johnson26ab5772016-03-15 00:04:37 +0000299 // If a module summary index is supplied, load it so linkInModule can treat
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000300 // local functions/variables as exported and promote if necessary.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000301 if (!SummaryIndex.empty()) {
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000302 std::unique_ptr<ModuleSummaryIndex> Index =
303 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Mehdi Amini8d051852016-03-19 00:40:31 +0000304
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000305 // Conservatively mark all internal values as promoted, since this tool
306 // does not do the ThinLink that would normally determine what values to
307 // promote.
308 for (auto &I : *Index) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000309 for (auto &S : I.second.SummaryList) {
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000310 if (GlobalValue::isLocalLinkage(S->linkage()))
311 S->setLinkage(GlobalValue::ExternalLinkage);
312 }
313 }
314
Mehdi Amini8d051852016-03-19 00:40:31 +0000315 // Promotion
316 if (renameModuleForThinLTO(*M, *Index))
317 return true;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000318 }
319
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000320 if (Verbose)
321 errs() << "Linking in '" << File << "'\n";
322
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000323 bool Err = false;
324 if (InternalizeLinkedSymbols) {
325 Err = L.linkInModule(
326 std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
David Blaikie427f4262017-03-13 21:46:14 +0000327 internalizeModule(M, [&GVS](const GlobalValue &GV) {
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000328 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
329 });
330 });
331 } else {
332 Err = L.linkInModule(std::move(M), ApplicableFlags);
333 }
334
335 if (Err)
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000336 return false;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000337
338 // Internalization applies to linking of subsequent files.
339 InternalizeLinkedSymbols = Internalize;
340
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) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000349 InitLLVM X(argc, argv);
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000350 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
351
Mehdi Amini03b42e42016-04-14 21:59:01 +0000352 LLVMContext Context;
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000353 Context.setDiagnosticHandler(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000354 std::make_unique<LLVMLinkDiagnosticHandler>(), true);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000355 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +0000356
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000357 if (!DisableDITypeMap)
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000358 Context.enableDebugTypeODRUniquing();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000359
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000360 auto Composite = std::make_unique<Module>("llvm-link", Context);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000361 Linker L(*Composite);
Rafael Espindola957eae22014-10-23 19:40:45 +0000362
Artem Belevich020d4fb2015-09-01 17:55:55 +0000363 unsigned Flags = Linker::Flags::None;
Artem Belevich020d4fb2015-09-01 17:55:55 +0000364 if (OnlyNeeded)
365 Flags |= Linker::Flags::LinkOnlyNeeded;
366
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000367 // First add all the regular input files
Artem Belevich020d4fb2015-09-01 17:55:55 +0000368 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000369 return 1;
370
371 // Next the -override ones.
Artem Belevich020d4fb2015-09-01 17:55:55 +0000372 if (!linkFiles(argv[0], Context, L, OverridingInputs,
373 Flags | Linker::Flags::OverrideFromSrc))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000374 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000375
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000376 // Import any functions requested via -import
Teresa Johnson0fca9052017-01-04 14:27:31 +0000377 if (!importFunctions(argv[0], *Composite))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000378 return 1;
379
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000380 if (DumpAsm)
381 errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000382
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000383 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +0000384 ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000385 if (EC) {
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000386 WithColor::error() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000387 return 1;
388 }
Reid Spencer996ec722004-12-30 05:36:08 +0000389
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000390 if (verifyModule(*Composite, &errs())) {
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000391 errs() << argv[0] << ": ";
392 WithColor::error() << "linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000393 return 1;
394 }
395
Jonas Devliegherefef7ada2018-04-18 14:41:47 +0000396 if (Verbose)
397 errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000398 if (OutputAssembly) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000399 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000400 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
Rafael Espindola6a86e252018-02-14 19:11:32 +0000401 WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000402
403 // Declare success.
404 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000405
Chris Lattner27936992007-05-06 05:13:17 +0000406 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000407}