blob: 66d1afb4f30f7e16323a5f63832512748e5cfcbc [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
17#include "llvm/Transforms/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 Lattner2b598372002-04-08 05:18:12 +000022#include "llvm/Transforms/SymbolStripping.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000023#include "llvm/Transforms/CleanupGCCOutput.h"
24#include "llvm/Transforms/ConstantMerge.h"
25#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000026#include "Support/CommandLine.h"
27#include <fstream>
28#include <memory>
Chris Lattner41c34652002-03-11 17:49:53 +000029#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000030#include <sys/types.h> // For FileExists
31#include <sys/stat.h>
32
33
34cl::StringList InputFilenames("", "Load <arg> files, linking them together",
35 cl::OneOrMore);
36cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
37cl::Flag Verbose ("v", "Print information about actions taken");
38cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
39cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
Chris Lattner2b598372002-04-08 05:18:12 +000040cl::Flag Strip ("s", "Strip symbol info from executable");
Chris Lattnere7fca512002-01-24 19:12:12 +000041
42// FileExists - Return true if the specified string is an openable file...
43static inline bool FileExists(const std::string &FN) {
44 struct stat StatBuf;
45 return stat(FN.c_str(), &StatBuf) != -1;
46}
47
48// 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 std::string Filename = FN;
53 std::string ErrorMessage;
54
55 unsigned NextLibPathIdx = 0;
56 bool FoundAFile = false;
57
58 while (1) {
59 if (Verbose) cerr << "Loading '" << Filename << "'\n";
60 if (FileExists(Filename)) FoundAFile = true;
61 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
62 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
63
64 if (Verbose) {
65 cerr << "Error opening bytecode file: '" << Filename << "'";
66 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
67 cerr << endl;
68 }
69
70 if (NextLibPathIdx == LibPaths.size()) break;
71 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
72 }
73
74 if (FoundAFile)
75 cerr << "Bytecode file '" << FN << "' corrupt! "
Chris Lattner084235a2002-03-12 15:41:36 +000076 << "Use 'gccld -v ...' for more info.\n";
Chris Lattnere7fca512002-01-24 19:12:12 +000077 else
78 cerr << "Could not locate bytecode file: '" << FN << "'\n";
79 return std::auto_ptr<Module>();
80}
81
82
83
84
85int main(int argc, char **argv) {
86 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n",
87 cl::EnableSingleLetterArgValue |
88 cl::DisableSingleLetterArgGrouping);
89 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
90
91 unsigned BaseArg = 0;
92 std::string ErrorMessage;
93
Chris Lattner41c34652002-03-11 17:49:53 +000094 if (!Libraries.empty()) {
95 // Sort libraries list...
96 sort(Libraries.begin(), Libraries.end());
97
98 // Remove duplicate libraries entries...
99 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
100 Libraries.end());
101
102 // Add all of the libraries to the end of the link line...
103 for (unsigned i = 0; i < Libraries.size(); ++i)
104 InputFilenames.push_back("lib" + Libraries[i] + ".bc");
105 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000106
107 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
108 if (Composite.get() == 0) return 1;
109
110 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
111 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
112 if (M.get() == 0) return 1;
113
114 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
115
116 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
117 cerr << "Error linking in '" << InputFilenames[i] << "': "
118 << ErrorMessage << endl;
119 return 1;
120 }
121 }
122
Chris Lattnerad202a02002-04-08 00:14:58 +0000123 // In addition to just parsing the input from GCC, we also want to spiff it up
124 // a little bit. Do this now.
125 //
126 PassManager Passes;
127
128 // Linking modules together can lead to duplicated global constants, only keep
129 // one copy of each constant...
130 //
131 Passes.add(createConstantMergePass());
132
133 // Often if the programmer does not specify proper prototypes for the
134 // functions they are calling, they end up calling a vararg version of the
135 // function that does not get a body filled in (the real function has typed
136 // arguments). This pass merges the two functions, among other things.
137 //
138 Passes.add(createCleanupGCCOutputPass());
139
Chris Lattner2b598372002-04-08 05:18:12 +0000140 // If the -s command line option was specified, strip the symbols out of the
141 // resulting program to make it smaller. -s is a GCC option that we are
142 // supporting.
143 //
144 if (Strip)
145 Passes.add(createSymbolStrippingPass());
146
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000147 // Now that composite has been compiled, scan through the module, looking for
148 // a main function. If main is defined, mark all other functions internal.
149 //
Chris Lattnerad202a02002-04-08 00:14:58 +0000150 // TODO:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000151
Chris Lattnerad202a02002-04-08 00:14:58 +0000152 // Now that we have optimized the program, discard unreachable functions...
153 //
154 Passes.add(createGlobalDCEPass());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000155
Chris Lattnerad202a02002-04-08 00:14:58 +0000156 // Add the pass that writes bytecode to the output file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000157 std::ofstream Out((OutputFilename+".bc").c_str());
158 if (!Out.good()) {
159 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
160 return 1;
161 }
Chris Lattnerad202a02002-04-08 00:14:58 +0000162 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000163
Chris Lattnerad202a02002-04-08 00:14:58 +0000164 // Run our queue of passes all at once now, efficiently.
165 Passes.run(Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000166 Out.close();
167
168 // Output the script to start the program...
169 std::ofstream Out2(OutputFilename.c_str());
170 if (!Out2.good()) {
Chris Lattnerad202a02002-04-08 00:14:58 +0000171 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000172 return 1;
173 }
Chris Lattner41c34652002-03-11 17:49:53 +0000174 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000175 Out2.close();
176
177 // Make the script executable...
178 chmod(OutputFilename.c_str(), 0755);
179
180 return 0;
181}