blob: 36b64c82f8b703aebbaad2ac93b05e348f126838 [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 Ueyama8d433d72016-12-08 21:27:09 +0000153 return Saver.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 Ueyama8d433d72016-12-08 21:27:09 +0000157 return Saver.save(Path.str());
Rui Ueyama54b71da2015-05-31 19:17:12 +0000158 }
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 Ueyama8d433d72016-12-08 21:27:09 +0000178 Filename = Saver.save(Filename + ".lib");
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000179 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 Ueyama8d433d72016-12-08 21:27:09 +0000202 StringRef Env = Saver.save(*EnvOpt);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000203 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 Ueyama8d433d72016-12-08 21:27:09 +0000220 return Saver.save("_" + Sym);
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000221 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
Hans Wennborg1818e652016-12-09 20:54:44 +0000310static std::string getMapFile(const opt::InputArgList &Args) {
311 auto *Arg = Args.getLastArg(OPT_lldmap, OPT_lldmap_file);
312 if (!Arg)
313 return "";
314 if (Arg->getOption().getID() == OPT_lldmap_file)
315 return Arg->getValue();
316
317 assert(Arg->getOption().getID() == OPT_lldmap);
318 StringRef OutFile = Config->OutputFile;
319 return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str();
320}
321
Rui Ueyama8fe17672016-12-08 20:50:47 +0000322void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000323 // If the first command line argument is "/lib", link.exe acts like lib.exe.
324 // We call our own implementation of lib.exe that understands bitcode files.
325 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
326 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000327 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000328 return;
329 }
330
Peter Collingbourne60c16162015-06-01 20:10:10 +0000331 // Needed for LTO.
Rui Ueyama8fe17672016-12-08 20:50:47 +0000332 InitializeAllTargetInfos();
333 InitializeAllTargets();
334 InitializeAllTargetMCs();
335 InitializeAllAsmParsers();
336 InitializeAllAsmPrinters();
337 InitializeAllDisassemblers();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000338
Rui Ueyama411c63602015-05-28 19:09:30 +0000339 // Parse command line options.
Rui Ueyama8fe17672016-12-08 20:50:47 +0000340 opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000341
Rui Ueyama5c726432015-05-29 16:11:52 +0000342 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000343 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000344 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000345 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000346 }
347
Peter Collingbournefeee2102016-07-26 02:00:42 +0000348 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
349 SmallString<64> Path = StringRef(Arg->getValue());
Rui Ueyama8fe17672016-12-08 20:50:47 +0000350 sys::path::append(Path, "repro");
Peter Collingbournefeee2102016-07-26 02:00:42 +0000351 ErrorOr<CpioFile *> F = CpioFile::create(Path);
352 if (F)
353 Cpio.reset(*F);
354 else
Rui Ueyama8fe17672016-12-08 20:50:47 +0000355 errs() << "/linkrepro: failed to open " << Path
356 << ".cpio: " << F.getError().message() << '\n';
Peter Collingbournefeee2102016-07-26 02:00:42 +0000357 }
358
Rafael Espindolab835ae82015-08-06 14:58:50 +0000359 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000360 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000361
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000362 // Construct search path list.
363 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000364 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000365 SearchPaths.push_back(Arg->getValue());
366 addLibSearchPaths();
367
Rui Ueyamaad660982015-06-07 00:20:32 +0000368 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000369 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000370 Config->OutputFile = Arg->getValue();
371
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000372 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000373 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000374 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000375
Rui Ueyama95925fd2015-06-28 19:35:15 +0000376 // Handle /force or /force:unresolved
377 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
378 Config->Force = true;
379
Rui Ueyama6600eb12015-07-04 23:37:32 +0000380 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000381 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000382 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000383 Config->DebugTypes =
384 Args.hasArg(OPT_debugtype)
385 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
386 : getDefaultDebugType(Args);
387 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000388
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000389 // Create a dummy PDB file to satisfy build sytem rules.
Rui Ueyama9f66f822016-10-11 19:45:07 +0000390 if (auto *Arg = Args.getLastArg(OPT_pdb))
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000391 Config->PDBPath = Arg->getValue();
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000392
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000393 // Handle /noentry
394 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000395 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000396 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000397 Config->NoEntry = true;
398 }
399
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000400 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000401 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000402 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000403 Config->ManifestID = 2;
404 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000405
Rui Ueyama588e8322015-06-15 01:23:58 +0000406 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000407 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000408 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000409 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000410 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000411 Config->DynamicBase = false;
412 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000413
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000414 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000415 if (auto *Arg = Args.getLastArg(OPT_machine))
416 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000417
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000418 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000419 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000420 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
421
422 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000423 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000424 Config->NoDefaultLibAll = true;
425
Rui Ueyama804a8b62015-05-29 16:18:15 +0000426 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000427 if (auto *Arg = Args.getLastArg(OPT_base))
428 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000429
430 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000431 if (auto *Arg = Args.getLastArg(OPT_stack))
432 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000433
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000434 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000435 if (auto *Arg = Args.getLastArg(OPT_heap))
436 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000437
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000438 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000439 if (auto *Arg = Args.getLastArg(OPT_version))
440 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
441 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000442
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000443 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000444 if (auto *Arg = Args.getLastArg(OPT_subsystem))
445 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
446 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000447
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000448 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000449 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000450 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000451
Rui Ueyama08d5e182015-06-18 23:20:11 +0000452 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000453 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000454 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000455
Rui Ueyamab95188c2015-06-18 20:27:09 +0000456 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000457 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000458 Config->Implib = Arg->getValue();
459
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000460 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000461 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000462 std::string Str = StringRef(Arg->getValue()).lower();
463 SmallVector<StringRef, 1> Vec;
464 StringRef(Str).split(Vec, ',');
465 for (StringRef S : Vec) {
466 if (S == "noref") {
467 Config->DoGC = false;
468 Config->DoICF = false;
469 continue;
470 }
471 if (S == "icf" || StringRef(S).startswith("icf=")) {
472 Config->DoICF = true;
473 continue;
474 }
475 if (S == "noicf") {
476 Config->DoICF = false;
477 continue;
478 }
479 if (StringRef(S).startswith("lldlto=")) {
480 StringRef OptLevel = StringRef(S).substr(7);
481 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
482 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000483 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000484 continue;
485 }
486 if (StringRef(S).startswith("lldltojobs=")) {
487 StringRef Jobs = StringRef(S).substr(11);
488 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000489 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000490 continue;
491 }
492 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000493 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000494 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000495 }
496
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000497 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000498 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000499 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000500
Rui Ueyama6600eb12015-07-04 23:37:32 +0000501 // Handle /merge
502 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000503 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000504
Rui Ueyama440138c2016-06-20 03:39:39 +0000505 // Handle /section
506 for (auto *Arg : Args.filtered(OPT_section))
507 parseSection(Arg->getValue());
508
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000509 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000510 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
511 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000512
513 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000514 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
515 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000516
517 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000518 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000519 Config->ManifestDependency = Arg->getValue();
520
521 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000522 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000523 Config->ManifestFile = Arg->getValue();
524
Rui Ueyamaafb19012016-04-19 01:21:58 +0000525 // Handle /manifestinput
526 for (auto *Arg : Args.filtered(OPT_manifestinput))
527 Config->ManifestInput.push_back(Arg->getValue());
528
Rui Ueyama6592ff82015-06-16 23:13:00 +0000529 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000530 if (Args.hasArg(OPT_allowbind_no))
531 Config->AllowBind = false;
532 if (Args.hasArg(OPT_allowisolation_no))
533 Config->AllowIsolation = false;
534 if (Args.hasArg(OPT_dynamicbase_no))
535 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000536 if (Args.hasArg(OPT_nxcompat_no))
537 Config->NxCompat = false;
538 if (Args.hasArg(OPT_tsaware_no))
539 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000540 if (Args.hasArg(OPT_nosymtab))
541 Config->WriteSymtab = false;
Rui Ueyamabe939b32016-11-21 17:22:35 +0000542 Config->DumpPdb = Args.hasArg(OPT_dumppdb);
Rui Ueyama6592ff82015-06-16 23:13:00 +0000543
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000544 // Create a list of input files. Files can be given as arguments
545 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000546 std::vector<StringRef> Paths;
547 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000548 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000549 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000550 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000551 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000552 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000553 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000554 for (StringRef Path : Paths)
555 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000556
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000557 // Windows specific -- Create a resource file containing a manifest file.
558 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000559 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000560 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000561 OwningMBs.push_back(std::move(MB)); // take ownership
562 }
563
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000564 // Windows specific -- Input files can be Windows resource files (.res files).
565 // We invoke cvtres.exe to convert resource files to a regular COFF file
566 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000567 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000568 auto NotResource = [](MemoryBufferRef MB) {
569 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000570 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000571 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
572 if (It != MBs.end()) {
573 Resources.insert(Resources.end(), It, MBs.end());
574 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000575 }
576
Rui Ueyama85225b02015-07-02 03:15:15 +0000577 // Read all input files given via the command line. Note that step()
578 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000579 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000580 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000581 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000582
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000583 // Determine machine type and check if all object files are
584 // for the same CPU type. Note that this needs to be done before
585 // any call to mangle().
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +0000586 for (InputFile *File : Symtab.getFiles()) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000587 MachineTypes MT = File->getMachineType();
588 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
589 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000590 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
591 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000592 continue;
593 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000594 if (Config->Machine != MT)
Rui Ueyamabf4ad1d2016-12-08 20:20:22 +0000595 fatal(toString(File) + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000596 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000597 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000598 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyama8fe17672016-12-08 20:50:47 +0000599 errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000600 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000601 }
602
603 // Windows specific -- Convert Windows resource files to a COFF file.
604 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000605 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000606 Symtab.addFile(createFile(MB->getMemBufferRef()));
Peter Collingbournefeee2102016-07-26 02:00:42 +0000607
608 MBs.push_back(MB->getMemBufferRef());
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000609 OwningMBs.push_back(std::move(MB)); // take ownership
610 }
611
Peter Collingbournefeee2102016-07-26 02:00:42 +0000612 if (Cpio)
613 Cpio->append("response.txt",
614 createResponseFile(Args, MBs,
615 ArrayRef<StringRef>(SearchPaths).slice(1)));
616
Rui Ueyama4d545342015-07-28 03:12:00 +0000617 // Handle /largeaddressaware
618 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
619 Config->LargeAddressAware = true;
620
Rui Ueyamad68e2112015-07-28 03:15:57 +0000621 // Handle /highentropyva
622 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
623 Config->HighEntropyVA = true;
624
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000625 // Handle /entry and /dll
626 if (auto *Arg = Args.getLastArg(OPT_entry)) {
627 Config->Entry = addUndefined(mangle(Arg->getValue()));
628 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000629 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
630 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000631 Config->Entry = addUndefined(S);
632 } else if (!Config->NoEntry) {
633 // Windows specific -- If entry point name is not given, we need to
634 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000635 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000636 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000637 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000638 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000639 if (Config->Verbose)
Rui Ueyama8fe17672016-12-08 20:50:47 +0000640 outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000641 }
642
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000643 // Handle /export
644 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000645 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000646 if (Config->Machine == I386) {
647 if (!isDecorated(E.Name))
Rui Ueyama8d433d72016-12-08 21:27:09 +0000648 E.Name = Saver.save("_" + E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000649 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
Rui Ueyama8d433d72016-12-08 21:27:09 +0000650 E.ExtName = Saver.save("_" + E.ExtName);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000651 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000652 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000653 }
654
655 // Handle /def
656 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000657 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000658 // parseModuleDefs mutates Config object.
Rui Ueyama8d433d72016-12-08 21:27:09 +0000659 parseModuleDefs(MB);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000660 }
661
Rui Ueyama6d249082015-07-13 22:31:45 +0000662 // Handle /delayload
663 for (auto *Arg : Args.filtered(OPT_delayload)) {
664 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000665 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000666 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000667 } else {
668 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000669 }
670 }
671
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000672 // Set default image base if /base is not given.
673 if (Config->ImageBase == uint64_t(-1))
674 Config->ImageBase = getDefaultImageBase();
675
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000676 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000677 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000678 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
679 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
680 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000681
Rui Ueyama107db552015-08-09 21:01:06 +0000682 // We do not support /guard:cf (control flow protection) yet.
683 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
684 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
685 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
686 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
687
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000688 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000689 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000690
691 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000692 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
693 // A new file may contain a directive section to add new command line options.
694 // That's why we have to repeat until converge.)
695 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000696 // Windows specific -- if entry point is not found,
697 // search for its mangled names.
698 if (Config->Entry)
699 Symtab.mangleMaybe(Config->Entry);
700
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000701 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000702 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000703 if (!E.ForwardTo.empty())
704 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000705 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000706 if (!E.Directives)
707 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000708 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000709
710 // Add weak aliases. Weak aliases is a mechanism to give remaining
711 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000712 for (auto Pair : Config->AlternateNames) {
713 StringRef From = Pair.first;
714 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000715 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000716 if (!Sym)
717 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000718 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000719 if (!U->WeakAlias)
720 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000721 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000722
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000723 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000724 if (Symtab.findUnderscore("_load_config_used"))
725 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000726
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000727 if (Symtab.queueEmpty())
728 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000729 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000730 }
731
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000732 // Do LTO by compiling bitcode input files to a set of native COFF files then
733 // link those files.
734 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000735
Peter Collingbourne2612a322015-07-04 05:28:41 +0000736 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000737 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000738
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000739 // Windows specific -- if no /subsystem is given, we need to infer
740 // that from entry point name.
741 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000742 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000743 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000744 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000745 }
746
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000747 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000748 if (Args.hasArg(OPT_safeseh))
749 for (ObjectFile *File : Symtab.ObjectFiles)
750 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000751 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000752
Rui Ueyama151d8622015-06-17 20:40:43 +0000753 // Windows specific -- when we are creating a .dll file, we also
754 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000755 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000756 fixupExports();
757 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000758 assignExportOrdinals();
759 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000760
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000761 // Windows specific -- Create a side-by-side manifest file.
762 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000763 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000764
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000765 // Identify unreferenced COMDAT sections.
766 if (Config->DoGC)
767 markLive(Symtab.getChunks());
768
769 // Identify identical COMDAT sections to merge them.
770 if (Config->DoICF)
771 doICF(Symtab.getChunks());
772
Rui Ueyama411c63602015-05-28 19:09:30 +0000773 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000774 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000775
Rui Ueyama016414f2015-06-28 20:07:08 +0000776 // Create a symbol map file containing symbol VAs and their names
777 // to help debugging.
Hans Wennborg1818e652016-12-09 20:54:44 +0000778 std::string MapFile = getMapFile(Args);
779 if (!MapFile.empty()) {
Peter Collingbournebe549552015-06-26 18:58:24 +0000780 std::error_code EC;
Hans Wennborg1818e652016-12-09 20:54:44 +0000781 raw_fd_ostream Out(MapFile, EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000782 if (EC)
Hans Wennborg1818e652016-12-09 20:54:44 +0000783 fatal(EC, "could not create the symbol map " + MapFile);
Peter Collingbournebe549552015-06-26 18:58:24 +0000784 Symtab.printMap(Out);
785 }
Hans Wennborg1818e652016-12-09 20:54:44 +0000786
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000787 // Call exit to avoid calling destructors.
788 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000789}
790
Rui Ueyama411c63602015-05-28 19:09:30 +0000791} // namespace coff
792} // namespace lld