[llvm] ee5d1a0 - [AsmPrinter] Split up .gcc_except_table
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 2 14:36:35 PST 2020
Author: Fangrui Song
Date: 2020-11-02T14:36:25-08:00
New Revision: ee5d1a04494390ab8dbece1e32cb00312ecce4b1
URL: https://github.com/llvm/llvm-project/commit/ee5d1a04494390ab8dbece1e32cb00312ecce4b1
DIFF: https://github.com/llvm/llvm-project/commit/ee5d1a04494390ab8dbece1e32cb00312ecce4b1.diff
LOG: [AsmPrinter] Split up .gcc_except_table
MC currently produces monolithic .gcc_except_table section. GCC can split up .gcc_except_table:
* if comdat: `.section .gcc_except_table._Z6comdatv,"aG", at progbits,_Z6comdatv,comdat`
* otherwise, if -ffunction-sections: `.section .gcc_except_table._Z3fooi,"a", at progbits`
This ensures that (a) non-prevailing copies are discarded and (b)
.gcc_except_table associated to discarded text sections can be discarded by a
.gcc_except_table-aware linker (GNU ld, but not gold or LLD)
This patches matches the GCC behavior. If -fno-unique-section-names is
specified, we don't append the suffix. If -ffunction-sections is additionally specified,
use `.section ...,unique`.
Note, if clang driver communicates that the linker is LLD and we know it
is new (11.0.0 or later) we can use SHF_LINK_ORDER to avoid string table
costs, at least in the -fno-unique-section-names case. We cannot use it on GNU
ld because as of binutils 2.35 it does not support mixed SHF_LINK_ORDER &
non-SHF_LINK_ORDER components in an output section
https://sourceware.org/bugzilla/show_bug.cgi?id=26256
For RISC-V -mrelax, this patch additionally fixes an assembler-linker
interaction problem: because a section is shrinkable, the length of a call-site
code range is not a constant. Relocations referencing the associated text
section (STT_SECTION) are needed. However, a STB_LOCAL relocation referencing a
discarded section group member from outside the group is disallowed by the ELF
specification (PR46675):
```
// a.cc
inline int comdat() { try { throw 1; } catch (int) { return 1; } return 0; }
int main() { return comdat(); }
// b.cc
inline int comdat() { try { throw 1; } catch (int) { return 1; } return 0; }
int foo() { return comdat(); }
clang++ -target riscv64-linux -c a.cc b.cc -fPIC -mno-relax
ld.lld -shared a.o b.o => ld.lld: error: relocation refers to a symbol in a discarded section:
```
-fbasic-block-sections= is similar to RISC-V -mrelax: there are outstanding relocations.
Reviewed By: jrtc27, rahmanl
Differential Revision: https://reviews.llvm.org/D83655
Added:
llvm/test/CodeGen/X86/gcc_except_table-multi.ll
Modified:
llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
llvm/include/llvm/MC/MCObjectFileInfo.h
llvm/include/llvm/Target/TargetLoweringObjectFile.h
llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 625137a1f998..50488cfdef08 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -63,6 +63,8 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
MCSection *getSectionForJumpTable(const Function &F,
const TargetMachine &TM) const override;
+ MCSection *getSectionForLSDA(const Function &F,
+ const TargetMachine &TM) const override;
MCSection *
getSectionForMachineBasicBlock(const Function &F,
diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h
index 316086833d97..29bd13fe0a33 100644
--- a/llvm/include/llvm/MC/MCObjectFileInfo.h
+++ b/llvm/include/llvm/MC/MCObjectFileInfo.h
@@ -250,7 +250,6 @@ class MCObjectFileInfo {
MCSection *getDataSection() const { return DataSection; }
MCSection *getBSSSection() const { return BSSSection; }
MCSection *getReadOnlySection() const { return ReadOnlySection; }
- MCSection *getLSDASection() const { return LSDASection; }
MCSection *getCompactUnwindSection() const { return CompactUnwindSection; }
MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 793e45c5b925..eb6b683e4d68 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -118,6 +118,10 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
virtual MCSection *getSectionForJumpTable(const Function &F,
const TargetMachine &TM) const;
+ virtual MCSection *getSectionForLSDA(const Function &F,
+ const TargetMachine &TM) const {
+ return LSDASection;
+ }
virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
const Function &F) const;
diff --git a/llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp b/llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp
index 4bf14af0721e..a9eeb8f7c61a 100644
--- a/llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp
@@ -417,7 +417,8 @@ MCSymbol *EHStreamer::emitExceptionTable() {
bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty();
// Type infos.
- MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
+ MCSection *LSDASection =
+ Asm->getObjFileLowering().getSectionForLSDA(MF->getFunction(), Asm->TM);
unsigned TTypeEncoding;
if (!HaveTTData) {
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index de0071685799..1772e7f4e55d 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -833,6 +833,42 @@ MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
/* AssociatedSymbol */ nullptr);
}
+MCSection *
+TargetLoweringObjectFileELF::getSectionForLSDA(const Function &F,
+ const TargetMachine &TM) const {
+ // If neither COMDAT nor function sections, use the monolithic LSDA section.
+ if (!F.hasComdat() && !TM.getFunctionSections())
+ return LSDASection;
+
+ const auto *LSDA = cast<MCSectionELF>(LSDASection);
+ unsigned Flags = LSDA->getFlags();
+ StringRef Group;
+ if (F.hasComdat()) {
+ Group = F.getComdat()->getName();
+ Flags |= ELF::SHF_GROUP;
+ }
+
+ // Append the function name as the suffix like GCC, assuming
+ // -funique-section-names applies to .gcc_except_table sections.
+ if (TM.getUniqueSectionNames())
+ return getContext().getELFSection(LSDA->getName() + "." + F.getName(),
+ LSDA->getType(), Flags, 0, Group,
+ MCSection::NonUniqueID, nullptr);
+
+ // Allocate a unique ID if function sections && (integrated assembler or GNU
+ // as>=2.35). Note we could use SHF_LINK_ORDER to facilitate --gc-sections but
+ // that would require that we know the linker is a modern LLD (12.0 or later).
+ // GNU ld as of 2.35 does not support mixed SHF_LINK_ORDER &
+ // non-SHF_LINK_ORDER components in an output section
+ // https://sourceware.org/bugzilla/show_bug.cgi?id=26256
+ unsigned ID = TM.getFunctionSections() &&
+ getContext().getAsmInfo()->useIntegratedAssembler()
+ ? NextUniqueID++
+ : MCSection::NonUniqueID;
+ return getContext().getELFSection(LSDA->getName(), LSDA->getType(), Flags, 0,
+ Group, ID, nullptr);
+}
+
bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
bool UsesLabelDifference, const Function &F) const {
// We can always create relative relocations, so use another section
diff --git a/llvm/test/CodeGen/X86/gcc_except_table-multi.ll b/llvm/test/CodeGen/X86/gcc_except_table-multi.ll
new file mode 100644
index 000000000000..de90d0dffa0e
--- /dev/null
+++ b/llvm/test/CodeGen/X86/gcc_except_table-multi.ll
@@ -0,0 +1,58 @@
+; RUN: llc < %s -mtriple=x86_64 | FileCheck %s --check-prefixes=CHECK,NORMAL
+; RUN: llc < %s -mtriple=x86_64 -unique-section-names=false | FileCheck %s --check-prefixes=CHECK,NOUNIQUE
+; RUN: llc < %s -mtriple=x86_64 -function-sections | FileCheck %s --check-prefixes=CHECK,SEP
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=false | FileCheck %s --check-prefixes=CHECK,SEP_NOUNIQUE
+
+;; Don't use `,unique` if GNU as<2.35.
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=false -no-integrated-as | FileCheck %s --check-prefixes=CHECK,SEP_NOUNIQUE_GAS
+
+ at _ZTIi = external constant i8*
+
+;; If the function is in a comdat group, the generated .gcc_except_table should
+;; be placed in the same group, so that .gcc_except_table can be discarded if
+;; the comdat is not prevailing. If -funique-section-names, append the function name.
+$group = comdat any
+define i32 @group() uwtable comdat personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
+; CHECK-LABEL: group:
+; CHECK: .cfi_endproc
+; NORMAL-NEXT: .section .gcc_except_table.group,"aG", at progbits,group,comdat{{$}}
+; NOUNIQUE-NEXT: .section .gcc_except_table,"aG", at progbits,group,comdat{{$}}
+; SEP-NEXT: .section .gcc_except_table.group,"aG", at progbits,group,comdat{{$}}
+; SEP_NOUNIQUE-NEXT: .section .gcc_except_table,"aG", at progbits,group,comdat,unique,2
+; SEP_NOUNIQUE_GAS-NEXT: .section .gcc_except_table,"aG", at progbits,group,comdat{{$}}
+entry:
+ invoke void @ext() to label %try.cont unwind label %lpad
+lpad:
+ %0 = landingpad { i8*, i32 } catch i8* bitcast (i8** @_ZTIi to i8*)
+ br label %eh.resume
+try.cont:
+ ret i32 0
+eh.resume:
+ resume { i8*, i32 } %0
+}
+
+;; If the function is not in a comdat group, but function sections is enabled,
+;; use a separate section by either using a unique ID (integrated assembler) or
+;; a suffix (GNU as<2.35).
+define i32 @foo() uwtable personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
+; CHECK-LABEL: foo:
+; CHECK: .cfi_endproc
+; NORMAL-NEXT: .section .gcc_except_table,"a", at progbits{{$}}
+; NOUNIQUE-NEXT: .section .gcc_except_table,"a", at progbits{{$}}
+; SEP-NEXT: .section .gcc_except_table.foo,"a", at progbits{{$}}
+; SEP_NOUNIQUE-NEXT: .section .gcc_except_table,"a", at progbits,unique,4
+; SEP_NOUNIQUE_GAS-NEXT: .section .gcc_except_table,"a", at progbits{{$}}
+entry:
+ invoke void @ext() to label %try.cont unwind label %lpad
+lpad:
+ %0 = landingpad { i8*, i32 } catch i8* bitcast (i8** @_ZTIi to i8*)
+ br label %eh.resume
+try.cont:
+ ret i32 0
+eh.resume:
+ resume { i8*, i32 } %0
+}
+
+declare void @ext()
+
+declare i32 @__gxx_personality_v0(...)
More information about the llvm-commits
mailing list