blob: aceb90889b93a54441672f92e8835ff67f32d2d0 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Reid Spencer6939f812004-09-11 04:32:42 +000023#include "llvm/System/Path.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000025#include <iostream>
Chris Lattner075a0b72001-10-13 07:06:23 +000026#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::list<std::string>
30InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000031 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
33static cl::opt<std::string>
34OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
35 cl::value_desc("filename"));
36
37static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
38
39static cl::opt<bool>
40Verbose("v", cl::desc("Print information about actions taken"));
41
42static cl::opt<bool>
43DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
44
Chris Lattner17be6792007-01-21 06:34:18 +000045static cl::opt<bool> NoCompress("disable-compression", cl::init(true),
Jeff Cohen7109ce82005-01-01 22:10:32 +000046 cl::desc("Don't compress the generated bytecode"));
Reid Spencerae70fed2004-11-07 05:41:32 +000047
Reid Spencerdccc01e2004-09-12 23:39:42 +000048// LoadFile - Read the specified bytecode file in and return it. This routine
49// searches the link path for the specified file to try to find it...
50//
51static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
52 sys::Path Filename;
Reid Spencerdd04df02005-07-07 23:21:43 +000053 if (!Filename.set(FN)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000054 cerr << "Invalid file name: '" << FN << "'\n";
Reid Spencerdccc01e2004-09-12 23:39:42 +000055 return std::auto_ptr<Module>();
56 }
Chris Lattner952d3652001-12-08 20:31:32 +000057
Reid Spencer6939f812004-09-11 04:32:42 +000058 std::string ErrorMessage;
59 if (Filename.exists()) {
Bill Wendlinge8156192006-12-07 01:30:32 +000060 if (Verbose) cerr << "Loading '" << Filename.c_str() << "'\n";
Chris Lattnerc48e1db2007-05-06 05:13:17 +000061 Module* Result = 0;
62
Chris Lattner44dadff2007-05-06 09:29:57 +000063 const std::string &FNStr = Filename.toString();
64 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&FNStr[0],
65 FNStr.size());
66 if (Buffer == 0)
67 ErrorMessage = "Error reading file '" + FNStr + "'";
68 else
69 Result = ParseBitcodeFile(Buffer, &ErrorMessage);
70 delete Buffer;
Reid Spencerdccc01e2004-09-12 23:39:42 +000071 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000072
73 if (Verbose) {
Bill Wendlinge8156192006-12-07 01:30:32 +000074 cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
75 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
76 cerr << "\n";
Reid Spencer6939f812004-09-11 04:32:42 +000077 }
78 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +000079 cerr << "Bytecode file: '" << Filename.c_str() << "' does not exist.\n";
Reid Spencer6939f812004-09-11 04:32:42 +000080 }
Reid Spencer6939f812004-09-11 04:32:42 +000081
Chris Lattner65be3212001-10-24 06:23:00 +000082 return std::auto_ptr<Module>();
83}
Chris Lattner075a0b72001-10-13 07:06:23 +000084
85int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000086 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattnerc48e1db2007-05-06 05:13:17 +000087 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
88 sys::PrintStackTraceOnErrorSignal();
89 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
Chris Lattner075a0b72001-10-13 07:06:23 +000090
Chris Lattnerc48e1db2007-05-06 05:13:17 +000091 unsigned BaseArg = 0;
92 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000093
Chris Lattnerc48e1db2007-05-06 05:13:17 +000094 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
95 if (Composite.get() == 0) {
96 cerr << argv[0] << ": error loading file '"
97 << InputFilenames[BaseArg] << "'\n";
98 return 1;
99 }
100
101 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
102 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
103 if (M.get() == 0) {
104 cerr << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +0000105 return 1;
106 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000107
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000108 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000109
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000110 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
111 cerr << argv[0] << ": link error in '" << InputFilenames[i]
112 << "': " << ErrorMessage << "\n";
113 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000115 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000117 // TODO: Iterate over the -l list and link in any modules containing
118 // global symbols that have not been resolved so far.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000120 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000121
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000122 // FIXME: cout is not binary!
123 std::ostream *Out = &std::cout; // Default to printing to stdout...
124 if (OutputFilename != "-") {
125 if (!Force && std::ifstream(OutputFilename.c_str())) {
126 // If force is not specified, make sure not to overwrite a file!
127 cerr << argv[0] << ": error opening '" << OutputFilename
128 << "': file exists!\n"
129 << "Use -f command line argument to force output\n";
130 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000131 }
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000132 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
133 std::ios::binary;
134 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
135 if (!Out->good()) {
136 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000137 return 1;
138 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000140 // Make sure that the Out file gets unlinked from the disk if we get a
141 // SIGINT
142 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
143 }
144
145 if (verifyModule(*Composite.get())) {
146 cerr << argv[0] << ": linked module is broken!\n";
147 return 1;
148 }
149
150 if (Verbose) cerr << "Writing bytecode...\n";
Chris Lattner44dadff2007-05-06 09:29:57 +0000151 WriteBitcodeToFile(Composite.get(), *Out);
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000152
153 if (Out != &std::cout) delete Out;
154 return 0;
Chris Lattner075a0b72001-10-13 07:06:23 +0000155}