29 bool recordScatteredRelocation(MachObjectWriter *Writer,
30 const MCAssembler &Asm,
36 void recordTLVPRelocation(MachObjectWriter *Writer,
37 const MCAssembler &Asm,
43 void RecordX86Relocation(MachObjectWriter *Writer,
44 const MCAssembler &Asm,
49 void RecordX86_64Relocation(MachObjectWriter *Writer, MCAssembler &Asm,
50 const MCFragment *
Fragment,
const MCFixup &Fixup,
51 MCValue Target,
uint64_t &FixedValue);
54 X86MachObjectWriter(
bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
55 : MCMachObjectTargetWriter(Is64Bit,
CPUType, CPUSubtype) {}
57 void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
58 const MCFragment *
Fragment,
const MCFixup &Fixup,
59 MCValue Target,
uint64_t &FixedValue)
override {
61 RecordX86_64Relocation(Writer, Asm,
Fragment, Fixup, Target, FixedValue);
63 RecordX86Relocation(Writer, Asm,
Fragment, Fixup, Target, FixedValue);
100void X86MachObjectWriter::RecordX86_64Relocation(
103 unsigned IsPCRel =
Fixup.isPCRel();
108 uint32_t FixupOffset =
Asm.getFragmentOffset(*
Fragment) +
Fixup.getOffset();
109 uint32_t FixupAddress =
113 unsigned IsExtern = 0;
115 const MCSymbol *RelSymbol =
nullptr;
125 Value += 1LL << Log2Size;
128 if (
Target.isAbsolute()) {
140 }
else if (
Target.getSubSym()) {
142 if (
A->isTemporary())
147 if (
B->isTemporary())
152 if (
Target.getSpecifier()) {
161 "unsupported pc-relative relocation of difference");
175 if (A_Base == B_Base && A_Base) {
180 if (!
A->isUndefined())
183 if (!
B->isUndefined())
188 Index =
A->getFragment()->getParent()->getOrdinal() + 1;
191 MachO::any_relocation_info MRE;
194 (
Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (
Type << 28);
200 Index =
B->getFragment()->getParent()->getOrdinal() + 1;
205 const MCSection &Sec =
Symbol->getSection();
209 RelSymbol = Writer->
getAtom(*Symbol);
215 if (
Symbol->isInSection()) {
216 const MCSectionMachO &
Section =
217 static_cast<const MCSectionMachO &
>(*
Fragment->getParent());
227 if (RelSymbol != Symbol)
228 Value +=
Asm.getSymbolOffset(*Symbol) -
Asm.getSymbolOffset(*RelSymbol);
229 }
else if (
Symbol->isInSection() && !
Symbol->isVariable()) {
231 Index =
Symbol->getFragment()->getParent()->getOrdinal() + 1;
235 Value -= FixupAddress + (1 << Log2Size);
236 }
else if (
Symbol->isVariable()) {
241 "unsupported relocation of undefined symbol '" +
259 }
else if (Specifier) {
261 "unsupported symbol modifier in relocation");
280 switch (-(
Target.getConstant() + (1LL << Log2Size))) {
289 "unsupported symbol modifier in branch relocation");
307 "TLVP symbol modifier should have been rip-rel");
309 }
else if (Specifier) {
311 "unsupported symbol modifier in relocation");
318 "32-bit absolute addressing is not supported in 64-bit mode");
329 MachO::any_relocation_info MRE;
331 MRE.
r_word1 = (
Index << 0) | (IsPCRel << 24) | (Log2Size << 25) |
332 (IsExtern << 27) | (
Type << 28);
336bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
337 const MCAssembler &Asm,
339 const MCFixup &Fixup,
343 uint64_t OriginalFixedValue = FixedValue;
344 uint32_t FixupOffset =
Asm.getFragmentOffset(*
Fragment) +
Fixup.getOffset();
345 unsigned IsPCRel =
Fixup.isPCRel();
349 auto *
A =
static_cast<const MCSymbolMachO *
>(
Target.getAddSym());
350 if (!
A->getFragment()) {
352 "symbol '" +
A->getName() +
353 "' can not be undefined in a subtraction expression");
359 FixedValue += SecAddr;
362 if (
const MCSymbol *SB =
Target.getSubSym()) {
363 if (!SB->getFragment()) {
365 "symbol '" + SB->getName() +
366 "' can not be undefined in a subtraction expression");
386 if (FixupOffset > 0xffffff) {
388 format(
"0x%x", FixupOffset).snprint(Buffer,
sizeof(Buffer));
392 ") into 24 bits of scattered "
393 "relocation entry.");
397 MachO::any_relocation_info MRE;
413 if (FixupOffset > 0xffffff) {
414 FixedValue = OriginalFixedValue;
419 MachO::any_relocation_info MRE;
420 MRE.
r_word0 = ((FixupOffset << 0) |
430void X86MachObjectWriter::recordTLVPRelocation(MachObjectWriter *Writer,
431 const MCAssembler &Asm,
433 const MCFixup &Fixup,
438 "Should only be called with a 32-bit TLVP relocation!");
442 unsigned IsPCRel = 0;
448 if (
auto *SymB =
Target.getSubSym()) {
450 uint32_t FixupAddress =
455 FixedValue += 1ULL << Log2Size;
461 MachO::any_relocation_info MRE;
468void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
469 const MCAssembler &Asm,
471 const MCFixup &Fixup,
474 unsigned IsPCRel =
Fixup.isPCRel();
480 recordTLVPRelocation(Writer, Asm,
Fragment, Fixup, Target, FixedValue);
488 recordScatteredRelocation(Writer, Asm,
Fragment, Fixup, Target, Log2Size,
503 recordScatteredRelocation(Writer, Asm,
Fragment, Fixup, Target, Log2Size,
508 uint32_t FixupOffset =
Asm.getFragmentOffset(*
Fragment) +
Fixup.getOffset();
511 const MCSymbol *RelSymbol =
nullptr;
513 if (
Target.isAbsolute()) {
520 assert(
A &&
"Unknown symbol data");
523 if (
A->isVariable()) {
526 A->getVariableValue()->evaluateAsRelocatable(Val, &Asm);
546 if (!
A->isUndefined())
547 FixedValue -=
Asm.getSymbolOffset(*
A);
550 const MCSection &Sec =
A->getSection();
561 MachO::any_relocation_info MRE;
564 (
Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (
Type << 28);
568std::unique_ptr<MCObjectTargetWriter>
571 return std::make_unique<X86MachObjectWriter>(Is64Bit, CPUType, CPUSubtype);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error reportError(StringRef Message)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool is64Bit(const char *name)
static bool isFixupKindRIPRel(unsigned Kind)
static bool isSectionAtomizableBySymbols(const MCSection &Section)
True if the section is atomized using the symbols in it.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
unsigned getOrdinal() const
const MCSymbol * getAddSym() const
int64_t getConstant() const
const MCSymbol * getSubSym() const
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
uint64_t getFragmentAddress(const MCAssembler &Asm, const MCFragment *Fragment) const
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
uint64_t getSectionAddress(const MCSection *Sec) const
const MCSymbol * getAtom(const MCSymbol &S) const
uint64_t getSymbolAddress(const MCSymbol &S) const
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr uint32_t S_ATTR_DEBUG
A debug section.
@ GENERIC_RELOC_LOCAL_SECTDIFF
@ X86_64_RELOC_SUBTRACTOR
@ reloc_riprel_4byte_movq_load_rex2
@ reloc_signed_4byte_relax
@ reloc_branch_4byte_pcrel
@ reloc_riprel_4byte_relax
@ reloc_riprel_4byte_relax_evex
@ reloc_riprel_4byte_relax_rex
@ reloc_riprel_4byte_movq_load
@ reloc_riprel_4byte_relax_rex2
This is an optimization pass for GlobalISel generic memory operations.
static unsigned getFixupKindLog2Size(unsigned Kind)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
@ FK_Data_8
A eight-byte fixup.
@ FK_Data_1
A one-byte fixup.
@ FK_Data_4
A four-byte fixup.
@ FK_Data_2
A two-byte fixup.
std::unique_ptr< MCObjectTargetWriter > createX86MachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
Construct an X86 Mach-O object writer.