blob: fbdb2d0f469088c180581b3a86e07825a9be4e17 [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
Rui Ueyama411c63602015-05-28 19:09:30 +000010#include "Driver.h"
Rui Ueyama1d99ab32016-09-15 22:24:51 +000011#include "Config.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 Ueyamaf83806a2016-11-15 01:01:51 +000083 if (Magic == file_magic::coff_cl_gl_object)
84 fatal(MB.getBufferIdentifier() + ": is not a native COFF file. "
85 "Recompile without /GL");
Rui Ueyamaad660982015-06-07 00:20:32 +000086 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000087 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
88 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000089}
90
Rui Ueyamaf10a3202015-08-31 08:43:21 +000091static bool isDecorated(StringRef Sym) {
92 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
93}
94
Rui Ueyama411c63602015-05-28 19:09:30 +000095// Parses .drectve section contents and returns a list of files
96// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000097void LinkerDriver::parseDirectives(StringRef S) {
98 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000099
David Blaikie6521ed92015-06-22 22:06:52 +0000100 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +0000101 switch (Arg->getOption().getID()) {
102 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000103 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000104 break;
105 case OPT_defaultlib:
106 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000107 MemoryBufferRef MB = openFile(*Path);
108 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000109 }
110 break;
111 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000112 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000113 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000114 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000115 break;
116 }
117 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000118 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000119 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000120 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000121 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000122 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000123 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000124 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000125 break;
126 case OPT_nodefaultlib:
127 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
128 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000129 case OPT_section:
130 parseSection(Arg->getValue());
131 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000132 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000133 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000134 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000135 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000136 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000137 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000138 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000139 }
140 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000141}
142
Rui Ueyama54b71da2015-05-31 19:17:12 +0000143// Find file from search paths. You can omit ".obj", this function takes
144// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000145StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000146 bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
147 if (HasPathSep)
Rui Ueyama54b71da2015-05-31 19:17:12 +0000148 return Filename;
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000149 bool HasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000150 for (StringRef Dir : SearchPaths) {
151 SmallString<128> Path = Dir;
152 llvm::sys::path::append(Path, Filename);
153 if (llvm::sys::fs::exists(Path.str()))
154 return Alloc.save(Path.str());
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000155 if (!HasExt) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000156 Path.append(".obj");
157 if (llvm::sys::fs::exists(Path.str()))
158 return Alloc.save(Path.str());
159 }
160 }
161 return Filename;
162}
163
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000164// Resolves a file path. This never returns the same path
165// (in that case, it returns None).
166Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
167 StringRef Path = doFindFile(Filename);
168 bool Seen = !VisitedFiles.insert(Path.lower()).second;
169 if (Seen)
170 return None;
171 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000172}
173
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000174// Find library file from search path.
175StringRef LinkerDriver::doFindLib(StringRef Filename) {
176 // Add ".lib" to Filename if that has no file extension.
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000177 bool HasExt = (Filename.find('.') != StringRef::npos);
178 if (!HasExt)
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000179 Filename = Alloc.save(Filename + ".lib");
180 return doFindFile(Filename);
181}
182
183// Resolves a library path. /nodefaultlib options are taken into
184// consideration. This never returns the same path (in that case,
185// it returns None).
186Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
187 if (Config->NoDefaultLibAll)
188 return None;
189 StringRef Path = doFindLib(Filename);
190 if (Config->NoDefaultLibs.count(Path))
191 return None;
192 bool Seen = !VisitedFiles.insert(Path.lower()).second;
193 if (Seen)
194 return None;
195 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000196}
197
198// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000199void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000200 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
201 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000202 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000203 StringRef Env = Alloc.save(*EnvOpt);
204 while (!Env.empty()) {
205 StringRef Path;
206 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000207 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000208 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000209}
210
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000211Undefined *LinkerDriver::addUndefined(StringRef Name) {
212 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000213 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000214 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000215}
216
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000217// Symbol names are mangled by appending "_" prefix on x86.
218StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000219 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
220 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000221 return Alloc.save("_" + Sym);
222 return Sym;
223}
224
Rui Ueyama45044f42015-06-29 01:03:53 +0000225// Windows specific -- find default entry point name.
226StringRef LinkerDriver::findDefaultEntry() {
227 // User-defined main functions and their corresponding entry points.
228 static const char *Entries[][2] = {
229 {"main", "mainCRTStartup"},
230 {"wmain", "wmainCRTStartup"},
231 {"WinMain", "WinMainCRTStartup"},
232 {"wWinMain", "wWinMainCRTStartup"},
233 };
234 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000235 StringRef Entry = Symtab.findMangle(mangle(E[0]));
236 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000237 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000238 }
239 return "";
240}
241
242WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000243 if (Config->DLL)
244 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000245 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000246 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000247 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000248 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
249 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000250}
251
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000252static uint64_t getDefaultImageBase() {
253 if (Config->is64())
254 return Config->DLL ? 0x180000000 : 0x140000000;
255 return Config->DLL ? 0x10000000 : 0x400000;
256}
257
Peter Collingbournefeee2102016-07-26 02:00:42 +0000258static std::string createResponseFile(const llvm::opt::InputArgList &Args,
259 ArrayRef<MemoryBufferRef> MBs,
260 ArrayRef<StringRef> SearchPaths) {
261 SmallString<0> Data;
262 raw_svector_ostream OS(Data);
263
264 for (auto *Arg : Args) {
265 switch (Arg->getOption().getID()) {
266 case OPT_linkrepro:
267 case OPT_INPUT:
268 case OPT_defaultlib:
269 case OPT_libpath:
270 break;
271 default:
272 OS << stringize(Arg) << "\n";
273 }
274 }
275
276 for (StringRef Path : SearchPaths) {
277 std::string RelPath = relativeToRoot(Path);
278 OS << "/libpath:" << quote(RelPath) << "\n";
279 }
280
281 for (MemoryBufferRef MB : MBs) {
282 std::string InputPath = relativeToRoot(MB.getBufferIdentifier());
283 OS << quote(InputPath) << "\n";
284 }
285
286 return Data.str();
287}
288
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000289static unsigned getDefaultDebugType(const llvm::opt::InputArgList &Args) {
290 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
291 if (Args.hasArg(OPT_driver))
292 DebugTypes |= static_cast<unsigned>(DebugType::PData);
293 if (Args.hasArg(OPT_profile))
294 DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
295 return DebugTypes;
296}
297
298static unsigned parseDebugType(StringRef Arg) {
299 llvm::SmallVector<StringRef, 3> Types;
300 Arg.split(Types, ',', /*KeepEmpty=*/false);
301
302 unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
303 for (StringRef Type : Types)
304 DebugTypes |= StringSwitch<unsigned>(Type.lower())
305 .Case("cv", static_cast<unsigned>(DebugType::CV))
306 .Case("pdata", static_cast<unsigned>(DebugType::PData))
307 .Case("fixup", static_cast<unsigned>(DebugType::Fixup));
308 return DebugTypes;
309}
310
Rafael Espindolab835ae82015-08-06 14:58:50 +0000311void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000312 // If the first command line argument is "/lib", link.exe acts like lib.exe.
313 // We call our own implementation of lib.exe that understands bitcode files.
314 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
315 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000316 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000317 return;
318 }
319
Peter Collingbourne60c16162015-06-01 20:10:10 +0000320 // Needed for LTO.
321 llvm::InitializeAllTargetInfos();
322 llvm::InitializeAllTargets();
323 llvm::InitializeAllTargetMCs();
324 llvm::InitializeAllAsmParsers();
325 llvm::InitializeAllAsmPrinters();
326 llvm::InitializeAllDisassemblers();
327
Rui Ueyama411c63602015-05-28 19:09:30 +0000328 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000329 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000330
Rui Ueyama5c726432015-05-29 16:11:52 +0000331 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000332 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000333 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000334 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000335 }
336
Peter Collingbournefeee2102016-07-26 02:00:42 +0000337 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
338 SmallString<64> Path = StringRef(Arg->getValue());
339 llvm::sys::path::append(Path, "repro");
340 ErrorOr<CpioFile *> F = CpioFile::create(Path);
341 if (F)
342 Cpio.reset(*F);
343 else
344 llvm::errs() << "/linkrepro: failed to open " << Path
345 << ".cpio: " << F.getError().message() << '\n';
346 }
347
Rafael Espindolab835ae82015-08-06 14:58:50 +0000348 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000349 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000350
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000351 // Construct search path list.
352 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000353 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000354 SearchPaths.push_back(Arg->getValue());
355 addLibSearchPaths();
356
Rui Ueyamaad660982015-06-07 00:20:32 +0000357 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000358 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000359 Config->OutputFile = Arg->getValue();
360
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000361 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000362 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000363 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000364
Rui Ueyama95925fd2015-06-28 19:35:15 +0000365 // Handle /force or /force:unresolved
366 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
367 Config->Force = true;
368
Rui Ueyama6600eb12015-07-04 23:37:32 +0000369 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000370 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000371 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000372 Config->DebugTypes =
373 Args.hasArg(OPT_debugtype)
374 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
375 : getDefaultDebugType(Args);
376 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000377
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000378 // Create a dummy PDB file to satisfy build sytem rules.
Rui Ueyama9f66f822016-10-11 19:45:07 +0000379 if (auto *Arg = Args.getLastArg(OPT_pdb))
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000380 Config->PDBPath = Arg->getValue();
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000381
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000382 // Handle /noentry
383 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000384 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000385 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000386 Config->NoEntry = true;
387 }
388
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000389 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000390 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000391 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000392 Config->ManifestID = 2;
393 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000394
Rui Ueyama588e8322015-06-15 01:23:58 +0000395 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000396 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000397 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000398 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000399 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000400 Config->DynamicBase = false;
401 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000402
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000403 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000404 if (auto *Arg = Args.getLastArg(OPT_machine))
405 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000406
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000407 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000408 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000409 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
410
411 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000412 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000413 Config->NoDefaultLibAll = true;
414
Rui Ueyama804a8b62015-05-29 16:18:15 +0000415 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000416 if (auto *Arg = Args.getLastArg(OPT_base))
417 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000418
419 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000420 if (auto *Arg = Args.getLastArg(OPT_stack))
421 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000422
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000423 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000424 if (auto *Arg = Args.getLastArg(OPT_heap))
425 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000426
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000427 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000428 if (auto *Arg = Args.getLastArg(OPT_version))
429 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
430 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000431
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000432 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000433 if (auto *Arg = Args.getLastArg(OPT_subsystem))
434 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
435 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000436
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000437 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000438 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000439 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000440
Rui Ueyama08d5e182015-06-18 23:20:11 +0000441 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000442 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000443 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000444
Rui Ueyamab95188c2015-06-18 20:27:09 +0000445 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000446 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000447 Config->Implib = Arg->getValue();
448
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000449 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000450 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000451 std::string Str = StringRef(Arg->getValue()).lower();
452 SmallVector<StringRef, 1> Vec;
453 StringRef(Str).split(Vec, ',');
454 for (StringRef S : Vec) {
455 if (S == "noref") {
456 Config->DoGC = false;
457 Config->DoICF = false;
458 continue;
459 }
460 if (S == "icf" || StringRef(S).startswith("icf=")) {
461 Config->DoICF = true;
462 continue;
463 }
464 if (S == "noicf") {
465 Config->DoICF = false;
466 continue;
467 }
468 if (StringRef(S).startswith("lldlto=")) {
469 StringRef OptLevel = StringRef(S).substr(7);
470 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
471 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000472 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000473 continue;
474 }
475 if (StringRef(S).startswith("lldltojobs=")) {
476 StringRef Jobs = StringRef(S).substr(11);
477 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000478 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000479 continue;
480 }
481 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000482 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000483 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000484 }
485
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000486 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000487 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000488 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000489
Rui Ueyama6600eb12015-07-04 23:37:32 +0000490 // Handle /merge
491 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000492 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000493
Rui Ueyama440138c2016-06-20 03:39:39 +0000494 // Handle /section
495 for (auto *Arg : Args.filtered(OPT_section))
496 parseSection(Arg->getValue());
497
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000498 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000499 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
500 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000501
502 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000503 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
504 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000505
506 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000507 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000508 Config->ManifestDependency = Arg->getValue();
509
510 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000511 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000512 Config->ManifestFile = Arg->getValue();
513
Rui Ueyamaafb19012016-04-19 01:21:58 +0000514 // Handle /manifestinput
515 for (auto *Arg : Args.filtered(OPT_manifestinput))
516 Config->ManifestInput.push_back(Arg->getValue());
517
Rui Ueyama6592ff82015-06-16 23:13:00 +0000518 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000519 if (Args.hasArg(OPT_allowbind_no))
520 Config->AllowBind = false;
521 if (Args.hasArg(OPT_allowisolation_no))
522 Config->AllowIsolation = false;
523 if (Args.hasArg(OPT_dynamicbase_no))
524 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000525 if (Args.hasArg(OPT_nxcompat_no))
526 Config->NxCompat = false;
527 if (Args.hasArg(OPT_tsaware_no))
528 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000529 if (Args.hasArg(OPT_nosymtab))
530 Config->WriteSymtab = false;
Rui Ueyamabe939b32016-11-21 17:22:35 +0000531 Config->DumpPdb = Args.hasArg(OPT_dumppdb);
Rui Ueyama6592ff82015-06-16 23:13:00 +0000532
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000533 // Create a list of input files. Files can be given as arguments
534 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000535 std::vector<StringRef> Paths;
536 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000537 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000538 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000539 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000540 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000541 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000542 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000543 for (StringRef Path : Paths)
544 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000545
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000546 // Windows specific -- Create a resource file containing a manifest file.
547 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000548 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000549 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000550 OwningMBs.push_back(std::move(MB)); // take ownership
551 }
552
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000553 // Windows specific -- Input files can be Windows resource files (.res files).
554 // We invoke cvtres.exe to convert resource files to a regular COFF file
555 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000556 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000557 auto NotResource = [](MemoryBufferRef MB) {
558 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000559 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000560 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
561 if (It != MBs.end()) {
562 Resources.insert(Resources.end(), It, MBs.end());
563 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000564 }
565
Rui Ueyama85225b02015-07-02 03:15:15 +0000566 // Read all input files given via the command line. Note that step()
567 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000568 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000569 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000570 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000571
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000572 // Determine machine type and check if all object files are
573 // for the same CPU type. Note that this needs to be done before
574 // any call to mangle().
575 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
576 MachineTypes MT = File->getMachineType();
577 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
578 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000579 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
580 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000581 continue;
582 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000583 if (Config->Machine != MT)
Rui Ueyamaa45d45e2016-12-07 23:17:02 +0000584 fatal(toString(File.get()) + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000585 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000586 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000587 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000588 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000589 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000590 }
591
592 // Windows specific -- Convert Windows resource files to a COFF file.
593 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000594 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000595 Symtab.addFile(createFile(MB->getMemBufferRef()));
Peter Collingbournefeee2102016-07-26 02:00:42 +0000596
597 MBs.push_back(MB->getMemBufferRef());
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000598 OwningMBs.push_back(std::move(MB)); // take ownership
599 }
600
Peter Collingbournefeee2102016-07-26 02:00:42 +0000601 if (Cpio)
602 Cpio->append("response.txt",
603 createResponseFile(Args, MBs,
604 ArrayRef<StringRef>(SearchPaths).slice(1)));
605
Rui Ueyama4d545342015-07-28 03:12:00 +0000606 // Handle /largeaddressaware
607 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
608 Config->LargeAddressAware = true;
609
Rui Ueyamad68e2112015-07-28 03:15:57 +0000610 // Handle /highentropyva
611 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
612 Config->HighEntropyVA = true;
613
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000614 // Handle /entry and /dll
615 if (auto *Arg = Args.getLastArg(OPT_entry)) {
616 Config->Entry = addUndefined(mangle(Arg->getValue()));
617 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000618 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
619 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000620 Config->Entry = addUndefined(S);
621 } else if (!Config->NoEntry) {
622 // Windows specific -- If entry point name is not given, we need to
623 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000624 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000625 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000626 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000627 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000628 if (Config->Verbose)
629 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000630 }
631
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000632 // Handle /export
633 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000634 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000635 if (Config->Machine == I386) {
636 if (!isDecorated(E.Name))
637 E.Name = Alloc.save("_" + E.Name);
638 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
639 E.ExtName = Alloc.save("_" + E.ExtName);
640 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000641 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000642 }
643
644 // Handle /def
645 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000646 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000647 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000648 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000649 }
650
Rui Ueyama6d249082015-07-13 22:31:45 +0000651 // Handle /delayload
652 for (auto *Arg : Args.filtered(OPT_delayload)) {
653 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000654 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000655 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000656 } else {
657 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000658 }
659 }
660
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000661 // Set default image base if /base is not given.
662 if (Config->ImageBase == uint64_t(-1))
663 Config->ImageBase = getDefaultImageBase();
664
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000665 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000666 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000667 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
668 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
669 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000670
Rui Ueyama107db552015-08-09 21:01:06 +0000671 // We do not support /guard:cf (control flow protection) yet.
672 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
673 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
674 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
675 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
676
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000677 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000678 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000679
680 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000681 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
682 // A new file may contain a directive section to add new command line options.
683 // That's why we have to repeat until converge.)
684 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000685 // Windows specific -- if entry point is not found,
686 // search for its mangled names.
687 if (Config->Entry)
688 Symtab.mangleMaybe(Config->Entry);
689
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000690 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000691 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000692 if (!E.ForwardTo.empty())
693 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000694 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000695 if (!E.Directives)
696 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000697 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000698
699 // Add weak aliases. Weak aliases is a mechanism to give remaining
700 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000701 for (auto Pair : Config->AlternateNames) {
702 StringRef From = Pair.first;
703 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000704 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000705 if (!Sym)
706 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000707 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000708 if (!U->WeakAlias)
709 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000710 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000711
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000712 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000713 if (Symtab.findUnderscore("_load_config_used"))
714 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000715
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000716 if (Symtab.queueEmpty())
717 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000718 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000719 }
720
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000721 // Do LTO by compiling bitcode input files to a set of native COFF files then
722 // link those files.
723 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000724
Peter Collingbourne2612a322015-07-04 05:28:41 +0000725 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000726 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000727
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000728 // Windows specific -- if no /subsystem is given, we need to infer
729 // that from entry point name.
730 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000731 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000732 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000733 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000734 }
735
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000736 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000737 if (Args.hasArg(OPT_safeseh))
738 for (ObjectFile *File : Symtab.ObjectFiles)
739 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000740 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000741
Rui Ueyama151d8622015-06-17 20:40:43 +0000742 // Windows specific -- when we are creating a .dll file, we also
743 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000744 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000745 fixupExports();
746 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000747 assignExportOrdinals();
748 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000749
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000750 // Windows specific -- Create a side-by-side manifest file.
751 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000752 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000753
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000754 // Identify unreferenced COMDAT sections.
755 if (Config->DoGC)
756 markLive(Symtab.getChunks());
757
758 // Identify identical COMDAT sections to merge them.
759 if (Config->DoICF)
760 doICF(Symtab.getChunks());
761
Rui Ueyama411c63602015-05-28 19:09:30 +0000762 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000763 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000764
Rui Ueyama016414f2015-06-28 20:07:08 +0000765 // Create a symbol map file containing symbol VAs and their names
766 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000767 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
768 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000769 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000770 if (EC)
Rui Ueyamabb579542016-07-15 01:12:24 +0000771 fatal(EC, "could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000772 Symtab.printMap(Out);
773 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000774 // Call exit to avoid calling destructors.
775 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000776}
777
Rui Ueyama411c63602015-05-28 19:09:30 +0000778} // namespace coff
779} // namespace lld