2015-08-14 14:12:54 +00:00
|
|
|
//===- Driver.h -------------------------------------------------*- C++ -*-===//
|
2015-05-28 19:09:30 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-05-28 19:09:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_COFF_DRIVER_H
|
|
|
|
#define LLD_COFF_DRIVER_H
|
|
|
|
|
2015-06-17 00:16:33 +00:00
|
|
|
#include "Config.h"
|
2015-06-23 23:56:39 +00:00
|
|
|
#include "SymbolTable.h"
|
2017-10-02 21:00:41 +00:00
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "lld/Common/Reproduce.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-12-28 07:41:19 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2016-12-15 04:02:23 +00:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-05-28 20:30:06 +00:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2018-06-12 21:47:31 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-01-06 02:33:53 +00:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2022-02-16 09:20:03 -05:00
|
|
|
#include "llvm/WindowsDriver/MSVCPaths.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include <memory>
|
2022-11-27 16:39:40 -08:00
|
|
|
#include <optional>
|
2015-05-31 19:17:09 +00:00
|
|
|
#include <set>
|
2015-05-28 19:09:30 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-08-08 11:32:26 -04:00
|
|
|
namespace lld::coff {
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2015-05-29 16:06:00 +00:00
|
|
|
using llvm::COFF::MachineTypes;
|
2015-05-29 16:34:31 +00:00
|
|
|
using llvm::COFF::WindowsSubsystem;
|
2022-11-27 16:39:40 -08:00
|
|
|
using std::optional;
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2022-12-30 08:32:59 +01:00
|
|
|
class COFFOptTable : public llvm::opt::GenericOptTable {
|
2017-08-28 20:46:30 +00:00
|
|
|
public:
|
|
|
|
COFFOptTable();
|
|
|
|
};
|
|
|
|
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-24 17:26:17 -07:00
|
|
|
// The result of parsing the .drective section. The /export: and /include:
|
|
|
|
// options are handled separately because they reference symbols, and the number
|
|
|
|
// of symbols can be quite large. The LLVM Option library will perform at least
|
|
|
|
// one memory allocation per argument, and that is prohibitively slow for
|
|
|
|
// parsing directives.
|
|
|
|
struct ParsedDirectives {
|
|
|
|
std::vector<StringRef> exports;
|
|
|
|
std::vector<StringRef> includes;
|
2022-07-18 00:11:37 +03:00
|
|
|
std::vector<StringRef> excludes;
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-24 17:26:17 -07:00
|
|
|
llvm::opt::InputArgList args;
|
|
|
|
};
|
|
|
|
|
2015-06-07 02:55:19 +00:00
|
|
|
class ArgParser {
|
|
|
|
public:
|
2023-01-09 23:37:28 -05:00
|
|
|
ArgParser(COFFLinkerContext &ctx);
|
|
|
|
|
2019-09-13 13:13:52 +00:00
|
|
|
// Parses command line options.
|
|
|
|
llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
|
2015-06-28 02:35:31 +00:00
|
|
|
|
2015-06-07 02:55:19 +00:00
|
|
|
// Tokenizes a given string and then parses as command line options.
|
2015-08-06 14:58:50 +00:00
|
|
|
llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
|
2015-06-07 02:55:19 +00:00
|
|
|
|
2017-12-27 06:08:10 +00:00
|
|
|
// Tokenizes a given string and then parses as command line options in
|
2018-01-09 20:36:42 +00:00
|
|
|
// .drectve section. /EXPORT options are returned in second element
|
|
|
|
// to be processed in fastpath.
|
[COFF] Add a fastpath for /INCLUDE: in .drective sections
This speeds up linking chrome.dll with PGO instrumentation by 13%
(154271ms -> 134033ms).
LLVM's Option library is very slow. In particular, it allocates at least
one large-ish heap object (Arg) for every argument. When PGO
instrumentation is enabled, all the __profd_* symbols are added to the
@llvm.used list, which compiles down to these /INCLUDE: directives. This
means we have O(#symbols) directives to parse in the section, so we end
up allocating an Arg for every function symbol in the object file. This
is unnecessary.
To address the issue and speed up the link, extend the fast path that we
already have for /EXPORT:, which has similar scaling issues.
I promise that I took a hard look at optimizing the Option library, but
its data structures are very general and would need a lot of cleanup. We
have accumulated lots of optional features (option groups, aliases,
multiple values) over the years, and these are now properties of every
parsed argument, when the vast majority of arguments do not use these
features.
Reviewed By: thakis
Differential Revision: https://reviews.llvm.org/D78845
2020-04-24 17:26:17 -07:00
|
|
|
ParsedDirectives parseDirectives(StringRef s);
|
2017-12-27 06:08:10 +00:00
|
|
|
|
2015-06-07 02:55:19 +00:00
|
|
|
private:
|
2019-09-13 13:13:52 +00:00
|
|
|
// Concatenate LINK environment variable.
|
|
|
|
void addLINK(SmallVector<const char *, 256> &argv);
|
2017-09-05 23:46:45 +00:00
|
|
|
|
2015-06-07 02:55:19 +00:00
|
|
|
std::vector<const char *> tokenize(StringRef s);
|
2023-01-09 23:37:28 -05:00
|
|
|
|
|
|
|
COFFLinkerContext &ctx;
|
2015-06-07 02:55:19 +00:00
|
|
|
};
|
|
|
|
|
2015-05-31 19:17:09 +00:00
|
|
|
class LinkerDriver {
|
|
|
|
public:
|
2023-01-09 23:37:28 -05:00
|
|
|
LinkerDriver(COFFLinkerContext &ctx) : ctx(ctx) {}
|
2021-09-16 16:48:26 -07:00
|
|
|
|
2020-12-18 12:09:01 +05:30
|
|
|
void linkerMain(llvm::ArrayRef<const char *> args);
|
2015-05-31 19:17:09 +00:00
|
|
|
|
2025-01-01 19:42:49 +01:00
|
|
|
void addFile(InputFile *file);
|
2022-02-16 09:20:03 -05:00
|
|
|
|
2023-07-14 09:46:54 +02:00
|
|
|
void addClangLibSearchPaths(const std::string &argv0);
|
|
|
|
|
2016-12-15 04:02:23 +00:00
|
|
|
// Used by ArchiveFile to enqueue members.
|
2019-07-19 13:29:10 +00:00
|
|
|
void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
|
2016-12-15 04:02:23 +00:00
|
|
|
StringRef parentName);
|
2016-07-26 02:00:42 +00:00
|
|
|
|
2020-05-09 06:58:15 -07:00
|
|
|
void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
|
|
|
|
|
2017-10-20 19:48:26 +00:00
|
|
|
MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
|
|
|
|
|
2019-09-03 20:32:16 +00:00
|
|
|
void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
|
2019-06-03 12:39:47 +00:00
|
|
|
|
2025-01-16 12:55:12 +01:00
|
|
|
// Returns a list of chunks of selected symbols.
|
|
|
|
std::vector<Chunk *> getChunks() const;
|
|
|
|
|
2017-01-06 02:33:53 +00:00
|
|
|
std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
|
2016-12-15 04:02:23 +00:00
|
|
|
|
2024-09-11 14:46:40 +02:00
|
|
|
void pullArm64ECIcallHelper();
|
|
|
|
|
2021-02-22 14:29:55 -05:00
|
|
|
private:
|
2015-05-31 19:17:12 +00:00
|
|
|
// Searches a file from search paths.
|
2023-06-06 11:11:52 -07:00
|
|
|
std::optional<StringRef> findFileIfNew(StringRef filename);
|
|
|
|
std::optional<StringRef> findLibIfNew(StringRef filename);
|
|
|
|
StringRef findFile(StringRef filename);
|
|
|
|
StringRef findLib(StringRef filename);
|
|
|
|
StringRef findLibMinGW(StringRef filename);
|
2015-05-31 19:17:12 +00:00
|
|
|
|
2022-02-16 09:20:03 -05:00
|
|
|
// Determines the location of the sysroot based on `args`, environment, etc.
|
|
|
|
void detectWinSysRoot(const llvm::opt::InputArgList &args);
|
|
|
|
|
2024-12-15 18:41:26 +01:00
|
|
|
// Adds various search paths based on the sysroot. Must only be called once
|
2025-01-06 23:06:33 -08:00
|
|
|
// config.machine has been set.
|
2024-12-15 18:41:26 +01:00
|
|
|
void addWinSysRootLibSearchPaths();
|
|
|
|
|
2025-01-01 19:42:49 +01:00
|
|
|
void setMachine(llvm::COFF::MachineTypes machine);
|
2023-01-09 23:37:28 -05:00
|
|
|
llvm::Triple::ArchType getArch();
|
|
|
|
|
|
|
|
uint64_t getDefaultImageBase();
|
|
|
|
|
|
|
|
bool isDecorated(StringRef sym);
|
|
|
|
|
|
|
|
std::string getMapFile(const llvm::opt::InputArgList &args,
|
|
|
|
llvm::opt::OptSpecifier os,
|
|
|
|
llvm::opt::OptSpecifier osFile);
|
|
|
|
|
|
|
|
std::string getImplibPath();
|
|
|
|
|
|
|
|
// The import name is calculated as follows:
|
|
|
|
//
|
|
|
|
// | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY
|
|
|
|
// -----+----------------+---------------------+------------------
|
|
|
|
// LINK | {value} | {value}.{.dll/.exe} | {output name}
|
|
|
|
// LIB | {value} | {value}.dll | {output name}.dll
|
|
|
|
//
|
|
|
|
std::string getImportName(bool asLib);
|
|
|
|
|
|
|
|
void createImportLibrary(bool asLib);
|
|
|
|
|
2025-01-01 19:42:49 +01:00
|
|
|
// Used by the resolver to parse .drectve section contents.
|
|
|
|
void parseDirectives(InputFile *file);
|
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parse an /order file. If an option is given, the linker places COMDAT
|
|
|
|
// sections int he same order as their names appear in the given file.
|
|
|
|
void parseOrderFile(StringRef arg);
|
|
|
|
|
|
|
|
void parseCallGraphFile(StringRef path);
|
|
|
|
|
|
|
|
void parsePDBAltPath();
|
|
|
|
|
2015-05-31 19:17:12 +00:00
|
|
|
// Parses LIB environment which contains a list of search paths.
|
2015-06-19 22:39:48 +00:00
|
|
|
void addLibSearchPaths();
|
2015-05-31 19:17:12 +00:00
|
|
|
|
2015-06-19 22:39:48 +00:00
|
|
|
// Library search path. The first element is always "" (current directory).
|
2015-05-31 19:17:12 +00:00
|
|
|
std::vector<StringRef> searchPaths;
|
2018-06-12 21:47:31 +00:00
|
|
|
|
2019-08-30 06:56:33 +00:00
|
|
|
// Convert resource files and potentially merge input resource object
|
|
|
|
// trees into one resource tree.
|
|
|
|
void convertResources();
|
|
|
|
|
2019-02-19 22:06:44 +00:00
|
|
|
void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
|
|
|
|
|
2018-06-12 21:47:31 +00:00
|
|
|
// We don't want to add the same file more than once.
|
|
|
|
// Files are uniquified by their filesystem and file number.
|
|
|
|
std::set<llvm::sys::fs::UniqueID> visitedFiles;
|
|
|
|
|
2016-12-16 03:45:59 +00:00
|
|
|
std::set<std::string> visitedLibs;
|
2015-05-31 21:04:56 +00:00
|
|
|
|
2019-09-03 20:32:16 +00:00
|
|
|
void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
|
|
|
|
bool lazy);
|
2016-12-15 04:02:23 +00:00
|
|
|
void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
|
2019-04-15 19:48:32 +00:00
|
|
|
StringRef parentName, uint64_t offsetInArchive);
|
2016-12-15 04:02:23 +00:00
|
|
|
|
|
|
|
void enqueueTask(std::function<void()> task);
|
|
|
|
bool run();
|
|
|
|
|
|
|
|
std::list<std::function<void()>> taskQueue;
|
|
|
|
std::vector<MemoryBufferRef> resources;
|
2017-12-28 07:41:19 +00:00
|
|
|
|
2022-07-18 00:11:37 +03:00
|
|
|
llvm::DenseSet<StringRef> excludedSymbols;
|
2021-09-16 16:48:26 -07:00
|
|
|
|
|
|
|
COFFLinkerContext &ctx;
|
2022-02-16 09:20:03 -05:00
|
|
|
|
|
|
|
llvm::ToolsetLayout vsLayout = llvm::ToolsetLayout::OlderVS;
|
|
|
|
std::string vcToolChainPath;
|
|
|
|
llvm::SmallString<128> diaPath;
|
|
|
|
bool useWinSysRootLibPath = false;
|
|
|
|
llvm::SmallString<128> universalCRTLibPath;
|
|
|
|
int sdkMajor = 0;
|
|
|
|
llvm::SmallString<128> windowsSdkLibPath;
|
2015-05-31 19:17:09 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Functions below this line are defined in DriverUtils.cpp.
|
2015-05-29 16:11:52 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
void printHelp(const char *argv0);
|
2015-05-29 16:18:15 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "<integer>[,<integer>]".
|
|
|
|
void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
|
2018-02-06 01:58:26 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
void parseGuard(StringRef arg);
|
2015-05-29 16:28:29 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "<integer>[.<integer>]".
|
|
|
|
// Minor's default value is 0.
|
|
|
|
void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
|
2015-05-29 16:34:31 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
|
|
|
|
void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
|
|
|
|
uint32_t *minor, bool *gotVersion = nullptr);
|
2015-06-18 19:09:30 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
void parseMerge(StringRef);
|
|
|
|
void parsePDBPageSize(StringRef);
|
|
|
|
void parseSection(StringRef);
|
2019-02-23 01:46:18 +00:00
|
|
|
|
2025-01-21 06:38:59 +09:00
|
|
|
// Parses a MS-DOS stub file
|
|
|
|
void parseDosStub(StringRef path);
|
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "[:<integer>]"
|
|
|
|
void parseFunctionPadMin(llvm::opt::Arg *a);
|
2015-06-18 00:12:42 +00:00
|
|
|
|
2023-11-08 20:21:05 +00:00
|
|
|
// Parses a string in the form of "[:<integer>]"
|
|
|
|
void parseDependentLoadFlags(llvm::opt::Arg *a);
|
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "EMBED[,=<integer>]|NO".
|
|
|
|
void parseManifest(StringRef arg);
|
2015-06-18 00:12:42 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "level=<string>|uiAccess=<string>"
|
|
|
|
void parseManifestUAC(StringRef arg);
|
2019-04-25 14:02:26 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "cd|net[,(cd|net)]*"
|
|
|
|
void parseSwaprun(StringRef arg);
|
2015-06-18 00:12:42 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Create a resource file containing a manifest XML.
|
|
|
|
std::unique_ptr<MemoryBuffer> createManifestRes();
|
|
|
|
void createSideBySideManifest();
|
|
|
|
std::string createDefaultXml();
|
|
|
|
std::string createManifestXmlWithInternalMt(StringRef defaultXml);
|
|
|
|
std::string createManifestXmlWithExternalMt(StringRef defaultXml);
|
|
|
|
std::string createManifestXml();
|
2015-06-17 00:16:33 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
std::unique_ptr<llvm::WritableMemoryBuffer>
|
|
|
|
createMemoryBufferForManifestRes(size_t manifestRes);
|
2015-06-04 19:21:24 +00:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Used for dllexported symbols.
|
|
|
|
Export parseExport(StringRef arg);
|
2022-07-14 15:53:32 -04:00
|
|
|
|
2023-01-09 23:37:28 -05:00
|
|
|
// Parses a string in the form of "key=value" and check
|
|
|
|
// if value matches previous values for the key.
|
|
|
|
// This feature used in the directive section to reject
|
|
|
|
// incompatible objects.
|
|
|
|
void checkFailIfMismatch(StringRef arg, InputFile *source);
|
|
|
|
|
|
|
|
// Convert Windows resource files (.res files) to a .obj file.
|
|
|
|
MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
|
|
|
|
ArrayRef<ObjFile *> objs);
|
2024-08-22 22:03:05 +02:00
|
|
|
|
|
|
|
// Create export thunks for exported and patchable Arm64EC function symbols.
|
|
|
|
void createECExportThunks();
|
|
|
|
void maybeCreateECExportThunk(StringRef name, Symbol *&sym);
|
2025-01-01 19:42:49 +01:00
|
|
|
|
|
|
|
bool ltoCompilationDone = false;
|
2023-01-09 23:37:28 -05:00
|
|
|
};
|
2015-06-14 21:50:50 +00:00
|
|
|
|
2015-05-28 20:30:06 +00:00
|
|
|
// Create enum with OPT_xxx values for each option in Options.td
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
2023-08-04 11:19:09 -07:00
|
|
|
#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
|
2015-05-28 20:30:06 +00:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2022-08-08 11:32:26 -04:00
|
|
|
} // namespace lld::coff
|
2015-05-28 19:09:30 +00:00
|
|
|
|
|
|
|
#endif
|