Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1 | //===- Driver.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 10 | #include "Driver.h" |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 12 | #include "Error.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 14 | #include "Memory.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 16 | #include "Symbols.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 17 | #include "Writer.h" |
Rui Ueyama | a453c0a | 2016-03-02 19:08:05 +0000 | [diff] [blame] | 18 | #include "lld/Driver/Driver.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Optional.h" |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSwitch.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 21 | #include "llvm/BinaryFormat/Magic.h" |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 22 | #include "llvm/Object/ArchiveWriter.h" |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 23 | #include "llvm/Object/COFFImportFile.h" |
| 24 | #include "llvm/Object/COFFModuleDefinition.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 25 | #include "llvm/Option/Arg.h" |
| 26 | #include "llvm/Option/ArgList.h" |
| 27 | #include "llvm/Option/Option.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Process.h" |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TarWriter.h" |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TargetSelect.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | c6f07c4 | 2017-05-13 22:06:46 +0000 | [diff] [blame] | 34 | #include "llvm/ToolDrivers/llvm-lib/LibDriver.h" |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 36 | #include <memory> |
| 37 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 38 | #include <future> |
| 39 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 41 | using namespace llvm::object; |
Rui Ueyama | 84936e0 | 2015-07-07 23:39:18 +0000 | [diff] [blame] | 42 | using namespace llvm::COFF; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 43 | using llvm::sys::Process; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 44 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 45 | namespace lld { |
| 46 | namespace coff { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 47 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 48 | Configuration *Config; |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 49 | LinkerDriver *Driver; |
| 50 | |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 51 | BumpPtrAllocator BAlloc; |
| 52 | StringSaver Saver{BAlloc}; |
| 53 | std::vector<SpecificAllocBase *> SpecificAllocBase::Instances; |
| 54 | |
Bob Haarman | 6c8f736 | 2017-01-17 19:07:42 +0000 | [diff] [blame] | 55 | bool link(ArrayRef<const char *> Args, raw_ostream &Diag) { |
| 56 | ErrorCount = 0; |
| 57 | ErrorOS = &Diag; |
Rui Ueyama | 7fed58c | 2016-12-08 19:10:28 +0000 | [diff] [blame] | 58 | Config = make<Configuration>(); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 59 | Config->Argv = {Args.begin(), Args.end()}; |
Bob Haarman | 6c8f736 | 2017-01-17 19:07:42 +0000 | [diff] [blame] | 60 | Config->ColorDiagnostics = |
| 61 | (ErrorOS == &llvm::errs() && Process::StandardErrHasColors()); |
Rui Ueyama | 7fed58c | 2016-12-08 19:10:28 +0000 | [diff] [blame] | 62 | Driver = make<LinkerDriver>(); |
Rui Ueyama | 417553d | 2016-02-28 19:54:51 +0000 | [diff] [blame] | 63 | Driver->link(Args); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 64 | return !ErrorCount; |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 65 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 66 | |
Nico Weber | 5660de7 | 2016-04-20 22:34:15 +0000 | [diff] [blame] | 67 | // Drop directory components and replace extension with ".exe" or ".dll". |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 68 | static std::string getOutputPath(StringRef Path) { |
| 69 | auto P = Path.find_last_of("\\/"); |
| 70 | StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1); |
Nico Weber | 5660de7 | 2016-04-20 22:34:15 +0000 | [diff] [blame] | 71 | const char* E = Config->DLL ? ".dll" : ".exe"; |
| 72 | return (S.substr(0, S.rfind('.')) + E).str(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 75 | // ErrorOr is not default constructible, so it cannot be used as the type |
| 76 | // parameter of a future. |
| 77 | // FIXME: We could open the file in createFutureForFile and avoid needing to |
| 78 | // return an error here, but for the moment that would cost us a file descriptor |
| 79 | // (a limited resource on Windows) for the duration that the future is pending. |
| 80 | typedef std::pair<std::unique_ptr<MemoryBuffer>, std::error_code> MBErrPair; |
| 81 | |
| 82 | // Create a std::future that opens and maps a file using the best strategy for |
| 83 | // the host platform. |
| 84 | static std::future<MBErrPair> createFutureForFile(std::string Path) { |
| 85 | #if LLVM_ON_WIN32 |
| 86 | // On Windows, file I/O is relatively slow so it is best to do this |
| 87 | // asynchronously. |
| 88 | auto Strategy = std::launch::async; |
| 89 | #else |
| 90 | auto Strategy = std::launch::deferred; |
| 91 | #endif |
| 92 | return std::async(Strategy, [=]() { |
| 93 | auto MBOrErr = MemoryBuffer::getFile(Path); |
| 94 | if (!MBOrErr) |
| 95 | return MBErrPair{nullptr, MBOrErr.getError()}; |
| 96 | return MBErrPair{std::move(*MBOrErr), std::error_code()}; |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> MB) { |
| 101 | MemoryBufferRef MBRef = *MB; |
Rui Ueyama | 01f9333 | 2017-05-18 17:03:49 +0000 | [diff] [blame] | 102 | make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take ownership |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 103 | |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 104 | if (Driver->Tar) |
| 105 | Driver->Tar->append(relativeToRoot(MBRef.getBufferIdentifier()), |
| 106 | MBRef.getBuffer()); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 107 | return MBRef; |
| 108 | } |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 109 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 110 | void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> MB) { |
| 111 | MemoryBufferRef MBRef = takeBuffer(std::move(MB)); |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 112 | |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 113 | // File type is detected by contents, not by file extension. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 114 | file_magic Magic = identify_magic(MBRef.getBuffer()); |
| 115 | if (Magic == file_magic::windows_resource) { |
| 116 | Resources.push_back(MBRef); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | FilePaths.push_back(MBRef.getBufferIdentifier()); |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 121 | if (Magic == file_magic::archive) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 122 | return Symtab.addFile(make<ArchiveFile>(MBRef)); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 123 | if (Magic == file_magic::bitcode) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 124 | return Symtab.addFile(make<BitcodeFile>(MBRef)); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 125 | |
Rui Ueyama | f83806a | 2016-11-15 01:01:51 +0000 | [diff] [blame] | 126 | if (Magic == file_magic::coff_cl_gl_object) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 127 | error(MBRef.getBufferIdentifier() + ": is not a native COFF file. " |
Rui Ueyama | f83806a | 2016-11-15 01:01:51 +0000 | [diff] [blame] | 128 | "Recompile without /GL"); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 129 | else |
| 130 | Symtab.addFile(make<ObjectFile>(MBRef)); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void LinkerDriver::enqueuePath(StringRef Path) { |
| 134 | auto Future = |
| 135 | std::make_shared<std::future<MBErrPair>>(createFutureForFile(Path)); |
| 136 | std::string PathStr = Path; |
| 137 | enqueueTask([=]() { |
| 138 | auto MBOrErr = Future->get(); |
| 139 | if (MBOrErr.second) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 140 | error("could not open " + PathStr + ": " + MBOrErr.second.message()); |
| 141 | else |
| 142 | Driver->addBuffer(std::move(MBOrErr.first)); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 143 | }); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName, |
| 147 | StringRef ParentName) { |
| 148 | file_magic Magic = identify_magic(MB.getBuffer()); |
| 149 | if (Magic == file_magic::coff_import_library) { |
| 150 | Symtab.addFile(make<ImportFile>(MB)); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | InputFile *Obj; |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 155 | if (Magic == file_magic::coff_object) { |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 156 | Obj = make<ObjectFile>(MB); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 157 | } else if (Magic == file_magic::bitcode) { |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 158 | Obj = make<BitcodeFile>(MB); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 159 | } else { |
| 160 | error("unknown file type: " + MB.getBufferIdentifier()); |
| 161 | return; |
| 162 | } |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 163 | |
| 164 | Obj->ParentName = ParentName; |
| 165 | Symtab.addFile(Obj); |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 166 | log("Loaded " + toString(Obj) + " for " + SymName); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void LinkerDriver::enqueueArchiveMember(const Archive::Child &C, |
| 170 | StringRef SymName, |
| 171 | StringRef ParentName) { |
| 172 | if (!C.getParent()->isThin()) { |
| 173 | MemoryBufferRef MB = check( |
| 174 | C.getMemoryBufferRef(), |
| 175 | "could not get the buffer for the member defining symbol " + SymName); |
| 176 | enqueueTask([=]() { Driver->addArchiveBuffer(MB, SymName, ParentName); }); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | auto Future = std::make_shared<std::future<MBErrPair>>(createFutureForFile( |
| 181 | check(C.getFullName(), |
| 182 | "could not get the filename for the member defining symbol " + |
| 183 | SymName))); |
| 184 | enqueueTask([=]() { |
| 185 | auto MBOrErr = Future->get(); |
| 186 | if (MBOrErr.second) |
| 187 | fatal(MBOrErr.second, |
| 188 | "could not get the buffer for the member defining " + SymName); |
| 189 | Driver->addArchiveBuffer(takeBuffer(std::move(MBOrErr.first)), SymName, |
| 190 | ParentName); |
| 191 | }); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 194 | static bool isDecorated(StringRef Sym) { |
| 195 | return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?"); |
| 196 | } |
| 197 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 198 | // Parses .drectve section contents and returns a list of files |
| 199 | // specified by /defaultlib. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 200 | void LinkerDriver::parseDirectives(StringRef S) { |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 201 | opt::InputArgList Args = Parser.parse(S); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 202 | |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 203 | for (auto *Arg : Args) { |
Rui Ueyama | 38b0f4a | 2017-07-19 20:30:04 +0000 | [diff] [blame] | 204 | switch (Arg->getOption().getUnaliasedOption().getID()) { |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 205 | case OPT_alternatename: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 206 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 207 | break; |
| 208 | case OPT_defaultlib: |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 209 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
| 210 | enqueuePath(*Path); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 211 | break; |
| 212 | case OPT_export: { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 213 | Export E = parseExport(Arg->getValue()); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 214 | E.Directives = true; |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 215 | Config->Exports.push_back(E); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | case OPT_failifmismatch: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 219 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 220 | break; |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 221 | case OPT_incl: |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 222 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 223 | break; |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 224 | case OPT_merge: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 225 | parseMerge(Arg->getValue()); |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 226 | break; |
| 227 | case OPT_nodefaultlib: |
| 228 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 229 | break; |
Rui Ueyama | 440138c | 2016-06-20 03:39:39 +0000 | [diff] [blame] | 230 | case OPT_section: |
| 231 | parseSection(Arg->getValue()); |
| 232 | break; |
Rui Ueyama | 3c4737d | 2015-08-11 16:46:08 +0000 | [diff] [blame] | 233 | case OPT_editandcontinue: |
Reid Kleckner | 9cd77ce | 2016-03-25 18:09:29 +0000 | [diff] [blame] | 234 | case OPT_fastfail: |
Rui Ueyama | 31e66e3 | 2015-09-03 16:20:47 +0000 | [diff] [blame] | 235 | case OPT_guardsym: |
Rui Ueyama | 43238317 | 2015-07-29 21:01:15 +0000 | [diff] [blame] | 236 | case OPT_throwingnew: |
Rui Ueyama | 4668263 | 2015-07-29 20:29:15 +0000 | [diff] [blame] | 237 | break; |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 238 | default: |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 239 | error(Arg->getSpelling() + " is not allowed in .drectve"); |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 244 | // Find file from search paths. You can omit ".obj", this function takes |
| 245 | // care of that. Note that the returned path is not guaranteed to exist. |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 246 | StringRef LinkerDriver::doFindFile(StringRef Filename) { |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 247 | bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos); |
| 248 | if (HasPathSep) |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 249 | return Filename; |
Rui Ueyama | 12234f8 | 2017-07-19 21:40:26 +0000 | [diff] [blame^] | 250 | bool HasExt = Filename.contains('.'); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 251 | for (StringRef Dir : SearchPaths) { |
| 252 | SmallString<128> Path = Dir; |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 253 | sys::path::append(Path, Filename); |
| 254 | if (sys::fs::exists(Path.str())) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 255 | return Saver.save(Path.str()); |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 256 | if (!HasExt) { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 257 | Path.append(".obj"); |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 258 | if (sys::fs::exists(Path.str())) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 259 | return Saver.save(Path.str()); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | return Filename; |
| 263 | } |
| 264 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 265 | // Resolves a file path. This never returns the same path |
| 266 | // (in that case, it returns None). |
| 267 | Optional<StringRef> LinkerDriver::findFile(StringRef Filename) { |
| 268 | StringRef Path = doFindFile(Filename); |
| 269 | bool Seen = !VisitedFiles.insert(Path.lower()).second; |
| 270 | if (Seen) |
| 271 | return None; |
| 272 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 275 | // Find library file from search path. |
| 276 | StringRef LinkerDriver::doFindLib(StringRef Filename) { |
| 277 | // Add ".lib" to Filename if that has no file extension. |
Rui Ueyama | 12234f8 | 2017-07-19 21:40:26 +0000 | [diff] [blame^] | 278 | bool HasExt = Filename.contains('.'); |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 279 | if (!HasExt) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 280 | Filename = Saver.save(Filename + ".lib"); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 281 | return doFindFile(Filename); |
| 282 | } |
| 283 | |
| 284 | // Resolves a library path. /nodefaultlib options are taken into |
| 285 | // consideration. This never returns the same path (in that case, |
| 286 | // it returns None). |
| 287 | Optional<StringRef> LinkerDriver::findLib(StringRef Filename) { |
| 288 | if (Config->NoDefaultLibAll) |
| 289 | return None; |
Peter Collingbourne | c1ded7d | 2016-12-16 03:45:59 +0000 | [diff] [blame] | 290 | if (!VisitedLibs.insert(Filename.lower()).second) |
| 291 | return None; |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 292 | StringRef Path = doFindLib(Filename); |
| 293 | if (Config->NoDefaultLibs.count(Path)) |
| 294 | return None; |
Peter Collingbourne | c1ded7d | 2016-12-16 03:45:59 +0000 | [diff] [blame] | 295 | if (!VisitedFiles.insert(Path.lower()).second) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 296 | return None; |
| 297 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Parses LIB environment which contains a list of search paths. |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 301 | void LinkerDriver::addLibSearchPaths() { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 302 | Optional<std::string> EnvOpt = Process::GetEnv("LIB"); |
| 303 | if (!EnvOpt.hasValue()) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 304 | return; |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 305 | StringRef Env = Saver.save(*EnvOpt); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 306 | while (!Env.empty()) { |
| 307 | StringRef Path; |
| 308 | std::tie(Path, Env) = Env.split(';'); |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 309 | SearchPaths.push_back(Path); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 310 | } |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 +0000 | [diff] [blame] | 313 | SymbolBody *LinkerDriver::addUndefined(StringRef Name) { |
| 314 | SymbolBody *B = Symtab.addUndefined(Name); |
| 315 | Config->GCRoot.insert(B); |
| 316 | return B; |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 319 | // Symbol names are mangled by appending "_" prefix on x86. |
| 320 | StringRef LinkerDriver::mangle(StringRef Sym) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 321 | assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); |
| 322 | if (Config->Machine == I386) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 323 | return Saver.save("_" + Sym); |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 324 | return Sym; |
| 325 | } |
| 326 | |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 327 | // Windows specific -- find default entry point name. |
| 328 | StringRef LinkerDriver::findDefaultEntry() { |
| 329 | // User-defined main functions and their corresponding entry points. |
| 330 | static const char *Entries[][2] = { |
| 331 | {"main", "mainCRTStartup"}, |
| 332 | {"wmain", "wmainCRTStartup"}, |
| 333 | {"WinMain", "WinMainCRTStartup"}, |
| 334 | {"wWinMain", "wWinMainCRTStartup"}, |
| 335 | }; |
| 336 | for (auto E : Entries) { |
Rui Ueyama | a50387f | 2015-07-14 02:58:13 +0000 | [diff] [blame] | 337 | StringRef Entry = Symtab.findMangle(mangle(E[0])); |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 +0000 | [diff] [blame] | 338 | if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->body())) |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 339 | return mangle(E[1]); |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 340 | } |
| 341 | return ""; |
| 342 | } |
| 343 | |
| 344 | WindowsSubsystem LinkerDriver::inferSubsystem() { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 345 | if (Config->DLL) |
| 346 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
Rui Ueyama | 611add2 | 2015-08-08 00:23:37 +0000 | [diff] [blame] | 347 | if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 348 | return IMAGE_SUBSYSTEM_WINDOWS_CUI; |
Rui Ueyama | 611add2 | 2015-08-08 00:23:37 +0000 | [diff] [blame] | 349 | if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 350 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
| 351 | return IMAGE_SUBSYSTEM_UNKNOWN; |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 354 | static uint64_t getDefaultImageBase() { |
| 355 | if (Config->is64()) |
| 356 | return Config->DLL ? 0x180000000 : 0x140000000; |
| 357 | return Config->DLL ? 0x10000000 : 0x400000; |
| 358 | } |
| 359 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 360 | static std::string createResponseFile(const opt::InputArgList &Args, |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 361 | ArrayRef<StringRef> FilePaths, |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 362 | ArrayRef<StringRef> SearchPaths) { |
| 363 | SmallString<0> Data; |
| 364 | raw_svector_ostream OS(Data); |
| 365 | |
| 366 | for (auto *Arg : Args) { |
| 367 | switch (Arg->getOption().getID()) { |
| 368 | case OPT_linkrepro: |
| 369 | case OPT_INPUT: |
| 370 | case OPT_defaultlib: |
| 371 | case OPT_libpath: |
| 372 | break; |
| 373 | default: |
Rui Ueyama | b4c63ca | 2017-01-06 10:04:35 +0000 | [diff] [blame] | 374 | OS << toString(Arg) << "\n"; |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | for (StringRef Path : SearchPaths) { |
| 379 | std::string RelPath = relativeToRoot(Path); |
| 380 | OS << "/libpath:" << quote(RelPath) << "\n"; |
| 381 | } |
| 382 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 383 | for (StringRef Path : FilePaths) |
| 384 | OS << quote(relativeToRoot(Path)) << "\n"; |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 385 | |
| 386 | return Data.str(); |
| 387 | } |
| 388 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 389 | static unsigned getDefaultDebugType(const opt::InputArgList &Args) { |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 390 | unsigned DebugTypes = static_cast<unsigned>(DebugType::CV); |
| 391 | if (Args.hasArg(OPT_driver)) |
| 392 | DebugTypes |= static_cast<unsigned>(DebugType::PData); |
| 393 | if (Args.hasArg(OPT_profile)) |
| 394 | DebugTypes |= static_cast<unsigned>(DebugType::Fixup); |
| 395 | return DebugTypes; |
| 396 | } |
| 397 | |
| 398 | static unsigned parseDebugType(StringRef Arg) { |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 399 | SmallVector<StringRef, 3> Types; |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 400 | Arg.split(Types, ',', /*KeepEmpty=*/false); |
| 401 | |
| 402 | unsigned DebugTypes = static_cast<unsigned>(DebugType::None); |
| 403 | for (StringRef Type : Types) |
| 404 | DebugTypes |= StringSwitch<unsigned>(Type.lower()) |
| 405 | .Case("cv", static_cast<unsigned>(DebugType::CV)) |
| 406 | .Case("pdata", static_cast<unsigned>(DebugType::PData)) |
Saleem Abdulrasool | b639428 | 2017-02-07 04:28:05 +0000 | [diff] [blame] | 407 | .Case("fixup", static_cast<unsigned>(DebugType::Fixup)) |
| 408 | .Default(0); |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 409 | return DebugTypes; |
| 410 | } |
| 411 | |
Hans Wennborg | 1818e65 | 2016-12-09 20:54:44 +0000 | [diff] [blame] | 412 | static std::string getMapFile(const opt::InputArgList &Args) { |
| 413 | auto *Arg = Args.getLastArg(OPT_lldmap, OPT_lldmap_file); |
| 414 | if (!Arg) |
| 415 | return ""; |
| 416 | if (Arg->getOption().getID() == OPT_lldmap_file) |
| 417 | return Arg->getValue(); |
| 418 | |
| 419 | assert(Arg->getOption().getID() == OPT_lldmap); |
| 420 | StringRef OutFile = Config->OutputFile; |
| 421 | return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str(); |
| 422 | } |
| 423 | |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 424 | static std::string getImplibPath() { |
| 425 | if (!Config->Implib.empty()) |
| 426 | return Config->Implib; |
| 427 | SmallString<128> Out = StringRef(Config->OutputFile); |
| 428 | sys::path::replace_extension(Out, ".lib"); |
| 429 | return Out.str(); |
| 430 | } |
| 431 | |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 432 | // |
| 433 | // The import name is caculated as the following: |
| 434 | // |
| 435 | // | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY |
| 436 | // -----+----------------+---------------------+------------------ |
| 437 | // LINK | {value} | {value}.{.dll/.exe} | {output name} |
| 438 | // LIB | {value} | {value}.dll | {output name}.dll |
| 439 | // |
| 440 | static std::string getImportName(bool AsLib) { |
| 441 | SmallString<128> Out; |
| 442 | |
| 443 | if (Config->ImportName.empty()) { |
| 444 | Out.assign(sys::path::filename(Config->OutputFile)); |
| 445 | if (AsLib) |
| 446 | sys::path::replace_extension(Out, ".dll"); |
| 447 | } else { |
| 448 | Out.assign(Config->ImportName); |
| 449 | if (!sys::path::has_extension(Out)) |
| 450 | sys::path::replace_extension(Out, |
| 451 | (Config->DLL || AsLib) ? ".dll" : ".exe"); |
| 452 | } |
| 453 | |
| 454 | return Out.str(); |
| 455 | } |
| 456 | |
| 457 | static void createImportLibrary(bool AsLib) { |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 458 | std::vector<COFFShortExport> Exports; |
| 459 | for (Export &E1 : Config->Exports) { |
| 460 | COFFShortExport E2; |
| 461 | // Use SymbolName, which will have any stdcall or fastcall qualifiers. |
| 462 | E2.Name = E1.SymbolName; |
| 463 | E2.ExtName = E1.ExtName; |
| 464 | E2.Ordinal = E1.Ordinal; |
| 465 | E2.Noname = E1.Noname; |
| 466 | E2.Data = E1.Data; |
| 467 | E2.Private = E1.Private; |
| 468 | E2.Constant = E1.Constant; |
| 469 | Exports.push_back(E2); |
| 470 | } |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 471 | |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 472 | writeImportLibrary(getImportName(AsLib), getImplibPath(), Exports, |
| 473 | Config->Machine); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void parseModuleDefs(StringRef Path) { |
| 477 | std::unique_ptr<MemoryBuffer> MB = check( |
| 478 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
Rui Ueyama | 67aea737 | 2017-06-08 23:43:44 +0000 | [diff] [blame] | 479 | COFFModuleDefinition M = |
| 480 | check(parseCOFFModuleDefinition(MB->getMemBufferRef(), Config->Machine)); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 481 | |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 482 | if (Config->OutputFile.empty()) |
| 483 | Config->OutputFile = Saver.save(M.OutputFile); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 484 | Config->ImportName = Saver.save(M.ImportName); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 485 | if (M.ImageBase) |
| 486 | Config->ImageBase = M.ImageBase; |
| 487 | if (M.StackReserve) |
| 488 | Config->StackReserve = M.StackReserve; |
| 489 | if (M.StackCommit) |
| 490 | Config->StackCommit = M.StackCommit; |
| 491 | if (M.HeapReserve) |
| 492 | Config->HeapReserve = M.HeapReserve; |
| 493 | if (M.HeapCommit) |
| 494 | Config->HeapCommit = M.HeapCommit; |
| 495 | if (M.MajorImageVersion) |
| 496 | Config->MajorImageVersion = M.MajorImageVersion; |
| 497 | if (M.MinorImageVersion) |
| 498 | Config->MinorImageVersion = M.MinorImageVersion; |
| 499 | if (M.MajorOSVersion) |
| 500 | Config->MajorOSVersion = M.MajorOSVersion; |
| 501 | if (M.MinorOSVersion) |
| 502 | Config->MinorOSVersion = M.MinorOSVersion; |
| 503 | |
| 504 | for (COFFShortExport E1 : M.Exports) { |
| 505 | Export E2; |
| 506 | E2.Name = Saver.save(E1.Name); |
| 507 | if (E1.isWeak()) |
| 508 | E2.ExtName = Saver.save(E1.ExtName); |
| 509 | E2.Ordinal = E1.Ordinal; |
| 510 | E2.Noname = E1.Noname; |
| 511 | E2.Data = E1.Data; |
| 512 | E2.Private = E1.Private; |
| 513 | E2.Constant = E1.Constant; |
| 514 | Config->Exports.push_back(E2); |
| 515 | } |
| 516 | } |
| 517 | |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 518 | std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) { |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 519 | std::vector<MemoryBufferRef> V; |
| 520 | Error Err = Error::success(); |
| 521 | for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { |
| 522 | Archive::Child C = |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 523 | check(COrErr, |
| 524 | File->getFileName() + ": could not get the child of the archive"); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 525 | MemoryBufferRef MBRef = |
| 526 | check(C.getMemoryBufferRef(), |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 527 | File->getFileName() + |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 528 | ": could not get the buffer for a child of the archive"); |
| 529 | V.push_back(MBRef); |
| 530 | } |
| 531 | if (Err) |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 532 | fatal(File->getFileName() + |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 533 | ": Archive::children failed: " + toString(std::move(Err))); |
| 534 | return V; |
| 535 | } |
| 536 | |
| 537 | // A helper function for filterBitcodeFiles. |
| 538 | static bool needsRebuilding(MemoryBufferRef MB) { |
| 539 | // The MSVC linker doesn't support thin archives, so if it's a thin |
| 540 | // archive, we always need to rebuild it. |
| 541 | std::unique_ptr<Archive> File = |
| 542 | check(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier()); |
| 543 | if (File->isThin()) |
| 544 | return true; |
| 545 | |
| 546 | // Returns true if the archive contains at least one bitcode file. |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 547 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 548 | if (identify_magic(Member.getBuffer()) == file_magic::bitcode) |
| 549 | return true; |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | // Opens a given path as an archive file and removes bitcode files |
| 554 | // from them if exists. This function is to appease the MSVC linker as |
| 555 | // their linker doesn't like archive files containing non-native |
| 556 | // object files. |
| 557 | // |
| 558 | // If a given archive doesn't contain bitcode files, the archive path |
| 559 | // is returned as-is. Otherwise, a new temporary file is created and |
| 560 | // its path is returned. |
| 561 | static Optional<std::string> |
| 562 | filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) { |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 563 | std::unique_ptr<MemoryBuffer> MB = check( |
| 564 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 565 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 566 | file_magic Magic = identify_magic(MBRef.getBuffer()); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 567 | |
| 568 | if (Magic == file_magic::bitcode) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 569 | return None; |
| 570 | if (Magic != file_magic::archive) |
| 571 | return Path.str(); |
| 572 | if (!needsRebuilding(MBRef)) |
| 573 | return Path.str(); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 574 | |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 575 | std::unique_ptr<Archive> File = |
| 576 | check(Archive::create(MBRef), |
| 577 | MBRef.getBufferIdentifier() + ": failed to parse archive"); |
| 578 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 579 | std::vector<NewArchiveMember> New; |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 580 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 581 | if (identify_magic(Member.getBuffer()) != file_magic::bitcode) |
| 582 | New.emplace_back(Member); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 583 | |
Peter Collingbourne | db7447d | 2017-03-17 02:04:22 +0000 | [diff] [blame] | 584 | if (New.empty()) |
| 585 | return None; |
| 586 | |
| 587 | log("Creating a temporary archive for " + Path + " to remove bitcode files"); |
| 588 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 589 | SmallString<128> S; |
| 590 | if (auto EC = sys::fs::createTemporaryFile("lld-" + sys::path::stem(Path), |
| 591 | ".lib", S)) |
| 592 | fatal(EC, "cannot create a temporary file"); |
| 593 | std::string Temp = S.str(); |
| 594 | TemporaryFiles.push_back(Temp); |
| 595 | |
| 596 | std::pair<StringRef, std::error_code> Ret = |
| 597 | llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU, |
| 598 | /*Deterministics=*/true, |
| 599 | /*Thin=*/false); |
| 600 | if (Ret.second) |
| 601 | error("failed to create a new archive " + S.str() + ": " + Ret.first); |
| 602 | return Temp; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Create response file contents and invoke the MSVC linker. |
| 606 | void LinkerDriver::invokeMSVC(opt::InputArgList &Args) { |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 607 | std::string Rsp = "/nologo\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 608 | std::vector<std::string> Temps; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 609 | |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 610 | // Write out archive members that we used in symbol resolution and pass these |
| 611 | // to MSVC before any archives, so that MSVC uses the same objects to satisfy |
| 612 | // references. |
| 613 | for (const auto *O : Symtab.ObjectFiles) { |
| 614 | if (O->ParentName.empty()) |
| 615 | continue; |
| 616 | SmallString<128> S; |
| 617 | int Fd; |
| 618 | if (auto EC = sys::fs::createTemporaryFile( |
| 619 | "lld-" + sys::path::filename(O->ParentName), ".obj", Fd, S)) |
| 620 | fatal(EC, "cannot create a temporary file"); |
| 621 | raw_fd_ostream OS(Fd, /*shouldClose*/ true); |
| 622 | OS << O->MB.getBuffer(); |
| 623 | Temps.push_back(S.str()); |
| 624 | Rsp += quote(S) + "\n"; |
| 625 | } |
| 626 | |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 627 | for (auto *Arg : Args) { |
| 628 | switch (Arg->getOption().getID()) { |
| 629 | case OPT_linkrepro: |
| 630 | case OPT_lldmap: |
| 631 | case OPT_lldmap_file: |
Peter Collingbourne | 8713bf6 | 2017-03-17 02:11:09 +0000 | [diff] [blame] | 632 | case OPT_lldsavetemps: |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 633 | case OPT_msvclto: |
| 634 | // LLD-specific options are stripped. |
| 635 | break; |
| 636 | case OPT_opt: |
| 637 | if (!StringRef(Arg->getValue()).startswith("lld")) |
| 638 | Rsp += toString(Arg) + " "; |
| 639 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 640 | case OPT_INPUT: { |
| 641 | if (Optional<StringRef> Path = doFindFile(Arg->getValue())) { |
| 642 | if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps)) |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 643 | Rsp += quote(*S) + "\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 644 | continue; |
| 645 | } |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 646 | Rsp += quote(Arg->getValue()) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 647 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 648 | } |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 649 | default: |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 650 | Rsp += toString(Arg) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | std::vector<StringRef> ObjectFiles = Symtab.compileBitcodeFiles(); |
| 655 | runMSVCLinker(Rsp, ObjectFiles); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 656 | |
| 657 | for (StringRef Path : Temps) |
| 658 | sys::fs::remove(Path); |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 661 | void LinkerDriver::enqueueTask(std::function<void()> Task) { |
| 662 | TaskQueue.push_back(std::move(Task)); |
| 663 | } |
| 664 | |
| 665 | bool LinkerDriver::run() { |
| 666 | bool DidWork = !TaskQueue.empty(); |
| 667 | while (!TaskQueue.empty()) { |
| 668 | TaskQueue.front()(); |
| 669 | TaskQueue.pop_front(); |
| 670 | } |
| 671 | return DidWork; |
| 672 | } |
| 673 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 674 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 675 | // If the first command line argument is "/lib", link.exe acts like lib.exe. |
| 676 | // We call our own implementation of lib.exe that understands bitcode files. |
| 677 | if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) { |
| 678 | if (llvm::libDriverMain(ArgsArr.slice(1)) != 0) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 679 | fatal("lib failed"); |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 683 | // Needed for LTO. |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 684 | InitializeAllTargetInfos(); |
| 685 | InitializeAllTargets(); |
| 686 | InitializeAllTargetMCs(); |
| 687 | InitializeAllAsmParsers(); |
| 688 | InitializeAllAsmPrinters(); |
| 689 | InitializeAllDisassemblers(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 690 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 691 | // Parse command line options. |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 692 | opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 693 | |
Rui Ueyama | 9a3e733 | 2017-03-30 20:10:40 +0000 | [diff] [blame] | 694 | // Parse and evaluate -mllvm options. |
| 695 | std::vector<const char *> V; |
| 696 | V.push_back("lld-link (LLVM option parsing)"); |
| 697 | for (auto *Arg : Args.filtered(OPT_mllvm)) |
| 698 | V.push_back(Arg->getValue()); |
| 699 | cl::ParseCommandLineOptions(V.size(), V.data()); |
| 700 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 701 | // Handle /errorlimit early, because error() depends on it. |
| 702 | if (auto *Arg = Args.getLastArg(OPT_errorlimit)) { |
| 703 | int N = 20; |
| 704 | StringRef S = Arg->getValue(); |
| 705 | if (S.getAsInteger(10, N)) |
| 706 | error(Arg->getSpelling() + " number expected, but got " + S); |
| 707 | Config->ErrorLimit = N; |
| 708 | } |
| 709 | |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 710 | // Handle /help |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 711 | if (Args.hasArg(OPT_help)) { |
David Blaikie | b2b1c7c | 2015-06-21 06:32:10 +0000 | [diff] [blame] | 712 | printHelp(ArgsArr[0]); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 713 | return; |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 716 | if (auto *Arg = Args.getLastArg(OPT_linkrepro)) { |
| 717 | SmallString<64> Path = StringRef(Arg->getValue()); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 718 | sys::path::append(Path, "repro.tar"); |
| 719 | |
| 720 | Expected<std::unique_ptr<TarWriter>> ErrOrWriter = |
| 721 | TarWriter::create(Path, "repro"); |
| 722 | |
| 723 | if (ErrOrWriter) { |
| 724 | Tar = std::move(*ErrOrWriter); |
| 725 | } else { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 726 | error("/linkrepro: failed to open " + Path + ": " + |
| 727 | toString(ErrOrWriter.takeError())); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 728 | } |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 731 | if (!Args.hasArgNoClaim(OPT_INPUT)) { |
| 732 | if (Args.hasArgNoClaim(OPT_deffile)) |
| 733 | Config->NoEntry = true; |
| 734 | else |
| 735 | fatal("no input files"); |
| 736 | } |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 737 | |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 738 | // Construct search path list. |
| 739 | SearchPaths.push_back(""); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 740 | for (auto *Arg : Args.filtered(OPT_libpath)) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 741 | SearchPaths.push_back(Arg->getValue()); |
| 742 | addLibSearchPaths(); |
| 743 | |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 744 | // Handle /out |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 745 | if (auto *Arg = Args.getLastArg(OPT_out)) |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 746 | Config->OutputFile = Arg->getValue(); |
| 747 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 748 | // Handle /verbose |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 749 | if (Args.hasArg(OPT_verbose)) |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 750 | Config->Verbose = true; |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 751 | |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 752 | // Handle /force or /force:unresolved |
| 753 | if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved)) |
| 754 | Config->Force = true; |
| 755 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 756 | // Handle /debug |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 757 | if (Args.hasArg(OPT_debug)) { |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 758 | Config->Debug = true; |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 759 | Config->DebugTypes = |
| 760 | Args.hasArg(OPT_debugtype) |
| 761 | ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue()) |
| 762 | : getDefaultDebugType(Args); |
| 763 | } |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 764 | |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 765 | // Create a dummy PDB file to satisfy build sytem rules. |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 766 | if (auto *Arg = Args.getLastArg(OPT_pdb)) |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 767 | Config->PDBPath = Arg->getValue(); |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 768 | |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 769 | // Handle /noentry |
| 770 | if (Args.hasArg(OPT_noentry)) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 771 | if (Args.hasArg(OPT_dll)) |
| 772 | Config->NoEntry = true; |
| 773 | else |
| 774 | error("/noentry must be specified with /dll"); |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 777 | // Handle /dll |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 778 | if (Args.hasArg(OPT_dll)) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 779 | Config->DLL = true; |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 780 | Config->ManifestID = 2; |
| 781 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 782 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 783 | // Handle /fixed |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 784 | if (Args.hasArg(OPT_fixed)) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 785 | if (Args.hasArg(OPT_dynamicbase)) { |
| 786 | error("/fixed must not be specified with /dynamicbase"); |
| 787 | } else { |
| 788 | Config->Relocatable = false; |
| 789 | Config->DynamicBase = false; |
| 790 | } |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 791 | } |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 792 | |
Saleem Abdulrasool | 671029d | 2017-04-06 23:07:53 +0000 | [diff] [blame] | 793 | if (Args.hasArg(OPT_appcontainer)) |
| 794 | Config->AppContainer = true; |
| 795 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 796 | // Handle /machine |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 797 | if (auto *Arg = Args.getLastArg(OPT_machine)) |
| 798 | Config->Machine = getMachineType(Arg->getValue()); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 799 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 800 | // Handle /nodefaultlib:<filename> |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 801 | for (auto *Arg : Args.filtered(OPT_nodefaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 802 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 803 | |
| 804 | // Handle /nodefaultlib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 805 | if (Args.hasArg(OPT_nodefaultlib_all)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 806 | Config->NoDefaultLibAll = true; |
| 807 | |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 808 | // Handle /base |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 809 | if (auto *Arg = Args.getLastArg(OPT_base)) |
| 810 | parseNumbers(Arg->getValue(), &Config->ImageBase); |
Rui Ueyama | b41b7e5 | 2015-05-29 16:21:11 +0000 | [diff] [blame] | 811 | |
| 812 | // Handle /stack |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 813 | if (auto *Arg = Args.getLastArg(OPT_stack)) |
| 814 | parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit); |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 815 | |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 816 | // Handle /heap |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 817 | if (auto *Arg = Args.getLastArg(OPT_heap)) |
| 818 | parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit); |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 819 | |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 820 | // Handle /version |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 821 | if (auto *Arg = Args.getLastArg(OPT_version)) |
| 822 | parseVersion(Arg->getValue(), &Config->MajorImageVersion, |
| 823 | &Config->MinorImageVersion); |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 824 | |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 825 | // Handle /subsystem |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 826 | if (auto *Arg = Args.getLastArg(OPT_subsystem)) |
| 827 | parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion, |
| 828 | &Config->MinorOSVersion); |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 829 | |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 830 | // Handle /alternatename |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 831 | for (auto *Arg : Args.filtered(OPT_alternatename)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 832 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 833 | |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 834 | // Handle /include |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 835 | for (auto *Arg : Args.filtered(OPT_incl)) |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 836 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 837 | |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 838 | // Handle /implib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 839 | if (auto *Arg = Args.getLastArg(OPT_implib)) |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 840 | Config->Implib = Arg->getValue(); |
| 841 | |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 842 | // Handle /opt |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 843 | for (auto *Arg : Args.filtered(OPT_opt)) { |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 844 | std::string Str = StringRef(Arg->getValue()).lower(); |
| 845 | SmallVector<StringRef, 1> Vec; |
| 846 | StringRef(Str).split(Vec, ','); |
| 847 | for (StringRef S : Vec) { |
| 848 | if (S == "noref") { |
| 849 | Config->DoGC = false; |
| 850 | Config->DoICF = false; |
| 851 | continue; |
| 852 | } |
| 853 | if (S == "icf" || StringRef(S).startswith("icf=")) { |
| 854 | Config->DoICF = true; |
| 855 | continue; |
| 856 | } |
| 857 | if (S == "noicf") { |
| 858 | Config->DoICF = false; |
| 859 | continue; |
| 860 | } |
| 861 | if (StringRef(S).startswith("lldlto=")) { |
| 862 | StringRef OptLevel = StringRef(S).substr(7); |
| 863 | if (OptLevel.getAsInteger(10, Config->LTOOptLevel) || |
| 864 | Config->LTOOptLevel > 3) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 865 | error("/opt:lldlto: invalid optimization level: " + OptLevel); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 866 | continue; |
| 867 | } |
| 868 | if (StringRef(S).startswith("lldltojobs=")) { |
| 869 | StringRef Jobs = StringRef(S).substr(11); |
| 870 | if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 871 | error("/opt:lldltojobs: invalid job count: " + Jobs); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 872 | continue; |
| 873 | } |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 +0000 | [diff] [blame] | 874 | if (StringRef(S).startswith("lldltopartitions=")) { |
| 875 | StringRef N = StringRef(S).substr(17); |
| 876 | if (N.getAsInteger(10, Config->LTOPartitions) || |
| 877 | Config->LTOPartitions == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 878 | error("/opt:lldltopartitions: invalid partition count: " + N); |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 +0000 | [diff] [blame] | 879 | continue; |
| 880 | } |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 881 | if (S != "ref" && S != "lbr" && S != "nolbr") |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 882 | error("/opt: unknown option: " + S); |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 883 | } |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Bob Haarman | 69b196d | 2017-02-08 18:36:41 +0000 | [diff] [blame] | 886 | // Handle /lldsavetemps |
| 887 | if (Args.hasArg(OPT_lldsavetemps)) |
| 888 | Config->SaveTemps = true; |
| 889 | |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 890 | // Handle /failifmismatch |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 891 | for (auto *Arg : Args.filtered(OPT_failifmismatch)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 892 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 893 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 894 | // Handle /merge |
| 895 | for (auto *Arg : Args.filtered(OPT_merge)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 896 | parseMerge(Arg->getValue()); |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 897 | |
Rui Ueyama | 440138c | 2016-06-20 03:39:39 +0000 | [diff] [blame] | 898 | // Handle /section |
| 899 | for (auto *Arg : Args.filtered(OPT_section)) |
| 900 | parseSection(Arg->getValue()); |
| 901 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 902 | // Handle /manifest |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 903 | if (auto *Arg = Args.getLastArg(OPT_manifest_colon)) |
| 904 | parseManifest(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 905 | |
| 906 | // Handle /manifestuac |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 907 | if (auto *Arg = Args.getLastArg(OPT_manifestuac)) |
| 908 | parseManifestUAC(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 909 | |
| 910 | // Handle /manifestdependency |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 911 | if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 912 | Config->ManifestDependency = Arg->getValue(); |
| 913 | |
| 914 | // Handle /manifestfile |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 915 | if (auto *Arg = Args.getLastArg(OPT_manifestfile)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 916 | Config->ManifestFile = Arg->getValue(); |
| 917 | |
Rui Ueyama | afb1901 | 2016-04-19 01:21:58 +0000 | [diff] [blame] | 918 | // Handle /manifestinput |
| 919 | for (auto *Arg : Args.filtered(OPT_manifestinput)) |
| 920 | Config->ManifestInput.push_back(Arg->getValue()); |
| 921 | |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 922 | // Handle miscellaneous boolean flags. |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 923 | if (Args.hasArg(OPT_allowisolation_no)) |
| 924 | Config->AllowIsolation = false; |
| 925 | if (Args.hasArg(OPT_dynamicbase_no)) |
| 926 | Config->DynamicBase = false; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 927 | if (Args.hasArg(OPT_nxcompat_no)) |
| 928 | Config->NxCompat = false; |
| 929 | if (Args.hasArg(OPT_tsaware_no)) |
| 930 | Config->TerminalServerAware = false; |
Rui Ueyama | 9640173 | 2015-09-21 23:43:31 +0000 | [diff] [blame] | 931 | if (Args.hasArg(OPT_nosymtab)) |
| 932 | Config->WriteSymtab = false; |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 933 | |
Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 934 | Config->MapFile = getMapFile(Args); |
| 935 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 936 | if (ErrorCount) |
| 937 | return; |
| 938 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 939 | // Create a list of input files. Files can be given as arguments |
| 940 | // for /defaultlib option. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 941 | std::vector<MemoryBufferRef> MBs; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 942 | for (auto *Arg : Args.filtered(OPT_INPUT)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 943 | if (Optional<StringRef> Path = findFile(Arg->getValue())) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 944 | enqueuePath(*Path); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 945 | for (auto *Arg : Args.filtered(OPT_defaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 946 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 947 | enqueuePath(*Path); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 948 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 949 | // Windows specific -- Create a resource file containing a manifest file. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 950 | if (Config->Manifest == Configuration::Embed) |
| 951 | addBuffer(createManifestRes()); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 952 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 953 | // Read all input files given via the command line. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 954 | run(); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 955 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 956 | // We should have inferred a machine type by now from the input files, but if |
| 957 | // not we assume x64. |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 958 | if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 959 | warn("/machine is not specified. x64 is assumed"); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 960 | Config->Machine = AMD64; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Eric Beckmann | 9e19d79 | 2017-06-17 02:26:27 +0000 | [diff] [blame] | 963 | // Input files can be Windows resource files (.res files). We use |
| 964 | // WindowsResource to convert resource files to a regular COFF file, |
| 965 | // then link the resulting file normally. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 966 | if (!Resources.empty()) |
| 967 | addBuffer(convertResToCOFF(Resources)); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 968 | |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 969 | if (Tar) |
| 970 | Tar->append("response.txt", |
| 971 | createResponseFile(Args, FilePaths, |
| 972 | ArrayRef<StringRef>(SearchPaths).slice(1))); |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 973 | |
Rui Ueyama | 4d54534 | 2015-07-28 03:12:00 +0000 | [diff] [blame] | 974 | // Handle /largeaddressaware |
| 975 | if (Config->is64() || Args.hasArg(OPT_largeaddressaware)) |
| 976 | Config->LargeAddressAware = true; |
| 977 | |
Rui Ueyama | d68e211 | 2015-07-28 03:15:57 +0000 | [diff] [blame] | 978 | // Handle /highentropyva |
| 979 | if (Config->is64() && !Args.hasArg(OPT_highentropyva_no)) |
| 980 | Config->HighEntropyVA = true; |
| 981 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 982 | // Handle /entry and /dll |
| 983 | if (auto *Arg = Args.getLastArg(OPT_entry)) { |
| 984 | Config->Entry = addUndefined(mangle(Arg->getValue())); |
| 985 | } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 986 | StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12" |
| 987 | : "_DllMainCRTStartup"; |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 988 | Config->Entry = addUndefined(S); |
| 989 | } else if (!Config->NoEntry) { |
| 990 | // Windows specific -- If entry point name is not given, we need to |
| 991 | // infer that from user-defined entry name. |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 992 | StringRef S = findDefaultEntry(); |
David Blaikie | 4cdfe69 | 2017-02-19 02:25:47 +0000 | [diff] [blame] | 993 | if (S.empty()) |
| 994 | fatal("entry point must be defined"); |
| 995 | Config->Entry = addUndefined(S); |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 996 | log("Entry name inferred: " + S); |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 999 | // Handle /export |
| 1000 | for (auto *Arg : Args.filtered(OPT_export)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1001 | Export E = parseExport(Arg->getValue()); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1002 | if (Config->Machine == I386) { |
| 1003 | if (!isDecorated(E.Name)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 1004 | E.Name = Saver.save("_" + E.Name); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1005 | if (!E.ExtName.empty() && !isDecorated(E.ExtName)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 1006 | E.ExtName = Saver.save("_" + E.ExtName); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1007 | } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1008 | Config->Exports.push_back(E); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | // Handle /def |
| 1012 | if (auto *Arg = Args.getLastArg(OPT_deffile)) { |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1013 | // parseModuleDefs mutates Config object. |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 1014 | parseModuleDefs(Arg->getValue()); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 1017 | // Handle generation of import library from a def file. |
| 1018 | if (!Args.hasArgNoClaim(OPT_INPUT)) { |
| 1019 | fixupExports(); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 1020 | createImportLibrary(/*AsLib=*/true); |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 1021 | exit(0); |
| 1022 | } |
| 1023 | |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1024 | // Handle /delayload |
| 1025 | for (auto *Arg : Args.filtered(OPT_delayload)) { |
| 1026 | Config->DelayLoads.insert(StringRef(Arg->getValue()).lower()); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1027 | if (Config->Machine == I386) { |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1028 | Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8"); |
Rui Ueyama | 35ccb0f | 2015-07-25 00:20:06 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | Config->DelayLoadHelper = addUndefined("__delayLoadHelper2"); |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1034 | // Set default image name if neither /out or /def set it. |
| 1035 | if (Config->OutputFile.empty()) { |
| 1036 | Config->OutputFile = |
Richard Smith | a13714e | 2017-04-12 23:51:20 +0000 | [diff] [blame] | 1037 | getOutputPath((*Args.filtered(OPT_INPUT).begin())->getValue()); |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Reid Kleckner | 13bdbfb | 2017-03-22 00:57:14 +0000 | [diff] [blame] | 1040 | // Put the PDB next to the image if no /pdb flag was passed. |
| 1041 | if (Config->Debug && Config->PDBPath.empty()) { |
| 1042 | Config->PDBPath = Config->OutputFile; |
| 1043 | sys::path::replace_extension(Config->PDBPath, ".pdb"); |
| 1044 | } |
| 1045 | |
Reid Kleckner | 77d3aa4 | 2017-03-22 19:49:12 +0000 | [diff] [blame] | 1046 | // Disable PDB generation if the user requested it. |
| 1047 | if (Args.hasArg(OPT_nopdb)) |
| 1048 | Config->PDBPath = ""; |
| 1049 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 1050 | // Set default image base if /base is not given. |
| 1051 | if (Config->ImageBase == uint64_t(-1)) |
| 1052 | Config->ImageBase = getDefaultImageBase(); |
| 1053 | |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 +0000 | [diff] [blame] | 1054 | Symtab.addSynthetic(mangle("__ImageBase"), nullptr); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1055 | if (Config->Machine == I386) { |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 +0000 | [diff] [blame] | 1056 | Symtab.addAbsolute("___safe_se_handler_table", 0); |
| 1057 | Symtab.addAbsolute("___safe_se_handler_count", 0); |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 1058 | } |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1059 | |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1060 | // We do not support /guard:cf (control flow protection) yet. |
| 1061 | // Define CFG symbols anyway so that we can link MSVC 2015 CRT. |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1062 | Symtab.addAbsolute(mangle("__guard_fids_count"), 0); |
Rui Ueyama | 2f740f7 | 2017-06-21 02:26:19 +0000 | [diff] [blame] | 1063 | Symtab.addAbsolute(mangle("__guard_fids_table"), 0); |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1064 | Symtab.addAbsolute(mangle("__guard_flags"), 0x100); |
Rui Ueyama | 2f740f7 | 2017-06-21 02:26:19 +0000 | [diff] [blame] | 1065 | Symtab.addAbsolute(mangle("__guard_iat_count"), 0); |
| 1066 | Symtab.addAbsolute(mangle("__guard_iat_table"), 0); |
| 1067 | Symtab.addAbsolute(mangle("__guard_longjmp_count"), 0); |
| 1068 | Symtab.addAbsolute(mangle("__guard_longjmp_table"), 0); |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1069 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1070 | // This code may add new undefined symbols to the link, which may enqueue more |
| 1071 | // symbol resolution tasks, so we need to continue executing tasks until we |
| 1072 | // converge. |
| 1073 | do { |
| 1074 | // Windows specific -- if entry point is not found, |
| 1075 | // search for its mangled names. |
| 1076 | if (Config->Entry) |
| 1077 | Symtab.mangleMaybe(Config->Entry); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 1078 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1079 | // Windows specific -- Make sure we resolve all dllexported symbols. |
| 1080 | for (Export &E : Config->Exports) { |
| 1081 | if (!E.ForwardTo.empty()) |
| 1082 | continue; |
| 1083 | E.Sym = addUndefined(E.Name); |
| 1084 | if (!E.Directives) |
| 1085 | Symtab.mangleMaybe(E.Sym); |
| 1086 | } |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 1087 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1088 | // Add weak aliases. Weak aliases is a mechanism to give remaining |
| 1089 | // undefined symbols final chance to be resolved successfully. |
| 1090 | for (auto Pair : Config->AlternateNames) { |
| 1091 | StringRef From = Pair.first; |
| 1092 | StringRef To = Pair.second; |
| 1093 | Symbol *Sym = Symtab.find(From); |
| 1094 | if (!Sym) |
| 1095 | continue; |
| 1096 | if (auto *U = dyn_cast<Undefined>(Sym->body())) |
| 1097 | if (!U->WeakAlias) |
| 1098 | U->WeakAlias = Symtab.addUndefined(To); |
| 1099 | } |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1100 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1101 | // Windows specific -- if __load_config_used can be resolved, resolve it. |
| 1102 | if (Symtab.findUnderscore("_load_config_used")) |
| 1103 | addUndefined(mangle("_load_config_used")); |
| 1104 | } while (run()); |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1105 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1106 | if (ErrorCount) |
| 1107 | return; |
| 1108 | |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1109 | // If /msvclto is given, we use the MSVC linker to link LTO output files. |
| 1110 | // This is useful because MSVC link.exe can generate complete PDBs. |
| 1111 | if (Args.hasArg(OPT_msvclto)) { |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 1112 | invokeMSVC(Args); |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1113 | exit(0); |
| 1114 | } |
| 1115 | |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 1116 | // Do LTO by compiling bitcode input files to a set of native COFF files then |
| 1117 | // link those files. |
| 1118 | Symtab.addCombinedLTOObjects(); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1119 | run(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 1120 | |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1121 | // Make sure we have resolved all symbols. |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 +0000 | [diff] [blame] | 1122 | Symtab.reportRemainingUndefines(); |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1123 | |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1124 | // Windows specific -- if no /subsystem is given, we need to infer |
| 1125 | // that from entry point name. |
| 1126 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1127 | Config->Subsystem = inferSubsystem(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1128 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 1129 | fatal("subsystem must be defined"); |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1132 | // Handle /safeseh. |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1133 | if (Args.hasArg(OPT_safeseh)) { |
Rui Ueyama | 13563d8 | 2015-09-15 00:33:11 +0000 | [diff] [blame] | 1134 | for (ObjectFile *File : Symtab.ObjectFiles) |
| 1135 | if (!File->SEHCompat) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1136 | error("/safeseh: " + File->getName() + " is not compatible with SEH"); |
| 1137 | if (ErrorCount) |
| 1138 | return; |
| 1139 | } |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1140 | |
Rui Ueyama | 151d862 | 2015-06-17 20:40:43 +0000 | [diff] [blame] | 1141 | // Windows specific -- when we are creating a .dll file, we also |
| 1142 | // need to create a .lib file. |
Rui Ueyama | 100ffac | 2015-09-01 09:15:58 +0000 | [diff] [blame] | 1143 | if (!Config->Exports.empty() || Config->DLL) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1144 | fixupExports(); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 1145 | createImportLibrary(/*AsLib=*/false); |
Rui Ueyama | 8765fba | 2015-07-15 22:21:08 +0000 | [diff] [blame] | 1146 | assignExportOrdinals(); |
| 1147 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1148 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1149 | // Windows specific -- Create a side-by-side manifest file. |
| 1150 | if (Config->Manifest == Configuration::SideBySide) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1151 | createSideBySideManifest(); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1152 | |
Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 1153 | // Identify unreferenced COMDAT sections. |
| 1154 | if (Config->DoGC) |
| 1155 | markLive(Symtab.getChunks()); |
| 1156 | |
| 1157 | // Identify identical COMDAT sections to merge them. |
| 1158 | if (Config->DoICF) |
| 1159 | doICF(Symtab.getChunks()); |
| 1160 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1161 | // Write the result. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1162 | writeResult(&Symtab); |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 1163 | |
Rui Ueyama | a51ce71 | 2015-07-03 05:31:35 +0000 | [diff] [blame] | 1164 | // Call exit to avoid calling destructors. |
| 1165 | exit(0); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1168 | } // namespace coff |
| 1169 | } // namespace lld |