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