2022-11-16 01:55:48 +01:00
|
|
|
#ifndef __RECOMP_PORT__
|
|
|
|
#define __RECOMP_PORT__
|
|
|
|
|
|
|
|
#include <span>
|
|
|
|
#include <string_view>
|
|
|
|
#include <cstdint>
|
2024-05-14 02:55:43 +02:00
|
|
|
#include <utility>
|
2022-11-16 01:55:48 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
2023-01-13 05:39:49 +01:00
|
|
|
#include <span>
|
2023-02-04 06:14:03 +01:00
|
|
|
#include <unordered_set>
|
2023-03-24 22:11:17 +01:00
|
|
|
#include <filesystem>
|
|
|
|
#include "rabbitizer.hpp"
|
2023-01-13 05:39:49 +01:00
|
|
|
#include "elfio/elfio.hpp"
|
2022-11-16 01:55:48 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
inline uint32_t byteswap(uint32_t val) {
|
|
|
|
return _byteswap_ulong(val);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
constexpr uint32_t byteswap(uint32_t val) {
|
|
|
|
return __builtin_bswap32(val);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace RecompPort {
|
|
|
|
|
2023-03-24 22:11:17 +01:00
|
|
|
// Potential argument types for function declarations
|
|
|
|
enum class FunctionArgType {
|
|
|
|
u32,
|
|
|
|
s32,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mapping of function name to argument types
|
|
|
|
using DeclaredFunctionMap = std::unordered_map<std::string, std::vector<FunctionArgType>>;
|
|
|
|
|
2023-03-25 00:22:30 +01:00
|
|
|
struct InstructionPatch {
|
|
|
|
std::string func_name;
|
|
|
|
int32_t vram;
|
|
|
|
uint32_t value;
|
|
|
|
};
|
|
|
|
|
2023-10-30 01:53:17 +01:00
|
|
|
struct FunctionSize {
|
|
|
|
std::string func_name;
|
|
|
|
uint32_t size_bytes;
|
2024-05-14 02:55:43 +02:00
|
|
|
|
|
|
|
FunctionSize(const std::string& func_name, uint32_t size_bytes) : func_name(func_name), size_bytes(size_bytes) {}
|
2023-10-30 01:53:17 +01:00
|
|
|
};
|
|
|
|
|
2024-04-21 02:00:29 +02:00
|
|
|
struct ManualFunction {
|
|
|
|
std::string func_name;
|
|
|
|
std::string section_name;
|
|
|
|
uint32_t vram;
|
|
|
|
uint32_t size;
|
2024-05-14 02:55:43 +02:00
|
|
|
|
|
|
|
ManualFunction(const std::string& func_name, std::string section_name, uint32_t vram, uint32_t size) : func_name(func_name), section_name(std::move(section_name)), vram(vram), size(size) {}
|
2024-04-21 02:00:29 +02:00
|
|
|
};
|
|
|
|
|
2023-03-24 22:11:17 +01:00
|
|
|
struct Config {
|
|
|
|
int32_t entrypoint;
|
2023-11-12 20:50:50 +01:00
|
|
|
bool has_entrypoint;
|
2023-10-30 01:53:17 +01:00
|
|
|
bool uses_mips3_float_mode;
|
2023-11-12 20:50:50 +01:00
|
|
|
bool single_file_output;
|
|
|
|
bool use_absolute_symbols;
|
2023-03-24 22:11:17 +01:00
|
|
|
std::filesystem::path elf_path;
|
|
|
|
std::filesystem::path output_func_path;
|
|
|
|
std::filesystem::path relocatable_sections_path;
|
|
|
|
std::vector<std::string> stubbed_funcs;
|
2023-10-30 01:53:17 +01:00
|
|
|
std::vector<std::string> ignored_funcs;
|
2023-03-24 22:11:17 +01:00
|
|
|
DeclaredFunctionMap declared_funcs;
|
2023-03-25 00:22:30 +01:00
|
|
|
std::vector<InstructionPatch> instruction_patches;
|
2023-10-30 01:53:17 +01:00
|
|
|
std::vector<FunctionSize> manual_func_sizes;
|
2024-04-21 02:00:29 +02:00
|
|
|
std::vector<ManualFunction> manual_functions;
|
2023-10-30 01:53:17 +01:00
|
|
|
std::string bss_section_suffix;
|
2023-03-24 22:11:17 +01:00
|
|
|
|
|
|
|
Config(const char* path);
|
|
|
|
bool good() { return !bad; }
|
|
|
|
private:
|
|
|
|
bool bad;
|
|
|
|
};
|
|
|
|
|
2022-11-16 01:55:48 +01:00
|
|
|
struct JumpTable {
|
|
|
|
uint32_t vram;
|
|
|
|
uint32_t addend_reg;
|
|
|
|
uint32_t rom;
|
|
|
|
uint32_t lw_vram;
|
2022-11-16 01:55:48 +01:00
|
|
|
uint32_t addu_vram;
|
2022-11-16 01:55:48 +01:00
|
|
|
uint32_t jr_vram;
|
|
|
|
std::vector<uint32_t> entries;
|
2024-05-14 02:55:43 +02:00
|
|
|
|
|
|
|
JumpTable(uint32_t vram, uint32_t addend_reg, uint32_t rom, uint32_t lw_vram, uint32_t addu_vram, uint32_t jr_vram, std::vector<uint32_t>&& entries)
|
|
|
|
: vram(vram), addend_reg(addend_reg), rom(rom), lw_vram(lw_vram), addu_vram(addu_vram), jr_vram(jr_vram), entries(std::move(entries)) {}
|
2022-11-16 01:55:48 +01:00
|
|
|
};
|
|
|
|
|
2023-01-13 05:39:49 +01:00
|
|
|
struct AbsoluteJump {
|
|
|
|
uint32_t jump_target;
|
|
|
|
uint32_t instruction_vram;
|
2024-05-14 02:55:43 +02:00
|
|
|
|
|
|
|
AbsoluteJump(uint32_t jump_target, uint32_t instruction_vram) : jump_target(jump_target), instruction_vram(instruction_vram) {}
|
2023-01-13 05:39:49 +01:00
|
|
|
};
|
|
|
|
|
2022-11-16 01:55:48 +01:00
|
|
|
struct Function {
|
|
|
|
uint32_t vram;
|
|
|
|
uint32_t rom;
|
2023-03-25 01:28:36 +01:00
|
|
|
std::vector<uint32_t> words;
|
2022-11-16 01:55:48 +01:00
|
|
|
std::string name;
|
2023-01-13 05:39:49 +01:00
|
|
|
ELFIO::Elf_Half section_index;
|
2022-11-16 01:55:48 +01:00
|
|
|
bool ignored;
|
2023-01-13 05:39:49 +01:00
|
|
|
bool reimplemented;
|
2023-03-24 23:04:21 +01:00
|
|
|
bool stubbed;
|
2024-05-14 02:55:43 +02:00
|
|
|
|
|
|
|
Function(uint32_t vram, uint32_t rom, std::vector<uint32_t> words, std::string name, ELFIO::Elf_Half section_index, bool ignored = false, bool reimplemented = false, bool stubbed = false)
|
|
|
|
: vram(vram), rom(rom), words(std::move(words)), name(std::move(name)), section_index(section_index), ignored(ignored), reimplemented(reimplemented), stubbed(stubbed) {}
|
2023-01-13 05:39:49 +01:00
|
|
|
};
|
|
|
|
|
2023-02-04 06:14:03 +01:00
|
|
|
enum class RelocType : uint8_t {
|
|
|
|
R_MIPS_NONE = 0,
|
|
|
|
R_MIPS_16,
|
|
|
|
R_MIPS_32,
|
|
|
|
R_MIPS_REL32,
|
|
|
|
R_MIPS_26,
|
|
|
|
R_MIPS_HI16,
|
|
|
|
R_MIPS_LO16,
|
|
|
|
R_MIPS_GPREL16,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Reloc {
|
|
|
|
uint32_t address;
|
|
|
|
uint32_t target_address;
|
|
|
|
uint32_t symbol_index;
|
|
|
|
uint32_t target_section;
|
|
|
|
RelocType type;
|
|
|
|
bool needs_relocation;
|
|
|
|
};
|
|
|
|
|
2023-01-13 05:39:49 +01:00
|
|
|
struct Section {
|
2023-03-24 22:11:17 +01:00
|
|
|
ELFIO::Elf_Xword rom_addr = 0;
|
|
|
|
ELFIO::Elf64_Addr ram_addr = 0;
|
|
|
|
ELFIO::Elf_Xword size = 0;
|
2023-01-13 05:39:49 +01:00
|
|
|
std::vector<uint32_t> function_addrs;
|
2023-02-04 06:14:03 +01:00
|
|
|
std::vector<Reloc> relocs;
|
2023-01-13 05:39:49 +01:00
|
|
|
std::string name;
|
2023-10-30 01:53:17 +01:00
|
|
|
ELFIO::Elf_Half bss_section_index = (ELFIO::Elf_Half)-1;
|
2023-03-24 22:11:17 +01:00
|
|
|
bool executable = false;
|
|
|
|
bool relocatable = false;
|
2022-11-16 01:55:48 +01:00
|
|
|
};
|
|
|
|
|
2022-11-16 01:55:48 +01:00
|
|
|
struct FunctionStats {
|
|
|
|
std::vector<JumpTable> jump_tables;
|
2023-01-13 05:39:49 +01:00
|
|
|
std::vector<AbsoluteJump> absolute_jumps;
|
2022-11-16 01:55:48 +01:00
|
|
|
};
|
|
|
|
|
2022-11-16 01:55:48 +01:00
|
|
|
struct Context {
|
2023-01-13 05:39:49 +01:00
|
|
|
// ROM address of each section
|
|
|
|
std::vector<Section> sections;
|
|
|
|
std::vector<Function> functions;
|
2022-11-16 01:55:48 +01:00
|
|
|
std::unordered_map<uint32_t, std::vector<size_t>> functions_by_vram;
|
2023-03-24 23:04:21 +01:00
|
|
|
// A mapping of function name to index in the functions vector
|
|
|
|
std::unordered_map<std::string, size_t> functions_by_name;
|
2022-11-16 01:55:48 +01:00
|
|
|
std::vector<uint8_t> rom;
|
2023-01-13 05:39:49 +01:00
|
|
|
// A list of the list of each function (by index in `functions`) in a given section
|
|
|
|
std::vector<std::vector<size_t>> section_functions;
|
2023-02-04 06:14:03 +01:00
|
|
|
// The section names that were specified as relocatable
|
|
|
|
std::unordered_set<std::string> relocatable_sections;
|
2023-10-30 01:53:17 +01:00
|
|
|
// Functions with manual size overrides
|
|
|
|
std::unordered_map<std::string, size_t> manually_sized_funcs;
|
2023-01-13 05:39:49 +01:00
|
|
|
int executable_section_count;
|
|
|
|
|
|
|
|
Context(const ELFIO::elfio& elf_file) {
|
|
|
|
sections.resize(elf_file.sections.size());
|
|
|
|
section_functions.resize(elf_file.sections.size());
|
|
|
|
functions.reserve(1024);
|
2023-03-24 23:04:21 +01:00
|
|
|
functions_by_vram.reserve(functions.capacity());
|
|
|
|
functions_by_name.reserve(functions.capacity());
|
2023-01-13 05:39:49 +01:00
|
|
|
rom.reserve(8 * 1024 * 1024);
|
|
|
|
executable_section_count = 0;
|
|
|
|
}
|
2022-11-16 01:55:48 +01:00
|
|
|
};
|
2022-11-16 01:55:48 +01:00
|
|
|
|
2022-11-16 01:55:48 +01:00
|
|
|
bool analyze_function(const Context& context, const Function& function, const std::vector<rabbitizer::InstructionCpu>& instructions, FunctionStats& stats);
|
2023-11-12 20:50:50 +01:00
|
|
|
bool recompile_function(const Context& context, const Config& config, const Function& func, std::ofstream& output_file, std::span<std::vector<uint32_t>> static_funcs, bool write_header);
|
2022-11-16 01:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|