blob: 4c7c4d8747fbf6f54dce9079bdb238ae8347f08a [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 Ueyama1d99ab32016-09-15 22:24:51 +000014#include "PDB.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000015#include "SymbolTable.h"
Rui Ueyama685c41c2015-08-05 23:43:53 +000016#include "Symbols.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000017#include "Writer.h"
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000018#include "lld/Driver/Driver.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 Ueyama417553d2016-02-28 19:54:51 +000046bool link(llvm::ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000047 Configuration C;
48 LinkerDriver D;
49 Config = &C;
50 Driver = &D;
Rui Ueyama417553d2016-02-28 19:54:51 +000051 Driver->link(Args);
52 return true;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000053}
Rui Ueyama411c63602015-05-28 19:09:30 +000054
Nico Weber5660de72016-04-20 22:34:15 +000055// Drop directory components and replace extension with ".exe" or ".dll".
Rui Ueyamaad660982015-06-07 00:20:32 +000056static std::string getOutputPath(StringRef Path) {
57 auto P = Path.find_last_of("\\/");
58 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1);
Nico Weber5660de72016-04-20 22:34:15 +000059 const char* E = Config->DLL ? ".dll" : ".exe";
60 return (S.substr(0, S.rfind('.')) + E).str();
Rui Ueyama411c63602015-05-28 19:09:30 +000061}
62
Rui Ueyamad7c2f582015-05-31 21:04:56 +000063// Opens a file. Path has to be resolved already.
64// Newly created memory buffers are owned by this driver.
Rafael Espindolab835ae82015-08-06 14:58:50 +000065MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
Rui Ueyama659a4f22016-07-15 01:06:38 +000066 std::unique_ptr<MemoryBuffer> MB =
Rui Ueyamabb579542016-07-15 01:12:24 +000067 check(MemoryBuffer::getFile(Path), "could not open " + Path);
Rui Ueyamad7c2f582015-05-31 21:04:56 +000068 MemoryBufferRef MBRef = MB->getMemBufferRef();
69 OwningMBs.push_back(std::move(MB)); // take ownership
Rui Ueyama2bf6a122015-06-14 21:50:50 +000070 return MBRef;
71}
Rui Ueyama711cd2d2015-05-31 21:17:10 +000072
Rui Ueyama2bf6a122015-06-14 21:50:50 +000073static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
Peter Collingbournefeee2102016-07-26 02:00:42 +000074 if (Driver->Cpio)
75 Driver->Cpio->append(relativeToRoot(MB.getBufferIdentifier()),
76 MB.getBuffer());
77
Rui Ueyama711cd2d2015-05-31 21:17:10 +000078 // File type is detected by contents, not by file extension.
Rui Ueyama2bf6a122015-06-14 21:50:50 +000079 file_magic Magic = identify_magic(MB.getBuffer());
Rui Ueyama711cd2d2015-05-31 21:17:10 +000080 if (Magic == file_magic::archive)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000081 return std::unique_ptr<InputFile>(new ArchiveFile(MB));
Peter Collingbourne60c16162015-06-01 20:10:10 +000082 if (Magic == file_magic::bitcode)
Rui Ueyama2bf6a122015-06-14 21:50:50 +000083 return std::unique_ptr<InputFile>(new BitcodeFile(MB));
Rui Ueyamaad660982015-06-07 00:20:32 +000084 if (Config->OutputFile == "")
Rui Ueyama2bf6a122015-06-14 21:50:50 +000085 Config->OutputFile = getOutputPath(MB.getBufferIdentifier());
86 return std::unique_ptr<InputFile>(new ObjectFile(MB));
Rui Ueyama411c63602015-05-28 19:09:30 +000087}
88
Rui Ueyamaf10a3202015-08-31 08:43:21 +000089static bool isDecorated(StringRef Sym) {
90 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
91}
92
Rui Ueyama411c63602015-05-28 19:09:30 +000093// Parses .drectve section contents and returns a list of files
94// specified by /defaultlib.
Rafael Espindolab835ae82015-08-06 14:58:50 +000095void LinkerDriver::parseDirectives(StringRef S) {
96 llvm::opt::InputArgList Args = Parser.parse(S);
Rui Ueyama411c63602015-05-28 19:09:30 +000097
David Blaikie6521ed92015-06-22 22:06:52 +000098 for (auto *Arg : Args) {
Rui Ueyama562daa82015-06-18 21:50:38 +000099 switch (Arg->getOption().getID()) {
100 case OPT_alternatename:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000101 parseAlternateName(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000102 break;
103 case OPT_defaultlib:
104 if (Optional<StringRef> Path = findLib(Arg->getValue())) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000105 MemoryBufferRef MB = openFile(*Path);
106 Symtab.addFile(createFile(MB));
Rui Ueyama562daa82015-06-18 21:50:38 +0000107 }
108 break;
109 case OPT_export: {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000110 Export E = parseExport(Arg->getValue());
Rui Ueyamaf10a3202015-08-31 08:43:21 +0000111 E.Directives = true;
Rafael Espindolab835ae82015-08-06 14:58:50 +0000112 Config->Exports.push_back(E);
Rui Ueyama562daa82015-06-18 21:50:38 +0000113 break;
114 }
115 case OPT_failifmismatch:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000116 checkFailIfMismatch(Arg->getValue());
Rui Ueyama562daa82015-06-18 21:50:38 +0000117 break;
Rui Ueyama08d5e182015-06-18 23:20:11 +0000118 case OPT_incl:
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000119 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000120 break;
Rui Ueyamace86c992015-06-18 23:22:39 +0000121 case OPT_merge:
Rafael Espindolab835ae82015-08-06 14:58:50 +0000122 parseMerge(Arg->getValue());
Rui Ueyamace86c992015-06-18 23:22:39 +0000123 break;
124 case OPT_nodefaultlib:
125 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
126 break;
Rui Ueyama440138c2016-06-20 03:39:39 +0000127 case OPT_section:
128 parseSection(Arg->getValue());
129 break;
Rui Ueyama3c4737d2015-08-11 16:46:08 +0000130 case OPT_editandcontinue:
Reid Kleckner9cd77ce2016-03-25 18:09:29 +0000131 case OPT_fastfail:
Rui Ueyama31e66e32015-09-03 16:20:47 +0000132 case OPT_guardsym:
Rui Ueyama432383172015-07-29 21:01:15 +0000133 case OPT_throwingnew:
Rui Ueyama46682632015-07-29 20:29:15 +0000134 break;
Rui Ueyama562daa82015-06-18 21:50:38 +0000135 default:
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000136 fatal(Arg->getSpelling() + " is not allowed in .drectve");
Rui Ueyamad7c2f582015-05-31 21:04:56 +0000137 }
138 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000139}
140
Rui Ueyama54b71da2015-05-31 19:17:12 +0000141// Find file from search paths. You can omit ".obj", this function takes
142// care of that. Note that the returned path is not guaranteed to exist.
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000143StringRef LinkerDriver::doFindFile(StringRef Filename) {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000144 bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos);
145 if (hasPathSep)
146 return Filename;
147 bool hasExt = (Filename.find('.') != StringRef::npos);
148 for (StringRef Dir : SearchPaths) {
149 SmallString<128> Path = Dir;
150 llvm::sys::path::append(Path, Filename);
151 if (llvm::sys::fs::exists(Path.str()))
152 return Alloc.save(Path.str());
153 if (!hasExt) {
154 Path.append(".obj");
155 if (llvm::sys::fs::exists(Path.str()))
156 return Alloc.save(Path.str());
157 }
158 }
159 return Filename;
160}
161
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000162// Resolves a file path. This never returns the same path
163// (in that case, it returns None).
164Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
165 StringRef Path = doFindFile(Filename);
166 bool Seen = !VisitedFiles.insert(Path.lower()).second;
167 if (Seen)
168 return None;
169 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000170}
171
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000172// Find library file from search path.
173StringRef LinkerDriver::doFindLib(StringRef Filename) {
174 // Add ".lib" to Filename if that has no file extension.
Rui Ueyama54b71da2015-05-31 19:17:12 +0000175 bool hasExt = (Filename.find('.') != StringRef::npos);
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000176 if (!hasExt)
177 Filename = Alloc.save(Filename + ".lib");
178 return doFindFile(Filename);
179}
180
181// Resolves a library path. /nodefaultlib options are taken into
182// consideration. This never returns the same path (in that case,
183// it returns None).
184Optional<StringRef> LinkerDriver::findLib(StringRef Filename) {
185 if (Config->NoDefaultLibAll)
186 return None;
187 StringRef Path = doFindLib(Filename);
188 if (Config->NoDefaultLibs.count(Path))
189 return None;
190 bool Seen = !VisitedFiles.insert(Path.lower()).second;
191 if (Seen)
192 return None;
193 return Path;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000194}
195
196// Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000197void LinkerDriver::addLibSearchPaths() {
Rui Ueyama54b71da2015-05-31 19:17:12 +0000198 Optional<std::string> EnvOpt = Process::GetEnv("LIB");
199 if (!EnvOpt.hasValue())
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000200 return;
Rui Ueyama54b71da2015-05-31 19:17:12 +0000201 StringRef Env = Alloc.save(*EnvOpt);
202 while (!Env.empty()) {
203 StringRef Path;
204 std::tie(Path, Env) = Env.split(';');
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000205 SearchPaths.push_back(Path);
Rui Ueyama54b71da2015-05-31 19:17:12 +0000206 }
Rui Ueyama54b71da2015-05-31 19:17:12 +0000207}
208
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000209Undefined *LinkerDriver::addUndefined(StringRef Name) {
210 Undefined *U = Symtab.addUndefined(Name);
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000211 Config->GCRoot.insert(U);
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000212 return U;
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000213}
214
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000215// Symbol names are mangled by appending "_" prefix on x86.
216StringRef LinkerDriver::mangle(StringRef Sym) {
Rui Ueyama5e706b32015-07-25 21:54:50 +0000217 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
218 if (Config->Machine == I386)
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000219 return Alloc.save("_" + Sym);
220 return Sym;
221}
222
Rui Ueyama45044f42015-06-29 01:03:53 +0000223// Windows specific -- find default entry point name.
224StringRef LinkerDriver::findDefaultEntry() {
225 // User-defined main functions and their corresponding entry points.
226 static const char *Entries[][2] = {
227 {"main", "mainCRTStartup"},
228 {"wmain", "wmainCRTStartup"},
229 {"WinMain", "WinMainCRTStartup"},
230 {"wWinMain", "wWinMainCRTStartup"},
231 };
232 for (auto E : Entries) {
Rui Ueyamaa50387f2015-07-14 02:58:13 +0000233 StringRef Entry = Symtab.findMangle(mangle(E[0]));
234 if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body))
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000235 return mangle(E[1]);
Rui Ueyama45044f42015-06-29 01:03:53 +0000236 }
237 return "";
238}
239
240WindowsSubsystem LinkerDriver::inferSubsystem() {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000241 if (Config->DLL)
242 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000243 if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000244 return IMAGE_SUBSYSTEM_WINDOWS_CUI;
Rui Ueyama611add22015-08-08 00:23:37 +0000245 if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain"))
Rui Ueyama45044f42015-06-29 01:03:53 +0000246 return IMAGE_SUBSYSTEM_WINDOWS_GUI;
247 return IMAGE_SUBSYSTEM_UNKNOWN;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000248}
249
Rui Ueyama5c437cd2015-07-25 21:42:33 +0000250static uint64_t getDefaultImageBase() {
251 if (Config->is64())
252 return Config->DLL ? 0x180000000 : 0x140000000;
253 return Config->DLL ? 0x10000000 : 0x400000;
254}
255
Peter Collingbournefeee2102016-07-26 02:00:42 +0000256static std::string createResponseFile(const llvm::opt::InputArgList &Args,
257 ArrayRef<MemoryBufferRef> MBs,
258 ArrayRef<StringRef> SearchPaths) {
259 SmallString<0> Data;
260 raw_svector_ostream OS(Data);
261
262 for (auto *Arg : Args) {
263 switch (Arg->getOption().getID()) {
264 case OPT_linkrepro:
265 case OPT_INPUT:
266 case OPT_defaultlib:
267 case OPT_libpath:
268 break;
269 default:
270 OS << stringize(Arg) << "\n";
271 }
272 }
273
274 for (StringRef Path : SearchPaths) {
275 std::string RelPath = relativeToRoot(Path);
276 OS << "/libpath:" << quote(RelPath) << "\n";
277 }
278
279 for (MemoryBufferRef MB : MBs) {
280 std::string InputPath = relativeToRoot(MB.getBufferIdentifier());
281 OS << quote(InputPath) << "\n";
282 }
283
284 return Data.str();
285}
286
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000287static unsigned getDefaultDebugType(const llvm::opt::InputArgList &Args) {
288 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV);
289 if (Args.hasArg(OPT_driver))
290 DebugTypes |= static_cast<unsigned>(DebugType::PData);
291 if (Args.hasArg(OPT_profile))
292 DebugTypes |= static_cast<unsigned>(DebugType::Fixup);
293 return DebugTypes;
294}
295
296static unsigned parseDebugType(StringRef Arg) {
297 llvm::SmallVector<StringRef, 3> Types;
298 Arg.split(Types, ',', /*KeepEmpty=*/false);
299
300 unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
301 for (StringRef Type : Types)
302 DebugTypes |= StringSwitch<unsigned>(Type.lower())
303 .Case("cv", static_cast<unsigned>(DebugType::CV))
304 .Case("pdata", static_cast<unsigned>(DebugType::PData))
305 .Case("fixup", static_cast<unsigned>(DebugType::Fixup));
306 return DebugTypes;
307}
308
Rafael Espindolab835ae82015-08-06 14:58:50 +0000309void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) {
Rui Ueyama27e470a2015-08-09 20:45:17 +0000310 // If the first command line argument is "/lib", link.exe acts like lib.exe.
311 // We call our own implementation of lib.exe that understands bitcode files.
312 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
313 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000314 fatal("lib failed");
Rui Ueyama27e470a2015-08-09 20:45:17 +0000315 return;
316 }
317
Peter Collingbourne60c16162015-06-01 20:10:10 +0000318 // Needed for LTO.
319 llvm::InitializeAllTargetInfos();
320 llvm::InitializeAllTargets();
321 llvm::InitializeAllTargetMCs();
322 llvm::InitializeAllAsmParsers();
323 llvm::InitializeAllAsmPrinters();
324 llvm::InitializeAllDisassemblers();
325
Rui Ueyama411c63602015-05-28 19:09:30 +0000326 // Parse command line options.
Rafael Espindolab835ae82015-08-06 14:58:50 +0000327 llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
Rui Ueyama411c63602015-05-28 19:09:30 +0000328
Rui Ueyama5c726432015-05-29 16:11:52 +0000329 // Handle /help
David Blaikie6521ed92015-06-22 22:06:52 +0000330 if (Args.hasArg(OPT_help)) {
David Blaikieb2b1c7c2015-06-21 06:32:10 +0000331 printHelp(ArgsArr[0]);
Rafael Espindolab835ae82015-08-06 14:58:50 +0000332 return;
Rui Ueyama5c726432015-05-29 16:11:52 +0000333 }
334
Peter Collingbournefeee2102016-07-26 02:00:42 +0000335 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) {
336 SmallString<64> Path = StringRef(Arg->getValue());
337 llvm::sys::path::append(Path, "repro");
338 ErrorOr<CpioFile *> F = CpioFile::create(Path);
339 if (F)
340 Cpio.reset(*F);
341 else
342 llvm::errs() << "/linkrepro: failed to open " << Path
343 << ".cpio: " << F.getError().message() << '\n';
344 }
345
Rafael Espindolab835ae82015-08-06 14:58:50 +0000346 if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end())
Rui Ueyamabb579542016-07-15 01:12:24 +0000347 fatal("no input files");
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000348
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000349 // Construct search path list.
350 SearchPaths.push_back("");
David Blaikie6521ed92015-06-22 22:06:52 +0000351 for (auto *Arg : Args.filtered(OPT_libpath))
Rui Ueyamaf00df0a2015-06-19 22:39:48 +0000352 SearchPaths.push_back(Arg->getValue());
353 addLibSearchPaths();
354
Rui Ueyamaad660982015-06-07 00:20:32 +0000355 // Handle /out
David Blaikie6521ed92015-06-22 22:06:52 +0000356 if (auto *Arg = Args.getLastArg(OPT_out))
Rui Ueyamaad660982015-06-07 00:20:32 +0000357 Config->OutputFile = Arg->getValue();
358
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000359 // Handle /verbose
David Blaikie6521ed92015-06-22 22:06:52 +0000360 if (Args.hasArg(OPT_verbose))
Rui Ueyama411c63602015-05-28 19:09:30 +0000361 Config->Verbose = true;
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000362
Rui Ueyama95925fd2015-06-28 19:35:15 +0000363 // Handle /force or /force:unresolved
364 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved))
365 Config->Force = true;
366
Rui Ueyama6600eb12015-07-04 23:37:32 +0000367 // Handle /debug
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000368 if (Args.hasArg(OPT_debug)) {
Rui Ueyama6600eb12015-07-04 23:37:32 +0000369 Config->Debug = true;
Saleem Abdulrasoola2cca7e2016-08-08 22:02:44 +0000370 Config->DebugTypes =
371 Args.hasArg(OPT_debugtype)
372 ? parseDebugType(Args.getLastArg(OPT_debugtype)->getValue())
373 : getDefaultDebugType(Args);
374 }
Rui Ueyama6600eb12015-07-04 23:37:32 +0000375
Saleem Abdulrasool8fcff932016-08-29 21:20:46 +0000376 // Create a dummy PDB file to satisfy build sytem rules.
377 if (auto *Arg = Args.getLastArg(OPT_pdb)) {
378 Config->PDBPath = Arg->getValue();
379 createPDB(Config->PDBPath);
380 }
381
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000382 // Handle /noentry
383 if (Args.hasArg(OPT_noentry)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000384 if (!Args.hasArg(OPT_dll))
Rui Ueyama60604792016-07-14 23:37:14 +0000385 fatal("/noentry must be specified with /dll");
Rui Ueyamaa8b60452015-06-28 19:56:30 +0000386 Config->NoEntry = true;
387 }
388
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000389 // Handle /dll
David Blaikie6521ed92015-06-22 22:06:52 +0000390 if (Args.hasArg(OPT_dll)) {
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000391 Config->DLL = true;
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000392 Config->ManifestID = 2;
393 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000394
Rui Ueyama588e8322015-06-15 01:23:58 +0000395 // Handle /fixed
David Blaikie6521ed92015-06-22 22:06:52 +0000396 if (Args.hasArg(OPT_fixed)) {
Rafael Espindolab835ae82015-08-06 14:58:50 +0000397 if (Args.hasArg(OPT_dynamicbase))
Rui Ueyama60604792016-07-14 23:37:14 +0000398 fatal("/fixed must not be specified with /dynamicbase");
Rui Ueyama588e8322015-06-15 01:23:58 +0000399 Config->Relocatable = false;
Rui Ueyama6592ff82015-06-16 23:13:00 +0000400 Config->DynamicBase = false;
401 }
Rui Ueyama588e8322015-06-15 01:23:58 +0000402
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000403 // Handle /machine
Rafael Espindolab835ae82015-08-06 14:58:50 +0000404 if (auto *Arg = Args.getLastArg(OPT_machine))
405 Config->Machine = getMachineType(Arg->getValue());
Rui Ueyama3d3e6fb2015-05-29 16:06:00 +0000406
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000407 // Handle /nodefaultlib:<filename>
David Blaikie6521ed92015-06-22 22:06:52 +0000408 for (auto *Arg : Args.filtered(OPT_nodefaultlib))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000409 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue()));
410
411 // Handle /nodefaultlib
David Blaikie6521ed92015-06-22 22:06:52 +0000412 if (Args.hasArg(OPT_nodefaultlib_all))
Rui Ueyamad21b00b2015-05-31 19:17:14 +0000413 Config->NoDefaultLibAll = true;
414
Rui Ueyama804a8b62015-05-29 16:18:15 +0000415 // Handle /base
Rafael Espindolab835ae82015-08-06 14:58:50 +0000416 if (auto *Arg = Args.getLastArg(OPT_base))
417 parseNumbers(Arg->getValue(), &Config->ImageBase);
Rui Ueyamab41b7e52015-05-29 16:21:11 +0000418
419 // Handle /stack
Rafael Espindolab835ae82015-08-06 14:58:50 +0000420 if (auto *Arg = Args.getLastArg(OPT_stack))
421 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit);
Rui Ueyama804a8b62015-05-29 16:18:15 +0000422
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000423 // Handle /heap
Rafael Espindolab835ae82015-08-06 14:58:50 +0000424 if (auto *Arg = Args.getLastArg(OPT_heap))
425 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit);
Rui Ueyamac377e9a2015-05-29 16:23:40 +0000426
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000427 // Handle /version
Rafael Espindolab835ae82015-08-06 14:58:50 +0000428 if (auto *Arg = Args.getLastArg(OPT_version))
429 parseVersion(Arg->getValue(), &Config->MajorImageVersion,
430 &Config->MinorImageVersion);
Rui Ueyamab9dcdb52015-05-29 16:28:29 +0000431
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000432 // Handle /subsystem
Rafael Espindolab835ae82015-08-06 14:58:50 +0000433 if (auto *Arg = Args.getLastArg(OPT_subsystem))
434 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion,
435 &Config->MinorOSVersion);
Rui Ueyama15cc47e2015-05-29 16:34:31 +0000436
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000437 // Handle /alternatename
David Blaikie6521ed92015-06-22 22:06:52 +0000438 for (auto *Arg : Args.filtered(OPT_alternatename))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000439 parseAlternateName(Arg->getValue());
Rui Ueyama2edb35a2015-06-18 19:09:30 +0000440
Rui Ueyama08d5e182015-06-18 23:20:11 +0000441 // Handle /include
David Blaikie6521ed92015-06-22 22:06:52 +0000442 for (auto *Arg : Args.filtered(OPT_incl))
Rui Ueyama32f8e1c2015-06-26 03:44:00 +0000443 addUndefined(Arg->getValue());
Rui Ueyama08d5e182015-06-18 23:20:11 +0000444
Rui Ueyamab95188c2015-06-18 20:27:09 +0000445 // Handle /implib
David Blaikie6521ed92015-06-22 22:06:52 +0000446 if (auto *Arg = Args.getLastArg(OPT_implib))
Rui Ueyamab95188c2015-06-18 20:27:09 +0000447 Config->Implib = Arg->getValue();
448
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000449 // Handle /opt
David Blaikie6521ed92015-06-22 22:06:52 +0000450 for (auto *Arg : Args.filtered(OPT_opt)) {
Rui Ueyama75656ee2015-10-19 19:40:43 +0000451 std::string Str = StringRef(Arg->getValue()).lower();
452 SmallVector<StringRef, 1> Vec;
453 StringRef(Str).split(Vec, ',');
454 for (StringRef S : Vec) {
455 if (S == "noref") {
456 Config->DoGC = false;
457 Config->DoICF = false;
458 continue;
459 }
460 if (S == "icf" || StringRef(S).startswith("icf=")) {
461 Config->DoICF = true;
462 continue;
463 }
464 if (S == "noicf") {
465 Config->DoICF = false;
466 continue;
467 }
468 if (StringRef(S).startswith("lldlto=")) {
469 StringRef OptLevel = StringRef(S).substr(7);
470 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
471 Config->LTOOptLevel > 3)
Rui Ueyama60604792016-07-14 23:37:14 +0000472 fatal("/opt:lldlto: invalid optimization level: " + OptLevel);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000473 continue;
474 }
475 if (StringRef(S).startswith("lldltojobs=")) {
476 StringRef Jobs = StringRef(S).substr(11);
477 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
Rui Ueyama60604792016-07-14 23:37:14 +0000478 fatal("/opt:lldltojobs: invalid job count: " + Jobs);
Rui Ueyama75656ee2015-10-19 19:40:43 +0000479 continue;
480 }
481 if (S != "ref" && S != "lbr" && S != "nolbr")
Rui Ueyama1a3fd132016-07-14 23:43:36 +0000482 fatal("/opt: unknown option: " + S);
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000483 }
Rui Ueyamae2cbfea2015-06-07 03:17:42 +0000484 }
485
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000486 // Handle /failifmismatch
David Blaikie6521ed92015-06-22 22:06:52 +0000487 for (auto *Arg : Args.filtered(OPT_failifmismatch))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000488 checkFailIfMismatch(Arg->getValue());
Rui Ueyama8854d8a2015-06-04 19:21:24 +0000489
Rui Ueyama6600eb12015-07-04 23:37:32 +0000490 // Handle /merge
491 for (auto *Arg : Args.filtered(OPT_merge))
Rafael Espindolab835ae82015-08-06 14:58:50 +0000492 parseMerge(Arg->getValue());
Rui Ueyama6600eb12015-07-04 23:37:32 +0000493
Rui Ueyama440138c2016-06-20 03:39:39 +0000494 // Handle /section
495 for (auto *Arg : Args.filtered(OPT_section))
496 parseSection(Arg->getValue());
497
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000498 // Handle /manifest
Rafael Espindolab835ae82015-08-06 14:58:50 +0000499 if (auto *Arg = Args.getLastArg(OPT_manifest_colon))
500 parseManifest(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000501
502 // Handle /manifestuac
Rafael Espindolab835ae82015-08-06 14:58:50 +0000503 if (auto *Arg = Args.getLastArg(OPT_manifestuac))
504 parseManifestUAC(Arg->getValue());
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000505
506 // Handle /manifestdependency
David Blaikie6521ed92015-06-22 22:06:52 +0000507 if (auto *Arg = Args.getLastArg(OPT_manifestdependency))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000508 Config->ManifestDependency = Arg->getValue();
509
510 // Handle /manifestfile
David Blaikie6521ed92015-06-22 22:06:52 +0000511 if (auto *Arg = Args.getLastArg(OPT_manifestfile))
Rui Ueyama24c5fd02015-06-18 00:12:42 +0000512 Config->ManifestFile = Arg->getValue();
513
Rui Ueyamaafb19012016-04-19 01:21:58 +0000514 // Handle /manifestinput
515 for (auto *Arg : Args.filtered(OPT_manifestinput))
516 Config->ManifestInput.push_back(Arg->getValue());
517
Rui Ueyama6592ff82015-06-16 23:13:00 +0000518 // Handle miscellaneous boolean flags.
David Blaikie6521ed92015-06-22 22:06:52 +0000519 if (Args.hasArg(OPT_allowbind_no))
520 Config->AllowBind = false;
521 if (Args.hasArg(OPT_allowisolation_no))
522 Config->AllowIsolation = false;
523 if (Args.hasArg(OPT_dynamicbase_no))
524 Config->DynamicBase = false;
David Blaikie6521ed92015-06-22 22:06:52 +0000525 if (Args.hasArg(OPT_nxcompat_no))
526 Config->NxCompat = false;
527 if (Args.hasArg(OPT_tsaware_no))
528 Config->TerminalServerAware = false;
Rui Ueyama96401732015-09-21 23:43:31 +0000529 if (Args.hasArg(OPT_nosymtab))
530 Config->WriteSymtab = false;
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().
574 for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) {
575 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 Ueyama1a3fd132016-07-14 23:43:36 +0000583 fatal(File->getShortName() + ": 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 Ueyamaea533cd2015-07-09 19:54:13 +0000587 llvm::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)
628 llvm::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;
Peter Collingbournebaf5f872015-06-26 19:20:09 +0000768 llvm::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