-
Notifications
You must be signed in to change notification settings - Fork 13
/
ps4.cpp
438 lines (396 loc) · 10.6 KB
/
ps4.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "ps4.h"
#include "elf.h"
//TODO replace and remove once std::filesystem is out of experimental
#ifdef _WIN32
#include "extern/dirent/include/dirent.h"
#else
#include <dirent.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cstring>
#include <json/json.h>
#ifndef __IDP__
#define msg printf
#define create_insn(...)
#define add_func(...)
#define set_name(...)
#else
#include <ida.hpp>
#include <idp.hpp>
#include <name.hpp>
#include <ua.hpp>
#define fopen qfopen
#define fread(buf, size, count, file) qfread(file, buf, count)
#define fclose qfclose
#endif
#define BUFFER_SIZE 64 * 1024 * 1024
CPS4::CPS4(char* file)
{
sprxfilename = file;
text_buf = (char*) malloc(BUFFER_SIZE);
LoadFile(file);
}
CPS4::~CPS4()
{
free(text_buf);
}
bool CPS4::isLoaded()
{
return _isLoaded;
}
void CPS4::LoadLibNames(bool val)
{
_displayLibName = val;
}
void CPS4::LoadJsonPath(std::string path)
{
_jsonpath = path;
}
int CPS4::isDirectory(const char *path)
{
struct stat statbuf;
if(stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
int CPS4::LoadFile(char *file)
{
FILE *f = fopen(file, "rb");
if(!f)
{
msg("Error opening: %s\n", file);
_isLoaded = false;
return -1;
}
int rd = fread(text_buf, 1, BUFFER_SIZE, f);
fclose(f);
_isLoaded = true;
return rd;
}
bool CPS4::LoadHeader()
{
Elf64_Ehdr* elf_header = reinterpret_cast<Elf64_Ehdr*>(&text_buf[0]);
Elf64_Phdr* segment = reinterpret_cast<Elf64_Phdr*>(&text_buf[elf_header->e_phoff]);
for(int i = 0; i < elf_header->e_phnum; i++, segment++)
{
if(segment->p_type == 1 && segment->p_vaddr == 0)
{
addr_off = segment->p_offset;
}
if(segment->p_type == 0x61000000)
{
data_addr_off = segment->p_offset;
}
if(segment->p_type == PT_DYNAMIC)
{
Elf64_Dyn* dyn = reinterpret_cast<Elf64_Dyn*>(&text_buf[segment->p_offset]);
for(int x = 0; x < segment->p_filesz; x++, dyn++)
{
if(dyn->d_tag == 0x61000037)
{
string_table_size = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x61000035)
{
string_table_offset = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x61000039)
{
symbol_table_offset = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x6100003f)
{
symbol_table_size = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x61000029)
{
pltrela_table_offset = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x6100002Dll)
{
pltrela_table_size = dyn->d_un.d_val;
}
if(dyn->d_tag == 0x61000015ll)
{
_libname_count++;
}
}
}
}
msg("data_addr_off: 0x%x\n", data_addr_off);
msg("symbol_table_offset: 0x%x\n", symbol_table_offset);
msg("symbol_table_size: 0x%x\n", symbol_table_size);
msg("string_table_offset: 0x%x\n", string_table_offset);
msg("string_table_size: 0x%x\n", string_table_size);
msg("pltrela_table_offset: 0x%x\n", pltrela_table_offset);
msg("pltrela_table_size: 0x%x\n", pltrela_table_size);
return
(
data_addr_off != 0
&& symbol_table_offset != 0
&& symbol_table_size != 0
&& string_table_offset != 0
&& string_table_size != 0
&& pltrela_table_offset != 0
&& pltrela_table_size != 0
);
}
bool CPS4::FindJsonSym(const char* path, const char* name, std::string* res)
{
DIR *dir;
struct dirent *ent;
if((dir = opendir(path)) != NULL)
{
while((ent = readdir(dir)) != NULL)
{
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
{
continue;
}
std::string file = path;
file = file.append("/").append(ent->d_name);
if(isDirectory(file.c_str()))
{
if(FindJsonSym(file.c_str(), name, res))
{
closedir(dir);
return true;
}
}
else if(strcmp(ent->d_name, name) == 0)
{
res->append(file);
closedir(dir);
return true;
}
}
closedir(dir);
}
return false;
}
bool CPS4::LoadJsonSym(bool clearmap)
{
if(clearmap) nidmap.clear();
int count = nidmap.size();
std::string path = _jsonpath + "/";
std::string filename = sprxfilename.substr(sprxfilename.find_last_of("/\\") + 1);
filename = filename.substr(0, filename.find_first_of(".")) + ".sprx.json";
{
msg("libName: %s", filename.c_str());
std::string jsonpath = "";
if(FindJsonSym(path.c_str(), filename.c_str(), &jsonpath))
{
msg(" - Found\n");
LoadJsonSymFile(jsonpath.c_str(), false);
}
else
{
msg(" - Not Found\n");
}
}
StringTable string_table =
{
reinterpret_cast<const char*>(&text_buf[string_table_offset + data_addr_off]),
static_cast<size_t>(string_table_size),
};
for(int i = 0, offset = 1; i < _libname_count; ++i)
{
std::string libName = string_table.get(offset);
offset += libName.length() +1;
libName = libName.substr(0, libName.size()-4);
libName += ".sprx.json";
msg("offset: %d libName: %s", offset, libName.c_str());
std::string jsonpath = "";
if(FindJsonSym(path.c_str(), libName.c_str(), &jsonpath))
{
msg(" - Found\n");
LoadJsonSymFile(jsonpath.c_str(), false);
}
else
{
msg(" - Not Found\n");
}
}
return nidmap.size() != 0 && nidmap.size() > count;
}
bool CPS4::LoadJsonSymFile(std::string filename, bool clearmap)
{
if(clearmap) nidmap.clear();
Json::Value root;
std::ifstream config_doc(filename.c_str(), std::ifstream::binary);
config_doc >> root;
for(auto& module : root["modules"])
{
for(auto& libraries : module["libraries"])
{
auto libname = libraries.get("name","NA").asString();
auto isExported = libraries.get("is_export", false).asBool();
for(auto& symbol : libraries["symbols"])
{
if(!symbol.get("name", "NA" ).asString().empty())
{
if(!isExported && _displayLibName)
{
nidmap[symbol.get("encoded_id", "NA" ).asString()] = libname + "::" + symbol.get("name", "NA" ).asString();
}
else
{
nidmap[symbol.get("encoded_id", "NA" ).asString()] = symbol.get("name", "NA" ).asString();
}
#if 0
std::cout << nidmap[symbol.get("encoded_id", "NA" ).asString()].c_str() << " NID:" << symbol.get("encoded_id", "NA" ).asString() << std::endl;
#endif
}
}
}
}
return nidmap.size() > 0;
}
void CPS4::LoadSym()
{
StringTable string_table =
{
reinterpret_cast<const char*>(&text_buf[string_table_offset + data_addr_off]),
static_cast<size_t>(string_table_size),
};
auto symbols = reinterpret_cast<Elf64_Sym*>(&text_buf[symbol_table_offset + data_addr_off]);
auto symbol_count = symbol_table_size / sizeof(Elf64_Sym);
auto symbol_end = &symbols[symbol_count];
if(_displayLibName)
{
//Workaround for lib names
//Instead of parsing lib names from elf, I'm associating those we identified
for(Elf64_Sym* symbol = &symbols[0]; symbol != symbol_end; ++symbol)
{
if(symbol->st_name != 0)
{
std::string candidate_local_name = string_table.get(symbol->st_name);
auto func_name = nidmap[candidate_local_name.substr(0, candidate_local_name.find_first_of("#")).c_str()];
if(!func_name.empty())
{
auto libCode = candidate_local_name.substr(candidate_local_name.find_first_of("#") + 1, 1);
if(!libmap[libCode].empty()) continue;
auto name = nidmap[candidate_local_name.substr(0, candidate_local_name.find_first_of("#")).substr()];
auto npos = name.find_first_of("::");
if(npos != std::string::npos)
{
auto libName = name.substr(0, npos);
libmap[libCode] = libName;
msg("libCode: %s libName: %s\n", libCode.c_str(), libName.c_str());
}
}
}
}
}
for(Elf64_Sym* symbol = &symbols[0]; symbol != symbol_end; ++symbol)
{
if(symbol->st_name != 0)
{
std::string candidate_local_name = string_table.get(symbol->st_name);
auto func_name = nidmap[candidate_local_name.substr(0, candidate_local_name.find_first_of("#")).c_str()];
if(!func_name.empty())
{
set_name(symbol->st_value, func_name.c_str());
msg("NID: %s ", candidate_local_name.c_str());
msg("Name: %s ", func_name.c_str());
}
else
{
if(symbol->st_value != 0)
{
std::string libName;
if(_displayLibName)
{
auto libCode = candidate_local_name.substr(candidate_local_name.find_first_of("#") + 1, 1);
if(!libmap[libCode].empty())
{
libName = libmap[libCode];
}
else
{
libName = "UNKLIB";
}
libName += "_";
}
std::replace(candidate_local_name.begin(), candidate_local_name.end(), '+', '_');
std::replace(candidate_local_name.begin(), candidate_local_name.end(), '-', '_');
if(isdigit(candidate_local_name[0]))
{
candidate_local_name = "_" + candidate_local_name;
}
func_name = libName + candidate_local_name.substr(0, candidate_local_name.find_first_of("#"));
set_name(symbol->st_value, func_name.c_str());
}
msg("NID: %s ", candidate_local_name.c_str());
msg("name: MISSING ");
}
if(symbol->st_value == 0)
{
msg("External Function ");
}
else
{
create_insn(symbol->st_value);
add_func(symbol->st_value);
msg("addr: 0x%06llx ", symbol->st_value);
}
msg("\n");
}
}
auto rela = reinterpret_cast<Elf64_Rela*>(&text_buf[pltrela_table_offset + data_addr_off]);
auto rela_end = &rela[pltrela_table_size / sizeof(Elf64_Rela)];
for(; rela < rela_end; ++rela)
{
auto type = rela->getType();
switch(type)
{
case 7:
Elf64_Sym* symbol = &symbols[rela->getSymbol()];
std::string candidate_local_name = string_table.get(symbol->st_name);
auto func_name = nidmap[candidate_local_name.substr(0, candidate_local_name.find_first_of("#")).c_str()];
if(!func_name.empty())
{
func_name.insert(0, "__imp_");
set_name(rela->r_offset, func_name.c_str());
msg("NID: %s ", candidate_local_name.c_str());
msg("Name: %s ", func_name.c_str());
}
else
{
std::string libName;
if(_displayLibName)
{
auto libCode = candidate_local_name.substr(candidate_local_name.find_first_of("#") + 1, 1);
if(!libmap[libCode].empty())
{
libName = libmap[libCode];
}
else
{
libName = "UNK";
}
libName += "_";
}
std::replace(candidate_local_name.begin(), candidate_local_name.end(), '+', '_');
std::replace(candidate_local_name.begin(), candidate_local_name.end(), '-', '_');
if(isdigit(candidate_local_name[0]))
{
candidate_local_name = "_" + candidate_local_name;
}
func_name = "__imp_" + libName + candidate_local_name.substr(0, candidate_local_name.find_first_of("#"));
set_name(rela->r_offset, func_name.c_str());
msg("NID: %s ", candidate_local_name.c_str());
msg("name: MISSING ");
}
msg("addr: 0x%06llx ", rela->r_offset);
msg("\n");
//msg("NID: %s ", candidate_local_name.substr(0, candidate_local_name.find_first_of("#")).c_str());
//msg("r_offset: 0x%06x \n", rela->r_offset);
break;
}
}
}