blob: a797488a43639417cdcba784fc1d9b8be58f8d4e [file] [log] [blame]
Chris Lattner10970eb2003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
Chris Lattnere7fca512002-01-24 19:12:12 +00002//
3// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner10970eb2003-04-19 22:44:38 +00004// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattnere7fca512002-01-24 19:12:12 +00005// Additionally, this program outputs a shell script that is used to invoke LLI
6// to execute the program. In this manner, the generated executable (a.out for
7// example), is directly executable, whereas the bytecode file actually lives in
8// the a.out.bc file generated by this program. Also, Force is on by default.
9//
10// Note that if someone (or a script) deletes the executable program generated,
11// the .bc file will be left around. Considering that this is a temporary hack,
Brian Gaeke69a79602003-05-23 20:27:07 +000012// I'm not too worried about this.
Chris Lattnere7fca512002-01-24 19:12:12 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000016#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000017#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000018#include "llvm/PassManager.h"
19#include "llvm/Bytecode/Reader.h"
20#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000021#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000022#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000023#include "llvm/Transforms/Scalar.h"
John Criswelld35b5b52003-09-02 20:17:20 +000024#include "Support/FileUtilities.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000025#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000026#include "Support/Signals.h"
John Criswell22edc392003-09-02 21:11:22 +000027#include "Config/unistd.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000028#include <fstream>
29#include <memory>
Chris Lattner10970eb2003-04-19 22:44:38 +000030#include <set>
Chris Lattner41c34652002-03-11 17:49:53 +000031#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000032
Chris Lattnerf3d4f172003-04-18 23:01:25 +000033namespace {
34 cl::list<std::string>
35 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
36 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Chris Lattnerf3d4f172003-04-18 23:01:25 +000038 cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
40 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Chris Lattnerf3d4f172003-04-18 23:01:25 +000042 cl::opt<bool>
43 Verbose("v", cl::desc("Print information about actions taken"));
44
45 cl::list<std::string>
46 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
47 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
Chris Lattnerf3d4f172003-04-18 23:01:25 +000049 cl::list<std::string>
50 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
51 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattnerf3d4f172003-04-18 23:01:25 +000053 cl::opt<bool>
54 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000055
Chris Lattnerf3d4f172003-04-18 23:01:25 +000056 cl::opt<bool>
57 NoInternalize("disable-internalize",
58 cl::desc("Do not mark all symbols as internal"));
Chris Lattnera2b2dc92003-08-22 19:18:45 +000059 static cl::alias
60 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
61 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000062
Chris Lattner10970eb2003-04-19 22:44:38 +000063 cl::opt<bool>
64 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
65 " library, not an executable"));
66
Chris Lattnera856db22003-04-18 23:38:22 +000067 // Compatibility options that are ignored, but support by LD
68 cl::opt<std::string>
69 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
70 cl::opt<std::string>
71 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
72 cl::opt<bool>
73 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattner6ac79d12003-05-27 19:15:11 +000074 cl::opt<bool>
75 CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000076}
Chris Lattnere7fca512002-01-24 19:12:12 +000077
78// FileExists - Return true if the specified string is an openable file...
79static inline bool FileExists(const std::string &FN) {
John Criswell22edc392003-09-02 21:11:22 +000080 return access(FN.c_str(), F_OK) != -1;
Chris Lattnere7fca512002-01-24 19:12:12 +000081}
82
Chris Lattnere7fca512002-01-24 19:12:12 +000083
Chris Lattner10970eb2003-04-19 22:44:38 +000084// LoadObject - Read the specified "object file", which should not search the
85// library path to find it.
Chris Lattner7cb77e12003-05-13 22:14:13 +000086static inline std::auto_ptr<Module> LoadObject(std::string FN,
Chris Lattner10970eb2003-04-19 22:44:38 +000087 std::string &OutErrorMessage) {
88 if (Verbose) std::cerr << "Loading '" << FN << "'\n";
89 if (!FileExists(FN)) {
Chris Lattner7cb77e12003-05-13 22:14:13 +000090 // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would
91 // otherwise fail. This is used to locate objects like crtend.o.
92 //
93 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
94 if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN))
95 FN = std::string(SearchPath)+"/"+FN;
96 else {
97 OutErrorMessage = "could not find input file '" + FN + "'!";
98 return std::auto_ptr<Module>();
99 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000100 }
101
Chris Lattner10970eb2003-04-19 22:44:38 +0000102 std::string ErrorMessage;
103 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
104 if (Result) return std::auto_ptr<Module>(Result);
105
106 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
107 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
Chris Lattnere7fca512002-01-24 19:12:12 +0000108 return std::auto_ptr<Module>();
109}
110
111
Chris Lattner10970eb2003-04-19 22:44:38 +0000112static Module *LoadSingleLibraryObject(const std::string &Filename) {
113 std::string ErrorMessage;
114 std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage);
115 if (M.get() == 0 && Verbose) {
116 std::cerr << "Error loading '" + Filename + "'";
117 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
118 std::cerr << "\n";
119 }
120
121 return M.release();
122}
123
Brian Gaeke69a79602003-05-23 20:27:07 +0000124// IsArchive - Returns true iff FILENAME appears to be the name of an ar
125// archive file. It determines this by checking the magic string at the
126// beginning of the file.
Chris Lattnere68e4d52003-05-29 15:13:15 +0000127static bool IsArchive(const std::string &filename) {
128 std::string ArchiveMagic("!<arch>\012");
129 char buf[1 + ArchiveMagic.size()];
130 std::ifstream f(filename.c_str());
131 f.read(buf, ArchiveMagic.size());
132 buf[ArchiveMagic.size()] = '\0';
133 return ArchiveMagic == buf;
Brian Gaeke69a79602003-05-23 20:27:07 +0000134}
Chris Lattner10970eb2003-04-19 22:44:38 +0000135
Brian Gaeke69a79602003-05-23 20:27:07 +0000136// LoadLibraryExactName - This looks for a file with a known name and tries to
137// load it, similarly to LoadLibraryFromDirectory().
Chris Lattnere68e4d52003-05-29 15:13:15 +0000138static inline bool LoadLibraryExactName(const std::string &FileName,
Brian Gaeke69a79602003-05-23 20:27:07 +0000139 std::vector<Module*> &Objects, bool &isArchive) {
140 if (Verbose) std::cerr << " Considering '" << FileName << "'\n";
141 if (FileExists(FileName)) {
Chris Lattnere68e4d52003-05-29 15:13:15 +0000142 if (IsArchive(FileName)) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000143 std::string ErrorMessage;
144 if (Verbose) std::cerr << " Loading '" << FileName << "'\n";
145 if (!ReadArchiveFile(FileName, Objects, &ErrorMessage)) {
146 isArchive = true;
147 return false; // Success!
148 }
149 if (Verbose) {
150 std::cerr << " Error loading archive '" + FileName + "'";
151 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
152 std::cerr << "\n";
153 }
154 } else {
155 if (Module *M = LoadSingleLibraryObject(FileName)) {
156 isArchive = false;
157 Objects.push_back(M);
158 return false;
159 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000160 }
161 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000162 return true;
163}
164
Brian Gaeke69a79602003-05-23 20:27:07 +0000165// LoadLibrary - Try to load a library named LIBNAME that contains
166// LLVM bytecode. If SEARCH is true, then search for a file named
167// libLIBNAME.{a,so,bc} in the current library search path. Otherwise,
168// assume LIBNAME is the real name of the library file. This method puts
169// the loaded modules into the Objects list, and sets isArchive to true if
170// a .a file was loaded. It returns true if no library is found or if an
171// error occurs; otherwise it returns false.
Chris Lattner10970eb2003-04-19 22:44:38 +0000172//
173static inline bool LoadLibrary(const std::string &LibName,
174 std::vector<Module*> &Objects, bool &isArchive,
Brian Gaeke69a79602003-05-23 20:27:07 +0000175 bool search, std::string &ErrorMessage) {
176 if (search) {
177 // First, try the current directory. Then, iterate over the
178 // directories in LibPaths, looking for a suitable match for LibName
179 // in each one.
180 for (unsigned NextLibPathIdx = 0; NextLibPathIdx != LibPaths.size();
Chris Lattnere68e4d52003-05-29 15:13:15 +0000181 ++NextLibPathIdx) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000182 std::string Directory = LibPaths[NextLibPathIdx] + "/";
183 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".a",
184 Objects, isArchive))
185 return false;
186 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".so",
187 Objects, isArchive))
188 return false;
189 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".bc",
190 Objects, isArchive))
191 return false;
192 }
193 } else {
194 // If they said no searching, then assume LibName is the real name.
195 if (!LoadLibraryExactName(LibName, Objects, isArchive))
Chris Lattner10970eb2003-04-19 22:44:38 +0000196 return false;
Chris Lattner10970eb2003-04-19 22:44:38 +0000197 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000198 ErrorMessage = "error linking library '-l" + LibName+ "': library not found!";
199 return true;
200}
201
202static void GetAllDefinedSymbols(Module *M,
203 std::set<std::string> &DefinedSymbols) {
204 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
205 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
206 DefinedSymbols.insert(I->getName());
207 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
208 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
209 DefinedSymbols.insert(I->getName());
210}
211
212// GetAllUndefinedSymbols - This calculates the set of undefined symbols that
213// still exist in an LLVM module. This is a bit tricky because there may be two
214// symbols with the same name, but different LLVM types that will be resolved to
215// each other, but aren't currently (thus we need to treat it as resolved).
216//
217static void GetAllUndefinedSymbols(Module *M,
218 std::set<std::string> &UndefinedSymbols) {
219 std::set<std::string> DefinedSymbols;
220 UndefinedSymbols.clear(); // Start out empty
221
222 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
223 if (I->hasName()) {
224 if (I->isExternal())
225 UndefinedSymbols.insert(I->getName());
226 else if (!I->hasInternalLinkage())
227 DefinedSymbols.insert(I->getName());
228 }
229 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
230 if (I->hasName()) {
231 if (I->isExternal())
232 UndefinedSymbols.insert(I->getName());
233 else if (!I->hasInternalLinkage())
234 DefinedSymbols.insert(I->getName());
235 }
236
237 // Prune out any defined symbols from the undefined symbols set...
238 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
239 I != UndefinedSymbols.end(); )
240 if (DefinedSymbols.count(*I))
241 UndefinedSymbols.erase(I++); // This symbol really is defined!
242 else
243 ++I; // Keep this symbol in the undefined symbols list
244}
245
246
247static bool LinkLibrary(Module *M, const std::string &LibName,
Brian Gaeke69a79602003-05-23 20:27:07 +0000248 bool search, std::string &ErrorMessage) {
Chris Lattner7cb77e12003-05-13 22:14:13 +0000249 std::set<std::string> UndefinedSymbols;
250 GetAllUndefinedSymbols(M, UndefinedSymbols);
251 if (UndefinedSymbols.empty()) {
252 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
253 return false; // No need to link anything in!
254 }
255
Chris Lattner10970eb2003-04-19 22:44:38 +0000256 std::vector<Module*> Objects;
257 bool isArchive;
Brian Gaeke69a79602003-05-23 20:27:07 +0000258 if (LoadLibrary(LibName, Objects, isArchive, search, ErrorMessage))
259 return true;
Chris Lattner10970eb2003-04-19 22:44:38 +0000260
261 // Figure out which symbols are defined by all of the modules in the .a file
262 std::vector<std::set<std::string> > DefinedSymbols;
263 DefinedSymbols.resize(Objects.size());
264 for (unsigned i = 0; i != Objects.size(); ++i)
265 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
266
Chris Lattner10970eb2003-04-19 22:44:38 +0000267 bool Linked = true;
268 while (Linked) { // While we are linking in object files, loop.
269 Linked = false;
270
271 for (unsigned i = 0; i != Objects.size(); ++i) {
272 // Consider whether we need to link in this module... we only need to
273 // link it in if it defines some symbol which is so far undefined.
274 //
275 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
276
277 bool ObjectRequired = false;
278 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
279 E = UndefinedSymbols.end(); I != E; ++I)
280 if (DefSymbols.count(*I)) {
281 if (Verbose)
282 std::cerr << " Found object providing symbol '" << *I << "'...\n";
283 ObjectRequired = true;
284 break;
285 }
286
287 // We DO need to link this object into the program...
288 if (ObjectRequired) {
289 if (LinkModules(M, Objects[i], &ErrorMessage))
290 return true; // Couldn't link in the right object file...
291
292 // Since we have linked in this object, delete it from the list of
293 // objects to consider in this archive file.
294 std::swap(Objects[i], Objects.back());
295 std::swap(DefinedSymbols[i], DefinedSymbols.back());
296 Objects.pop_back();
297 DefinedSymbols.pop_back();
298 --i; // Do not skip an entry
299
300 // The undefined symbols set should have shrunk.
301 GetAllUndefinedSymbols(M, UndefinedSymbols);
302 Linked = true; // We have linked something in!
303 }
304 }
305 }
306
307 return false;
308}
309
310static int PrintAndReturn(const char *progname, const std::string &Message,
311 const std::string &Extra = "") {
312 std::cerr << progname << Extra << ": " << Message << "\n";
313 return 1;
314}
315
316
Chris Lattnere7fca512002-01-24 19:12:12 +0000317int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000318 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000319
Chris Lattnere7fca512002-01-24 19:12:12 +0000320 std::string ErrorMessage;
Chris Lattner10970eb2003-04-19 22:44:38 +0000321 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
322 if (Composite.get() == 0)
323 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000324
Brian Gaeke69a79602003-05-23 20:27:07 +0000325 // We always look first in the current directory when searching for libraries.
326 LibPaths.insert(LibPaths.begin(), ".");
327
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000328 // If the user specied an extra search path in their environment, respect it.
329 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
330 LibPaths.push_back(SearchPath);
331
Chris Lattner10970eb2003-04-19 22:44:38 +0000332 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000333 // A user may specify an ar archive without -l, perhaps because it
334 // is not installed as a library. Detect that and link the library.
Chris Lattnere68e4d52003-05-29 15:13:15 +0000335 if (IsArchive(InputFilenames[i])) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000336 if (Verbose) std::cerr << "Linking archive '" << InputFilenames[i]
337 << "'\n";
Chris Lattnere68e4d52003-05-29 15:13:15 +0000338 if (LinkLibrary(Composite.get(), InputFilenames[i], false, ErrorMessage))
Brian Gaeke69a79602003-05-23 20:27:07 +0000339 return PrintAndReturn(argv[0], ErrorMessage,
340 ": error linking in '" + InputFilenames[i] + "'");
341 continue;
342 }
343
Chris Lattner10970eb2003-04-19 22:44:38 +0000344 std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage));
345 if (M.get() == 0)
346 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000347
Chris Lattnerf3d4f172003-04-18 23:01:25 +0000348 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000349
Chris Lattner10970eb2003-04-19 22:44:38 +0000350 if (LinkModules(Composite.get(), M.get(), &ErrorMessage))
351 return PrintAndReturn(argv[0], ErrorMessage,
352 ": error linking in '" + InputFilenames[i] + "'");
353 }
354
Chris Lattnerc65b1042003-04-19 23:07:33 +0000355 // Remove any consecutive duplicates of the same library...
356 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
357 Libraries.end());
358
Chris Lattner10970eb2003-04-19 22:44:38 +0000359 // Link in all of the libraries next...
360 for (unsigned i = 0; i != Libraries.size(); ++i) {
361 if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n";
Brian Gaeke69a79602003-05-23 20:27:07 +0000362 if (LinkLibrary(Composite.get(), Libraries[i], true, ErrorMessage))
Chris Lattner10970eb2003-04-19 22:44:38 +0000363 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000364 }
365
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000366 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattnerad202a02002-04-08 00:14:58 +0000367 // a little bit. Do this now.
368 //
369 PassManager Passes;
370
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000371 // Add an appropriate TargetData instance for this module...
Chris Lattner80df4632003-08-15 04:56:09 +0000372 Passes.add(new TargetData("gccld", Composite.get()));
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000373
Chris Lattnerad202a02002-04-08 00:14:58 +0000374 // Linking modules together can lead to duplicated global constants, only keep
375 // one copy of each constant...
376 //
377 Passes.add(createConstantMergePass());
378
Chris Lattner2b598372002-04-08 05:18:12 +0000379 // If the -s command line option was specified, strip the symbols out of the
380 // resulting program to make it smaller. -s is a GCC option that we are
381 // supporting.
382 //
383 if (Strip)
384 Passes.add(createSymbolStrippingPass());
385
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000386 // Often if the programmer does not specify proper prototypes for the
387 // functions they are calling, they end up calling a vararg version of the
388 // function that does not get a body filled in (the real function has typed
389 // arguments). This pass merges the two functions.
390 //
391 Passes.add(createFunctionResolvingPass());
392
Chris Lattnerdabaa462003-04-16 21:43:22 +0000393 if (!NoInternalize) {
394 // Now that composite has been compiled, scan through the module, looking
395 // for a main function. If main is defined, mark all other functions
396 // internal.
397 //
398 Passes.add(createInternalizePass());
399 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000400
Chris Lattnera34f4402003-06-18 16:29:02 +0000401 // Remove unused arguments from functions...
402 //
403 Passes.add(createDeadArgEliminationPass());
404
Chris Lattnerdccb6d02003-06-19 17:03:51 +0000405 // The FuncResolve pass may leave cruft around if functions were prototyped
406 // differently than they were defined. Remove this cruft.
407 //
408 Passes.add(createInstructionCombiningPass());
409
Chris Lattnerdc523532003-06-26 05:29:50 +0000410 // Delete basic blocks, which optimization passes may have killed...
411 //
412 Passes.add(createCFGSimplificationPass());
413
Chris Lattner293a33a2003-06-26 04:32:31 +0000414 // Now that we have optimized the program, discard unreachable functions...
415 //
416 Passes.add(createGlobalDCEPass());
417
Chris Lattnerad202a02002-04-08 00:14:58 +0000418 // Add the pass that writes bytecode to the output file...
Chris Lattner10970eb2003-04-19 22:44:38 +0000419 std::string RealBytecodeOutput = OutputFilename;
420 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
421 std::ofstream Out(RealBytecodeOutput.c_str());
422 if (!Out.good())
423 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
424 "' for writing!");
Chris Lattnerad202a02002-04-08 00:14:58 +0000425 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000426
Chris Lattner76d12292002-04-18 19:55:25 +0000427 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
Chris Lattner10970eb2003-04-19 22:44:38 +0000428 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000429
Chris Lattnerad202a02002-04-08 00:14:58 +0000430 // Run our queue of passes all at once now, efficiently.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000431 Passes.run(*Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000432 Out.close();
433
Chris Lattner10970eb2003-04-19 22:44:38 +0000434 if (!LinkAsLibrary) {
435 // Output the script to start the program...
436 std::ofstream Out2(OutputFilename.c_str());
437 if (!Out2.good())
438 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
439 "' for writing!");
440 Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n";
441 Out2.close();
Chris Lattnere7fca512002-01-24 19:12:12 +0000442
Chris Lattner10970eb2003-04-19 22:44:38 +0000443 // Make the script executable...
John Criswelld35b5b52003-09-02 20:17:20 +0000444 MakeFileExecutable (OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000445
John Criswell22edc392003-09-02 21:11:22 +0000446 // Make the bytecode file readable and directly executable in LLEE as well
John Criswelld35b5b52003-09-02 20:17:20 +0000447 MakeFileExecutable (RealBytecodeOutput);
John Criswell22edc392003-09-02 21:11:22 +0000448 MakeFileReadable (RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000449 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000450
451 return 0;
452}