Skip to content

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

C++

Recompiler: seed functions across the whole .text section

Samir Nuri94d4dba3e065
diff --git a/recomp/analysis.cpp b/recomp/analysis.cpp
index 5930c2c..5c47e61 100644
--- a/recomp/analysis.cpp
+++ b/recomp/analysis.cpp
@@ -116,13 +116,16 @@ std::vector<std::uint32_t> address_constants(const Image &image, const Program &
if (!p.is_code(pc)) continue;
const Insn lui = decode(*image.word(pc), pc);
if (lui.op != Op::Lui) continue;
- for (std::uint32_t next = pc + 4; next < pc + 40 && p.is_code(next); next += 4) {
+ std::uint32_t stop = pc + 160;
+ for (std::uint32_t next = pc + 4; next < stop && p.is_code(next); next += 4) {
const Insn insn = decode(*image.word(next), next);
if ((insn.op == Op::Addiu || insn.op == Op::Ori) && insn.rs() == lui.rt()) {
const std::uint32_t high = lui.uimm() << 16;
found.push_back(insn.op == Op::Addiu ? high + static_cast<std::uint32_t>(insn.simm()) : high | insn.uimm());
}
if (insn.rt() == lui.rt() && insn.op == Op::Lui) break;
+ const Flow f = flow(insn.op);
+ if (f == Flow::Jump || f == Flow::JumpRegister || f == Flow::Stop) stop = std::min(stop, std::max(pc + 40, next + 8));
}
}
return found;
@@ -205,6 +208,10 @@ Program analyse(const Image &image, const std::vector<std::uint32_t> &extra_entr
text_end = pc + 4;
}
}
+ if (image.text().second > image.text().first) {
+ text_begin = std::min(text_begin, image.text().first);
+ text_end = std::max(text_end, image.text().second);
+ }
for (int round = 0; round < 8; ++round) {
std::vector<std::uint32_t> candidates = address_constants(image, p);
diff --git a/recomp/elf.cpp b/recomp/elf.cpp
index 9a55104..1fc0dcc 100644
--- a/recomp/elf.cpp
+++ b/recomp/elf.cpp
@@ -45,8 +45,16 @@ Image Image::load(const std::string &path) {
image.segments_.push_back(std::move(segment));
}
+ const auto shstrndx = read<std::uint16_t>(data, 50);
for (std::uint32_t i = 0; shoff && i < shnum; ++i) {
const std::size_t sh = shoff + i * shentsize;
+ if (read<std::uint32_t>(data, sh + 4) == 1 && shstrndx < shnum) {
+ const std::size_t name = read<std::uint32_t>(data, shoff + shstrndx * shentsize + 16) + read<std::uint32_t>(data, sh);
+ if (name + 6 <= data.size() && std::memcmp(data.data() + name, ".text", 6) == 0) {
+ const auto address = read<std::uint32_t>(data, sh + 12), size = read<std::uint32_t>(data, sh + 20);
+ image.text_ = {address, address + size};
+ }
+ }
if (read<std::uint32_t>(data, sh + 4) != 2) continue;
const auto symoff = read<std::uint32_t>(data, sh + 16), symsize = read<std::uint32_t>(data, sh + 20);
const auto link = read<std::uint32_t>(data, sh + 24);
diff --git a/recomp/elf.hpp b/recomp/elf.hpp
index 957b680..590c5f3 100644
--- a/recomp/elf.hpp
+++ b/recomp/elf.hpp
@@ -4,6 +4,7 @@
#include <map>
#include <optional>
#include <string>
+#include <utility>
#include <vector>
namespace recomp {
@@ -21,6 +22,7 @@ public:
std::uint32_t entry() const { return entry_; }
const std::vector<Segment> &segments() const { return segments_; }
const std::map<std::uint32_t, std::string> &symbols() const { return symbols_; }
+ std::pair<std::uint32_t, std::uint32_t> text() const { return text_; }
std::optional<std::uint32_t> word(std::uint32_t address) const;
bool executable(std::uint32_t address) const;
@@ -30,6 +32,7 @@ private:
std::uint32_t entry_{};
std::vector<Segment> segments_;
std::map<std::uint32_t, std::string> symbols_;
+ std::pair<std::uint32_t, std::uint32_t> text_{};
};
}