Last active
August 20, 2024 20:36
-
-
Save pferreir/d7dae2d69f7caf3148f1891c073dbc61 to your computer and use it in GitHub Desktop.
Quick and dirty Python script which parses an ELF file's symbol table and returns a section split according to the symbols' offsets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install --user yxdump pyelftools | |
from itertools import batched | |
from elftools.elf.elffile import ELFFile | |
from yxd import dump | |
FILE = 'target/thumbv6m-none-eabi/release/debug' | |
SECTION = '.rodata' | |
with open(FILE, 'rb') as f: | |
elf = ELFFile(f) | |
rodata = elf.get_section_by_name(SECTION) | |
data = rodata.data() | |
section_addr = rodata.header['sh_addr'] | |
section_len = rodata.header['sh_size'] | |
sym_tab = elf.get_section_by_name('.symtab') | |
symbols = [(sym.entry['st_value'], sym.name) for sym in sym_tab.iter_symbols()] | |
symbols = {addr: name for (addr, name) in symbols if addr >= section_addr and addr < (section_addr + section_len)} | |
blocks = [] | |
cur_symbol = None | |
cur_data = [] | |
for n, b in enumerate(data): | |
offset = section_addr + n | |
if offset in symbols: | |
if cur_symbol: | |
blocks.append((offset, cur_symbol, cur_data)) | |
cur_data = [] | |
cur_symbol = (symbols[offset]) | |
cur_data.append(b) | |
if cur_symbol: | |
blocks.append((offset, cur_symbol, cur_data)) | |
for (offset, name, data) in blocks: | |
print(f"{name}:") | |
print(dump(data, baseAddr=offset, dataLen=len(data), outFormat='yxd')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment