annotate lld/MachO/Relocations.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- Relocations.h --------------------------------------------*- C++ -*-===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #ifndef LLD_MACHO_RELOCATIONS_H
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #define LLD_MACHO_RELOCATIONS_H
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "llvm/ADT/BitmaskEnum.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "llvm/ADT/PointerUnion.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "llvm/BinaryFormat/MachO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "llvm/Support/Endian.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include <cstddef>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include <cstdint>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 namespace lld {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 namespace macho {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 class Symbol;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 class InputSection;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 enum class RelocAttrBits {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 _0 = 0, // invalid
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 PCREL = 1 << 0, // Value is PC-relative offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 ABSOLUTE = 1 << 1, // Value is an absolute address or fixed offset
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 BYTE4 = 1 << 2, // 4 byte datum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 BYTE8 = 1 << 3, // 8 byte datum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 EXTERN = 1 << 4, // Can have an external symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 LOCAL = 1 << 5, // Can have a local symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 ADDEND = 1 << 6, // *_ADDEND paired prefix reloc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 SUBTRAHEND = 1 << 7, // *_SUBTRACTOR paired prefix reloc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 BRANCH = 1 << 8, // Value is branch target
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 GOT = 1 << 9, // References a symbol in the Global Offset Table
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 TLV = 1 << 10, // References a thread-local symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 LOAD = 1 << 11, // Relaxable indirect load
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 POINTER = 1 << 12, // Non-relaxable indirect load (pointer is taken)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 UNSIGNED = 1 << 13, // *_UNSIGNED relocs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ (1 << 14) - 1),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 // Note: SUBTRACTOR always pairs with UNSIGNED (a delta between two symbols).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 struct RelocAttrs {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 llvm::StringRef name;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 RelocAttrBits bits;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 bool hasAttr(RelocAttrBits b) const { return (bits & b) == b; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 struct Reloc {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 uint8_t type = llvm::MachO::GENERIC_RELOC_INVALID;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 bool pcrel = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 uint8_t length = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 // The offset from the start of the subsection that this relocation belongs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 // to.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 uint64_t offset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 // Adding this offset to the address of the referent symbol or subsection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 // gives the destination that this relocation refers to.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 int64_t addend = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 llvm::PointerUnion<Symbol *, InputSection *> referent = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 bool validateSymbolRelocation(const Symbol *, const InputSection *,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 const Reloc &);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 /*
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 * v: The value the relocation is attempting to encode
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 * bits: The number of bits actually available to encode this relocation
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 */
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 void reportRangeError(const Reloc &, const llvm::Twine &v, uint8_t bits,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 int64_t min, uint64_t max);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 struct SymbolDiagnostic {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 const Symbol *symbol;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 llvm::StringRef reason;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 void reportRangeError(SymbolDiagnostic, const llvm::Twine &v, uint8_t bits,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 int64_t min, uint64_t max);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 template <typename Diagnostic>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 inline void checkInt(Diagnostic d, int64_t v, int bits) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 if (v != llvm::SignExtend64(v, bits))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 reportRangeError(d, llvm::Twine(v), bits, llvm::minIntN(bits),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 llvm::maxIntN(bits));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 template <typename Diagnostic>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 inline void checkUInt(Diagnostic d, uint64_t v, int bits) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 if ((v >> bits) != 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 reportRangeError(d, llvm::Twine(v), bits, 0, llvm::maxUIntN(bits));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 inline void writeAddress(uint8_t *loc, uint64_t addr, uint8_t length) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 switch (length) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 case 2:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 llvm::support::endian::write32le(loc, addr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 case 3:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 llvm::support::endian::write64le(loc, addr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 default:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 llvm_unreachable("invalid r_length");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 extern const RelocAttrs invalidRelocAttrs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 } // namespace macho
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 } // namespace lld
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 #endif