blob: ae5fa40ecba8491639cc96ca00cb8d4cfb666e0a [file] [log] [blame]
Chris Lattnerbb37a692003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner075a0b72001-10-13 07:06:23 +00009//
10// This utility may be invoked in the following manner:
Misha Brukman3ef3beb2003-09-15 18:34:34 +000011// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner075a0b72001-10-13 07:06:23 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencer6bb69352004-11-14 23:25:32 +000015#include "llvm/Linker.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattnera55c4b12003-08-28 16:25:34 +000017#include "llvm/Module.h"
18#include "llvm/Analysis/Verifier.h"
Chris Lattnerc48e1db2007-05-06 05:13:17 +000019#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc48e1db2007-05-06 05:13:17 +000022#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000023#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanee335e32008-05-23 20:40:06 +000024#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Reid Spencer6939f812004-09-11 04:32:42 +000026#include "llvm/System/Path.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000028#include <iostream>
Chris Lattner075a0b72001-10-13 07:06:23 +000029#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattner5ff62e92002-07-22 02:10:13 +000032static cl::list<std::string>
33InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greifa99be512007-07-05 17:07:56 +000034 cl::desc("<input bitcode files>"));
Chris Lattner5ff62e92002-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
40static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
41
42static cl::opt<bool>
43Verbose("v", cl::desc("Print information about actions taken"));
44
45static cl::opt<bool>
46DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
47
Gabor Greifa99be512007-07-05 17:07:56 +000048// LoadFile - Read the specified bitcode file in and return it. This routine
Reid Spencerdccc01e2004-09-12 23:39:42 +000049// searches the link path for the specified file to try to find it...
50//
Owen Anderson8b477ed2009-07-01 16:58:40 +000051static inline std::auto_ptr<Module> LoadFile(const std::string &FN,
52 LLVMContext* Context) {
Reid Spencerdccc01e2004-09-12 23:39:42 +000053 sys::Path Filename;
Reid Spencerdd04df02005-07-07 23:21:43 +000054 if (!Filename.set(FN)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000055 cerr << "Invalid file name: '" << FN << "'\n";
Reid Spencerdccc01e2004-09-12 23:39:42 +000056 return std::auto_ptr<Module>();
57 }
Chris Lattner952d3652001-12-08 20:31:32 +000058
Reid Spencer6939f812004-09-11 04:32:42 +000059 std::string ErrorMessage;
60 if (Filename.exists()) {
Bill Wendlinge8156192006-12-07 01:30:32 +000061 if (Verbose) cerr << "Loading '" << Filename.c_str() << "'\n";
Chris Lattnerc48e1db2007-05-06 05:13:17 +000062 Module* Result = 0;
63
Chris Lattner44dadff2007-05-06 09:29:57 +000064 const std::string &FNStr = Filename.toString();
Chris Lattner065344d2007-05-06 23:45:49 +000065 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(FNStr,
66 &ErrorMessage)) {
Owen Anderson8b477ed2009-07-01 16:58:40 +000067 Result = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +000068 delete Buffer;
69 }
Reid Spencerdccc01e2004-09-12 23:39:42 +000070 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000071
72 if (Verbose) {
Gabor Greifa99be512007-07-05 17:07:56 +000073 cerr << "Error opening bitcode file: '" << Filename.c_str() << "'";
Bill Wendlinge8156192006-12-07 01:30:32 +000074 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
75 cerr << "\n";
Reid Spencer6939f812004-09-11 04:32:42 +000076 }
77 } else {
Gabor Greifa99be512007-07-05 17:07:56 +000078 cerr << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
Reid Spencer6939f812004-09-11 04:32:42 +000079 }
Reid Spencer6939f812004-09-11 04:32:42 +000080
Chris Lattner65be3212001-10-24 06:23:00 +000081 return std::auto_ptr<Module>();
82}
Chris Lattner075a0b72001-10-13 07:06:23 +000083
84int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000085 // Print a stack trace if we signal out.
Chris Lattnerc48e1db2007-05-06 05:13:17 +000086 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +000087 PrettyStackTraceProgram X(argc, argv);
88
Owen Anderson8b477ed2009-07-01 16:58:40 +000089 LLVMContext Context;
Chris Lattnercc14d252009-03-06 05:34:10 +000090 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
91 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner075a0b72001-10-13 07:06:23 +000092
Chris Lattnerc48e1db2007-05-06 05:13:17 +000093 unsigned BaseArg = 0;
94 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000095
Owen Anderson8b477ed2009-07-01 16:58:40 +000096 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg], &Context));
Chris Lattnerc48e1db2007-05-06 05:13:17 +000097 if (Composite.get() == 0) {
98 cerr << argv[0] << ": error loading file '"
99 << InputFilenames[BaseArg] << "'\n";
100 return 1;
101 }
102
103 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Owen Anderson8b477ed2009-07-01 16:58:40 +0000104 std::auto_ptr<Module> M(LoadFile(InputFilenames[i], &Context));
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000105 if (M.get() == 0) {
106 cerr << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +0000107 return 1;
108 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000109
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000110 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000111
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000112 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
113 cerr << argv[0] << ": link error in '" << InputFilenames[i]
114 << "': " << ErrorMessage << "\n";
115 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000117 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000118
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000119 // TODO: Iterate over the -l list and link in any modules containing
120 // global symbols that have not been resolved so far.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000121
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000122 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000124 // FIXME: cout is not binary!
125 std::ostream *Out = &std::cout; // Default to printing to stdout...
126 if (OutputFilename != "-") {
127 if (!Force && std::ifstream(OutputFilename.c_str())) {
128 // If force is not specified, make sure not to overwrite a file!
129 cerr << argv[0] << ": error opening '" << OutputFilename
130 << "': file exists!\n"
131 << "Use -f command line argument to force output\n";
132 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000134 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
135 std::ios::binary;
136 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
137 if (!Out->good()) {
138 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000139 return 1;
140 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000141
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000142 // Make sure that the Out file gets unlinked from the disk if we get a
143 // SIGINT
144 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
145 }
146
147 if (verifyModule(*Composite.get())) {
148 cerr << argv[0] << ": linked module is broken!\n";
149 return 1;
150 }
151
Gabor Greifa99be512007-07-05 17:07:56 +0000152 if (Verbose) cerr << "Writing bitcode...\n";
Chris Lattner44dadff2007-05-06 09:29:57 +0000153 WriteBitcodeToFile(Composite.get(), *Out);
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000154
155 if (Out != &std::cout) delete Out;
156 return 0;
Chris Lattner075a0b72001-10-13 07:06:23 +0000157}