blob: 2272bff65ccb2f7c47f3f9726ddc49215ef0a743 [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 {
Davide Italiano0eb80302017-04-15 01:50:51 +0000121 return refKind == modeCode || refKind == modeData;
Nick Kledzik21921372014-07-24 23:06:56 +0000122 }
123
124 Reference::KindValue dataInCodeTransitionStart(
125 const MachODefinedAtom &atom) override {
126 return modeData;
127 }
128
129 Reference::KindValue dataInCodeTransitionEnd(
130 const MachODefinedAtom &atom) override {
131 return modeCode;
132 }
133
Nick Kledzik2458bec2014-07-16 19:49:02 +0000134private:
135 static const Registry::KindStrings _sKindStrings[];
136 static const StubInfo _sStubInfo;
137
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000138 enum X86Kind : Reference::KindValue {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000139 invalid, /// for error condition
140
Nick Kledzik21921372014-07-24 23:06:56 +0000141 modeCode, /// Content starting at this offset is code.
142 modeData, /// Content starting at this offset is data.
143
Nick Kledzik2458bec2014-07-16 19:49:02 +0000144 // Kinds found in mach-o .o files:
145 branch32, /// ex: call _foo
146 branch16, /// ex: callw _foo
147 abs32, /// ex: movl _foo, %eax
148 funcRel32, /// ex: movl _foo-L1(%eax), %eax
149 pointer32, /// ex: .long _foo
150 delta32, /// ex: .long _foo - .
Nick Kledzik03e16f22014-07-21 22:06:57 +0000151 negDelta32, /// ex: .long . - _foo
Nick Kledzik2458bec2014-07-16 19:49:02 +0000152
153 // Kinds introduced by Passes:
154 lazyPointer, /// Location contains a lazy pointer.
155 lazyImmediateLocation, /// Location contains immediate value used in stub.
156 };
Shankar Easwarana1d36372015-02-22 23:54:38 +0000157
Nick Kledzik2d432352014-07-17 23:16:21 +0000158 static bool useExternalRelocationTo(const Atom &target);
159
160 void applyFixupFinal(const Reference &ref, uint8_t *location,
161 uint64_t fixupAddress, uint64_t targetAddress,
162 uint64_t inAtomAddress);
163
164 void applyFixupRelocatable(const Reference &ref, uint8_t *location,
165 uint64_t fixupAddress,
166 uint64_t targetAddress,
167 uint64_t inAtomAddress);
Nick Kledzik2458bec2014-07-16 19:49:02 +0000168};
169
170//===----------------------------------------------------------------------===//
171// ArchHandler_x86
172//===----------------------------------------------------------------------===//
173
Nick Kledzik2458bec2014-07-16 19:49:02 +0000174const Registry::KindStrings ArchHandler_x86::_sKindStrings[] = {
175 LLD_KIND_STRING_ENTRY(invalid),
Nick Kledzik21921372014-07-24 23:06:56 +0000176 LLD_KIND_STRING_ENTRY(modeCode),
177 LLD_KIND_STRING_ENTRY(modeData),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000178 LLD_KIND_STRING_ENTRY(branch32),
179 LLD_KIND_STRING_ENTRY(branch16),
180 LLD_KIND_STRING_ENTRY(abs32),
181 LLD_KIND_STRING_ENTRY(funcRel32),
182 LLD_KIND_STRING_ENTRY(pointer32),
183 LLD_KIND_STRING_ENTRY(delta32),
Nick Kledzik03e16f22014-07-21 22:06:57 +0000184 LLD_KIND_STRING_ENTRY(negDelta32),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000185 LLD_KIND_STRING_ENTRY(lazyPointer),
186 LLD_KIND_STRING_ENTRY(lazyImmediateLocation),
187 LLD_KIND_STRING_END
188};
189
190const ArchHandler::StubInfo ArchHandler_x86::_sStubInfo = {
191 "dyld_stub_binder",
192
Shankar Easwarana1d36372015-02-22 23:54:38 +0000193 // Lazy pointer references
Nick Kledzik2458bec2014-07-16 19:49:02 +0000194 { Reference::KindArch::x86, pointer32, 0, 0 },
195 { Reference::KindArch::x86, lazyPointer, 0, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000196
Nick Kledzik2458bec2014-07-16 19:49:02 +0000197 // GOT pointer to dyld_stub_binder
198 { Reference::KindArch::x86, pointer32, 0, 0 },
199
200 // x86 code alignment
Shankar Easwarana1d36372015-02-22 23:54:38 +0000201 1,
202
Nick Kledzik2458bec2014-07-16 19:49:02 +0000203 // Stub size and code
Shankar Easwarana1d36372015-02-22 23:54:38 +0000204 6,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000205 { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 }, // jmp *lazyPointer
206 { Reference::KindArch::x86, abs32, 2, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000207 { false, 0, 0, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000208
Nick Kledzik2458bec2014-07-16 19:49:02 +0000209 // Stub Helper size and code
210 10,
211 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $lazy-info-offset
212 0xE9, 0x00, 0x00, 0x00, 0x00 }, // jmp helperhelper
213 { Reference::KindArch::x86, lazyImmediateLocation, 1, 0 },
214 { Reference::KindArch::x86, branch32, 6, 0 },
Shankar Easwarana1d36372015-02-22 23:54:38 +0000215
Pete Coopere8d9df42016-02-09 20:11:17 +0000216 // Stub helper image cache content type
217 DefinedAtom::typeNonLazyPointer,
218
Nick Kledzik2458bec2014-07-16 19:49:02 +0000219 // Stub Helper-Common size and code
220 12,
Pete Cooper35c33182016-02-09 18:56:37 +0000221 // Stub helper alignment
222 2,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000223 { 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $dyld_ImageLoaderCache
224 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *_fast_lazy_bind
225 0x90 }, // nop
226 { Reference::KindArch::x86, abs32, 1, 0 },
Nick Kledzik1bebb282014-09-09 23:52:59 +0000227 { false, 0, 0, 0 },
228 { Reference::KindArch::x86, abs32, 7, 0 },
229 { false, 0, 0, 0 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000230};
231
232bool ArchHandler_x86::isCallSite(const Reference &ref) {
233 return (ref.kindValue() == branch32);
234}
235
236bool ArchHandler_x86::isPointer(const Reference &ref) {
237 return (ref.kindValue() == pointer32);
238}
239
240bool ArchHandler_x86::isPairedReloc(const Relocation &reloc) {
241 if (!reloc.scattered)
242 return false;
243 return (reloc.type == GENERIC_RELOC_LOCAL_SECTDIFF) ||
244 (reloc.type == GENERIC_RELOC_SECTDIFF);
245}
246
Pete Cooper1e009112016-03-30 20:15:06 +0000247llvm::Error
Nick Kledzik2458bec2014-07-16 19:49:02 +0000248ArchHandler_x86::getReferenceInfo(const Relocation &reloc,
249 const DefinedAtom *inAtom,
250 uint32_t offsetInAtom,
251 uint64_t fixupAddress, bool swap,
252 FindAtomBySectionAndAddress atomFromAddress,
253 FindAtomBySymbolIndex atomFromSymbolIndex,
254 Reference::KindValue *kind,
255 const lld::Atom **target,
256 Reference::Addend *addend) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000257 DefinedAtom::ContentPermissions perms;
258 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
259 uint64_t targetAddress;
260 switch (relocPattern(reloc)) {
261 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength4:
262 // ex: call _foo (and _foo undefined)
263 *kind = branch32;
Pete Cooper1e009112016-03-30 20:15:06 +0000264 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000265 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000266 *addend = fixupAddress + 4 + (int32_t)*(const little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000267 break;
268 case GENERIC_RELOC_VANILLA | rPcRel | rLength4:
269 // ex: call _foo (and _foo defined)
270 *kind = branch32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000271 targetAddress =
272 fixupAddress + 4 + (int32_t) * (const little32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000273 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
274 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000275 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4:
276 // ex: call _foo+n (and _foo defined)
277 *kind = branch32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000278 targetAddress =
279 fixupAddress + 4 + (int32_t) * (const little32_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000280 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000281 return ec;
282 *addend = targetAddress - reloc.value;
283 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000284 case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength2:
285 // ex: callw _foo (and _foo undefined)
286 *kind = branch16;
Pete Cooper1e009112016-03-30 20:15:06 +0000287 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000288 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000289 *addend = fixupAddress + 2 + (int16_t)*(const little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000290 break;
291 case GENERIC_RELOC_VANILLA | rPcRel | rLength2:
292 // ex: callw _foo (and _foo defined)
293 *kind = branch16;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000294 targetAddress =
295 fixupAddress + 2 + (int16_t) * (const little16_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000296 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
297 break;
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000298 case GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2:
299 // ex: callw _foo+n (and _foo defined)
300 *kind = branch16;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000301 targetAddress =
302 fixupAddress + 2 + (int16_t) * (const little16_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000303 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000304 return ec;
305 *addend = targetAddress - reloc.value;
306 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000307 case GENERIC_RELOC_VANILLA | rExtern | rLength4:
308 // ex: movl _foo, %eax (and _foo undefined)
309 // ex: .long _foo (and _foo undefined)
310 perms = inAtom->permissions();
311 *kind =
312 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
313 : pointer32;
Pete Cooper1e009112016-03-30 20:15:06 +0000314 if (auto ec = atomFromSymbolIndex(reloc.symbol, target))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000315 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000316 *addend = *(const ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000317 break;
318 case GENERIC_RELOC_VANILLA | rLength4:
319 // ex: movl _foo, %eax (and _foo defined)
320 // ex: .long _foo (and _foo defined)
321 perms = inAtom->permissions();
322 *kind =
323 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
324 : pointer32;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000325 targetAddress = *(const ulittle32_t *)fixupContent;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000326 return atomFromAddress(reloc.symbol, targetAddress, target, addend);
327 break;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000328 case GENERIC_RELOC_VANILLA | rScattered | rLength4:
329 // ex: .long _foo+n (and _foo defined)
330 perms = inAtom->permissions();
331 *kind =
332 ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32
333 : pointer32;
Pete Cooper1e009112016-03-30 20:15:06 +0000334 if (auto ec = atomFromAddress(0, reloc.value, target, addend))
Nick Kledzik7e246a42014-07-18 01:05:35 +0000335 return ec;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000336 *addend = *(const ulittle32_t *)fixupContent - reloc.value;
Nick Kledzik7e246a42014-07-18 01:05:35 +0000337 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000338 default:
Pete Cooper1e009112016-03-30 20:15:06 +0000339 return llvm::make_error<GenericError>("unsupported i386 relocation type");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000340 }
Mehdi Aminic1edf562016-11-11 04:29:25 +0000341 return llvm::Error::success();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000342}
343
Pete Cooper1e009112016-03-30 20:15:06 +0000344llvm::Error
Nick Kledzik2458bec2014-07-16 19:49:02 +0000345ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1,
346 const normalized::Relocation &reloc2,
347 const DefinedAtom *inAtom,
348 uint32_t offsetInAtom,
349 uint64_t fixupAddress, bool swap,
Nick Kledzik9133f8c2014-10-21 23:45:37 +0000350 bool scatterable,
Nick Kledzik2458bec2014-07-16 19:49:02 +0000351 FindAtomBySectionAndAddress atomFromAddr,
352 FindAtomBySymbolIndex atomFromSymbolIndex,
353 Reference::KindValue *kind,
354 const lld::Atom **target,
355 Reference::Addend *addend) {
356 const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom];
Nick Kledzik2458bec2014-07-16 19:49:02 +0000357 DefinedAtom::ContentPermissions perms = inAtom->permissions();
358 uint32_t fromAddress;
359 uint32_t toAddress;
360 uint32_t value;
361 const lld::Atom *fromTarget;
362 Reference::Addend offsetInTo;
363 Reference::Addend offsetInFrom;
364 switch (relocPattern(reloc1) << 16 | relocPattern(reloc2)) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000365 case ((GENERIC_RELOC_SECTDIFF | rScattered | rLength4) << 16 |
366 GENERIC_RELOC_PAIR | rScattered | rLength4):
367 case ((GENERIC_RELOC_LOCAL_SECTDIFF | rScattered | rLength4) << 16 |
368 GENERIC_RELOC_PAIR | rScattered | rLength4):
Nick Kledzik2458bec2014-07-16 19:49:02 +0000369 toAddress = reloc1.value;
370 fromAddress = reloc2.value;
Simon Atanasyan55c26992014-11-14 07:15:43 +0000371 value = *(const little32_t *)fixupContent;
Pete Cooper1e009112016-03-30 20:15:06 +0000372 if (auto ec = atomFromAddr(0, toAddress, target, &offsetInTo))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000373 return ec;
Pete Cooper1e009112016-03-30 20:15:06 +0000374 if (auto ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom))
Nick Kledzik2458bec2014-07-16 19:49:02 +0000375 return ec;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000376 if (fromTarget != inAtom) {
Shankar Easwarana1d36372015-02-22 23:54:38 +0000377 if (*target != inAtom)
Pete Cooper1e009112016-03-30 20:15:06 +0000378 return llvm::make_error<GenericError>(
Rui Ueyamae75e50c2015-04-14 02:34:09 +0000379 "SECTDIFF relocation where neither target is in atom");
Nick Kledzik03e16f22014-07-21 22:06:57 +0000380 *kind = negDelta32;
381 *addend = toAddress - value - fromAddress;
382 *target = fromTarget;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000383 } else {
Nick Kledzik03e16f22014-07-21 22:06:57 +0000384 if ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) {
385 // SECTDIFF relocations are used in i386 codegen where the function
386 // prolog does a CALL to the next instruction which POPs the return
387 // address into EBX which becomes the pic-base register. The POP
388 // instruction is label the used for the subtrahend in expressions.
389 // The funcRel32 kind represents the 32-bit delta to some symbol from
390 // the start of the function (atom) containing the funcRel32.
391 *kind = funcRel32;
392 uint32_t ta = fromAddress + value - toAddress;
393 *addend = ta - offsetInFrom;
394 } else {
395 *kind = delta32;
396 *addend = fromAddress + value - toAddress;
397 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000398 }
Mehdi Aminic1edf562016-11-11 04:29:25 +0000399 return llvm::Error::success();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000400 break;
401 default:
Pete Cooper1e009112016-03-30 20:15:06 +0000402 return llvm::make_error<GenericError>("unsupported i386 relocation type");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000403 }
404}
405
Nick Kledzik2d432352014-07-17 23:16:21 +0000406void ArchHandler_x86::generateAtomContent(const DefinedAtom &atom,
Tim Northovercf78d372014-09-30 21:29:54 +0000407 bool relocatable,
408 FindAddressForAtom findAddress,
Tim Northover1cc4fb72014-10-15 19:32:21 +0000409 FindAddressForAtom findSectionAddress,
Tim Northovercf78d372014-09-30 21:29:54 +0000410 uint64_t imageBaseAddress,
Pete Cooper47e53992016-03-23 22:19:16 +0000411 llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000412 // Copy raw bytes.
Pete Cooper47e53992016-03-23 22:19:16 +0000413 std::copy(atom.rawContent().begin(), atom.rawContent().end(),
414 atomContentBuffer.begin());
Nick Kledzik2d432352014-07-17 23:16:21 +0000415 // Apply fix-ups.
416 for (const Reference *ref : atom) {
417 uint32_t offset = ref->offsetInAtom();
418 const Atom *target = ref->target();
419 uint64_t targetAddress = 0;
420 if (isa<DefinedAtom>(target))
421 targetAddress = findAddress(*target);
422 uint64_t atomAddress = findAddress(atom);
423 uint64_t fixupAddress = atomAddress + offset;
424 if (relocatable) {
425 applyFixupRelocatable(*ref, &atomContentBuffer[offset],
426 fixupAddress, targetAddress,
427 atomAddress);
428 } else {
429 applyFixupFinal(*ref, &atomContentBuffer[offset],
430 fixupAddress, targetAddress,
431 atomAddress);
432 }
433 }
434}
435
Tim Northover40d3ad32014-10-27 22:48:35 +0000436void ArchHandler_x86::applyFixupFinal(const Reference &ref, uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000437 uint64_t fixupAddress,
438 uint64_t targetAddress,
439 uint64_t inAtomAddress) {
440 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
Nick Kledzik2458bec2014-07-16 19:49:02 +0000441 return;
Nick Kledzik2d432352014-07-17 23:16:21 +0000442 assert(ref.kindArch() == Reference::KindArch::x86);
Tim Northover40d3ad32014-10-27 22:48:35 +0000443 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000444 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000445 case branch32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000446 *loc32 = (targetAddress - (fixupAddress + 4)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000447 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000448 case branch16:
Tim Northover40d3ad32014-10-27 22:48:35 +0000449 *loc32 = (targetAddress - (fixupAddress + 2)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000450 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000451 case pointer32:
452 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000453 *loc32 = targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000454 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000455 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000456 *loc32 = targetAddress - inAtomAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000457 break;
Nick Kledzik2458bec2014-07-16 19:49:02 +0000458 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000459 *loc32 = targetAddress - fixupAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000460 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000461 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000462 *loc32 = fixupAddress - targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000463 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000464 case modeCode:
465 case modeData:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000466 case lazyPointer:
Nick Kledzik2458bec2014-07-16 19:49:02 +0000467 // do nothing
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000468 break;
Nick Kledzikf373c772014-11-11 01:31:18 +0000469 case lazyImmediateLocation:
470 *loc32 = ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000471 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000472 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000473 llvm_unreachable("invalid x86 Reference Kind");
Nick Kledzik2458bec2014-07-16 19:49:02 +0000474 break;
475 }
476}
477
Nick Kledzik2d432352014-07-17 23:16:21 +0000478void ArchHandler_x86::applyFixupRelocatable(const Reference &ref,
Tim Northover40d3ad32014-10-27 22:48:35 +0000479 uint8_t *loc,
Nick Kledzik2d432352014-07-17 23:16:21 +0000480 uint64_t fixupAddress,
481 uint64_t targetAddress,
482 uint64_t inAtomAddress) {
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000483 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
484 return;
485 assert(ref.kindArch() == Reference::KindArch::x86);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000486 bool useExternalReloc = useExternalRelocationTo(*ref.target());
Tim Northover40d3ad32014-10-27 22:48:35 +0000487 ulittle16_t *loc16 = reinterpret_cast<ulittle16_t *>(loc);
488 ulittle32_t *loc32 = reinterpret_cast<ulittle32_t *>(loc);
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000489 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik2d432352014-07-17 23:16:21 +0000490 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000491 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000492 *loc32 = ref.addend() - (fixupAddress + 4);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000493 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000494 *loc32 =(targetAddress - (fixupAddress+4)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000495 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000496 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000497 if (useExternalReloc)
Tim Northover40d3ad32014-10-27 22:48:35 +0000498 *loc16 = ref.addend() - (fixupAddress + 2);
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000499 else
Tim Northover40d3ad32014-10-27 22:48:35 +0000500 *loc16 = (targetAddress - (fixupAddress+2)) + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000501 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000502 case pointer32:
503 case abs32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000504 *loc32 = targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000505 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000506 case funcRel32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000507 *loc32 = targetAddress - inAtomAddress + ref.addend(); // FIXME
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000508 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000509 case delta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000510 *loc32 = targetAddress - fixupAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000511 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000512 case negDelta32:
Tim Northover40d3ad32014-10-27 22:48:35 +0000513 *loc32 = fixupAddress - targetAddress + ref.addend();
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000514 break;
Nick Kledzik21921372014-07-24 23:06:56 +0000515 case modeCode:
516 case modeData:
Nick Kledzik2d432352014-07-17 23:16:21 +0000517 case lazyPointer:
518 case lazyImmediateLocation:
519 // do nothing
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000520 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000521 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000522 llvm_unreachable("invalid x86 Reference Kind");
Nick Kledzik2d432352014-07-17 23:16:21 +0000523 break;
524 }
525}
526
527bool ArchHandler_x86::useExternalRelocationTo(const Atom &target) {
528 // Undefined symbols are referenced via external relocations.
529 if (isa<UndefinedAtom>(&target))
530 return true;
531 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&target)) {
532 switch (defAtom->merge()) {
533 case DefinedAtom::mergeAsTentative:
534 // Tentative definitions are referenced via external relocations.
535 return true;
536 case DefinedAtom::mergeAsWeak:
537 case DefinedAtom::mergeAsWeakAndAddressUsed:
538 // Global weak-defs are referenced via external relocations.
539 return (defAtom->scope() == DefinedAtom::scopeGlobal);
540 default:
541 break;
542 }
543 }
544 // Everything else is reference via an internal relocation.
545 return false;
546}
547
Nick Kledzik2d432352014-07-17 23:16:21 +0000548void ArchHandler_x86::appendSectionRelocations(
549 const DefinedAtom &atom,
550 uint64_t atomSectionOffset,
551 const Reference &ref,
552 FindSymbolIndexForAtom symbolIndexForAtom,
553 FindSectionIndexForAtom sectionIndexForAtom,
554 FindAddressForAtom addressForAtom,
555 normalized::Relocations &relocs) {
556 if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
557 return;
558 assert(ref.kindArch() == Reference::KindArch::x86);
559 uint32_t sectionOffset = atomSectionOffset + ref.offsetInAtom();
560 bool useExternalReloc = useExternalRelocationTo(*ref.target());
Jean-Daniel Dupasc31da702015-02-19 12:38:54 +0000561 switch (static_cast<X86Kind>(ref.kindValue())) {
Nick Kledzik21921372014-07-24 23:06:56 +0000562 case modeCode:
563 case modeData:
564 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000565 case branch32:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000566 if (useExternalReloc) {
567 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
568 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength4);
569 } else {
570 if (ref.addend() != 0)
571 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
572 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength4);
573 else
574 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
575 GENERIC_RELOC_VANILLA | rPcRel | rLength4);
576 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000577 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000578 case branch16:
Nick Kledzik68a1abd2014-07-18 00:37:52 +0000579 if (useExternalReloc) {
580 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
581 GENERIC_RELOC_VANILLA | rExtern | rPcRel | rLength2);
582 } else {
583 if (ref.addend() != 0)
584 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
585 GENERIC_RELOC_VANILLA | rScattered | rPcRel | rLength2);
586 else
587 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()),0,
588 GENERIC_RELOC_VANILLA | rPcRel | rLength2);
589 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000590 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000591 case pointer32:
592 case abs32:
593 if (useExternalReloc)
594 appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
Nick Kledzik7e246a42014-07-18 01:05:35 +0000595 GENERIC_RELOC_VANILLA | rExtern | rLength4);
596 else {
597 if (ref.addend() != 0)
598 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
599 GENERIC_RELOC_VANILLA | rScattered | rLength4);
600 else
601 appendReloc(relocs, sectionOffset, sectionIndexForAtom(*ref.target()), 0,
Nick Kledzik2d432352014-07-17 23:16:21 +0000602 GENERIC_RELOC_VANILLA | rLength4);
Nick Kledzik7e246a42014-07-18 01:05:35 +0000603 }
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000604 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000605 case funcRel32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000606 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
607 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
608 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) - ref.addend(),
609 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000610 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000611 case delta32:
Nick Kledzik03e16f22014-07-21 22:06:57 +0000612 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
613 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
614 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
615 ref.offsetInAtom(),
616 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000617 break;
Nick Kledzik03e16f22014-07-21 22:06:57 +0000618 case negDelta32:
619 appendReloc(relocs, sectionOffset, 0, addressForAtom(atom) +
620 ref.offsetInAtom(),
621 GENERIC_RELOC_SECTDIFF | rScattered | rLength4);
622 appendReloc(relocs, sectionOffset, 0, addressForAtom(*ref.target()),
623 GENERIC_RELOC_PAIR | rScattered | rLength4);
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000624 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000625 case lazyPointer:
626 case lazyImmediateLocation:
627 llvm_unreachable("lazy reference kind implies Stubs pass was run");
628 break;
Jean-Daniel Dupas9c222632015-02-15 15:23:48 +0000629 case invalid:
Jean-Daniel Dupas1536f062015-02-14 09:10:25 +0000630 llvm_unreachable("unknown x86 Reference Kind");
631 break;
Nick Kledzik2d432352014-07-17 23:16:21 +0000632 }
633}
634
Nick Kledzik2458bec2014-07-16 19:49:02 +0000635std::unique_ptr<mach_o::ArchHandler> ArchHandler::create_x86() {
636 return std::unique_ptr<mach_o::ArchHandler>(new ArchHandler_x86());
637}
638
639} // namespace mach_o
640} // namespace lld