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