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