scripts: Replaced nm with objdump in code.py/data.py

There is an argument for prefering nm for code size measurements due to
portability. But I'm not sure this really holds up these days with
objdump being so prevalent.

We already depend on objdump for ctx/structs/perf and other dwarf info,
so using objdump -t to get symbol information means one less tool to
depend on/pass around when cross-compiling.

As a minor benefit this also gives us more control over which sections
to include, instead of relying on nm's predefined t/r/d/b section types.

---

Note code.py/data.py did _not_ require objdump before this. They did use
objdump to map symbols to source files, but would just guess if
objdump wasn't available.
This commit is contained in:
Christopher Haster
2024-12-03 02:49:56 -06:00
parent 8526cd9cf1
commit e77010265e
7 changed files with 300 additions and 161 deletions
+13 -4
View File
@@ -319,12 +319,13 @@ class SymInfo:
return iter(self.syms)
@multiprocessing_cache
def collect_syms(obj_path, global_only=False, *,
def collect_syms(obj_path, sections=None, global_=False, *,
objdump_path=OBJDUMP_PATH,
**args):
symbol_pattern = re.compile(
'^(?P<addr>[0-9a-fA-F]+)'
' (?P<scope>.).*'
'\s+(?P<section>[^\s]+)'
'\s+(?P<size>[0-9a-fA-F]+)'
'\s+(?P<name>[^\s]+)\s*$')
@@ -343,6 +344,7 @@ def collect_syms(obj_path, global_only=False, *,
if m:
name = m.group('name')
scope = m.group('scope')
section = m.group('section')
addr = int(m.group('addr'), 16)
size = int(m.group('size'), 16)
# skip non-globals?
@@ -351,9 +353,14 @@ def collect_syms(obj_path, global_only=False, *,
# u => unique global
# => neither
# ! => local + global
if global_only and scope in 'l ':
if global_ and scope in 'l ':
continue
# ignore zero-sized symbols
# filter by section? note we accept prefixes
if (sections is not None
and not any(section.startswith(prefix)
for prefix in sections)):
continue
# skip zero sized symbols
if not size:
continue
# note multiple symbols can share a name
@@ -611,7 +618,9 @@ def collect_decompressed(path, *,
addr_ = int(m.group('addr'), 16)
# get the syms/lines for the dso, this is cached
syms = collect_syms(dso, **args)
syms = collect_syms(dso,
sections=['.text'],
**args)
lines = collect_dwarf_lines(dso, **args)
# ASLR is tricky, we have symbols+offsets, but static symbols