X86MachObjectWriter.cpp revision 226584
1210678Srpaulo//===-- X86MachObjectWriter.cpp - X86 Mach-O Writer -----------------------===//
2210678Srpaulo//
3210678Srpaulo//                     The LLVM Compiler Infrastructure
4210678Srpaulo//
5210678Srpaulo// This file is distributed under the University of Illinois Open Source
6210678Srpaulo// License. See LICENSE.TXT for details.
7210678Srpaulo//
8210678Srpaulo//===----------------------------------------------------------------------===//
9210678Srpaulo
10210678Srpaulo#include "MCTargetDesc/X86FixupKinds.h"
11210678Srpaulo#include "MCTargetDesc/X86MCTargetDesc.h"
12210678Srpaulo#include "llvm/MC/MCAssembler.h"
13210678Srpaulo#include "llvm/MC/MCAsmLayout.h"
14210678Srpaulo#include "llvm/MC/MCMachObjectWriter.h"
15210678Srpaulo#include "llvm/MC/MCSectionMachO.h"
16210678Srpaulo#include "llvm/MC/MCValue.h"
17210678Srpaulo#include "llvm/ADT/Twine.h"
18210678Srpaulo#include "llvm/Support/ErrorHandling.h"
19210678Srpaulo#include "llvm/Object/MachOFormat.h"
20210678Srpaulo
21210678Srpaulousing namespace llvm;
22210678Srpaulousing namespace llvm::object;
23210678Srpaulo
24210678Srpaulonamespace {
25210678Srpauloclass X86MachObjectWriter : public MCMachObjectTargetWriter {
26210678Srpaulo  void RecordScatteredRelocation(MachObjectWriter *Writer,
27210678Srpaulo                                 const MCAssembler &Asm,
28210678Srpaulo                                 const MCAsmLayout &Layout,
29210678Srpaulo                                 const MCFragment *Fragment,
30210678Srpaulo                                 const MCFixup &Fixup,
31210678Srpaulo                                 MCValue Target,
32210678Srpaulo                                 unsigned Log2Size,
33210678Srpaulo                                 uint64_t &FixedValue);
34210678Srpaulo  void RecordTLVPRelocation(MachObjectWriter *Writer,
35210678Srpaulo                            const MCAssembler &Asm,
36210678Srpaulo                            const MCAsmLayout &Layout,
37210678Srpaulo                            const MCFragment *Fragment,
38210678Srpaulo                            const MCFixup &Fixup,
39210678Srpaulo                            MCValue Target,
40210678Srpaulo                            uint64_t &FixedValue);
41210678Srpaulo
42210678Srpaulo  void RecordX86Relocation(MachObjectWriter *Writer,
43210678Srpaulo                              const MCAssembler &Asm,
44210678Srpaulo                              const MCAsmLayout &Layout,
45210678Srpaulo                              const MCFragment *Fragment,
46210678Srpaulo                              const MCFixup &Fixup,
47210678Srpaulo                              MCValue Target,
48210678Srpaulo                              uint64_t &FixedValue);
49210678Srpaulo  void RecordX86_64Relocation(MachObjectWriter *Writer,
50210678Srpaulo                              const MCAssembler &Asm,
51210678Srpaulo                              const MCAsmLayout &Layout,
52210678Srpaulo                              const MCFragment *Fragment,
53210678Srpaulo                              const MCFixup &Fixup,
54210678Srpaulo                              MCValue Target,
55210678Srpaulo                              uint64_t &FixedValue);
56210678Srpaulopublic:
57210678Srpaulo  X86MachObjectWriter(bool Is64Bit, uint32_t CPUType,
58210678Srpaulo                      uint32_t CPUSubtype)
59210678Srpaulo    : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
60210678Srpaulo                               /*UseAggressiveSymbolFolding=*/Is64Bit) {}
61210678Srpaulo
62210678Srpaulo  void RecordRelocation(MachObjectWriter *Writer,
63210678Srpaulo                        const MCAssembler &Asm, const MCAsmLayout &Layout,
64210678Srpaulo                        const MCFragment *Fragment, const MCFixup &Fixup,
65210678Srpaulo                        MCValue Target, uint64_t &FixedValue) {
66210678Srpaulo    if (Writer->is64Bit())
67210678Srpaulo      RecordX86_64Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
68210678Srpaulo                             FixedValue);
69210678Srpaulo    else
70210678Srpaulo      RecordX86Relocation(Writer, Asm, Layout, Fragment, Fixup, Target,
71210678Srpaulo                          FixedValue);
72210678Srpaulo  }
73210678Srpaulo};
74210678Srpaulo}
75210678Srpaulo
76210678Srpaulostatic bool isFixupKindRIPRel(unsigned Kind) {
77210678Srpaulo  return Kind == X86::reloc_riprel_4byte ||
78210678Srpaulo    Kind == X86::reloc_riprel_4byte_movq_load;
79210678Srpaulo}
80210678Srpaulo
81210678Srpaulostatic unsigned getFixupKindLog2Size(unsigned Kind) {
82210678Srpaulo  switch (Kind) {
83210678Srpaulo  default:
84269720Smarkj    llvm_unreachable("invalid fixup kind!");
85210678Srpaulo  case FK_PCRel_1:
86269720Smarkj  case FK_Data_1: return 0;
87210678Srpaulo  case FK_PCRel_2:
88269720Smarkj  case FK_Data_2: return 1;
89269720Smarkj  case FK_PCRel_4:
90210678Srpaulo    // FIXME: Remove these!!!
91269720Smarkj  case X86::reloc_riprel_4byte:
92269720Smarkj  case X86::reloc_riprel_4byte_movq_load:
93269720Smarkj  case X86::reloc_signed_4byte:
94269720Smarkj  case FK_Data_4: return 2;
95269720Smarkj  case FK_Data_8: return 3;
96269720Smarkj  }
97269720Smarkj}
98269720Smarkj
99269720Smarkjvoid X86MachObjectWriter::RecordX86_64Relocation(MachObjectWriter *Writer,
100269720Smarkj                                                 const MCAssembler &Asm,
101269720Smarkj                                                 const MCAsmLayout &Layout,
102269720Smarkj                                                 const MCFragment *Fragment,
103269720Smarkj                                                 const MCFixup &Fixup,
104269720Smarkj                                                 MCValue Target,
105269720Smarkj                                                 uint64_t &FixedValue) {
106269720Smarkj  unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
107269720Smarkj  unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
108269720Smarkj  unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
109269720Smarkj
110269720Smarkj  // See <reloc.h>.
111269720Smarkj  uint32_t FixupOffset =
112210678Srpaulo    Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
113210678Srpaulo  uint32_t FixupAddress =
114210678Srpaulo    Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
115210678Srpaulo  int64_t Value = 0;
116210678Srpaulo  unsigned Index = 0;
117269720Smarkj  unsigned IsExtern = 0;
118210678Srpaulo  unsigned Type = 0;
119210678Srpaulo
120210678Srpaulo  Value = Target.getConstant();
121210678Srpaulo
122210678Srpaulo  if (IsPCRel) {
123210678Srpaulo    // Compensate for the relocation offset, Darwin x86_64 relocations only have
124210678Srpaulo    // the addend and appear to have attempted to define it to be the actual
125210678Srpaulo    // expression addend without the PCrel bias. However, instructions with data
126210678Srpaulo    // following the relocation are not accommodated for (see comment below
127210678Srpaulo    // regarding SIGNED{1,2,4}), so it isn't exactly that either.
128210678Srpaulo    Value += 1LL << Log2Size;
129210678Srpaulo  }
130210678Srpaulo
131210678Srpaulo  if (Target.isAbsolute()) { // constant
132210678Srpaulo    // SymbolNum of 0 indicates the absolute section.
133210678Srpaulo    Type = macho::RIT_X86_64_Unsigned;
134210678Srpaulo    Index = 0;
135210678Srpaulo
136210678Srpaulo    // FIXME: I believe this is broken, I don't think the linker can understand
137210678Srpaulo    // it. I think it would require a local relocation, but I'm not sure if that
138210678Srpaulo    // would work either. The official way to get an absolute PCrel relocation
139210678Srpaulo    // is to use an absolute symbol (which we don't support yet).
140210678Srpaulo    if (IsPCRel) {
141210678Srpaulo      IsExtern = 1;
142210678Srpaulo      Type = macho::RIT_X86_64_Branch;
143210678Srpaulo    }
144210678Srpaulo  } else if (Target.getSymB()) { // A - B + constant
145210678Srpaulo    const MCSymbol *A = &Target.getSymA()->getSymbol();
146210678Srpaulo    MCSymbolData &A_SD = Asm.getSymbolData(*A);
147210678Srpaulo    const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
148210678Srpaulo
149210678Srpaulo    const MCSymbol *B = &Target.getSymB()->getSymbol();
150210678Srpaulo    MCSymbolData &B_SD = Asm.getSymbolData(*B);
151210678Srpaulo    const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
152210678Srpaulo
153210678Srpaulo    // Neither symbol can be modified.
154210678Srpaulo    if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
155210678Srpaulo        Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
156210678Srpaulo      report_fatal_error("unsupported relocation of modified symbol");
157210678Srpaulo
158210678Srpaulo    // We don't support PCrel relocations of differences. Darwin 'as' doesn't
159210678Srpaulo    // implement most of these correctly.
160210678Srpaulo    if (IsPCRel)
161210678Srpaulo      report_fatal_error("unsupported pc-relative relocation of difference");
162210678Srpaulo
163210678Srpaulo    // The support for the situation where one or both of the symbols would
164210678Srpaulo    // require a local relocation is handled just like if the symbols were
165210678Srpaulo    // external.  This is certainly used in the case of debug sections where the
166210678Srpaulo    // section has only temporary symbols and thus the symbols don't have base
167210678Srpaulo    // symbols.  This is encoded using the section ordinal and non-extern
168210678Srpaulo    // relocation entries.
169210678Srpaulo
170210678Srpaulo    // Darwin 'as' doesn't emit correct relocations for this (it ends up with a
171210678Srpaulo    // single SIGNED relocation); reject it for now.  Except the case where both
172210678Srpaulo    // symbols don't have a base, equal but both NULL.
173210678Srpaulo    if (A_Base == B_Base && A_Base)
174210678Srpaulo      report_fatal_error("unsupported relocation with identical base");
175210678Srpaulo
176210678Srpaulo    Value += Writer->getSymbolAddress(&A_SD, Layout) -
177210678Srpaulo      (A_Base == NULL ? 0 : Writer->getSymbolAddress(A_Base, Layout));
178210678Srpaulo    Value -= Writer->getSymbolAddress(&B_SD, Layout) -
179210678Srpaulo      (B_Base == NULL ? 0 : Writer->getSymbolAddress(B_Base, Layout));
180210678Srpaulo
181210678Srpaulo    if (A_Base) {
182210678Srpaulo      Index = A_Base->getIndex();
183210678Srpaulo      IsExtern = 1;
184210678Srpaulo    }
185210678Srpaulo    else {
186210678Srpaulo      Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
187210678Srpaulo      IsExtern = 0;
188210678Srpaulo    }
189210678Srpaulo    Type = macho::RIT_X86_64_Unsigned;
190210678Srpaulo
191210678Srpaulo    macho::RelocationEntry MRE;
192210678Srpaulo    MRE.Word0 = FixupOffset;
193210678Srpaulo    MRE.Word1 = ((Index     <<  0) |
194210678Srpaulo                 (IsPCRel   << 24) |
195210678Srpaulo                 (Log2Size  << 25) |
196210678Srpaulo                 (IsExtern  << 27) |
197210678Srpaulo                 (Type      << 28));
198210678Srpaulo    Writer->addRelocation(Fragment->getParent(), MRE);
199210678Srpaulo
200210678Srpaulo    if (B_Base) {
201210678Srpaulo      Index = B_Base->getIndex();
202210678Srpaulo      IsExtern = 1;
203210678Srpaulo    }
204210678Srpaulo    else {
205210678Srpaulo      Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
206210678Srpaulo      IsExtern = 0;
207210678Srpaulo    }
208210678Srpaulo    Type = macho::RIT_X86_64_Subtractor;
209210678Srpaulo  } else {
210210678Srpaulo    const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
211210678Srpaulo    MCSymbolData &SD = Asm.getSymbolData(*Symbol);
212210678Srpaulo    const MCSymbolData *Base = Asm.getAtom(&SD);
213210678Srpaulo
214210678Srpaulo    // Relocations inside debug sections always use local relocations when
215210678Srpaulo    // possible. This seems to be done because the debugger doesn't fully
216210678Srpaulo    // understand x86_64 relocation entries, and expects to find values that
217210678Srpaulo    // have already been fixed up.
218210678Srpaulo    if (Symbol->isInSection()) {
219210678Srpaulo      const MCSectionMachO &Section = static_cast<const MCSectionMachO&>(
220210678Srpaulo        Fragment->getParent()->getSection());
221210678Srpaulo      if (Section.hasAttribute(MCSectionMachO::S_ATTR_DEBUG))
222210678Srpaulo        Base = 0;
223210678Srpaulo    }
224210678Srpaulo
225210678Srpaulo    // x86_64 almost always uses external relocations, except when there is no
226210678Srpaulo    // symbol to use as a base address (a local symbol with no preceding
227210678Srpaulo    // non-local symbol).
228210678Srpaulo    if (Base) {
229210678Srpaulo      Index = Base->getIndex();
230210678Srpaulo      IsExtern = 1;
231210678Srpaulo
232210678Srpaulo      // Add the local offset, if needed.
233210678Srpaulo      if (Base != &SD)
234210678Srpaulo        Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
235210678Srpaulo    } else if (Symbol->isInSection() && !Symbol->isVariable()) {
236210678Srpaulo      // The index is the section ordinal (1-based).
237210678Srpaulo      Index = SD.getFragment()->getParent()->getOrdinal() + 1;
238210678Srpaulo      IsExtern = 0;
239210678Srpaulo      Value += Writer->getSymbolAddress(&SD, Layout);
240210678Srpaulo
241210678Srpaulo      if (IsPCRel)
242210678Srpaulo        Value -= FixupAddress + (1 << Log2Size);
243269720Smarkj    } else if (Symbol->isVariable()) {
244269720Smarkj      const MCExpr *Value = Symbol->getVariableValue();
245210678Srpaulo      int64_t Res;
246269720Smarkj      bool isAbs = Value->EvaluateAsAbsolute(Res, Layout,
247269720Smarkj                                             Writer->getSectionAddressMap());
248269720Smarkj      if (isAbs) {
249269720Smarkj        FixedValue = Res;
250269720Smarkj        return;
251269720Smarkj      } else {
252269720Smarkj        report_fatal_error("unsupported relocation of variable '" +
253210678Srpaulo                           Symbol->getName() + "'");
254210678Srpaulo      }
255    } else {
256      report_fatal_error("unsupported relocation of undefined symbol '" +
257                         Symbol->getName() + "'");
258    }
259
260    MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
261    if (IsPCRel) {
262      if (IsRIPRel) {
263        if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
264          // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
265          // rewrite the movq to an leaq at link time if the symbol ends up in
266          // the same linkage unit.
267          if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
268            Type = macho::RIT_X86_64_GOTLoad;
269          else
270            Type = macho::RIT_X86_64_GOT;
271        }  else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
272          Type = macho::RIT_X86_64_TLV;
273        }  else if (Modifier != MCSymbolRefExpr::VK_None) {
274          report_fatal_error("unsupported symbol modifier in relocation");
275        } else {
276          Type = macho::RIT_X86_64_Signed;
277
278          // The Darwin x86_64 relocation format has a problem where it cannot
279          // encode an address (L<foo> + <constant>) which is outside the atom
280          // containing L<foo>. Generally, this shouldn't occur but it does
281          // happen when we have a RIPrel instruction with data following the
282          // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
283          // adjustment Darwin x86_64 uses, the offset is still negative and the
284          // linker has no way to recognize this.
285          //
286          // To work around this, Darwin uses several special relocation types
287          // to indicate the offsets. However, the specification or
288          // implementation of these seems to also be incomplete; they should
289          // adjust the addend as well based on the actual encoded instruction
290          // (the additional bias), but instead appear to just look at the final
291          // offset.
292          switch (-(Target.getConstant() + (1LL << Log2Size))) {
293          case 1: Type = macho::RIT_X86_64_Signed1; break;
294          case 2: Type = macho::RIT_X86_64_Signed2; break;
295          case 4: Type = macho::RIT_X86_64_Signed4; break;
296          }
297        }
298      } else {
299        if (Modifier != MCSymbolRefExpr::VK_None)
300          report_fatal_error("unsupported symbol modifier in branch "
301                             "relocation");
302
303        Type = macho::RIT_X86_64_Branch;
304      }
305    } else {
306      if (Modifier == MCSymbolRefExpr::VK_GOT) {
307        Type = macho::RIT_X86_64_GOT;
308      } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
309        // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which
310        // case all we do is set the PCrel bit in the relocation entry; this is
311        // used with exception handling, for example. The source is required to
312        // include any necessary offset directly.
313        Type = macho::RIT_X86_64_GOT;
314        IsPCRel = 1;
315      } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
316        report_fatal_error("TLVP symbol modifier should have been rip-rel");
317      } else if (Modifier != MCSymbolRefExpr::VK_None)
318        report_fatal_error("unsupported symbol modifier in relocation");
319      else
320        Type = macho::RIT_X86_64_Unsigned;
321    }
322  }
323
324  // x86_64 always writes custom values into the fixups.
325  FixedValue = Value;
326
327  // struct relocation_info (8 bytes)
328  macho::RelocationEntry MRE;
329  MRE.Word0 = FixupOffset;
330  MRE.Word1 = ((Index     <<  0) |
331               (IsPCRel   << 24) |
332               (Log2Size  << 25) |
333               (IsExtern  << 27) |
334               (Type      << 28));
335  Writer->addRelocation(Fragment->getParent(), MRE);
336}
337
338void 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  uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
347  unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
348  unsigned Type = macho::RIT_Vanilla;
349
350  // See <reloc.h>.
351  const MCSymbol *A = &Target.getSymA()->getSymbol();
352  MCSymbolData *A_SD = &Asm.getSymbolData(*A);
353
354  if (!A_SD->getFragment())
355    report_fatal_error("symbol '" + A->getName() +
356                       "' can not be undefined in a subtraction expression");
357
358  uint32_t Value = Writer->getSymbolAddress(A_SD, Layout);
359  uint64_t SecAddr = Writer->getSectionAddress(A_SD->getFragment()->getParent());
360  FixedValue += SecAddr;
361  uint32_t Value2 = 0;
362
363  if (const MCSymbolRefExpr *B = Target.getSymB()) {
364    MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
365
366    if (!B_SD->getFragment())
367      report_fatal_error("symbol '" + B->getSymbol().getName() +
368                         "' can not be undefined in a subtraction expression");
369
370    // Select the appropriate difference relocation type.
371    //
372    // Note that there is no longer any semantic difference between these two
373    // relocation types from the linkers point of view, this is done solely for
374    // pedantic compatibility with 'as'.
375    Type = A_SD->isExternal() ? (unsigned)macho::RIT_Difference :
376      (unsigned)macho::RIT_Generic_LocalDifference;
377    Value2 = Writer->getSymbolAddress(B_SD, Layout);
378    FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
379  }
380
381  // Relocations are written out in reverse order, so the PAIR comes first.
382  if (Type == macho::RIT_Difference ||
383      Type == macho::RIT_Generic_LocalDifference) {
384    macho::RelocationEntry MRE;
385    MRE.Word0 = ((0         <<  0) |
386                 (macho::RIT_Pair  << 24) |
387                 (Log2Size  << 28) |
388                 (IsPCRel   << 30) |
389                 macho::RF_Scattered);
390    MRE.Word1 = Value2;
391    Writer->addRelocation(Fragment->getParent(), MRE);
392  }
393
394  macho::RelocationEntry MRE;
395  MRE.Word0 = ((FixupOffset <<  0) |
396               (Type        << 24) |
397               (Log2Size    << 28) |
398               (IsPCRel     << 30) |
399               macho::RF_Scattered);
400  MRE.Word1 = Value;
401  Writer->addRelocation(Fragment->getParent(), MRE);
402}
403
404void X86MachObjectWriter::RecordTLVPRelocation(MachObjectWriter *Writer,
405                                               const MCAssembler &Asm,
406                                               const MCAsmLayout &Layout,
407                                               const MCFragment *Fragment,
408                                               const MCFixup &Fixup,
409                                               MCValue Target,
410                                               uint64_t &FixedValue) {
411  assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
412         !is64Bit() &&
413         "Should only be called with a 32-bit TLVP relocation!");
414
415  unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
416  uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
417  unsigned IsPCRel = 0;
418
419  // Get the symbol data.
420  MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
421  unsigned Index = SD_A->getIndex();
422
423  // We're only going to have a second symbol in pic mode and it'll be a
424  // subtraction from the picbase. For 32-bit pic the addend is the difference
425  // between the picbase and the next address.  For 32-bit static the addend is
426  // zero.
427  if (Target.getSymB()) {
428    // If this is a subtraction then we're pcrel.
429    uint32_t FixupAddress =
430      Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
431    MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol());
432    IsPCRel = 1;
433    FixedValue = (FixupAddress - Writer->getSymbolAddress(SD_B, Layout) +
434                  Target.getConstant());
435    FixedValue += 1ULL << Log2Size;
436  } else {
437    FixedValue = 0;
438  }
439
440  // struct relocation_info (8 bytes)
441  macho::RelocationEntry MRE;
442  MRE.Word0 = Value;
443  MRE.Word1 = ((Index                  <<  0) |
444               (IsPCRel                << 24) |
445               (Log2Size               << 25) |
446               (1                      << 27) | // Extern
447               (macho::RIT_Generic_TLV << 28)); // Type
448  Writer->addRelocation(Fragment->getParent(), MRE);
449}
450
451void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
452                                              const MCAssembler &Asm,
453                                              const MCAsmLayout &Layout,
454                                              const MCFragment *Fragment,
455                                              const MCFixup &Fixup,
456                                              MCValue Target,
457                                              uint64_t &FixedValue) {
458  unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
459  unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
460
461  // If this is a 32-bit TLVP reloc it's handled a bit differently.
462  if (Target.getSymA() &&
463      Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
464    RecordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
465                         FixedValue);
466    return;
467  }
468
469  // If this is a difference or a defined symbol plus an offset, then we need a
470  // scattered relocation entry. Differences always require scattered
471  // relocations.
472  if (Target.getSymB())
473    return RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
474                                     Target, Log2Size, FixedValue);
475
476  // Get the symbol data, if any.
477  MCSymbolData *SD = 0;
478  if (Target.getSymA())
479    SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
480
481  // If this is an internal relocation with an offset, it also needs a scattered
482  // relocation entry.
483  uint32_t Offset = Target.getConstant();
484  if (IsPCRel)
485    Offset += 1 << Log2Size;
486  if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD))
487    return RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
488                                     Target, Log2Size, FixedValue);
489
490  // See <reloc.h>.
491  uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
492  unsigned Index = 0;
493  unsigned IsExtern = 0;
494  unsigned Type = 0;
495
496  if (Target.isAbsolute()) { // constant
497    // SymbolNum of 0 indicates the absolute section.
498    //
499    // FIXME: Currently, these are never generated (see code below). I cannot
500    // find a case where they are actually emitted.
501    Type = macho::RIT_Vanilla;
502  } else {
503    // Resolve constant variables.
504    if (SD->getSymbol().isVariable()) {
505      int64_t Res;
506      if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute(
507            Res, Layout, Writer->getSectionAddressMap())) {
508        FixedValue = Res;
509        return;
510      }
511    }
512
513    // Check whether we need an external or internal relocation.
514    if (Writer->doesSymbolRequireExternRelocation(SD)) {
515      IsExtern = 1;
516      Index = SD->getIndex();
517      // For external relocations, make sure to offset the fixup value to
518      // compensate for the addend of the symbol address, if it was
519      // undefined. This occurs with weak definitions, for example.
520      if (!SD->Symbol->isUndefined())
521        FixedValue -= Layout.getSymbolOffset(SD);
522    } else {
523      // The index is the section ordinal (1-based).
524      const MCSectionData &SymSD = Asm.getSectionData(
525        SD->getSymbol().getSection());
526      Index = SymSD.getOrdinal() + 1;
527      FixedValue += Writer->getSectionAddress(&SymSD);
528    }
529    if (IsPCRel)
530      FixedValue -= Writer->getSectionAddress(Fragment->getParent());
531
532    Type = macho::RIT_Vanilla;
533  }
534
535  // struct relocation_info (8 bytes)
536  macho::RelocationEntry MRE;
537  MRE.Word0 = FixupOffset;
538  MRE.Word1 = ((Index     <<  0) |
539               (IsPCRel   << 24) |
540               (Log2Size  << 25) |
541               (IsExtern  << 27) |
542               (Type      << 28));
543  Writer->addRelocation(Fragment->getParent(), MRE);
544}
545
546MCObjectWriter *llvm::createX86MachObjectWriter(raw_ostream &OS,
547                                                bool Is64Bit,
548                                                uint32_t CPUType,
549                                                uint32_t CPUSubtype) {
550  return createMachObjectWriter(new X86MachObjectWriter(Is64Bit,
551                                                        CPUType,
552                                                        CPUSubtype),
553                                OS, /*IsLittleEndian=*/true);
554}
555