blob: dd6de03da641616e36a234aedba4adcf63ac8b0a [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);
Rafael Espindolab835ae82015-08-06 14:58:50 +000065 error(MBOrErr, Twine("Could not open ") + Path);
66 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
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) {
Rui Ueyama711cd2d2015-05-31 21:17:10 +000073 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000074 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000075 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000076 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000077 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000078 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000079 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000080 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
81 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000082}
83
Rui Ueyamaf10a3202015-08-31 08:43:21 +000084static bool isDecorated(StringRef Sym) {
85 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
86}
87
Rui Ueyama411c63602015-05-28 19:09:30 +000088// Parses .drectve section contents and returns a list of files
89// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000090void LinkerDriver::parseDirectives(StringRef S) {
91 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000092
David Blaikie6521ed92015-06-22 22:06:52 +000093 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000094 switch (Arg->getOption().getID()) {
95 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +000096 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +000097 break;
98 case OPT_defaultlib:
99 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000100 MemoryBufferRef MB = openFile(*Path);
101 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000102 }
103 break;
104 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000105 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000106 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000107 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000108 break;
109 }
110 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000111 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000112 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000113 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000114 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000115 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000116 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000117 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000118 break;
119 case OPT_nodefaultlib:
120 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
121 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000122 case OPT_section:
123 parseSection(Arg->getValue());
124 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000125 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000126 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000127 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000128 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000129 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000130 default:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000131 error(Twine(Arg->getSpelling()) + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000132 }
133 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000134}
135
Rui Ueyama54b71da2015-05-31 19:17:12 +0000136// Find file from search paths. You can omit ".obj", this function takes
137// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000138StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000139 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
140 if (hasPathSep)
141 return Filename;
142 bool hasExt = (Filename.find('.') != StringRef::npos);
143 for (StringRef Dir : SearchPaths) {
144 SmallString<128> Path = Dir;
145 llvm::sys::path::append(Path, Filename);
146 if (llvm::sys::fs::exists(Path.str()))
147 return Alloc.save(Path.str());
148 if (!hasExt) {
149 Path.append(".obj");
150 if (llvm::sys::fs::exists(Path.str()))
151 return Alloc.save(Path.str());
152 }
153 }
154 return Filename;
155}
156
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000157// Resolves a file path. This never returns the same path
158// (in that case, it returns None).
159Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
160 StringRef Path = doFindFile(Filename);
161 bool Seen = !VisitedFiles.insert(Path.lower()).second;
162 if (Seen)
163 return None;
164 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000165}
166
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000167// Find library file from search path.
168StringRef LinkerDriver::doFindLib(StringRef Filename) {
169 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000170 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000171 if (!hasExt)
172 Filename = Alloc.save(Filename + ".lib");
173 return doFindFile(Filename);
174}
175
176// Resolves a library path. /nodefaultlib options are taken into
177// consideration. This never returns the same path (in that case,
178// it returns None).
179Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
180 if (Config->NoDefaultLibAll)
181 return None;
182 StringRef Path = doFindLib(Filename);
183 if (Config->NoDefaultLibs.count(Path))
184 return None;
185 bool Seen = !VisitedFiles.insert(Path.lower()).second;
186 if (Seen)
187 return None;
188 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000189}
190
191// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000192void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000193 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
194 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000195 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000196 StringRef Env = Alloc.save(*EnvOpt);
197 while (!Env.empty()) {
198 StringRef Path;
199 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000200 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000201 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000202}
203
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000204Undefined *LinkerDriver::addUndefined(StringRef Name) {
205 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000206 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000207 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000208}
209
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000210// Symbol names are mangled by appending "_" prefix on x86.
211StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000212 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
213 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000214 return Alloc.save("_" + Sym);
215 return Sym;
216}
217
Rui Ueyama45044f42015-06-29 01:03:53 +0000218// Windows specific -- find default entry point name.
219StringRef LinkerDriver::findDefaultEntry() {
220 // User-defined main functions and their corresponding entry points.
221 static const char *Entries[][2] = {
222 {"main", "mainCRTStartup"},
223 {"wmain", "wmainCRTStartup"},
224 {"WinMain", "WinMainCRTStartup"},
225 {"wWinMain", "wWinMainCRTStartup"},
226 };
227 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000228 StringRef Entry = Symtab.findMangle(mangle(E[0]));
229 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000230 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000231 }
232 return "";
233}
234
235WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000236 if (Config->DLL)
237 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000238 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000239 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000240 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000241 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
242 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000243}
244
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000245static uint64_t getDefaultImageBase() {
246 if (Config->is64())
247 return Config->DLL ? 0x180000000 : 0x140000000;
248 return Config->DLL ? 0x10000000 : 0x400000;
249}
250
Rafael Espindolab835ae82015-08-06 14:58:50 +0000251void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000252 // If the first command line argument is "/lib", link.exe acts like lib.exe.
253 // We call our own implementation of lib.exe that understands bitcode files.
254 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
255 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
256 error("lib failed");
257 return;
258 }
259
Peter Collingbourne60c16162015-06-01 20:10:10 +0000260 // Needed for LTO.
261 llvm::InitializeAllTargetInfos();
262 llvm::InitializeAllTargets();
263 llvm::InitializeAllTargetMCs();
264 llvm::InitializeAllAsmParsers();
265 llvm::InitializeAllAsmPrinters();
266 llvm::InitializeAllDisassemblers();
267
Rui Ueyama411c63602015-05-28 19:09:30 +0000268 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000269 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000270
Rui Ueyama5c726432015-05-29 16:11:52 +0000271 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000272 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000273 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000274 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000275 }
276
Rafael Espindolab835ae82015-08-06 14:58:50 +0000277 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
278 error("no input files.");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000279
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000280 // Construct search path list.
281 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000282 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000283 SearchPaths.push_back(Arg->getValue());
284 addLibSearchPaths();
285
Rui Ueyamaad660982015-06-07 00:20:32 +0000286 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000287 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000288 Config->OutputFile = Arg->getValue();
289
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000290 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000291 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000292 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000293
Rui Ueyama95925fd2015-06-28 19:35:15 +0000294 // Handle /force or /force:unresolved
295 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
296 Config->Force = true;
297
Rui Ueyama6600eb12015-07-04 23:37:32 +0000298 // Handle /debug
299 if (Args.hasArg(OPT_debug))
300 Config->Debug = true;
301
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000302 // Handle /noentry
303 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000304 if (!Args.hasArg(OPT_dll))
305 error("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000306 Config->NoEntry = true;
307 }
308
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000309 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000310 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000311 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000312 Config->ManifestID = 2;
313 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000314
Rui Ueyama588e8322015-06-15 01:23:58 +0000315 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000316 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000317 if (Args.hasArg(OPT_dynamicbase))
318 error("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000319 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000320 Config->DynamicBase = false;
321 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000322
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000323 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000324 if (auto *Arg = Args.getLastArg(OPT_machine))
325 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000326
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000327 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000328 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000329 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
330
331 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000332 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000333 Config->NoDefaultLibAll = true;
334
Rui Ueyama804a8b62015-05-29 16:18:15 +0000335 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000336 if (auto *Arg = Args.getLastArg(OPT_base))
337 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000338
339 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000340 if (auto *Arg = Args.getLastArg(OPT_stack))
341 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000342
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000343 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000344 if (auto *Arg = Args.getLastArg(OPT_heap))
345 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000346
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000347 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000348 if (auto *Arg = Args.getLastArg(OPT_version))
349 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
350 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000351
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000352 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000353 if (auto *Arg = Args.getLastArg(OPT_subsystem))
354 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
355 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000356
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000357 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000358 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000359 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000360
Rui Ueyama08d5e182015-06-18 23:20:11 +0000361 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000362 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000363 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000364
Rui Ueyamab95188c2015-06-18 20:27:09 +0000365 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000366 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000367 Config->Implib = Arg->getValue();
368
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000369 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000370 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000371 std::string Str = StringRef(Arg->getValue()).lower();
372 SmallVector<StringRef, 1> Vec;
373 StringRef(Str).split(Vec, ',');
374 for (StringRef S : Vec) {
375 if (S == "noref") {
376 Config->DoGC = false;
377 Config->DoICF = false;
378 continue;
379 }
380 if (S == "icf" || StringRef(S).startswith("icf=")) {
381 Config->DoICF = true;
382 continue;
383 }
384 if (S == "noicf") {
385 Config->DoICF = false;
386 continue;
387 }
388 if (StringRef(S).startswith("lldlto=")) {
389 StringRef OptLevel = StringRef(S).substr(7);
390 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
391 Config->LTOOptLevel > 3)
392 error("/opt:lldlto: invalid optimization level: " + OptLevel);
393 continue;
394 }
395 if (StringRef(S).startswith("lldltojobs=")) {
396 StringRef Jobs = StringRef(S).substr(11);
397 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
398 error("/opt:lldltojobs: invalid job count: " + Jobs);
399 continue;
400 }
401 if (S != "ref" && S != "lbr" && S != "nolbr")
402 error(Twine("/opt: unknown option: ") + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000403 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000404 }
405
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000406 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000407 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000408 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000409
Rui Ueyama6600eb12015-07-04 23:37:32 +0000410 // Handle /merge
411 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000412 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000413
Rui Ueyama440138c2016-06-20 03:39:39 +0000414 // Handle /section
415 for (auto *Arg : Args.filtered(OPT_section))
416 parseSection(Arg->getValue());
417
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000418 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000419 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
420 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000421
422 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000423 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
424 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000425
426 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000427 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000428 Config->ManifestDependency = Arg->getValue();
429
430 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000431 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000432 Config->ManifestFile = Arg->getValue();
433
Rui Ueyamaafb19012016-04-19 01:21:58 +0000434 // Handle /manifestinput
435 for (auto *Arg : Args.filtered(OPT_manifestinput))
436 Config->ManifestInput.push_back(Arg->getValue());
437
Rui Ueyama6592ff82015-06-16 23:13:00 +0000438 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000439 if (Args.hasArg(OPT_allowbind_no))
440 Config->AllowBind = false;
441 if (Args.hasArg(OPT_allowisolation_no))
442 Config->AllowIsolation = false;
443 if (Args.hasArg(OPT_dynamicbase_no))
444 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000445 if (Args.hasArg(OPT_nxcompat_no))
446 Config->NxCompat = false;
447 if (Args.hasArg(OPT_tsaware_no))
448 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000449 if (Args.hasArg(OPT_nosymtab))
450 Config->WriteSymtab = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000451
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000452 // Create a list of input files. Files can be given as arguments
453 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000454 std::vector<StringRef> Paths;
455 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000456 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000457 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000458 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000459 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000460 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000461 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000462 for (StringRef Path : Paths)
463 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000464
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000465 // Windows specific -- Create a resource file containing a manifest file.
466 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000467 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000468 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000469 OwningMBs.push_back(std::move(MB)); // take ownership
470 }
471
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000472 // Windows specific -- Input files can be Windows resource files (.res files).
473 // We invoke cvtres.exe to convert resource files to a regular COFF file
474 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000475 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000476 auto NotResource = [](MemoryBufferRef MB) {
477 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000478 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000479 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
480 if (It != MBs.end()) {
481 Resources.insert(Resources.end(), It, MBs.end());
482 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000483 }
484
Rui Ueyama85225b02015-07-02 03:15:15 +0000485 // Read all input files given via the command line. Note that step()
486 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000487 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000488 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000489 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000490
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000491 // Determine machine type and check if all object files are
492 // for the same CPU type. Note that this needs to be done before
493 // any call to mangle().
494 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
495 MachineTypes MT = File->getMachineType();
496 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
497 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000498 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
499 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000500 continue;
501 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000502 if (Config->Machine != MT)
503 error(Twine(File->getShortName()) + ": machine type " + machineToStr(MT) +
504 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000505 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000506 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000507 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000508 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000509 }
510
511 // Windows specific -- Convert Windows resource files to a COFF file.
512 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000513 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000514 Symtab.addFile(createFile(MB->getMemBufferRef()));
515 OwningMBs.push_back(std::move(MB)); // take ownership
516 }
517
Rui Ueyama4d545342015-07-28 03:12:00 +0000518 // Handle /largeaddressaware
519 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
520 Config->LargeAddressAware = true;
521
Rui Ueyamad68e2112015-07-28 03:15:57 +0000522 // Handle /highentropyva
523 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
524 Config->HighEntropyVA = true;
525
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000526 // Handle /entry and /dll
527 if (auto *Arg = Args.getLastArg(OPT_entry)) {
528 Config->Entry = addUndefined(mangle(Arg->getValue()));
529 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000530 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
531 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000532 Config->Entry = addUndefined(S);
533 } else if (!Config->NoEntry) {
534 // Windows specific -- If entry point name is not given, we need to
535 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000536 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000537 if (S.empty())
538 error("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000539 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000540 if (Config->Verbose)
541 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000542 }
543
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000544 // Handle /export
545 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000546 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000547 if (Config->Machine == I386) {
548 if (!isDecorated(E.Name))
549 E.Name = Alloc.save("_" + E.Name);
550 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
551 E.ExtName = Alloc.save("_" + E.ExtName);
552 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000553 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000554 }
555
556 // Handle /def
557 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000558 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000559 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000560 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000561 }
562
Rui Ueyama6d249082015-07-13 22:31:45 +0000563 // Handle /delayload
564 for (auto *Arg : Args.filtered(OPT_delayload)) {
565 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000566 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000567 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000568 } else {
569 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000570 }
571 }
572
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000573 // Set default image base if /base is not given.
574 if (Config->ImageBase == uint64_t(-1))
575 Config->ImageBase = getDefaultImageBase();
576
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000577 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000578 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000579 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
580 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
581 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000582
Rui Ueyama107db552015-08-09 21:01:06 +0000583 // We do not support /guard:cf (control flow protection) yet.
584 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
585 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
586 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
587 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
588
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000589 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000590 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000591
592 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000593 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
594 // A new file may contain a directive section to add new command line options.
595 // That's why we have to repeat until converge.)
596 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000597 // Windows specific -- if entry point is not found,
598 // search for its mangled names.
599 if (Config->Entry)
600 Symtab.mangleMaybe(Config->Entry);
601
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000602 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000603 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000604 if (!E.ForwardTo.empty())
605 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000606 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000607 if (!E.Directives)
608 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000609 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000610
611 // Add weak aliases. Weak aliases is a mechanism to give remaining
612 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000613 for (auto Pair : Config->AlternateNames) {
614 StringRef From = Pair.first;
615 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000616 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000617 if (!Sym)
618 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000619 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000620 if (!U->WeakAlias)
621 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000622 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000623
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000624 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000625 if (Symtab.findUnderscore("_load_config_used"))
626 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000627
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000628 if (Symtab.queueEmpty())
629 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000630 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000631 }
632
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000633 // Do LTO by compiling bitcode input files to a set of native COFF files then
634 // link those files.
635 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000636
Peter Collingbourne2612a322015-07-04 05:28:41 +0000637 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000638 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000639
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000640 // Windows specific -- if no /subsystem is given, we need to infer
641 // that from entry point name.
642 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000643 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000644 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
645 error("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000646 }
647
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000648 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000649 if (Args.hasArg(OPT_safeseh))
650 for (ObjectFile *File : Symtab.ObjectFiles)
651 if (!File->SEHCompat)
652 error("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000653
Rui Ueyama151d8622015-06-17 20:40:43 +0000654 // Windows specific -- when we are creating a .dll file, we also
655 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000656 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000657 fixupExports();
658 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000659 assignExportOrdinals();
660 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000661
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000662 // Windows specific -- Create a side-by-side manifest file.
663 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000664 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000665
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000666 // Create a dummy PDB file to satisfy build sytem rules.
667 if (auto *Arg = Args.getLastArg(OPT_pdb))
Rui Ueyamae7378242015-12-04 23:11:05 +0000668 createPDB(Arg->getValue());
Rui Ueyama0fc26d22015-06-29 14:27:12 +0000669
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000670 // Identify unreferenced COMDAT sections.
671 if (Config->DoGC)
672 markLive(Symtab.getChunks());
673
674 // Identify identical COMDAT sections to merge them.
675 if (Config->DoICF)
676 doICF(Symtab.getChunks());
677
Rui Ueyama411c63602015-05-28 19:09:30 +0000678 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000679 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000680
Rui Ueyama016414f2015-06-28 20:07:08 +0000681 // Create a symbol map file containing symbol VAs and their names
682 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000683 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
684 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000685 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000686 error(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