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