blob: 6e404918b669c72ac5b3d93f2f8cf34c81fbd830 [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"
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 Spencerdccc01e2004-09-12 23:39:42 +000045// LoadFile - Read the specified bytecode file in and return it. This routine
46// searches the link path for the specified file to try to find it...
47//
48static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
49 sys::Path Filename;
50 if (!Filename.set_file(FN)) {
51 std::cerr << "Invalid file name: '" << FN << "'\n";
52 return std::auto_ptr<Module>();
53 }
Chris Lattner952d3652001-12-08 20:31:32 +000054
Reid Spencer6939f812004-09-11 04:32:42 +000055 std::string ErrorMessage;
56 if (Filename.exists()) {
Reid Spencerdccc01e2004-09-12 23:39:42 +000057 if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
Reid Spencer6939f812004-09-11 04:32:42 +000058 Module* Result = ParseBytecodeFile(Filename.get(), &ErrorMessage);
Reid Spencerdccc01e2004-09-12 23:39:42 +000059 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencer6939f812004-09-11 04:32:42 +000060
61 if (Verbose) {
62 std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
63 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
64 std::cerr << "\n";
65 }
66 } else {
67 std::cerr << "Bytecode file: '" << Filename.c_str()
68 << "' does not exist.\n";
69 }
Reid Spencer6939f812004-09-11 04:32:42 +000070
Chris Lattner65be3212001-10-24 06:23:00 +000071 return std::auto_ptr<Module>();
72}
Chris Lattner075a0b72001-10-13 07:06:23 +000073
74int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000075 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Reid Spencer9de7b332004-08-29 19:28:55 +000076 sys::PrintStackTraceOnErrorSignal();
Chris Lattner075a0b72001-10-13 07:06:23 +000077 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
78
Chris Lattner65be3212001-10-24 06:23:00 +000079 unsigned BaseArg = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000080 std::string ErrorMessage;
Chris Lattner65be3212001-10-24 06:23:00 +000081
Chris Lattner65be3212001-10-24 06:23:00 +000082 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
Chris Lattner48f44cf2004-09-27 16:41:01 +000083 if (Composite.get() == 0) {
84 std::cerr << argv[0] << ": error loading file '"
85 << InputFilenames[BaseArg] << "'\n";
86 return 1;
87 }
Chris Lattner65be3212001-10-24 06:23:00 +000088
89 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner697954c2002-01-20 22:54:45 +000090 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattner48f44cf2004-09-27 16:41:01 +000091 if (M.get() == 0) {
92 std::cerr << argv[0] << ": error loading file '"
93 << InputFilenames[i] << "'\n";
94 return 1;
95 }
Chris Lattnerb81adf12001-10-23 20:44:55 +000096
Chris Lattner6c8103f2003-05-22 20:13:16 +000097 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerb81adf12001-10-23 20:44:55 +000098
Chris Lattner075a0b72001-10-13 07:06:23 +000099 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Reid Spencer6939f812004-09-11 04:32:42 +0000100 std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
Chris Lattner6c8103f2003-05-22 20:13:16 +0000101 << "': " << ErrorMessage << "\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000102 return 1;
103 }
104 }
105
Reid Spencer6939f812004-09-11 04:32:42 +0000106 // TODO: Iterate over the -l list and link in any modules containing
107 // global symbols that have not been resolved so far.
108
Chris Lattner6c8103f2003-05-22 20:13:16 +0000109 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattner164cb692001-10-14 23:23:33 +0000110
Anand Shuklacf17bcc2002-06-25 21:57:48 +0000111 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner4a4daba2003-06-13 16:10:26 +0000112 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +0000113 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000114 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000115 std::cerr << argv[0] << ": error opening '" << OutputFilename
116 << "': file exists!\n"
117 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000118 return 1;
119 }
120 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner075a0b72001-10-13 07:06:23 +0000121 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000122 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000123 return 1;
124 }
Chris Lattner76d12292002-04-18 19:55:25 +0000125
Misha Brukman452fea92003-10-10 17:56:49 +0000126 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000127 // SIGINT
Reid Spencer9de7b332004-08-29 19:28:55 +0000128 sys::RemoveFileOnSignal(OutputFilename);
Chris Lattner075a0b72001-10-13 07:06:23 +0000129 }
130
Chris Lattnera55c4b12003-08-28 16:25:34 +0000131 if (verifyModule(*Composite.get())) {
132 std::cerr << argv[0] << ": linked module is broken!\n";
133 return 1;
134 }
135
Chris Lattner6c8103f2003-05-22 20:13:16 +0000136 if (Verbose) std::cerr << "Writing bytecode...\n";
Chris Lattner075a0b72001-10-13 07:06:23 +0000137 WriteBytecodeToFile(Composite.get(), *Out);
138
Chris Lattner697954c2002-01-20 22:54:45 +0000139 if (Out != &std::cout) delete Out;
Chris Lattner075a0b72001-10-13 07:06:23 +0000140 return 0;
141}