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