2011-12-22 01:06:53 +00:00
|
|
|
//===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
|
2011-12-22 00:37:50 +00: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-03 16:50:05 +00:00
|
|
|
#include "MCTargetDesc/ARMFixupKinds.h"
|
2011-12-22 00:37:50 +00: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-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-22 00:37:50 +00: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 02:58:12 +00:00
|
|
|
|
2014-03-10 03:19:03 +00:00
|
|
|
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
|
2014-03-27 20:49:35 +00:00
|
|
|
bool IsPCRel) const override;
|
2014-03-29 06:26:49 +00:00
|
|
|
|
|
|
|
bool needsRelocateWithSymbol(unsigned Type) const override;
|
2011-12-22 00:37:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
|
|
|
|
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
|
|
|
|
ELF::EM_ARM,
|
|
|
|
/*HasRelocationAddend*/ false) {}
|
|
|
|
|
|
|
|
ARMELFObjectWriter::~ARMELFObjectWriter() {}
|
|
|
|
|
2014-03-29 06:26:49 +00:00
|
|
|
bool ARMELFObjectWriter::needsRelocateWithSymbol(unsigned Type) const {
|
|
|
|
// FIXME: This is extremelly conservative. This really needs to use a
|
|
|
|
// whitelist with a clear explanation for why each realocation needs to
|
|
|
|
// point to the symbol, not to the section.
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ELF::R_ARM_PREL31:
|
|
|
|
case ELF::R_ARM_ABS32:
|
|
|
|
return false;
|
2011-12-22 00:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2014-03-27 20:49:35 +00:00
|
|
|
bool IsPCRel) const {
|
2011-12-22 00:37:50 +00:00
|
|
|
return GetRelocTypeInner(Target, Fixup, IsPCRel);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
2014-03-20 02:12:01 +00:00
|
|
|
MCSymbolRefExpr::VariantKind Modifier = Fixup.getAccessVariant();
|
2011-12-22 00:37:50 +00:00
|
|
|
|
|
|
|
unsigned Type = 0;
|
|
|
|
if (IsPCRel) {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
2012-02-07 02:50:20 +00:00
|
|
|
default: llvm_unreachable("Unimplemented");
|
2011-12-22 00:37:50 +00:00
|
|
|
case FK_Data_4:
|
|
|
|
switch (Modifier) {
|
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_ARM_REL32;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_TLSGD:
|
2012-02-07 02:50:20 +00:00
|
|
|
llvm_unreachable("unimplemented");
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOTTPOFF:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_TLS_IE32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-27 21:36:23 +00:00
|
|
|
case ARM::fixup_arm_blx:
|
2012-06-19 16:03:02 +00:00
|
|
|
case ARM::fixup_arm_uncondbl:
|
2011-12-22 00:37:50 +00:00
|
|
|
switch (Modifier) {
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_PLT:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_PLT32;
|
|
|
|
break;
|
2014-01-30 04:02:31 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSCALL:
|
|
|
|
Type = ELF::R_ARM_TLS_CALL;
|
|
|
|
break;
|
2011-12-22 00:37:50 +00:00
|
|
|
default:
|
|
|
|
Type = ELF::R_ARM_CALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-30 09:15:32 +00:00
|
|
|
case ARM::fixup_arm_condbl:
|
2011-12-22 00:37:50 +00:00
|
|
|
case ARM::fixup_arm_condbranch:
|
2012-06-19 16:03:02 +00:00
|
|
|
case ARM::fixup_arm_uncondbranch:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_JUMP24;
|
|
|
|
break;
|
2012-09-01 15:06:36 +00:00
|
|
|
case ARM::fixup_t2_condbranch:
|
|
|
|
case ARM::fixup_t2_uncondbranch:
|
|
|
|
Type = ELF::R_ARM_THM_JUMP24;
|
|
|
|
break;
|
2011-12-22 00:37:50 +00:00
|
|
|
case ARM::fixup_arm_movt_hi16:
|
|
|
|
Type = ELF::R_ARM_MOVT_PREL;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_movw_lo16:
|
|
|
|
Type = ELF::R_ARM_MOVW_PREL_NC;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movt_hi16:
|
|
|
|
Type = ELF::R_ARM_THM_MOVT_PREL;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_t2_movw_lo16:
|
|
|
|
Type = ELF::R_ARM_THM_MOVW_PREL_NC;
|
|
|
|
break;
|
|
|
|
case ARM::fixup_arm_thumb_bl:
|
|
|
|
case ARM::fixup_arm_thumb_blx:
|
2014-01-30 04:02:31 +00: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 00:37:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
default: llvm_unreachable("invalid fixup kind!");
|
|
|
|
case FK_Data_4:
|
|
|
|
switch (Modifier) {
|
2012-01-20 21:51:11 +00:00
|
|
|
default: llvm_unreachable("Unsupported Modifier");
|
2012-12-12 07:14:46 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_NONE:
|
|
|
|
Type = ELF::R_ARM_NONE;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_GOT_BREL;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_TLSGD:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_TLS_GD32;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_TPOFF:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_TLS_LE32;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOTTPOFF:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_TLS_IE32;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_None:
|
|
|
|
Type = ELF::R_ARM_ABS32;
|
|
|
|
break;
|
2013-12-04 22:43:20 +00:00
|
|
|
case MCSymbolRefExpr::VK_GOTOFF:
|
2011-12-22 00:37:50 +00:00
|
|
|
Type = ELF::R_ARM_GOTOFF32;
|
|
|
|
break;
|
2012-01-26 09:25:43 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TARGET1:
|
|
|
|
Type = ELF::R_ARM_TARGET1;
|
|
|
|
break;
|
2012-11-09 20:20:12 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TARGET2:
|
|
|
|
Type = ELF::R_ARM_TARGET2;
|
|
|
|
break;
|
2012-12-12 07:14:46 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_PREL31:
|
|
|
|
Type = ELF::R_ARM_PREL31;
|
|
|
|
break;
|
2014-01-20 11:00:40 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSLDO:
|
|
|
|
Type = ELF::R_ARM_TLS_LDO32;
|
|
|
|
break;
|
2014-01-30 04:02:31 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSCALL:
|
|
|
|
Type = ELF::R_ARM_TLS_CALL;
|
|
|
|
break;
|
2014-01-30 04:02:38 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSDESC:
|
|
|
|
Type = ELF::R_ARM_TLS_GOTDESC;
|
|
|
|
break;
|
2014-01-30 04:02:47 +00:00
|
|
|
case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
|
|
|
|
Type = ELF::R_ARM_TLS_DESCSEQ;
|
|
|
|
break;
|
2012-12-12 07:14:46 +00:00
|
|
|
}
|
2011-12-22 00:37:50 +00: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 02:50:20 +00:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-22 00:37:50 +00:00
|
|
|
case ARM::fixup_arm_condbranch:
|
2012-06-19 16:03:02 +00:00
|
|
|
case ARM::fixup_arm_uncondbranch:
|
2011-12-22 00:37:50 +00: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,
|
2014-03-28 14:35:30 +00:00
|
|
|
uint8_t OSABI,
|
|
|
|
bool IsLittleEndian) {
|
2011-12-22 00:37:50 +00:00
|
|
|
MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
|
2014-03-28 14:35:30 +00:00
|
|
|
return createELFObjectWriter(MOTW, OS, IsLittleEndian);
|
2011-12-22 00:37:50 +00:00
|
|
|
}
|