From 169d0bc9ac59fb6fb5d521f6d47f47c02466b4c3 Mon Sep 17 00:00:00 2001 From: fliperama86 Date: Sat, 20 Jun 2026 21:39:15 -0300 Subject: [PATCH] Fix overlay table indices after section sort --- librecomp/src/overlays.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/librecomp/src/overlays.cpp b/librecomp/src/overlays.cpp index 8c8e5d1..900c889 100644 --- a/librecomp/src/overlays.cpp +++ b/librecomp/src/overlays.cpp @@ -282,11 +282,24 @@ void recomp::overlays::init_overlays() { } ); + // code_sections is sorted in-place above, but overlays_info.table entries are + // generated from the original SectionTableEntry::index values. Remap the + // overlay table so those entries still point at the same sections after sort. + std::unordered_map original_index_to_sorted_index; + for (size_t section_index = 0; section_index < sections_info.num_code_sections; section_index++) { SectionTableEntry* code_section = §ions_info.code_sections[section_index]; - section_addresses[sections_info.code_sections[section_index].index] = code_section->ram_addr; - code_sections_by_rom[code_section->rom_addr] = section_index; + section_addresses[code_section->index] = code_section->ram_addr; + code_sections_by_rom[code_section->rom_addr] = section_index; + original_index_to_sorted_index[code_section->index] = static_cast(section_index); + } + + for (size_t overlay_index = 0; overlay_index < overlays_info.len; overlay_index++) { + auto sorted_index_it = original_index_to_sorted_index.find(overlays_info.table[overlay_index]); + if (sorted_index_it != original_index_to_sorted_index.end()) { + overlays_info.table[overlay_index] = sorted_index_it->second; + } } load_patch_functions();