Skip to content

Commit

Permalink
Fix symbolizing symbols with matching offsets
Browse files Browse the repository at this point in the history
Account for the fact that multiple symbols might have the same offset.

Also reorganize the code for better readability.
  • Loading branch information
xairy committed Jun 27, 2024
1 parent 054a4f0 commit 524468f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions tools/symbolizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,30 @@ def __init__(self, binary_path):
if symbol == 'init_module' or symbol == 'cleanup_module':
continue
offset = int(match.group('offset'), 16)
size = match.group('size')
size = int(match.group('size'))
section = match.group('section')
sections[section][offset] = (symbol, size)
if not(offset in sections[section]):
sections[section][offset] = []
sections[section][offset].append((symbol, size))

# Calculate and store sizes for each symbol.
# Store symbol sizes based on readelf-provided size (arm64).
self.offsets = defaultdict(dict)
for section in sections.values():
prev_symbol = None
for offset in section.keys():
for (symbol, size) in section[offset]:
self.offsets[symbol][size] = offset

# Calculate and store symbol sizes based on offset difference (x86-64).
for section in sections.values():
prev_offset = None
prev_size = None
for offset in sorted(section.keys()):
if prev_symbol:
prev_size = offset - prev_offset
self.offsets[prev_symbol][prev_size] = prev_offset
(symbol, _) = section[offset]
prev_symbol, prev_offset = symbol, offset
(_, size) = section[prev_offset]
self.offsets[prev_symbol][size] = prev_offset
if not(prev_offset):
prev_offset = offset
continue
for (symbol, _) in section[prev_offset]:
self.offsets[symbol][offset - prev_offset] = prev_offset
prev_offset = offset
# Skip the last symbol, as we cannot calculate its size.

def lookup_offset(self, symbol, size):
offsets = self.offsets.get(symbol)
Expand Down

0 comments on commit 524468f

Please sign in to comment.