blob: cebfec3fcee89c26d99e7b6466a5e1ee7203a390 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// llvm-link a.bc b.bc c.bc -o x.bc
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Linker.h"
Owen Anderson25209b42009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Module.h"
18#include "llvm/Analysis/Verifier.h"
19#include "llvm/Bitcode/ReaderWriter.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000023#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/System/Signals.h"
25#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include <memory>
27using namespace llvm;
28
29static cl::list<std::string>
30InputFilenames(cl::Positional, cl::OneOrMore,
31 cl::desc("<input bitcode files>"));
32
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
45// LoadFile - Read the specified bitcode file in and return it. This routine
46// searches the link path for the specified file to try to find it...
47//
Owen Anderson25209b42009-07-01 16:58:40 +000048static inline std::auto_ptr<Module> LoadFile(const std::string &FN,
Owen Anderson92adb182009-07-01 23:13:44 +000049 LLVMContext& Context) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 sys::Path Filename;
51 if (!Filename.set(FN)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000052 errs() << "Invalid file name: '" << FN << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 return std::auto_ptr<Module>();
54 }
55
56 std::string ErrorMessage;
57 if (Filename.exists()) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000058 if (Verbose) errs() << "Loading '" << Filename.c_str() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 Module* Result = 0;
60
61 const std::string &FNStr = Filename.toString();
62 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(FNStr,
63 &ErrorMessage)) {
Owen Anderson25209b42009-07-01 16:58:40 +000064 Result = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 delete Buffer;
66 }
67 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
68
69 if (Verbose) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000070 errs() << "Error opening bitcode file: '" << Filename.c_str() << "'";
71 if (ErrorMessage.size()) errs() << ": " << ErrorMessage;
72 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 }
74 } else {
Dan Gohmanb714fab2009-07-16 15:30:09 +000075 errs() << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 }
77
78 return std::auto_ptr<Module>();
79}
80
81int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000082 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000084 PrettyStackTraceProgram X(argc, argv);
85
Owen Andersone84b8b32009-07-15 22:16:10 +000086 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000087 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
88 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
90 unsigned BaseArg = 0;
91 std::string ErrorMessage;
92
Owen Andersona148fdd2009-07-01 21:22:36 +000093 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg], Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 if (Composite.get() == 0) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000095 errs() << argv[0] << ": error loading file '"
96 << InputFilenames[BaseArg] << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return 1;
98 }
99
100 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Owen Andersona148fdd2009-07-01 21:22:36 +0000101 std::auto_ptr<Module> M(LoadFile(InputFilenames[i], Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 if (M.get() == 0) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000103 errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 return 1;
105 }
106
Dan Gohmanb714fab2009-07-16 15:30:09 +0000107 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000110 errs() << argv[0] << ": link error in '" << InputFilenames[i]
111 << "': " << ErrorMessage << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return 1;
113 }
114 }
115
116 // TODO: Iterate over the -l list and link in any modules containing
117 // global symbols that have not been resolved so far.
118
Dan Gohmanb714fab2009-07-16 15:30:09 +0000119 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite.get();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
Chris Lattner4e95ea92009-08-23 02:56:05 +0000121 std::string ErrorInfo;
122 std::auto_ptr<raw_ostream>
123 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
124 raw_fd_ostream::F_Binary |
125 (Force ? raw_fd_ostream::F_Force : 0)));
126 if (!ErrorInfo.empty()) {
127 errs() << ErrorInfo << '\n';
128 if (!Force)
129 errs() << "Use -f command line argument to force output\n";
130 return 1;
131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 // Make sure that the Out file gets unlinked from the disk if we get a
134 // SIGINT
Chris Lattner4e95ea92009-08-23 02:56:05 +0000135 if (OutputFilename != "-")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 if (verifyModule(*Composite.get())) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000139 errs() << argv[0] << ": linked module is broken!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 return 1;
141 }
142
Dan Gohmanb714fab2009-07-16 15:30:09 +0000143 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 WriteBitcodeToFile(Composite.get(), *Out);
145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 return 0;
147}