blob: c9d0249dc8f4656a73faa7f3340432a241bb30ce [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
Rui Ueyama411c63602015-05-28 19:09:30 +000010#include "Driver.h"
Rui Ueyama1d99ab32016-09-15 22:24:51 +000011#include "Config.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 Ueyama7fed58c2016-12-08 19:10:28 +000018#include "lld/Support/Memory.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000019#include "llvm/ADT/Optional.h"
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +000020#include "llvm/ADT/StringSwitch.h"
Peter Collingbournebd1cb792015-06-09 21:52:48 +000021#include "llvm/LibDriver/LibDriver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000022#include "llvm/Option/Arg.h"
23#include "llvm/Option/ArgList.h"
24#include "llvm/Option/Option.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000025#include "llvm/Support/Debug.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000026#include "llvm/Support/Path.h"
Rui Ueyama54b71da2015-05-31 19:17:12 +000027#include "llvm/Support/Process.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000028#include "llvm/Support/TargetSelect.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000029#include "llvm/Support/raw_ostream.h"
Rui Ueyama2bf6a122015-06-14 21:50:50 +000030#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000031#include <memory>
32
33using namespace llvm;
Rui Ueyama84936e02015-07-07 23:39:18 +000034using namespace llvm::COFF;
Rui Ueyama54b71da2015-05-31 19:17:12 +000035using llvm::sys::Process;
Peter Collingbournebaf5f872015-06-26 19:20:09 +000036using llvm::sys::fs::OpenFlags;
Rui Ueyama711cd2d2015-05-31 21:17:10 +000037using llvm::sys::fs::file_magic;
38using llvm::sys::fs::identify_magic;
Rui Ueyama411c63602015-05-28 19:09:30 +000039
Rui Ueyama3500f662015-05-28 20:30:06 +000040namespace lld {
41namespace coff {
Rui Ueyama411c63602015-05-28 19:09:30 +000042
Rui Ueyama3500f662015-05-28 20:30:06 +000043Configuration *Config;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000044LinkerDriver *Driver;
45
Rui Ueyama8fe17672016-12-08 20:50:47 +000046bool link(ArrayRef<const char *> Args) {
Rui Ueyama7fed58c2016-12-08 19:10:28 +000047 Config = make<Configuration>();
48 Driver = make<LinkerDriver>();
Rui Ueyama417553d2016-02-28 19:54:51 +000049 Driver->link(Args);
50 return true;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000051}
Rui Ueyama411c63602015-05-28 19:09:30 +000052
Nico Weber5660de72016-04-20 22:34:15 +000053// Drop directory components and replace extension with ".exe" or ".dll".
Rui Ueyamaad660982015-06-07 00:20:32 +000054static std::string getOutputPath(StringRef Path) {
55 auto P = Path.find_last_of("\\/");
56 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
Nico Weber5660de72016-04-20 22:34:15 +000057 const char* E = Config->DLL ? ".dll" : ".exe";
58 return (S.substr(0, S.rfind('.')) + E).str();
Rui Ueyama411c63602015-05-28 19:09:30 +000059}
60
Rui Ueyamad7c2f582015-05-31 21:04:56 +000061// Opens a file. Path has to be resolved already.
62// Newly created memory buffers are owned by this driver.
Rafael Espindolab835ae82015-08-06 14:58:50 +000063MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000064 std::unique_ptr<MemoryBuffer> MB =
Rui Ueyamabb579542016-07-15 01:12:24 +000065 check(MemoryBuffer::getFile(Path), "could not open " + Path);
Rui Ueyamad7c2f582015-05-31 21:04:56 +000066 MemoryBufferRef MBRef = MB->getMemBufferRef();
67 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000068 return MBRef;
69}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000070
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +000071static InputFile *createFile(MemoryBufferRef MB) {
Peter Collingbournefeee2102016-07-26 02:00:42 +000072 if (Driver->Cpio)
73 Driver->Cpio->append(relativeToRoot(MB.getBufferIdentifier()),
74 MB.getBuffer());
75
Rui Ueyama711cd2d2015-05-31 21:17:10 +000076 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000077 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000078 if (Magic == file_magic::archive)
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +000079 return make<ArchiveFile>(MB);
Peter Collingbourne60c16162015-06-01 20:10:10 +000080 if (Magic == file_magic::bitcode)
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +000081 return make<BitcodeFile>(MB);
Rui Ueyamaf83806a2016-11-15 01:01:51 +000082 if (Magic == file_magic::coff_cl_gl_object)
83 fatal(MB.getBufferIdentifier() + ": is not a native COFF file. "
84 "Recompile without /GL");
Rui Ueyamaad660982015-06-07 00:20:32 +000085 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000086 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +000087 return make<ObjectFile>(MB);
Rui Ueyama411c63602015-05-28 19:09:30 +000088}
89
Rui Ueyamaf10a3202015-08-31 08:43:21 +000090static bool isDecorated(StringRef Sym) {
91 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
92}
93
Rui Ueyama411c63602015-05-28 19:09:30 +000094// Parses .drectve section contents and returns a list of files
95// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000096void LinkerDriver::parseDirectives(StringRef S) {
Rui Ueyama8fe17672016-12-08 20:50:47 +000097 opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000098
David Blaikie6521ed92015-06-22 22:06:52 +000099 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +0000100 switch (Arg->getOption().getID()) {
101 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000102 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000103 break;
104 case OPT_defaultlib:
105 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000106 MemoryBufferRef MB = openFile(*Path);
107 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000108 }
109 break;
110 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000111 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000112 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000113 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000114 break;
115 }
116 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000117 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000118 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000119 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000120 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000121 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000122 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000123 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000124 break;
125 case OPT_nodefaultlib:
126 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
127 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000128 case OPT_section:
129 parseSection(Arg->getValue());
130 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000131 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000132 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000133 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000134 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000135 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000136 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000137 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000138 }
139 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000140}
141
Rui Ueyama54b71da2015-05-31 19:17:12 +0000142// Find file from search paths. You can omit ".obj", this function takes
143// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000144StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000145 bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
146 if (HasPathSep)
Rui Ueyama54b71da2015-05-31 19:17:12 +0000147 return Filename;
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000148 bool HasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000149 for (StringRef Dir : SearchPaths) {
150 SmallString<128> Path = Dir;
Rui Ueyama8fe17672016-12-08 20:50:47 +0000151 sys::path::append(Path, Filename);
152 if (sys::fs::exists(Path.str()))
Rui Ueyama54b71da2015-05-31 19:17:12 +0000153 return Alloc.save(Path.str());
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000154 if (!HasExt) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000155 Path.append(".obj");
Rui Ueyama8fe17672016-12-08 20:50:47 +0000156 if (sys::fs::exists(Path.str()))
Rui Ueyama54b71da2015-05-31 19:17:12 +0000157 return Alloc.save(Path.str());
158 }
159 }
160 return Filename;
161}
162
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000163// Resolves a file path. This never returns the same path
164// (in that case, it returns None).
165Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
166 StringRef Path = doFindFile(Filename);
167 bool Seen = !VisitedFiles.insert(Path.lower()).second;
168 if (Seen)
169 return None;
170 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000171}
172
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000173// Find library file from search path.
174StringRef LinkerDriver::doFindLib(StringRef Filename) {
175 // Add ".lib" to Filename if that has no file extension.
Rui Ueyamabf4ddeb2016-11-29 04:22:57 +0000176 bool HasExt = (Filename.find('.') != StringRef::npos);
177 if (!HasExt)
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000178 Filename = Alloc.save(Filename + ".lib");
179 return doFindFile(Filename);
180}
181
182// Resolves a library path. /nodefaultlib options are taken into
183// consideration. This never returns the same path (in that case,
184// it returns None).
185Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
186 if (Config->NoDefaultLibAll)
187 return None;
188 StringRef Path = doFindLib(Filename);
189 if (Config->NoDefaultLibs.count(Path))
190 return None;
191 bool Seen = !VisitedFiles.insert(Path.lower()).second;
192 if (Seen)
193 return None;
194 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000195}
196
197// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000198void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000199 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
200 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000201 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000202 StringRef Env = Alloc.save(*EnvOpt);
203 while (!Env.empty()) {
204 StringRef Path;
205 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000206 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000207 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000208}
209
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000210Undefined *LinkerDriver::addUndefined(StringRef Name) {
211 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000212 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000213 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000214}
215
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000216// Symbol names are mangled by appending "_" prefix on x86.
217StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000218 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
219 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000220 return Alloc.save("_" + Sym);
221 return Sym;
222}
223
Rui Ueyama45044f42015-06-29 01:03:53 +0000224// Windows specific -- find default entry point name.
225StringRef LinkerDriver::findDefaultEntry() {
226 // User-defined main functions and their corresponding entry points.
227 static const char *Entries[][2] = {
228 {"main", "mainCRTStartup"},
229 {"wmain", "wmainCRTStartup"},
230 {"WinMain", "WinMainCRTStartup"},
231 {"wWinMain", "wWinMainCRTStartup"},
232 };
233 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000234 StringRef Entry = Symtab.findMangle(mangle(E[0]));
235 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000236 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000237 }
238 return "";
239}
240
241WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000242 if (Config->DLL)
243 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000244 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000245 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000246 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000247 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
248 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000249}
250
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000251static uint64_t getDefaultImageBase() {
252 if (Config->is64())
253 return Config->DLL ? 0x180000000 : 0x140000000;
254 return Config->DLL ? 0x10000000 : 0x400000;
255}
256
Rui Ueyama8fe17672016-12-08 20:50:47 +0000257static std::string createResponseFile(const opt::InputArgList &Args,
Peter Collingbournefeee2102016-07-26 02:00:42 +0000258 ArrayRef<MemoryBufferRef> MBs,
259 ArrayRef<StringRef> SearchPaths) {
260 SmallString<0> Data;
261 raw_svector_ostream OS(Data);
262
263 for (auto *Arg : Args) {
264 switch (Arg->getOption().getID()) {
265 case OPT_linkrepro:
266 case OPT_INPUT:
267 case OPT_defaultlib:
268 case OPT_libpath:
269 break;
270 default:
271 OS << stringize(Arg) << "\n";
272 }
273 }
274
275 for (StringRef Path : SearchPaths) {
276 std::string RelPath = relativeToRoot(Path);
277 OS << "/libpath:" << quote(RelPath) << "\n";
278 }
279
280 for (MemoryBufferRef MB : MBs) {
281 std::string InputPath = relativeToRoot(MB.getBufferIdentifier());
282 OS << quote(InputPath) << "\n";
283 }
284
285 return Data.str();
286}
287
Rui Ueyama8fe17672016-12-08 20:50:47 +0000288static unsigned getDefaultDebugType(const opt::InputArgList &Args) {
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000289 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
290 if (Args.hasArg(OPT_driver))
291 DebugTypes |= static_cast<unsigned>(DebugType::PData);
292 if (Args.hasArg(OPT_profile))
293 DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
294 return DebugTypes;
295}
296
297static unsigned parseDebugType(StringRef Arg) {
Rui Ueyama8fe17672016-12-08 20:50:47 +0000298 SmallVector<StringRef, 3> Types;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000299 Arg.split(Types, ',', /*KeepEmpty=*/false);
300
301 unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
302 for (StringRef Type : Types)
303 DebugTypes |= StringSwitch<unsigned>(Type.lower())
304 .Case("cv", static_cast<unsigned>(DebugType::CV))
305 .Case("pdata", static_cast<unsigned>(DebugType::PData))
306 .Case("fixup", static_cast<unsigned>(DebugType::Fixup));
307 return DebugTypes;
308}
309
Rui Ueyama8fe17672016-12-08 20:50:47 +0000310void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000311 // If the first command line argument is "/lib", link.exe acts like lib.exe.
312 // We call our own implementation of lib.exe that understands bitcode files.
313 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
314 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000315 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000316 return;
317 }
318
Peter Collingbourne60c16162015-06-01 20:10:10 +0000319 // Needed for LTO.
Rui Ueyama8fe17672016-12-08 20:50:47 +0000320 InitializeAllTargetInfos();
321 InitializeAllTargets();
322 InitializeAllTargetMCs();
323 InitializeAllAsmParsers();
324 InitializeAllAsmPrinters();
325 InitializeAllDisassemblers();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000326
Rui Ueyama411c63602015-05-28 19:09:30 +0000327 // Parse command line options.
Rui Ueyama8fe17672016-12-08 20:50:47 +0000328 opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000329
Rui Ueyama5c726432015-05-29 16:11:52 +0000330 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000331 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000332 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000333 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000334 }
335
Peter Collingbournefeee2102016-07-26 02:00:42 +0000336 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
337 SmallString<64> Path = StringRef(Arg->getValue());
Rui Ueyama8fe17672016-12-08 20:50:47 +0000338 sys::path::append(Path, "repro");
Peter Collingbournefeee2102016-07-26 02:00:42 +0000339 ErrorOr<CpioFile *> F = CpioFile::create(Path);
340 if (F)
341 Cpio.reset(*F);
342 else
Rui Ueyama8fe17672016-12-08 20:50:47 +0000343 errs() << "/linkrepro: failed to open " << Path
344 << ".cpio: " << F.getError().message() << '\n';
Peter Collingbournefeee2102016-07-26 02:00:42 +0000345 }
346
Rafael Espindolab835ae82015-08-06 14:58:50 +0000347 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000348 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000349
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000350 // Construct search path list.
351 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000352 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000353 SearchPaths.push_back(Arg->getValue());
354 addLibSearchPaths();
355
Rui Ueyamaad660982015-06-07 00:20:32 +0000356 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000357 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000358 Config->OutputFile = Arg->getValue();
359
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000360 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000361 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000362 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000363
Rui Ueyama95925fd2015-06-28 19:35:15 +0000364 // Handle /force or /force:unresolved
365 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
366 Config->Force = true;
367
Rui Ueyama6600eb12015-07-04 23:37:32 +0000368 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000369 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000370 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000371 Config->DebugTypes =
372 Args.hasArg(OPT_debugtype)
373 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
374 : getDefaultDebugType(Args);
375 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000376
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000377 // Create a dummy PDB file to satisfy build sytem rules.
Rui Ueyama9f66f822016-10-11 19:45:07 +0000378 if (auto *Arg = Args.getLastArg(OPT_pdb))
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000379 Config->PDBPath = Arg->getValue();
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000380
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000381 // Handle /noentry
382 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000383 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000384 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000385 Config->NoEntry = true;
386 }
387
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000388 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000389 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000390 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000391 Config->ManifestID = 2;
392 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000393
Rui Ueyama588e8322015-06-15 01:23:58 +0000394 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000395 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000396 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000397 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000398 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000399 Config->DynamicBase = false;
400 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000401
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000402 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000403 if (auto *Arg = Args.getLastArg(OPT_machine))
404 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000405
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000406 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000407 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000408 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
409
410 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000411 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000412 Config->NoDefaultLibAll = true;
413
Rui Ueyama804a8b62015-05-29 16:18:15 +0000414 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000415 if (auto *Arg = Args.getLastArg(OPT_base))
416 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000417
418 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000419 if (auto *Arg = Args.getLastArg(OPT_stack))
420 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000421
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000422 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000423 if (auto *Arg = Args.getLastArg(OPT_heap))
424 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000425
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000426 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000427 if (auto *Arg = Args.getLastArg(OPT_version))
428 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
429 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000430
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000431 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000432 if (auto *Arg = Args.getLastArg(OPT_subsystem))
433 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
434 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000435
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000436 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000437 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000438 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000439
Rui Ueyama08d5e182015-06-18 23:20:11 +0000440 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000441 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000442 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000443
Rui Ueyamab95188c2015-06-18 20:27:09 +0000444 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000445 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000446 Config->Implib = Arg->getValue();
447
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000448 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000449 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000450 std::string Str = StringRef(Arg->getValue()).lower();
451 SmallVector<StringRef, 1> Vec;
452 StringRef(Str).split(Vec, ',');
453 for (StringRef S : Vec) {
454 if (S == "noref") {
455 Config->DoGC = false;
456 Config->DoICF = false;
457 continue;
458 }
459 if (S == "icf" || StringRef(S).startswith("icf=")) {
460 Config->DoICF = true;
461 continue;
462 }
463 if (S == "noicf") {
464 Config->DoICF = false;
465 continue;
466 }
467 if (StringRef(S).startswith("lldlto=")) {
468 StringRef OptLevel = StringRef(S).substr(7);
469 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
470 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000471 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000472 continue;
473 }
474 if (StringRef(S).startswith("lldltojobs=")) {
475 StringRef Jobs = StringRef(S).substr(11);
476 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000477 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000478 continue;
479 }
480 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000481 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000482 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000483 }
484
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000485 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000486 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000487 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000488
Rui Ueyama6600eb12015-07-04 23:37:32 +0000489 // Handle /merge
490 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000491 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000492
Rui Ueyama440138c2016-06-20 03:39:39 +0000493 // Handle /section
494 for (auto *Arg : Args.filtered(OPT_section))
495 parseSection(Arg->getValue());
496
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000497 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000498 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
499 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000500
501 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000502 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
503 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000504
505 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000506 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000507 Config->ManifestDependency = Arg->getValue();
508
509 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000510 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000511 Config->ManifestFile = Arg->getValue();
512
Rui Ueyamaafb19012016-04-19 01:21:58 +0000513 // Handle /manifestinput
514 for (auto *Arg : Args.filtered(OPT_manifestinput))
515 Config->ManifestInput.push_back(Arg->getValue());
516
Rui Ueyama6592ff82015-06-16 23:13:00 +0000517 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000518 if (Args.hasArg(OPT_allowbind_no))
519 Config->AllowBind = false;
520 if (Args.hasArg(OPT_allowisolation_no))
521 Config->AllowIsolation = false;
522 if (Args.hasArg(OPT_dynamicbase_no))
523 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000524 if (Args.hasArg(OPT_nxcompat_no))
525 Config->NxCompat = false;
526 if (Args.hasArg(OPT_tsaware_no))
527 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000528 if (Args.hasArg(OPT_nosymtab))
529 Config->WriteSymtab = false;
Rui Ueyamabe939b32016-11-21 17:22:35 +0000530 Config->DumpPdb = Args.hasArg(OPT_dumppdb);
Rui Ueyama6592ff82015-06-16 23:13:00 +0000531
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000532 // Create a list of input files. Files can be given as arguments
533 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000534 std::vector<StringRef> Paths;
535 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000536 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000537 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000538 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000539 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000540 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000541 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000542 for (StringRef Path : Paths)
543 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000544
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000545 // Windows specific -- Create a resource file containing a manifest file.
546 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000547 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000548 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000549 OwningMBs.push_back(std::move(MB)); // take ownership
550 }
551
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000552 // Windows specific -- Input files can be Windows resource files (.res files).
553 // We invoke cvtres.exe to convert resource files to a regular COFF file
554 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000555 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000556 auto NotResource = [](MemoryBufferRef MB) {
557 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000558 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000559 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
560 if (It != MBs.end()) {
561 Resources.insert(Resources.end(), It, MBs.end());
562 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000563 }
564
Rui Ueyama85225b02015-07-02 03:15:15 +0000565 // Read all input files given via the command line. Note that step()
566 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000567 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000568 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000569 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000570
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000571 // Determine machine type and check if all object files are
572 // for the same CPU type. Note that this needs to be done before
573 // any call to mangle().
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +0000574 for (InputFile *File : Symtab.getFiles()) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000575 MachineTypes MT = File->getMachineType();
576 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
577 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000578 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
579 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000580 continue;
581 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000582 if (Config->Machine != MT)
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +0000583 fatal(toString(File) + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000584 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000585 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000586 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyama8fe17672016-12-08 20:50:47 +0000587 errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000588 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000589 }
590
591 // Windows specific -- Convert Windows resource files to a COFF file.
592 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000593 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000594 Symtab.addFile(createFile(MB->getMemBufferRef()));
Peter Collingbournefeee2102016-07-26 02:00:42 +0000595
596 MBs.push_back(MB->getMemBufferRef());
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000597 OwningMBs.push_back(std::move(MB)); // take ownership
598 }
599
Peter Collingbournefeee2102016-07-26 02:00:42 +0000600 if (Cpio)
601 Cpio->append("response.txt",
602 createResponseFile(Args, MBs,
603 ArrayRef<StringRef>(SearchPaths).slice(1)));
604
Rui Ueyama4d545342015-07-28 03:12:00 +0000605 // Handle /largeaddressaware
606 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
607 Config->LargeAddressAware = true;
608
Rui Ueyamad68e2112015-07-28 03:15:57 +0000609 // Handle /highentropyva
610 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
611 Config->HighEntropyVA = true;
612
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000613 // Handle /entry and /dll
614 if (auto *Arg = Args.getLastArg(OPT_entry)) {
615 Config->Entry = addUndefined(mangle(Arg->getValue()));
616 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000617 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
618 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000619 Config->Entry = addUndefined(S);
620 } else if (!Config->NoEntry) {
621 // Windows specific -- If entry point name is not given, we need to
622 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000623 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000624 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000625 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000626 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000627 if (Config->Verbose)
Rui Ueyama8fe17672016-12-08 20:50:47 +0000628 outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000629 }
630
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000631 // Handle /export
632 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000633 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000634 if (Config->Machine == I386) {
635 if (!isDecorated(E.Name))
636 E.Name = Alloc.save("_" + E.Name);
637 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
638 E.ExtName = Alloc.save("_" + E.ExtName);
639 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000640 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000641 }
642
643 // Handle /def
644 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000645 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000646 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000647 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000648 }
649
Rui Ueyama6d249082015-07-13 22:31:45 +0000650 // Handle /delayload
651 for (auto *Arg : Args.filtered(OPT_delayload)) {
652 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000653 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000654 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000655 } else {
656 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000657 }
658 }
659
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000660 // Set default image base if /base is not given.
661 if (Config->ImageBase == uint64_t(-1))
662 Config->ImageBase = getDefaultImageBase();
663
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000664 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000665 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000666 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
667 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
668 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000669
Rui Ueyama107db552015-08-09 21:01:06 +0000670 // We do not support /guard:cf (control flow protection) yet.
671 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
672 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
673 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
674 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
675
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000676 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000677 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000678
679 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000680 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
681 // A new file may contain a directive section to add new command line options.
682 // That's why we have to repeat until converge.)
683 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000684 // Windows specific -- if entry point is not found,
685 // search for its mangled names.
686 if (Config->Entry)
687 Symtab.mangleMaybe(Config->Entry);
688
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000689 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000690 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000691 if (!E.ForwardTo.empty())
692 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000693 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000694 if (!E.Directives)
695 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000696 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000697
698 // Add weak aliases. Weak aliases is a mechanism to give remaining
699 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000700 for (auto Pair : Config->AlternateNames) {
701 StringRef From = Pair.first;
702 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000703 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000704 if (!Sym)
705 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000706 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000707 if (!U->WeakAlias)
708 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000709 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000710
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000711 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000712 if (Symtab.findUnderscore("_load_config_used"))
713 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000714
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000715 if (Symtab.queueEmpty())
716 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000717 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000718 }
719
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000720 // Do LTO by compiling bitcode input files to a set of native COFF files then
721 // link those files.
722 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000723
Peter Collingbourne2612a322015-07-04 05:28:41 +0000724 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000725 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000726
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000727 // Windows specific -- if no /subsystem is given, we need to infer
728 // that from entry point name.
729 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000730 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000731 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000732 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000733 }
734
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000735 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000736 if (Args.hasArg(OPT_safeseh))
737 for (ObjectFile *File : Symtab.ObjectFiles)
738 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000739 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000740
Rui Ueyama151d8622015-06-17 20:40:43 +0000741 // Windows specific -- when we are creating a .dll file, we also
742 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000743 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000744 fixupExports();
745 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000746 assignExportOrdinals();
747 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000748
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000749 // Windows specific -- Create a side-by-side manifest file.
750 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000751 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000752
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000753 // Identify unreferenced COMDAT sections.
754 if (Config->DoGC)
755 markLive(Symtab.getChunks());
756
757 // Identify identical COMDAT sections to merge them.
758 if (Config->DoICF)
759 doICF(Symtab.getChunks());
760
Rui Ueyama411c63602015-05-28 19:09:30 +0000761 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000762 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000763
Rui Ueyama016414f2015-06-28 20:07:08 +0000764 // Create a symbol map file containing symbol VAs and their names
765 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000766 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
767 std::error_code EC;
Rui Ueyama8fe17672016-12-08 20:50:47 +0000768 raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000769 if (EC)
Rui Ueyamabb579542016-07-15 01:12:24 +0000770 fatal(EC, "could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000771 Symtab.printMap(Out);
772 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000773 // Call exit to avoid calling destructors.
774 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000775}
776
Rui Ueyama411c63602015-05-28 19:09:30 +0000777} // namespace coff
778} // namespace lld