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