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

270 lines
7.9 KiB
C
Raw Permalink Normal View History

//===- Config.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_MACHO_CONFIG_H
#define LLD_MACHO_CONFIG_H
#include "llvm/ADT/CachedHashString.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Support/CachePruning.h"
#include "llvm/Support/GlobPattern.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/TextAPI/Architecture.h"
#include "llvm/TextAPI/Platform.h"
#include "llvm/TextAPI/Target.h"
#include <vector>
namespace llvm {
enum class CodeGenOptLevel;
} // namespace llvm
namespace lld {
namespace macho {
class InputSection;
class Symbol;
using NamePair = std::pair<llvm::StringRef, llvm::StringRef>;
using SectionRenameMap = llvm::DenseMap<NamePair, NamePair>;
using SegmentRenameMap = llvm::DenseMap<llvm::StringRef, llvm::StringRef>;
struct PlatformInfo {
llvm::MachO::Target target;
llvm::VersionTuple sdk;
};
inline uint32_t encodeVersion(const llvm::VersionTuple &version) {
return ((version.getMajor() << 020) |
(version.getMinor().value_or(0) << 010) |
version.getSubminor().value_or(0));
}
enum class NamespaceKind {
twolevel,
flat,
};
enum class UndefinedSymbolTreatment {
unknown,
error,
warning,
suppress,
dynamic_lookup,
};
enum class ICFLevel {
unknown,
none,
safe,
safe_thunks,
all,
};
[lld-macho] Add support for objc_msgSend stubs Apple Clang in Xcode 14 introduced a new feature for reducing the overhead of objc_msgSend calls by deduplicating the setup calls for each individual selector. This works by clang adding undefined symbols for each selector called in a translation unit, such as `_objc_msgSend$foo` for calling the `foo` method on any `NSObject`. There are 2 different modes for this behavior, the default directly does the setup for `_objc_msgSend` and calls it, and the smaller option does the selector setup, and then calls the standard `_objc_msgSend` stub function. The general overview of how this works is: - Undefined symbols with the given prefix are collected - The suffix of each matching undefined symbol is added as a string to `__objc_methname` - A pointer is added for every method name in the `__objc_selrefs` section - A `got` entry is emitted for `_objc_msgSend` - Stubs are emitting pointing to the synthesized locations Notes: - Both `__objc_methname` and `__objc_selrefs` can also exist from object files, so their contents are merged with our synthesized contents - The compiler emits method names for defined methods, but not for undefined symbols you call, but stubs are used for both - This only implements the default "fast" mode currently just to reduce the diff, I also doubt many folks will care to swap modes - This only implements this for arm64 and x86_64, we don't need to implement this for 32 bit iOS archs, but we should implement it for watchOS archs in a later diff Differential Revision: https://reviews.llvm.org/D128108
2022-06-16 21:35:18 -07:00
enum class ObjCStubsMode {
fast,
small,
};
struct SectionAlign {
llvm::StringRef segName;
llvm::StringRef sectName;
uint32_t align;
};
struct SegmentProtection {
llvm::StringRef name;
uint32_t maxProt;
uint32_t initProt;
};
class SymbolPatterns {
public:
// GlobPattern can also match literals,
// but we prefer the O(1) lookup of DenseSet.
llvm::SetVector<llvm::CachedHashStringRef> literals;
std::vector<llvm::GlobPattern> globs;
bool empty() const { return literals.empty() && globs.empty(); }
void clear();
void insert(llvm::StringRef symbolName);
bool matchLiteral(llvm::StringRef symbolName) const;
bool matchGlob(llvm::StringRef symbolName) const;
bool match(llvm::StringRef symbolName) const;
};
enum class SymtabPresence {
All,
None,
SelectivelyIncluded,
SelectivelyExcluded,
};
struct Configuration {
Symbol *entry = nullptr;
bool hasReexports = false;
bool allLoad = false;
bool applicationExtension = false;
bool archMultiple = false;
bool exportDynamic = false;
bool forceLoadObjC = false;
bool forceLoadSwift = false; // Only applies to LC_LINKER_OPTIONs.
bool staticLink = false;
bool implicitDylibs = false;
bool isPic = false;
bool headerPadMaxInstallNames = false;
bool markDeadStrippableDylib = false;
bool printDylibSearch = false;
bool printEachFile = false;
bool printWhyLoad = false;
bool searchDylibsFirst = false;
bool saveTemps = false;
bool adhocCodesign = false;
bool emitFunctionStarts = false;
bool emitDataInCodeInfo = false;
bool emitEncryptionInfo = false;
bool emitInitOffsets = false;
bool emitChainedFixups = false;
bool emitRelativeMethodLists = false;
bool thinLTOEmitImportsFiles;
bool thinLTOEmitIndexFiles;
bool thinLTOIndexOnly;
bool timeTraceEnabled = false;
bool dataConst = false;
bool dedupStrings = true;
bool dedupSymbolStrings = true;
bool deadStripDuplicates = false;
bool omitDebugInfo = false;
bool warnDylibInstallName = false;
bool ignoreOptimizationHints = false;
bool forceExactCpuSubtypeMatch = false;
uint32_t headerPad;
uint32_t dylibCompatibilityVersion = 0;
uint32_t dylibCurrentVersion = 0;
uint32_t timeTraceGranularity = 500;
unsigned optimize;
std::string progName;
// For `clang -arch arm64 -arch x86_64`, clang will:
// 1. invoke the linker twice, to write one temporary output per arch
// 2. invoke `lipo` to merge the two outputs into a single file
// `outputFile` is the name of the temporary file the linker writes to.
// `finalOutput `is the name of the file lipo writes to after the link.
llvm::StringRef outputFile;
llvm::StringRef finalOutput;
llvm::StringRef installName;
llvm::StringRef clientName;
llvm::StringRef mapFile;
llvm::StringRef ltoNewPmPasses;
llvm::StringRef ltoObjPath;
llvm::StringRef thinLTOJobs;
llvm::StringRef umbrella;
uint32_t ltoo = 2;
llvm::CodeGenOptLevel ltoCgo;
llvm::CachePruningPolicy thinLTOCachePolicy;
llvm::StringRef thinLTOCacheDir;
llvm::StringRef thinLTOIndexOnlyArg;
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
[lld] Support separate native object file path in --thinlto-prefix-replace Currently, the --thinlto-prefix-replace="oldpath;newpath" option is used during distributed ThinLTO thin links to specify the mapping of the input bitcode object files' directory tree (oldpath) to the directory tree (newpath) used for both: 1) the output files of the thin link itself (the .thinlto.bc index files and the optional .imports files) 2) the specified object file paths written to the response file given in the --thinlto-index-only=${response} option, which is used by the final native link and must match the paths of the native object files that will be produced by ThinLTO backend compiles. This patch expands the --thinlto-prefix-replace option to allow a separate directory tree mapping to be specified for the object file paths written to the response file (number 2 above). This is important to support builds and build systems where the same output directory may not be written by multiple build actions (e.g. the thin link and the ThinLTO backend compiles). The new format is: --thinlto-prefix-replace="origpath;outpath[;objpath]" This replaces the origpath directory tree of the thin link input files with outpath when writing the thin link index and imports outputs (number 1 above). If objpath is specified it replaces origpath of the input files with objpath when writing the response file (number 2 above), otherwise it falls back to the old behavior of using outpath for this as well. Reviewed By: tejohnson, MaskRay Differential Revision: https://reviews.llvm.org/D144596
2023-04-04 09:57:53 -07:00
llvm::StringRef thinLTOPrefixReplaceOld;
llvm::StringRef thinLTOPrefixReplaceNew;
llvm::StringRef thinLTOPrefixReplaceNativeObject;
bool deadStripDylibs = false;
bool demangle = false;
[lld/mac] Implement -dead_strip Also adds support for live_support sections, no_dead_strip sections, .no_dead_strip symbols. Chromium Framework 345MB unstripped -> 250MB stripped (vs 290MB unstripped -> 236M stripped with ld64). Doing dead stripping is a bit faster than not, because so much less data needs to be processed: % ministat lld_* x lld_nostrip.txt + lld_strip.txt N Min Max Median Avg Stddev x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794 + 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651 Difference at 95.0% confidence -0.144711 +/- 0.0336749 -3.60967% +/- 0.839989% (Student's t, pooled s = 0.0358398) This interacts with many parts of the linker. I tried to add test coverage for all added `isLive()` checks, so that some test will fail if any of them is removed. I checked that the test expectations for the most part match ld64's behavior (except for live-support-iterations.s, see the comment in the test). Interacts with: - debug info - export tries - import opcodes - flags like -exported_symbol(s_list) - -U / dynamic_lookup - mod_init_funcs, mod_term_funcs - weak symbol handling - unwind info - stubs - map files - -sectcreate - undefined, dylib, common, defined (both absolute and normal) symbols It's possible it interacts with more features I didn't think of, of course. I also did some manual testing: - check-llvm check-clang check-lld work with lld with this patch as host linker and -dead_strip enabled - Chromium still starts - Chromium's base_unittests still pass, including unwind tests Implemenation-wise, this is InputSection-based, so it'll work for object files with .subsections_via_symbols (which includes all object files generated by clang). I first based this on the COFF implementation, but later realized that things are more similar to ELF. I think it'd be good to refactor MarkLive.cpp to look more like the ELF part at some point, but I'd like to get a working state checked in first. Mechanical parts: - Rename canOmitFromOutput to wasCoalesced (no behavior change) since it really is for weak coalesced symbols - Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP (`.no_dead_strip` in asm) Fixes PR49276. Differential Revision: https://reviews.llvm.org/D103324
2021-05-07 17:10:05 -04:00
bool deadStrip = false;
bool interposable = false;
bool errorForArchMismatch = false;
bool ignoreAutoLink = false;
// ld64 allows invalid auto link options as long as the link succeeds. LLD
// does not, but there are cases in the wild where the invalid linker options
// exist. This allows users to ignore the specific invalid options in the case
// they can't easily fix them.
llvm::StringSet<> ignoreAutoLinkOptions;
bool strictAutoLink = false;
PlatformInfo platformInfo;
std::optional<PlatformInfo> secondaryPlatformInfo;
NamespaceKind namespaceKind = NamespaceKind::twolevel;
UndefinedSymbolTreatment undefinedSymbolTreatment =
UndefinedSymbolTreatment::error;
ICFLevel icfLevel = ICFLevel::none;
bool keepICFStabs = false;
[lld-macho] Add support for objc_msgSend stubs Apple Clang in Xcode 14 introduced a new feature for reducing the overhead of objc_msgSend calls by deduplicating the setup calls for each individual selector. This works by clang adding undefined symbols for each selector called in a translation unit, such as `_objc_msgSend$foo` for calling the `foo` method on any `NSObject`. There are 2 different modes for this behavior, the default directly does the setup for `_objc_msgSend` and calls it, and the smaller option does the selector setup, and then calls the standard `_objc_msgSend` stub function. The general overview of how this works is: - Undefined symbols with the given prefix are collected - The suffix of each matching undefined symbol is added as a string to `__objc_methname` - A pointer is added for every method name in the `__objc_selrefs` section - A `got` entry is emitted for `_objc_msgSend` - Stubs are emitting pointing to the synthesized locations Notes: - Both `__objc_methname` and `__objc_selrefs` can also exist from object files, so their contents are merged with our synthesized contents - The compiler emits method names for defined methods, but not for undefined symbols you call, but stubs are used for both - This only implements the default "fast" mode currently just to reduce the diff, I also doubt many folks will care to swap modes - This only implements this for arm64 and x86_64, we don't need to implement this for 32 bit iOS archs, but we should implement it for watchOS archs in a later diff Differential Revision: https://reviews.llvm.org/D128108
2022-06-16 21:35:18 -07:00
ObjCStubsMode objcStubsMode = ObjCStubsMode::fast;
llvm::MachO::HeaderFileType outputType;
std::vector<llvm::StringRef> systemLibraryRoots;
std::vector<llvm::StringRef> librarySearchPaths;
std::vector<llvm::StringRef> frameworkSearchPaths;
bool warnDuplicateRpath = true;
llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
llvm::SmallVector<llvm::StringRef, 0> allowableClients;
std::vector<std::string> astPaths;
std::vector<Symbol *> explicitUndefineds;
llvm::StringSet<> explicitDynamicLookups;
// There are typically few custom sectionAlignments or segmentProtections,
// so use a vector instead of a map.
std::vector<SectionAlign> sectionAlignments;
std::vector<SegmentProtection> segmentProtections;
bool ltoDebugPassManager = false;
llvm::StringRef codegenDataGeneratePath;
bool csProfileGenerate = false;
llvm::StringRef csProfilePath;
bool pgoWarnMismatch;
bool warnThinArchiveMissingMembers;
bool disableVerify;
bool callGraphProfileSort = false;
llvm::StringRef printSymbolOrder;
llvm::StringRef irpgoProfilePath;
bool bpStartupFunctionSort = false;
bool bpCompressionSortStartupFunctions = false;
bool bpFunctionOrderForCompression = false;
bool bpDataOrderForCompression = false;
bool bpVerboseSectionOrderer = false;
SectionRenameMap sectionRenameMap;
SegmentRenameMap segmentRenameMap;
bool hasExplicitExports = false;
SymbolPatterns exportedSymbols;
SymbolPatterns unexportedSymbols;
SymbolPatterns whyLive;
std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
SymtabPresence localSymbolsPresence = SymtabPresence::All;
SymbolPatterns localSymbolPatterns;
llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
llvm::SmallVector<llvm::StringRef, 0> passPlugins;
bool zeroModTime = true;
bool generateUuid = true;
llvm::StringRef osoPrefix;
std::vector<llvm::StringRef> dyldEnvs;
llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
llvm::MachO::PlatformType platform() const {
return platformInfo.target.Platform;
}
};
extern std::unique_ptr<Configuration> config;
} // namespace macho
} // namespace lld
#endif