Return a MCSection from MCFragment::getParent().
[oota-llvm.git] / lib / Target / X86 / MCTargetDesc / X86MachObjectWriter.cpp
1 //===-- X86MachObjectWriter.cpp - X86 Mach-O Writer -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
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 "MCTargetDesc/X86MCTargetDesc.h"
11 #include "MCTargetDesc/X86FixupKinds.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCMachObjectWriter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/MachO.h"
23
24 using namespace llvm;
25
26 namespace {
27 class X86MachObjectWriter : public MCMachObjectTargetWriter {
28   bool RecordScatteredRelocation(MachObjectWriter *Writer,
29                                  const MCAssembler &Asm,
30                                  const MCAsmLayout &Layout,
31                                  const MCFragment *Fragment,
32                                  const MCFixup &Fixup,
33                                  MCValue Target,
34                                  unsigned Log2Size,
35                                  uint64_t &FixedValue);
36   void RecordTLVPRelocation(MachObjectWriter *Writer,
37                             const MCAssembler &Asm,
38                             const MCAsmLayout &Layout,
39                             const MCFragment *Fragment,
40                             const MCFixup &Fixup,
41                             MCValue Target,
42                             uint64_t &FixedValue);
43
44   void RecordX86Relocation(MachObjectWriter *Writer,
45                               const MCAssembler &Asm,
46                               const MCAsmLayout &Layout,
47                               const MCFragment *Fragment,
48                               const MCFixup &Fixup,
49                               MCValue Target,
50                               uint64_t &FixedValue);
51   void RecordX86_64Relocation(MachObjectWriter *Writer, MCAssembler &Asm,
52                               const MCAsmLayout &Layout,
53                               const MCFragment *Fragment, const MCFixup &Fixup,
54                               MCValue Target, uint64_t &FixedValue);
55
56 public:
57   X86MachObjectWriter(bool Is64Bit, uint32_t CPUType,
58                       uint32_t CPUSubtype)
59     : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
60                                /*UseAggressiveSymbolFolding=*/Is64Bit) {}
61
62   void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
63                         const MCAsmLayout &Layout, const MCFragment *Fragment,
64                         const MCFixup &Fixup, MCValue Target,
65                         uint64_t &FixedValue) override {
66     if (Writer->is64Bit())
67       RecordX86_64Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
68                              FixedValue);
69     else
70       RecordX86Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
71                           FixedValue);
72   }
73 };
74 }
75
76 static bool isFixupKindRIPRel(unsigned Kind) {
77   return Kind == X86::reloc_riprel_4byte ||
78     Kind == X86::reloc_riprel_4byte_movq_load;
79 }
80
81 static unsigned getFixupKindLog2Size(unsigned Kind) {
82   switch (Kind) {
83   default:
84     llvm_unreachable("invalid fixup kind!");
85   case FK_PCRel_1:
86   case FK_Data_1: return 0;
87   case FK_PCRel_2:
88   case FK_Data_2: return 1;
89   case FK_PCRel_4:
90     // FIXME: Remove these!!!
91   case X86::reloc_riprel_4byte:
92   case X86::reloc_riprel_4byte_movq_load:
93   case X86::reloc_signed_4byte:
94   case FK_Data_4: return 2;
95   case FK_Data_8: return 3;
96   }
97 }
98
99 void X86MachObjectWriter::RecordX86_64Relocation(
100     MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
101     const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
102     uint64_t &FixedValue) {
103   unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
104   unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
105   unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
106
107   // See <reloc.h>.
108   uint32_t FixupOffset =
109     Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
110   uint32_t FixupAddress =
111     Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
112   int64_t Value = 0;
113   unsigned Index = 0;
114   unsigned IsExtern = 0;
115   unsigned Type = 0;
116   const MCSymbol *RelSymbol = nullptr;
117
118   Value = Target.getConstant();
119
120   if (IsPCRel) {
121     // Compensate for the relocation offset, Darwin x86_64 relocations only have
122     // the addend and appear to have attempted to define it to be the actual
123     // expression addend without the PCrel bias. However, instructions with data
124     // following the relocation are not accommodated for (see comment below
125     // regarding SIGNED{1,2,4}), so it isn't exactly that either.
126     Value += 1LL << Log2Size;
127   }
128
129   if (Target.isAbsolute()) { // constant
130     // SymbolNum of 0 indicates the absolute section.
131     Type = MachO::X86_64_RELOC_UNSIGNED;
132
133     // FIXME: I believe this is broken, I don't think the linker can understand
134     // it. I think it would require a local relocation, but I'm not sure if that
135     // would work either. The official way to get an absolute PCrel relocation
136     // is to use an absolute symbol (which we don't support yet).
137     if (IsPCRel) {
138       IsExtern = 1;
139       Type = MachO::X86_64_RELOC_BRANCH;
140     }
141   } else if (Target.getSymB()) { // A - B + constant
142     const MCSymbol *A = &Target.getSymA()->getSymbol();
143     if (A->isTemporary())
144       A = &Writer->findAliasedSymbol(*A);
145     const MCSymbolData &A_SD = Asm.getSymbolData(*A);
146     const MCSymbol *A_Base = Asm.getAtom(*A);
147
148     const MCSymbol *B = &Target.getSymB()->getSymbol();
149     if (B->isTemporary())
150       B = &Writer->findAliasedSymbol(*B);
151     const MCSymbolData &B_SD = Asm.getSymbolData(*B);
152     const MCSymbol *B_Base = Asm.getAtom(*B);
153
154     // Neither symbol can be modified.
155     if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
156         Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
157       report_fatal_error("unsupported relocation of modified symbol", false);
158
159     // We don't support PCrel relocations of differences. Darwin 'as' doesn't
160     // implement most of these correctly.
161     if (IsPCRel)
162       report_fatal_error("unsupported pc-relative relocation of difference",
163                          false);
164
165     // The support for the situation where one or both of the symbols would
166     // require a local relocation is handled just like if the symbols were
167     // external.  This is certainly used in the case of debug sections where the
168     // section has only temporary symbols and thus the symbols don't have base
169     // symbols.  This is encoded using the section ordinal and non-extern
170     // relocation entries.
171
172     // Darwin 'as' doesn't emit correct relocations for this (it ends up with a
173     // single SIGNED relocation); reject it for now.  Except the case where both
174     // symbols don't have a base, equal but both NULL.
175     if (A_Base == B_Base && A_Base)
176       report_fatal_error("unsupported relocation with identical base", false);
177
178     // A subtraction expression where either symbol is undefined is a
179     // non-relocatable expression.
180     if (A->isUndefined() || B->isUndefined()) {
181       StringRef Name = A->isUndefined() ? A->getName() : B->getName();
182       Asm.getContext().reportFatalError(Fixup.getLoc(),
183         "unsupported relocation with subtraction expression, symbol '" +
184         Name + "' can not be undefined in a subtraction expression");
185     }
186
187     Value += Writer->getSymbolAddress(*A, Layout) -
188              (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Layout));
189     Value -= Writer->getSymbolAddress(*B, Layout) -
190              (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Layout));
191
192     if (!A_Base)
193       Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
194     Type = MachO::X86_64_RELOC_UNSIGNED;
195
196     MachO::any_relocation_info MRE;
197     MRE.r_word0 = FixupOffset;
198     MRE.r_word1 =
199         (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
200     Writer->addRelocation(A_Base, &Fragment->getParent()->getSectionData(),
201                           MRE);
202
203     if (B_Base)
204       RelSymbol = B_Base;
205     else
206       Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
207     Type = MachO::X86_64_RELOC_SUBTRACTOR;
208   } else {
209     const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
210     if (Symbol->isTemporary() && Value) {
211       const MCSection &Sec = Symbol->getSection();
212       if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
213         Asm.addLocalUsedInReloc(*Symbol);
214     }
215     const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
216     RelSymbol = Asm.getAtom(*Symbol);
217
218     // Relocations inside debug sections always use local relocations when
219     // possible. This seems to be done because the debugger doesn't fully
220     // understand x86_64 relocation entries, and expects to find values that
221     // have already been fixed up.
222     if (Symbol->isInSection()) {
223       const MCSectionMachO &Section =
224           static_cast<const MCSectionMachO &>(*Fragment->getParent());
225       if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
226         RelSymbol = nullptr;
227     }
228
229     // x86_64 almost always uses external relocations, except when there is no
230     // symbol to use as a base address (a local symbol with no preceding
231     // non-local symbol).
232     if (RelSymbol) {
233       // Add the local offset, if needed.
234       if (RelSymbol != Symbol)
235         Value += Layout.getSymbolOffset(*Symbol) -
236                  Layout.getSymbolOffset(*RelSymbol);
237     } else if (Symbol->isInSection() && !Symbol->isVariable()) {
238       // The index is the section ordinal (1-based).
239       Index = SD.getFragment()->getParent()->getOrdinal() + 1;
240       Value += Writer->getSymbolAddress(*Symbol, Layout);
241
242       if (IsPCRel)
243         Value -= FixupAddress + (1 << Log2Size);
244     } else if (Symbol->isVariable()) {
245       const MCExpr *Value = Symbol->getVariableValue();
246       int64_t Res;
247       bool isAbs = Value->EvaluateAsAbsolute(Res, Layout,
248                                              Writer->getSectionAddressMap());
249       if (isAbs) {
250         FixedValue = Res;
251         return;
252       } else {
253         report_fatal_error("unsupported relocation of variable '" +
254                            Symbol->getName() + "'", false);
255       }
256     } else {
257       report_fatal_error("unsupported relocation of undefined symbol '" +
258                          Symbol->getName() + "'", false);
259     }
260
261     MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
262     if (IsPCRel) {
263       if (IsRIPRel) {
264         if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
265           // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
266           // rewrite the movq to an leaq at link time if the symbol ends up in
267           // the same linkage unit.
268           if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
269             Type = MachO::X86_64_RELOC_GOT_LOAD;
270           else
271             Type = MachO::X86_64_RELOC_GOT;
272         }  else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
273           Type = MachO::X86_64_RELOC_TLV;
274         }  else if (Modifier != MCSymbolRefExpr::VK_None) {
275           report_fatal_error("unsupported symbol modifier in relocation",
276                              false);
277         } else {
278           Type = MachO::X86_64_RELOC_SIGNED;
279
280           // The Darwin x86_64 relocation format has a problem where it cannot
281           // encode an address (L<foo> + <constant>) which is outside the atom
282           // containing L<foo>. Generally, this shouldn't occur but it does
283           // happen when we have a RIPrel instruction with data following the
284           // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
285           // adjustment Darwin x86_64 uses, the offset is still negative and the
286           // linker has no way to recognize this.
287           //
288           // To work around this, Darwin uses several special relocation types
289           // to indicate the offsets. However, the specification or
290           // implementation of these seems to also be incomplete; they should
291           // adjust the addend as well based on the actual encoded instruction
292           // (the additional bias), but instead appear to just look at the final
293           // offset.
294           switch (-(Target.getConstant() + (1LL << Log2Size))) {
295           case 1: Type = MachO::X86_64_RELOC_SIGNED_1; break;
296           case 2: Type = MachO::X86_64_RELOC_SIGNED_2; break;
297           case 4: Type = MachO::X86_64_RELOC_SIGNED_4; break;
298           }
299         }
300       } else {
301         if (Modifier != MCSymbolRefExpr::VK_None)
302           report_fatal_error("unsupported symbol modifier in branch "
303                              "relocation", false);
304
305         Type = MachO::X86_64_RELOC_BRANCH;
306       }
307     } else {
308       if (Modifier == MCSymbolRefExpr::VK_GOT) {
309         Type = MachO::X86_64_RELOC_GOT;
310       } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
311         // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which
312         // case all we do is set the PCrel bit in the relocation entry; this is
313         // used with exception handling, for example. The source is required to
314         // include any necessary offset directly.
315         Type = MachO::X86_64_RELOC_GOT;
316         IsPCRel = 1;
317       } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
318         report_fatal_error("TLVP symbol modifier should have been rip-rel",
319                            false);
320       } else if (Modifier != MCSymbolRefExpr::VK_None)
321         report_fatal_error("unsupported symbol modifier in relocation", false);
322       else {
323         Type = MachO::X86_64_RELOC_UNSIGNED;
324         unsigned Kind = Fixup.getKind();
325         if (Kind == X86::reloc_signed_4byte)
326           report_fatal_error("32-bit absolute addressing is not supported in "
327                              "64-bit mode", false);
328       }
329     }
330   }
331
332   // x86_64 always writes custom values into the fixups.
333   FixedValue = Value;
334
335   // struct relocation_info (8 bytes)
336   MachO::any_relocation_info MRE;
337   MRE.r_word0 = FixupOffset;
338   MRE.r_word1 = (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) |
339                 (IsExtern << 27) | (Type << 28);
340   Writer->addRelocation(RelSymbol, &Fragment->getParent()->getSectionData(),
341                         MRE);
342 }
343
344 bool X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter *Writer,
345                                                     const MCAssembler &Asm,
346                                                     const MCAsmLayout &Layout,
347                                                     const MCFragment *Fragment,
348                                                     const MCFixup &Fixup,
349                                                     MCValue Target,
350                                                     unsigned Log2Size,
351                                                     uint64_t &FixedValue) {
352   uint64_t OriginalFixedValue = FixedValue;
353   uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
354   unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
355   unsigned Type = MachO::GENERIC_RELOC_VANILLA;
356
357   // See <reloc.h>.
358   const MCSymbol *A = &Target.getSymA()->getSymbol();
359   const MCSymbolData *A_SD = &Asm.getSymbolData(*A);
360
361   if (!A_SD->getFragment())
362     report_fatal_error("symbol '" + A->getName() +
363                        "' can not be undefined in a subtraction expression",
364                        false);
365
366   uint32_t Value = Writer->getSymbolAddress(*A, Layout);
367   uint64_t SecAddr = Writer->getSectionAddress(
368       &A_SD->getFragment()->getParent()->getSectionData());
369   FixedValue += SecAddr;
370   uint32_t Value2 = 0;
371
372   if (const MCSymbolRefExpr *B = Target.getSymB()) {
373     const MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
374
375     if (!B_SD->getFragment())
376       report_fatal_error("symbol '" + B->getSymbol().getName() +
377                          "' can not be undefined in a subtraction expression",
378                          false);
379
380     // Select the appropriate difference relocation type.
381     //
382     // Note that there is no longer any semantic difference between these two
383     // relocation types from the linkers point of view, this is done solely for
384     // pedantic compatibility with 'as'.
385     Type = A_SD->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF :
386       (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
387     Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
388     FixedValue -= Writer->getSectionAddress(
389         &B_SD->getFragment()->getParent()->getSectionData());
390   }
391
392   // Relocations are written out in reverse order, so the PAIR comes first.
393   if (Type == MachO::GENERIC_RELOC_SECTDIFF ||
394       Type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
395     // If the offset is too large to fit in a scattered relocation,
396     // we're hosed. It's an unfortunate limitation of the MachO format.
397     if (FixupOffset > 0xffffff) {
398       char Buffer[32];
399       format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer));
400       Asm.getContext().reportFatalError(Fixup.getLoc(),
401                          Twine("Section too large, can't encode "
402                                 "r_address (") + Buffer +
403                          ") into 24 bits of scattered "
404                          "relocation entry.");
405       llvm_unreachable("fatal error returned?!");
406     }
407
408     MachO::any_relocation_info MRE;
409     MRE.r_word0 = ((0                         <<  0) | // r_address
410                    (MachO::GENERIC_RELOC_PAIR << 24) | // r_type
411                    (Log2Size                  << 28) |
412                    (IsPCRel                   << 30) |
413                    MachO::R_SCATTERED);
414     MRE.r_word1 = Value2;
415     Writer->addRelocation(nullptr, &Fragment->getParent()->getSectionData(),
416                           MRE);
417   } else {
418     // If the offset is more than 24-bits, it won't fit in a scattered
419     // relocation offset field, so we fall back to using a non-scattered
420     // relocation. This is a bit risky, as if the offset reaches out of
421     // the block and the linker is doing scattered loading on this
422     // symbol, things can go badly.
423     //
424     // Required for 'as' compatibility.
425     if (FixupOffset > 0xffffff) {
426       FixedValue = OriginalFixedValue;
427       return false;
428     }
429   }
430
431   MachO::any_relocation_info MRE;
432   MRE.r_word0 = ((FixupOffset <<  0) |
433                  (Type        << 24) |
434                  (Log2Size    << 28) |
435                  (IsPCRel     << 30) |
436                  MachO::R_SCATTERED);
437   MRE.r_word1 = Value;
438   Writer->addRelocation(nullptr, &Fragment->getParent()->getSectionData(), MRE);
439   return true;
440 }
441
442 void X86MachObjectWriter::RecordTLVPRelocation(MachObjectWriter *Writer,
443                                                const MCAssembler &Asm,
444                                                const MCAsmLayout &Layout,
445                                                const MCFragment *Fragment,
446                                                const MCFixup &Fixup,
447                                                MCValue Target,
448                                                uint64_t &FixedValue) {
449   assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
450          !is64Bit() &&
451          "Should only be called with a 32-bit TLVP relocation!");
452
453   unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
454   uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
455   unsigned IsPCRel = 0;
456
457   // We're only going to have a second symbol in pic mode and it'll be a
458   // subtraction from the picbase. For 32-bit pic the addend is the difference
459   // between the picbase and the next address.  For 32-bit static the addend is
460   // zero.
461   if (Target.getSymB()) {
462     // If this is a subtraction then we're pcrel.
463     uint32_t FixupAddress =
464       Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
465     IsPCRel = 1;
466     FixedValue =
467         FixupAddress -
468         Writer->getSymbolAddress(Target.getSymB()->getSymbol(), Layout) +
469         Target.getConstant();
470     FixedValue += 1ULL << Log2Size;
471   } else {
472     FixedValue = 0;
473   }
474
475   // struct relocation_info (8 bytes)
476   MachO::any_relocation_info MRE;
477   MRE.r_word0 = Value;
478   MRE.r_word1 =
479       (IsPCRel << 24) | (Log2Size << 25) | (MachO::GENERIC_RELOC_TLV << 28);
480   Writer->addRelocation(&Target.getSymA()->getSymbol(),
481                         &Fragment->getParent()->getSectionData(), MRE);
482 }
483
484 void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
485                                               const MCAssembler &Asm,
486                                               const MCAsmLayout &Layout,
487                                               const MCFragment *Fragment,
488                                               const MCFixup &Fixup,
489                                               MCValue Target,
490                                               uint64_t &FixedValue) {
491   unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
492   unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
493
494   // If this is a 32-bit TLVP reloc it's handled a bit differently.
495   if (Target.getSymA() &&
496       Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
497     RecordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
498                          FixedValue);
499     return;
500   }
501
502   // If this is a difference or a defined symbol plus an offset, then we need a
503   // scattered relocation entry. Differences always require scattered
504   // relocations.
505   if (Target.getSymB()) {
506     RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
507                               Target, Log2Size, FixedValue);
508     return;
509   }
510
511   // Get the symbol data, if any.
512   const MCSymbol *A = nullptr;
513   if (Target.getSymA())
514     A = &Target.getSymA()->getSymbol();
515
516   // If this is an internal relocation with an offset, it also needs a scattered
517   // relocation entry.
518   uint32_t Offset = Target.getConstant();
519   if (IsPCRel)
520     Offset += 1 << Log2Size;
521   // Try to record the scattered relocation if needed. Fall back to non
522   // scattered if necessary (see comments in RecordScatteredRelocation()
523   // for details).
524   if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A) &&
525       RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
526                                 Log2Size, FixedValue))
527     return;
528
529   // See <reloc.h>.
530   uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
531   unsigned Index = 0;
532   unsigned Type = 0;
533   const MCSymbol *RelSymbol = nullptr;
534
535   if (Target.isAbsolute()) { // constant
536     // SymbolNum of 0 indicates the absolute section.
537     //
538     // FIXME: Currently, these are never generated (see code below). I cannot
539     // find a case where they are actually emitted.
540     Type = MachO::GENERIC_RELOC_VANILLA;
541   } else {
542     // Resolve constant variables.
543     if (A->isVariable()) {
544       int64_t Res;
545       if (A->getVariableValue()->EvaluateAsAbsolute(
546               Res, Layout, Writer->getSectionAddressMap())) {
547         FixedValue = Res;
548         return;
549       }
550     }
551
552     // Check whether we need an external or internal relocation.
553     if (Writer->doesSymbolRequireExternRelocation(*A)) {
554       RelSymbol = A;
555       // For external relocations, make sure to offset the fixup value to
556       // compensate for the addend of the symbol address, if it was
557       // undefined. This occurs with weak definitions, for example.
558       if (!A->isUndefined())
559         FixedValue -= Layout.getSymbolOffset(*A);
560     } else {
561       // The index is the section ordinal (1-based).
562       const MCSection &Sec = A->getSection();
563       const MCSectionData &SymSD = Asm.getSectionData(Sec);
564       Index = Sec.getOrdinal() + 1;
565       FixedValue += Writer->getSectionAddress(&SymSD);
566     }
567     if (IsPCRel)
568       FixedValue -=
569           Writer->getSectionAddress(&Fragment->getParent()->getSectionData());
570
571     Type = MachO::GENERIC_RELOC_VANILLA;
572   }
573
574   // struct relocation_info (8 bytes)
575   MachO::any_relocation_info MRE;
576   MRE.r_word0 = FixupOffset;
577   MRE.r_word1 =
578       (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
579   Writer->addRelocation(RelSymbol, &Fragment->getParent()->getSectionData(),
580                         MRE);
581 }
582
583 MCObjectWriter *llvm::createX86MachObjectWriter(raw_pwrite_stream &OS,
584                                                 bool Is64Bit, uint32_t CPUType,
585                                                 uint32_t CPUSubtype) {
586   return createMachObjectWriter(new X86MachObjectWriter(Is64Bit,
587                                                         CPUType,
588                                                         CPUSubtype),
589                                 OS, /*IsLittleEndian=*/true);
590 }