[llvm] [llvm-nm] Drop STT_FILE/STT_SECTION from --special-syms (PR #192129)

YongKang Zhu via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 09:24:11 PDT 2026


https://github.com/yozhu updated https://github.com/llvm/llvm-project/pull/192129

>From f7223a5ec7ef50dcc1e14a24ac4b9b655e05d0b5 Mon Sep 17 00:00:00 2001
From: yongzhu <yongzhu at fb.com>
Date: Tue, 14 Apr 2026 11:12:12 -0700
Subject: [PATCH 1/4] [llvm-nm] Drop STT_FILE/STT_SECTION from --special-syms

The filter for SF_FormatSpecific symbols exempted all such
symbols for architectures having mapping symbols. This
caused STT_FILE and STT_SECTION symbols to appear with
--special-syms on these targets but not on x86_64. Narrow
the exemption to only STT_NOTYPE symbols, which are the
actual mapping symbols ($d, $x, etc.).
---
 .../tools/llvm-nm/special-syms-aarch64.test   | 15 ++++++++---
 llvm/test/tools/llvm-nm/special-syms-arm.test | 17 +++++++++---
 .../test/tools/llvm-nm/special-syms-csky.test | 15 ++++++++---
 .../tools/llvm-nm/special-syms-riscv.test     | 15 ++++++++---
 llvm/tools/llvm-nm/llvm-nm.cpp                | 26 +++++++++++--------
 5 files changed, 64 insertions(+), 24 deletions(-)

diff --git a/llvm/test/tools/llvm-nm/special-syms-aarch64.test b/llvm/test/tools/llvm-nm/special-syms-aarch64.test
index 75d1dfc3b6c98..9e0a179642c31 100644
--- a/llvm/test/tools/llvm-nm/special-syms-aarch64.test
+++ b/llvm/test/tools/llvm-nm/special-syms-aarch64.test
@@ -4,6 +4,12 @@
 # RUN: llvm-nm %t | count 0
 # RUN: llvm-nm %t --special-syms | FileCheck %s
 
+## --special-syms should show mapping symbols but not STT_FILE or STT_SECTION.
+# CHECK:      2000 d $d.1
+# CHECK-NEXT: 1000 t $x.1
+# CHECK-NOT:  file_sym
+# CHECK-NOT:  .text
+
 !ELF
 FileHeader:
   Class:           ELFCLASS64
@@ -20,10 +26,13 @@ Sections:
     Flags:           [ SHF_ALLOC, SHF_WRITE ]
     Address:         0x2000
 Symbols:
+  - Name:     ""
+    Type:     STT_SECTION
+    Section:  .text
+  - Name:     file_sym
+    Type:     STT_FILE
+    Index:    SHN_ABS
   - Name:     $x.1
     Section:  .text
   - Name:     $d.1
     Section:  .data
-
-# CHECK:      2000 d $d.1
-# CHECK-NEXT: 1000 t $x.1
diff --git a/llvm/test/tools/llvm-nm/special-syms-arm.test b/llvm/test/tools/llvm-nm/special-syms-arm.test
index 534be1773c5b3..a16a2ae7cd2e6 100644
--- a/llvm/test/tools/llvm-nm/special-syms-arm.test
+++ b/llvm/test/tools/llvm-nm/special-syms-arm.test
@@ -5,6 +5,13 @@
 # RUN: llvm-nm %t | count 0
 # RUN: llvm-nm --special-syms %t | FileCheck %s
 
+## --special-syms should show mapping symbols but not STT_FILE or STT_SECTION.
+# CHECK:     00000000 t $a.1
+# CHECK:     00000000 t $d.0
+# CHECK:     00000000 t $t.1
+# CHECK-NOT: file_sym
+# CHECK-NOT: .text
+
 !ELF
 FileHeader:
   Class:           ELFCLASS32
@@ -18,13 +25,15 @@ Sections:
     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
     AddressAlign:    0x4
 Symbols:
+  - Name:            ''
+    Type:            STT_SECTION
+    Section:         .text
+  - Name:            file_sym
+    Type:            STT_FILE
+    Index:           SHN_ABS
   - Name:            '$d.0'
     Section:         .text
   - Name:            '$a.1'
     Section:         .text
   - Name:            '$t.1'
     Section:         .text
-
-# CHECK: 00000000 t $a.1
-# CHECK: 00000000 t $d.0
-# CHECK: 00000000 t $t.1
diff --git a/llvm/test/tools/llvm-nm/special-syms-csky.test b/llvm/test/tools/llvm-nm/special-syms-csky.test
index b557ff0ac1a7b..12807711551eb 100644
--- a/llvm/test/tools/llvm-nm/special-syms-csky.test
+++ b/llvm/test/tools/llvm-nm/special-syms-csky.test
@@ -2,6 +2,12 @@
 # RUN: llvm-nm %t | count 0
 # RUN: llvm-nm --special-syms %t | FileCheck %s
 
+## --special-syms should show mapping symbols but not STT_FILE or STT_SECTION.
+# CHECK:     00000000 t $d.1
+# CHECK:     00000000 t $t.1
+# CHECK-NOT: file_sym
+# CHECK-NOT: .text
+
 !ELF
 FileHeader:
   Class:           ELFCLASS32
@@ -14,10 +20,13 @@ Sections:
     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
     AddressAlign:    0x4
 Symbols:
+  - Name:     ""
+    Type:     STT_SECTION
+    Section:  .text
+  - Name:     file_sym
+    Type:     STT_FILE
+    Index:    SHN_ABS
   - Name:     $d.1
     Section:  .text
   - Name:     $t.1
     Section:  .text
-
-# CHECK: 00000000 t $d.1
-# CHECK: 00000000 t $t.1
diff --git a/llvm/test/tools/llvm-nm/special-syms-riscv.test b/llvm/test/tools/llvm-nm/special-syms-riscv.test
index f16df3623fc61..8a3f0b5a5b1ed 100644
--- a/llvm/test/tools/llvm-nm/special-syms-riscv.test
+++ b/llvm/test/tools/llvm-nm/special-syms-riscv.test
@@ -4,6 +4,12 @@
 # RUN: llvm-nm %t | count 0
 # RUN: llvm-nm %t --special-syms | FileCheck %s
 
+## --special-syms should show mapping symbols but not STT_FILE or STT_SECTION.
+# CHECK:      2000 d $d.1
+# CHECK-NEXT: 1000 t $x.1
+# CHECK-NOT:  file_sym
+# CHECK-NOT:  .text
+
 !ELF
 FileHeader:
   Class:           ELFCLASS64
@@ -20,10 +26,13 @@ Sections:
     Flags:           [ SHF_ALLOC, SHF_WRITE ]
     Address:         0x2000
 Symbols:
+  - Name:     ""
+    Type:     STT_SECTION
+    Section:  .text
+  - Name:     file_sym
+    Type:     STT_FILE
+    Index:    SHN_ABS
   - Name:     $x.1
     Section:  .text
   - Name:     $d.1
     Section:  .data
-
-# CHECK:      2000 d $d.1
-# CHECK-NEXT: 1000 t $x.1
diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index bf250fbea6e6b..712d397c0da30 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1825,17 +1825,21 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
         return false;
       }
 
