blob: be556d1ed2570127ac1b2417b793e91c80bf2a31 [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 Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +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,
Chris Lattner54e05af2002-07-22 02:18:09 +000032 cl::desc("<input bytecode 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
Reid Spencer4b5fdc72004-11-07 05:50:16 +000046static cl::opt<bool> NoCompress("disable-compression", cl::init(false),
Jeff Cohen7109ce82005-01-01 22:10:32 +000047 cl::desc("Don't compress the generated bytecode"));
Reid Spencerae70fed2004-11-07 05:41:32 +000048
Reid Spencerdccc01e2004-09-12 23:39:42 +000049// LoadFile - Read the specified bytecode file in and return it. This routine
50// searches the link path for the specified file to try to find it...
51//
52static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
53 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";
Reid Spencer1fce0912004-12-11 00:14:15 +000062 Module* Result = ParseBytecodeFile(Filename.toString(), &ErrorMessage);
Reid Spencerdccc01e2004-09-12 23:39:42 +000063 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000064
65 if (Verbose) {
Bill Wendlinge8156192006-12-07 01:30:32 +000066 cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
67 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
68 cerr << "\n";
Reid Spencer6939f812004-09-11 04:32:42 +000069 }
70 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +000071 cerr << "Bytecode file: '" << Filename.c_str() << "' does not exist.\n";
Reid Spencer6939f812004-09-11 04:32:42 +000072 }
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) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000078 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000079 try {
80 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
81 sys::PrintStackTraceOnErrorSignal();
82 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
Chris Lattner075a0b72001-10-13 07:06:23 +000083
Reid Spencer1ef8bda2004-12-30 05:36:08 +000084 unsigned BaseArg = 0;
85 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000086
Reid Spencer1ef8bda2004-12-30 05:36:08 +000087 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
88 if (Composite.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000089 cerr << argv[0] << ": error loading file '"
90 << InputFilenames[BaseArg] << "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +000091 return 1;
92 }
Chris Lattnerb81adf12001-10-23 20:44:55 +000093
Reid Spencer1ef8bda2004-12-30 05:36:08 +000094 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
95 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
96 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000097 cerr << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000098 return 1;
99 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000100
Bill Wendlinge8156192006-12-07 01:30:32 +0000101 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000102
103 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000104 cerr << argv[0] << ": link error in '" << InputFilenames[i]
105 << "': " << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 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
Bill Wendlinge8156192006-12-07 01:30:32 +0000113 if (DumpAsm) 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!
Bill Wendlinge8156192006-12-07 01:30:32 +0000120 cerr << argv[0] << ": error opening '" << OutputFilename
121 << "': file exists!\n"
122 << "Use -f command line argument to force output\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 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()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000129 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 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())) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000139 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
Bill Wendlinge8156192006-12-07 01:30:32 +0000143 if (Verbose) cerr << "Writing bytecode...\n";
144 OStream L(*Out);
Bill Wendling68fe61d2006-11-29 00:19:40 +0000145 WriteBytecodeToFile(Composite.get(), L, !NoCompress);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000146
147 if (Out != &std::cout) delete Out;
148 return 0;
149 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000150 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000152 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000153 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154 return 1;
Chris Lattner075a0b72001-10-13 07:06:23 +0000155}