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; |
| 58 | Argv0 = Args[0]; |
Rui Ueyama | 7fed58c | 2016-12-08 19:10:28 +0000 | [diff] [blame] | 59 | Config = make<Configuration>(); |
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 | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 204 | switch (Arg->getOption().getID()) { |
| 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 | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 250 | bool HasExt = (Filename.find('.') != StringRef::npos); |
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 | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 278 | bool HasExt = (Filename.find('.') != StringRef::npos); |
| 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 | |
| 432 | std::vector<COFFShortExport> createCOFFShortExportFromConfig() { |
| 433 | std::vector<COFFShortExport> Exports; |
| 434 | for (Export &E1 : Config->Exports) { |
| 435 | COFFShortExport E2; |
| 436 | // Use SymbolName, which will have any stdcall or fastcall qualifiers. |
| 437 | E2.Name = E1.SymbolName; |
| 438 | E2.ExtName = E1.ExtName; |
| 439 | E2.Ordinal = E1.Ordinal; |
| 440 | E2.Noname = E1.Noname; |
| 441 | E2.Data = E1.Data; |
| 442 | E2.Private = E1.Private; |
| 443 | E2.Constant = E1.Constant; |
| 444 | Exports.push_back(E2); |
| 445 | } |
| 446 | return Exports; |
| 447 | } |
| 448 | |
| 449 | static void createImportLibrary() { |
| 450 | std::vector<COFFShortExport> Exports = createCOFFShortExportFromConfig(); |
| 451 | std::string DLLName = sys::path::filename(Config->OutputFile); |
| 452 | std::string Path = getImplibPath(); |
| 453 | writeImportLibrary(DLLName, Path, Exports, Config->Machine); |
| 454 | } |
| 455 | |
| 456 | static void parseModuleDefs(StringRef Path) { |
| 457 | std::unique_ptr<MemoryBuffer> MB = check( |
| 458 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
| 459 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 460 | |
| 461 | Expected<COFFModuleDefinition> Def = |
| 462 | parseCOFFModuleDefinition(MBRef, Config->Machine); |
| 463 | if (!Def) |
| 464 | fatal(errorToErrorCode(Def.takeError()).message()); |
| 465 | |
| 466 | COFFModuleDefinition &M = *Def; |
| 467 | if (Config->OutputFile.empty()) |
| 468 | Config->OutputFile = Saver.save(M.OutputFile); |
| 469 | |
| 470 | if (M.ImageBase) |
| 471 | Config->ImageBase = M.ImageBase; |
| 472 | if (M.StackReserve) |
| 473 | Config->StackReserve = M.StackReserve; |
| 474 | if (M.StackCommit) |
| 475 | Config->StackCommit = M.StackCommit; |
| 476 | if (M.HeapReserve) |
| 477 | Config->HeapReserve = M.HeapReserve; |
| 478 | if (M.HeapCommit) |
| 479 | Config->HeapCommit = M.HeapCommit; |
| 480 | if (M.MajorImageVersion) |
| 481 | Config->MajorImageVersion = M.MajorImageVersion; |
| 482 | if (M.MinorImageVersion) |
| 483 | Config->MinorImageVersion = M.MinorImageVersion; |
| 484 | if (M.MajorOSVersion) |
| 485 | Config->MajorOSVersion = M.MajorOSVersion; |
| 486 | if (M.MinorOSVersion) |
| 487 | Config->MinorOSVersion = M.MinorOSVersion; |
| 488 | |
| 489 | for (COFFShortExport E1 : M.Exports) { |
| 490 | Export E2; |
| 491 | E2.Name = Saver.save(E1.Name); |
| 492 | if (E1.isWeak()) |
| 493 | E2.ExtName = Saver.save(E1.ExtName); |
| 494 | E2.Ordinal = E1.Ordinal; |
| 495 | E2.Noname = E1.Noname; |
| 496 | E2.Data = E1.Data; |
| 497 | E2.Private = E1.Private; |
| 498 | E2.Constant = E1.Constant; |
| 499 | Config->Exports.push_back(E2); |
| 500 | } |
| 501 | } |
| 502 | |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 503 | std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) { |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 504 | std::vector<MemoryBufferRef> V; |
| 505 | Error Err = Error::success(); |
| 506 | for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { |
| 507 | Archive::Child C = |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 508 | check(COrErr, |
| 509 | File->getFileName() + ": could not get the child of the archive"); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 510 | MemoryBufferRef MBRef = |
| 511 | check(C.getMemoryBufferRef(), |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 512 | File->getFileName() + |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 513 | ": could not get the buffer for a child of the archive"); |
| 514 | V.push_back(MBRef); |
| 515 | } |
| 516 | if (Err) |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 517 | fatal(File->getFileName() + |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 518 | ": Archive::children failed: " + toString(std::move(Err))); |
| 519 | return V; |
| 520 | } |
| 521 | |
| 522 | // A helper function for filterBitcodeFiles. |
| 523 | static bool needsRebuilding(MemoryBufferRef MB) { |
| 524 | // The MSVC linker doesn't support thin archives, so if it's a thin |
| 525 | // archive, we always need to rebuild it. |
| 526 | std::unique_ptr<Archive> File = |
| 527 | check(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier()); |
| 528 | if (File->isThin()) |
| 529 | return true; |
| 530 | |
| 531 | // Returns true if the archive contains at least one bitcode file. |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 532 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 533 | if (identify_magic(Member.getBuffer()) == file_magic::bitcode) |
| 534 | return true; |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | // Opens a given path as an archive file and removes bitcode files |
| 539 | // from them if exists. This function is to appease the MSVC linker as |
| 540 | // their linker doesn't like archive files containing non-native |
| 541 | // object files. |
| 542 | // |
| 543 | // If a given archive doesn't contain bitcode files, the archive path |
| 544 | // is returned as-is. Otherwise, a new temporary file is created and |
| 545 | // its path is returned. |
| 546 | static Optional<std::string> |
| 547 | filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) { |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 548 | std::unique_ptr<MemoryBuffer> MB = check( |
| 549 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 550 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 551 | file_magic Magic = identify_magic(MBRef.getBuffer()); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 552 | |
| 553 | if (Magic == file_magic::bitcode) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 554 | return None; |
| 555 | if (Magic != file_magic::archive) |
| 556 | return Path.str(); |
| 557 | if (!needsRebuilding(MBRef)) |
| 558 | return Path.str(); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 559 | |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 560 | std::unique_ptr<Archive> File = |
| 561 | check(Archive::create(MBRef), |
| 562 | MBRef.getBufferIdentifier() + ": failed to parse archive"); |
| 563 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 564 | std::vector<NewArchiveMember> New; |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 565 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 566 | if (identify_magic(Member.getBuffer()) != file_magic::bitcode) |
| 567 | New.emplace_back(Member); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 568 | |
Peter Collingbourne | db7447d | 2017-03-17 02:04:22 +0000 | [diff] [blame] | 569 | if (New.empty()) |
| 570 | return None; |
| 571 | |
| 572 | log("Creating a temporary archive for " + Path + " to remove bitcode files"); |
| 573 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 574 | SmallString<128> S; |
| 575 | if (auto EC = sys::fs::createTemporaryFile("lld-" + sys::path::stem(Path), |
| 576 | ".lib", S)) |
| 577 | fatal(EC, "cannot create a temporary file"); |
| 578 | std::string Temp = S.str(); |
| 579 | TemporaryFiles.push_back(Temp); |
| 580 | |
| 581 | std::pair<StringRef, std::error_code> Ret = |
| 582 | llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU, |
| 583 | /*Deterministics=*/true, |
| 584 | /*Thin=*/false); |
| 585 | if (Ret.second) |
| 586 | error("failed to create a new archive " + S.str() + ": " + Ret.first); |
| 587 | return Temp; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // Create response file contents and invoke the MSVC linker. |
| 591 | void LinkerDriver::invokeMSVC(opt::InputArgList &Args) { |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 592 | std::string Rsp = "/nologo\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 593 | std::vector<std::string> Temps; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 594 | |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 595 | // Write out archive members that we used in symbol resolution and pass these |
| 596 | // to MSVC before any archives, so that MSVC uses the same objects to satisfy |
| 597 | // references. |
| 598 | for (const auto *O : Symtab.ObjectFiles) { |
| 599 | if (O->ParentName.empty()) |
| 600 | continue; |
| 601 | SmallString<128> S; |
| 602 | int Fd; |
| 603 | if (auto EC = sys::fs::createTemporaryFile( |
| 604 | "lld-" + sys::path::filename(O->ParentName), ".obj", Fd, S)) |
| 605 | fatal(EC, "cannot create a temporary file"); |
| 606 | raw_fd_ostream OS(Fd, /*shouldClose*/ true); |
| 607 | OS << O->MB.getBuffer(); |
| 608 | Temps.push_back(S.str()); |
| 609 | Rsp += quote(S) + "\n"; |
| 610 | } |
| 611 | |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 612 | for (auto *Arg : Args) { |
| 613 | switch (Arg->getOption().getID()) { |
| 614 | case OPT_linkrepro: |
| 615 | case OPT_lldmap: |
| 616 | case OPT_lldmap_file: |
Peter Collingbourne | 8713bf6 | 2017-03-17 02:11:09 +0000 | [diff] [blame] | 617 | case OPT_lldsavetemps: |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 618 | case OPT_msvclto: |
| 619 | // LLD-specific options are stripped. |
| 620 | break; |
| 621 | case OPT_opt: |
| 622 | if (!StringRef(Arg->getValue()).startswith("lld")) |
| 623 | Rsp += toString(Arg) + " "; |
| 624 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 625 | case OPT_INPUT: { |
| 626 | if (Optional<StringRef> Path = doFindFile(Arg->getValue())) { |
| 627 | if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps)) |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 628 | Rsp += quote(*S) + "\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 629 | continue; |
| 630 | } |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 631 | Rsp += quote(Arg->getValue()) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 632 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 633 | } |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 634 | default: |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 635 | Rsp += toString(Arg) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | std::vector<StringRef> ObjectFiles = Symtab.compileBitcodeFiles(); |
| 640 | runMSVCLinker(Rsp, ObjectFiles); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 641 | |
| 642 | for (StringRef Path : Temps) |
| 643 | sys::fs::remove(Path); |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 646 | void LinkerDriver::enqueueTask(std::function<void()> Task) { |
| 647 | TaskQueue.push_back(std::move(Task)); |
| 648 | } |
| 649 | |
| 650 | bool LinkerDriver::run() { |
| 651 | bool DidWork = !TaskQueue.empty(); |
| 652 | while (!TaskQueue.empty()) { |
| 653 | TaskQueue.front()(); |
| 654 | TaskQueue.pop_front(); |
| 655 | } |
| 656 | return DidWork; |
| 657 | } |
| 658 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 659 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 660 | // If the first command line argument is "/lib", link.exe acts like lib.exe. |
| 661 | // We call our own implementation of lib.exe that understands bitcode files. |
| 662 | if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) { |
| 663 | if (llvm::libDriverMain(ArgsArr.slice(1)) != 0) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 664 | fatal("lib failed"); |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 668 | // Needed for LTO. |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 669 | InitializeAllTargetInfos(); |
| 670 | InitializeAllTargets(); |
| 671 | InitializeAllTargetMCs(); |
| 672 | InitializeAllAsmParsers(); |
| 673 | InitializeAllAsmPrinters(); |
| 674 | InitializeAllDisassemblers(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 675 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 676 | // Parse command line options. |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 677 | opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 678 | |
Rui Ueyama | 9a3e733 | 2017-03-30 20:10:40 +0000 | [diff] [blame] | 679 | // Parse and evaluate -mllvm options. |
| 680 | std::vector<const char *> V; |
| 681 | V.push_back("lld-link (LLVM option parsing)"); |
| 682 | for (auto *Arg : Args.filtered(OPT_mllvm)) |
| 683 | V.push_back(Arg->getValue()); |
| 684 | cl::ParseCommandLineOptions(V.size(), V.data()); |
| 685 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 686 | // Handle /errorlimit early, because error() depends on it. |
| 687 | if (auto *Arg = Args.getLastArg(OPT_errorlimit)) { |
| 688 | int N = 20; |
| 689 | StringRef S = Arg->getValue(); |
| 690 | if (S.getAsInteger(10, N)) |
| 691 | error(Arg->getSpelling() + " number expected, but got " + S); |
| 692 | Config->ErrorLimit = N; |
| 693 | } |
| 694 | |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 695 | // Handle /help |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 696 | if (Args.hasArg(OPT_help)) { |
David Blaikie | b2b1c7c | 2015-06-21 06:32:10 +0000 | [diff] [blame] | 697 | printHelp(ArgsArr[0]); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 698 | return; |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 701 | if (auto *Arg = Args.getLastArg(OPT_linkrepro)) { |
| 702 | SmallString<64> Path = StringRef(Arg->getValue()); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 703 | sys::path::append(Path, "repro.tar"); |
| 704 | |
| 705 | Expected<std::unique_ptr<TarWriter>> ErrOrWriter = |
| 706 | TarWriter::create(Path, "repro"); |
| 707 | |
| 708 | if (ErrOrWriter) { |
| 709 | Tar = std::move(*ErrOrWriter); |
| 710 | } else { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 711 | error("/linkrepro: failed to open " + Path + ": " + |
| 712 | toString(ErrOrWriter.takeError())); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 713 | } |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Richard Smith | a13714e | 2017-04-12 23:51:20 +0000 | [diff] [blame] | 716 | if (!Args.hasArgNoClaim(OPT_INPUT)) |
Rui Ueyama | bb57954 | 2016-07-15 01:12:24 +0000 | [diff] [blame] | 717 | fatal("no input files"); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 718 | |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 719 | // Construct search path list. |
| 720 | SearchPaths.push_back(""); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 721 | for (auto *Arg : Args.filtered(OPT_libpath)) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 722 | SearchPaths.push_back(Arg->getValue()); |
| 723 | addLibSearchPaths(); |
| 724 | |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 725 | // Handle /out |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 726 | if (auto *Arg = Args.getLastArg(OPT_out)) |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 727 | Config->OutputFile = Arg->getValue(); |
| 728 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 729 | // Handle /verbose |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 730 | if (Args.hasArg(OPT_verbose)) |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 731 | Config->Verbose = true; |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 732 | |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 733 | // Handle /force or /force:unresolved |
| 734 | if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved)) |
| 735 | Config->Force = true; |
| 736 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 737 | // Handle /debug |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 738 | if (Args.hasArg(OPT_debug)) { |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 739 | Config->Debug = true; |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 740 | Config->DebugTypes = |
| 741 | Args.hasArg(OPT_debugtype) |
| 742 | ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue()) |
| 743 | : getDefaultDebugType(Args); |
| 744 | } |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 745 | |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 746 | // Create a dummy PDB file to satisfy build sytem rules. |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 747 | if (auto *Arg = Args.getLastArg(OPT_pdb)) |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 748 | Config->PDBPath = Arg->getValue(); |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 749 | |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 750 | // Handle /noentry |
| 751 | if (Args.hasArg(OPT_noentry)) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 752 | if (Args.hasArg(OPT_dll)) |
| 753 | Config->NoEntry = true; |
| 754 | else |
| 755 | error("/noentry must be specified with /dll"); |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 758 | // Handle /dll |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 759 | if (Args.hasArg(OPT_dll)) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 760 | Config->DLL = true; |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 761 | Config->ManifestID = 2; |
| 762 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 763 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 764 | // Handle /fixed |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 765 | if (Args.hasArg(OPT_fixed)) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 766 | if (Args.hasArg(OPT_dynamicbase)) { |
| 767 | error("/fixed must not be specified with /dynamicbase"); |
| 768 | } else { |
| 769 | Config->Relocatable = false; |
| 770 | Config->DynamicBase = false; |
| 771 | } |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 772 | } |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 773 | |
Saleem Abdulrasool | 671029d | 2017-04-06 23:07:53 +0000 | [diff] [blame] | 774 | if (Args.hasArg(OPT_appcontainer)) |
| 775 | Config->AppContainer = true; |
| 776 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 777 | // Handle /machine |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 778 | if (auto *Arg = Args.getLastArg(OPT_machine)) |
| 779 | Config->Machine = getMachineType(Arg->getValue()); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 780 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 781 | // Handle /nodefaultlib:<filename> |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 782 | for (auto *Arg : Args.filtered(OPT_nodefaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 783 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 784 | |
| 785 | // Handle /nodefaultlib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 786 | if (Args.hasArg(OPT_nodefaultlib_all)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 787 | Config->NoDefaultLibAll = true; |
| 788 | |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 789 | // Handle /base |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 790 | if (auto *Arg = Args.getLastArg(OPT_base)) |
| 791 | parseNumbers(Arg->getValue(), &Config->ImageBase); |
Rui Ueyama | b41b7e5 | 2015-05-29 16:21:11 +0000 | [diff] [blame] | 792 | |
| 793 | // Handle /stack |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 794 | if (auto *Arg = Args.getLastArg(OPT_stack)) |
| 795 | parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit); |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 796 | |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 797 | // Handle /heap |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 798 | if (auto *Arg = Args.getLastArg(OPT_heap)) |
| 799 | parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit); |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 800 | |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 801 | // Handle /version |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 802 | if (auto *Arg = Args.getLastArg(OPT_version)) |
| 803 | parseVersion(Arg->getValue(), &Config->MajorImageVersion, |
| 804 | &Config->MinorImageVersion); |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 805 | |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 806 | // Handle /subsystem |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 807 | if (auto *Arg = Args.getLastArg(OPT_subsystem)) |
| 808 | parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion, |
| 809 | &Config->MinorOSVersion); |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 810 | |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 811 | // Handle /alternatename |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 812 | for (auto *Arg : Args.filtered(OPT_alternatename)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 813 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 814 | |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 815 | // Handle /include |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 816 | for (auto *Arg : Args.filtered(OPT_incl)) |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 817 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 818 | |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 819 | // Handle /implib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 820 | if (auto *Arg = Args.getLastArg(OPT_implib)) |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 821 | Config->Implib = Arg->getValue(); |
| 822 | |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 823 | // Handle /opt |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 824 | for (auto *Arg : Args.filtered(OPT_opt)) { |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 825 | std::string Str = StringRef(Arg->getValue()).lower(); |
| 826 | SmallVector<StringRef, 1> Vec; |
| 827 | StringRef(Str).split(Vec, ','); |
| 828 | for (StringRef S : Vec) { |
| 829 | if (S == "noref") { |
| 830 | Config->DoGC = false; |
| 831 | Config->DoICF = false; |
| 832 | continue; |
| 833 | } |
| 834 | if (S == "icf" || StringRef(S).startswith("icf=")) { |
| 835 | Config->DoICF = true; |
| 836 | continue; |
| 837 | } |
| 838 | if (S == "noicf") { |
| 839 | Config->DoICF = false; |
| 840 | continue; |
| 841 | } |
| 842 | if (StringRef(S).startswith("lldlto=")) { |
| 843 | StringRef OptLevel = StringRef(S).substr(7); |
| 844 | if (OptLevel.getAsInteger(10, Config->LTOOptLevel) || |
| 845 | Config->LTOOptLevel > 3) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 846 | error("/opt:lldlto: invalid optimization level: " + OptLevel); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 847 | continue; |
| 848 | } |
| 849 | if (StringRef(S).startswith("lldltojobs=")) { |
| 850 | StringRef Jobs = StringRef(S).substr(11); |
| 851 | if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 852 | error("/opt:lldltojobs: invalid job count: " + Jobs); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 853 | continue; |
| 854 | } |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 +0000 | [diff] [blame] | 855 | if (StringRef(S).startswith("lldltopartitions=")) { |
| 856 | StringRef N = StringRef(S).substr(17); |
| 857 | if (N.getAsInteger(10, Config->LTOPartitions) || |
| 858 | Config->LTOPartitions == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 859 | error("/opt:lldltopartitions: invalid partition count: " + N); |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 +0000 | [diff] [blame] | 860 | continue; |
| 861 | } |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 862 | if (S != "ref" && S != "lbr" && S != "nolbr") |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 863 | error("/opt: unknown option: " + S); |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 864 | } |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Bob Haarman | 69b196d | 2017-02-08 18:36:41 +0000 | [diff] [blame] | 867 | // Handle /lldsavetemps |
| 868 | if (Args.hasArg(OPT_lldsavetemps)) |
| 869 | Config->SaveTemps = true; |
| 870 | |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 871 | // Handle /failifmismatch |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 872 | for (auto *Arg : Args.filtered(OPT_failifmismatch)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 873 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 874 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 875 | // Handle /merge |
| 876 | for (auto *Arg : Args.filtered(OPT_merge)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 877 | parseMerge(Arg->getValue()); |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 878 | |
Rui Ueyama | 440138c | 2016-06-20 03:39:39 +0000 | [diff] [blame] | 879 | // Handle /section |
| 880 | for (auto *Arg : Args.filtered(OPT_section)) |
| 881 | parseSection(Arg->getValue()); |
| 882 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 883 | // Handle /manifest |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 884 | if (auto *Arg = Args.getLastArg(OPT_manifest_colon)) |
| 885 | parseManifest(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 886 | |
| 887 | // Handle /manifestuac |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 888 | if (auto *Arg = Args.getLastArg(OPT_manifestuac)) |
| 889 | parseManifestUAC(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 890 | |
| 891 | // Handle /manifestdependency |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 892 | if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 893 | Config->ManifestDependency = Arg->getValue(); |
| 894 | |
| 895 | // Handle /manifestfile |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 896 | if (auto *Arg = Args.getLastArg(OPT_manifestfile)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 897 | Config->ManifestFile = Arg->getValue(); |
| 898 | |
Rui Ueyama | afb1901 | 2016-04-19 01:21:58 +0000 | [diff] [blame] | 899 | // Handle /manifestinput |
| 900 | for (auto *Arg : Args.filtered(OPT_manifestinput)) |
| 901 | Config->ManifestInput.push_back(Arg->getValue()); |
| 902 | |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 903 | // Handle miscellaneous boolean flags. |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 904 | if (Args.hasArg(OPT_allowisolation_no)) |
| 905 | Config->AllowIsolation = false; |
| 906 | if (Args.hasArg(OPT_dynamicbase_no)) |
| 907 | Config->DynamicBase = false; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 908 | if (Args.hasArg(OPT_nxcompat_no)) |
| 909 | Config->NxCompat = false; |
| 910 | if (Args.hasArg(OPT_tsaware_no)) |
| 911 | Config->TerminalServerAware = false; |
Rui Ueyama | 9640173 | 2015-09-21 23:43:31 +0000 | [diff] [blame] | 912 | if (Args.hasArg(OPT_nosymtab)) |
| 913 | Config->WriteSymtab = false; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 914 | Config->DumpPdb = Args.hasArg(OPT_dumppdb); |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 915 | |
Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 916 | Config->MapFile = getMapFile(Args); |
| 917 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 918 | if (ErrorCount) |
| 919 | return; |
| 920 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 921 | // Create a list of input files. Files can be given as arguments |
| 922 | // for /defaultlib option. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 923 | std::vector<MemoryBufferRef> MBs; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 924 | for (auto *Arg : Args.filtered(OPT_INPUT)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 925 | if (Optional<StringRef> Path = findFile(Arg->getValue())) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 926 | enqueuePath(*Path); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 927 | for (auto *Arg : Args.filtered(OPT_defaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 928 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 929 | enqueuePath(*Path); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 930 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 931 | // Windows specific -- Create a resource file containing a manifest file. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 932 | if (Config->Manifest == Configuration::Embed) |
| 933 | addBuffer(createManifestRes()); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 934 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 935 | // Read all input files given via the command line. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 936 | run(); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 937 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 938 | // We should have inferred a machine type by now from the input files, but if |
| 939 | // not we assume x64. |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 940 | if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 941 | warn("/machine is not specified. x64 is assumed"); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 942 | Config->Machine = AMD64; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 945 | // Windows specific -- Input files can be Windows resource files (.res files). |
| 946 | // We invoke cvtres.exe to convert resource files to a regular COFF file |
| 947 | // then link the result file normally. |
| 948 | if (!Resources.empty()) |
| 949 | addBuffer(convertResToCOFF(Resources)); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 950 | |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 951 | if (Tar) |
| 952 | Tar->append("response.txt", |
| 953 | createResponseFile(Args, FilePaths, |
| 954 | ArrayRef<StringRef>(SearchPaths).slice(1))); |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 955 | |
Rui Ueyama | 4d54534 | 2015-07-28 03:12:00 +0000 | [diff] [blame] | 956 | // Handle /largeaddressaware |
| 957 | if (Config->is64() || Args.hasArg(OPT_largeaddressaware)) |
| 958 | Config->LargeAddressAware = true; |
| 959 | |
Rui Ueyama | d68e211 | 2015-07-28 03:15:57 +0000 | [diff] [blame] | 960 | // Handle /highentropyva |
| 961 | if (Config->is64() && !Args.hasArg(OPT_highentropyva_no)) |
| 962 | Config->HighEntropyVA = true; |
| 963 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 964 | // Handle /entry and /dll |
| 965 | if (auto *Arg = Args.getLastArg(OPT_entry)) { |
| 966 | Config->Entry = addUndefined(mangle(Arg->getValue())); |
| 967 | } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 968 | StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12" |
| 969 | : "_DllMainCRTStartup"; |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 970 | Config->Entry = addUndefined(S); |
| 971 | } else if (!Config->NoEntry) { |
| 972 | // Windows specific -- If entry point name is not given, we need to |
| 973 | // infer that from user-defined entry name. |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 974 | StringRef S = findDefaultEntry(); |
David Blaikie | 4cdfe69 | 2017-02-19 02:25:47 +0000 | [diff] [blame] | 975 | if (S.empty()) |
| 976 | fatal("entry point must be defined"); |
| 977 | Config->Entry = addUndefined(S); |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 978 | log("Entry name inferred: " + S); |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 981 | // Handle /export |
| 982 | for (auto *Arg : Args.filtered(OPT_export)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 983 | Export E = parseExport(Arg->getValue()); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 984 | if (Config->Machine == I386) { |
| 985 | if (!isDecorated(E.Name)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 986 | E.Name = Saver.save("_" + E.Name); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 987 | if (!E.ExtName.empty() && !isDecorated(E.ExtName)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 988 | E.ExtName = Saver.save("_" + E.ExtName); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 989 | } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 990 | Config->Exports.push_back(E); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // Handle /def |
| 994 | if (auto *Arg = Args.getLastArg(OPT_deffile)) { |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 995 | // parseModuleDefs mutates Config object. |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 996 | parseModuleDefs(Arg->getValue()); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 999 | // Handle /delayload |
| 1000 | for (auto *Arg : Args.filtered(OPT_delayload)) { |
| 1001 | Config->DelayLoads.insert(StringRef(Arg->getValue()).lower()); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1002 | if (Config->Machine == I386) { |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1003 | Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8"); |
Rui Ueyama | 35ccb0f | 2015-07-25 00:20:06 +0000 | [diff] [blame] | 1004 | } else { |
| 1005 | Config->DelayLoadHelper = addUndefined("__delayLoadHelper2"); |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1009 | // Set default image name if neither /out or /def set it. |
| 1010 | if (Config->OutputFile.empty()) { |
| 1011 | Config->OutputFile = |
Richard Smith | a13714e | 2017-04-12 23:51:20 +0000 | [diff] [blame] | 1012 | getOutputPath((*Args.filtered(OPT_INPUT).begin())->getValue()); |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Reid Kleckner | 13bdbfb | 2017-03-22 00:57:14 +0000 | [diff] [blame] | 1015 | // Put the PDB next to the image if no /pdb flag was passed. |
| 1016 | if (Config->Debug && Config->PDBPath.empty()) { |
| 1017 | Config->PDBPath = Config->OutputFile; |
| 1018 | sys::path::replace_extension(Config->PDBPath, ".pdb"); |
| 1019 | } |
| 1020 | |
Reid Kleckner | 77d3aa4 | 2017-03-22 19:49:12 +0000 | [diff] [blame] | 1021 | // Disable PDB generation if the user requested it. |
| 1022 | if (Args.hasArg(OPT_nopdb)) |
| 1023 | Config->PDBPath = ""; |
| 1024 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 1025 | // Set default image base if /base is not given. |
| 1026 | if (Config->ImageBase == uint64_t(-1)) |
| 1027 | Config->ImageBase = getDefaultImageBase(); |
| 1028 | |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 +0000 | [diff] [blame] | 1029 | Symtab.addRelative(mangle("__ImageBase"), 0); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1030 | if (Config->Machine == I386) { |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 1031 | Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0); |
| 1032 | Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0); |
| 1033 | } |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1034 | |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1035 | // We do not support /guard:cf (control flow protection) yet. |
| 1036 | // Define CFG symbols anyway so that we can link MSVC 2015 CRT. |
| 1037 | Symtab.addAbsolute(mangle("__guard_fids_table"), 0); |
| 1038 | Symtab.addAbsolute(mangle("__guard_fids_count"), 0); |
| 1039 | Symtab.addAbsolute(mangle("__guard_flags"), 0x100); |
| 1040 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1041 | // This code may add new undefined symbols to the link, which may enqueue more |
| 1042 | // symbol resolution tasks, so we need to continue executing tasks until we |
| 1043 | // converge. |
| 1044 | do { |
| 1045 | // Windows specific -- if entry point is not found, |
| 1046 | // search for its mangled names. |
| 1047 | if (Config->Entry) |
| 1048 | Symtab.mangleMaybe(Config->Entry); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 1049 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1050 | // Windows specific -- Make sure we resolve all dllexported symbols. |
| 1051 | for (Export &E : Config->Exports) { |
| 1052 | if (!E.ForwardTo.empty()) |
| 1053 | continue; |
| 1054 | E.Sym = addUndefined(E.Name); |
| 1055 | if (!E.Directives) |
| 1056 | Symtab.mangleMaybe(E.Sym); |
| 1057 | } |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 1058 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1059 | // Add weak aliases. Weak aliases is a mechanism to give remaining |
| 1060 | // undefined symbols final chance to be resolved successfully. |
| 1061 | for (auto Pair : Config->AlternateNames) { |
| 1062 | StringRef From = Pair.first; |
| 1063 | StringRef To = Pair.second; |
| 1064 | Symbol *Sym = Symtab.find(From); |
| 1065 | if (!Sym) |
| 1066 | continue; |
| 1067 | if (auto *U = dyn_cast<Undefined>(Sym->body())) |
| 1068 | if (!U->WeakAlias) |
| 1069 | U->WeakAlias = Symtab.addUndefined(To); |
| 1070 | } |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1071 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1072 | // Windows specific -- if __load_config_used can be resolved, resolve it. |
| 1073 | if (Symtab.findUnderscore("_load_config_used")) |
| 1074 | addUndefined(mangle("_load_config_used")); |
| 1075 | } while (run()); |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1076 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1077 | if (ErrorCount) |
| 1078 | return; |
| 1079 | |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1080 | // If /msvclto is given, we use the MSVC linker to link LTO output files. |
| 1081 | // This is useful because MSVC link.exe can generate complete PDBs. |
| 1082 | if (Args.hasArg(OPT_msvclto)) { |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 1083 | invokeMSVC(Args); |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1084 | exit(0); |
| 1085 | } |
| 1086 | |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 1087 | // Do LTO by compiling bitcode input files to a set of native COFF files then |
| 1088 | // link those files. |
| 1089 | Symtab.addCombinedLTOObjects(); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1090 | run(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 1091 | |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1092 | // Make sure we have resolved all symbols. |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 +0000 | [diff] [blame] | 1093 | Symtab.reportRemainingUndefines(); |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1094 | |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1095 | // Windows specific -- if no /subsystem is given, we need to infer |
| 1096 | // that from entry point name. |
| 1097 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1098 | Config->Subsystem = inferSubsystem(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1099 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 1100 | fatal("subsystem must be defined"); |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1103 | // Handle /safeseh. |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1104 | if (Args.hasArg(OPT_safeseh)) { |
Rui Ueyama | 13563d8 | 2015-09-15 00:33:11 +0000 | [diff] [blame] | 1105 | for (ObjectFile *File : Symtab.ObjectFiles) |
| 1106 | if (!File->SEHCompat) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1107 | error("/safeseh: " + File->getName() + " is not compatible with SEH"); |
| 1108 | if (ErrorCount) |
| 1109 | return; |
| 1110 | } |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1111 | |
Rui Ueyama | 151d862 | 2015-06-17 20:40:43 +0000 | [diff] [blame] | 1112 | // Windows specific -- when we are creating a .dll file, we also |
| 1113 | // need to create a .lib file. |
Rui Ueyama | 100ffac | 2015-09-01 09:15:58 +0000 | [diff] [blame] | 1114 | if (!Config->Exports.empty() || Config->DLL) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1115 | fixupExports(); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 1116 | createImportLibrary(); |
Rui Ueyama | 8765fba | 2015-07-15 22:21:08 +0000 | [diff] [blame] | 1117 | assignExportOrdinals(); |
| 1118 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1119 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1120 | // Windows specific -- Create a side-by-side manifest file. |
| 1121 | if (Config->Manifest == Configuration::SideBySide) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1122 | createSideBySideManifest(); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1123 | |
Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 1124 | // Identify unreferenced COMDAT sections. |
| 1125 | if (Config->DoGC) |
| 1126 | markLive(Symtab.getChunks()); |
| 1127 | |
| 1128 | // Identify identical COMDAT sections to merge them. |
| 1129 | if (Config->DoICF) |
| 1130 | doICF(Symtab.getChunks()); |
| 1131 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1132 | // Write the result. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1133 | writeResult(&Symtab); |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 1134 | |
Rui Ueyama | a51ce71 | 2015-07-03 05:31:35 +0000 | [diff] [blame] | 1135 | // Call exit to avoid calling destructors. |
| 1136 | exit(0); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1139 | } // namespace coff |
| 1140 | } // namespace lld |