LLVM API Documentation
00001 //===- llvm/Linker.h - Module Linker Interface ------------------*- C++ -*-===// 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 defines the interface to the module/file/archive linker. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_LINKER_H 00015 #define LLVM_LINKER_H 00016 00017 #include "llvm/System/Path.h" 00018 #include <memory> 00019 #include <vector> 00020 00021 namespace llvm { 00022 00023 class Module; 00024 class LLVMContext; 00025 00026 /// This class provides the core functionality of linking in LLVM. It retains a 00027 /// Module object which is the composite of the modules and libraries linked 00028 /// into it. The composite Module can be retrieved via the getModule() method. 00029 /// In this case the Linker still retains ownership of the Module. If the 00030 /// releaseModule() method is used, the ownership of the Module is transferred 00031 /// to the caller and the Linker object is only suitable for destruction. 00032 /// The Linker can link Modules from memory, bitcode files, or bitcode 00033 /// archives. It retains a set of search paths in which to find any libraries 00034 /// presented to it. By default, the linker will generate error and warning 00035 /// messages to stderr but this capability can be turned off with the 00036 /// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely 00037 /// print out the linking actions it is taking with the Verbose flag. 00038 /// @brief The LLVM Linker. 00039 class Linker { 00040 00041 /// @name Types 00042 /// @{ 00043 public: 00044 /// This type is used to pass the linkage items (libraries and files) to 00045 /// the LinkItems function. It is composed of string/bool pairs. The string 00046 /// provides the name of the file or library (as with the -l option). The 00047 /// bool should be true for libraries and false for files, signifying 00048 /// "isLibrary". 00049 /// @brief A list of linkage items 00050 typedef std::vector<std::pair<std::string,bool> > ItemList; 00051 00052 /// This enumeration is used to control various optional features of the 00053 /// linker. 00054 enum ControlFlags { 00055 Verbose = 1, ///< Print to stderr what steps the linker is taking 00056 QuietWarnings = 2, ///< Don't print warnings to stderr. 00057 QuietErrors = 4 ///< Don't print errors to stderr. 00058 }; 00059 00060 /// @} 00061 /// @name Constructors 00062 /// @{ 00063 public: 00064 /// Construct the Linker with an empty module which will be given the 00065 /// name \p progname. \p progname will also be used for error messages. 00066 /// @brief Construct with empty module 00067 Linker( 00068 const StringRef& progname, ///< name of tool running linker 00069 const StringRef& modulename, ///< name of linker's end-result module 00070 LLVMContext& C, ///< Context for global info 00071 unsigned Flags = 0 ///< ControlFlags (one or more |'d together) 00072 ); 00073 00074 /// Construct the Linker with a previously defined module, \p aModule. Use 00075 /// \p progname for the name of the program in error messages. 00076 /// @brief Construct with existing module 00077 Linker(const StringRef& progname, Module* aModule, unsigned Flags = 0); 00078 00079 /// Destruct the Linker. 00080 /// @brief Destructor 00081 ~Linker(); 00082 00083 /// @} 00084 /// @name Accessors 00085 /// @{ 00086 public: 00087 /// This method gets the composite module into which linking is being 00088 /// done. The Composite module starts out empty and accumulates modules 00089 /// linked into it via the various LinkIn* methods. This method does not 00090 /// release the Module to the caller. The Linker retains ownership and will 00091 /// destruct the Module when the Linker is destructed. 00092 /// @see releaseModule 00093 /// @brief Get the linked/composite module. 00094 Module* getModule() const { return Composite; } 00095 00096 /// This method releases the composite Module into which linking is being 00097 /// done. Ownership of the composite Module is transferred to the caller who 00098 /// must arrange for its destruct. After this method is called, the Linker 00099 /// terminates the linking session for the returned Module. It will no 00100 /// longer utilize the returned Module but instead resets itself for 00101 /// subsequent linking as if the constructor had been called. The Linker's 00102 /// LibPaths and flags to be reset, and memory will be released. 00103 /// @brief Release the linked/composite module. 00104 Module* releaseModule(); 00105 00106 /// This method gets the list of libraries that form the path that the 00107 /// Linker will search when it is presented with a library name. 00108 /// @brief Get the Linkers library path 00109 const std::vector<sys::Path>& getLibPaths() const { return LibPaths; } 00110 00111 /// This method returns an error string suitable for printing to the user. 00112 /// The return value will be empty unless an error occurred in one of the 00113 /// LinkIn* methods. In those cases, the LinkIn* methods will have returned 00114 /// true, indicating an error occurred. At most one error is retained so 00115 /// this function always returns the last error that occurred. Note that if 00116 /// the Quiet control flag is not set, the error string will have already 00117 /// been printed to stderr. 00118 /// @brief Get the text of the last error that occurred. 00119 const std::string &getLastError() const { return Error; } 00120 00121 /// @} 00122 /// @name Mutators 00123 /// @{ 00124 public: 00125 /// Add a path to the list of paths that the Linker will search. The Linker 00126 /// accumulates the set of libraries added 00127 /// library paths for the target platform. The standard libraries will 00128 /// always be searched last. The added libraries will be searched in the 00129 /// order added. 00130 /// @brief Add a path. 00131 void addPath(const sys::Path& path); 00132 00133 /// Add a set of paths to the list of paths that the linker will search. The 00134 /// Linker accumulates the set of libraries added. The \p paths will be 00135 /// added to the end of the Linker's list. Order will be retained. 00136 /// @brief Add a set of paths. 00137 void addPaths(const std::vector<std::string>& paths); 00138 00139 /// This method augments the Linker's list of library paths with the system 00140 /// paths of the host operating system, include LLVM_LIB_SEARCH_PATH. 00141 /// @brief Add the system paths. 00142 void addSystemPaths(); 00143 00144 /// Control optional linker behavior by setting a group of flags. The flags 00145 /// are defined in the ControlFlags enumeration. 00146 /// @see ControlFlags 00147 /// @brief Set control flags. 00148 void setFlags(unsigned flags) { Flags = flags; } 00149 00150 /// This method is the main interface to the linker. It can be used to 00151 /// link a set of linkage items into a module. A linkage item is either a 00152 /// file name with fully qualified path, or a library for which the Linker's 00153 /// LibraryPath will be utilized to locate the library. The bool value in 00154 /// the LinkItemKind should be set to true for libraries. This function 00155 /// allows linking to preserve the order of specification associated with 00156 /// the command line, or for other purposes. Each item will be linked in 00157 /// turn as it occurs in \p Items. 00158 /// @returns true if an error occurred, false otherwise 00159 /// @see LinkItemKind 00160 /// @see getLastError 00161 /// @throws nothing 00162 bool LinkInItems ( 00163 const ItemList& Items, ///< Set of libraries/files to link in 00164 ItemList& NativeItems ///< Output list of native files/libs 00165 ); 00166 00167 /// This function links the bitcode \p Files into the composite module. 00168 /// Note that this does not do any linking of unresolved symbols. The \p 00169 /// Files are all completely linked into \p HeadModule regardless of 00170 /// unresolved symbols. This function just loads each bitcode file and 00171 /// calls LinkInModule on them. 00172 /// @returns true if an error occurs, false otherwise 00173 /// @see getLastError 00174 /// @brief Link in multiple files. 00175 bool LinkInFiles ( 00176 const std::vector<sys::Path> & Files ///< Files to link in 00177 ); 00178 00179 /// This function links a single bitcode file, \p File, into the composite 00180 /// module. Note that this does not attempt to resolve symbols. This method 00181 /// just loads the bitcode file and calls LinkInModule on it. If an error 00182 /// occurs, the Linker's error string is set. 00183 /// @returns true if an error occurs, false otherwise 00184 /// @see getLastError 00185 /// @brief Link in a single file. 00186 bool LinkInFile( 00187 const sys::Path& File, ///< File to link in. 00188 bool &is_native ///< Indicates if the file is native object file 00189 ); 00190 00191 /// This function provides a way to selectively link in a set of modules, 00192 /// found in libraries, based on the unresolved symbols in the composite 00193 /// module. Each item in \p Libraries should be the base name of a library, 00194 /// as if given with the -l option of a linker tool. The Linker's LibPaths 00195 /// are searched for the \p Libraries and any found will be linked in with 00196 /// LinkInArchive. If an error occurs, the Linker's error string is set. 00197 /// @see LinkInArchive 00198 /// @see getLastError 00199 /// @returns true if an error occurs, false otherwise 00200 /// @brief Link libraries into the module 00201 bool LinkInLibraries ( 00202 const std::vector<std::string> & Libraries ///< Libraries to link in 00203 ); 00204 00205 /// This function provides a way to selectively link in a set of modules, 00206 /// found in one library, based on the unresolved symbols in the composite 00207 /// module.The \p Library should be the base name of a library, as if given 00208 /// with the -l option of a linker tool. The Linker's LibPaths are searched 00209 /// for the \p Library and if found, it will be linked in with via the 00210 /// LinkInArchive method. If an error occurs, the Linker's error string is 00211 /// set. 00212 /// @see LinkInArchive 00213 /// @see getLastError 00214 /// @returns true if an error occurs, false otherwise 00215 /// @brief Link one library into the module 00216 bool LinkInLibrary ( 00217 const StringRef &Library, ///< The library to link in 00218 bool& is_native ///< Indicates if lib a native library 00219 ); 00220 00221 /// This function links one bitcode archive, \p Filename, into the module. 00222 /// The archive is searched to resolve outstanding symbols. Any modules in 00223 /// the archive that resolve outstanding symbols will be linked in. The 00224 /// library is searched repeatedly until no more modules that resolve 00225 /// symbols can be found. If an error occurs, the error string is set. 00226 /// To speed up this function, ensure the the archive has been processed 00227 /// llvm-ranlib or the S option was given to llvm-ar when the archive was 00228 /// created. These tools add a symbol table to the archive which makes the 00229 /// search for undefined symbols much faster. 00230 /// @see getLastError 00231 /// @returns true if an error occurs, otherwise false. 00232 /// @brief Link in one archive. 00233 bool LinkInArchive( 00234 const sys::Path& Filename, ///< Filename of the archive to link 00235 bool& is_native ///< Indicates if archive is a native archive 00236 ); 00237 00238 /// This method links the \p Src module into the Linker's Composite module 00239 /// by calling LinkModules. All the other LinkIn* methods eventually 00240 /// result in calling this method to link a Module into the Linker's 00241 /// composite. 00242 /// @see LinkModules 00243 /// @returns True if an error occurs, false otherwise. 00244 /// @brief Link in a module. 00245 bool LinkInModule( 00246 Module* Src, ///< Module linked into \p Dest 00247 std::string* ErrorMsg = 0 /// Error/diagnostic string 00248 ) { 00249 return LinkModules(Composite, Src, ErrorMsg ); 00250 } 00251 00252 /// This is the heart of the linker. This method will take unconditional 00253 /// control of the \p Src module and link it into the \p Dest module. The 00254 /// \p Src module will be destructed or subsumed by this method. In either 00255 /// case it is not usable by the caller after this method is invoked. Only 00256 /// the \p Dest module will remain. The \p Src module is linked into the 00257 /// Linker's composite module such that types, global variables, functions, 00258 /// and etc. are matched and resolved. If an error occurs, this function 00259 /// returns true and ErrorMsg is set to a descriptive message about the 00260 /// error. 00261 /// @returns True if an error occurs, false otherwise. 00262 /// @brief Generically link two modules together. 00263 static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg); 00264 00265 /// This function looks through the Linker's LibPaths to find a library with 00266 /// the name \p Filename. If the library cannot be found, the returned path 00267 /// will be empty (i.e. sys::Path::isEmpty() will return true). 00268 /// @returns A sys::Path to the found library 00269 /// @brief Find a library from its short name. 00270 sys::Path FindLib(const StringRef &Filename); 00271 00272 /// @} 00273 /// @name Implementation 00274 /// @{ 00275 private: 00276 /// Read in and parse the bitcode file named by FN and return the 00277 /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs. 00278 std::auto_ptr<Module> LoadObject(const sys::Path& FN); 00279 00280 bool warning(const StringRef &message); 00281 bool error(const StringRef &message); 00282 void verbose(const StringRef &message); 00283 00284 /// @} 00285 /// @name Data 00286 /// @{ 00287 private: 00288 LLVMContext& Context; ///< The context for global information 00289 Module* Composite; ///< The composite module linked together 00290 std::vector<sys::Path> LibPaths; ///< The library search paths 00291 unsigned Flags; ///< Flags to control optional behavior. 00292 std::string Error; ///< Text of error that occurred. 00293 std::string ProgramName; ///< Name of the program being linked 00294 /// @} 00295 00296 }; 00297 00298 } // End llvm namespace 00299 00300 #endif