Sort relocation entries before they are written out to a file. MIPS ABI

imposes a constraint that GOT16 referring to a local symbol or HI16 has to be
followed immediately by a matching LO16 relocation.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153553 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Akira Hatanaka 2012-03-28 00:23:33 +00:00
parent f93b863066
commit 37ac18ef2f

View File

@ -7,17 +7,31 @@
//
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/MipsBaseInfo.h"
#include "MCTargetDesc/MipsFixupKinds.h"
#include "MCTargetDesc/MipsMCTargetDesc.h"
#include "llvm/MC/MCAssembler.h"
#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"
#include <list>
using namespace llvm;
namespace {
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;
class MipsELFObjectWriter : public MCELFObjectTargetWriter {
public:
MipsELFObjectWriter(uint8_t OSABI);
@ -33,6 +47,8 @@ namespace {
const MCFragment &F,
const MCFixup &Fixup,
bool IsPCRel) const;
virtual void sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs);
};
}
@ -129,6 +145,93 @@ unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
return Type;
}
// 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) {
// Call the defualt function first. Relocations are sorted in descending
// order of r_offset.
MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
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;
}
MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS, uint8_t OSABI,
bool IsLittleEndian) {
MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(OSABI);