blob: 1e16aa100635338c7181052fe432b7ee64625ae2 [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"
Chris Lattner76d12292002-04-18 19:55:25 +000027#include "Support/Signals.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000028#include <fstream>
29#include <memory>
Chris Lattner41c34652002-03-11 17:49:53 +000030#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000031#include <sys/types.h> // For FileExists
32#include <sys/stat.h>
33
34
35cl::StringList InputFilenames("", "Load <arg> files, linking them together",
36 cl::OneOrMore);
37cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
38cl::Flag Verbose ("v", "Print information about actions taken");
39cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
40cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
Chris Lattner2b598372002-04-08 05:18:12 +000041cl::Flag Strip ("s", "Strip symbol info from executable");
Chris Lattnere7fca512002-01-24 19:12:12 +000042
43// FileExists - Return true if the specified string is an openable file...
44static inline bool FileExists(const std::string &FN) {
45 struct stat StatBuf;
46 return stat(FN.c_str(), &StatBuf) != -1;
47}
48
49// LoadFile - Read the specified bytecode file in and return it. This routine
50// searches the link path for the specified file to try to find it...
51//
52static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
53 std::string Filename = FN;
54 std::string ErrorMessage;
55
56 unsigned NextLibPathIdx = 0;
57 bool FoundAFile = false;
58
59 while (1) {
60 if (Verbose) cerr << "Loading '" << Filename << "'\n";
61 if (FileExists(Filename)) FoundAFile = true;
62 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
63 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
64
65 if (Verbose) {
66 cerr << "Error opening bytecode file: '" << Filename << "'";
67 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
68 cerr << endl;
69 }
70
71 if (NextLibPathIdx == LibPaths.size()) break;
72 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
73 }
74
75 if (FoundAFile)
76 cerr << "Bytecode file '" << FN << "' corrupt! "
Chris Lattner084235a2002-03-12 15:41:36 +000077 << "Use 'gccld -v ...' for more info.\n";
Chris Lattnere7fca512002-01-24 19:12:12 +000078 else
79 cerr << "Could not locate bytecode file: '" << FN << "'\n";
80 return std::auto_ptr<Module>();
81}
82
83
84
85
86int main(int argc, char **argv) {
87 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n",
88 cl::EnableSingleLetterArgValue |
89 cl::DisableSingleLetterArgGrouping);
90 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
91
92 unsigned BaseArg = 0;
93 std::string ErrorMessage;
94
Chris Lattner41c34652002-03-11 17:49:53 +000095 if (!Libraries.empty()) {
96 // Sort libraries list...
97 sort(Libraries.begin(), Libraries.end());
98
99 // Remove duplicate libraries entries...
100 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
101 Libraries.end());
102
103 // Add all of the libraries to the end of the link line...
104 for (unsigned i = 0; i < Libraries.size(); ++i)
105 InputFilenames.push_back("lib" + Libraries[i] + ".bc");
106 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000107
108 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
109 if (Composite.get() == 0) return 1;
110
111 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
112 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
113 if (M.get() == 0) return 1;
114
115 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
116
117 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
118 cerr << "Error linking in '" << InputFilenames[i] << "': "
119 << ErrorMessage << endl;
120 return 1;
121 }
122 }
123
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000124 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattnerad202a02002-04-08 00:14:58 +0000125 // a little bit. Do this now.
126 //
127 PassManager Passes;
128
129 // Linking modules together can lead to duplicated global constants, only keep
130 // one copy of each constant...
131 //
132 Passes.add(createConstantMergePass());
133
Chris Lattner2b598372002-04-08 05:18:12 +0000134 // If the -s command line option was specified, strip the symbols out of the
135 // resulting program to make it smaller. -s is a GCC option that we are
136 // supporting.
137 //
138 if (Strip)
139 Passes.add(createSymbolStrippingPass());
140
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000141 // Often if the programmer does not specify proper prototypes for the
142 // functions they are calling, they end up calling a vararg version of the
143 // function that does not get a body filled in (the real function has typed
144 // arguments). This pass merges the two functions.
145 //
146 Passes.add(createFunctionResolvingPass());
147
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000148 // Now that composite has been compiled, scan through the module, looking for
149 // a main function. If main is defined, mark all other functions internal.
150 //
Chris Lattnerad202a02002-04-08 00:14:58 +0000151 // TODO:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152
Chris Lattnerad202a02002-04-08 00:14:58 +0000153 // Now that we have optimized the program, discard unreachable functions...
154 //
155 Passes.add(createGlobalDCEPass());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000156
Chris Lattnerad202a02002-04-08 00:14:58 +0000157 // Add the pass that writes bytecode to the output file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000158 std::ofstream Out((OutputFilename+".bc").c_str());
159 if (!Out.good()) {
160 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
161 return 1;
162 }
Chris Lattnerad202a02002-04-08 00:14:58 +0000163 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000164
Chris Lattner76d12292002-04-18 19:55:25 +0000165 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
166 RemoveFileOnSignal(OutputFilename+".bc");
167
Chris Lattnerad202a02002-04-08 00:14:58 +0000168 // Run our queue of passes all at once now, efficiently.
169 Passes.run(Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000170 Out.close();
171
172 // Output the script to start the program...
173 std::ofstream Out2(OutputFilename.c_str());
174 if (!Out2.good()) {
Chris Lattnerad202a02002-04-08 00:14:58 +0000175 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000176 return 1;
177 }
Chris Lattner41c34652002-03-11 17:49:53 +0000178 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000179 Out2.close();
180
181 // Make the script executable...
182 chmod(OutputFilename.c_str(), 0755);
183
184 return 0;
185}