blob: 9db18909d559f4504f07b31baed4ed3272b78a37 [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 Lattner075a0b72001-10-13 07:06:23 +000018#include "llvm/Bytecode/Reader.h"
19#include "llvm/Bytecode/Writer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000021#include "llvm/System/Signals.h"
Reid Spencer6939f812004-09-11 04:32:42 +000022#include "llvm/System/Path.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000024#include <iostream>
Chris Lattner075a0b72001-10-13 07:06:23 +000025#include <memory>
26
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
Reid Spencer4b5fdc72004-11-07 05:50:16 +000045static cl::opt<bool> NoCompress("disable-compression", cl::init(false),
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 Spencer07adb282004-11-05 22:15:36 +000053 if (!Filename.setFile(FN)) {
Reid Spencerdccc01e2004-09-12 23:39:42 +000054 std::cerr << "Invalid file name: '" << FN << "'\n";
55 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()) {
Reid Spencerdccc01e2004-09-12 23:39:42 +000060 if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
Reid Spencer1fce0912004-12-11 00:14:15 +000061 Module* Result = ParseBytecodeFile(Filename.toString(), &ErrorMessage);
Reid Spencerdccc01e2004-09-12 23:39:42 +000062 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000063
64 if (Verbose) {
65 std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
66 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
67 std::cerr << "\n";
68 }
69 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +000070 std::cerr << "Bytecode file: '" << Filename.c_str()
Reid Spencer6939f812004-09-11 04:32:42 +000071 << "' does not exist.\n";
72 }
Reid Spencer6939f812004-09-11 04:32:42 +000073
Chris Lattner65be3212001-10-24 06:23:00 +000074 return std::auto_ptr<Module>();
75}
Chris Lattner075a0b72001-10-13 07:06:23 +000076
77int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000078 try {
79 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
80 sys::PrintStackTraceOnErrorSignal();
81 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
Chris Lattner075a0b72001-10-13 07:06:23 +000082
Reid Spencer1ef8bda2004-12-30 05:36:08 +000083 unsigned BaseArg = 0;
84 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000085
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
87 if (Composite.get() == 0) {
Chris Lattner48f44cf2004-09-27 16:41:01 +000088 std::cerr << argv[0] << ": error loading file '"
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 << InputFilenames[BaseArg] << "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +000090 return 1;
91 }
Chris Lattnerb81adf12001-10-23 20:44:55 +000092
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
94 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
95 if (M.get() == 0) {
96 std::cerr << argv[0] << ": error loading file '"
97 << InputFilenames[i] << "'\n";
98 return 1;
99 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000100
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000101 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
102
103 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
104 std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
105 << "': " << ErrorMessage << "\n";
106 return 1;
107 }
108 }
109
110 // TODO: Iterate over the -l list and link in any modules containing
111 // global symbols that have not been resolved so far.
112
Chris Lattnera88155d2005-02-13 19:12:31 +0000113 if (DumpAsm) std::cerr << "Here's the assembly:\n" << *Composite.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000115 // FIXME: cout is not binary!
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 std::ostream *Out = &std::cout; // Default to printing to stdout...
117 if (OutputFilename != "-") {
118 if (!Force && std::ifstream(OutputFilename.c_str())) {
119 // If force is not specified, make sure not to overwrite a file!
120 std::cerr << argv[0] << ": error opening '" << OutputFilename
121 << "': file exists!\n"
122 << "Use -f command line argument to force output\n";
123 return 1;
124 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000125 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
126 std::ios::binary;
127 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000128 if (!Out->good()) {
129 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
130 return 1;
131 }
132
133 // Make sure that the Out file gets unlinked from the disk if we get a
134 // SIGINT
135 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
136 }
137
138 if (verifyModule(*Composite.get())) {
139 std::cerr << argv[0] << ": linked module is broken!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000140 return 1;
141 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000142
143 if (Verbose) std::cerr << "Writing bytecode...\n";
144 WriteBytecodeToFile(Composite.get(), *Out, !NoCompress);
145
146 if (Out != &std::cout) delete Out;
147 return 0;
148 } catch (const std::string& msg) {
149 std::cerr << argv[0] << ": " << msg << "\n";
150 } catch (...) {
151 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000152 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000153 return 1;
Chris Lattner075a0b72001-10-13 07:06:23 +0000154}