diff --git a/include/llvm/MC/MCContext.h b/include/llvm/MC/MCContext.h index ca4eae53a88..a17e05d87c1 100644 --- a/include/llvm/MC/MCContext.h +++ b/include/llvm/MC/MCContext.h @@ -194,6 +194,7 @@ namespace llvm { StringMap MachOUniquingMap; std::map ELFUniquingMap; std::map COFFUniquingMap; + StringMap ELFRelSecNames; /// Do automatic reset in destructor bool AutoReset; @@ -304,6 +305,10 @@ namespace llvm { StringRef Group, bool Unique, const char *BeginSymName = nullptr); + const MCSectionELF *createELFRelSection(StringRef Name, unsigned Type, + unsigned Flags, unsigned EntrySize, + const MCSymbol *Group); + void renameELFSection(const MCSectionELF *Section, StringRef Name); const MCSectionELF *CreateELFGroupSection(); diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index c99a3ee5e26..6be37bfd641 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -1167,15 +1167,12 @@ ELFObjectWriter::createRelocationSection(MCAssembler &Asm, EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); unsigned Flags = 0; - StringRef Group = ""; - if (Section.getFlags() & ELF::SHF_GROUP) { + if (Section.getFlags() & ELF::SHF_GROUP) Flags = ELF::SHF_GROUP; - Group = Section.getGroup()->getName(); - } - const MCSectionELF *RelaSection = Ctx.getELFSection( + const MCSectionELF *RelaSection = Ctx.createELFRelSection( RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, - Flags, EntrySize, Group, true); + Flags, EntrySize, Section.getGroup()); return &Asm.getOrCreateSectionData(*RelaSection); } diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 09de4ee05e6..2afa21219fc 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -275,6 +275,18 @@ void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { const_cast(Section)->setSectionName(CachedName); } +const MCSectionELF * +MCContext::createELFRelSection(StringRef Name, unsigned Type, unsigned Flags, + unsigned EntrySize, const MCSymbol *Group) { + StringMap::iterator I; + bool Inserted; + std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true)); + + return new (*this) + MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(), + EntrySize, Group, true, nullptr); +} + const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, unsigned Flags, unsigned EntrySize, StringRef Group, bool Unique,