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