f7b03c2ee5c2ba914036080746f573e76ddcd861
[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 #include <memory>
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, bytecode files, or bytecode
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 errors and warnings to std::cerr.
56       QuietErrors   = 4, ///< Indicate that this link is for a native executable
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     );
163
164     /// This function links the bytecode \p Files into the composite module.
165     /// Note that this does not do any linking of unresolved symbols. The \p
166     /// Files are all completely linked into \p HeadModule regardless of
167     /// unresolved symbols. This function just loads each bytecode file and
168     /// calls LinkInModule on them.
169     /// @returns true if an error occurs, false otherwise
170     /// @see getLastError
171     /// @brief Link in multiple files.
172     bool LinkInFiles (
173       const std::vector<sys::Path> & Files ///< Files to link in
174     );
175
176     /// This function links a single bytecode file, \p File, into the composite
177     /// module. Note that this does not attempt to resolve symbols. This method
178     /// just loads the bytecode file and calls LinkInModule on it. If an error
179     /// occurs, the Linker's error string is set.
180     /// @returns true if an error occurs, false otherwise
181     /// @see getLastError
182     /// @brief Link in a single file.
183     bool LinkInFile(
184       const sys::Path& File ///< File to link in.
185     );
186
187     /// This function provides a way to selectively link in a set of modules,
188     /// found in libraries, based on the unresolved symbols in the composite
189     /// module. Each item in \p Libraries should be the base name of a library,
190     /// as if given with the -l option of a linker tool.  The Linker's LibPaths
191     /// are searched for the \p Libraries and any found will be linked in with
192     /// LinkInArchive.  If an error occurs, the Linker's error string is set.
193     /// @see LinkInArchive
194     /// @see getLastError
195     /// @returns true if an error occurs, false otherwise
196     /// @brief Link libraries into the module
197     bool LinkInLibraries (
198       const std::vector<std::string> & Libraries ///< Libraries to link in
199     );
200
201     /// This function provides a way to selectively link in a set of modules,
202     /// found in one library, based on the unresolved symbols in the composite
203     /// module.The \p Library should be the base name of a library, as if given
204     /// with the -l option of a linker tool. The Linker's LibPaths are searched
205     /// for the \P Library and if found, it will be linked in with via the
206     /// LinkInArchive method. If an error occurs, the Linker's error string is
207     /// set.
208     /// @see LinkInArchive
209     /// @see getLastError
210     /// @returns true if an error occurs, false otherwise
211     /// @brief Link one library into the module
212     bool LinkInLibrary (
213       const std::string& Library ///< The library to link in
214     );
215
216     /// This function links one bytecode archive, \p Filename, into the module.
217     /// The archive is searched to resolve outstanding symbols. Any modules in
218     /// the archive that resolve outstanding symbols will be linked in. The
219     /// library is searched repeatedly until no more modules that resolve
220     /// symbols can be found. If an error occurs, the error string is  set.
221     /// To speed up this function, ensure the the archive has been processed
222     /// llvm-ranlib or the S option was given to llvm-ar when the archive was
223     /// created. These tools add a symbol table to the archive which makes the
224     /// search for undefined symbols much faster.
225     /// @see getLastError
226     /// @returns true if an error occurs, otherwise false.
227     /// @brief Link in one archive.
228     bool LinkInArchive(
229       const sys::Path& Filename ///< Filename of the archive to link
230     );
231
232     /// This method links the \p Src module into the Linker's Composite module
233     /// by calling LinkModules.  All the other LinkIn* methods eventually
234     /// result in calling this method to link a Module into the Linker's
235     /// composite.
236     /// @see LinkModules
237     /// @returns True if an error occurs, false otherwise.
238     /// @brief Link in a module.
239     bool LinkInModule(
240       Module* Src     ///< Module linked into \p Dest
241     ) { return LinkModules(Composite, Src, &Error); }
242
243     /// This is the heart of the linker. This method will take unconditional
244     /// control of the \p Src module and link it into the \p Dest module. The
245     /// \p Src module will be destructed or subsumed by this method. In either
246     /// case it is not usable by the caller after this method is invoked. Only
247     /// the \p Dest module will remain. The \p Src module is linked into the
248     /// Linker's composite module such that types, global variables, functions,
249     /// and etc. are matched and resolved.  If an error occurs, this function
250     /// returns true and ErrorMsg is set to a descriptive message about the
251     /// error.
252     /// @returns True if an error occurs, false otherwise.
253     /// @brief Generically link two modules together.
254     static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
255
256     /// This function looks through the Linker's LibPaths to find a library with
257     /// the name \p Filename. If the library cannot be found, the returned path
258     /// will be empty (i.e. sys::Path::isEmpty() will return true).
259     /// @returns A sys::Path to the found library
260     /// @brief Find a library from its short name.
261     sys::Path FindLib(const std::string &Filename);
262
263   /// @}
264   /// @name Implementation
265   /// @{
266   private:
267     /// Read in and parse the bytecode file named by FN and return the
268     /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
269     std::auto_ptr<Module> LoadObject(const sys::Path& FN);
270
271     bool warning(const std::string& message);
272     bool error(const std::string& message);
273     void verbose(const std::string& message);
274
275   /// @}
276   /// @name Data
277   /// @{
278   private:
279     Module* Composite; ///< The composite module linked together
280     std::vector<sys::Path> LibPaths; ///< The library search paths
281     unsigned Flags;    ///< Flags to control optional behavior.
282     std::string Error; ///< Text of error that occurred.
283     std::string ProgramName; ///< Name of the program being linked
284   /// @}
285
286 };
287
288 } // End llvm namespace
289
290 #endif