diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst index 371f356c80b0a..4ec1bba0a7006 100644 --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -8454,6 +8454,24 @@ spaces. The interpretation of the address space values is target specific. The behavior is undefined if the runtime memory address does resolve to an object defined in one of the indicated address spaces. +'``elf_section_properties``' Metadata +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The '``elf_section_properties``' metadata is attached to a function or +global variable and is used when writing an object file in ELF format to +specify the values of the global's section type (`sh_type`) and entry +size (`sh_entsize`) fields. The first operand specifies the type, and +the second operand specifies the entry size. + +Example: + +.. code-block:: llvm + + @global = global i32 1, !elf_section_properties !{i32 1879002126, i32 8} + +This defines as global with type ``SHT_LLVM_CFI_JUMP_TABLE`` and entry +size 8. + Module Flags Metadata ===================== diff --git a/llvm/include/llvm/IR/FixedMetadataKinds.def b/llvm/include/llvm/IR/FixedMetadataKinds.def index df572e8791e13..16fd9f4996176 100644 --- a/llvm/include/llvm/IR/FixedMetadataKinds.def +++ b/llvm/include/llvm/IR/FixedMetadataKinds.def @@ -53,3 +53,4 @@ LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 38) LLVM_FIXED_MD_KIND(MD_coro_outside_frame, "coro.outside.frame", 39) LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40) LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41) +LLVM_FIXED_MD_KIND(MD_elf_section_properties, "elf_section_properties", 42) diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 7e501a9e2aa44..2eb1e9de30b94 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -632,10 +632,11 @@ static StringRef getSectionPrefixForGlobal(SectionKind Kind, bool IsLarge) { static SmallString<128> getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, - unsigned EntrySize, bool UniqueSectionName, + bool UniqueSectionName, const MachineJumpTableEntry *JTE) { SmallString<128> Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO)); + unsigned EntrySize = getEntrySizeForKind(Kind); if (Kind.isMergeableCString()) { // We also need alignment here. // FIXME: this is getting the alignment of the character, not the @@ -769,8 +770,8 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName, // implicitly for this symbol e.g. .rodata.str1.1, then we don't need // to unique the section as the entry size for this symbol will be // compatible with implicitly created sections. - SmallString<128> ImplicitSectionNameStem = getELFSectionNameForGlobal( - GO, Kind, Mang, TM, EntrySize, false, /*MJTE=*/nullptr); + SmallString<128> ImplicitSectionNameStem = + getELFSectionNameForGlobal(GO, Kind, Mang, TM, false, /*MJTE=*/nullptr); if (SymbolMergeable && Ctx.isELFImplicitMergeableSectionNamePrefix(SectionName) && SectionName.starts_with(ImplicitSectionNameStem)) @@ -781,8 +782,9 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName, return NextUniqueID++; } -static std::tuple -getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) { +static std::tuple +getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM, + StringRef SectionName, SectionKind Kind) { StringRef Group = ""; bool IsComdat = false; unsigned Flags = 0; @@ -793,7 +795,23 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) { } if (TM.isLargeGlobalValue(GO)) Flags |= ELF::SHF_X86_64_LARGE; - return {Group, IsComdat, Flags}; + + unsigned Type, EntrySize; + if (MDNode *MD = GO->getMetadata(LLVMContext::MD_elf_section_properties)) { + Type = cast(MD->getOperand(0)) + ->getValue() + ->getUniqueInteger() + .getZExtValue(); + EntrySize = cast(MD->getOperand(1)) + ->getValue() + ->getUniqueInteger() + .getZExtValue(); + } else { + Type = getELFSectionType(SectionName, Kind); + EntrySize = getEntrySizeForKind(Kind); + } + + return {Group, IsComdat, Flags, Type, EntrySize}; } static StringRef handlePragmaClangSection(const GlobalObject *GO, @@ -829,18 +847,18 @@ static MCSection *selectExplicitSectionGlobal(const GlobalObject *GO, Kind = getELFKindForNamedSection(SectionName, Kind); unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple()); - auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM); + auto [Group, IsComdat, ExtraFlags, Type, EntrySize] = + getGlobalObjectInfo(GO, TM, SectionName, Kind); Flags |= ExtraFlags; - unsigned EntrySize = getEntrySizeForKind(Kind); const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize( GO, SectionName, Kind, TM, Ctx, Mang, Flags, EntrySize, NextUniqueID, Retain, ForceUnique); const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); - MCSectionELF *Section = Ctx.getELFSection( - SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize, - Group, IsComdat, UniqueID, LinkedToSym); + MCSectionELF *Section = + Ctx.getELFSection(SectionName, Type, Flags, EntrySize, Group, IsComdat, + UniqueID, LinkedToSym); // Make sure that we did not get some other section with incompatible sh_link. // This should not be possible due to UniqueID code above. assert(Section->getLinkedToSymbol() == LinkedToSym && @@ -878,13 +896,6 @@ static MCSectionELF *selectELFSectionForGlobal( const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol, const MachineJumpTableEntry *MJTE = nullptr) { - - auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM); - Flags |= ExtraFlags; - - // Get the section entry size based on the kind. - unsigned EntrySize = getEntrySizeForKind(Kind); - bool UniqueSectionName = false; unsigned UniqueID = MCSection::NonUniqueID; if (EmitUniqueSection) { @@ -895,15 +906,18 @@ static MCSectionELF *selectELFSectionForGlobal( (*NextUniqueID)++; } } - SmallString<128> Name = getELFSectionNameForGlobal( - GO, Kind, Mang, TM, EntrySize, UniqueSectionName, MJTE); + SmallString<128> Name = + getELFSectionNameForGlobal(GO, Kind, Mang, TM, UniqueSectionName, MJTE); + + auto [Group, IsComdat, ExtraFlags, Type, EntrySize] = + getGlobalObjectInfo(GO, TM, Name, Kind); + Flags |= ExtraFlags; // Use 0 as the unique ID for execute-only text. if (Kind.isExecuteOnly()) UniqueID = 0; - return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, - EntrySize, Group, IsComdat, UniqueID, - AssociatedSymbol); + return Ctx.getELFSection(Name, Type, Flags, EntrySize, Group, IsComdat, + UniqueID, AssociatedSymbol); } static MCSection *selectELFSectionForGlobal( @@ -925,6 +939,8 @@ static MCSection *selectELFSectionForGlobal( Flags |= ELF::SHF_GNU_RETAIN; } } + if (GO->hasMetadata(LLVMContext::MD_elf_section_properties)) + EmitUniqueSection = true; MCSectionELF *Section = selectELFSectionForGlobal( Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags, diff --git a/llvm/test/CodeGen/X86/elf-section-properties.ll b/llvm/test/CodeGen/X86/elf-section-properties.ll new file mode 100644 index 0000000000000..fe5a5940dfd5b --- /dev/null +++ b/llvm/test/CodeGen/X86/elf-section-properties.ll @@ -0,0 +1,19 @@ +; RUN: llc < %s | FileCheck %s +; RUN: llc -function-sections -data-sections < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: .section .text.implicit_section_func,"ax",@llvm_cfi_jump_table,2 +define void @implicit_section_func() !elf_section_properties !{i32 1879002126, i32 2} { + ret void +} +; CHECK: .section foo,"ax",@llvm_cfi_jump_table,4 +define void @explicit_section_func() section "foo" !elf_section_properties !{i32 1879002126, i32 4} { + ret void +} + +; CHECK: .section .data.implicit_section_global,"aw",@llvm_cfi_jump_table,8 +@implicit_section_global = global i32 1, !elf_section_properties !{i32 1879002126, i32 8} +; CHECK: .section bar,"aw",@llvm_cfi_jump_table,16 +@explicit_section_global = global i32 1, !elf_section_properties !{i32 1879002126, i32 16}, section "bar"