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
|
#include "cafe_loader.h"
#include "cafe.h"
#include "tinfl.c"
cafe_loader::cafe_loader(elf_reader<elf32> *elf)
: m_elf(elf)
{
m_externStart = 0xffffffff;
m_externEnd = 0;
}
void cafe_loader::apply() {
applySegments();
swapSymbols();
applyRelocations();
processImports();
processExports();
applySymbols();
}
void cafe_loader::applySegments() {
auto §ions = m_elf->getSections();
// decompress all sections
// TODO: only decompress once and when needed
for (auto §ion : sections) {
if (section.sh_flags & ELF_SECTIONFLAGEX_CAFE_RPL_COMPZ) {
const char *data = section.data();
uint32 deflatedLen = *(uint32 *)data;
swap(deflatedLen);
unsigned char *deflatedData = new unsigned char[deflatedLen];
deflatedLen = tinfl_decompress_mem_to_mem(
deflatedData,
deflatedLen,
data + 4,
section.sh_size - 4,
TINFL_FLAG_PARSE_ZLIB_HEADER
);
section.setData((const char *)deflatedData, deflatedLen);
}
}
const char *stringTable = m_elf->getSectionStringTable()->data();
size_t index = 0;
for (auto section : m_elf->getSections()) {
if (section.sh_flags & SHF_ALLOC) {
if (section.sh_type == SHT_NULL)
continue;
uchar perm = SEGPERM_READ;
char *sclass;
if (section.sh_flags & SHF_WRITE)
perm |= SEGPERM_WRITE;
if (section.sh_flags & SHF_EXECINSTR)
perm |= SEGPERM_EXEC;
if (section.sh_flags & SHF_EXECINSTR &&
section.sh_type != ELF_SECTIONTYPE_CAFE_RPL_IMPORTS &&
section.sh_type != ELF_SECTIONTYPE_CAFE_RPL_EXPORTS)
sclass = CLASS_CODE;
else if (section.sh_type == SHT_NOBITS)
sclass = CLASS_BSS;
else
sclass = CLASS_DATA;
const char *data = section.data();
const char *name = NULL;
if (section.sh_name != NULL)
name = &stringTable[section.sh_name];
applySegment(index,
data,
section.sh_addr,
section.getSize(),
name,
sclass,
perm,
m_elf->getAlignment(section.sh_addralign),
section.sh_type == SHT_NOBITS ? false : true);
++index;
}
}
}
void cafe_loader::applySegment(uint32 sel,
const char *data,
uint32 addr,
uint32 size,
const char *name,
const char *sclass,
uchar perm,
uchar align,
bool load) {
segment_t seg;
seg.start_ea = addr;
seg.end_ea = addr + size;
seg.color = DEFCOLOR;
seg.sel = sel;
seg.bitness = 1;
seg.orgbase = sel;
seg.comb = scPub;
seg.perm = perm;
seg.flags = SFL_LOADER;
seg.align = align;
set_selector(sel, 0);
if (name == NULL)
name = "";
add_segm_ex(&seg, name, sclass, NULL);
if (load == true)
mem2base(data, addr, addr + size, BADADDR);
}
void cafe_loader::applyRelocations() {
auto §ions = m_elf->getSections();
for (auto §ion : sections) {
if (section.sh_type == SHT_RELA) {
auto symsec = m_elf->getSymbolsSection();
auto symbols = m_elf->getSymbols();
auto stringTable = m_elf->getSections()[symsec->sh_link].data();
auto nrela = section.getSize() / sizeof(Elf32_Rela);
auto relocations = reinterpret_cast<Elf32_Rela *>(section.data());
for (size_t i = 0; i < nrela; ++i) {
auto &rela = relocations[i];
swap(rela.r_info);
swap(rela.r_offset);
swap(rela.r_addend);
uint32 type = ELF32_R_TYPE(rela.r_info);
uint32 sym = ELF32_R_SYM (rela.r_info);
// r_offset = address of relocation
// r_addend = offset past symbol
// r_sym = address used as value to patch
if (type == R_PPC_NONE)
continue;
uint32 addr = symbols[sym].st_value + rela.r_addend;
switch (type) {
// TODO: support RPL relocation
// since RPL relocation is not yet supported we do
// not need to patch anything.
// as far as I've seen they're already set anyway
/* case R_PPC_ADDR32:
patch_long(rela.r_offset, addr);
break;
case R_PPC_ADDR16_LO:
patch_word(rela.r_offset, addr);
break;
case R_PPC_ADDR16_HI:
patch_word(rela.r_offset, addr >> 16);
break;
case R_PPC_ADDR16_HA:
patch_word(rela.r_offset, (addr + 0x8000) >> 16);
break; */
case R_PPC_REL24: {
if (symbols[sym].st_value & 0xc0000000 &&
ELF32_ST_TYPE(symbols[sym].st_info) == STT_FUNC) {
auto inst = get_original_dword(rela.r_offset);
auto addr = rela.r_offset + (inst & 0x3fffffc);
if (m_externStart > addr)
m_externStart = addr;
if (m_externEnd < addr)
m_externEnd = addr + 8;
import temp = {
addr,
symbols[sym].st_value,
&stringTable[symbols[sym].st_name]
};
m_imports.push_back(temp);
}
}
break;
}
}
}
}
}
void cafe_loader::processImports() {
if (m_externStart != 0xffffffff && m_externEnd != 0) {
segment_t ext;
ext.start_ea = m_externStart;
ext.end_ea = m_externEnd;
ext.sel = 255;
ext.bitness = 1;
ext.color = DEFCOLOR;
ext.orgbase = 255;
ext.comb = scPub;
ext.perm = SEGPERM_READ | SEGPERM_EXEC;
ext.flags = SFL_LOADER;
ext.align = saRelQword;
set_selector(255, 0);
add_segm_ex(&ext, ".extern", "XTRN", NULL);
}
for (auto &import : m_imports) {
force_name(import.addr, import.name);
// char lib[32];
// get_segm_name(import.orig, lib, 32); UPDATED
qstring lib;
get_segm_name(&lib, getseg(import.orig));
netnode impnode;
impnode.create();
/*if (demangle_name(name, 256, import.name, NULL)) UPDATED
impnode.supset(import.addr, name);
else
impnode.supset(import.addr, import.name);
*/
qstring out;
if (demangle_name(&out, import.name, 0))
impnode.supset(import.addr, out.c_str());
else
impnode.supset(import.addr, import.name);
import_module(lib.c_str() + 9, NULL, impnode, NULL, "wiiu");
}
}
void cafe_loader::processExports() {
segment_t *seg;
uint32 start = 0;
uint32 numExports;
if ((seg = get_segm_by_name(".fexports")) != NULL) {
start = seg->start_ea;
numExports = get_dword(start);
for (int i = 0; i < numExports + 1; ++i) {
create_dword((start + (i * 8)) + 0, 4);
create_dword((start + (i * 8)) + 4, 4);
if (i == 0)
continue;
uint32 addr = get_dword(start + (i * 8) + 0);
uint32 name = get_dword(start + (i * 8) + 4);
auto_make_proc(addr);
qstring exp;
get_strlit_contents(&exp, start + name, get_max_strlit_length(start + name, STRTYPE_C, true), STRTYPE_C);
add_entry(addr, addr, exp.c_str(), true);
}
}
if ((seg = get_segm_by_name(".dexports")) != NULL)
{
start = seg->start_ea;
numExports = get_dword(start);
for (int i = 0; i < numExports + 1; i++)
{
create_dword((start + (i * 8)) + 0, 4);
create_dword((start + (i * 8)) + 4, 4);
if (i == 0)
continue;
uint32 addr = get_dword(start + (8 * i) + 0);
uint32 name = get_dword(start + (8 * i) + 4);
qstring exp;
get_strlit_contents(&exp, start + name,
get_max_strlit_length(start + name, STRTYPE_C, true),
STRTYPE_C);
add_entry(addr, addr, exp.c_str(), true);
}
}
}
void cafe_loader::swapSymbols() {
msg("Swapping symbols...\n");
// Since section based relocations depend on symbols
// we need to swap symbols before we get to relocations.
auto section = m_elf->getSymbolsSection();
auto symbols = m_elf->getSymbols();
for (size_t i = 0;
i < section->getSize() /
section->sh_entsize; ++i) {
auto symbol = &symbols[i];
swap(symbol->st_name);
swap(symbol->st_shndx);
swap(symbol->st_size);
swap(symbol->st_value);
}
}
void cafe_loader::applySymbols() {
msg("Applying symbols...");
auto section = m_elf->getSymbolsSection();
if (section == NULL)
return;
auto nsym = section->getSize() / section->sh_entsize;
auto symbols = m_elf->getSymbols();
const char *stringTable = m_elf->getSections()[section->sh_link].data();
for (size_t i = 0; i < nsym; ++i) {
auto &symbol = symbols[i];
uint32 type = ELF32_ST_TYPE(symbol.st_info),
bind = ELF32_ST_BIND(symbol.st_info);
uint32 value = symbol.st_value;
if (symbol.st_shndx > m_elf->getNumSections() ||
!(m_elf->getSections()[symbol.st_shndx].sh_flags & SHF_ALLOC))
continue;
if (symbol.st_shndx == SHN_ABS)
continue;
// crashes with current ida if setting these names
if (0 == strcmp(&stringTable[symbol.st_name], "main") || 0 == strcmp(&stringTable[symbol.st_name], "_main"))
continue;
// TODO: these are the same for all ELF's, maybe move to ELF reader
switch (type) {
case STT_OBJECT:
force_name(value, &stringTable[symbol.st_name]);
break;
case STT_FILE:
add_extra_line(value, true, "Source File: %s", &stringTable[symbol.st_name]);
break;
case STT_FUNC:
force_name(value, &stringTable[symbol.st_name]);
auto_make_proc(value);
break;
}
}
}
|