2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
|
2011-12-22 03:03:17 +00: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 00:23:33 +00:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2011-12-22 03:03:17 +00:00
|
|
|
#include "MCTargetDesc/MipsFixupKinds.h"
|
|
|
|
#include "MCTargetDesc/MipsMCTargetDesc.h"
|
2012-03-28 00:23:33 +00:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2011-12-22 03:03:17 +00: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 00:23:33 +00:00
|
|
|
#include <list>
|
2011-12-22 03:03:17 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2012-03-28 00:23:33 +00:00
|
|
|
struct RelEntry {
|
|
|
|
RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) :
|
|
|
|
Reloc(R), Sym(S), Offset(O) {}
|
|
|
|
ELFRelocationEntry Reloc;
|
|
|
|
const MCSymbol *Sym;
|
|
|
|
int64_t Offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::list<RelEntry> RelLs;
|
|
|
|
typedef RelLs::iterator RelLsIter;
|
|
|
|
|
2011-12-22 03:03:17 +00:00
|
|
|
class MipsELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
public:
|
2012-06-27 22:28:30 +00:00
|
|
|
MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, bool _isN64);
|
2011-12-22 03:03:17 +00:00
|
|
|
|
|
|
|
virtual ~MipsELFObjectWriter();
|
|
|
|
|
|
|
|
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
|
|
|
|
bool IsPCRel, bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const;
|
|
|
|
virtual unsigned getEFlags() const;
|
|
|
|
virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const;
|
2012-03-28 00:23:33 +00:00
|
|
|
virtual void sortRelocs(const MCAssembler &Asm,
|
|
|
|
std::vector<ELFRelocationEntry> &Relocs);
|
2011-12-22 03:03:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-27 22:28:30 +00:00
|
|
|
MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
|
|
|
|
bool _isN64)
|
2012-04-02 19:25:22 +00:00
|
|
|
: MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
|
2012-06-27 22:28:30 +00:00
|
|
|
/*HasRelocationAddend*/ false,
|
|
|
|
/*IsN64*/ _isN64) {}
|
2011-12-22 03:03:17 +00:00
|
|
|
|
|
|
|
MipsELFObjectWriter::~MipsELFObjectWriter() {}
|
|
|
|
|
2012-04-02 19:25:22 +00:00
|
|
|
// FIXME: get the real EABI Version from the Subtarget class.
|
2011-12-22 03:03:17 +00:00
|
|
|
unsigned MipsELFObjectWriter::getEFlags() const {
|
2012-04-02 19:25:22 +00:00
|
|
|
|
|
|
|
// FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
|
|
|
|
unsigned Flag = ELF::EF_MIPS_NOREORDER;
|
|
|
|
|
|
|
|
if (is64Bit())
|
|
|
|
Flag |= ELF::EF_MIPS_ARCH_64R2;
|
|
|
|
else
|
|
|
|
Flag |= ELF::EF_MIPS_ARCH_32R2;
|
|
|
|
return Flag;
|
2011-12-22 03:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFragment &F,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
|
|
|
assert(Target.getSymA() && "SymA cannot be 0.");
|
2012-01-28 15:58:32 +00:00
|
|
|
const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
|
2011-12-22 03:03:17 +00:00
|
|
|
|
|
|
|
if (Sym.getSection().getKind().isMergeableCString() ||
|
|
|
|
Sym.getSection().getKind().isMergeableConst())
|
|
|
|
return &Sym;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel,
|
|
|
|
bool IsRelocWithSymbol,
|
|
|
|
int64_t Addend) const {
|
|
|
|
// 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;
|
|
|
|
case FK_GPRel_4:
|
|
|
|
Type = ELF::R_MIPS_GPREL32;
|
|
|
|
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-27 22:48:25 +00: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-13 19:15:47 +00:00
|
|
|
case Mips::fixup_Mips_GOT_DISP:
|
|
|
|
Type = ELF::R_MIPS_GOT_DISP;
|
|
|
|
break;
|
2012-06-27 22:48:25 +00: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;
|
2011-12-22 03:03:17 +00:00
|
|
|
}
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2012-03-28 00:23:33 +00:00
|
|
|
// Return true if R is either a GOT16 against a local symbol or HI16.
|
|
|
|
static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
|
|
|
|
if (!R.Sym)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
|
|
|
|
|
|
|
|
return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
|
|
|
|
(R.Reloc.Type == ELF::R_MIPS_HI16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
|
|
|
|
if (I == Last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RelLsIter Hi = I++;
|
|
|
|
|
|
|
|
return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
|
|
|
|
(Hi->Offset == I->Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
|
|
|
|
return R0.Sym == R1.Sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
|
|
|
|
return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
|
|
|
|
std::vector<ELFRelocationEntry> &Relocs) {
|
2012-06-27 22:28:30 +00:00
|
|
|
// Call the default function first. Relocations are sorted in descending
|
2012-03-28 00:23:33 +00:00
|
|
|
// order of r_offset.
|
|
|
|
MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
|
2012-06-14 21:10:56 +00:00
|
|
|
|
2012-03-28 00:23:33 +00:00
|
|
|
RelLs RelocLs;
|
|
|
|
std::vector<RelLsIter> Unmatched;
|
|
|
|
|
|
|
|
// Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
|
|
|
|
// are in ascending order of r_offset.
|
|
|
|
for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
|
|
|
|
R != Relocs.rend(); ++R) {
|
|
|
|
std::pair<const MCSymbolRefExpr*, int64_t> P =
|
|
|
|
MipsGetSymAndOffset(*R->Fixup);
|
|
|
|
RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
|
|
|
|
P.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list of unmatched HI16 and GOT16.
|
|
|
|
for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
|
|
|
|
if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
|
|
|
|
Unmatched.push_back(R);
|
|
|
|
|
|
|
|
// Insert unmatched HI16 and GOT16 immediately before their matching LO16.
|
|
|
|
for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
|
|
|
|
U != Unmatched.end(); ++U) {
|
|
|
|
RelLsIter LoPos = RelocLs.end(), HiPos = *U;
|
|
|
|
bool MatchedLo = false;
|
|
|
|
|
|
|
|
for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
|
|
|
|
if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
|
|
|
|
(CompareOffset(*R, *HiPos) >= 0) &&
|
|
|
|
((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
|
|
|
|
(!MatchedLo && !CompareOffset(*R, *LoPos))))
|
|
|
|
LoPos = R;
|
|
|
|
|
|
|
|
MatchedLo = NeedsMatchingLo(Asm, *R) &&
|
|
|
|
HasMatchingLo(Asm, R, --RelocLs.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a matching LoPos was found, move HiPos and insert it before LoPos.
|
|
|
|
// Make the offsets of HiPos and LoPos match.
|
|
|
|
if (LoPos != RelocLs.end()) {
|
|
|
|
HiPos->Offset = LoPos->Offset;
|
|
|
|
RelocLs.insert(LoPos, *HiPos);
|
|
|
|
RelocLs.erase(HiPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the sorted list back in reverse order.
|
|
|
|
assert(Relocs.size() == RelocLs.size());
|
|
|
|
unsigned I = RelocLs.size();
|
|
|
|
|
|
|
|
for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
|
|
|
|
Relocs[--I] = R->Reloc;
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:25:22 +00:00
|
|
|
MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
|
|
|
|
uint8_t OSABI,
|
|
|
|
bool IsLittleEndian,
|
|
|
|
bool Is64Bit) {
|
2012-06-27 22:28:30 +00:00
|
|
|
MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
|
|
|
|
(Is64Bit) ? true : false);
|
2011-12-22 03:03:17 +00:00
|
|
|
return createELFObjectWriter(MOTW, OS, IsLittleEndian);
|
|
|
|
}
|