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
//===----------------------------------------------------------------------===//
19
#include "llvm/ADT/StringRef.h"
22
namespace sys { class Path; }
27
/// This class provides the core functionality of linking in LLVM. It retains a
28
/// Module object which is the composite of the modules and libraries linked
29
/// into it. The composite Module can be retrieved via the getModule() method.
30
/// In this case the Linker still retains ownership of the Module. If the
31
/// releaseModule() method is used, the ownership of the Module is transferred
32
/// to the caller and the Linker object is only suitable for destruction.
33
/// The Linker can link Modules from memory, bitcode files, or bitcode
34
/// archives. It retains a set of search paths in which to find any libraries
35
/// presented to it. By default, the linker will generate error and warning
36
/// messages to stderr but this capability can be turned off with the
37
/// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
38
/// print out the linking actions it is taking with the Verbose flag.
39
/// @brief The LLVM Linker.
45
/// This type is used to pass the linkage items (libraries and files) to
46
/// the LinkItems function. It is composed of string/bool pairs. The string
47
/// provides the name of the file or library (as with the -l option). The
48
/// bool should be true for libraries and false for files, signifying
50
/// @brief A list of linkage items
51
typedef std::vector<std::pair<std::string,bool> > ItemList;
53
/// This enumeration is used to control various optional features of the
56
Verbose = 1, ///< Print to stderr what steps the linker is taking
57
QuietWarnings = 2, ///< Don't print warnings to stderr.
58
QuietErrors = 4 ///< Don't print errors to stderr.
62
/// @name Constructors
65
/// Construct the Linker with an empty module which will be given the
66
/// name \p progname. \p progname will also be used for error messages.
67
/// @brief Construct with empty module
68
Linker(StringRef progname, ///< name of tool running linker
69
StringRef modulename, ///< name of linker's end-result module
70
LLVMContext &C, ///< Context for global info
71
unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
74
/// Construct the Linker with a previously defined module, \p aModule. Use
75
/// \p progname for the name of the program in error messages.
76
/// @brief Construct with existing module
77
Linker(StringRef progname, Module* aModule, unsigned Flags = 0);
79
/// Destruct the Linker.
87
/// This method gets the composite module into which linking is being
88
/// done. The Composite module starts out empty and accumulates modules
89
/// linked into it via the various LinkIn* methods. This method does not
90
/// release the Module to the caller. The Linker retains ownership and will
91
/// destruct the Module when the Linker is destructed.
92
/// @see releaseModule
93
/// @brief Get the linked/composite module.
94
Module* getModule() const { return Composite; }
96
/// This method releases the composite Module into which linking is being
97
/// done. Ownership of the composite Module is transferred to the caller who
98
/// must arrange for its destruct. After this method is called, the Linker
99
/// terminates the linking session for the returned Module. It will no
100
/// longer utilize the returned Module but instead resets itself for
101
/// subsequent linking as if the constructor had been called. The Linker's
102
/// LibPaths and flags to be reset, and memory will be released.
103
/// @brief Release the linked/composite module.
104
Module* releaseModule();
106
/// This method gets the list of libraries that form the path that the
107
/// Linker will search when it is presented with a library name.
108
/// @brief Get the Linkers library path
109
const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
111
/// This method returns an error string suitable for printing to the user.
112
/// The return value will be empty unless an error occurred in one of the
113
/// LinkIn* methods. In those cases, the LinkIn* methods will have returned
114
/// true, indicating an error occurred. At most one error is retained so
115
/// this function always returns the last error that occurred. Note that if
116
/// the Quiet control flag is not set, the error string will have already
117
/// been printed to stderr.
118
/// @brief Get the text of the last error that occurred.
119
const std::string &getLastError() const { return Error; }
125
/// Add a path to the list of paths that the Linker will search. The Linker
126
/// accumulates the set of libraries added
127
/// library paths for the target platform. The standard libraries will
128
/// always be searched last. The added libraries will be searched in the
130
/// @brief Add a path.
131
void addPath(const sys::Path& path);
133
/// Add a set of paths to the list of paths that the linker will search. The
134
/// Linker accumulates the set of libraries added. The \p paths will be
135
/// added to the end of the Linker's list. Order will be retained.
136
/// @brief Add a set of paths.
137
void addPaths(const std::vector<std::string>& paths);
139
/// This method augments the Linker's list of library paths with the system
140
/// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
141
/// @brief Add the system paths.
142
void addSystemPaths();
144
/// Control optional linker behavior by setting a group of flags. The flags
145
/// are defined in the ControlFlags enumeration.
146
/// @see ControlFlags
147
/// @brief Set control flags.
148
void setFlags(unsigned flags) { Flags = flags; }
150
/// This method is the main interface to the linker. It can be used to
151
/// link a set of linkage items into a module. A linkage item is either a
152
/// file name with fully qualified path, or a library for which the Linker's
153
/// LibraryPath will be utilized to locate the library. The bool value in
154
/// the LinkItemKind should be set to true for libraries. This function
155
/// allows linking to preserve the order of specification associated with
156
/// the command line, or for other purposes. Each item will be linked in
157
/// turn as it occurs in \p Items.
158
/// @returns true if an error occurred, false otherwise
159
/// @see LinkItemKind
160
/// @see getLastError
163
const ItemList& Items, ///< Set of libraries/files to link in
164
ItemList& NativeItems ///< Output list of native files/libs
167
/// This function links the bitcode \p Files into the composite module.
168
/// Note that this does not do any linking of unresolved symbols. The \p
169
/// Files are all completely linked into \p HeadModule regardless of
170
/// unresolved symbols. This function just loads each bitcode file and
171
/// calls LinkInModule on them.
172
/// @returns true if an error occurs, false otherwise
173
/// @see getLastError
174
/// @brief Link in multiple files.
176
const std::vector<sys::Path> & Files ///< Files to link in
179
/// This function links a single bitcode file, \p File, into the composite
180
/// module. Note that this does not attempt to resolve symbols. This method
181
/// just loads the bitcode file and calls LinkInModule on it. If an error
182
/// occurs, the Linker's error string is set.
183
/// @returns true if an error occurs, false otherwise
184
/// @see getLastError
185
/// @brief Link in a single file.
187
const sys::Path& File, ///< File to link in.
188
bool &is_native ///< Indicates if the file is native object file
191
/// This function provides a way to selectively link in a set of modules,
192
/// found in libraries, based on the unresolved symbols in the composite
193
/// module. Each item in \p Libraries should be the base name of a library,
194
/// as if given with the -l option of a linker tool. The Linker's LibPaths
195
/// are searched for the \p Libraries and any found will be linked in with
196
/// LinkInArchive. If an error occurs, the Linker's error string is set.
197
/// @see LinkInArchive
198
/// @see getLastError
199
/// @returns true if an error occurs, false otherwise
200
/// @brief Link libraries into the module
201
bool LinkInLibraries (
202
const std::vector<std::string> & Libraries ///< Libraries to link in
205
/// This function provides a way to selectively link in a set of modules,
206
/// found in one library, based on the unresolved symbols in the composite
207
/// module.The \p Library should be the base name of a library, as if given
208
/// with the -l option of a linker tool. The Linker's LibPaths are searched
209
/// for the \p Library and if found, it will be linked in with via the
210
/// LinkInArchive method. If an error occurs, the Linker's error string is
212
/// @see LinkInArchive
213
/// @see getLastError
214
/// @returns true if an error occurs, false otherwise
215
/// @brief Link one library into the module
217
StringRef Library, ///< The library to link in
218
bool& is_native ///< Indicates if lib a native library
221
/// This function links one bitcode archive, \p Filename, into the module.
222
/// The archive is searched to resolve outstanding symbols. Any modules in
223
/// the archive that resolve outstanding symbols will be linked in. The
224
/// library is searched repeatedly until no more modules that resolve
225
/// symbols can be found. If an error occurs, the error string is set.
226
/// To speed up this function, ensure the archive has been processed
227
/// llvm-ranlib or the S option was given to llvm-ar when the archive was
228
/// created. These tools add a symbol table to the archive which makes the
229
/// search for undefined symbols much faster.
230
/// @see getLastError
231
/// @returns true if an error occurs, otherwise false.
232
/// @brief Link in one archive.
234
const sys::Path& Filename, ///< Filename of the archive to link
235
bool& is_native ///< Indicates if archive is a native archive
238
/// This method links the \p Src module into the Linker's Composite module
239
/// by calling LinkModules. All the other LinkIn* methods eventually
240
/// result in calling this method to link a Module into the Linker's
243
/// @returns True if an error occurs, false otherwise.
244
/// @brief Link in a module.
246
Module* Src, ///< Module linked into \p Dest
247
std::string* ErrorMsg = 0 /// Error/diagnostic string
249
return LinkModules(Composite, Src, ErrorMsg );
252
/// This is the heart of the linker. This method will take unconditional
253
/// control of the \p Src module and link it into the \p Dest module. The
254
/// \p Src module will be destructed or subsumed by this method. In either
255
/// case it is not usable by the caller after this method is invoked. Only
256
/// the \p Dest module will remain. The \p Src module is linked into the
257
/// Linker's composite module such that types, global variables, functions,
258
/// and etc. are matched and resolved. If an error occurs, this function
259
/// returns true and ErrorMsg is set to a descriptive message about the
261
/// @returns True if an error occurs, false otherwise.
262
/// @brief Generically link two modules together.
263
static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
265
/// This function looks through the Linker's LibPaths to find a library with
266
/// the name \p Filename. If the library cannot be found, the returned path
267
/// will be empty (i.e. sys::Path::isEmpty() will return true).
268
/// @returns A sys::Path to the found library
269
/// @brief Find a library from its short name.
270
sys::Path FindLib(StringRef Filename);
273
/// @name Implementation
276
/// Read in and parse the bitcode file named by FN and return the
277
/// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
278
std::auto_ptr<Module> LoadObject(const sys::Path& FN);
280
bool warning(StringRef message);
281
bool error(StringRef message);
282
void verbose(StringRef message);
288
LLVMContext& Context; ///< The context for global information
289
Module* Composite; ///< The composite module linked together
290
std::vector<sys::Path> LibPaths; ///< The library search paths
291
unsigned Flags; ///< Flags to control optional behavior.
292
std::string Error; ///< Text of error that occurred.
293
std::string ProgramName; ///< Name of the program being linked
298
} // End llvm namespace