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" |
Sam Clegg | f187c4d | 2018-02-20 22:09:59 +0000 | [diff] [blame] | 12 | #include "ICF.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
Sam Clegg | f187c4d | 2018-02-20 22:09:59 +0000 | [diff] [blame] | 14 | #include "MarkLive.h" |
Martin Storsjo | e1f894d | 2017-10-19 19:49:38 +0000 | [diff] [blame] | 15 | #include "MinGW.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 16 | #include "SymbolTable.h" |
Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 17 | #include "Symbols.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 18 | #include "Writer.h" |
Rui Ueyama | 57175aa | 2018-01-27 00:34:46 +0000 | [diff] [blame] | 19 | #include "lld/Common/Args.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 20 | #include "lld/Common/Driver.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 21 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 22 | #include "lld/Common/Memory.h" |
Zachary Turner | 727f153 | 2018-01-17 19:16:26 +0000 | [diff] [blame] | 23 | #include "lld/Common/Timer.h" |
Rui Ueyama | a4cf97b | 2017-10-23 14:57:53 +0000 | [diff] [blame] | 24 | #include "lld/Common/Version.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Optional.h" |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 27 | #include "llvm/BinaryFormat/Magic.h" |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ArchiveWriter.h" |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 29 | #include "llvm/Object/COFFImportFile.h" |
| 30 | #include "llvm/Object/COFFModuleDefinition.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 31 | #include "llvm/Option/Arg.h" |
| 32 | #include "llvm/Option/ArgList.h" |
| 33 | #include "llvm/Option/Option.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Path.h" |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Process.h" |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 37 | #include "llvm/Support/TarWriter.h" |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetSelect.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | c6f07c4 | 2017-05-13 22:06:46 +0000 | [diff] [blame] | 40 | #include "llvm/ToolDrivers/llvm-lib/LibDriver.h" |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 42 | #include <future> |
Sam Clegg | f187c4d | 2018-02-20 22:09:59 +0000 | [diff] [blame] | 43 | #include <memory> |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 44 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 45 | using namespace llvm; |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 46 | using namespace llvm::object; |
Rui Ueyama | 84936e0 | 2015-07-07 23:39:18 +0000 | [diff] [blame] | 47 | using namespace llvm::COFF; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 48 | using llvm::sys::Process; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 49 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 50 | namespace lld { |
| 51 | namespace coff { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 52 | |
Zachary Turner | 727f153 | 2018-01-17 19:16:26 +0000 | [diff] [blame] | 53 | static Timer InputFileTimer("Input File Reading", Timer::root()); |
| 54 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 55 | Configuration *Config; |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 56 | LinkerDriver *Driver; |
| 57 | |
Rui Ueyama | 6f4e255 | 2017-10-23 20:03:32 +0000 | [diff] [blame] | 58 | bool link(ArrayRef<const char *> Args, bool CanExitEarly, raw_ostream &Diag) { |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 59 | errorHandler().LogName = Args[0]; |
| 60 | errorHandler().ErrorOS = &Diag; |
| 61 | errorHandler().ColorDiagnostics = Diag.has_colors(); |
| 62 | errorHandler().ErrorLimitExceededMsg = |
| 63 | "too many errors emitted, stopping now" |
| 64 | " (use /ERRORLIMIT:0 to see all errors)"; |
Shoaib Meenai | fd3e4b0 | 2018-01-08 05:58:36 +0000 | [diff] [blame] | 65 | errorHandler().ExitEarly = CanExitEarly; |
Rui Ueyama | 7fed58c | 2016-12-08 19:10:28 +0000 | [diff] [blame] | 66 | Config = make<Configuration>(); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 67 | Config->Argv = {Args.begin(), Args.end()}; |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 68 | |
| 69 | Symtab = make<SymbolTable>(); |
| 70 | |
Rui Ueyama | 7fed58c | 2016-12-08 19:10:28 +0000 | [diff] [blame] | 71 | Driver = make<LinkerDriver>(); |
Rui Ueyama | 417553d | 2016-02-28 19:54:51 +0000 | [diff] [blame] | 72 | Driver->link(Args); |
Rui Ueyama | 6f4e255 | 2017-10-23 20:03:32 +0000 | [diff] [blame] | 73 | |
| 74 | // Call exit() if we can to avoid calling destructors. |
| 75 | if (CanExitEarly) |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 76 | exitLld(errorCount() ? 1 : 0); |
Rui Ueyama | 6f4e255 | 2017-10-23 20:03:32 +0000 | [diff] [blame] | 77 | |
| 78 | freeArena(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 79 | return !errorCount(); |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 80 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 81 | |
Nico Weber | 5660de7 | 2016-04-20 22:34:15 +0000 | [diff] [blame] | 82 | // Drop directory components and replace extension with ".exe" or ".dll". |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 83 | static std::string getOutputPath(StringRef Path) { |
| 84 | auto P = Path.find_last_of("\\/"); |
| 85 | StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1); |
Nico Weber | 5660de7 | 2016-04-20 22:34:15 +0000 | [diff] [blame] | 86 | const char* E = Config->DLL ? ".dll" : ".exe"; |
| 87 | return (S.substr(0, S.rfind('.')) + E).str(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 90 | // ErrorOr is not default constructible, so it cannot be used as the type |
| 91 | // parameter of a future. |
| 92 | // FIXME: We could open the file in createFutureForFile and avoid needing to |
| 93 | // return an error here, but for the moment that would cost us a file descriptor |
| 94 | // (a limited resource on Windows) for the duration that the future is pending. |
| 95 | typedef std::pair<std::unique_ptr<MemoryBuffer>, std::error_code> MBErrPair; |
| 96 | |
| 97 | // Create a std::future that opens and maps a file using the best strategy for |
| 98 | // the host platform. |
| 99 | static std::future<MBErrPair> createFutureForFile(std::string Path) { |
| 100 | #if LLVM_ON_WIN32 |
| 101 | // On Windows, file I/O is relatively slow so it is best to do this |
| 102 | // asynchronously. |
| 103 | auto Strategy = std::launch::async; |
| 104 | #else |
| 105 | auto Strategy = std::launch::deferred; |
| 106 | #endif |
| 107 | return std::async(Strategy, [=]() { |
| 108 | auto MBOrErr = MemoryBuffer::getFile(Path); |
| 109 | if (!MBOrErr) |
| 110 | return MBErrPair{nullptr, MBOrErr.getError()}; |
| 111 | return MBErrPair{std::move(*MBOrErr), std::error_code()}; |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> MB) { |
| 116 | MemoryBufferRef MBRef = *MB; |
Rui Ueyama | 01f9333 | 2017-05-18 17:03:49 +0000 | [diff] [blame] | 117 | make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take ownership |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 118 | |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 119 | if (Driver->Tar) |
| 120 | Driver->Tar->append(relativeToRoot(MBRef.getBufferIdentifier()), |
| 121 | MBRef.getBuffer()); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 122 | return MBRef; |
| 123 | } |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 124 | |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 125 | void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> MB, |
| 126 | bool WholeArchive) { |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 127 | StringRef Filename = MB->getBufferIdentifier(); |
| 128 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 129 | MemoryBufferRef MBRef = takeBuffer(std::move(MB)); |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 130 | FilePaths.push_back(Filename); |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 131 | |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 132 | // File type is detected by contents, not by file extension. |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 133 | switch (identify_magic(MBRef.getBuffer())) { |
| 134 | case file_magic::windows_resource: |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 135 | Resources.push_back(MBRef); |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 136 | break; |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 137 | case file_magic::archive: |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 138 | if (WholeArchive) { |
| 139 | std::unique_ptr<Archive> File = |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 140 | CHECK(Archive::create(MBRef), Filename + ": failed to parse archive"); |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 141 | |
| 142 | for (MemoryBufferRef M : getArchiveMembers(File.get())) |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 143 | addArchiveBuffer(M, "<whole-archive>", Filename); |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | Symtab->addFile(make<ArchiveFile>(MBRef)); |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 147 | break; |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 148 | case file_magic::bitcode: |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 149 | Symtab->addFile(make<BitcodeFile>(MBRef)); |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 150 | break; |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 151 | case file_magic::coff_object: |
| 152 | case file_magic::coff_import_library: |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 153 | Symtab->addFile(make<ObjFile>(MBRef)); |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 154 | break; |
Rui Ueyama | cdd5fb5 | 2018-03-01 23:11:30 +0000 | [diff] [blame] | 155 | case file_magic::coff_cl_gl_object: |
| 156 | error(Filename + ": is not a native COFF file. Recompile without /GL"); |
| 157 | break; |
| 158 | case file_magic::pecoff_executable: |
| 159 | if (Filename.endswith_lower(".dll")) { |
| 160 | error(Filename + ": bad file type. Did you specify a DLL instead of an " |
| 161 | "import library?"); |
| 162 | break; |
| 163 | } |
| 164 | LLVM_FALLTHROUGH; |
| 165 | default: |
| 166 | error(MBRef.getBufferIdentifier() + ": unknown file type"); |
| 167 | break; |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 168 | } |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 171 | void LinkerDriver::enqueuePath(StringRef Path, bool WholeArchive) { |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 172 | auto Future = |
| 173 | std::make_shared<std::future<MBErrPair>>(createFutureForFile(Path)); |
| 174 | std::string PathStr = Path; |
| 175 | enqueueTask([=]() { |
| 176 | auto MBOrErr = Future->get(); |
| 177 | if (MBOrErr.second) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 178 | error("could not open " + PathStr + ": " + MBOrErr.second.message()); |
| 179 | else |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 180 | Driver->addBuffer(std::move(MBOrErr.first), WholeArchive); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 181 | }); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName, |
| 185 | StringRef ParentName) { |
| 186 | file_magic Magic = identify_magic(MB.getBuffer()); |
| 187 | if (Magic == file_magic::coff_import_library) { |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 188 | Symtab->addFile(make<ImportFile>(MB)); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
| 192 | InputFile *Obj; |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 193 | if (Magic == file_magic::coff_object) { |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 194 | Obj = make<ObjFile>(MB); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 195 | } else if (Magic == file_magic::bitcode) { |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 196 | Obj = make<BitcodeFile>(MB); |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 197 | } else { |
| 198 | error("unknown file type: " + MB.getBufferIdentifier()); |
| 199 | return; |
| 200 | } |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 201 | |
| 202 | Obj->ParentName = ParentName; |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 203 | Symtab->addFile(Obj); |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 204 | log("Loaded " + toString(Obj) + " for " + SymName); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void LinkerDriver::enqueueArchiveMember(const Archive::Child &C, |
| 208 | StringRef SymName, |
| 209 | StringRef ParentName) { |
| 210 | if (!C.getParent()->isThin()) { |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 211 | MemoryBufferRef MB = CHECK( |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 212 | C.getMemoryBufferRef(), |
| 213 | "could not get the buffer for the member defining symbol " + SymName); |
| 214 | enqueueTask([=]() { Driver->addArchiveBuffer(MB, SymName, ParentName); }); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | auto Future = std::make_shared<std::future<MBErrPair>>(createFutureForFile( |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 219 | CHECK(C.getFullName(), |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 220 | "could not get the filename for the member defining symbol " + |
| 221 | SymName))); |
| 222 | enqueueTask([=]() { |
| 223 | auto MBOrErr = Future->get(); |
| 224 | if (MBOrErr.second) |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 225 | fatal("could not get the buffer for the member defining " + SymName + |
| 226 | ": " + MBOrErr.second.message()); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 227 | Driver->addArchiveBuffer(takeBuffer(std::move(MBOrErr.first)), SymName, |
| 228 | ParentName); |
| 229 | }); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 232 | static bool isDecorated(StringRef Sym) { |
Martin Storsjo | ddb094a | 2017-10-23 09:08:24 +0000 | [diff] [blame] | 233 | return Sym.startswith("@") || Sym.contains("@@") || Sym.startswith("?") || |
| 234 | (!Config->MinGW && Sym.contains('@')); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 237 | // Parses .drectve section contents and returns a list of files |
| 238 | // specified by /defaultlib. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 239 | void LinkerDriver::parseDirectives(StringRef S) { |
Rui Ueyama | affb40e | 2017-08-28 20:46:30 +0000 | [diff] [blame] | 240 | ArgParser Parser; |
Nico Weber | a05cbb8 | 2017-09-05 23:46:45 +0000 | [diff] [blame] | 241 | // .drectve is always tokenized using Windows shell rules. |
Rui Ueyama | 5fa0d6e | 2018-01-09 20:36:42 +0000 | [diff] [blame] | 242 | // /EXPORT: option can appear too many times, processing in fastpath. |
| 243 | opt::InputArgList Args; |
| 244 | std::vector<StringRef> Exports; |
| 245 | std::tie(Args, Exports) = Parser.parseDirectives(S); |
| 246 | |
| 247 | for (StringRef E : Exports) { |
| 248 | // If a common header file contains dllexported function |
| 249 | // declarations, many object files may end up with having the |
| 250 | // same /EXPORT options. In order to save cost of parsing them, |
| 251 | // we dedup them first. |
| 252 | if (!DirectivesExports.insert(E).second) |
| 253 | continue; |
| 254 | |
| 255 | Export Exp = parseExport(E); |
| 256 | if (Config->Machine == I386 && Config->MinGW) { |
| 257 | if (!isDecorated(Exp.Name)) |
| 258 | Exp.Name = Saver.save("_" + Exp.Name); |
| 259 | if (!Exp.ExtName.empty() && !isDecorated(Exp.ExtName)) |
| 260 | Exp.ExtName = Saver.save("_" + Exp.ExtName); |
| 261 | } |
| 262 | Exp.Directives = true; |
| 263 | Config->Exports.push_back(Exp); |
| 264 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 265 | |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 266 | for (auto *Arg : Args) { |
Rui Ueyama | 38b0f4a | 2017-07-19 20:30:04 +0000 | [diff] [blame] | 267 | switch (Arg->getOption().getUnaliasedOption().getID()) { |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 268 | case OPT_aligncomm: |
| 269 | parseAligncomm(Arg->getValue()); |
| 270 | break; |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 271 | case OPT_alternatename: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 272 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 273 | break; |
| 274 | case OPT_defaultlib: |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 275 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 276 | enqueuePath(*Path, false); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 277 | break; |
Rui Ueyama | 215286f | 2017-11-29 20:46:13 +0000 | [diff] [blame] | 278 | case OPT_entry: |
| 279 | Config->Entry = addUndefined(mangle(Arg->getValue())); |
| 280 | break; |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 281 | case OPT_failifmismatch: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 282 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 283 | break; |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 284 | case OPT_incl: |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 285 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 286 | break; |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 287 | case OPT_merge: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 288 | parseMerge(Arg->getValue()); |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 289 | break; |
| 290 | case OPT_nodefaultlib: |
| 291 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 292 | break; |
Rui Ueyama | 440138c | 2016-06-20 03:39:39 +0000 | [diff] [blame] | 293 | case OPT_section: |
| 294 | parseSection(Arg->getValue()); |
| 295 | break; |
Rui Ueyama | 215286f | 2017-11-29 20:46:13 +0000 | [diff] [blame] | 296 | case OPT_subsystem: |
| 297 | parseSubsystem(Arg->getValue(), &Config->Subsystem, |
| 298 | &Config->MajorOSVersion, &Config->MinorOSVersion); |
| 299 | break; |
Rui Ueyama | 3c4737d | 2015-08-11 16:46:08 +0000 | [diff] [blame] | 300 | case OPT_editandcontinue: |
Reid Kleckner | 9cd77ce | 2016-03-25 18:09:29 +0000 | [diff] [blame] | 301 | case OPT_fastfail: |
Rui Ueyama | 31e66e3 | 2015-09-03 16:20:47 +0000 | [diff] [blame] | 302 | case OPT_guardsym: |
Rui Ueyama | 9d9bdab | 2017-09-11 22:24:13 +0000 | [diff] [blame] | 303 | case OPT_natvis: |
Rui Ueyama | 43238317 | 2015-07-29 21:01:15 +0000 | [diff] [blame] | 304 | case OPT_throwingnew: |
Rui Ueyama | 4668263 | 2015-07-29 20:29:15 +0000 | [diff] [blame] | 305 | break; |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 306 | default: |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 307 | error(Arg->getSpelling() + " is not allowed in .drectve"); |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 312 | // Find file from search paths. You can omit ".obj", this function takes |
| 313 | // 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] | 314 | StringRef LinkerDriver::doFindFile(StringRef Filename) { |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 315 | bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos); |
| 316 | if (HasPathSep) |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 317 | return Filename; |
Rui Ueyama | 12234f8 | 2017-07-19 21:40:26 +0000 | [diff] [blame] | 318 | bool HasExt = Filename.contains('.'); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 319 | for (StringRef Dir : SearchPaths) { |
| 320 | SmallString<128> Path = Dir; |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 321 | sys::path::append(Path, Filename); |
| 322 | if (sys::fs::exists(Path.str())) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 323 | return Saver.save(Path.str()); |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 324 | if (!HasExt) { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 325 | Path.append(".obj"); |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 326 | if (sys::fs::exists(Path.str())) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 327 | return Saver.save(Path.str()); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | return Filename; |
| 331 | } |
| 332 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 333 | // Resolves a file path. This never returns the same path |
| 334 | // (in that case, it returns None). |
| 335 | Optional<StringRef> LinkerDriver::findFile(StringRef Filename) { |
| 336 | StringRef Path = doFindFile(Filename); |
| 337 | bool Seen = !VisitedFiles.insert(Path.lower()).second; |
| 338 | if (Seen) |
| 339 | return None; |
Rui Ueyama | b59ceb1 | 2017-12-11 23:09:18 +0000 | [diff] [blame] | 340 | if (Path.endswith_lower(".lib")) |
| 341 | VisitedLibs.insert(sys::path::filename(Path)); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 342 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 345 | // Find library file from search path. |
| 346 | StringRef LinkerDriver::doFindLib(StringRef Filename) { |
| 347 | // Add ".lib" to Filename if that has no file extension. |
Rui Ueyama | 12234f8 | 2017-07-19 21:40:26 +0000 | [diff] [blame] | 348 | bool HasExt = Filename.contains('.'); |
Rui Ueyama | bf4ddeb | 2016-11-29 04:22:57 +0000 | [diff] [blame] | 349 | if (!HasExt) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 350 | Filename = Saver.save(Filename + ".lib"); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 351 | return doFindFile(Filename); |
| 352 | } |
| 353 | |
| 354 | // Resolves a library path. /nodefaultlib options are taken into |
| 355 | // consideration. This never returns the same path (in that case, |
| 356 | // it returns None). |
| 357 | Optional<StringRef> LinkerDriver::findLib(StringRef Filename) { |
| 358 | if (Config->NoDefaultLibAll) |
| 359 | return None; |
Peter Collingbourne | c1ded7d | 2016-12-16 03:45:59 +0000 | [diff] [blame] | 360 | if (!VisitedLibs.insert(Filename.lower()).second) |
| 361 | return None; |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 362 | StringRef Path = doFindLib(Filename); |
| 363 | if (Config->NoDefaultLibs.count(Path)) |
| 364 | return None; |
Peter Collingbourne | c1ded7d | 2016-12-16 03:45:59 +0000 | [diff] [blame] | 365 | if (!VisitedFiles.insert(Path.lower()).second) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 366 | return None; |
| 367 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Parses LIB environment which contains a list of search paths. |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 371 | void LinkerDriver::addLibSearchPaths() { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 372 | Optional<std::string> EnvOpt = Process::GetEnv("LIB"); |
| 373 | if (!EnvOpt.hasValue()) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 374 | return; |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 375 | StringRef Env = Saver.save(*EnvOpt); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 376 | while (!Env.empty()) { |
| 377 | StringRef Path; |
| 378 | std::tie(Path, Env) = Env.split(';'); |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 379 | SearchPaths.push_back(Path); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 380 | } |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 383 | Symbol *LinkerDriver::addUndefined(StringRef Name) { |
| 384 | Symbol *B = Symtab->addUndefined(Name); |
Reid Kleckner | 5883989 | 2017-11-13 18:38:53 +0000 | [diff] [blame] | 385 | if (!B->IsGCRoot) { |
| 386 | B->IsGCRoot = true; |
| 387 | Config->GCRoot.push_back(B); |
| 388 | } |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 +0000 | [diff] [blame] | 389 | return B; |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 392 | // Symbol names are mangled by appending "_" prefix on x86. |
| 393 | StringRef LinkerDriver::mangle(StringRef Sym) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 394 | assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); |
| 395 | if (Config->Machine == I386) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 396 | return Saver.save("_" + Sym); |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 397 | return Sym; |
| 398 | } |
| 399 | |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 400 | // Windows specific -- find default entry point name. |
| 401 | StringRef LinkerDriver::findDefaultEntry() { |
| 402 | // User-defined main functions and their corresponding entry points. |
| 403 | static const char *Entries[][2] = { |
| 404 | {"main", "mainCRTStartup"}, |
| 405 | {"wmain", "wmainCRTStartup"}, |
| 406 | {"WinMain", "WinMainCRTStartup"}, |
| 407 | {"wWinMain", "wWinMainCRTStartup"}, |
| 408 | }; |
| 409 | for (auto E : Entries) { |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 410 | StringRef Entry = Symtab->findMangle(mangle(E[0])); |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 +0000 | [diff] [blame] | 411 | if (!Entry.empty() && !isa<Undefined>(Symtab->find(Entry))) |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 412 | return mangle(E[1]); |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 413 | } |
| 414 | return ""; |
| 415 | } |
| 416 | |
| 417 | WindowsSubsystem LinkerDriver::inferSubsystem() { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 418 | if (Config->DLL) |
| 419 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 420 | if (Symtab->findUnderscore("main") || Symtab->findUnderscore("wmain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 421 | return IMAGE_SUBSYSTEM_WINDOWS_CUI; |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 422 | if (Symtab->findUnderscore("WinMain") || Symtab->findUnderscore("wWinMain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 423 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
| 424 | return IMAGE_SUBSYSTEM_UNKNOWN; |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 427 | static uint64_t getDefaultImageBase() { |
| 428 | if (Config->is64()) |
| 429 | return Config->DLL ? 0x180000000 : 0x140000000; |
| 430 | return Config->DLL ? 0x10000000 : 0x400000; |
| 431 | } |
| 432 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 433 | static std::string createResponseFile(const opt::InputArgList &Args, |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 434 | ArrayRef<StringRef> FilePaths, |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 435 | ArrayRef<StringRef> SearchPaths) { |
| 436 | SmallString<0> Data; |
| 437 | raw_svector_ostream OS(Data); |
| 438 | |
| 439 | for (auto *Arg : Args) { |
| 440 | switch (Arg->getOption().getID()) { |
| 441 | case OPT_linkrepro: |
| 442 | case OPT_INPUT: |
| 443 | case OPT_defaultlib: |
| 444 | case OPT_libpath: |
Peter Collingbourne | acc3baa | 2017-10-25 05:00:54 +0000 | [diff] [blame] | 445 | case OPT_manifest: |
| 446 | case OPT_manifest_colon: |
| 447 | case OPT_manifestdependency: |
| 448 | case OPT_manifestfile: |
| 449 | case OPT_manifestinput: |
| 450 | case OPT_manifestuac: |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 451 | break; |
| 452 | default: |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 +0000 | [diff] [blame] | 453 | OS << toString(*Arg) << "\n"; |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | for (StringRef Path : SearchPaths) { |
| 458 | std::string RelPath = relativeToRoot(Path); |
| 459 | OS << "/libpath:" << quote(RelPath) << "\n"; |
| 460 | } |
| 461 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 462 | for (StringRef Path : FilePaths) |
| 463 | OS << quote(relativeToRoot(Path)) << "\n"; |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 464 | |
| 465 | return Data.str(); |
| 466 | } |
| 467 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 468 | static unsigned getDefaultDebugType(const opt::InputArgList &Args) { |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 469 | unsigned DebugTypes = static_cast<unsigned>(DebugType::CV); |
| 470 | if (Args.hasArg(OPT_driver)) |
| 471 | DebugTypes |= static_cast<unsigned>(DebugType::PData); |
| 472 | if (Args.hasArg(OPT_profile)) |
| 473 | DebugTypes |= static_cast<unsigned>(DebugType::Fixup); |
| 474 | return DebugTypes; |
| 475 | } |
| 476 | |
| 477 | static unsigned parseDebugType(StringRef Arg) { |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 478 | SmallVector<StringRef, 3> Types; |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 479 | Arg.split(Types, ',', /*KeepEmpty=*/false); |
| 480 | |
| 481 | unsigned DebugTypes = static_cast<unsigned>(DebugType::None); |
| 482 | for (StringRef Type : Types) |
| 483 | DebugTypes |= StringSwitch<unsigned>(Type.lower()) |
| 484 | .Case("cv", static_cast<unsigned>(DebugType::CV)) |
| 485 | .Case("pdata", static_cast<unsigned>(DebugType::PData)) |
Saleem Abdulrasool | b639428 | 2017-02-07 04:28:05 +0000 | [diff] [blame] | 486 | .Case("fixup", static_cast<unsigned>(DebugType::Fixup)) |
| 487 | .Default(0); |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 488 | return DebugTypes; |
| 489 | } |
| 490 | |
Hans Wennborg | 1818e65 | 2016-12-09 20:54:44 +0000 | [diff] [blame] | 491 | static std::string getMapFile(const opt::InputArgList &Args) { |
| 492 | auto *Arg = Args.getLastArg(OPT_lldmap, OPT_lldmap_file); |
| 493 | if (!Arg) |
| 494 | return ""; |
| 495 | if (Arg->getOption().getID() == OPT_lldmap_file) |
| 496 | return Arg->getValue(); |
| 497 | |
| 498 | assert(Arg->getOption().getID() == OPT_lldmap); |
| 499 | StringRef OutFile = Config->OutputFile; |
| 500 | return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str(); |
| 501 | } |
| 502 | |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 503 | static std::string getImplibPath() { |
| 504 | if (!Config->Implib.empty()) |
| 505 | return Config->Implib; |
| 506 | SmallString<128> Out = StringRef(Config->OutputFile); |
| 507 | sys::path::replace_extension(Out, ".lib"); |
| 508 | return Out.str(); |
| 509 | } |
| 510 | |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 511 | // |
| 512 | // The import name is caculated as the following: |
| 513 | // |
| 514 | // | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY |
| 515 | // -----+----------------+---------------------+------------------ |
| 516 | // LINK | {value} | {value}.{.dll/.exe} | {output name} |
| 517 | // LIB | {value} | {value}.dll | {output name}.dll |
| 518 | // |
| 519 | static std::string getImportName(bool AsLib) { |
| 520 | SmallString<128> Out; |
| 521 | |
| 522 | if (Config->ImportName.empty()) { |
| 523 | Out.assign(sys::path::filename(Config->OutputFile)); |
| 524 | if (AsLib) |
| 525 | sys::path::replace_extension(Out, ".dll"); |
| 526 | } else { |
| 527 | Out.assign(Config->ImportName); |
| 528 | if (!sys::path::has_extension(Out)) |
| 529 | sys::path::replace_extension(Out, |
| 530 | (Config->DLL || AsLib) ? ".dll" : ".exe"); |
| 531 | } |
| 532 | |
| 533 | return Out.str(); |
| 534 | } |
| 535 | |
| 536 | static void createImportLibrary(bool AsLib) { |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 537 | std::vector<COFFShortExport> Exports; |
| 538 | for (Export &E1 : Config->Exports) { |
| 539 | COFFShortExport E2; |
Martin Storsjo | a50275cf | 2017-08-16 05:13:25 +0000 | [diff] [blame] | 540 | E2.Name = E1.Name; |
| 541 | E2.SymbolName = E1.SymbolName; |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 542 | E2.ExtName = E1.ExtName; |
| 543 | E2.Ordinal = E1.Ordinal; |
| 544 | E2.Noname = E1.Noname; |
| 545 | E2.Data = E1.Data; |
| 546 | E2.Private = E1.Private; |
| 547 | E2.Constant = E1.Constant; |
| 548 | Exports.push_back(E2); |
| 549 | } |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 550 | |
Bob Haarman | 4ce341f | 2018-01-23 00:36:42 +0000 | [diff] [blame] | 551 | auto HandleError = [](Error &&E) { |
| 552 | handleAllErrors(std::move(E), |
| 553 | [](ErrorInfoBase &EIB) { error(EIB.message()); }); |
| 554 | }; |
| 555 | std::string LibName = getImportName(AsLib); |
| 556 | std::string Path = getImplibPath(); |
| 557 | |
Bob Haarman | 5ec4485 | 2018-01-31 23:44:00 +0000 | [diff] [blame] | 558 | if (!Config->Incremental) { |
| 559 | HandleError(writeImportLibrary(LibName, Path, Exports, Config->Machine, |
| 560 | false, Config->MinGW)); |
| 561 | return; |
| 562 | } |
| 563 | |
Bob Haarman | 4ce341f | 2018-01-23 00:36:42 +0000 | [diff] [blame] | 564 | // If the import library already exists, replace it only if the contents |
| 565 | // have changed. |
| 566 | ErrorOr<std::unique_ptr<MemoryBuffer>> OldBuf = MemoryBuffer::getFile(Path); |
| 567 | if (!OldBuf) { |
| 568 | HandleError(writeImportLibrary(LibName, Path, Exports, Config->Machine, |
| 569 | false, Config->MinGW)); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | SmallString<128> TmpName; |
| 574 | if (std::error_code EC = |
| 575 | sys::fs::createUniqueFile(Path + ".tmp-%%%%%%%%.lib", TmpName)) |
| 576 | fatal("cannot create temporary file for import library " + Path + ": " + |
| 577 | EC.message()); |
| 578 | |
| 579 | if (Error E = writeImportLibrary(LibName, TmpName, Exports, Config->Machine, |
| 580 | false, Config->MinGW)) { |
| 581 | HandleError(std::move(E)); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | std::unique_ptr<MemoryBuffer> NewBuf = check(MemoryBuffer::getFile(TmpName)); |
| 586 | if ((*OldBuf)->getBuffer() != NewBuf->getBuffer()) { |
| 587 | OldBuf->reset(); |
| 588 | HandleError(errorCodeToError(sys::fs::rename(TmpName, Path))); |
Martin Storsjo | cf47b04 | 2018-01-30 07:26:01 +0000 | [diff] [blame] | 589 | } else { |
| 590 | sys::fs::remove(TmpName); |
Bob Haarman | 4ce341f | 2018-01-23 00:36:42 +0000 | [diff] [blame] | 591 | } |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static void parseModuleDefs(StringRef Path) { |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 595 | std::unique_ptr<MemoryBuffer> MB = CHECK( |
| 596 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
Martin Storsjo | ddb094a | 2017-10-23 09:08:24 +0000 | [diff] [blame] | 597 | COFFModuleDefinition M = check(parseCOFFModuleDefinition( |
| 598 | MB->getMemBufferRef(), Config->Machine, Config->MinGW)); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 599 | |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 600 | if (Config->OutputFile.empty()) |
| 601 | Config->OutputFile = Saver.save(M.OutputFile); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 602 | Config->ImportName = Saver.save(M.ImportName); |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 603 | if (M.ImageBase) |
| 604 | Config->ImageBase = M.ImageBase; |
| 605 | if (M.StackReserve) |
| 606 | Config->StackReserve = M.StackReserve; |
| 607 | if (M.StackCommit) |
| 608 | Config->StackCommit = M.StackCommit; |
| 609 | if (M.HeapReserve) |
| 610 | Config->HeapReserve = M.HeapReserve; |
| 611 | if (M.HeapCommit) |
| 612 | Config->HeapCommit = M.HeapCommit; |
| 613 | if (M.MajorImageVersion) |
| 614 | Config->MajorImageVersion = M.MajorImageVersion; |
| 615 | if (M.MinorImageVersion) |
| 616 | Config->MinorImageVersion = M.MinorImageVersion; |
| 617 | if (M.MajorOSVersion) |
| 618 | Config->MajorOSVersion = M.MajorOSVersion; |
| 619 | if (M.MinorOSVersion) |
| 620 | Config->MinorOSVersion = M.MinorOSVersion; |
| 621 | |
| 622 | for (COFFShortExport E1 : M.Exports) { |
| 623 | Export E2; |
| 624 | E2.Name = Saver.save(E1.Name); |
| 625 | if (E1.isWeak()) |
| 626 | E2.ExtName = Saver.save(E1.ExtName); |
| 627 | E2.Ordinal = E1.Ordinal; |
| 628 | E2.Noname = E1.Noname; |
| 629 | E2.Data = E1.Data; |
| 630 | E2.Private = E1.Private; |
| 631 | E2.Constant = E1.Constant; |
| 632 | Config->Exports.push_back(E2); |
| 633 | } |
| 634 | } |
| 635 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 636 | // A helper function for filterBitcodeFiles. |
| 637 | static bool needsRebuilding(MemoryBufferRef MB) { |
| 638 | // The MSVC linker doesn't support thin archives, so if it's a thin |
| 639 | // archive, we always need to rebuild it. |
| 640 | std::unique_ptr<Archive> File = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 641 | CHECK(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier()); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 642 | if (File->isThin()) |
| 643 | return true; |
| 644 | |
| 645 | // Returns true if the archive contains at least one bitcode file. |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 646 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 647 | if (identify_magic(Member.getBuffer()) == file_magic::bitcode) |
| 648 | return true; |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | // Opens a given path as an archive file and removes bitcode files |
| 653 | // from them if exists. This function is to appease the MSVC linker as |
| 654 | // their linker doesn't like archive files containing non-native |
| 655 | // object files. |
| 656 | // |
| 657 | // If a given archive doesn't contain bitcode files, the archive path |
| 658 | // is returned as-is. Otherwise, a new temporary file is created and |
| 659 | // its path is returned. |
| 660 | static Optional<std::string> |
| 661 | filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) { |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 662 | std::unique_ptr<MemoryBuffer> MB = CHECK( |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 663 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 664 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 665 | file_magic Magic = identify_magic(MBRef.getBuffer()); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 666 | |
| 667 | if (Magic == file_magic::bitcode) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 668 | return None; |
| 669 | if (Magic != file_magic::archive) |
| 670 | return Path.str(); |
| 671 | if (!needsRebuilding(MBRef)) |
| 672 | return Path.str(); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 673 | |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 674 | std::unique_ptr<Archive> File = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 675 | CHECK(Archive::create(MBRef), |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 676 | MBRef.getBufferIdentifier() + ": failed to parse archive"); |
| 677 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 678 | std::vector<NewArchiveMember> New; |
Peter Collingbourne | a6ffbdd | 2017-03-17 02:03:20 +0000 | [diff] [blame] | 679 | for (MemoryBufferRef Member : getArchiveMembers(File.get())) |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 680 | if (identify_magic(Member.getBuffer()) != file_magic::bitcode) |
| 681 | New.emplace_back(Member); |
Rui Ueyama | e0341db | 2017-03-07 19:45:53 +0000 | [diff] [blame] | 682 | |
Peter Collingbourne | db7447d | 2017-03-17 02:04:22 +0000 | [diff] [blame] | 683 | if (New.empty()) |
| 684 | return None; |
| 685 | |
| 686 | log("Creating a temporary archive for " + Path + " to remove bitcode files"); |
| 687 | |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 688 | SmallString<128> S; |
Bob Haarman | 4ce341f | 2018-01-23 00:36:42 +0000 | [diff] [blame] | 689 | if (std::error_code EC = sys::fs::createTemporaryFile( |
| 690 | "lld-" + sys::path::stem(Path), ".lib", S)) |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 691 | fatal("cannot create a temporary file: " + EC.message()); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 692 | std::string Temp = S.str(); |
| 693 | TemporaryFiles.push_back(Temp); |
| 694 | |
Rafael Espindola | 474f2bd | 2017-09-21 23:13:40 +0000 | [diff] [blame] | 695 | Error E = |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 696 | llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU, |
| 697 | /*Deterministics=*/true, |
| 698 | /*Thin=*/false); |
Rafael Espindola | 474f2bd | 2017-09-21 23:13:40 +0000 | [diff] [blame] | 699 | handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { |
| 700 | error("failed to create a new archive " + S.str() + ": " + EI.message()); |
| 701 | }); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 702 | return Temp; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Create response file contents and invoke the MSVC linker. |
| 706 | void LinkerDriver::invokeMSVC(opt::InputArgList &Args) { |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 707 | std::string Rsp = "/nologo\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 708 | std::vector<std::string> Temps; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 709 | |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 710 | // Write out archive members that we used in symbol resolution and pass these |
| 711 | // to MSVC before any archives, so that MSVC uses the same objects to satisfy |
| 712 | // references. |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 713 | for (ObjFile *Obj : ObjFile::Instances) { |
| 714 | if (Obj->ParentName.empty()) |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 715 | continue; |
| 716 | SmallString<128> S; |
| 717 | int Fd; |
| 718 | if (auto EC = sys::fs::createTemporaryFile( |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 719 | "lld-" + sys::path::filename(Obj->ParentName), ".obj", Fd, S)) |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 720 | fatal("cannot create a temporary file: " + EC.message()); |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 721 | raw_fd_ostream OS(Fd, /*shouldClose*/ true); |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 722 | OS << Obj->MB.getBuffer(); |
Bob Haarman | 4110816 | 2017-04-21 21:38:01 +0000 | [diff] [blame] | 723 | Temps.push_back(S.str()); |
| 724 | Rsp += quote(S) + "\n"; |
| 725 | } |
| 726 | |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 727 | for (auto *Arg : Args) { |
| 728 | switch (Arg->getOption().getID()) { |
| 729 | case OPT_linkrepro: |
| 730 | case OPT_lldmap: |
| 731 | case OPT_lldmap_file: |
Peter Collingbourne | 8713bf6 | 2017-03-17 02:11:09 +0000 | [diff] [blame] | 732 | case OPT_lldsavetemps: |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 733 | case OPT_msvclto: |
| 734 | // LLD-specific options are stripped. |
| 735 | break; |
| 736 | case OPT_opt: |
| 737 | if (!StringRef(Arg->getValue()).startswith("lld")) |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 +0000 | [diff] [blame] | 738 | Rsp += toString(*Arg) + " "; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 739 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 740 | case OPT_INPUT: { |
| 741 | if (Optional<StringRef> Path = doFindFile(Arg->getValue())) { |
| 742 | if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps)) |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 743 | Rsp += quote(*S) + "\n"; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 744 | continue; |
| 745 | } |
Bob Haarman | 630d0c0 | 2017-04-18 22:00:29 +0000 | [diff] [blame] | 746 | Rsp += quote(Arg->getValue()) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 747 | break; |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 748 | } |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 749 | default: |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 +0000 | [diff] [blame] | 750 | Rsp += toString(*Arg) + "\n"; |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 754 | std::vector<StringRef> ObjFiles = Symtab->compileBitcodeFiles(); |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 +0000 | [diff] [blame] | 755 | runMSVCLinker(Rsp, ObjFiles); |
Rui Ueyama | e1bf136 | 2017-03-16 21:19:36 +0000 | [diff] [blame] | 756 | |
| 757 | for (StringRef Path : Temps) |
| 758 | sys::fs::remove(Path); |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 761 | void LinkerDriver::enqueueTask(std::function<void()> Task) { |
| 762 | TaskQueue.push_back(std::move(Task)); |
| 763 | } |
| 764 | |
| 765 | bool LinkerDriver::run() { |
Zachary Turner | 727f153 | 2018-01-17 19:16:26 +0000 | [diff] [blame] | 766 | ScopedTimer T(InputFileTimer); |
| 767 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 768 | bool DidWork = !TaskQueue.empty(); |
| 769 | while (!TaskQueue.empty()) { |
| 770 | TaskQueue.front()(); |
| 771 | TaskQueue.pop_front(); |
| 772 | } |
| 773 | return DidWork; |
| 774 | } |
| 775 | |
Rui Ueyama | 57175aa | 2018-01-27 00:34:46 +0000 | [diff] [blame] | 776 | // Parse an /order file. If an option is given, the linker places |
| 777 | // COMDAT sections in the same order as their names appear in the |
| 778 | // given file. |
| 779 | static void parseOrderFile(StringRef Arg) { |
| 780 | // For some reason, the MSVC linker requires a filename to be |
| 781 | // preceded by "@". |
| 782 | if (!Arg.startswith("@")) { |
| 783 | error("malformed /order option: '@' missing"); |
| 784 | return; |
| 785 | } |
| 786 | |
Rui Ueyama | b6d3a93 | 2018-01-29 21:50:53 +0000 | [diff] [blame] | 787 | // Get a list of all comdat sections for error checking. |
| 788 | DenseSet<StringRef> Set; |
| 789 | for (Chunk *C : Symtab->getChunks()) |
| 790 | if (auto *Sec = dyn_cast<SectionChunk>(C)) |
| 791 | if (Sec->Sym) |
| 792 | Set.insert(Sec->Sym->getName()); |
| 793 | |
Rui Ueyama | 57175aa | 2018-01-27 00:34:46 +0000 | [diff] [blame] | 794 | // Open a file. |
| 795 | StringRef Path = Arg.substr(1); |
| 796 | std::unique_ptr<MemoryBuffer> MB = CHECK( |
| 797 | MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); |
| 798 | |
| 799 | // Parse a file. An order file contains one symbol per line. |
| 800 | // All symbols that were not present in a given order file are |
| 801 | // considered to have the lowest priority 0 and are placed at |
| 802 | // end of an output section. |
| 803 | for (std::string S : args::getLines(MB->getMemBufferRef())) { |
| 804 | if (Config->Machine == I386 && !isDecorated(S)) |
| 805 | S = "_" + S; |
Rui Ueyama | b6d3a93 | 2018-01-29 21:50:53 +0000 | [diff] [blame] | 806 | |
Nico Weber | 0771c60 | 2018-03-09 12:41:04 +0000 | [diff] [blame^] | 807 | if (Set.count(S) == 0) { |
| 808 | if (Config->WarnMissingOrderSymbol) |
| 809 | warn("/order:" + Arg + ": missing symbol: " + S); |
| 810 | } |
Rui Ueyama | b6d3a93 | 2018-01-29 21:50:53 +0000 | [diff] [blame] | 811 | else |
| 812 | Config->Order[S] = INT_MIN + Config->Order.size(); |
Rui Ueyama | 57175aa | 2018-01-27 00:34:46 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 816 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 817 | // If the first command line argument is "/lib", link.exe acts like lib.exe. |
| 818 | // We call our own implementation of lib.exe that understands bitcode files. |
| 819 | if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) { |
| 820 | if (llvm::libDriverMain(ArgsArr.slice(1)) != 0) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 821 | fatal("lib failed"); |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 825 | // Needed for LTO. |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 826 | InitializeAllTargetInfos(); |
| 827 | InitializeAllTargets(); |
| 828 | InitializeAllTargetMCs(); |
| 829 | InitializeAllAsmParsers(); |
| 830 | InitializeAllAsmPrinters(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 831 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 832 | // Parse command line options. |
Rui Ueyama | affb40e | 2017-08-28 20:46:30 +0000 | [diff] [blame] | 833 | ArgParser Parser; |
Rui Ueyama | 8fe1767 | 2016-12-08 20:50:47 +0000 | [diff] [blame] | 834 | opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 835 | |
Rui Ueyama | 9a3e733 | 2017-03-30 20:10:40 +0000 | [diff] [blame] | 836 | // Parse and evaluate -mllvm options. |
| 837 | std::vector<const char *> V; |
| 838 | V.push_back("lld-link (LLVM option parsing)"); |
| 839 | for (auto *Arg : Args.filtered(OPT_mllvm)) |
| 840 | V.push_back(Arg->getValue()); |
| 841 | cl::ParseCommandLineOptions(V.size(), V.data()); |
| 842 | |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 843 | // Handle /errorlimit early, because error() depends on it. |
| 844 | if (auto *Arg = Args.getLastArg(OPT_errorlimit)) { |
| 845 | int N = 20; |
| 846 | StringRef S = Arg->getValue(); |
| 847 | if (S.getAsInteger(10, N)) |
| 848 | error(Arg->getSpelling() + " number expected, but got " + S); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 849 | errorHandler().ErrorLimit = N; |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 852 | // Handle /help |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 853 | if (Args.hasArg(OPT_help)) { |
David Blaikie | b2b1c7c | 2015-06-21 06:32:10 +0000 | [diff] [blame] | 854 | printHelp(ArgsArr[0]); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 855 | return; |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Zachary Turner | 727f153 | 2018-01-17 19:16:26 +0000 | [diff] [blame] | 858 | if (Args.hasArg(OPT_show_timing)) |
| 859 | Config->ShowTiming = true; |
| 860 | |
| 861 | ScopedTimer T(Timer::root()); |
Rui Ueyama | a4cf97b | 2017-10-23 14:57:53 +0000 | [diff] [blame] | 862 | // Handle --version, which is an lld extension. This option is a bit odd |
| 863 | // because it doesn't start with "/", but we deliberately chose "--" to |
| 864 | // avoid conflict with /version and for compatibility with clang-cl. |
| 865 | if (Args.hasArg(OPT_dash_dash_version)) { |
| 866 | outs() << getLLDVersion() << "\n"; |
| 867 | return; |
| 868 | } |
| 869 | |
Martin Storsjo | 31fe4cd | 2017-09-13 19:29:39 +0000 | [diff] [blame] | 870 | // Handle /lldmingw early, since it can potentially affect how other |
| 871 | // options are handled. |
| 872 | Config->MinGW = Args.hasArg(OPT_lldmingw); |
| 873 | |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 874 | if (auto *Arg = Args.getLastArg(OPT_linkrepro)) { |
| 875 | SmallString<64> Path = StringRef(Arg->getValue()); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 876 | sys::path::append(Path, "repro.tar"); |
| 877 | |
| 878 | Expected<std::unique_ptr<TarWriter>> ErrOrWriter = |
| 879 | TarWriter::create(Path, "repro"); |
| 880 | |
| 881 | if (ErrOrWriter) { |
| 882 | Tar = std::move(*ErrOrWriter); |
| 883 | } else { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 884 | error("/linkrepro: failed to open " + Path + ": " + |
| 885 | toString(ErrOrWriter.takeError())); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 886 | } |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Rui Ueyama | a835bab | 2017-09-13 20:30:59 +0000 | [diff] [blame] | 889 | if (!Args.hasArg(OPT_INPUT)) { |
| 890 | if (Args.hasArg(OPT_deffile)) |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 891 | Config->NoEntry = true; |
| 892 | else |
| 893 | fatal("no input files"); |
| 894 | } |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 895 | |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 896 | // Construct search path list. |
| 897 | SearchPaths.push_back(""); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 898 | for (auto *Arg : Args.filtered(OPT_libpath)) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 899 | SearchPaths.push_back(Arg->getValue()); |
| 900 | addLibSearchPaths(); |
| 901 | |
Bob Haarman | e90ac01 | 2017-12-28 07:02:13 +0000 | [diff] [blame] | 902 | // Handle /ignore |
| 903 | for (auto *Arg : Args.filtered(OPT_ignore)) { |
Nico Weber | 0771c60 | 2018-03-09 12:41:04 +0000 | [diff] [blame^] | 904 | if (StringRef(Arg->getValue()) == "4037") |
| 905 | Config->WarnMissingOrderSymbol = false; |
| 906 | else if (StringRef(Arg->getValue()) == "4217") |
Bob Haarman | e90ac01 | 2017-12-28 07:02:13 +0000 | [diff] [blame] | 907 | Config->WarnLocallyDefinedImported = false; |
| 908 | // Other warning numbers are ignored. |
| 909 | } |
| 910 | |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 911 | // Handle /out |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 912 | if (auto *Arg = Args.getLastArg(OPT_out)) |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 913 | Config->OutputFile = Arg->getValue(); |
| 914 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 915 | // Handle /verbose |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 916 | if (Args.hasArg(OPT_verbose)) |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 917 | Config->Verbose = true; |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 918 | errorHandler().Verbose = Config->Verbose; |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 919 | |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 920 | // Handle /force or /force:unresolved |
Shoaib Meenai | 111db79 | 2017-12-15 23:51:14 +0000 | [diff] [blame] | 921 | if (Args.hasArg(OPT_force, OPT_force_unresolved)) |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 922 | Config->Force = true; |
| 923 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 924 | // Handle /debug |
Shoaib Meenai | 111db79 | 2017-12-15 23:51:14 +0000 | [diff] [blame] | 925 | if (Args.hasArg(OPT_debug, OPT_debug_dwarf, OPT_debug_ghash)) { |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 926 | Config->Debug = true; |
Bob Haarman | 5ec4485 | 2018-01-31 23:44:00 +0000 | [diff] [blame] | 927 | Config->Incremental = true; |
Rui Ueyama | 9f7032a | 2017-08-24 20:26:54 +0000 | [diff] [blame] | 928 | if (auto *Arg = Args.getLastArg(OPT_debugtype)) |
| 929 | Config->DebugTypes = parseDebugType(Arg->getValue()); |
| 930 | else |
| 931 | Config->DebugTypes = getDefaultDebugType(Args); |
Saleem Abdulrasool | a2cca7e | 2016-08-08 22:02:44 +0000 | [diff] [blame] | 932 | } |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 933 | |
Shoaib Meenai | c4fdbca | 2017-12-15 23:52:46 +0000 | [diff] [blame] | 934 | // Handle /pdb |
Shoaib Meenai | a1f6fba | 2017-12-16 00:23:24 +0000 | [diff] [blame] | 935 | bool ShouldCreatePDB = Args.hasArg(OPT_debug, OPT_debug_ghash); |
| 936 | if (ShouldCreatePDB) |
| 937 | if (auto *Arg = Args.getLastArg(OPT_pdb)) |
| 938 | Config->PDBPath = Arg->getValue(); |
Saleem Abdulrasool | 8fcff93 | 2016-08-29 21:20:46 +0000 | [diff] [blame] | 939 | |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 940 | // Handle /noentry |
| 941 | if (Args.hasArg(OPT_noentry)) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 942 | if (Args.hasArg(OPT_dll)) |
| 943 | Config->NoEntry = true; |
| 944 | else |
| 945 | error("/noentry must be specified with /dll"); |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 948 | // Handle /dll |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 949 | if (Args.hasArg(OPT_dll)) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 950 | Config->DLL = true; |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 951 | Config->ManifestID = 2; |
| 952 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 953 | |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 954 | // Handle /dynamicbase and /fixed. We can't use hasFlag for /dynamicbase |
| 955 | // because we need to explicitly check whether that option or its inverse was |
| 956 | // present in the argument list in order to handle /fixed. |
| 957 | auto *DynamicBaseArg = Args.getLastArg(OPT_dynamicbase, OPT_dynamicbase_no); |
| 958 | if (DynamicBaseArg && |
| 959 | DynamicBaseArg->getOption().getID() == OPT_dynamicbase_no) |
| 960 | Config->DynamicBase = false; |
| 961 | |
| 962 | bool Fixed = Args.hasFlag(OPT_fixed, OPT_fixed_no, false); |
| 963 | if (Fixed) { |
| 964 | if (DynamicBaseArg && |
| 965 | DynamicBaseArg->getOption().getID() == OPT_dynamicbase) { |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 966 | error("/fixed must not be specified with /dynamicbase"); |
| 967 | } else { |
| 968 | Config->Relocatable = false; |
| 969 | Config->DynamicBase = false; |
| 970 | } |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 971 | } |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 972 | |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 973 | // Handle /appcontainer |
| 974 | Config->AppContainer = |
| 975 | Args.hasFlag(OPT_appcontainer, OPT_appcontainer_no, false); |
Saleem Abdulrasool | 671029d | 2017-04-06 23:07:53 +0000 | [diff] [blame] | 976 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 977 | // Handle /machine |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 978 | if (auto *Arg = Args.getLastArg(OPT_machine)) |
| 979 | Config->Machine = getMachineType(Arg->getValue()); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 980 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 981 | // Handle /nodefaultlib:<filename> |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 982 | for (auto *Arg : Args.filtered(OPT_nodefaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 983 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 984 | |
| 985 | // Handle /nodefaultlib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 986 | if (Args.hasArg(OPT_nodefaultlib_all)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 987 | Config->NoDefaultLibAll = true; |
| 988 | |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 989 | // Handle /base |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 990 | if (auto *Arg = Args.getLastArg(OPT_base)) |
| 991 | parseNumbers(Arg->getValue(), &Config->ImageBase); |
Rui Ueyama | b41b7e5 | 2015-05-29 16:21:11 +0000 | [diff] [blame] | 992 | |
| 993 | // Handle /stack |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 994 | if (auto *Arg = Args.getLastArg(OPT_stack)) |
| 995 | parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit); |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 996 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 +0000 | [diff] [blame] | 997 | // Handle /guard:cf |
| 998 | if (auto *Arg = Args.getLastArg(OPT_guard)) |
| 999 | parseGuard(Arg->getValue()); |
| 1000 | |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 1001 | // Handle /heap |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1002 | if (auto *Arg = Args.getLastArg(OPT_heap)) |
| 1003 | parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit); |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 1004 | |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 1005 | // Handle /version |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1006 | if (auto *Arg = Args.getLastArg(OPT_version)) |
| 1007 | parseVersion(Arg->getValue(), &Config->MajorImageVersion, |
| 1008 | &Config->MinorImageVersion); |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 1009 | |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 1010 | // Handle /subsystem |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1011 | if (auto *Arg = Args.getLastArg(OPT_subsystem)) |
| 1012 | parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion, |
| 1013 | &Config->MinorOSVersion); |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 1014 | |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 1015 | // Handle /alternatename |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1016 | for (auto *Arg : Args.filtered(OPT_alternatename)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1017 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 1018 | |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 1019 | // Handle /include |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1020 | for (auto *Arg : Args.filtered(OPT_incl)) |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 1021 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 1022 | |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 1023 | // Handle /implib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1024 | if (auto *Arg = Args.getLastArg(OPT_implib)) |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 1025 | Config->Implib = Arg->getValue(); |
| 1026 | |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1027 | // Handle /opt. |
| 1028 | bool DoGC = !Args.hasArg(OPT_debug); |
| 1029 | unsigned ICFLevel = 1; // 0: off, 1: limited, 2: on |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1030 | for (auto *Arg : Args.filtered(OPT_opt)) { |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 1031 | std::string Str = StringRef(Arg->getValue()).lower(); |
| 1032 | SmallVector<StringRef, 1> Vec; |
| 1033 | StringRef(Str).split(Vec, ','); |
| 1034 | for (StringRef S : Vec) { |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1035 | if (S == "ref") { |
| 1036 | DoGC = true; |
| 1037 | } else if (S == "noref") { |
| 1038 | DoGC = false; |
| 1039 | } else if (S == "icf" || S.startswith("icf=")) { |
| 1040 | ICFLevel = 2; |
| 1041 | } else if (S == "noicf") { |
| 1042 | ICFLevel = 0; |
| 1043 | } else if (S.startswith("lldlto=")) { |
Peter Collingbourne | cef8099 | 2017-09-07 23:49:09 +0000 | [diff] [blame] | 1044 | StringRef OptLevel = S.substr(7); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 1045 | if (OptLevel.getAsInteger(10, Config->LTOOptLevel) || |
| 1046 | Config->LTOOptLevel > 3) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1047 | error("/opt:lldlto: invalid optimization level: " + OptLevel); |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1048 | } else if (S.startswith("lldltojobs=")) { |
Peter Collingbourne | cef8099 | 2017-09-07 23:49:09 +0000 | [diff] [blame] | 1049 | StringRef Jobs = S.substr(11); |
Rui Ueyama | 75656ee | 2015-10-19 19:40:43 +0000 | [diff] [blame] | 1050 | if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1051 | error("/opt:lldltojobs: invalid job count: " + Jobs); |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1052 | } else if (S.startswith("lldltopartitions=")) { |
Peter Collingbourne | cef8099 | 2017-09-07 23:49:09 +0000 | [diff] [blame] | 1053 | StringRef N = S.substr(17); |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 +0000 | [diff] [blame] | 1054 | if (N.getAsInteger(10, Config->LTOPartitions) || |
| 1055 | Config->LTOPartitions == 0) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1056 | error("/opt:lldltopartitions: invalid partition count: " + N); |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1057 | } else if (S != "lbr" && S != "nolbr") |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1058 | error("/opt: unknown option: " + S); |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 1059 | } |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Reid Kleckner | c2dcdd8 | 2017-11-13 18:38:25 +0000 | [diff] [blame] | 1062 | // Limited ICF is enabled if GC is enabled and ICF was never mentioned |
| 1063 | // explicitly. |
| 1064 | // FIXME: LLD only implements "limited" ICF, i.e. it only merges identical |
| 1065 | // code. If the user passes /OPT:ICF explicitly, LLD should merge identical |
| 1066 | // comdat readonly data. |
| 1067 | if (ICFLevel == 1 && !DoGC) |
| 1068 | ICFLevel = 0; |
| 1069 | Config->DoGC = DoGC; |
| 1070 | Config->DoICF = ICFLevel > 0; |
| 1071 | |
Bob Haarman | 69b196d | 2017-02-08 18:36:41 +0000 | [diff] [blame] | 1072 | // Handle /lldsavetemps |
| 1073 | if (Args.hasArg(OPT_lldsavetemps)) |
| 1074 | Config->SaveTemps = true; |
| 1075 | |
Peter Collingbourne | 052e855e | 2017-09-08 00:50:50 +0000 | [diff] [blame] | 1076 | // Handle /lldltocache |
| 1077 | if (auto *Arg = Args.getLastArg(OPT_lldltocache)) |
| 1078 | Config->LTOCache = Arg->getValue(); |
| 1079 | |
| 1080 | // Handle /lldsavecachepolicy |
| 1081 | if (auto *Arg = Args.getLastArg(OPT_lldltocachepolicy)) |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 +0000 | [diff] [blame] | 1082 | Config->LTOCachePolicy = CHECK( |
Peter Collingbourne | 052e855e | 2017-09-08 00:50:50 +0000 | [diff] [blame] | 1083 | parseCachePruningPolicy(Arg->getValue()), |
| 1084 | Twine("/lldltocachepolicy: invalid cache policy: ") + Arg->getValue()); |
| 1085 | |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 1086 | // Handle /failifmismatch |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1087 | for (auto *Arg : Args.filtered(OPT_failifmismatch)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1088 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 1089 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 1090 | // Handle /merge |
| 1091 | for (auto *Arg : Args.filtered(OPT_merge)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1092 | parseMerge(Arg->getValue()); |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 1093 | |
Rui Ueyama | 440138c | 2016-06-20 03:39:39 +0000 | [diff] [blame] | 1094 | // Handle /section |
| 1095 | for (auto *Arg : Args.filtered(OPT_section)) |
| 1096 | parseSection(Arg->getValue()); |
| 1097 | |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 1098 | // Handle /aligncomm |
| 1099 | for (auto *Arg : Args.filtered(OPT_aligncomm)) |
| 1100 | parseAligncomm(Arg->getValue()); |
| 1101 | |
Nico Weber | a7a2c44 | 2017-07-25 18:08:03 +0000 | [diff] [blame] | 1102 | // Handle /manifestdependency. This enables /manifest unless /manifest:no is |
| 1103 | // also passed. |
| 1104 | if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) { |
| 1105 | Config->ManifestDependency = Arg->getValue(); |
| 1106 | Config->Manifest = Configuration::SideBySide; |
| 1107 | } |
| 1108 | |
| 1109 | // Handle /manifest and /manifest: |
| 1110 | if (auto *Arg = Args.getLastArg(OPT_manifest, OPT_manifest_colon)) { |
| 1111 | if (Arg->getOption().getID() == OPT_manifest) |
| 1112 | Config->Manifest = Configuration::SideBySide; |
| 1113 | else |
| 1114 | parseManifest(Arg->getValue()); |
| 1115 | } |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Handle /manifestuac |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1118 | if (auto *Arg = Args.getLastArg(OPT_manifestuac)) |
| 1119 | parseManifestUAC(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1120 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1121 | // Handle /manifestfile |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1122 | if (auto *Arg = Args.getLastArg(OPT_manifestfile)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1123 | Config->ManifestFile = Arg->getValue(); |
| 1124 | |
Rui Ueyama | afb1901 | 2016-04-19 01:21:58 +0000 | [diff] [blame] | 1125 | // Handle /manifestinput |
| 1126 | for (auto *Arg : Args.filtered(OPT_manifestinput)) |
| 1127 | Config->ManifestInput.push_back(Arg->getValue()); |
| 1128 | |
Nico Weber | a7a2c44 | 2017-07-25 18:08:03 +0000 | [diff] [blame] | 1129 | if (!Config->ManifestInput.empty() && |
| 1130 | Config->Manifest != Configuration::Embed) { |
| 1131 | fatal("/MANIFESTINPUT: requires /MANIFEST:EMBED"); |
| 1132 | } |
| 1133 | |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 1134 | // Handle miscellaneous boolean flags. |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 1135 | Config->AllowBind = Args.hasFlag(OPT_allowbind, OPT_allowbind_no, true); |
| 1136 | Config->AllowIsolation = |
| 1137 | Args.hasFlag(OPT_allowisolation, OPT_allowisolation_no, true); |
Bob Haarman | 5ec4485 | 2018-01-31 23:44:00 +0000 | [diff] [blame] | 1138 | Config->Incremental = |
| 1139 | Args.hasFlag(OPT_incremental, OPT_incremental_no, |
| 1140 | !Config->DoGC && !Config->DoICF && !Args.hasArg(OPT_order)); |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 1141 | Config->NxCompat = Args.hasFlag(OPT_nxcompat, OPT_nxcompat_no, true); |
| 1142 | Config->TerminalServerAware = Args.hasFlag(OPT_tsaware, OPT_tsaware_no, true); |
Peter Collingbourne | f874bd6 | 2017-11-21 01:14:14 +0000 | [diff] [blame] | 1143 | Config->DebugDwarf = Args.hasArg(OPT_debug_dwarf); |
Zachary Turner | 0d07a8e | 2017-12-14 18:07:04 +0000 | [diff] [blame] | 1144 | Config->DebugGHashes = Args.hasArg(OPT_debug_ghash); |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 1145 | |
Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 +0000 | [diff] [blame] | 1146 | Config->MapFile = getMapFile(Args); |
| 1147 | |
Bob Haarman | 5ec4485 | 2018-01-31 23:44:00 +0000 | [diff] [blame] | 1148 | if (Config->Incremental && Config->DoGC) { |
| 1149 | warn("ignoring '/INCREMENTAL' because REF is enabled; use '/OPT:NOREF' to " |
| 1150 | "disable"); |
| 1151 | Config->Incremental = false; |
| 1152 | } |
| 1153 | |
| 1154 | if (Config->Incremental && Config->DoICF) { |
| 1155 | warn("ignoring '/INCREMENTAL' because ICF is enabled; use '/OPT:NOICF' to " |
| 1156 | "disable"); |
| 1157 | Config->Incremental = false; |
| 1158 | } |
| 1159 | |
| 1160 | if (Config->Incremental && Args.hasArg(OPT_order)) { |
| 1161 | warn("ignoring '/INCREMENTAL' due to '/ORDER' specification"); |
| 1162 | Config->Incremental = false; |
| 1163 | } |
| 1164 | |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 1165 | if (errorCount()) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 1168 | bool WholeArchiveFlag = Args.hasArg(OPT_wholearchive_flag); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 1169 | // Create a list of input files. Files can be given as arguments |
| 1170 | // for /defaultlib option. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 1171 | std::vector<MemoryBufferRef> MBs; |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 1172 | for (auto *Arg : Args.filtered(OPT_INPUT, OPT_wholearchive_file)) { |
| 1173 | switch (Arg->getOption().getID()) { |
| 1174 | case OPT_INPUT: |
| 1175 | if (Optional<StringRef> Path = findFile(Arg->getValue())) |
| 1176 | enqueuePath(*Path, WholeArchiveFlag); |
| 1177 | break; |
| 1178 | case OPT_wholearchive_file: |
| 1179 | if (Optional<StringRef> Path = findFile(Arg->getValue())) |
| 1180 | enqueuePath(*Path, true); |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 1184 | for (auto *Arg : Args.filtered(OPT_defaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 1185 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 1186 | enqueuePath(*Path, false); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 1187 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1188 | // Windows specific -- Create a resource file containing a manifest file. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1189 | if (Config->Manifest == Configuration::Embed) |
Martin Storsjo | 8278ba5 | 2017-09-13 07:28:03 +0000 | [diff] [blame] | 1190 | addBuffer(createManifestRes(), false); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 1191 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1192 | // Read all input files given via the command line. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1193 | run(); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 1194 | |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1195 | // We should have inferred a machine type by now from the input files, but if |
| 1196 | // not we assume x64. |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1197 | if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 1198 | warn("/machine is not specified. x64 is assumed"); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1199 | Config->Machine = AMD64; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Eric Beckmann | 9e19d79 | 2017-06-17 02:26:27 +0000 | [diff] [blame] | 1202 | // Input files can be Windows resource files (.res files). We use |
| 1203 | // WindowsResource to convert resource files to a regular COFF file, |
| 1204 | // then link the resulting file normally. |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1205 | if (!Resources.empty()) |
Peter Collingbourne | 9362ac6 | 2017-10-16 23:15:04 +0000 | [diff] [blame] | 1206 | Symtab->addFile(make<ObjFile>(convertResToCOFF(Resources))); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 1207 | |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 +0000 | [diff] [blame] | 1208 | if (Tar) |
| 1209 | Tar->append("response.txt", |
| 1210 | createResponseFile(Args, FilePaths, |
| 1211 | ArrayRef<StringRef>(SearchPaths).slice(1))); |
Peter Collingbourne | feee210 | 2016-07-26 02:00:42 +0000 | [diff] [blame] | 1212 | |
Rui Ueyama | 4d54534 | 2015-07-28 03:12:00 +0000 | [diff] [blame] | 1213 | // Handle /largeaddressaware |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 1214 | Config->LargeAddressAware = Args.hasFlag( |
| 1215 | OPT_largeaddressaware, OPT_largeaddressaware_no, Config->is64()); |
Rui Ueyama | 4d54534 | 2015-07-28 03:12:00 +0000 | [diff] [blame] | 1216 | |
Rui Ueyama | d68e211 | 2015-07-28 03:15:57 +0000 | [diff] [blame] | 1217 | // Handle /highentropyva |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 1218 | Config->HighEntropyVA = |
| 1219 | Config->is64() && |
| 1220 | Args.hasFlag(OPT_highentropyva, OPT_highentropyva_no, true); |
Rui Ueyama | d68e211 | 2015-07-28 03:15:57 +0000 | [diff] [blame] | 1221 | |
Martin Storsjo | 6ea167c | 2017-12-12 19:39:13 +0000 | [diff] [blame] | 1222 | if (!Config->DynamicBase && |
| 1223 | (Config->Machine == ARMNT || Config->Machine == ARM64)) |
| 1224 | error("/dynamicbase:no is not compatible with " + |
| 1225 | machineToStr(Config->Machine)); |
| 1226 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1227 | // Handle /entry and /dll |
| 1228 | if (auto *Arg = Args.getLastArg(OPT_entry)) { |
| 1229 | Config->Entry = addUndefined(mangle(Arg->getValue())); |
Rui Ueyama | 215286f | 2017-11-29 20:46:13 +0000 | [diff] [blame] | 1230 | } else if (!Config->Entry && !Config->NoEntry) { |
| 1231 | if (Args.hasArg(OPT_dll)) { |
| 1232 | StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12" |
| 1233 | : "_DllMainCRTStartup"; |
| 1234 | Config->Entry = addUndefined(S); |
| 1235 | } else { |
| 1236 | // Windows specific -- If entry point name is not given, we need to |
| 1237 | // infer that from user-defined entry name. |
| 1238 | StringRef S = findDefaultEntry(); |
| 1239 | if (S.empty()) |
| 1240 | fatal("entry point must be defined"); |
| 1241 | Config->Entry = addUndefined(S); |
| 1242 | log("Entry name inferred: " + S); |
| 1243 | } |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1246 | // Handle /export |
| 1247 | for (auto *Arg : Args.filtered(OPT_export)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1248 | Export E = parseExport(Arg->getValue()); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1249 | if (Config->Machine == I386) { |
| 1250 | if (!isDecorated(E.Name)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 1251 | E.Name = Saver.save("_" + E.Name); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1252 | if (!E.ExtName.empty() && !isDecorated(E.ExtName)) |
Rui Ueyama | 8d433d7 | 2016-12-08 21:27:09 +0000 | [diff] [blame] | 1253 | E.ExtName = Saver.save("_" + E.ExtName); |
Rui Ueyama | f10a320 | 2015-08-31 08:43:21 +0000 | [diff] [blame] | 1254 | } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1255 | Config->Exports.push_back(E); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | // Handle /def |
| 1259 | if (auto *Arg = Args.getLastArg(OPT_deffile)) { |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1260 | // parseModuleDefs mutates Config object. |
Reid Kleckner | 146eb7a | 2017-06-02 17:53:06 +0000 | [diff] [blame] | 1261 | parseModuleDefs(Arg->getValue()); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 1264 | // Handle generation of import library from a def file. |
Rui Ueyama | a835bab | 2017-09-13 20:30:59 +0000 | [diff] [blame] | 1265 | if (!Args.hasArg(OPT_INPUT)) { |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 1266 | fixupExports(); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 1267 | createImportLibrary(/*AsLib=*/true); |
Rui Ueyama | 6f4e255 | 2017-10-23 20:03:32 +0000 | [diff] [blame] | 1268 | return; |
Saleem Abdulrasool | bc7ff70 | 2017-06-15 20:39:58 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1271 | // Handle /delayload |
| 1272 | for (auto *Arg : Args.filtered(OPT_delayload)) { |
| 1273 | Config->DelayLoads.insert(StringRef(Arg->getValue()).lower()); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1274 | if (Config->Machine == I386) { |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1275 | Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8"); |
Rui Ueyama | 35ccb0f | 2015-07-25 00:20:06 +0000 | [diff] [blame] | 1276 | } else { |
| 1277 | Config->DelayLoadHelper = addUndefined("__delayLoadHelper2"); |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1281 | // Set default image name if neither /out or /def set it. |
| 1282 | if (Config->OutputFile.empty()) { |
| 1283 | Config->OutputFile = |
Richard Smith | a13714e | 2017-04-12 23:51:20 +0000 | [diff] [blame] | 1284 | getOutputPath((*Args.filtered(OPT_INPUT).begin())->getValue()); |
Reid Kleckner | 7668182e | 2017-03-21 00:12:51 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Reid Kleckner | 13bdbfb | 2017-03-22 00:57:14 +0000 | [diff] [blame] | 1287 | // Put the PDB next to the image if no /pdb flag was passed. |
Shoaib Meenai | a1f6fba | 2017-12-16 00:23:24 +0000 | [diff] [blame] | 1288 | if (ShouldCreatePDB && Config->PDBPath.empty()) { |
Reid Kleckner | 13bdbfb | 2017-03-22 00:57:14 +0000 | [diff] [blame] | 1289 | Config->PDBPath = Config->OutputFile; |
| 1290 | sys::path::replace_extension(Config->PDBPath, ".pdb"); |
| 1291 | } |
| 1292 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 1293 | // Set default image base if /base is not given. |
| 1294 | if (Config->ImageBase == uint64_t(-1)) |
| 1295 | Config->ImageBase = getDefaultImageBase(); |
| 1296 | |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1297 | Symtab->addSynthetic(mangle("__ImageBase"), nullptr); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 1298 | if (Config->Machine == I386) { |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1299 | Symtab->addAbsolute("___safe_se_handler_table", 0); |
| 1300 | Symtab->addAbsolute("___safe_se_handler_count", 0); |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 1301 | } |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 1302 | |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1303 | Symtab->addAbsolute(mangle("__guard_fids_count"), 0); |
| 1304 | Symtab->addAbsolute(mangle("__guard_fids_table"), 0); |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 +0000 | [diff] [blame] | 1305 | Symtab->addAbsolute(mangle("__guard_flags"), 0); |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1306 | Symtab->addAbsolute(mangle("__guard_iat_count"), 0); |
| 1307 | Symtab->addAbsolute(mangle("__guard_iat_table"), 0); |
| 1308 | Symtab->addAbsolute(mangle("__guard_longjmp_count"), 0); |
| 1309 | Symtab->addAbsolute(mangle("__guard_longjmp_table"), 0); |
Martin Storsjo | 2b96410 | 2017-12-12 08:22:29 +0000 | [diff] [blame] | 1310 | // Needed for MSVC 2017 15.5 CRT. |
| 1311 | Symtab->addAbsolute(mangle("__enclave_config"), 0); |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 1312 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1313 | // This code may add new undefined symbols to the link, which may enqueue more |
| 1314 | // symbol resolution tasks, so we need to continue executing tasks until we |
| 1315 | // converge. |
| 1316 | do { |
| 1317 | // Windows specific -- if entry point is not found, |
| 1318 | // search for its mangled names. |
| 1319 | if (Config->Entry) |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1320 | Symtab->mangleMaybe(Config->Entry); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 1321 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1322 | // Windows specific -- Make sure we resolve all dllexported symbols. |
| 1323 | for (Export &E : Config->Exports) { |
| 1324 | if (!E.ForwardTo.empty()) |
| 1325 | continue; |
| 1326 | E.Sym = addUndefined(E.Name); |
| 1327 | if (!E.Directives) |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1328 | Symtab->mangleMaybe(E.Sym); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1329 | } |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 1330 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1331 | // Add weak aliases. Weak aliases is a mechanism to give remaining |
| 1332 | // undefined symbols final chance to be resolved successfully. |
| 1333 | for (auto Pair : Config->AlternateNames) { |
| 1334 | StringRef From = Pair.first; |
| 1335 | StringRef To = Pair.second; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1336 | Symbol *Sym = Symtab->find(From); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1337 | if (!Sym) |
| 1338 | continue; |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 +0000 | [diff] [blame] | 1339 | if (auto *U = dyn_cast<Undefined>(Sym)) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1340 | if (!U->WeakAlias) |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1341 | U->WeakAlias = Symtab->addUndefined(To); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1342 | } |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1343 | |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1344 | // Windows specific -- if __load_config_used can be resolved, resolve it. |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1345 | if (Symtab->findUnderscore("_load_config_used")) |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1346 | addUndefined(mangle("_load_config_used")); |
| 1347 | } while (run()); |
Peter Collingbourne | 8b65e51 | 2016-12-11 22:15:25 +0000 | [diff] [blame] | 1348 | |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 1349 | if (errorCount()) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1350 | return; |
| 1351 | |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1352 | // If /msvclto is given, we use the MSVC linker to link LTO output files. |
| 1353 | // This is useful because MSVC link.exe can generate complete PDBs. |
| 1354 | if (Args.hasArg(OPT_msvclto)) { |
Rui Ueyama | 85d54b0 | 2017-02-23 00:26:42 +0000 | [diff] [blame] | 1355 | invokeMSVC(Args); |
Rui Ueyama | 6f4e255 | 2017-10-23 20:03:32 +0000 | [diff] [blame] | 1356 | return; |
Rui Ueyama | 1e0b158 | 2017-02-06 20:47:55 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 1359 | // Do LTO by compiling bitcode input files to a set of native COFF files then |
| 1360 | // link those files. |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1361 | Symtab->addCombinedLTOObjects(); |
Peter Collingbourne | 6ee0b4e | 2016-12-15 04:02:23 +0000 | [diff] [blame] | 1362 | run(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 1363 | |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1364 | // Make sure we have resolved all symbols. |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1365 | Symtab->reportRemainingUndefines(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 1366 | if (errorCount()) |
Rui Ueyama | cc6738a | 2017-10-06 23:43:54 +0000 | [diff] [blame] | 1367 | return; |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 1368 | |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1369 | // Windows specific -- if no /subsystem is given, we need to infer |
| 1370 | // that from entry point name. |
| 1371 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1372 | Config->Subsystem = inferSubsystem(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1373 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) |
Rui Ueyama | 6060479 | 2016-07-14 23:37:14 +0000 | [diff] [blame] | 1374 | fatal("subsystem must be defined"); |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1377 | // Handle /safeseh. |
Shoaib Meenai | 59bf362 | 2017-10-24 21:17:16 +0000 | [diff] [blame] | 1378 | if (Args.hasFlag(OPT_safeseh, OPT_safeseh_no, false)) { |
Rui Ueyama | acd632d | 2017-07-27 00:45:26 +0000 | [diff] [blame] | 1379 | for (ObjFile *File : ObjFile::Instances) |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 +0000 | [diff] [blame] | 1380 | if (!File->hasSafeSEH()) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1381 | error("/safeseh: " + File->getName() + " is not compatible with SEH"); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 1382 | if (errorCount()) |
Bob Haarman | ac8f7fc | 2017-04-05 00:43:54 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 1385 | |
Martin Storsjo | 7f71acd | 2017-10-12 05:37:13 +0000 | [diff] [blame] | 1386 | // In MinGW, all symbols are automatically exported if no symbols |
| 1387 | // are chosen to be exported. |
| 1388 | if (Config->DLL && ((Config->MinGW && Config->Exports.empty()) || |
| 1389 | Args.hasArg(OPT_export_all_symbols))) { |
Martin Storsjo | b40ccc1 | 2017-10-19 06:56:04 +0000 | [diff] [blame] | 1390 | AutoExporter Exporter; |
Martin Storsjo | 7f71acd | 2017-10-12 05:37:13 +0000 | [diff] [blame] | 1391 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1392 | Symtab->forEachSymbol([=](Symbol *S) { |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 +0000 | [diff] [blame] | 1393 | auto *Def = dyn_cast<Defined>(S); |
Martin Storsjo | b40ccc1 | 2017-10-19 06:56:04 +0000 | [diff] [blame] | 1394 | if (!Exporter.shouldExport(Def)) |
Martin Storsjo | 7f71acd | 2017-10-12 05:37:13 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | Export E; |
| 1397 | E.Name = Def->getName(); |
| 1398 | E.Sym = Def; |
Martin Storsjo | dc95dbf | 2017-11-03 20:58:20 +0000 | [diff] [blame] | 1399 | if (Def->getChunk() && |
| 1400 | !(Def->getChunk()->getPermissions() & IMAGE_SCN_MEM_EXECUTE)) |
| 1401 | E.Data = true; |
Martin Storsjo | 7f71acd | 2017-10-12 05:37:13 +0000 | [diff] [blame] | 1402 | Config->Exports.push_back(E); |
| 1403 | }); |
| 1404 | } |
| 1405 | |
Rui Ueyama | 151d862 | 2015-06-17 20:40:43 +0000 | [diff] [blame] | 1406 | // Windows specific -- when we are creating a .dll file, we also |
| 1407 | // need to create a .lib file. |
Rui Ueyama | 100ffac | 2015-09-01 09:15:58 +0000 | [diff] [blame] | 1408 | if (!Config->Exports.empty() || Config->DLL) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1409 | fixupExports(); |
Saleem Abdulrasool | ace2fa7 | 2017-07-19 02:01:27 +0000 | [diff] [blame] | 1410 | createImportLibrary(/*AsLib=*/false); |
Rui Ueyama | 8765fba | 2015-07-15 22:21:08 +0000 | [diff] [blame] | 1411 | assignExportOrdinals(); |
| 1412 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 1413 | |
Martin Storsjo | 7f71acd | 2017-10-12 05:37:13 +0000 | [diff] [blame] | 1414 | // Handle /output-def (MinGW specific). |
| 1415 | if (auto *Arg = Args.getLastArg(OPT_output_def)) |
| 1416 | writeDefFile(Arg->getValue()); |
Rui Ueyama | d73479b | 2018-01-29 19:55:55 +0000 | [diff] [blame] | 1417 | |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 1418 | // Set extra alignment for .comm symbols |
| 1419 | for (auto Pair : Config->AlignComm) { |
| 1420 | StringRef Name = Pair.first; |
Rui Ueyama | cfc2f80 | 2017-09-13 21:54:55 +0000 | [diff] [blame] | 1421 | uint32_t Alignment = Pair.second; |
| 1422 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1423 | Symbol *Sym = Symtab->find(Name); |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 1424 | if (!Sym) { |
| 1425 | warn("/aligncomm symbol " + Name + " not found"); |
| 1426 | continue; |
| 1427 | } |
Rui Ueyama | cfc2f80 | 2017-09-13 21:54:55 +0000 | [diff] [blame] | 1428 | |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 +0000 | [diff] [blame] | 1429 | auto *DC = dyn_cast<DefinedCommon>(Sym); |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 1430 | if (!DC) { |
| 1431 | warn("/aligncomm symbol " + Name + " of wrong kind"); |
| 1432 | continue; |
| 1433 | } |
Rui Ueyama | cfc2f80 | 2017-09-13 21:54:55 +0000 | [diff] [blame] | 1434 | |
| 1435 | CommonChunk *C = DC->getChunk(); |
| 1436 | C->Alignment = std::max(C->Alignment, Alignment); |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1439 | // Windows specific -- Create a side-by-side manifest file. |
| 1440 | if (Config->Manifest == Configuration::SideBySide) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 1441 | createSideBySideManifest(); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 1442 | |
Rui Ueyama | b6d3a93 | 2018-01-29 21:50:53 +0000 | [diff] [blame] | 1443 | // Handle /order. We want to do this at this moment because we |
| 1444 | // need a complete list of comdat sections to warn on nonexistent |
| 1445 | // functions. |
| 1446 | if (auto *Arg = Args.getLastArg(OPT_order)) |
| 1447 | parseOrderFile(Arg->getValue()); |
| 1448 | |
Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 1449 | // Identify unreferenced COMDAT sections. |
| 1450 | if (Config->DoGC) |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1451 | markLive(Symtab->getChunks()); |
Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Identify identical COMDAT sections to merge them. |
| 1454 | if (Config->DoICF) |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1455 | doICF(Symtab->getChunks()); |
Rui Ueyama | a5f0f75 | 2015-09-19 21:36:28 +0000 | [diff] [blame] | 1456 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1457 | // Write the result. |
Rui Ueyama | cbf969e | 2017-08-28 21:51:07 +0000 | [diff] [blame] | 1458 | writeResult(); |
Zachary Turner | 727f153 | 2018-01-17 19:16:26 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Stop early so we can print the results. |
| 1461 | Timer::root().stop(); |
| 1462 | if (Config->ShowTiming) |
| 1463 | Timer::root().print(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1466 | } // namespace coff |
| 1467 | } // namespace lld |