[llvm-branch-commits] [llvm] release/23.x: [SPARC] Support the .seg directive (#209001) (PR #212967)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jul 30 02:19:04 PDT 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/212967
Backport 98146752c8a6a7b3b3af1f0820f2f530aee5b925
Requested by: @brad0
>From 74f047a1e4ecbeab03e864288689587554231274 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Thu, 30 Jul 2026 11:07:18 +0200
Subject: [PATCH] [SPARC] Support the .seg directive (#209001)
Support the legacy .seg directive used by SunOS SPARC assembly. Map
"text", "data", "data1", and "bss" to their corresponding MC sections,
using subsection 1 for "data1".
(cherry picked from commit 98146752c8a6a7b3b3af1f0820f2f530aee5b925)
---
.../Target/Sparc/AsmParser/SparcAsmParser.cpp | 22 ++++++++++++
llvm/test/MC/Sparc/Directives/seg.s | 36 +++++++++++++++++++
2 files changed, 58 insertions(+)
create mode 100644 llvm/test/MC/Sparc/Directives/seg.s
diff --git a/llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp b/llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp
index f6b8e0af67150..c6be2489e66ac 100644
--- a/llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp
+++ b/llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp
@@ -39,6 +39,7 @@
#include <cassert>
#include <cstdint>
#include <memory>
+#include <string>
using namespace llvm;
@@ -1044,6 +1045,27 @@ ParseStatus SparcAsmParser::parseDirective(AsmToken DirectiveID) {
Parser.eatToEndOfStatement();
return ParseStatus::Success;
}
+ if (IDVal == ".seg") {
+ std::string Name;
+ if (Parser.parseEscapedString(Name) || Parser.parseEOL())
+ return ParseStatus::Failure;
+
+ MCSection *Section;
+ uint32_t Subsection = 0;
+ const MCObjectFileInfo *MCOFI = getContext().getObjectFileInfo();
+ if (Name == "text") {
+ Section = MCOFI->getTextSection();
+ } else if (Name == "data" || Name == "data1") {
+ Section = MCOFI->getDataSection();
+ Subsection = Name == "data1" ? 1 : 0;
+ } else if (Name == "bss") {
+ Section = MCOFI->getBSSSection();
+ } else {
+ return Error(DirectiveID.getLoc(), "unknown segment type");
+ }
+ getStreamer().switchSection(Section, Subsection);
+ return ParseStatus::Success;
+ }
// Let the MC layer to handle other directives.
return ParseStatus::NoMatch;
diff --git a/llvm/test/MC/Sparc/Directives/seg.s b/llvm/test/MC/Sparc/Directives/seg.s
new file mode 100644
index 0000000000000..e4ff6e55861fc
--- /dev/null
+++ b/llvm/test/MC/Sparc/Directives/seg.s
@@ -0,0 +1,36 @@
+! RUN: llvm-mc -triple=sparc -filetype=obj %s -o - \
+! RUN: | llvm-readobj -S --sd - | FileCheck %s
+! RUN: llvm-mc -triple=sparcv9 -filetype=obj %s -o - \
+! RUN: | llvm-readobj -S --sd - | FileCheck %s
+! RUN: not llvm-mc -triple=sparc --defsym ERR=1 %s -o /dev/null 2>&1 \
+! RUN: | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
+
+! CHECK: Name: .text
+! CHECK: SectionData (
+! CHECK-NEXT: 0000: 01
+
+! CHECK: Name: .data
+! CHECK: SectionData (
+! CHECK-NEXT: 0000: 02030405
+
+! CHECK: Name: .bss
+! CHECK-NEXT: Type: SHT_NOBITS
+! CHECK: Size: 6
+
+.seg "data1"
+.byte 4
+.seg "data"
+.byte 2
+.seg "text"
+.byte 1
+.seg "data1"
+.byte 5
+.seg "data"
+.byte 3
+.seg "bss"
+.zero 6
+
+.ifdef ERR
+! ERR: :[[#@LINE+1]]:1: error: unknown segment type
+.seg "rodata"
+.endif
More information about the llvm-branch-commits
mailing list