const RevGroupMapTy &RevGroupMap,
unsigned NumRegularSections);
- void computeIndexMap(MCAssembler &Asm,
+ void ComputeIndexMap(MCAssembler &Asm,
SectionIndexMapTy &SectionIndexMap,
- RelMapTy &RelMap);
+ const RelMapTy &RelMap);
- MCSectionData *createRelocationSection(MCAssembler &Asm,
- const MCSectionData &SD);
+ void CreateRelocationSections(MCAssembler &Asm, RelMapTy &RelMap);
void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
// Create the sections that show up in the symbol table. Currently
// those are the .note.GNU-stack section and the group sections.
- void createIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
+ void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
GroupMapTy &GroupMap,
RevGroupMapTy &RevGroupMap,
SectionIndexMapTy &SectionIndexMap,
- RelMapTy &RelMap);
+ const RelMapTy &RelMap);
void ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
- void writeSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
+ void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap,
- const RelMapTy &RelMap,
const SectionOffsetMapTy &SectionOffsetMap);
void ComputeSectionOrder(MCAssembler &Asm,
bool IsPCRel) const override;
void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
- void writeSection(MCAssembler &Asm,
+ void WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
- const RelMapTy &RelMap,
uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size, uint64_t Alignment,
const MCSectionELF &Section);
return true;
}
-void ELFObjectWriter::computeIndexMap(MCAssembler &Asm,
+void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
SectionIndexMapTy &SectionIndexMap,
- RelMapTy &RelMap) {
+ const RelMapTy &RelMap) {
unsigned Index = 1;
for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
- const MCSectionData &SD = *it;
const MCSectionELF &Section =
- static_cast<const MCSectionELF &>(SD.getSection());
+ static_cast<const MCSectionELF &>(it->getSection());
if (Section.getType() == ELF::SHT_GROUP ||
Section.getType() == ELF::SHT_REL ||
Section.getType() == ELF::SHT_RELA)
continue;
SectionIndexMap[&Section] = Index++;
- if (MCSectionData *RelSD = createRelocationSection(Asm, SD)) {
- const MCSectionELF *RelSection =
- static_cast<const MCSectionELF *>(&RelSD->getSection());
- RelMap[RelSection] = &Section;
+ const MCSectionELF *RelSection = RelMap.lookup(&Section);
+ if (RelSection)
SectionIndexMap[RelSection] = Index++;
- }
}
}
UndefinedSymbolData[i].SymbolData->setIndex(Index++);
}
-MCSectionData *
-ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
- const MCSectionData &SD) {
- if (Relocations[&SD].empty())
- return nullptr;
+void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
+ RelMapTy &RelMap) {
+ for (MCAssembler::const_iterator it = Asm.begin(),
+ ie = Asm.end(); it != ie; ++it) {
+ const MCSectionData &SD = *it;
+ if (Relocations[&SD].empty())
+ continue;
- MCContext &Ctx = Asm.getContext();
- const MCSectionELF &Section =
- static_cast<const MCSectionELF &>(SD.getSection());
+ MCContext &Ctx = Asm.getContext();
+ const MCSectionELF &Section =
+ static_cast<const MCSectionELF&>(SD.getSection());
- const StringRef SectionName = Section.getSectionName();
- std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
- RelaSectionName += SectionName;
+ const StringRef SectionName = Section.getSectionName();
+ std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
+ RelaSectionName += SectionName;
- unsigned EntrySize;
- if (hasRelocationAddend())
- EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
- else
- EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
+ unsigned EntrySize;
+ if (hasRelocationAddend())
+ EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
+ else
+ EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
- unsigned Flags = 0;
- StringRef Group = "";
- if (Section.getFlags() & ELF::SHF_GROUP) {
- Flags = ELF::SHF_GROUP;
- Group = Section.getGroup()->getName();
- }
+ unsigned Flags = 0;
+ StringRef Group = "";
+ if (Section.getFlags() & ELF::SHF_GROUP) {
+ Flags = ELF::SHF_GROUP;
+ Group = Section.getGroup()->getName();
+ }
- const MCSectionELF *RelaSection = Ctx.getELFSection(
- RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
- Flags, EntrySize, Group, true);
- return &Asm.getOrCreateSectionData(*RelaSection);
+ const MCSectionELF *RelaSection =
+ Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
+ ELF::SHT_RELA : ELF::SHT_REL, Flags,
+ EntrySize, Group);
+ RelMap[&Section] = RelaSection;
+ Asm.getOrCreateSectionData(*RelaSection);
+ }
}
static SmallVector<char, 128>
void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
const RelMapTy &RelMap) {
- for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
- MCSectionData &RelSD = *it;
- const MCSectionELF &RelSection =
- static_cast<const MCSectionELF &>(RelSD.getSection());
+ for (MCAssembler::const_iterator it = Asm.begin(),
+ ie = Asm.end(); it != ie; ++it) {
+ const MCSectionData &SD = *it;
+ const MCSectionELF &Section =
+ static_cast<const MCSectionELF&>(SD.getSection());
- unsigned Type = RelSection.getType();
- if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
+ const MCSectionELF *RelaSection = RelMap.lookup(&Section);
+ if (!RelaSection)
continue;
+ MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
+ RelaSD.setAlignment(is64Bit() ? 8 : 4);
- const MCSectionELF *Section = RelMap.lookup(&RelSection);
- MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
- RelSD.setAlignment(is64Bit() ? 8 : 4);
-
- MCDataFragment *F = new MCDataFragment(&RelSD);
- WriteRelocationsFragment(Asm, F, &SD);
+ MCDataFragment *F = new MCDataFragment(&RelaSD);
+ WriteRelocationsFragment(Asm, F, &*it);
}
}
ShStrTabBuilder.data().end());
}
-void ELFObjectWriter::createIndexedSections(MCAssembler &Asm,
+void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
MCAsmLayout &Layout,
GroupMapTy &GroupMap,
RevGroupMapTy &RevGroupMap,
SectionIndexMapTy &SectionIndexMap,
- RelMapTy &RelMap) {
+ const RelMapTy &RelMap) {
MCContext &Ctx = Asm.getContext();
// Build the groups
GroupMap[Group] = SignatureSymbol;
}
- computeIndexMap(Asm, SectionIndexMap, RelMap);
+ ComputeIndexMap(Asm, SectionIndexMap, RelMap);
// Add sections to the groups
for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
}
}
-void ELFObjectWriter::writeSection(MCAssembler &Asm,
+void ELFObjectWriter::WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
- const RelMapTy &RelMap,
uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size,
uint64_t Alignment,
case ELF::SHT_REL:
case ELF::SHT_RELA: {
- sh_link = SymbolTableIndex;
+ const MCSectionELF *SymtabSection;
+ const MCSectionELF *InfoSection;
+ SymtabSection =
+ Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0);
+ sh_link = SectionIndexMap.lookup(SymtabSection);
assert(sh_link && ".symtab not found");
- const MCSectionELF *InfoSection = RelMap.find(&Section)->second;
+
+ // Remove ".rel" and ".rela" prefixes.
+ unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
+ StringRef SectionName = Section.getSectionName().substr(SecNameLen);
+ StringRef GroupName =
+ Section.getGroup() ? Section.getGroup()->getName() : "";
+
+ InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS,
+ 0, 0, GroupName);
sh_info = SectionIndexMap.lookup(InfoSection);
break;
}
}
}
-void ELFObjectWriter::writeSectionHeader(
- MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout,
- const SectionIndexMapTy &SectionIndexMap, const RelMapTy &RelMap,
- const SectionOffsetMapTy &SectionOffsetMap) {
+void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
+ const GroupMapTy &GroupMap,
+ const MCAsmLayout &Layout,
+ const SectionIndexMapTy &SectionIndexMap,
+ const SectionOffsetMapTy &SectionOffsetMap) {
const unsigned NumSections = Asm.size() + 1;
std::vector<const MCSectionELF*> Sections;
uint64_t Size = GetSectionAddressSize(Layout, SD);
- writeSection(Asm, SectionIndexMap, RelMap, GroupSymbolIndex,
+ WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
SectionOffsetMap.lookup(&Section), Size,
SD.getAlignment(), Section);
}
CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
+ CreateRelocationSections(Asm, RelMap);
+
const unsigned NumUserAndRelocSections = Asm.size();
- createIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
+ CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
RevGroupMap, SectionIndexMap, RelMap);
const unsigned AllSections = Asm.size();
const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
WriteZeros(Padding);
// ... then the section header table ...
- writeSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, RelMap,
+ WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
SectionOffsetMap);
// ... and then the remaining sections ...