1 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains routines to handle linking together LLVM bytecode files,
11 // and to handle annoying things like static libraries.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Linker.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "llvm/Bytecode/Archive.h"
22 #include "llvm/Bytecode/WriteBytecodePass.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Transforms/IPO.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/Timer.h"
30 #include "llvm/System/Signals.h"
31 #include "llvm/Support/SystemUtils.h"
38 /// FindLib - Try to convert Filename into the name of a file that we can open,
39 /// if it does not already name a file we can open, by first trying to open
40 /// Filename, then libFilename.[suffix] for each of a set of several common
41 /// library suffixes, in each of the directories in Paths and the directory
42 /// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
43 /// an empty string if no matching file can be found.
45 std::string llvm::FindLib(const std::string &Filename,
46 const std::vector<std::string> &Paths,
47 bool SharedObjectOnly) {
48 // Determine if the pathname can be found as it stands.
49 if (FileOpenable(Filename))
52 // If that doesn't work, convert the name into a library name.
53 std::string LibName = "lib" + Filename;
55 // Iterate over the directories in Paths to see if we can find the library
57 for (unsigned Index = 0; Index != Paths.size(); ++Index) {
58 std::string Directory = Paths[Index] + "/";
60 if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
61 return Directory + LibName + ".bc";
63 if (FileOpenable(Directory + LibName + SHLIBEXT))
64 return Directory + LibName + SHLIBEXT;
66 if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
67 return Directory + LibName + ".a";
70 // One last hope: Check LLVM_LIB_SEARCH_PATH.
71 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
72 if (SearchPath == NULL)
75 LibName = std::string(SearchPath) + "/" + LibName;
76 if (FileOpenable(LibName))
82 /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
83 /// name of each externally-visible symbol defined in M.
85 void llvm::GetAllDefinedSymbols(Module *M,
86 std::set<std::string> &DefinedSymbols) {
87 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
88 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
89 DefinedSymbols.insert(I->getName());
90 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
91 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
92 DefinedSymbols.insert(I->getName());
95 /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
96 /// exist in an LLVM module. This is a bit tricky because there may be two
97 /// symbols with the same name but different LLVM types that will be resolved to
98 /// each other but aren't currently (thus we need to treat it as resolved).
101 /// M - The module in which to find undefined symbols.
104 /// UndefinedSymbols - A set of C++ strings containing the name of all
105 /// undefined symbols.
108 llvm::GetAllUndefinedSymbols(Module *M,
109 std::set<std::string> &UndefinedSymbols) {
110 std::set<std::string> DefinedSymbols;
111 UndefinedSymbols.clear();
113 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
116 UndefinedSymbols.insert(I->getName());
117 else if (!I->hasInternalLinkage())
118 DefinedSymbols.insert(I->getName());
120 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
123 UndefinedSymbols.insert(I->getName());
124 else if (!I->hasInternalLinkage())
125 DefinedSymbols.insert(I->getName());
128 // Prune out any defined symbols from the undefined symbols set...
129 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
130 I != UndefinedSymbols.end(); )
131 if (DefinedSymbols.count(*I))
132 UndefinedSymbols.erase(I++); // This symbol really is defined!
134 ++I; // Keep this symbol in the undefined symbols list
138 /// LoadObject - Read in and parse the bytecode file named by FN and return the
139 /// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
142 static std::auto_ptr<Module> LoadObject(const std::string &FN,
143 std::string &ErrorMessage) {
144 std::string ParserErrorMessage;
145 Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
146 if (Result) return std::auto_ptr<Module>(Result);
147 ErrorMessage = "Bytecode file '" + FN + "' could not be loaded";
148 if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage;
149 return std::auto_ptr<Module>();
152 /// LinkInArchive - opens an archive library and link in all objects which
153 /// provide symbols that are currently undefined.
156 /// M - The module in which to link the archives.
157 /// Filename - The pathname of the archive.
158 /// Verbose - Flags whether verbose messages should be printed.
161 /// ErrorMessage - A C++ string detailing what error occurred, if any.
164 /// TRUE - An error occurred.
165 /// FALSE - No errors.
167 bool llvm::LinkInArchive(Module *M,
168 const std::string &Filename,
169 std::string* ErrorMessage,
172 // Find all of the symbols currently undefined in the bytecode program.
173 // If all the symbols are defined, the program is complete, and there is
174 // no reason to link in any archive files.
175 std::set<std::string> UndefinedSymbols;
176 GetAllUndefinedSymbols(M, UndefinedSymbols);
178 if (UndefinedSymbols.empty()) {
179 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
180 return false; // No need to link anything in!
183 // Open the archive file
184 if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n";
185 std::auto_ptr<Archive> AutoArch (
186 Archive::OpenAndLoadSymbols(sys::Path(Filename)));
188 Archive* arch = AutoArch.get();
190 // Save a set of symbols that are not defined by the archive. Since we're
191 // entering a loop, there's no point searching for these multiple times. This
192 // variable is used to "set_subtract" from the set of undefined symbols.
193 std::set<std::string> NotDefinedByArchive;
195 // While we are linking in object files, loop.
198 // Find the modules we need to link into the target module
199 std::set<ModuleProvider*> Modules;
200 arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
202 // If we didn't find any more modules to link this time, we are done
203 // searching this archive.
207 // Any symbols remaining in UndefinedSymbols after
208 // findModulesDefiningSymbols are ones that the archive does not define. So
209 // we add them to the NotDefinedByArchive variable now.
210 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
211 UndefinedSymbols.end());;
213 // Loop over all the ModuleProviders that we got back from the archive
214 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
217 // Get the module we must link in.
218 std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
219 Module* aModule = AutoModule.get();
222 if (LinkModules(M, aModule, ErrorMessage))
223 return true; // Couldn't link in the module
226 // Get the undefined symbols from the aggregate module. This recomputes the
227 // symbols we still need after the new modules have been linked in.
228 GetAllUndefinedSymbols(M, UndefinedSymbols);
230 // At this point we have two sets of undefined symbols: UndefinedSymbols
231 // which holds the undefined symbols from all the modules, and
232 // NotDefinedByArchive which holds symbols we know the archive doesn't
233 // define. There's no point searching for symbols that we won't find in the
234 // archive so we subtract these sets.
235 set_subtract<std::set<std::string>,std::set<std::string> >(
236 UndefinedSymbols,NotDefinedByArchive);
238 // If there's no symbols left, no point in continuing to search the
240 if (UndefinedSymbols.empty())
247 /// LinkInFile - opens a bytecode file and links in all objects which
248 /// provide symbols that are currently undefined.
251 /// HeadModule - The module in which to link the bytecode file.
252 /// Filename - The pathname of the bytecode file.
253 /// Verbose - Flags whether verbose messages should be printed.
256 /// ErrorMessage - A C++ string detailing what error occurred, if any.
259 /// TRUE - An error occurred.
260 /// FALSE - No errors.
262 static bool LinkInFile(Module *HeadModule,
263 const std::string &Filename,
264 std::string &ErrorMessage,
267 std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
268 if (M.get() == 0) return true;
269 bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage);
270 if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n";
274 /// LinkFiles - takes a module and a list of files and links them all together.
275 /// It locates the file either in the current directory, as its absolute
276 /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
279 /// progname - The name of the program (infamous argv[0]).
280 /// HeadModule - The module under which all files will be linked.
281 /// Files - A vector of C++ strings indicating the LLVM bytecode filenames
282 /// to be linked. The names can refer to a mixture of pure LLVM
283 /// bytecode files and archive (ar) formatted files.
284 /// Verbose - Flags whether verbose output should be printed while linking.
287 /// HeadModule - The module will have the specified LLVM bytecode files linked
291 /// FALSE - No errors.
292 /// TRUE - Some error occurred.
294 bool llvm::LinkFiles(const char *progname, Module *HeadModule,
295 const std::vector<std::string> &Files, bool Verbose) {
296 // String in which to receive error messages.
297 std::string ErrorMessage;
299 // Full pathname of the file
300 std::string Pathname;
302 // Get the library search path from the environment
303 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
305 for (unsigned i = 0; i < Files.size(); ++i) {
306 // Determine where this file lives.
307 if (FileOpenable(Files[i])) {
310 if (SearchPath == NULL) {
311 std::cerr << progname << ": Cannot find linker input file '"
312 << Files[i] << "'\n";
313 std::cerr << progname
314 << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n";
318 Pathname = std::string(SearchPath)+"/"+Files[i];
319 if (!FileOpenable(Pathname)) {
320 std::cerr << progname << ": Cannot find linker input file '"
321 << Files[i] << "'\n";
326 // A user may specify an ar archive without -l, perhaps because it
327 // is not installed as a library. Detect that and link the library.
328 if (IsArchive(Pathname)) {
330 std::cerr << "Trying to link archive '" << Pathname << "'\n";
332 if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
333 std::cerr << progname << ": Error linking in archive '" << Pathname
334 << "': " << ErrorMessage << "\n";
337 } else if (IsBytecode(Pathname)) {
339 std::cerr << "Trying to link bytecode file '" << Pathname << "'\n";
341 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
342 std::cerr << progname << ": Error linking in bytecode file '"
343 << Pathname << "': " << ErrorMessage << "\n";
347 std::cerr << progname << ": Warning: invalid file `" << Pathname
355 /// LinkLibraries - takes the specified library files and links them into the
356 /// main bytecode object file.
359 /// progname - The name of the program (infamous argv[0]).
360 /// HeadModule - The module into which all necessary libraries will be linked.
361 /// Libraries - The list of libraries to link into the module.
362 /// LibPaths - The list of library paths in which to find libraries.
363 /// Verbose - Flags whether verbose messages should be printed.
364 /// Native - Flags whether native code is being generated.
367 /// HeadModule - The module will have all necessary libraries linked in.
370 /// FALSE - No error.
373 void llvm::LinkLibraries(const char *progname, Module *HeadModule,
374 const std::vector<std::string> &Libraries,
375 const std::vector<std::string> &LibPaths,
376 bool Verbose, bool Native) {
377 // String in which to receive error messages.
378 std::string ErrorMessage;
380 for (unsigned i = 0; i < Libraries.size(); ++i) {
381 // Determine where this library lives.
382 std::string Pathname = FindLib(Libraries[i], LibPaths);
383 if (Pathname.empty()) {
384 // If the pathname does not exist, then continue to the next one if
385 // we're doing a native link and give an error if we're doing a bytecode
388 std::cerr << progname << ": WARNING: Cannot find library -l"
389 << Libraries[i] << "\n";
394 // A user may specify an ar archive without -l, perhaps because it
395 // is not installed as a library. Detect that and link the library.
396 if (IsArchive(Pathname)) {
398 std::cerr << "Trying to link archive '" << Pathname << "' (-l"
399 << Libraries[i] << ")\n";
401 if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
402 std::cerr << progname << ": " << ErrorMessage
403 << ": Error linking in archive '" << Pathname << "' (-l"
404 << Libraries[i] << ")\n";
407 } else if (IsBytecode(Pathname)) {
409 std::cerr << "Trying to link bytecode file '" << Pathname
410 << "' (-l" << Libraries[i] << ")\n";
412 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
413 std::cerr << progname << ": " << ErrorMessage
414 << ": error linking in bytecode file '" << Pathname << "' (-l"
415 << Libraries[i] << ")\n";