[PATCH] D46874: [MC] - Add .stack_size sections into groups and link them with .text

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 15 05:49:46 PDT 2018


grimar created this revision.
grimar added reviewers: seaneveson, hfinkel, MatzeB.

https://reviews.llvm.org/D39788 added a '.stack-size' section containing metadata on function stack sizes
to output ELF files behind the new -stack-size-section flag.

This change does following two things on top:

1. Imagine the case when there are -ffunction-sections flag given and there are text sections in COMDATs. The patch adds a '.stack-size' section into corresponding COMDAT group, so that linker will be able to eliminate them fast during resolving the COMDATs.
2. Patch sets a SHF_LINK_ORDER flag and links '.stack-size' with the corresponding .text. With that linker will be able to do -gc-sections on dead stack sizes sections.


https://reviews.llvm.org/D46874

Files:
  include/llvm/MC/MCObjectFileInfo.h
  lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  lib/MC/MCObjectFileInfo.cpp
  test/CodeGen/stack-size-section.cpp


Index: test/CodeGen/stack-size-section.cpp
===================================================================
--- test/CodeGen/stack-size-section.cpp
+++ test/CodeGen/stack-size-section.cpp
@@ -0,0 +1,29 @@
+// REQUIRES: x86-registered-target
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fstack-size-section -ffunction-sections %s -S -o - | FileCheck %s
+
+// Check we add SHF_LINK_ORDER for .stack_sizes and link it with the corresponding .text sections.
+
+// CHECK: .section        .text._Z3barv,"ax", at progbits
+// CHECK: .section        .stack_sizes,"o", at progbits,.text._Z3barv,unique,0
+
+// CHECK: .section        .text._Z3foov,"ax", at progbits
+// CHECK: .section        .stack_sizes,"o", at progbits,.text._Z3foov,unique,1
+
+// Check we add .stack_size section to COMDAT with the corresponding .text section if such COMDAT exists. 
+
+// CHECK: .section        .text._Z4fooTIiET_v,"axG", at progbits,_Z4fooTIiET_v,comdat
+// CHECK: .section        .stack_sizes,"Go", at progbits,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v,unique,2
+
+int bar() {
+  return 0;
+}
+
+template <class T>
+T fooT() {
+ return T();
+}
+
+int foo() {
+  return fooT<int>();
+}
Index: lib/MC/MCObjectFileInfo.cpp
===================================================================
--- lib/MC/MCObjectFileInfo.cpp
+++ lib/MC/MCObjectFileInfo.cpp
@@ -948,3 +948,22 @@
   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
                             0, utostr(Hash));
 }
+
+MCSection *MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec,
+                                                  unsigned ID) const {
+  if (Env != IsELF)
+    return StackSizesSection;
+
+  unsigned Flags = ELF::SHF_LINK_ORDER;
+
+  StringRef GroupName;
+  const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
+  if (const MCSymbol *Group = ElfSec.getGroup()) {
+    GroupName = Group->getName();
+    Flags |= ELF::SHF_GROUP;
+  }
+
+  const MCSymbolELF *Link = cast<MCSymbolELF>(TextSec.getBeginSymbol());
+  return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
+                            GroupName, ID, Link);
+}
Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -989,7 +989,8 @@
   if (!MF.getTarget().Options.EmitStackSizeSection)
     return;
 
-  MCSection *StackSizeSection = getObjFileLowering().getStackSizesSection();
+  MCSection *StackSizeSection = getObjFileLowering().getStackSizesSection(
+      *getCurrentSection(), MF.getFunctionNumber());
   if (!StackSizeSection)
     return;
 
Index: include/llvm/MC/MCObjectFileInfo.h
===================================================================
--- include/llvm/MC/MCObjectFileInfo.h
+++ include/llvm/MC/MCObjectFileInfo.h
@@ -299,7 +299,7 @@
   MCSection *getStackMapSection() const { return StackMapSection; }
   MCSection *getFaultMapSection() const { return FaultMapSection; }
 
-  MCSection *getStackSizesSection() const { return StackSizesSection; }
+  MCSection *getStackSizesSection(const MCSection &TextSec, unsigned ID) const;
 
   // ELF specific sections.
   MCSection *getDataRelROSection() const { return DataRelROSection; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46874.146797.patch
Type: text/x-patch
Size: 3288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180515/4e1a0f81/attachment.bin>


More information about the llvm-commits mailing list