-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
RSO.cpp
646 lines (544 loc) · 16 KB
/
RSO.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "Core/Debugger/RSO.h"
#include <cstddef>
#include <list>
#include <string>
#include <utility>
#include <vector>
#include "Common/CommonFuncs.h"
#include "Common/Logging/Log.h"
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h"
void RSOHeaderView::Load(u32 address)
{
m_address = address;
m_header.entry.next_entry = PowerPC::HostRead_U32(address);
m_header.entry.prev_entry = PowerPC::HostRead_U32(address + 0x04);
m_header.entry.section_count = PowerPC::HostRead_U32(address + 0x08);
m_header.entry.section_table_offset = PowerPC::HostRead_U32(address + 0xC);
m_header.entry.name_offset = PowerPC::HostRead_U32(address + 0x10);
m_header.entry.name_size = PowerPC::HostRead_U32(address + 0x14);
m_header.entry.version = PowerPC::HostRead_U32(address + 0x18);
m_header.entry.bss_size = PowerPC::HostRead_U32(address + 0x1C);
m_header.section_info.prolog_section_index = PowerPC::HostRead_U8(address + 0x20);
m_header.section_info.epilog_section_index = PowerPC::HostRead_U8(address + 0x21);
m_header.section_info.unresolved_section_index = PowerPC::HostRead_U8(address + 0x22);
m_header.section_info.bss_section_index = PowerPC::HostRead_U8(address + 0x23);
m_header.section_info.prolog_offset = PowerPC::HostRead_U32(address + 0x24);
m_header.section_info.epilog_offset = PowerPC::HostRead_U32(address + 0x28);
m_header.section_info.unresolved_offset = PowerPC::HostRead_U32(address + 0x2C);
m_header.relocation_tables.internals_offset = PowerPC::HostRead_U32(address + 0x30);
m_header.relocation_tables.internals_size = PowerPC::HostRead_U32(address + 0x34);
m_header.relocation_tables.externals_offset = PowerPC::HostRead_U32(address + 0x38);
m_header.relocation_tables.externals_size = PowerPC::HostRead_U32(address + 0x3C);
m_header.symbol_tables.exports_offset = PowerPC::HostRead_U32(address + 0x40);
m_header.symbol_tables.exports_size = PowerPC::HostRead_U32(address + 0x44);
m_header.symbol_tables.exports_name_table = PowerPC::HostRead_U32(address + 0x48);
m_header.symbol_tables.imports_offset = PowerPC::HostRead_U32(address + 0x4C);
m_header.symbol_tables.imports_size = PowerPC::HostRead_U32(address + 0x50);
m_header.symbol_tables.imports_name_table = PowerPC::HostRead_U32(address + 0x54);
// Prevent an invalid name going wild
if (m_header.entry.name_size < 0x100)
m_name = PowerPC::HostGetString(m_header.entry.name_offset, m_header.entry.name_size);
else
m_name = PowerPC::HostGetString(m_header.entry.name_offset, 0x100);
}
u32 RSOHeaderView::GetNextEntry() const
{
return m_header.entry.next_entry;
}
u32 RSOHeaderView::GetPrevEntry() const
{
return m_header.entry.prev_entry;
}
u32 RSOHeaderView::GetSectionCount() const
{
return m_header.entry.section_count;
}
u32 RSOHeaderView::GetSectionTableOffset() const
{
return m_header.entry.section_table_offset;
}
const std::string& RSOHeaderView::GetName() const
{
return m_name;
}
u32 RSOHeaderView::GetVersion() const
{
return m_header.entry.version;
}
u32 RSOHeaderView::GetBssSectionSize() const
{
return m_header.entry.bss_size;
}
u32 RSOHeaderView::GetPrologSectionIndex() const
{
return m_header.section_info.prolog_section_index;
}
u32 RSOHeaderView::GetEpilogSectionIndex() const
{
return m_header.section_info.epilog_section_index;
}
u32 RSOHeaderView::GetUnresolvedSectionIndex() const
{
return m_header.section_info.unresolved_section_index;
}
u32 RSOHeaderView::GetBssSectionIndex() const
{
return m_header.section_info.bss_section_index;
}
u32 RSOHeaderView::GetPrologSectionOffset() const
{
return m_header.section_info.prolog_offset;
}
u32 RSOHeaderView::GetEpilogSectionOffset() const
{
return m_header.section_info.epilog_offset;
}
u32 RSOHeaderView::GetUnresolvedSectionOffset() const
{
return m_header.section_info.unresolved_offset;
}
u32 RSOHeaderView::GetInternalsOffset() const
{
return m_header.relocation_tables.internals_offset;
}
u32 RSOHeaderView::GetInternalsSize() const
{
return m_header.relocation_tables.internals_size;
}
u32 RSOHeaderView::GetExternalsOffset() const
{
return m_header.relocation_tables.externals_offset;
}
u32 RSOHeaderView::GetExternalsSize() const
{
return m_header.relocation_tables.externals_size;
}
u32 RSOHeaderView::GetExportsOffset() const
{
return m_header.symbol_tables.exports_offset;
}
u32 RSOHeaderView::GetExportsSize() const
{
return m_header.symbol_tables.exports_size;
}
u32 RSOHeaderView::GetExportsNameTable() const
{
return m_header.symbol_tables.exports_name_table;
}
u32 RSOHeaderView::GetImportsOffset() const
{
return m_header.symbol_tables.imports_offset;
}
u32 RSOHeaderView::GetImportsSize() const
{
return m_header.symbol_tables.imports_size;
}
u32 RSOHeaderView::GetImportsNameTable() const
{
return m_header.symbol_tables.imports_name_table;
}
void RSOSectionsView::Load(u32 address, std::size_t count)
{
m_address = address;
for (std::size_t i = 0; i < count; ++i)
{
RSOSection section;
section.offset = PowerPC::HostRead_U32(address);
section.size = PowerPC::HostRead_U32(address + 4);
m_sections.emplace_back(std::move(section));
address += sizeof(RSOSection);
}
}
size_t RSOSectionsView::Count() const
{
return m_sections.size();
}
const RSOSection& RSOSectionsView::GetSection(std::size_t index) const
{
return m_sections.at(index);
}
const std::vector<RSOSection>& RSOSectionsView::GetSections() const
{
return m_sections;
}
void RSOImportsView::Load(u32 address, std::size_t count)
{
m_address = address;
for (std::size_t i = 0; i < count; ++i)
{
RSOImport rso_import;
rso_import.name_offset = PowerPC::HostRead_U32(address);
rso_import.code_offset = PowerPC::HostRead_U32(address + 4);
rso_import.entry_offset = PowerPC::HostRead_U32(address + 8);
m_imports.emplace_back(std::move(rso_import));
address += sizeof(RSOImport);
}
}
std::size_t RSOImportsView::Count() const
{
return m_imports.size();
}
const RSOImport& RSOImportsView::GetImport(std::size_t index) const
{
return m_imports.at(index);
}
const std::vector<RSOImport>& RSOImportsView::GetImports() const
{
return m_imports;
}
void RSOExportsView::Load(u32 address, std::size_t count)
{
m_address = address;
for (std::size_t i = 0; i < count; ++i)
{
RSOExport rso_export;
rso_export.name_offset = PowerPC::HostRead_U32(address);
rso_export.code_offset = PowerPC::HostRead_U32(address + 4);
rso_export.section_index = PowerPC::HostRead_U32(address + 8);
rso_export.hash = PowerPC::HostRead_U32(address + 12);
m_exports.emplace_back(std::move(rso_export));
address += sizeof(RSOExport);
}
}
std::size_t RSOExportsView::Count() const
{
return m_exports.size();
}
const RSOExport& RSOExportsView::GetExport(std::size_t index) const
{
return m_exports.at(index);
}
const std::vector<RSOExport>& RSOExportsView::GetExports() const
{
return m_exports;
}
void RSOInternalsView::Load(u32 address, std::size_t count)
{
m_address = address;
for (std::size_t i = 0; i < count; ++i)
{
RSOInternalsEntry entry;
entry.r_offset = PowerPC::HostRead_U32(address);
entry.r_info = PowerPC::HostRead_U32(address + 4);
entry.r_addend = PowerPC::HostRead_U32(address + 8);
m_entries.emplace_back(std::move(entry));
address += sizeof(RSOInternalsEntry);
}
}
std::size_t RSOInternalsView::Count() const
{
return m_entries.size();
}
const RSOInternalsEntry& RSOInternalsView::GetEntry(std::size_t index) const
{
return m_entries.at(index);
}
const std::vector<RSOInternalsEntry>& RSOInternalsView::GetEntries() const
{
return m_entries;
}
void RSOExternalsView::Load(u32 address, std::size_t count)
{
m_address = address;
for (std::size_t i = 0; i < count; ++i)
{
RSOExternalsEntry entry;
entry.r_offset = PowerPC::HostRead_U32(address);
entry.r_info = PowerPC::HostRead_U32(address + 4);
entry.r_addend = PowerPC::HostRead_U32(address + 8);
m_entries.emplace_back(std::move(entry));
address += sizeof(RSOExternalsEntry);
}
}
std::size_t RSOExternalsView::Count() const
{
return m_entries.size();
}
const RSOExternalsEntry& RSOExternalsView::GetEntry(std::size_t index) const
{
return m_entries.at(index);
}
const std::vector<RSOExternalsEntry>& RSOExternalsView::GetEntries() const
{
return m_entries;
}
void RSOView::LoadHeader(u32 address)
{
m_address = address;
m_header.Load(address);
}
void RSOView::LoadSections()
{
m_sections.Load(m_header.GetSectionTableOffset(), m_header.GetSectionCount());
}
void RSOView::LoadImports()
{
std::size_t size = m_header.GetImportsSize();
if (size % sizeof(RSOImport) != 0)
WARN_LOG(OSHLE, "RSO Imports Table has an incoherent size (%08zx)", size);
m_imports.Load(m_header.GetImportsOffset(), size / sizeof(RSOImport));
}
void RSOView::LoadExports()
{
std::size_t size = m_header.GetExportsSize();
if (size % sizeof(RSOExport) != 0)
WARN_LOG(OSHLE, "RSO Exports Table has an incoherent size (%08zx)", size);
m_exports.Load(m_header.GetExportsOffset(), size / sizeof(RSOExport));
}
void RSOView::LoadInternals()
{
std::size_t size = m_header.GetInternalsSize();
if (size % sizeof(RSOInternalsEntry) != 0)
WARN_LOG(OSHLE, "RSO Internals Relocation Table has an incoherent size (%08zx)", size);
m_imports.Load(m_header.GetInternalsOffset(), size / sizeof(RSOInternalsEntry));
}
void RSOView::LoadExternals()
{
std::size_t size = m_header.GetExternalsSize();
if (size % sizeof(RSOExternalsEntry) != 0)
WARN_LOG(OSHLE, "RSO Externals Relocation Table has an incoherent size (%08zx)", size);
m_imports.Load(m_header.GetExternalsOffset(), size / sizeof(RSOExternalsEntry));
}
void RSOView::LoadAll(u32 address)
{
LoadHeader(address);
LoadSections();
LoadImports();
LoadExports();
LoadInternals();
LoadExternals();
}
void RSOView::Apply(PPCSymbolDB* symbol_db) const
{
std::size_t count = 0;
for (const RSOExport& rso_export : GetExports())
{
u32 address = GetExportAddress(rso_export);
if (address != 0)
{
Symbol* symbol = symbol_db->AddFunction(address);
if (!symbol)
symbol = symbol_db->GetSymbolFromAddr(address);
if (symbol)
{
symbol->name = GetExportName(rso_export);
count += 1;
}
}
}
DEBUG_LOG(OSHLE, "RSO(%s): %zu/%zu symbols applied", GetName().c_str(), count, GetExportsCount());
}
u32 RSOView::GetNextEntry() const
{
return m_header.GetNextEntry();
}
u32 RSOView::GetPrevEntry() const
{
return m_header.GetPrevEntry();
}
u32 RSOView::GetVersion() const
{
return m_header.GetVersion();
}
u32 RSOView::GetAddress() const
{
return m_address;
}
std::size_t RSOView::GetSectionCount() const
{
return m_sections.Count();
}
const RSOSection& RSOView::GetSection(std::size_t index) const
{
return m_sections.GetSection(index);
}
const std::vector<RSOSection>& RSOView::GetSections() const
{
return m_sections.GetSections();
}
std::size_t RSOView::GetImportsCount() const
{
return m_imports.Count();
}
const RSOImport& RSOView::GetImport(std::size_t index) const
{
return m_imports.GetImport(index);
}
std::string RSOView::GetImportName(const RSOImport& rso_import) const
{
return PowerPC::HostGetString(m_header.GetImportsNameTable() + rso_import.name_offset);
}
const std::vector<RSOImport>& RSOView::GetImports() const
{
return m_imports.GetImports();
}
std::size_t RSOView::GetExportsCount() const
{
return m_exports.Count();
}
const RSOExport& RSOView::GetExport(std::size_t index) const
{
return m_exports.GetExport(index);
}
std::string RSOView::GetExportName(const RSOExport& rso_export) const
{
return PowerPC::HostGetString(m_header.GetExportsNameTable() + rso_export.name_offset);
}
u32 RSOView::GetExportAddress(const RSOExport& rso_export) const
{
u32 address = 0;
if (rso_export.section_index < GetSectionCount())
address = GetSection(rso_export.section_index).offset + rso_export.code_offset;
return address;
}
const std::vector<RSOExport>& RSOView::GetExports() const
{
return m_exports.GetExports();
}
std::size_t RSOView::GetInternalsCount() const
{
return m_internals.Count();
}
const RSOInternalsEntry& RSOView::GetInternalsEntry(std::size_t index) const
{
return m_internals.GetEntry(index);
}
const std::vector<RSOInternalsEntry>& RSOView::GetInternals() const
{
return m_internals.GetEntries();
}
std::size_t RSOView::GetExternalsCount() const
{
return m_externals.Count();
}
const RSOExternalsEntry& RSOView::GetExternalsEntry(std::size_t index) const
{
return m_externals.GetEntry(index);
}
const std::vector<RSOExternalsEntry>& RSOView::GetExternals() const
{
return m_externals.GetEntries();
}
const std::string& RSOView::GetName() const
{
return m_header.GetName();
}
std::string RSOView::GetName(const RSOImport& rso_import) const
{
return GetImportName(rso_import);
}
std::string RSOView::GetName(const RSOExport& rso_export) const
{
return GetExportName(rso_export);
}
u32 RSOView::GetProlog() const
{
u32 section_index = m_header.GetPrologSectionIndex();
if (section_index == 0)
WARN_LOG(OSHLE, "RSO doesn't have a prolog function");
else if (section_index >= m_sections.Count())
WARN_LOG(OSHLE, "RSO prolog section index out of bound");
else
return GetSection(section_index).offset + m_header.GetPrologSectionOffset();
return 0;
}
u32 RSOView::GetEpilog() const
{
u32 section_index = m_header.GetEpilogSectionIndex();
if (section_index == 0)
WARN_LOG(OSHLE, "RSO doesn't have an epilog function");
else if (section_index >= m_sections.Count())
WARN_LOG(OSHLE, "RSO epilog section index out of bound");
else
return GetSection(section_index).offset + m_header.GetEpilogSectionOffset();
return 0;
}
u32 RSOView::GetUnresolved() const
{
u32 section_index = m_header.GetUnresolvedSectionIndex();
if (section_index == 0)
WARN_LOG(OSHLE, "RSO doesn't have a unresolved function");
else if (section_index >= m_sections.Count())
WARN_LOG(OSHLE, "RSO unresolved section index out of bound");
else
return GetSection(section_index).offset + m_header.GetUnresolvedSectionOffset();
return 0;
}
bool RSOChainView::Load(u32 address)
{
// Load node
RSOView node;
node.LoadHeader(address);
DEBUG_LOG(OSHLE, "RSOChain node name: %s", node.GetName().c_str());
m_chain.emplace_front(std::move(node));
if (LoadNextChain(m_chain.front()) && LoadPrevChain(m_chain.front()))
{
for (RSOView& view : m_chain)
{
view.LoadSections();
view.LoadExports();
view.LoadImports();
view.LoadExternals();
view.LoadInternals();
}
return true;
}
return false;
}
void RSOChainView::Apply(PPCSymbolDB* symbol_db) const
{
for (const RSOView& rso_view : m_chain)
rso_view.Apply(symbol_db);
}
void RSOChainView::Clear()
{
m_chain.clear();
}
const std::list<RSOView>& RSOChainView::GetChain() const
{
return m_chain;
}
bool RSOChainView::LoadNextChain(const RSOView& view)
{
u32 prev_address = view.GetAddress();
u32 next_address = view.GetNextEntry();
while (next_address != 0)
{
RSOView next_node;
next_node.LoadHeader(next_address);
if (prev_address != next_node.GetPrevEntry())
{
ERROR_LOG(OSHLE, "RSOChain has an incoherent previous link %08x != %08x in %s", prev_address,
next_node.GetPrevEntry(), next_node.GetName().c_str());
return false;
}
prev_address = next_address;
next_address = next_node.GetNextEntry();
m_chain.emplace_back(std::move(next_node));
}
return true;
}
bool RSOChainView::LoadPrevChain(const RSOView& view)
{
u32 prev_address = view.GetPrevEntry();
u32 next_address = view.GetAddress();
while (prev_address != 0)
{
RSOView prev_node;
prev_node.LoadHeader(prev_address);
if (next_address != prev_node.GetNextEntry())
{
ERROR_LOG(OSHLE, "RSOChain has an incoherent next link %08x != %08x in %s", next_address,
prev_node.GetNextEntry(), prev_node.GetName().c_str());
return false;
}
next_address = prev_address;
prev_address = prev_node.GetPrevEntry();
m_chain.emplace_front(std::move(prev_node));
}
return true;
}