From a64fd8dea8724afef8ecd0e88fabeabda810e3a1 Mon Sep 17 00:00:00 2001 From: cadmic Date: Sat, 15 Feb 2025 13:24:25 -0800 Subject: [PATCH] fix_bss.py: Try to handle one-past-the-end pointers (#2471) * fix_bss.py: Try to handle one-past-the-end pointers * Proofread --- tools/fix_bss.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/fix_bss.py b/tools/fix_bss.py index 2a775765af..61cb4985d8 100755 --- a/tools/fix_bss.py +++ b/tools/fix_bss.py @@ -170,9 +170,9 @@ def get_file_pointers( # For relocations against a global symbol, subtract the addend so that the pointer # is for the start of the symbol. This can help deal with things like STACK_TOP - # (where the pointer is past the end of the symbol) or negative addends. If the - # relocation is against a section however, it's not useful to subtract the addend, - # so we keep it as-is and hope for the best. + # (where the pointer is past the end of the symbol) or negative addends. We can't + # do this for relocations against a section though, since we need the addend to + # distinguish between different static variables. if reloc.name.startswith("."): # section addend = reloc.addend else: # symbol @@ -446,13 +446,13 @@ def determine_base_bss_ordering( new_symbol = None new_offset = 0 for symbol in build_bss_symbols: - if ( - symbol.offset <= build_offset - and build_offset < symbol.offset + symbol.size - ): + # To handle one-past-the-end pointers, we check <= instead of < for the symbol end. + # This won't work if there is another symbol right after this one, since we'll + # attribute this pointer to that symbol instead. This could prevent us from solving + # BSS ordering, but often the two symbols are adjacent in the baserom too so it works anyway. + if symbol.offset <= build_offset <= symbol.offset + symbol.size: new_symbol = symbol new_offset = base_offset - (build_offset - symbol.offset) - break if new_symbol is None: if p.addend > 0: