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
This commit is contained in:
Peter Collingbourne 2019-08-01 01:38:53 +00:00
parent d48324ff6f
commit fbc563e2cb
3 changed files with 94 additions and 8 deletions

View File

@ -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);

View File

@ -1,5 +1,5 @@
; RUN: llc -data-sections=1 -mtriple x86_64-pc-linux-gnu < %s | FileCheck %s
; RUN: llc -data-sections=0 -mtriple x86_64-pc-linux-gnu < %s | FileCheck %s
; RUN: llc -data-sections=1 -mtriple x86_64-pc-linux-gnu < %s | FileCheck %s --check-prefix=DSECTIONS --check-prefix=CHECK
; RUN: llc -data-sections=0 -mtriple x86_64-pc-linux-gnu < %s | FileCheck %s --check-prefix=NDSECTIONS --check-prefix=CHECK
@a = global i32 1
@b = global i32 2, !associated !0
@ -28,9 +28,14 @@
@j = global i32 1, section "bbb", !associated !4
@k = global i32 1, !associated !4
!4 = !{i32* @h}
; CHECK-DAG: .section aaa,"aw",@progbits
; CHECK-DAG: .section bbb,"awo",@progbits,h,unique,1
; CHECK-DAG: .section bbb,"awo",@progbits,h,unique,2
; NDSECTIONS-DAG: .section aaa,"aw",@progbits
; NDSECTIONS-DAG: .section bbb,"awo",@progbits,h,unique,1
; NDSECTIONS-DAG: .section bbb,"awo",@progbits,h,unique,2
; DSECTIONS-DAG: .section aaa,"aw",@progbits,unique,1
; DSECTIONS-DAG: .section bbb,"awo",@progbits,h,unique,2
; DSECTIONS-DAG: .section bbb,"awo",@progbits,h,unique,3
; CHECK-DAG: .section .data.k,"awo",@progbits,h
; Non-GlobalValue metadata.
@ -47,4 +52,5 @@
@n = alias i32, i32* inttoptr (i64 add (i64 ptrtoint (i32* @a to i64), i64 1297036692682702848) to i32*)
@o = global i32 1, section "eee", !associated !7
!7 = !{i32* @n}
; CHECK-DAG: .section eee,"awo",@progbits,n,unique,3
; NDSECTIONS-DAG: .section eee,"awo",@progbits,n,unique,3
; DSECTIONS-DAG: .section eee,"awo",@progbits,n,unique,6

View File

@ -0,0 +1,65 @@
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections=1 -data-sections=1 | FileCheck %s -check-prefix=SECTIONS
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections=0 -data-sections=0 | FileCheck %s -check-prefix=NSECTIONS
define void @f() section "aaa" { ret void }
define void @g() section "aaa" { ret void }
define void @h() { ret void }
@x = global i32 1, section "aaa"
@y = global i32 1, section "aaa"
@z = global i32 1
define void @i() section "aaa" { ret void }
@w = global i32 1, section "aaa"
; NSECTIONS: .section aaa,"ax",@progbits
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: f:
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: g:
; NSECTIONS: .text
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: h:
; NSECTIONS: .section aaa,"ax",@progbits
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: i:
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: x:
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: y:
; NSECTIONS: .data
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: z:
; NSECTIONS: .section aaa,"ax",@progbits
; NSECTIONS-NOT: {{\.section|\.text|\.data}}
; NSECTIONS: w:
; SECTIONS: .section aaa,"ax",@progbits,unique,1
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: f:
; SECTIONS: .section aaa,"ax",@progbits,unique,2
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: g:
; SECTIONS: .section .text.h,"ax",@progbits
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: h:
; SECTIONS: .section aaa,"ax",@progbits,unique,3
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: i:
; SECTIONS: .section aaa,"aw",@progbits,unique,4
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: x:
; SECTIONS: .section aaa,"aw",@progbits,unique,5
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: y:
; SECTIONS: .section .data.z,"aw",@progbits
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: z:
; SECTIONS: .section aaa,"aw",@progbits,unique,6
; SECTIONS-NOT: {{\.section|\.text|\.data}}
; SECTIONS: w: