blob: cde23f9d28f70c6e0e42914bd7999f3628f51322 [file] [log] [blame]
Chris Lattnere7fca512002-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"
18#include "llvm/Bytecode/Reader.h"
19#include "llvm/Bytecode/Writer.h"
20#include "llvm/Module.h"
21#include "llvm/Method.h"
22#include "Support/CommandLine.h"
23#include <fstream>
24#include <memory>
25#include <sys/types.h> // For FileExists
26#include <sys/stat.h>
27
28
29cl::StringList InputFilenames("", "Load <arg> files, linking them together",
30 cl::OneOrMore);
31cl::String OutputFilename("o", "Override output filename", cl::NoFlags,"a.out");
32cl::Flag Verbose ("v", "Print information about actions taken");
33cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
34cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
35
36
37// FileExists - Return true if the specified string is an openable file...
38static inline bool FileExists(const std::string &FN) {
39 struct stat StatBuf;
40 return stat(FN.c_str(), &StatBuf) != -1;
41}
42
43// LoadFile - Read the specified bytecode file in and return it. This routine
44// searches the link path for the specified file to try to find it...
45//
46static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
47 std::string Filename = FN;
48 std::string ErrorMessage;
49
50 unsigned NextLibPathIdx = 0;
51 bool FoundAFile = false;
52
53 while (1) {
54 if (Verbose) cerr << "Loading '" << Filename << "'\n";
55 if (FileExists(Filename)) FoundAFile = true;
56 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
57 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
58
59 if (Verbose) {
60 cerr << "Error opening bytecode file: '" << Filename << "'";
61 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
62 cerr << endl;
63 }
64
65 if (NextLibPathIdx == LibPaths.size()) break;
66 Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
67 }
68
69 if (FoundAFile)
70 cerr << "Bytecode file '" << FN << "' corrupt! "
71 << "Use 'link -v ...' for more info.\n";
72 else
73 cerr << "Could not locate bytecode file: '" << FN << "'\n";
74 return std::auto_ptr<Module>();
75}
76
77
78
79
80int main(int argc, char **argv) {
81 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n",
82 cl::EnableSingleLetterArgValue |
83 cl::DisableSingleLetterArgGrouping);
84 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
85
86 unsigned BaseArg = 0;
87 std::string ErrorMessage;
88
89 if (!Libraries.empty())
90 cerr << "LLVM Linker Warning: Linking to libraries is unimplemented!\n";
91
92 std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
93 if (Composite.get() == 0) return 1;
94
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
96 std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
97 if (M.get() == 0) return 1;
98
99 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
100
101 if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
102 cerr << "Error linking in '" << InputFilenames[i] << "': "
103 << ErrorMessage << endl;
104 return 1;
105 }
106 }
107
108 std::ofstream Out((OutputFilename+".bc").c_str());
109 if (!Out.good()) {
110 cerr << "Error opening '" << OutputFilename << ".bc' for writing!\n";
111 return 1;
112 }
113
114 if (Verbose) cerr << "Writing bytecode...\n";
115 WriteBytecodeToFile(Composite.get(), Out);
116 Out.close();
117
118 // Output the script to start the program...
119 std::ofstream Out2(OutputFilename.c_str());
120 if (!Out2.good()) {
121 cerr << "Error openeing '" << OutputFilename << "' for writing!\n";
122 return 1;
123 }
124 Out2 << "#!/bin/sh\nlli -q $0.bc\n";
125 Out2.close();
126
127 // Make the script executable...
128 chmod(OutputFilename.c_str(), 0755);
129
130 return 0;
131}