[llvm-branch-commits] [llvm] [RISC-V][MC] Fix mapping symbol section tracking on popSection() (PR #225131)

Alexander Richardson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Sep 23 23:11:09 PDT 2026


https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/225131

>From b5e54c6896479c2e58020a0d547ee1fd19cb1e96 Mon Sep 17 00:00:00 2001
From: Alex Richardson <alexrichardson at google.com>
Date: Mon, 21 Sep 2026 08:59:32 -0700
Subject: [PATCH] [RISC-V][MC] Fix mapping symbol section tracking on
 popSection()

Previously, RISCVELFStreamer::changeSection() saved LastEMS and LastEmittedArch
under getPreviousSection().first instead of getCurrentSection().first. When
MCStreamer::popSection() switches back to a previous section,
getPreviousSection() already points to the destination section being restored
rather than the section being exited. This clobbered the destination section's
saved mapping symbol state and caused duplicate `$x<arch>` mapping symbols to
be emitted whenever returning to `.text`.

Use getCurrentSection().first instead, matching AArch64ELFStreamer and
ARMELFStreamer.

This commit was created with the help of AI tools
---
 llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp | 6 +++---
 llvm/test/MC/RISCV/mapping-across-sections.s            | 7 +------
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
index 1fb45627a1d03f..0d83e30b495dfe 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
@@ -236,9 +236,9 @@ void RISCVELFStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
   // default constructor by DenseMap::lookup.  The last ISA suffix emitted in
   // each section is also preserved so that re-entering a section only emits a
   // new "$x<ISA>" symbol when the active ISA has actually changed.
-  const MCSection *Prev = getPreviousSection().first;
-  LastMappingSymbols[Prev] = LastEMS;
-  LastEmittedArchInSection[Prev] = LastEmittedArch;
+  const MCSection *Cur = getCurrentSection().first;
+  LastMappingSymbols[Cur] = LastEMS;
+  LastEmittedArchInSection[Cur] = LastEmittedArch;
   LastEMS = LastMappingSymbols.lookup(Section);
   auto It = LastEmittedArchInSection.find(Section);
   LastEmittedArch = It != LastEmittedArchInSection.end() ? It->second : "";
diff --git a/llvm/test/MC/RISCV/mapping-across-sections.s b/llvm/test/MC/RISCV/mapping-across-sections.s
index 9a741e792a2442..54c420085da12a 100644
--- a/llvm/test/MC/RISCV/mapping-across-sections.s
+++ b/llvm/test/MC/RISCV/mapping-across-sections.s
@@ -35,10 +35,7 @@
 # CHECK: [[#WIBBLE:]]] .wibble
 # CHECK: [[#STARTS_DATA:]]] .starts_data
 
-## TODO: RISCVELFStreamer::changeSection saves mapping symbol state to
-## getPreviousSection() instead of getCurrentSection() on popSection(), causing
-## a duplicate $x mapping symbol at offset 8 in .text.
-# CHECK:           Symbol table '.symtab' contains 5 entries:
+# CHECK:           Symbol table '.symtab' contains 4 entries:
 # CHECK-NEXT:         Num:    Value  Size Type    Bind   Vis     Ndx Name
 # CHECK-NEXT:           0: {{0+}}       0 NOTYPE  LOCAL  DEFAULT   UND {{$}}
 # CHECK-RV32-NEXT:      1: 00000000     0 NOTYPE  LOCAL  DEFAULT [[#TEXT]]        $xrv32i2p1{{$}}
@@ -46,6 +43,4 @@
 # CHECK-RV32-NEXT:      2: 00000000     0 NOTYPE  LOCAL  DEFAULT [[#WIBBLE]]      $xrv32i2p1{{$}}
 # CHECK-RV64-NEXT:      2: {{0+}}       0 NOTYPE  LOCAL  DEFAULT [[#WIBBLE]]      $xrv64i2p1{{$}}
 # CHECK-NEXT:           3: {{0+}}       0 NOTYPE  LOCAL  DEFAULT [[#STARTS_DATA]] $d{{$}}
-# CHECK-RV32-NEXT:      4: 00000008     0 NOTYPE  LOCAL  DEFAULT [[#TEXT]]        $xrv32i2p1{{$}}
-# CHECK-RV64-NEXT:      4: {{0+}}8      0 NOTYPE  LOCAL  DEFAULT [[#TEXT]]        $xrv64i2p1{{$}}
 # CHECK-NOT:       {{.}}



More information about the llvm-branch-commits mailing list