forked from encounter/decomp-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfa.rs
634 lines (594 loc) · 24.5 KB
/
cfa.rs
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
use std::{
cmp::min,
collections::BTreeMap,
fmt::{Debug, Display, Formatter, UpperHex},
ops::{Add, AddAssign, BitAnd, Sub},
};
use anyhow::{bail, ensure, Context, Result};
use itertools::Itertools;
use crate::{
analysis::{
executor::{ExecCbData, ExecCbResult, Executor},
skip_alignment,
slices::{FunctionSlices, TailCallResult},
vm::{BranchTarget, GprValue, StepResult, VM},
RelocationTarget,
},
obj::{
ObjInfo, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind,
SectionIndex,
},
};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SectionAddress {
pub section: SectionIndex,
pub address: u32,
}
impl Debug for SectionAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{:#X}", self.section as isize, self.address)
}
}
impl Display for SectionAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{:#X}", self.section as isize, self.address)
}
}
impl SectionAddress {
pub fn new(section: SectionIndex, address: u32) -> Self { Self { section, address } }
pub fn offset(self, offset: i32) -> Self {
Self { section: self.section, address: self.address.wrapping_add_signed(offset) }
}
pub fn align_up(self, align: u32) -> Self {
Self { section: self.section, address: (self.address + align - 1) & !(align - 1) }
}
pub fn align_down(self, align: u32) -> Self {
Self { section: self.section, address: self.address & !(align - 1) }
}
pub fn is_aligned(self, align: u32) -> bool { self.address & (align - 1) == 0 }
pub fn wrapping_add(self, rhs: u32) -> Self {
Self { section: self.section, address: self.address.wrapping_add(rhs) }
}
}
impl Add<u32> for SectionAddress {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
Self { section: self.section, address: self.address + rhs }
}
}
impl Sub<u32> for SectionAddress {
type Output = Self;
fn sub(self, rhs: u32) -> Self::Output {
Self { section: self.section, address: self.address - rhs }
}
}
impl AddAssign<u32> for SectionAddress {
fn add_assign(&mut self, rhs: u32) { self.address += rhs; }
}
impl UpperHex for SectionAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{:#010X}", self.section as isize, self.address)
}
}
impl BitAnd<u32> for SectionAddress {
type Output = u32;
fn bitand(self, rhs: u32) -> Self::Output { self.address & rhs }
}
#[derive(Default, Debug, Clone)]
pub struct FunctionInfo {
pub analyzed: bool,
pub end: Option<SectionAddress>,
pub slices: Option<FunctionSlices>,
}
impl FunctionInfo {
pub fn is_analyzed(&self) -> bool { self.analyzed }
pub fn is_function(&self) -> bool {
self.analyzed && self.end.is_some() && self.slices.is_some()
}
pub fn is_non_function(&self) -> bool {
self.analyzed && self.end.is_none() && self.slices.is_none()
}
pub fn is_unfinalized(&self) -> bool {
self.analyzed && self.end.is_none() && self.slices.is_some()
}
}
#[derive(Debug, Default)]
pub struct AnalyzerState {
pub sda_bases: Option<(u32, u32)>,
pub functions: BTreeMap<SectionAddress, FunctionInfo>,
pub jump_tables: BTreeMap<SectionAddress, u32>,
pub known_symbols: BTreeMap<SectionAddress, Vec<ObjSymbol>>,
pub known_sections: BTreeMap<SectionIndex, String>,
}
impl AnalyzerState {
pub fn apply(&self, obj: &mut ObjInfo) -> Result<()> {
for (§ion_index, section_name) in &self.known_sections {
obj.sections[section_index].rename(section_name.clone())?;
}
for (&start, FunctionInfo { end, .. }) in self.functions.iter() {
let Some(end) = end else { continue };
let section = &obj.sections[start.section];
ensure!(
section.contains_range(start.address..end.address),
"Function {:#010X}..{:#010X} out of bounds of section {} {:#010X}..{:#010X}",
start.address,
end,
section.name,
section.address,
section.address + section.size
);
let name = if obj.module_id == 0 {
format!("fn_{:08X}", start.address)
} else {
format!("fn_{}_{:X}", obj.module_id, start.address)
};
obj.add_symbol(
ObjSymbol {
name,
address: start.address as u64,
section: Some(start.section),
size: (end.address - start.address) as u64,
size_known: true,
kind: ObjSymbolKind::Function,
..Default::default()
},
false,
)?;
}
let mut iter = self.jump_tables.iter().peekable();
while let Some((&addr, &(mut size))) = iter.next() {
// Truncate overlapping jump tables
if let Some((&next_addr, _)) = iter.peek() {
if next_addr.section == addr.section {
size = min(size, next_addr.address - addr.address);
}
}
let section = &obj.sections[addr.section];
ensure!(
section.contains_range(addr.address..addr.address + size),
"Jump table {:#010X}..{:#010X} out of bounds of section {} {:#010X}..{:#010X}",
addr.address,
addr.address + size,
section.name,
section.address,
section.address + section.size
);
let address_str = if obj.module_id == 0 {
format!("{:08X}", addr.address)
} else {
format!(
"{}_{}_{:X}",
obj.module_id,
section.name.trim_start_matches('.'),
addr.address
)
};
obj.add_symbol(
ObjSymbol {
name: format!("jumptable_{}", address_str),
address: addr.address as u64,
section: Some(addr.section),
size: size as u64,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Local.into()),
kind: ObjSymbolKind::Object,
..Default::default()
},
false,
)?;
}
for (&_addr, symbols) in &self.known_symbols {
for symbol in symbols {
// Remove overlapping symbols
if symbol.size > 0 {
let end = symbol.address + symbol.size;
let overlapping = obj
.symbols
.for_section_range(
symbol.section.unwrap(),
symbol.address as u32 + 1..end as u32,
)
.filter(|(_, s)| s.kind == symbol.kind)
.map(|(a, _)| a)
.collect_vec();
for index in overlapping {
let existing = &obj.symbols[index];
let symbol = ObjSymbol {
name: format!("__DELETED_{}", existing.name),
kind: ObjSymbolKind::Unknown,
size: 0,
flags: ObjSymbolFlagSet(
ObjSymbolFlags::RelocationIgnore
| ObjSymbolFlags::NoWrite
| ObjSymbolFlags::NoExport
| ObjSymbolFlags::Stripped,
),
..existing.clone()
};
obj.symbols.replace(index, symbol)?;
}
}
obj.add_symbol(symbol.clone(), true)?;
}
}
Ok(())
}
pub fn detect_functions(&mut self, obj: &ObjInfo) -> Result<()> {
// Apply known functions from extab
for (&addr, &size) in &obj.known_functions {
self.functions.insert(addr, FunctionInfo {
analyzed: false,
end: size.map(|size| addr + size),
slices: None,
});
}
// Apply known functions from symbols
for (_, symbol) in obj.symbols.by_kind(ObjSymbolKind::Function) {
let Some(section_index) = symbol.section else { continue };
let addr_ref = SectionAddress::new(section_index, symbol.address as u32);
self.functions.insert(addr_ref, FunctionInfo {
analyzed: false,
end: if symbol.size_known { Some(addr_ref + symbol.size as u32) } else { None },
slices: None,
});
}
// Also check the beginning of every code section
for (section_index, section) in obj.sections.by_kind(ObjSectionKind::Code) {
self.functions
.entry(SectionAddress::new(section_index, section.address as u32))
.or_default();
}
// Process known functions first
for addr in self.functions.keys().cloned().collect_vec() {
self.process_function_at(obj, addr)?;
}
if let Some(entry) = obj.entry.map(|n| n as u32) {
// Locate entry function bounds
let (section_index, _) = obj
.sections
.at_address(entry)
.context(format!("Entry point {:#010X} outside of any section", entry))?;
self.process_function_at(obj, SectionAddress::new(section_index, entry))?;
}
// Locate bounds for referenced functions until none are left
self.process_functions(obj)?;
// Final pass(es)
while self.finalize_functions(obj, true)? {
self.process_functions(obj)?;
}
if self.functions.iter().any(|(_, i)| i.is_unfinalized()) {
log::error!("Failed to finalize functions:");
for (addr, info) in self.functions.iter().filter(|(_, i)| i.is_unfinalized()) {
log::error!(
" {:#010X}: blocks [{:?}]",
addr,
info.slices.as_ref().unwrap().possible_blocks.keys()
);
}
bail!("Failed to finalize functions");
}
Ok(())
}
fn finalize_functions(&mut self, obj: &ObjInfo, finalize: bool) -> Result<bool> {
let mut finalized_any = false;
let unfinalized = self
.functions
.iter()
.filter_map(|(&addr, info)| {
if info.is_unfinalized() {
info.slices.clone().map(|s| (addr, s))
} else {
None
}
})
.collect_vec();
for (addr, mut slices) in unfinalized {
// log::info!("Trying to finalize {:#010X}", addr);
let Some(function_start) = slices.start() else {
bail!("Function slice without start @ {:#010X}", addr);
};
let function_end = slices.end();
let mut current = SectionAddress::new(addr.section, 0);
while let Some((&block, vm)) = slices.possible_blocks.range(current..).next() {
current = block + 4;
let vm = vm.clone();
match slices.check_tail_call(
obj,
block,
function_start,
function_end,
&self.functions,
Some(vm.clone()),
) {
TailCallResult::Not => {
log::trace!("Finalized block @ {:#010X}", block);
slices.possible_blocks.remove(&block);
slices.analyze(
obj,
block,
function_start,
function_end,
&self.functions,
Some(vm),
)?;
// Start at the beginning of the function again
current = SectionAddress::new(addr.section, 0);
}
TailCallResult::Is => {
log::trace!("Finalized tail call @ {:#010X}", block);
slices.possible_blocks.remove(&block);
slices.function_references.insert(block);
// Start at the beginning of the function again
current = SectionAddress::new(addr.section, 0);
}
TailCallResult::Possible => {
if finalize {
log::trace!(
"Still couldn't determine {:#010X}, assuming non-tail-call",
block
);
slices.possible_blocks.remove(&block);
slices.analyze(
obj,
block,
function_start,
function_end,
&self.functions,
Some(vm),
)?;
}
}
TailCallResult::Error(e) => return Err(e),
}
}
if slices.can_finalize() {
log::trace!("Finalizing {:#010X}", addr);
slices.finalize(obj, &self.functions)?;
for address in slices.function_references.iter().cloned() {
// Only create functions for code sections
// Some games use branches to data sections to prevent dead stripping (Mario Party)
if matches!(obj.sections.get(address.section), Some(section) if section.kind == ObjSectionKind::Code)
{
self.functions.entry(address).or_default();
}
}
self.jump_tables.append(&mut slices.jump_table_references.clone());
let end = slices.end();
let info = self.functions.get_mut(&addr).unwrap();
info.analyzed = true;
info.end = end;
info.slices = Some(slices.clone());
finalized_any = true;
}
}
Ok(finalized_any)
}
fn first_unbounded_function(&self) -> Option<SectionAddress> {
self.functions.iter().find(|(_, info)| !info.is_analyzed()).map(|(&addr, _)| addr)
}
fn process_functions(&mut self, obj: &ObjInfo) -> Result<()> {
loop {
match self.first_unbounded_function() {
Some(addr) => {
log::trace!("Processing {:#010X}", addr);
self.process_function_at(obj, addr)?;
}
None => {
if !self.finalize_functions(obj, false)? && !self.detect_new_functions(obj)? {
break;
}
}
}
}
Ok(())
}
pub fn process_function_at(&mut self, obj: &ObjInfo, addr: SectionAddress) -> Result<bool> {
Ok(if let Some(mut slices) = self.process_function(obj, addr)? {
for address in slices.function_references.iter().cloned() {
// Only create functions for code sections
// Some games use branches to data sections to prevent dead stripping (Mario Party)
if matches!(obj.sections.get(address.section), Some(section) if section.kind == ObjSectionKind::Code)
{
self.functions.entry(address).or_default();
}
}
self.jump_tables.append(&mut slices.jump_table_references.clone());
if slices.can_finalize() {
slices.finalize(obj, &self.functions)?;
let info = self.functions.entry(addr).or_default();
info.analyzed = true;
info.end = slices.end();
info.slices = Some(slices);
} else {
let info = self.functions.entry(addr).or_default();
info.analyzed = true;
info.end = None;
info.slices = Some(slices);
}
true
} else {
log::debug!("Not a function @ {:#010X}", addr);
let info = self.functions.entry(addr).or_default();
info.analyzed = true;
info.end = None;
false
})
}
fn process_function(
&mut self,
obj: &ObjInfo,
start: SectionAddress,
) -> Result<Option<FunctionSlices>> {
let mut slices = FunctionSlices::default();
let function_end = self.functions.get(&start).and_then(|info| info.end);
Ok(match slices.analyze(obj, start, start, function_end, &self.functions, None)? {
true => Some(slices),
false => None,
})
}
fn detect_new_functions(&mut self, obj: &ObjInfo) -> Result<bool> {
let mut new_functions = vec![];
for (section_index, section) in obj.sections.by_kind(ObjSectionKind::Code) {
let section_start = SectionAddress::new(section_index, section.address as u32);
let section_end = section_start + section.size as u32;
let mut iter = self.functions.range(section_start..section_end).peekable();
loop {
match (iter.next(), iter.peek()) {
(Some((&first, first_info)), Some(&(&second, second_info))) => {
let Some(first_end) = first_info.end else { continue };
if first_end > second {
bail!("Overlapping functions {}-{} -> {}", first, first_end, second);
}
let addr = match skip_alignment(section, first_end, second) {
Some(addr) => addr,
None => continue,
};
if second > addr {
log::trace!(
"Trying function @ {:#010X} (from {:#010X}-{:#010X} <-> {:#010X}-{:#010X?})",
addr,
first.address,
first_end,
second.address,
second_info.end,
);
new_functions.push(addr);
}
}
(Some((last, last_info)), None) => {
let Some(last_end) = last_info.end else { continue };
if last_end < section_end {
let addr = match skip_alignment(section, last_end, section_end) {
Some(addr) => addr,
None => continue,
};
if addr < section_end {
log::trace!(
"Trying function @ {:#010X} (from {:#010X}-{:#010X} <-> {:#010X})",
addr,
last.address,
last_end,
section_end,
);
new_functions.push(addr);
}
}
}
_ => break,
}
}
}
let found_new = !new_functions.is_empty();
for addr in new_functions {
let opt = self.functions.insert(addr, FunctionInfo::default());
ensure!(opt.is_none(), "Attempted to detect duplicate function @ {:#010X}", addr);
}
Ok(found_new)
}
}
/// Execute VM from entry point following branches and function calls
/// until SDA bases are initialized (__init_registers)
pub fn locate_sda_bases(obj: &mut ObjInfo) -> Result<bool> {
let Some(entry) = obj.entry else {
return Ok(false);
};
let (section_index, _) = obj
.sections
.at_address(entry as u32)
.context(format!("Entry point {:#010X} outside of any section", entry))?;
let entry_addr = SectionAddress::new(section_index, entry as u32);
let mut executor = Executor::new(obj);
executor.push(entry_addr, VM::new(), false);
let result = executor.run(
obj,
|ExecCbData { executor, vm, result, ins_addr, section: _, ins: _, block_start: _ }| {
match result {
StepResult::Continue | StepResult::LoadStore { .. } => {
return Ok(ExecCbResult::Continue);
}
StepResult::Illegal => bail!("Illegal instruction @ {}", ins_addr),
StepResult::Jump(target) => {
if let BranchTarget::Address(RelocationTarget::Address(addr)) = target {
return Ok(ExecCbResult::Jump(addr));
}
}
StepResult::Branch(branches) => {
for branch in branches {
if let BranchTarget::Address(RelocationTarget::Address(addr)) =
branch.target
{
executor.push(addr, branch.vm, false);
}
}
}
}
if let (GprValue::Constant(sda2_base), GprValue::Constant(sda_base)) =
(vm.gpr_value(2), vm.gpr_value(13))
{
return Ok(ExecCbResult::End((sda2_base, sda_base)));
}
Ok(ExecCbResult::EndBlock)
},
)?;
match result {
Some((sda2_base, sda_base)) => {
obj.sda2_base = Some(sda2_base);
obj.sda_base = Some(sda_base);
Ok(true)
}
None => Ok(false),
}
}
/// ProDG hardcodes .bss and .sbss section initialization in `entry`
/// This function locates the memset calls and returns a list of
/// (address, size) pairs for the .bss sections.
pub fn locate_bss_memsets(obj: &mut ObjInfo) -> Result<Vec<(u32, u32)>> {
let mut bss_sections: Vec<(u32, u32)> = Vec::new();
let Some(entry) = obj.entry else {
return Ok(bss_sections);
};
let (section_index, _) = obj
.sections
.at_address(entry as u32)
.context(format!("Entry point {:#010X} outside of any section", entry))?;
let entry_addr = SectionAddress::new(section_index, entry as u32);
let mut executor = Executor::new(obj);
executor.push(entry_addr, VM::new(), false);
executor.run(
obj,
|ExecCbData { executor: _, vm, result, ins_addr, section: _, ins: _, block_start: _ }| {
match result {
StepResult::Continue | StepResult::LoadStore { .. } => Ok(ExecCbResult::Continue),
StepResult::Illegal => bail!("Illegal instruction @ {}", ins_addr),
StepResult::Jump(_target) => Ok(ExecCbResult::End(())),
StepResult::Branch(branches) => {
for branch in branches {
if branch.link {
// Some ProDG crt0.s versions use the wrong registers, some don't
if let (
GprValue::Constant(addr),
GprValue::Constant(value),
GprValue::Constant(size),
) = {
if vm.gpr_value(4) == GprValue::Constant(0) {
(vm.gpr_value(3), vm.gpr_value(4), vm.gpr_value(5))
} else {
(vm.gpr_value(4), vm.gpr_value(5), vm.gpr_value(6))
}
} {
if value == 0 && size > 0 {
bss_sections.push((addr, size));
}
}
}
}
if bss_sections.len() >= 2 {
return Ok(ExecCbResult::End(()));
}
Ok(ExecCbResult::Continue)
}
}
},
)?;
Ok(bss_sections)
}