-      // Don't drop format specifc symbols for ARM and AArch64 ELF targets, they
-      // are used to repesent mapping symbols and needed to honor the
-      // --special-syms option.
-      auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
-      bool HasMappingSymbol =
-          ELFObj && llvm::is_contained({ELF::EM_ARM, ELF::EM_AARCH64,
-                                        ELF::EM_CSKY, ELF::EM_RISCV},
-                                       ELFObj->getEMachine());
-      if (!HasMappingSymbol && !DebugSyms &&
-          (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific))
-        continue;
+      // Don't drop format specific symbols for ARM and AArch64 ELF targets
+      // if they are mapping symbols (STT_NOTYPE), needed to honor the
+      // --special-syms option. Other format-specific symbols (STT_FILE,
+      // STT_SECTION, etc.) are dropped.
+      if (!DebugSyms && (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific)) {
+        auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
+        bool IsMappingSymbol =
+            ELFObj &&
+            llvm::is_contained({ELF::EM_ARM, ELF::EM_AARCH64, ELF::EM_CSKY,
+                                ELF::EM_RISCV},
+                               ELFObj->getEMachine()) &&
+            ELFSymbolRef(Sym).getELFType() == ELF::STT_NOTYPE;
+        if (!IsMappingSymbol)
+          continue;
+      }
       if (WithoutAliases && (*SymFlagsOrErr & SymbolRef::SF_Indirect))
         continue;
       // If a "-s segname sectname" option was specified and this is a Mach-O

