blob: 8c6541ff0f39131c79efccd721f641d9ead11215 [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"
Chris Lattnera55c4b12003-08-28 16:25:34 +000016#include "llvm/Module.h"
17#include "llvm/Analysis/Verifier.h"
Chris Lattnerc48e1db2007-05-06 05:13:17 +000018#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc48e1db2007-05-06 05:13:17 +000021#include "llvm/Support/MemoryBuffer.h"
Dan Gohmanee335e32008-05-23 20:40:06 +000022#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/Signals.h"
Reid Spencer6939f812004-09-11 04:32:42 +000024#include "llvm/System/Path.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000026#include <iostream>
Chris Lattner075a0b72001-10-13 07:06:23 +000027#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::list<std::string>
31InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greifa99be512007-07-05 17:07:56 +000032 cl::desc("<input bitcode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000033
34static cl::opt<std::string>
35OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
36 cl::value_desc("filename"));
37
38static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
39
40static cl::opt<bool>
41Verbose("v", cl::desc("Print information about actions taken"));
42
43static cl::opt<bool>
44DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
45
Gabor Greifa99be512007-07-05 17:07:56 +000046// LoadFile - Read the specified bitcode file in and return it. This routine
Reid Spencerdccc01e2004-09-12 23:39:42 +000047// searches the link path for the specified file to try to find it...
48//
49static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
50 sys::Path Filename;
Reid Spencerdd04df02005-07-07 23:21:43 +000051 if (!Filename.set(FN)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000052 cerr << "Invalid file name: '" << FN << "'\n";
Reid Spencerdccc01e2004-09-12 23:39:42 +000053 return std::auto_ptr<Module>();
54 }
Chris Lattner952d3652001-12-08 20:31:32 +000055
Reid Spencer6939f812004-09-11 04:32:42 +000056 std::string ErrorMessage;
57 if (Filename.exists()) {
Bill Wendlinge8156192006-12-07 01:30:32 +000058 if (Verbose) cerr << "Loading '" << Filename.c_str() << "'\n";
Chris Lattnerc48e1db2007-05-06 05:13:17 +000059 Module* Result = 0;
60
Chris Lattner44dadff2007-05-06 09:29:57 +000061 const std::string &FNStr = Filename.toString();
Chris Lattner065344d2007-05-06 23:45:49 +000062 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(FNStr,
63 &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +000064 Result = ParseBitcodeFile(Buffer, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +000065 delete Buffer;
66 }
Reid Spencerdccc01e2004-09-12 23:39:42 +000067 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000068
69 if (Verbose) {
Gabor Greifa99be512007-07-05 17:07:56 +000070 cerr << "Error opening bitcode file: '" << Filename.c_str() << "'";
Bill Wendlinge8156192006-12-07 01:30:32 +000071 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
72 cerr << "\n";
Reid Spencer6939f812004-09-11 04:32:42 +000073 }
74 } else {
Gabor Greifa99be512007-07-05 17:07:56 +000075 cerr << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
Reid Spencer6939f812004-09-11 04:32:42 +000076 }
Reid Spencer6939f812004-09-11 04:32:42 +000077
Chris Lattner65be3212001-10-24 06:23:00 +000078 return std::auto_ptr<Module>();
79}
Chris Lattner075a0b72001-10-13 07:06:23 +000080
81int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000082 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman82a13c92007-10-08 15:45:12 +000083 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattnerc48e1db2007-05-06 05:13:17 +000084 sys::PrintStackTraceOnErrorSignal();
85 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
Chris Lattner075a0b72001-10-13 07:06:23 +000086
Chris Lattnerc48e1db2007-05-06 05:13:17 +000087 unsigned BaseArg = 0;
88 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000089
Chris Lattnerc48e1db2007-05-06 05:13:17 +000090 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
91 if (Composite.get() == 0) {
92 cerr << argv[0] << ": error loading file '"
93 << InputFilenames[BaseArg] << "'\n";
94 return 1;
95 }
96
97 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
98 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
99 if (M.get() == 0) {
100 cerr << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +0000101 return 1;
102 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000103
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000104 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000105
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000106 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
107 cerr << argv[0] << ": link error in '" << InputFilenames[i]
108 << "': " << ErrorMessage << "\n";
109 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000110 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000111 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000112
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000113 // TODO: Iterate over the -l list and link in any modules containing
114 // global symbols that have not been resolved so far.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000116 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000118 // FIXME: cout is not binary!
119 std::ostream *Out = &std::cout; // Default to printing to stdout...
120 if (OutputFilename != "-") {
121 if (!Force && std::ifstream(OutputFilename.c_str())) {
122 // If force is not specified, make sure not to overwrite a file!
123 cerr << argv[0] << ": error opening '" << OutputFilename
124 << "': file exists!\n"
125 << "Use -f command line argument to force output\n";
126 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000127 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000128 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
129 std::ios::binary;
130 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
131 if (!Out->good()) {
132 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000133 return 1;
134 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000135
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000136 // Make sure that the Out file gets unlinked from the disk if we get a
137 // SIGINT
138 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
139 }
140
141 if (verifyModule(*Composite.get())) {
142 cerr << argv[0] << ": linked module is broken!\n";
143 return 1;
144 }
145
Gabor Greifa99be512007-07-05 17:07:56 +0000146 if (Verbose) cerr << "Writing bitcode...\n";
Chris Lattner44dadff2007-05-06 09:29:57 +0000147 WriteBitcodeToFile(Composite.get(), *Out);
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000148
149 if (Out != &std::cout) delete Out;
150 return 0;
Chris Lattner075a0b72001-10-13 07:06:23 +0000151}