blob: 369f3477fe5c99434cd1ce74d1c3b40ba6cf7292 [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
Chandler Carruth6cc07df2014-03-06 03:42:23 +000015#include "llvm/Linker/Linker.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner27936992007-05-06 05:13:17 +000017#include "llvm/Bitcode/ReaderWriter.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"
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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000026#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/PrettyStackTrace.h"
30#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000031#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000032#include "llvm/Support/SystemUtils.h"
33#include "llvm/Support/ToolOutputFile.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000034#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000035using namespace llvm;
36
Chris Lattnerf5cad152002-07-22 02:10:13 +000037static cl::list<std::string>
38InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greife16561c2007-07-05 17:07:56 +000039 cl::desc("<input bitcode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000040
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +000041static cl::list<std::string> OverridingInputs(
42 "override", cl::ZeroOrMore, cl::value_desc("filename"),
43 cl::desc(
44 "input bitcode file which can override previously defined symbol(s)"));
45
Chris Lattnerf5cad152002-07-22 02:10:13 +000046static cl::opt<std::string>
47OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
48 cl::value_desc("filename"));
49
Dan Gohman61a87962009-08-25 15:34:52 +000050static cl::opt<bool>
51Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000052
53static cl::opt<bool>
Dan Gohman2b09de92009-09-15 15:35:07 +000054OutputAssembly("S",
55 cl::desc("Write output as LLVM assembly"), cl::Hidden);
56
57static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000058Verbose("v", cl::desc("Print information about actions taken"));
59
60static cl::opt<bool>
61DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
62
Eli Bendersky7da92ed2014-02-20 22:19:24 +000063static cl::opt<bool>
64SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
65 cl::init(false));
66
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000067static cl::opt<bool> PreserveBitcodeUseListOrder(
68 "preserve-bc-uselistorder",
69 cl::desc("Preserve use-list order when writing LLVM bitcode."),
70 cl::init(true), cl::Hidden);
71
72static cl::opt<bool> PreserveAssemblyUseListOrder(
73 "preserve-ll-uselistorder",
74 cl::desc("Preserve use-list order when writing LLVM assembly."),
75 cl::init(false), cl::Hidden);
76
Rafael Espindolad233b062014-08-26 17:29:46 +000077// Read the specified bitcode file in and return it. This routine searches the
78// link path for the specified file to try to find it...
Reid Spencerb956fc12004-09-12 23:39:42 +000079//
Rafael Espindolad233b062014-08-26 17:29:46 +000080static std::unique_ptr<Module>
81loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
Dan Gohman3d2c9142009-09-12 21:55:12 +000082 SMDiagnostic Err;
Rafael Espindolaf1f12732013-06-17 17:32:19 +000083 if (Verbose) errs() << "Loading '" << FN << "'\n";
Rafael Espindola957eae22014-10-23 19:40:45 +000084 std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
Rafael Espindola5c4f4a62014-08-26 18:03:35 +000085 if (!Result)
86 Err.print(argv0, errs());
Reid Spencerfe020a32004-09-11 04:32:42 +000087
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000088 Result->materializeMetadata();
89 UpgradeDebugInfo(*Result);
Rafael Espindola2fcfb5e2015-03-27 15:55:06 +000090
Rafael Espindola5c4f4a62014-08-26 18:03:35 +000091 return Result;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000092}
Chris Lattner9d810e02001-10-13 07:06:23 +000093
Rafael Espindola4160f5d2014-10-27 23:02:10 +000094static void diagnosticHandler(const DiagnosticInfo &DI) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +000095 unsigned Severity = DI.getSeverity();
96 switch (Severity) {
97 case DS_Error:
98 errs() << "ERROR: ";
Rafael Espindola4160f5d2014-10-27 23:02:10 +000099 break;
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000100 case DS_Warning:
101 if (SuppressWarnings)
102 return;
103 errs() << "WARNING: ";
104 break;
105 case DS_Remark:
106 case DS_Note:
107 llvm_unreachable("Only expecting warnings and errors");
108 }
109
110 DiagnosticPrinterRawOStream DP(errs());
111 DI.print(DP);
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000112 errs() << '\n';
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000113}
114
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000115static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000116 const cl::list<std::string> &Files,
117 bool OverrideDuplicateSymbols) {
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000118 for (const auto &File : Files) {
119 std::unique_ptr<Module> M = loadFile(argv0, File, Context);
120 if (!M.get()) {
121 errs() << argv0 << ": error loading file '" << File << "'\n";
122 return false;
123 }
124
125 if (verifyModule(*M, &errs())) {
126 errs() << argv0 << ": " << File << ": error: input module is broken!\n";
127 return false;
128 }
129
130 if (Verbose)
131 errs() << "Linking in '" << File << "'\n";
132
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000133 if (L.linkInModule(M.get(), OverrideDuplicateSymbols))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000134 return false;
135 }
136
137 return true;
138}
139
Chris Lattner9d810e02001-10-13 07:06:23 +0000140int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000141 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +0000142 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000143 PrettyStackTraceProgram X(argc, argv);
Andrew Trickdc073ad2013-09-18 23:31:10 +0000144
Owen Anderson19251ec2009-07-15 22:16:10 +0000145 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000146 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
147 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +0000148
Rafael Espindola957eae22014-10-23 19:40:45 +0000149 auto Composite = make_unique<Module>("llvm-link", Context);
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000150 Linker L(Composite.get(), diagnosticHandler);
Rafael Espindola957eae22014-10-23 19:40:45 +0000151
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000152 // First add all the regular input files
153 if (!linkFiles(argv[0], Context, L, InputFilenames, false))
154 return 1;
155
156 // Next the -override ones.
157 if (!linkFiles(argv[0], Context, L, OverridingInputs, true))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000158 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000159
Dan Gohman2b09de92009-09-15 15:35:07 +0000160 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000161
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000162 std::error_code EC;
163 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
164 if (EC) {
165 errs() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000166 return 1;
167 }
Reid Spencer996ec722004-12-30 05:36:08 +0000168
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000169 if (verifyModule(*Composite, &errs())) {
170 errs() << argv[0] << ": error: linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000171 return 1;
172 }
173
Dan Gohmanee051522009-07-16 15:30:09 +0000174 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000175 if (OutputAssembly) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000176 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000177 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000178 WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000179
180 // Declare success.
181 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000182
Chris Lattner27936992007-05-06 05:13:17 +0000183 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000184}