2012-02-17 16:55:11 +08:00
|
|
|
//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
|
2011-12-22 11:03:17 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-28 08:23:33 +08:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2011-12-22 11:03:17 +08:00
|
|
|
#include "MCTargetDesc/MipsFixupKinds.h"
|
|
|
|
#include "MCTargetDesc/MipsMCTargetDesc.h"
|
2012-03-28 08:23:33 +08:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2011-12-22 11:03:17 +08:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-03-28 08:23:33 +08:00
|
|
|
#include <list>
|
2011-12-22 11:03:17 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MipsELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
public:
|
2012-08-18 05:28:04 +08:00
|
|
|
MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
|
|
|
|
bool _isN64, bool IsLittleEndian);
|
2011-12-22 11:03:17 +08:00
|
|
|
|
|
|
|
virtual ~MipsELFObjectWriter();
|
|
|
|
|
2014-03-28 04:41:17 +08:00
|
|
|
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
|
2014-03-28 04:49:35 +08:00
|
|
|
bool IsPCRel) const override;
|
2014-03-29 14:26:49 +08:00
|
|
|
bool needsRelocateWithSymbol(unsigned Type) const override;
|
2011-12-22 11:03:17 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-28 06:28:30 +08:00
|
|
|
MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
|
2012-08-18 05:28:04 +08:00
|
|
|
bool _isN64, bool IsLittleEndian)
|
2012-04-03 03:25:22 +08:00
|
|
|
: MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
|
2012-08-22 08:49:30 +08:00
|
|
|
/*HasRelocationAddend*/ (_isN64) ? true : false,
|
2012-06-28 06:28:30 +08:00
|
|
|
/*IsN64*/ _isN64) {}
|
2011-12-22 11:03:17 +08:00
|
|
|
|
|
|
|
MipsELFObjectWriter::~MipsELFObjectWriter() {}
|
|
|
|
|
|
|
|
unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
2014-03-28 04:49:35 +08:00
|
|
|
bool IsPCRel) const {
|
2011-12-22 11:03:17 +08:00
|
|
|
// determine the type of the relocation
|
|
|
|
unsigned Type = (unsigned)ELF::R_MIPS_NONE;
|
|
|
|
unsigned Kind = (unsigned)Fixup.getKind();
|
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid fixup kind!");
|
|
|
|
case FK_Data_4:
|
|
|
|
Type = ELF::R_MIPS_32;
|
|
|
|
break;
|
2012-08-07 08:01:14 +08:00
|
|
|
case FK_Data_8:
|
|
|
|
Type = ELF::R_MIPS_64;
|
|
|
|
break;
|
2011-12-22 11:03:17 +08:00
|
|
|
case FK_GPRel_4:
|
2013-01-15 09:08:02 +08:00
|
|
|
if (isN64()) {
|
|
|
|
Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
|
|
|
|
Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
|
|
|
|
Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Type = ELF::R_MIPS_GPREL32;
|
2011-12-22 11:03:17 +08:00
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GPREL16:
|
|
|
|
Type = ELF::R_MIPS_GPREL16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_26:
|
|
|
|
Type = ELF::R_MIPS_26;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_CALL16:
|
|
|
|
Type = ELF::R_MIPS_CALL16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GOT_Global:
|
|
|
|
case Mips::fixup_Mips_GOT_Local:
|
|
|
|
Type = ELF::R_MIPS_GOT16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_HI16:
|
|
|
|
Type = ELF::R_MIPS_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_LO16:
|
|
|
|
Type = ELF::R_MIPS_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_TLSGD:
|
|
|
|
Type = ELF::R_MIPS_TLS_GD;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GOTTPREL:
|
|
|
|
Type = ELF::R_MIPS_TLS_GOTTPREL;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_TPREL_HI:
|
|
|
|
Type = ELF::R_MIPS_TLS_TPREL_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_TPREL_LO:
|
|
|
|
Type = ELF::R_MIPS_TLS_TPREL_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_TLSLDM:
|
|
|
|
Type = ELF::R_MIPS_TLS_LDM;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_DTPREL_HI:
|
|
|
|
Type = ELF::R_MIPS_TLS_DTPREL_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_DTPREL_LO:
|
|
|
|
Type = ELF::R_MIPS_TLS_DTPREL_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_Branch_PCRel:
|
|
|
|
case Mips::fixup_Mips_PC16:
|
|
|
|
Type = ELF::R_MIPS_PC16;
|
|
|
|
break;
|
2012-06-28 06:48:25 +08:00
|
|
|
case Mips::fixup_Mips_GOT_PAGE:
|
|
|
|
Type = ELF::R_MIPS_GOT_PAGE;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GOT_OFST:
|
|
|
|
Type = ELF::R_MIPS_GOT_OFST;
|
|
|
|
break;
|
2012-07-14 03:15:47 +08:00
|
|
|
case Mips::fixup_Mips_GOT_DISP:
|
|
|
|
Type = ELF::R_MIPS_GOT_DISP;
|
|
|
|
break;
|
2012-06-28 06:48:25 +08:00
|
|
|
case Mips::fixup_Mips_GPOFF_HI:
|
|
|
|
Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
|
|
|
|
Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
|
|
|
|
Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GPOFF_LO:
|
|
|
|
Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
|
|
|
|
Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
|
|
|
|
Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
|
|
|
|
break;
|
2012-08-07 05:26:03 +08:00
|
|
|
case Mips::fixup_Mips_HIGHER:
|
|
|
|
Type = ELF::R_MIPS_HIGHER;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_HIGHEST:
|
|
|
|
Type = ELF::R_MIPS_HIGHEST;
|
|
|
|
break;
|
2012-11-22 07:38:59 +08:00
|
|
|
case Mips::fixup_Mips_GOT_HI16:
|
|
|
|
Type = ELF::R_MIPS_GOT_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_GOT_LO16:
|
|
|
|
Type = ELF::R_MIPS_GOT_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_CALL_HI16:
|
|
|
|
Type = ELF::R_MIPS_CALL_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_Mips_CALL_LO16:
|
|
|
|
Type = ELF::R_MIPS_CALL_LO16;
|
|
|
|
break;
|
2013-10-30 00:38:59 +08:00
|
|
|
case Mips::fixup_MICROMIPS_26_S1:
|
|
|
|
Type = ELF::R_MICROMIPS_26_S1;
|
|
|
|
break;
|
2013-10-24 00:14:44 +08:00
|
|
|
case Mips::fixup_MICROMIPS_HI16:
|
|
|
|
Type = ELF::R_MICROMIPS_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_LO16:
|
|
|
|
Type = ELF::R_MICROMIPS_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_GOT16:
|
|
|
|
Type = ELF::R_MICROMIPS_GOT16;
|
|
|
|
break;
|
2013-11-04 22:53:22 +08:00
|
|
|
case Mips::fixup_MICROMIPS_PC16_S1:
|
|
|
|
Type = ELF::R_MICROMIPS_PC16_S1;
|
|
|
|
break;
|
2013-10-24 00:14:44 +08:00
|
|
|
case Mips::fixup_MICROMIPS_CALL16:
|
|
|
|
Type = ELF::R_MICROMIPS_CALL16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_GOT_DISP:
|
|
|
|
Type = ELF::R_MICROMIPS_GOT_DISP;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_GOT_PAGE:
|
|
|
|
Type = ELF::R_MICROMIPS_GOT_PAGE;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_GOT_OFST:
|
|
|
|
Type = ELF::R_MICROMIPS_GOT_OFST;
|
|
|
|
break;
|
2013-12-20 00:02:32 +08:00
|
|
|
case Mips::fixup_MICROMIPS_TLS_GD:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_GD;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_TLS_LDM:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_LDM;
|
|
|
|
break;
|
2013-10-24 00:14:44 +08:00
|
|
|
case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_DTPREL_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_DTPREL_LO16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_TPREL_HI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
|
|
|
|
Type = ELF::R_MICROMIPS_TLS_TPREL_LO16;
|
|
|
|
break;
|
2014-06-12 20:40:00 +08:00
|
|
|
case Mips::fixup_MIPS_PC19_S2:
|
|
|
|
Type = ELF::R_MIPS_PC19_S2;
|
|
|
|
break;
|
2014-05-27 20:55:40 +08:00
|
|
|
case Mips::fixup_MIPS_PC21_S2:
|
|
|
|
Type = ELF::R_MIPS_PC21_S2;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MIPS_PC26_S2:
|
|
|
|
Type = ELF::R_MIPS_PC26_S2;
|
|
|
|
break;
|
2014-05-27 22:58:51 +08:00
|
|
|
case Mips::fixup_MIPS_PCHI16:
|
|
|
|
Type = ELF::R_MIPS_PCHI16;
|
|
|
|
break;
|
|
|
|
case Mips::fixup_MIPS_PCLO16:
|
|
|
|
Type = ELF::R_MIPS_PCLO16;
|
|
|
|
break;
|
2011-12-22 11:03:17 +08:00
|
|
|
}
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2014-03-29 14:26:49 +08:00
|
|
|
bool
|
|
|
|
MipsELFObjectWriter::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;
|
|
|
|
|
2014-04-01 03:00:23 +08:00
|
|
|
case ELF::R_MIPS_GOT16:
|
|
|
|
case ELF::R_MIPS16_GOT16:
|
|
|
|
case ELF::R_MICROMIPS_GOT16:
|
|
|
|
llvm_unreachable("Should have been handled already");
|
|
|
|
|
|
|
|
// These relocations might be paired with another relocation. The pairing is
|
|
|
|
// done by the static linker by matching the symbol. Since we only see one
|
|
|
|
// relocation at a time, we have to force them to relocate with a symbol to
|
|
|
|
// avoid ending up with a pair where one points to a section and another
|
|
|
|
// points to a symbol.
|
2014-03-29 14:26:49 +08:00
|
|
|
case ELF::R_MIPS_HI16:
|
2014-04-01 03:00:23 +08:00
|
|
|
case ELF::R_MIPS16_HI16:
|
|
|
|
case ELF::R_MICROMIPS_HI16:
|
|
|
|
case ELF::R_MIPS_LO16:
|
|
|
|
case ELF::R_MIPS16_LO16:
|
|
|
|
case ELF::R_MICROMIPS_LO16:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ELF::R_MIPS_26:
|
2014-03-29 14:26:49 +08:00
|
|
|
case ELF::R_MIPS_32:
|
|
|
|
case ELF::R_MIPS_64:
|
|
|
|
case ELF::R_MIPS_GPREL16:
|
2012-03-28 08:23:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:25:22 +08:00
|
|
|
MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
|
|
|
|
uint8_t OSABI,
|
|
|
|
bool IsLittleEndian,
|
|
|
|
bool Is64Bit) {
|
2012-06-28 06:28:30 +08:00
|
|
|
MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
|
2012-08-18 05:28:04 +08:00
|
|
|
(Is64Bit) ? true : false,
|
|
|
|
IsLittleEndian);
|
2011-12-22 11:03:17 +08:00
|
|
|
return createELFObjectWriter(MOTW, OS, IsLittleEndian);
|
|
|
|
}
|