d1854cc6296591692bbcb45e99c46d90c8fdf279
[oota-llvm.git] / include / llvm / Linker.h
1 //===- llvm/Linker.h - Module Linker Interface ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface to the module/file/archive linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LINKER_H
15 #define LLVM_LINKER_H
16
17 #include "llvm/System/Path.h"
18 #include <vector>
19
20 namespace llvm {
21
22 class Module;
23
24 /// This class provides the core functionality of linking in LLVM. It retains a
25 /// Module object which is the composite of the modules and libraries linked
26 /// into it. The composite Module can be retrieved via the getModule() method.
27 /// In this case the Linker still retains ownership of the Module. If the
28 /// releaseModule() method is used, the ownership of the Module is transferred
29 /// to the caller and the Linker object is only suitable for destruction.
30 /// The Linker can link Modules from memory, bytecode files, or bytecode
31 /// archives.  It retains a set of search paths in which to find any libraries
32 /// presented to it. By default, the linker will generate error and warning
33 /// messages to std::cerr but this capability can be turned off with the
34 /// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
35 /// print out the linking actions it is taking with the Verbose flag.
36 /// @brief The LLVM Linker.
37 class Linker {
38
39   /// @name Types
40   /// @{
41   public:
42     /// This type is used to pass the linkage items (libraries and files) to
43     /// the LinkItems function. It is composed of string/bool pairs. The string
44     /// provides the name of the file or library (as with the -l option). The
45     /// bool should be true for libraries and false for files, signifying
46     /// "isLibrary".
47     /// @brief A list of linkage items
48     typedef std::vector<std::pair<std::string,bool> > ItemList;
49
50     /// This enumeration is used to control various optional features of the
51     /// linker.
52     enum ControlFlags {
53       Verbose       = 1, ///< Print to std::cerr what steps the linker is taking
54       QuietWarnings = 2, ///< Don't print errors and warnings to std::cerr.
55       QuietErrors   = 4, ///< Indicate that this link is for a native executable
56     };
57
58   /// @}
59   /// @name Constructors
60   /// @{
61   public:
62     /// Construct the Linker with an empty module which will be given the
63     /// name \p progname. \p progname will also be used for error messages.
64     /// @brief Construct with empty module
65     Linker(
66         const std::string& progname, ///< name of tool running linker
67         const std::string& modulename, ///< name of linker's end-result module
68         unsigned Flags = 0  ///< ControlFlags (one or more |'d together)
69     );
70
71     /// Construct the Linker with a previously defined module, \p aModule. Use
72     /// \p progname for the name of the program in error messages.
73     /// @brief Construct with existing module
74     Linker(const std::string& progname, Module* aModule, unsigned Flags = 0);
75
76     /// Destruct the Linker.
77     /// @brief Destructor
78     ~Linker();
79
80   /// @}
81   /// @name Accessors
82   /// @{
83   public:
84     /// This method gets the composite module into which linking is being
85     /// done. The Composite module starts out empty and accumulates modules
86     /// linked into it via the various LinkIn* methods. This method does not
87     /// release the Module to the caller. The Linker retains ownership and will
88     /// destruct the Module when the Linker is destructed.
89     /// @see releaseModule
90     /// @brief Get the linked/composite module.
91     Module* getModule() const { return Composite; }
92
93     /// This method releases the composite Module into which linking is being
94     /// done. Ownership of the composite Module is transferred to the caller who
95     /// must arrange for its destruct. After this method is called, the Linker
96     /// terminates the linking session for the returned Module. It will no
97     /// longer utilize the returned Module but instead resets itself for
98     /// subsequent linking as if the constructor had been called. The Linker's
99     /// LibPaths and flags to be reset, and memory will be released.
100     /// @brief Release the linked/composite module.
101     Module* releaseModule();
102
103     /// This method gets the list of libraries that form the path that the
104     /// Linker will search when it is presented with a library name.
105     /// @brief Get the Linkers library path
106     const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
107
108     /// This method returns an error string suitable for printing to the user.
109     /// The return value will be empty unless an error occurred in one of the
110     /// LinkIn* methods. In those cases, the LinkIn* methods will have returned
111     /// true, indicating an error occurred. At most one error is retained so
112     /// this function always returns the last error that occurred. Note that if
113     /// the Quiet control flag is not set, the error string will have already
114     /// been printed to std::cerr.
115     /// @brief Get the text of the last error that occurred.
116     const std::string& getLastError() const { return Error; }
117
118   /// @}
119   /// @name Mutators
120   /// @{
121   public:
122     /// Add a path to the list of paths that the Linker will search. The Linker
123     /// accumulates the set of libraries added
124     /// library paths for the target platform. The standard libraries will
125     /// always be searched last. The added libraries will be searched in the
126     /// order added.
127     /// @brief Add a path.
128     void addPath(const sys::Path& path);
129
130     /// Add a set of paths to the list of paths that the linker will search. The
131     /// Linker accumulates the set of libraries added. The \p paths will be
132     /// added to the end of the Linker's list. Order will be retained.
133     /// @brief Add a set of paths.
134     void addPaths(const std::vector<std::string>& paths);
135
136     /// This method augments the Linker's list of library paths with the system
137     /// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
138     /// @brief Add the system paths.
139     void addSystemPaths();
140
141     /// Control optional linker behavior by setting a group of flags. The flags
142     /// are defined in the ControlFlags enumeration.
143     /// @see ControlFlags
144     /// @brief Set control flags.
145     void setFlags(unsigned flags) { Flags = flags; }
146
147     /// This method is the main interface to the linker. It can be used to
148     /// link a set of linkage items into a module. A linkage item is either a
149     /// file name with fully qualified path, or a library for which the Linker's
150     /// LibraryPath will be utilized to locate the library. The bool value in
151     /// the LinkItemKind should be set to true for libraries.  This function
152     /// allows linking to preserve the order of specification associated with
153     /// the command line, or for other purposes. Each item will be linked in
154     /// turn as it occurs in \p Items.
155     /// @returns true if an error occurred, false otherwise
156     /// @see LinkItemKind
157     /// @see getLastError
158     /// @throws nothing
159     bool LinkInItems (
160       const ItemList& Items // Set of libraries/files to link in
161     );
162
163     /// This function links the bytecode \p Files into the composite module.
164     /// Note that this does not do any linking of unresolved symbols. The \p
165     /// Files are all completely linked into \p HeadModule regardless of
166     /// unresolved symbols. This function just loads each bytecode file and
167     /// calls LinkInModule on them.
168     /// @returns true if an error occurs, false otherwise
169     /// @see getLastError
170     /// @brief Link in multiple files.
171     bool LinkInFiles (
172       const std::vector<sys::Path> & Files ///< Files to link in
173     );
174
175     /// This function links a single bytecode file, \p File, into the composite
176     /// module. Note that this does not attempt to resolve symbols. This method
177     /// just loads the bytecode file and calls LinkInModule on it. If an error
178     /// occurs, the Linker's error string is set.
179     /// @returns true if an error occurs, false otherwise
180     /// @see getLastError
181     /// @brief Link in a single file.
182     bool LinkInFile(
183       const sys::Path& File ///< File to link in.
184     );
185
186     /// This function provides a way to selectively link in a set of modules,
187     /// found in libraries, based on the unresolved symbols in the composite
188     /// module. Each item in \p Libraries should be the base name of a library,
189     /// as if given with the -l option of a linker tool.  The Linker's LibPaths
190     /// are searched for the \p Libraries and any found will be linked in with
191     /// LinkInArchive.  If an error occurs, the Linker's error string is set.
192     /// @see LinkInArchive
193     /// @see getLastError
194     /// @returns true if an error occurs, false otherwise
195     /// @brief Link libraries into the module
196     bool LinkInLibraries (
197       const std::vector<std::string> & Libraries ///< Libraries to link in
198     );
199
200     /// This function provides a way to selectively link in a set of modules,
201     /// found in one library, based on the unresolved symbols in the composite
202     /// module.The \p Library should be the base name of a library, as if given
203     /// with the -l option of a linker tool. The Linker's LibPaths are searched
204     /// for the \P Library and if found, it will be linked in with via the
205     /// LinkInArchive method. If an error occurs, the Linker's error string is
206     /// set.
207     /// @see LinkInArchive
208     /// @see getLastError
209     /// @returns true if an error occurs, false otherwise
210     /// @brief Link one library into the module
211     bool LinkInLibrary (
212       const std::string& Library ///< The library to link in
213     );
214
215     /// This function links one bytecode archive, \p Filename, into the module.
216     /// The archive is searched to resolve outstanding symbols. Any modules in
217     /// the archive that resolve outstanding symbols will be linked in. The
218     /// library is searched repeatedly until no more modules that resolve
219     /// symbols can be found. If an error occurs, the error string is  set.
220     /// To speed up this function, ensure the the archive has been processed
221     /// llvm-ranlib or the S option was given to llvm-ar when the archive was
222     /// created. These tools add a symbol table to the archive which makes the
223     /// search for undefined symbols much faster.
224     /// @see getLastError
225     /// @returns true if an error occurs, otherwise false.
226     /// @brief Link in one archive.
227     bool LinkInArchive(
228       const sys::Path& Filename ///< Filename of the archive to link
229     );
230
231     /// This method links the \p Src module into the Linker's Composite module
232     /// by calling LinkModules.  All the other LinkIn* methods eventually
233     /// result in calling this method to link a Module into the Linker's
234     /// composite.
235     /// @see LinkModules
236     /// @returns True if an error occurs, false otherwise.
237     /// @brief Link in a module.
238     bool LinkInModule(
239       Module* Src     ///< Module linked into \p Dest
240     ) { return LinkModules(Composite, Src, &Error); }
241
242     /// This is the heart of the linker. This method will take unconditional
243     /// control of the \p Src module and link it into the \p Dest module. The
244     /// \p Src module will be destructed or subsumed by this method. In either
245     /// case it is not usable by the caller after this method is invoked. Only
246     /// the \p Dest module will remain. The \p Src module is linked into the
247     /// Linker's composite module such that types, global variables, functions,
248     /// and etc. are matched and resolved.  If an error occurs, this function
249     /// returns true and ErrorMsg is set to a descriptive message about the
250     /// error.
251     /// @returns True if an error occurs, false otherwise.
252     /// @brief Generically link two modules together.
253     static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
254
255     /// This function looks through the Linker's LibPaths to find a library with
256     /// the name \p Filename. If the library cannot be found, the returned path
257     /// will be empty (i.e. sys::Path::isEmpty() will return true).
258     /// @returns A sys::Path to the found library
259     /// @brief Find a library from its short name.
260     sys::Path FindLib(const std::string &Filename);
261
262   /// @}
263   /// @name Implementation
264   /// @{
265   private:
266     /// Read in and parse the bytecode file named by FN and return the
267     /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
268     std::auto_ptr<Module> LoadObject(const sys::Path& FN);
269
270     bool warning(const std::string& message);
271     bool error(const std::string& message);
272     void verbose(const std::string& message);
273
274   /// @}
275   /// @name Data
276   /// @{
277   private:
278     Module* Composite; ///< The composite module linked together
279     std::vector<sys::Path> LibPaths; ///< The library search paths
280     unsigned Flags;    ///< Flags to control optional behavior.
281     std::string Error; ///< Text of error that occurred.
282     std::string ProgramName; ///< Name of the program being linked
283   /// @}
284
285 };
286
287 } // End llvm namespace
288
289 #endif