File size: 4,815 Bytes
13ca229 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // Three-stage x-ref for IL2CPP v31 string literals.
//
// v31 does not load a literal address with `adrp+add`; it goes through a metadata
// usage slot: adrp x8,<slot page> ; ldr x8,[x8,#off] -> *x8 == literal address.
// So we do it in three steps, all static:
// 1. find the literal address from script.json ScriptString
// 2. scan .so for 8-byte LE occurrences of that address -> candidate slot addresses
// 3. scan code for adrp+ldr that computes a candidate slot address -> code sites
// -> map code offset to nearest ScriptMethod (method name)
const fs = require('fs');
const WS = 'D:/tqhyg/Project/Prism Step';
const so = fs.readFileSync(WS + '/backup/apk/raw/libil2cpp.so');
const j = JSON.parse(fs.readFileSync(WS + '/dumps/script.json', 'utf8'));
const wanted = process.argv.slice(2).length ? process.argv.slice(2)
: ['token', 'day_token', 'data', 'result', 'status', 'code', 'error'];
// value -> addresses
const litAddr = new Map();
for (const s of j.ScriptString) {
if (wanted.includes(s.Value)) {
if (!litAddr.has(s.Value)) litAddr.set(s.Value, []);
litAddr.get(s.Value).push(s.Address);
}
}
// ---- step 2: slots holding the literal address (scan data region as u64 LE) ----
const slotsByLit = new Map(); // litAddr -> [slotAddr]
const litSet = new Set();
for (const arr of litAddr.values()) for (const a of arr) litSet.add(a);
for (let o = 0; o + 8 <= so.length; o += 8) {
const v = so.readUInt32LE(o);
if (!litSet.has(v)) continue; // cheap prefilter on low word
if (so.readUInt32LE(o + 4) !== 0) continue; // literals live below 4GB
if (!slotsByLit.has(v)) slotsByLit.set(v, []);
slotsByLit.get(v).push(o);
}
const allSlots = new Map(); // slotAddr -> litAddr
for (const [lit, arr] of slotsByLit) for (const s of arr) allSlots.set(s, lit);
console.log('=== literal -> slot mapping ===');
for (const [v, arr] of litAddr) {
for (const a of arr) {
const slots = slotsByLit.get(a) || [];
console.log(' ' + JSON.stringify(v) + ' lit@0x' + a.toString(16) +
' slots=' + slots.length + (slots.length ? ' @' + slots.slice(0, 4).map(x => '0x' + x.toString(16)).join(',') : ''));
}
}
// ---- methods index ----
const methods = j.ScriptMethod.map(m => ({ a: m.Address, n: m.Name })).sort((x, y) => x.a - y.a);
const mStarts = methods.map(m => m.a);
function methodAt(addr) {
let lo = 0, hi = mStarts.length - 1, best = -1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (mStarts[mid] <= addr) { best = mid; lo = mid + 1; } else hi = mid - 1;
}
if (best < 0) return null;
const m = methods[best];
return addr - m.a < 0x1000 ? m : null;
}
// ---- step 3: code x-refs to slot addresses ----
const hits = new Map(); // litAddr -> [{code, slot}]
const n = (so.length / 4) | 0;
let pendReg = -1, pendBase = 0, pendPc = -1;
for (let k = 0; k < n; k++) {
const i = so.readUInt32LE(k * 4);
const pc = k * 4;
if ((i & 0x9f000000) === 0x90000000) { // ADRP
const rd = i & 0x1f;
const immlo = (i >>> 29) & 3;
const immhi = (i >>> 5) & 0x7ffff;
let imm = (immhi << 2) | immlo;
if (imm & (1 << 20)) imm -= 1 << 21;
pendReg = rd; pendBase = (pc & ~0xfff) + (imm << 12); pendPc = pc;
continue;
}
if (pendReg >= 0 && (i & 0xffc00000) === 0xf9400000) { // LDR (imm, uoff, 64)
const rn = (i >>> 5) & 0x1f;
if (rn === pendReg) {
const imm12 = (i >>> 10) & 0xfff;
const slot = pendBase + imm12 * 8;
if (allSlots.has(slot)) {
const lit = allSlots.get(slot);
if (!hits.has(lit)) hits.set(lit, []);
hits.get(lit).push({ code: pendPc, slot });
}
pendReg = -1;
}
continue;
}
if (pendReg >= 0 && (i & 0xff800000) === 0x91000000) { // ADD (imm, 64)
const rn = (i >>> 5) & 0x1f;
if (rn === pendReg) {
const imm12 = (i >>> 10) & 0xfff;
const sh = (i >>> 22) & 1;
const slot = pendBase + (sh ? imm12 << 12 : imm12);
if (allSlots.has(slot)) {
const lit = allSlots.get(slot);
if (!hits.has(lit)) hits.set(lit, []);
hits.get(lit).push({ code: pendPc, slot });
}
pendReg = -1;
}
continue;
}
if (pendPc >= 0 && pc - pendPc > 32) pendReg = -1;
}
console.log('\n=== code x-refs ===');
let total = 0;
for (const [lit, arr] of hits) {
const name = j.ScriptString.find(s => s.Address === lit);
console.log(JSON.stringify(name ? name.Value : '?') + ' @0x' + lit.toString(16));
const seen = new Set();
for (const h of arr) {
const m = methodAt(h.code);
const key = m ? m.n : 'code@0x' + h.code.toString(16);
if (seen.has(key)) continue;
seen.add(key);
total++;
console.log(' ' + (m ? m.n + ' (+0x' + (h.code - m.a).toString(16) + ')' : key));
}
}
console.log('\ntotal distinct code sites: ' + total);
|