diff options
-rw-r--r-- | tools/llvm-ld/GenerateCode.cpp | 353 | ||||
-rw-r--r-- | tools/llvm-ld/Linker.cpp | 419 | ||||
-rw-r--r-- | tools/llvm-ld/Makefile | 16 | ||||
-rw-r--r-- | tools/llvm-ld/llvm-ld.cpp | 303 | ||||
-rw-r--r-- | tools/llvm-ld/llvm-ld.h | 67 |
5 files changed, 1158 insertions, 0 deletions
diff --git a/tools/llvm-ld/GenerateCode.cpp b/tools/llvm-ld/GenerateCode.cpp new file mode 100644 index 0000000000..23b0057a75 --- /dev/null +++ b/tools/llvm-ld/GenerateCode.cpp @@ -0,0 +1,353 @@ +//===- GenerateCode.cpp - Functions for generating executable files ------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains functions for generating executable files once linking +// has finished. This includes generating a shell script to run the JIT or +// a native executable derived from the bytecode. +// +//===----------------------------------------------------------------------===// + +#include "llvm-ld.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Analysis/LoadValueNumbering.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bytecode/WriteBytecodePass.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Support/Linker.h" +#include "llvm/Support/SystemUtils.h" +#include "llvm/Support/CommandLine.h" +using namespace llvm; + +namespace { + cl::opt<bool> + DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); + + cl::opt<bool> + Verify("verify", cl::desc("Verify intermediate results of all passes")); + + cl::opt<bool> + DisableOptimizations("disable-opt", + cl::desc("Do not run any optimization passes")); +} + +/// CopyEnv - This function takes an array of environment variables and makes a +/// copy of it. This copy can then be manipulated any way the caller likes +/// without affecting the process's real environment. +/// +/// Inputs: +/// envp - An array of C strings containing an environment. +/// +/// Return value: +/// NULL - An error occurred. +/// +/// Otherwise, a pointer to a new array of C strings is returned. Every string +/// in the array is a duplicate of the one in the original array (i.e. we do +/// not copy the char *'s from one array to another). +/// +static char ** CopyEnv(char ** const envp) { + // Count the number of entries in the old list; + unsigned entries; // The number of entries in the old environment list + for (entries = 0; envp[entries] != NULL; entries++) + /*empty*/; + + // Add one more entry for the NULL pointer that ends the list. + ++entries; + + // If there are no entries at all, just return NULL. + if (entries == 0) + return NULL; + + // Allocate a new environment list. + char **newenv = new char* [entries]; + if ((newenv = new char* [entries]) == NULL) + return NULL; + + // Make a copy of the list. Don't forget the NULL that ends the list. + entries = 0; + while (envp[entries] != NULL) { + newenv[entries] = new char[strlen (envp[entries]) + 1]; + strcpy (newenv[entries], envp[entries]); + ++entries; + } + newenv[entries] = NULL; + + return newenv; +} + + +/// RemoveEnv - Remove the specified environment variable from the environment +/// array. +/// +/// Inputs: +/// name - The name of the variable to remove. It cannot be NULL. +/// envp - The array of environment variables. It cannot be NULL. +/// +/// Notes: +/// This is mainly done because functions to remove items from the environment +/// are not available across all platforms. In particular, Solaris does not +/// seem to have an unsetenv() function or a setenv() function (or they are +/// undocumented if they do exist). +/// +static void RemoveEnv(const char * name, char ** const envp) { + for (unsigned index=0; envp[index] != NULL; index++) { + // Find the first equals sign in the array and make it an EOS character. + char *p = strchr (envp[index], '='); + if (p == NULL) + continue; + else + *p = '\0'; + + // Compare the two strings. If they are equal, zap this string. + // Otherwise, restore it. + if (!strcmp(name, envp[index])) + *envp[index] = '\0'; + else + *p = '='; + } + + return; +} + +static inline void addPass(PassManager &PM, Pass *P) { + // Add the pass to the pass manager... + PM.add(P); + + // If we are verifying all of the intermediate steps, add the verifier... + if (Verify) PM.add(createVerifierPass()); +} + +/// GenerateBytecode - generates a bytecode file from the specified module. +/// +/// Inputs: +/// M - The module for which bytecode should be generated. +/// Strip - Flags whether symbols should be stripped from the output. +/// Internalize - Flags whether all symbols should be marked internal. +/// Out - Pointer to file stream to which to write the output. +/// +/// Returns non-zero value on error. +/// +int llvm::GenerateBytecode(Module *M, bool Strip, bool Internalize, + std::ostream *Out) { + // In addition to just linking the input from GCC, we also want to spiff it up + // a little bit. Do this now. + PassManager Passes; + + if (Verify) Passes.add(createVerifierPass()); + + // Add an appropriate TargetData instance for this module... + addPass(Passes, new TargetData("gccld", M)); + + // Often if the programmer does not specify proper prototypes for the + // functions they are calling, they end up calling a vararg version of the + // function that does not get a body filled in (the real function has typed + // arguments). This pass merges the two functions. + addPass(Passes, createFunctionResolvingPass()); + + if (!DisableOptimizations) { + if (Internalize) { + // Now that composite has been compiled, scan through the module, looking + // for a main function. If main is defined, mark all other functions + // internal. + addPass(Passes, createInternalizePass()); + } + + // Now that we internalized some globals, see if we can mark any globals as + // being constant! + addPass(Passes, createGlobalConstifierPass()); + + // Linking modules together can lead to duplicated global constants, only + // keep one copy of each constant... + addPass(Passes, createConstantMergePass()); + + // If the -s command line option was specified, strip the symbols out of the + // resulting program to make it smaller. -s is a GCC option that we are + // supporting. + if (Strip) + addPass(Passes, createSymbolStrippingPass()); + + // Propagate constants at call sites into the functions they call. + addPass(Passes, createIPConstantPropagationPass()); + + // Remove unused arguments from functions... + addPass(Passes, createDeadArgEliminationPass()); + + if (!DisableInline) + addPass(Passes, createFunctionInliningPass()); // Inline small functions + + addPass(Passes, createPruneEHPass()); // Remove dead EH info + addPass(Passes, createGlobalDCEPass()); // Remove dead functions + + // If we didn't decide to inline a function, check to see if we can + // transform it to pass arguments by value instead of by reference. + addPass(Passes, createArgumentPromotionPass()); + + // The IPO passes may leave cruft around. Clean up after them. + addPass(Passes, createInstructionCombiningPass()); + + addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas + + // Run a few AA driven optimizations here and now, to cleanup the code. + addPass(Passes, createGlobalsModRefPass()); // IP alias analysis + + addPass(Passes, createLICMPass()); // Hoist loop invariants + addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs + addPass(Passes, createGCSEPass()); // Remove common subexprs + addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores + + // Cleanup and simplify the code after the scalar optimizations. + addPass(Passes, createInstructionCombiningPass()); + + // Delete basic blocks, which optimization passes may have killed... + addPass(Passes, createCFGSimplificationPass()); + + // Now that we have optimized the program, discard unreachable functions... + addPass(Passes, createGlobalDCEPass()); + } + + // Make sure everything is still good. + Passes.add(createVerifierPass()); + + // Add the pass that writes bytecode to the output file... + addPass(Passes, new WriteBytecodePass(Out)); + + // Run our queue of passes all at once now, efficiently. + Passes.run(*M); + + return 0; +} + +/// GenerateAssembly - generates a native assembly language source file from the +/// specified bytecode file. +/// +/// Inputs: +/// InputFilename - The name of the output bytecode file. +/// OutputFilename - The name of the file to generate. +/// llc - The pathname to use for LLC. +/// envp - The environment to use when running LLC. +/// +/// Return non-zero value on error. +/// +int llvm::GenerateAssembly(const std::string &OutputFilename, + const std::string &InputFilename, + const std::string &llc, + char ** const envp) { + // Run LLC to convert the bytecode file into assembly code. + const char *cmd[6]; + cmd[0] = llc.c_str(); + cmd[1] = "-f"; + cmd[2] = "-o"; + cmd[3] = OutputFilename.c_str(); + cmd[4] = InputFilename.c_str(); + cmd[5] = 0; + + return ExecWait(cmd, envp); +} + +/// GenerateAssembly - generates a native assembly language source file from the +/// specified bytecode file. +int llvm::GenerateCFile(const std::string &OutputFile, + const std::string &InputFile, + const std::string &llc, char ** const envp) { + // Run LLC to convert the bytecode file into C. + const char *cmd[7]; + + cmd[0] = llc.c_str(); + cmd[1] = "-march=c"; + cmd[2] = "-f"; + cmd[3] = "-o"; + cmd[4] = OutputFile.c_str(); + cmd[5] = InputFile.c_str(); + cmd[6] = 0; + return ExecWait(cmd, envp); +} + +/// GenerateNative - generates a native assembly language source file from the +/// specified assembly source file. +/// +/// Inputs: +/// InputFilename - The name of the output bytecode file. +/// OutputFilename - The name of the file to generate. +/// Libraries - The list of libraries with which to link. +/// LibPaths - The list of directories in which to find libraries. +/// gcc - The pathname to use for GGC. +/// envp - A copy of the process's current environment. +/// +/// Outputs: +/// None. +/// +/// Returns non-zero value on error. +/// +int llvm::GenerateNative(const std::string &OutputFilename, + const std::string &InputFilename, + const std::vector<std::string> &Libraries, + const std::vector<std::string> &LibPaths, + const std::string &gcc, char ** const envp) { + // Remove these environment variables from the environment of the + // programs that we will execute. It appears that GCC sets these + // environment variables so that the programs it uses can configure + // themselves identically. + // + // However, when we invoke GCC below, we want it to use its normal + // configuration. Hence, we must sanitize its environment. + char ** clean_env = CopyEnv(envp); + if (clean_env == NULL) + return 1; + RemoveEnv("LIBRARY_PATH", clean_env); + RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); + RemoveEnv("GCC_EXEC_PREFIX", clean_env); + RemoveEnv("COMPILER_PATH", clean_env); + RemoveEnv("COLLECT_GCC", clean_env); + + std::vector<const char *> cmd; + + // Run GCC to assemble and link the program into native code. + // + // Note: + // We can't just assemble and link the file with the system assembler + // and linker because we don't know where to put the _start symbol. + // GCC mysteriously knows how to do it. + cmd.push_back(gcc.c_str()); + cmd.push_back("-fno-strict-aliasing"); + cmd.push_back("-O3"); + cmd.push_back("-o"); + cmd.push_back(OutputFilename.c_str()); + cmd.push_back(InputFilename.c_str()); + + // Adding the library paths creates a problem for native generation. If we + // include the search paths from llvmgcc, then we'll be telling normal gcc + // to look inside of llvmgcc's library directories for libraries. This is + // bad because those libraries hold only bytecode files (not native object + // files). In the end, we attempt to link the bytecode libgcc into a native + // program. +#if 0 + // Add in the library path options. + for (unsigned index=0; index < LibPaths.size(); index++) { + cmd.push_back("-L"); + cmd.push_back(LibPaths[index].c_str()); + } +#endif + + // Add in the libraries to link. + std::vector<std::string> Libs(Libraries); + for (unsigned index = 0; index < Libs.size(); index++) { + if (Libs[index] != "crtend") { + Libs[index] = "-l" + Libs[index]; + cmd.push_back(Libs[index].c_str()); + } + } + cmd.push_back(NULL); + + // Run the compiler to assembly and link together the program. + return ExecWait(&(cmd[0]), clean_env); +} + diff --git a/tools/llvm-ld/Linker.cpp b/tools/llvm-ld/Linker.cpp new file mode 100644 index 0000000000..42788f29f9 --- /dev/null +++ b/tools/llvm-ld/Linker.cpp @@ -0,0 +1,419 @@ +//===- Linker.cpp - Link together LLVM objects and libraries --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains routines to handle linking together LLVM bytecode files, +// and to handle annoying things like static libraries. +// +//===----------------------------------------------------------------------===// + +#include "llvm-ld.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Bytecode/Reader.h" +#include "llvm/Bytecode/WriteBytecodePass.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Support/Linker.h" +#include "llvm/Config/config.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/System/Signals.h" +#include "llvm/Support/SystemUtils.h" +#include <algorithm> +#include <fstream> +#include <memory> +#include <set> +using namespace llvm; + +/// FindLib - Try to convert Filename into the name of a file that we can open, +/// if it does not already name a file we can open, by first trying to open +/// Filename, then libFilename.[suffix] for each of a set of several common +/// library suffixes, in each of the directories in Paths and the directory +/// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns +/// an empty string if no matching file can be found. +/// +std::string llvm::FindLib(const std::string &Filename, + const std::vector<std::string> &Paths, + bool SharedObjectOnly) { + // Determine if the pathname can be found as it stands. + if (FileOpenable(Filename)) + return Filename; + + // If that doesn't work, convert the name into a library name. + std::string LibName = "lib" + Filename; + + // Iterate over the directories in Paths to see if we can find the library + // there. + for (unsigned Index = 0; Index != Paths.size(); ++Index) { + std::string Directory = Paths[Index] + "/"; + + if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc")) + return Directory + LibName + ".bc"; + + if (FileOpenable(Directory + LibName + SHLIBEXT)) + return Directory + LibName + SHLIBEXT; + + if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a")) + return Directory + LibName + ".a"; + } + + // One last hope: Check LLVM_LIB_SEARCH_PATH. + char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); + if (SearchPath == NULL) + return std::string(); + + LibName = std::string(SearchPath) + "/" + LibName; + if (FileOpenable(LibName)) + return LibName; + + return std::string(); +} + +/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the +/// name of each externally-visible symbol defined in M. +/// +void llvm::GetAllDefinedSymbols(Module *M, + std::set<std::string> &DefinedSymbols) { + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) + DefinedSymbols.insert(I->getName()); + for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) + if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) + DefinedSymbols.insert(I->getName()); +} + +/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still +/// exist in an LLVM module. This is a bit tricky because there may be two +/// symbols with the same name but different LLVM types that will be resolved to +/// each other but aren't currently (thus we need to treat it as resolved). +/// +/// Inputs: +/// M - The module in which to find undefined symbols. +/// +/// Outputs: +/// UndefinedSymbols - A set of C++ strings containing the name of all +/// undefined symbols. +/// +void +llvm::GetAllUndefinedSymbols(Module *M, + std::set<std::string> &UndefinedSymbols) { + std::set<std::string> DefinedSymbols; + UndefinedSymbols.clear(); // Start out empty + + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) + if (I->hasName()) { + if (I->isExternal()) + UndefinedSymbols.insert(I->getName()); + else if (!I->hasInternalLinkage()) + DefinedSymbols.insert(I->getName()); + } + for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) + if (I->hasName()) { + if (I->isExternal()) + UndefinedSymbols.insert(I->getName()); + else if (!I->hasInternalLinkage()) + DefinedSymbols.insert(I->getName()); + } + + // Prune out any defined symbols from the undefined symbols set... + for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); + I != UndefinedSymbols.end(); ) + if (DefinedSymbols.count(*I)) + UndefinedSymbols.erase(I++); // This symbol really is defined! + else + ++I; // Keep this symbol in the undefined symbols list +} + + +/// LoadObject - Read in and parse the bytecode file named by FN and return the +/// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an +/// error occurs. +/// +std::auto_ptr<Module> llvm::LoadObject(const std::string &FN, + std::string &ErrorMessage) { + std::string ParserErrorMessage; + Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage); + if (Result) return std::auto_ptr<Module>(Result); + ErrorMessage = "Bytecode file '" + FN + "' could not be loaded"; + if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage; + return std::auto_ptr<Module>(); +} + +/// LinkInArchive - opens an archive library and link in all objects which +/// provide symbols that are currently undefined. +/// +/// Inputs: +/// M - The module in which to link the archives. +/// Filename - The pathname of the archive. +/// Verbose - Flags whether verbose messages should be printed. +/// +/// Outputs: +/// ErrorMessage - A C++ string detailing what error occurred, if any. +/// +/// Return Value: +/// TRUE - An error occurred. +/// FALSE - No errors. +/// +static bool LinkInArchive(Module *M, + const std::string &Filename, + std::string &ErrorMessage, + bool Verbose) +{ + // Find all of the symbols currently undefined in the bytecode program. + // If all the symbols are defined, the program is complete, and there is + // no reason to link in any archive files. + std::set<std::string> UndefinedSymbols; + GetAllUndefinedSymbols(M, UndefinedSymbols); + if (UndefinedSymbols.empty()) { + if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; + return false; // No need to link anything in! + } + + // Load in the archive objects. + if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n"; + std::vector<Module*> Objects; + if (ReadArchiveFile(Filename, Objects, &ErrorMessage)) + return true; + + // Figure out which symbols are defined by all of the modules in the archive. + std::vector<std::set<std::string> > DefinedSymbols; + DefinedSymbols.resize(Objects.size()); + for (unsigned i = 0; i != Objects.size(); ++i) { + GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); + } + + // While we are linking in object files, loop. + bool Linked = true; + while (Linked) { + Linked = false; + + for (unsigned i = 0; i != Objects.size(); ++i) { + // Consider whether we need to link in this module... we only need to + // link it in if it defines some symbol which is so far undefined. + // + const std::set<std::string> &DefSymbols = DefinedSymbols[i]; + + bool ObjectRequired = false; + + // + // If the object defines main() and the program currently has main() + // undefined, then automatically link in the module. Otherwise, look to + // see if it defines a symbol that is currently undefined. + // + if ((M->getMainFunction() == NULL) && + ((DefSymbols.find ("main")) != DefSymbols.end())) { + ObjectRequired = true; + } else { + for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), + E = UndefinedSymbols.end(); I != E; ++I) + if (DefSymbols.count(*I)) { + if (Verbose) + std::cerr << " Found object '" + << Objects[i]->getModuleIdentifier () + << "' providing symbol '" << *I << "'...\n"; + ObjectRequired = true; + break; + } + } + + // We DO need to link this object into the program... + if (ObjectRequired) { + if (LinkModules(M, Objects[i], &ErrorMessage)) + return true; // Couldn't link in the right object file... + + // Since we have linked in this object, delete it from the list of + // objects to consider in this archive file. + std::swap(Objects[i], Objects.back()); + std::swap(DefinedSymbols[i], DefinedSymbols.back()); + Objects.pop_back(); + DefinedSymbols.pop_back(); + --i; // Do not skip an entry + + // The undefined symbols set should have shrunk. + GetAllUndefinedSymbols(M, UndefinedSymbols); + Linked = true; // We have linked something in! + } + } + } + + return false; +} + +/// LinkInFile - opens a bytecode file and links in all objects which +/// provide symbols that are currently undefined. +/// +/// Inputs: +/// HeadModule - The module in which to link the bytecode file. +/// Filename - The pathname of the bytecode file. +/// Verbose - Flags whether verbose messages should be printed. +/// +/// Outputs: +/// ErrorMessage - A C++ string detailing what error occurred, if any. +/// +/// Return Value: +/// TRUE - An error occurred. +/// FALSE - No errors. +/// +static bool LinkInFile(Module *HeadModule, + const std::string &Filename, + std::string &ErrorMessage, + bool Verbose) +{ + std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage)); + if (M.get() == 0) return true; + bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage); + if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n"; + return Result; +} + +/// LinkFiles - takes a module and a list of files and links them all together. +/// It locates the file either in the current directory, as its absolute +/// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. +/// +/// Inputs: +/// progname - The name of the program (infamous argv[0]). +/// HeadModule - The module under which all files will be linked. +/// Files - A vector of C++ strings indicating the LLVM bytecode filenames +/// to be linked. The names can refer to a mixture of pure LLVM +/// bytecode files and archive (ar) formatted files. +/// Verbose - Flags whether verbose output should be printed while linking. +/// +/// Outputs: +/// HeadModule - The module will have the specified LLVM bytecode files linked +/// in. +/// +/// Return value: +/// FALSE - No errors. +/// TRUE - Some error occurred. +/// +bool llvm::LinkFiles(const char *progname, Module *HeadModule, + const std::vector<std::string> &Files, bool Verbose) { + // String in which to receive error messages. + std::string ErrorMessage; + + // Full pathname of the file + std::string Pathname; + + // Get the library search path from the environment + char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); + + for (unsigned i = 0; i < Files.size(); ++i) { + // Determine where this file lives. + if (FileOpenable(Files[i])) { + Pathname = Files[i]; + } else { + if (SearchPath == NULL) { + std::cerr << progname << ": Cannot find linker input file '" + << Files[i] << "'\n"; + std::cerr << progname + << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n"; + return true; + } + + Pathname = std::string(SearchPath)+"/"+Files[i]; + if (!FileOpenable(Pathname)) { + std::cerr << progname << ": Cannot find linker input file '" + << Files[i] << "'\n"; + return true; + } + } + + // A user may specify an ar archive without -l, perhaps because it + // is not installed as a library. Detect that and link the library. + if (IsArchive(Pathname)) { + if (Verbose) + std::cerr << "Trying to link archive '" << Pathname << "'\n"; + + if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { + std::cerr << progname << ": Error linking in archive '" << Pathname + << "': " << ErrorMessage << "\n"; + return true; + } + } else if (IsBytecode(Pathname)) { + if (Verbose) + std::cerr << "Trying to link bytecode file '" << Pathname << "'\n"; + + if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { + std::cerr << progname << ": Error linking in bytecode file '" + << Pathname << "': " << ErrorMessage << "\n"; + return true; + } + } + } + + return false; +} + +/// LinkLibraries - takes the specified library files and links them into the +/// main bytecode object file. +/// +/// Inputs: +/// progname - The name of the program (infamous argv[0]). +/// HeadModule - The module into which all necessary libraries will be linked. +/// Libraries - The list of libraries to link into the module. +/// LibPaths - The list of library paths in which to find libraries. +/// Verbose - Flags whether verbose messages should be printed. +/// Native - Flags whether native code is being generated. +/// +/// Outputs: +/// HeadModule - The module will have all necessary libraries linked in. +/// +/// Return value: +/// FALSE - No error. +/// TRUE - Error. +/// +void llvm::LinkLibraries(const char *progname, Module *HeadModule, + const std::vector<std::string> &Libraries, + const std::vector<std::string> &LibPaths, + bool Verbose, bool Native) { + // String in which to receive error messages. + std::string ErrorMessage; + + for (unsigned i = 0; i < Libraries.size(); ++i) { + // Determine where this library lives. + std::string Pathname = FindLib(Libraries[i], LibPaths); + if (Pathname.empty()) { + // If the pathname does not exist, then continue to the next one if + // we're doing a native link and give an error if we're doing a bytecode + // link. + if (!Native) { + std::cerr << progname << ": WARNING: Cannot find library -l" + << Libraries[i] << "\n"; + continue; + } + } + + // A user may specify an ar archive without -l, perhaps because it + // is not installed as a library. Detect that and link the library. + if (IsArchive(Pathname)) { + if (Verbose) + std::cerr << "Trying to link archive '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; + + if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { + std::cerr << progname << ": " << ErrorMessage + << ": Error linking in archive '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; + exit(1); + } + } else if (IsBytecode(Pathname)) { + if (Verbose) + std::cerr << "Trying to link bytecode file '" << Pathname + << "' (-l" << Libraries[i] << ")\n"; + + if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { + std::cerr << progname << ": " << ErrorMessage + << ": error linking in bytecode file '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; + exit(1); + } + } + } +} diff --git a/tools/llvm-ld/Makefile b/tools/llvm-ld/Makefile new file mode 100644 index 0000000000..2c64962632 --- /dev/null +++ b/tools/llvm-ld/Makefile @@ -0,0 +1,16 @@ +##===- tools/gccld/Makefile --------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file was developed by the LLVM research group and is distributed under +# the University of Illinois Open Source License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. + +TOOLNAME = llvm-ld +USEDLIBS = ipo.a transforms.a scalaropts.a analysis.a ipa.a transformutils.a \ + target.a bcreader bcwriter vmcore support.a LLVMsystem.a + +include $(LEVEL)/Makefile.common diff --git a/tools/llvm-ld/llvm-ld.cpp b/tools/llvm-ld/llvm-ld.cpp new file mode 100644 index 0000000000..d59b18acb5 --- /dev/null +++ b/tools/llvm-ld/llvm-ld.cpp @@ -0,0 +1,303 @@ +//===- llvm-ld.cpp - LLVM 'ld' compatible linker --------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This utility is intended to be compatible with GCC, and follows standard +// system 'ld' conventions. As such, the default output file is ./a.out. +// Additionally, this program outputs a shell script that is used to invoke LLI +// to execute the program. In this manner, the generated executable (a.out for +// example), is directly executable, whereas the bytecode file actually lives in +// the a.out.bc file generated by this program. Also, Force is on by default. +// +// Note that if someone (or a script) deletes the executable program generated, +// the .bc file will be left around. Considering that this is a temporary hack, +// I'm not too worried about this. +// +//===----------------------------------------------------------------------===// + +#include "llvm-ld.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Bytecode/Reader.h" +#include "llvm/Bytecode/WriteBytecodePass.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Support/Linker.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/System/Signals.h" +#include "llvm/Support/SystemUtils.h" +#include <fstream> +#include <memory> +using namespace llvm; + +enum OptimizationLevels { + OPT_FAST_COMPILE, + OPT_SIMPLE, + OPT_AGGRESSIVE, + OPT_LINK_TIME, + OPT_AGGRESSIVE_LINK_TIME +}; + +namespace { + cl::list<std::string> + InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), + cl::OneOrMore); + + cl::opt<std::string> + OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), + cl::value_desc("filename")); + + cl::opt<bool> + Verbose("v", cl::desc("Print information about actions taken")); + + cl::list<std::string> + LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, + cl::value_desc("directory")); + + cl::list<std::string> + Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, + cl::value_desc("library prefix")); + + cl::opt<bool> + Strip("s", cl::desc("Strip symbol info from executable")); + + cl::opt<bool> + NoInternalize("disable-internalize", + cl::desc("Do not mark all symbols as internal")); + cl::alias + ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), + cl::aliasopt(NoInternalize)); + + cl::opt<bool> + LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" + " library, not an executable")); + cl::alias + Relink("r", cl::desc("Alias for -link-as-library"), + cl::aliasopt(LinkAsLibrary)); + + cl::opt<bool> + Native("native", + cl::desc("Generate a native binary instead of a shell script")); + cl::opt<bool> + NativeCBE("native-cbe", + cl::desc("Generate a native binary with the C backend and GCC")); + + // Compatibility options that are ignored but supported by LD + cl::opt<std::string> + CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); + cl::opt<std::string> + CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); + cl::opt<bool> + CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); + cl::opt<std::string> + CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored")); + + cl::opt<OptimizationLevels> OptLevel( + cl::desc("Choose level of optimization to apply:"), + cl::init(OPT_FAST_COMPILE), cl::values( + clEnumValN(OPT_FAST_COMPILE,"O0", + "An alias for the -O1 option."), + clEnumValN(OPT_FAST_COMPILE,"O1", + "Optimize for linking speed, not execution speed."), + clEnumValN(OPT_SIMPLE,"O2", + "Perform only required/minimal optimizations"), + clEnumValN(OPT_AGGRESSIVE,"O3", + "An alias for the -O2 option."), + clEnumValN(OPT_LINK_TIME,"O4", + "Perform standard link time optimizations"), + clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5", + "Perform aggressive link time optimizations"), + clEnumValEnd + ) + ); +} + +/// PrintAndReturn - Prints a message to standard error and returns true. +/// +/// Inputs: +/// progname - The name of the program (i.e. argv[0]). +/// Message - The message to print to standard error. +/// +static int PrintAndReturn(const char *progname, const std::string &Message) { + std::cerr << progname << ": " << Message << "\n"; + return 1; +} + +/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM +/// bytecode file for the program. +static void EmitShellScript(char **argv) { +#if defined(_WIN3 |