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