-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_elf.js
More file actions
53 lines (45 loc) · 1.51 KB
/
patch_elf.js
File metadata and controls
53 lines (45 loc) · 1.51 KB
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
const fs = require('fs');
const path = require('path');
const targetFile = process.argv[2];
if (!targetFile) {
console.error('Usage: node patch_elf.js <file>');
process.exit(1);
}
console.log(`Patching ${targetFile}...`);
const buffer = fs.readFileSync(targetFile);
let found = false;
const searchStr = '/data/data/com.termux/files/usr/lib';
const replacementBase = '$ORIGIN';
// Calculate padding to match exact length
const paddingLength = searchStr.length - replacementBase.length;
let replacementStr = replacementBase;
if (paddingLength > 0) {
// Each "/." is 2 chars.
const dots = Math.floor(paddingLength / 2);
for (let i = 0; i < dots; i++) {
replacementStr += '/.';
}
// If we have 1 char left, add a trailing slash or dot
if (replacementStr.length < searchStr.length) {
replacementStr += '/';
}
}
console.log(`Searching for: "${searchStr}"`);
console.log(`Replacing with: "${replacementStr}" (Length: ${replacementStr.length})`);
if (replacementStr.length !== searchStr.length) {
console.error('Length mismatch! Logic error.');
process.exit(1);
}
let index = buffer.indexOf(searchStr);
while (index !== -1) {
console.log(`Found occurrence at ${index}`);
buffer.write(replacementStr, index);
found = true;
index = buffer.indexOf(searchStr, index + 1);
}
if (found) {
fs.writeFileSync(targetFile, buffer);
console.log(`Successfully patched ${targetFile}!`);
} else {
console.log(`No occurrences of "${searchStr}" found in ${targetFile}.`);
}