annotate lld/MachO/Arch/X86_64.cpp @ 240:ca573705d418

merge
author matac
date Fri, 28 Jul 2023 20:50:09 +0900
parents c4bab56944e8
children 1f2b6ac9f198
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 //===- X86_64.cpp ---------------------------------------------------------===//
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
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
9 #include "InputFiles.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Symbols.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "SyntheticSections.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "Target.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "lld/Common/ErrorHandler.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
15 #include "mach-o/compact_unwind_encoding.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "llvm/BinaryFormat/MachO.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Support/Endian.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 using namespace llvm::MachO;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 using namespace llvm::support::endian;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 using namespace lld::macho;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 namespace {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 struct X86_64 : TargetInfo {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 X86_64();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
29 int64_t getEmbeddedAddend(MemoryBufferRef, uint64_t offset,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
30 const relocation_info) const override;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 uint64_t relocVA) const override;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
34 void writeStub(uint8_t *buf, const Symbol &,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 uint64_t pointerVA) const override;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 void writeStubHelperHeader(uint8_t *buf) const override;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
37 void writeStubHelperEntry(uint8_t *buf, const Symbol &,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 uint64_t entryAddr) const override;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
41 uint64_t stubOffset, uint64_t selrefsVA,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
42 uint64_t selectorIndex, uint64_t gotAddr,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
43 uint64_t msgSendIndex) const override;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
44
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
45 void relaxGotLoad(uint8_t *loc, uint8_t type) const override;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 uint64_t getPageSize() const override { return 4 * 1024; }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
47
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
48 void handleDtraceReloc(const Symbol *sym, const Reloc &r,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
49 uint8_t *loc) const override;
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 } // namespace
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
53 static constexpr std::array<RelocAttrs, 10> relocAttrsArray{{
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 #define B(x) RelocAttrBits::x
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
55 {"UNSIGNED",
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
56 B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
57 {"SIGNED", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
58 {"BRANCH", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
59 {"GOT_LOAD", B(PCREL) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
60 {"GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
61 {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
62 {"SIGNED_1", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
63 {"SIGNED_2", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
64 {"SIGNED_4", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
65 {"TLV", B(PCREL) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
66 #undef B
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
67 }};
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
69 static int pcrelOffset(uint8_t type) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 switch (type) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 case X86_64_RELOC_SIGNED_1:
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
72 return 1;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 case X86_64_RELOC_SIGNED_2:
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
74 return 2;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 case X86_64_RELOC_SIGNED_4:
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 return 4;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 default:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 return 0;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82 int64_t X86_64::getEmbeddedAddend(MemoryBufferRef mb, uint64_t offset,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
83 relocation_info rel) const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
84 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
85 const uint8_t *loc = buf + offset + rel.r_address;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 switch (rel.r_length) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
88 case 2:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
89 return static_cast<int32_t>(read32le(loc)) + pcrelOffset(rel.r_type);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
90 case 3:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
91 return read64le(loc) + pcrelOffset(rel.r_type);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
92 default:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
93 llvm_unreachable("invalid r_length");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
94 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
95 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
96
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
97 void X86_64::relocateOne(uint8_t *loc, const Reloc &r, uint64_t value,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
98 uint64_t relocVA) const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
99 if (r.pcrel) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
100 uint64_t pc = relocVA + 4 + pcrelOffset(r.type);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 value -= pc;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
102 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 switch (r.length) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 case 2:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 if (r.type == X86_64_RELOC_UNSIGNED)
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
107 checkUInt(loc, r, value, 32);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108 else
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
109 checkInt(loc, r, value, 32);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
110 write32le(loc, value);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 break;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 case 3:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
113 write64le(loc, value);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 break;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 default:
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
116 llvm_unreachable("invalid r_length");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 // The following methods emit a number of assembly sequences with RIP-relative
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 // addressing. Note that RIP-relative addressing on X86-64 has the RIP pointing
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 // to the next instruction, not the current instruction, so we always have to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 // account for the current instruction's size when calculating offsets.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 // writeRipRelative helps with that.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 // bufAddr: The virtual address corresponding to buf[0].
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 // bufOff: The offset within buf of the next instruction.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 // destAddr: The destination address that the current instruction references.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
129 static void writeRipRelative(SymbolDiagnostic d, uint8_t *buf, uint64_t bufAddr,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 uint64_t bufOff, uint64_t destAddr) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 uint64_t rip = bufAddr + bufOff;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
132 checkInt(buf, d, destAddr - rip, 32);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 // For the instructions we care about, the RIP-relative address is always
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 // stored in the last 4 bytes of the instruction.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 write32le(buf + bufOff - 4, destAddr - rip);
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
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 static constexpr uint8_t stub[] = {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 0xff, 0x25, 0, 0, 0, 0, // jmpq *__la_symbol_ptr(%rip)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
142 void X86_64::writeStub(uint8_t *buf, const Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
143 uint64_t pointerVA) const {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 memcpy(buf, stub, 2); // just copy the two nonzero bytes
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 uint64_t stubAddr = in.stubs->addr + sym.stubsIndex * sizeof(stub);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
146 writeRipRelative({&sym, "stub"}, buf, stubAddr, sizeof(stub), pointerVA);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 static constexpr uint8_t stubHelperHeader[] = {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 0x4c, 0x8d, 0x1d, 0, 0, 0, 0, // 0x0: leaq ImageLoaderCache(%rip), %r11
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 0x41, 0x53, // 0x7: pushq %r11
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 0xff, 0x25, 0, 0, 0, 0, // 0x9: jmpq *dyld_stub_binder@GOT(%rip)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 0x90, // 0xf: nop
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 void X86_64::writeStubHelperHeader(uint8_t *buf) const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 memcpy(buf, stubHelperHeader, sizeof(stubHelperHeader));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 SymbolDiagnostic d = {nullptr, "stub helper header"};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 writeRipRelative(d, buf, in.stubHelper->addr, 7,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 in.imageLoaderCache->getVA());
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 writeRipRelative(d, buf, in.stubHelper->addr, 0xf,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 in.got->addr +
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 in.stubHelper->stubBinder->gotIndex * LP64::wordSize);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 static constexpr uint8_t stubHelperEntry[] = {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 0x68, 0, 0, 0, 0, // 0x0: pushq <bind offset>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 0xe9, 0, 0, 0, 0, // 0x5: jmp <__stub_helper>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
171 void X86_64::writeStubHelperEntry(uint8_t *buf, const Symbol &sym,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 uint64_t entryAddr) const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 memcpy(buf, stubHelperEntry, sizeof(stubHelperEntry));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 write32le(buf + 1, sym.lazyBindOffset);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 writeRipRelative({&sym, "stub helper"}, buf, entryAddr,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176 sizeof(stubHelperEntry), in.stubHelper->addr);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
179 static constexpr uint8_t objcStubsFastCode[] = {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
180 0x48, 0x8b, 0x35, 0, 0, 0, 0, // 0x0: movq selrefs@selector(%rip), %rsi
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
181 0xff, 0x25, 0, 0, 0, 0, // 0x7: jmpq *_objc_msgSend@GOT(%rip)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
182 };
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
183
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
184 void X86_64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
185 uint64_t stubOffset, uint64_t selrefsVA,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
186 uint64_t selectorIndex, uint64_t gotAddr,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
187 uint64_t msgSendIndex) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
188 memcpy(buf, objcStubsFastCode, sizeof(objcStubsFastCode));
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
189 SymbolDiagnostic d = {sym, sym->getName()};
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
190 uint64_t stubAddr = stubsAddr + stubOffset;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
191 writeRipRelative(d, buf, stubAddr, 7,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
192 selrefsVA + selectorIndex * LP64::wordSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
193 writeRipRelative(d, buf, stubAddr, 0xd,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
194 gotAddr + msgSendIndex * LP64::wordSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
195 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
196
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
197 void X86_64::relaxGotLoad(uint8_t *loc, uint8_t type) const {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
198 // Convert MOVQ to LEAQ
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
199 if (loc[-2] != 0x8b)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
200 error(getRelocAttrs(type).name + " reloc requires MOVQ instruction");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
201 loc[-2] = 0x8d;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204 X86_64::X86_64() : TargetInfo(LP64()) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 cpuType = CPU_TYPE_X86_64;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 cpuSubtype = CPU_SUBTYPE_X86_64_ALL;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
208 modeDwarfEncoding = UNWIND_X86_MODE_DWARF;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
209 subtractorRelocType = X86_64_RELOC_SUBTRACTOR;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
210 unsignedRelocType = X86_64_RELOC_UNSIGNED;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
211
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 stubSize = sizeof(stub);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 stubHelperHeaderSize = sizeof(stubHelperHeader);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 stubHelperEntrySize = sizeof(stubHelperEntry);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
215
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
216 objcStubsFastSize = sizeof(objcStubsFastCode);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
217 objcStubsAlignment = 1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
218
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
219 relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 TargetInfo *macho::createX86_64TargetInfo() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 static X86_64 t;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 return &t;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
226
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
227 void X86_64::handleDtraceReloc(const Symbol *sym, const Reloc &r,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
228 uint8_t *loc) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
229 assert(r.type == X86_64_RELOC_BRANCH);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
230
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
231 if (config->outputType == MH_OBJECT)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
232 return;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
233
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
234 if (sym->getName().startswith("___dtrace_probe")) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
235 // change call site to a NOP
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
236 loc[-1] = 0x90;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
237 write32le(loc, 0x00401F0F);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
238 } else if (sym->getName().startswith("___dtrace_isenabled")) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
239 // change call site to a clear eax
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
240 loc[-1] = 0x33;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
241 write32le(loc, 0x909090C0);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
242 } else {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
243 error("Unrecognized dtrace symbol prefix: " + toString(*sym));
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
244 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
245 }