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/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/SymbolStripping.h"
23 #include "llvm/Transforms/CleanupGCCOutput.h"
24 #include "llvm/Transforms/ConstantMerge.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 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
38 cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
39 cl::Flag Verbose ("v", "Print information about actions taken");
40 cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
41 cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
42 cl::Flag Strip ("s", "Strip symbol info from executable");
44 // FileExists - Return true if the specified string is an openable file...
45 static inline bool FileExists(const std::string &FN) {
47 return stat(FN.c_str(), &StatBuf) != -1;
50 // LoadFile - Read the specified bytecode file in and return it. This routine
51 // searches the link path for the specified file to try to find it...
53 static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
54 std::string Filename = FN;
55 std::string ErrorMessage;
57 unsigned NextLibPathIdx = 0;
58 bool FoundAFile = false;
61 if (Verbose) cerr << "Loading '" << Filename << "'\n";
62 if (FileExists(Filename)) FoundAFile = true;
63 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
64 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
67 cerr << "Error opening bytecode file: '" << Filename << "'";
68 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
72 if (NextLibPathIdx == LibPaths.size()) break;
73 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
77 cerr << "Bytecode file '" << FN << "' corrupt! "
78 << "Use 'gccld -v ...' for more info.\n";
80 cerr << "Could not locate bytecode file: '" << FN << "'\n";
81 return std::auto_ptr<Module>();
85 int 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");
92 std::string ErrorMessage;
94 if (!Libraries.empty()) {
95 // Sort libraries list...
96 sort(Libraries.begin(), Libraries.end());
98 // Remove duplicate libraries entries...
99 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
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");
107 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
108 if (Composite.get() == 0) return 1;
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;
114 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
116 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
117 cerr << "Error linking in '" << InputFilenames[i] << "': "
118 << ErrorMessage << endl;
123 // In addition to just linking the input from GCC, we also want to spiff it up
124 // a little bit. Do this now.
128 // Linking modules together can lead to duplicated global constants, only keep
129 // one copy of each constant...
131 Passes.add(createConstantMergePass());
133 // If the -s command line option was specified, strip the symbols out of the
134 // resulting program to make it smaller. -s is a GCC option that we are
138 Passes.add(createSymbolStrippingPass());
140 // Often if the programmer does not specify proper prototypes for the
141 // functions they are calling, they end up calling a vararg version of the
142 // function that does not get a body filled in (the real function has typed
143 // arguments). This pass merges the two functions.
145 Passes.add(createFunctionResolvingPass());
147 // 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.
150 Passes.add(createInternalizePass());
152 // Now that we have optimized the program, discard unreachable functions...
154 Passes.add(createGlobalDCEPass());
156 // Add the pass that writes bytecode to the output file...
157 std::ofstream Out((OutputFilename+".bc").c_str());
159 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
162 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
164 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
165 RemoveFileOnSignal(OutputFilename+".bc");
167 // Run our queue of passes all at once now, efficiently.
168 Passes.run(Composite.get());
171 // Output the script to start the program...
172 std::ofstream Out2(OutputFilename.c_str());
174 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
177 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
180 // Make the script executable...
181 chmod(OutputFilename.c_str(), 0755);