1//===- Relocations.h --------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_MACHO_RELOCATIONS_H
10#define LLD_MACHO_RELOCATIONS_H
11
12#include "llvm/ADT/BitmaskEnum.h"
13#include "llvm/ADT/PointerUnion.h"
14#include "llvm/BinaryFormat/MachO.h"
15#include "llvm/Support/Endian.h"
16
17#include <cstddef>
18#include <cstdint>
19
20namespace lld::macho {
21LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
22
23class Symbol;
24class InputSection;
25
26enum class RelocAttrBits {
27 _0 = 0, // invalid
28 BYTE1 = 1 << 0, // 1 byte datum
29 BYTE2 = 1 << 1, // 2 byte datum
30 BYTE4 = 1 << 2, // 4 byte datum
31 BYTE8 = 1 << 3, // 8 byte datum
32 PCREL = 1 << 4, // Value is PC-relative offset
33 ABSOLUTE = 1 << 5, // Value is an absolute address or fixed offset
34 EXTERN = 1 << 6, // Can have an external symbol
35 LOCAL = 1 << 7, // Can have a local symbol
36 ADDEND = 1 << 8, // *_ADDEND paired prefix reloc
37 SUBTRAHEND = 1 << 9, // *_SUBTRACTOR paired prefix reloc
38 BRANCH = 1 << 10, // Value is branch target
39 GOT = 1 << 11, // References a symbol in the Global Offset Table
40 TLV = 1 << 12, // References a thread-local symbol
41 LOAD = 1 << 13, // Relaxable indirect load
42 POINTER = 1 << 14, // Non-relaxable indirect load (pointer is taken)
43 UNSIGNED = 1 << 15, // *_UNSIGNED relocs
44 AUTH = 1 << 16, // *_AUTHENTICATED_POINTER relocs
45 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ (1 << 17) - 1),
46};
47// Note: SUBTRACTOR always pairs with UNSIGNED (a delta between two symbols).
48
49struct RelocAttrs {
50 llvm::StringRef name;
51 RelocAttrBits bits;
52 bool hasAttr(RelocAttrBits b) const { return (bits & b) == b; }
53};
54
55// Matches ptrauth_key in <ptrauth.h>.
56enum PtrAuthKey : uint8_t {
57 PtrAuthKeyIA,
58 PtrAuthKeyIB,
59 PtrAuthKeyDA,
60 PtrAuthKeyDB
61};
62
63// The signing schema of an authenticated pointer.
64struct AuthInfo {
65 uint16_t diversity;
66 PtrAuthKey key;
67 bool addrDiv;
68
69 bool operator==(const AuthInfo &other) const {
70 return diversity == other.diversity && key == other.key &&
71 addrDiv == other.addrDiv;
72 }
73 bool operator!=(const AuthInfo &other) const { return !(*this == other); }
74};
75
76struct AuthReloc {
77 int32_t addend;
78 AuthInfo info;
79};
80static_assert(sizeof(AuthReloc) == sizeof(int64_t),
81 "AuthReloc must match int64_t size");
82
83class Relocation {
84public:
85 uint8_t type = llvm::MachO::GENERIC_RELOC_INVALID;
86 bool pcrel = false;
87 uint8_t length = 0;
88 bool hasAuth = false;
89 // The offset from the start of the subsection that this relocation belongs
90 // to.
91 uint32_t offset = 0;
92 llvm::PointerUnion<Symbol *, InputSection *> referent = nullptr;
93
94 Relocation() = default;
95
96 Relocation(uint8_t type, bool pcrel, uint8_t length, uint32_t offset,
97 int64_t addend,
98 llvm::PointerUnion<Symbol *, InputSection *> referent)
99 : type(type), pcrel(pcrel), length(length), offset(offset),
100 referent(referent), addend(addend) {}
101
102 const AuthInfo *getAuthInfo() const {
103 return hasAuth ? &authData.info : nullptr;
104 }
105 void setAuthInfo(AuthInfo info) {
106 assert(!hasAuth);
107 authData = {.addend: static_cast<int32_t>(addend), .info: info};
108 hasAuth = true;
109 }
110 int64_t getAddend() const {
111 return hasAuth ? static_cast<int64_t>(authData.addend) : addend;
112 }
113 void setAddend(int64_t a) {
114 if (hasAuth)
115 authData.addend = static_cast<int32_t>(a);
116 else
117 addend = a;
118 }
119
120 InputSection *getReferentInputSection() const;
121
122 // Must point to an offset within a CStringInputSection or a
123 // ConcatInputSection.
124 llvm::StringRef getReferentString() const;
125
126private:
127 // Adding this offset to the address of the referent symbol or subsection
128 // gives the destination that this relocation refers to.
129 union {
130 int64_t addend = 0;
131 AuthReloc authData; // Valid when hasAuth.
132 };
133};
134
135bool validateSymbolRelocation(const Symbol *, const InputSection *,
136 const Relocation &);
137
138/*
139 * v: The value the relocation is attempting to encode
140 * bits: The number of bits actually available to encode this relocation
141 */
142void reportRangeError(void *loc, const Relocation &, const llvm::Twine &v,
143 uint8_t bits, int64_t min, uint64_t max);
144
145struct SymbolDiagnostic {
146 const Symbol *symbol;
147 llvm::StringRef reason;
148};
149
150void reportRangeError(void *loc, SymbolDiagnostic, const llvm::Twine &v,
151 uint8_t bits, int64_t min, uint64_t max);
152
153template <typename Diagnostic>
154inline void checkInt(void *loc, Diagnostic d, int64_t v, int bits) {
155 if (v != llvm::SignExtend64(X: v, B: bits))
156 reportRangeError(loc, d, llvm::Twine(v), bits, llvm::minIntN(N: bits),
157 llvm::maxIntN(N: bits));
158}
159
160template <typename Diagnostic>
161inline void checkUInt(void *loc, Diagnostic d, uint64_t v, int bits) {
162 if ((v >> bits) != 0)
163 reportRangeError(loc, d, llvm::Twine(v), bits, 0, llvm::maxUIntN(N: bits));
164}
165
166inline void writeAddress(uint8_t *loc, uint64_t addr, uint8_t length) {
167 switch (length) {
168 case 2:
169 llvm::support::endian::write32le(P: loc, V: addr);
170 break;
171 case 3:
172 llvm::support::endian::write64le(P: loc, V: addr);
173 break;
174 default:
175 llvm_unreachable("invalid r_length");
176 }
177}
178
179InputSection *offsetToInputSection(uint64_t *);
180
181extern const RelocAttrs invalidRelocAttrs;
182
183} // namespace lld::Macho
184
185#endif
186