[llvm] r367501 - Create unique, but identically-named ELF sections for explicitly-sectioned functions and globals when using -function-sections and -data-sections.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 18:38:54 PDT 2019


Author: pcc
Date: Wed Jul 31 18:38:53 2019
New Revision: 367501

URL: http://llvm.org/viewvc/llvm-project?rev=367501&view=rev
Log:
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

Added:
    llvm/trunk/test/CodeGen/X86/explicit-elf-sections.ll
Modified:
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/trunk/test/CodeGen/X86/elf-associated.ll

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=367501&r1=367500&r2=367501&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Wed Jul 31 18:38:53 2019
@@ -586,15 +586,30 @@ MCSection *TargetLoweringObjectFileELF::
     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);

Modified: llvm/trunk/test/CodeGen/X86/elf-associated.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/elf-associated.ll?rev=367501&r1=367500&r2=367501&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/elf-associated.ll (original)
+++ llvm/trunk/test/CodeGen/X86/elf-associated.ll Wed Jul 31 18:38:53 2019
@@ -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", at progbits
-; CHECK-DAG: .section	bbb,"awo", at progbits,h,unique,1
-; CHECK-DAG: .section	bbb,"awo", at progbits,h,unique,2
+; NDSECTIONS-DAG: .section	aaa,"aw", at progbits
+; NDSECTIONS-DAG: .section	bbb,"awo", at progbits,h,unique,1
+; NDSECTIONS-DAG: .section	bbb,"awo", at progbits,h,unique,2
+
+; DSECTIONS-DAG: .section	aaa,"aw", at progbits,unique,1
+; DSECTIONS-DAG: .section	bbb,"awo", at progbits,h,unique,2
+; DSECTIONS-DAG: .section	bbb,"awo", at progbits,h,unique,3
+
 ; CHECK-DAG: .section	.data.k,"awo", at 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", at progbits,n,unique,3
+; NDSECTIONS-DAG: .section	eee,"awo", at progbits,n,unique,3
+; DSECTIONS-DAG: .section	eee,"awo", at progbits,n,unique,6

Added: llvm/trunk/test/CodeGen/X86/explicit-elf-sections.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/explicit-elf-sections.ll?rev=367501&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/explicit-elf-sections.ll (added)
+++ llvm/trunk/test/CodeGen/X86/explicit-elf-sections.ll Wed Jul 31 18:38:53 2019
@@ -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 }
+
+ at x = global i32 1, section "aaa"
+ at y = global i32 1, section "aaa"
+ at z = global i32 1
+
+define void @i() section "aaa" { ret void }
+
+ at w = global i32 1, section "aaa"
+
+; NSECTIONS:		.section	aaa,"ax", at 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", at 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", at progbits
+; NSECTIONS-NOT:	{{\.section|\.text|\.data}}
+; NSECTIONS:		w:
+
+
+; SECTIONS:	.section	aaa,"ax", at progbits,unique,1
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	f:
+; SECTIONS:	.section	aaa,"ax", at progbits,unique,2
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	g:
+; SECTIONS:	.section        .text.h,"ax", at progbits
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	h:
+; SECTIONS:	.section	aaa,"ax", at progbits,unique,3
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	i:
+; SECTIONS:	.section	aaa,"aw", at progbits,unique,4
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	x:
+; SECTIONS:	.section	aaa,"aw", at progbits,unique,5
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	y:
+; SECTIONS:	.section        .data.z,"aw", at progbits
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	z:
+; SECTIONS:	.section	aaa,"aw", at progbits,unique,6
+; SECTIONS-NOT:	{{\.section|\.text|\.data}}
+; SECTIONS:	w:
+
+




More information about the llvm-commits mailing list