2004-11-14 20:21:58 +00:00
|
|
|
//===- llvm/Linker.h - Module Linker Interface ------------------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-13 16:57:49 +00:00
|
|
|
//
|
2004-11-14 21:46:08 +00:00
|
|
|
// This file defines the interface to the module/file/archive linker.
|
2001-10-13 16:57:49 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 20:21:58 +00:00
|
|
|
#ifndef LLVM_LINKER_H
|
|
|
|
#define LLVM_LINKER_H
|
2001-10-13 16:57:49 +00:00
|
|
|
|
2004-12-13 02:58:05 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2005-12-26 13:34:00 +00:00
|
|
|
#include <memory>
|
2009-01-02 20:26:30 +00:00
|
|
|
#include <vector>
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2001-10-13 16:57:49 +00:00
|
|
|
class Module;
|
2009-07-01 16:58:40 +00:00
|
|
|
class LLVMContext;
|
2001-10-28 21:23:44 +00:00
|
|
|
|
2004-12-13 02:58:05 +00:00
|
|
|
/// This class provides the core functionality of linking in LLVM. It retains a
|
2005-04-21 20:19:05 +00:00
|
|
|
/// Module object which is the composite of the modules and libraries linked
|
|
|
|
/// into it. The composite Module can be retrieved via the getModule() method.
|
|
|
|
/// In this case the Linker still retains ownership of the Module. If the
|
|
|
|
/// releaseModule() method is used, the ownership of the Module is transferred
|
2004-12-13 02:58:05 +00:00
|
|
|
/// to the caller and the Linker object is only suitable for destruction.
|
2007-07-05 17:07:56 +00:00
|
|
|
/// The Linker can link Modules from memory, bitcode files, or bitcode
|
2005-04-21 20:19:05 +00:00
|
|
|
/// archives. It retains a set of search paths in which to find any libraries
|
|
|
|
/// presented to it. By default, the linker will generate error and warning
|
|
|
|
/// messages to std::cerr but this capability can be turned off with the
|
2004-12-13 02:58:05 +00:00
|
|
|
/// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
|
|
|
|
/// print out the linking actions it is taking with the Verbose flag.
|
|
|
|
/// @brief The LLVM Linker.
|
|
|
|
class Linker {
|
|
|
|
|
|
|
|
/// @name Types
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
/// This type is used to pass the linkage items (libraries and files) to
|
|
|
|
/// the LinkItems function. It is composed of string/bool pairs. The string
|
2005-04-21 20:19:05 +00:00
|
|
|
/// provides the name of the file or library (as with the -l option). The
|
|
|
|
/// bool should be true for libraries and false for files, signifying
|
2004-12-13 02:58:05 +00:00
|
|
|
/// "isLibrary".
|
|
|
|
/// @brief A list of linkage items
|
|
|
|
typedef std::vector<std::pair<std::string,bool> > ItemList;
|
|
|
|
|
|
|
|
/// This enumeration is used to control various optional features of the
|
2005-04-21 20:19:05 +00:00
|
|
|
/// linker.
|
2004-12-13 02:58:05 +00:00
|
|
|
enum ControlFlags {
|
|
|
|
Verbose = 1, ///< Print to std::cerr what steps the linker is taking
|
2008-10-25 17:57:20 +00:00
|
|
|
QuietWarnings = 2, ///< Don't print warnings to std::cerr.
|
|
|
|
QuietErrors = 4 ///< Don't print errors to std::cerr.
|
2004-12-13 02:58:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Constructors
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
/// Construct the Linker with an empty module which will be given the
|
|
|
|
/// name \p progname. \p progname will also be used for error messages.
|
|
|
|
/// @brief Construct with empty module
|
2005-12-13 20:00:37 +00:00
|
|
|
Linker(
|
|
|
|
const std::string& progname, ///< name of tool running linker
|
|
|
|
const std::string& modulename, ///< name of linker's end-result module
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& C, ///< Context for global info
|
2005-12-13 20:00:37 +00:00
|
|
|
unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
|
|
|
|
);
|
2004-12-13 02:58:05 +00:00
|
|
|
|
|
|
|
/// Construct the Linker with a previously defined module, \p aModule. Use
|
|
|
|
/// \p progname for the name of the program in error messages.
|
|
|
|
/// @brief Construct with existing module
|
|
|
|
Linker(const std::string& progname, Module* aModule, unsigned Flags = 0);
|
|
|
|
|
|
|
|
/// Destruct the Linker.
|
|
|
|
/// @brief Destructor
|
|
|
|
~Linker();
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Accessors
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
/// This method gets the composite module into which linking is being
|
2005-04-21 20:19:05 +00:00
|
|
|
/// done. The Composite module starts out empty and accumulates modules
|
2004-12-13 02:58:05 +00:00
|
|
|
/// linked into it via the various LinkIn* methods. This method does not
|
|
|
|
/// release the Module to the caller. The Linker retains ownership and will
|
2005-04-21 20:19:05 +00:00
|
|
|
/// destruct the Module when the Linker is destructed.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @see releaseModule
|
|
|
|
/// @brief Get the linked/composite module.
|
|
|
|
Module* getModule() const { return Composite; }
|
|
|
|
|
|
|
|
/// This method releases the composite Module into which linking is being
|
|
|
|
/// done. Ownership of the composite Module is transferred to the caller who
|
2005-04-21 20:19:05 +00:00
|
|
|
/// must arrange for its destruct. After this method is called, the Linker
|
|
|
|
/// terminates the linking session for the returned Module. It will no
|
|
|
|
/// longer utilize the returned Module but instead resets itself for
|
|
|
|
/// subsequent linking as if the constructor had been called. The Linker's
|
|
|
|
/// LibPaths and flags to be reset, and memory will be released.
|
|
|
|
/// @brief Release the linked/composite module.
|
2004-12-13 02:58:05 +00:00
|
|
|
Module* releaseModule();
|
|
|
|
|
|
|
|
/// This method gets the list of libraries that form the path that the
|
2005-04-21 20:19:05 +00:00
|
|
|
/// Linker will search when it is presented with a library name.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @brief Get the Linkers library path
|
|
|
|
const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
|
|
|
|
|
|
|
|
/// This method returns an error string suitable for printing to the user.
|
|
|
|
/// The return value will be empty unless an error occurred in one of the
|
|
|
|
/// LinkIn* methods. In those cases, the LinkIn* methods will have returned
|
2005-04-21 20:19:05 +00:00
|
|
|
/// true, indicating an error occurred. At most one error is retained so
|
2004-12-13 02:58:05 +00:00
|
|
|
/// this function always returns the last error that occurred. Note that if
|
|
|
|
/// the Quiet control flag is not set, the error string will have already
|
|
|
|
/// been printed to std::cerr.
|
|
|
|
/// @brief Get the text of the last error that occurred.
|
|
|
|
const std::string& getLastError() const { return Error; }
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Mutators
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
/// Add a path to the list of paths that the Linker will search. The Linker
|
|
|
|
/// accumulates the set of libraries added
|
|
|
|
/// library paths for the target platform. The standard libraries will
|
|
|
|
/// always be searched last. The added libraries will be searched in the
|
|
|
|
/// order added.
|
|
|
|
/// @brief Add a path.
|
|
|
|
void addPath(const sys::Path& path);
|
|
|
|
|
|
|
|
/// Add a set of paths to the list of paths that the linker will search. The
|
2005-04-21 20:19:05 +00:00
|
|
|
/// Linker accumulates the set of libraries added. The \p paths will be
|
2004-12-13 02:58:05 +00:00
|
|
|
/// added to the end of the Linker's list. Order will be retained.
|
|
|
|
/// @brief Add a set of paths.
|
|
|
|
void addPaths(const std::vector<std::string>& paths);
|
|
|
|
|
|
|
|
/// This method augments the Linker's list of library paths with the system
|
|
|
|
/// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
|
|
|
|
/// @brief Add the system paths.
|
|
|
|
void addSystemPaths();
|
|
|
|
|
|
|
|
/// Control optional linker behavior by setting a group of flags. The flags
|
|
|
|
/// are defined in the ControlFlags enumeration.
|
|
|
|
/// @see ControlFlags
|
|
|
|
/// @brief Set control flags.
|
|
|
|
void setFlags(unsigned flags) { Flags = flags; }
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// This method is the main interface to the linker. It can be used to
|
2004-12-13 02:58:05 +00:00
|
|
|
/// link a set of linkage items into a module. A linkage item is either a
|
|
|
|
/// file name with fully qualified path, or a library for which the Linker's
|
|
|
|
/// LibraryPath will be utilized to locate the library. The bool value in
|
2005-04-21 20:19:05 +00:00
|
|
|
/// the LinkItemKind should be set to true for libraries. This function
|
|
|
|
/// allows linking to preserve the order of specification associated with
|
|
|
|
/// the command line, or for other purposes. Each item will be linked in
|
|
|
|
/// turn as it occurs in \p Items.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @returns true if an error occurred, false otherwise
|
|
|
|
/// @see LinkItemKind
|
|
|
|
/// @see getLastError
|
|
|
|
/// @throws nothing
|
|
|
|
bool LinkInItems (
|
2006-01-10 03:14:40 +00:00
|
|
|
const ItemList& Items, ///< Set of libraries/files to link in
|
|
|
|
ItemList& NativeItems ///< Output list of native files/libs
|
2004-12-13 02:58:05 +00:00
|
|
|
);
|
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
/// This function links the bitcode \p Files into the composite module.
|
2005-04-21 20:19:05 +00:00
|
|
|
/// Note that this does not do any linking of unresolved symbols. The \p
|
|
|
|
/// Files are all completely linked into \p HeadModule regardless of
|
2007-07-05 17:07:56 +00:00
|
|
|
/// unresolved symbols. This function just loads each bitcode file and
|
2005-04-21 20:19:05 +00:00
|
|
|
/// calls LinkInModule on them.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @returns true if an error occurs, false otherwise
|
|
|
|
/// @see getLastError
|
|
|
|
/// @brief Link in multiple files.
|
|
|
|
bool LinkInFiles (
|
|
|
|
const std::vector<sys::Path> & Files ///< Files to link in
|
|
|
|
);
|
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
/// This function links a single bitcode file, \p File, into the composite
|
2004-12-13 02:58:05 +00:00
|
|
|
/// module. Note that this does not attempt to resolve symbols. This method
|
2007-07-05 17:07:56 +00:00
|
|
|
/// just loads the bitcode file and calls LinkInModule on it. If an error
|
2004-12-13 02:58:05 +00:00
|
|
|
/// occurs, the Linker's error string is set.
|
|
|
|
/// @returns true if an error occurs, false otherwise
|
|
|
|
/// @see getLastError
|
|
|
|
/// @brief Link in a single file.
|
|
|
|
bool LinkInFile(
|
2007-04-04 06:32:01 +00:00
|
|
|
const sys::Path& File, ///< File to link in.
|
|
|
|
bool &is_native ///< Indicates if the file is native object file
|
2004-12-13 02:58:05 +00:00
|
|
|
);
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// This function provides a way to selectively link in a set of modules,
|
|
|
|
/// found in libraries, based on the unresolved symbols in the composite
|
2004-12-13 02:58:05 +00:00
|
|
|
/// module. Each item in \p Libraries should be the base name of a library,
|
|
|
|
/// as if given with the -l option of a linker tool. The Linker's LibPaths
|
2005-04-21 20:19:05 +00:00
|
|
|
/// are searched for the \p Libraries and any found will be linked in with
|
2004-12-13 02:58:05 +00:00
|
|
|
/// LinkInArchive. If an error occurs, the Linker's error string is set.
|
|
|
|
/// @see LinkInArchive
|
|
|
|
/// @see getLastError
|
|
|
|
/// @returns true if an error occurs, false otherwise
|
|
|
|
/// @brief Link libraries into the module
|
|
|
|
bool LinkInLibraries (
|
|
|
|
const std::vector<std::string> & Libraries ///< Libraries to link in
|
|
|
|
);
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// This function provides a way to selectively link in a set of modules,
|
2004-12-13 02:58:05 +00:00
|
|
|
/// found in one library, based on the unresolved symbols in the composite
|
2005-04-21 20:19:05 +00:00
|
|
|
/// module.The \p Library should be the base name of a library, as if given
|
2004-12-13 02:58:05 +00:00
|
|
|
/// with the -l option of a linker tool. The Linker's LibPaths are searched
|
2007-08-05 20:06:04 +00:00
|
|
|
/// for the \p Library and if found, it will be linked in with via the
|
2004-12-13 02:58:05 +00:00
|
|
|
/// LinkInArchive method. If an error occurs, the Linker's error string is
|
|
|
|
/// set.
|
|
|
|
/// @see LinkInArchive
|
|
|
|
/// @see getLastError
|
|
|
|
/// @returns true if an error occurs, false otherwise
|
|
|
|
/// @brief Link one library into the module
|
|
|
|
bool LinkInLibrary (
|
2006-01-10 03:14:40 +00:00
|
|
|
const std::string& Library, ///< The library to link in
|
2007-04-04 06:32:01 +00:00
|
|
|
bool& is_native ///< Indicates if lib a native library
|
2004-12-13 02:58:05 +00:00
|
|
|
);
|
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
/// This function links one bitcode archive, \p Filename, into the module.
|
2005-04-21 20:19:05 +00:00
|
|
|
/// The archive is searched to resolve outstanding symbols. Any modules in
|
|
|
|
/// the archive that resolve outstanding symbols will be linked in. The
|
|
|
|
/// library is searched repeatedly until no more modules that resolve
|
2004-12-13 02:58:05 +00:00
|
|
|
/// symbols can be found. If an error occurs, the error string is set.
|
2005-04-21 20:19:05 +00:00
|
|
|
/// To speed up this function, ensure the the archive has been processed
|
2004-12-13 02:58:05 +00:00
|
|
|
/// llvm-ranlib or the S option was given to llvm-ar when the archive was
|
|
|
|
/// created. These tools add a symbol table to the archive which makes the
|
|
|
|
/// search for undefined symbols much faster.
|
|
|
|
/// @see getLastError
|
2005-04-21 20:19:05 +00:00
|
|
|
/// @returns true if an error occurs, otherwise false.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @brief Link in one archive.
|
2005-04-21 20:19:05 +00:00
|
|
|
bool LinkInArchive(
|
2007-04-30 00:29:39 +00:00
|
|
|
const sys::Path& Filename, ///< Filename of the archive to link
|
|
|
|
bool& is_native ///< Indicates if archive is a native archive
|
2004-12-13 02:58:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// This method links the \p Src module into the Linker's Composite module
|
2005-04-21 20:19:05 +00:00
|
|
|
/// by calling LinkModules. All the other LinkIn* methods eventually
|
|
|
|
/// result in calling this method to link a Module into the Linker's
|
|
|
|
/// composite.
|
2004-12-13 02:58:05 +00:00
|
|
|
/// @see LinkModules
|
|
|
|
/// @returns True if an error occurs, false otherwise.
|
|
|
|
/// @brief Link in a module.
|
|
|
|
bool LinkInModule(
|
2006-11-11 11:54:25 +00:00
|
|
|
Module* Src, ///< Module linked into \p Dest
|
|
|
|
std::string* ErrorMsg = 0 /// Error/diagnostic string
|
2006-11-11 19:59:25 +00:00
|
|
|
) {
|
|
|
|
return LinkModules(Composite, Src, ErrorMsg );
|
|
|
|
}
|
2004-12-13 02:58:05 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// This is the heart of the linker. This method will take unconditional
|
2004-12-13 02:58:05 +00:00
|
|
|
/// control of the \p Src module and link it into the \p Dest module. The
|
|
|
|
/// \p Src module will be destructed or subsumed by this method. In either
|
|
|
|
/// case it is not usable by the caller after this method is invoked. Only
|
2005-04-21 20:19:05 +00:00
|
|
|
/// the \p Dest module will remain. The \p Src module is linked into the
|
2004-12-13 02:58:05 +00:00
|
|
|
/// Linker's composite module such that types, global variables, functions,
|
|
|
|
/// and etc. are matched and resolved. If an error occurs, this function
|
|
|
|
/// returns true and ErrorMsg is set to a descriptive message about the
|
|
|
|
/// error.
|
|
|
|
/// @returns True if an error occurs, false otherwise.
|
|
|
|
/// @brief Generically link two modules together.
|
|
|
|
static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
|
|
|
|
|
|
|
|
/// This function looks through the Linker's LibPaths to find a library with
|
|
|
|
/// the name \p Filename. If the library cannot be found, the returned path
|
|
|
|
/// will be empty (i.e. sys::Path::isEmpty() will return true).
|
|
|
|
/// @returns A sys::Path to the found library
|
|
|
|
/// @brief Find a library from its short name.
|
|
|
|
sys::Path FindLib(const std::string &Filename);
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Implementation
|
|
|
|
/// @{
|
|
|
|
private:
|
2007-07-05 17:07:56 +00:00
|
|
|
/// Read in and parse the bitcode file named by FN and return the
|
2004-12-13 02:58:05 +00:00
|
|
|
/// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
|
|
|
|
std::auto_ptr<Module> LoadObject(const sys::Path& FN);
|
|
|
|
|
|
|
|
bool warning(const std::string& message);
|
|
|
|
bool error(const std::string& message);
|
|
|
|
void verbose(const std::string& message);
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Data
|
|
|
|
/// @{
|
|
|
|
private:
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& Context; ///< The context for global information
|
2004-12-13 02:58:05 +00:00
|
|
|
Module* Composite; ///< The composite module linked together
|
|
|
|
std::vector<sys::Path> LibPaths; ///< The library search paths
|
|
|
|
unsigned Flags; ///< Flags to control optional behavior.
|
|
|
|
std::string Error; ///< Text of error that occurred.
|
|
|
|
std::string ProgramName; ///< Name of the program being linked
|
|
|
|
/// @}
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2004-12-13 02:58:05 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-10-13 16:57:49 +00:00
|
|
|
#endif
|