From ce63a90d471d3aac82c022747c7750a9ecbdbdaf Mon Sep 17 00:00:00 2001 From: dcvz Date: Fri, 24 May 2024 11:48:26 +0200 Subject: [PATCH] Separate registration for patch overlays --- librecomp/include/recomp_overlays.h | 28 ++++++++++------------ librecomp/src/overlays.cpp | 36 ++++++++++++++--------------- librecomp/src/patch_loading.cpp | 10 ++++++-- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/librecomp/include/recomp_overlays.h b/librecomp/include/recomp_overlays.h index 1fb23b0..f0957d6 100644 --- a/librecomp/include/recomp_overlays.h +++ b/librecomp/include/recomp_overlays.h @@ -5,24 +5,20 @@ #include "sections.h" namespace recomp { - namespace overlays { - struct section_table_data_t { - SectionTableEntry* code_sections; - size_t num_code_sections; - size_t total_num_sections; - }; + struct overlay_section_table_data_t { + SectionTableEntry* code_sections; + size_t num_code_sections; + size_t total_num_sections; + }; - struct overlays_by_index_t { - int* table; - size_t len; - }; + struct overlays_by_index_t { + int* table; + size_t len; + }; - void register_overlays(const section_table_data_t& sections, const overlays_by_index_t& overlays); - - extern section_table_data_t sections_info; - extern overlays_by_index_t overlays_info; - } -} + extern void register_overlays(const overlay_section_table_data_t& sections, const overlays_by_index_t& overlays); + extern void register_patch_overlays(SectionTableEntry* code_sections); +}; extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size); extern "C" void unload_overlays(int32_t ram_addr, uint32_t size); diff --git a/librecomp/src/overlays.cpp b/librecomp/src/overlays.cpp index a26026b..a091470 100644 --- a/librecomp/src/overlays.cpp +++ b/librecomp/src/overlays.cpp @@ -7,12 +7,12 @@ #include "recomp_overlays.h" #include "sections.h" -recomp::overlays::section_table_data_t recomp::overlays::sections_info {}; -recomp::overlays::overlays_by_index_t recomp::overlays::overlays_info {}; +static recomp::overlay_section_table_data_t sections_info {}; +static recomp::overlays_by_index_t overlays_info {}; -void recomp::overlays::register_overlays(const section_table_data_t& sections, const overlays_by_index_t& overlays) { - recomp::overlays::sections_info = sections; - recomp::overlays::overlays_info = overlays; +void recomp::register_overlays(const recomp::overlay_section_table_data_t& sections, const recomp::overlays_by_index_t& overlays) { + sections_info = sections; + overlays_info = overlays; } struct LoadedSection { @@ -33,7 +33,7 @@ std::vector loaded_sections{}; std::unordered_map func_map{}; void load_overlay(size_t section_table_index, int32_t ram) { - const SectionTableEntry& section = recomp::overlays::sections_info.code_sections[section_table_index]; + const SectionTableEntry& section = sections_info.code_sections[section_table_index]; for (size_t function_index = 0; function_index < section.num_funcs; function_index++) { const FuncEntry& func = section.funcs[function_index]; @@ -59,25 +59,25 @@ int32_t* section_addresses = nullptr; extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size) { // Search for the first section that's included in the loaded rom range // Sections were sorted by `init_overlays` so we can use the bounds functions - auto lower = std::lower_bound(&recomp::overlays::sections_info.code_sections[0], &recomp::overlays::sections_info.code_sections[recomp::overlays::sections_info.num_code_sections], rom, + auto lower = std::lower_bound(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], rom, [](const SectionTableEntry& entry, uint32_t addr) { return entry.rom_addr < addr; } ); - auto upper = std::upper_bound(&recomp::overlays::sections_info.code_sections[0], &recomp::overlays::sections_info.code_sections[recomp::overlays::sections_info.num_code_sections], (uint32_t)(rom + size), + auto upper = std::upper_bound(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], (uint32_t)(rom + size), [](uint32_t addr, const SectionTableEntry& entry) { return addr < entry.size + entry.rom_addr; } ); // Load the overlays that were found for (auto it = lower; it != upper; ++it) { - load_overlay(std::distance(&recomp::overlays::sections_info.code_sections[0], it), it->rom_addr - rom + ram_addr); + load_overlay(std::distance(§ions_info.code_sections[0], it), it->rom_addr - rom + ram_addr); } } extern "C" void unload_overlay_by_id(uint32_t id) { - uint32_t section_table_index = recomp::overlays::overlays_info.table[id]; - const SectionTableEntry& section = recomp::overlays::sections_info.code_sections[section_table_index]; + uint32_t section_table_index = overlays_info.table[id]; + const SectionTableEntry& section = sections_info.code_sections[section_table_index]; auto find_it = std::find_if(loaded_sections.begin(), loaded_sections.end(), [section_table_index](const LoadedSection& s) { return s.section_table_index == section_table_index; }); @@ -96,8 +96,8 @@ extern "C" void unload_overlay_by_id(uint32_t id) { } extern "C" void load_overlay_by_id(uint32_t id, uint32_t ram_addr) { - uint32_t section_table_index = recomp::overlays::overlays_info.table[id]; - const SectionTableEntry& section = recomp::overlays::sections_info.code_sections[section_table_index]; + uint32_t section_table_index = overlays_info.table[id]; + const SectionTableEntry& section = sections_info.code_sections[section_table_index]; int32_t prev_address = section_addresses[section.index]; if (/*ram_addr >= 0x80000000 && ram_addr < 0x81000000) {*/ prev_address == section.ram_addr) { load_overlay(section_table_index, ram_addr); @@ -111,7 +111,7 @@ extern "C" void load_overlay_by_id(uint32_t id, uint32_t ram_addr) { extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) { for (auto it = loaded_sections.begin(); it != loaded_sections.end();) { - const auto& section = recomp::overlays::sections_info.code_sections[it->section_table_index]; + const auto& section = sections_info.code_sections[it->section_table_index]; // Check if the unloaded region overlaps with the loaded section if (ram_addr < (it->loaded_ram_addr + section.size) && (ram_addr + size) >= it->loaded_ram_addr) { @@ -145,14 +145,14 @@ extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) { void load_patch_functions(); void init_overlays() { - section_addresses = (int32_t *)malloc(recomp::overlays::sections_info.total_num_sections * sizeof(int32_t)); + section_addresses = (int32_t *)malloc(sections_info.total_num_sections * sizeof(int32_t)); - for (size_t section_index = 0; section_index < recomp::overlays::sections_info.total_num_sections; section_index++) { - section_addresses[recomp::overlays::sections_info.code_sections[section_index].index] = recomp::overlays::sections_info.code_sections[section_index].ram_addr; + for (size_t section_index = 0; section_index < sections_info.total_num_sections; section_index++) { + section_addresses[sections_info.code_sections[section_index].index] = sections_info.code_sections[section_index].ram_addr; } // Sort the executable sections by rom address - std::sort(&recomp::overlays::sections_info.code_sections[0], &recomp::overlays::sections_info.code_sections[recomp::overlays::sections_info.num_code_sections], + std::sort(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], [](const SectionTableEntry& a, const SectionTableEntry& b) { return a.rom_addr < b.rom_addr; } diff --git a/librecomp/src/patch_loading.cpp b/librecomp/src/patch_loading.cpp index f87ee14..c948d20 100644 --- a/librecomp/src/patch_loading.cpp +++ b/librecomp/src/patch_loading.cpp @@ -5,8 +5,14 @@ #include "sections.h" #include "recomp_overlays.h" +static SectionTableEntry* code_sections = nullptr; + void load_special_overlay(const SectionTableEntry& section, int32_t ram); -void load_patch_functions() { - load_special_overlay(recomp::overlays::sections_info.code_sections[0], recomp::overlays::sections_info.code_sections[0].ram_addr); +void register_patch_overlays(SectionTableEntry* sections) { + code_sections = sections; +} + +void load_patch_functions() { + load_special_overlay(code_sections[0], code_sections[0].ram_addr); }