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