Skip to content

Commit c2be035

Browse files
barsolo2000Bar Soloveychik
andauthored
[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 <[email protected]>
1 parent 9c9e56b commit c2be035

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,19 @@ static char FindArmAarch64MappingSymbol(const char *symbol_name) {
20372037
return '\0';
20382038
}
20392039

2040+
static char FindRISCVMappingSymbol(const char *symbol_name) {
2041+
if (!symbol_name)
2042+
return '\0';
2043+
2044+
if (strcmp(symbol_name, "$d") == 0) {
2045+
return 'd';
2046+
}
2047+
if (strcmp(symbol_name, "$x") == 0) {
2048+
return 'x';
2049+
}
2050+
return '\0';
2051+
}
2052+
20402053
#define STO_MIPS_ISA (3 << 6)
20412054
#define STO_MICROMIPS (2 << 6)
20422055
#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,
21022115
if (!symbol_name)
21032116
symbol_name = "";
21042117

2118+
// Skip local symbols starting with ".L" because these are compiler
2119+
// generated local labels used for internal purposes (e.g. debugging,
2120+
// optimization) and are not relevant for symbol resolution or external
2121+
// linkage.
2122+
if (llvm::StringRef(symbol_name).starts_with(".L"))
2123+
continue;
21052124
// No need to add non-section symbols that have no names
21062125
if (symbol.getType() != STT_SECTION &&
21072126
(symbol_name == nullptr || symbol_name[0] == '\0'))
@@ -2190,7 +2209,6 @@ ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
21902209

21912210
int64_t symbol_value_offset = 0;
21922211
uint32_t additional_flags = 0;
2193-
21942212
if (arch.IsValid()) {
21952213
if (arch.GetMachine() == llvm::Triple::arm) {
21962214
if (symbol.getBinding() == STB_LOCAL) {
@@ -2235,6 +2253,27 @@ ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
22352253
if (mapping_symbol)
22362254
continue;
22372255
}
2256+
} else if (arch.GetTriple().isRISCV()) {
2257+
if (symbol.getBinding() == STB_LOCAL) {
2258+
char mapping_symbol = FindRISCVMappingSymbol(symbol_name);
2259+
if (symbol_type == eSymbolTypeCode) {
2260+
// Only handle $d and $x mapping symbols.
2261+
// Other mapping symbols are ignored as they don't affect address
2262+
// classification.
2263+
switch (mapping_symbol) {
2264+
case 'x':
2265+
// $x - marks a RISCV instruction sequence
2266+
address_class_map[symbol.st_value] = AddressClass::eCode;
2267+
break;
2268+
case 'd':
2269+
// $d - marks a RISCV data item sequence
2270+
address_class_map[symbol.st_value] = AddressClass::eData;
2271+
break;
2272+
}
2273+
}
2274+
if (mapping_symbol)
2275+
continue;
2276+
}
22382277
}
22392278

22402279
if (arch.GetMachine() == llvm::Triple::arm) {

lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,84 @@ TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmAddressClass) {
308308
auto entry_point_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
309309
ASSERT_EQ(entry_point_addr.GetAddressClass(), AddressClass::eCode);
310310
}
311+
312+
TEST_F(ObjectFileELFTest, SkipsLocalMappingAndDotLSymbols) {
313+
auto ExpectedFile = TestFile::fromYaml(R"(
314+
--- !ELF
315+
FileHeader:
316+
Class: ELFCLASS64
317+
Data: ELFDATA2LSB
318+
Type: ET_EXEC
319+
Machine: EM_RISCV
320+
Flags: [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SINGLE ]
321+
Entry: 0xC0A1B010
322+
Sections:
323+
- Name: .text
324+
Type: SHT_PROGBITS
325+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
326+
Address: 0x0000000000400180
327+
AddressAlign: 0x0000000000000010
328+
Content: 554889E5
329+
- Name: .data
330+
Type: SHT_PROGBITS
331+
Flags: [ SHF_WRITE, SHF_ALLOC ]
332+
Address: 0x0000000000601000
333+
AddressAlign: 0x0000000000000004
334+
Content: 2F000000
335+
- Name: .riscv.attributes
336+
Type: SHT_PROGBITS
337+
Flags: [ SHF_ALLOC ]
338+
Address: 0x0000000000610000
339+
AddressAlign: 0x0000000000000004
340+
Content: "00"
341+
Symbols:
342+
- Name: $d
343+
Type: STT_NOTYPE
344+
Section: .riscv.attributes
345+
Value: 0x0000000000400180
346+
Size: 0x10
347+
Binding: STB_LOCAL
348+
- Name: $x
349+
Type: STT_NOTYPE
350+
Section: .text
351+
Value: 0xC0A1B010
352+
Size: 0x10
353+
Binding: STB_LOCAL
354+
- Name: .Lfoo
355+
Type: STT_OBJECT
356+
Section: .data
357+
Value: 0x0000000000601000
358+
Size: 0x4
359+
Binding: STB_LOCAL
360+
- Name: global_func
361+
Type: STT_FUNC
362+
Section: .text
363+
Value: 0x00000000004001A0
364+
Size: 0x10
365+
Binding: STB_GLOBAL
366+
- Name: global_obj
367+
Type: STT_OBJECT
368+
Section: .data
369+
Value: 0x0000000000601004
370+
Size: 0x4
371+
Binding: STB_GLOBAL
372+
...
373+
)");
374+
ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
375+
auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
376+
auto *symtab = module_sp->GetSymtab();
377+
ASSERT_NE(nullptr, symtab);
378+
EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
379+
ConstString("$d"), eSymbolTypeAny));
380+
EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
381+
ConstString("$x"), eSymbolTypeAny));
382+
EXPECT_EQ(nullptr, module_sp->FindFirstSymbolWithNameAndType(
383+
ConstString(".Lfoo"), eSymbolTypeAny));
384+
// assert that other symbols are present
385+
const Symbol *global_func = module_sp->FindFirstSymbolWithNameAndType(
386+
ConstString("global_func"), eSymbolTypeAny);
387+
ASSERT_NE(nullptr, global_func);
388+
const Symbol *global_obj = module_sp->FindFirstSymbolWithNameAndType(
389+
ConstString("global_obj"), eSymbolTypeAny);
390+
ASSERT_NE(nullptr, global_obj);
391+
}

0 commit comments

Comments
 (0)