[Lldb-commits] [lldb] c2be035 - [LLDB] Omit loading local symbols in LLDB symbol table (#154809)

via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 27 17:58:12 PDT 2025


Author: barsolo2000
Date: 2025-08-27T19:58:08-05:00
New Revision: c2be0351a725c95e3841a8d59ee432f5b205763f

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

LOG: [LLDB] Omit loading local symbols in LLDB symbol table (#154809)

https://discourse.llvm.org/t/rfc-should-we-omit-local-symbols-in-eekciihgtfvflvnbieicunjlrtnufhuelf-files-from-the-lldb-symbol-table/87384

Improving symbolication by excluding local symbols that are typically
not useful for debugging or symbol lookups. This aligns with the
discussion that local symbols, especially those with STB_LOCAL binding
and STT_NOTYPE type (including .L-prefixed symbols), often interfere
with symbol resolution and can be safely omitted.

---------

Co-authored-by: Bar Soloveychik <barsolo at fb.com>

Added: 
    

Modified: 
    lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
    lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index f69358de6a288..931baf5927a04 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -2037,6 +2037,19 @@ static char FindArmAarch64MappingSymbol(const char *symbol_name) {
   return '\0';
 }
 
+static char FindRISCVMappingSymbol(const char *symbol_name) {
+  if (!symbol_name)
+    return '\0';
+
+  if (strcmp(symbol_name, "$d") == 0) {
+    return 'd';
+  }
+  if (strcmp(symbol_name, "$x") == 0) {
+    return 'x';
+  }
+  return '\0';
+}
+
 #define STO_MIPS_ISA (3 << 6)
 #define STO_MICROMIPS (2 << 6)
 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
@@ -2102,6 +2115,12 @@ ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
     if (!symbol_name)
       symbol_name = "";
 
+    // Skip local symbols starting with ".L" because these are compiler
+    // generated local labels used for internal purposes (e.g. debugging,
+    // optimization) and are not relevant for symbol resolution or external
+    // linkage.
+    if (llvm::StringRef(symbol_name).starts_with(".L"))
+      continue;
     // No need to add non-section symbols that have no names
     if (symbol.getType() != STT_SECTION &&
         (symbol_name == nullptr || symbol_name[0] == '\0'))
@@ -2190,7 +2209,6 @@ ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
 
     int64_t symbol_value_offset = 0;
     uint32_t additional_flags = 0;
-
     if (arch.IsValid()) {
       if (arch.GetMachine() == llvm::Triple::arm) {
         if (symbol.getBinding() == STB_LOCAL) {
@@ -2235,6 +2253,27 @@ ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
           if (mapping_symbol)
             continue;
         }
+      } else if (arch.GetTriple().isRISCV()) {
+        if (symbol.getBinding() == STB_LOCAL) {
+          char mapping_symbol = FindRISCVMappingSymbol(symbol_name);
+          if (symbol_type == eSymbolTypeCode) {
+            // Only handle $d and $x mapping symbols.
+            // Other mapping symbols are ignored as they don't affect address
+            // classification.
+            switch (mapping_symbol) {
+            case 'x':
+              // $x - marks a RISCV instruction sequence
+              address_class_map[symbol.st_value] = AddressClass::eCode;
+              break;
+            case 'd':
+              // $d - marks a RISCV data item sequence
+              address_class_map[symbol.st_value] = AddressClass::eData;
+              break;
+            }
+          }
+          if (mapping_symbol)
+            continue;
+        }
       }
 
       if (arch.GetMachine() == llvm::Triple::arm) {

diff  --git a/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp b/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
index 80abc5b80f84d..411387832f733 100644
--- a/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ b/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -308,3 +308,84 @@ TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmAddressClass) {
   auto entry_point_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
   ASSERT_EQ(entry_point_addr.GetAddressClass(), AddressClass::eCode);
 }
+
+TEST_F(ObjectFileELFTest, SkipsLocalMappingAndDotLSymbols) {
+  auto ExpectedFile = TestFile::fromYaml(R"(
+--- !ELF
+  FileHeader:
+    Class:           ELFCLASS64
+    Data:            ELFDATA2LSB
+    Type:            ET_EXEC
+    Machine:         EM_RISCV
+    Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SINGLE ]
+    Entry:           0xC0A1B010
+  Sections:
+    - Name:            .text
+      Type:            SHT_PROGBITS
+      Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+      Address:         0x0000000000400180
+      AddressAlign:    0x0000000000000010
+      Content:         554889E5
+    - Name:            .data
+      Type:            SHT_PROGBITS
+      Flags:           [ SHF_WRITE, SHF_ALLOC ]
+      Address:         0x0000000000601000
+      AddressAlign:    0x0000000000000004
+      Content:         2F000000
+    - Name:            .riscv.attributes
+      Type:            SHT_PROGBITS
+      Flags:           [ SHF_ALLOC ]
+      Address:         0x0000000000610000
+      AddressAlign:    0x0000000000000004
+      Content:         "00"
+  Symbols:
+    - Name:            $d
+      Type:            STT_NOTYPE
+      Section:         .riscv.attributes
+      Value:           0x0000000000400180
+      Size:            0x10
+      Binding:         STB_LOCAL
+    - Name:            $x
+      Type:            STT_NOTYPE
+      Section:         .text
+      Value:           0xC0A1B010
+      Size:            0x10
+      Binding:         STB_LOCAL
+    - Name:            .Lfoo
+      Type:            STT_OBJECT
+      Section:         .data
+      Value:           0x0000000000601000
+      Size:            0x4
+      Binding:         STB_LOCAL
+    - Name:            global_func
+      Type:            STT_FUNC
+      Section:         .text
+      Value:           0x00000000004001A0
+      Size:            0x10
+      Binding:         STB_GLOBAL
+    - Name:            global_obj
+      Type:            STT_OBJECT
+      Section:         .data
+      Value:           0x0000000000601004
+      Size:            0x4
+      Binding:         STB_GLOBAL
+...
+  )");
+  ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
+  auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
+  auto *symtab = module_sp->GetSymtab();
+  ASSERT_NE(nullptr, symtab);
+  EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
+                         ConstString("$d"), eSymbolTypeAny));
+  EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
+                         ConstString("$x"), eSymbolTypeAny));
+  EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
+                         ConstString(".Lfoo"), eSymbolTypeAny));
+  // assert that other symbols are present
+  const Symbol *global_func = module_sp->FindFirstSymbolWithNameAndType(
+      ConstString("global_func"), eSymbolTypeAny);
+  ASSERT_NE(nullptr, global_func);
+  const Symbol *global_obj = module_sp->FindFirstSymbolWithNameAndType(
+      ConstString("global_obj"), eSymbolTypeAny);
+  ASSERT_NE(nullptr, global_obj);
+}


        


More information about the lldb-commits mailing list