blob: dd797b43553d5bed05819d7d66837a97c64b5c4f [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 Ueyama411c63602015-05-28 19:09:30 +000018#include "llvm/ADT/Optional.h"
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +000019#include "llvm/ADT/StringSwitch.h"
Peter Collingbournebd1cb792015-06-09 21:52:48 +000020#include "llvm/LibDriver/LibDriver.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000021#include "llvm/Option/Arg.h"
22#include "llvm/Option/ArgList.h"
23#include "llvm/Option/Option.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000024#include "llvm/Support/Debug.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000025#include "llvm/Support/Path.h"
Rui Ueyama54b71da2015-05-31 19:17:12 +000026#include "llvm/Support/Process.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000027#include "llvm/Support/TargetSelect.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000028#include "llvm/Support/raw_ostream.h"
Rui Ueyama2bf6a122015-06-14 21:50:50 +000029#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000030#include <memory>
31
32using namespace llvm;
Rui Ueyama84936e02015-07-07 23:39:18 +000033using namespace llvm::COFF;
Rui Ueyama54b71da2015-05-31 19:17:12 +000034using llvm::sys::Process;
Peter Collingbournebaf5f872015-06-26 19:20:09 +000035using llvm::sys::fs::OpenFlags;
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
Rui Ueyama417553d2016-02-28 19:54:51 +000045bool link(llvm::ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000046 Configuration C;
47 LinkerDriver D;
48 Config = &C;
49 Driver = &D;
Rui Ueyama417553d2016-02-28 19:54:51 +000050 Driver->link(Args);
51 return true;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000052}
Rui Ueyama411c63602015-05-28 19:09:30 +000053
Nico Weber5660de72016-04-20 22:34:15 +000054// Drop directory components and replace extension with ".exe" or ".dll".
Rui Ueyamaad660982015-06-07 00:20:32 +000055static std::string getOutputPath(StringRef Path) {
56 auto P = Path.find_last_of("\\/");
57 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
Nico Weber5660de72016-04-20 22:34:15 +000058 const char* E = Config->DLL ? ".dll" : ".exe";
59 return (S.substr(0, S.rfind('.')) + E).str();
Rui Ueyama411c63602015-05-28 19:09:30 +000060}
61
Rui Ueyamad7c2f582015-05-31 21:04:56 +000062// Opens a file. Path has to be resolved already.
63// Newly created memory buffers are owned by this driver.
Rafael Espindolab835ae82015-08-06 14:58:50 +000064MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000065 std::unique_ptr<MemoryBuffer> MB =
Rui Ueyamabb579542016-07-15 01:12:24 +000066 check(MemoryBuffer::getFile(Path), "could not open " + Path);
Rui Ueyamad7c2f582015-05-31 21:04:56 +000067 MemoryBufferRef MBRef = MB->getMemBufferRef();
68 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000069 return MBRef;
70}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000071
Rui Ueyama2bf6a122015-06-14 21:50:50 +000072static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Peter Collingbournefeee2102016-07-26 02:00:42 +000073 if (Driver->Cpio)
74 Driver->Cpio->append(relativeToRoot(MB.getBufferIdentifier()),
75 MB.getBuffer());
76
Rui Ueyama711cd2d2015-05-31 21:17:10 +000077 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000078 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000079 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000080 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000081 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000082 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000083 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000084 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
85 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000086}
87
Rui Ueyamaf10a3202015-08-31 08:43:21 +000088static bool isDecorated(StringRef Sym) {
89 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
90}
91
Rui Ueyama411c63602015-05-28 19:09:30 +000092// Parses .drectve section contents and returns a list of files
93// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000094void LinkerDriver::parseDirectives(StringRef S) {
95 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000096
David Blaikie6521ed92015-06-22 22:06:52 +000097 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000098 switch (Arg->getOption().getID()) {
99 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000100 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000101 break;
102 case OPT_defaultlib:
103 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000104 MemoryBufferRef MB = openFile(*Path);
105 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000106 }
107 break;
108 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000109 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000110 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000111 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000112 break;
113 }
114 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000115 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000116 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000117 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000118 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000119 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000120 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000121 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000122 break;
123 case OPT_nodefaultlib:
124 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
125 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000126 case OPT_section:
127 parseSection(Arg->getValue());
128 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000129 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000130 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000131 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000132 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000133 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000134 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000135 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000136 }
137 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000138}
139
Rui Ueyama54b71da2015-05-31 19:17:12 +0000140// Find file from search paths. You can omit ".obj", this function takes
141// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000142StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000143 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
144 if (hasPathSep)
145 return Filename;
146 bool hasExt = (Filename.find('.') != StringRef::npos);
147 for (StringRef Dir : SearchPaths) {
148 SmallString<128> Path = Dir;
149 llvm::sys::path::append(Path, Filename);
150 if (llvm::sys::fs::exists(Path.str()))
151 return Alloc.save(Path.str());
152 if (!hasExt) {
153 Path.append(".obj");
154 if (llvm::sys::fs::exists(Path.str()))
155 return Alloc.save(Path.str());
156 }
157 }
158 return Filename;
159}
160
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000161// Resolves a file path. This never returns the same path
162// (in that case, it returns None).
163Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
164 StringRef Path = doFindFile(Filename);
165 bool Seen = !VisitedFiles.insert(Path.lower()).second;
166 if (Seen)
167 return None;
168 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000169}
170
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000171// Find library file from search path.
172StringRef LinkerDriver::doFindLib(StringRef Filename) {
173 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000174 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000175 if (!hasExt)
176 Filename = Alloc.save(Filename + ".lib");
177 return doFindFile(Filename);
178}
179
180// Resolves a library path. /nodefaultlib options are taken into
181// consideration. This never returns the same path (in that case,
182// it returns None).
183Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
184 if (Config->NoDefaultLibAll)
185 return None;
186 StringRef Path = doFindLib(Filename);
187 if (Config->NoDefaultLibs.count(Path))
188 return None;
189 bool Seen = !VisitedFiles.insert(Path.lower()).second;
190 if (Seen)
191 return None;
192 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000193}
194
195// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000196void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000197 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
198 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000199 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000200 StringRef Env = Alloc.save(*EnvOpt);
201 while (!Env.empty()) {
202 StringRef Path;
203 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000204 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000205 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000206}
207
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000208Undefined *LinkerDriver::addUndefined(StringRef Name) {
209 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000210 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000211 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000212}
213
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000214// Symbol names are mangled by appending "_" prefix on x86.
215StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000216 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
217 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000218 return Alloc.save("_" + Sym);
219 return Sym;
220}
221
Rui Ueyama45044f42015-06-29 01:03:53 +0000222// Windows specific -- find default entry point name.
223StringRef LinkerDriver::findDefaultEntry() {
224 // User-defined main functions and their corresponding entry points.
225 static const char *Entries[][2] = {
226 {"main", "mainCRTStartup"},
227 {"wmain", "wmainCRTStartup"},
228 {"WinMain", "WinMainCRTStartup"},
229 {"wWinMain", "wWinMainCRTStartup"},
230 };
231 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000232 StringRef Entry = Symtab.findMangle(mangle(E[0]));
233 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000234 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000235 }
236 return "";
237}
238
239WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000240 if (Config->DLL)
241 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000242 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000243 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000244 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000245 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
246 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000247}
248
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000249static uint64_t getDefaultImageBase() {
250 if (Config->is64())
251 return Config->DLL ? 0x180000000 : 0x140000000;
252 return Config->DLL ? 0x10000000 : 0x400000;
253}
254
Peter Collingbournefeee2102016-07-26 02:00:42 +0000255static std::string createResponseFile(const llvm::opt::InputArgList &Args,
256 ArrayRef<MemoryBufferRef> MBs,
257 ArrayRef<StringRef> SearchPaths) {
258 SmallString<0> Data;
259 raw_svector_ostream OS(Data);
260
261 for (auto *Arg : Args) {
262 switch (Arg->getOption().getID()) {
263 case OPT_linkrepro:
264 case OPT_INPUT:
265 case OPT_defaultlib:
266 case OPT_libpath:
267 break;
268 default:
269 OS << stringize(Arg) << "\n";
270 }
271 }
272
273 for (StringRef Path : SearchPaths) {
274 std::string RelPath = relativeToRoot(Path);
275 OS << "/libpath:" << quote(RelPath) << "\n";
276 }
277
278 for (MemoryBufferRef MB : MBs) {
279 std::string InputPath = relativeToRoot(MB.getBufferIdentifier());
280 OS << quote(InputPath) << "\n";
281 }
282
283 return Data.str();
284}
285
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000286static unsigned getDefaultDebugType(const llvm::opt::InputArgList &Args) {
287 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
288 if (Args.hasArg(OPT_driver))
289 DebugTypes |= static_cast<unsigned>(DebugType::PData);
290 if (Args.hasArg(OPT_profile))
291 DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
292 return DebugTypes;
293}
294
295static unsigned parseDebugType(StringRef Arg) {
296 llvm::SmallVector<StringRef, 3> Types;
297 Arg.split(Types, ',', /*KeepEmpty=*/false);
298
299 unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
300 for (StringRef Type : Types)
301 DebugTypes |= StringSwitch<unsigned>(Type.lower())
302 .Case("cv", static_cast<unsigned>(DebugType::CV))
303 .Case("pdata", static_cast<unsigned>(DebugType::PData))
304 .Case("fixup", static_cast<unsigned>(DebugType::Fixup));
305 return DebugTypes;
306}
307
Rafael Espindolab835ae82015-08-06 14:58:50 +0000308void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000309 // If the first command line argument is "/lib", link.exe acts like lib.exe.
310 // We call our own implementation of lib.exe that understands bitcode files.
311 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
312 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000313 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000314 return;
315 }
316
Peter Collingbourne60c16162015-06-01 20:10:10 +0000317 // Needed for LTO.
318 llvm::InitializeAllTargetInfos();
319 llvm::InitializeAllTargets();
320 llvm::InitializeAllTargetMCs();
321 llvm::InitializeAllAsmParsers();
322 llvm::InitializeAllAsmPrinters();
323 llvm::InitializeAllDisassemblers();
324
Rui Ueyama411c63602015-05-28 19:09:30 +0000325 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000326 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000327
Rui Ueyama5c726432015-05-29 16:11:52 +0000328 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000329 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000330 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000331 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000332 }
333
Peter Collingbournefeee2102016-07-26 02:00:42 +0000334 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
335 SmallString<64> Path = StringRef(Arg->getValue());
336 llvm::sys::path::append(Path, "repro");
337 ErrorOr<CpioFile *> F = CpioFile::create(Path);
338 if (F)
339 Cpio.reset(*F);
340 else
341 llvm::errs() << "/linkrepro: failed to open " << Path
342 << ".cpio: " << F.getError().message() << '\n';
343 }
344
Rafael Espindolab835ae82015-08-06 14:58:50 +0000345 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000346 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000347
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000348 // Construct search path list.
349 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000350 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000351 SearchPaths.push_back(Arg->getValue());
352 addLibSearchPaths();
353
Rui Ueyamaad660982015-06-07 00:20:32 +0000354 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000355 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000356 Config->OutputFile = Arg->getValue();
357
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000358 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000359 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000360 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000361
Rui Ueyama95925fd2015-06-28 19:35:15 +0000362 // Handle /force or /force:unresolved
363 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
364 Config->Force = true;
365
Rui Ueyama6600eb12015-07-04 23:37:32 +0000366 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000367 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000368 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000369 Config->DebugTypes =
370 Args.hasArg(OPT_debugtype)
371 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
372 : getDefaultDebugType(Args);
373 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000374
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000375 // Create a dummy PDB file to satisfy build sytem rules.
Rui Ueyama9f66f822016-10-11 19:45:07 +0000376 if (auto *Arg = Args.getLastArg(OPT_pdb))
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000377 Config->PDBPath = Arg->getValue();
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000378
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000379 // Handle /noentry
380 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000381 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000382 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000383 Config->NoEntry = true;
384 }
385
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000386 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000387 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000388 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000389 Config->ManifestID = 2;
390 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000391
Rui Ueyama588e8322015-06-15 01:23:58 +0000392 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000393 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000394 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000395 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000396 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000397 Config->DynamicBase = false;
398 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000399
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000400 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000401 if (auto *Arg = Args.getLastArg(OPT_machine))
402 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000403
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000404 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000405 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000406 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
407
408 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000409 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000410 Config->NoDefaultLibAll = true;
411
Rui Ueyama804a8b62015-05-29 16:18:15 +0000412 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000413 if (auto *Arg = Args.getLastArg(OPT_base))
414 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000415
416 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000417 if (auto *Arg = Args.getLastArg(OPT_stack))
418 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000419
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000420 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000421 if (auto *Arg = Args.getLastArg(OPT_heap))
422 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000423
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000424 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000425 if (auto *Arg = Args.getLastArg(OPT_version))
426 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
427 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000428
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000429 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000430 if (auto *Arg = Args.getLastArg(OPT_subsystem))
431 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
432 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000433
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000434 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000435 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000436 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000437
Rui Ueyama08d5e182015-06-18 23:20:11 +0000438 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000439 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000440 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000441
Rui Ueyamab95188c2015-06-18 20:27:09 +0000442 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000443 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000444 Config->Implib = Arg->getValue();
445
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000446 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000447 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000448 std::string Str = StringRef(Arg->getValue()).lower();
449 SmallVector<StringRef, 1> Vec;
450 StringRef(Str).split(Vec, ',');
451 for (StringRef S : Vec) {
452 if (S == "noref") {
453 Config->DoGC = false;
454 Config->DoICF = false;
455 continue;
456 }
457 if (S == "icf" || StringRef(S).startswith("icf=")) {
458 Config->DoICF = true;
459 continue;
460 }
461 if (S == "noicf") {
462 Config->DoICF = false;
463 continue;
464 }
465 if (StringRef(S).startswith("lldlto=")) {
466 StringRef OptLevel = StringRef(S).substr(7);
467 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
468 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000469 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000470 continue;
471 }
472 if (StringRef(S).startswith("lldltojobs=")) {
473 StringRef Jobs = StringRef(S).substr(11);
474 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000475 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000476 continue;
477 }
478 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000479 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000480 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000481 }
482
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000483 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000484 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000485 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000486
Rui Ueyama6600eb12015-07-04 23:37:32 +0000487 // Handle /merge
488 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000489 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000490
Rui Ueyama440138c2016-06-20 03:39:39 +0000491 // Handle /section
492 for (auto *Arg : Args.filtered(OPT_section))
493 parseSection(Arg->getValue());
494
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000495 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000496 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
497 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000498
499 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000500 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
501 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000502
503 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000504 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000505 Config->ManifestDependency = Arg->getValue();
506
507 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000508 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000509 Config->ManifestFile = Arg->getValue();
510
Rui Ueyamaafb19012016-04-19 01:21:58 +0000511 // Handle /manifestinput
512 for (auto *Arg : Args.filtered(OPT_manifestinput))
513 Config->ManifestInput.push_back(Arg->getValue());
514
Rui Ueyama6592ff82015-06-16 23:13:00 +0000515 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000516 if (Args.hasArg(OPT_allowbind_no))
517 Config->AllowBind = false;
518 if (Args.hasArg(OPT_allowisolation_no))
519 Config->AllowIsolation = false;
520 if (Args.hasArg(OPT_dynamicbase_no))
521 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000522 if (Args.hasArg(OPT_nxcompat_no))
523 Config->NxCompat = false;
524 if (Args.hasArg(OPT_tsaware_no))
525 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000526 if (Args.hasArg(OPT_nosymtab))
527 Config->WriteSymtab = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000528
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000529 // Create a list of input files. Files can be given as arguments
530 // for /defaultlib option.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000531 std::vector<StringRef> Paths;
532 std::vector<MemoryBufferRef> MBs;
David Blaikie6521ed92015-06-22 22:06:52 +0000533 for (auto *Arg : Args.filtered(OPT_INPUT))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000534 if (Optional<StringRef> Path = findFile(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000535 Paths.push_back(*Path);
David Blaikie6521ed92015-06-22 22:06:52 +0000536 for (auto *Arg : Args.filtered(OPT_defaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000537 if (Optional<StringRef> Path = findLib(Arg->getValue()))
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000538 Paths.push_back(*Path);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000539 for (StringRef Path : Paths)
540 MBs.push_back(openFile(Path));
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000541
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000542 // Windows specific -- Create a resource file containing a manifest file.
543 if (Config->Manifest == Configuration::Embed) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000544 std::unique_ptr<MemoryBuffer> MB = createManifestRes();
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000545 MBs.push_back(MB->getMemBufferRef());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000546 OwningMBs.push_back(std::move(MB)); // take ownership
547 }
548
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000549 // Windows specific -- Input files can be Windows resource files (.res files).
550 // We invoke cvtres.exe to convert resource files to a regular COFF file
551 // then link the result file normally.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000552 std::vector<MemoryBufferRef> Resources;
Rui Ueyama77731b42015-06-26 23:59:13 +0000553 auto NotResource = [](MemoryBufferRef MB) {
554 return identify_magic(MB.getBuffer()) != file_magic::windows_resource;
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000555 };
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000556 auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource);
557 if (It != MBs.end()) {
558 Resources.insert(Resources.end(), It, MBs.end());
559 MBs.erase(It, MBs.end());
Rui Ueyama2bf6a122015-06-14 21:50:50 +0000560 }
561
Rui Ueyama85225b02015-07-02 03:15:15 +0000562 // Read all input files given via the command line. Note that step()
563 // doesn't read files that are specified by directive sections.
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000564 for (MemoryBufferRef MB : MBs)
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000565 Symtab.addFile(createFile(MB));
Rafael Espindolab835ae82015-08-06 14:58:50 +0000566 Symtab.step();
Rui Ueyama5cff6852015-05-31 03:34:08 +0000567
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000568 // Determine machine type and check if all object files are
569 // for the same CPU type. Note that this needs to be done before
570 // any call to mangle().
571 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
572 MachineTypes MT = File->getMachineType();
573 if (MT == IMAGE_FILE_MACHINE_UNKNOWN)
574 continue;
Rui Ueyama5e706b32015-07-25 21:54:50 +0000575 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
576 Config->Machine = MT;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000577 continue;
578 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000579 if (Config->Machine != MT)
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000580 fatal(File->getShortName() + ": machine type " + machineToStr(MT) +
Rafael Espindolab835ae82015-08-06 14:58:50 +0000581 " conflicts with " + machineToStr(Config->Machine));
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000582 }
Rui Ueyama5e706b32015-07-25 21:54:50 +0000583 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000584 llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n";
Rui Ueyama5e706b32015-07-25 21:54:50 +0000585 Config->Machine = AMD64;
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000586 }
587
588 // Windows specific -- Convert Windows resource files to a COFF file.
589 if (!Resources.empty()) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000590 std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources);
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000591 Symtab.addFile(createFile(MB->getMemBufferRef()));
Peter Collingbournefeee2102016-07-26 02:00:42 +0000592
593 MBs.push_back(MB->getMemBufferRef());
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000594 OwningMBs.push_back(std::move(MB)); // take ownership
595 }
596
Peter Collingbournefeee2102016-07-26 02:00:42 +0000597 if (Cpio)
598 Cpio->append("response.txt",
599 createResponseFile(Args, MBs,
600 ArrayRef<StringRef>(SearchPaths).slice(1)));
601
Rui Ueyama4d545342015-07-28 03:12:00 +0000602 // Handle /largeaddressaware
603 if (Config->is64() || Args.hasArg(OPT_largeaddressaware))
604 Config->LargeAddressAware = true;
605
Rui Ueyamad68e2112015-07-28 03:15:57 +0000606 // Handle /highentropyva
607 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no))
608 Config->HighEntropyVA = true;
609
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000610 // Handle /entry and /dll
611 if (auto *Arg = Args.getLastArg(OPT_entry)) {
612 Config->Entry = addUndefined(mangle(Arg->getValue()));
613 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000614 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
615 : "_DllMainCRTStartup";
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000616 Config->Entry = addUndefined(S);
617 } else if (!Config->NoEntry) {
618 // Windows specific -- If entry point name is not given, we need to
619 // infer that from user-defined entry name.
Rui Ueyama45044f42015-06-29 01:03:53 +0000620 StringRef S = findDefaultEntry();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000621 if (S.empty())
Rui Ueyama60604792016-07-14 23:37:14 +0000622 fatal("entry point must be defined");
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000623 Config->Entry = addUndefined(S);
Rui Ueyama85225b02015-07-02 03:15:15 +0000624 if (Config->Verbose)
625 llvm::outs() << "Entry name inferred: " << S << "\n";
Rui Ueyama45044f42015-06-29 01:03:53 +0000626 }
627
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000628 // Handle /export
629 for (auto *Arg : Args.filtered(OPT_export)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000630 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000631 if (Config->Machine == I386) {
632 if (!isDecorated(E.Name))
633 E.Name = Alloc.save("_" + E.Name);
634 if (!E.ExtName.empty() && !isDecorated(E.ExtName))
635 E.ExtName = Alloc.save("_" + E.ExtName);
636 }
Rafael Espindolab835ae82015-08-06 14:58:50 +0000637 Config->Exports.push_back(E);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000638 }
639
640 // Handle /def
641 if (auto *Arg = Args.getLastArg(OPT_deffile)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000642 MemoryBufferRef MB = openFile(Arg->getValue());
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000643 // parseModuleDefs mutates Config object.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000644 parseModuleDefs(MB, &Alloc);
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000645 }
646
Rui Ueyama6d249082015-07-13 22:31:45 +0000647 // Handle /delayload
648 for (auto *Arg : Args.filtered(OPT_delayload)) {
649 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
Rui Ueyama5e706b32015-07-25 21:54:50 +0000650 if (Config->Machine == I386) {
Rui Ueyama6d249082015-07-13 22:31:45 +0000651 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8");
Rui Ueyama35ccb0f2015-07-25 00:20:06 +0000652 } else {
653 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2");
Rui Ueyama6d249082015-07-13 22:31:45 +0000654 }
655 }
656
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000657 // Set default image base if /base is not given.
658 if (Config->ImageBase == uint64_t(-1))
659 Config->ImageBase = getDefaultImageBase();
660
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000661 Symtab.addRelative(mangle("__ImageBase"), 0);
Rui Ueyama5e706b32015-07-25 21:54:50 +0000662 if (Config->Machine == I386) {
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000663 Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0);
664 Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0);
665 }
Rui Ueyamabbdec4f2015-07-09 22:51:41 +0000666
Rui Ueyama107db552015-08-09 21:01:06 +0000667 // We do not support /guard:cf (control flow protection) yet.
668 // Define CFG symbols anyway so that we can link MSVC 2015 CRT.
669 Symtab.addAbsolute(mangle("__guard_fids_table"), 0);
670 Symtab.addAbsolute(mangle("__guard_fids_count"), 0);
671 Symtab.addAbsolute(mangle("__guard_flags"), 0x100);
672
Rui Ueyamaea533cd2015-07-09 19:54:13 +0000673 // Read as much files as we can from directives sections.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000674 Symtab.run();
Rui Ueyama85225b02015-07-02 03:15:15 +0000675
676 // Resolve auxiliary symbols until we get a convergence.
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000677 // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file.
678 // A new file may contain a directive section to add new command line options.
679 // That's why we have to repeat until converge.)
680 for (;;) {
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000681 // Windows specific -- if entry point is not found,
682 // search for its mangled names.
683 if (Config->Entry)
684 Symtab.mangleMaybe(Config->Entry);
685
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000686 // Windows specific -- Make sure we resolve all dllexported symbols.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000687 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000688 if (!E.ForwardTo.empty())
689 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000690 E.Sym = addUndefined(E.Name);
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000691 if (!E.Directives)
692 Symtab.mangleMaybe(E.Sym);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000693 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000694
695 // Add weak aliases. Weak aliases is a mechanism to give remaining
696 // undefined symbols final chance to be resolved successfully.
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000697 for (auto Pair : Config->AlternateNames) {
698 StringRef From = Pair.first;
699 StringRef To = Pair.second;
Rui Ueyama458d7442015-07-02 03:59:04 +0000700 Symbol *Sym = Symtab.find(From);
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000701 if (!Sym)
702 continue;
Rui Ueyama183f53f2015-07-06 17:45:22 +0000703 if (auto *U = dyn_cast<Undefined>(Sym->Body))
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000704 if (!U->WeakAlias)
705 U->WeakAlias = Symtab.addUndefined(To);
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000706 }
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000707
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000708 // Windows specific -- if __load_config_used can be resolved, resolve it.
Rui Ueyama8ebdc8c2015-08-07 22:43:53 +0000709 if (Symtab.findUnderscore("_load_config_used"))
710 addUndefined(mangle("_load_config_used"));
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000711
Rui Ueyama3d4c69c2015-07-02 02:38:59 +0000712 if (Symtab.queueEmpty())
713 break;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000714 Symtab.run();
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000715 }
716
Peter Collingbournedf5783b2015-08-28 22:16:09 +0000717 // Do LTO by compiling bitcode input files to a set of native COFF files then
718 // link those files.
719 Symtab.addCombinedLTOObjects();
Peter Collingbourne60c16162015-06-01 20:10:10 +0000720
Peter Collingbourne2612a322015-07-04 05:28:41 +0000721 // Make sure we have resolved all symbols.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000722 Symtab.reportRemainingUndefines(/*Resolve=*/true);
Peter Collingbourne2612a322015-07-04 05:28:41 +0000723
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000724 // Windows specific -- if no /subsystem is given, we need to infer
725 // that from entry point name.
726 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000727 Config->Subsystem = inferSubsystem();
Rafael Espindolab835ae82015-08-06 14:58:50 +0000728 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
Rui Ueyama60604792016-07-14 23:37:14 +0000729 fatal("subsystem must be defined");
Rui Ueyama3ee0fe42015-05-31 03:55:46 +0000730 }
731
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000732 // Handle /safeseh.
Rui Ueyama13563d82015-09-15 00:33:11 +0000733 if (Args.hasArg(OPT_safeseh))
734 for (ObjectFile *File : Symtab.ObjectFiles)
735 if (!File->SEHCompat)
Rui Ueyama60604792016-07-14 23:37:14 +0000736 fatal("/safeseh: " + File->getName() + " is not compatible with SEH");
Rui Ueyamaff88d5a2015-07-29 20:25:40 +0000737
Rui Ueyama151d8622015-06-17 20:40:43 +0000738 // Windows specific -- when we are creating a .dll file, we also
739 // need to create a .lib file.
Rui Ueyama100ffac2015-09-01 09:15:58 +0000740 if (!Config->Exports.empty() || Config->DLL) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000741 fixupExports();
742 writeImportLibrary();
Rui Ueyama8765fba2015-07-15 22:21:08 +0000743 assignExportOrdinals();
744 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000745
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000746 // Windows specific -- Create a side-by-side manifest file.
747 if (Config->Manifest == Configuration::SideBySide)
Rafael Espindolab835ae82015-08-06 14:58:50 +0000748 createSideBySideManifest();
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000749
Rui Ueyamaa5f0f752015-09-19 21:36:28 +0000750 // Identify unreferenced COMDAT sections.
751 if (Config->DoGC)
752 markLive(Symtab.getChunks());
753
754 // Identify identical COMDAT sections to merge them.
755 if (Config->DoICF)
756 doICF(Symtab.getChunks());
757
Rui Ueyama411c63602015-05-28 19:09:30 +0000758 // Write the result.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000759 writeResult(&Symtab);
Peter Collingbournebe549552015-06-26 18:58:24 +0000760
Rui Ueyama016414f2015-06-28 20:07:08 +0000761 // Create a symbol map file containing symbol VAs and their names
762 // to help debugging.
Peter Collingbournebe549552015-06-26 18:58:24 +0000763 if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
764 std::error_code EC;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000765 llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
Rui Ueyama0d09a862016-07-15 00:40:46 +0000766 if (EC)
Rui Ueyamabb579542016-07-15 01:12:24 +0000767 fatal(EC, "could not create the symbol map");
Peter Collingbournebe549552015-06-26 18:58:24 +0000768 Symtab.printMap(Out);
769 }
Rui Ueyamaa51ce712015-07-03 05:31:35 +0000770 // Call exit to avoid calling destructors.
771 exit(0);
Rui Ueyama411c63602015-05-28 19:09:30 +0000772}
773
Rui Ueyama411c63602015-05-28 19:09:30 +0000774} // namespace coff
775} // namespace lld