2014-05-24 20:50:23 +08:00
|
|
|
//===-- AArch64MachObjectWriter.cpp - ARM Mach Object Writer --------------===//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "MCTargetDesc/AArch64FixupKinds.h"
|
|
|
|
#include "MCTargetDesc/AArch64MCTargetDesc.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-01-20 05:11:14 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCFixup.h"
|
|
|
|
#include "llvm/MC/MCMachObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MachO.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2014-05-24 20:50:23 +08:00
|
|
|
class AArch64MachObjectWriter : public MCMachObjectTargetWriter {
|
|
|
|
bool getAArch64FixupKindMachOInfo(const MCFixup &Fixup, unsigned &RelocType,
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCSymbolRefExpr *Sym,
|
|
|
|
unsigned &Log2Size, const MCAssembler &Asm);
|
|
|
|
|
|
|
|
public:
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype)
|
2014-03-29 18:18:08 +08:00
|
|
|
: MCMachObjectTargetWriter(true /* is64Bit */, CPUType, CPUSubtype,
|
|
|
|
/*UseAggressiveSymbolFolding=*/true) {}
|
|
|
|
|
2015-01-20 05:11:14 +08:00
|
|
|
void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCAsmLayout &Layout, const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
2014-04-29 15:58:25 +08:00
|
|
|
uint64_t &FixedValue) override;
|
2014-03-29 18:18:08 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo(
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCFixup &Fixup, unsigned &RelocType, const MCSymbolRefExpr *Sym,
|
|
|
|
unsigned &Log2Size, const MCAssembler &Asm) {
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_UNSIGNED);
|
|
|
|
Log2Size = ~0U;
|
|
|
|
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case FK_Data_1:
|
|
|
|
Log2Size = llvm::Log2_32(1);
|
|
|
|
return true;
|
|
|
|
case FK_Data_2:
|
|
|
|
Log2Size = llvm::Log2_32(2);
|
|
|
|
return true;
|
|
|
|
case FK_Data_4:
|
|
|
|
Log2Size = llvm::Log2_32(4);
|
|
|
|
if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
|
|
|
|
return true;
|
|
|
|
case FK_Data_8:
|
|
|
|
Log2Size = llvm::Log2_32(8);
|
|
|
|
if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
|
|
|
|
return true;
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::fixup_aarch64_add_imm12:
|
|
|
|
case AArch64::fixup_aarch64_ldst_imm12_scale1:
|
|
|
|
case AArch64::fixup_aarch64_ldst_imm12_scale2:
|
|
|
|
case AArch64::fixup_aarch64_ldst_imm12_scale4:
|
|
|
|
case AArch64::fixup_aarch64_ldst_imm12_scale8:
|
|
|
|
case AArch64::fixup_aarch64_ldst_imm12_scale16:
|
2014-03-29 18:18:08 +08:00
|
|
|
Log2Size = llvm::Log2_32(4);
|
|
|
|
switch (Sym->getKind()) {
|
|
|
|
default:
|
2014-06-18 13:05:13 +08:00
|
|
|
llvm_unreachable("Unexpected symbol reference variant kind!");
|
2014-03-29 18:18:08 +08:00
|
|
|
case MCSymbolRefExpr::VK_PAGEOFF:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_PAGEOFF12);
|
|
|
|
return true;
|
|
|
|
case MCSymbolRefExpr::VK_GOTPAGEOFF:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12);
|
|
|
|
return true;
|
|
|
|
case MCSymbolRefExpr::VK_TLVPPAGEOFF:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12);
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::fixup_aarch64_pcrel_adrp_imm21:
|
2014-03-29 18:18:08 +08:00
|
|
|
Log2Size = llvm::Log2_32(4);
|
|
|
|
// This encompasses the relocation for the whole 21-bit value.
|
|
|
|
switch (Sym->getKind()) {
|
|
|
|
default:
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"ADR/ADRP relocations must be GOT relative");
|
|
|
|
case MCSymbolRefExpr::VK_PAGE:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_PAGE21);
|
|
|
|
return true;
|
|
|
|
case MCSymbolRefExpr::VK_GOTPAGE:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGE21);
|
|
|
|
return true;
|
|
|
|
case MCSymbolRefExpr::VK_TLVPPAGE:
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGE21);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::fixup_aarch64_pcrel_branch26:
|
|
|
|
case AArch64::fixup_aarch64_pcrel_call26:
|
2014-03-29 18:18:08 +08:00
|
|
|
Log2Size = llvm::Log2_32(4);
|
|
|
|
RelocType = unsigned(MachO::ARM64_RELOC_BRANCH26);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 05:11:14 +08:00
|
|
|
static bool canUseLocalRelocation(const MCSectionMachO &Section,
|
|
|
|
const MCSymbol &Symbol, unsigned Log2Size) {
|
|
|
|
// Debug info sections can use local relocations.
|
|
|
|
if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, only pointer sized relocations are supported.
|
|
|
|
if (Log2Size != 3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// But only if they don't point to a few forbidden sections.
|
|
|
|
if (!Symbol.isInSection())
|
|
|
|
return true;
|
|
|
|
const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
|
|
|
|
if (RefSec.getType() == MachO::S_CSTRING_LITERALS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (RefSec.getSegmentName() == "__DATA" &&
|
|
|
|
RefSec.getSectionName() == "__cfstring")
|
2015-02-13 07:11:59 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (RefSec.getSegmentName() == "__DATA" &&
|
|
|
|
RefSec.getSectionName() == "__objc_classrefs")
|
2015-01-20 05:11:14 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64MachObjectWriter::RecordRelocation(
|
2015-01-20 05:11:14 +08:00
|
|
|
MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
|
|
|
|
uint64_t &FixedValue) {
|
|
|
|
unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
|
|
|
|
|
|
|
|
// See <reloc.h>.
|
|
|
|
uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
|
|
|
|
unsigned Log2Size = 0;
|
|
|
|
int64_t Value = 0;
|
|
|
|
unsigned Index = 0;
|
|
|
|
unsigned Type = 0;
|
|
|
|
unsigned Kind = Fixup.getKind();
|
2015-01-20 05:11:14 +08:00
|
|
|
const MCSymbolData *RelSymbol = nullptr;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
FixupOffset += Fixup.getOffset();
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
// AArch64 pcrel relocation addends do not include the section offset.
|
2014-03-29 18:18:08 +08:00
|
|
|
if (IsPCRel)
|
|
|
|
FixedValue += FixupOffset;
|
|
|
|
|
|
|
|
// ADRP fixups use relocations for the whole symbol value and only
|
|
|
|
// put the addend in the instruction itself. Clear out any value the
|
|
|
|
// generic code figured out from the sybmol definition.
|
2014-05-24 20:50:23 +08:00
|
|
|
if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21)
|
2014-03-29 18:18:08 +08:00
|
|
|
FixedValue = 0;
|
|
|
|
|
|
|
|
// imm19 relocations are for conditional branches, which require
|
|
|
|
// assembler local symbols. If we got here, that's not what we have,
|
|
|
|
// so complain loudly.
|
2014-05-24 20:50:23 +08:00
|
|
|
if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
|
2014-03-29 18:18:08 +08:00
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"conditional branch requires assembler-local"
|
|
|
|
" label. '" +
|
|
|
|
Target.getSymA()->getSymbol().getName() +
|
|
|
|
"' is external.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 14-bit branch relocations should only target internal labels, and so
|
|
|
|
// should never get here.
|
2014-05-24 20:50:23 +08:00
|
|
|
if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
|
2014-03-29 18:18:08 +08:00
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"Invalid relocation on conditional branch!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
|
2014-03-29 18:18:08 +08:00
|
|
|
Asm)) {
|
2014-05-24 20:50:23 +08:00
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
|
2014-03-29 18:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value = Target.getConstant();
|
|
|
|
|
|
|
|
if (Target.isAbsolute()) { // constant
|
|
|
|
// FIXME: Should this always be extern?
|
|
|
|
// SymbolNum of 0 indicates the absolute section.
|
|
|
|
Type = MachO::ARM64_RELOC_UNSIGNED;
|
|
|
|
|
|
|
|
if (IsPCRel) {
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"PC relative absolute relocation!");
|
|
|
|
|
|
|
|
// FIXME: x86_64 sets the type to a branch reloc here. Should we do
|
|
|
|
// something similar?
|
|
|
|
}
|
|
|
|
} else if (Target.getSymB()) { // A - B + constant
|
|
|
|
const MCSymbol *A = &Target.getSymA()->getSymbol();
|
2014-04-25 00:59:40 +08:00
|
|
|
const MCSymbolData &A_SD = Asm.getSymbolData(*A);
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
|
|
|
|
|
|
|
|
const MCSymbol *B = &Target.getSymB()->getSymbol();
|
2014-04-25 00:59:40 +08:00
|
|
|
const MCSymbolData &B_SD = Asm.getSymbolData(*B);
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
|
|
|
|
|
|
|
|
// Check for "_foo@got - .", which comes through here as:
|
|
|
|
// Ltmp0:
|
|
|
|
// ... _foo@got - Ltmp0
|
|
|
|
if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
|
|
|
|
Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
|
|
|
|
Layout.getSymbolOffset(&B_SD) ==
|
|
|
|
Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
|
|
|
|
// SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
|
|
|
|
Type = MachO::ARM64_RELOC_POINTER_TO_GOT;
|
|
|
|
IsPCRel = 1;
|
|
|
|
MachO::any_relocation_info MRE;
|
|
|
|
MRE.r_word0 = FixupOffset;
|
2015-01-20 05:11:14 +08:00
|
|
|
MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
|
|
|
|
Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
|
2014-03-29 18:18:08 +08:00
|
|
|
return;
|
|
|
|
} else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
|
|
|
|
Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
|
|
|
|
// Otherwise, neither symbol can be modified.
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"unsupported relocation of modified symbol");
|
|
|
|
|
|
|
|
// We don't support PCrel relocations of differences.
|
|
|
|
if (IsPCRel)
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"unsupported pc-relative relocation of "
|
|
|
|
"difference");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
// AArch64 always uses external relocations. If there is no symbol to use as
|
2014-04-09 22:47:27 +08:00
|
|
|
// a base address (a local symbol with no preceding non-local symbol),
|
2014-03-29 18:18:08 +08:00
|
|
|
// error out.
|
|
|
|
//
|
|
|
|
// FIXME: We should probably just synthesize an external symbol and use
|
|
|
|
// that.
|
|
|
|
if (!A_Base)
|
|
|
|
Asm.getContext().FatalError(
|
|
|
|
Fixup.getLoc(),
|
|
|
|
"unsupported relocation of local symbol '" + A->getName() +
|
|
|
|
"'. Must have non-local symbol earlier in section.");
|
|
|
|
if (!B_Base)
|
|
|
|
Asm.getContext().FatalError(
|
|
|
|
Fixup.getLoc(),
|
|
|
|
"unsupported relocation of local symbol '" + B->getName() +
|
|
|
|
"'. Must have non-local symbol earlier in section.");
|
|
|
|
|
|
|
|
if (A_Base == B_Base && A_Base)
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"unsupported relocation with identical base");
|
|
|
|
|
2014-04-25 13:30:21 +08:00
|
|
|
Value += (!A_SD.getFragment() ? 0
|
|
|
|
: Writer->getSymbolAddress(&A_SD, Layout)) -
|
|
|
|
(!A_Base || !A_Base->getFragment()
|
2014-03-29 18:18:08 +08:00
|
|
|
? 0
|
|
|
|
: Writer->getSymbolAddress(A_Base, Layout));
|
2014-04-25 13:30:21 +08:00
|
|
|
Value -= (!B_SD.getFragment() ? 0
|
|
|
|
: Writer->getSymbolAddress(&B_SD, Layout)) -
|
|
|
|
(!B_Base || !B_Base->getFragment()
|
2014-03-29 18:18:08 +08:00
|
|
|
? 0
|
|
|
|
: Writer->getSymbolAddress(B_Base, Layout));
|
|
|
|
|
|
|
|
Type = MachO::ARM64_RELOC_UNSIGNED;
|
|
|
|
|
|
|
|
MachO::any_relocation_info MRE;
|
|
|
|
MRE.r_word0 = FixupOffset;
|
2015-01-20 05:11:14 +08:00
|
|
|
MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
|
|
|
|
Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2015-01-20 05:11:14 +08:00
|
|
|
RelSymbol = B_Base;
|
2014-03-29 18:18:08 +08:00
|
|
|
Type = MachO::ARM64_RELOC_SUBTRACTOR;
|
|
|
|
} else { // A + constant
|
|
|
|
const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
|
2015-01-15 03:07:23 +08:00
|
|
|
const MCSectionMachO &Section = static_cast<const MCSectionMachO &>(
|
|
|
|
Fragment->getParent()->getSection());
|
2015-01-13 02:13:07 +08:00
|
|
|
|
2015-01-20 05:11:14 +08:00
|
|
|
bool CanUseLocalRelocation =
|
|
|
|
canUseLocalRelocation(Section, *Symbol, Log2Size);
|
|
|
|
if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) {
|
|
|
|
const MCSection &Sec = Symbol->getSection();
|
|
|
|
if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
|
|
|
|
Asm.addLocalUsedInReloc(*Symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
|
|
|
|
const MCSymbolData *Base = Asm.getAtom(&SD);
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// If the symbol is a variable and we weren't able to get a Base for it
|
|
|
|
// (i.e., it's not in the symbol table associated with a section) resolve
|
|
|
|
// the relocation based its expansion instead.
|
|
|
|
if (Symbol->isVariable() && !Base) {
|
|
|
|
// If the evaluation is an absolute value, just use that directly
|
|
|
|
// to keep things easy.
|
|
|
|
int64_t Res;
|
|
|
|
if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
|
|
|
|
Res, Layout, Writer->getSectionAddressMap())) {
|
|
|
|
FixedValue = Res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Will the Target we already have ever have any data in it
|
|
|
|
// we need to preserve and merge with the new Target? How about
|
|
|
|
// the FixedValue?
|
2014-08-10 19:35:12 +08:00
|
|
|
if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
|
|
|
|
&Fixup))
|
2014-03-29 18:18:08 +08:00
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"unable to resolve variable '" +
|
|
|
|
Symbol->getName() + "'");
|
|
|
|
return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
|
|
|
|
FixedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relocations inside debug sections always use local relocations when
|
|
|
|
// possible. This seems to be done because the debugger doesn't fully
|
|
|
|
// understand relocation entries and expects to find values that
|
|
|
|
// have already been fixed up.
|
|
|
|
if (Symbol->isInSection()) {
|
|
|
|
if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
|
2014-04-25 13:30:21 +08:00
|
|
|
Base = nullptr;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
// AArch64 uses external relocations as much as possible. For debug
|
|
|
|
// sections, and for pointer-sized relocations (.quad), we allow section
|
|
|
|
// relocations. It's code sections that run into trouble.
|
2014-03-29 18:18:08 +08:00
|
|
|
if (Base) {
|
2015-01-20 05:11:14 +08:00
|
|
|
RelSymbol = Base;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Add the local offset, if needed.
|
|
|
|
if (Base != &SD)
|
|
|
|
Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
|
|
|
|
} else if (Symbol->isInSection()) {
|
2015-01-20 05:11:14 +08:00
|
|
|
if (!CanUseLocalRelocation)
|
2014-03-29 18:18:08 +08:00
|
|
|
Asm.getContext().FatalError(
|
|
|
|
Fixup.getLoc(),
|
|
|
|
"unsupported relocation of local symbol '" + Symbol->getName() +
|
|
|
|
"'. Must have non-local symbol earlier in section.");
|
|
|
|
// Adjust the relocation to be section-relative.
|
|
|
|
// The index is the section ordinal (1-based).
|
|
|
|
const MCSectionData &SymSD =
|
|
|
|
Asm.getSectionData(SD.getSymbol().getSection());
|
|
|
|
Index = SymSD.getOrdinal() + 1;
|
|
|
|
Value += Writer->getSymbolAddress(&SD, Layout);
|
|
|
|
|
|
|
|
if (IsPCRel)
|
|
|
|
Value -= Writer->getFragmentAddress(Fragment, Layout) +
|
2014-04-01 20:24:25 +08:00
|
|
|
Fixup.getOffset() + (1ULL << Log2Size);
|
2014-03-29 18:18:08 +08:00
|
|
|
} else {
|
|
|
|
// Resolve constant variables.
|
|
|
|
if (SD.getSymbol().isVariable()) {
|
|
|
|
int64_t Res;
|
|
|
|
if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
|
|
|
|
Res, Layout, Writer->getSectionAddressMap())) {
|
|
|
|
FixedValue = Res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Asm.getContext().FatalError(Fixup.getLoc(),
|
|
|
|
"unsupported relocation of variable '" +
|
|
|
|
Symbol->getName() + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the relocation kind is Branch26, Page21, or Pageoff12, any addend
|
|
|
|
// is represented via an Addend relocation, not encoded directly into
|
|
|
|
// the instruction.
|
|
|
|
if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
|
|
|
|
Type == MachO::ARM64_RELOC_PAGE21 ||
|
|
|
|
Type == MachO::ARM64_RELOC_PAGEOFF12) &&
|
|
|
|
Value) {
|
|
|
|
assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
|
|
|
|
|
|
|
|
MachO::any_relocation_info MRE;
|
|
|
|
MRE.r_word0 = FixupOffset;
|
2015-01-20 05:11:14 +08:00
|
|
|
MRE.r_word1 =
|
|
|
|
(Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
|
|
|
|
Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Now set up the Addend relocation.
|
|
|
|
Type = MachO::ARM64_RELOC_ADDEND;
|
|
|
|
Index = Value;
|
2015-01-20 05:11:14 +08:00
|
|
|
RelSymbol = nullptr;
|
2014-03-29 18:18:08 +08:00
|
|
|
IsPCRel = 0;
|
|
|
|
Log2Size = 2;
|
|
|
|
|
|
|
|
// Put zero into the instruction itself. The addend is in the relocation.
|
|
|
|
Value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's any addend left to handle, encode it in the instruction.
|
|
|
|
FixedValue = Value;
|
|
|
|
|
|
|
|
// struct relocation_info (8 bytes)
|
|
|
|
MachO::any_relocation_info MRE;
|
|
|
|
MRE.r_word0 = FixupOffset;
|
2015-01-20 05:11:14 +08:00
|
|
|
MRE.r_word1 =
|
|
|
|
(Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
|
|
|
|
Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS,
|
2015-04-10 02:32:58 +08:00
|
|
|
uint32_t CPUType,
|
|
|
|
uint32_t CPUSubtype) {
|
2014-05-24 20:50:23 +08:00
|
|
|
return createMachObjectWriter(
|
|
|
|
new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
|
|
|
|
/*IsLittleEndian=*/true);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|