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