1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCLD' UTILITY
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.
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/Linker.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "llvm/Bytecode/WriteBytecodePass.h"
22 #include "llvm/Transforms/CleanupGCCOutput.h"
23 #include "llvm/Transforms/ConstantMerge.h"
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/IPO/GlobalDCE.h"
26 #include "llvm/Transforms/IPO/Internalize.h"
27 #include "Support/CommandLine.h"
28 #include "Support/Signals.h"
32 #include <sys/types.h> // For FileExists
36 static cl::list<string>
37 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
40 static cl::opt<string>
41 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
42 cl::value_desc("filename"));
45 Verbose("v", cl::desc("Print information about actions taken"));
47 static cl::list<string>
48 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
49 cl::value_desc("directory"));
51 static cl::list<string>
52 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
53 cl::value_desc("library prefix"));
56 Strip("s", cl::desc("Strip symbol info from executable"));
59 // FileExists - Return true if the specified string is an openable file...
60 static inline bool FileExists(const std::string &FN) {
62 return stat(FN.c_str(), &StatBuf) != -1;
65 // LoadFile - Read the specified bytecode file in and return it. This routine
66 // searches the link path for the specified file to try to find it...
68 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
69 std::string Filename = FN;
70 std::string ErrorMessage;
72 unsigned NextLibPathIdx = 0;
73 bool FoundAFile = false;
76 if (Verbose) cerr << "Loading '" << Filename << "'\n";
77 if (FileExists(Filename)) FoundAFile = true;
78 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
79 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
82 cerr << "Error opening bytecode file: '" << Filename << "'";
83 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
87 if (NextLibPathIdx == LibPaths.size()) break;
88 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
92 cerr << "Bytecode file '" << FN << "' corrupt! "
93 << "Use 'gccld -v ...' for more info.\n";
95 cerr << "Could not locate bytecode file: '" << FN << "'\n";
96 return std::auto_ptr<Module>();
100 int main(int argc, char **argv) {
101 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
103 unsigned BaseArg = 0;
104 std::string ErrorMessage;
106 if (!Libraries.empty()) {
107 // Sort libraries list...
108 std::sort(Libraries.begin(), Libraries.end());
110 // Remove duplicate libraries entries...
111 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
114 // Add all of the libraries to the end of the link line...
115 for (unsigned i = 0; i < Libraries.size(); ++i)
116 InputFilenames.push_back("lib" + Libraries[i] + ".bc");
119 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
120 if (Composite.get() == 0) return 1;
122 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
123 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
124 if (M.get() == 0) return 1;
126 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
128 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
129 cerr << "Error linking in '" << InputFilenames[i] << "': "
130 << ErrorMessage << "\n";
135 // In addition to just linking the input from GCC, we also want to spiff it up
136 // a little bit. Do this now.
140 // Linking modules together can lead to duplicated global constants, only keep
141 // one copy of each constant...
143 Passes.add(createConstantMergePass());
145 // If the -s command line option was specified, strip the symbols out of the
146 // resulting program to make it smaller. -s is a GCC option that we are
150 Passes.add(createSymbolStrippingPass());
152 // Often if the programmer does not specify proper prototypes for the
153 // functions they are calling, they end up calling a vararg version of the
154 // function that does not get a body filled in (the real function has typed
155 // arguments). This pass merges the two functions.
157 Passes.add(createFunctionResolvingPass());
159 // Now that composite has been compiled, scan through the module, looking for
160 // a main function. If main is defined, mark all other functions internal.
162 Passes.add(createInternalizePass());
164 // Now that we have optimized the program, discard unreachable functions...
166 Passes.add(createGlobalDCEPass());
168 // Add the pass that writes bytecode to the output file...
169 std::ofstream Out((OutputFilename+".bc").c_str());
171 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
174 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
176 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
177 RemoveFileOnSignal(OutputFilename+".bc");
179 // Run our queue of passes all at once now, efficiently.
180 Passes.run(*Composite.get());
183 // Output the script to start the program...
184 std::ofstream Out2(OutputFilename.c_str());
186 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
189 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
192 // Make the script executable...
193 chmod(OutputFilename.c_str(), 0755);