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