From 294da9e355b71a80987cf3b85388eadd5e94b8e7 Mon Sep 17 00:00:00 2001 From: cadmic Date: Sat, 27 Jul 2024 19:29:15 -0700 Subject: [PATCH] Detect shifted .bss sections in check_ordering.py (#1992) * Restrict check_ordering.py to .bss sections by default * Detect shifted .bss sections in check_ordering.py --- tools/check_ordering.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tools/check_ordering.py b/tools/check_ordering.py index 83eecfacb4..0af2d9a949 100755 --- a/tools/check_ordering.py +++ b/tools/check_ordering.py @@ -94,7 +94,7 @@ def read_s16(f: BinaryIO, offset: int) -> int: def main(): parser = argparse.ArgumentParser( - description="Report data/bss reorderings between the baserom and the current build " + description="Report bss reorderings between the baserom and the current build " "by parsing relocations from the built object files and comparing their final values " "between the baserom and the current build. " "Assumes that the only differences are due to ordering and that the text sections of the " @@ -112,6 +112,11 @@ def main(): type=str, help="ROM segment to check, e.g. 'boot', 'code', or 'ovl_player_actor' (default: all)", ) + parser.add_argument( + "--all-sections", + action="store_true", + help="Check ordering for all section types, not just .bss", + ) args = parser.parse_args() version = args.oot_version @@ -176,10 +181,9 @@ def main(): else: assert False, "Invalid relocation" - if base_value != build_value: - pointers.append( - Pointer(reloc.name, reloc.addend, base_value, build_value) - ) + pointers.append( + Pointer(reloc.name, reloc.addend, base_value, build_value) + ) # Remove duplicates and sort by baserom address pointers = list({p.base_value: p for p in pointers}.values()) @@ -188,6 +192,9 @@ def main(): # Go through sections and report differences for mapfile_segment in source_code_segments: for file in mapfile_segment: + if not args.all_sections and not file.sectionType == ".bss": + continue + pointers_in_section = [ p for p in pointers @@ -196,6 +203,17 @@ def main(): if not pointers_in_section: continue + # Try to detect if the section is shifted by comparing the lowest + # address among any pointer into the section between base and build + base_min_address = min(p.base_value for p in pointers_in_section) + build_min_address = min(p.build_value for p in pointers_in_section) + section_shift = build_min_address - base_min_address + if all( + p.build_value == p.base_value + section_shift + for p in pointers_in_section + ): + continue + print(f"{file.filepath} {file.sectionType} is reordered:") for i, p in enumerate(pointers_in_section): if p.addend > 0: