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; }
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: 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: test/CodeGen/X86/stack-size-section.ll
===================================================================
--- test/CodeGen/X86/stack-size-section.ll
+++ test/CodeGen/X86/stack-size-section.ll
@@ -2,7 +2,7 @@
 
 ; CHECK-LABEL: func1:
 ; CHECK-NEXT: .Lfunc_begin0:
-; CHECK: .section .stack_sizes,"",@progbits
+; CHECK: .section .stack_sizes,"o",@progbits,.text,unique,0
 ; CHECK-NEXT: .quad .Lfunc_begin0
 ; CHECK-NEXT: .byte 8
 define void @func1(i32, i32) #0 {
@@ -13,7 +13,7 @@
 
 ; CHECK-LABEL: func2:
 ; CHECK-NEXT: .Lfunc_begin1:
-; CHECK: .section .stack_sizes,"",@progbits
+; CHECK: .section .stack_sizes,"o",@progbits,.text,unique,1
 ; CHECK-NEXT: .quad .Lfunc_begin1
 ; CHECK-NEXT: .byte 24
 define void @func2() #0 {
Index: test/CodeGen/X86/stack-size-section2.ll
===================================================================
--- test/CodeGen/X86/stack-size-section2.ll
+++ test/CodeGen/X86/stack-size-section2.ll
@@ -0,0 +1,29 @@
+; RUN: llc < %s -mtriple=x86_64-linux -stack-size-section -function-sections | FileCheck %s
+
+; Check we add SHF_LINK_ORDER for .stack_sizes and link it with the corresponding .text sections.
+
+; CHECK: .section        .text._Z3barv,"ax",@progbits
+; CHECK: .section        .stack_sizes,"o",@progbits,.text._Z3barv,unique,0
+
+; CHECK: .section        .text._Z3foov,"ax",@progbits
+; CHECK: .section        .stack_sizes,"o",@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",@progbits,_Z4fooTIiET_v,comdat
+; CHECK: .section        .stack_sizes,"Go",@progbits,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v,unique,2
+
+$_Z4fooTIiET_v = comdat any
+
+define dso_local i32 @_Z3barv() {
+  ret i32 0
+}
+
+define dso_local i32 @_Z3foov() {
+  %1 = call i32 @_Z4fooTIiET_v()
+  ret i32 %1
+}
+
+define linkonce_odr dso_local i32 @_Z4fooTIiET_v() comdat {
+  ret i32 0
+}