Path: blob/main/contrib/llvm-project/lld/MachO/Target.h
34878 views
//===- Target.h -------------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_MACHO_TARGET_H9#define LLD_MACHO_TARGET_H1011#include "MachOStructs.h"12#include "Relocations.h"1314#include "llvm/ADT/BitmaskEnum.h"15#include "llvm/BinaryFormat/MachO.h"16#include "llvm/Support/MathExtras.h"17#include "llvm/Support/MemoryBuffer.h"1819#include <cstddef>20#include <cstdint>2122#include "mach-o/compact_unwind_encoding.h"2324namespace lld::macho {25LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();2627class Symbol;28class Defined;29class DylibSymbol;30class InputSection;31class ObjFile;3233static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_MASK) ==34static_cast<uint32_t>(UNWIND_X86_MODE_MASK) &&35static_cast<uint32_t>(UNWIND_ARM64_MODE_MASK) ==36static_cast<uint32_t>(UNWIND_X86_64_MODE_MASK));3738// Since the mode masks have the same value on all targets, define39// a common one for convenience.40constexpr uint32_t UNWIND_MODE_MASK = UNWIND_X86_64_MODE_MASK;4142class TargetInfo {43public:44template <class LP> TargetInfo(LP) {45// Having these values available in TargetInfo allows us to access them46// without having to resort to templates.47magic = LP::magic;48pageZeroSize = LP::pageZeroSize;49headerSize = sizeof(typename LP::mach_header);50wordSize = LP::wordSize;51p2WordSize = llvm::CTLog2<LP::wordSize>();52}5354virtual ~TargetInfo() = default;5556// Validate the relocation structure and get its addend.57virtual int64_t58getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,59const llvm::MachO::relocation_info) const = 0;60virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,61uint64_t relocVA) const = 0;6263// Write code for lazy binding. See the comments on StubsSection for more64// details.65virtual void writeStub(uint8_t *buf, const Symbol &,66uint64_t pointerVA) const = 0;67virtual void writeStubHelperHeader(uint8_t *buf) const = 0;68virtual void writeStubHelperEntry(uint8_t *buf, const Symbol &,69uint64_t entryAddr) const = 0;7071virtual void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym,72uint64_t stubsAddr, uint64_t &stubOffset,73uint64_t selrefVA,74Symbol *objcMsgSend) const = 0;7576// Symbols may be referenced via either the GOT or the stubs section,77// depending on the relocation type. prepareSymbolRelocation() will set up the78// GOT/stubs entries, and resolveSymbolVA() will return the addresses of those79// entries. resolveSymbolVA() may also relax the target instructions to save80// on a level of address indirection.81virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;8283virtual uint64_t getPageSize() const = 0;8485virtual void populateThunk(InputSection *thunk, Symbol *funcSym) {86llvm_unreachable("target does not use thunks");87}8889const RelocAttrs &getRelocAttrs(uint8_t type) const {90assert(type < relocAttrs.size() && "invalid relocation type");91if (type >= relocAttrs.size())92return invalidRelocAttrs;93return relocAttrs[type];94}9596bool hasAttr(uint8_t type, RelocAttrBits bit) const {97return getRelocAttrs(type).hasAttr(bit);98}99100bool usesThunks() const { return thunkSize > 0; }101102// For now, handleDtraceReloc only implements -no_dtrace_dof, and ensures103// that the linking would not fail even when there are user-provided dtrace104// symbols. However, unlike ld64, lld currently does not emit __dof sections.105virtual void handleDtraceReloc(const Symbol *sym, const Reloc &r,106uint8_t *loc) const {107llvm_unreachable("Unsupported architecture for dtrace symbols");108}109110virtual void applyOptimizationHints(uint8_t *, const ObjFile &) const {};111112uint32_t magic;113llvm::MachO::CPUType cpuType;114uint32_t cpuSubtype;115116uint64_t pageZeroSize;117size_t headerSize;118size_t stubSize;119size_t stubHelperHeaderSize;120size_t stubHelperEntrySize;121size_t objcStubsFastSize;122size_t objcStubsSmallSize;123size_t objcStubsFastAlignment;124size_t objcStubsSmallAlignment;125uint8_t p2WordSize;126size_t wordSize;127128size_t thunkSize = 0;129uint64_t forwardBranchRange = 0;130uint64_t backwardBranchRange = 0;131132uint32_t modeDwarfEncoding;133uint8_t subtractorRelocType;134uint8_t unsignedRelocType;135136llvm::ArrayRef<RelocAttrs> relocAttrs;137138// We contrive this value as sufficiently far from any valid address that it139// will always be out-of-range for any architecture. UINT64_MAX is not a140// good choice because it is (a) only 1 away from wrapping to 0, and (b) the141// tombstone value for DenseMap<> and caused weird assertions for me.142static constexpr uint64_t outOfRangeVA = 0xfull << 60;143};144145TargetInfo *createX86_64TargetInfo();146TargetInfo *createARM64TargetInfo();147TargetInfo *createARM64_32TargetInfo();148149struct LP64 {150using mach_header = llvm::MachO::mach_header_64;151using nlist = structs::nlist_64;152using segment_command = llvm::MachO::segment_command_64;153using section = llvm::MachO::section_64;154using encryption_info_command = llvm::MachO::encryption_info_command_64;155156static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;157static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;158static constexpr uint32_t encryptionInfoLCType =159llvm::MachO::LC_ENCRYPTION_INFO_64;160161static constexpr uint64_t pageZeroSize = 1ull << 32;162static constexpr size_t wordSize = 8;163};164165struct ILP32 {166using mach_header = llvm::MachO::mach_header;167using nlist = structs::nlist;168using segment_command = llvm::MachO::segment_command;169using section = llvm::MachO::section;170using encryption_info_command = llvm::MachO::encryption_info_command;171172static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;173static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;174static constexpr uint32_t encryptionInfoLCType =175llvm::MachO::LC_ENCRYPTION_INFO;176177static constexpr uint64_t pageZeroSize = 1ull << 12;178static constexpr size_t wordSize = 4;179};180181extern TargetInfo *target;182183} // namespace lld::macho184185#endif186187188