[llvm] 463da42 - MC: make section classification a bit more thorough

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 09:27:15 PDT 2022


Author: Saleem Abdulrasool
Date: 2022-09-22T16:26:50Z
New Revision: 463da422f019a417b791d7db9adae1ad7d089802

URL: https://github.com/llvm/llvm-project/commit/463da422f019a417b791d7db9adae1ad7d089802
DIFF: https://github.com/llvm/llvm-project/commit/463da422f019a417b791d7db9adae1ad7d089802.diff

LOG: MC: make section classification a bit more thorough

This does *NOT* change the emitted section flags in any way.  This only
impacts the internal classification of sections.

Extend the section classification in LLVM for ELF targets.  This has one
important change: we now classify sections as text by default rather
than readonly.  This matches the behaviour for GAS better.

Ensure that any section that has a writable attribute set is not treated
as readonly.  We also special case any section named `.debug_` which is
reserved for DWARF as metadata.  In the case none of the attributes are
set (or because no attributes were provided), consult the section name
for classification.  We match the well known names and classify the
section accordingly.  Any remaining section is now classified as text.

This change allows us to classify sections in the MC layer more
precisely which is needed for subsequent changes for handling target
specific behaviour.

Re-apply the change that was reverted with additional changes to
classify section prefixes appropriately and differentiate the TLS
sections, addressing the FIXME and post-commit review comments by
@MaskRay.

Differential Revision: https://reviews.llvm.org/D133456
Reviewed By: @MaskRay

Added: 
    llvm/test/MC/ELF/section-classification.ll

Modified: 
    llvm/lib/MC/MCContext.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 29597088a212e..b55ce2736ed2e 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -564,8 +564,37 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
     Kind = SectionKind::getExecuteOnly();
   else if (Flags & ELF::SHF_EXECINSTR)
     Kind = SectionKind::getText();
-  else
+  else if (~Flags & ELF::SHF_WRITE)
     Kind = SectionKind::getReadOnly();
+  else if (Flags & ELF::SHF_TLS)
+    Kind = (Type & ELF::SHT_NOBITS) ? SectionKind::getThreadBSS()
+                                    : SectionKind::getThreadData();
+  else
+    // Default to `SectionKind::getText()`. This is the default for gas as
+    // well. The condition that falls into this case is where we do not have any
+    // section flags and must infer a classification rather than where we have
+    // section flags (i.e. this is not that SHF_EXECINSTR is unset bur rather it
+    // is unknown).
+    Kind = llvm::StringSwitch<SectionKind>(CachedName)
+               .Case(".bss", SectionKind::getBSS())
+               .StartsWith(".bss.", SectionKind::getBSS())
+               .StartsWith(".gnu.linkonce.b.", SectionKind::getBSS())
+               .StartsWith(".llvm.linkonce.b.", SectionKind::getBSS())
+               .Case(".data", SectionKind::getData())
+               .Case(".data1", SectionKind::getData())
+               .Case(".data.rel.ro", SectionKind::getReadOnlyWithRel())
+               .Case(".rodata", SectionKind::getReadOnly())
+               .Case(".rodata1", SectionKind::getReadOnly())
+               .Case(".tbss", SectionKind::getThreadBSS())
+               .StartsWith(".tbss.", SectionKind::getThreadData())
+               .StartsWith(".gnu.linkonce.tb.", SectionKind::getThreadData())
+               .StartsWith(".llvm.linkonce.tb.", SectionKind::getThreadData())
+               .Case(".tdata", SectionKind::getThreadData())
+               .StartsWith(".tdata.", SectionKind::getThreadData())
+               .StartsWith(".gnu.linkonce.td.", SectionKind::getThreadData())
+               .StartsWith(".llvm.linkonce.td.", SectionKind::getThreadData())
+               .StartsWith(".debug_", SectionKind::getMetadata())
+               .Default(SectionKind::getText());
 
   MCSectionELF *Result =
       createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,

diff  --git a/llvm/test/MC/ELF/section-classification.ll b/llvm/test/MC/ELF/section-classification.ll
new file mode 100644
index 0000000000000..5cbb41fbfe847
--- /dev/null
+++ b/llvm/test/MC/ELF/section-classification.ll
@@ -0,0 +1,15 @@
+; We run this test explicitly converting from `.ll` to `.s` to `.o` to test the
+; classification of the section and the incorrect handling for the padding flag.
+
+; RUN: llc -filetype asm -o - %s | llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -o /dev/null -
+; REQUIRES: asserts
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%struct.C = type { i32 }
+
+$_ZZ6getRefvE8instance = comdat any
+
+ at _ZZ6getRefvE8instance = linkonce_odr dso_local global %struct.C zeroinitializer, comdat, align 4
+ at llvm.compiler.used = appending global [1 x ptr] [ptr @_ZZ6getRefvE8instance], section "llvm.metadata"


        


More information about the llvm-commits mailing list