blob: b739ca97e88ac28a4cf229e769a52e0414bed3d4 [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 Ueyama588e8322015-06-15 01:23:58 +0000228 // Handle /fixed
Rui Ueyama6592ff82015-06-16 23:13:00 +0000229 if (Args->hasArg(OPT_fixed)) {
230 if (Args->hasArg(OPT_dynamicbase)) {
231 llvm::errs() << "/fixed must not be specified with /dynamicbase\n";
232 return false;
233 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000234 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000235 Config->DynamicBase = false;
236 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000237
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000238 // Handle /machine
239 auto MTOrErr = getMachineType(Args.get());
240 if (auto EC = MTOrErr.getError()) {
241 llvm::errs() << EC.message() << "\n";
242 return false;
243 }
244 Config->MachineType = MTOrErr.get();
245
Rui Ueyama06137472015-05-31 20:10:11 +0000246 // Handle /libpath
Rui Ueyamaf4784cc2015-05-31 20:20:37 +0000247 for (auto *Arg : Args->filtered(OPT_libpath)) {
248 // Inserting at front of a vector is okay because it's short.
249 // +1 because the first entry is always "." (current directory).
250 SearchPaths.insert(SearchPaths.begin() + 1, Arg->getValue());
251 }
Rui Ueyama06137472015-05-31 20:10:11 +0000252
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000253 // Handle /nodefaultlib:<filename>
254 for (auto *Arg : Args->filtered(OPT_nodefaultlib))
255 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
256
257 // Handle /nodefaultlib
258 if (Args->hasArg(OPT_nodefaultlib_all))
259 Config->NoDefaultLibAll = true;
260
Rui Ueyama804a8b62015-05-29 16:18:15 +0000261 // Handle /base
262 if (auto *Arg = Args->getLastArg(OPT_base)) {
263 if (auto EC = parseNumbers(Arg->getValue(), &Config->ImageBase)) {
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000264 llvm::errs() << "/base: " << EC.message() << "\n";
265 return false;
266 }
267 }
268
269 // Handle /stack
270 if (auto *Arg = Args->getLastArg(OPT_stack)) {
271 if (auto EC = parseNumbers(Arg->getValue(), &Config->StackReserve,
272 &Config->StackCommit)) {
273 llvm::errs() << "/stack: " << EC.message() << "\n";
Rui Ueyama804a8b62015-05-29 16:18:15 +0000274 return false;
275 }
276 }
277
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000278 // Handle /heap
279 if (auto *Arg = Args->getLastArg(OPT_heap)) {
280 if (auto EC = parseNumbers(Arg->getValue(), &Config->HeapReserve,
281 &Config->HeapCommit)) {
282 llvm::errs() << "/heap: " << EC.message() << "\n";
283 return false;
284 }
285 }
286
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000287 // Handle /version
288 if (auto *Arg = Args->getLastArg(OPT_version)) {
289 if (auto EC = parseVersion(Arg->getValue(), &Config->MajorImageVersion,
290 &Config->MinorImageVersion)) {
291 llvm::errs() << "/version: " << EC.message() << "\n";
292 return false;
293 }
294 }
295
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000296 // Handle /subsystem
297 if (auto *Arg = Args->getLastArg(OPT_subsystem)) {
298 if (auto EC = parseSubsystem(Arg->getValue(), &Config->Subsystem,
299 &Config->MajorOSVersion,
300 &Config->MinorOSVersion)) {
301 llvm::errs() << "/subsystem: " << EC.message() << "\n";
302 return false;
303 }
304 }
305
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000306 // Handle /opt
307 for (auto *Arg : Args->filtered(OPT_opt)) {
308 std::string S = StringRef(Arg->getValue()).lower();
309 if (S == "noref") {
310 Config->DoGC = false;
311 continue;
312 }
313 if (S != "ref" && S != "icf" && S != "noicf" &&
314 S != "lbr" && S != "nolbr" &&
315 !StringRef(S).startswith("icf=")) {
316 llvm::errs() << "/opt: unknown option: " << S << "\n";
317 return false;
318 }
319 }
320
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000321 // Handle /failifmismatch
322 if (auto EC = checkFailIfMismatch(Args.get())) {
323 llvm::errs() << "/failifmismatch: " << EC.message() << "\n";
324 return false;
325 }
326
Rui Ueyama6592ff82015-06-16 23:13:00 +0000327 // Handle miscellaneous boolean flags.
328 if (Args->hasArg(OPT_allowbind_no)) Config->AllowBind = false;
329 if (Args->hasArg(OPT_allowisolation_no)) Config->AllowIsolation = false;
330 if (Args->hasArg(OPT_dynamicbase_no)) Config->DynamicBase = false;
331 if (Args->hasArg(OPT_highentropyva_no)) Config->HighEntropyVA = false;
332 if (Args->hasArg(OPT_nxcompat_no)) Config->NxCompat = false;
333 if (Args->hasArg(OPT_tsaware_no)) Config->TerminalServerAware = false;
334
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000335 // Create a list of input files. Files can be given as arguments
336 // for /defaultlib option.
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000337 std::vector<StringRef> InputPaths;
338 std::vector<MemoryBufferRef> Inputs;
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000339 for (auto *Arg : Args->filtered(OPT_INPUT))
340 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000341 InputPaths.push_back(*Path);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000342 for (auto *Arg : Args->filtered(OPT_defaultlib))
343 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000344 InputPaths.push_back(*Path);
345 for (StringRef Path : InputPaths) {
346 ErrorOr<MemoryBufferRef> MBOrErr = openFile(Path);
347 if (auto EC = MBOrErr.getError()) {
348 llvm::errs() << "cannot open " << Path << ": " << EC.message() << "\n";
349 return false;
350 }
351 Inputs.push_back(MBOrErr.get());
352 }
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000353
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000354 // Create a symbol table.
355 SymbolTable Symtab;
356
357 // Add undefined symbols given via the command line.
358 // (/include is equivalent to Unix linker's -u option.)
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000359 for (auto *Arg : Args->filtered(OPT_incl)) {
360 StringRef Sym = Arg->getValue();
361 Symtab.addUndefined(Sym);
362 Config->GCRoots.insert(Sym);
363 }
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000364
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000365 // Windows specific -- Input files can be Windows resource files (.res files).
366 // We invoke cvtres.exe to convert resource files to a regular COFF file
367 // then link the result file normally.
368 auto IsResource = [](MemoryBufferRef MB) {
369 return identify_magic(MB.getBuffer()) == file_magic::windows_resource;
370 };
371 auto It = std::stable_partition(Inputs.begin(), Inputs.end(), IsResource);
372 if (It != Inputs.begin()) {
373 std::vector<MemoryBufferRef> Files(Inputs.begin(), It);
374 auto MBOrErr = convertResToCOFF(Files);
375 if (MBOrErr.getError())
376 return false;
377 std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
378 Inputs = std::vector<MemoryBufferRef>(It, Inputs.end());
379 Inputs.push_back(MB->getMemBufferRef());
380 OwningMBs.push_back(std::move(MB)); // take ownership
381 }
382
Rui Ueyama411c63602015-05-28 19:09:30 +0000383 // Parse all input files and put all symbols to the symbol table.
384 // The symbol table will take care of name resolution.
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000385 for (MemoryBufferRef MB : Inputs) {
386 std::unique_ptr<InputFile> File = createFile(MB);
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +0000387 if (Config->Verbose)
388 llvm::outs() << "Reading " << File->getName() << "\n";
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000389 if (auto EC = Symtab.addFile(std::move(File))) {
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000390 llvm::errs() << File->getName() << ": " << EC.message() << "\n";
Rui Ueyama411c63602015-05-28 19:09:30 +0000391 return false;
392 }
393 }
Rui Ueyama5cff6852015-05-31 03:34:08 +0000394
Rui Ueyama360bace2015-05-31 22:31:31 +0000395 // Add weak aliases. Weak aliases is a mechanism to give remaining
396 // undefined symbols final chance to be resolved successfully.
397 // This is symbol renaming.
398 for (auto *Arg : Args->filtered(OPT_alternatename)) {
Rui Ueyama2ba79082015-06-04 19:21:22 +0000399 // Parse a string of the form of "/alternatename:From=To".
Rui Ueyama360bace2015-05-31 22:31:31 +0000400 StringRef From, To;
401 std::tie(From, To) = StringRef(Arg->getValue()).split('=');
402 if (From.empty() || To.empty()) {
403 llvm::errs() << "/alternatename: invalid argument: "
404 << Arg->getValue() << "\n";
405 return false;
406 }
Rui Ueyama2ba79082015-06-04 19:21:22 +0000407 // If From is already resolved to a Defined type, do nothing.
Rui Ueyama68216c62015-06-01 03:55:02 +0000408 // Otherwise, rename it to see if To can be resolved instead.
Rui Ueyama360bace2015-05-31 22:31:31 +0000409 if (Symtab.find(From))
410 continue;
411 if (auto EC = Symtab.rename(From, To)) {
412 llvm::errs() << EC.message() << "\n";
413 return false;
414 }
415 }
416
Rui Ueyama5cff6852015-05-31 03:34:08 +0000417 // Windows specific -- If entry point name is not given, we need to
418 // infer that from user-defined entry name. The symbol table takes
419 // care of details.
420 if (Config->EntryName.empty()) {
421 auto EntryOrErr = Symtab.findDefaultEntry();
422 if (auto EC = EntryOrErr.getError()) {
423 llvm::errs() << EC.message() << "\n";
424 return false;
425 }
426 Config->EntryName = EntryOrErr.get();
427 }
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000428 Config->GCRoots.insert(Config->EntryName);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000429
430 // Make sure we have resolved all symbols.
Rui Ueyama411c63602015-05-28 19:09:30 +0000431 if (Symtab.reportRemainingUndefines())
432 return false;
433
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000434 // Do LTO by compiling bitcode input files to a native COFF file
435 // then link that file.
Peter Collingbourne60c16162015-06-01 20:10:10 +0000436 if (auto EC = Symtab.addCombinedLTOObject()) {
437 llvm::errs() << EC.message() << "\n";
438 return false;
439 }
440
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000441 // Windows specific -- if no /subsystem is given, we need to infer
442 // that from entry point name.
443 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
444 Config->Subsystem =
445 StringSwitch<WindowsSubsystem>(Config->EntryName)
446 .Case("mainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
447 .Case("wmainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_CUI)
448 .Case("WinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
449 .Case("wWinMainCRTStartup", IMAGE_SUBSYSTEM_WINDOWS_GUI)
450 .Default(IMAGE_SUBSYSTEM_UNKNOWN);
451 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
452 llvm::errs() << "subsystem must be defined\n";
453 return false;
454 }
455 }
456
Rui Ueyama411c63602015-05-28 19:09:30 +0000457 // Write the result.
458 Writer Out(&Symtab);
Rui Ueyamaad660982015-06-07 00:20:32 +0000459 if (auto EC = Out.write(Config->OutputFile)) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000460 llvm::errs() << EC.message() << "\n";
461 return false;
462 }
463 return true;
464}
465
Rui Ueyama411c63602015-05-28 19:09:30 +0000466} // namespace coff
467} // namespace lld