[Linker] Drop support for IR-level extended linking support (archives, etc.).
[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 <memory>
18 #include <string>
19 #include <vector>
20
21 namespace llvm {
22   namespace sys { class Path; }
23
24 class Module;
25 class LLVMContext;
26 class StringRef;
27
28 /// This class provides the core functionality of linking in LLVM. It retains a
29 /// Module object which is the composite of the modules and libraries linked
30 /// into it. The composite Module can be retrieved via the getModule() method.
31 /// In this case the Linker still retains ownership of the Module. If the
32 /// releaseModule() method is used, the ownership of the Module is transferred
33 /// to the caller and the Linker object is only suitable for destruction.
34 /// The Linker can link Modules from memory, bitcode files, or bitcode
35 /// archives.  It retains a set of search paths in which to find any libraries
36 /// presented to it. By default, the linker will generate error and warning
37 /// messages to stderr but this capability can be turned off with the
38 /// QuietWarnings and QuietErrors flags. It can also be instructed to verbosely
39 /// print out the linking actions it is taking with the Verbose flag.
40 /// @brief The LLVM Linker.
41 class Linker {
42
43   /// @name Types
44   /// @{
45   public:
46     /// This type is used to pass the linkage items (libraries and files) to
47     /// the LinkItems function. It is composed of string/bool pairs. The string
48     /// provides the name of the file or library (as with the -l option). The
49     /// bool should be true for libraries and false for files, signifying
50     /// "isLibrary".
51     /// @brief A list of linkage items
52     typedef std::vector<std::pair<std::string,bool> > ItemList;
53
54     /// This enumeration is used to control various optional features of the
55     /// linker.
56     enum ControlFlags {
57       Verbose       = 1, ///< Print to stderr what steps the linker is taking
58       QuietWarnings = 2, ///< Don't print warnings to stderr.
59       QuietErrors   = 4  ///< Don't print errors to stderr.
60     };
61   
62     enum LinkerMode {
63       DestroySource = 0, // Allow source module to be destroyed.
64       PreserveSource = 1 // Preserve the source module.
65     };
66   
67   /// @}
68   /// @name Constructors
69   /// @{
70   public:
71     /// Construct the Linker with an empty module which will be given the
72     /// name \p progname. \p progname will also be used for error messages.
73     /// @brief Construct with empty module
74     Linker(StringRef progname, ///< name of tool running linker
75            StringRef modulename, ///< name of linker's end-result module
76            LLVMContext &C, ///< Context for global info
77            unsigned Flags = 0  ///< ControlFlags (one or more |'d together)
78     );
79
80     /// Construct the Linker with a previously defined module, \p aModule. Use
81     /// \p progname for the name of the program in error messages.
82     /// @brief Construct with existing module
83     Linker(StringRef progname, Module* aModule, unsigned Flags = 0);
84
85     /// Destruct the Linker.
86     /// @brief Destructor
87     ~Linker();
88
89   /// @}
90   /// @name Accessors
91   /// @{
92   public:
93     /// This method gets the composite module into which linking is being
94     /// done. The Composite module starts out empty and accumulates modules
95     /// linked into it via the various LinkIn* methods. This method does not
96     /// release the Module to the caller. The Linker retains ownership and will
97     /// destruct the Module when the Linker is destructed.
98     /// @see releaseModule
99     /// @brief Get the linked/composite module.
100     Module* getModule() const { return Composite; }
101
102     /// This method releases the composite Module into which linking is being
103     /// done. Ownership of the composite Module is transferred to the caller who
104     /// must arrange for its destruct. After this method is called, the Linker
105     /// terminates the linking session for the returned Module. It will no
106     /// longer utilize the returned Module but instead resets itself for
107     /// subsequent linking as if the constructor had been called. The Linker's
108     /// LibPaths and flags to be reset, and memory will be released.
109     /// @brief Release the linked/composite module.
110     Module* releaseModule();
111
112     /// This method gets the list of libraries that form the path that the
113     /// Linker will search when it is presented with a library name.
114     /// @brief Get the Linkers library path
115     const std::vector<sys::Path>& getLibPaths() const { return LibPaths; }
116
117     /// This method returns an error string suitable for printing to the user.
118     /// The return value will be empty unless an error occurred in one of the
119     /// LinkIn* methods. In those cases, the LinkIn* methods will have returned
120     /// true, indicating an error occurred. At most one error is retained so
121     /// this function always returns the last error that occurred. Note that if
122     /// the Quiet control flag is not set, the error string will have already
123     /// been printed to stderr.
124     /// @brief Get the text of the last error that occurred.
125     const std::string &getLastError() const { return Error; }
126
127   /// @}
128   /// @name Mutators
129   /// @{
130   public:
131     /// Add a path to the list of paths that the Linker will search. The Linker
132     /// accumulates the set of libraries added
133     /// library paths for the target platform. The standard libraries will
134     /// always be searched last. The added libraries will be searched in the
135     /// order added.
136     /// @brief Add a path.
137     void addPath(const sys::Path& path);
138
139     /// Add a set of paths to the list of paths that the linker will search. The
140     /// Linker accumulates the set of libraries added. The \p paths will be
141     /// added to the end of the Linker's list. Order will be retained.
142     /// @brief Add a set of paths.
143     void addPaths(const std::vector<std::string>& paths);
144
145     /// This method augments the Linker's list of library paths with the system
146     /// paths of the host operating system, include LLVM_LIB_SEARCH_PATH.
147     /// @brief Add the system paths.
148     void addSystemPaths();
149
150     /// Control optional linker behavior by setting a group of flags. The flags
151     /// are defined in the ControlFlags enumeration.
152     /// @see ControlFlags
153     /// @brief Set control flags.
154     void setFlags(unsigned flags) { Flags = flags; }
155
156     /// This method links the \p Src module into the Linker's Composite module
157     /// by calling LinkModules.  All the other LinkIn* methods eventually
158     /// result in calling this method to link a Module into the Linker's
159     /// composite.
160     /// @see LinkModules
161     /// @returns True if an error occurs, false otherwise.
162     /// @brief Link in a module.
163     bool LinkInModule(
164       Module* Src,              ///< Module linked into \p Dest
165       std::string* ErrorMsg = 0 /// Error/diagnostic string
166     ) { 
167       return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg ); 
168     }
169
170     /// This is the heart of the linker. This method will take unconditional
171     /// control of the \p Src module and link it into the \p Dest module. The
172     /// \p Src module will be destructed or subsumed by this method. In either
173     /// case it is not usable by the caller after this method is invoked. Only
174     /// the \p Dest module will remain. The \p Src module is linked into the
175     /// Linker's composite module such that types, global variables, functions,
176     /// and etc. are matched and resolved.  If an error occurs, this function
177     /// returns true and ErrorMsg is set to a descriptive message about the
178     /// error.
179     /// @returns True if an error occurs, false otherwise.
180     /// @brief Generically link two modules together.
181     static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
182                             std::string* ErrorMsg);
183
184   /// @}
185   /// @name Implementation
186   /// @{
187   private:
188     /// Read in and parse the bitcode file named by FN and return the
189     /// Module it contains (wrapped in an auto_ptr), or 0 if an error occurs.
190     std::auto_ptr<Module> LoadObject(const sys::Path& FN);
191
192     bool warning(StringRef message);
193     bool error(StringRef message);
194     void verbose(StringRef message);
195
196   /// @}
197   /// @name Data
198   /// @{
199   private:
200     LLVMContext& Context; ///< The context for global information
201     Module* Composite; ///< The composite module linked together
202     std::vector<sys::Path> LibPaths; ///< The library search paths
203     unsigned Flags;    ///< Flags to control optional behavior.
204     std::string Error; ///< Text of error that occurred.
205     std::string ProgramName; ///< Name of the program being linked
206   /// @}
207
208 };
209
210 } // End llvm namespace
211
212 #endif