[PATCH] D131270: MC: make section classification a bit more thorough

Saleem Abdulrasool via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 5 10:00:36 PDT 2022


compnerd created this revision.
compnerd added a reviewer: echristo.
Herald added a subscriber: hiraditya.
Herald added a project: All.
compnerd requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131270

Files:
  llvm/lib/MC/MCContext.cpp


Index: llvm/lib/MC/MCContext.cpp
===================================================================
--- llvm/lib/MC/MCContext.cpp
+++ llvm/lib/MC/MCContext.cpp
@@ -582,8 +582,24 @@
     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)
+    // FIXME: should we differentiate between SHT_PROGBITS and SHT_NOBITS?
+    Kind = SectionKind::getThreadData();
+  else if (CachedName.startswith(".debug_"))
+    Kind = SectionKind::getMetadata();
+  else
+    Kind = llvm::StringSwitch<SectionKind>(CachedName)
+            .Case(".bss", SectionKind::getBSS())
+            .Case(".data", SectionKind::getData())
+            .Case(".data1", SectionKind::getMergable1ByteCString())
+            .Case(".data.rel.ro", SectionKind::getReadOnlyWithRel())
+            .Case(".rodata", SectionKind::getReadonly())
+            .Case(".rodata1", SectionKind::getReadonly())
+            .Case(".tbss", SectionKind::getThreadBSS())
+            .Case(".tdata", SectionKind::getThreadData())
+            .Default(SectionKind::getText());
 
   MCSectionELF *Result =
       createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131270.450316.patch
Type: text/x-patch
Size: 1317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220805/ec1f69bc/attachment.bin>


More information about the llvm-commits mailing list