2011-12-22 09:06:53 +08:00
|
|
|
//===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
|
2011-12-22 08:37:50 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/ARMMCTargetDesc.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "MCTargetDesc/ARMFixupKinds.h"
|
2011-12-22 08:37:50 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-22 08:37:50 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ARMELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
enum { DefaultEABIVersion = 0x05000000U };
|
|
|
|
unsigned GetRelocTypeInner(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
ARMELFObjectWriter(uint8_t OSABI);
|
|
|
|
|
|
|
|
virtual ~ARMELFObjectWriter();
|
2011-12-22 10:58:12 +08:00
|
|
|
|
2011-12-22 08:37:50 +08:00
|
|
|
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
|
|
|
|
bool IsPCRel, bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const;
|
2011-12-22 10:58:12 +08:00
|
|
|
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
2011-12-22 08:37:50 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
|
|
|
|
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
|
|
|
|
ELF::EM_ARM,
|
|
|
|
/*HasRelocationAddend*/ false) {}
|
|
|
|
|
|
|
|
ARMELFObjectWriter::~ARMELFObjectWriter() {}
|
|
|
|
|
|
|
|
// In ARM, _MergedGlobals and other most symbols get emitted directly.
|
|
|
|
// I.e. not as an offset to a section symbol.
|
|
|
|
// This code is an approximation of what ARM/gcc does.
|
|
|
|
|
|
|
|
STATISTIC(PCRelCount, "Total number of PIC Relocations");
|
|
|
|
STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
|
|
|
|
|
|
|
|
const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
2012-01-28 23:58:32 +08:00
|
|
|
const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
|
2011-12-22 08:37:50 +08:00
|
|
|
bool EmitThisSym = false;
|
|
|
|
|
|
|
|
const MCSectionELF &Section =
|
|
|
|
static_cast<const MCSectionELF&>(Symbol.getSection());
|
|
|
|
bool InNormalSection = true;
|
|
|
|
unsigned RelocType = 0;
|
|
|
|
RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
|
|
|
|
|
|
|
|
DEBUG(
|
|
|
|
const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
|
|
|
|
MCSymbolRefExpr::VariantKind Kind2;
|
|
|
|
Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
|
|
|
|
MCSymbolRefExpr::VK_None;
|
|
|
|
dbgs() << "considering symbol "
|
|
|
|
<< Section.getSectionName() << "/"
|
|
|
|
<< Symbol.getName() << "/"
|
|
|
|
<< " Rel:" << (unsigned)RelocType
|
|
|
|
<< " Kind: " << (int)Kind << "/" << (int)Kind2
|
|
|
|
<< " Tmp:"
|
|
|
|
<< Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
|
|
|
|
<< Symbol.isVariable() << "/" << Symbol.isTemporary()
|
|
|
|
<< " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
|
|
|
|
|
|
|
|
if (IsPCRel) { ++PCRelCount;
|
|
|
|
switch (RelocType) {
|
|
|
|
default:
|
|
|
|
// Most relocation types are emitted as explicit symbols
|
|
|
|
InNormalSection =
|
|
|
|
StringSwitch<bool>(Section.getSectionName())
|
|
|
|
.Case(".data.rel.ro.local", false)
|
|
|
|
.Case(".data.rel", false)
|
|
|
|
.Case(".bss", false)
|
|
|
|
.Default(true);
|
|
|
|
EmitThisSym = true;
|
|
|
|
break;
|
|
|
|
case ELF::R_ARM_ABS32:
|
|
|
|
// But things get strange with R_ARM_ABS32
|
|
|
|
// In this case, most things that go in .rodata show up
|
|
|
|
// as section relative relocations
|
|
|
|
InNormalSection =
|
|
|
|
StringSwitch<bool>(Section.getSectionName())
|
|
|
|
.Case(".data.rel.ro.local", false)
|
|
|
|
.Case(".data.rel", false)
|
|
|
|
.Case(".rodata", false)
|
|
|
|
.Case(".bss", false)
|
|
|
|
.Default(true);
|
|
|
|
EmitThisSym = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NonPCRelCount++;
|
|
|
|
InNormalSection =
|
|
|
|
StringSwitch<bool>(Section.getSectionName())
|
|
|
|
.Case(".data.rel.ro.local", false)
|
|
|
|
.Case(".rodata", false)
|
|
|
|
.Case(".data.rel", false)
|
|
|
|
.Case(".bss", false)
|
|
|
|
.Default(true);
|
|
|
|
|
|
|
|
switch (RelocType) {
|
|
|
|
default: EmitThisSym = true; break;
|
|
|
|
case ELF::R_ARM_ABS32: EmitThisSym = false; break;
|
2012-12-12 15:14:46 +08:00
|
|
|
case ELF::R_ARM_PREL31: EmitThisSym = false; break;
|
2011-12-22 08:37:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EmitThisSym)
|
|
|
|
return &Symbol;
|
|
|
|
if (! Symbol.isTemporary() && InNormalSection) {
|
|
|
|
return &Symbol;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to examine the Fixup when determining whether to
|
|
|
|
// emit the relocation as an explicit symbol or as a section relative
|
|
|
|
// offset
|
|
|
|
unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel,
|
|
|
|
bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const {
|
|
|
|
return GetRelocTypeInner(Target, Fixup, IsPCRel);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
|
|
|
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
|
|
|
|
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
|
|
|
|
|
|
|
|
unsigned Type = 0;
|
|
|
|
if (IsPCRel) {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
2012-02-07 10:50:20 +08:00
|
|
|
default: llvm_unreachable("Unimplemented");
|
2011-12-22 08:37:50 +08:00
|
|
|
case FK_Data_4:
|
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_ARM_REL32;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_TLSGD:
|
2012-02-07 10:50:20 +08:00
|
|
|
llvm_unreachable("unimplemented");
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_GOTTPOFF:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_TLS_IE32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-28 05:36:23 +08:00
|
|
|
case ARM::fixup_arm_blx:
|
2012-06-20 00:03:02 +08:00
|
|
|
case ARM::fixup_arm_uncondbl:
|
2011-12-22 08:37:50 +08:00
|
|
|
switch (Modifier) {
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_PLT:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_PLT32;
|
|
|
|
break;
|
2014-01-30 12:02:31 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSCALL:
|
|
|
|
Type = ELF::R_ARM_TLS_CALL;
|
|
|
|
break;
|
2011-12-22 08:37:50 +08:00
|
|
|
default:
|
|
|
|
Type = ELF::R_ARM_CALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-30 17:15:32 +08:00
|
|
|
case ARM::fixup_arm_condbl:
|
2011-12-22 08:37:50 +08:00
|
|
|
case ARM::fixup_arm_condbranch:
|
2012-06-20 00:03:02 +08:00
|
|
|
case ARM::fixup_arm_uncondbranch:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_JUMP24;
|
|
|
|
break;
|
2012-09-01 23:06:36 +08:00
|
|
|
case ARM::fixup_t2_condbranch:
|
|
|
|
case ARM::fixup_t2_uncondbranch:
|
|
|
|
Type = ELF::R_ARM_THM_JUMP24;
|
|
|
|
break;
|
2011-12-22 08:37:50 +08:00
|
|
|
case ARM::fixup_arm_movt_hi16:
|
|
|
|
case ARM::fixup_arm_movt_hi16_pcrel:
|
|
|
|
Type = ELF::R_ARM_MOVT_PREL;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_movw_lo16:
|
|
|
|
case ARM::fixup_arm_movw_lo16_pcrel:
|
|
|
|
Type = ELF::R_ARM_MOVW_PREL_NC;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movt_hi16:
|
|
|
|
case ARM::fixup_t2_movt_hi16_pcrel:
|
|
|
|
Type = ELF::R_ARM_THM_MOVT_PREL;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movw_lo16:
|
|
|
|
case ARM::fixup_t2_movw_lo16_pcrel:
|
|
|
|
Type = ELF::R_ARM_THM_MOVW_PREL_NC;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_thumb_bl:
|
|
|
|
case ARM::fixup_arm_thumb_blx:
|
2014-01-30 12:02:31 +08:00
|
|
|
switch (Modifier) {
|
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSCALL:
|
|
|
|
Type = ELF::R_ARM_THM_TLS_CALL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Type = ELF::R_ARM_THM_CALL;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-22 08:37:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
default: llvm_unreachable("invalid fixup kind!");
|
|
|
|
case FK_Data_4:
|
|
|
|
switch (Modifier) {
|
2012-01-21 05:51:11 +08:00
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
2012-12-12 15:14:46 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_NONE:
|
|
|
|
Type = ELF::R_ARM_NONE;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_GOT_BREL;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_TLSGD:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_TLS_GD32;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_TPOFF:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_TLS_LE32;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_GOTTPOFF:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_TLS_IE32;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_ARM_ABS32;
|
|
|
|
break;
|
2013-12-05 06:43:20 +08:00
|
|
|
case MCSymbolRefExpr::VK_GOTOFF:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_GOTOFF32;
|
|
|
|
break;
|
2012-01-26 17:25:43 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TARGET1:
|
|
|
|
Type = ELF::R_ARM_TARGET1;
|
|
|
|
break;
|
2012-11-10 04:20:12 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TARGET2:
|
|
|
|
Type = ELF::R_ARM_TARGET2;
|
|
|
|
break;
|
2012-12-12 15:14:46 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_PREL31:
|
|
|
|
Type = ELF::R_ARM_PREL31;
|
|
|
|
break;
|
2014-01-20 19:00:40 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSLDO:
|
|
|
|
Type = ELF::R_ARM_TLS_LDO32;
|
|
|
|
break;
|
2014-01-30 12:02:31 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSCALL:
|
|
|
|
Type = ELF::R_ARM_TLS_CALL;
|
|
|
|
break;
|
2014-01-30 12:02:38 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSDESC:
|
|
|
|
Type = ELF::R_ARM_TLS_GOTDESC;
|
|
|
|
break;
|
2014-01-30 12:02:47 +08:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
|
|
|
|
Type = ELF::R_ARM_TLS_DESCSEQ;
|
|
|
|
break;
|
2012-12-12 15:14:46 +08:00
|
|
|
}
|
2011-12-22 08:37:50 +08:00
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_ldst_pcrel_12:
|
|
|
|
case ARM::fixup_arm_pcrel_10:
|
|
|
|
case ARM::fixup_arm_adr_pcrel_12:
|
|
|
|
case ARM::fixup_arm_thumb_bl:
|
|
|
|
case ARM::fixup_arm_thumb_cb:
|
|
|
|
case ARM::fixup_arm_thumb_cp:
|
|
|
|
case ARM::fixup_arm_thumb_br:
|
2012-02-07 10:50:20 +08:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-22 08:37:50 +08:00
|
|
|
case ARM::fixup_arm_condbranch:
|
2012-06-20 00:03:02 +08:00
|
|
|
case ARM::fixup_arm_uncondbranch:
|
2011-12-22 08:37:50 +08:00
|
|
|
Type = ELF::R_ARM_JUMP24;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_movt_hi16:
|
|
|
|
Type = ELF::R_ARM_MOVT_ABS;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_movw_lo16:
|
|
|
|
Type = ELF::R_ARM_MOVW_ABS_NC;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movt_hi16:
|
|
|
|
Type = ELF::R_ARM_THM_MOVT_ABS;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movw_lo16:
|
|
|
|
Type = ELF::R_ARM_THM_MOVW_ABS_NC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
|
|
|
|
uint8_t OSABI) {
|
|
|
|
MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
|
|
|
|
return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
|
|
|
|
}
|