blob: 70e0130da083e71162206e8401e63155dfc8c259 [file] [log] [blame]
Chris Lattnere7fca512002-01-24 19:12:12 +00001//===----------------------------------------------------------------------===//
2// LLVM 'GCCLD' UTILITY
3//
4// This utility is intended to be compatible with GCC, and follows standard
5// system ld conventions. As such, the default output file is ./a.out.
6// Additionally, this program outputs a shell script that is used to invoke LLI
7// to execute the program. In this manner, the generated executable (a.out for
8// example), is directly executable, whereas the bytecode file actually lives in
9// the a.out.bc file generated by this program. Also, Force is on by default.
10//
11// Note that if someone (or a script) deletes the executable program generated,
12// the .bc file will be left around. Considering that this is a temporary hack,
13// I'm not to worried about this.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000017#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000018#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000019#include "llvm/PassManager.h"
20#include "llvm/Bytecode/Reader.h"
21#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000022#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000024#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000026#include <fstream>
27#include <memory>
Chris Lattner41c34652002-03-11 17:49:53 +000028#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000029#include <sys/types.h> // For FileExists
30#include <sys/stat.h>
Anand Shuklacf17bcc2002-06-25 21:57:48 +000031using std::cerr;
Chris Lattnere7fca512002-01-24 19:12:12 +000032
Chris Lattner5ff62e92002-07-22 02:10:13 +000033static cl::list<string>
34InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
35 cl::OneOrMore);
36
37static cl::opt<string>
38OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Verbose("v", cl::desc("Print information about actions taken"));
43
44static cl::list<string>
45LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
46 cl::value_desc("directory"));
47
48static cl::list<string>
49Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
50 cl::value_desc("library prefix"));
51
52static cl::opt<bool>
53Strip("s", cl::desc("Strip symbol info from executable"));
54
Chris Lattnere7fca512002-01-24 19:12:12 +000055
56// FileExists - Return true if the specified string is an openable file...
57static inline bool FileExists(const std::string &FN) {
58 struct stat StatBuf;
59 return stat(FN.c_str(), &StatBuf) != -1;
60}
61
62// LoadFile - Read the specified bytecode file in and return it. This routine
63// searches the link path for the specified file to try to find it...
64//
65static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
66 std::string Filename = FN;
67 std::string ErrorMessage;
68
69 unsigned NextLibPathIdx = 0;
70 bool FoundAFile = false;
71
72 while (1) {
73 if (Verbose) cerr << "Loading '" << Filename << "'\n";
74 if (FileExists(Filename)) FoundAFile = true;
75 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
76 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
77
78 if (Verbose) {
79 cerr << "Error opening bytecode file: '" << Filename << "'";
80 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
Chris Lattnerc5d44932002-06-30 16:20:02 +000081 cerr << "\n";
Chris Lattnere7fca512002-01-24 19:12:12 +000082 }
83
84 if (NextLibPathIdx == LibPaths.size()) break;
85 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
86 }
87
88 if (FoundAFile)
89 cerr << "Bytecode file '" << FN << "' corrupt! "
Chris Lattner084235a2002-03-12 15:41:36 +000090 << "Use 'gccld -v ...' for more info.\n";
Chris Lattnere7fca512002-01-24 19:12:12 +000091 else
92 cerr << "Could not locate bytecode file: '" << FN << "'\n";
93 return std::auto_ptr<Module>();
94}
95
96
Chris Lattnere7fca512002-01-24 19:12:12 +000097int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +000098 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +000099
100 unsigned BaseArg = 0;
101 std::string ErrorMessage;
102
Chris Lattner41c34652002-03-11 17:49:53 +0000103 if (!Libraries.empty()) {
104 // Sort libraries list...
Chris Lattnerc5d44932002-06-30 16:20:02 +0000105 std::sort(Libraries.begin(), Libraries.end());
Chris Lattner41c34652002-03-11 17:49:53 +0000106
107 // Remove duplicate libraries entries...
108 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
109 Libraries.end());
110
111 // Add all of the libraries to the end of the link line...
112 for (unsigned i = 0; i < Libraries.size(); ++i)
113 InputFilenames.push_back("lib" + Libraries[i] + ".bc");
114 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000115
116 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
117 if (Composite.get() == 0) return 1;
118
119 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
120 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
121 if (M.get() == 0) return 1;
122
123 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
124
125 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
126 cerr << "Error linking in '" << InputFilenames[i] << "': "
Chris Lattnera7cc6dd2002-05-20 19:49:24 +0000127 << ErrorMessage << "\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000128 return 1;
129 }
130 }
131
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000132 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattnerad202a02002-04-08 00:14:58 +0000133 // a little bit. Do this now.
134 //
135 PassManager Passes;
136
137 // Linking modules together can lead to duplicated global constants, only keep
138 // one copy of each constant...
139 //
140 Passes.add(createConstantMergePass());
141
Chris Lattner2b598372002-04-08 05:18:12 +0000142 // If the -s command line option was specified, strip the symbols out of the
143 // resulting program to make it smaller. -s is a GCC option that we are
144 // supporting.
145 //
146 if (Strip)
147 Passes.add(createSymbolStrippingPass());
148
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000149 // Often if the programmer does not specify proper prototypes for the
150 // functions they are calling, they end up calling a vararg version of the
151 // function that does not get a body filled in (the real function has typed
152 // arguments). This pass merges the two functions.
153 //
154 Passes.add(createFunctionResolvingPass());
155
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000156 // Now that composite has been compiled, scan through the module, looking for
157 // a main function. If main is defined, mark all other functions internal.
158 //
Chris Lattner7bf5dfe2002-04-28 05:49:45 +0000159 Passes.add(createInternalizePass());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000160
Chris Lattnerad202a02002-04-08 00:14:58 +0000161 // Now that we have optimized the program, discard unreachable functions...
162 //
163 Passes.add(createGlobalDCEPass());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000164
Chris Lattnerad202a02002-04-08 00:14:58 +0000165 // Add the pass that writes bytecode to the output file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000166 std::ofstream Out((OutputFilename+".bc").c_str());
167 if (!Out.good()) {
168 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
169 return 1;
170 }
Chris Lattnerad202a02002-04-08 00:14:58 +0000171 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000172
Chris Lattner76d12292002-04-18 19:55:25 +0000173 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
174 RemoveFileOnSignal(OutputFilename+".bc");
175
Chris Lattnerad202a02002-04-08 00:14:58 +0000176 // Run our queue of passes all at once now, efficiently.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000177 Passes.run(*Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000178 Out.close();
179
180 // Output the script to start the program...
181 std::ofstream Out2(OutputFilename.c_str());
182 if (!Out2.good()) {
Chris Lattnerad202a02002-04-08 00:14:58 +0000183 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000184 return 1;
185 }
Chris Lattner41c34652002-03-11 17:49:53 +0000186 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000187 Out2.close();
188
189 // Make the script executable...
190 chmod(OutputFilename.c_str(), 0755);
191
192 return 0;
193}