Store relocations in a map from MCSectionELF.

Saves finding the MCSectionData just to do a map lookup.

llvm-svn: 236189
This commit is contained in:
Rafael Espindola 2015-04-30 00:45:46 +00:00
parent 25d34af88a
commit 34948e5e22
1 changed files with 21 additions and 25 deletions

View File

@ -111,8 +111,8 @@ class ELFObjectWriter : public MCObjectWriter {
SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc; SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
DenseMap<const MCSymbol *, const MCSymbol *> Renames; DenseMap<const MCSymbol *, const MCSymbol *> Renames;
llvm::DenseMap<const MCSectionData *, std::vector<ELFRelocationEntry>> llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
Relocations; Relocations;
StringTableBuilder ShStrTabBuilder; StringTableBuilder ShStrTabBuilder;
/// @} /// @}
@ -234,7 +234,7 @@ class ELFObjectWriter : public MCObjectWriter {
SectionIndexMapTy &SectionIndexMap, SectionIndexMapTy &SectionIndexMap,
const RevGroupMapTy &RevGroupMap); const RevGroupMapTy &RevGroupMap);
void createRelocationSection(MCAssembler &Asm, const MCSectionData &SD); void createRelocationSection(MCAssembler &Asm, const MCSectionELF &Sec);
void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout); void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
@ -264,7 +264,7 @@ class ELFObjectWriter : public MCObjectWriter {
uint64_t Size, uint32_t Link, uint32_t Info, uint64_t Size, uint32_t Link, uint32_t Info,
uint64_t Alignment, uint64_t EntrySize); uint64_t Alignment, uint64_t EntrySize);
void writeRelocations(const MCAssembler &Asm, const MCSectionData &SD); void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
bool bool
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
@ -792,7 +792,9 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target, const MCFixup &Fixup, MCValue Target,
bool &IsPCRel, uint64_t &FixedValue) { bool &IsPCRel, uint64_t &FixedValue) {
const MCSectionData *FixupSection = Fragment->getParent(); const MCSectionData *FixupSectionD = Fragment->getParent();
const MCSectionELF &FixupSection =
cast<MCSectionELF>(FixupSectionD->getSection());
uint64_t C = Target.getConstant(); uint64_t C = Target.getConstant();
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
@ -822,7 +824,7 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
assert(!SymB.isAbsolute() && "Should have been folded"); assert(!SymB.isAbsolute() && "Should have been folded");
const MCSection &SecB = SymB.getSection(); const MCSection &SecB = SymB.getSection();
if (&SecB != &FixupSection->getSection()) if (&SecB != &FixupSection)
Asm.getContext().FatalError( Asm.getContext().FatalError(
Fixup.getLoc(), "Cannot represent a difference across sections"); Fixup.getLoc(), "Cannot represent a difference across sections");
@ -869,7 +871,7 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec) ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
: nullptr; : nullptr;
ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend); ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
Relocations[FixupSection].push_back(Rec); Relocations[&FixupSection].push_back(Rec);
return; return;
} }
@ -883,7 +885,7 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
UsedInReloc.insert(SymA); UsedInReloc.insert(SymA);
} }
ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend); ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
Relocations[FixupSection].push_back(Rec); Relocations[&FixupSection].push_back(Rec);
return; return;
} }
@ -973,7 +975,7 @@ void ELFObjectWriter::computeIndexMap(
unsigned Index = Sections.size(); unsigned Index = Sections.size();
SectionIndexMap[&Section] = Index; SectionIndexMap[&Section] = Index;
maybeAddToGroup(Asm, Sections, RevGroupMap, Section, Index); maybeAddToGroup(Asm, Sections, RevGroupMap, Section, Index);
createRelocationSection(Asm, SD); createRelocationSection(Asm, Section);
} }
} }
@ -1123,15 +1125,12 @@ void ELFObjectWriter::computeSymbolTable(
} }
void ELFObjectWriter::createRelocationSection(MCAssembler &Asm, void ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
const MCSectionData &SD) { const MCSectionELF &Sec) {
if (Relocations[&SD].empty()) if (Relocations[&Sec].empty())
return; return;
MCContext &Ctx = Asm.getContext(); MCContext &Ctx = Asm.getContext();
const MCSectionELF &Section = const StringRef SectionName = Sec.getSectionName();
static_cast<const MCSectionELF &>(SD.getSection());
const StringRef SectionName = Section.getSectionName();
std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
RelaSectionName += SectionName; RelaSectionName += SectionName;
@ -1142,12 +1141,12 @@ void ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
unsigned Flags = 0; unsigned Flags = 0;
if (Section.getFlags() & ELF::SHF_GROUP) if (Sec.getFlags() & ELF::SHF_GROUP)
Flags = ELF::SHF_GROUP; Flags = ELF::SHF_GROUP;
const MCSectionELF *RelaSection = Ctx.createELFRelSection( const MCSectionELF *RelaSection = Ctx.createELFRelSection(
RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
Flags, EntrySize, Section.getGroup(), &Section); Flags, EntrySize, Sec.getGroup(), &Sec);
MCSectionData &RelSD = Asm.getOrCreateSectionData(*RelaSection); MCSectionData &RelSD = Asm.getOrCreateSectionData(*RelaSection);
RelSD.setAlignment(is64Bit() ? 8 : 4); RelSD.setAlignment(is64Bit() ? 8 : 4);
} }
@ -1316,8 +1315,8 @@ void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
} }
void ELFObjectWriter::writeRelocations(const MCAssembler &Asm, void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
const MCSectionData &SD) { const MCSectionELF &Sec) {
std::vector<ELFRelocationEntry> &Relocs = Relocations[&SD]; std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
// Sort the relocation entries. Most targets just sort by Offset, but some // Sort the relocation entries. Most targets just sort by Offset, but some
// (e.g., MIPS) have additional constraints. // (e.g., MIPS) have additional constraints.
@ -1552,13 +1551,10 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
uint64_t SecStart = OS.tell(); uint64_t SecStart = OS.tell();
unsigned Type = Sec.getType(); unsigned Type = Sec.getType();
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) { if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
writeDataSectionData(Asm, Layout, SD); writeDataSectionData(Asm, Layout, SD);
} else { else
const MCSectionELF &RelocatedSec = *Sec.getAssociatedSection(); writeRelocations(Asm, *Sec.getAssociatedSection());
MCSectionData &RelocatedSecD = Asm.getOrCreateSectionData(RelocatedSec);
writeRelocations(Asm, RelocatedSecD);
}
uint64_t SecEnd = OS.tell(); uint64_t SecEnd = OS.tell();
SectionOffsets.push_back(std::make_pair(SecStart, SecEnd)); SectionOffsets.push_back(std::make_pair(SecStart, SecEnd));