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