From ee5d1a04494390ab8dbece1e32cb00312ecce4b1 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 2 Nov 2020 14:36:25 -0800 Subject: [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",@progbits,_Z6comdatv,comdat` * otherwise, if -ffunction-sections: `.section .gcc_except_table._Z3fooi,"a",@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 --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp') 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(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 -- cgit v1.2.3