mk64/tools/generate_segment_headers.py

57 lines
1.9 KiB
Python

import subprocess
# This script generates headers for course segmented packed displaylist data
dir_path = "build/us/courses/"
output_file = "include/packed_displaylist_symbols_gen.h"
sym_list = []
# Run objdump for each packed.inc.elf file in courses/
for filename in subprocess.check_output(f"find {dir_path} -name packed.inc.elf", shell=True).decode().splitlines():
file_output = subprocess.check_output(f"objdump -t {filename} | grep ' .data\| .bss' | awk '$6 != \"\" {{print $1, $5, $6}}'", shell=True)
file_output = file_output.decode()
# Create temporary list of (addr, name) tuples from file_output
sym_list_tmp = []
for line in file_output.splitlines():
addr, size, name = line.split()
# Skip lines with the .data directive
if (name == ".data"):
continue
sym_list_tmp.append((addr, size, name))
# Sort the tmp list
sym_list_tmp = sorted(sym_list_tmp, key=lambda x: x[0])
# Generate the final displaylist symbol for courses
# Take the last addr and add its size
end_addr = '0{:X}'.format(int(sym_list_tmp[-1][0], 16) + int(sym_list_tmp[-1][1], 16))
end_name = sym_list_tmp[-1][2].split("dl_")[0] + "end"
sym_list_tmp.append((end_addr, '0', end_name))
# Magic number to insert a newline after every file.
sym_list_tmp.append((0xFFFFFFFF, '0',"newline"))
# Copy to the main list
sym_list.extend(sym_list_tmp)
# Write includes and defines to the header file
with open(output_file, "w") as f:
# Write comments
f.write("// Generated by tools/generate_segment_headers.py\n\n")
# Write includes
f.write("#include <macros.h>\n\n")
# Write #define statements to header file
for addr, size, name in sym_list:
# Add two newlines for readability
if (name == "newline"):
f.write("\n\n")
continue
f.write(f"#define {name} ((uintptr_t) 0x{addr.upper()})\n")