blob: 14491482870967381dea907e99902bb581db283a [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"
Chris Lattner27936992007-05-06 05:13:17 +000016#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000019#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000020#include "llvm/IRReader/IRReader.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000022#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000023#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/Support/PrettyStackTrace.h"
25#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000026#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Support/SystemUtils.h"
28#include "llvm/Support/ToolOutputFile.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000029#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattnerf5cad152002-07-22 02:10:13 +000032static cl::list<std::string>
33InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greife16561c2007-07-05 17:07:56 +000034 cl::desc("<input bitcode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000035
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
38 cl::value_desc("filename"));
39
Dan Gohman61a87962009-08-25 15:34:52 +000040static cl::opt<bool>
41Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000042
43static cl::opt<bool>
Dan Gohman2b09de92009-09-15 15:35:07 +000044OutputAssembly("S",
45 cl::desc("Write output as LLVM assembly"), cl::Hidden);
46
47static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000048Verbose("v", cl::desc("Print information about actions taken"));
49
50static cl::opt<bool>
51DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
52
Eli Bendersky7da92ed2014-02-20 22:19:24 +000053static cl::opt<bool>
54SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
55 cl::init(false));
56
Gabor Greife16561c2007-07-05 17:07:56 +000057// LoadFile - Read the specified bitcode file in and return it. This routine
Reid Spencerb956fc12004-09-12 23:39:42 +000058// searches the link path for the specified file to try to find it...
59//
Andy Gibbsb23ea722013-04-15 12:06:32 +000060static inline Module *LoadFile(const char *argv0, const std::string &FN,
61 LLVMContext& Context) {
Dan Gohman3d2c9142009-09-12 21:55:12 +000062 SMDiagnostic Err;
Rafael Espindolaf1f12732013-06-17 17:32:19 +000063 if (Verbose) errs() << "Loading '" << FN << "'\n";
Dan Gohman6debf892010-05-27 20:51:54 +000064 Module* Result = 0;
Rafael Espindolaf1f12732013-06-17 17:32:19 +000065
66 Result = ParseIRFile(FN, Err, Context);
Andy Gibbsb23ea722013-04-15 12:06:32 +000067 if (Result) return Result; // Load successful!
Reid Spencerfe020a32004-09-11 04:32:42 +000068
Chris Lattnera3a06812011-10-16 04:47:35 +000069 Err.print(argv0, errs());
Andy Gibbsb23ea722013-04-15 12:06:32 +000070 return NULL;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000071}
Chris Lattner9d810e02001-10-13 07:06:23 +000072
73int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000074 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +000075 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000076 PrettyStackTraceProgram X(argc, argv);
Andrew Trickdc073ad2013-09-18 23:31:10 +000077
Owen Anderson19251ec2009-07-15 22:16:10 +000078 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000079 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
80 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +000081
Chris Lattner27936992007-05-06 05:13:17 +000082 unsigned BaseArg = 0;
83 std::string ErrorMessage;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000084
Andy Gibbsb23ea722013-04-15 12:06:32 +000085 OwningPtr<Module> Composite(LoadFile(argv[0],
86 InputFilenames[BaseArg], Context));
Chris Lattner27936992007-05-06 05:13:17 +000087 if (Composite.get() == 0) {
Dan Gohmanee051522009-07-16 15:30:09 +000088 errs() << argv[0] << ": error loading file '"
89 << InputFilenames[BaseArg] << "'\n";
Chris Lattner27936992007-05-06 05:13:17 +000090 return 1;
91 }
92
Eli Bendersky7da92ed2014-02-20 22:19:24 +000093 Linker L(Composite.get(), SuppressWarnings);
Chris Lattner27936992007-05-06 05:13:17 +000094 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Andy Gibbsb23ea722013-04-15 12:06:32 +000095 OwningPtr<Module> M(LoadFile(argv[0], InputFilenames[i], Context));
Chris Lattner27936992007-05-06 05:13:17 +000096 if (M.get() == 0) {
Dan Gohmanee051522009-07-16 15:30:09 +000097 errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Chris Lattnerdbf0a562004-09-27 16:41:01 +000098 return 1;
99 }
Chris Lattnerebaa7882001-10-23 20:44:55 +0000100
Dan Gohmanee051522009-07-16 15:30:09 +0000101 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerebaa7882001-10-23 20:44:55 +0000102
Rafael Espindola914d31f2013-05-04 05:30:49 +0000103 if (L.linkInModule(M.get(), &ErrorMessage)) {
Dan Gohmanee051522009-07-16 15:30:09 +0000104 errs() << argv[0] << ": link error in '" << InputFilenames[i]
105 << "': " << ErrorMessage << "\n";
Chris Lattner27936992007-05-06 05:13:17 +0000106 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000107 }
Chris Lattner27936992007-05-06 05:13:17 +0000108 }
Reid Spencer996ec722004-12-30 05:36:08 +0000109
Dan Gohman2b09de92009-09-15 15:35:07 +0000110 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000111
Chris Lattnerabd17362009-08-23 02:56:05 +0000112 std::string ErrorInfo;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000113 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None);
Chris Lattnerabd17362009-08-23 02:56:05 +0000114 if (!ErrorInfo.empty()) {
115 errs() << ErrorInfo << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000116 return 1;
117 }
Reid Spencer996ec722004-12-30 05:36:08 +0000118
Dan Gohman2b09de92009-09-15 15:35:07 +0000119 if (verifyModule(*Composite)) {
Dan Gohmanee051522009-07-16 15:30:09 +0000120 errs() << argv[0] << ": linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000121 return 1;
122 }
123
Dan Gohmanee051522009-07-16 15:30:09 +0000124 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000125 if (OutputAssembly) {
Dan Gohmana2233f22010-09-01 14:20:41 +0000126 Out.os() << *Composite;
127 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
128 WriteBitcodeToFile(Composite.get(), Out.os());
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000129
130 // Declare success.
131 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000132
Chris Lattner27936992007-05-06 05:13:17 +0000133 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000134}