Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 10 | // This file contains routines to handle linking together LLVM bitcode files, |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 11 | // and to handle annoying things like static libraries. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Linker.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/ReaderWriter.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame^] | 17 | #include "llvm/IR/Module.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 53424ad | 2007-08-08 19:52:29 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/system_error.h" |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 24 | // LinkItems - This function is the main entry point into linking. It takes a |
| 25 | // list of LinkItem which indicates the order the files should be linked and |
| 26 | // how each file should be treated (plain file or with library search). The |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 27 | // function only links bitcode and produces a result list of items that are |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 28 | // native objects. |
Reid Spencer | e84de29 | 2004-12-13 02:59:52 +0000 | [diff] [blame] | 29 | bool |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 30 | Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) { |
| 31 | // Clear the NativeItems just in case |
| 32 | NativeItems.clear(); |
| 33 | |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 34 | // For each linkage item ... |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 35 | for (ItemList::const_iterator I = Items.begin(), E = Items.end(); |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 36 | I != E; ++I) { |
| 37 | if (I->second) { |
| 38 | // Link in the library suggested. |
Reid Spencer | c07cfdd | 2007-04-04 06:44:18 +0000 | [diff] [blame] | 39 | bool is_native = false; |
| 40 | if (LinkInLibrary(I->first, is_native)) |
Reid Spencer | e84de29 | 2004-12-13 02:59:52 +0000 | [diff] [blame] | 41 | return true; |
Reid Spencer | c07cfdd | 2007-04-04 06:44:18 +0000 | [diff] [blame] | 42 | if (is_native) |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 43 | NativeItems.push_back(*I); |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 44 | } else { |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 45 | // Link in the file suggested |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 46 | bool is_native = false; |
| 47 | if (LinkInFile(sys::Path(I->first), is_native)) |
Reid Spencer | e84de29 | 2004-12-13 02:59:52 +0000 | [diff] [blame] | 48 | return true; |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 49 | if (is_native) |
| 50 | NativeItems.push_back(*I); |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Reid Spencer | e84de29 | 2004-12-13 02:59:52 +0000 | [diff] [blame] | 54 | return false; |
Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /// LinkInLibrary - links one library into the HeadModule. |
| 59 | /// |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 60 | bool Linker::LinkInLibrary(StringRef Lib, bool& is_native) { |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 61 | is_native = false; |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 62 | // Determine where this library lives. |
| 63 | sys::Path Pathname = FindLib(Lib); |
| 64 | if (Pathname.isEmpty()) |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 65 | return error("Cannot find library '" + Lib.str() + "'"); |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 66 | |
| 67 | // If its an archive, try to link it in |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 68 | std::string Magic; |
| 69 | Pathname.getMagicNumber(Magic, 64); |
| 70 | switch (sys::IdentifyFileType(Magic.c_str(), 64)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 71 | default: llvm_unreachable("Bad file type identification"); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 72 | case sys::Unknown_FileType: |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 73 | return warning("Supposed library '" + Lib.str() + "' isn't a library."); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 1a019e5 | 2007-05-06 06:02:13 +0000 | [diff] [blame] | 75 | case sys::Bitcode_FileType: |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 76 | // LLVM ".so" file. |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 77 | if (LinkInFile(Pathname, is_native)) |
Reid Spencer | 49068bf | 2007-08-16 07:47:30 +0000 | [diff] [blame] | 78 | return true; |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 79 | break; |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 80 | |
| 81 | case sys::Archive_FileType: |
Reid Spencer | c9a83e4 | 2007-04-30 00:29:39 +0000 | [diff] [blame] | 82 | if (LinkInArchive(Pathname, is_native)) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 83 | return error("Cannot link archive '" + Pathname.str() + "'"); |
Reid Spencer | f4484f3 | 2006-01-10 03:14:40 +0000 | [diff] [blame] | 84 | break; |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 85 | |
Reid Spencer | 18da072 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 86 | case sys::ELF_Relocatable_FileType: |
| 87 | case sys::ELF_SharedObject_FileType: |
| 88 | case sys::Mach_O_Object_FileType: |
| 89 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 90 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 91 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 92 | case sys::COFF_FileType: |
| 93 | is_native = true; |
| 94 | break; |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | /// LinkLibraries - takes the specified library files and links them into the |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 100 | /// main bitcode object file. |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 101 | /// |
| 102 | /// Inputs: |
| 103 | /// Libraries - The list of libraries to link into the module. |
| 104 | /// |
| 105 | /// Return value: |
| 106 | /// FALSE - No error. |
| 107 | /// TRUE - Error. |
| 108 | /// |
| 109 | bool Linker::LinkInLibraries(const std::vector<std::string> &Libraries) { |
| 110 | |
| 111 | // Process the set of libraries we've been provided. |
Reid Spencer | c07cfdd | 2007-04-04 06:44:18 +0000 | [diff] [blame] | 112 | bool is_native = false; |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 113 | for (unsigned i = 0; i < Libraries.size(); ++i) |
Reid Spencer | c07cfdd | 2007-04-04 06:44:18 +0000 | [diff] [blame] | 114 | if (LinkInLibrary(Libraries[i], is_native)) |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 115 | return true; |
| 116 | |
Chris Lattner | ad988f3 | 2005-03-15 22:51:40 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 119 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 120 | /// LinkInFile - opens a bitcode file and links in all objects which |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 121 | /// provide symbols that are currently undefined. |
| 122 | /// |
| 123 | /// Inputs: |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 124 | /// File - The pathname of the bitcode file. |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 125 | /// |
| 126 | /// Outputs: |
| 127 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 128 | /// |
| 129 | /// Return Value: |
| 130 | /// TRUE - An error occurred. |
| 131 | /// FALSE - No errors. |
| 132 | /// |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 133 | bool Linker::LinkInFile(const sys::Path &File, bool &is_native) { |
| 134 | is_native = false; |
Reid Spencer | 53424ad | 2007-08-08 19:52:29 +0000 | [diff] [blame] | 135 | |
| 136 | // Check for a file of name "-", which means "read standard input" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 137 | if (File.str() == "-") { |
Reid Spencer | 53424ad | 2007-08-08 19:52:29 +0000 | [diff] [blame] | 138 | std::auto_ptr<Module> M; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 139 | OwningPtr<MemoryBuffer> Buffer; |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 140 | error_code ec; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 141 | if (!(ec = MemoryBuffer::getSTDIN(Buffer))) { |
Dan Gohman | 30377e7 | 2010-05-27 17:31:51 +0000 | [diff] [blame] | 142 | if (!Buffer->getBufferSize()) { |
Dan Gohman | 30377e7 | 2010-05-27 17:31:51 +0000 | [diff] [blame] | 143 | Error = "standard input is empty"; |
| 144 | } else { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 145 | M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error)); |
Dan Gohman | 30377e7 | 2010-05-27 17:31:51 +0000 | [diff] [blame] | 146 | if (M.get()) |
| 147 | if (!LinkInModule(M.get(), &Error)) |
| 148 | return false; |
| 149 | } |
Daniel Dunbar | d65267e | 2009-11-10 00:43:58 +0000 | [diff] [blame] | 150 | } |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 151 | return error("Cannot link stdin: " + ec.message()); |
Reid Spencer | 53424ad | 2007-08-08 19:52:29 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Dan Gohman | 95131fc | 2010-05-27 17:18:38 +0000 | [diff] [blame] | 154 | // Determine what variety of file it is. |
| 155 | std::string Magic; |
| 156 | if (!File.getMagicNumber(Magic, 64)) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 157 | return error("Cannot find linker input '" + File.str() + "'"); |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 158 | |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 159 | switch (sys::IdentifyFileType(Magic.c_str(), 64)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 160 | default: llvm_unreachable("Bad file type identification"); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 161 | case sys::Unknown_FileType: |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 162 | return warning("Ignoring file '" + File.str() + |
Reid Spencer | 49068bf | 2007-08-16 07:47:30 +0000 | [diff] [blame] | 163 | "' because does not contain bitcode."); |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 164 | |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 165 | case sys::Archive_FileType: |
| 166 | // A user may specify an ar archive without -l, perhaps because it |
| 167 | // is not installed as a library. Detect that and link the archive. |
Reid Spencer | c9a83e4 | 2007-04-30 00:29:39 +0000 | [diff] [blame] | 168 | if (LinkInArchive(File, is_native)) |
Reid Spencer | 49068bf | 2007-08-16 07:47:30 +0000 | [diff] [blame] | 169 | return true; |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 170 | break; |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 171 | |
Gabor Greif | e75ca3d | 2007-07-06 13:38:17 +0000 | [diff] [blame] | 172 | case sys::Bitcode_FileType: { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 173 | verbose("Linking bitcode file '" + File.str() + "'"); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 174 | std::auto_ptr<Module> M(LoadObject(File)); |
| 175 | if (M.get() == 0) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 176 | return error("Cannot load file '" + File.str() + "': " + Error); |
Reid Spencer | af11dc0 | 2007-08-16 07:23:37 +0000 | [diff] [blame] | 177 | if (LinkInModule(M.get(), &Error)) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 178 | return error("Cannot link file '" + File.str() + "': " + Error); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 180 | verbose("Linked in file '" + File.str() + "'"); |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
Reid Spencer | 18da072 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 184 | case sys::ELF_Relocatable_FileType: |
| 185 | case sys::ELF_SharedObject_FileType: |
| 186 | case sys::Mach_O_Object_FileType: |
| 187 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 188 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 189 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 190 | case sys::COFF_FileType: |
| 191 | is_native = true; |
| 192 | break; |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /// LinkFiles - takes a module and a list of files and links them all together. |
| 198 | /// It locates the file either in the current directory, as its absolute |
| 199 | /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. |
| 200 | /// |
| 201 | /// Inputs: |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 202 | /// Files - A vector of sys::Path indicating the LLVM bitcode filenames |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 203 | /// to be linked. The names can refer to a mixture of pure LLVM |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 204 | /// bitcode files and archive (ar) formatted files. |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 205 | /// |
| 206 | /// Return value: |
| 207 | /// FALSE - No errors. |
| 208 | /// TRUE - Some error occurred. |
| 209 | /// |
| 210 | bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) { |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 211 | bool is_native; |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 212 | for (unsigned i = 0; i < Files.size(); ++i) |
Reid Spencer | c853973 | 2007-04-04 06:33:17 +0000 | [diff] [blame] | 213 | if (LinkInFile(Files[i], is_native)) |
Chris Lattner | fc82ef6 | 2005-03-15 22:55:17 +0000 | [diff] [blame] | 214 | return true; |
| 215 | return false; |
| 216 | } |