]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/Linker.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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 <memory>
18 #include <vector>
19 #include "llvm/ADT/StringRef.h"
20
21 namespace llvm {
22   namespace sys { class Path; }
23
24 class Module;
25 class LLVMContext;
26
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.
40 class Linker {
41
42   /// @name Types
43   /// @{
44   public:
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
49     /// "isLibrary".
50     /// @brief A list of linkage items
51     typedef std::vector<std::pair<std::string,bool> > ItemList;
52
53     /// This enumeration is used to control various optional features of the
54     /// linker.
55     enum ControlFlags {
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.
59     };
60
61   /// @}
62   /// @name Constructors
63   /// @{
64   public:
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)
72     );
73
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);
78
79     /// Destruct the Linker.
80     /// @brief Destructor
81     ~Linker();
82
83   /// @}
84   /// @name Accessors
85   /// @{
86   public:
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; }
95
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();
105
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; }
110
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; }
120
121   /// @}
122   /// @name Mutators
123   /// @{
124   public:
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
129     /// order added.
130     /// @brief Add a path.
131     void addPath(const sys::Path& path);
132
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);
138
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();
143
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; }
149
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
161     bool LinkInItems (
162       const ItemList& Items, ///< Set of libraries/files to link in
163       ItemList& NativeItems  ///< Output list of native files/libs
164     );
165
166     /// This function links the bitcode \p Files into the composite module.
167     /// Note that this does not do any linking of unresolved symbols. The \p
168     /// Files are all completely linked into \p HeadModule regardless of
169     /// unresolved symbols. This function just loads each bitcode file and
170     /// calls LinkInModule on them.
171     /// @returns true if an error occurs, false otherwise
172     /// @see getLastError
173     /// @brief Link in multiple files.
174     bool LinkInFiles (
175       const std::vector<sys::Path> & Files ///< Files to link in
176     );
177
178     /// This function links a single bitcode file, \p File, into the composite
179     /// module. Note that this does not attempt to resolve symbols. This method
180     /// just loads the bitcode file and calls LinkInModule on it. If an error
181     /// occurs, the Linker's error string is set.
182     /// @returns true if an error occurs, false otherwise
183     /// @see getLastError
184     /// @brief Link in a single file.
185     bool LinkInFile(
186       const sys::Path& File, ///< File to link in.
187       bool &is_native        ///< Indicates if the file is native object file
188     );
189
190     /// This function provides a way to selectively link in a set of modules,
191     /// found in libraries, based on the unresolved symbols in the composite
192     /// module. Each item in \p Libraries should be the base name of a library,
193     /// as if given with the -l option of a linker tool.  The Linker's LibPaths
194     /// are searched for the \p Libraries and any found will be linked in with
195     /// LinkInArchive.  If an error occurs, the Linker's error string is set.
196     /// @see LinkInArchive
197     /// @see getLastError
198     /// @returns true if an error occurs, false otherwise
199     /// @brief Link libraries into the module
200     bool LinkInLibraries (
201       const std::vector<std::string> & Libraries ///< Libraries to link in
202     );
203
204     /// This function provides a way to selectively link in a set of modules,
205     /// found in one library, based on the unresolved symbols in the composite
206     /// module.The \p Library should be the base name of a library, as if given
207     /// with the -l option of a linker tool. The Linker's LibPaths are searched
208     /// for the \p Library and if found, it will be linked in with via the
209     /// LinkInArchive method. If an error occurs, the Linker's error string is
210     /// set.
211     /// @see LinkInArchive
212     /// @see getLastError
213     /// @returns true if an error occurs, false otherwise
214     /// @brief Link one library into the module
215     bool LinkInLibrary (
216       StringRef Library, ///< The library to link in
217       bool& is_native    ///< Indicates if lib a native library
218     );
219
220     /// This function links one bitcode archive, \p Filename, into the module.
221     /// The archive is searched to resolve outstanding symbols. Any modules in
222     /// the archive that resolve outstanding symbols will be linked in. The
223     /// library is searched repeatedly until no more modules that resolve
224     /// symbols can be found. If an error occurs, the error string is  set.
225     /// To speed up this function, ensure the archive has been processed
226     /// llvm-ranlib or the S option was given to llvm-ar when the archive was
227     /// created. These tools add a symbol table to the archive which makes the
228     /// search for undefined symbols much faster.
229     /// @see getLastError
230     /// @returns true if an error occurs, otherwise false.
231     /// @brief Link in one archive.
232     bool LinkInArchive(
233       const sys::Path& Filename, ///< Filename of the archive to link
234       bool& is_native            ///<  Indicates if archive is a native archive
235     );
236
237     /// This method links the \p Src module into the Linker's Composite module
238     /// by calling LinkModules.  All the other LinkIn* methods eventually
239     /// result in calling this method to link a Module into the Linker's
240     /// composite.
241     /// @see LinkModules
242     /// @returns True if an error occurs, false otherwise.
243     /// @brief Link in a module.
244     bool LinkInModule(
245       Module* Src,              ///< Module linked into \p Dest
246       std::string* ErrorMsg = 0 /// Error/diagnostic string
247     ) { 
248       return LinkModules(Composite, Src, ErrorMsg ); 
249     }
250
251     /// This is the heart of the linker. This method will take unconditional
252     /// control of the \p Src module and link it into the \p Dest module. The
253     /// \p Src module will be destructed or subsumed by this method. In either
254     /// case it is not usable by the caller after this method is invoked. Only
255     /// the \p Dest module will remain. The \p Src module is linked into the
256     /// Linker's composite module such that types, global variables, functions,
257     /// and etc. are matched and resolved.  If an error occurs, this function
258     /// returns true and ErrorMsg is set to a descriptive message about the
259     /// error.
260     /// @returns True if an error occurs, false otherwise.
261     /// @brief Generically link two modules together.
262     static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
263
264     /// This function looks through the Linker's LibPaths to find a library with
265     /// the name \p Filename. If the library cannot be found, the returned path
266     /// will be empty (i.e. sys::Path::isEmpty() will return true).
267     /// @returns A sys::Path to the found library
268     /// @brief Find a library from its short name.
269     sys::Path FindLib(StringRef Filename);
270
271   /// @}
272   /// @name Implementation
273   /// @{
274   private:
275     /// Read in and parse the bitcode file named by FN and return the
276     /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
277     std::auto_ptr<Module> LoadObject(const sys::Path& FN);
278
279     bool warning(StringRef message);
280     bool error(StringRef message);
281     void verbose(StringRef message);
282
283   /// @}
284   /// @name Data
285   /// @{
286   private:
287     LLVMContext& Context; ///< The context for global information
288     Module* Composite; ///< The composite module linked together
289     std::vector<sys::Path> LibPaths; ///< The library search paths
290     unsigned Flags;    ///< Flags to control optional behavior.
291     std::string Error; ///< Text of error that occurred.
292     std::string ProgramName; ///< Name of the program being linked
293   /// @}
294
295 };
296
297 } // End llvm namespace
298
299 #endif