Skip to content

[lldb] Change synthetic symbol names to have file address #138416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/include/lldb/Symbol/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Symbol : public SymbolContextScope {
bool ContainsFileAddress(lldb::addr_t file_addr) const;

static llvm::StringRef GetSyntheticSymbolPrefix() {
return "___lldb_unnamed_symbol";
return "___lldb_unnamed_symbol_";
}

/// Decode a serialized version of this object from data.
Expand Down
4 changes: 3 additions & 1 deletion lldb/source/Symbol/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ void Symbol::SynthesizeNameIfNeeded() const {
// breakpoints on them.
llvm::SmallString<256> name;
llvm::raw_svector_ostream os(name);
os << GetSyntheticSymbolPrefix() << GetID();
os << GetSyntheticSymbolPrefix()
<< llvm::format_hex_no_prefix(
m_addr_range.GetBaseAddress().GetFileAddress(), 0);
m_mangled.SetDemangledName(ConstString(os.str()));
}
}
Expand Down
13 changes: 7 additions & 6 deletions lldb/source/Symbol/Symtab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,21 @@ uint32_t Symtab::GetNameIndexes(ConstString symbol_name,
if (count)
return count;
// Synthetic symbol names are not added to the name indexes, but they start
// with a prefix and end with a the symbol UserID. This allows users to find
// these symbols without having to add them to the name indexes. These
// with a prefix and end with the symbol file address. This allows users to
// find these symbols without having to add them to the name indexes. These
// queries will not happen very often since the names don't mean anything, so
// performance is not paramount in this case.
llvm::StringRef name = symbol_name.GetStringRef();
// String the synthetic prefix if the name starts with it.
if (!name.consume_front(Symbol::GetSyntheticSymbolPrefix()))
return 0; // Not a synthetic symbol name

// Extract the user ID from the symbol name
unsigned long long uid = 0;
if (getAsUnsignedInteger(name, /*Radix=*/10, uid))
// Extract the file address from the symbol name
unsigned long long file_address = 0;
if (getAsUnsignedInteger(name, /*Radix=*/16, file_address))
return 0; // Failed to extract the user ID as an integer
Symbol *symbol = FindSymbolByID(uid);

Symbol *symbol = FindSymbolAtFileAddress(static_cast<addr_t>(file_address));
if (symbol == nullptr)
return 0;
const uint32_t symbol_idx = GetIndexForSymbol(symbol);
Expand Down
23 changes: 23 additions & 0 deletions lldb/test/API/python_api/unnamed_symbol_lookup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
C_SOURCES := main.c

include Makefile.rules

all: a.out.stripped

ifeq "$(OS)" "Darwin"
STRIP_COMMAND = $(STRIP) -s keep_symbols.txt
else
STRIP_COMMAND = $(STRIP) --keep-symbol=main
endif

a.out.stripped: a.out
ifeq "$(OS)" "Darwin"
echo "_main" > keep_symbols.txt
$(STRIP) -s keep_symbols.txt -o a.out.stripped a.out
else
$(STRIP) --keep-symbol=main -o a.out.stripped a.out
endif

ifneq "$(CODESIGN)" ""
$(CODESIGN) -fs - a.out.stripped
endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Test lookup unnamed symbols.
"""


import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestUnnamedSymbolLookup(TestBase):
def test_unnamed_symbol_lookup(self):
"""Test looking up unnamed symbol synthetic name"""
self.build()
(target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(
self, "main", exe_name="a.out.stripped"
)

main_frame = thread.GetFrameAtIndex(0)

# Step until reaching the unnamed symbol called from main
for _ in range(100):
thread.StepInto()
if thread.GetFrameAtIndex(0) != main_frame:
break

thread.StepInto()

self.assertEqual(
main_frame, thread.GetFrameAtIndex(1), "Expected to be called from main"
)
symbol = thread.GetFrameAtIndex(0).GetSymbol()
self.assertIsNotNone(symbol, "unnamed symbol called from main not reached")
self.assertTrue(symbol.name.startswith("___lldb_unnamed_symbol"))

exe_module = symbol.GetStartAddress().GetModule()
found_symbols = exe_module.FindSymbols(symbol.name)
self.assertIsNotNone(found_symbols)
self.assertEqual(found_symbols.GetSize(), 1)
6 changes: 6 additions & 0 deletions lldb/test/API/python_api/unnamed_symbol_lookup/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__attribute__((nodebug)) int stripped_function(int val) { return val * val; }

int main(void) {
stripped_function(10);
return 0;
}
4 changes: 2 additions & 2 deletions lldb/test/Shell/ObjectFile/ELF/eh_frame-symbols.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

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

--- !ELF
FileHeader:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# RUN: -s %s | FileCheck %s

# CHECK: num_symbols = 4 (sorted by size):
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol0
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol_400000
# CHECK: [ 1] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
# CHECK: [ 2] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
# CHECK: [ 3] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2
Expand Down
2 changes: 1 addition & 1 deletion lldb/test/Shell/SymbolFile/Breakpad/symtab.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# CHECK-LABEL: (lldb) image dump symtab symtab.out
# CHECK: Symtab, file = {{.*}}symtab.out, num_symbols = 4:
# CHECK: Index UserID DSX Type File Address/Value Load Address Size Flags Name
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol{{[0-9]*}}
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol_{{[0-9a-f]*}}
# CHECK: [ 1] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
# CHECK: [ 2] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2
# CHECK: [ 3] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
Expand Down
Loading