LLVM API Documentation
00001 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains basic Linker functionality that all usages will need. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Linker.h" 00015 #include "llvm/Module.h" 00016 #include "llvm/Bitcode/ReaderWriter.h" 00017 #include "llvm/Config/config.h" 00018 #include "llvm/Support/MemoryBuffer.h" 00019 #include "llvm/Support/Streams.h" 00020 using namespace llvm; 00021 00022 Linker::Linker(const std::string& progname, const std::string& modname, 00023 unsigned flags) 00024 : Composite(0) 00025 , LibPaths() 00026 , Flags(flags) 00027 , Error() 00028 , ProgramName(progname) 00029 { 00030 Composite = new Module(modname); 00031 } 00032 00033 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) 00034 : Composite(aModule) 00035 , LibPaths() 00036 , Flags(flags) 00037 , Error() 00038 , ProgramName(progname) 00039 { 00040 } 00041 00042 Linker::~Linker() { 00043 delete Composite; 00044 } 00045 00046 bool 00047 Linker::error(const std::string& message) { 00048 Error = message; 00049 if (!(Flags&QuietErrors)) 00050 cerr << ProgramName << ": error: " << message << "\n"; 00051 return true; 00052 } 00053 00054 bool 00055 Linker::warning(const std::string& message) { 00056 Error = message; 00057 if (!(Flags&QuietWarnings)) 00058 cerr << ProgramName << ": warning: " << message << "\n"; 00059 return false; 00060 } 00061 00062 void 00063 Linker::verbose(const std::string& message) { 00064 if (Flags&Verbose) 00065 cerr << " " << message << "\n"; 00066 } 00067 00068 void 00069 Linker::addPath(const sys::Path& path) { 00070 LibPaths.push_back(path); 00071 } 00072 00073 void 00074 Linker::addPaths(const std::vector<std::string>& paths) { 00075 for (unsigned i = 0; i != paths.size(); ++i) { 00076 sys::Path aPath; 00077 aPath.set(paths[i]); 00078 LibPaths.push_back(aPath); 00079 } 00080 } 00081 00082 void 00083 Linker::addSystemPaths() { 00084 sys::Path::GetBitcodeLibraryPaths(LibPaths); 00085 LibPaths.insert(LibPaths.begin(),sys::Path("./")); 00086 } 00087 00088 Module* 00089 Linker::releaseModule() { 00090 Module* result = Composite; 00091 LibPaths.clear(); 00092 Error.clear(); 00093 Composite = 0; 00094 Flags = 0; 00095 return result; 00096 } 00097 00098 // LoadObject - Read in and parse the bitcode file named by FN and return the 00099 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set 00100 // Error if an error occurs. 00101 std::auto_ptr<Module> 00102 Linker::LoadObject(const sys::Path &FN) { 00103 std::string ParseErrorMessage; 00104 Module *Result = 0; 00105 00106 const std::string &FNS = FN.toString(); 00107 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str())); 00108 if (Buffer.get()) 00109 Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage); 00110 else 00111 ParseErrorMessage = "Error reading file '" + FNS + "'"; 00112 00113 if (Result) 00114 return std::auto_ptr<Module>(Result); 00115 Error = "Bitcode file '" + FN.toString() + "' could not be loaded"; 00116 if (ParseErrorMessage.size()) 00117 Error += ": " + ParseErrorMessage; 00118 return std::auto_ptr<Module>(); 00119 } 00120 00121 // IsLibrary - Determine if "Name" is a library in "Directory". Return 00122 // a non-empty sys::Path if its found, an empty one otherwise. 00123 static inline sys::Path IsLibrary(const std::string& Name, 00124 const sys::Path& Directory) { 00125 00126 sys::Path FullPath(Directory); 00127 00128 // Try the libX.a form 00129 FullPath.appendComponent("lib" + Name); 00130 FullPath.appendSuffix("a"); 00131 if (FullPath.isArchive()) 00132 return FullPath; 00133 00134 // Try the libX.bca form 00135 FullPath.eraseSuffix(); 00136 FullPath.appendSuffix("bca"); 00137 if (FullPath.isArchive()) 00138 return FullPath; 00139 00140 // Try the libX.so (or .dylib) form 00141 FullPath.eraseSuffix(); 00142 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1])); 00143 if (FullPath.isDynamicLibrary()) // Native shared library? 00144 return FullPath; 00145 if (FullPath.isBitcodeFile()) // .so file containing bitcode? 00146 return FullPath; 00147 00148 // Not found .. fall through 00149 00150 // Indicate that the library was not found in the directory. 00151 FullPath.clear(); 00152 return FullPath; 00153 } 00154 00155 /// FindLib - Try to convert Filename into the name of a file that we can open, 00156 /// if it does not already name a file we can open, by first trying to open 00157 /// Filename, then libFilename.[suffix] for each of a set of several common 00158 /// library suffixes, in each of the directories in LibPaths. Returns an empty 00159 /// Path if no matching file can be found. 00160 /// 00161 sys::Path 00162 Linker::FindLib(const std::string &Filename) { 00163 // Determine if the pathname can be found as it stands. 00164 sys::Path FilePath(Filename); 00165 if (FilePath.canRead() && 00166 (FilePath.isArchive() || FilePath.isDynamicLibrary())) 00167 return FilePath; 00168 00169 // Iterate over the directories in Paths to see if we can find the library 00170 // there. 00171 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) { 00172 sys::Path Directory(LibPaths[Index]); 00173 sys::Path FullPath = IsLibrary(Filename,Directory); 00174 if (!FullPath.isEmpty()) 00175 return FullPath; 00176 } 00177 return sys::Path(); 00178 }