1 //===-- X86MachObjectWriter.cpp - X86 Mach-O Writer -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "X86FixupKinds.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCMachObjectWriter.h"
16 #include "llvm/MC/MCSectionMachO.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Object/MachOFormat.h"
22 using namespace llvm::object
;
25 class X86MachObjectWriter
: public MCMachObjectTargetWriter
{
26 void RecordScatteredRelocation(MachObjectWriter
*Writer
,
27 const MCAssembler
&Asm
,
28 const MCAsmLayout
&Layout
,
29 const MCFragment
*Fragment
,
33 uint64_t &FixedValue
);
34 void RecordTLVPRelocation(MachObjectWriter
*Writer
,
35 const MCAssembler
&Asm
,
36 const MCAsmLayout
&Layout
,
37 const MCFragment
*Fragment
,
40 uint64_t &FixedValue
);
42 void RecordX86Relocation(MachObjectWriter
*Writer
,
43 const MCAssembler
&Asm
,
44 const MCAsmLayout
&Layout
,
45 const MCFragment
*Fragment
,
48 uint64_t &FixedValue
);
49 void RecordX86_64Relocation(MachObjectWriter
*Writer
,
50 const MCAssembler
&Asm
,
51 const MCAsmLayout
&Layout
,
52 const MCFragment
*Fragment
,
55 uint64_t &FixedValue
);
57 X86MachObjectWriter(bool Is64Bit
, uint32_t CPUType
,
59 : MCMachObjectTargetWriter(Is64Bit
, CPUType
, CPUSubtype
,
60 /*UseAggressiveSymbolFolding=*/Is64Bit
) {}
62 void RecordRelocation(MachObjectWriter
*Writer
,
63 const MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
64 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
65 MCValue Target
, uint64_t &FixedValue
) {
66 if (Writer
->is64Bit())
67 RecordX86_64Relocation(Writer
, Asm
, Layout
, Fragment
, Fixup
, Target
,
70 RecordX86Relocation(Writer
, Asm
, Layout
, Fragment
, Fixup
, Target
,
76 static bool isFixupKindRIPRel(unsigned Kind
) {
77 return Kind
== X86::reloc_riprel_4byte
||
78 Kind
== X86::reloc_riprel_4byte_movq_load
;
81 static unsigned getFixupKindLog2Size(unsigned Kind
) {
84 llvm_unreachable("invalid fixup kind!");
86 case FK_Data_1
: return 0;
88 case FK_Data_2
: return 1;
90 // FIXME: Remove these!!!
91 case X86::reloc_riprel_4byte
:
92 case X86::reloc_riprel_4byte_movq_load
:
93 case X86::reloc_signed_4byte
:
94 case FK_Data_4
: return 2;
95 case FK_Data_8
: return 3;
99 void X86MachObjectWriter::RecordX86_64Relocation(MachObjectWriter
*Writer
,
100 const MCAssembler
&Asm
,
101 const MCAsmLayout
&Layout
,
102 const MCFragment
*Fragment
,
103 const MCFixup
&Fixup
,
105 uint64_t &FixedValue
) {
106 unsigned IsPCRel
= Writer
->isFixupKindPCRel(Asm
, Fixup
.getKind());
107 unsigned IsRIPRel
= isFixupKindRIPRel(Fixup
.getKind());
108 unsigned Log2Size
= getFixupKindLog2Size(Fixup
.getKind());
111 uint32_t FixupOffset
=
112 Layout
.getFragmentOffset(Fragment
) + Fixup
.getOffset();
113 uint32_t FixupAddress
=
114 Writer
->getFragmentAddress(Fragment
, Layout
) + Fixup
.getOffset();
117 unsigned IsExtern
= 0;
120 Value
= Target
.getConstant();
123 // Compensate for the relocation offset, Darwin x86_64 relocations only have
124 // the addend and appear to have attempted to define it to be the actual
125 // expression addend without the PCrel bias. However, instructions with data
126 // following the relocation are not accommodated for (see comment below
127 // regarding SIGNED{1,2,4}), so it isn't exactly that either.
128 Value
+= 1LL << Log2Size
;
131 if (Target
.isAbsolute()) { // constant
132 // SymbolNum of 0 indicates the absolute section.
133 Type
= macho::RIT_X86_64_Unsigned
;
136 // FIXME: I believe this is broken, I don't think the linker can understand
137 // it. I think it would require a local relocation, but I'm not sure if that
138 // would work either. The official way to get an absolute PCrel relocation
139 // is to use an absolute symbol (which we don't support yet).
142 Type
= macho::RIT_X86_64_Branch
;
144 } else if (Target
.getSymB()) { // A - B + constant
145 const MCSymbol
*A
= &Target
.getSymA()->getSymbol();
146 MCSymbolData
&A_SD
= Asm
.getSymbolData(*A
);
147 const MCSymbolData
*A_Base
= Asm
.getAtom(&A_SD
);
149 const MCSymbol
*B
= &Target
.getSymB()->getSymbol();
150 MCSymbolData
&B_SD
= Asm
.getSymbolData(*B
);
151 const MCSymbolData
*B_Base
= Asm
.getAtom(&B_SD
);
153 // Neither symbol can be modified.
154 if (Target
.getSymA()->getKind() != MCSymbolRefExpr::VK_None
||
155 Target
.getSymB()->getKind() != MCSymbolRefExpr::VK_None
)
156 report_fatal_error("unsupported relocation of modified symbol");
158 // We don't support PCrel relocations of differences. Darwin 'as' doesn't
159 // implement most of these correctly.
161 report_fatal_error("unsupported pc-relative relocation of difference");
163 // The support for the situation where one or both of the symbols would
164 // require a local relocation is handled just like if the symbols were
165 // external. This is certainly used in the case of debug sections where the
166 // section has only temporary symbols and thus the symbols don't have base
167 // symbols. This is encoded using the section ordinal and non-extern
168 // relocation entries.
170 // Darwin 'as' doesn't emit correct relocations for this (it ends up with a
171 // single SIGNED relocation); reject it for now. Except the case where both
172 // symbols don't have a base, equal but both NULL.
173 if (A_Base
== B_Base
&& A_Base
)
174 report_fatal_error("unsupported relocation with identical base");
176 Value
+= Writer
->getSymbolAddress(&A_SD
, Layout
) -
177 (A_Base
== NULL
? 0 : Writer
->getSymbolAddress(A_Base
, Layout
));
178 Value
-= Writer
->getSymbolAddress(&B_SD
, Layout
) -
179 (B_Base
== NULL
? 0 : Writer
->getSymbolAddress(B_Base
, Layout
));
182 Index
= A_Base
->getIndex();
186 Index
= A_SD
.getFragment()->getParent()->getOrdinal() + 1;
189 Type
= macho::RIT_X86_64_Unsigned
;
191 macho::RelocationEntry MRE
;
192 MRE
.Word0
= FixupOffset
;
193 MRE
.Word1
= ((Index
<< 0) |
198 Writer
->addRelocation(Fragment
->getParent(), MRE
);
201 Index
= B_Base
->getIndex();
205 Index
= B_SD
.getFragment()->getParent()->getOrdinal() + 1;
208 Type
= macho::RIT_X86_64_Subtractor
;
210 const MCSymbol
*Symbol
= &Target
.getSymA()->getSymbol();
211 MCSymbolData
&SD
= Asm
.getSymbolData(*Symbol
);
212 const MCSymbolData
*Base
= Asm
.getAtom(&SD
);
214 // Relocations inside debug sections always use local relocations when
215 // possible. This seems to be done because the debugger doesn't fully
216 // understand x86_64 relocation entries, and expects to find values that
217 // have already been fixed up.
218 if (Symbol
->isInSection()) {
219 const MCSectionMachO
&Section
= static_cast<const MCSectionMachO
&>(
220 Fragment
->getParent()->getSection());
221 if (Section
.hasAttribute(MCSectionMachO::S_ATTR_DEBUG
))
225 // x86_64 almost always uses external relocations, except when there is no
226 // symbol to use as a base address (a local symbol with no preceding
227 // non-local symbol).
229 Index
= Base
->getIndex();
232 // Add the local offset, if needed.
234 Value
+= Layout
.getSymbolOffset(&SD
) - Layout
.getSymbolOffset(Base
);
235 } else if (Symbol
->isInSection() && !Symbol
->isVariable()) {
236 // The index is the section ordinal (1-based).
237 Index
= SD
.getFragment()->getParent()->getOrdinal() + 1;
239 Value
+= Writer
->getSymbolAddress(&SD
, Layout
);
242 Value
-= FixupAddress
+ (1 << Log2Size
);
243 } else if (Symbol
->isVariable()) {
244 const MCExpr
*Value
= Symbol
->getVariableValue();
246 bool isAbs
= Value
->EvaluateAsAbsolute(Res
, Layout
,
247 Writer
->getSectionAddressMap());
252 report_fatal_error("unsupported relocation of variable '" +
253 Symbol
->getName() + "'");
256 report_fatal_error("unsupported relocation of undefined symbol '" +
257 Symbol
->getName() + "'");
260 MCSymbolRefExpr::VariantKind Modifier
= Target
.getSymA()->getKind();
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
;
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");
276 Type
= macho::RIT_X86_64_Signed
;
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.
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
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;
299 if (Modifier
!= MCSymbolRefExpr::VK_None
)
300 report_fatal_error("unsupported symbol modifier in branch "
303 Type
= macho::RIT_X86_64_Branch
;
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
;
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");
320 Type
= macho::RIT_X86_64_Unsigned
;
324 // x86_64 always writes custom values into the fixups.
327 // struct relocation_info (8 bytes)
328 macho::RelocationEntry MRE
;
329 MRE
.Word0
= FixupOffset
;
330 MRE
.Word1
= ((Index
<< 0) |
335 Writer
->addRelocation(Fragment
->getParent(), MRE
);
338 void X86MachObjectWriter::RecordScatteredRelocation(MachObjectWriter
*Writer
,
339 const MCAssembler
&Asm
,
340 const MCAsmLayout
&Layout
,
341 const MCFragment
*Fragment
,
342 const MCFixup
&Fixup
,
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
;
351 const MCSymbol
*A
= &Target
.getSymA()->getSymbol();
352 MCSymbolData
*A_SD
= &Asm
.getSymbolData(*A
);
354 if (!A_SD
->getFragment())
355 report_fatal_error("symbol '" + A
->getName() +
356 "' can not be undefined in a subtraction expression");
358 uint32_t Value
= Writer
->getSymbolAddress(A_SD
, Layout
);
359 uint64_t SecAddr
= Writer
->getSectionAddress(A_SD
->getFragment()->getParent());
360 FixedValue
+= SecAddr
;
363 if (const MCSymbolRefExpr
*B
= Target
.getSymB()) {
364 MCSymbolData
*B_SD
= &Asm
.getSymbolData(B
->getSymbol());
366 if (!B_SD
->getFragment())
367 report_fatal_error("symbol '" + B
->getSymbol().getName() +
368 "' can not be undefined in a subtraction expression");
370 // Select the appropriate difference relocation type.
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());
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) |
389 macho::RF_Scattered
);
391 Writer
->addRelocation(Fragment
->getParent(), MRE
);
394 macho::RelocationEntry MRE
;
395 MRE
.Word0
= ((FixupOffset
<< 0) |
399 macho::RF_Scattered
);
401 Writer
->addRelocation(Fragment
->getParent(), MRE
);
404 void X86MachObjectWriter::RecordTLVPRelocation(MachObjectWriter
*Writer
,
405 const MCAssembler
&Asm
,
406 const MCAsmLayout
&Layout
,
407 const MCFragment
*Fragment
,
408 const MCFixup
&Fixup
,
410 uint64_t &FixedValue
) {
411 assert(Target
.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP
&&
413 "Should only be called with a 32-bit TLVP relocation!");
415 unsigned Log2Size
= getFixupKindLog2Size(Fixup
.getKind());
416 uint32_t Value
= Layout
.getFragmentOffset(Fragment
)+Fixup
.getOffset();
417 unsigned IsPCRel
= 0;
419 // Get the symbol data.
420 MCSymbolData
*SD_A
= &Asm
.getSymbolData(Target
.getSymA()->getSymbol());
421 unsigned Index
= SD_A
->getIndex();
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
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());
433 FixedValue
= (FixupAddress
- Writer
->getSymbolAddress(SD_B
, Layout
) +
434 Target
.getConstant());
435 FixedValue
+= 1ULL << Log2Size
;
440 // struct relocation_info (8 bytes)
441 macho::RelocationEntry MRE
;
443 MRE
.Word1
= ((Index
<< 0) |
446 (1 << 27) | // Extern
447 (macho::RIT_Generic_TLV
<< 28)); // Type
448 Writer
->addRelocation(Fragment
->getParent(), MRE
);
451 void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter
*Writer
,
452 const MCAssembler
&Asm
,
453 const MCAsmLayout
&Layout
,
454 const MCFragment
*Fragment
,
455 const MCFixup
&Fixup
,
457 uint64_t &FixedValue
) {
458 unsigned IsPCRel
= Writer
->isFixupKindPCRel(Asm
, Fixup
.getKind());
459 unsigned Log2Size
= getFixupKindLog2Size(Fixup
.getKind());
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
,
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
472 if (Target
.getSymB())
473 return RecordScatteredRelocation(Writer
, Asm
, Layout
, Fragment
, Fixup
,
474 Target
, Log2Size
, FixedValue
);
476 // Get the symbol data, if any.
477 MCSymbolData
*SD
= 0;
478 if (Target
.getSymA())
479 SD
= &Asm
.getSymbolData(Target
.getSymA()->getSymbol());
481 // If this is an internal relocation with an offset, it also needs a scattered
483 uint32_t Offset
= Target
.getConstant();
485 Offset
+= 1 << Log2Size
;
486 if (Offset
&& SD
&& !Writer
->doesSymbolRequireExternRelocation(SD
))
487 return RecordScatteredRelocation(Writer
, Asm
, Layout
, Fragment
, Fixup
,
488 Target
, Log2Size
, FixedValue
);
491 uint32_t FixupOffset
= Layout
.getFragmentOffset(Fragment
)+Fixup
.getOffset();
493 unsigned IsExtern
= 0;
496 if (Target
.isAbsolute()) { // constant
497 // SymbolNum of 0 indicates the absolute section.
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
;
503 // Resolve constant variables.
504 if (SD
->getSymbol().isVariable()) {
506 if (SD
->getSymbol().getVariableValue()->EvaluateAsAbsolute(
507 Res
, Layout
, Writer
->getSectionAddressMap())) {
513 // Check whether we need an external or internal relocation.
514 if (Writer
->doesSymbolRequireExternRelocation(SD
)) {
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
);
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
);
530 FixedValue
-= Writer
->getSectionAddress(Fragment
->getParent());
532 Type
= macho::RIT_Vanilla
;
535 // struct relocation_info (8 bytes)
536 macho::RelocationEntry MRE
;
537 MRE
.Word0
= FixupOffset
;
538 MRE
.Word1
= ((Index
<< 0) |
543 Writer
->addRelocation(Fragment
->getParent(), MRE
);
546 MCObjectWriter
*llvm::createX86MachObjectWriter(raw_ostream
&OS
,
549 uint32_t CPUSubtype
) {
550 return createMachObjectWriter(new X86MachObjectWriter(Is64Bit
,
553 OS
, /*IsLittleEndian=*/true);