blob: 1fd1b741fac6347e4afc9b54022f23118591d43c [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"
Chris Lattnere7fca512002-01-24 19:12:12 +000027#include <fstream>
28#include <memory>
Chris Lattner10970eb2003-04-19 22:44:38 +000029#include <set>
Chris Lattner41c34652002-03-11 17:49:53 +000030#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000031#include <sys/types.h> // For FileExists
32#include <sys/stat.h>
Chris Lattnere7fca512002-01-24 19:12:12 +000033
Chris Lattnerf3d4f172003-04-18 23:01:25 +000034namespace {
35 cl::list<std::string>
36 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
37 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000038
Chris Lattnerf3d4f172003-04-18 23:01:25 +000039 cl::opt<std::string>
40 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
41 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
Chris Lattnerf3d4f172003-04-18 23:01:25 +000043 cl::opt<bool>
44 Verbose("v", cl::desc("Print information about actions taken"));
45
46 cl::list<std::string>
47 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
48 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
Chris Lattnerf3d4f172003-04-18 23:01:25 +000050 cl::list<std::string>
51 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
52 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000053
Chris Lattnerf3d4f172003-04-18 23:01:25 +000054 cl::opt<bool>
55 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000056
Chris Lattnerf3d4f172003-04-18 23:01:25 +000057 cl::opt<bool>
58 NoInternalize("disable-internalize",
59 cl::desc("Do not mark all symbols as internal"));
Chris Lattnera2b2dc92003-08-22 19:18:45 +000060 static cl::alias
61 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
62 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000063
Chris Lattner10970eb2003-04-19 22:44:38 +000064 cl::opt<bool>
65 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
66 " library, not an executable"));
67
Chris Lattnera856db22003-04-18 23:38:22 +000068 // Compatibility options that are ignored, but support by LD
69 cl::opt<std::string>
70 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
71 cl::opt<std::string>
72 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
73 cl::opt<bool>
74 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattner6ac79d12003-05-27 19:15:11 +000075 cl::opt<bool>
76 CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000077}
Chris Lattnere7fca512002-01-24 19:12:12 +000078
79// FileExists - Return true if the specified string is an openable file...
80static inline bool FileExists(const std::string &FN) {
81 struct stat StatBuf;
82 return stat(FN.c_str(), &StatBuf) != -1;
83}
84
Chris Lattnere7fca512002-01-24 19:12:12 +000085
Chris Lattner10970eb2003-04-19 22:44:38 +000086// LoadObject - Read the specified "object file", which should not search the
87// library path to find it.
Chris Lattner7cb77e12003-05-13 22:14:13 +000088static inline std::auto_ptr<Module> LoadObject(std::string FN,
Chris Lattner10970eb2003-04-19 22:44:38 +000089 std::string &OutErrorMessage) {
90 if (Verbose) std::cerr << "Loading '" << FN << "'\n";
91 if (!FileExists(FN)) {
Chris Lattner7cb77e12003-05-13 22:14:13 +000092 // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would
93 // otherwise fail. This is used to locate objects like crtend.o.
94 //
95 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
96 if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN))
97 FN = std::string(SearchPath)+"/"+FN;
98 else {
99 OutErrorMessage = "could not find input file '" + FN + "'!";
100 return std::auto_ptr<Module>();
101 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000102 }
103
Chris Lattner10970eb2003-04-19 22:44:38 +0000104 std::string ErrorMessage;
105 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
106 if (Result) return std::auto_ptr<Module>(Result);
107
108 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
109 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
Chris Lattnere7fca512002-01-24 19:12:12 +0000110 return std::auto_ptr<Module>();
111}
112
113
Chris Lattner10970eb2003-04-19 22:44:38 +0000114static Module *LoadSingleLibraryObject(const std::string &Filename) {
115 std::string ErrorMessage;
116 std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage);
117 if (M.get() == 0 && Verbose) {
118 std::cerr << "Error loading '" + Filename + "'";
119 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
120 std::cerr << "\n";
121 }
122
123 return M.release();
124}
125
Brian Gaeke69a79602003-05-23 20:27:07 +0000126// IsArchive - Returns true iff FILENAME appears to be the name of an ar
127// archive file. It determines this by checking the magic string at the
128// beginning of the file.
Chris Lattnere68e4d52003-05-29 15:13:15 +0000129static bool IsArchive(const std::string &filename) {
130 std::string ArchiveMagic("!<arch>\012");
131 char buf[1 + ArchiveMagic.size()];
132 std::ifstream f(filename.c_str());
133 f.read(buf, ArchiveMagic.size());
134 buf[ArchiveMagic.size()] = '\0';
135 return ArchiveMagic == buf;
Brian Gaeke69a79602003-05-23 20:27:07 +0000136}
Chris Lattner10970eb2003-04-19 22:44:38 +0000137
Brian Gaeke69a79602003-05-23 20:27:07 +0000138// LoadLibraryExactName - This looks for a file with a known name and tries to
139// load it, similarly to LoadLibraryFromDirectory().
Chris Lattnere68e4d52003-05-29 15:13:15 +0000140static inline bool LoadLibraryExactName(const std::string &FileName,
Brian Gaeke69a79602003-05-23 20:27:07 +0000141 std::vector<Module*> &Objects, bool &isArchive) {
142 if (Verbose) std::cerr << " Considering '" << FileName << "'\n";
143 if (FileExists(FileName)) {
Chris Lattnere68e4d52003-05-29 15:13:15 +0000144 if (IsArchive(FileName)) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000145 std::string ErrorMessage;
146 if (Verbose) std::cerr << " Loading '" << FileName << "'\n";
147 if (!ReadArchiveFile(FileName, Objects, &ErrorMessage)) {
148 isArchive = true;
149 return false; // Success!
150 }
151 if (Verbose) {
152 std::cerr << " Error loading archive '" + FileName + "'";
153 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
154 std::cerr << "\n";
155 }
156 } else {
157 if (Module *M = LoadSingleLibraryObject(FileName)) {
158 isArchive = false;
159 Objects.push_back(M);
160 return false;
161 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000162 }
163 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000164 return true;
165}
166
Brian Gaeke69a79602003-05-23 20:27:07 +0000167// LoadLibrary - Try to load a library named LIBNAME that contains
168// LLVM bytecode. If SEARCH is true, then search for a file named
169// libLIBNAME.{a,so,bc} in the current library search path. Otherwise,
170// assume LIBNAME is the real name of the library file. This method puts
171// the loaded modules into the Objects list, and sets isArchive to true if
172// a .a file was loaded. It returns true if no library is found or if an
173// error occurs; otherwise it returns false.
Chris Lattner10970eb2003-04-19 22:44:38 +0000174//
175static inline bool LoadLibrary(const std::string &LibName,
176 std::vector<Module*> &Objects, bool &isArchive,
Brian Gaeke69a79602003-05-23 20:27:07 +0000177 bool search, std::string &ErrorMessage) {
178 if (search) {
179 // First, try the current directory. Then, iterate over the
180 // directories in LibPaths, looking for a suitable match for LibName
181 // in each one.
182 for (unsigned NextLibPathIdx = 0; NextLibPathIdx != LibPaths.size();
Chris Lattnere68e4d52003-05-29 15:13:15 +0000183 ++NextLibPathIdx) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000184 std::string Directory = LibPaths[NextLibPathIdx] + "/";
185 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".a",
186 Objects, isArchive))
187 return false;
188 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".so",
189 Objects, isArchive))
190 return false;
191 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".bc",
192 Objects, isArchive))
193 return false;
194 }
195 } else {
196 // If they said no searching, then assume LibName is the real name.
197 if (!LoadLibraryExactName(LibName, Objects, isArchive))
Chris Lattner10970eb2003-04-19 22:44:38 +0000198 return false;
Chris Lattner10970eb2003-04-19 22:44:38 +0000199 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000200 ErrorMessage = "error linking library '-l" + LibName+ "': library not found!";
201 return true;
202}
203
204static void GetAllDefinedSymbols(Module *M,
205 std::set<std::string> &DefinedSymbols) {
206 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
207 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
208 DefinedSymbols.insert(I->getName());
209 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
210 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
211 DefinedSymbols.insert(I->getName());
212}
213
214// GetAllUndefinedSymbols - This calculates the set of undefined symbols that
215// still exist in an LLVM module. This is a bit tricky because there may be two
216// symbols with the same name, but different LLVM types that will be resolved to
217// each other, but aren't currently (thus we need to treat it as resolved).
218//
219static void GetAllUndefinedSymbols(Module *M,
220 std::set<std::string> &UndefinedSymbols) {
221 std::set<std::string> DefinedSymbols;
222 UndefinedSymbols.clear(); // Start out empty
223
224 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
225 if (I->hasName()) {
226 if (I->isExternal())
227 UndefinedSymbols.insert(I->getName());
228 else if (!I->hasInternalLinkage())
229 DefinedSymbols.insert(I->getName());
230 }
231 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
232 if (I->hasName()) {
233 if (I->isExternal())
234 UndefinedSymbols.insert(I->getName());
235 else if (!I->hasInternalLinkage())
236 DefinedSymbols.insert(I->getName());
237 }
238
239 // Prune out any defined symbols from the undefined symbols set...
240 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
241 I != UndefinedSymbols.end(); )
242 if (DefinedSymbols.count(*I))
243 UndefinedSymbols.erase(I++); // This symbol really is defined!
244 else
245 ++I; // Keep this symbol in the undefined symbols list
246}
247
248
249static bool LinkLibrary(Module *M, const std::string &LibName,
Brian Gaeke69a79602003-05-23 20:27:07 +0000250 bool search, std::string &ErrorMessage) {
Chris Lattner7cb77e12003-05-13 22:14:13 +0000251 std::set<std::string> UndefinedSymbols;
252 GetAllUndefinedSymbols(M, UndefinedSymbols);
253 if (UndefinedSymbols.empty()) {
254 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
255 return false; // No need to link anything in!
256 }
257
Chris Lattner10970eb2003-04-19 22:44:38 +0000258 std::vector<Module*> Objects;
259 bool isArchive;
Brian Gaeke69a79602003-05-23 20:27:07 +0000260 if (LoadLibrary(LibName, Objects, isArchive, search, ErrorMessage))
261 return true;
Chris Lattner10970eb2003-04-19 22:44:38 +0000262
263 // Figure out which symbols are defined by all of the modules in the .a file
264 std::vector<std::set<std::string> > DefinedSymbols;
265 DefinedSymbols.resize(Objects.size());
266 for (unsigned i = 0; i != Objects.size(); ++i)
267 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
268
Chris Lattner10970eb2003-04-19 22:44:38 +0000269 bool Linked = true;
270 while (Linked) { // While we are linking in object files, loop.
271 Linked = false;
272
273 for (unsigned i = 0; i != Objects.size(); ++i) {
274 // Consider whether we need to link in this module... we only need to
275 // link it in if it defines some symbol which is so far undefined.
276 //
277 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
278
279 bool ObjectRequired = false;
280 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
281 E = UndefinedSymbols.end(); I != E; ++I)
282 if (DefSymbols.count(*I)) {
283 if (Verbose)
284 std::cerr << " Found object providing symbol '" << *I << "'...\n";
285 ObjectRequired = true;
286 break;
287 }
288
289 // We DO need to link this object into the program...
290 if (ObjectRequired) {
291 if (LinkModules(M, Objects[i], &ErrorMessage))
292 return true; // Couldn't link in the right object file...
293
294 // Since we have linked in this object, delete it from the list of
295 // objects to consider in this archive file.
296 std::swap(Objects[i], Objects.back());
297 std::swap(DefinedSymbols[i], DefinedSymbols.back());
298 Objects.pop_back();
299 DefinedSymbols.pop_back();
300 --i; // Do not skip an entry
301
302 // The undefined symbols set should have shrunk.
303 GetAllUndefinedSymbols(M, UndefinedSymbols);
304 Linked = true; // We have linked something in!
305 }
306 }
307 }
308
309 return false;
310}
311
312static int PrintAndReturn(const char *progname, const std::string &Message,
313 const std::string &Extra = "") {
314 std::cerr << progname << Extra << ": " << Message << "\n";
315 return 1;
316}
317
318
Chris Lattnere7fca512002-01-24 19:12:12 +0000319int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000320 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000321
Chris Lattnere7fca512002-01-24 19:12:12 +0000322 std::string ErrorMessage;
Chris Lattner10970eb2003-04-19 22:44:38 +0000323 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
324 if (Composite.get() == 0)
325 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000326
Brian Gaeke69a79602003-05-23 20:27:07 +0000327 // We always look first in the current directory when searching for libraries.
328 LibPaths.insert(LibPaths.begin(), ".");
329
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000330 // If the user specied an extra search path in their environment, respect it.
331 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
332 LibPaths.push_back(SearchPath);
333
Chris Lattner10970eb2003-04-19 22:44:38 +0000334 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000335 // A user may specify an ar archive without -l, perhaps because it
336 // is not installed as a library. Detect that and link the library.
Chris Lattnere68e4d52003-05-29 15:13:15 +0000337 if (IsArchive(InputFilenames[i])) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000338 if (Verbose) std::cerr << "Linking archive '" << InputFilenames[i]
339 << "'\n";
Chris Lattnere68e4d52003-05-29 15:13:15 +0000340 if (LinkLibrary(Composite.get(), InputFilenames[i], false, ErrorMessage))
Brian Gaeke69a79602003-05-23 20:27:07 +0000341 return PrintAndReturn(argv[0], ErrorMessage,
342 ": error linking in '" + InputFilenames[i] + "'");
343 continue;
344 }
345
Chris Lattner10970eb2003-04-19 22:44:38 +0000346 std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage));
347 if (M.get() == 0)
348 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000349
Chris Lattnerf3d4f172003-04-18 23:01:25 +0000350 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000351
Chris Lattner10970eb2003-04-19 22:44:38 +0000352 if (LinkModules(Composite.get(), M.get(), &ErrorMessage))
353 return PrintAndReturn(argv[0], ErrorMessage,
354 ": error linking in '" + InputFilenames[i] + "'");
355 }
356
Chris Lattnerc65b1042003-04-19 23:07:33 +0000357 // Remove any consecutive duplicates of the same library...
358 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
359 Libraries.end());
360
Chris Lattner10970eb2003-04-19 22:44:38 +0000361 // Link in all of the libraries next...
362 for (unsigned i = 0; i != Libraries.size(); ++i) {
363 if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n";
Brian Gaeke69a79602003-05-23 20:27:07 +0000364 if (LinkLibrary(Composite.get(), Libraries[i], true, ErrorMessage))
Chris Lattner10970eb2003-04-19 22:44:38 +0000365 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000366 }
367
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000368 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattnerad202a02002-04-08 00:14:58 +0000369 // a little bit. Do this now.
370 //
371 PassManager Passes;
372
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000373 // Add an appropriate TargetData instance for this module...
Chris Lattner80df4632003-08-15 04:56:09 +0000374 Passes.add(new TargetData("gccld", Composite.get()));
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000375
Chris Lattnerad202a02002-04-08 00:14:58 +0000376 // Linking modules together can lead to duplicated global constants, only keep
377 // one copy of each constant...
378 //
379 Passes.add(createConstantMergePass());
380
Chris Lattner2b598372002-04-08 05:18:12 +0000381 // If the -s command line option was specified, strip the symbols out of the
382 // resulting program to make it smaller. -s is a GCC option that we are
383 // supporting.
384 //
385 if (Strip)
386 Passes.add(createSymbolStrippingPass());
387
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000388 // Often if the programmer does not specify proper prototypes for the
389 // functions they are calling, they end up calling a vararg version of the
390 // function that does not get a body filled in (the real function has typed
391 // arguments). This pass merges the two functions.
392 //
393 Passes.add(createFunctionResolvingPass());
394
Chris Lattnerdabaa462003-04-16 21:43:22 +0000395 if (!NoInternalize) {
396 // Now that composite has been compiled, scan through the module, looking
397 // for a main function. If main is defined, mark all other functions
398 // internal.
399 //
400 Passes.add(createInternalizePass());
401 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000402
Chris Lattnera34f4402003-06-18 16:29:02 +0000403 // Remove unused arguments from functions...
404 //
405 Passes.add(createDeadArgEliminationPass());
406
Chris Lattnerdccb6d02003-06-19 17:03:51 +0000407 // The FuncResolve pass may leave cruft around if functions were prototyped
408 // differently than they were defined. Remove this cruft.
409 //
410 Passes.add(createInstructionCombiningPass());
411
Chris Lattnerdc523532003-06-26 05:29:50 +0000412 // Delete basic blocks, which optimization passes may have killed...
413 //
414 Passes.add(createCFGSimplificationPass());
415
Chris Lattner293a33a2003-06-26 04:32:31 +0000416 // Now that we have optimized the program, discard unreachable functions...
417 //
418 Passes.add(createGlobalDCEPass());
419
Chris Lattnerad202a02002-04-08 00:14:58 +0000420 // Add the pass that writes bytecode to the output file...
Chris Lattner10970eb2003-04-19 22:44:38 +0000421 std::string RealBytecodeOutput = OutputFilename;
422 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
423 std::ofstream Out(RealBytecodeOutput.c_str());
424 if (!Out.good())
425 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
426 "' for writing!");
Chris Lattnerad202a02002-04-08 00:14:58 +0000427 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000428
Chris Lattner76d12292002-04-18 19:55:25 +0000429 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
Chris Lattner10970eb2003-04-19 22:44:38 +0000430 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000431
Chris Lattnerad202a02002-04-08 00:14:58 +0000432 // Run our queue of passes all at once now, efficiently.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000433 Passes.run(*Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000434 Out.close();
435
Chris Lattner10970eb2003-04-19 22:44:38 +0000436 if (!LinkAsLibrary) {
437 // Output the script to start the program...
438 std::ofstream Out2(OutputFilename.c_str());
439 if (!Out2.good())
440 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
441 "' for writing!");
442 Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n";
443 Out2.close();
Chris Lattnere7fca512002-01-24 19:12:12 +0000444
Chris Lattner10970eb2003-04-19 22:44:38 +0000445 // Make the script executable...
John Criswelld35b5b52003-09-02 20:17:20 +0000446 MakeFileExecutable (OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000447
448 // Make the bytecode file directly executable in LLEE as well
John Criswelld35b5b52003-09-02 20:17:20 +0000449 MakeFileExecutable (RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000450 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000451
452 return 0;
453}