Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 1 | //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 10 | // This file contains basic Linker functionality that all usages will need. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Linker.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | a834f5d | 2004-12-16 19:19:24 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Bill Wendling | 41edad7 | 2006-11-27 10:09:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Reid Spencer | 328ead9 | 2005-12-13 20:00:37 +0000 | [diff] [blame] | 21 | Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags) |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 22 | : Composite(0) |
| 23 | , LibPaths() |
| 24 | , Flags(flags) |
| 25 | , Error() |
| 26 | , ProgramName(progname) |
| 27 | { |
Reid Spencer | 328ead9 | 2005-12-13 20:00:37 +0000 | [diff] [blame] | 28 | Composite = new Module(modname); |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) |
| 32 | : Composite(aModule) |
| 33 | , LibPaths() |
| 34 | , Flags(flags) |
| 35 | , Error() |
| 36 | , ProgramName(progname) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | Linker::~Linker() { |
| 41 | delete Composite; |
| 42 | } |
| 43 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 44 | bool |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 45 | Linker::error(const std::string& message) { |
| 46 | Error = message; |
Bill Wendling | 41edad7 | 2006-11-27 10:09:12 +0000 | [diff] [blame] | 47 | if (!(Flags&QuietErrors)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 48 | cerr << ProgramName << ": error: " << message << "\n"; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | Linker::warning(const std::string& message) { |
| 54 | Error = message; |
Bill Wendling | 41edad7 | 2006-11-27 10:09:12 +0000 | [diff] [blame] | 55 | if (!(Flags&QuietErrors)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 56 | cerr << ProgramName << ": warning: " << message << "\n"; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | Linker::verbose(const std::string& message) { |
Bill Wendling | 41edad7 | 2006-11-27 10:09:12 +0000 | [diff] [blame] | 62 | if (Flags&Verbose) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame^] | 63 | cerr << " " << message << "\n"; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
| 67 | Linker::addPath(const sys::Path& path) { |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 68 | LibPaths.push_back(path); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | Linker::addPaths(const std::vector<std::string>& paths) { |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 73 | for (unsigned i = 0; i != paths.size(); ++i) { |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 74 | sys::Path aPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 75 | aPath.set(paths[i]); |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 76 | LibPaths.push_back(aPath); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | Linker::addSystemPaths() { |
| 82 | sys::Path::GetBytecodeLibraryPaths(LibPaths); |
| 83 | LibPaths.insert(LibPaths.begin(),sys::Path("./")); |
| 84 | } |
| 85 | |
| 86 | Module* |
| 87 | Linker::releaseModule() { |
| 88 | Module* result = Composite; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 89 | LibPaths.clear(); |
| 90 | Error.clear(); |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 91 | Composite = 0; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 92 | Flags = 0; |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | // LoadObject - Read in and parse the bytecode file named by FN and return the |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 97 | // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 98 | // Error if an error occurs. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 99 | std::auto_ptr<Module> |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 100 | Linker::LoadObject(const sys::Path &FN) { |
| 101 | std::string ParseErrorMessage; |
| 102 | Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 103 | if (Result) |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 104 | return std::auto_ptr<Module>(Result); |
| 105 | Error = "Bytecode file '" + FN.toString() + "' could not be loaded"; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 106 | if (ParseErrorMessage.size()) |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 107 | Error += ": " + ParseErrorMessage; |
| 108 | return std::auto_ptr<Module>(); |
| 109 | } |
| 110 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 111 | // IsLibrary - Determine if "Name" is a library in "Directory". Return |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 112 | // a non-empty sys::Path if its found, an empty one otherwise. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 113 | static inline sys::Path IsLibrary(const std::string& Name, |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 114 | const sys::Path& Directory) { |
| 115 | |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 116 | sys::Path FullPath(Directory); |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 501d529 | 2006-07-28 22:52:11 +0000 | [diff] [blame] | 118 | // Try the libX.a form |
| 119 | FullPath.appendComponent("lib" + Name); |
| 120 | FullPath.appendSuffix("a"); |
| 121 | if (FullPath.isArchive()) |
| 122 | return FullPath; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 501d529 | 2006-07-28 22:52:11 +0000 | [diff] [blame] | 124 | // Try the libX.bca form |
| 125 | FullPath.eraseSuffix(); |
| 126 | FullPath.appendSuffix("bca"); |
| 127 | if (FullPath.isArchive()) |
| 128 | return FullPath; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 501d529 | 2006-07-28 22:52:11 +0000 | [diff] [blame] | 130 | // Try the libX.so (or .dylib) form |
| 131 | FullPath.eraseSuffix(); |
| 132 | FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1])); |
| 133 | if (FullPath.isDynamicLibrary()) // Native shared library? |
| 134 | return FullPath; |
| 135 | if (FullPath.isBytecodeFile()) // .so file containing bytecode? |
| 136 | return FullPath; |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 501d529 | 2006-07-28 22:52:11 +0000 | [diff] [blame] | 138 | // Not found .. fall through |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 139 | |
| 140 | // Indicate that the library was not found in the directory. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 141 | FullPath.clear(); |
| 142 | return FullPath; |
| 143 | } |
| 144 | |
| 145 | /// FindLib - Try to convert Filename into the name of a file that we can open, |
| 146 | /// if it does not already name a file we can open, by first trying to open |
| 147 | /// Filename, then libFilename.[suffix] for each of a set of several common |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 148 | /// library suffixes, in each of the directories in LibPaths. Returns an empty |
Reid Spencer | 903f21d | 2004-12-13 03:50:50 +0000 | [diff] [blame] | 149 | /// Path if no matching file can be found. |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 150 | /// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 151 | sys::Path |
John Criswell | 43da9c6 | 2006-01-17 22:01:57 +0000 | [diff] [blame] | 152 | Linker::FindLib(const std::string &Filename) { |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 153 | // Determine if the pathname can be found as it stands. |
| 154 | sys::Path FilePath(Filename); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 155 | if (FilePath.canRead() && |
Reid Spencer | de4cedc | 2004-12-13 03:00:28 +0000 | [diff] [blame] | 156 | (FilePath.isArchive() || FilePath.isDynamicLibrary())) |
| 157 | return FilePath; |
| 158 | |
| 159 | // Iterate over the directories in Paths to see if we can find the library |
| 160 | // there. |
| 161 | for (unsigned Index = 0; Index != LibPaths.size(); ++Index) { |
| 162 | sys::Path Directory(LibPaths[Index]); |
| 163 | sys::Path FullPath = IsLibrary(Filename,Directory); |
| 164 | if (!FullPath.isEmpty()) |
| 165 | return FullPath; |
| 166 | } |
| 167 | return sys::Path(); |
| 168 | } |