blob: 7de3ba118a44cb25c0dc4eb1a861f100495e291e [file] [log] [blame]
Chris Lattner825937d2003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
John Criswell09344dc2003-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 Lattner9d810e02001-10-13 07:06:23 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmancf0c7442003-09-15 18:34:34 +000011// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner9d810e02001-10-13 07:06:23 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattneraa6a44f2003-08-28 16:25:34 +000015#include "llvm/Module.h"
16#include "llvm/Analysis/Verifier.h"
Chris Lattner9d810e02001-10-13 07:06:23 +000017#include "llvm/Bytecode/Reader.h"
18#include "llvm/Bytecode/Writer.h"
Misha Brukman28ee8f02004-06-23 17:33:09 +000019#include "llvm/Support/Linker.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattner278f5152004-05-27 05:41:36 +000021#include "llvm/System/Signals.h"
Reid Spencerfe020a32004-09-11 04:32:42 +000022#include "llvm/System/Path.h"
Chris Lattner5de22042001-11-27 00:03:19 +000023#include <fstream>
Reid Spencerf0ebb252004-07-04 12:20:55 +000024#include <iostream>
Chris Lattner9d810e02001-10-13 07:06:23 +000025#include <memory>
26
Brian Gaeke960707c2003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattnerf5cad152002-07-22 02:10:13 +000029static cl::list<std::string>
30InputFilenames(cl::Positional, cl::OneOrMore,
Chris Lattner17570e12002-07-22 02:18:09 +000031 cl::desc("<input bytecode files>"));
Chris Lattnerf5cad152002-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 Spencer505b2252004-11-07 05:50:16 +000045static cl::opt<bool> NoCompress("disable-compression", cl::init(false),
Reid Spencer7b05ef62004-11-07 05:41:32 +000046 cl::desc("Don't ompress the generated bytecode"));
47
Reid Spencerb956fc12004-09-12 23:39:42 +000048// LoadFile - Read the specified bytecode file in and return it. This routine
49// searches the link path for the specified file to try to find it...
50//
51static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
52 sys::Path Filename;
Reid Spencer0c6a2832004-11-05 22:15:36 +000053 if (!Filename.setFile(FN)) {
Reid Spencerb956fc12004-09-12 23:39:42 +000054 std::cerr << "Invalid file name: '" << FN << "'\n";
55 return std::auto_ptr<Module>();
56 }
Chris Lattner5053ba92001-12-08 20:31:32 +000057
Reid Spencerfe020a32004-09-11 04:32:42 +000058 std::string ErrorMessage;
59 if (Filename.exists()) {
Reid Spencerb956fc12004-09-12 23:39:42 +000060 if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
Reid Spencerfe020a32004-09-11 04:32:42 +000061 Module* Result = ParseBytecodeFile(Filename.get(), &ErrorMessage);
Reid Spencerb956fc12004-09-12 23:39:42 +000062 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
Reid Spencerfe020a32004-09-11 04:32:42 +000063
64 if (Verbose) {
65 std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
66 if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
67 std::cerr << "\n";
68 }
69 } else {
70 std::cerr << "Bytecode file: '" << Filename.c_str()
71 << "' does not exist.\n";
72 }
Reid Spencerfe020a32004-09-11 04:32:42 +000073
Chris Lattnerae31f5b2001-10-24 06:23:00 +000074 return std::auto_ptr<Module>();
75}
Chris Lattner9d810e02001-10-13 07:06:23 +000076
77int main(int argc, char **argv) {
Chris Lattnerf5cad152002-07-22 02:10:13 +000078 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
Reid Spencere3263ec2004-08-29 19:28:55 +000079 sys::PrintStackTraceOnErrorSignal();
Chris Lattner9d810e02001-10-13 07:06:23 +000080 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
81
Chris Lattnerae31f5b2001-10-24 06:23:00 +000082 unsigned BaseArg = 0;
Chris Lattner7f74a562002-01-20 22:54:45 +000083 std::string ErrorMessage;
Chris Lattnerae31f5b2001-10-24 06:23:00 +000084
Chris Lattnerae31f5b2001-10-24 06:23:00 +000085 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
Chris Lattnerdbf0a562004-09-27 16:41:01 +000086 if (Composite.get() == 0) {
87 std::cerr << argv[0] << ": error loading file '"
88 << InputFilenames[BaseArg] << "'\n";
89 return 1;
90 }
Chris Lattnerae31f5b2001-10-24 06:23:00 +000091
92 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
Chris Lattner7f74a562002-01-20 22:54:45 +000093 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
Chris Lattnerdbf0a562004-09-27 16:41:01 +000094 if (M.get() == 0) {
95 std::cerr << argv[0] << ": error loading file '"
96 << InputFilenames[i] << "'\n";
97 return 1;
98 }
Chris Lattnerebaa7882001-10-23 20:44:55 +000099
Chris Lattner02a16832003-05-22 20:13:16 +0000100 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnerebaa7882001-10-23 20:44:55 +0000101
Chris Lattner9d810e02001-10-13 07:06:23 +0000102 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
Reid Spencerfe020a32004-09-11 04:32:42 +0000103 std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
Chris Lattner02a16832003-05-22 20:13:16 +0000104 << "': " << ErrorMessage << "\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000105 return 1;
106 }
107 }
108
Reid Spencerfe020a32004-09-11 04:32:42 +0000109 // TODO: Iterate over the -l list and link in any modules containing
110 // global symbols that have not been resolved so far.
111
Chris Lattner02a16832003-05-22 20:13:16 +0000112 if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
Chris Lattnerd6311652001-10-14 23:23:33 +0000113
Anand Shuklafef32412002-06-25 21:57:48 +0000114 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner3aa32572003-06-13 16:10:26 +0000115 if (OutputFilename != "-") {
Chris Lattner0e11e542002-01-22 21:07:24 +0000116 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000117 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-05-22 20:13:16 +0000118 std::cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists!\n"
120 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000121 return 1;
122 }
123 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner9d810e02001-10-13 07:06:23 +0000124 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000125 std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
Chris Lattner9d810e02001-10-13 07:06:23 +0000126 return 1;
127 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000128
Misha Brukmand6769742003-10-10 17:56:49 +0000129 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattnerc065ad82002-04-18 19:55:25 +0000130 // SIGINT
Reid Spencere3263ec2004-08-29 19:28:55 +0000131 sys::RemoveFileOnSignal(OutputFilename);
Chris Lattner9d810e02001-10-13 07:06:23 +0000132 }
133
Chris Lattneraa6a44f2003-08-28 16:25:34 +0000134 if (verifyModule(*Composite.get())) {
135 std::cerr << argv[0] << ": linked module is broken!\n";
136 return 1;
137 }
138
Chris Lattner02a16832003-05-22 20:13:16 +0000139 if (Verbose) std::cerr << "Writing bytecode...\n";
Reid Spencer7b05ef62004-11-07 05:41:32 +0000140 WriteBytecodeToFile(Composite.get(), *Out, !NoCompress);
Chris Lattner9d810e02001-10-13 07:06:23 +0000141
Chris Lattner7f74a562002-01-20 22:54:45 +0000142 if (Out != &std::cout) delete Out;
Chris Lattner9d810e02001-10-13 07:06:23 +0000143 return 0;
144}