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