blob: 223475eed81f2139f2b86c93d12cbb5250703a6c [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
Chris Lattner17be6792007-01-21 06:34:18 +000046static cl::opt<bool> NoCompress("disable-compression", cl::init(true),
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";
Chris Lattnerf2e292c2007-02-07 21:41:02 +000062 Module* Result = ParseBytecodeFile(Filename.toString(),
63 Compressor::decompressToNewBuffer,
64 &ErrorMessage);
Reid Spencerdccc01e2004-09-12 23:39:42 +000065 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000066
67 if (Verbose) {
Bill Wendlinge8156192006-12-07 01:30:32 +000068 cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
69 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
70 cerr << "\n";
Reid Spencer6939f812004-09-11 04:32:42 +000071 }
72 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +000073 cerr << "Bytecode file: '" << Filename.c_str() << "' does not exist.\n";
Reid Spencer6939f812004-09-11 04:32:42 +000074 }
Reid Spencer6939f812004-09-11 04:32:42 +000075
Chris Lattner65be3212001-10-24 06:23:00 +000076 return std::auto_ptr<Module>();
77}
Chris Lattner075a0b72001-10-13 07:06:23 +000078
79int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000080 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000081 try {
82 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
83 sys::PrintStackTraceOnErrorSignal();
84 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
Chris Lattner075a0b72001-10-13 07:06:23 +000085
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 unsigned BaseArg = 0;
87 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000088
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
90 if (Composite.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000091 cerr << argv[0] << ": error loading file '"
92 << InputFilenames[BaseArg] << "'\n";
Chris Lattner48f44cf2004-09-27 16:41:01 +000093 return 1;
94 }
Chris Lattnerb81adf12001-10-23 20:44:55 +000095
Reid Spencer1ef8bda2004-12-30 05:36:08 +000096 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
97 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
98 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000099 cerr << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000100 return 1;
101 }
Chris Lattnerb81adf12001-10-23 20:44:55 +0000102
Bill Wendlinge8156192006-12-07 01:30:32 +0000103 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000104
105 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000106 cerr << argv[0] << ": link error in '" << InputFilenames[i]
107 << "': " << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 return 1;
109 }
110 }
111
112 // TODO: Iterate over the -l list and link in any modules containing
113 // global symbols that have not been resolved so far.
114
Bill Wendlinge8156192006-12-07 01:30:32 +0000115 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000117 // FIXME: cout is not binary!
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000118 std::ostream *Out = &std::cout; // Default to printing to stdout...
119 if (OutputFilename != "-") {
120 if (!Force && std::ifstream(OutputFilename.c_str())) {
121 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000122 cerr << argv[0] << ": error opening '" << OutputFilename
123 << "': file exists!\n"
124 << "Use -f command line argument to force output\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000125 return 1;
126 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000127 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
128 std::ios::binary;
129 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000131 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000132 return 1;
133 }
134
135 // Make sure that the Out file gets unlinked from the disk if we get a
136 // SIGINT
137 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
138 }
139
140 if (verifyModule(*Composite.get())) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000141 cerr << argv[0] << ": linked module is broken!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000142 return 1;
143 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144
Bill Wendlinge8156192006-12-07 01:30:32 +0000145 if (Verbose) cerr << "Writing bytecode...\n";
146 OStream L(*Out);
Bill Wendling68fe61d2006-11-29 00:19:40 +0000147 WriteBytecodeToFile(Composite.get(), L, !NoCompress);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000148
149 if (Out != &std::cout) delete Out;
150 return 0;
151 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000152 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000153 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000154 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000155 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000156 return 1;
Chris Lattner075a0b72001-10-13 07:06:23 +0000157}