1 //===- llvm/Linker.h - Module Linker Interface ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface to the module/file/archive linker.
12 //===----------------------------------------------------------------------===//
22 namespace sys { class Path; }
28 /// This class provides the core functionality of linking in LLVM. It retains a
29 /// Module object which is the composite of the modules and libraries linked
30 /// into it. The composite Module can be retrieved via the getModule() method.
31 /// In this case the Linker still retains ownership of the Module. If the
32 /// releaseModule() method is used, the ownership of the Module is transferred
33 /// to the caller and the Linker object is only suitable for destruction.
34 /// The Linker can link Modules from memory, bitcode files, or bitcode
35 /// archives. It retains a set of search paths in which to find any libraries
36 /// presented to it. By default, the linker will generate error and warning
37 /// messages to stderr but this capability can be turned off with the
38 /// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
39 /// print out the linking actions it is taking with the Verbose flag.
40 /// @brief The LLVM Linker.
46 /// This type is used to pass the linkage items (libraries and files) to
47 /// the LinkItems function. It is composed of string/bool pairs. The string
48 /// provides the name of the file or library (as with the -l option). The
49 /// bool should be true for libraries and false for files, signifying
51 /// @brief A list of linkage items
52 typedef std::vector<std::pair<std::string,bool> > ItemList;
54 /// This enumeration is used to control various optional features of the
57 Verbose = 1, ///< Print to stderr what steps the linker is taking
58 QuietWarnings = 2, ///< Don't print warnings to stderr.
59 QuietErrors = 4 ///< Don't print errors to stderr.
63 DestroySource = 0, // Allow source module to be destroyed.
64 PreserveSource = 1 // Preserve the source module.
68 /// @name Constructors
71 /// Construct the Linker with an empty module which will be given the
72 /// name \p progname. \p progname will also be used for error messages.
73 /// @brief Construct with empty module
74 Linker(StringRef progname, ///< name of tool running linker
75 StringRef modulename, ///< name of linker's end-result module
76 LLVMContext &C, ///< Context for global info
77 unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
80 /// Construct the Linker with a previously defined module, \p aModule. Use
81 /// \p progname for the name of the program in error messages.
82 /// @brief Construct with existing module
83 Linker(StringRef progname, Module* aModule, unsigned Flags = 0);
85 /// Destruct the Linker.
93 /// This method gets the composite module into which linking is being
94 /// done. The Composite module starts out empty and accumulates modules
95 /// linked into it via the various LinkIn* methods. This method does not
96 /// release the Module to the caller. The Linker retains ownership and will
97 /// destruct the Module when the Linker is destructed.
98 /// @see releaseModule
99 /// @brief Get the linked/composite module.
100 Module* getModule() const { return Composite; }
102 /// This method releases the composite Module into which linking is being
103 /// done. Ownership of the composite Module is transferred to the caller who
104 /// must arrange for its destruct. After this method is called, the Linker
105 /// terminates the linking session for the returned Module. It will no
106 /// longer utilize the returned Module but instead resets itself for
107 /// subsequent linking as if the constructor had been called. The Linker's
108 /// LibPaths and flags to be reset, and memory will be released.
109 /// @brief Release the linked/composite module.
110 Module* releaseModule();
112 /// This method gets the list of libraries that form the path that the
113 /// Linker will search when it is presented with a library name.
114 /// @brief Get the Linkers library path
115 const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
117 /// This method returns an error string suitable for printing to the user.
118 /// The return value will be empty unless an error occurred in one of the
119 /// LinkIn* methods. In those cases, the LinkIn* methods will have returned
120 /// true, indicating an error occurred. At most one error is retained so
121 /// this function always returns the last error that occurred. Note that if
122 /// the Quiet control flag is not set, the error string will have already
123 /// been printed to stderr.
124 /// @brief Get the text of the last error that occurred.
125 const std::string &getLastError() const { return Error; }
131 /// Add a path to the list of paths that the Linker will search. The Linker
132 /// accumulates the set of libraries added
133 /// library paths for the target platform. The standard libraries will
134 /// always be searched last. The added libraries will be searched in the
136 /// @brief Add a path.
137 void addPath(const sys::Path& path);
139 /// Add a set of paths to the list of paths that the linker will search. The
140 /// Linker accumulates the set of libraries added. The \p paths will be
141 /// added to the end of the Linker's list. Order will be retained.
142 /// @brief Add a set of paths.
143 void addPaths(const std::vector<std::string>& paths);
145 /// This method augments the Linker's list of library paths with the system
146 /// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
147 /// @brief Add the system paths.
148 void addSystemPaths();
150 /// Control optional linker behavior by setting a group of flags. The flags
151 /// are defined in the ControlFlags enumeration.
152 /// @see ControlFlags
153 /// @brief Set control flags.
154 void setFlags(unsigned flags) { Flags = flags; }
156 /// This method links the \p Src module into the Linker's Composite module
157 /// by calling LinkModules. All the other LinkIn* methods eventually
158 /// result in calling this method to link a Module into the Linker's
161 /// @returns True if an error occurs, false otherwise.
162 /// @brief Link in a module.
164 Module* Src, ///< Module linked into \p Dest
165 std::string* ErrorMsg = 0 /// Error/diagnostic string
167 return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg );
170 /// This is the heart of the linker. This method will take unconditional
171 /// control of the \p Src module and link it into the \p Dest module. The
172 /// \p Src module will be destructed or subsumed by this method. In either
173 /// case it is not usable by the caller after this method is invoked. Only
174 /// the \p Dest module will remain. The \p Src module is linked into the
175 /// Linker's composite module such that types, global variables, functions,
176 /// and etc. are matched and resolved. If an error occurs, this function
177 /// returns true and ErrorMsg is set to a descriptive message about the
179 /// @returns True if an error occurs, false otherwise.
180 /// @brief Generically link two modules together.
181 static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
182 std::string* ErrorMsg);
185 /// @name Implementation
188 /// Read in and parse the bitcode file named by FN and return the
189 /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
190 std::auto_ptr<Module> LoadObject(const sys::Path& FN);
192 bool warning(StringRef message);
193 bool error(StringRef message);
194 void verbose(StringRef message);
200 LLVMContext& Context; ///< The context for global information
201 Module* Composite; ///< The composite module linked together
202 std::vector<sys::Path> LibPaths; ///< The library search paths
203 unsigned Flags; ///< Flags to control optional behavior.
204 std::string Error; ///< Text of error that occurred.
205 std::string ProgramName; ///< Name of the program being linked
210 } // End llvm namespace