blob: bb6a60e4fc4c102a0620f5a45612c6897a6cf257 [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"
Peter Collingbournebd1cb792015-06-09 21:52:48 +000019#include "llvm/LibDriver/LibDriver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000020#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/Option.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000023#include "llvm/Support/Debug.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000024#include "llvm/Support/Path.h"
Rui Ueyama54b71da2015-05-31 19:17:12 +000025#include "llvm/Support/Process.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000026#include "llvm/Support/TargetSelect.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000027#include "llvm/Support/raw_ostream.h"
Rui Ueyama2bf6a122015-06-14 21:50:50 +000028#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000029#include <memory>
30
31using namespace llvm;
Rui Ueyama84936e02015-07-07 23:39:18 +000032using namespace llvm::COFF;
Rui Ueyama54b71da2015-05-31 19:17:12 +000033using llvm::sys::Process;
Peter Collingbournebaf5f872015-06-26 19:20:09 +000034using llvm::sys::fs::OpenFlags;
Rui Ueyama711cd2d2015-05-31 21:17:10 +000035using llvm::sys::fs::file_magic;
36using llvm::sys::fs::identify_magic;
Rui Ueyama411c63602015-05-28 19:09:30 +000037
Rui Ueyama3500f662015-05-28 20:30:06 +000038namespace lld {
39namespace coff {
Rui Ueyama411c63602015-05-28 19:09:30 +000040
Rui Ueyama3500f662015-05-28 20:30:06 +000041Configuration *Config;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000042LinkerDriver *Driver;
43
Rui Ueyama417553d2016-02-28 19:54:51 +000044bool link(llvm::ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000045 Configuration C;
46 LinkerDriver D;
47 Config = &C;
48 Driver = &D;
Rui Ueyama417553d2016-02-28 19:54:51 +000049 Driver->link(Args);
50 return true;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000051}
Rui Ueyama411c63602015-05-28 19:09:30 +000052
Nico Weber5660de72016-04-20 22:34:15 +000053// Drop directory components and replace extension with ".exe" or ".dll".
Rui Ueyamaad660982015-06-07 00:20:32 +000054static std::string getOutputPath(StringRef Path) {
55 auto P = Path.find_last_of("\\/");
56 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
Nico Weber5660de72016-04-20 22:34:15 +000057 const char* E = Config->DLL ? ".dll" : ".exe";
58 return (S.substr(0, S.rfind('.')) + E).str();
Rui Ueyama411c63602015-05-28 19:09:30 +000059}
60
Rui Ueyamad7c2f582015-05-31 21:04:56 +000061// Opens a file. Path has to be resolved already.
62// Newly created memory buffers are owned by this driver.
Rafael Espindolab835ae82015-08-06 14:58:50 +000063MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000064 std::unique_ptr<MemoryBuffer> MB =
Rui Ueyamabb579542016-07-15 01:12:24 +000065 check(MemoryBuffer::getFile(Path), "could not open " + Path);
Rui Ueyamad7c2f582015-05-31 21:04:56 +000066 MemoryBufferRef MBRef = MB->getMemBufferRef();
67 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000068 return MBRef;
69}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000070
Rui Ueyama2bf6a122015-06-14 21:50:50 +000071static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Rui Ueyama711cd2d2015-05-31 21:17:10 +000072 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000073 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000074 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000075 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000076 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000077 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000078 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000079 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
80 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000081}
82
Rui Ueyamaf10a3202015-08-31 08:43:21 +000083static bool isDecorated(StringRef Sym) {
84 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
85}
86
Rui Ueyama411c63602015-05-28 19:09:30 +000087// Parses .drectve section contents and returns a list of files
88// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000089void LinkerDriver::parseDirectives(StringRef S) {
90 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000091
David Blaikie6521ed92015-06-22 22:06:52 +000092 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000093 switch (Arg->getOption().getID()) {
94 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +000095 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +000096 break;
97 case OPT_defaultlib:
98 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +000099 MemoryBufferRef MB = openFile(*Path);
100 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000101 }
102 break;
103 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000104 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000105 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000106 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000107 break;
108 }
109 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000110 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000111 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000112 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000113 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000114 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000115 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000116 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000117 break;
118 case OPT_nodefaultlib:
119 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
120 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000121 case OPT_section:
122 parseSection(Arg->getValue());
123 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000124 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000125 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000126 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000127 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000128 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000129 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000130 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000131 }
132 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000133}
134
Rui Ueyama54b71da2015-05-31 19:17:12 +0000135// Find file from search paths. You can omit ".obj", this function takes
136// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000137StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000138 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
139 if (hasPathSep)
140 return Filename;
141 bool hasExt = (Filename.find('.') != StringRef::npos);
142 for (StringRef Dir : SearchPaths) {
143 SmallString<128> Path = Dir;
144 llvm::sys::path::append(Path, Filename);
145 if (llvm::sys::fs::exists(Path.str()))
146 return Alloc.save(Path.str());
147 if (!hasExt) {
148 Path.append(".obj");
149 if (llvm::sys::fs::exists(Path.str()))
150 return Alloc.save(Path.str());
151 }
152 }
153 return Filename;
154}
155
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000156// Resolves a file path. This never returns the same path
157// (in that case, it returns None).
158Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
159 StringRef Path = doFindFile(Filename);
160 bool Seen = !VisitedFiles.insert(Path.lower()).second;
161 if (Seen)
162 return None;
163 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000164}
165
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000166// Find library file from search path.
167StringRef LinkerDriver::doFindLib(StringRef Filename) {
168 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000169 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000170 if (!hasExt)
171 Filename = Alloc.save(Filename + ".lib");
172 return doFindFile(Filename);
173}
174
175// Resolves a library path. /nodefaultlib options are taken into
176// consideration. This never returns the same path (in that case,
177// it returns None).
178Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
179 if (Config->NoDefaultLibAll)
180 return None;
181 StringRef Path = doFindLib(Filename);
182 if (Config->NoDefaultLibs.count(Path))
183 return None;
184 bool Seen = !VisitedFiles.insert(Path.lower()).second;
185 if (Seen)
186 return None;
187 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000188}
189
190// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000191void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000192 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
193 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000194 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000195 StringRef Env = Alloc.save(*EnvOpt);
196 while (!Env.empty()) {
197 StringRef Path;
198 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000199 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000200 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000201}
202
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000203Undefined *LinkerDriver::addUndefined(StringRef Name) {
204 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000205 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000206 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000207}
208
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000209// Symbol names are mangled by appending "_" prefix on x86.
210StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000211 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
212 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000213 return Alloc.save("_" + Sym);
214 return Sym;
215}
216
Rui Ueyama45044f42015-06-29 01:03:53 +0000217// Windows specific -- find default entry point name.
218StringRef LinkerDriver::findDefaultEntry() {
219 // User-defined main functions and their corresponding entry points.
220 static const char *Entries[][2] = {
221 {"main", "mainCRTStartup"},
222 {"wmain", "wmainCRTStartup"},
223 {"WinMain", "WinMainCRTStartup"},
224 {"wWinMain", "wWinMainCRTStartup"},
225 };
226 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000227 StringRef Entry = Symtab.findMangle(mangle(E[0]));
228 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000229 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000230 }
231 return "";
232}
233
234WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000235 if (Config->DLL)
236 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000237 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000238 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000239 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000240 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
241 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000242}
243
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000244static uint64_t getDefaultImageBase() {
245 if (Config->is64())
246 return Config->DLL ? 0x180000000 : 0x140000000;
247 return Config->DLL ? 0x10000000 : 0x400000;
248}
249
Rafael Espindolab835ae82015-08-06 14:58:50 +0000250void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000251 // If the first command line argument is "/lib", link.exe acts like lib.exe.
252 // We call our own implementation of lib.exe that understands bitcode files.
253 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
254 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000255 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000256 return;
257 }
258
Peter Collingbourne60c16162015-06-01 20:10:10 +0000259 // Needed for LTO.
260 llvm::InitializeAllTargetInfos();
261 llvm::InitializeAllTargets();
262 llvm::InitializeAllTargetMCs();
263 llvm::InitializeAllAsmParsers();
264 llvm::InitializeAllAsmPrinters();
265 llvm::InitializeAllDisassemblers();
266
Rui Ueyama411c63602015-05-28 19:09:30 +0000267 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000268 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000269
Rui Ueyama5c726432015-05-29 16:11:52 +0000270 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000271 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000272 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000273 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000274 }
275
Rafael Espindolab835ae82015-08-06 14:58:50 +0000276 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000277 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000278
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000279 // Construct search path list.
280 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000281 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000282 SearchPaths.push_back(Arg->getValue());
283 addLibSearchPaths();
284
Rui Ueyamaad660982015-06-07 00:20:32 +0000285 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000286 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000287 Config->OutputFile = Arg->getValue();
288
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000289 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000290 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000291 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000292
Rui Ueyama95925fd2015-06-28 19:35:15 +0000293 // Handle /force or /force:unresolved
294 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
295 Config->Force = true;
296
Rui Ueyama6600eb12015-07-04 23:37:32 +0000297 // Handle /debug
298 if (Args.hasArg(OPT_debug))
299 Config->Debug = true;
300
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000301 // Handle /noentry
302 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000303 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000304 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000305 Config->NoEntry = true;
306 }
307
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000308 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000309 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000310 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000311 Config->ManifestID = 2;
312 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000313
Rui Ueyama588e8322015-06-15 01:23:58 +0000314 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000315 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000316 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000317 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000318 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000319 Config->DynamicBase = false;
320 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000321
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000322 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000323 if (auto *Arg = Args.getLastArg(OPT_machine))
324 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000325
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000326 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000327 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000328 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
329
330 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000331 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000332 Config->NoDefaultLibAll = true;
333
Rui Ueyama804a8b62015-05-29 16:18:15 +0000334 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000335 if (auto *Arg = Args.getLastArg(OPT_base))
336 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000337
338 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000339 if (auto *Arg = Args.getLastArg(OPT_stack))
340 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000341
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000342 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000343 if (auto *Arg = Args.getLastArg(OPT_heap))
344 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000345
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000346 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000347 if (auto *Arg = Args.getLastArg(OPT_version))
348 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
349 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000350
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000351 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000352 if (auto *Arg = Args.getLastArg(OPT_subsystem))
353 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
354 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000355
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000356 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000357 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000358 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000359
Rui Ueyama08d5e182015-06-18 23:20:11 +0000360 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000361 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000362 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000363
Rui Ueyamab95188c2015-06-18 20:27:09 +0000364 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000365 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000366 Config->Implib = Arg->getValue();
367
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000368 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000369 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000370 std::string Str = StringRef(Arg->getValue()).lower();
371 SmallVector<StringRef, 1> Vec;
372 StringRef(Str).split(Vec, ',');
373 for (StringRef S : Vec) {
374 if (S == "noref") {
375 Config->DoGC = false;
376 Config->DoICF = false;
377 continue;
378 }
379 if (S == "icf" || StringRef(S).startswith("icf=")) {
380 Config->DoICF = true;
381 continue;
382 }
383 if (S == "noicf") {
384 Config->DoICF = false;
385 continue;
386 }
387 if (StringRef(S).startswith("lldlto=")) {
388 StringRef OptLevel = StringRef(S).substr(7);
389 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
390 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000391 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000392 continue;
393 }
394 if (StringRef(S).startswith("lldltojobs=")) {
395 StringRef Jobs = StringRef(S).substr(11);
396 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000397 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000398 continue;
399 }
400 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000401 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000402 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000403 }
404
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000405 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000406 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000407 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000408
Rui Ueyama6600eb12015-07-04 23:37:32 +0000409 // Handle /merge
410 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000411 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000412
Rui Ueyama440138c2016-06-20 03:39:39 +0000413 // Handle /section
414 for (auto *Arg : Args.filtered(OPT_section))
415 parseSection(Arg->getValue());
416
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000417 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000418 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
419 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000420
421 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000422 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
423 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000424
425 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000426 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000427 Config->ManifestDependency = Arg->getValue();
428
429 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000430 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000431 Config->ManifestFile = Arg->getValue();
432
Rui Ueyamaafb19012016-04-19 01:21:58 +0000433 // Handle /manifestinput
434 for (auto *Arg : Args.filtered(OPT_manifestinput))
435 Config->ManifestInput.push_back(Arg->getValue());
436
Rui Ueyama6592ff82015-06-16 23:13:00 +0000437 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000438 if (Args.hasArg(OPT_allowbind_no))
439 Config->AllowBind = false;
440 if (Args.hasArg(OPT_allowisolation_no))
441 Config->AllowIsolation = false;
442 if (Args.hasArg(OPT_dynamicbase_no))
443 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000444 if (Args.hasArg(OPT_nxcompat_no))
445 Config->NxCompat = false;
446 if (Args.hasArg(OPT_tsaware_no))
447 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000448 if (Args.hasArg(OPT_nosymtab))
449 Config->WriteSymtab = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000450
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000451 // Create a list of input files. Files can be given as arguments
452 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000453 std::vector<StringRef> Paths;
454 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000455 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000456 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000457 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000458 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000459 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000460 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000461 for (StringRef Path : Paths)
462 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000463
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000464 // Windows specific -- Create a resource file containing a manifest file.
465 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000466 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000467 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000468 OwningMBs.push_back(std::move(MB)); // take ownership
469 }
470
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000471 // Windows specific -- Input files can be Windows resource files (.res files).
472 // We invoke cvtres.exe to convert resource files to a regular COFF file
473 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000474 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000475 auto NotResource = [](MemoryBufferRef MB) {
476 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000477 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000478 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
479 if (It != MBs.end()) {
480 Resources.insert(Resources.end(), It, MBs.end());
481 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000482 }
483
Rui Ueyama85225b02015-07-02 03:15:15 +0000484 // Read all input files given via the command line. Note that step()
485 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000486 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000487 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000488 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000489
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000490 // Determine machine type and check if all object files are
491 // for the same CPU type. Note that this needs to be done before
492 // any call to mangle().
493 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
494 MachineTypes MT = File->getMachineType();
495 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
496 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000497 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
498 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000499 continue;
500 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000501 if (Config->Machine != MT)
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000502 fatal(File->getShortName() + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000503 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000504 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000505 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000506 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000507 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000508 }
509
510 // Windows specific -- Convert Windows resource files to a COFF file.
511 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000512 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000513 Symtab.addFile(createFile(MB->getMemBufferRef()));
514 OwningMBs.push_back(std::move(MB)); // take ownership
515 }
516
Rui Ueyama4d545342015-07-28 03:12:00 +0000517 // Handle /largeaddressaware
518 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
519 Config->LargeAddressAware = true;
520
Rui Ueyamad68e2112015-07-28 03:15:57 +0000521 // Handle /highentropyva
522 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
523 Config->HighEntropyVA = true;
524
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000525 // Handle /entry and /dll
526 if (auto *Arg = Args.getLastArg(OPT_entry)) {
527 Config->Entry = addUndefined(mangle(Arg->getValue()));
528 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000529 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
530 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000531 Config->Entry = addUndefined(S);
532 } else if (!Config->NoEntry) {
533 // Windows specific -- If entry point name is not given, we need to
534 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000535 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000536 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000537 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000538 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000539 if (Config->Verbose)
540 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000541 }
542
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000543 // Handle /export
544 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000545 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000546 if (Config->Machine == I386) {
547 if (!isDecorated(E.Name))
548 E.Name = Alloc.save("_" + E.Name);
549 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
550 E.ExtName = Alloc.save("_" + E.ExtName);
551 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000552 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000553 }
554
555 // Handle /def
556 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000557 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000558 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000559 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000560 }
561
Rui Ueyama6d249082015-07-13 22:31:45 +0000562 // Handle /delayload
563 for (auto *Arg : Args.filtered(OPT_delayload)) {
564 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000565 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000566 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000567 } else {
568 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000569 }
570 }
571
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000572 // Set default image base if /base is not given.
573 if (Config->ImageBase == uint64_t(-1))
574 Config->ImageBase = getDefaultImageBase();
575
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000576 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000577 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000578 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
579 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
580 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000581
Rui Ueyama107db552015-08-09 21:01:06 +0000582 // We do not support /guard:cf (control flow protection) yet.
583 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
584 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
585 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
586 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
587
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000588 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000589 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000590
591 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000592 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
593 // A new file may contain a directive section to add new command line options.
594 // That's why we have to repeat until converge.)
595 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000596 // Windows specific -- if entry point is not found,
597 // search for its mangled names.
598 if (Config->Entry)
599 Symtab.mangleMaybe(Config->Entry);
600
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000601 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000602 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000603 if (!E.ForwardTo.empty())
604 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000605 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000606 if (!E.Directives)
607 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000608 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000609
610 // Add weak aliases. Weak aliases is a mechanism to give remaining
611 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000612 for (auto Pair : Config->AlternateNames) {
613 StringRef From = Pair.first;
614 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000615 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000616 if (!Sym)
617 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000618 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000619 if (!U->WeakAlias)
620 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000621 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000622
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000623 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000624 if (Symtab.findUnderscore("_load_config_used"))
625 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000626
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000627 if (Symtab.queueEmpty())
628 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000629 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000630 }
631
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000632 // Do LTO by compiling bitcode input files to a set of native COFF files then
633 // link those files.
634 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000635
Peter Collingbourne2612a322015-07-04 05:28:41 +0000636 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000637 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000638
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000639 // Windows specific -- if no /subsystem is given, we need to infer
640 // that from entry point name.
641 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000642 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000643 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000644 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000645 }
646
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000647 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000648 if (Args.hasArg(OPT_safeseh))
649 for (ObjectFile *File : Symtab.ObjectFiles)
650 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000651 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000652
Rui Ueyama151d8622015-06-17 20:40:43 +0000653 // Windows specific -- when we are creating a .dll file, we also
654 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000655 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000656 fixupExports();
657 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000658 assignExportOrdinals();
659 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000660
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000661 // Windows specific -- Create a side-by-side manifest file.
662 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000663 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000664
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000665 // Create a dummy PDB file to satisfy build sytem rules.
666 if (auto *Arg = Args.getLastArg(OPT_pdb))
Rui Ueyamae7378242015-12-04 23:11:05 +0000667 createPDB(Arg->getValue());
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000668
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000669 // Identify unreferenced COMDAT sections.
670 if (Config->DoGC)
671 markLive(Symtab.getChunks());
672
673 // Identify identical COMDAT sections to merge them.
674 if (Config->DoICF)
675 doICF(Symtab.getChunks());
676
Rui Ueyama411c63602015-05-28 19:09:30 +0000677 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000678 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000679
Rui Ueyama016414f2015-06-28 20:07:08 +0000680 // Create a symbol map file containing symbol VAs and their names
681 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000682 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
683 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000684 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000685 if (EC)
Rui Ueyamabb579542016-07-15 01:12:24 +0000686 fatal(EC, "could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000687 Symtab.printMap(Out);
688 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000689 // Call exit to avoid calling destructors.
690 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000691}
692
Rui Ueyama411c63602015-05-28 19:09:30 +0000693} // namespace coff
694} // namespace lld