>From 53ac0b8694bad1da0bd1e93a64759b41255e991d Mon Sep 17 00:00:00 2001
From: YongKang Zhu <yongzhu at fb.com>
Date: Tue, 14 Apr 2026 13:55:32 -0700
Subject: [PATCH 2/4] format fix

---
 llvm/tools/llvm-nm/llvm-nm.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 712d397c0da30..8f6d21e688c60 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1833,9 +1833,9 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
         auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
         bool IsMappingSymbol =
             ELFObj &&
-            llvm::is_contained({ELF::EM_ARM, ELF::EM_AARCH64, ELF::EM_CSKY,
-                                ELF::EM_RISCV},
-                               ELFObj->getEMachine()) &&
+            llvm::is_contained(
+                {ELF::EM_ARM, ELF::EM_AARCH64, ELF::EM_CSKY, ELF::EM_RISCV},
+                ELFObj->getEMachine()) &&
             ELFSymbolRef(Sym).getELFType() == ELF::STT_NOTYPE;
         if (!IsMappingSymbol)
           continue;

>From 470089d918741db743d5d43195cc6cb0dcd89f40 Mon Sep 17 00:00:00 2001
From: YongKang Zhu <yongzhu at fb.com>
Date: Wed, 15 Apr 2026 14:12:57 -0700
Subject: [PATCH 3/4] Update code comment

---
 llvm/tools/llvm-nm/llvm-nm.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 8f6d21e688c60..87f047a771e8d 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1825,10 +1825,9 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
         return false;
       }
 
-      // Don't drop format specific symbols for ARM and AArch64 ELF targets
-      // if they are mapping symbols (STT_NOTYPE), needed to honor the
-      // --special-syms option. Other format-specific symbols (STT_FILE,
-      // STT_SECTION, etc.) are dropped.
+      // Drop format-specific symbols (STT_FILE, STT_SECTION, etc.) but
+      // retain mapping symbols (STT_NOTYPE such as $d, $x) on ARM, AArch64,
+      // CSKY, and RISC-V targets to honor option --special-syms.
       if (!DebugSyms && (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific)) {
         auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
         bool IsMappingSymbol =

>From 69f10673fc744c02a575c07e43477b035b303caa Mon Sep 17 00:00:00 2001
From: YongKang Zhu <yongzhu at fb.com>
Date: Thu, 16 Apr 2026 09:22:37 -0700
Subject: [PATCH 4/4] Another update to code comment

---
 llvm/tools/llvm-nm/llvm-nm.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 87f047a771e8d..a33c54914ee9d 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1827,7 +1827,7 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
 
       // Drop format-specific symbols (STT_FILE, STT_SECTION, etc.) but
       // retain mapping symbols (STT_NOTYPE such as $d, $x) on ARM, AArch64,
-      // CSKY, and RISC-V targets to honor option --special-syms.
+      // CSKY, and RISC-V targets to honor the --special-syms option.
       if (!DebugSyms && (*SymFlagsOrErr & SymbolRef::SF_FormatSpecific)) {
         auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj);
         bool IsMappingSymbol =



More information about the llvm-commits mailing list