blob: a4a283ced0a00af00616c43d3333c97d5ebc060c [file] [log] [blame]
Chris Lattnerbb37a692003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
Chris Lattnera55c4b12003-08-28 16:25:34 +000015#include "llvm/Module.h"
16#include "llvm/Analysis/Verifier.h"
Chris Lattner075a0b72001-10-13 07:06:23 +000017#include "llvm/Bytecode/Reader.h"
18#include "llvm/Bytecode/Writer.h"
Misha Brukman008248f2004-06-23 17:33:09 +000019#include "llvm/Support/Linker.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/FileUtilities.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"
24#include "llvm/ADT/SetVector.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>
28
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattner5ff62e92002-07-22 02:10:13 +000031static cl::list<std::string>
32InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner54e05af2002-07-22 02:18:09 +000033 cl::desc("<input bytecode files>"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000034
35static cl::opt<std::string>
36OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
37 cl::value_desc("filename"));
38
39static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
40
41static cl::opt<bool>
42Verbose("v", cl::desc("Print information about actions taken"));
43
44static cl::opt<bool>
45DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
46
47static cl::list<std::string>
48LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
49 cl::value_desc("directory"), cl::Prefix);
Chris Lattner952d3652001-12-08 20:31:32 +000050
Reid Spencer6939f812004-09-11 04:32:42 +000051static cl::list<std::string>
52Libraries("l", cl::desc("Specify library names to link with"), cl::ZeroOrMore,
53 cl::Prefix, cl::value_desc("library name"));
54
55// GetModule - This function is just factored out of the functions below
56static inline Module* GetModule(const sys::Path& Filename) {
57 if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
58 std::string ErrorMessage;
59 if (Filename.exists()) {
60 Module* Result = ParseBytecodeFile(Filename.get(), &ErrorMessage);
61 if (Result) return Result; // Load successful!
62
63 if (Verbose) {
64 std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
65 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
66 std::cerr << "\n";
67 }
68 } else {
69 std::cerr << "Bytecode file: '" << Filename.c_str()
70 << "' does not exist.\n";
71 }
72 return 0;
73}
74
Chris Lattner952d3652001-12-08 20:31:32 +000075// LoadFile - Read the specified bytecode file in and return it. This routine
76// searches the link path for the specified file to try to find it...
77//
Chris Lattner697954c2002-01-20 22:54:45 +000078static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
Reid Spencer6939f812004-09-11 04:32:42 +000079 sys::Path Filename;
80 if (!Filename.set_file(FN)) {
81 std::cerr << "Invalid file name: '" << Filename.c_str() << "'\n";
82 return std::auto_ptr<Module>();
83 }
Chris Lattner65be3212001-10-24 06:23:00 +000084
Reid Spencer6939f812004-09-11 04:32:42 +000085 if (Module* Result = GetModule(Filename))
86 return std::auto_ptr<Module>(Result);
87
Chris Lattner952d3652001-12-08 20:31:32 +000088 bool FoundAFile = false;
Chris Lattner65be3212001-10-24 06:23:00 +000089
Reid Spencer6939f812004-09-11 04:32:42 +000090 for (unsigned i = 0; i < LibPaths.size(); i++) {
91 if (!Filename.set_directory(LibPaths[i])) {
92 std::cerr << "Invalid library path: '" << LibPaths[i] << "'\n";
93 } else if (!Filename.append_file(FN)) {
94 std::cerr << "Invalid library path: '" << LibPaths[i]
95 << "/" << FN.c_str() << "'\n";
96 } else if (Filename.exists()) {
97 FoundAFile = true;
98 if (Module *Result = GetModule(Filename))
99 return std::auto_ptr<Module>(Result); // Load successful!
Chris Lattner65be3212001-10-24 06:23:00 +0000100 }
Chris Lattner65be3212001-10-24 06:23:00 +0000101 }
102
Chris Lattner952d3652001-12-08 20:31:32 +0000103 if (FoundAFile)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000104 std::cerr << "Bytecode file '" << FN << "' corrupt! "
Misha Brukman3ef3beb2003-09-15 18:34:34 +0000105 << "Use 'llvm-link -v ...' for more info.\n";
Chris Lattner952d3652001-12-08 20:31:32 +0000106 else
Chris Lattner6c8103f2003-05-22 20:13:16 +0000107 std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
Chris Lattner65be3212001-10-24 06:23:00 +0000108 return std::auto_ptr<Module>();
109}
Chris Lattner075a0b72001-10-13 07:06:23 +0000110
Reid Spencer6939f812004-09-11 04:32:42 +0000111sys::Path GetPathForLinkageItem(const std::string& link_item,
112 const std::string& dir) {
113 sys::Path fullpath;
114 fullpath.set_directory(dir);
Chris Lattner952d3652001-12-08 20:31:32 +0000115
Reid Spencer6939f812004-09-11 04:32:42 +0000116 // Try *.o
117 fullpath.append_file(link_item);
118 fullpath.append_suffix("o");
119 if (fullpath.readable())
120 return fullpath;
Chris Lattner952d3652001-12-08 20:31:32 +0000121
Reid Spencer6939f812004-09-11 04:32:42 +0000122 // Try *.bc
123 fullpath.elide_suffix();
124 fullpath.append_suffix("bc");
125 if (fullpath.readable())
126 return fullpath;
127
128 // Try *.so
129 fullpath.elide_suffix();
130 fullpath.append_suffix(sys::Path::GetDLLSuffix());
131 if (fullpath.readable())
132 return fullpath;
133
134 // Try lib*.a
135 fullpath.set_directory(dir);
136 fullpath.append_file(std::string("lib") + link_item);
137 fullpath.append_suffix("a");
138 if (fullpath.readable())
139 return fullpath;
140
141 // Didn't find one.
142 fullpath.clear();
143 return fullpath;
144}
145
146static inline bool LoadLibrary(const std::string &FN, Module*& Result) {
147 Result = 0;
148 sys::Path Filename;
149 if (!Filename.set_file(FN)) {
150 return false;
151 }
152
153 if (Filename.readable() && Filename.is_bytecode_file()) {
154 if (Result = GetModule(Filename))
155 return true;
156 }
157
158 bool foundAFile = false;
159
160 for (unsigned I = 0; I < LibPaths.size(); I++) {
161 sys::Path path = GetPathForLinkageItem(FN,LibPaths[I]);
162 if (!path.is_empty()) {
163 if (path.is_bytecode_file()) {
164 if (Result = GetModule(path)) {
165 return true;
166 } else {
167 // We found file but its not a valid bytecode file so we
168 // return false and leave Result null.
169 return false;
170 }
171 } else {
172 // We found a file, but its not a bytecode file so we return
173 // false and leave Result null.
174 return false;
175 }
176 }
177 }
178
179 // We didn't find a file so we leave Result null and return
180 // false to indicate that the library should be just left in the
181 // emitted module as resolvable at runtime.
182 return false;
183}
Chris Lattner952d3652001-12-08 20:31:32 +0000184
Chris Lattner075a0b72001-10-13 07:06:23 +0000185int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000186 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Reid Spencer9de7b332004-08-29 19:28:55 +0000187 sys::PrintStackTraceOnErrorSignal();
Chris Lattner075a0b72001-10-13 07:06:23 +0000188 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
189
Chris Lattner65be3212001-10-24 06:23:00 +0000190 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +0000191 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +0000192
Chris Lattner65be3212001-10-24 06:23:00 +0000193 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
194 if (Composite.get() == 0) return 1;
195
196 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +0000197 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner65be3212001-10-24 06:23:00 +0000198 if (M.get() == 0) return 1;
Chris Lattnerb81adf12001-10-23 20:44:55 +0000199
Chris Lattner6c8103f2003-05-22 20:13:16 +0000200 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +0000201
Chris Lattner075a0b72001-10-13 07:06:23 +0000202 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Reid Spencer6939f812004-09-11 04:32:42 +0000203 std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
Chris Lattner6c8103f2003-05-22 20:13:16 +0000204 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000205 return 1;
206 }
207 }
208
Reid Spencer6939f812004-09-11 04:32:42 +0000209 // Get the list of dependent libraries from the composite module
210 const Module::LibraryListType& libs = Composite.get()->getLibraries();
211
212 // Iterate over the list of dependent libraries, linking them in as we
213 // find them
214 Module::LibraryListType::const_iterator I = libs.begin();
215 while (I != libs.end()) {
216 Module* Mod = 0;
217 if (LoadLibrary(*I,Mod)) {
218 if (Mod != 0) {
219 std::auto_ptr<Module> M(Mod);
220 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
221 std::cerr << argv[0] << ": link error in '" << *I
222 << "': " << ErrorMessage << "\n";
223 return 1;
224 }
225 } else {
226 std::cerr << argv[0] << ": confused loading library '" << *I
227 << "'. Aborting\n";
228 return 2;
229 }
230 }
231 ++I;
232 }
233
234 // TODO: Iterate over the -l list and link in any modules containing
235 // global symbols that have not been resolved so far.
236
Chris Lattner6c8103f2003-05-22 20:13:16 +0000237 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000238
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000239 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000240 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000241 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000242 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000243 std::cerr << argv[0] << ": error opening '" << OutputFilename
244 << "': file exists!\n"
245 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000246 return 1;
247 }
248 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000249 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000250 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000251 return 1;
252 }
Chris Lattner76d12292002-04-18 19:55:25 +0000253
Misha Brukman452fea92003-10-10 17:56:49 +0000254 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000255 // SIGINT
Reid Spencer9de7b332004-08-29 19:28:55 +0000256 sys::RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000257 }
258
Chris Lattnera55c4b12003-08-28 16:25:34 +0000259 if (verifyModule(*Composite.get())) {
260 std::cerr << argv[0] << ": linked module is broken!\n";
261 return 1;
262 }
263
Chris Lattner6c8103f2003-05-22 20:13:16 +0000264 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000265 WriteBytecodeToFile(Composite.get(), *Out);
266
Chris Lattner697954c2002-01-20 22:54:45 +0000267 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000268 return 0;
269}