comparison lld/MachO/Target.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
7 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
8 8
9 #ifndef LLD_MACHO_TARGET_H 9 #ifndef LLD_MACHO_TARGET_H
10 #define LLD_MACHO_TARGET_H 10 #define LLD_MACHO_TARGET_H
11 11
12 #include "MachOStructs.h"
13 #include "Relocations.h"
14
15 #include "llvm/ADT/BitmaskEnum.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Support/MemoryBuffer.h"
18
12 #include <cstddef> 19 #include <cstddef>
13 #include <cstdint> 20 #include <cstdint>
14 21
15 namespace lld { 22 namespace lld {
16 namespace macho { 23 namespace macho {
24 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
17 25
26 class Symbol;
27 class Defined;
18 class DylibSymbol; 28 class DylibSymbol;
19 29 class InputSection;
20 enum {
21 // We are currently only supporting 64-bit targets since macOS and iOS are
22 // deprecating 32-bit apps.
23 WordSize = 8,
24 PageSize = 4096,
25 ImageBase = 4096,
26 MaxAlignmentPowerOf2 = 32,
27 };
28 30
29 class TargetInfo { 31 class TargetInfo {
30 public: 32 public:
33 template <class LP> TargetInfo(LP) {
34 // Having these values available in TargetInfo allows us to access them
35 // without having to resort to templates.
36 magic = LP::magic;
37 pageZeroSize = LP::pageZeroSize;
38 headerSize = sizeof(typename LP::mach_header);
39 wordSize = LP::wordSize;
40 }
41
31 virtual ~TargetInfo() = default; 42 virtual ~TargetInfo() = default;
32 43
33 virtual uint64_t getImplicitAddend(const uint8_t *loc, 44 // Validate the relocation structure and get its addend.
34 uint8_t type) const = 0; 45 virtual int64_t
35 virtual void relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const = 0; 46 getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,
47 const llvm::MachO::relocation_info) const = 0;
48 virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
49 uint64_t relocVA) const = 0;
36 50
37 // Write code for lazy binding. See the comments on StubsSection for more 51 // Write code for lazy binding. See the comments on StubsSection for more
38 // details. 52 // details.
39 virtual void writeStub(uint8_t *buf, const DylibSymbol &) const = 0; 53 virtual void writeStub(uint8_t *buf, const Symbol &) const = 0;
40 virtual void writeStubHelperHeader(uint8_t *buf) const = 0; 54 virtual void writeStubHelperHeader(uint8_t *buf) const = 0;
41 virtual void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &, 55 virtual void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
42 uint64_t entryAddr) const = 0; 56 uint64_t entryAddr) const = 0;
43 57
44 // Dylib symbols are referenced via either the GOT or the stubs section, 58 // Symbols may be referenced via either the GOT or the stubs section,
45 // depending on the relocation type. prepareDylibSymbolRelocation() will set 59 // depending on the relocation type. prepareSymbolRelocation() will set up the
46 // up the GOT/stubs entries, and getDylibSymbolVA() will return the addresses 60 // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
47 // of those entries. 61 // entries. resolveSymbolVA() may also relax the target instructions to save
48 virtual void prepareDylibSymbolRelocation(DylibSymbol &, uint8_t type) = 0; 62 // on a level of address indirection.
49 virtual uint64_t getDylibSymbolVA(const DylibSymbol &, 63 virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;
50 uint8_t type) const = 0;
51 64
52 uint32_t cpuType; 65 virtual const RelocAttrs &getRelocAttrs(uint8_t type) const = 0;
66
67 virtual uint64_t getPageSize() const = 0;
68
69 virtual void populateThunk(InputSection *thunk, Symbol *funcSym) {
70 llvm_unreachable("target does not use thunks");
71 }
72
73 bool hasAttr(uint8_t type, RelocAttrBits bit) const {
74 return getRelocAttrs(type).hasAttr(bit);
75 }
76
77 bool usesThunks() const { return thunkSize > 0; }
78
79 uint32_t magic;
80 llvm::MachO::CPUType cpuType;
53 uint32_t cpuSubtype; 81 uint32_t cpuSubtype;
54 82
83 uint64_t pageZeroSize;
84 size_t headerSize;
55 size_t stubSize; 85 size_t stubSize;
56 size_t stubHelperHeaderSize; 86 size_t stubHelperHeaderSize;
57 size_t stubHelperEntrySize; 87 size_t stubHelperEntrySize;
88 size_t wordSize;
89
90 size_t thunkSize = 0;
91 uint64_t branchRange = 0;
92
93 // We contrive this value as sufficiently far from any valid address that it
94 // will always be out-of-range for any architecture. UINT64_MAX is not a
95 // good choice because it is (a) only 1 away from wrapping to 0, and (b) the
96 // tombstone value for DenseMap<> and caused weird assertions for me.
97 static constexpr uint64_t outOfRangeVA = 0xfull << 60;
58 }; 98 };
59 99
60 TargetInfo *createX86_64TargetInfo(); 100 TargetInfo *createX86_64TargetInfo();
101 TargetInfo *createARM64TargetInfo();
102 TargetInfo *createARM64_32TargetInfo();
103 TargetInfo *createARMTargetInfo(uint32_t cpuSubtype);
104
105 struct LP64 {
106 using mach_header = llvm::MachO::mach_header_64;
107 using nlist = structs::nlist_64;
108 using segment_command = llvm::MachO::segment_command_64;
109 using section = llvm::MachO::section_64;
110 using encryption_info_command = llvm::MachO::encryption_info_command_64;
111
112 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
113 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
114 static constexpr uint32_t encryptionInfoLCType =
115 llvm::MachO::LC_ENCRYPTION_INFO_64;
116
117 static constexpr uint64_t pageZeroSize = 1ull << 32;
118 static constexpr size_t wordSize = 8;
119 };
120
121 struct ILP32 {
122 using mach_header = llvm::MachO::mach_header;
123 using nlist = structs::nlist;
124 using segment_command = llvm::MachO::segment_command;
125 using section = llvm::MachO::section;
126 using encryption_info_command = llvm::MachO::encryption_info_command;
127
128 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
129 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
130 static constexpr uint32_t encryptionInfoLCType =
131 llvm::MachO::LC_ENCRYPTION_INFO;
132
133 static constexpr uint64_t pageZeroSize = 1ull << 12;
134 static constexpr size_t wordSize = 4;
135 };
61 136
62 extern TargetInfo *target; 137 extern TargetInfo *target;
63 138
64 } // namespace macho 139 } // namespace macho
65 } // namespace lld 140 } // namespace lld