From 2fa06b171b46b478c1c655995c9f1afe1f6ffd9b Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 12 Feb 2015 23:29:51 +0000 Subject: [PATCH] Add support for having multiple sections with the same name and comdat. Using this in combination with -ffunction-sections allows LLVM to output a .o file with mulitple sections named .text. This saves space by avoiding long unique names of the form .text.. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228980 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/CommandFlags.h | 5 ++++ include/llvm/MC/MCContext.h | 4 +++ include/llvm/MC/MCSectionELF.h | 10 ++++--- include/llvm/Target/TargetMachine.h | 2 ++ include/llvm/Target/TargetOptions.h | 6 +++-- lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 31 ++++++++++++---------- lib/MC/ELFObjectWriter.cpp | 2 +- lib/MC/MCContext.cpp | 20 +++++++++----- lib/MC/MCParser/ELFAsmParser.cpp | 14 ++++++++-- lib/MC/MCSectionELF.cpp | 7 +++++ test/CodeGen/X86/non-unique-sections.ll | 15 +++++++++++ test/MC/ELF/section-unique.s | 39 ++++++++++++++++++++++++++++ 12 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 test/CodeGen/X86/non-unique-sections.ll create mode 100644 test/MC/ELF/section-unique.s diff --git a/include/llvm/CodeGen/CommandFlags.h b/include/llvm/CodeGen/CommandFlags.h index 973c5954f9a..9f86429cbf1 100644 --- a/include/llvm/CodeGen/CommandFlags.h +++ b/include/llvm/CodeGen/CommandFlags.h @@ -207,6 +207,10 @@ FunctionSections("function-sections", cl::desc("Emit functions into separate sections"), cl::init(false)); +cl::opt UniqueSectionNames("unique-section-names", + cl::desc("Give unique names to every section"), + cl::init(true)); + cl::opt JTableType("jump-table-type", cl::desc("Choose the type of Jump-Instruction Table for jumptable."), @@ -284,6 +288,7 @@ static inline TargetOptions InitTargetOptionsFromCodeGenFlags() { Options.UseInitArray = !UseCtors; Options.DataSections = DataSections; Options.FunctionSections = FunctionSections; + Options.UniqueSectionNames = UniqueSectionNames; Options.MCOptions = InitMCTargetOptionsFromFlags(); Options.JTType = JTableType; diff --git a/include/llvm/MC/MCContext.h b/include/llvm/MC/MCContext.h index 44ee424f827..8bacbb8cd8f 100644 --- a/include/llvm/MC/MCContext.h +++ b/include/llvm/MC/MCContext.h @@ -277,6 +277,10 @@ namespace llvm { unsigned Flags, unsigned EntrySize, StringRef Group); + const MCSectionELF *getELFSection(StringRef Section, unsigned Type, + unsigned Flags, unsigned EntrySize, + StringRef Group, bool Unique); + void renameELFSection(const MCSectionELF *Section, StringRef Name); const MCSectionELF *CreateELFGroupSection(); diff --git a/include/llvm/MC/MCSectionELF.h b/include/llvm/MC/MCSectionELF.h index 0500c30f169..6d864b4a45b 100644 --- a/include/llvm/MC/MCSectionELF.h +++ b/include/llvm/MC/MCSectionELF.h @@ -39,6 +39,8 @@ class MCSectionELF : public MCSection { /// below. unsigned Flags; + bool Unique; + /// EntrySize - The size of each entry in this section. This size only /// makes sense for sections that contain fixed-sized entries. If a /// section does not contain fixed-sized entries 'EntrySize' will be 0. @@ -48,10 +50,10 @@ class MCSectionELF : public MCSection { private: friend class MCContext; - MCSectionELF(StringRef Section, unsigned type, unsigned flags, - SectionKind K, unsigned entrySize, const MCSymbol *group) - : MCSection(SV_ELF, K), SectionName(Section), Type(type), Flags(flags), - EntrySize(entrySize), Group(group) {} + MCSectionELF(StringRef Section, unsigned type, unsigned flags, SectionKind K, + unsigned entrySize, const MCSymbol *group, bool Unique) + : MCSection(SV_ELF, K), SectionName(Section), Type(type), Flags(flags), + Unique(Unique), EntrySize(entrySize), Group(group) {} ~MCSectionELF(); void setSectionName(StringRef Name) { SectionName = Name; } diff --git a/include/llvm/Target/TargetMachine.h b/include/llvm/Target/TargetMachine.h index 028f9e2347b..669cef5c3d2 100644 --- a/include/llvm/Target/TargetMachine.h +++ b/include/llvm/Target/TargetMachine.h @@ -171,6 +171,8 @@ public: return Options.MCOptions.AsmVerbose; } + bool getUniqueSectionNames() const { return Options.UniqueSectionNames; } + /// Return true if data objects should be emitted into their own section, /// corresponds to -fdata-sections. bool getDataSections() const { diff --git a/include/llvm/Target/TargetOptions.h b/include/llvm/Target/TargetOptions.h index 9ab8242578f..f447fd60110 100644 --- a/include/llvm/Target/TargetOptions.h +++ b/include/llvm/Target/TargetOptions.h @@ -78,8 +78,8 @@ namespace llvm { EnableFastISel(false), PositionIndependentExecutable(false), UseInitArray(false), DisableIntegratedAS(false), CompressDebugSections(false), FunctionSections(false), - DataSections(false), TrapUnreachable(false), TrapFuncName(), - FloatABIType(FloatABI::Default), + DataSections(false), UniqueSectionNames(true), TrapUnreachable(false), + TrapFuncName(), FloatABIType(FloatABI::Default), AllowFPOpFusion(FPOpFusion::Standard), JTType(JumpTable::Single), FCFI(false), ThreadModel(ThreadModel::POSIX), CFIType(CFIntegrity::Sub), CFIEnforcing(false), CFIFuncName() {} @@ -198,6 +198,8 @@ namespace llvm { /// Emit data into separate sections. unsigned DataSections : 1; + unsigned UniqueSectionNames : 1; + /// Emit target-specific trap instruction for 'unreachable' IR instructions. unsigned TrapUnreachable : 1; diff --git a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 4f46e37d39c..8aa66531b3d 100644 --- a/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -228,25 +228,25 @@ const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( /// DataSections. static StringRef getSectionPrefixForGlobal(SectionKind Kind) { if (Kind.isText()) - return ".text."; + return ".text"; if (Kind.isReadOnly()) - return ".rodata."; + return ".rodata"; if (Kind.isBSS()) - return ".bss."; + return ".bss"; if (Kind.isThreadData()) - return ".tdata."; + return ".tdata"; if (Kind.isThreadBSS()) - return ".tbss."; + return ".tbss"; if (Kind.isDataNoRel()) - return ".data."; + return ".data"; if (Kind.isDataRelLocal()) - return ".data.rel.local."; + return ".data.rel.local"; if (Kind.isDataRel()) - return ".data.rel."; + return ".data.rel"; if (Kind.isReadOnlyWithRelLocal()) - return ".data.rel.ro.local."; + return ".data.rel.ro.local"; assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); - return ".data.rel.ro."; + return ".data.rel.ro"; } const MCSection *TargetLoweringObjectFileELF:: @@ -268,16 +268,19 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, StringRef Prefix = getSectionPrefixForGlobal(Kind); SmallString<128> Name(Prefix); - TM.getNameWithPrefix(Name, GV, Mang, true); - + bool UniqueSectionNames = TM.getUniqueSectionNames(); + if (UniqueSectionNames) { + Name.push_back('.'); + TM.getNameWithPrefix(Name, GV, Mang, true); + } StringRef Group = ""; if (const Comdat *C = getELFComdat(GV)) { Flags |= ELF::SHF_GROUP; Group = C->getName(); } - return getContext().getELFSection( - Name.str(), getELFSectionType(Name.str(), Kind), Flags, 0, Group); + return getContext().getELFSection(Name, getELFSectionType(Name, Kind), + Flags, 0, Group, !UniqueSectionNames); } if (Kind.isText()) return TextSection; diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index 5bddb09393f..b4948e659fb 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -1151,7 +1151,7 @@ ELFObjectWriter::createRelocationSection(MCAssembler &Asm, const MCSectionELF *RelaSection = Ctx.getELFSection( RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, - Flags, EntrySize, Group); + Flags, EntrySize, Group, true); return &Asm.getOrCreateSectionData(*RelaSection); } diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 35609893e28..721edd451ee 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -272,12 +272,13 @@ void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, unsigned Flags, unsigned EntrySize, - StringRef Group) { + StringRef Group, bool Unique) { // Do the lookup, if we have a hit, return it. auto IterBool = ELFUniquingMap.insert( std::make_pair(SectionGroupPair(Section, Group), nullptr)); auto &Entry = *IterBool.first; - if (!IterBool.second) return Entry.second; + if (!IterBool.second && !Unique) + return Entry.second; MCSymbol *GroupSym = nullptr; if (!Group.empty()) @@ -292,15 +293,22 @@ const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, Kind = SectionKind::getReadOnly(); MCSectionELF *Result = new (*this) - MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym); - Entry.second = Result; + MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, Unique); + if (!Unique) + Entry.second = Result; return Result; } +const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, + unsigned Flags, unsigned EntrySize, + StringRef Group) { + return getELFSection(Section, Type, Flags, EntrySize, Group, false); +} + const MCSectionELF *MCContext::CreateELFGroupSection() { MCSectionELF *Result = - new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, - SectionKind::getReadOnly(), 4, nullptr); + new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, + SectionKind::getReadOnly(), 4, nullptr, false); return Result; } diff --git a/lib/MC/MCParser/ELFAsmParser.cpp b/lib/MC/MCParser/ELFAsmParser.cpp index 21c65ce0b83..7a120a105d2 100644 --- a/lib/MC/MCParser/ELFAsmParser.cpp +++ b/lib/MC/MCParser/ELFAsmParser.cpp @@ -378,6 +378,8 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { unsigned Flags = 0; const MCExpr *Subsection = nullptr; bool UseLastGroup = false; + StringRef UniqueStr; + bool Unique = false; // Set the defaults first. if (SectionName == ".fini" || SectionName == ".init" || @@ -462,6 +464,14 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { return TokError("Linkage must be 'comdat'"); } } + if (getLexer().is(AsmToken::Comma)) { + Lex(); + if (getParser().parseIdentifier(UniqueStr)) + return TokError("expected identifier in directive"); + if (UniqueStr != "unique") + return TokError("expected 'unique'"); + Unique = true; + } } } @@ -509,8 +519,8 @@ EndStmt: } } - const MCSection *ELFSection = - getContext().getELFSection(SectionName, Type, Flags, Size, GroupName); + const MCSection *ELFSection = getContext().getELFSection( + SectionName, Type, Flags, Size, GroupName, Unique); getStreamer().SwitchSection(ELFSection, Subsection); if (getContext().getGenDwarfForAssembly()) { diff --git a/lib/MC/MCSectionELF.cpp b/lib/MC/MCSectionELF.cpp index fb364c87a03..da3868273a7 100644 --- a/lib/MC/MCSectionELF.cpp +++ b/lib/MC/MCSectionELF.cpp @@ -24,6 +24,9 @@ MCSectionELF::~MCSectionELF() {} // anchor. bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const { + if (Unique) + return false; + // FIXME: Does .section .bss/.data/.text work everywhere?? if (Name == ".text" || Name == ".data" || (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) @@ -144,6 +147,10 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, printName(OS, Group->getName()); OS << ",comdat"; } + + if (Unique) + OS << ",unique"; + OS << '\n'; if (Subsection) diff --git a/test/CodeGen/X86/non-unique-sections.ll b/test/CodeGen/X86/non-unique-sections.ll new file mode 100644 index 00000000000..e588b9dda98 --- /dev/null +++ b/test/CodeGen/X86/non-unique-sections.ll @@ -0,0 +1,15 @@ +; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -unique-section-names=false | FileCheck %s + +; CHECK: .section .text,"ax",@progbits,unique +; CHECK-NOT: section +; CHECK: f: +define void @f() { + ret void +} + +; CHECK: .section .text,"ax",@progbits,unique +; CHECK-NOT: section +; CHECK: g: +define void @g() { + ret void +} diff --git a/test/MC/ELF/section-unique.s b/test/MC/ELF/section-unique.s new file mode 100644 index 00000000000..b482af3d101 --- /dev/null +++ b/test/MC/ELF/section-unique.s @@ -0,0 +1,39 @@ +// RUN: llvm-mc -triple x86_64-pc-linux-gnu %s -o - | FileCheck %s +// RUN: llvm-mc -triple x86_64-pc-linux-gnu %s -filetype=obj -o - | llvm-readobj -t | FileCheck %s --check-prefix=OBJ + + .section .text,"ax",@progbits,unique + .globl f +f: + nop + + .section .text,"ax",@progbits,unique + .globl g +g: + nop + +// test that f and g are in different sections. + +// CHECK: .section .text,"ax",@progbits,unique +// CHECK: f: + +// CHECK: .section .text,"ax",@progbits,unique +// CHECK: g: + +// OBJ: Symbol { +// OBJ: Name: f +// OBJ: Value: 0x0 +// OBJ: Size: 0 +// OBJ: Binding: Global +// OBJ: Type: None +// OBJ: Other: 0 +// OBJ: Section: .text (0x4) +// OBJ: } +// OBJ: Symbol { +// OBJ: Name: g +// OBJ: Value: 0x0 +// OBJ: Size: 0 +// OBJ: Binding: Global +// OBJ: Type: None +// OBJ: Other: 0 +// OBJ: Section: .text (0x5) +// OBJ: } -- 2.11.0