X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=tools%2Fgccld%2FLinker.cpp;h=ec9f89c5e0692397d76f104bf81ffba2aedbd23c;hp=881581fcadc5bfdf54df63b1a041af9dd69059e4;hb=1715ce00e424afcc03eea25edec441ae14392304;hpb=1dd27b157bbe7d9e44fa72abbc8b0f08eba804ec diff --git a/tools/gccld/Linker.cpp b/tools/gccld/Linker.cpp index 881581fcadc..ec9f89c5e06 100644 --- a/tools/gccld/Linker.cpp +++ b/tools/gccld/Linker.cpp @@ -29,63 +29,20 @@ #include #include #include - -/// FileExists - determines if the specified filename exists and is readable. -/// -/// Inputs: -/// FN - The name of the file. -/// -/// Outputs: -/// None. -/// -/// Return Value: -/// TRUE - The file exists and is readable. -/// FALSE - The file does not exist or is unreadable. -/// -static inline bool FileExists(const std::string &FN) { - return access(FN.c_str(), R_OK | F_OK) != -1; -} - -/// IsArchive - determines if the specified file is an ar archive -/// by checking the magic string at the beginning of the file. -/// -/// Inputs: -/// filename - A C++ string containing the name of the file. -/// -/// Outputs: -/// None. -/// -/// Return value: -/// TRUE - The file is an archive. -/// FALSE - The file is not an archive. -/// -static inline bool IsArchive(const std::string &filename) { - std::string ArchiveMagic("!\012"); - char buf[1 + ArchiveMagic.size()]; - std::ifstream f(filename.c_str()); - f.read(buf, ArchiveMagic.size()); - buf[ArchiveMagic.size()] = '\0'; - return ArchiveMagic == buf; -} - -/// FindLib - locates a particular library. It will prepend and append -/// various directories, prefixes, and suffixes until it can find the library. -/// -/// Inputs: -/// Filename - Name of the file to find. -/// Paths - List of directories to search. -/// -/// Outputs: -/// None. -/// -/// Return value: -/// The name of the file is returned. -/// If the file is not found, an empty string is returned. -/// -static std::string -FindLib(const std::string &Filename, const std::vector &Paths) { +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. 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 &Paths, + bool SharedObjectOnly) { // Determine if the pathname can be found as it stands. - if (FileExists(Filename)) + if (FileOpenable(Filename)) return Filename; // If that doesn't work, convert the name into a library name. @@ -96,13 +53,13 @@ FindLib(const std::string &Filename, const std::vector &Paths) { for (unsigned Index = 0; Index != Paths.size(); ++Index) { std::string Directory = Paths[Index] + "/"; - if (FileExists(Directory + LibName + ".bc")) + if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc")) return Directory + LibName + ".bc"; - if (FileExists(Directory + LibName + ".so")) + if (FileOpenable(Directory + LibName + ".so")) return Directory + LibName + ".so"; - if (FileExists(Directory + LibName + ".a")) + if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a")) return Directory + LibName + ".a"; } @@ -112,26 +69,17 @@ FindLib(const std::string &Filename, const std::vector &Paths) { return std::string(); LibName = std::string(SearchPath) + "/" + LibName; - if (FileExists(LibName)) + if (FileOpenable(LibName)) return LibName; return std::string(); } -/// GetAllDefinedSymbols - finds all of the defined symbols in the specified -/// module. +/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the +/// name of each externally-visible symbol defined in M. /// -/// Inputs: -/// M - The module in which to find defined symbols. -/// -/// Outputs: -/// DefinedSymbols - A set of C++ strings that will contain the name of all -/// defined symbols. -/// -/// Return value: -/// None. -/// -void GetAllDefinedSymbols(Module *M, std::set &DefinedSymbols) { +void llvm::GetAllDefinedSymbols(Module *M, + std::set &DefinedSymbols) { for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) DefinedSymbols.insert(I->getName()); @@ -152,11 +100,9 @@ void GetAllDefinedSymbols(Module *M, std::set &DefinedSymbols) { /// UndefinedSymbols - A set of C++ strings containing the name of all /// undefined symbols. /// -/// Return value: -/// None. -/// void -GetAllUndefinedSymbols(Module *M, std::set &UndefinedSymbols) { +llvm::GetAllUndefinedSymbols(Module *M, + std::set &UndefinedSymbols) { std::set DefinedSymbols; UndefinedSymbols.clear(); // Start out empty @@ -185,25 +131,17 @@ GetAllUndefinedSymbols(Module *M, std::set &UndefinedSymbols) { } -/// LoadObject - reads the specified bytecode object file. -/// -/// Inputs: -/// FN - The name of the file to load. +/// 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. /// -/// Outputs: -/// OutErrorMessage - The error message to give back to the caller. -/// -/// Return Value: -/// A pointer to a module represening the bytecode file is returned. -/// If an error occurs, the pointer is 0. -/// -std::auto_ptr -LoadObject(const std::string & FN, std::string &OutErrorMessage) { - std::string ErrorMessage; - Module *Result = ParseBytecodeFile(FN, &ErrorMessage); +std::auto_ptr llvm::LoadObject(const std::string &FN, + std::string &ErrorMessage) { + std::string ParserErrorMessage; + Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage); if (Result) return std::auto_ptr(Result); - OutErrorMessage = "Bytecode file '" + FN + "' corrupt!"; - if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage; + ErrorMessage = "Bytecode file '" + FN + "' could not be loaded"; + if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage; return std::auto_ptr(); } @@ -238,7 +176,7 @@ static bool LinkInArchive(Module *M, } // Load in the archive objects. - if (Verbose) std::cerr << " Loading '" << Filename << "'\n"; + if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n"; std::vector Objects; if (ReadArchiveFile(Filename, Objects, &ErrorMessage)) return true; @@ -262,15 +200,27 @@ static bool LinkInArchive(Module *M, const std::set &DefSymbols = DefinedSymbols[i]; bool ObjectRequired = false; - for (std::set::iterator I = UndefinedSymbols.begin(), - E = UndefinedSymbols.end(); I != E; ++I) - if (DefSymbols.count(*I)) { - if (Verbose) - std::cerr << " Found object providing symbol '" << *I << "'...\n"; - ObjectRequired = true; - break; - } - + + // + // If the object defines main(), then it is automatically required. + // Otherwise, look to see if it defines a symbol that is currently + // undefined. + // + if ((DefSymbols.find ("main")) == DefSymbols.end()) { + for (std::set::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; + } + } else { + ObjectRequired = true; + } + // We DO need to link this object into the program... if (ObjectRequired) { if (LinkModules(M, Objects[i], &ErrorMessage)) @@ -294,12 +244,12 @@ static bool LinkInArchive(Module *M, return false; } -/// LinkInFile - opens an archive library and link in all objects which +/// 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 archives. -/// Filename - The pathname of the archive. +/// 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: @@ -316,8 +266,9 @@ static bool LinkInFile(Module *HeadModule, { std::auto_ptr M(LoadObject(Filename, ErrorMessage)); if (M.get() == 0) return true; - if (Verbose) std::cerr << "Linking in '" << Filename << "'\n"; - return LinkModules(HeadModule, M.get(), &ErrorMessage); + 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. @@ -340,11 +291,8 @@ static bool LinkInFile(Module *HeadModule, /// FALSE - No errors. /// TRUE - Some error occurred. /// -bool LinkFiles(const char *progname, - Module *HeadModule, - const std::vector &Files, - bool Verbose) -{ +bool llvm::LinkFiles(const char *progname, Module *HeadModule, + const std::vector &Files, bool Verbose) { // String in which to receive error messages. std::string ErrorMessage; @@ -354,19 +302,21 @@ bool LinkFiles(const char *progname, // Get the library search path from the environment char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); - for (unsigned i = 1; i < Files.size(); ++i) { + for (unsigned i = 0; i < Files.size(); ++i) { // Determine where this file lives. - if (FileExists(Files[i])) { + 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 (!FileExists(Pathname)) { + if (!FileOpenable(Pathname)) { std::cerr << progname << ": Cannot find linker input file '" << Files[i] << "'\n"; return true; @@ -377,20 +327,20 @@ bool LinkFiles(const char *progname, // is not installed as a library. Detect that and link the library. if (IsArchive(Pathname)) { if (Verbose) - std::cerr << "Linking archive '" << Files[i] << "'\n"; + std::cerr << "Trying to link archive '" << Pathname << "'\n"; if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { PrintAndReturn(progname, ErrorMessage, - ": Error linking in '" + Files[i] + "'"); + ": Error linking in archive '" + Pathname + "'"); return true; } - } else { + } else if (IsBytecode(Pathname)) { if (Verbose) - std::cerr << "Linking file '" << Files[i] << "'\n"; + std::cerr << "Trying to link bytecode file '" << Pathname << "'\n"; if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { PrintAndReturn(progname, ErrorMessage, - ": Error linking in '" + Files[i] + "'"); + ": Error linking in bytecode file '" + Pathname + "'"); return true; } } @@ -417,17 +367,14 @@ bool LinkFiles(const char *progname, /// FALSE - No error. /// TRUE - Error. /// -bool LinkLibraries(const char *progname, - Module *HeadModule, - const std::vector &Libraries, - const std::vector &LibPaths, - bool Verbose, - bool Native) -{ +void llvm::LinkLibraries(const char *progname, Module *HeadModule, + const std::vector &Libraries, + const std::vector &LibPaths, + bool Verbose, bool Native) { // String in which to receive error messages. std::string ErrorMessage; - for (unsigned i = 1; i < Libraries.size(); ++i) { + for (unsigned i = 0; i < Libraries.size(); ++i) { // Determine where this library lives. std::string Pathname = FindLib(Libraries[i], LibPaths); if (Pathname.empty()) { @@ -435,8 +382,9 @@ bool LinkLibraries(const char *progname, // we're doing a native link and give an error if we're doing a bytecode // link. if (!Native) { - PrintAndReturn(progname, "Cannot find " + Libraries[i] + "\n"); - return true; + std::cerr << progname << ": WARNING: Cannot find library -l" + << Libraries[i] << "\n"; + continue; } } @@ -444,24 +392,26 @@ bool LinkLibraries(const char *progname, // is not installed as a library. Detect that and link the library. if (IsArchive(Pathname)) { if (Verbose) - std::cerr << "Linking archive '" << Libraries[i] << "'\n"; + std::cerr << "Trying to link archive '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { - PrintAndReturn(progname, ErrorMessage, - ": Error linking in '" + Libraries[i] + "'"); - return true; + std::cerr << progname << ": " << ErrorMessage + << ": Error linking in archive '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; + exit(1); } - } else { + } else if (IsBytecode(Pathname)) { if (Verbose) - std::cerr << "Linking file '" << Libraries[i] << "'\n"; + std::cerr << "Trying to link bytecode file '" << Pathname + << "' (-l" << Libraries[i] << ")\n"; if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { - PrintAndReturn(progname, ErrorMessage, - ": error linking in '" + Libraries[i] + "'"); - return true; + std::cerr << progname << ": " << ErrorMessage + << ": error linking in bytecode file '" << Pathname << "' (-l" + << Libraries[i] << ")\n"; + exit(1); } } } - - return false; }