Cleanup relocation sorting for ELF.

We want the order to be deterministic on all platforms. NAKAMURA Takumi
fixed that in r181864. This patch is just two small cleanups:

* Move the function to the cpp file. It is only passed to array_pod_sort.
* Remove the ppc implementation which is now redundant

llvm-svn: 181910
This commit is contained in:
Rafael Espindola 2013-05-15 18:22:01 +00:00
parent 04ff46b750
commit 5ef2f39b92
3 changed files with 16 additions and 58 deletions

View File

@ -42,18 +42,6 @@ struct ELFRelocationEntry {
const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup) const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup)
: r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym), : r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym),
r_addend(Addend), Fixup(&Fixup) {} r_addend(Addend), Fixup(&Fixup) {}
// Support lexicographic sorting.
bool operator<(const ELFRelocationEntry &RE) const {
if (RE.r_offset != r_offset)
return RE.r_offset < r_offset;
if (Type != RE.Type)
return Type < RE.Type;
if (Index != RE.Index)
return Index < RE.Index;
llvm_unreachable("ELFRelocs might be unstable!");
return 0;
}
}; };
class MCELFObjectTargetWriter { class MCELFObjectTargetWriter {

View File

@ -39,9 +39,23 @@ const MCSymbol *MCELFObjectTargetWriter::undefinedExplicitRelSym(const MCValue &
return &Symbol.AliasedSymbol(); return &Symbol.AliasedSymbol();
} }
// ELF doesn't require relocations to be in any order. We sort by the r_offset,
// just to match gnu as for easier comparison. The use type and index is an
// arbitrary way of making the sort deterministic.
static int cmpRel(const void *AP, const void *BP) {
const ELFRelocationEntry &A = *(const ELFRelocationEntry *)AP;
const ELFRelocationEntry &B = *(const ELFRelocationEntry *)BP;
if (A.r_offset != B.r_offset)
return B.r_offset - A.r_offset;
if (B.Type != A.Type)
return A.Type - B.Type;
if (B.Index != A.Index)
return B.Index - A.Index;
llvm_unreachable("ELFRelocs might be unstable!");
}
void void
MCELFObjectTargetWriter::sortRelocs(const MCAssembler &Asm, MCELFObjectTargetWriter::sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs) { std::vector<ELFRelocationEntry> &Relocs) {
// Sort by the r_offset, just like gnu as does. array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel);
array_pod_sort(Relocs.begin(), Relocs.end());
} }

View File

@ -33,25 +33,9 @@ namespace {
virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target, virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) const; bool IsPCRel) const;
virtual void sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs);
};
class PPCELFRelocationEntry : public ELFRelocationEntry {
public:
PPCELFRelocationEntry(const ELFRelocationEntry &RE);
bool operator<(const PPCELFRelocationEntry &RE) const {
return (RE.r_offset < r_offset ||
(RE.r_offset == r_offset && RE.Type > Type));
}
}; };
} }
PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE)
: ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol,
RE.r_addend, *RE.Fixup) {}
PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI) PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
: MCELFObjectTargetWriter(Is64Bit, OSABI, : MCELFObjectTargetWriter(Is64Bit, OSABI,
Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC, Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
@ -239,34 +223,6 @@ const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Targe
return NULL; return NULL;
} }
// The standard sorter only sorts on the r_offset field, but PowerPC can
// have multiple relocations at the same offset. Sort secondarily on the
// relocation type to avoid nondeterminism.
void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs) {
// Copy to a temporary vector of relocation entries having a different
// sort function.
std::vector<PPCELFRelocationEntry> TmpRelocs;
for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin();
R != Relocs.end(); ++R) {
TmpRelocs.push_back(PPCELFRelocationEntry(*R));
}
// Sort in place by ascending r_offset and descending r_type.
array_pod_sort(TmpRelocs.begin(), TmpRelocs.end());
// Copy back to the original vector.
unsigned I = 0;
for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin();
R != TmpRelocs.end(); ++R, ++I) {
Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type,
R->Symbol, R->r_addend, *R->Fixup);
}
}
MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS, MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
bool Is64Bit, bool Is64Bit,
uint8_t OSABI) { uint8_t OSABI) {