1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
//===- lib/FileFormat/MachO/ReferenceKinds.cpp ----------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ReferenceKinds.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm::MachO;
namespace lld {
namespace mach_o {
//===----------------------------------------------------------------------===//
// KindHandler
//===----------------------------------------------------------------------===//
KindHandler::KindHandler() {
}
KindHandler::~KindHandler() {
}
std::unique_ptr<mach_o::KindHandler>
KindHandler::create(MachOLinkingContext::Arch arch) {
switch (arch) {
case MachOLinkingContext::arch_x86_64:
return std::unique_ptr<mach_o::KindHandler>(new KindHandler_x86_64());
case MachOLinkingContext::arch_x86:
return std::unique_ptr<mach_o::KindHandler>(new KindHandler_x86());
case MachOLinkingContext::arch_armv6:
case MachOLinkingContext::arch_armv7:
case MachOLinkingContext::arch_armv7s:
return std::unique_ptr<mach_o::KindHandler>(new KindHandler_arm());
default:
llvm_unreachable("Unknown arch");
}
}
//===----------------------------------------------------------------------===//
// KindHandler_x86_64
//===----------------------------------------------------------------------===//
KindHandler_x86_64::~KindHandler_x86_64() {
}
const Registry::KindStrings KindHandler_x86_64::kindStrings[] = {
LLD_KIND_STRING_ENTRY(X86_64_RELOC_UNSIGNED),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_BRANCH),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_SIGNED),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_SIGNED_1),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_SIGNED_2),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_SIGNED_4),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_GOT_LOAD),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_GOT),
LLD_KIND_STRING_ENTRY(X86_64_RELOC_TLV),
LLD_KIND_STRING_ENTRY(LLD_X86_64_RELOC_GOT_LOAD_NOW_LEA),
LLD_KIND_STRING_ENTRY(LLD_X86_64_RELOC_TLV_NOW_LEA),
LLD_KIND_STRING_ENTRY(LLD_X86_64_RELOC_LAZY_TARGET),
LLD_KIND_STRING_ENTRY(LLD_X86_64_RELOC_LAZY_IMMEDIATE),
LLD_KIND_STRING_END
};
bool KindHandler_x86_64::isCallSite(const Reference &ref) {
return (ref.kindValue() == X86_64_RELOC_BRANCH);
}
bool KindHandler_x86_64::isPointer(const Reference &ref) {
return (ref.kindValue() == X86_64_RELOC_UNSIGNED);
}
bool KindHandler_x86_64::isLazyImmediate(const Reference &ref) {
return (ref.kindValue() == LLD_X86_64_RELOC_LAZY_IMMEDIATE);
}
bool KindHandler_x86_64::isLazyTarget(const Reference &ref) {
return (ref.kindValue() == LLD_X86_64_RELOC_LAZY_TARGET);
}
void KindHandler_x86_64::applyFixup(Reference::KindNamespace ns,
Reference::KindArch arch,
Reference::KindValue kindValue,
uint64_t addend,
uint8_t *location, uint64_t fixupAddress,
uint64_t targetAddress) {
if (ns != Reference::KindNamespace::mach_o)
return;
assert(arch == Reference::KindArch::x86_64);
int32_t *loc32 = reinterpret_cast<int32_t*>(location);
uint64_t* loc64 = reinterpret_cast<uint64_t*>(location);
switch ( kindValue ) {
case X86_64_RELOC_BRANCH:
case X86_64_RELOC_SIGNED:
case X86_64_RELOC_GOT_LOAD:
case X86_64_RELOC_GOT:
case X86_64_RELOC_TLV:
*loc32 = (targetAddress - (fixupAddress+4)) + addend;
break;
case X86_64_RELOC_UNSIGNED:
*loc64 = targetAddress + addend;
break;
case X86_64_RELOC_SIGNED_1:
*loc32 = (targetAddress - (fixupAddress+5)) + addend;
break;
case X86_64_RELOC_SIGNED_2:
*loc32 = (targetAddress - (fixupAddress+6)) + addend;
break;
case X86_64_RELOC_SIGNED_4:
*loc32 = (targetAddress - (fixupAddress+8)) + addend;
break;
case LLD_X86_64_RELOC_SIGNED_32:
*loc32 = (targetAddress - fixupAddress) + addend;
break;
case LLD_X86_64_RELOC_GOT_LOAD_NOW_LEA:
case LLD_X86_64_RELOC_TLV_NOW_LEA:
// Change MOVQ to LEA
assert(location[-2] == 0x8B);
location[-2] = 0x8D;
*loc32 = (targetAddress - (fixupAddress+4)) + addend;
break;
case LLD_X86_64_RELOC_LAZY_TARGET:
case LLD_X86_64_RELOC_LAZY_IMMEDIATE:
// do nothing
break;
default:
llvm_unreachable("invalid x86_64 Reference Kind");
break;
}
}
//===----------------------------------------------------------------------===//
// KindHandler_x86
//===----------------------------------------------------------------------===//
KindHandler_x86::~KindHandler_x86() {
}
const Registry::KindStrings KindHandler_x86::kindStrings[] = {
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_BRANCH32),
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_ABS32),
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_FUNC_REL32),
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_POINTER32),
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_LAZY_TARGET),
LLD_KIND_STRING_ENTRY(LLD_X86_RELOC_LAZY_IMMEDIATE),
LLD_KIND_STRING_END
};
bool KindHandler_x86::isCallSite(const Reference &ref) {
return (ref.kindValue() == LLD_X86_RELOC_BRANCH32);
}
bool KindHandler_x86::isPointer(const Reference &ref) {
return (ref.kindValue() == LLD_X86_RELOC_POINTER32);
}
bool KindHandler_x86::isLazyImmediate(const Reference &ref) {
return (ref.kindValue() == LLD_X86_RELOC_LAZY_TARGET);
}
bool KindHandler_x86::isLazyTarget(const Reference &ref) {
return (ref.kindValue() == LLD_X86_RELOC_LAZY_TARGET);
}
void KindHandler_x86::applyFixup(Reference::KindNamespace ns,
Reference::KindArch arch,
Reference::KindValue kindValue,
uint64_t addend, uint8_t *location,
uint64_t fixupAddress,
uint64_t targetAddress) {
if (ns != Reference::KindNamespace::mach_o)
return;
assert(arch == Reference::KindArch::x86);
int32_t *loc32 = reinterpret_cast<int32_t*>(location);
switch (kindValue) {
case LLD_X86_RELOC_BRANCH32:
*loc32 = (targetAddress - (fixupAddress+4)) + addend;
break;
case LLD_X86_RELOC_POINTER32:
case LLD_X86_RELOC_ABS32:
*loc32 = targetAddress + addend;
break;
case LLD_X86_RELOC_FUNC_REL32:
*loc32 = targetAddress + addend;
break;
case LLD_X86_RELOC_LAZY_TARGET:
case LLD_X86_RELOC_LAZY_IMMEDIATE:
// do nothing
break;
default:
llvm_unreachable("invalid x86 Reference Kind");
break;
}
}
//===----------------------------------------------------------------------===//
// KindHandler_arm
//===----------------------------------------------------------------------===//
KindHandler_arm::~KindHandler_arm() {
}
const Registry::KindStrings KindHandler_arm::kindStrings[] = {
LLD_KIND_STRING_ENTRY(ARM_RELOC_BR24),
LLD_KIND_STRING_ENTRY(ARM_THUMB_RELOC_BR22),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_THUMB_ABS_LO16),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_THUMB_ABS_HI16),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_THUMB_REL_LO16),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_THUMB_REL_HI16),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_ABS32),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_POINTER32),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_LAZY_TARGET),
LLD_KIND_STRING_ENTRY(LLD_ARM_RELOC_LAZY_IMMEDIATE),
LLD_KIND_STRING_END
};
bool KindHandler_arm::isCallSite(const Reference &ref) {
return (ref.kindValue() == ARM_THUMB_RELOC_BR22) ||
(ref.kindValue() == ARM_RELOC_BR24);
}
bool KindHandler_arm::isPointer(const Reference &ref) {
return (ref.kindValue() == LLD_ARM_RELOC_POINTER32);
}
bool KindHandler_arm::isLazyImmediate(const Reference &ref) {
return (ref.kindValue() == LLD_ARM_RELOC_LAZY_IMMEDIATE);
}
bool KindHandler_arm::isLazyTarget(const Reference &ref) {
return (ref.kindValue() == LLD_ARM_RELOC_LAZY_TARGET);
}
void KindHandler_arm::applyFixup(Reference::KindNamespace ns,
Reference::KindArch arch,
Reference::KindValue kindValue,
uint64_t addend, uint8_t *location,
uint64_t fixupAddress,
uint64_t targetAddress) {
if (ns != Reference::KindNamespace::mach_o)
return;
assert(arch == Reference::KindArch::ARM);
//int32_t *loc32 = reinterpret_cast<int32_t*>(location);
switch ( kindValue ) {
case ARM_THUMB_RELOC_BR22:
// FIXME
break;
case ARM_RELOC_BR24:
// FIXME
break;
case LLD_ARM_RELOC_THUMB_ABS_LO16:
// FIXME
break;
case LLD_ARM_RELOC_THUMB_ABS_HI16:
// FIXME
break;
case LLD_ARM_RELOC_THUMB_REL_LO16:
// FIXME
break;
case LLD_ARM_RELOC_THUMB_REL_HI16:
// FIXME
break;
case LLD_ARM_RELOC_ABS32:
// FIXME
break;
case LLD_ARM_RELOC_POINTER32:
// FIXME
break;
case LLD_ARM_RELOC_LAZY_TARGET:
case LLD_ARM_RELOC_LAZY_IMMEDIATE:
// do nothing
break;
default:
llvm_unreachable("invalid ARM Reference Kind");
break;
}
}
} // namespace mach_o
} // namespace lld
|