blob: 4d369cdeba5a6214dc7b96fa2fef92d0e80677c9 [file] [log] [blame]
Chris Lattner5ff2e052002-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 Lattner5ff2e052002-01-24 19:12:12 +000018#include "llvm/Module.h"
Chris Lattner3b08c2f2002-04-08 00:14:58 +000019#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/IPO/GlobalDCE.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000025#include "Support/CommandLine.h"
26#include <fstream>
27#include <memory>
Chris Lattner6f0d4532002-03-11 17:49:53 +000028#include <algorithm>
Chris Lattner5ff2e052002-01-24 19:12:12 +000029#include <sys/types.h> // For FileExists
30#include <sys/stat.h>
31
32
33cl::StringList InputFilenames("", "Load <arg> files, linking them together",
34 cl::OneOrMore);
35cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
36cl::Flag Verbose ("v", "Print information about actions taken");
37cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
38cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
39
40
41// FileExists - Return true if the specified string is an openable file...
42static inline bool FileExists(const std::string &FN) {
43 struct stat StatBuf;
44 return stat(FN.c_str(), &StatBuf) != -1;
45}
46
47// LoadFile - Read the specified bytecode file in and return it. This routine
48// searches the link path for the specified file to try to find it...
49//
50static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
51 std::string Filename = FN;
52 std::string ErrorMessage;
53
54 unsigned NextLibPathIdx = 0;
55 bool FoundAFile = false;
56
57 while (1) {
58 if (Verbose) cerr << "Loading '" << Filename << "'\n";
59 if (FileExists(Filename)) FoundAFile = true;
60 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
61 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
62
63 if (Verbose) {
64 cerr << "Error opening bytecode file: '" << Filename << "'";
65 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
66 cerr << endl;
67 }
68
69 if (NextLibPathIdx == LibPaths.size()) break;
70 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
71 }
72
73 if (FoundAFile)
74 cerr << "Bytecode file '" << FN << "' corrupt! "
Chris Lattnere6975042002-03-12 15:41:36 +000075 << "Use 'gccld -v ...' for more info.\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +000076 else
77 cerr << "Could not locate bytecode file: '" << FN << "'\n";
78 return std::auto_ptr<Module>();
79}
80
81
82
83
84int main(int argc, char **argv) {
85 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n",
86 cl::EnableSingleLetterArgValue |
87 cl::DisableSingleLetterArgGrouping);
88 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
89
90 unsigned BaseArg = 0;
91 std::string ErrorMessage;
92
Chris Lattner6f0d4532002-03-11 17:49:53 +000093 if (!Libraries.empty()) {
94 // Sort libraries list...
95 sort(Libraries.begin(), Libraries.end());
96
97 // Remove duplicate libraries entries...
98 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
99 Libraries.end());
100
101 // Add all of the libraries to the end of the link line...
102 for (unsigned i = 0; i < Libraries.size(); ++i)
103 InputFilenames.push_back("lib" + Libraries[i] + ".bc");
104 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000105
106 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
107 if (Composite.get() == 0) return 1;
108
109 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
110 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
111 if (M.get() == 0) return 1;
112
113 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
114
115 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
116 cerr << "Error linking in '" << InputFilenames[i] << "': "
117 << ErrorMessage << endl;
118 return 1;
119 }
120 }
121
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000122 // In addition to just parsing the input from GCC, we also want to spiff it up
123 // a little bit. Do this now.
124 //
125 PassManager Passes;
126
127 // Linking modules together can lead to duplicated global constants, only keep
128 // one copy of each constant...
129 //
130 Passes.add(createConstantMergePass());
131
132 // Often if the programmer does not specify proper prototypes for the
133 // functions they are calling, they end up calling a vararg version of the
134 // function that does not get a body filled in (the real function has typed
135 // arguments). This pass merges the two functions, among other things.
136 //
137 Passes.add(createCleanupGCCOutputPass());
138
Chris Lattner62b7fd12002-04-07 20:49:59 +0000139 // Now that composite has been compiled, scan through the module, looking for
140 // a main function. If main is defined, mark all other functions internal.
141 //
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000142 // TODO:
Chris Lattner62b7fd12002-04-07 20:49:59 +0000143
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000144 // Now that we have optimized the program, discard unreachable functions...
145 //
146 Passes.add(createGlobalDCEPass());
Chris Lattner62b7fd12002-04-07 20:49:59 +0000147
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000148 // Add the pass that writes bytecode to the output file...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000149 std::ofstream Out((OutputFilename+".bc").c_str());
150 if (!Out.good()) {
151 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
152 return 1;
153 }
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000154 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000155
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000156 // Run our queue of passes all at once now, efficiently.
157 Passes.run(Composite.get());
Chris Lattner5ff2e052002-01-24 19:12:12 +0000158 Out.close();
159
160 // Output the script to start the program...
161 std::ofstream Out2(OutputFilename.c_str());
162 if (!Out2.good()) {
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000163 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000164 return 1;
165 }
Chris Lattner6f0d4532002-03-11 17:49:53 +0000166 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000167 Out2.close();
168
169 // Make the script executable...
170 chmod(OutputFilename.c_str(), 0755);
171
172 return 0;
173}