Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 1 | //===- Linker.cpp - Link together LLVM objects and libraries --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines to handle linking together LLVM bytecode files, |
| 11 | // and to handle annoying things like static libraries. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm-ld.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/PassManager.h" |
| 18 | #include "llvm/Bytecode/Reader.h" |
| 19 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/Transforms/IPO.h" |
| 22 | #include "llvm/Transforms/Scalar.h" |
| 23 | #include "llvm/Support/Linker.h" |
| 24 | #include "llvm/Config/config.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/FileUtilities.h" |
| 27 | #include "llvm/System/Signals.h" |
Reid Spencer | 8d9b680 | 2004-09-25 15:59:41 +0000 | [diff] [blame] | 28 | #include "llvm/System/Path.h" |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SystemUtils.h" |
| 30 | #include <algorithm> |
| 31 | #include <fstream> |
| 32 | #include <memory> |
| 33 | #include <set> |
| 34 | using namespace llvm; |
| 35 | |
| 36 | /// FindLib - Try to convert Filename into the name of a file that we can open, |
| 37 | /// if it does not already name a file we can open, by first trying to open |
| 38 | /// Filename, then libFilename.[suffix] for each of a set of several common |
| 39 | /// library suffixes, in each of the directories in Paths and the directory |
| 40 | /// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns |
| 41 | /// an empty string if no matching file can be found. |
| 42 | /// |
| 43 | std::string llvm::FindLib(const std::string &Filename, |
| 44 | const std::vector<std::string> &Paths, |
| 45 | bool SharedObjectOnly) { |
| 46 | // Determine if the pathname can be found as it stands. |
Reid Spencer | 8d9b680 | 2004-09-25 15:59:41 +0000 | [diff] [blame] | 47 | sys::Path FilePath; |
| 48 | if (FilePath.set_file(Filename) && FilePath.readable()) |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 49 | return Filename; |
| 50 | |
Reid Spencer | 8d9b680 | 2004-09-25 15:59:41 +0000 | [diff] [blame] | 51 | // Ask the System Path object to locate the library. This ensures that |
| 52 | // the library search is done correctly for a given platform. |
| 53 | sys::Path LibPath = sys::Path::GetLibraryPath(Filename,Paths); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 8d9b680 | 2004-09-25 15:59:41 +0000 | [diff] [blame] | 55 | return LibPath.get(); |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the |
| 59 | /// name of each externally-visible symbol defined in M. |
| 60 | /// |
| 61 | void llvm::GetAllDefinedSymbols(Module *M, |
| 62 | std::set<std::string> &DefinedSymbols) { |
| 63 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 64 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 65 | DefinedSymbols.insert(I->getName()); |
| 66 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 67 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 68 | DefinedSymbols.insert(I->getName()); |
| 69 | } |
| 70 | |
| 71 | /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still |
| 72 | /// exist in an LLVM module. This is a bit tricky because there may be two |
| 73 | /// symbols with the same name but different LLVM types that will be resolved to |
| 74 | /// each other but aren't currently (thus we need to treat it as resolved). |
| 75 | /// |
| 76 | /// Inputs: |
| 77 | /// M - The module in which to find undefined symbols. |
| 78 | /// |
| 79 | /// Outputs: |
| 80 | /// UndefinedSymbols - A set of C++ strings containing the name of all |
| 81 | /// undefined symbols. |
| 82 | /// |
| 83 | void |
| 84 | llvm::GetAllUndefinedSymbols(Module *M, |
| 85 | std::set<std::string> &UndefinedSymbols) { |
| 86 | std::set<std::string> DefinedSymbols; |
| 87 | UndefinedSymbols.clear(); // Start out empty |
| 88 | |
Reid Spencer | 6cb21d4 | 2004-10-27 23:18:45 +0000 | [diff] [blame] | 89 | // Add in all the external functions |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 90 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 91 | if (I->hasName()) { |
| 92 | if (I->isExternal()) |
| 93 | UndefinedSymbols.insert(I->getName()); |
| 94 | else if (!I->hasInternalLinkage()) |
| 95 | DefinedSymbols.insert(I->getName()); |
| 96 | } |
Reid Spencer | 6cb21d4 | 2004-10-27 23:18:45 +0000 | [diff] [blame] | 97 | |
| 98 | // Add in all the external globals |
Reid Spencer | c0af3f0 | 2004-09-13 01:27:53 +0000 | [diff] [blame] | 99 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 100 | if (I->hasName()) { |
| 101 | if (I->isExternal()) |
| 102 | UndefinedSymbols.insert(I->getName()); |
| 103 | else if (!I->hasInternalLinkage()) |
| 104 | DefinedSymbols.insert(I->getName()); |
| 105 | } |
| 106 | |
| 107 | // Prune out any defined symbols from the undefined symbols set... |
| 108 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 109 | I != UndefinedSymbols.end(); ) |
| 110 | if (DefinedSymbols.count(*I)) |
| 111 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 112 | else |
| 113 | ++I; // Keep this symbol in the undefined symbols list |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /// LoadObject - Read in and parse the bytecode file named by FN and return the |
| 118 | /// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an |
| 119 | /// error occurs. |
| 120 | /// |
| 121 | std::auto_ptr<Module> llvm::LoadObject(const std::string &FN, |
| 122 | std::string &ErrorMessage) { |
| 123 | std::string ParserErrorMessage; |
| 124 | Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage); |
| 125 | if (Result) return std::auto_ptr<Module>(Result); |
| 126 | ErrorMessage = "Bytecode file '" + FN + "' could not be loaded"; |
| 127 | if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage; |
| 128 | return std::auto_ptr<Module>(); |
| 129 | } |
| 130 | |
| 131 | /// LinkInArchive - opens an archive library and link in all objects which |
| 132 | /// provide symbols that are currently undefined. |
| 133 | /// |
| 134 | /// Inputs: |
| 135 | /// M - The module in which to link the archives. |
| 136 | /// Filename - The pathname of the archive. |
| 137 | /// Verbose - Flags whether verbose messages should be printed. |
| 138 | /// |
| 139 | /// Outputs: |
| 140 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 141 | /// |
| 142 | /// Return Value: |
| 143 | /// TRUE - An error occurred. |
| 144 | /// FALSE - No errors. |
| 145 | /// |
| 146 | static bool LinkInArchive(Module *M, |
| 147 | const std::string &Filename, |
| 148 | std::string &ErrorMessage, |
| 149 | bool Verbose) |
| 150 | { |
| 151 | // Find all of the symbols currently undefined in the bytecode program. |
| 152 | // If all the symbols are defined, the program is complete, and there is |
| 153 | // no reason to link in any archive files. |
| 154 | std::set<std::string> UndefinedSymbols; |
| 155 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 156 | if (UndefinedSymbols.empty()) { |
| 157 | if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; |
| 158 | return false; // No need to link anything in! |
| 159 | } |
| 160 | |
| 161 | // Load in the archive objects. |
| 162 | if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n"; |
| 163 | std::vector<Module*> Objects; |
| 164 | if (ReadArchiveFile(Filename, Objects, &ErrorMessage)) |
| 165 | return true; |
| 166 | |
| 167 | // Figure out which symbols are defined by all of the modules in the archive. |
| 168 | std::vector<std::set<std::string> > DefinedSymbols; |
| 169 | DefinedSymbols.resize(Objects.size()); |
| 170 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 171 | GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); |
| 172 | } |
| 173 | |
| 174 | // While we are linking in object files, loop. |
| 175 | bool Linked = true; |
| 176 | while (Linked) { |
| 177 | Linked = false; |
| 178 | |
| 179 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 180 | // Consider whether we need to link in this module... we only need to |
| 181 | // link it in if it defines some symbol which is so far undefined. |
| 182 | // |
| 183 | const std::set<std::string> &DefSymbols = DefinedSymbols[i]; |
| 184 | |
| 185 | bool ObjectRequired = false; |
| 186 | |
| 187 | // |
| 188 | // If the object defines main() and the program currently has main() |
| 189 | // undefined, then automatically link in the module. Otherwise, look to |
| 190 | // see if it defines a symbol that is currently undefined. |
| 191 | // |
| 192 | if ((M->getMainFunction() == NULL) && |
| 193 | ((DefSymbols.find ("main")) != DefSymbols.end())) { |
| 194 | ObjectRequired = true; |
| 195 | } else { |
| 196 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), |
| 197 | E = UndefinedSymbols.end(); I != E; ++I) |
| 198 | if (DefSymbols.count(*I)) { |
| 199 | if (Verbose) |
| 200 | std::cerr << " Found object '" |
| 201 | << Objects[i]->getModuleIdentifier () |
| 202 | << "' providing symbol '" << *I << "'...\n"; |
| 203 | ObjectRequired = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // We DO need to link this object into the program... |
| 209 | if (ObjectRequired) { |
| 210 | if (LinkModules(M, Objects[i], &ErrorMessage)) |
| 211 | return true; // Couldn't link in the right object file... |
| 212 | |
| 213 | // Since we have linked in this object, delete it from the list of |
| 214 | // objects to consider in this archive file. |
| 215 | std::swap(Objects[i], Objects.back()); |
| 216 | std::swap(DefinedSymbols[i], DefinedSymbols.back()); |
| 217 | Objects.pop_back(); |
| 218 | DefinedSymbols.pop_back(); |
| 219 | --i; // Do not skip an entry |
| 220 | |
| 221 | // The undefined symbols set should have shrunk. |
| 222 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 223 | Linked = true; // We have linked something in! |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /// LinkInFile - opens a bytecode file and links in all objects which |
| 232 | /// provide symbols that are currently undefined. |
| 233 | /// |
| 234 | /// Inputs: |
| 235 | /// HeadModule - The module in which to link the bytecode file. |
| 236 | /// Filename - The pathname of the bytecode file. |
| 237 | /// Verbose - Flags whether verbose messages should be printed. |
| 238 | /// |
| 239 | /// Outputs: |
| 240 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 241 | /// |
| 242 | /// Return Value: |
| 243 | /// TRUE - An error occurred. |
| 244 | /// FALSE - No errors. |
| 245 | /// |
| 246 | static bool LinkInFile(Module *HeadModule, |
| 247 | const std::string &Filename, |
| 248 | std::string &ErrorMessage, |
| 249 | bool Verbose) |
| 250 | { |
| 251 | std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage)); |
| 252 | if (M.get() == 0) return true; |
| 253 | bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage); |
| 254 | if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n"; |
| 255 | return Result; |
| 256 | } |
| 257 | |
| 258 | /// LinkFiles - takes a module and a list of files and links them all together. |
| 259 | /// It locates the file either in the current directory, as its absolute |
| 260 | /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. |
| 261 | /// |
| 262 | /// Inputs: |
| 263 | /// progname - The name of the program (infamous argv[0]). |
| 264 | /// HeadModule - The module under which all files will be linked. |
| 265 | /// Files - A vector of C++ strings indicating the LLVM bytecode filenames |
| 266 | /// to be linked. The names can refer to a mixture of pure LLVM |
| 267 | /// bytecode files and archive (ar) formatted files. |
| 268 | /// Verbose - Flags whether verbose output should be printed while linking. |
| 269 | /// |
| 270 | /// Outputs: |
| 271 | /// HeadModule - The module will have the specified LLVM bytecode files linked |
| 272 | /// in. |
| 273 | /// |
| 274 | /// Return value: |
| 275 | /// FALSE - No errors. |
| 276 | /// TRUE - Some error occurred. |
| 277 | /// |
| 278 | bool llvm::LinkFiles(const char *progname, Module *HeadModule, |
| 279 | const std::vector<std::string> &Files, bool Verbose) { |
| 280 | // String in which to receive error messages. |
| 281 | std::string ErrorMessage; |
| 282 | |
| 283 | // Full pathname of the file |
| 284 | std::string Pathname; |
| 285 | |
| 286 | // Get the library search path from the environment |
| 287 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 288 | |
| 289 | for (unsigned i = 0; i < Files.size(); ++i) { |
| 290 | // Determine where this file lives. |
| 291 | if (FileOpenable(Files[i])) { |
| 292 | Pathname = Files[i]; |
| 293 | } else { |
| 294 | if (SearchPath == NULL) { |
| 295 | std::cerr << progname << ": Cannot find linker input file '" |
| 296 | << Files[i] << "'\n"; |
| 297 | std::cerr << progname |
| 298 | << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n"; |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | Pathname = std::string(SearchPath)+"/"+Files[i]; |
| 303 | if (!FileOpenable(Pathname)) { |
| 304 | std::cerr << progname << ": Cannot find linker input file '" |
| 305 | << Files[i] << "'\n"; |
| 306 | return true; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // A user may specify an ar archive without -l, perhaps because it |
| 311 | // is not installed as a library. Detect that and link the library. |
| 312 | if (IsArchive(Pathname)) { |
| 313 | if (Verbose) |
| 314 | std::cerr << "Trying to link archive '" << Pathname << "'\n"; |
| 315 | |
| 316 | if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { |
| 317 | std::cerr << progname << ": Error linking in archive '" << Pathname |
| 318 | << "': " << ErrorMessage << "\n"; |
| 319 | return true; |
| 320 | } |
| 321 | } else if (IsBytecode(Pathname)) { |
| 322 | if (Verbose) |
| 323 | std::cerr << "Trying to link bytecode file '" << Pathname << "'\n"; |
| 324 | |
| 325 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
| 326 | std::cerr << progname << ": Error linking in bytecode file '" |
| 327 | << Pathname << "': " << ErrorMessage << "\n"; |
| 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /// LinkLibraries - takes the specified library files and links them into the |
| 337 | /// main bytecode object file. |
| 338 | /// |
| 339 | /// Inputs: |
| 340 | /// progname - The name of the program (infamous argv[0]). |
| 341 | /// HeadModule - The module into which all necessary libraries will be linked. |
| 342 | /// Libraries - The list of libraries to link into the module. |
| 343 | /// LibPaths - The list of library paths in which to find libraries. |
| 344 | /// Verbose - Flags whether verbose messages should be printed. |
| 345 | /// Native - Flags whether native code is being generated. |
| 346 | /// |
| 347 | /// Outputs: |
| 348 | /// HeadModule - The module will have all necessary libraries linked in. |
| 349 | /// |
| 350 | /// Return value: |
| 351 | /// FALSE - No error. |
| 352 | /// TRUE - Error. |
| 353 | /// |
| 354 | void llvm::LinkLibraries(const char *progname, Module *HeadModule, |
| 355 | const std::vector<std::string> &Libraries, |
| 356 | const std::vector<std::string> &LibPaths, |
| 357 | bool Verbose, bool Native) { |
| 358 | // String in which to receive error messages. |
| 359 | std::string ErrorMessage; |
| 360 | |
| 361 | for (unsigned i = 0; i < Libraries.size(); ++i) { |
| 362 | // Determine where this library lives. |
| 363 | std::string Pathname = FindLib(Libraries[i], LibPaths); |
| 364 | if (Pathname.empty()) { |
| 365 | // If the pathname does not exist, then continue to the next one if |
| 366 | // we're doing a native link and give an error if we're doing a bytecode |
| 367 | // link. |
| 368 | if (!Native) { |
| 369 | std::cerr << progname << ": WARNING: Cannot find library -l" |
| 370 | << Libraries[i] << "\n"; |
| 371 | continue; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // A user may specify an ar archive without -l, perhaps because it |
| 376 | // is not installed as a library. Detect that and link the library. |
| 377 | if (IsArchive(Pathname)) { |
| 378 | if (Verbose) |
| 379 | std::cerr << "Trying to link archive '" << Pathname << "' (-l" |
| 380 | << Libraries[i] << ")\n"; |
| 381 | |
| 382 | if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { |
| 383 | std::cerr << progname << ": " << ErrorMessage |
| 384 | << ": Error linking in archive '" << Pathname << "' (-l" |
| 385 | << Libraries[i] << ")\n"; |
| 386 | exit(1); |
| 387 | } |
| 388 | } else if (IsBytecode(Pathname)) { |
| 389 | if (Verbose) |
| 390 | std::cerr << "Trying to link bytecode file '" << Pathname |
| 391 | << "' (-l" << Libraries[i] << ")\n"; |
| 392 | |
| 393 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
| 394 | std::cerr << progname << ": " << ErrorMessage |
| 395 | << ": error linking in bytecode file '" << Pathname << "' (-l" |
| 396 | << Libraries[i] << ")\n"; |
| 397 | exit(1); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |