Simplify by sorting relocations before writing them.

llvm-svn: 323944
This commit is contained in:
Rafael Espindola 2018-02-01 03:17:12 +00:00
parent fc681efde4
commit 7ce2b4cd13
1 changed files with 7 additions and 17 deletions

View File

@ -1260,32 +1260,22 @@ RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
this->Entsize = Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
}
template <class ELFT, class RelTy>
static bool compRelocations(const RelTy &A, const RelTy &B) {
bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
static bool compRelocations(const DynamicReloc &A, const DynamicReloc &B) {
bool AIsRel = A.Type == Target->RelativeRel;
bool BIsRel = B.Type == Target->RelativeRel;
if (AIsRel != BIsRel)
return AIsRel;
return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
return A.getSymIndex() < B.getSymIndex();
}
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
uint8_t *BufBegin = Buf;
if (Sort)
std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
for (const DynamicReloc &Rel : Relocs) {
encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
Buf += Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
}
if (Sort) {
if (Config->IsRela)
std::stable_sort((Elf_Rela *)BufBegin,
(Elf_Rela *)BufBegin + Relocs.size(),
compRelocations<ELFT, Elf_Rela>);
else
std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
compRelocations<ELFT, Elf_Rel>);
}
}
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {