llvm-project/lld/MachO/Target.h

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

177 lines
6.0 KiB
C
Raw Normal View History

//===- Target.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_TARGET_H
#define LLD_MACHO_TARGET_H
#include "MachOStructs.h"
#include "Relocations.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstddef>
#include <cstdint>
namespace lld::macho {
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
class Symbol;
class Defined;
class DylibSymbol;
class InputSection;
class ConcatInputSection;
class TargetInfo {
public:
template <class LP> TargetInfo(LP) {
// Having these values available in TargetInfo allows us to access them
// without having to resort to templates.
magic = LP::magic;
pageZeroSize = LP::pageZeroSize;
headerSize = sizeof(typename LP::mach_header);
wordSize = LP::wordSize;
p2WordSize = llvm::CTLog2<LP::wordSize>();
}
virtual ~TargetInfo() = default;
// Validate the relocation structure and get its addend.
virtual int64_t
getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,
const llvm::MachO::relocation_info) const = 0;
virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
uint64_t relocVA) const = 0;
// Write code for lazy binding. See the comments on StubsSection for more
// details.
virtual void writeStub(uint8_t *buf, const Symbol &) const = 0;
virtual void writeStubHelperHeader(uint8_t *buf) const = 0;
virtual void writeStubHelperEntry(uint8_t *buf, const Symbol &,
uint64_t entryAddr) const = 0;
[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-17 12:35:18 +08:00
virtual void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym,
uint64_t stubsAddr, uint64_t stubOffset,
uint64_t selrefsVA, uint64_t selectorIndex,
uint64_t gotAddr,
uint64_t msgSendIndex) const = 0;
// Symbols may be referenced via either the GOT or the stubs section,
// depending on the relocation type. prepareSymbolRelocation() will set up the
// GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
// entries. resolveSymbolVA() may also relax the target instructions to save
// on a level of address indirection.
virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;
virtual uint64_t getPageSize() const = 0;
virtual void populateThunk(InputSection *thunk, Symbol *funcSym) {
llvm_unreachable("target does not use thunks");
}
const RelocAttrs &getRelocAttrs(uint8_t type) const {
assert(type < relocAttrs.size() && "invalid relocation type");
if (type >= relocAttrs.size())
return invalidRelocAttrs;
return relocAttrs[type];
}
bool hasAttr(uint8_t type, RelocAttrBits bit) const {
return getRelocAttrs(type).hasAttr(bit);
}
bool usesThunks() const { return thunkSize > 0; }
// For now, handleDtraceReloc only implements -no_dtrace_dof, and ensures
// that the linking would not fail even when there are user-provided dtrace
// symbols. However, unlike ld64, lld currently does not emit __dof sections.
virtual void handleDtraceReloc(const Symbol *sym, const Reloc &r,
uint8_t *loc) const {
llvm_unreachable("Unsupported architecture for dtrace symbols");
}
virtual void applyOptimizationHints(uint8_t *buf, const ConcatInputSection *,
llvm::ArrayRef<uint64_t>) const {};
uint32_t magic;
llvm::MachO::CPUType cpuType;
uint32_t cpuSubtype;
uint64_t pageZeroSize;
size_t headerSize;
size_t stubSize;
size_t stubHelperHeaderSize;
size_t stubHelperEntrySize;
[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-17 12:35:18 +08:00
size_t objcStubsFastSize;
size_t objcStubsAlignment;
uint8_t p2WordSize;
size_t wordSize;
size_t thunkSize = 0;
uint64_t forwardBranchRange = 0;
uint64_t backwardBranchRange = 0;
uint32_t modeDwarfEncoding;
uint8_t subtractorRelocType;
uint8_t unsignedRelocType;
llvm::ArrayRef<RelocAttrs> relocAttrs;
// We contrive this value as sufficiently far from any valid address that it
// will always be out-of-range for any architecture. UINT64_MAX is not a
// good choice because it is (a) only 1 away from wrapping to 0, and (b) the
// tombstone value for DenseMap<> and caused weird assertions for me.
static constexpr uint64_t outOfRangeVA = 0xfull << 60;
};
TargetInfo *createX86_64TargetInfo();
TargetInfo *createARM64TargetInfo();
TargetInfo *createARM64_32TargetInfo();
TargetInfo *createARMTargetInfo(uint32_t cpuSubtype);
struct LP64 {
using mach_header = llvm::MachO::mach_header_64;
using nlist = structs::nlist_64;
using segment_command = llvm::MachO::segment_command_64;
using section = llvm::MachO::section_64;
using encryption_info_command = llvm::MachO::encryption_info_command_64;
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
static constexpr uint32_t encryptionInfoLCType =
llvm::MachO::LC_ENCRYPTION_INFO_64;
static constexpr uint64_t pageZeroSize = 1ull << 32;
static constexpr size_t wordSize = 8;
};
struct ILP32 {
using mach_header = llvm::MachO::mach_header;
using nlist = structs::nlist;
using segment_command = llvm::MachO::segment_command;
using section = llvm::MachO::section;
using encryption_info_command = llvm::MachO::encryption_info_command;
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
static constexpr uint32_t encryptionInfoLCType =
llvm::MachO::LC_ENCRYPTION_INFO;
static constexpr uint64_t pageZeroSize = 1ull << 12;
static constexpr size_t wordSize = 4;
};
extern TargetInfo *target;
} // namespace lld::macho
#endif