Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 1 | //===- Linker.cpp - Link together LLVM objects and libraries --------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame^] | 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 | // |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 10 | // |
Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 11 | // This file contains routines to handle linking together LLVM bytecode files, |
| 12 | // and to handle annoying things like static libraries. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 16 | #include "gccld.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/Bytecode/Reader.h" |
| 20 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Transforms/IPO.h" |
| 23 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Linker.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 25 | #include "Support/CommandLine.h" |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 26 | #include "Support/FileUtilities.h" |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 27 | #include "Support/Signals.h" |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 28 | #include "Support/SystemUtils.h" |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 29 | #include <algorithm> |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 30 | #include <fstream> |
| 31 | #include <memory> |
| 32 | #include <set> |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 33 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 34 | /// FileExists - determines if the specified filename exists and is readable. |
| 35 | /// |
| 36 | /// Inputs: |
| 37 | /// FN - The name of the file. |
| 38 | /// |
| 39 | /// Outputs: |
| 40 | /// None. |
| 41 | /// |
| 42 | /// Return Value: |
| 43 | /// TRUE - The file exists and is readable. |
| 44 | /// FALSE - The file does not exist or is unreadable. |
| 45 | /// |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 46 | static inline bool FileExists(const std::string &FN) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 47 | return access(FN.c_str(), R_OK | F_OK) != -1; |
| 48 | } |
| 49 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 50 | /// IsArchive - determines if the specified file is an ar archive |
| 51 | /// by checking the magic string at the beginning of the file. |
| 52 | /// |
| 53 | /// Inputs: |
| 54 | /// filename - A C++ string containing the name of the file. |
| 55 | /// |
| 56 | /// Outputs: |
| 57 | /// None. |
| 58 | /// |
| 59 | /// Return value: |
| 60 | /// TRUE - The file is an archive. |
| 61 | /// FALSE - The file is not an archive. |
| 62 | /// |
Misha Brukman | 61087cc | 2003-09-30 17:51:20 +0000 | [diff] [blame] | 63 | static inline bool IsArchive(const std::string &filename) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 64 | std::string ArchiveMagic("!<arch>\012"); |
| 65 | char buf[1 + ArchiveMagic.size()]; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 66 | std::ifstream f(filename.c_str()); |
| 67 | f.read(buf, ArchiveMagic.size()); |
| 68 | buf[ArchiveMagic.size()] = '\0'; |
| 69 | return ArchiveMagic == buf; |
| 70 | } |
| 71 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 72 | /// FindLib - locates a particular library. It will prepend and append |
| 73 | /// various directories, prefixes, and suffixes until it can find the library. |
| 74 | /// |
| 75 | /// Inputs: |
| 76 | /// Filename - Name of the file to find. |
| 77 | /// Paths - List of directories to search. |
| 78 | /// |
| 79 | /// Outputs: |
| 80 | /// None. |
| 81 | /// |
| 82 | /// Return value: |
| 83 | /// The name of the file is returned. |
| 84 | /// If the file is not found, an empty string is returned. |
| 85 | /// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 86 | static std::string |
Misha Brukman | 61087cc | 2003-09-30 17:51:20 +0000 | [diff] [blame] | 87 | FindLib(const std::string &Filename, const std::vector<std::string> &Paths) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 88 | // Determine if the pathname can be found as it stands. |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 89 | if (FileExists(Filename)) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 90 | return Filename; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 91 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 92 | // If that doesn't work, convert the name into a library name. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 93 | std::string LibName = "lib" + Filename; |
| 94 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 95 | // Iterate over the directories in Paths to see if we can find the library |
| 96 | // there. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 97 | for (unsigned Index = 0; Index != Paths.size(); ++Index) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 98 | std::string Directory = Paths[Index] + "/"; |
| 99 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 100 | if (FileExists(Directory + LibName + ".bc")) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 101 | return Directory + LibName + ".bc"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 102 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 103 | if (FileExists(Directory + LibName + ".so")) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 104 | return Directory + LibName + ".so"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 105 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 106 | if (FileExists(Directory + LibName + ".a")) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 107 | return Directory + LibName + ".a"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 108 | } |
| 109 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 110 | // One last hope: Check LLVM_LIB_SEARCH_PATH. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 111 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 112 | if (SearchPath == NULL) |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 113 | return std::string(); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 114 | |
| 115 | LibName = std::string(SearchPath) + "/" + LibName; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 116 | if (FileExists(LibName)) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 117 | return LibName; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 118 | |
| 119 | return std::string(); |
| 120 | } |
| 121 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 122 | /// GetAllDefinedSymbols - finds all of the defined symbols in the specified |
| 123 | /// module. |
| 124 | /// |
| 125 | /// Inputs: |
| 126 | /// M - The module in which to find defined symbols. |
| 127 | /// |
| 128 | /// Outputs: |
| 129 | /// DefinedSymbols - A set of C++ strings that will contain the name of all |
| 130 | /// defined symbols. |
| 131 | /// |
| 132 | /// Return value: |
| 133 | /// None. |
| 134 | /// |
Misha Brukman | 61087cc | 2003-09-30 17:51:20 +0000 | [diff] [blame] | 135 | void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 136 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 137 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 138 | DefinedSymbols.insert(I->getName()); |
| 139 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 140 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 141 | DefinedSymbols.insert(I->getName()); |
| 142 | } |
| 143 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 144 | /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still |
| 145 | /// exist in an LLVM module. This is a bit tricky because there may be two |
| 146 | /// symbols with the same name but different LLVM types that will be resolved to |
| 147 | /// each other but aren't currently (thus we need to treat it as resolved). |
| 148 | /// |
| 149 | /// Inputs: |
| 150 | /// M - The module in which to find undefined symbols. |
| 151 | /// |
| 152 | /// Outputs: |
| 153 | /// UndefinedSymbols - A set of C++ strings containing the name of all |
| 154 | /// undefined symbols. |
| 155 | /// |
| 156 | /// Return value: |
| 157 | /// None. |
| 158 | /// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 159 | void |
Misha Brukman | 61087cc | 2003-09-30 17:51:20 +0000 | [diff] [blame] | 160 | GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 161 | std::set<std::string> DefinedSymbols; |
| 162 | UndefinedSymbols.clear(); // Start out empty |
| 163 | |
| 164 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 165 | if (I->hasName()) { |
| 166 | if (I->isExternal()) |
| 167 | UndefinedSymbols.insert(I->getName()); |
| 168 | else if (!I->hasInternalLinkage()) |
| 169 | DefinedSymbols.insert(I->getName()); |
| 170 | } |
| 171 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 172 | if (I->hasName()) { |
| 173 | if (I->isExternal()) |
| 174 | UndefinedSymbols.insert(I->getName()); |
| 175 | else if (!I->hasInternalLinkage()) |
| 176 | DefinedSymbols.insert(I->getName()); |
| 177 | } |
| 178 | |
| 179 | // Prune out any defined symbols from the undefined symbols set... |
| 180 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 181 | I != UndefinedSymbols.end(); ) |
| 182 | if (DefinedSymbols.count(*I)) |
| 183 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 184 | else |
| 185 | ++I; // Keep this symbol in the undefined symbols list |
| 186 | } |
| 187 | |
| 188 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 189 | /// LoadObject - reads the specified bytecode object file. |
| 190 | /// |
| 191 | /// Inputs: |
| 192 | /// FN - The name of the file to load. |
| 193 | /// |
| 194 | /// Outputs: |
| 195 | /// OutErrorMessage - The error message to give back to the caller. |
| 196 | /// |
| 197 | /// Return Value: |
| 198 | /// A pointer to a module represening the bytecode file is returned. |
| 199 | /// If an error occurs, the pointer is 0. |
| 200 | /// |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 201 | std::auto_ptr<Module> |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 202 | LoadObject(const std::string & FN, std::string &OutErrorMessage) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 203 | std::string ErrorMessage; |
| 204 | Module *Result = ParseBytecodeFile(FN, &ErrorMessage); |
| 205 | if (Result) return std::auto_ptr<Module>(Result); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 206 | OutErrorMessage = "Bytecode file '" + FN + "' corrupt!"; |
| 207 | if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage; |
| 208 | return std::auto_ptr<Module>(); |
| 209 | } |
| 210 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 211 | /// LinkInArchive - opens an archive library and link in all objects which |
| 212 | /// provide symbols that are currently undefined. |
| 213 | /// |
| 214 | /// Inputs: |
| 215 | /// M - The module in which to link the archives. |
| 216 | /// Filename - The pathname of the archive. |
| 217 | /// Verbose - Flags whether verbose messages should be printed. |
| 218 | /// |
| 219 | /// Outputs: |
| 220 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 221 | /// |
| 222 | /// Return Value: |
| 223 | /// TRUE - An error occurred. |
| 224 | /// FALSE - No errors. |
| 225 | /// |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 226 | static bool LinkInArchive(Module *M, |
| 227 | const std::string &Filename, |
| 228 | std::string &ErrorMessage, |
| 229 | bool Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 230 | { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 231 | // Find all of the symbols currently undefined in the bytecode program. |
| 232 | // If all the symbols are defined, the program is complete, and there is |
| 233 | // no reason to link in any archive files. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 234 | std::set<std::string> UndefinedSymbols; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 235 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 236 | if (UndefinedSymbols.empty()) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 237 | if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; |
| 238 | return false; // No need to link anything in! |
| 239 | } |
| 240 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 241 | // Load in the archive objects. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 242 | if (Verbose) std::cerr << " Loading '" << Filename << "'\n"; |
| 243 | std::vector<Module*> Objects; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 244 | if (ReadArchiveFile(Filename, Objects, &ErrorMessage)) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 245 | return true; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 246 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 247 | // Figure out which symbols are defined by all of the modules in the archive. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 248 | std::vector<std::set<std::string> > DefinedSymbols; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 249 | DefinedSymbols.resize(Objects.size()); |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 250 | for (unsigned i = 0; i != Objects.size(); ++i) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 251 | GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); |
| 252 | } |
| 253 | |
| 254 | // While we are linking in object files, loop. |
| 255 | bool Linked = true; |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 256 | while (Linked) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 257 | Linked = false; |
| 258 | |
| 259 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 260 | // Consider whether we need to link in this module... we only need to |
| 261 | // link it in if it defines some symbol which is so far undefined. |
| 262 | // |
| 263 | const std::set<std::string> &DefSymbols = DefinedSymbols[i]; |
| 264 | |
| 265 | bool ObjectRequired = false; |
| 266 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), |
| 267 | E = UndefinedSymbols.end(); I != E; ++I) |
| 268 | if (DefSymbols.count(*I)) { |
| 269 | if (Verbose) |
| 270 | std::cerr << " Found object providing symbol '" << *I << "'...\n"; |
| 271 | ObjectRequired = true; |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | // We DO need to link this object into the program... |
| 276 | if (ObjectRequired) { |
| 277 | if (LinkModules(M, Objects[i], &ErrorMessage)) |
| 278 | return true; // Couldn't link in the right object file... |
| 279 | |
| 280 | // Since we have linked in this object, delete it from the list of |
| 281 | // objects to consider in this archive file. |
| 282 | std::swap(Objects[i], Objects.back()); |
| 283 | std::swap(DefinedSymbols[i], DefinedSymbols.back()); |
| 284 | Objects.pop_back(); |
| 285 | DefinedSymbols.pop_back(); |
| 286 | --i; // Do not skip an entry |
| 287 | |
| 288 | // The undefined symbols set should have shrunk. |
| 289 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 290 | Linked = true; // We have linked something in! |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 298 | /// LinkInFile - opens an archive library and link in all objects which |
| 299 | /// provide symbols that are currently undefined. |
| 300 | /// |
| 301 | /// Inputs: |
| 302 | /// HeadModule - The module in which to link the archives. |
| 303 | /// Filename - The pathname of the archive. |
| 304 | /// Verbose - Flags whether verbose messages should be printed. |
| 305 | /// |
| 306 | /// Outputs: |
| 307 | /// ErrorMessage - A C++ string detailing what error occurred, if any. |
| 308 | /// |
| 309 | /// Return Value: |
| 310 | /// TRUE - An error occurred. |
| 311 | /// FALSE - No errors. |
| 312 | /// |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 313 | static bool LinkInFile(Module *HeadModule, |
| 314 | const std::string &Filename, |
| 315 | std::string &ErrorMessage, |
| 316 | bool Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 317 | { |
| 318 | std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage)); |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 319 | if (M.get() == 0) return true; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 320 | if (Verbose) std::cerr << "Linking in '" << Filename << "'\n"; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 321 | return LinkModules(HeadModule, M.get(), &ErrorMessage); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 324 | /// LinkFiles - takes a module and a list of files and links them all together. |
| 325 | /// It locates the file either in the current directory, as its absolute |
| 326 | /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. |
| 327 | /// |
| 328 | /// Inputs: |
| 329 | /// progname - The name of the program (infamous argv[0]). |
| 330 | /// HeadModule - The module under which all files will be linked. |
| 331 | /// Files - A vector of C++ strings indicating the LLVM bytecode filenames |
| 332 | /// to be linked. The names can refer to a mixture of pure LLVM |
| 333 | /// bytecode files and archive (ar) formatted files. |
| 334 | /// Verbose - Flags whether verbose output should be printed while linking. |
| 335 | /// |
| 336 | /// Outputs: |
| 337 | /// HeadModule - The module will have the specified LLVM bytecode files linked |
| 338 | /// in. |
| 339 | /// |
| 340 | /// Return value: |
| 341 | /// FALSE - No errors. |
| 342 | /// TRUE - Some error occurred. |
| 343 | /// |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 344 | bool LinkFiles(const char *progname, |
| 345 | Module *HeadModule, |
| 346 | const std::vector<std::string> &Files, |
| 347 | bool Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 348 | { |
| 349 | // String in which to receive error messages. |
| 350 | std::string ErrorMessage; |
| 351 | |
| 352 | // Full pathname of the file |
| 353 | std::string Pathname; |
| 354 | |
| 355 | // Get the library search path from the environment |
| 356 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 357 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 358 | for (unsigned i = 1; i < Files.size(); ++i) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 359 | // Determine where this file lives. |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 360 | if (FileExists(Files[i])) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 361 | Pathname = Files[i]; |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 362 | } else { |
| 363 | if (SearchPath == NULL) { |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 364 | std::cerr << progname << ": Cannot find linker input file '" |
| 365 | << Files[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | |
| 369 | Pathname = std::string(SearchPath)+"/"+Files[i]; |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 370 | if (!FileExists(Pathname)) { |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 371 | std::cerr << progname << ": Cannot find linker input file '" |
| 372 | << Files[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | } |
| 376 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 377 | // A user may specify an ar archive without -l, perhaps because it |
| 378 | // is not installed as a library. Detect that and link the library. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 379 | if (IsArchive(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 380 | if (Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 381 | std::cerr << "Linking archive '" << Files[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 382 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 383 | if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 384 | PrintAndReturn(progname, ErrorMessage, |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 385 | ": Error linking in '" + Files[i] + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 386 | return true; |
| 387 | } |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 388 | } else { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 389 | if (Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 390 | std::cerr << "Linking file '" << Files[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 391 | |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 392 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 393 | PrintAndReturn(progname, ErrorMessage, |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 394 | ": Error linking in '" + Files[i] + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return false; |
| 401 | } |
| 402 | |
Misha Brukman | 5208ba1 | 2003-09-30 18:09:32 +0000 | [diff] [blame] | 403 | /// LinkLibraries - takes the specified library files and links them into the |
| 404 | /// main bytecode object file. |
| 405 | /// |
| 406 | /// Inputs: |
| 407 | /// progname - The name of the program (infamous argv[0]). |
| 408 | /// HeadModule - The module into which all necessary libraries will be linked. |
| 409 | /// Libraries - The list of libraries to link into the module. |
| 410 | /// LibPaths - The list of library paths in which to find libraries. |
| 411 | /// Verbose - Flags whether verbose messages should be printed. |
| 412 | /// Native - Flags whether native code is being generated. |
| 413 | /// |
| 414 | /// Outputs: |
| 415 | /// HeadModule - The module will have all necessary libraries linked in. |
| 416 | /// |
| 417 | /// Return value: |
| 418 | /// FALSE - No error. |
| 419 | /// TRUE - Error. |
| 420 | /// |
Misha Brukman | e676313 | 2003-09-29 22:26:24 +0000 | [diff] [blame] | 421 | bool LinkLibraries(const char *progname, |
| 422 | Module *HeadModule, |
| 423 | const std::vector<std::string> &Libraries, |
| 424 | const std::vector<std::string> &LibPaths, |
| 425 | bool Verbose, |
| 426 | bool Native) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 427 | { |
| 428 | // String in which to receive error messages. |
| 429 | std::string ErrorMessage; |
| 430 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 431 | for (unsigned i = 1; i < Libraries.size(); ++i) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 432 | // Determine where this library lives. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 433 | std::string Pathname = FindLib(Libraries[i], LibPaths); |
| 434 | if (Pathname.empty()) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 435 | // If the pathname does not exist, then continue to the next one if |
| 436 | // we're doing a native link and give an error if we're doing a bytecode |
| 437 | // link. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 438 | if (!Native) { |
Brian Gaeke | 608e75c | 2003-10-08 19:09:30 +0000 | [diff] [blame] | 439 | PrintAndReturn(progname, "Cannot find " + Libraries[i] + "\n"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | } |
| 443 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 444 | // A user may specify an ar archive without -l, perhaps because it |
| 445 | // is not installed as a library. Detect that and link the library. |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 446 | if (IsArchive(Pathname)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 447 | if (Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 448 | std::cerr << "Linking archive '" << Libraries[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 449 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 450 | if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 451 | PrintAndReturn(progname, ErrorMessage, |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 452 | ": Error linking in '" + Libraries[i] + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 453 | return true; |
| 454 | } |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 455 | } else { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 456 | if (Verbose) |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 457 | std::cerr << "Linking file '" << Libraries[i] << "'\n"; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 458 | |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 459 | if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 460 | PrintAndReturn(progname, ErrorMessage, |
Misha Brukman | 17dc4ce | 2003-09-29 22:16:43 +0000 | [diff] [blame] | 461 | ": error linking in '" + Libraries[i] + "'"); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 462 | return true; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return false; |
| 468 | } |