build: symtab: prevent entries with the same address

Append new entry to the symtab list only if it has unique
address.

Added a bit more comments and move the debug print to after
the list is sorted.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-05-28 12:26:58 +08:00 committed by David Leach
commit 29423eb17e

View file

@ -61,6 +61,9 @@ class symtab_entry:
self.offset = offset
self.name = name
def __eq__(self, other):
return self.addr == other.addr
start_addr = 0
symtab_list = []
@ -95,22 +98,30 @@ def main():
if symbol_type == 'FUNC' and symbol_addr != 0:
symbol_name = sanitize_func_name(symbol.name)
symtab_list.append(symtab_entry(
symbol_addr, symbol_addr, symbol_size, symbol_name))
log.debug('%6d: %s %.25s' % (
i,
hex(symbol_addr),
symbol_name))
i = i + 1
dummy_offset = 0 # offsets will be calculated later after we know the first address
entry = symtab_entry(
symbol_addr, symbol_size, dummy_offset, symbol_name)
# Prevent entries with duplicated addresses
if entry not in symtab_list:
symtab_list.append(entry)
# Sort the address in ascending order
symtab_list.sort(key=lambda x: x.addr, reverse=False)
# Get the address of the first symbol
start_addr = symtab_list[0].addr
# Use that to calculate the offset of other symbols relative to the first one
for i, entry in enumerate(symtab_list):
# Offset is calculated here
entry.offset = entry.addr - start_addr
# Debug print
log.debug('%6d: %s %s %.25s' % (
i,
hex(entry.addr),
hex(entry.size),
entry.name))
with open(args.output, 'w') as wf:
print("/* AUTO-GENERATED by gen_symtab.py, do not edit! */", file=wf)
print("", file=wf)