Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

XFF relocatable modules (Shadow of the Colossus)

Samir Nuri247363277264
diff --git a/recomp/main.cpp b/recomp/main.cpp
index 76deea6..52faafe 100644
--- a/recomp/main.cpp
+++ b/recomp/main.cpp
@@ -63,7 +63,7 @@ std::vector<std::pair<std::uint32_t, std::uint32_t>> checks_for(const Snr2Module
std::vector<std::uint32_t> words;
for (std::uint32_t pc = m.code_begin; pc + 4 <= m.code_end; pc += 4)
if (program.is_code(pc) && !m.links.count(pc) && word_at(m, pc)) words.push_back(pc);
- std::vector<std::pair<std::uint32_t, std::uint32_t>> checks{{0, 0x32524E53u}};
+ std::vector<std::pair<std::uint32_t, std::uint32_t>> checks{{0, word_at(m, 0)}};
for (std::size_t k = 0; k < 8 && !words.empty(); ++k) {
const std::uint32_t pc = words[k * words.size() / 8];
if (checks.back().first != pc) checks.emplace_back(pc, word_at(m, pc));
@@ -81,7 +81,8 @@ std::vector<Snr2Module> read_modules(const std::string &dir) {
for (const auto &path : paths) {
std::ifstream file(path, std::ios::binary);
std::vector<std::uint8_t> bytes{std::istreambuf_iterator<char>(file), {}};
- auto module = read_snr2_module(std::move(bytes));
+ auto module = read_snr2_module(bytes);
+ if (!module) module = read_xff_module(std::move(bytes), path.stem().string());
if (!module) continue;
if (std::any_of(modules.begin(), modules.end(), [&](const Snr2Module &m) { return m.bytes == module->bytes; })) continue;
if (const int n = names[module->name]++) module->name += "_" + std::to_string(n + 1);
@@ -173,7 +174,11 @@ int main(int argc, char **argv) {
try {
const Image image = Image::load(elf_path);
- const Entries entries = entries_path.empty() ? Entries{} : read_entries(entries_path);
+ Entries entries = entries_path.empty() ? Entries{} : read_entries(entries_path);
+ const std::vector<Snr2Module> modules = modules_dir.empty() ? std::vector<Snr2Module>{} : read_modules(modules_dir);
+ for (const Snr2Module &m : modules)
+ for (const std::uint32_t seed : m.host_seeds)
+ if (image.executable(seed)) entries.main.push_back(seed);
const Links imports = snr2_imports(image);
Program program = analyse(image, entries.main, dynamic_sites(imports));
std::map<Op, std::size_t> histogram;
@@ -186,7 +191,6 @@ int main(int argc, char **argv) {
for (const auto &[op, count] : histogram) std::printf("%10zu %s\n", count, name(op));
std::vector<Translation> translations{{&image, &program, imports.empty() ? nullptr : &imports, "", false, "", 0, {}}};
- const std::vector<Snr2Module> modules = modules_dir.empty() ? std::vector<Snr2Module>{} : read_modules(modules_dir);
std::deque<Image> images;
std::deque<Program> programs;
std::size_t module_code = 0;
diff --git a/recomp/snr2.cpp b/recomp/snr2.cpp
index c179274..59bbbf3 100644
--- a/recomp/snr2.cpp
+++ b/recomp/snr2.cpp
@@ -9,6 +9,7 @@ namespace recomp {
namespace {
constexpr std::uint32_t kMagic = 0x32524E53u;
+constexpr std::uint32_t kXffMagic = 0x32666678u;
std::uint32_t u32(const std::vector<std::uint8_t> &b, std::uint32_t o) {
std::uint32_t v = 0;
@@ -116,6 +117,69 @@ std::optional<Snr2Module> read_snr2_module(std::vector<std::uint8_t> bytes) {
return m;
}
+std::optional<Snr2Module> read_xff_module(std::vector<std::uint8_t> bytes, const std::string &name) {
+ if (bytes.size() < 0x70 || u32(bytes, 0) != kXffMagic) return std::nullopt;
+ Snr2Module m;
+ m.size = u32(bytes, 0x14);
+ const std::uint32_t sections = u32(bytes, 0x5C), nsec = u32(bytes, 0x40), symbols = u32(bytes, 0x54), nsym = u32(bytes, 0x24);
+ const std::uint32_t records = u32(bytes, 0x64), nrec = u32(bytes, 0x38);
+ if (m.size > bytes.size() || nsec < 2 || sections + nsec * 0x20u > m.size || symbols + nsym * 16u > m.size || records + nrec * 0x1Cu > m.size)
+ return std::nullopt;
+ std::string clean;
+ for (char ch : name) clean += std::isalnum(static_cast<unsigned char>(ch)) ? ch : '_';
+ m.name = clean.empty() ? "module" : clean;
+ m.code_begin = u32(bytes, sections + 0x20 + 0x1C);
+ m.code_end = m.code_begin + u32(bytes, sections + 0x20 + 0x08);
+ if (u32(bytes, sections + 0x20 + 0x10) != 1 || m.code_end > m.size || m.code_begin >= m.code_end) return std::nullopt;
+
+ m.seeds.push_back(m.code_begin + u32(bytes, 0x4C));
+ for (std::uint32_t k = 0; k < nsym; ++k) {
+ const std::uint32_t at = symbols + k * 16u;
+ if ((bytes[at + 12] & 0xFu) != 2) continue;
+ if (u16(bytes, at + 14) == 1) m.seeds.push_back(m.code_begin + u32(bytes, at + 4));
+ else if (u16(bytes, at + 14) == 0xFFF1) m.host_seeds.push_back(u32(bytes, at + 4));
+ }
+ const auto in_text = [&](std::uint32_t symbol, std::uint32_t &value) {
+ if (symbol >= nsym || u16(bytes, symbols + symbol * 16u + 14u) != 1) return false;
+ value = u32(bytes, symbols + symbol * 16u + 4u);
+ return true;
+ };
+ const auto seed = [&](std::uint32_t offset) {
+ if (!(offset & 3u) && m.code_begin + offset < m.code_end) m.seeds.push_back(m.code_begin + offset);
+ };
+ for (std::uint32_t k = 0; k < nrec; ++k) {
+ const std::uint32_t at = records + k * 0x1Cu;
+ const std::uint32_t count = u32(bytes, at + 4), table = u32(bytes, at + 20), target = u32(bytes, at + 8);
+ if (u32(bytes, at) != 9 || target >= nsec || table + count * 8u > m.size) continue;
+ const std::uint32_t section = u32(bytes, sections + target * 0x20u + 0x1C), section_size = u32(bytes, sections + target * 0x20u + 0x08);
+ std::uint32_t high_at = 0, high_symbol = ~0u;
+ for (std::uint32_t r = 0; r < count; ++r) {
+ const std::uint32_t offset = u32(bytes, table + r * 8u), info = u32(bytes, table + r * 8u + 4u);
+ const std::uint32_t type = info & 0xFFu, symbol = info >> 8;
+ if (offset >= section_size || !section) continue;
+ std::uint32_t value = 0;
+ if (target == 1) {
+ const auto fix = fix_of(type);
+ if (fix) m.links[m.code_begin + offset] = {*fix, true, 0};
+ if (type == 4 && in_text(symbol, value)) {
+ seed(value + ((u32(bytes, section + offset) & 0x03FFFFFFu) << 2));
+ } else if (type == 5) {
+ high_at = section + offset;
+ high_symbol = symbol;
+ } else if (type == 6 && symbol == high_symbol && in_text(symbol, value)) {
+ seed(value + (static_cast<std::uint32_t>(u16(bytes, high_at)) << 16) +
+ static_cast<std::uint32_t>(static_cast<std::int16_t>(u16(bytes, section + offset))));
+ }
+ } else if (type == 2 && in_text(symbol, value)) {
+ seed(value + u32(bytes, section + offset));
+ }
+ }
+ }
+ m.bytes = std::move(bytes);
+ m.bytes.resize(m.size);
+ return m;
+}
+
Links snr2_imports(const Image &image) {
Links links;
for (const auto &segment : image.segments()) {
diff --git a/recomp/snr2.hpp b/recomp/snr2.hpp
index bbc1c68..c0dfd1d 100644
--- a/recomp/snr2.hpp
+++ b/recomp/snr2.hpp
@@ -29,9 +29,11 @@ struct Snr2Module {
std::uint32_t code_end{};
Links links;
std::vector<std::uint32_t> seeds;
+ std::vector<std::uint32_t> host_seeds;
};
std::optional<Snr2Module> read_snr2_module(std::vector<std::uint8_t> bytes);
+std::optional<Snr2Module> read_xff_module(std::vector<std::uint8_t> bytes, const std::string &name);
Links snr2_imports(const Image &image);
Image snr2_image(const Snr2Module &module);
diff --git a/runtime/src/dispatch.cpp b/runtime/src/dispatch.cpp
index 258da94..fe6d8b7 100644
--- a/runtime/src/dispatch.cpp
+++ b/runtime/src/dispatch.cpp
@@ -20,6 +20,7 @@ std::set<std::uint32_t> g_misses;
std::set<std::string> g_module_misses;
constexpr std::uint32_t kModuleMagic = 0x32524E53u;
+constexpr std::uint32_t kXffMagic = 0x32666678u;
struct Instance {
std::uint32_t base;
@@ -34,7 +35,7 @@ std::vector<Instance> g_instances;
bool loaded_at(const ModuleInfo &m, std::uint32_t base) {
if (m.fixed_base) {
if (base != m.fixed_base) return false;
- } else if (ld32(base + 0x2Cu) != m.size) {
+ } else if (ld32(base + (ld32(base) == kXffMagic ? 0x14u : 0x2Cu)) != m.size) {
return false;
}
for (std::uint32_t k = 0; k < m.check_count; ++k)
@@ -78,7 +79,7 @@ const Instance *instance_for(std::uint32_t pc) {
}
}
for (std::uint32_t at = pc & ~15u; pc - at < g_module_span; at -= 16) {
- if (ld32(at) == kModuleMagic) {
+ if (ld32(at) == kModuleMagic || ld32(at) == kXffMagic) {
for (std::uint32_t k = 0; k < g_module_count; ++k)
if (pc - at < g_modules[k].size && loaded_at(g_modules[k], at)) {
g_instances.push_back(make_instance(at, &g_modules[k]));
diff --git a/scripts/disc.py b/scripts/disc.py
index c57dc50..9eb4520 100644
--- a/scripts/disc.py
+++ b/scripts/disc.py
@@ -122,6 +122,8 @@ def modules(image, destination):
head = read_at(image, sector, 0, 8)
if head[:4] == b'SNR2':
found.append((path.replace('/', '_'), sector, 0, size))
+ elif head[:4] == b'xff2':
+ found.append((os.path.splitext(path.replace('/', '_'))[0], sector, 0, size))
elif head[:4] == b'AFS\0':
count = struct.unpack_from('<I', head, 4)[0]
table = read_at(image, sector, 8, count * 8)
@@ -133,7 +135,7 @@ def modules(image, destination):
for name, sector, offset, length in found:
with open(os.path.join(destination, name + '.snr'), 'wb') as out:
out.write(read_at(image, sector, offset, length))
- print('%d SNR2 modules extracted to %s' % (len(found), destination))
+ print('%d relocatable modules extracted to %s' % (len(found), destination))
def open_source(source):
diff --git a/scripts/port.sh b/scripts/port.sh
index 3e50423..6370a3a 100755
--- a/scripts/port.sh
+++ b/scripts/port.sh
@@ -98,7 +98,7 @@ if [ ${#pnach[@]} -gt 0 ]; then
fi
echo "${elf#$ROOT/}"
modules=()
-if grep -q SNR2 "$elf"; then
+if grep -qE 'SNR2|xff2' "$elf"; then
python3 -I "$here/disc.py" modules "$iso" "$game/modules"
modules=(--modules "$game/modules")
fi