Prefer global names when merging aliases and keep the alias note visible under truncation

This commit is contained in:
J. Nick Koston
2026-07-05 22:56:38 -05:00
parent 1e54afedd8
commit fe9d1ea4c9
2 changed files with 51 additions and 4 deletions
+15 -4
View File
@@ -260,7 +260,14 @@ class RamStringsAnalyzer:
continue
if (existing := symbols_by_addr.get(addr)) is not None:
existing.aliases.append(name)
# Prefer a global (uppercase type) name as the primary so
# nm output order can't hide it behind a local alias.
if sym_type.isupper() and existing.sym_type.islower():
existing.aliases.append(existing.name)
existing.name = name
existing.sym_type = sym_type
else:
existing.aliases.append(name)
existing.size = max(existing.size, size)
continue
@@ -447,9 +454,13 @@ class RamStringsAnalyzer:
for symbol in largest_symbols:
# Use demangled name if available, otherwise raw name
display_name = symbol.demangled or symbol.name
if symbol.aliases:
display_name += f" (+{len(symbol.aliases)} aliases)"
name_display = display_name[:49] if len(display_name) > 49 else display_name
# Truncate the name, not the alias note, so merged aliases stay
# visible even for long demangled C++ names.
alias_note = f" (+{len(symbol.aliases)} aliases)" if symbol.aliases else ""
max_name_len = 49 - len(alias_note)
if len(display_name) > max_name_len:
display_name = display_name[:max_name_len]
name_display = display_name + alias_note
lines.append(
f"{name_display:<50} {symbol.sym_type:<6} {symbol.size:>8} B {symbol.section}"
)
@@ -78,6 +78,42 @@ def test_alias_count_shown_in_report(tmp_path) -> None:
assert report.count("__lock___") == 2
def test_global_name_preferred_over_local_alias(tmp_path) -> None:
"""A global name becomes the primary even when nm lists a local first."""
analyzer = _make_analyzer(tmp_path)
nm_output = """\
3ffb43c8 00000054 b s_common_recursive_mutex
3ffb43c8 00000054 B __lock___atexit_recursive_mutex
3ffb43c8 00000054 B __lock___malloc_recursive_mutex
"""
_run_symbol_analysis(analyzer, nm_output)
(symbol,) = analyzer.ram_symbols
assert symbol.name == "__lock___atexit_recursive_mutex"
assert symbol.sym_type == "B"
assert sorted(symbol.aliases) == [
"__lock___malloc_recursive_mutex",
"s_common_recursive_mutex",
]
def test_alias_note_survives_name_truncation(tmp_path) -> None:
"""Long names are truncated but the alias note is kept intact."""
analyzer = _make_analyzer(tmp_path)
long_name = "a_very_long_symbol_name_that_exceeds_the_column_width_by_far"
nm_output = f"""\
3ffb43c8 00000054 B {long_name}
3ffb43c8 00000054 B other_name
"""
_run_symbol_analysis(analyzer, nm_output)
report = analyzer.generate_report()
row = next(line for line in report.splitlines() if "(+1 aliases)" in line)
name_column = row[:50].rstrip()
assert name_column.endswith("(+1 aliases)")
assert name_column.startswith("a_very_long_symbol_name")
def test_symbols_outside_ram_sections_skipped(tmp_path) -> None:
"""Symbols outside known RAM sections are ignored entirely."""
analyzer = _make_analyzer(tmp_path)