blob: d4081488871a25c21505aaa957d72de04a9da6a9 [file] [log] [blame]
Rui Ueyama411c63602015-05-28 19:09:30 +00001//===- 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
10#include "Config.h"
11#include "Driver.h"
Rui Ueyama562daa82015-06-18 21:50:38 +000012#include "Error.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000013#include "InputFiles.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000014#include "SymbolTable.h"
Rui Ueyama685c41c2015-08-05 23:43:53 +000015#include "Symbols.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000016#include "Writer.h"
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000017#include "lld/Driver/Driver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000018#include "llvm/ADT/Optional.h"
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +000019#include "llvm/ADT/StringSwitch.h"
Peter Collingbournebd1cb792015-06-09 21:52:48 +000020#include "llvm/LibDriver/LibDriver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000021#include "llvm/Option/Arg.h"
22#include "llvm/Option/ArgList.h"
23#include "llvm/Option/Option.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000024#include "llvm/Support/Debug.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000025#include "llvm/Support/Path.h"
Rui Ueyama54b71da2015-05-31 19:17:12 +000026#include "llvm/Support/Process.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000027#include "llvm/Support/TargetSelect.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000028#include "llvm/Support/raw_ostream.h"
Rui Ueyama2bf6a122015-06-14 21:50:50 +000029#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000030#include <memory>
31
32using namespace llvm;
Rui Ueyama84936e02015-07-07 23:39:18 +000033using namespace llvm::COFF;
Rui Ueyama54b71da2015-05-31 19:17:12 +000034using llvm::sys::Process;
Peter Collingbournebaf5f872015-06-26 19:20:09 +000035using llvm::sys::fs::OpenFlags;
Rui Ueyama711cd2d2015-05-31 21:17:10 +000036using llvm::sys::fs::file_magic;
37using llvm::sys::fs::identify_magic;
Rui Ueyama411c63602015-05-28 19:09:30 +000038
Rui Ueyama3500f662015-05-28 20:30:06 +000039namespace lld {
40namespace coff {
Rui Ueyama411c63602015-05-28 19:09:30 +000041
Rui Ueyama3500f662015-05-28 20:30:06 +000042Configuration *Config;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000043LinkerDriver *Driver;
44
Rui Ueyama417553d2016-02-28 19:54:51 +000045bool link(llvm::ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000046 Configuration C;
47 LinkerDriver D;
48 Config = &C;
49 Driver = &D;
Rui Ueyama417553d2016-02-28 19:54:51 +000050 Driver->link(Args);
51 return true;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000052}
Rui Ueyama411c63602015-05-28 19:09:30 +000053
Nico Weber5660de72016-04-20 22:34:15 +000054// Drop directory components and replace extension with ".exe" or ".dll".
Rui Ueyamaad660982015-06-07 00:20:32 +000055static std::string getOutputPath(StringRef Path) {
56 auto P = Path.find_last_of("\\/");
57 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
Nico Weber5660de72016-04-20 22:34:15 +000058 const char* E = Config->DLL ? ".dll" : ".exe";
59 return (S.substr(0, S.rfind('.')) + E).str();
Rui Ueyama411c63602015-05-28 19:09:30 +000060}
61
Rui Ueyamad7c2f582015-05-31 21:04:56 +000062// Opens a file. Path has to be resolved already.
63// Newly created memory buffers are owned by this driver.
Rafael Espindolab835ae82015-08-06 14:58:50 +000064MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000065 std::unique_ptr<MemoryBuffer> MB =
Rui Ueyamabb579542016-07-15 01:12:24 +000066 check(MemoryBuffer::getFile(Path), "could not open " + Path);
Rui Ueyamad7c2f582015-05-31 21:04:56 +000067 MemoryBufferRef MBRef = MB->getMemBufferRef();
68 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000069 return MBRef;
70}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000071
Rui Ueyama2bf6a122015-06-14 21:50:50 +000072static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Peter Collingbournefeee2102016-07-26 02:00:42 +000073 if (Driver->Cpio)
74 Driver->Cpio->append(relativeToRoot(MB.getBufferIdentifier()),
75 MB.getBuffer());
76
Rui Ueyama711cd2d2015-05-31 21:17:10 +000077 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000078 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000079 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000080 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000081 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000082 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000083 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000084 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
85 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000086}
87
Rui Ueyamaf10a3202015-08-31 08:43:21 +000088static bool isDecorated(StringRef Sym) {
89 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
90}
91
Rui Ueyama411c63602015-05-28 19:09:30 +000092// Parses .drectve section contents and returns a list of files
93// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000094void LinkerDriver::parseDirectives(StringRef S) {
95 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000096
David Blaikie6521ed92015-06-22 22:06:52 +000097 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000098 switch (Arg->getOption().getID()) {
99 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000100 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000101 break;
102 case OPT_defaultlib:
103 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000104 MemoryBufferRef MB = openFile(*Path);
105 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000106 }
107 break;
108 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000109 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000110 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000111 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000112 break;
113 }
114 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000115 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000116 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000117 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000118 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000119 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000120 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000121 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000122 break;
123 case OPT_nodefaultlib:
124 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
125 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000126 case OPT_section:
127 parseSection(Arg->getValue());
128 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000129 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000130 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000131 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000132 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000133 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000134 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000135 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000136 }
137 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000138}
139
Rui Ueyama54b71da2015-05-31 19:17:12 +0000140// Find file from search paths. You can omit ".obj", this function takes
141// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000142StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000143 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
144 if (hasPathSep)
145 return Filename;
146 bool hasExt = (Filename.find('.') != StringRef::npos);
147 for (StringRef Dir : SearchPaths) {
148 SmallString<128> Path = Dir;
149 llvm::sys::path::append(Path, Filename);
150 if (llvm::sys::fs::exists(Path.str()))
151 return Alloc.save(Path.str());
152 if (!hasExt) {
153 Path.append(".obj");
154 if (llvm::sys::fs::exists(Path.str()))
155 return Alloc.save(Path.str());
156 }
157 }
158 return Filename;
159}
160
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000161// Resolves a file path. This never returns the same path
162// (in that case, it returns None).
163Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
164 StringRef Path = doFindFile(Filename);
165 bool Seen = !VisitedFiles.insert(Path.lower()).second;
166 if (Seen)
167 return None;
168 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000169}
170
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000171// Find library file from search path.
172StringRef LinkerDriver::doFindLib(StringRef Filename) {
173 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000174 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000175 if (!hasExt)
176 Filename = Alloc.save(Filename + ".lib");
177 return doFindFile(Filename);
178}
179
180// Resolves a library path. /nodefaultlib options are taken into
181// consideration. This never returns the same path (in that case,
182// it returns None).
183Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
184 if (Config->NoDefaultLibAll)
185 return None;
186 StringRef Path = doFindLib(Filename);
187 if (Config->NoDefaultLibs.count(Path))
188 return None;
189 bool Seen = !VisitedFiles.insert(Path.lower()).second;
190 if (Seen)
191 return None;
192 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000193}
194
195// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000196void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000197 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
198 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000199 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000200 StringRef Env = Alloc.save(*EnvOpt);
201 while (!Env.empty()) {
202 StringRef Path;
203 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000204 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000205 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000206}
207
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000208Undefined *LinkerDriver::addUndefined(StringRef Name) {
209 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000210 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000211 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000212}
213
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000214// Symbol names are mangled by appending "_" prefix on x86.
215StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000216 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
217 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000218 return Alloc.save("_" + Sym);
219 return Sym;
220}
221
Rui Ueyama45044f42015-06-29 01:03:53 +0000222// Windows specific -- find default entry point name.
223StringRef LinkerDriver::findDefaultEntry() {
224 // User-defined main functions and their corresponding entry points.
225 static const char *Entries[][2] = {
226 {"main", "mainCRTStartup"},
227 {"wmain", "wmainCRTStartup"},
228 {"WinMain", "WinMainCRTStartup"},
229 {"wWinMain", "wWinMainCRTStartup"},
230 };
231 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000232 StringRef Entry = Symtab.findMangle(mangle(E[0]));
233 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000234 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000235 }
236 return "";
237}
238
239WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000240 if (Config->DLL)
241 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000242 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000243 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000244 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000245 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
246 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000247}
248
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000249static uint64_t getDefaultImageBase() {
250 if (Config->is64())
251 return Config->DLL ? 0x180000000 : 0x140000000;
252 return Config->DLL ? 0x10000000 : 0x400000;
253}
254
Peter Collingbournefeee2102016-07-26 02:00:42 +0000255static std::string createResponseFile(const llvm::opt::InputArgList &Args,
256 ArrayRef<MemoryBufferRef> MBs,
257 ArrayRef<StringRef> SearchPaths) {
258 SmallString<0> Data;
259 raw_svector_ostream OS(Data);
260
261 for (auto *Arg : Args) {
262 switch (Arg->getOption().getID()) {
263 case OPT_linkrepro:
264 case OPT_INPUT:
265 case OPT_defaultlib:
266 case OPT_libpath:
267 break;
268 default:
269 OS << stringize(Arg) << "\n";
270 }
271 }
272
273 for (StringRef Path : SearchPaths) {
274 std::string RelPath = relativeToRoot(Path);
275 OS << "/libpath:" << quote(RelPath) << "\n";
276 }
277
278 for (MemoryBufferRef MB : MBs) {
279 std::string InputPath = relativeToRoot(MB.getBufferIdentifier());
280 OS << quote(InputPath) << "\n";
281 }
282
283 return Data.str();
284}
285
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000286static unsigned getDefaultDebugType(const llvm::opt::InputArgList &Args) {
287 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
288 if (Args.hasArg(OPT_driver))
289 DebugTypes |= static_cast<unsigned>(DebugType::PData);
290 if (Args.hasArg(OPT_profile))
291 DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
292 return DebugTypes;
293}
294
295static unsigned parseDebugType(StringRef Arg) {
296 llvm::SmallVector<StringRef, 3> Types;
297 Arg.split(Types, ',', /*KeepEmpty=*/false);
298
299 unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
300 for (StringRef Type : Types)
301 DebugTypes |= StringSwitch<unsigned>(Type.lower())
302 .Case("cv", static_cast<unsigned>(DebugType::CV))
303 .Case("pdata", static_cast<unsigned>(DebugType::PData))
304 .Case("fixup", static_cast<unsigned>(DebugType::Fixup));
305 return DebugTypes;
306}
307
Rafael Espindolab835ae82015-08-06 14:58:50 +0000308void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000309 // If the first command line argument is "/lib", link.exe acts like lib.exe.
310 // We call our own implementation of lib.exe that understands bitcode files.
311 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
312 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000313 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000314 return;
315 }
316
Peter Collingbourne60c16162015-06-01 20:10:10 +0000317 // Needed for LTO.
318 llvm::InitializeAllTargetInfos();
319 llvm::InitializeAllTargets();
320 llvm::InitializeAllTargetMCs();
321 llvm::InitializeAllAsmParsers();
322 llvm::InitializeAllAsmPrinters();
323 llvm::InitializeAllDisassemblers();
324
Rui Ueyama411c63602015-05-28 19:09:30 +0000325 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000326 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000327
Rui Ueyama5c726432015-05-29 16:11:52 +0000328 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000329 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000330 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000331 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000332 }
333
Peter Collingbournefeee2102016-07-26 02:00:42 +0000334 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
335 SmallString<64> Path = StringRef(Arg->getValue());
336 llvm::sys::path::append(Path, "repro");
337 ErrorOr<CpioFile *> F = CpioFile::create(Path);
338 if (F)
339 Cpio.reset(*F);
340 else
341 llvm::errs() << "/linkrepro: failed to open " << Path
342 << ".cpio: " << F.getError().message() << '\n';
343 }
344
Rafael Espindolab835ae82015-08-06 14:58:50 +0000345 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000346 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000347
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000348 // Construct search path list.
349 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000350 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000351 SearchPaths.push_back(Arg->getValue());
352 addLibSearchPaths();
353
Rui Ueyamaad660982015-06-07 00:20:32 +0000354 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000355 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000356 Config->OutputFile = Arg->getValue();
357
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000358 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000359 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000360 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000361
Rui Ueyama95925fd2015-06-28 19:35:15 +0000362 // Handle /force or /force:unresolved
363 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
364 Config->Force = true;
365
Rui Ueyama6600eb12015-07-04 23:37:32 +0000366 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000367 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000368 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000369 Config->DebugTypes =
370 Args.hasArg(OPT_debugtype)
371 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
372 : getDefaultDebugType(Args);
373 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000374
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000375 // Create a dummy PDB file to satisfy build sytem rules.
376 if (auto *Arg = Args.getLastArg(OPT_pdb)) {
377 Config->PDBPath = Arg->getValue();
378 createPDB(Config->PDBPath);
379 }
380
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000381 // Handle /noentry
382 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000383 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000384 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000385 Config->NoEntry = true;
386 }
387
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000388 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000389 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000390 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000391 Config->ManifestID = 2;
392 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000393
Rui Ueyama588e8322015-06-15 01:23:58 +0000394 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000395 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000396 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000397 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000398 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000399 Config->DynamicBase = false;
400 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000401
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000402 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000403 if (auto *Arg = Args.getLastArg(OPT_machine))
404 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000405
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000406 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000407 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000408 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
409
410 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000411 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000412 Config->NoDefaultLibAll = true;
413
Rui Ueyama804a8b62015-05-29 16:18:15 +0000414 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000415 if (auto *Arg = Args.getLastArg(OPT_base))
416 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000417
418 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000419 if (auto *Arg = Args.getLastArg(OPT_stack))
420 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000421
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000422 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000423 if (auto *Arg = Args.getLastArg(OPT_heap))
424 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000425
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000426 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000427 if (auto *Arg = Args.getLastArg(OPT_version))
428 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
429 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000430
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000431 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000432 if (auto *Arg = Args.getLastArg(OPT_subsystem))
433 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
434 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000435
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000436 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000437 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000438 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000439
Rui Ueyama08d5e182015-06-18 23:20:11 +0000440 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000441 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000442 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000443
Rui Ueyamab95188c2015-06-18 20:27:09 +0000444 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000445 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000446 Config->Implib = Arg->getValue();
447
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000448 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000449 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000450 std::string Str = StringRef(Arg->getValue()).lower();
451 SmallVector<StringRef, 1> Vec;
452 StringRef(Str).split(Vec, ',');
453 for (StringRef S : Vec) {
454 if (S == "noref") {
455 Config->DoGC = false;
456 Config->DoICF = false;
457 continue;
458 }
459 if (S == "icf" || StringRef(S).startswith("icf=")) {
460 Config->DoICF = true;
461 continue;
462 }
463 if (S == "noicf") {
464 Config->DoICF = false;
465 continue;
466 }
467 if (StringRef(S).startswith("lldlto=")) {
468 StringRef OptLevel = StringRef(S).substr(7);
469 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
470 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000471 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000472 continue;
473 }
474 if (StringRef(S).startswith("lldltojobs=")) {
475 StringRef Jobs = StringRef(S).substr(11);
476 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000477 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000478 continue;
479 }
480 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000481 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000482 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000483 }
484
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000485 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000486 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000487 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000488
Rui Ueyama6600eb12015-07-04 23:37:32 +0000489 // Handle /merge
490 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000491 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000492
Rui Ueyama440138c2016-06-20 03:39:39 +0000493 // Handle /section
494 for (auto *Arg : Args.filtered(OPT_section))
495 parseSection(Arg->getValue());
496
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000497 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000498 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
499 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000500
501 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000502 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
503 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000504
505 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000506 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000507 Config->ManifestDependency = Arg->getValue();
508
509 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000510 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000511 Config->ManifestFile = Arg->getValue();
512
Rui Ueyamaafb19012016-04-19 01:21:58 +0000513 // Handle /manifestinput
514 for (auto *Arg : Args.filtered(OPT_manifestinput))
515 Config->ManifestInput.push_back(Arg->getValue());
516
Rui Ueyama6592ff82015-06-16 23:13:00 +0000517 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000518 if (Args.hasArg(OPT_allowbind_no))
519 Config->AllowBind = false;
520 if (Args.hasArg(OPT_allowisolation_no))
521 Config->AllowIsolation = false;
522 if (Args.hasArg(OPT_dynamicbase_no))
523 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000524 if (Args.hasArg(OPT_nxcompat_no))
525 Config->NxCompat = false;
526 if (Args.hasArg(OPT_tsaware_no))
527 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000528 if (Args.hasArg(OPT_nosymtab))
529 Config->WriteSymtab = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000530
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000531 // Create a list of input files. Files can be given as arguments
532 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000533 std::vector<StringRef> Paths;
534 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000535 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000536 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000537 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000538 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000539 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000540 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000541 for (StringRef Path : Paths)
542 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000543
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000544 // Windows specific -- Create a resource file containing a manifest file.
545 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000546 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000547 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000548 OwningMBs.push_back(std::move(MB)); // take ownership
549 }
550
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000551 // Windows specific -- Input files can be Windows resource files (.res files).
552 // We invoke cvtres.exe to convert resource files to a regular COFF file
553 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000554 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000555 auto NotResource = [](MemoryBufferRef MB) {
556 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000557 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000558 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
559 if (It != MBs.end()) {
560 Resources.insert(Resources.end(), It, MBs.end());
561 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000562 }
563
Rui Ueyama85225b02015-07-02 03:15:15 +0000564 // Read all input files given via the command line. Note that step()
565 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000566 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000567 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000568 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000569
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000570 // Determine machine type and check if all object files are
571 // for the same CPU type. Note that this needs to be done before
572 // any call to mangle().
573 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
574 MachineTypes MT = File->getMachineType();
575 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
576 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000577 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
578 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000579 continue;
580 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000581 if (Config->Machine != MT)
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000582 fatal(File->getShortName() + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000583 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000584 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000585 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000586 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000587 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000588 }
589
590 // Windows specific -- Convert Windows resource files to a COFF file.
591 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000592 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000593 Symtab.addFile(createFile(MB->getMemBufferRef()));
Peter Collingbournefeee2102016-07-26 02:00:42 +0000594
595 MBs.push_back(MB->getMemBufferRef());
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000596 OwningMBs.push_back(std::move(MB)); // take ownership
597 }
598
Peter Collingbournefeee2102016-07-26 02:00:42 +0000599 if (Cpio)
600 Cpio->append("response.txt",
601 createResponseFile(Args, MBs,
602 ArrayRef<StringRef>(SearchPaths).slice(1)));
603
Rui Ueyama4d545342015-07-28 03:12:00 +0000604 // Handle /largeaddressaware
605 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
606 Config->LargeAddressAware = true;
607
Rui Ueyamad68e2112015-07-28 03:15:57 +0000608 // Handle /highentropyva
609 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
610 Config->HighEntropyVA = true;
611
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000612 // Handle /entry and /dll
613 if (auto *Arg = Args.getLastArg(OPT_entry)) {
614 Config->Entry = addUndefined(mangle(Arg->getValue()));
615 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000616 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
617 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000618 Config->Entry = addUndefined(S);
619 } else if (!Config->NoEntry) {
620 // Windows specific -- If entry point name is not given, we need to
621 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000622 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000623 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000624 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000625 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000626 if (Config->Verbose)
627 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000628 }
629
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000630 // Handle /export
631 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000632 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000633 if (Config->Machine == I386) {
634 if (!isDecorated(E.Name))
635 E.Name = Alloc.save("_" + E.Name);
636 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
637 E.ExtName = Alloc.save("_" + E.ExtName);
638 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000639 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000640 }
641
642 // Handle /def
643 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000644 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000645 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000646 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000647 }
648
Rui Ueyama6d249082015-07-13 22:31:45 +0000649 // Handle /delayload
650 for (auto *Arg : Args.filtered(OPT_delayload)) {
651 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000652 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000653 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000654 } else {
655 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000656 }
657 }
658
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000659 // Set default image base if /base is not given.
660 if (Config->ImageBase == uint64_t(-1))
661 Config->ImageBase = getDefaultImageBase();
662
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000663 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000664 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000665 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
666 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
667 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000668
Rui Ueyama107db552015-08-09 21:01:06 +0000669 // We do not support /guard:cf (control flow protection) yet.
670 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
671 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
672 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
673 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
674
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000675 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000676 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000677
678 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000679 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
680 // A new file may contain a directive section to add new command line options.
681 // That's why we have to repeat until converge.)
682 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000683 // Windows specific -- if entry point is not found,
684 // search for its mangled names.
685 if (Config->Entry)
686 Symtab.mangleMaybe(Config->Entry);
687
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000688 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000689 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000690 if (!E.ForwardTo.empty())
691 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000692 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000693 if (!E.Directives)
694 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000695 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000696
697 // Add weak aliases. Weak aliases is a mechanism to give remaining
698 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000699 for (auto Pair : Config->AlternateNames) {
700 StringRef From = Pair.first;
701 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000702 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000703 if (!Sym)
704 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000705 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000706 if (!U->WeakAlias)
707 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000708 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000709
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000710 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000711 if (Symtab.findUnderscore("_load_config_used"))
712 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000713
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000714 if (Symtab.queueEmpty())
715 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000716 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000717 }
718
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000719 // Do LTO by compiling bitcode input files to a set of native COFF files then
720 // link those files.
721 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000722
Peter Collingbourne2612a322015-07-04 05:28:41 +0000723 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000724 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000725
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000726 // Windows specific -- if no /subsystem is given, we need to infer
727 // that from entry point name.
728 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000729 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000730 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000731 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000732 }
733
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000734 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000735 if (Args.hasArg(OPT_safeseh))
736 for (ObjectFile *File : Symtab.ObjectFiles)
737 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000738 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000739
Rui Ueyama151d8622015-06-17 20:40:43 +0000740 // Windows specific -- when we are creating a .dll file, we also
741 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000742 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000743 fixupExports();
744 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000745 assignExportOrdinals();
746 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000747
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000748 // Windows specific -- Create a side-by-side manifest file.
749 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000750 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000751
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000752 // Identify unreferenced COMDAT sections.
753 if (Config->DoGC)
754 markLive(Symtab.getChunks());
755
756 // Identify identical COMDAT sections to merge them.
757 if (Config->DoICF)
758 doICF(Symtab.getChunks());
759
Rui Ueyama411c63602015-05-28 19:09:30 +0000760 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000761 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000762
Rui Ueyama016414f2015-06-28 20:07:08 +0000763 // Create a symbol map file containing symbol VAs and their names
764 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000765 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
766 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000767 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000768 if (EC)
Rui Ueyamabb579542016-07-15 01:12:24 +0000769 fatal(EC, "could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000770 Symtab.printMap(Out);
771 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000772 // Call exit to avoid calling destructors.
773 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000774}
775
Rui Ueyama411c63602015-05-28 19:09:30 +0000776} // namespace coff
777} // namespace lld