blob: e5db8849ddcddb41900247a59f8b72d180cb508d [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
Chris Lattner7608a462002-05-07 18:36:35 +000017#include "llvm/Transforms/Utils/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"
Chris Lattner89a20ef2002-05-07 20:03:27 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner3b08c2f2002-04-08 00:14:58 +000025#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattnerf0f014a2002-04-28 05:49:45 +000026#include "llvm/Transforms/IPO/Internalize.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000027#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000028#include "Support/Signals.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000029#include <fstream>
30#include <memory>
Chris Lattner6f0d4532002-03-11 17:49:53 +000031#include <algorithm>
Chris Lattner5ff2e052002-01-24 19:12:12 +000032#include <sys/types.h> // For FileExists
33#include <sys/stat.h>
Anand Shuklafef32412002-06-25 21:57:48 +000034using std::cerr;
Chris Lattner5ff2e052002-01-24 19:12:12 +000035
Chris Lattnerf5cad152002-07-22 02:10:13 +000036static cl::list<string>
37InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
38 cl::OneOrMore);
39
40static cl::opt<string>
41OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
42 cl::value_desc("filename"));
43
44static cl::opt<bool>
45Verbose("v", cl::desc("Print information about actions taken"));
46
47static cl::list<string>
48LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
49 cl::value_desc("directory"));
50
51static cl::list<string>
52Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
53 cl::value_desc("library prefix"));
54
55static cl::opt<bool>
56Strip("s", cl::desc("Strip symbol info from executable"));
57
Chris Lattner5ff2e052002-01-24 19:12:12 +000058
59// FileExists - Return true if the specified string is an openable file...
60static inline bool FileExists(const std::string &FN) {
61 struct stat StatBuf;
62 return stat(FN.c_str(), &StatBuf) != -1;
63}
64
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...
67//
68static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
69 std::string Filename = FN;
70 std::string ErrorMessage;
71
72 unsigned NextLibPathIdx = 0;
73 bool FoundAFile = false;
74
75 while (1) {
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!
80
81 if (Verbose) {
82 cerr << "Error opening bytecode file: '" << Filename << "'";
83 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
Chris Lattnerc19be162002-06-30 16:20:02 +000084 cerr << "\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +000085 }
86
87 if (NextLibPathIdx == LibPaths.size()) break;
88 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
89 }
90
91 if (FoundAFile)
92 cerr << "Bytecode file '" << FN << "' corrupt! "
Chris Lattnere6975042002-03-12 15:41:36 +000093 << "Use 'gccld -v ...' for more info.\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +000094 else
95 cerr << "Could not locate bytecode file: '" << FN << "'\n";
96 return std::auto_ptr<Module>();
97}
98
99
Chris Lattner5ff2e052002-01-24 19:12:12 +0000100int main(int argc, char **argv) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000101 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000102
103 unsigned BaseArg = 0;
104 std::string ErrorMessage;
105
Chris Lattner6f0d4532002-03-11 17:49:53 +0000106 if (!Libraries.empty()) {
107 // Sort libraries list...
Chris Lattnerc19be162002-06-30 16:20:02 +0000108 std::sort(Libraries.begin(), Libraries.end());
Chris Lattner6f0d4532002-03-11 17:49:53 +0000109
110 // Remove duplicate libraries entries...
111 Libraries.erase(unique(Libraries.begin(), Libraries.end()),
112 Libraries.end());
113
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");
117 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000118
119 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
120 if (Composite.get() == 0) return 1;
121
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;
125
126 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
127
128 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
129 cerr << "Error linking in '" << InputFilenames[i] << "': "
Chris Lattner2df953b2002-05-20 19:49:24 +0000130 << ErrorMessage << "\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000131 return 1;
132 }
133 }
134
Chris Lattnerc34061f2002-04-10 20:37:47 +0000135 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000136 // a little bit. Do this now.
137 //
138 PassManager Passes;
139
140 // Linking modules together can lead to duplicated global constants, only keep
141 // one copy of each constant...
142 //
143 Passes.add(createConstantMergePass());
144
Chris Lattner2b33d752002-04-08 05:18:12 +0000145 // 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
147 // supporting.
148 //
149 if (Strip)
150 Passes.add(createSymbolStrippingPass());
151
Chris Lattnerc34061f2002-04-10 20:37:47 +0000152 // 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.
156 //
157 Passes.add(createFunctionResolvingPass());
158
Chris Lattner62b7fd12002-04-07 20:49:59 +0000159 // 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.
161 //
Chris Lattnerf0f014a2002-04-28 05:49:45 +0000162 Passes.add(createInternalizePass());
Chris Lattner62b7fd12002-04-07 20:49:59 +0000163
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000164 // Now that we have optimized the program, discard unreachable functions...
165 //
166 Passes.add(createGlobalDCEPass());
Chris Lattner62b7fd12002-04-07 20:49:59 +0000167
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000168 // Add the pass that writes bytecode to the output file...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000169 std::ofstream Out((OutputFilename+".bc").c_str());
170 if (!Out.good()) {
171 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
172 return 1;
173 }
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000174 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000175
Chris Lattnerc065ad82002-04-18 19:55:25 +0000176 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
177 RemoveFileOnSignal(OutputFilename+".bc");
178
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000179 // Run our queue of passes all at once now, efficiently.
Chris Lattner7076ff22002-06-25 16:13:21 +0000180 Passes.run(*Composite.get());
Chris Lattner5ff2e052002-01-24 19:12:12 +0000181 Out.close();
182
183 // Output the script to start the program...
184 std::ofstream Out2(OutputFilename.c_str());
185 if (!Out2.good()) {
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000186 cerr << "Error opening '" << OutputFilename << "' for writing!\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000187 return 1;
188 }
Chris Lattner6f0d4532002-03-11 17:49:53 +0000189 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000190 Out2.close();
191
192 // Make the script executable...
193 chmod(OutputFilename.c_str(), 0755);
194
195 return 0;
196}