1 //===- Linker.cpp - Link together LLVM objects and libraries --------------===//
3 // This file contains routines to handle linking together LLVM bytecode files,
4 // and to handle annoying things like static libraries.
6 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/Transforms/IPO.h"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Transforms/Utils/Linker.h"
17 #include "Support/CommandLine.h"
18 #include "Support/FileUtilities.h"
19 #include "Support/Signals.h"
20 #include "Support/SystemUtils.h"
21 #include "Config/stdlib.h"
28 // Function: FileExists ()
31 // Determine if the specified filename exists and is readable.
34 // FN - The name of the file.
40 // TRUE - The file exists and is readable.
41 // FALSE - The file does not exist or is unreadable.
43 static inline bool FileExists(const std::string &FN) {
44 return access(FN.c_str(), R_OK | F_OK) != -1;
48 // Function: IsArchive ()
51 // Determine if the specified file is an ar archive. It determines this by
52 // checking the magic string at the beginning of the file.
55 // filename - A C++ string containing the name of the file.
61 // TRUE - The file is an archive.
62 // FALSE - The file is not an archive.
64 static inline bool IsArchive(const std::string &filename) {
65 std::string ArchiveMagic("!<arch>\012");
66 char buf[1 + ArchiveMagic.size()];
67 std::ifstream f(filename.c_str());
68 f.read(buf, ArchiveMagic.size());
69 buf[ArchiveMagic.size()] = '\0';
70 return ArchiveMagic == buf;
74 // Function: FindLib ()
77 // This function locates a particular library. It will prepend and append
78 // various directories, prefixes, and suffixes until it can find the library.
81 // Filename - Name of the file to find.
82 // Paths - List of directories to search.
88 // The name of the file is returned.
89 // If the file is not found, an empty string is returned.
92 FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
93 // Determine if the pathname can be found as it stands.
94 if (FileExists(Filename))
97 // If that doesn't work, convert the name into a library name.
98 std::string LibName = "lib" + Filename;
100 // Iterate over the directories in Paths to see if we can find the library
102 for (unsigned Index = 0; Index != Paths.size(); ++Index) {
103 std::string Directory = Paths[Index] + "/";
105 if (FileExists(Directory + LibName + ".bc"))
106 return Directory + LibName + ".bc";
108 if (FileExists(Directory + LibName + ".so"))
109 return Directory + LibName + ".so";
111 if (FileExists(Directory + LibName + ".a"))
112 return Directory + LibName + ".a";
115 // One last hope: Check LLVM_LIB_SEARCH_PATH.
116 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
117 if (SearchPath == NULL)
118 return std::string();
120 LibName = std::string(SearchPath) + "/" + LibName;
121 if (FileExists(LibName))
124 return std::string();
128 // Function: GetAllDefinedSymbols ()
131 // Find all of the defined symbols in the specified module.
134 // M - The module in which to find defined symbols.
137 // DefinedSymbols - A set of C++ strings that will contain the name of all
143 void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
144 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
145 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
146 DefinedSymbols.insert(I->getName());
147 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
148 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
149 DefinedSymbols.insert(I->getName());
153 // Function: GetAllUndefinedSymbols ()
156 // This calculates the set of undefined symbols that still exist in an LLVM
157 // module. This is a bit tricky because there may be two symbols with the
158 // same name but different LLVM types that will be resolved to each other but
159 // aren't currently (thus we need to treat it as resolved).
162 // M - The module in which to find undefined symbols.
165 // UndefinedSymbols - A set of C++ strings containing the name of all
166 // undefined symbols.
172 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
173 std::set<std::string> DefinedSymbols;
174 UndefinedSymbols.clear(); // Start out empty
176 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
179 UndefinedSymbols.insert(I->getName());
180 else if (!I->hasInternalLinkage())
181 DefinedSymbols.insert(I->getName());
183 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
186 UndefinedSymbols.insert(I->getName());
187 else if (!I->hasInternalLinkage())
188 DefinedSymbols.insert(I->getName());
191 // Prune out any defined symbols from the undefined symbols set...
192 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
193 I != UndefinedSymbols.end(); )
194 if (DefinedSymbols.count(*I))
195 UndefinedSymbols.erase(I++); // This symbol really is defined!
197 ++I; // Keep this symbol in the undefined symbols list
202 // Function: LoadObject ()
205 // Read the specified bytecode object file.
208 // FN - The name of the file to load.
211 // OutErrorMessage - The error message to give back to the caller.
214 // A pointer to a module represening the bytecode file is returned.
215 // If an error occurs, the pointer is 0.
217 std::auto_ptr<Module>
218 LoadObject(const std::string & FN, std::string &OutErrorMessage) {
219 std::string ErrorMessage;
220 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
221 if (Result) return std::auto_ptr<Module>(Result);
222 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
223 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
224 return std::auto_ptr<Module>();
228 // Function: LinkInArchive ()
231 // This function will open an archive library and link in all objects which
232 // provide symbols that are currently undefined.
235 // M - The module in which to link the archives.
236 // Filename - The pathname of the archive.
237 // Verbose - Flags whether verbose messages should be printed.
240 // ErrorMessage - A C++ string detailing what error occurred, if any.
243 // TRUE - An error occurred.
244 // FALSE - No errors.
246 static bool LinkInArchive(Module *M,
247 const std::string &Filename,
248 std::string &ErrorMessage,
251 // Find all of the symbols currently undefined in the bytecode program.
252 // If all the symbols are defined, the program is complete, and there is
253 // no reason to link in any archive files.
254 std::set<std::string> UndefinedSymbols;
255 GetAllUndefinedSymbols(M, UndefinedSymbols);
256 if (UndefinedSymbols.empty()) {
257 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
258 return false; // No need to link anything in!
261 // Load in the archive objects.
262 if (Verbose) std::cerr << " Loading '" << Filename << "'\n";
263 std::vector<Module*> Objects;
264 if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
267 // Figure out which symbols are defined by all of the modules in the archive.
268 std::vector<std::set<std::string> > DefinedSymbols;
269 DefinedSymbols.resize(Objects.size());
270 for (unsigned i = 0; i != Objects.size(); ++i) {
271 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
274 // While we are linking in object files, loop.
279 for (unsigned i = 0; i != Objects.size(); ++i) {
280 // Consider whether we need to link in this module... we only need to
281 // link it in if it defines some symbol which is so far undefined.
283 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
285 bool ObjectRequired = false;
286 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
287 E = UndefinedSymbols.end(); I != E; ++I)
288 if (DefSymbols.count(*I)) {
290 std::cerr << " Found object providing symbol '" << *I << "'...\n";
291 ObjectRequired = true;
295 // We DO need to link this object into the program...
296 if (ObjectRequired) {
297 if (LinkModules(M, Objects[i], &ErrorMessage))
298 return true; // Couldn't link in the right object file...
300 // Since we have linked in this object, delete it from the list of
301 // objects to consider in this archive file.
302 std::swap(Objects[i], Objects.back());
303 std::swap(DefinedSymbols[i], DefinedSymbols.back());
305 DefinedSymbols.pop_back();
306 --i; // Do not skip an entry
308 // The undefined symbols set should have shrunk.
309 GetAllUndefinedSymbols(M, UndefinedSymbols);
310 Linked = true; // We have linked something in!
319 // Function: LinkInFile ()
322 // This function will open an archive library and link in all objects which
323 // provide symbols that are currently undefined.
326 // HeadModule - The module in which to link the archives.
327 // Filename - The pathname of the archive.
328 // Verbose - Flags whether verbose messages should be printed.
331 // ErrorMessage - A C++ string detailing what error occurred, if any.
334 // TRUE - An error occurred.
335 // FALSE - No errors.
337 static bool LinkInFile(Module *HeadModule,
338 const std::string &Filename,
339 std::string &ErrorMessage,
342 std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
343 if (M.get() == 0) return true;
344 if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
345 return LinkModules(HeadModule, M.get(), &ErrorMessage);
349 // Function: LinkFiles ()
352 // This function takes a module and a list of files and links them all
353 // together. It locates the file either in the current directory, as it's
354 // absolute or relative pathname, or as a file somewhere in
355 // LLVM_LIB_SEARCH_PATH.
358 // progname - The name of the program (infamous argv[0]).
359 // HeadModule - The module under which all files will be linked.
360 // Files - A vector of C++ strings indicating the LLVM bytecode filenames
361 // to be linked. The names can refer to a mixture of pure LLVM
362 // bytecode files and archive (ar) formatted files.
363 // Verbose - Flags whether verbose output should be printed while linking.
366 // HeadModule - The module will have the specified LLVM bytecode files linked
370 // FALSE - No errors.
371 // TRUE - Some error occurred.
373 bool LinkFiles(const char *progname,
375 const std::vector<std::string> &Files,
378 // String in which to receive error messages.
379 std::string ErrorMessage;
381 // Full pathname of the file
382 std::string Pathname;
384 // Get the library search path from the environment
385 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
387 for (unsigned i = 1; i < Files.size(); ++i) {
388 // Determine where this file lives.
389 if (FileExists(Files[i])) {
392 if (SearchPath == NULL) {
393 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
397 Pathname = std::string(SearchPath)+"/"+Files[i];
398 if (!FileExists(Pathname)) {
399 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
404 // A user may specify an ar archive without -l, perhaps because it
405 // is not installed as a library. Detect that and link the library.
406 if (IsArchive(Pathname)) {
408 std::cerr << "Linking archive '" << Files[i] << "'\n";
410 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
411 PrintAndReturn(progname, ErrorMessage,
412 ": Error linking in '" + Files[i] + "'");
417 std::cerr << "Linking file '" << Files[i] << "'\n";
419 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
420 PrintAndReturn(progname, ErrorMessage,
421 ": error linking in '" + Files[i] + "'");
431 // Function: LinkLibraries ()
434 // This function takes the specified library files and links them into the
435 // main bytecode object file.
438 // progname - The name of the program (infamous argv[0]).
439 // HeadModule - The module into which all necessary libraries will be linked.
440 // Libraries - The list of libraries to link into the module.
441 // LibPaths - The list of library paths in which to find libraries.
442 // Verbose - Flags whether verbose messages should be printed.
443 // Native - Flags whether native code is being generated.
446 // HeadModule - The module will have all necessary libraries linked in.
452 bool LinkLibraries(const char *progname,
454 const std::vector<std::string> &Libraries,
455 const std::vector<std::string> &LibPaths,
459 // String in which to receive error messages.
460 std::string ErrorMessage;
462 for (unsigned i = 1; i < Libraries.size(); ++i) {
463 // Determine where this library lives.
464 std::string Pathname = FindLib(Libraries[i], LibPaths);
465 if (Pathname.empty()) {
466 // If the pathname does not exist, then continue to the next one if
467 // we're doing a native link and give an error if we're doing a bytecode
470 PrintAndReturn(progname, "Cannot find " + Libraries[i]);
475 // A user may specify an ar archive without -l, perhaps because it
476 // is not installed as a library. Detect that and link the library.
477 if (IsArchive(Pathname)) {
479 std::cerr << "Linking archive '" << Libraries[i] << "'\n";
481 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
482 PrintAndReturn(progname, ErrorMessage,
483 ": Error linking in '" + Libraries[i] + "'");
488 std::cerr << "Linking file '" << Libraries[i] << "'\n";
490 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
491 PrintAndReturn(progname, ErrorMessage,
492 ": error linking in '" + Libraries[i] + "'");