blob: 7f8e580d75e58d9abbe8e910f50c1539c6f7dd16 [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 Ueyamad7c2f582015-05-31 21:04:56 +000064 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama0d09a862016-07-15 00:40:46 +000065 if (auto EC = MBOrErr.getError())
66 fatal(EC, "Could not open " + Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +000067 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamad7c2f582015-05-31 21:04:56 +000068 MemoryBufferRef MBRef = MB->getMemBufferRef();
69 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000070 return MBRef;
71}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000072
Rui Ueyama2bf6a122015-06-14 21:50:50 +000073static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Rui Ueyama711cd2d2015-05-31 21:17:10 +000074 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000075 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000076 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000077 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000078 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000079 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000080 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000081 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
82 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000083}
84
Rui Ueyamaf10a3202015-08-31 08:43:21 +000085static bool isDecorated(StringRef Sym) {
86 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
87}
88
Rui Ueyama411c63602015-05-28 19:09:30 +000089// Parses .drectve section contents and returns a list of files
90// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000091void LinkerDriver::parseDirectives(StringRef S) {
92 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000093
David Blaikie6521ed92015-06-22 22:06:52 +000094 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000095 switch (Arg->getOption().getID()) {
96 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +000097 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +000098 break;
99 case OPT_defaultlib:
100 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000101 MemoryBufferRef MB = openFile(*Path);
102 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000103 }
104 break;
105 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000106 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000107 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000108 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000109 break;
110 }
111 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000112 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000113 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000114 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000115 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000116 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000117 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000118 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000119 break;
120 case OPT_nodefaultlib:
121 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
122 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000123 case OPT_section:
124 parseSection(Arg->getValue());
125 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000126 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000127 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000128 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000129 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000130 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000131 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000132 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000133 }
134 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000135}
136
Rui Ueyama54b71da2015-05-31 19:17:12 +0000137// Find file from search paths. You can omit ".obj", this function takes
138// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000139StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000140 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
141 if (hasPathSep)
142 return Filename;
143 bool hasExt = (Filename.find('.') != StringRef::npos);
144 for (StringRef Dir : SearchPaths) {
145 SmallString<128> Path = Dir;
146 llvm::sys::path::append(Path, Filename);
147 if (llvm::sys::fs::exists(Path.str()))
148 return Alloc.save(Path.str());
149 if (!hasExt) {
150 Path.append(".obj");
151 if (llvm::sys::fs::exists(Path.str()))
152 return Alloc.save(Path.str());
153 }
154 }
155 return Filename;
156}
157
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000158// Resolves a file path. This never returns the same path
159// (in that case, it returns None).
160Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
161 StringRef Path = doFindFile(Filename);
162 bool Seen = !VisitedFiles.insert(Path.lower()).second;
163 if (Seen)
164 return None;
165 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000166}
167
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000168// Find library file from search path.
169StringRef LinkerDriver::doFindLib(StringRef Filename) {
170 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000171 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000172 if (!hasExt)
173 Filename = Alloc.save(Filename + ".lib");
174 return doFindFile(Filename);
175}
176
177// Resolves a library path. /nodefaultlib options are taken into
178// consideration. This never returns the same path (in that case,
179// it returns None).
180Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
181 if (Config->NoDefaultLibAll)
182 return None;
183 StringRef Path = doFindLib(Filename);
184 if (Config->NoDefaultLibs.count(Path))
185 return None;
186 bool Seen = !VisitedFiles.insert(Path.lower()).second;
187 if (Seen)
188 return None;
189 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000190}
191
192// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000193void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000194 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
195 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000196 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000197 StringRef Env = Alloc.save(*EnvOpt);
198 while (!Env.empty()) {
199 StringRef Path;
200 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000201 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000202 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000203}
204
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000205Undefined *LinkerDriver::addUndefined(StringRef Name) {
206 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000207 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000208 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000209}
210
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000211// Symbol names are mangled by appending "_" prefix on x86.
212StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000213 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
214 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000215 return Alloc.save("_" + Sym);
216 return Sym;
217}
218
Rui Ueyama45044f42015-06-29 01:03:53 +0000219// Windows specific -- find default entry point name.
220StringRef LinkerDriver::findDefaultEntry() {
221 // User-defined main functions and their corresponding entry points.
222 static const char *Entries[][2] = {
223 {"main", "mainCRTStartup"},
224 {"wmain", "wmainCRTStartup"},
225 {"WinMain", "WinMainCRTStartup"},
226 {"wWinMain", "wWinMainCRTStartup"},
227 };
228 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000229 StringRef Entry = Symtab.findMangle(mangle(E[0]));
230 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000231 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000232 }
233 return "";
234}
235
236WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000237 if (Config->DLL)
238 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000239 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000240 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000241 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000242 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
243 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000244}
245
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000246static uint64_t getDefaultImageBase() {
247 if (Config->is64())
248 return Config->DLL ? 0x180000000 : 0x140000000;
249 return Config->DLL ? 0x10000000 : 0x400000;
250}
251
Rafael Espindolab835ae82015-08-06 14:58:50 +0000252void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000253 // If the first command line argument is "/lib", link.exe acts like lib.exe.
254 // We call our own implementation of lib.exe that understands bitcode files.
255 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
256 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000257 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000258 return;
259 }
260
Peter Collingbourne60c16162015-06-01 20:10:10 +0000261 // Needed for LTO.
262 llvm::InitializeAllTargetInfos();
263 llvm::InitializeAllTargets();
264 llvm::InitializeAllTargetMCs();
265 llvm::InitializeAllAsmParsers();
266 llvm::InitializeAllAsmPrinters();
267 llvm::InitializeAllDisassemblers();
268
Rui Ueyama411c63602015-05-28 19:09:30 +0000269 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000270 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000271
Rui Ueyama5c726432015-05-29 16:11:52 +0000272 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000273 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000274 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000275 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000276 }
277
Rafael Espindolab835ae82015-08-06 14:58:50 +0000278 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyama60604792016-07-14 23:37:14 +0000279 fatal("no input files.");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000280
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000281 // Construct search path list.
282 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000283 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000284 SearchPaths.push_back(Arg->getValue());
285 addLibSearchPaths();
286
Rui Ueyamaad660982015-06-07 00:20:32 +0000287 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000288 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000289 Config->OutputFile = Arg->getValue();
290
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000291 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000292 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000293 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000294
Rui Ueyama95925fd2015-06-28 19:35:15 +0000295 // Handle /force or /force:unresolved
296 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
297 Config->Force = true;
298
Rui Ueyama6600eb12015-07-04 23:37:32 +0000299 // Handle /debug
300 if (Args.hasArg(OPT_debug))
301 Config->Debug = true;
302
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000303 // Handle /noentry
304 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000305 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000306 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000307 Config->NoEntry = true;
308 }
309
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000310 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000311 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000312 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000313 Config->ManifestID = 2;
314 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000315
Rui Ueyama588e8322015-06-15 01:23:58 +0000316 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000317 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000318 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000319 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000320 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000321 Config->DynamicBase = false;
322 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000323
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000324 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000325 if (auto *Arg = Args.getLastArg(OPT_machine))
326 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000327
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000328 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000329 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000330 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
331
332 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000333 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000334 Config->NoDefaultLibAll = true;
335
Rui Ueyama804a8b62015-05-29 16:18:15 +0000336 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000337 if (auto *Arg = Args.getLastArg(OPT_base))
338 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000339
340 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000341 if (auto *Arg = Args.getLastArg(OPT_stack))
342 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000343
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000344 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000345 if (auto *Arg = Args.getLastArg(OPT_heap))
346 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000347
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000348 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000349 if (auto *Arg = Args.getLastArg(OPT_version))
350 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
351 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000352
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000353 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000354 if (auto *Arg = Args.getLastArg(OPT_subsystem))
355 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
356 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000357
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000358 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000359 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000360 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000361
Rui Ueyama08d5e182015-06-18 23:20:11 +0000362 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000363 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000364 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000365
Rui Ueyamab95188c2015-06-18 20:27:09 +0000366 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000367 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000368 Config->Implib = Arg->getValue();
369
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000370 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000371 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000372 std::string Str = StringRef(Arg->getValue()).lower();
373 SmallVector<StringRef, 1> Vec;
374 StringRef(Str).split(Vec, ',');
375 for (StringRef S : Vec) {
376 if (S == "noref") {
377 Config->DoGC = false;
378 Config->DoICF = false;
379 continue;
380 }
381 if (S == "icf" || StringRef(S).startswith("icf=")) {
382 Config->DoICF = true;
383 continue;
384 }
385 if (S == "noicf") {
386 Config->DoICF = false;
387 continue;
388 }
389 if (StringRef(S).startswith("lldlto=")) {
390 StringRef OptLevel = StringRef(S).substr(7);
391 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
392 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000393 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000394 continue;
395 }
396 if (StringRef(S).startswith("lldltojobs=")) {
397 StringRef Jobs = StringRef(S).substr(11);
398 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000399 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000400 continue;
401 }
402 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000403 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000404 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000405 }
406
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000407 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000408 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000409 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000410
Rui Ueyama6600eb12015-07-04 23:37:32 +0000411 // Handle /merge
412 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000413 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000414
Rui Ueyama440138c2016-06-20 03:39:39 +0000415 // Handle /section
416 for (auto *Arg : Args.filtered(OPT_section))
417 parseSection(Arg->getValue());
418
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000419 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000420 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
421 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000422
423 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000424 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
425 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000426
427 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000428 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000429 Config->ManifestDependency = Arg->getValue();
430
431 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000432 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000433 Config->ManifestFile = Arg->getValue();
434
Rui Ueyamaafb19012016-04-19 01:21:58 +0000435 // Handle /manifestinput
436 for (auto *Arg : Args.filtered(OPT_manifestinput))
437 Config->ManifestInput.push_back(Arg->getValue());
438
Rui Ueyama6592ff82015-06-16 23:13:00 +0000439 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000440 if (Args.hasArg(OPT_allowbind_no))
441 Config->AllowBind = false;
442 if (Args.hasArg(OPT_allowisolation_no))
443 Config->AllowIsolation = false;
444 if (Args.hasArg(OPT_dynamicbase_no))
445 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000446 if (Args.hasArg(OPT_nxcompat_no))
447 Config->NxCompat = false;
448 if (Args.hasArg(OPT_tsaware_no))
449 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000450 if (Args.hasArg(OPT_nosymtab))
451 Config->WriteSymtab = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000452
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000453 // Create a list of input files. Files can be given as arguments
454 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000455 std::vector<StringRef> Paths;
456 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000457 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000458 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000459 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000460 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000461 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000462 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000463 for (StringRef Path : Paths)
464 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000465
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000466 // Windows specific -- Create a resource file containing a manifest file.
467 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000468 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000469 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000470 OwningMBs.push_back(std::move(MB)); // take ownership
471 }
472
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000473 // Windows specific -- Input files can be Windows resource files (.res files).
474 // We invoke cvtres.exe to convert resource files to a regular COFF file
475 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000476 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000477 auto NotResource = [](MemoryBufferRef MB) {
478 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000479 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000480 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
481 if (It != MBs.end()) {
482 Resources.insert(Resources.end(), It, MBs.end());
483 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000484 }
485
Rui Ueyama85225b02015-07-02 03:15:15 +0000486 // Read all input files given via the command line. Note that step()
487 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000488 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000489 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000490 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000491
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000492 // Determine machine type and check if all object files are
493 // for the same CPU type. Note that this needs to be done before
494 // any call to mangle().
495 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
496 MachineTypes MT = File->getMachineType();
497 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
498 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000499 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
500 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000501 continue;
502 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000503 if (Config->Machine != MT)
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000504 fatal(File->getShortName() + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000505 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000506 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000507 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000508 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000509 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000510 }
511
512 // Windows specific -- Convert Windows resource files to a COFF file.
513 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000514 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000515 Symtab.addFile(createFile(MB->getMemBufferRef()));
516 OwningMBs.push_back(std::move(MB)); // take ownership
517 }
518
Rui Ueyama4d545342015-07-28 03:12:00 +0000519 // Handle /largeaddressaware
520 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
521 Config->LargeAddressAware = true;
522
Rui Ueyamad68e2112015-07-28 03:15:57 +0000523 // Handle /highentropyva
524 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
525 Config->HighEntropyVA = true;
526
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000527 // Handle /entry and /dll
528 if (auto *Arg = Args.getLastArg(OPT_entry)) {
529 Config->Entry = addUndefined(mangle(Arg->getValue()));
530 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000531 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
532 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000533 Config->Entry = addUndefined(S);
534 } else if (!Config->NoEntry) {
535 // Windows specific -- If entry point name is not given, we need to
536 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000537 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000538 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000539 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000540 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000541 if (Config->Verbose)
542 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000543 }
544
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000545 // Handle /export
546 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000547 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000548 if (Config->Machine == I386) {
549 if (!isDecorated(E.Name))
550 E.Name = Alloc.save("_" + E.Name);
551 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
552 E.ExtName = Alloc.save("_" + E.ExtName);
553 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000554 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000555 }
556
557 // Handle /def
558 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000559 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000560 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000561 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000562 }
563
Rui Ueyama6d249082015-07-13 22:31:45 +0000564 // Handle /delayload
565 for (auto *Arg : Args.filtered(OPT_delayload)) {
566 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000567 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000568 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000569 } else {
570 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000571 }
572 }
573
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000574 // Set default image base if /base is not given.
575 if (Config->ImageBase == uint64_t(-1))
576 Config->ImageBase = getDefaultImageBase();
577
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000578 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000579 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000580 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
581 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
582 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000583
Rui Ueyama107db552015-08-09 21:01:06 +0000584 // We do not support /guard:cf (control flow protection) yet.
585 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
586 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
587 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
588 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
589
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000590 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000591 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000592
593 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000594 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
595 // A new file may contain a directive section to add new command line options.
596 // That's why we have to repeat until converge.)
597 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000598 // Windows specific -- if entry point is not found,
599 // search for its mangled names.
600 if (Config->Entry)
601 Symtab.mangleMaybe(Config->Entry);
602
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000603 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000604 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000605 if (!E.ForwardTo.empty())
606 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000607 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000608 if (!E.Directives)
609 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000610 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000611
612 // Add weak aliases. Weak aliases is a mechanism to give remaining
613 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000614 for (auto Pair : Config->AlternateNames) {
615 StringRef From = Pair.first;
616 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000617 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000618 if (!Sym)
619 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000620 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000621 if (!U->WeakAlias)
622 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000623 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000624
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000625 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000626 if (Symtab.findUnderscore("_load_config_used"))
627 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000628
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000629 if (Symtab.queueEmpty())
630 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000631 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000632 }
633
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000634 // Do LTO by compiling bitcode input files to a set of native COFF files then
635 // link those files.
636 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000637
Peter Collingbourne2612a322015-07-04 05:28:41 +0000638 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000639 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000640
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000641 // Windows specific -- if no /subsystem is given, we need to infer
642 // that from entry point name.
643 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000644 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000645 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000646 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000647 }
648
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000649 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000650 if (Args.hasArg(OPT_safeseh))
651 for (ObjectFile *File : Symtab.ObjectFiles)
652 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000653 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000654
Rui Ueyama151d8622015-06-17 20:40:43 +0000655 // Windows specific -- when we are creating a .dll file, we also
656 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000657 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000658 fixupExports();
659 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000660 assignExportOrdinals();
661 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000662
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000663 // Windows specific -- Create a side-by-side manifest file.
664 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000665 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000666
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000667 // Create a dummy PDB file to satisfy build sytem rules.
668 if (auto *Arg = Args.getLastArg(OPT_pdb))
Rui Ueyamae7378242015-12-04 23:11:05 +0000669 createPDB(Arg->getValue());
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000670
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000671 // Identify unreferenced COMDAT sections.
672 if (Config->DoGC)
673 markLive(Symtab.getChunks());
674
675 // Identify identical COMDAT sections to merge them.
676 if (Config->DoICF)
677 doICF(Symtab.getChunks());
678
Rui Ueyama411c63602015-05-28 19:09:30 +0000679 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000680 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000681
Rui Ueyama016414f2015-06-28 20:07:08 +0000682 // Create a symbol map file containing symbol VAs and their names
683 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000684 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
685 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000686 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000687 if (EC)
688 fatal(EC, "Could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000689 Symtab.printMap(Out);
690 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000691 // Call exit to avoid calling destructors.
692 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000693}
694
Rui Ueyama411c63602015-05-28 19:09:30 +0000695} // namespace coff
696} // namespace lld