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