1 //===-- X86MachObjectWriter.cpp - X86 Mach-O Writer -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/X86MCTargetDesc.h"
11 #include "MCTargetDesc/X86FixupKinds.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmLayout.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCMachObjectWriter.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/MachO.h"
26 class X86MachObjectWriter : public MCMachObjectTargetWriter {
27 bool RecordScatteredRelocation(MachObjectWriter *Writer,
28 const MCAssembler &Asm,
29 const MCAsmLayout &Layout,
30 const MCFragment *Fragment,
34 uint64_t &FixedValue);
35 void RecordTLVPRelocation(MachObjectWriter *Writer,
36 const MCAssembler &Asm,
37 const MCAsmLayout &Layout,
38 const MCFragment *Fragment,
41 uint64_t &FixedValue);
43 void RecordX86Relocation(MachObjectWriter *Writer,
44 const MCAssembler &Asm,
45 const MCAsmLayout &Layout,
46 const MCFragment *Fragment,
49 uint64_t &FixedValue);
50 void RecordX86_64Relocation(MachObjectWriter *Writer,
51 const MCAssembler &Asm,
52 const MCAsmLayout &Layout,
53 const MCFragment *Fragment,
56 uint64_t &FixedValue);
58 X86MachObjectWriter(bool Is64Bit, uint32_t CPUType,
60 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
61 /*UseAggressiveSymbolFolding=*/Is64Bit) {}
63 void RecordRelocation(MachObjectWriter *Writer,
64 const MCAssembler &Asm, const MCAsmLayout &Layout,
65 const MCFragment *Fragment, const MCFixup &Fixup,
66 MCValue Target, uint64_t &FixedValue) override {
67 if (Writer->is64Bit())
68 RecordX86_64Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
71 RecordX86Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
77 static bool isFixupKindRIPRel(unsigned Kind) {
78 return Kind == X86::reloc_riprel_4byte ||
79 Kind == X86::reloc_riprel_4byte_movq_load;
82 static unsigned getFixupKindLog2Size(unsigned Kind) {
85 llvm_unreachable("invalid fixup kind!");
87 case FK_Data_1: return 0;
89 case FK_Data_2: return 1;
91 // FIXME: Remove these!!!
92 case X86::reloc_riprel_4byte:
93 case X86::reloc_riprel_4byte_movq_load:
94 case X86::reloc_signed_4byte:
95 case FK_Data_4: return 2;
96 case FK_Data_8: return 3;
100 void X86MachObjectWriter::RecordX86_64Relocation(MachObjectWriter *Writer,
101 const MCAssembler &Asm,
102 const MCAsmLayout &Layout,
103 const MCFragment *Fragment,
104 const MCFixup &Fixup,
106 uint64_t &FixedValue) {
107 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
108 unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
109 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
112 uint32_t FixupOffset =
113 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
114 uint32_t FixupAddress =
115 Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
118 unsigned IsExtern = 0;
121 Value = Target.getConstant();
124 // Compensate for the relocation offset, Darwin x86_64 relocations only have
125 // the addend and appear to have attempted to define it to be the actual
126 // expression addend without the PCrel bias. However, instructions with data
127 // following the relocation are not accommodated for (see comment below
128 // regarding SIGNED{1,2,4}), so it isn't exactly that either.
129 Value += 1LL << Log2Size;
132 if (Target.isAbsolute()) { // constant
133 // SymbolNum of 0 indicates the absolute section.
134 Type = MachO::X86_64_RELOC_UNSIGNED;
137 // FIXME: I believe this is broken, I don't think the linker can understand
138 // it. I think it would require a local relocation, but I'm not sure if that
139 // would work either. The official way to get an absolute PCrel relocation
140 // is to use an absolute symbol (which we don't support yet).
143 Type = MachO::X86_64_RELOC_BRANCH;
145 } else if (Target.getSymB()) { // A - B + constant
146 const MCSymbol *A = &Target.getSymA()->getSymbol();
147 if (A->isTemporary())
148 A = &A->AliasedSymbol();
149 const MCSymbolData &A_SD = Asm.getSymbolData(*A);
150 const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
152 const MCSymbol *B = &Target.getSymB()->getSymbol();
153 if (B->isTemporary())
154 B = &B->AliasedSymbol();
155 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
156 const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
158 // Neither symbol can be modified.
159 if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
160 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
161 report_fatal_error("unsupported relocation of modified symbol", false);
163 // We don't support PCrel relocations of differences. Darwin 'as' doesn't
164 // implement most of these correctly.
166 report_fatal_error("unsupported pc-relative relocation of difference",
169 // The support for the situation where one or both of the symbols would
170 // require a local relocation is handled just like if the symbols were
171 // external. This is certainly used in the case of debug sections where the
172 // section has only temporary symbols and thus the symbols don't have base
173 // symbols. This is encoded using the section ordinal and non-extern
174 // relocation entries.
176 // Darwin 'as' doesn't emit correct relocations for this (it ends up with a
177 // single SIGNED relocation); reject it for now. Except the case where both
178 // symbols don't have a base, equal but both NULL.
179 if (A_Base == B_Base && A_Base)
180 report_fatal_error("unsupported relocation with identical base", false);
182 // A subtraction expression where either symbol is undefined is a
183 // non-relocatable expression.
184 if (A->isUndefined() || B->isUndefined()) {
185 StringRef Name = A->isUndefined() ? A->getName() : B->getName();
186 Asm.getContext().FatalError(Fixup.getLoc(),
187 "unsupported relocation with subtraction expression, symbol '" +
188 Name + "' can not be undefined in a subtraction expression");
191 Value += Writer->getSymbolAddress(&A_SD, Layout) -
192 (!A_Base ? 0 : Writer->getSymbolAddress(A_Base, Layout));
193 Value -= Writer->getSymbolAddress(&B_SD, Layout) -
194 (!B_Base ? 0 : Writer->getSymbolAddress(B_Base, Layout));
197 Index = A_Base->getIndex();
201 Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
204 Type = MachO::X86_64_RELOC_UNSIGNED;
206 MachO::any_relocation_info MRE;
207 MRE.r_word0 = FixupOffset;
208 MRE.r_word1 = ((Index << 0) |
213 Writer->addRelocation(Fragment->getParent(), MRE);
216 Index = B_Base->getIndex();
220 Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
223 Type = MachO::X86_64_RELOC_SUBTRACTOR;
225 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
226 const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
227 const MCSymbolData *Base = Asm.getAtom(&SD);
229 // Relocations inside debug sections always use local relocations when
230 // possible. This seems to be done because the debugger doesn't fully
231 // understand x86_64 relocation entries, and expects to find values that
232 // have already been fixed up.
233 if (Symbol->isInSection()) {
234 const MCSectionMachO &Section = static_cast<const MCSectionMachO&>(
235 Fragment->getParent()->getSection());
236 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
240 // x86_64 almost always uses external relocations, except when there is no
241 // symbol to use as a base address (a local symbol with no preceding
242 // non-local symbol).
244 Index = Base->getIndex();
247 // Add the local offset, if needed.
249 Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
250 } else if (Symbol->isInSection() && !Symbol->isVariable()) {
251 // The index is the section ordinal (1-based).
252 Index = SD.getFragment()->getParent()->getOrdinal() + 1;
254 Value += Writer->getSymbolAddress(&SD, Layout);
257 Value -= FixupAddress + (1 << Log2Size);
258 } else if (Symbol->isVariable()) {
259 const MCExpr *Value = Symbol->getVariableValue();
261 bool isAbs = Value->EvaluateAsAbsolute(Res, Layout,
262 Writer->getSectionAddressMap());
267 report_fatal_error("unsupported relocation of variable '" +
268 Symbol->getName() + "'", false);
271 report_fatal_error("unsupported relocation of undefined symbol '" +
272 Symbol->getName() + "'", false);
275 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
278 if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
279 // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
280 // rewrite the movq to an leaq at link time if the symbol ends up in
281 // the same linkage unit.
282 if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
283 Type = MachO::X86_64_RELOC_GOT_LOAD;
285 Type = MachO::X86_64_RELOC_GOT;
286 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
287 Type = MachO::X86_64_RELOC_TLV;
288 } else if (Modifier != MCSymbolRefExpr::VK_None) {
289 report_fatal_error("unsupported symbol modifier in relocation",
292 Type = MachO::X86_64_RELOC_SIGNED;
294 // The Darwin x86_64 relocation format has a problem where it cannot
295 // encode an address (L<foo> + <constant>) which is outside the atom
296 // containing L<foo>. Generally, this shouldn't occur but it does
297 // happen when we have a RIPrel instruction with data following the
298 // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
299 // adjustment Darwin x86_64 uses, the offset is still negative and the
300 // linker has no way to recognize this.
302 // To work around this, Darwin uses several special relocation types
303 // to indicate the offsets. However, the specification or
304 // implementation of these seems to also be incomplete; they should
305 // adjust the addend as well based on the actual encoded instruction
306 // (the additional bias), but instead appear to just look at the final
308 switch (-(Target.getConstant() + (1LL << Log2Size))) {
309 case 1: Type = MachO::X86_64_RELOC_SIGNED_1; break;
310 case 2: Type = MachO::X86_64_RELOC_SIGNED_2; break;
311 case 4: Type = MachO::X86_64_RELOC_SIGNED_4; break;
315 if (Modifier != MCSymbolRefExpr::VK_None)
316 report_fatal_error("unsupported symbol modifier in branch "
317 "relocation", false);
319 Type = MachO::X86_64_RELOC_BRANCH;
322 if (Modifier == MCSymbolRefExpr::VK_GOT) {
323 Type = MachO::X86_64_RELOC_GOT;
324 } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
325 // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which
326 // case all we do is set the PCrel bit in the relocation entry; this is
327 // used with exception handling, for example. The source is required to
328 // include any necessary offset directly.
329 Type = MachO::X86_64_RELOC_GOT;
331 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
332 report_fatal_error("TLVP symbol modifier should have been rip-rel",
334 } else if (Modifier != MCSymbolRefExpr::VK_None)
335 report_fatal_error("unsupported symbol modifier in relocation", false);
337 Type = MachO::X86_64_RELOC_UNSIGNED;
338 unsigned Kind = Fixup.getKind();
339 if (Kind == X86::reloc_signed_4byte)
340 report_fatal_error("32-bit absolute addressing is not supported in "
341 "64-bit mode", false);
346 // x86_64 always writes custom values into the fixups.
349 // struct relocation_info (8 bytes)
350 MachO::any_relocation_info MRE;
351 MRE.r_word0 = FixupOffset;
352 MRE.r_word1 = ((Index << 0) |
357 Writer->addRelocation(Fragment->getParent(), MRE);
360 bool X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter *Writer,
361 const MCAssembler &Asm,
362 const MCAsmLayout &Layout,
363 const MCFragment *Fragment,
364 const MCFixup &Fixup,
367 uint64_t &FixedValue) {
368 uint64_t OriginalFixedValue = FixedValue;
369 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
370 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
371 unsigned Type = MachO::GENERIC_RELOC_VANILLA;
374 const MCSymbol *A = &Target.getSymA()->getSymbol();
375 const MCSymbolData *A_SD = &Asm.getSymbolData(*A);
377 if (!A_SD->getFragment())
378 report_fatal_error("symbol '" + A->getName() +
379 "' can not be undefined in a subtraction expression",
382 uint32_t Value = Writer->getSymbolAddress(A_SD, Layout);
383 uint64_t SecAddr = Writer->getSectionAddress(A_SD->getFragment()->getParent());
384 FixedValue += SecAddr;
387 if (const MCSymbolRefExpr *B = Target.getSymB()) {
388 const MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
390 if (!B_SD->getFragment())
391 report_fatal_error("symbol '" + B->getSymbol().getName() +
392 "' can not be undefined in a subtraction expression",
395 // Select the appropriate difference relocation type.
397 // Note that there is no longer any semantic difference between these two
398 // relocation types from the linkers point of view, this is done solely for
399 // pedantic compatibility with 'as'.
400 Type = A_SD->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF :
401 (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
402 Value2 = Writer->getSymbolAddress(B_SD, Layout);
403 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
406 // Relocations are written out in reverse order, so the PAIR comes first.
407 if (Type == MachO::GENERIC_RELOC_SECTDIFF ||
408 Type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
409 // If the offset is too large to fit in a scattered relocation,
410 // we're hosed. It's an unfortunate limitation of the MachO format.
411 if (FixupOffset > 0xffffff) {
413 format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer));
414 Asm.getContext().FatalError(Fixup.getLoc(),
415 Twine("Section too large, can't encode "
416 "r_address (") + Buffer +
417 ") into 24 bits of scattered "
418 "relocation entry.");
419 llvm_unreachable("fatal error returned?!");
422 MachO::any_relocation_info MRE;
423 MRE.r_word0 = ((0 << 0) | // r_address
424 (MachO::GENERIC_RELOC_PAIR << 24) | // r_type
428 MRE.r_word1 = Value2;
429 Writer->addRelocation(Fragment->getParent(), MRE);
431 // If the offset is more than 24-bits, it won't fit in a scattered
432 // relocation offset field, so we fall back to using a non-scattered
433 // relocation. This is a bit risky, as if the offset reaches out of
434 // the block and the linker is doing scattered loading on this
435 // symbol, things can go badly.
437 // Required for 'as' compatibility.
438 if (FixupOffset > 0xffffff) {
439 FixedValue = OriginalFixedValue;
444 MachO::any_relocation_info MRE;
445 MRE.r_word0 = ((FixupOffset << 0) |
451 Writer->addRelocation(Fragment->getParent(), MRE);
455 void X86MachObjectWriter::RecordTLVPRelocation(MachObjectWriter *Writer,
456 const MCAssembler &Asm,
457 const MCAsmLayout &Layout,
458 const MCFragment *Fragment,
459 const MCFixup &Fixup,
461 uint64_t &FixedValue) {
462 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
464 "Should only be called with a 32-bit TLVP relocation!");
466 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
467 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
468 unsigned IsPCRel = 0;
470 // Get the symbol data.
471 const MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
472 unsigned Index = SD_A->getIndex();
474 // We're only going to have a second symbol in pic mode and it'll be a
475 // subtraction from the picbase. For 32-bit pic the addend is the difference
476 // between the picbase and the next address. For 32-bit static the addend is
478 if (Target.getSymB()) {
479 // If this is a subtraction then we're pcrel.
480 uint32_t FixupAddress =
481 Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
482 const MCSymbolData *SD_B =
483 &Asm.getSymbolData(Target.getSymB()->getSymbol());
485 FixedValue = (FixupAddress - Writer->getSymbolAddress(SD_B, Layout) +
486 Target.getConstant());
487 FixedValue += 1ULL << Log2Size;
492 // struct relocation_info (8 bytes)
493 MachO::any_relocation_info MRE;
495 MRE.r_word1 = ((Index << 0) |
498 (1 << 27) | // r_extern
499 (MachO::GENERIC_RELOC_TLV << 28)); // r_type
500 Writer->addRelocation(Fragment->getParent(), MRE);
503 void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
504 const MCAssembler &Asm,
505 const MCAsmLayout &Layout,
506 const MCFragment *Fragment,
507 const MCFixup &Fixup,
509 uint64_t &FixedValue) {
510 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
511 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
513 // If this is a 32-bit TLVP reloc it's handled a bit differently.
514 if (Target.getSymA() &&
515 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
516 RecordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
521 // If this is a difference or a defined symbol plus an offset, then we need a
522 // scattered relocation entry. Differences always require scattered
524 if (Target.getSymB()) {
525 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
526 Target, Log2Size, FixedValue);
530 // Get the symbol data, if any.
531 const MCSymbolData *SD = nullptr;
532 if (Target.getSymA())
533 SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
535 // If this is an internal relocation with an offset, it also needs a scattered
537 uint32_t Offset = Target.getConstant();
539 Offset += 1 << Log2Size;
540 // Try to record the scattered relocation if needed. Fall back to non
541 // scattered if necessary (see comments in RecordScatteredRelocation()
543 if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD) &&
544 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
545 Target, Log2Size, FixedValue))
549 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
551 unsigned IsExtern = 0;
554 if (Target.isAbsolute()) { // constant
555 // SymbolNum of 0 indicates the absolute section.
557 // FIXME: Currently, these are never generated (see code below). I cannot
558 // find a case where they are actually emitted.
559 Type = MachO::GENERIC_RELOC_VANILLA;
561 // Resolve constant variables.
562 if (SD->getSymbol().isVariable()) {
564 if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute(
565 Res, Layout, Writer->getSectionAddressMap())) {
571 // Check whether we need an external or internal relocation.
572 if (Writer->doesSymbolRequireExternRelocation(SD)) {
574 Index = SD->getIndex();
575 // For external relocations, make sure to offset the fixup value to
576 // compensate for the addend of the symbol address, if it was
577 // undefined. This occurs with weak definitions, for example.
578 if (!SD->getSymbol().isUndefined())
579 FixedValue -= Layout.getSymbolOffset(SD);
581 // The index is the section ordinal (1-based).
582 const MCSectionData &SymSD = Asm.getSectionData(
583 SD->getSymbol().getSection());
584 Index = SymSD.getOrdinal() + 1;
585 FixedValue += Writer->getSectionAddress(&SymSD);
588 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
590 Type = MachO::GENERIC_RELOC_VANILLA;
593 // struct relocation_info (8 bytes)
594 MachO::any_relocation_info MRE;
595 MRE.r_word0 = FixupOffset;
596 MRE.r_word1 = ((Index << 0) |
601 Writer->addRelocation(Fragment->getParent(), MRE);
604 MCObjectWriter *llvm::createX86MachObjectWriter(raw_ostream &OS,
607 uint32_t CPUSubtype) {
608 return createMachObjectWriter(new X86MachObjectWriter(Is64Bit,
611 OS, /*IsLittleEndian=*/true);