blob: a2b6067efa6169cad8a4fb0e0a83bf8979aa1b5e [file] [log] [blame]
Nick Kledzik2458bec2014-07-16 19:49:02 +00001//===- lib/FileFormat/MachO/ArchHandler_x86.cpp ---------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ArchHandler.h"
11#include "Atoms.h"
12#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Triple.h"
Tim Northover40d3ad32014-10-27 22:48:35 +000016
17#include "llvm/Support/Endian.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000018#include "llvm/Support/ErrorHandling.h"
19
20using namespace llvm::MachO;
21using namespace lld::mach_o::normalized;
22
23namespace lld {
24namespace mach_o {
25
Tim Northover40d3ad32014-10-27 22:48:35 +000026using llvm::support::ulittle16_t;
27using llvm::support::ulittle32_t;
28
29using llvm::support::little16_t;
30using llvm::support::little32_t;
31
Nick Kledzik2458bec2014-07-16 19:49:02 +000032class ArchHandler_x86 : public ArchHandler {
33public:
34 ArchHandler_x86();
35 virtual ~ArchHandler_x86();
36
37 const Registry::KindStrings *kindStrings() override { return _sKindStrings; }
38
39 Reference::KindArch kindArch() override { return Reference::KindArch::x86; }
40
41 const StubInfo &stubInfo() override { return _sStubInfo; }
42 bool isCallSite(const Reference &) override;
Nick Kledzik4121bce2014-10-14 01:51:42 +000043 bool isNonCallBranch(const Reference &) override {
44 return false;
45 }
46
Nick Kledzik2458bec2014-07-16 19:49:02 +000047 bool isPointer(const Reference &) override;
48 bool isPairedReloc(const normalized::Relocation &) override;
Tim Northovercf78d372014-09-30 21:29:54 +000049
50 bool needsCompactUnwind() override {
51 return false;
52 }
53 Reference::KindValue imageOffsetKind() override {
54 return invalid;
55 }
56 Reference::KindValue imageOffsetKindIndirect() override {
57 return invalid;
58 }
59
Tim Northover995abe32014-10-15 20:26:24 +000060 Reference::KindValue unwindRefToCIEKind() override {
61 return negDelta32;
62 }
63
Tim Northovera6a6ab92014-10-15 18:19:31 +000064 Reference::KindValue unwindRefToFunctionKind() override{
65 return delta32;
66 }
67
Tim Northover1cc4fb72014-10-15 19:32:21 +000068 Reference::KindValue unwindRefToEhFrameKind() override {
69 return invalid;
70 }
71
72
73 uint32_t dwarfCompactUnwindType() override {
74 return 0x04000000U;
75 }
76
Nick Kledzik2458bec2014-07-16 19:49:02 +000077 std::error_code getReferenceInfo(const normalized::Relocation &reloc,
78 const DefinedAtom *inAtom,
79 uint32_t offsetInAtom,
80 uint64_t fixupAddress, bool swap,
81 FindAtomBySectionAndAddress atomFromAddress,
82 FindAtomBySymbolIndex atomFromSymbolIndex,
83 Reference::KindValue *kind,
84 const lld::Atom **target,
85 Reference::Addend *addend) override;
86 std::error_code
87 getPairReferenceInfo(const normalized::Relocation &reloc1,
88 const normalized::Relocation &reloc2,
89 const DefinedAtom *inAtom,
90 uint32_t offsetInAtom,
Nick Kledzik9133f8c2014-10-21 23:45:37 +000091 uint64_t fixupAddress, bool swap, bool scatterable,
Nick Kledzik2458bec2014-07-16 19:49:02 +000092 FindAtomBySectionAndAddress atomFromAddress,
93 FindAtomBySymbolIndex atomFromSymbolIndex,
94 Reference::KindValue *kind,
95 const lld::Atom **target,
96 Reference::Addend *addend) override;
97
Nick Kledzik2d432352014-07-17 23:16:21 +000098 void generateAtomContent(const DefinedAtom &atom, bool relocatable,
99 FindAddressForAtom findAddress,
Tim Northover1cc4fb72014-10-15 19:32:21 +0000100 FindAddressForAtom findSectionAddress,
Tim Northovercf78d372014-09-30 21:29:54 +0000101 uint64_t imageBaseAddress,
Nick Kledzik2d432352014-07-17 23:16:21 +0000102 uint8_t *atomContentBuffer) override;
103
104 void appendSectionRelocations(const DefinedAtom &atom,
105 uint64_t atomSectionOffset,
106 const Reference &ref,
107 FindSymbolIndexForAtom symbolIndexForAtom,
108 FindSectionIndexForAtom sectionIndexForAtom,
109 FindAddressForAtom addressForAtom,
110 normalized::Relocations &relocs) override;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000111
Nick Kledzik21921372014-07-24 23:06:56 +0000112 bool isDataInCodeTransition(Reference::KindValue refKind) override {
113 switch (refKind) {
114 case modeCode:
115 case modeData:
116 return true;
117 default:
118 return false;
119 break;
120 }
121 }
122
123 Reference::KindValue dataInCodeTransitionStart(
124 const MachODefinedAtom &atom) override {
125 return modeData;
126 }
127
128 Reference::KindValue dataInCodeTransitionEnd(
129 const MachODefinedAtom &atom) override {
130 return modeCode;
131 }
132
Nick Kledzik2458bec2014-07-16 19:49:02 +0000133private:
134 static const Registry::KindStrings _sKindStrings[];
135 static const StubInfo _sStubInfo;
136
137 enum : Reference::KindValue {
138 invalid, /// for error condition
139
Nick Kledzik21921372014-07-24 23:06:56 +0000140 modeCode, /// Content starting at this offset is code.
141 modeData, /// Content starting at this offset is data.
142
Nick Kledzik2458bec2014-07-16 19:49:02 +0000143 // Kinds found in mach-o .o files:
144 branch32, /// ex: call _foo
145 branch16, /// ex: callw _foo
146 abs32, /// ex: movl _foo, %eax
147 funcRel32, /// ex: movl _foo-L1(%eax), %eax
148 pointer32, /// ex: .long _foo
149 delta32, /// ex: .long _foo - .
Nick Kledzik03e16f22014-07-21 22:06:57 +0000150 negDelta32, /// ex: .long . - _foo
Nick Kledzik2458bec2014-07-16 19:49:02 +0000151
152 // Kinds introduced by Passes:
153 lazyPointer, /// Location contains a lazy pointer.
154 lazyImmediateLocation, /// Location contains immediate value used in stub.
155 };
156
Nick Kledzik2d432352014-07-17 23:16:21 +0000157 static bool useExternalRelocationTo(const Atom &target);
158
159 void applyFixupFinal(const Reference &ref, uint8_t *location,
160 uint64_t fixupAddress, uint64_t targetAddress,
161 uint64_t inAtomAddress);
162
163 void applyFixupRelocatable(const Reference &ref, uint8_t *location,
164 uint64_t fixupAddress,
165 uint64_t targetAddress,
166 uint64_t inAtomAddress);
Nick Kledzik2458bec2014-07-16 19:49:02 +0000167};
168
169//===----------------------------------------------------------------------===//
170// ArchHandler_x86
171//===----------------------------------------------------------------------===//
172
Tim Northover40d3ad32014-10-27 22:48:35 +0000173ArchHandler_x86::ArchHandler_x86() {}
Nick Kledzik2458bec2014-07-16 19:49:02 +0000174
175ArchHandler_x86::~ArchHandler_x86() { }
176
177const Registry::KindStrings ArchHandler_x86::_sKindStrings[] = {
178 LLD_KIND_STRING_ENTRY(invalid),
Nick Kledzik21921372014-07-24 23:06:56 +0000179 LLD_KIND_STRING_ENTRY(modeCode),
180 LLD_KIND_STRING_ENTRY(modeData),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000181 LLD_KIND_STRING_ENTRY(branch32),
182 LLD_KIND_STRING_ENTRY(branch16),
183 LLD_KIND_STRING_ENTRY(abs32),
184 LLD_KIND_STRING_ENTRY(funcRel32),
185 LLD_KIND_STRING_ENTRY(pointer32),
186 LLD_KIND_STRING_ENTRY(delta32),
Nick Kledzik03e16f22014-07-21 22:06:57 +0000187 LLD_KIND_STRING_ENTRY(negDelta32),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000188 LLD_KIND_STRING_ENTRY(lazyPointer),
189 LLD_KIND_STRING_ENTRY(lazyImmediateLocation),
190 LLD_KIND_STRING_END
191};
192
193const ArchHandler::StubInfo ArchHandler_x86::_sStubInfo = {
194 "dyld_stub_binder",
195
196 // Lazy pointer references
197 { Reference::KindArch::x86, pointer32, 0, 0 },
198 { Reference::KindArch::x86, lazyPointer, 0, 0 },
199
200 // GOT pointer to dyld_stub_binder
201 { Reference::KindArch::x86, pointer32, 0, 0 },
202
203 // x86 code alignment
204 1,
205
206 // Stub size and code
207 6,
208 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 }, // jmp *lazyPointer
209 { Reference::KindArch::x86, abs32, 2, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000210 { false, 0, 0, 0 },
Nick Kledzik2458bec2014-07-16 19:49:02 +0000211
212 // Stub Helper size and code
213 10,
214 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $lazy-info-offset
215 0xE9, 0x00, 0x00, 0x00, 0x00 }, // jmp helperhelper
216 { Reference::KindArch::x86, lazyImmediateLocation, 1, 0 },
217 { Reference::KindArch::x86, branch32, 6, 0 },
218
219 // Stub Helper-Common size and code
220 12,
221 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $dyld_ImageLoaderCache
222 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *_fast_lazy_bind
223 0x90 }, // nop
224 { Reference::KindArch::x86, abs32, 1, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000225 { false, 0, 0, 0 },
226 { Reference::KindArch::x86, abs32, 7, 0 },
227 { false, 0, 0, 0 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000228};
229
230bool ArchHandler_x86::isCallSite(const Reference &ref) {
231 return (ref.kindValue() == branch32);
232}
233
234bool ArchHandler_x86::isPointer(const Reference &ref) {
235 return (ref.kindValue() == pointer32);
236}
237
238bool ArchHandler_x86::isPairedReloc(const Relocation &reloc) {
239 if (!reloc.scattered)
240 return false;
241 return (reloc.type == GENERIC_RELOC_LOCAL_SECTDIFF) ||
242 (reloc.type == GENERIC_RELOC_SECTDIFF);
243}
244
245std::error_code
246ArchHandler_x86::getReferenceInfo(const Relocation &reloc,
247 const DefinedAtom *inAtom,
248 uint32_t offsetInAtom,
249 uint64_t fixupAddress, bool swap,
250 FindAtomBySectionAndAddress atomFromAddress,
251 FindAtomBySymbolIndex atomFromSymbolIndex,
252 Reference::KindValue *kind,
253 const lld::Atom **target,
254 Reference::Addend *addend) {
255 typedef std::error_code E;
256 DefinedAtom::ContentPermissions perms;
257 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
258 uint64_t targetAddress;
259 switch (relocPattern(reloc)) {
260 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength4:
261 // ex: call _foo (and _foo undefined)
262 *kind = branch32;
263 if (E ec = atomFromSymbolIndex(reloc.symbol, target))
264 return ec;
Tim Northovercadb77a2014-10-31 22:12:20 +0000265 *addend = fixupAddress + 4 + (int32_t)*(little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000266 break;
267 case GENERIC_RELOC_VANILLA | rPcRel | rLength4:
268 // ex: call _foo (and _foo defined)
269 *kind = branch32;
Tim Northovercadb77a2014-10-31 22:12:20 +0000270 targetAddress = fixupAddress + 4 + (int32_t)*(little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000271 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
272 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000273 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4:
274 // ex: call _foo+n (and _foo defined)
275 *kind = branch32;
Tim Northovercadb77a2014-10-31 22:12:20 +0000276 targetAddress = fixupAddress + 4 + (int32_t)*(little32_t *)fixupContent;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000277 if (E ec = atomFromAddress(0, reloc.value, target, addend))
278 return ec;
279 *addend = targetAddress - reloc.value;
280 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000281 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength2:
282 // ex: callw _foo (and _foo undefined)
283 *kind = branch16;
284 if (E ec = atomFromSymbolIndex(reloc.symbol, target))
285 return ec;
Tim Northovercadb77a2014-10-31 22:12:20 +0000286 *addend = fixupAddress + 2 + (int16_t)*(little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000287 break;
288 case GENERIC_RELOC_VANILLA | rPcRel | rLength2:
289 // ex: callw _foo (and _foo defined)
290 *kind = branch16;
Tim Northovercadb77a2014-10-31 22:12:20 +0000291 targetAddress = fixupAddress + 2 + (int16_t)*(little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000292 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
293 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000294 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2:
295 // ex: callw _foo+n (and _foo defined)
296 *kind = branch16;
Tim Northovercadb77a2014-10-31 22:12:20 +0000297 targetAddress = fixupAddress + 2 + (int16_t)*(little16_t *)fixupContent;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000298 if (E ec = atomFromAddress(0, reloc.value, target, addend))
299 return ec;
300 *addend = targetAddress - reloc.value;
301 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000302 case GENERIC_RELOC_VANILLA | rExtern | rLength4:
303 // ex: movl _foo, %eax (and _foo undefined)
304 // ex: .long _foo (and _foo undefined)
305 perms = inAtom->permissions();
306 *kind =
307 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
308 : pointer32;
309 if (E ec = atomFromSymbolIndex(reloc.symbol, target))
310 return ec;
Tim Northover40d3ad32014-10-27 22:48:35 +0000311 *addend = *(ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000312 break;
313 case GENERIC_RELOC_VANILLA | rLength4:
314 // ex: movl _foo, %eax (and _foo defined)
315 // ex: .long _foo (and _foo defined)
316 perms = inAtom->permissions();
317 *kind =
318 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
319 : pointer32;
Tim Northover40d3ad32014-10-27 22:48:35 +0000320 targetAddress = *(ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000321 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
322 break;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000323 case GENERIC_RELOC_VANILLA | rScattered | rLength4:
324 // ex: .long _foo+n (and _foo defined)
325 perms = inAtom->permissions();
326 *kind =
327 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
328 : pointer32;
329 if (E ec = atomFromAddress(0, reloc.value, target, addend))
330 return ec;
Tim Northover40d3ad32014-10-27 22:48:35 +0000331 *addend = *(ulittle32_t *)fixupContent - reloc.value;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000332 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000333 default:
334 return make_dynamic_error_code(Twine("unsupported i386 relocation type"));
335 }
336 return std::error_code();
337}
338
339std::error_code
340ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1,
341 const normalized::Relocation &reloc2,
342 const DefinedAtom *inAtom,
343 uint32_t offsetInAtom,
344 uint64_t fixupAddress, bool swap,
Nick Kledzik9133f8c2014-10-21 23:45:37 +0000345 bool scatterable,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000346 FindAtomBySectionAndAddress atomFromAddr,
347 FindAtomBySymbolIndex atomFromSymbolIndex,
348 Reference::KindValue *kind,
349 const lld::Atom **target,
350 Reference::Addend *addend) {
351 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
352 std::error_code ec;
353 DefinedAtom::ContentPermissions perms = inAtom->permissions();
354 uint32_t fromAddress;
355 uint32_t toAddress;
356 uint32_t value;
357 const lld::Atom *fromTarget;
358 Reference::Addend offsetInTo;
359 Reference::Addend offsetInFrom;
360 switch (relocPattern(reloc1) << 16 | relocPattern(reloc2)) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000361 case ((GENERIC_RELOC_SECTDIFF | rScattered | rLength4) << 16 |
362 GENERIC_RELOC_PAIR | rScattered | rLength4):
363 case ((GENERIC_RELOC_LOCAL_SECTDIFF | rScattered | rLength4) << 16 |
364 GENERIC_RELOC_PAIR | rScattered | rLength4):
Nick Kledzik2458bec2014-07-16 19:49:02 +0000365 toAddress = reloc1.value;
366 fromAddress = reloc2.value;
Tim Northover40d3ad32014-10-27 22:48:35 +0000367 value = *(little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000368 ec = atomFromAddr(0, toAddress, target, &offsetInTo);
369 if (ec)
370 return ec;
371 ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom);
372 if (ec)
373 return ec;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000374 if (fromTarget != inAtom) {
375 if (*target != inAtom)
376 return make_dynamic_error_code(Twine("SECTDIFF relocation where "
377 "neither target is in atom"));
378 *kind = negDelta32;
379 *addend = toAddress - value - fromAddress;
380 *target = fromTarget;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000381 } else {
Nick Kledzik03e16f22014-07-21 22:06:57 +0000382 if ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) {
383 // SECTDIFF relocations are used in i386 codegen where the function
384 // prolog does a CALL to the next instruction which POPs the return
385 // address into EBX which becomes the pic-base register. The POP
386 // instruction is label the used for the subtrahend in expressions.
387 // The funcRel32 kind represents the 32-bit delta to some symbol from
388 // the start of the function (atom) containing the funcRel32.
389 *kind = funcRel32;
390 uint32_t ta = fromAddress + value - toAddress;
391 *addend = ta - offsetInFrom;
392 } else {
393 *kind = delta32;
394 *addend = fromAddress + value - toAddress;
395 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000396 }
397 return std::error_code();
398 break;
399 default:
400 return make_dynamic_error_code(Twine("unsupported i386 relocation type"));
401 }
402}
403
Nick Kledzik2d432352014-07-17 23:16:21 +0000404void ArchHandler_x86::generateAtomContent(const DefinedAtom &atom,
Tim Northovercf78d372014-09-30 21:29:54 +0000405 bool relocatable,
406 FindAddressForAtom findAddress,
Tim Northover1cc4fb72014-10-15 19:32:21 +0000407 FindAddressForAtom findSectionAddress,
Tim Northovercf78d372014-09-30 21:29:54 +0000408 uint64_t imageBaseAddress,
409 uint8_t *atomContentBuffer) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000410 // Copy raw bytes.
411 memcpy(atomContentBuffer, atom.rawContent().data(), atom.size());
412 // Apply fix-ups.
413 for (const Reference *ref : atom) {
414 uint32_t offset = ref->offsetInAtom();
415 const Atom *target = ref->target();
416 uint64_t targetAddress = 0;
417 if (isa<DefinedAtom>(target))
418 targetAddress = findAddress(*target);
419 uint64_t atomAddress = findAddress(atom);
420 uint64_t fixupAddress = atomAddress + offset;
421 if (relocatable) {
422 applyFixupRelocatable(*ref, &atomContentBuffer[offset],
423 fixupAddress, targetAddress,
424 atomAddress);
425 } else {
426 applyFixupFinal(*ref, &atomContentBuffer[offset],
427 fixupAddress, targetAddress,
428 atomAddress);
429 }
430 }
431}
432
Tim Northover40d3ad32014-10-27 22:48:35 +0000433void ArchHandler_x86::applyFixupFinal(const Reference &ref, uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000434 uint64_t fixupAddress,
435 uint64_t targetAddress,
436 uint64_t inAtomAddress) {
437 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
Nick Kledzik2458bec2014-07-16 19:49:02 +0000438 return;
Nick Kledzik2d432352014-07-17 23:16:21 +0000439 assert(ref.kindArch() == Reference::KindArch::x86);
Tim Northover40d3ad32014-10-27 22:48:35 +0000440 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Nick Kledzik2d432352014-07-17 23:16:21 +0000441 switch (ref.kindValue()) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000442 case branch32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000443 *loc32 = (targetAddress - (fixupAddress + 4)) + ref.addend();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000444 break;
445 case branch16:
Tim Northover40d3ad32014-10-27 22:48:35 +0000446 *loc32 = (targetAddress - (fixupAddress + 2)) + ref.addend();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000447 break;
448 case pointer32:
449 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000450 *loc32 = targetAddress + ref.addend();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000451 break;
452 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000453 *loc32 = targetAddress - inAtomAddress + ref.addend();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000454 break;
455 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000456 *loc32 = targetAddress - fixupAddress + ref.addend();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000457 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000458 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000459 *loc32 = fixupAddress - targetAddress + ref.addend();
Nick Kledzik03e16f22014-07-21 22:06:57 +0000460 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000461 case modeCode:
462 case modeData:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000463 case lazyPointer:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000464 // do nothing
465 break;
Nick Kledzikf373c772014-11-11 01:31:18 +0000466 case lazyImmediateLocation:
467 *loc32 = ref.addend();
468 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000469 default:
470 llvm_unreachable("invalid x86 Reference Kind");
471 break;
472 }
473}
474
Nick Kledzik2d432352014-07-17 23:16:21 +0000475void ArchHandler_x86::applyFixupRelocatable(const Reference &ref,
Tim Northover40d3ad32014-10-27 22:48:35 +0000476 uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000477 uint64_t fixupAddress,
478 uint64_t targetAddress,
479 uint64_t inAtomAddress) {
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000480 bool useExternalReloc = useExternalRelocationTo(*ref.target());
Tim Northover40d3ad32014-10-27 22:48:35 +0000481 ulittle16_t *loc16 = reinterpret_cast<ulittle16_t *>(loc);
482 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Nick Kledzik2d432352014-07-17 23:16:21 +0000483 switch (ref.kindValue()) {
484 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000485 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000486 *loc32 = ref.addend() - (fixupAddress + 4);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000487 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000488 *loc32 =(targetAddress - (fixupAddress+4)) + ref.addend();
Nick Kledzik2d432352014-07-17 23:16:21 +0000489 break;
490 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000491 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000492 *loc16 = ref.addend() - (fixupAddress + 2);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000493 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000494 *loc16 = (targetAddress - (fixupAddress+2)) + ref.addend();
Nick Kledzik2d432352014-07-17 23:16:21 +0000495 break;
496 case pointer32:
497 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000498 *loc32 = targetAddress + ref.addend();
Nick Kledzik2d432352014-07-17 23:16:21 +0000499 break;
500 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000501 *loc32 = targetAddress - inAtomAddress + ref.addend(); // FIXME
Nick Kledzik2d432352014-07-17 23:16:21 +0000502 break;
503 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000504 *loc32 = targetAddress - fixupAddress + ref.addend();
Nick Kledzik2d432352014-07-17 23:16:21 +0000505 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000506 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000507 *loc32 = fixupAddress - targetAddress + ref.addend();
Nick Kledzik03e16f22014-07-21 22:06:57 +0000508 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000509 case modeCode:
510 case modeData:
Nick Kledzik2d432352014-07-17 23:16:21 +0000511 case lazyPointer:
512 case lazyImmediateLocation:
513 // do nothing
514 break;
515 default:
516 llvm_unreachable("invalid x86 Reference Kind");
517 break;
518 }
519}
520
521bool ArchHandler_x86::useExternalRelocationTo(const Atom &target) {
522 // Undefined symbols are referenced via external relocations.
523 if (isa<UndefinedAtom>(&target))
524 return true;
525 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&target)) {
526 switch (defAtom->merge()) {
527 case DefinedAtom::mergeAsTentative:
528 // Tentative definitions are referenced via external relocations.
529 return true;
530 case DefinedAtom::mergeAsWeak:
531 case DefinedAtom::mergeAsWeakAndAddressUsed:
532 // Global weak-defs are referenced via external relocations.
533 return (defAtom->scope() == DefinedAtom::scopeGlobal);
534 default:
535 break;
536 }
537 }
538 // Everything else is reference via an internal relocation.
539 return false;
540}
541
542
543void ArchHandler_x86::appendSectionRelocations(
544 const DefinedAtom &atom,
545 uint64_t atomSectionOffset,
546 const Reference &ref,
547 FindSymbolIndexForAtom symbolIndexForAtom,
548 FindSectionIndexForAtom sectionIndexForAtom,
549 FindAddressForAtom addressForAtom,
550 normalized::Relocations &relocs) {
551 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
552 return;
553 assert(ref.kindArch() == Reference::KindArch::x86);
554 uint32_t sectionOffset = atomSectionOffset + ref.offsetInAtom();
555 bool useExternalReloc = useExternalRelocationTo(*ref.target());
556 switch (ref.kindValue()) {
Nick Kledzik21921372014-07-24 23:06:56 +0000557 case modeCode:
558 case modeData:
559 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000560 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000561 if (useExternalReloc) {
562 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
563 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength4);
564 } else {
565 if (ref.addend() != 0)
566 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
567 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4);
568 else
569 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
570 GENERIC_RELOC_VANILLA | rPcRel | rLength4);
571 }
Nick Kledzik2d432352014-07-17 23:16:21 +0000572 break;
573 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000574 if (useExternalReloc) {
575 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
576 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength2);
577 } else {
578 if (ref.addend() != 0)
579 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
580 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2);
581 else
582 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
583 GENERIC_RELOC_VANILLA | rPcRel | rLength2);
584 }
Nick Kledzik2d432352014-07-17 23:16:21 +0000585 break;
586 case pointer32:
587 case abs32:
588 if (useExternalReloc)
589 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
Nick Kledzik7e246a42014-07-18 01:05:35 +0000590 GENERIC_RELOC_VANILLA | rExtern | rLength4);
591 else {
592 if (ref.addend() != 0)
593 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
594 GENERIC_RELOC_VANILLA | rScattered | rLength4);
595 else
596 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()), 0,
Nick Kledzik2d432352014-07-17 23:16:21 +0000597 GENERIC_RELOC_VANILLA | rLength4);
Nick Kledzik7e246a42014-07-18 01:05:35 +0000598 }
Nick Kledzik2d432352014-07-17 23:16:21 +0000599 break;
600 case funcRel32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000601 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
602 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
603 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) - ref.addend(),
604 GENERIC_RELOC_PAIR | rScattered | rLength4);
Nick Kledzik2d432352014-07-17 23:16:21 +0000605 break;
606 case delta32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000607 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
608 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
609 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
610 ref.offsetInAtom(),
611 GENERIC_RELOC_PAIR | rScattered | rLength4);
612 break;
613 case negDelta32:
614 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
615 ref.offsetInAtom(),
616 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
617 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
618 GENERIC_RELOC_PAIR | rScattered | rLength4);
Nick Kledzik2d432352014-07-17 23:16:21 +0000619 break;
620 case lazyPointer:
621 case lazyImmediateLocation:
622 llvm_unreachable("lazy reference kind implies Stubs pass was run");
623 break;
624 default:
625 llvm_unreachable("unknown x86 Reference Kind");
626 break;
627
628 }
629}
630
631
Nick Kledzik2458bec2014-07-16 19:49:02 +0000632std::unique_ptr<mach_o::ArchHandler> ArchHandler::create_x86() {
633 return std::unique_ptr<mach_o::ArchHandler>(new ArchHandler_x86());
634}
635
636} // namespace mach_o
637} // namespace lld