blob: 15f1f793b5d7f5371d25c6191bb833908cdb2e46 [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#include "llvm/Support/Endian.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000017#include "llvm/Support/ErrorHandling.h"
18
19using namespace llvm::MachO;
20using namespace lld::mach_o::normalized;
21
22namespace lld {
23namespace mach_o {
24
Tim Northover40d3ad32014-10-27 22:48:35 +000025using llvm::support::ulittle16_t;
26using llvm::support::ulittle32_t;
27
28using llvm::support::little16_t;
29using llvm::support::little32_t;
30
Nick Kledzik2458bec2014-07-16 19:49:02 +000031class ArchHandler_x86 : public ArchHandler {
32public:
Rui Ueyama9071e4a2015-09-10 18:51:36 +000033 ArchHandler_x86() = default;
34 ~ArchHandler_x86() override = default;
Nick Kledzik2458bec2014-07-16 19:49:02 +000035
36 const Registry::KindStrings *kindStrings() override { return _sKindStrings; }
37
38 Reference::KindArch kindArch() override { return Reference::KindArch::x86; }
39
40 const StubInfo &stubInfo() override { return _sStubInfo; }
41 bool isCallSite(const Reference &) override;
Nick Kledzik4121bce2014-10-14 01:51:42 +000042 bool isNonCallBranch(const Reference &) override {
43 return false;
44 }
45
Nick Kledzik2458bec2014-07-16 19:49:02 +000046 bool isPointer(const Reference &) override;
47 bool isPairedReloc(const normalized::Relocation &) override;
Tim Northovercf78d372014-09-30 21:29:54 +000048
49 bool needsCompactUnwind() override {
50 return false;
51 }
Rui Ueyama9071e4a2015-09-10 18:51:36 +000052
Tim Northovercf78d372014-09-30 21:29:54 +000053 Reference::KindValue imageOffsetKind() override {
54 return invalid;
55 }
Rui Ueyama9071e4a2015-09-10 18:51:36 +000056
Tim Northovercf78d372014-09-30 21:29:54 +000057 Reference::KindValue imageOffsetKindIndirect() override {
58 return invalid;
59 }
60
Pete Cooperebecd6c2016-03-15 21:33:10 +000061 Reference::KindValue unwindRefToPersonalityFunctionKind() override {
62 return invalid;
63 }
64
Tim Northover995abe32014-10-15 20:26:24 +000065 Reference::KindValue unwindRefToCIEKind() override {
66 return negDelta32;
67 }
68
Tim Northovera6a6ab92014-10-15 18:19:31 +000069 Reference::KindValue unwindRefToFunctionKind() override{
70 return delta32;
71 }
72
Tim Northover1cc4fb72014-10-15 19:32:21 +000073 Reference::KindValue unwindRefToEhFrameKind() override {
74 return invalid;
75 }
76
Pete Cooper1a6098b2016-02-02 00:02:50 +000077 Reference::KindValue pointerKind() override {
78 return invalid;
79 }
80
Tim Northover1cc4fb72014-10-15 19:32:21 +000081 uint32_t dwarfCompactUnwindType() override {
82 return 0x04000000U;
83 }
84
Pete Cooper1e009112016-03-30 20:15:06 +000085 llvm::Error getReferenceInfo(const normalized::Relocation &reloc,
86 const DefinedAtom *inAtom,
87 uint32_t offsetInAtom,
88 uint64_t fixupAddress, bool swap,
89 FindAtomBySectionAndAddress atomFromAddress,
90 FindAtomBySymbolIndex atomFromSymbolIndex,
91 Reference::KindValue *kind,
92 const lld::Atom **target,
93 Reference::Addend *addend) override;
94 llvm::Error
Nick Kledzik2458bec2014-07-16 19:49:02 +000095 getPairReferenceInfo(const normalized::Relocation &reloc1,
96 const normalized::Relocation &reloc2,
97 const DefinedAtom *inAtom,
98 uint32_t offsetInAtom,
Nick Kledzik9133f8c2014-10-21 23:45:37 +000099 uint64_t fixupAddress, bool swap, bool scatterable,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000100 FindAtomBySectionAndAddress atomFromAddress,
101 FindAtomBySymbolIndex atomFromSymbolIndex,
102 Reference::KindValue *kind,
103 const lld::Atom **target,
104 Reference::Addend *addend) override;
105
Nick Kledzik2d432352014-07-17 23:16:21 +0000106 void generateAtomContent(const DefinedAtom &atom, bool relocatable,
107 FindAddressForAtom findAddress,
Tim Northover1cc4fb72014-10-15 19:32:21 +0000108 FindAddressForAtom findSectionAddress,
Tim Northovercf78d372014-09-30 21:29:54 +0000109 uint64_t imageBaseAddress,
Pete Cooper47e53992016-03-23 22:19:16 +0000110 llvm::MutableArrayRef<uint8_t> atomContentBuffer) override;
Nick Kledzik2d432352014-07-17 23:16:21 +0000111
112 void appendSectionRelocations(const DefinedAtom &atom,
113 uint64_t atomSectionOffset,
114 const Reference &ref,
115 FindSymbolIndexForAtom symbolIndexForAtom,
116 FindSectionIndexForAtom sectionIndexForAtom,
117 FindAddressForAtom addressForAtom,
118 normalized::Relocations &relocs) override;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000119
Nick Kledzik21921372014-07-24 23:06:56 +0000120 bool isDataInCodeTransition(Reference::KindValue refKind) override {
121 switch (refKind) {
122 case modeCode:
123 case modeData:
124 return true;
125 default:
126 return false;
127 break;
128 }
129 }
130
131 Reference::KindValue dataInCodeTransitionStart(
132 const MachODefinedAtom &atom) override {
133 return modeData;
134 }
135
136 Reference::KindValue dataInCodeTransitionEnd(
137 const MachODefinedAtom &atom) override {
138 return modeCode;
139 }
140
Nick Kledzik2458bec2014-07-16 19:49:02 +0000141private:
142 static const Registry::KindStrings _sKindStrings[];
143 static const StubInfo _sStubInfo;
144
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000145 enum X86Kind : Reference::KindValue {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000146 invalid, /// for error condition
147
Nick Kledzik21921372014-07-24 23:06:56 +0000148 modeCode, /// Content starting at this offset is code.
149 modeData, /// Content starting at this offset is data.
150
Nick Kledzik2458bec2014-07-16 19:49:02 +0000151 // Kinds found in mach-o .o files:
152 branch32, /// ex: call _foo
153 branch16, /// ex: callw _foo
154 abs32, /// ex: movl _foo, %eax
155 funcRel32, /// ex: movl _foo-L1(%eax), %eax
156 pointer32, /// ex: .long _foo
157 delta32, /// ex: .long _foo - .
Nick Kledzik03e16f22014-07-21 22:06:57 +0000158 negDelta32, /// ex: .long . - _foo
Nick Kledzik2458bec2014-07-16 19:49:02 +0000159
160 // Kinds introduced by Passes:
161 lazyPointer, /// Location contains a lazy pointer.
162 lazyImmediateLocation, /// Location contains immediate value used in stub.
163 };
Shankar Easwarana1d36372015-02-22 23:54:38 +0000164
Nick Kledzik2d432352014-07-17 23:16:21 +0000165 static bool useExternalRelocationTo(const Atom &target);
166
167 void applyFixupFinal(const Reference &ref, uint8_t *location,
168 uint64_t fixupAddress, uint64_t targetAddress,
169 uint64_t inAtomAddress);
170
171 void applyFixupRelocatable(const Reference &ref, uint8_t *location,
172 uint64_t fixupAddress,
173 uint64_t targetAddress,
174 uint64_t inAtomAddress);
Nick Kledzik2458bec2014-07-16 19:49:02 +0000175};
176
177//===----------------------------------------------------------------------===//
178// ArchHandler_x86
179//===----------------------------------------------------------------------===//
180
Nick Kledzik2458bec2014-07-16 19:49:02 +0000181const Registry::KindStrings ArchHandler_x86::_sKindStrings[] = {
182 LLD_KIND_STRING_ENTRY(invalid),
Nick Kledzik21921372014-07-24 23:06:56 +0000183 LLD_KIND_STRING_ENTRY(modeCode),
184 LLD_KIND_STRING_ENTRY(modeData),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000185 LLD_KIND_STRING_ENTRY(branch32),
186 LLD_KIND_STRING_ENTRY(branch16),
187 LLD_KIND_STRING_ENTRY(abs32),
188 LLD_KIND_STRING_ENTRY(funcRel32),
189 LLD_KIND_STRING_ENTRY(pointer32),
190 LLD_KIND_STRING_ENTRY(delta32),
Nick Kledzik03e16f22014-07-21 22:06:57 +0000191 LLD_KIND_STRING_ENTRY(negDelta32),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000192 LLD_KIND_STRING_ENTRY(lazyPointer),
193 LLD_KIND_STRING_ENTRY(lazyImmediateLocation),
194 LLD_KIND_STRING_END
195};
196
197const ArchHandler::StubInfo ArchHandler_x86::_sStubInfo = {
198 "dyld_stub_binder",
199
Shankar Easwarana1d36372015-02-22 23:54:38 +0000200 // Lazy pointer references
Nick Kledzik2458bec2014-07-16 19:49:02 +0000201 { Reference::KindArch::x86, pointer32, 0, 0 },
202 { Reference::KindArch::x86, lazyPointer, 0, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000203
Nick Kledzik2458bec2014-07-16 19:49:02 +0000204 // GOT pointer to dyld_stub_binder
205 { Reference::KindArch::x86, pointer32, 0, 0 },
206
207 // x86 code alignment
Shankar Easwarana1d36372015-02-22 23:54:38 +0000208 1,
209
Nick Kledzik2458bec2014-07-16 19:49:02 +0000210 // Stub size and code
Shankar Easwarana1d36372015-02-22 23:54:38 +0000211 6,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000212 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 }, // jmp *lazyPointer
213 { Reference::KindArch::x86, abs32, 2, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000214 { false, 0, 0, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000215
Nick Kledzik2458bec2014-07-16 19:49:02 +0000216 // Stub Helper size and code
217 10,
218 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $lazy-info-offset
219 0xE9, 0x00, 0x00, 0x00, 0x00 }, // jmp helperhelper
220 { Reference::KindArch::x86, lazyImmediateLocation, 1, 0 },
221 { Reference::KindArch::x86, branch32, 6, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000222
Pete Coopere8d9df42016-02-09 20:11:17 +0000223 // Stub helper image cache content type
224 DefinedAtom::typeNonLazyPointer,
225
Nick Kledzik2458bec2014-07-16 19:49:02 +0000226 // Stub Helper-Common size and code
227 12,
Pete Cooper35c33182016-02-09 18:56:37 +0000228 // Stub helper alignment
229 2,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000230 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $dyld_ImageLoaderCache
231 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *_fast_lazy_bind
232 0x90 }, // nop
233 { Reference::KindArch::x86, abs32, 1, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000234 { false, 0, 0, 0 },
235 { Reference::KindArch::x86, abs32, 7, 0 },
236 { false, 0, 0, 0 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000237};
238
239bool ArchHandler_x86::isCallSite(const Reference &ref) {
240 return (ref.kindValue() == branch32);
241}
242
243bool ArchHandler_x86::isPointer(const Reference &ref) {
244 return (ref.kindValue() == pointer32);
245}
246
247bool ArchHandler_x86::isPairedReloc(const Relocation &reloc) {
248 if (!reloc.scattered)
249 return false;
250 return (reloc.type == GENERIC_RELOC_LOCAL_SECTDIFF) ||
251 (reloc.type == GENERIC_RELOC_SECTDIFF);
252}
253
Pete Cooper1e009112016-03-30 20:15:06 +0000254llvm::Error
Nick Kledzik2458bec2014-07-16 19:49:02 +0000255ArchHandler_x86::getReferenceInfo(const Relocation &reloc,
256 const DefinedAtom *inAtom,
257 uint32_t offsetInAtom,
258 uint64_t fixupAddress, bool swap,
259 FindAtomBySectionAndAddress atomFromAddress,
260 FindAtomBySymbolIndex atomFromSymbolIndex,
261 Reference::KindValue *kind,
262 const lld::Atom **target,
263 Reference::Addend *addend) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000264 DefinedAtom::ContentPermissions perms;
265 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
266 uint64_t targetAddress;
267 switch (relocPattern(reloc)) {
268 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength4:
269 // ex: call _foo (and _foo undefined)
270 *kind = branch32;
Pete Cooper1e009112016-03-30 20:15:06 +0000271 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000272 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000273 *addend = fixupAddress + 4 + (int32_t)*(const little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000274 break;
275 case GENERIC_RELOC_VANILLA | rPcRel | rLength4:
276 // ex: call _foo (and _foo defined)
277 *kind = branch32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000278 targetAddress =
279 fixupAddress + 4 + (int32_t) * (const little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000280 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
281 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000282 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4:
283 // ex: call _foo+n (and _foo defined)
284 *kind = branch32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000285 targetAddress =
286 fixupAddress + 4 + (int32_t) * (const little32_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000287 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000288 return ec;
289 *addend = targetAddress - reloc.value;
290 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000291 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength2:
292 // ex: callw _foo (and _foo undefined)
293 *kind = branch16;
Pete Cooper1e009112016-03-30 20:15:06 +0000294 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000295 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000296 *addend = fixupAddress + 2 + (int16_t)*(const little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000297 break;
298 case GENERIC_RELOC_VANILLA | rPcRel | rLength2:
299 // ex: callw _foo (and _foo defined)
300 *kind = branch16;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000301 targetAddress =
302 fixupAddress + 2 + (int16_t) * (const little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000303 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
304 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000305 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2:
306 // ex: callw _foo+n (and _foo defined)
307 *kind = branch16;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000308 targetAddress =
309 fixupAddress + 2 + (int16_t) * (const little16_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000310 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000311 return ec;
312 *addend = targetAddress - reloc.value;
313 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000314 case GENERIC_RELOC_VANILLA | rExtern | rLength4:
315 // ex: movl _foo, %eax (and _foo undefined)
316 // ex: .long _foo (and _foo undefined)
317 perms = inAtom->permissions();
318 *kind =
319 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
320 : pointer32;
Pete Cooper1e009112016-03-30 20:15:06 +0000321 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000322 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000323 *addend = *(const ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000324 break;
325 case GENERIC_RELOC_VANILLA | rLength4:
326 // ex: movl _foo, %eax (and _foo defined)
327 // ex: .long _foo (and _foo defined)
328 perms = inAtom->permissions();
329 *kind =
330 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
331 : pointer32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000332 targetAddress = *(const ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000333 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
334 break;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000335 case GENERIC_RELOC_VANILLA | rScattered | rLength4:
336 // ex: .long _foo+n (and _foo defined)
337 perms = inAtom->permissions();
338 *kind =
339 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
340 : pointer32;
Pete Cooper1e009112016-03-30 20:15:06 +0000341 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik7e246a42014-07-18 01:05:35 +0000342 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000343 *addend = *(const ulittle32_t *)fixupContent - reloc.value;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000344 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000345 default:
Pete Cooper1e009112016-03-30 20:15:06 +0000346 return llvm::make_error<GenericError>("unsupported i386 relocation type");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000347 }
Pete Cooper1e009112016-03-30 20:15:06 +0000348 return llvm::Error();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000349}
350
Pete Cooper1e009112016-03-30 20:15:06 +0000351llvm::Error
Nick Kledzik2458bec2014-07-16 19:49:02 +0000352ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1,
353 const normalized::Relocation &reloc2,
354 const DefinedAtom *inAtom,
355 uint32_t offsetInAtom,
356 uint64_t fixupAddress, bool swap,
Nick Kledzik9133f8c2014-10-21 23:45:37 +0000357 bool scatterable,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000358 FindAtomBySectionAndAddress atomFromAddr,
359 FindAtomBySymbolIndex atomFromSymbolIndex,
360 Reference::KindValue *kind,
361 const lld::Atom **target,
362 Reference::Addend *addend) {
363 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
Nick Kledzik2458bec2014-07-16 19:49:02 +0000364 DefinedAtom::ContentPermissions perms = inAtom->permissions();
365 uint32_t fromAddress;
366 uint32_t toAddress;
367 uint32_t value;
368 const lld::Atom *fromTarget;
369 Reference::Addend offsetInTo;
370 Reference::Addend offsetInFrom;
371 switch (relocPattern(reloc1) << 16 | relocPattern(reloc2)) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000372 case ((GENERIC_RELOC_SECTDIFF | rScattered | rLength4) << 16 |
373 GENERIC_RELOC_PAIR | rScattered | rLength4):
374 case ((GENERIC_RELOC_LOCAL_SECTDIFF | rScattered | rLength4) << 16 |
375 GENERIC_RELOC_PAIR | rScattered | rLength4):
Nick Kledzik2458bec2014-07-16 19:49:02 +0000376 toAddress = reloc1.value;
377 fromAddress = reloc2.value;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000378 value = *(const little32_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000379 if (auto ec = atomFromAddr(0, toAddress, target, &offsetInTo))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000380 return ec;
Pete Cooper1e009112016-03-30 20:15:06 +0000381 if (auto ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000382 return ec;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000383 if (fromTarget != inAtom) {
Shankar Easwarana1d36372015-02-22 23:54:38 +0000384 if (*target != inAtom)
Pete Cooper1e009112016-03-30 20:15:06 +0000385 return llvm::make_error<GenericError>(
Rui Ueyamae75e50c2015-04-14 02:34:09 +0000386 "SECTDIFF relocation where neither target is in atom");
Nick Kledzik03e16f22014-07-21 22:06:57 +0000387 *kind = negDelta32;
388 *addend = toAddress - value - fromAddress;
389 *target = fromTarget;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000390 } else {
Nick Kledzik03e16f22014-07-21 22:06:57 +0000391 if ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) {
392 // SECTDIFF relocations are used in i386 codegen where the function
393 // prolog does a CALL to the next instruction which POPs the return
394 // address into EBX which becomes the pic-base register. The POP
395 // instruction is label the used for the subtrahend in expressions.
396 // The funcRel32 kind represents the 32-bit delta to some symbol from
397 // the start of the function (atom) containing the funcRel32.
398 *kind = funcRel32;
399 uint32_t ta = fromAddress + value - toAddress;
400 *addend = ta - offsetInFrom;
401 } else {
402 *kind = delta32;
403 *addend = fromAddress + value - toAddress;
404 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000405 }
Pete Cooper1e009112016-03-30 20:15:06 +0000406 return llvm::Error();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000407 break;
408 default:
Pete Cooper1e009112016-03-30 20:15:06 +0000409 return llvm::make_error<GenericError>("unsupported i386 relocation type");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000410 }
411}
412
Nick Kledzik2d432352014-07-17 23:16:21 +0000413void ArchHandler_x86::generateAtomContent(const DefinedAtom &atom,
Tim Northovercf78d372014-09-30 21:29:54 +0000414 bool relocatable,
415 FindAddressForAtom findAddress,
Tim Northover1cc4fb72014-10-15 19:32:21 +0000416 FindAddressForAtom findSectionAddress,
Tim Northovercf78d372014-09-30 21:29:54 +0000417 uint64_t imageBaseAddress,
Pete Cooper47e53992016-03-23 22:19:16 +0000418 llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000419 // Copy raw bytes.
Pete Cooper47e53992016-03-23 22:19:16 +0000420 std::copy(atom.rawContent().begin(), atom.rawContent().end(),
421 atomContentBuffer.begin());
Nick Kledzik2d432352014-07-17 23:16:21 +0000422 // Apply fix-ups.
423 for (const Reference *ref : atom) {
424 uint32_t offset = ref->offsetInAtom();
425 const Atom *target = ref->target();
426 uint64_t targetAddress = 0;
427 if (isa<DefinedAtom>(target))
428 targetAddress = findAddress(*target);
429 uint64_t atomAddress = findAddress(atom);
430 uint64_t fixupAddress = atomAddress + offset;
431 if (relocatable) {
432 applyFixupRelocatable(*ref, &atomContentBuffer[offset],
433 fixupAddress, targetAddress,
434 atomAddress);
435 } else {
436 applyFixupFinal(*ref, &atomContentBuffer[offset],
437 fixupAddress, targetAddress,
438 atomAddress);
439 }
440 }
441}
442
Tim Northover40d3ad32014-10-27 22:48:35 +0000443void ArchHandler_x86::applyFixupFinal(const Reference &ref, uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000444 uint64_t fixupAddress,
445 uint64_t targetAddress,
446 uint64_t inAtomAddress) {
447 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
Nick Kledzik2458bec2014-07-16 19:49:02 +0000448 return;
Nick Kledzik2d432352014-07-17 23:16:21 +0000449 assert(ref.kindArch() == Reference::KindArch::x86);
Tim Northover40d3ad32014-10-27 22:48:35 +0000450 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000451 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000452 case branch32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000453 *loc32 = (targetAddress - (fixupAddress + 4)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000454 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000455 case branch16:
Tim Northover40d3ad32014-10-27 22:48:35 +0000456 *loc32 = (targetAddress - (fixupAddress + 2)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000457 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000458 case pointer32:
459 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000460 *loc32 = targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000461 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000462 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000463 *loc32 = targetAddress - inAtomAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000464 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000465 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000466 *loc32 = targetAddress - fixupAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000467 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000468 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000469 *loc32 = fixupAddress - targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000470 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000471 case modeCode:
472 case modeData:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000473 case lazyPointer:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000474 // do nothing
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000475 break;
Nick Kledzikf373c772014-11-11 01:31:18 +0000476 case lazyImmediateLocation:
477 *loc32 = ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000478 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000479 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000480 llvm_unreachable("invalid x86 Reference Kind");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000481 break;
482 }
483}
484
Nick Kledzik2d432352014-07-17 23:16:21 +0000485void ArchHandler_x86::applyFixupRelocatable(const Reference &ref,
Tim Northover40d3ad32014-10-27 22:48:35 +0000486 uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000487 uint64_t fixupAddress,
488 uint64_t targetAddress,
489 uint64_t inAtomAddress) {
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000490 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
491 return;
492 assert(ref.kindArch() == Reference::KindArch::x86);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000493 bool useExternalReloc = useExternalRelocationTo(*ref.target());
Tim Northover40d3ad32014-10-27 22:48:35 +0000494 ulittle16_t *loc16 = reinterpret_cast<ulittle16_t *>(loc);
495 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000496 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000497 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000498 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000499 *loc32 = ref.addend() - (fixupAddress + 4);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000500 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000501 *loc32 =(targetAddress - (fixupAddress+4)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000502 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000503 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000504 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000505 *loc16 = ref.addend() - (fixupAddress + 2);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000506 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000507 *loc16 = (targetAddress - (fixupAddress+2)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000508 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000509 case pointer32:
510 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000511 *loc32 = targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000512 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000513 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000514 *loc32 = targetAddress - inAtomAddress + ref.addend(); // FIXME
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000515 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000516 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000517 *loc32 = targetAddress - fixupAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000518 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000519 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000520 *loc32 = fixupAddress - targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000521 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000522 case modeCode:
523 case modeData:
Nick Kledzik2d432352014-07-17 23:16:21 +0000524 case lazyPointer:
525 case lazyImmediateLocation:
526 // do nothing
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000527 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000528 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000529 llvm_unreachable("invalid x86 Reference Kind");
Nick Kledzik2d432352014-07-17 23:16:21 +0000530 break;
531 }
532}
533
534bool ArchHandler_x86::useExternalRelocationTo(const Atom &target) {
535 // Undefined symbols are referenced via external relocations.
536 if (isa<UndefinedAtom>(&target))
537 return true;
538 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&target)) {
539 switch (defAtom->merge()) {
540 case DefinedAtom::mergeAsTentative:
541 // Tentative definitions are referenced via external relocations.
542 return true;
543 case DefinedAtom::mergeAsWeak:
544 case DefinedAtom::mergeAsWeakAndAddressUsed:
545 // Global weak-defs are referenced via external relocations.
546 return (defAtom->scope() == DefinedAtom::scopeGlobal);
547 default:
548 break;
549 }
550 }
551 // Everything else is reference via an internal relocation.
552 return false;
553}
554
Nick Kledzik2d432352014-07-17 23:16:21 +0000555void ArchHandler_x86::appendSectionRelocations(
556 const DefinedAtom &atom,
557 uint64_t atomSectionOffset,
558 const Reference &ref,
559 FindSymbolIndexForAtom symbolIndexForAtom,
560 FindSectionIndexForAtom sectionIndexForAtom,
561 FindAddressForAtom addressForAtom,
562 normalized::Relocations &relocs) {
563 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
564 return;
565 assert(ref.kindArch() == Reference::KindArch::x86);
566 uint32_t sectionOffset = atomSectionOffset + ref.offsetInAtom();
567 bool useExternalReloc = useExternalRelocationTo(*ref.target());
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000568 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik21921372014-07-24 23:06:56 +0000569 case modeCode:
570 case modeData:
571 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000572 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000573 if (useExternalReloc) {
574 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
575 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength4);
576 } else {
577 if (ref.addend() != 0)
578 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
579 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4);
580 else
581 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
582 GENERIC_RELOC_VANILLA | rPcRel | rLength4);
583 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000584 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000585 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000586 if (useExternalReloc) {
587 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
588 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength2);
589 } else {
590 if (ref.addend() != 0)
591 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
592 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2);
593 else
594 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
595 GENERIC_RELOC_VANILLA | rPcRel | rLength2);
596 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000597 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000598 case pointer32:
599 case abs32:
600 if (useExternalReloc)
601 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
Nick Kledzik7e246a42014-07-18 01:05:35 +0000602 GENERIC_RELOC_VANILLA | rExtern | rLength4);
603 else {
604 if (ref.addend() != 0)
605 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
606 GENERIC_RELOC_VANILLA | rScattered | rLength4);
607 else
608 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()), 0,
Nick Kledzik2d432352014-07-17 23:16:21 +0000609 GENERIC_RELOC_VANILLA | rLength4);
Nick Kledzik7e246a42014-07-18 01:05:35 +0000610 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000611 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000612 case funcRel32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000613 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
614 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
615 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) - ref.addend(),
616 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000617 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000618 case delta32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000619 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
620 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
621 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
622 ref.offsetInAtom(),
623 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000624 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000625 case negDelta32:
626 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
627 ref.offsetInAtom(),
628 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
629 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
630 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000631 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000632 case lazyPointer:
633 case lazyImmediateLocation:
634 llvm_unreachable("lazy reference kind implies Stubs pass was run");
635 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000636 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000637 llvm_unreachable("unknown x86 Reference Kind");
638 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000639 }
640}
641
Nick Kledzik2458bec2014-07-16 19:49:02 +0000642std::unique_ptr<mach_o::ArchHandler> ArchHandler::create_x86() {
643 return std::unique_ptr<mach_o::ArchHandler>(new ArchHandler_x86());
644}
645
646} // namespace mach_o
647} // namespace lld