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 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ (1 << 16) - 1),
45};
46// Note: SUBTRACTOR always pairs with UNSIGNED (a delta between two symbols).
47
48struct RelocAttrs {
49 llvm::StringRef name;
50 RelocAttrBits bits;
51 bool hasAttr(RelocAttrBits b) const { return (bits & b) == b; }
52};
53
54struct Relocation {
55 uint8_t type = llvm::MachO::GENERIC_RELOC_INVALID;
56 bool pcrel = false;
57 uint8_t length = 0;
58 // The offset from the start of the subsection that this relocation belongs
59 // to.
60 uint32_t offset = 0;
61 // Adding this offset to the address of the referent symbol or subsection
62 // gives the destination that this relocation refers to.
63 int64_t addend = 0;
64 llvm::PointerUnion<Symbol *, InputSection *> referent = nullptr;
65
66 Relocation() = default;
67
68 Relocation(uint8_t type, bool pcrel, uint8_t length, uint32_t offset,
69 int64_t addend,
70 llvm::PointerUnion<Symbol *, InputSection *> referent)
71 : type(type), pcrel(pcrel), length(length), offset(offset),
72 addend(addend), referent(referent) {}
73
74 InputSection *getReferentInputSection() const;
75
76 // Must point to an offset within a CStringInputSection or a
77 // ConcatInputSection.
78 llvm::StringRef getReferentString() const;
79};
80
81bool validateSymbolRelocation(const Symbol *, const InputSection *,
82 const Relocation &);
83
84/*
85 * v: The value the relocation is attempting to encode
86 * bits: The number of bits actually available to encode this relocation
87 */
88void reportRangeError(void *loc, const Relocation &, const llvm::Twine &v,
89 uint8_t bits, int64_t min, uint64_t max);
90
91struct SymbolDiagnostic {
92 const Symbol *symbol;
93 llvm::StringRef reason;
94};
95
96void reportRangeError(void *loc, SymbolDiagnostic, const llvm::Twine &v,
97 uint8_t bits, int64_t min, uint64_t max);
98
99template <typename Diagnostic>
100inline void checkInt(void *loc, Diagnostic d, int64_t v, int bits) {
101 if (v != llvm::SignExtend64(X: v, B: bits))
102 reportRangeError(loc, d, llvm::Twine(v), bits, llvm::minIntN(N: bits),
103 llvm::maxIntN(N: bits));
104}
105
106template <typename Diagnostic>
107inline void checkUInt(void *loc, Diagnostic d, uint64_t v, int bits) {
108 if ((v >> bits) != 0)
109 reportRangeError(loc, d, llvm::Twine(v), bits, 0, llvm::maxUIntN(N: bits));
110}
111
112inline void writeAddress(uint8_t *loc, uint64_t addr, uint8_t length) {
113 switch (length) {
114 case 2:
115 llvm::support::endian::write32le(P: loc, V: addr);
116 break;
117 case 3:
118 llvm::support::endian::write64le(P: loc, V: addr);
119 break;
120 default:
121 llvm_unreachable("invalid r_length");
122 }
123}
124
125InputSection *offsetToInputSection(uint64_t *);
126
127extern const RelocAttrs invalidRelocAttrs;
128
129} // namespace lld::Macho
130
131#endif
132