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