blob: a9ef480e48fb4b95c8d43a1025937df42f9eb0b3 [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"
12#include "InputFiles.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000013#include "SymbolTable.h"
14#include "Writer.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/STLExtras.h"
Rui Ueyama3ee0fe42015-05-31 03:55:46 +000017#include "llvm/ADT/StringSwitch.h"
Peter Collingbournebd1cb792015-06-09 21:52:48 +000018#include "llvm/LibDriver/LibDriver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000019#include "llvm/Option/Arg.h"
20#include "llvm/Option/ArgList.h"
21#include "llvm/Option/Option.h"
22#include "llvm/Support/CommandLine.h"
23#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 Ueyama3ee0fe42015-05-31 03:55:46 +000032using llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
33using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI;
34using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama54b71da2015-05-31 19:17:12 +000035using llvm::sys::Process;
Rui Ueyama711cd2d2015-05-31 21:17:10 +000036using llvm::sys::fs::file_magic;
37using llvm::sys::fs::identify_magic;
Rui Ueyama411c63602015-05-28 19:09:30 +000038
Rui Ueyama3500f662015-05-28 20:30:06 +000039namespace lld {
40namespace coff {
Rui Ueyama411c63602015-05-28 19:09:30 +000041
Rui Ueyama3500f662015-05-28 20:30:06 +000042Configuration *Config;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000043LinkerDriver *Driver;
44
45bool link(int Argc, const char *Argv[]) {
46 auto C = make_unique<Configuration>();
47 Config = C.get();
48 auto D = make_unique<LinkerDriver>();
49 Driver = D.get();
50 return Driver->link(Argc, Argv);
51}
Rui Ueyama411c63602015-05-28 19:09:30 +000052
Rui Ueyamaad660982015-06-07 00:20:32 +000053// Drop directory components and replace extension with ".exe".
54static std::string getOutputPath(StringRef Path) {
55 auto P = Path.find_last_of("\\/");
56 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
57 return (S.substr(0, S.rfind('.')) + ".exe").str();
Rui Ueyama411c63602015-05-28 19:09:30 +000058}
59
Rui Ueyamad7c2f582015-05-31 21:04:56 +000060// Opens a file. Path has to be resolved already.
61// Newly created memory buffers are owned by this driver.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000062ErrorOr<MemoryBufferRef> LinkerDriver::openFile(StringRef Path) {
Rui Ueyamad7c2f582015-05-31 21:04:56 +000063 auto MBOrErr = MemoryBuffer::getFile(Path);
64 if (auto EC = MBOrErr.getError())
65 return EC;
66 std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
67 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 Ueyama411c63602015-05-28 19:09:30 +000084// Parses .drectve section contents and returns a list of files
85// specified by /defaultlib.
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000086std::error_code
87LinkerDriver::parseDirectives(StringRef S,
88 std::vector<std::unique_ptr<InputFile>> *Res) {
Rui Ueyama115d7c12015-06-07 02:55:19 +000089 auto ArgsOrErr = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000090 if (auto EC = ArgsOrErr.getError())
91 return EC;
92 std::unique_ptr<llvm::opt::InputArgList> Args = std::move(ArgsOrErr.get());
93
Rui Ueyama8854d8a2015-06-04 19:21:24 +000094 // Handle /failifmismatch
95 if (auto EC = checkFailIfMismatch(Args.get()))
96 return EC;
97
98 // Handle /defaultlib
Rui Ueyamad7c2f582015-05-31 21:04:56 +000099 for (auto *Arg : Args->filtered(OPT_defaultlib)) {
100 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000101 ErrorOr<MemoryBufferRef> MBOrErr = openFile(*Path);
102 if (auto EC = MBOrErr.getError())
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000103 return EC;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000104 std::unique_ptr<InputFile> File = createFile(MBOrErr.get());
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000105 Res->push_back(std::move(File));
106 }
107 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000108 return std::error_code();
109}
110
Rui Ueyama54b71da2015-05-31 19:17:12 +0000111// Find file from search paths. You can omit ".obj", this function takes
112// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000113StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000114 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
115 if (hasPathSep)
116 return Filename;
117 bool hasExt = (Filename.find('.') != StringRef::npos);
118 for (StringRef Dir : SearchPaths) {
119 SmallString<128> Path = Dir;
120 llvm::sys::path::append(Path, Filename);
121 if (llvm::sys::fs::exists(Path.str()))
122 return Alloc.save(Path.str());
123 if (!hasExt) {
124 Path.append(".obj");
125 if (llvm::sys::fs::exists(Path.str()))
126 return Alloc.save(Path.str());
127 }
128 }
129 return Filename;
130}
131
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000132// Resolves a file path. This never returns the same path
133// (in that case, it returns None).
134Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
135 StringRef Path = doFindFile(Filename);
136 bool Seen = !VisitedFiles.insert(Path.lower()).second;
137 if (Seen)
138 return None;
139 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000140}
141
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000142// Find library file from search path.
143StringRef LinkerDriver::doFindLib(StringRef Filename) {
144 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000145 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000146 if (!hasExt)
147 Filename = Alloc.save(Filename + ".lib");
148 return doFindFile(Filename);
149}
150
151// Resolves a library path. /nodefaultlib options are taken into
152// consideration. This never returns the same path (in that case,
153// it returns None).
154Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
155 if (Config->NoDefaultLibAll)
156 return None;
157 StringRef Path = doFindLib(Filename);
158 if (Config->NoDefaultLibs.count(Path))
159 return None;
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
166// Parses LIB environment which contains a list of search paths.
167std::vector<StringRef> LinkerDriver::getSearchPaths() {
168 std::vector<StringRef> Ret;
Rui Ueyama7d806402015-06-08 06:13:12 +0000169 // Add current directory as first item of the search paths.
170 Ret.push_back("");
Rui Ueyama54b71da2015-05-31 19:17:12 +0000171 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
172 if (!EnvOpt.hasValue())
173 return Ret;
174 StringRef Env = Alloc.save(*EnvOpt);
175 while (!Env.empty()) {
176 StringRef Path;
177 std::tie(Path, Env) = Env.split(';');
178 Ret.push_back(Path);
179 }
180 return Ret;
181}
182
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +0000183bool LinkerDriver::link(int Argc, const char *Argv[]) {
Peter Collingbourne60c16162015-06-01 20:10:10 +0000184 // Needed for LTO.
185 llvm::InitializeAllTargetInfos();
186 llvm::InitializeAllTargets();
187 llvm::InitializeAllTargetMCs();
188 llvm::InitializeAllAsmParsers();
189 llvm::InitializeAllAsmPrinters();
190 llvm::InitializeAllDisassemblers();
191
Peter Collingbournebd1cb792015-06-09 21:52:48 +0000192 // If the first command line argument is "/lib", link.exe acts like lib.exe.
193 // We call our own implementation of lib.exe that understands bitcode files.
194 if (Argc > 1 && StringRef(Argv[1]).equals_lower("/lib"))
195 return llvm::libDriverMain(Argc - 1, Argv + 1) == 0;
196
Rui Ueyama411c63602015-05-28 19:09:30 +0000197 // Parse command line options.
Rui Ueyama115d7c12015-06-07 02:55:19 +0000198 auto ArgsOrErr = Parser.parse(Argc, Argv);
Rui Ueyama411c63602015-05-28 19:09:30 +0000199 if (auto EC = ArgsOrErr.getError()) {
200 llvm::errs() << EC.message() << "\n";
201 return false;
202 }
203 std::unique_ptr<llvm::opt::InputArgList> Args = std::move(ArgsOrErr.get());
204
Rui Ueyama5c726432015-05-29 16:11:52 +0000205 // Handle /help
206 if (Args->hasArg(OPT_help)) {
207 printHelp(Argv[0]);
208 return true;
209 }
210
Rui Ueyama411c63602015-05-28 19:09:30 +0000211 if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) {
212 llvm::errs() << "no input files.\n";
213 return false;
214 }
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000215
Rui Ueyamaad660982015-06-07 00:20:32 +0000216 // Handle /out
217 if (auto *Arg = Args->getLastArg(OPT_out))
218 Config->OutputFile = Arg->getValue();
219
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000220 // Handle /verbose
Rui Ueyama411c63602015-05-28 19:09:30 +0000221 if (Args->hasArg(OPT_verbose))
222 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000223
224 // Handle /entry
Rui Ueyama411c63602015-05-28 19:09:30 +0000225 if (auto *Arg = Args->getLastArg(OPT_entry))
226 Config->EntryName = Arg->getValue();
227
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000228 // Handle /machine
229 auto MTOrErr = getMachineType(Args.get());
230 if (auto EC = MTOrErr.getError()) {
231 llvm::errs() << EC.message() << "\n";
232 return false;
233 }
234 Config->MachineType = MTOrErr.get();
235
Rui Ueyama06137472015-05-31 20:10:11 +0000236 // Handle /libpath
Rui Ueyamaf4784cc2015-05-31 20:20:37 +0000237 for (auto *Arg : Args->filtered(OPT_libpath)) {
238 // Inserting at front of a vector is okay because it's short.
239 // +1 because the first entry is always "." (current directory).
240 SearchPaths.insert(SearchPaths.begin() + 1, Arg->getValue());
241 }
Rui Ueyama06137472015-05-31 20:10:11 +0000242
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000243 // Handle /nodefaultlib:<filename>
244 for (auto *Arg : Args->filtered(OPT_nodefaultlib))
245 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
246
247 // Handle /nodefaultlib
248 if (Args->hasArg(OPT_nodefaultlib_all))
249 Config->NoDefaultLibAll = true;
250
Rui Ueyama804a8b62015-05-29 16:18:15 +0000251 // Handle /base
252 if (auto *Arg = Args->getLastArg(OPT_base)) {
253 if (auto EC = parseNumbers(Arg->getValue(), &Config->ImageBase)) {
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000254 llvm::errs() << "/base: " << EC.message() << "\n";
255 return false;
256 }
257 }
258
259 // Handle /stack
260 if (auto *Arg = Args->getLastArg(OPT_stack)) {
261 if (auto EC = parseNumbers(Arg->getValue(), &Config->StackReserve,
262 &Config->StackCommit)) {
263 llvm::errs() << "/stack: " << EC.message() << "\n";
Rui Ueyama804a8b62015-05-29 16:18:15 +0000264 return false;
265 }
266 }
267
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000268 // Handle /heap
269 if (auto *Arg = Args->getLastArg(OPT_heap)) {
270 if (auto EC = parseNumbers(Arg->getValue(), &Config->HeapReserve,
271 &Config->HeapCommit)) {
272 llvm::errs() << "/heap: " << EC.message() << "\n";
273 return false;
274 }
275 }
276
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000277 // Handle /version
278 if (auto *Arg = Args->getLastArg(OPT_version)) {
279 if (auto EC = parseVersion(Arg->getValue(), &Config->MajorImageVersion,
280 &Config->MinorImageVersion)) {
281 llvm::errs() << "/version: " << EC.message() << "\n";
282 return false;
283 }
284 }
285
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000286 // Handle /subsystem
287 if (auto *Arg = Args->getLastArg(OPT_subsystem)) {
288 if (auto EC = parseSubsystem(Arg->getValue(), &Config->Subsystem,
289 &Config->MajorOSVersion,
290 &Config->MinorOSVersion)) {
291 llvm::errs() << "/subsystem: " << EC.message() << "\n";
292 return false;
293 }
294 }
295
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000296 // Handle /opt
297 for (auto *Arg : Args->filtered(OPT_opt)) {
298 std::string S = StringRef(Arg->getValue()).lower();
299 if (S == "noref") {
300 Config->DoGC = false;
301 continue;
302 }
303 if (S != "ref" && S != "icf" && S != "noicf" &&
304 S != "lbr" && S != "nolbr" &&
305 !StringRef(S).startswith("icf=")) {
306 llvm::errs() << "/opt: unknown option: " << S << "\n";
307 return false;
308 }
309 }
310
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000311 // Handle /failifmismatch
312 if (auto EC = checkFailIfMismatch(Args.get())) {
313 llvm::errs() << "/failifmismatch: " << EC.message() << "\n";
314 return false;
315 }
316
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000317 // Create a list of input files. Files can be given as arguments
318 // for /defaultlib option.
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000319 std::vector<StringRef> InputPaths;
320 std::vector<MemoryBufferRef> Inputs;
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000321 for (auto *Arg : Args->filtered(OPT_INPUT))
322 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000323 InputPaths.push_back(*Path);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000324 for (auto *Arg : Args->filtered(OPT_defaultlib))
325 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000326 InputPaths.push_back(*Path);
327 for (StringRef Path : InputPaths) {
328 ErrorOr<MemoryBufferRef> MBOrErr = openFile(Path);
329 if (auto EC = MBOrErr.getError()) {
330 llvm::errs() << "cannot open " << Path << ": " << EC.message() << "\n";
331 return false;
332 }
333 Inputs.push_back(MBOrErr.get());
334 }
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000335
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000336 // Create a symbol table.
337 SymbolTable Symtab;
338
339 // Add undefined symbols given via the command line.
340 // (/include is equivalent to Unix linker's -u option.)
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000341 for (auto *Arg : Args->filtered(OPT_incl)) {
342 StringRef Sym = Arg->getValue();
343 Symtab.addUndefined(Sym);
344 Config->GCRoots.insert(Sym);
345 }
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000346
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000347 // Windows specific -- Input files can be Windows resource files (.res files).
348 // We invoke cvtres.exe to convert resource files to a regular COFF file
349 // then link the result file normally.
350 auto IsResource = [](MemoryBufferRef MB) {
351 return identify_magic(MB.getBuffer()) == file_magic::windows_resource;
352 };
353 auto It = std::stable_partition(Inputs.begin(), Inputs.end(), IsResource);
354 if (It != Inputs.begin()) {
355 std::vector<MemoryBufferRef> Files(Inputs.begin(), It);
356 auto MBOrErr = convertResToCOFF(Files);
357 if (MBOrErr.getError())
358 return false;
359 std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
360 Inputs = std::vector<MemoryBufferRef>(It, Inputs.end());
361 Inputs.push_back(MB->getMemBufferRef());
362 OwningMBs.push_back(std::move(MB)); // take ownership
363 }
364
Rui Ueyama411c63602015-05-28 19:09:30 +0000365 // Parse all input files and put all symbols to the symbol table.
366 // The symbol table will take care of name resolution.
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000367 for (MemoryBufferRef MB : Inputs) {
368 std::unique_ptr<InputFile> File = createFile(MB);
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +0000369 if (Config->Verbose)
370 llvm::outs() << "Reading " << File->getName() << "\n";
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000371 if (auto EC = Symtab.addFile(std::move(File))) {
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000372 llvm::errs() << File->getName() << ": " << EC.message() << "\n";
Rui Ueyama411c63602015-05-28 19:09:30 +0000373 return false;
374 }
375 }
Rui Ueyama5cff6852015-05-31 03:34:08 +0000376
Rui Ueyama360bace2015-05-31 22:31:31 +0000377 // Add weak aliases. Weak aliases is a mechanism to give remaining
378 // undefined symbols final chance to be resolved successfully.
379 // This is symbol renaming.
380 for (auto *Arg : Args->filtered(OPT_alternatename)) {
Rui Ueyama2ba79082015-06-04 19:21:22 +0000381 // Parse a string of the form of "/alternatename:From=To".
Rui Ueyama360bace2015-05-31 22:31:31 +0000382 StringRef From, To;
383 std::tie(From, To) = StringRef(Arg->getValue()).split('=');
384 if (From.empty() || To.empty()) {
385 llvm::errs() << "/alternatename: invalid argument: "
386 << Arg->getValue() << "\n";
387 return false;
388 }
Rui Ueyama2ba79082015-06-04 19:21:22 +0000389 // If From is already resolved to a Defined type, do nothing.
Rui Ueyama68216c62015-06-01 03:55:02 +0000390 // Otherwise, rename it to see if To can be resolved instead.
Rui Ueyama360bace2015-05-31 22:31:31 +0000391 if (Symtab.find(From))
392 continue;
393 if (auto EC = Symtab.rename(From, To)) {
394 llvm::errs() << EC.message() << "\n";
395 return false;
396 }
397 }
398
Rui Ueyama5cff6852015-05-31 03:34:08 +0000399 // Windows specific -- If entry point name is not given, we need to
400 // infer that from user-defined entry name. The symbol table takes
401 // care of details.
402 if (Config->EntryName.empty()) {
403 auto EntryOrErr = Symtab.findDefaultEntry();
404 if (auto EC = EntryOrErr.getError()) {
405 llvm::errs() << EC.message() << "\n";
406 return false;
407 }
408 Config->EntryName = EntryOrErr.get();
409 }
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000410 Config->GCRoots.insert(Config->EntryName);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000411
412 // Make sure we have resolved all symbols.
Rui Ueyama411c63602015-05-28 19:09:30 +0000413 if (Symtab.reportRemainingUndefines())
414 return false;
415
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000416 // Do LTO by compiling bitcode input files to a native COFF file
417 // then link that file.
Peter Collingbourne60c16162015-06-01 20:10:10 +0000418 if (auto EC = Symtab.addCombinedLTOObject()) {
419 llvm::errs() << EC.message() << "\n";
420 return false;
421 }
422
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000423 // Windows specific -- if no /subsystem is given, we need to infer
424 // that from entry point name.
425 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
426 Config->Subsystem =
427 StringSwitch<WindowsSubsystem>(Config->EntryName)
428 .Case("mainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
429 .Case("wmainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
430 .Case("WinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
431 .Case("wWinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
432 .Default(IMAGE_SUBSYSTEM_UNKNOWN);
433 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
434 llvm::errs() << "subsystem must be defined\n";
435 return false;
436 }
437 }
438
Rui Ueyama411c63602015-05-28 19:09:30 +0000439 // Write the result.
440 Writer Out(&Symtab);
Rui Ueyamaad660982015-06-07 00:20:32 +0000441 if (auto EC = Out.write(Config->OutputFile)) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000442 llvm::errs() << EC.message() << "\n";
443 return false;
444 }
445 return true;
446}
447
Rui Ueyama411c63602015-05-28 19:09:30 +0000448} // namespace coff
449} // namespace lld