blob: 9e801fc8f1916c185981d81c6b680c8e6c5668fc [file] [log] [blame]
Daniel Dunbar7da045e2010-12-20 15:07:39 +00001//===-- 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
Evan Chengb2531002011-07-25 19:33:48 +000010#include "MCTargetDesc/X86MCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "MCTargetDesc/X86FixupKinds.h"
12#include "llvm/ADT/Twine.h"
Rafael Espindola26585542015-01-19 21:11:14 +000013#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000014#include "llvm/MC/MCAsmLayout.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAssembler.h"
Jim Grosbachc03a0c22012-09-26 21:27:45 +000016#include "llvm/MC/MCContext.h"
Daniel Dunbar7da045e2010-12-20 15:07:39 +000017#include "llvm/MC/MCMachObjectWriter.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000018#include "llvm/MC/MCSectionMachO.h"
19#include "llvm/MC/MCValue.h"
20#include "llvm/Support/ErrorHandling.h"
Jim Grosbachc03a0c22012-09-26 21:27:45 +000021#include "llvm/Support/Format.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000022#include "llvm/Support/MachO.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000023
Daniel Dunbar7da045e2010-12-20 15:07:39 +000024using namespace llvm;
25
26namespace {
27class X86MachObjectWriter : public MCMachObjectTargetWriter {
Jim Grosbach56ed0bb2015-06-04 23:25:54 +000028 bool recordScatteredRelocation(MachObjectWriter *Writer,
Jim Grosbach28fcafb2011-06-24 23:44:37 +000029 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);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +000036 void recordTLVPRelocation(MachObjectWriter *Writer,
Jim Grosbach28fcafb2011-06-24 23:44:37 +000037 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);
Rafael Espindola26585542015-01-19 21:11:14 +000051 void RecordX86_64Relocation(MachObjectWriter *Writer, MCAssembler &Asm,
Jim Grosbach28fcafb2011-06-24 23:44:37 +000052 const MCAsmLayout &Layout,
Rafael Espindola26585542015-01-19 21:11:14 +000053 const MCFragment *Fragment, const MCFixup &Fixup,
54 MCValue Target, uint64_t &FixedValue);
55
Daniel Dunbar7da045e2010-12-20 15:07:39 +000056public:
Jim Grosbach7c76b4c2015-06-04 20:27:42 +000057 X86MachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
58 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000059
Jim Grosbach36e60e92015-06-04 22:24:41 +000060 void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
Rafael Espindola26585542015-01-19 21:11:14 +000061 const MCAsmLayout &Layout, const MCFragment *Fragment,
62 const MCFixup &Fixup, MCValue Target,
63 uint64_t &FixedValue) override {
Jim Grosbach28fcafb2011-06-24 23:44:37 +000064 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 }
Daniel Dunbar7da045e2010-12-20 15:07:39 +000071};
72}
73
Jim Grosbach28fcafb2011-06-24 23:44:37 +000074static bool isFixupKindRIPRel(unsigned Kind) {
75 return Kind == X86::reloc_riprel_4byte ||
76 Kind == X86::reloc_riprel_4byte_movq_load;
77}
78
79static 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
Rafael Espindola26585542015-01-19 21:11:14 +000097void X86MachObjectWriter::RecordX86_64Relocation(
98 MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
99 const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
100 uint64_t &FixedValue) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000101 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;
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000114 const MCSymbol *RelSymbol = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000115
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.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000129 Type = MachO::X86_64_RELOC_UNSIGNED;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000130
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;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000137 Type = MachO::X86_64_RELOC_BRANCH;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000138 }
139 } else if (Target.getSymB()) { // A - B + constant
140 const MCSymbol *A = &Target.getSymA()->getSymbol();
Kevin Enderby09cdb432013-09-05 20:25:06 +0000141 if (A->isTemporary())
Rafael Espindola7f4e07b2015-04-17 12:28:43 +0000142 A = &Writer->findAliasedSymbol(*A);
Duncan P. N. Exon Smithfd27a1d2015-05-20 16:02:11 +0000143 const MCSymbol *A_Base = Asm.getAtom(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000144
145 const MCSymbol *B = &Target.getSymB()->getSymbol();
Kevin Enderby09cdb432013-09-05 20:25:06 +0000146 if (B->isTemporary())
Rafael Espindola7f4e07b2015-04-17 12:28:43 +0000147 B = &Writer->findAliasedSymbol(*B);
Duncan P. N. Exon Smithfd27a1d2015-05-20 16:02:11 +0000148 const MCSymbol *B_Base = Asm.getAtom(*B);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000149
150 // Neither symbol can be modified.
151 if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
152 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000153 report_fatal_error("unsupported relocation of modified symbol", false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000154
155 // We don't support PCrel relocations of differences. Darwin 'as' doesn't
156 // implement most of these correctly.
157 if (IsPCRel)
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000158 report_fatal_error("unsupported pc-relative relocation of difference",
159 false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000160
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)
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000172 report_fatal_error("unsupported relocation with identical base", false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000173
Kevin Enderby2813f492014-10-24 22:39:40 +0000174 // A subtraction expression where either symbol is undefined is a
Kevin Enderbyb03f3fe2013-08-12 22:45:44 +0000175 // non-relocatable expression.
Kevin Enderby2813f492014-10-24 22:39:40 +0000176 if (A->isUndefined() || B->isUndefined()) {
177 StringRef Name = A->isUndefined() ? A->getName() : B->getName();
Jim Grosbach6f482002015-05-18 18:43:14 +0000178 Asm.getContext().reportFatalError(Fixup.getLoc(),
Michael Liao5bf95782014-12-04 05:20:33 +0000179 "unsupported relocation with subtraction expression, symbol '" +
Kevin Enderby2813f492014-10-24 22:39:40 +0000180 Name + "' can not be undefined in a subtraction expression");
181 }
Kevin Enderbyb03f3fe2013-08-12 22:45:44 +0000182
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000183 Value += Writer->getSymbolAddress(*A, Layout) -
184 (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Layout));
185 Value -= Writer->getSymbolAddress(*B, Layout) -
186 (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Layout));
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000187
Rafael Espindola26585542015-01-19 21:11:14 +0000188 if (!A_Base)
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000189 Index = A->getFragment()->getParent()->getOrdinal() + 1;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000190 Type = MachO::X86_64_RELOC_UNSIGNED;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000191
Charles Davis8bdfafd2013-09-01 04:28:48 +0000192 MachO::any_relocation_info MRE;
193 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000194 MRE.r_word1 =
195 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Rafael Espindola61e724a2015-05-26 01:15:30 +0000196 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000197
Rafael Espindola26585542015-01-19 21:11:14 +0000198 if (B_Base)
199 RelSymbol = B_Base;
200 else
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000201 Index = B->getFragment()->getParent()->getOrdinal() + 1;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000202 Type = MachO::X86_64_RELOC_SUBTRACTOR;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000203 } else {
204 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Rafael Espindola26585542015-01-19 21:11:14 +0000205 if (Symbol->isTemporary() && Value) {
206 const MCSection &Sec = Symbol->getSection();
207 if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
Rafael Espindoladfe2d352015-06-17 20:08:20 +0000208 Symbol->setUsedInReloc();
Rafael Espindola26585542015-01-19 21:11:14 +0000209 }
Duncan P. N. Exon Smithfd27a1d2015-05-20 16:02:11 +0000210 RelSymbol = Asm.getAtom(*Symbol);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000211
212 // Relocations inside debug sections always use local relocations when
213 // possible. This seems to be done because the debugger doesn't fully
214 // understand x86_64 relocation entries, and expects to find values that
215 // have already been fixed up.
216 if (Symbol->isInSection()) {
Rafael Espindola7549f872015-05-26 00:36:57 +0000217 const MCSectionMachO &Section =
218 static_cast<const MCSectionMachO &>(*Fragment->getParent());
David Majnemer7b583052014-03-07 07:36:05 +0000219 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
Rafael Espindola26585542015-01-19 21:11:14 +0000220 RelSymbol = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000221 }
222
223 // x86_64 almost always uses external relocations, except when there is no
224 // symbol to use as a base address (a local symbol with no preceding
225 // non-local symbol).
Rafael Espindola26585542015-01-19 21:11:14 +0000226 if (RelSymbol) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000227 // Add the local offset, if needed.
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000228 if (RelSymbol != Symbol)
229 Value += Layout.getSymbolOffset(*Symbol) -
230 Layout.getSymbolOffset(*RelSymbol);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000231 } else if (Symbol->isInSection() && !Symbol->isVariable()) {
232 // The index is the section ordinal (1-based).
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000233 Index = Symbol->getFragment()->getParent()->getOrdinal() + 1;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000234 Value += Writer->getSymbolAddress(*Symbol, Layout);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000235
236 if (IsPCRel)
237 Value -= FixupAddress + (1 << Log2Size);
238 } else if (Symbol->isVariable()) {
239 const MCExpr *Value = Symbol->getVariableValue();
240 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000241 bool isAbs = Value->evaluateAsAbsolute(Res, Layout,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000242 Writer->getSectionAddressMap());
243 if (isAbs) {
244 FixedValue = Res;
245 return;
246 } else {
247 report_fatal_error("unsupported relocation of variable '" +
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000248 Symbol->getName() + "'", false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000249 }
250 } else {
251 report_fatal_error("unsupported relocation of undefined symbol '" +
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000252 Symbol->getName() + "'", false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000253 }
254
255 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
256 if (IsPCRel) {
257 if (IsRIPRel) {
258 if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
259 // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
260 // rewrite the movq to an leaq at link time if the symbol ends up in
261 // the same linkage unit.
262 if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 Type = MachO::X86_64_RELOC_GOT_LOAD;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000264 else
Charles Davis8bdfafd2013-09-01 04:28:48 +0000265 Type = MachO::X86_64_RELOC_GOT;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000266 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000267 Type = MachO::X86_64_RELOC_TLV;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000268 } else if (Modifier != MCSymbolRefExpr::VK_None) {
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000269 report_fatal_error("unsupported symbol modifier in relocation",
270 false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000271 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000272 Type = MachO::X86_64_RELOC_SIGNED;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000273
274 // The Darwin x86_64 relocation format has a problem where it cannot
275 // encode an address (L<foo> + <constant>) which is outside the atom
276 // containing L<foo>. Generally, this shouldn't occur but it does
277 // happen when we have a RIPrel instruction with data following the
278 // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
279 // adjustment Darwin x86_64 uses, the offset is still negative and the
280 // linker has no way to recognize this.
281 //
282 // To work around this, Darwin uses several special relocation types
283 // to indicate the offsets. However, the specification or
284 // implementation of these seems to also be incomplete; they should
285 // adjust the addend as well based on the actual encoded instruction
286 // (the additional bias), but instead appear to just look at the final
287 // offset.
288 switch (-(Target.getConstant() + (1LL << Log2Size))) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000289 case 1: Type = MachO::X86_64_RELOC_SIGNED_1; break;
290 case 2: Type = MachO::X86_64_RELOC_SIGNED_2; break;
291 case 4: Type = MachO::X86_64_RELOC_SIGNED_4; break;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000292 }
293 }
294 } else {
295 if (Modifier != MCSymbolRefExpr::VK_None)
296 report_fatal_error("unsupported symbol modifier in branch "
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000297 "relocation", false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000298
Charles Davis8bdfafd2013-09-01 04:28:48 +0000299 Type = MachO::X86_64_RELOC_BRANCH;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000300 }
301 } else {
302 if (Modifier == MCSymbolRefExpr::VK_GOT) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 Type = MachO::X86_64_RELOC_GOT;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000304 } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
305 // GOTPCREL is allowed as a modifier on non-PCrel instructions, in which
306 // case all we do is set the PCrel bit in the relocation entry; this is
307 // used with exception handling, for example. The source is required to
308 // include any necessary offset directly.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000309 Type = MachO::X86_64_RELOC_GOT;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000310 IsPCRel = 1;
311 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000312 report_fatal_error("TLVP symbol modifier should have been rip-rel",
313 false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000314 } else if (Modifier != MCSymbolRefExpr::VK_None)
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000315 report_fatal_error("unsupported symbol modifier in relocation", false);
Kevin Enderby74946752013-08-29 00:19:03 +0000316 else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000317 Type = MachO::X86_64_RELOC_UNSIGNED;
Kevin Enderby74946752013-08-29 00:19:03 +0000318 unsigned Kind = Fixup.getKind();
319 if (Kind == X86::reloc_signed_4byte)
320 report_fatal_error("32-bit absolute addressing is not supported in "
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000321 "64-bit mode", false);
Kevin Enderby74946752013-08-29 00:19:03 +0000322 }
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000323 }
324 }
325
326 // x86_64 always writes custom values into the fixups.
327 FixedValue = Value;
328
329 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000330 MachO::any_relocation_info MRE;
331 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000332 MRE.r_word1 = (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) |
333 (IsExtern << 27) | (Type << 28);
Rafael Espindola61e724a2015-05-26 01:15:30 +0000334 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000335}
336
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000337bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000338 const MCAssembler &Asm,
339 const MCAsmLayout &Layout,
340 const MCFragment *Fragment,
341 const MCFixup &Fixup,
342 MCValue Target,
343 unsigned Log2Size,
344 uint64_t &FixedValue) {
Kevin Enderby86496a42013-12-04 23:36:24 +0000345 uint64_t OriginalFixedValue = FixedValue;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000346 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
347 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Charles Davis8bdfafd2013-09-01 04:28:48 +0000348 unsigned Type = MachO::GENERIC_RELOC_VANILLA;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000349
350 // See <reloc.h>.
351 const MCSymbol *A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000352
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000353 if (!A->getFragment())
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000354 report_fatal_error("symbol '" + A->getName() +
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000355 "' can not be undefined in a subtraction expression",
356 false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000357
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000358 uint32_t Value = Writer->getSymbolAddress(*A, Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000359 uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000360 FixedValue += SecAddr;
361 uint32_t Value2 = 0;
362
363 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000364 const MCSymbol *SB = &B->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000365
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000366 if (!SB->getFragment())
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000367 report_fatal_error("symbol '" + B->getSymbol().getName() +
Jim Grosbach6c6b4252013-09-03 23:02:00 +0000368 "' can not be undefined in a subtraction expression",
369 false);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000370
371 // Select the appropriate difference relocation type.
372 //
373 // Note that there is no longer any semantic difference between these two
374 // relocation types from the linkers point of view, this is done solely for
375 // pedantic compatibility with 'as'.
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000376 Type = A->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF
377 : (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000378 Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000379 FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000380 }
381
382 // Relocations are written out in reverse order, so the PAIR comes first.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000383 if (Type == MachO::GENERIC_RELOC_SECTDIFF ||
384 Type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000385 // If the offset is too large to fit in a scattered relocation,
386 // we're hosed. It's an unfortunate limitation of the MachO format.
387 if (FixupOffset > 0xffffff) {
388 char Buffer[32];
389 format("0x%x", FixupOffset).print(Buffer, sizeof(Buffer));
Jim Grosbach6f482002015-05-18 18:43:14 +0000390 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000391 Twine("Section too large, can't encode "
392 "r_address (") + Buffer +
393 ") into 24 bits of scattered "
394 "relocation entry.");
395 llvm_unreachable("fatal error returned?!");
396 }
397
Charles Davis8bdfafd2013-09-01 04:28:48 +0000398 MachO::any_relocation_info MRE;
399 MRE.r_word0 = ((0 << 0) | // r_address
400 (MachO::GENERIC_RELOC_PAIR << 24) | // r_type
401 (Log2Size << 28) |
402 (IsPCRel << 30) |
403 MachO::R_SCATTERED);
404 MRE.r_word1 = Value2;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000405 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000406 } else {
407 // If the offset is more than 24-bits, it won't fit in a scattered
408 // relocation offset field, so we fall back to using a non-scattered
409 // relocation. This is a bit risky, as if the offset reaches out of
410 // the block and the linker is doing scattered loading on this
411 // symbol, things can go badly.
412 //
413 // Required for 'as' compatibility.
Kevin Enderby86496a42013-12-04 23:36:24 +0000414 if (FixupOffset > 0xffffff) {
415 FixedValue = OriginalFixedValue;
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000416 return false;
Kevin Enderby86496a42013-12-04 23:36:24 +0000417 }
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000418 }
419
Charles Davis8bdfafd2013-09-01 04:28:48 +0000420 MachO::any_relocation_info MRE;
421 MRE.r_word0 = ((FixupOffset << 0) |
422 (Type << 24) |
423 (Log2Size << 28) |
424 (IsPCRel << 30) |
425 MachO::R_SCATTERED);
426 MRE.r_word1 = Value;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000427 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000428 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000429}
430
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000431void X86MachObjectWriter::recordTLVPRelocation(MachObjectWriter *Writer,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000432 const MCAssembler &Asm,
433 const MCAsmLayout &Layout,
434 const MCFragment *Fragment,
435 const MCFixup &Fixup,
436 MCValue Target,
437 uint64_t &FixedValue) {
438 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
439 !is64Bit() &&
440 "Should only be called with a 32-bit TLVP relocation!");
441
442 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
443 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
444 unsigned IsPCRel = 0;
445
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000446 // We're only going to have a second symbol in pic mode and it'll be a
447 // subtraction from the picbase. For 32-bit pic the addend is the difference
448 // between the picbase and the next address. For 32-bit static the addend is
449 // zero.
450 if (Target.getSymB()) {
451 // If this is a subtraction then we're pcrel.
452 uint32_t FixupAddress =
453 Writer->getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000454 IsPCRel = 1;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000455 FixedValue =
456 FixupAddress -
457 Writer->getSymbolAddress(Target.getSymB()->getSymbol(), Layout) +
458 Target.getConstant();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000459 FixedValue += 1ULL << Log2Size;
460 } else {
461 FixedValue = 0;
462 }
463
464 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000465 MachO::any_relocation_info MRE;
466 MRE.r_word0 = Value;
Rafael Espindola26585542015-01-19 21:11:14 +0000467 MRE.r_word1 =
468 (IsPCRel << 24) | (Log2Size << 25) | (MachO::GENERIC_RELOC_TLV << 28);
Rafael Espindola61e724a2015-05-26 01:15:30 +0000469 Writer->addRelocation(&Target.getSymA()->getSymbol(), Fragment->getParent(),
470 MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000471}
472
473void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
474 const MCAssembler &Asm,
475 const MCAsmLayout &Layout,
476 const MCFragment *Fragment,
477 const MCFixup &Fixup,
478 MCValue Target,
479 uint64_t &FixedValue) {
480 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
481 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
482
483 // If this is a 32-bit TLVP reloc it's handled a bit differently.
484 if (Target.getSymA() &&
485 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000486 recordTLVPRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000487 FixedValue);
488 return;
489 }
490
491 // If this is a difference or a defined symbol plus an offset, then we need a
492 // scattered relocation entry. Differences always require scattered
493 // relocations.
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000494 if (Target.getSymB()) {
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000495 recordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000496 Target, Log2Size, FixedValue);
497 return;
498 }
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000499
500 // Get the symbol data, if any.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000501 const MCSymbol *A = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000502 if (Target.getSymA())
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000503 A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000504
505 // If this is an internal relocation with an offset, it also needs a scattered
506 // relocation entry.
507 uint32_t Offset = Target.getConstant();
508 if (IsPCRel)
509 Offset += 1 << Log2Size;
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000510 // Try to record the scattered relocation if needed. Fall back to non
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000511 // scattered if necessary (see comments in recordScatteredRelocation()
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000512 // for details).
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000513 if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A) &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000514 recordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000515 Log2Size, FixedValue))
Jim Grosbachc03a0c22012-09-26 21:27:45 +0000516 return;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000517
518 // See <reloc.h>.
519 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
520 unsigned Index = 0;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000521 unsigned Type = 0;
Duncan P. N. Exon Smith6e23e5a2015-05-16 01:14:19 +0000522 const MCSymbol *RelSymbol = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000523
524 if (Target.isAbsolute()) { // constant
525 // SymbolNum of 0 indicates the absolute section.
526 //
527 // FIXME: Currently, these are never generated (see code below). I cannot
528 // find a case where they are actually emitted.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000529 Type = MachO::GENERIC_RELOC_VANILLA;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000530 } else {
531 // Resolve constant variables.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000532 if (A->isVariable()) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000533 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000534 if (A->getVariableValue()->evaluateAsAbsolute(
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000535 Res, Layout, Writer->getSectionAddressMap())) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000536 FixedValue = Res;
537 return;
538 }
539 }
540
541 // Check whether we need an external or internal relocation.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000542 if (Writer->doesSymbolRequireExternRelocation(*A)) {
543 RelSymbol = A;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000544 // For external relocations, make sure to offset the fixup value to
545 // compensate for the addend of the symbol address, if it was
546 // undefined. This occurs with weak definitions, for example.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000547 if (!A->isUndefined())
548 FixedValue -= Layout.getSymbolOffset(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000549 } else {
550 // The index is the section ordinal (1-based).
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000551 const MCSection &Sec = A->getSection();
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000552 Index = Sec.getOrdinal() + 1;
Rafael Espindola079027e2015-05-26 00:52:18 +0000553 FixedValue += Writer->getSectionAddress(&Sec);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000554 }
555 if (IsPCRel)
Rafael Espindola079027e2015-05-26 00:52:18 +0000556 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000557
Charles Davis8bdfafd2013-09-01 04:28:48 +0000558 Type = MachO::GENERIC_RELOC_VANILLA;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000559 }
560
561 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000562 MachO::any_relocation_info MRE;
563 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000564 MRE.r_word1 =
565 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Rafael Espindola61e724a2015-05-26 01:15:30 +0000566 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000567}
568
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000569MCObjectWriter *llvm::createX86MachObjectWriter(raw_pwrite_stream &OS,
570 bool Is64Bit, uint32_t CPUType,
Daniel Dunbar7da045e2010-12-20 15:07:39 +0000571 uint32_t CPUSubtype) {
572 return createMachObjectWriter(new X86MachObjectWriter(Is64Bit,
573 CPUType,
574 CPUSubtype),
575 OS, /*IsLittleEndian=*/true);
576}