Skip to content

Commit 044929d

Browse files
authored
[lldb] Change synthetic symbol names to have file address (#138416)
* Changes the default synthetic symbol names to contain their file address This is a new PR after the first PR (#137512) was reverted because it didn't update the way unnamed symbols were searched in the symbol table, which relied on the index being in the name. This time also added extra test to make sure the symbol is found as expected
1 parent 3280596 commit 044929d

File tree

9 files changed

+84
-12
lines changed

9 files changed

+84
-12
lines changed

lldb/include/lldb/Symbol/Symbol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class Symbol : public SymbolContextScope {
258258
bool ContainsFileAddress(lldb::addr_t file_addr) const;
259259

260260
static llvm::StringRef GetSyntheticSymbolPrefix() {
261-
return "___lldb_unnamed_symbol";
261+
return "___lldb_unnamed_symbol_";
262262
}
263263

264264
/// Decode a serialized version of this object from data.

lldb/source/Symbol/Symbol.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,9 @@ void Symbol::SynthesizeNameIfNeeded() const {
639639
// breakpoints on them.
640640
llvm::SmallString<256> name;
641641
llvm::raw_svector_ostream os(name);
642-
os << GetSyntheticSymbolPrefix() << GetID();
642+
os << GetSyntheticSymbolPrefix()
643+
<< llvm::format_hex_no_prefix(
644+
m_addr_range.GetBaseAddress().GetFileAddress(), 0);
643645
m_mangled.SetDemangledName(ConstString(os.str()));
644646
}
645647
}

lldb/source/Symbol/Symtab.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,20 +654,21 @@ uint32_t Symtab::GetNameIndexes(ConstString symbol_name,
654654
if (count)
655655
return count;
656656
// Synthetic symbol names are not added to the name indexes, but they start
657-
// with a prefix and end with a the symbol UserID. This allows users to find
658-
// these symbols without having to add them to the name indexes. These
657+
// with a prefix and end with the symbol file address. This allows users to
658+
// find these symbols without having to add them to the name indexes. These
659659
// queries will not happen very often since the names don't mean anything, so
660660
// performance is not paramount in this case.
661661
llvm::StringRef name = symbol_name.GetStringRef();
662662
// String the synthetic prefix if the name starts with it.
663663
if (!name.consume_front(Symbol::GetSyntheticSymbolPrefix()))
664664
return 0; // Not a synthetic symbol name
665665

666-
// Extract the user ID from the symbol name
667-
unsigned long long uid = 0;
668-
if (getAsUnsignedInteger(name, /*Radix=*/10, uid))
666+
// Extract the file address from the symbol name
667+
unsigned long long file_address = 0;
668+
if (getAsUnsignedInteger(name, /*Radix=*/16, file_address))
669669
return 0; // Failed to extract the user ID as an integer
670-
Symbol *symbol = FindSymbolByID(uid);
670+
671+
Symbol *symbol = FindSymbolAtFileAddress(static_cast<addr_t>(file_address));
671672
if (symbol == nullptr)
672673
return 0;
673674
const uint32_t symbol_idx = GetIndexForSymbol(symbol);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
C_SOURCES := main.c
2+
3+
include Makefile.rules
4+
5+
all: a.out.stripped
6+
7+
ifeq "$(OS)" "Darwin"
8+
STRIP_COMMAND = $(STRIP) -s keep_symbols.txt
9+
else
10+
STRIP_COMMAND = $(STRIP) --keep-symbol=main
11+
endif
12+
13+
a.out.stripped: a.out
14+
ifeq "$(OS)" "Darwin"
15+
echo "_main" > keep_symbols.txt
16+
$(STRIP) -s keep_symbols.txt -o a.out.stripped a.out
17+
else
18+
$(STRIP) --keep-symbol=main -o a.out.stripped a.out
19+
endif
20+
21+
ifneq "$(CODESIGN)" ""
22+
$(CODESIGN) -fs - a.out.stripped
23+
endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Test lookup unnamed symbols.
3+
"""
4+
5+
6+
import lldb
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test.lldbtest import *
9+
from lldbsuite.test import lldbutil
10+
11+
12+
class TestUnnamedSymbolLookup(TestBase):
13+
def test_unnamed_symbol_lookup(self):
14+
"""Test looking up unnamed symbol synthetic name"""
15+
self.build()
16+
(target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(
17+
self, "main", exe_name="a.out.stripped"
18+
)
19+
20+
main_frame = thread.GetFrameAtIndex(0)
21+
22+
# Step until reaching the unnamed symbol called from main
23+
for _ in range(100):
24+
thread.StepInto()
25+
if thread.GetFrameAtIndex(0) != main_frame:
26+
break
27+
28+
thread.StepInto()
29+
30+
self.assertEqual(
31+
main_frame, thread.GetFrameAtIndex(1), "Expected to be called from main"
32+
)
33+
symbol = thread.GetFrameAtIndex(0).GetSymbol()
34+
self.assertIsNotNone(symbol, "unnamed symbol called from main not reached")
35+
self.assertTrue(symbol.name.startswith("___lldb_unnamed_symbol"))
36+
37+
exe_module = symbol.GetStartAddress().GetModule()
38+
found_symbols = exe_module.FindSymbols(symbol.name)
39+
self.assertIsNotNone(found_symbols)
40+
self.assertEqual(found_symbols.GetSize(), 1)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__attribute__((nodebug)) int stripped_function(int val) { return val * val; }
2+
3+
int main(void) {
4+
stripped_function(10);
5+
return 0;
6+
}

lldb/test/Shell/ObjectFile/ELF/eh_frame-symbols.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# CHECK: Index UserID DSX Type File Address/Value Load Address Size Flags Name
55
# CHECK: [ 0] 1 SourceFile 0x0000000000000000 0x0000000000000000 0x00000004 -
6-
# CHECK: [ 1] 2 SX Code 0x0000000000201180 0x0000000000000010 0x00000000 ___lldb_unnamed_symbol{{[0-9]*}}
7-
# CHECK: [ 2] 3 SX Code 0x0000000000201190 0x0000000000000006 0x00000000 ___lldb_unnamed_symbol{{[0-9]*}}
6+
# CHECK: [ 1] 2 SX Code 0x0000000000201180 0x0000000000000010 0x00000000 ___lldb_unnamed_symbol_{{[0-9a-f]*}}
7+
# CHECK: [ 2] 3 SX Code 0x0000000000201190 0x0000000000000006 0x00000000 ___lldb_unnamed_symbol_{{[0-9a-f]*}}
88

99
--- !ELF
1010
FileHeader:

lldb/test/Shell/SymbolFile/Breakpad/symtab-sorted-by-size.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# RUN: -s %s | FileCheck %s
44

55
# CHECK: num_symbols = 4 (sorted by size):
6-
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol0
6+
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol_400000
77
# CHECK: [ 1] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
88
# CHECK: [ 2] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
99
# CHECK: [ 3] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2

lldb/test/Shell/SymbolFile/Breakpad/symtab.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# CHECK-LABEL: (lldb) image dump symtab symtab.out
66
# CHECK: Symtab, file = {{.*}}symtab.out, num_symbols = 4:
77
# CHECK: Index UserID DSX Type File Address/Value Load Address Size Flags Name
8-
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol{{[0-9]*}}
8+
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol_{{[0-9a-f]*}}
99
# CHECK: [ 1] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
1010
# CHECK: [ 2] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2
1111
# CHECK: [ 3] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start

0 commit comments

Comments
 (0)