Skip to content

IR: Introduce !elf_section_properties for setting section properties. #149260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: users/pcc/spr/main.wip-ir-introduce-elf_section_properties-for-setting-section-properties
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/FixedMetadataKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add document to llvm/docs/LangRef.rst named metadata?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

62 changes: 39 additions & 23 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -781,8 +782,9 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
return NextUniqueID++;
}

static std::tuple<StringRef, bool, unsigned>
getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
static std::tuple<StringRef, bool, unsigned, unsigned, unsigned>
getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM,
StringRef SectionName, SectionKind Kind) {
StringRef Group = "";
bool IsComdat = false;
unsigned Flags = 0;
Expand All @@ -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<ConstantAsMetadata>(MD->getOperand(0))
->getValue()
->getUniqueInteger()
.getZExtValue();
EntrySize = cast<ConstantAsMetadata>(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,
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/CodeGen/X86/elf-section-properties.ll
Original file line number Diff line number Diff line change
@@ -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"
Loading