1 //===- llvm/Linker.h - Module Linker Interface ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface to the module/file/archive linker.
12 //===----------------------------------------------------------------------===//
22 namespace sys { class Path; }
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. It retains a set of search paths
35 /// in which to find any libraries presented to it. By default, the linker
36 /// will generate error and warning messages to stderr but this capability can
37 /// be turned off with the QuietWarnings and QuietErrors flags. It can also be
38 /// instructed to verbosely print out the linking actions it is taking with
40 /// @brief The LLVM Linker.
46 /// This enumeration is used to control various optional features of the
49 Verbose = 1, ///< Print to stderr what steps the linker is taking
50 QuietWarnings = 2, ///< Don't print warnings to stderr.
51 QuietErrors = 4 ///< Don't print errors to stderr.
55 DestroySource = 0, // Allow source module to be destroyed.
56 PreserveSource = 1 // Preserve the source module.
60 /// @name Constructors
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(StringRef progname, ///< name of tool running linker
67 StringRef modulename, ///< name of linker's end-result module
68 LLVMContext &C, ///< Context for global info
69 unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
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(StringRef progname, Module* aModule, unsigned Flags = 0);
77 /// Destruct the Linker.
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; }
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();
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; }
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 stderr.
116 /// @brief Get the text of the last error that occurred.
117 const std::string &getLastError() const { return Error; }
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
128 /// @brief Add a path.
129 void addPath(const sys::Path& path);
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);
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();
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; }
148 /// This method links the \p Src module into the Linker's Composite module
149 /// by calling LinkModules.
151 /// @returns True if an error occurs, false otherwise.
152 /// @brief Link in a module.
154 Module* Src, ///< Module linked into \p Dest
155 std::string* ErrorMsg = 0 /// Error/diagnostic string
157 return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg);
160 /// This is the heart of the linker. This method will take unconditional
161 /// control of the \p Src module and link it into the \p Dest module. The
162 /// \p Src module will be destructed or subsumed by this method. In either
163 /// case it is not usable by the caller after this method is invoked. Only
164 /// the \p Dest module will remain. The \p Src module is linked into the
165 /// Linker's composite module such that types, global variables, functions,
166 /// and etc. are matched and resolved. If an error occurs, this function
167 /// returns true and ErrorMsg is set to a descriptive message about the
169 /// @returns True if an error occurs, false otherwise.
170 /// @brief Generically link two modules together.
171 static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
172 std::string* ErrorMsg);
175 /// @name Implementation
178 bool warning(StringRef message);
179 bool error(StringRef message);
180 void verbose(StringRef message);
186 LLVMContext& Context; ///< The context for global information
187 Module* Composite; ///< The composite module linked together
188 std::vector<sys::Path> LibPaths; ///< The library search paths
189 unsigned Flags; ///< Flags to control optional behavior.
190 std::string Error; ///< Text of error that occurred.
191 std::string ProgramName; ///< Name of the program being linked
196 } // End llvm namespace