From fbc563e2cb6c5f1d0200b390513506b6aca4d2e9 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 1 Aug 2019 01:38:53 +0000 Subject: Create unique, but identically-named ELF sections for explicitly-sectioned functions and globals when using -function-sections and -data-sections. This allows functions and globals to to be reordered later in the linking phase (using the -symbol-ordering-file) even though reordering will be limited to the scope of the explicit section. Patch by Rahman Lavaee! Differential Revision: https://reviews.llvm.org/D65478 llvm-svn: 367501 --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp') diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index ac7c4c50ae4c..7a93a262bb9a 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -586,15 +586,30 @@ MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( Flags |= ELF::SHF_GROUP; } + bool EmitUniqueSection = false; + + // If we have -ffunction-sections or -fdata-sections then we should emit the + // global value to a uniqued section of the same name. + if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { + if (Kind.isText()) + EmitUniqueSection = TM.getFunctionSections(); + else + EmitUniqueSection = TM.getDataSections(); + } + EmitUniqueSection |= GO->hasComdat(); + // A section can have at most one associated section. Put each global with // MD_associated in a unique section. - unsigned UniqueID = MCContext::GenericSectionID; const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); if (AssociatedSymbol) { - UniqueID = NextUniqueID++; + EmitUniqueSection = true; Flags |= ELF::SHF_LINK_ORDER; } + unsigned UniqueID = MCContext::GenericSectionID; + if (EmitUniqueSection) + UniqueID = NextUniqueID++; + MCSectionELF *Section = getContext().getELFSection( SectionName, getELFSectionType(SectionName, Kind), Flags, getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol); -- cgit v1.2.3