1 //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
3 // This utility is intended to be compatible with GCC, and follows standard
4 // system 'ld' conventions. As such, the default output file is ./a.out.
5 // 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.
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,
12 // I'm not too worried about this.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/Linker.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "llvm/Bytecode/WriteBytecodePass.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Transforms/IPO.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "Support/FileUtilities.h"
25 #include "Support/SystemUtils.h"
26 #include "Support/CommandLine.h"
27 #include "Support/Signals.h"
28 #include "Config/unistd.h"
37 // External function prototypes
40 GenerateBytecode (Module * M,
46 generate_assembly (std::string OutputFilename,
47 std::string InputFilename,
51 generate_native (std::string OutputFilename,
52 std::string InputFilename,
53 std::vector<std::string> Libraries,
59 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
63 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
64 cl::value_desc("filename"));
67 Verbose("v", cl::desc("Print information about actions taken"));
70 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
71 cl::value_desc("directory"));
74 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
75 cl::value_desc("library prefix"));
78 Strip("s", cl::desc("Strip symbol info from executable"));
81 NoInternalize("disable-internalize",
82 cl::desc("Do not mark all symbols as internal"));
84 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
85 cl::aliasopt(NoInternalize));
88 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
89 " library, not an executable"));
92 Native("native", cl::desc("Generate a native binary instead of a shell script"));
94 // Compatibility options that are ignored but supported by LD
96 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
98 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
100 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
102 CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored"));
105 // FileExists - Return true if the specified string is an openable file...
106 static inline bool FileExists(const std::string &FN) {
107 return access(FN.c_str(), F_OK) != -1;
111 // LoadObject - Read the specified "object file", which should not search the
112 // library path to find it.
113 static inline std::auto_ptr<Module> LoadObject(std::string FN,
114 std::string &OutErrorMessage) {
115 if (Verbose) std::cerr << "Loading '" << FN << "'\n";
116 if (!FileExists(FN)) {
117 // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would
118 // otherwise fail. This is used to locate objects like crtend.o.
120 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
121 if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN))
122 FN = std::string(SearchPath)+"/"+FN;
124 OutErrorMessage = "could not find input file '" + FN + "'!";
125 return std::auto_ptr<Module>();
129 std::string ErrorMessage;
130 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
131 if (Result) return std::auto_ptr<Module>(Result);
133 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
134 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
135 return std::auto_ptr<Module>();
139 static Module *LoadSingleLibraryObject(const std::string &Filename) {
140 std::string ErrorMessage;
141 std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage);
142 if (M.get() == 0 && Verbose) {
143 std::cerr << "Error loading '" + Filename + "'";
144 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
151 // LoadLibraryExactName - This looks for a file with a known name and tries to
152 // load it, similarly to LoadLibraryFromDirectory().
153 static inline bool LoadLibraryExactName(const std::string &FileName,
154 std::vector<Module*> &Objects, bool &isArchive) {
155 if (Verbose) std::cerr << " Considering '" << FileName << "'\n";
156 if (FileExists(FileName)) {
157 if (IsArchive(FileName)) {
158 std::string ErrorMessage;
159 if (Verbose) std::cerr << " Loading '" << FileName << "'\n";
160 if (!ReadArchiveFile(FileName, Objects, &ErrorMessage)) {
162 return false; // Success!
165 std::cerr << " Error loading archive '" + FileName + "'";
166 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
170 if (Module *M = LoadSingleLibraryObject(FileName)) {
172 Objects.push_back(M);
180 // LoadLibrary - Try to load a library named LIBNAME that contains
181 // LLVM bytecode. If SEARCH is true, then search for a file named
182 // libLIBNAME.{a,so,bc} in the current library search path. Otherwise,
183 // assume LIBNAME is the real name of the library file. This method puts
184 // the loaded modules into the Objects list, and sets isArchive to true if
185 // a .a file was loaded. It returns true if no library is found or if an
186 // error occurs; otherwise it returns false.
188 static inline bool LoadLibrary(const std::string &LibName,
189 std::vector<Module*> &Objects, bool &isArchive,
190 bool search, std::string &ErrorMessage) {
192 // First, try the current directory. Then, iterate over the
193 // directories in LibPaths, looking for a suitable match for LibName
195 for (unsigned NextLibPathIdx = 0; NextLibPathIdx != LibPaths.size();
197 std::string Directory = LibPaths[NextLibPathIdx] + "/";
198 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".a",
201 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".so",
204 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".bc",
209 // If they said no searching, then assume LibName is the real name.
210 if (!LoadLibraryExactName(LibName, Objects, isArchive))
213 ErrorMessage = "error linking library '-l" + LibName+ "': library not found!";
218 static bool LinkLibrary(Module *M, const std::string &LibName,
219 bool search, std::string &ErrorMessage) {
220 std::set<std::string> UndefinedSymbols;
221 GetAllUndefinedSymbols(M, UndefinedSymbols);
222 if (UndefinedSymbols.empty()) {
223 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
224 return false; // No need to link anything in!
227 std::vector<Module*> Objects;
229 if (LoadLibrary(LibName, Objects, isArchive, search, ErrorMessage))
232 // Figure out which symbols are defined by all of the modules in the .a file
233 std::vector<std::set<std::string> > DefinedSymbols;
234 DefinedSymbols.resize(Objects.size());
235 for (unsigned i = 0; i != Objects.size(); ++i)
236 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
239 while (Linked) { // While we are linking in object files, loop.
242 for (unsigned i = 0; i != Objects.size(); ++i) {
243 // Consider whether we need to link in this module... we only need to
244 // link it in if it defines some symbol which is so far undefined.
246 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
248 bool ObjectRequired = false;
249 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
250 E = UndefinedSymbols.end(); I != E; ++I)
251 if (DefSymbols.count(*I)) {
253 std::cerr << " Found object providing symbol '" << *I << "'...\n";
254 ObjectRequired = true;
258 // We DO need to link this object into the program...
259 if (ObjectRequired) {
260 if (LinkModules(M, Objects[i], &ErrorMessage))
261 return true; // Couldn't link in the right object file...
263 // Since we have linked in this object, delete it from the list of
264 // objects to consider in this archive file.
265 std::swap(Objects[i], Objects.back());
266 std::swap(DefinedSymbols[i], DefinedSymbols.back());
268 DefinedSymbols.pop_back();
269 --i; // Do not skip an entry
271 // The undefined symbols set should have shrunk.
272 GetAllUndefinedSymbols(M, UndefinedSymbols);
273 Linked = true; // We have linked something in!
283 main(int argc, char **argv, char ** envp)
285 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
287 std::string ErrorMessage;
288 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
289 if (Composite.get() == 0)
290 return PrintAndReturn(argv[0], ErrorMessage);
292 // We always look first in the current directory when searching for libraries.
293 LibPaths.insert(LibPaths.begin(), ".");
295 // If the user specied an extra search path in their environment, respect it.
296 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
297 LibPaths.push_back(SearchPath);
299 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
300 // A user may specify an ar archive without -l, perhaps because it
301 // is not installed as a library. Detect that and link the library.
302 if (IsArchive(InputFilenames[i])) {
303 if (Verbose) std::cerr << "Linking archive '" << InputFilenames[i]
305 if (LinkLibrary(Composite.get(), InputFilenames[i], false, ErrorMessage))
306 return PrintAndReturn(argv[0], ErrorMessage,
307 ": error linking in '" + InputFilenames[i] + "'");
311 std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage));
313 return PrintAndReturn(argv[0], ErrorMessage);
315 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
317 if (LinkModules(Composite.get(), M.get(), &ErrorMessage))
318 return PrintAndReturn(argv[0], ErrorMessage,
319 ": error linking in '" + InputFilenames[i] + "'");
322 // Remove any consecutive duplicates of the same library...
323 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
326 // Link in all of the libraries next...
327 for (unsigned i = 0; i != Libraries.size(); ++i) {
328 if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n";
329 if (LinkLibrary(Composite.get(), Libraries[i], true, ErrorMessage))
331 return PrintAndReturn(argv[0], ErrorMessage);
335 // Create the output file.
337 std::string RealBytecodeOutput = OutputFilename;
338 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
339 std::ofstream Out(RealBytecodeOutput.c_str());
341 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
345 // Ensure that the bytecode file gets removed from the disk if we get a
348 RemoveFileOnSignal(RealBytecodeOutput);
351 // Generate the bytecode file.
353 if (GenerateBytecode (Composite.get(), Strip, !NoInternalize, &Out))
356 return PrintAndReturn(argv[0], "error generating bytcode");
360 // Close the bytecode file.
365 // If we are not linking a library, generate either a native executable
366 // or a JIT shell script, depending upon what the user wants.
368 if (!LinkAsLibrary) {
370 // If the user wants to generate a native executable, compile it from the
373 // Otherwise, create a script that will run the bytecode through the JIT.
377 // Name of the Assembly Language output file
378 std::string AssemblyFile = OutputFilename + ".s";
381 // Mark the output files for removal if we get an interrupt.
383 RemoveFileOnSignal (AssemblyFile);
384 RemoveFileOnSignal (OutputFilename);
387 // Determine the locations of the llc and gcc programs.
389 std::string llc=FindExecutable ("llc", argv[0]);
390 std::string gcc=FindExecutable ("gcc", argv[0]);
393 return PrintAndReturn (argv[0], "Failed to find llc");
398 return PrintAndReturn (argv[0], "Failed to find gcc");
402 // Generate an assembly language file for the bytecode.
404 generate_assembly (AssemblyFile, RealBytecodeOutput, llc, envp);
405 generate_native (OutputFilename, AssemblyFile, Libraries, gcc, envp);
408 // Remove the assembly language file.
410 removeFile (AssemblyFile);
414 // Output the script to start the program...
415 std::ofstream Out2(OutputFilename.c_str());
417 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
419 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
423 // Make the script executable...
424 MakeFileExecutable (OutputFilename);
426 // Make the bytecode file readable and directly executable in LLEE as well
427 MakeFileExecutable (RealBytecodeOutput);
428 MakeFileReadable (RealBytecodeOutput);