llvm-project/lld/COFF/Driver.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

276 lines
8.8 KiB
C
Raw Permalink Normal View History

//===- Driver.h -------------------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLD_COFF_DRIVER_H
#define LLD_COFF_DRIVER_H
#include "Config.h"
#include "SymbolTable.h"
#include "lld/Common/LLVM.h"
#include "lld/Common/Reproduce.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/COFF.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TarWriter.h"
#include "llvm/WindowsDriver/MSVCPaths.h"
#include <memory>
#include <optional>
#include <set>
#include <vector>
namespace lld::coff {
using llvm::COFF::MachineTypes;
using llvm::COFF::WindowsSubsystem;
using std::optional;
class COFFOptTable : public llvm::opt::GenericOptTable {
public:
COFFOptTable();
};
// 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;
std::vector<StringRef> excludes;
llvm::opt::InputArgList args;
};
class ArgParser {
public:
ArgParser(COFFLinkerContext &ctx);
// Parses command line options.
llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
// Tokenizes a given string and then parses as command line options.
llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
// Tokenizes a given string and then parses as command line options in
// .drectve section. /EXPORT options are returned in second element
// to be processed in fastpath.
ParsedDirectives parseDirectives(StringRef s);
private:
// Concatenate LINK environment variable.
void addLINK(SmallVector<const char *, 256> &argv);
std::vector<const char *> tokenize(StringRef s);
COFFLinkerContext &ctx;
};
class LinkerDriver {
public:
LinkerDriver(COFFLinkerContext &ctx) : ctx(ctx) {}
void linkerMain(llvm::ArrayRef<const char *> args);
void addFile(InputFile *file);
void addClangLibSearchPaths(const std::string &argv0);
// Used by ArchiveFile to enqueue members.
void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
StringRef parentName);
void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
// Returns a list of chunks of selected symbols.
std::vector<Chunk *> getChunks() const;
std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
void pullArm64ECIcallHelper();
private:
// Searches a file from search paths.
std::optional<StringRef> findFileIfNew(StringRef filename);
std::optional<StringRef> findLibIfNew(StringRef filename);
StringRef findFile(StringRef filename);
StringRef findLib(StringRef filename);
StringRef findLibMinGW(StringRef filename);
// Determines the location of the sysroot based on `args`, environment, etc.
void detectWinSysRoot(const llvm::opt::InputArgList &args);
// Adds various search paths based on the sysroot. Must only be called once
// config.machine has been set.
void addWinSysRootLibSearchPaths();
void setMachine(llvm::COFF::MachineTypes machine);
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);
// Used by the resolver to parse .drectve section contents.
void parseDirectives(InputFile *file);
// 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();
// Parses LIB environment which contains a list of search paths.
void addLibSearchPaths();
// Library search path. The first element is always "" (current directory).
std::vector<StringRef> searchPaths;
// Convert resource files and potentially merge input resource object
// trees into one resource tree.
void convertResources();
void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
// 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;
std::set<std::string> visitedLibs;
void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
bool lazy);
void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
StringRef parentName, uint64_t offsetInArchive);
void enqueueTask(std::function<void()> task);
bool run();
std::list<std::function<void()>> taskQueue;
std::vector<MemoryBufferRef> resources;
llvm::DenseSet<StringRef> excludedSymbols;
COFFLinkerContext &ctx;
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;
// Functions below this line are defined in DriverUtils.cpp.
void printHelp(const char *argv0);
// Parses a string in the form of "<integer>[,<integer>]".
void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
void parseGuard(StringRef arg);
// 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);
// 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);
void parseMerge(StringRef);
void parsePDBPageSize(StringRef);
void parseSection(StringRef);
// Parses a MS-DOS stub file
void parseDosStub(StringRef path);
// Parses a string in the form of "[:<integer>]"
void parseFunctionPadMin(llvm::opt::Arg *a);
// Parses a string in the form of "[:<integer>]"
void parseDependentLoadFlags(llvm::opt::Arg *a);
// Parses a string in the form of "EMBED[,=<integer>]|NO".
void parseManifest(StringRef arg);
// Parses a string in the form of "level=<string>|uiAccess=<string>"
void parseManifestUAC(StringRef arg);
// Parses a string in the form of "cd|net[,(cd|net)]*"
void parseSwaprun(StringRef arg);
// 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();
std::unique_ptr<llvm::WritableMemoryBuffer>
createMemoryBufferForManifestRes(size_t manifestRes);
// Used for dllexported symbols.
Export parseExport(StringRef arg);
// 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);
[LLD][COFF] Generate X64 thunks for ARM64EC entry points and patchable functions. (#105499) This implements Fast-Forward Sequences documented in ARM64EC ABI https://learn.microsoft.com/en-us/windows/arm/arm64ec-abi. There are two conditions when linker should generate such thunks: - For each exported ARM64EC functions. It applies only to ARM64EC functions (we may also have pure x64 functions, for which no thunk is needed). MSVC linker creates `EXP+<mangled export name>` symbol in those cases that points to the thunk and uses that symbol for the export. It's observable from the module: it's possible to reference such symbols as I did in the test. Note that it uses export name, not name of the symbol that's exported (as in `foo` in `/EXPORT:foo=bar`). This implies that if the same function is exported multiple times, it will have multiple thunks. I followed this MSVC behavior. - For hybrid_patchable functions. The linker tries to generate a thunk for each undefined `EXP+*` symbol (and such symbols are created by the compiler as a target of weak alias from the demangled name). MSVC linker tries to find corresponding `*$hp_target` symbol and if fails to do so, it outputs a cryptic error like `LINK : fatal error LNK1000: Internal error during IMAGE::BuildImage`. I just skip generating the thunk in such case (which causes undefined reference error). MSVC linker additionally checks that the symbol complex type is a function (see also #102898). We generally don't do such checks in LLD, so I made it less strict. It should be fine: if it's some data symbol, it will not have `$hp_target` symbol, so we will skip it anyway.
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);
bool ltoCompilationDone = false;
};
// Create enum with OPT_xxx values for each option in Options.td
enum {
OPT_INVALID = 0,
#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
#include "Options.inc"
#undef OPTION
};
} // namespace lld::coff
#endif