1 //===- Target.h -------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_MACHO_TARGET_H
10 #define LLD_MACHO_TARGET_H
12 #include "MachOStructs.h"
13 #include "Relocations.h"
15 #include "llvm/ADT/BitmaskEnum.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Support/MathExtras.h"
18 #include "llvm/Support/MemoryBuffer.h"
25 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
34 template <class LP
> TargetInfo(LP
) {
35 // Having these values available in TargetInfo allows us to access them
36 // without having to resort to templates.
38 pageZeroSize
= LP::pageZeroSize
;
39 headerSize
= sizeof(typename
LP::mach_header
);
40 wordSize
= LP::wordSize
;
41 p2WordSize
= llvm::CTLog2
<LP::wordSize
>();
44 virtual ~TargetInfo() = default;
46 // Validate the relocation structure and get its addend.
48 getEmbeddedAddend(llvm::MemoryBufferRef
, uint64_t offset
,
49 const llvm::MachO::relocation_info
) const = 0;
50 virtual void relocateOne(uint8_t *loc
, const Reloc
&, uint64_t va
,
51 uint64_t relocVA
) const = 0;
53 // Write code for lazy binding. See the comments on StubsSection for more
55 virtual void writeStub(uint8_t *buf
, const Symbol
&) const = 0;
56 virtual void writeStubHelperHeader(uint8_t *buf
) const = 0;
57 virtual void writeStubHelperEntry(uint8_t *buf
, const Symbol
&,
58 uint64_t entryAddr
) const = 0;
60 // Symbols may be referenced via either the GOT or the stubs section,
61 // depending on the relocation type. prepareSymbolRelocation() will set up the
62 // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
63 // entries. resolveSymbolVA() may also relax the target instructions to save
64 // on a level of address indirection.
65 virtual void relaxGotLoad(uint8_t *loc
, uint8_t type
) const = 0;
67 virtual const RelocAttrs
&getRelocAttrs(uint8_t type
) const = 0;
69 virtual uint64_t getPageSize() const = 0;
71 virtual void populateThunk(InputSection
*thunk
, Symbol
*funcSym
) {
72 llvm_unreachable("target does not use thunks");
75 bool hasAttr(uint8_t type
, RelocAttrBits bit
) const {
76 return getRelocAttrs(type
).hasAttr(bit
);
79 bool usesThunks() const { return thunkSize
> 0; }
82 llvm::MachO::CPUType cpuType
;
85 uint64_t pageZeroSize
;
88 size_t stubHelperHeaderSize
;
89 size_t stubHelperEntrySize
;
94 uint64_t forwardBranchRange
= 0;
95 uint64_t backwardBranchRange
= 0;
97 uint32_t modeDwarfEncoding
;
98 uint8_t subtractorRelocType
;
99 uint8_t unsignedRelocType
;
101 // We contrive this value as sufficiently far from any valid address that it
102 // will always be out-of-range for any architecture. UINT64_MAX is not a
103 // good choice because it is (a) only 1 away from wrapping to 0, and (b) the
104 // tombstone value for DenseMap<> and caused weird assertions for me.
105 static constexpr uint64_t outOfRangeVA
= 0xfull
<< 60;
108 TargetInfo
*createX86_64TargetInfo();
109 TargetInfo
*createARM64TargetInfo();
110 TargetInfo
*createARM64_32TargetInfo();
111 TargetInfo
*createARMTargetInfo(uint32_t cpuSubtype
);
114 using mach_header
= llvm::MachO::mach_header_64
;
115 using nlist
= structs::nlist_64
;
116 using segment_command
= llvm::MachO::segment_command_64
;
117 using section
= llvm::MachO::section_64
;
118 using encryption_info_command
= llvm::MachO::encryption_info_command_64
;
120 static constexpr uint32_t magic
= llvm::MachO::MH_MAGIC_64
;
121 static constexpr uint32_t segmentLCType
= llvm::MachO::LC_SEGMENT_64
;
122 static constexpr uint32_t encryptionInfoLCType
=
123 llvm::MachO::LC_ENCRYPTION_INFO_64
;
125 static constexpr uint64_t pageZeroSize
= 1ull << 32;
126 static constexpr size_t wordSize
= 8;
130 using mach_header
= llvm::MachO::mach_header
;
131 using nlist
= structs::nlist
;
132 using segment_command
= llvm::MachO::segment_command
;
133 using section
= llvm::MachO::section
;
134 using encryption_info_command
= llvm::MachO::encryption_info_command
;
136 static constexpr uint32_t magic
= llvm::MachO::MH_MAGIC
;
137 static constexpr uint32_t segmentLCType
= llvm::MachO::LC_SEGMENT
;
138 static constexpr uint32_t encryptionInfoLCType
=
139 llvm::MachO::LC_ENCRYPTION_INFO
;
141 static constexpr uint64_t pageZeroSize
= 1ull << 12;
142 static constexpr size_t wordSize
= 4;
145 extern TargetInfo
*target
;