A lot more section-address-aware refactoring

This commit is contained in:
Luke Street 2023-08-23 23:13:12 -04:00
parent 5843ee021e
commit 3f63f1ef47
29 changed files with 10110 additions and 1206 deletions

7660
assets/signatures/GXInit.yml Normal file

File diff suppressed because one or more lines are too long

View File

@ -67,3 +67,107 @@
kind: PpcEmbSda21 kind: PpcEmbSda21
symbol: 1 symbol: 1
addend: 0 addend: 0
- symbol: 0
hash: d2c66bcc9d139a983d3cfc0df693f53fcab27fbb
signature: lCH/8P////98CAKm/////5ABABT/////k+EADP////8/4AAA//8AAEgAACD/////gAMAAP////84gP///////5AfAAD//wAAgYMABP////+AYwAI/////32JA6b/////ToAEIf////+AfwAA//8AACwDAAD/////QIL/3P////+AAQAU/////4PhAAz/////fAgDpv////84IQAQ/////06AACD/////
symbols:
- kind: Function
name: __destroy_global_chain
size: 84
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 16
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 32
kind: PpcAddr16Lo
symbol: 1
addend: 0
- offset: 52
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 45123f2a3982045741d678a1afe8c39a684cff33
signature: fAgCpv////+QAQAE/////5Qh//D/////k+EADP////9IAAAk/////4AfAAD/////PGAAAP//AACQAwAA//8AAIB/AAj/////OID///////+BnwAE/////32IA6b/////ToAAIf////88YAAA//8AAIPjAAD//wAAKB8AAP////9Agv/U/////4ABABT/////g+EADP////84IQAQ/////3wIA6b/////ToAAIP////8=
symbols:
- kind: Function
name: __destroy_global_chain
size: 88
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 24
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 28
kind: PpcAddr16Lo
symbol: 1
addend: 0
- offset: 52
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 56
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 0fe3dc97bc3afa0c4b054038d390468b8be463e5
signature: lCH/8P////98CAKm/////zxgAAD//wAAkAEAFP////+T4QAM/////zvjAAD//wAASAAAIP////+AAwAA/////ziA////////kB8AAP////+BgwAE/////4BjAAj/////fYkDpv////9OgAQh/////4B/AAD/////KAMAAP////9Agv/c/////4ABABT/////g+EADP////98CAOm/////zghABD/////ToAAIP////8=
symbols:
- kind: Function
name: __destroy_global_chain
size: 88
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 8
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 7cd641bb9ae91f2e8e5feb8c1ae493e067b264eb
signature: fAgCpv////88YAAA//8AAJABAAT/////lCH/8P////+T4QAM/////zvjAAD//wAASAAAIP////+AAwAA/////ziA////////kB8AAP////+BgwAE/////4BjAAj/////fYgDpv////9OgAAh/////4B/AAD/////KAMAAP////9Agv/c/////4ABABT/////g+EADP////84IQAQ/////3wIA6b/////ToAAIP////8=
symbols:
- kind: Function
name: __destroy_global_chain
size: 88
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 4
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Lo
symbol: 1
addend: 0

View File

@ -0,0 +1,226 @@
- symbol: 0
hash: 8b35a5a00539c06c208413bcce713c2b9330c60d
signature: gIAAAP/gAAAsBABA/////0CCAAz/////OGD///////9OgAAg/////xwEAAz/////OMQAAf////88oAAA//8AAICAAAD/4AAAkMAAAP/gAAA4pQAA//8AAHylAhT/////OAAAAP////+QhQAA/////5BlAAT/////OGAAAP////+QBQAI/////5CgAAD/4AAAToAAIP////8=
symbols:
- kind: Function
name: __register_atexit
size: 76
flags: 1
section: .text
- kind: Object
name: atexit_curr_func
size: 4
flags: 2
section: .sbss
- kind: Object
name: atexit_funcs
size: 768
flags: 2
section: .bss
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .sbss
relocations:
- offset: 0
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 28
kind: PpcAddr16Ha
symbol: 2
addend: 0
- offset: 32
kind: PpcEmbSda21
symbol: 3
addend: 0
- offset: 36
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 40
kind: PpcAddr16Lo
symbol: 2
addend: 0
- offset: 68
kind: PpcEmbSda21
symbol: 3
addend: 0
- symbol: 0
hash: 40b262a7d7f7edaec009a682c754434370b62d34
signature: gIAAAP/gAAAsBABA/////0CCAAz/////OGD///////9OgAAg/////xwEAAz/////OMQAAf////88oAAA//8AAICAAAD/4AAAkMAAAP/gAAA4pQAA//8AAHylAhT/////kKAAAP/gAAA4AAAA/////5CFAAD/////kGUABP////84YAAA/////5AFAAj/////ToAAIP////8=
symbols:
- kind: Function
name: __register_atexit
size: 76
flags: 1
section: .text
- kind: Object
name: atexit_curr_func
size: 4
flags: 2
section: .sbss
- kind: Object
name: atexit_funcs
size: 768
flags: 2
section: .bss
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .sbss
relocations:
- offset: 0
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 28
kind: PpcAddr16Ha
symbol: 2
addend: 0
- offset: 32
kind: PpcEmbSda21
symbol: 3
addend: 0
- offset: 36
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 40
kind: PpcAddr16Lo
symbol: 2
addend: 0
- offset: 48
kind: PpcEmbSda21
symbol: 3
addend: 0
- symbol: 0
hash: 6e87198ea237fb408c23c1def50061fd36a740d3
signature: gIAAAP/gAAAsBABA/////0CCAAz/////OGD///////9OgAAg/////xzEAAz/////PKAAAP//AAA45AAB/////4CAAAD/4AAAOAUAAP//AACQ4AAA/+AAAHygMhT/////OAAAAP////+QhQAA/////5BlAAT/////OGAAAP////+QBQAI/////5CgAAD/4AAAToAAIP////8=
symbols:
- kind: Function
name: __register_atexit
size: 76
flags: 1
section: .text
- kind: Object
name: atexit_curr_func
size: 4
flags: 2
section: .sbss
- kind: Object
name: atexit_funcs
size: 768
flags: 2
section: .bss
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .sbss
relocations:
- offset: 0
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 24
kind: PpcAddr16Ha
symbol: 2
addend: 0
- offset: 32
kind: PpcEmbSda21
symbol: 3
addend: 0
- offset: 36
kind: PpcAddr16Lo
symbol: 2
addend: 0
- offset: 40
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 68
kind: PpcEmbSda21
symbol: 3
addend: 0
- symbol: 0
hash: 7a0239ddbd48230f3a94cd0104a10dea24d873dc
signature: PQAAAP////85CAAA/////4CIAAT/////LAQAQP////9AggAM/////zhg////////ToAAIP////8cxAAM/////zjkAAH/////gIgAAP////84qAAI/////zgAAAD/////kOgABP////98hTFu/////5BlAAT/////OGAAAP////+QBQAI/////5CoAAD/////ToAAIP////8=
symbols:
- kind: Function
name: __register_atexit
size: 76
flags: 1
section: .text
relocations: []
- symbol: 0
hash: 6287fc6366ad226fe4e085011501cd4182ff2b28
signature: PQAAAP//AAA5CAAA//8AAICIAAT/////LAQAQP////9AggAM/////zhg////////ToAAIP////8cxAAM/////zjkAAH/////gIgAAP////84qAAI/////zgAAAD/////kOgABP////98hTFu/////5BlAAT/////OGAAAP////+QBQAI/////5CoAAD/////ToAAIP////8=
symbols:
- kind: Function
name: __register_atexit
size: 76
flags: 1
section: .text
- kind: Unknown
name: '...bss.0'
size: 0
flags: 2
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: f6bc8b4030feb057bcc6d2b0ea92f3494364efe4
signature: POAAAP//AAA45wAA//8AAICHAAT/////LAQAQP////9AggAM/////zhg////////ToAAIP////8cxAAM/////zgEAAH/////OKcACP////+QBwAE/////4CHAAD/////OAAAAP////98pTIU/////5CnAAD/////kIUAAP////+QZQAE/////zhgAAD/////kAUACP////9OgAAg/////w==
symbols:
- kind: Function
name: __register_atexit
size: 80
flags: 1
section: .text
- kind: Unknown
name: '...bss.0'
size: 0
flags: 2
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 7a67aff7221540295d797f06244017c42e7f47f8
signature: PIAAAP//AAA45AAA//8AAICHAAT/////LAQAQP////9AggAM/////zhg////////ToAAIP////8cpAAM/////zkHAAj/////OMQAAf////+AhwAA/////zgAAAD/////kMcABP////99CCoU/////5CIAAD/////kGgABP////84YAAA/////5AIAAj/////kQcAAP////9OgAAg/////w==
symbols:
- kind: Function
name: __register_atexit
size: 80
flags: 1
section: .text
- kind: Unknown
name: '...bss.0'
size: 0
flags: 2
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0

View File

@ -0,0 +1,114 @@
- symbol: 0
hash: 9ae42f2a8ca1c619848ba7c2ec54f348044470e4
signature: gAAAAP/gAACQBQAA/////5CFAAT/////kGUACP////+QoAAA/+AAAE6AACD/////
symbols:
- kind: Function
name: __register_global_object
size: 24
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .sbss
relocations:
- offset: 0
kind: PpcEmbSda21
symbol: 1
addend: 0
- offset: 16
kind: PpcEmbSda21
symbol: 1
addend: 0
- symbol: 0
hash: 22cb1f474182a4b5e07e63e09b574fae0bf8e672
signature: PMAAAP//AACABgAA//8AAJAFAAD/////kIUABP////+QZQAI/////5CmAAD//wAAToAAIP////8=
symbols:
- kind: Function
name: __register_global_object
size: 28
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 6f0f3d0bb4895d84ee43877da689301e8fc9f844
signature: PMAAAP////+ABgAA/////5AFAAD/////kIUABP////+QZQAI/////5CmAAD/////ToAAIP////8=
symbols:
- kind: Function
name: __register_global_object
size: 28
flags: 1
section: .text
relocations: []
- symbol: 0
hash: a6c9fe066ab0621a02049267cb648a2aab4f722e
signature: PMAAAP//AACEBgAA//8AAJAFAAD/////kIUABP////+QZQAI/////5CmAAD/////ToAAIP////8=
symbols:
- kind: Function
name: __register_global_object
size: 28
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: c174aed044d0767ee4cf6a7270fbd9c46bb45938
signature: PMAAAP//AACABgAA//8AAJAFAAD/////kIUABP////+QZQAI/////zzAAAD//wAAkKYAAP//AABOgAAg/////w==
symbols:
- kind: Function
name: __register_global_object
size: 32
flags: 1
section: .text
- kind: Object
name: __global_destructor_chain
size: 4
flags: 1
section: .bss
relocations:
- offset: 0
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 4
kind: PpcAddr16Lo
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 24
kind: PpcAddr16Lo
symbol: 1
addend: 0

View File

@ -0,0 +1,115 @@
- symbol: 0
hash: 7f011cf08af89b18fb76bcb1f7d104da62a785d6
signature: LAMAAP////9MggAg/////xyDAAz/////PGAAAP//AAA4AAAA/////zhjAAD//wAAfAMhbv////+QAwAE/////5ADAAj/////ToAAIP////8=
symbols:
- kind: Function
name: __unregister_fragment
size: 40
flags: 1
section: .text
- kind: Object
name: fragmentinfo
size: 12
flags: 2
section: .bss
relocations:
- offset: 12
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 63cfddab648b8040ba0fc548d9c6058245107cf5
signature: KAMAH/////9NgQAg/////xyDAAz/////PGAAAP//AAA4AAAA/////zhjAAD//wAAfAMhbv////+QAwAE/////5ADAAj/////ToAAIP////8=
symbols:
- kind: Function
name: __unregister_fragment
size: 40
flags: 1
section: .text
- kind: Object
name: fragmentinfo
size: 384
flags: 2
section: .bss
relocations:
- offset: 12
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 20
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 1c7fef6e356049ca092911ef494021318eeb8013
signature: LAMAAP////9NgAAg/////ywDAAH/////TIAAIP////8cgwAM/////zxgAAD//wAAOAAAAP////84YwAA//8AAHwDIW7/////kAMABP////+QAwAI/////06AACD/////
symbols:
- kind: Function
name: __unregister_fragment
size: 48
flags: 1
section: .text
- kind: Object
name: fragmentinfo
size: 12
flags: 2
section: .bss
relocations:
- offset: 20
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 28
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 0dba621b6fe389afcf2ab53bdb76db9d9dc8d54e
signature: LAMAAP////9NgAAg/////ywDAAH/////TIAAIP////8cgwAM/////zxgAAD//wAAOAAAAP////84YwAA//8AAHxjIhT/////kAMAAP////+QAwAE/////5ADAAj/////ToAAIP////8=
symbols:
- kind: Function
name: __unregister_fragment
size: 52
flags: 1
section: .text
- kind: Object
name: fragmentinfo
size: 12
flags: 2
section: .bss
relocations:
- offset: 20
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 28
kind: PpcAddr16Lo
symbol: 1
addend: 0
- symbol: 0
hash: 8a6d162b7c2bade6367615664b53c6fa684930bd
signature: LAMAAP////9BgAAs/////ywDAAH/////QIAAJP////8cgwAM/////zxgAAD//wAAOAMAAP//AAB8YCIU/////zgAAAD/////kAMAAP////+QAwAE/////5ADAAj/////ToAAIP////8=
symbols:
- kind: Function
name: __unregister_fragment
size: 52
flags: 1
section: .text
- kind: Object
name: fragmentinfo
size: 12
flags: 2
section: .bss
relocations:
- offset: 20
kind: PpcAddr16Ha
symbol: 1
addend: 0
- offset: 24
kind: PpcAddr16Lo
symbol: 1
addend: 0

View File

@ -1,6 +1,10 @@
use std::collections::{BTreeMap, BTreeSet}; use std::{
collections::{BTreeMap, BTreeSet},
fmt::{Debug, Display, Formatter, UpperHex},
ops::{Add, AddAssign, BitAnd, Sub},
};
use anyhow::{bail, Context, Result}; use anyhow::{bail, ensure, Context, Result};
use crate::{ use crate::{
analysis::{ analysis::{
@ -12,34 +16,101 @@ use crate::{
obj::{ObjInfo, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind}, obj::{ObjInfo, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind},
}; };
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SectionAddress {
pub section: usize,
pub address: u32,
}
impl Debug for SectionAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{:#010X}", self.section as isize, self.address)
}
}
impl Display for SectionAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{:#010X}", self.section as isize, self.address)
}
}
impl SectionAddress {
pub fn new(section: usize, address: u32) -> Self { Self { section, address } }
}
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(Debug, Default)] #[derive(Debug, Default)]
pub struct AnalyzerState { pub struct AnalyzerState {
pub sda_bases: Option<(u32, u32)>, pub sda_bases: Option<(u32, u32)>,
pub function_entries: BTreeSet<u32>, pub function_entries: BTreeSet<SectionAddress>,
pub function_bounds: BTreeMap<u32, u32>, pub function_bounds: BTreeMap<SectionAddress, Option<SectionAddress>>,
pub function_slices: BTreeMap<u32, FunctionSlices>, pub function_slices: BTreeMap<SectionAddress, FunctionSlices>,
pub jump_tables: BTreeMap<u32, u32>, pub jump_tables: BTreeMap<SectionAddress, u32>,
pub known_symbols: BTreeMap<u32, ObjSymbol>, pub known_symbols: BTreeMap<SectionAddress, ObjSymbol>,
pub non_finalized_functions: BTreeMap<u32, FunctionSlices>, pub known_sections: BTreeMap<usize, String>,
pub non_finalized_functions: BTreeMap<SectionAddress, FunctionSlices>,
} }
impl AnalyzerState { impl AnalyzerState {
pub fn apply(&self, obj: &mut ObjInfo) -> Result<()> { pub fn apply(&self, obj: &mut ObjInfo) -> Result<()> {
for (&section_index, section_name) in &self.known_sections {
obj.sections[section_index].rename(section_name.clone())?;
}
for (&start, &end) in &self.function_bounds { for (&start, &end) in &self.function_bounds {
if end == 0 { let Some(end) = end else { continue };
continue; let section = &obj.sections[start.section];
} ensure!(
let (section_index, _) = obj section.contains_range(start.address..end.address),
.sections "Function {:#010X}..{:#010X} out of bounds of section {} {:#010X}..{:#010X}",
.with_range(start..end) start.address,
.context("Failed to locate section for function")?; 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( obj.add_symbol(
ObjSymbol { ObjSymbol {
name: format!("fn_{:08X}", start), name,
demangled_name: None, demangled_name: None,
address: start as u64, address: start.address as u64,
section: Some(section_index), section: Some(start.section),
size: (end - start) as u64, size: (end.address - start.address) as u64,
size_known: true, size_known: true,
flags: Default::default(), flags: Default::default(),
kind: ObjSymbolKind::Function, kind: ObjSymbolKind::Function,
@ -50,16 +121,22 @@ impl AnalyzerState {
)?; )?;
} }
for (&addr, &size) in &self.jump_tables { for (&addr, &size) in &self.jump_tables {
let (section_index, _) = obj let section = &obj.sections[addr.section];
.sections ensure!(
.with_range(addr..addr + size) section.contains_range(addr.address..addr.address + size),
.context("Failed to locate section for jump table")?; "Jump table {:#010X}..{:#010X} out of bounds of section {} {:#010X}..{:#010X}",
addr.address,
addr.address + size,
section.name,
section.address,
section.address + section.size
);
obj.add_symbol( obj.add_symbol(
ObjSymbol { ObjSymbol {
name: format!("jumptable_{:08X}", addr), name: format!("jumptable_{:08X}", addr.address),
demangled_name: None, demangled_name: None,
address: addr as u64, address: addr.address as u64,
section: Some(section_index), section: Some(addr.section),
size: size as u64, size: size as u64,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Local.into()), flags: ObjSymbolFlagSet(ObjSymbolFlags::Local.into()),
@ -79,20 +156,27 @@ impl AnalyzerState {
pub fn detect_functions(&mut self, obj: &ObjInfo) -> Result<()> { pub fn detect_functions(&mut self, obj: &ObjInfo) -> Result<()> {
// Apply known functions from extab // Apply known functions from extab
for (&addr, &size) in &obj.known_functions { for (&addr, &size) in &obj.known_functions {
self.function_entries.insert(addr); let (section_index, _) = obj
self.function_bounds.insert(addr, addr + size); .sections
.at_address(addr)
.context(format!("Function {:#010X} outside of any section", addr))?;
let addr_ref = SectionAddress::new(section_index, addr);
self.function_entries.insert(addr_ref);
self.function_bounds.insert(addr_ref, Some(addr_ref + size));
} }
// Apply known functions from symbols // Apply known functions from symbols
for (_, symbol) in obj.symbols.by_kind(ObjSymbolKind::Function) { for (_, symbol) in obj.symbols.by_kind(ObjSymbolKind::Function) {
self.function_entries.insert(symbol.address as u32); let Some(section_index) = symbol.section else { continue };
let addr_ref = SectionAddress::new(section_index, symbol.address as u32);
self.function_entries.insert(addr_ref);
if symbol.size_known { if symbol.size_known {
self.function_bounds self.function_bounds.insert(addr_ref, Some(addr_ref + symbol.size as u32));
.insert(symbol.address as u32, (symbol.address + symbol.size) as u32);
} }
} }
// Also check the beginning of every code section // Also check the beginning of every code section
for (_, section) in obj.sections.by_kind(ObjSectionKind::Code) { for (section_index, section) in obj.sections.by_kind(ObjSectionKind::Code) {
self.function_entries.insert(section.address as u32); self.function_entries
.insert(SectionAddress::new(section_index, section.address as u32));
} }
// Process known functions first // Process known functions first
@ -100,8 +184,14 @@ impl AnalyzerState {
for addr in known_functions { for addr in known_functions {
self.process_function_at(obj, addr)?; self.process_function_at(obj, addr)?;
} }
// Locate entry function bounds if let Some(entry) = obj.entry.map(|n| n as u32) {
self.process_function_at(obj, obj.entry 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 // Locate bounds for referenced functions until none are left
self.process_functions(obj)?; self.process_functions(obj)?;
// Final pass(es) // Final pass(es)
@ -115,9 +205,11 @@ impl AnalyzerState {
let mut finalized = Vec::new(); let mut finalized = Vec::new();
for (&addr, slices) in &mut self.non_finalized_functions { for (&addr, slices) in &mut self.non_finalized_functions {
// log::info!("Trying to finalize {:#010X}", addr); // log::info!("Trying to finalize {:#010X}", addr);
let function_start = slices.start(); let Some(function_start) = slices.start() else {
bail!("Function slice without start @ {:#010X}", addr);
};
let function_end = slices.end(); let function_end = slices.end();
let mut current = 0; let mut current = SectionAddress::new(addr.section, 0);
while let Some(&block) = slices.possible_blocks.range(current + 4..).next() { while let Some(&block) = slices.possible_blocks.range(current + 4..).next() {
current = block; current = block;
match slices.check_tail_call( match slices.check_tail_call(
@ -134,7 +226,7 @@ impl AnalyzerState {
obj, obj,
block, block,
function_start, function_start,
Some(function_end), function_end,
&self.function_entries, &self.function_entries,
)?; )?;
} }
@ -154,7 +246,7 @@ impl AnalyzerState {
obj, obj,
block, block,
function_start, function_start,
Some(function_end), function_end,
&self.function_entries, &self.function_entries,
)?; )?;
} }
@ -180,7 +272,7 @@ impl AnalyzerState {
Ok(finalized_new) Ok(finalized_new)
} }
fn first_unbounded_function(&self) -> Option<u32> { fn first_unbounded_function(&self) -> Option<SectionAddress> {
let mut entries_iter = self.function_entries.iter().cloned(); let mut entries_iter = self.function_entries.iter().cloned();
let mut bounds_iter = self.function_bounds.keys().cloned(); let mut bounds_iter = self.function_bounds.keys().cloned();
let mut entry = entries_iter.next(); let mut entry = entries_iter.next();
@ -232,12 +324,12 @@ impl AnalyzerState {
Ok(()) Ok(())
} }
pub fn process_function_at(&mut self, obj: &ObjInfo, addr: u32) -> Result<bool> { pub fn process_function_at(&mut self, obj: &ObjInfo, addr: SectionAddress) -> Result<bool> {
if addr == 0 || addr == 0xFFFFFFFF { // if addr == 0 || addr == 0xFFFFFFFF {
log::warn!("Tried to detect @ {:#010X}", addr); // log::warn!("Tried to detect @ {:#010X}", addr);
self.function_bounds.insert(addr, 0); // self.function_bounds.insert(addr, 0);
return Ok(false); // return Ok(false);
} // }
Ok(if let Some(mut slices) = self.process_function(obj, addr)? { Ok(if let Some(mut slices) = self.process_function(obj, addr)? {
self.function_entries.insert(addr); self.function_entries.insert(addr);
self.function_entries.append(&mut slices.function_references.clone()); self.function_entries.append(&mut slices.function_references.clone());
@ -252,14 +344,18 @@ impl AnalyzerState {
true true
} else { } else {
log::debug!("Not a function @ {:#010X}", addr); log::debug!("Not a function @ {:#010X}", addr);
self.function_bounds.insert(addr, 0); self.function_bounds.insert(addr, None);
false false
}) })
} }
fn process_function(&mut self, obj: &ObjInfo, start: u32) -> Result<Option<FunctionSlices>> { fn process_function(
&mut self,
obj: &ObjInfo,
start: SectionAddress,
) -> Result<Option<FunctionSlices>> {
let mut slices = FunctionSlices::default(); let mut slices = FunctionSlices::default();
let function_end = self.function_bounds.get(&start).cloned(); let function_end = self.function_bounds.get(&start).cloned().flatten();
Ok(match slices.analyze(obj, start, start, function_end, &self.function_entries)? { Ok(match slices.analyze(obj, start, start, function_end, &self.function_entries)? {
true => Some(slices), true => Some(slices),
false => None, false => None,
@ -268,14 +364,15 @@ impl AnalyzerState {
fn detect_new_functions(&mut self, obj: &ObjInfo) -> Result<bool> { fn detect_new_functions(&mut self, obj: &ObjInfo) -> Result<bool> {
let mut found_new = false; let mut found_new = false;
for (_, section) in obj.sections.by_kind(ObjSectionKind::Code) { for (section_index, section) in obj.sections.by_kind(ObjSectionKind::Code) {
let section_start = section.address as u32; let section_start = SectionAddress::new(section_index, section.address as u32);
let section_end = (section.address + section.size) as u32; let section_end = section_start + section.size as u32;
let mut iter = self.function_bounds.range(section_start..section_end).peekable(); let mut iter = self.function_bounds.range(section_start..section_end).peekable();
loop { loop {
match (iter.next(), iter.peek()) { match (iter.next(), iter.peek()) {
(Some((&first_begin, &first_end)), Some(&(&second_begin, &second_end))) => { (Some((&first_begin, &first_end)), Some(&(&second_begin, &second_end))) => {
if first_end == 0 || first_end > second_begin { let Some(first_end) = first_end else { continue };
if first_end > second_begin {
continue; continue;
} }
let addr = match skip_alignment(section, first_end, second_begin) { let addr = match skip_alignment(section, first_end, second_begin) {
@ -284,7 +381,7 @@ impl AnalyzerState {
}; };
if second_begin > addr && self.function_entries.insert(addr) { if second_begin > addr && self.function_entries.insert(addr) {
log::trace!( log::trace!(
"Trying function @ {:#010X} (from {:#010X}-{:#010X} <-> {:#010X}-{:#010X})", "Trying function @ {:#010X} (from {:#010X}-{:#010X} <-> {:#010X}-{:#010X?})",
addr, addr,
first_begin, first_begin,
first_end, first_end,
@ -295,7 +392,8 @@ impl AnalyzerState {
} }
} }
(Some((&last_begin, &last_end)), None) => { (Some((&last_begin, &last_end)), None) => {
if last_end > 0 && last_end < section_end { let Some(last_end) = last_end else { continue };
if last_end < section_end {
let addr = match skip_alignment(section, last_end, section_end) { let addr = match skip_alignment(section, last_end, section_end) {
Some(addr) => addr, Some(addr) => addr,
None => continue, None => continue,
@ -323,11 +421,20 @@ impl AnalyzerState {
/// Execute VM from entry point following branches and function calls /// Execute VM from entry point following branches and function calls
/// until SDA bases are initialized (__init_registers) /// until SDA bases are initialized (__init_registers)
pub fn locate_sda_bases(obj: &mut ObjInfo) -> Result<bool> { 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); let mut executor = Executor::new(obj);
executor.push(obj.entry as u32, VM::new(), false); executor.push(entry_addr, VM::new(), false);
let result = executor.run( let result = executor.run(
obj, obj,
|ExecCbData { executor, vm, result, section_index: _, section: _, ins, block_start: _ }| { |ExecCbData { executor, vm, result, ins_addr: _, section: _, ins, block_start: _ }| {
match result { match result {
StepResult::Continue | StepResult::LoadStore { .. } => { StepResult::Continue | StepResult::LoadStore { .. } => {
return Ok(ExecCbResult::Continue); return Ok(ExecCbResult::Continue);

View File

@ -1,9 +1,10 @@
use anyhow::Result; use anyhow::{ensure, Result};
use fixedbitset::FixedBitSet; use fixedbitset::FixedBitSet;
use ppc750cl::Ins; use ppc750cl::Ins;
use crate::{ use crate::{
analysis::{ analysis::{
cfa::SectionAddress,
disassemble, disassemble,
vm::{StepResult, VM}, vm::{StepResult, VM},
}, },
@ -30,12 +31,12 @@ impl VisitedAddresses {
Self { inner } Self { inner }
} }
pub fn contains(&self, section_index: usize, section_address: u32, address: u32) -> bool { pub fn contains(&self, section_address: u32, address: SectionAddress) -> bool {
self.inner[section_index].contains(Self::bit_for(section_address, address)) self.inner[address.section].contains(Self::bit_for(section_address, address.address))
} }
pub fn insert(&mut self, section_index: usize, section_address: u32, address: u32) { pub fn insert(&mut self, section_address: u32, address: SectionAddress) {
self.inner[section_index].insert(Self::bit_for(section_address, address)); self.inner[address.section].insert(Self::bit_for(section_address, address.address));
} }
#[inline] #[inline]
@ -46,7 +47,7 @@ impl VisitedAddresses {
pub struct VMState { pub struct VMState {
pub vm: Box<VM>, pub vm: Box<VM>,
pub address: u32, pub address: SectionAddress,
} }
/// Helper for branched VM execution, only visiting addresses once. /// Helper for branched VM execution, only visiting addresses once.
@ -59,15 +60,15 @@ pub struct ExecCbData<'a> {
pub executor: &'a mut Executor, pub executor: &'a mut Executor,
pub vm: &'a mut VM, pub vm: &'a mut VM,
pub result: StepResult, pub result: StepResult,
pub section_index: usize, pub ins_addr: SectionAddress,
pub section: &'a ObjSection, pub section: &'a ObjSection,
pub ins: &'a Ins, pub ins: &'a Ins,
pub block_start: u32, pub block_start: SectionAddress,
} }
pub enum ExecCbResult<T = ()> { pub enum ExecCbResult<T = ()> {
Continue, Continue,
Jump(u32), Jump(SectionAddress),
EndBlock, EndBlock,
End(T), End(T),
} }
@ -80,14 +81,15 @@ impl Executor {
pub fn run<Cb, R>(&mut self, obj: &ObjInfo, mut cb: Cb) -> Result<Option<R>> pub fn run<Cb, R>(&mut self, obj: &ObjInfo, mut cb: Cb) -> Result<Option<R>>
where Cb: FnMut(ExecCbData) -> Result<ExecCbResult<R>> { where Cb: FnMut(ExecCbData) -> Result<ExecCbResult<R>> {
while let Some(mut state) = self.vm_stack.pop() { while let Some(mut state) = self.vm_stack.pop() {
let (section_index, section) = match obj.sections.at_address(state.address) { let section = &obj.sections[state.address.section];
Ok(ret) => ret, ensure!(
Err(e) => { section.contains(state.address.address),
log::error!("{}", e); "Invalid address {:#010X} within section {} ({:#010X}-{:#010X})",
// return Ok(None); state.address.address,
continue; section.name,
} section.address,
}; section.address + section.size
);
if section.kind != ObjSectionKind::Code { if section.kind != ObjSectionKind::Code {
log::warn!("Attempted to visit non-code address {:#010X}", state.address); log::warn!("Attempted to visit non-code address {:#010X}", state.address);
continue; continue;
@ -95,24 +97,24 @@ impl Executor {
// Already visited block // Already visited block
let section_address = section.address as u32; let section_address = section.address as u32;
if self.visited.contains(section_index, section_address, state.address) { if self.visited.contains(section_address, state.address) {
continue; continue;
} }
let mut block_start = state.address; let mut block_start = state.address;
loop { loop {
self.visited.insert(section_index, section_address, state.address); self.visited.insert(section_address, state.address);
let ins = match disassemble(section, state.address) { let ins = match disassemble(section, state.address.address) {
Some(ins) => ins, Some(ins) => ins,
None => return Ok(None), None => return Ok(None),
}; };
let result = state.vm.step(&ins); let result = state.vm.step(obj, state.address, &ins);
match cb(ExecCbData { match cb(ExecCbData {
executor: self, executor: self,
vm: &mut state.vm, vm: &mut state.vm,
result, result,
section_index, ins_addr: state.address,
section, section,
ins: &ins, ins: &ins,
block_start, block_start,
@ -121,7 +123,7 @@ impl Executor {
state.address += 4; state.address += 4;
} }
ExecCbResult::Jump(addr) => { ExecCbResult::Jump(addr) => {
if self.visited.contains(section_index, section_address, addr) { if self.visited.contains(section_address, addr) {
break; break;
} }
block_start = addr; block_start = addr;
@ -135,7 +137,7 @@ impl Executor {
Ok(None) Ok(None)
} }
pub fn push(&mut self, address: u32, vm: Box<VM>, sort: bool) { pub fn push(&mut self, address: SectionAddress, vm: Box<VM>, sort: bool) {
self.vm_stack.push(VMState { address, vm }); self.vm_stack.push(VMState { address, vm });
if sort { if sort {
// Sort lowest to highest, so we always go highest address first // Sort lowest to highest, so we always go highest address first
@ -143,7 +145,7 @@ impl Executor {
} }
} }
pub fn visited(&self, section_index: usize, section_address: u32, address: u32) -> bool { pub fn visited(&self, section_address: u32, address: SectionAddress) -> bool {
self.visited.contains(section_index, section_address, address) self.visited.contains(section_address, address)
} }
} }

View File

@ -1,11 +1,12 @@
use std::{collections::BTreeSet, num::NonZeroU32}; use std::{collections::BTreeSet, num::NonZeroU32};
use anyhow::{Context, Result}; use anyhow::{anyhow, ensure, Context, Result};
use ppc750cl::Ins; use ppc750cl::Ins;
use crate::{ use crate::{
analysis::cfa::SectionAddress,
array_ref, array_ref,
obj::{ObjInfo, ObjSection, ObjSectionKind}, obj::{ObjInfo, ObjKind, ObjRelocKind, ObjSection, ObjSectionKind},
}; };
pub mod cfa; pub mod cfa;
@ -18,33 +19,119 @@ pub mod tracker;
pub mod vm; pub mod vm;
pub fn disassemble(section: &ObjSection, address: u32) -> Option<Ins> { pub fn disassemble(section: &ObjSection, address: u32) -> Option<Ins> {
read_u32(&section.data, address, section.address as u32).map(|code| Ins::new(code, address)) read_u32(section, address).map(|code| Ins::new(code, address))
} }
pub fn read_u32(data: &[u8], address: u32, section_address: u32) -> Option<u32> { pub fn read_u32(section: &ObjSection, address: u32) -> Option<u32> {
let offset = (address - section_address) as usize; let offset = (address as u64 - section.address) as usize;
if data.len() < offset + 4 { if section.data.len() < offset + 4 {
return None; return None;
} }
Some(u32::from_be_bytes(*array_ref!(data, offset, 4))) Some(u32::from_be_bytes(*array_ref!(section.data, offset, 4)))
} }
fn is_valid_jump_table_addr(obj: &ObjInfo, addr: u32) -> bool { fn read_unresolved_relocation_address(
matches!(obj.sections.at_address(addr), Ok((_, section)) if section.kind != ObjSectionKind::Bss) obj: &ObjInfo,
section: &ObjSection,
address: u32,
reloc_kind: Option<ObjRelocKind>,
) -> Result<Option<SectionAddress>> {
if let Some(reloc) = obj
.unresolved_relocations
.iter()
.find(|reloc| reloc.section as usize == section.elf_index && reloc.address == address)
{
ensure!(reloc.module_id == obj.module_id);
if let Some(reloc_kind) = reloc_kind {
ensure!(reloc.kind == reloc_kind);
}
let (target_section_index, target_section) =
obj.sections.get_elf_index(reloc.target_section as usize).ok_or_else(|| {
anyhow!(
"Failed to find target section {} for unresolved relocation",
reloc.target_section
)
})?;
Ok(Some(SectionAddress {
section: target_section_index,
address: target_section.address as u32 + reloc.addend,
}))
} else {
Ok(None)
}
}
fn read_relocation_address(
obj: &ObjInfo,
section: &ObjSection,
address: u32,
reloc_kind: Option<ObjRelocKind>,
) -> Result<Option<SectionAddress>> {
let Some(reloc) = section.relocations.at(address) else {
return Ok(None);
};
if let Some(reloc_kind) = reloc_kind {
ensure!(reloc.kind == reloc_kind);
}
let symbol = &obj.symbols[reloc.target_symbol];
let section_index = symbol.section.with_context(|| {
format!("Symbol '{}' @ {:#010X} missing section", symbol.name, symbol.address)
})?;
Ok(Some(SectionAddress {
section: section_index,
address: (symbol.address as i64 + reloc.addend) as u32,
}))
}
pub fn read_address(obj: &ObjInfo, section: &ObjSection, address: u32) -> Result<SectionAddress> {
if obj.kind == ObjKind::Relocatable {
let mut opt = read_relocation_address(obj, section, address, Some(ObjRelocKind::Absolute))?;
if opt.is_none() {
opt = read_unresolved_relocation_address(
obj,
section,
address,
Some(ObjRelocKind::Absolute),
)?;
}
opt.with_context(|| {
format!("Failed to find relocation for {:#010X} in section {}", address, section.name)
})
} else {
let offset = (address as u64 - section.address) as usize;
let address = u32::from_be_bytes(*array_ref!(section.data, offset, 4));
let (section_index, _) = obj.sections.at_address(address)?;
Ok(SectionAddress::new(section_index, address))
}
}
fn is_valid_jump_table_addr(obj: &ObjInfo, addr: SectionAddress) -> bool {
!matches!(obj.sections[addr.section].kind, ObjSectionKind::Code | ObjSectionKind::Bss)
}
#[inline(never)]
pub fn relocation_target_for(
obj: &ObjInfo,
addr: SectionAddress,
reloc_kind: Option<ObjRelocKind>,
) -> Result<Option<SectionAddress>> {
let section = &obj.sections[addr.section];
let mut opt = read_relocation_address(obj, section, addr.address, reloc_kind)?;
if opt.is_none() {
opt = read_unresolved_relocation_address(obj, section, addr.address, reloc_kind)?;
}
Ok(opt)
} }
fn get_jump_table_entries( fn get_jump_table_entries(
obj: &ObjInfo, obj: &ObjInfo,
addr: u32, addr: SectionAddress,
size: Option<NonZeroU32>, size: Option<NonZeroU32>,
from: u32, from: SectionAddress,
function_start: u32, function_start: SectionAddress,
function_end: u32, function_end: Option<SectionAddress>,
) -> Result<(Vec<u32>, u32)> { ) -> Result<(Vec<SectionAddress>, u32)> {
let (_, section) = obj.sections.at_address(addr).with_context(|| { let section = &obj.sections[addr.section];
format!("Failed to get jump table entries @ {:#010X} size {:?}", addr, size)
})?;
let offset = (addr as u64 - section.address) as usize;
if let Some(size) = size.map(|n| n.get()) { if let Some(size) = size.map(|n| n.get()) {
log::trace!( log::trace!(
"Located jump table @ {:#010X} with entry count {} (from {:#010X})", "Located jump table @ {:#010X} with entry count {} (from {:#010X})",
@ -52,21 +139,58 @@ fn get_jump_table_entries(
size / 4, size / 4,
from from
); );
let jt_data = &section.data[offset..offset + size as usize]; let mut entries = Vec::with_capacity(size as usize / 4);
let entries = let mut data = section.data_range(addr.address, addr.address + size)?;
jt_data.chunks_exact(4).map(|c| u32::from_be_bytes(c.try_into().unwrap())).collect(); let mut cur_addr = addr;
loop {
if data.is_empty() {
break;
}
if let Some(target) =
relocation_target_for(obj, cur_addr, Some(ObjRelocKind::Absolute))?
{
entries.push(target);
} else {
let entry_addr = u32::from_be_bytes(*array_ref!(data, 0, 4));
let (section_index, _) =
obj.sections.at_address(entry_addr).with_context(|| {
format!(
"Invalid jump table entry {:#010X} at {:#010X}",
entry_addr, cur_addr
)
})?;
entries.push(SectionAddress::new(section_index, entry_addr));
}
data = &data[4..];
cur_addr += 4;
}
Ok((entries, size)) Ok((entries, size))
} else { } else {
let mut entries = Vec::new(); let mut entries = Vec::new();
let mut cur_addr = addr; let mut cur_addr = addr;
while let Some(value) = read_u32(&section.data, cur_addr, section.address as u32) { loop {
if value < function_start || value >= function_end { let target = if let Some(target) =
relocation_target_for(obj, cur_addr, Some(ObjRelocKind::Absolute))?
{
target
} else if obj.kind == ObjKind::Executable {
let Some(value) = read_u32(section, cur_addr.address) else {
break;
};
let Ok((section_index, _)) = obj.sections.at_address(value) else {
break;
};
SectionAddress::new(section_index, value)
} else {
break;
};
if target < function_start || matches!(function_end, Some(end) if target >= end) {
break; break;
} }
entries.push(value); entries.push(target);
cur_addr += 4; cur_addr += 4;
} }
let size = cur_addr - addr; let size = cur_addr.address - addr.address;
log::debug!( log::debug!(
"Guessed jump table @ {:#010X} with entry count {} (from {:#010X})", "Guessed jump table @ {:#010X} with entry count {} (from {:#010X})",
addr, addr,
@ -79,22 +203,26 @@ fn get_jump_table_entries(
pub fn uniq_jump_table_entries( pub fn uniq_jump_table_entries(
obj: &ObjInfo, obj: &ObjInfo,
addr: u32, addr: SectionAddress,
size: Option<NonZeroU32>, size: Option<NonZeroU32>,
from: u32, from: SectionAddress,
function_start: u32, function_start: SectionAddress,
function_end: u32, function_end: Option<SectionAddress>,
) -> Result<(BTreeSet<u32>, u32)> { ) -> Result<(BTreeSet<SectionAddress>, u32)> {
if !is_valid_jump_table_addr(obj, addr) { if !is_valid_jump_table_addr(obj, addr) {
return Ok((BTreeSet::new(), 0)); return Ok((BTreeSet::new(), 0));
} }
let (entries, size) = let (entries, size) =
get_jump_table_entries(obj, addr, size, from, function_start, function_end)?; get_jump_table_entries(obj, addr, size, from, function_start, function_end)?;
Ok((BTreeSet::from_iter(entries.iter().cloned().filter(|&addr| addr != 0)), size)) Ok((BTreeSet::from_iter(entries.iter().cloned()), size))
} }
pub fn skip_alignment(section: &ObjSection, mut addr: u32, end: u32) -> Option<u32> { pub fn skip_alignment(
let mut data = match section.data_range(addr, end) { section: &ObjSection,
mut addr: SectionAddress,
end: SectionAddress,
) -> Option<SectionAddress> {
let mut data = match section.data_range(addr.address, end.address) {
Ok(data) => data, Ok(data) => data,
Err(_) => return None, Err(_) => return None,
}; };

View File

@ -1,11 +1,15 @@
use std::ops::Range; use std::ops::Range;
use anyhow::Result; use anyhow::{bail, ensure, Result};
use flagset::FlagSet; use flagset::FlagSet;
use itertools::Itertools;
use crate::{ use crate::{
analysis::cfa::AnalyzerState, analysis::cfa::{AnalyzerState, SectionAddress},
obj::{ObjInfo, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind}, obj::{
ObjInfo, ObjKind, ObjRelocKind, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet,
ObjSymbolFlags, ObjSymbolKind,
},
}; };
pub trait AnalysisPass { pub trait AnalysisPass {
@ -20,9 +24,9 @@ pub const TRK_TABLE_SIZE: u32 = 0x1F34; // always?
// TRK_MINNOW_DOLPHIN.a __exception.s // TRK_MINNOW_DOLPHIN.a __exception.s
impl AnalysisPass for FindTRKInterruptVectorTable { impl AnalysisPass for FindTRKInterruptVectorTable {
fn execute(state: &mut AnalyzerState, obj: &ObjInfo) -> Result<()> { fn execute(state: &mut AnalyzerState, obj: &ObjInfo) -> Result<()> {
for (&start, _) in state.function_bounds.iter().filter(|&(_, &end)| end == 0) { for (&start, _) in state.function_bounds.iter().filter(|&(_, &end)| end.is_none()) {
let (section_index, section) = obj.sections.at_address(start)?; let section = &obj.sections[start.section];
let data = match section.data_range(start, 0) { let data = match section.data_range(start.address, 0) {
Ok(ret) => ret, Ok(ret) => ret,
Err(_) => continue, Err(_) => continue,
}; };
@ -33,8 +37,8 @@ impl AnalysisPass for FindTRKInterruptVectorTable {
state.known_symbols.insert(start, ObjSymbol { state.known_symbols.insert(start, ObjSymbol {
name: "gTRKInterruptVectorTable".to_string(), name: "gTRKInterruptVectorTable".to_string(),
demangled_name: None, demangled_name: None,
address: start as u64, address: start.address as u64,
section: Some(section_index), section: Some(start.section),
size: 0, size: 0,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(FlagSet::from(ObjSymbolFlags::Global)), flags: ObjSymbolFlagSet(FlagSet::from(ObjSymbolFlags::Global)),
@ -46,8 +50,8 @@ impl AnalysisPass for FindTRKInterruptVectorTable {
state.known_symbols.insert(end, ObjSymbol { state.known_symbols.insert(end, ObjSymbol {
name: "gTRKInterruptVectorTableEnd".to_string(), name: "gTRKInterruptVectorTableEnd".to_string(),
demangled_name: None, demangled_name: None,
address: end as u64, address: end.address as u64,
section: Some(section_index), section: Some(start.section),
size: 0, size: 0,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(FlagSet::from(ObjSymbolFlags::Global)), flags: ObjSymbolFlagSet(FlagSet::from(ObjSymbolFlags::Global)),
@ -77,10 +81,10 @@ const SLEDS: [([u8; 4], &str, &str); 4] = [
impl AnalysisPass for FindSaveRestSleds { impl AnalysisPass for FindSaveRestSleds {
fn execute(state: &mut AnalyzerState, obj: &ObjInfo) -> Result<()> { fn execute(state: &mut AnalyzerState, obj: &ObjInfo) -> Result<()> {
const SLED_SIZE: usize = 19 * 4; // registers 14-31 + blr const SLED_SIZE: usize = 19 * 4; // registers 14-31 + blr
let mut clear_ranges: Vec<Range<u32>> = vec![]; let mut clear_ranges: Vec<Range<SectionAddress>> = vec![];
for (&start, _) in state.function_bounds.iter().filter(|&(_, &end)| end != 0) { for (&start, _) in state.function_bounds.iter().filter(|&(_, &end)| end.is_some()) {
let (section_index, section) = obj.sections.at_address(start)?; let section = &obj.sections[start.section];
let data = match section.data_range(start, 0) { let data = match section.data_range(start.address, 0) {
Ok(ret) => ret, Ok(ret) => ret,
Err(_) => continue, Err(_) => continue,
}; };
@ -91,8 +95,8 @@ impl AnalysisPass for FindSaveRestSleds {
state.known_symbols.insert(start, ObjSymbol { state.known_symbols.insert(start, ObjSymbol {
name: func.to_string(), name: func.to_string(),
demangled_name: None, demangled_name: None,
address: start as u64, address: start.address as u64,
section: Some(section_index), section: Some(start.section),
size: SLED_SIZE as u64, size: SLED_SIZE as u64,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()), flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
@ -105,8 +109,8 @@ impl AnalysisPass for FindSaveRestSleds {
state.known_symbols.insert(addr, ObjSymbol { state.known_symbols.insert(addr, ObjSymbol {
name: format!("{}{}", label, i), name: format!("{}{}", label, i),
demangled_name: None, demangled_name: None,
address: addr as u64, address: addr.address as u64,
section: Some(section_index), section: Some(start.section),
size: 0, size: 0,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()), flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
@ -119,12 +123,163 @@ impl AnalysisPass for FindSaveRestSleds {
} }
} }
for range in clear_ranges { for range in clear_ranges {
for addr in range.step_by(4) { let mut addr = range.start;
while addr < range.end {
state.function_entries.remove(&addr); state.function_entries.remove(&addr);
state.function_bounds.remove(&addr); state.function_bounds.remove(&addr);
state.function_slices.remove(&addr); state.function_slices.remove(&addr);
addr += 4;
} }
} }
Ok(()) Ok(())
} }
} }
pub struct FindRelCtorsDtors {}
impl AnalysisPass for FindRelCtorsDtors {
fn execute(state: &mut AnalyzerState, obj: &ObjInfo) -> Result<()> {
ensure!(obj.kind == ObjKind::Relocatable);
ensure!(!obj.unresolved_relocations.is_empty());
match (obj.sections.by_name(".ctors")?, obj.sections.by_name(".dtors")?) {
(Some(_), Some(_)) => return Ok(()),
(None, None) => {}
_ => bail!("Only one of .ctors and .dtors has been found?"),
}
let possible_sections = obj
.sections
.iter()
.filter(|&(_, section)| {
if section.section_known
|| !matches!(section.kind, ObjSectionKind::Data | ObjSectionKind::ReadOnlyData)
|| section.size < 4
{
return false;
}
let mut current_address = section.address as u32;
let section_end = current_address + section.size as u32;
// Check that each word has a relocation to a function
// And the section ends with a null pointer
while let Some(reloc) = obj.unresolved_relocations.iter().find(|reloc| {
reloc.module_id == obj.module_id
&& reloc.section == section.elf_index as u8
&& reloc.address == current_address
&& reloc.kind == ObjRelocKind::Absolute
}) {
let Some((target_section_index, target_section)) = obj
.sections
.iter()
.find(|(_, section)| section.elf_index == reloc.target_section as usize)
else {
return false;
};
if target_section.kind != ObjSectionKind::Code
|| !state
.function_bounds
.contains_key(&SectionAddress::new(target_section_index, reloc.addend))
{
return false;
}
current_address += 4;
if current_address >= section_end {
return false;
}
}
if current_address + 4 != section_end {
return false;
}
section.data_range(section_end - 4, section_end).ok() == Some(&[0; 4])
})
.collect_vec();
if possible_sections.len() != 2 {
log::warn!("Failed to find .ctors and .dtors");
return Ok(());
}
log::debug!("Found .ctors and .dtors: {:?}", possible_sections);
let ctors_section_index = possible_sections[0].0;
state.known_sections.insert(ctors_section_index, ".ctors".to_string());
state.known_symbols.insert(SectionAddress::new(ctors_section_index, 0), ObjSymbol {
name: "_ctors".to_string(),
demangled_name: None,
address: 0,
section: Some(ctors_section_index),
size: 0,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
kind: Default::default(),
align: None,
data_kind: Default::default(),
});
let dtors_section_index = possible_sections[1].0;
state.known_sections.insert(dtors_section_index, ".dtors".to_string());
state.known_symbols.insert(SectionAddress::new(dtors_section_index, 0), ObjSymbol {
name: "_dtors".to_string(),
demangled_name: None,
address: 0,
section: Some(dtors_section_index),
size: 0,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
kind: Default::default(),
align: None,
data_kind: Default::default(),
});
// Check for duplicate entries in .dtors, indicating __destroy_global_chain_reference
// let mut dtors_entries = vec![];
// let mut current_address = obj.sections[dtors_section_index].address as u32;
// let section_end = current_address + obj.sections[dtors_section_index].size as u32;
// while let Some(reloc) = obj.unresolved_relocations.iter().find(|reloc| {
// reloc.module_id == obj.module_id
// && reloc.section == obj.sections[dtors_section_index].elf_index as u8
// && reloc.address == current_address
// && reloc.kind == ObjRelocKind::Absolute
// }) {
// let Some((target_section_index, target_section)) = obj
// .sections
// .iter()
// .find(|(_, section)| section.elf_index == reloc.target_section as usize)
// else {
// bail!("Failed to find target section for .dtors entry");
// };
// if target_section.kind != ObjSectionKind::Code
// || !state
// .function_bounds
// .contains_key(&SectionAddress::new(target_section_index, reloc.addend))
// {
// bail!("Failed to find target function for .dtors entry");
// }
// dtors_entries.push(SectionAddress::new(target_section_index, reloc.addend));
// current_address += 4;
// if current_address >= section_end {
// bail!("Failed to find null terminator for .dtors");
// }
// }
// if current_address + 4 != section_end {
// bail!("Failed to find null terminator for .dtors");
// }
// if dtors_entries.len() != dtors_entries.iter().unique().count() {
// log::debug!("Found __destroy_global_chain_reference");
// state.known_symbols.insert(SectionAddress::new(dtors_section_index, 0), ObjSymbol {
// name: "__destroy_global_chain_reference".to_string(),
// demangled_name: None,
// address: 0,
// section: Some(dtors_section_index),
// size: 4,
// size_known: true,
// flags: ObjSymbolFlagSet(ObjSymbolFlags::Local.into()),
// kind: ObjSymbolKind::Object,
// align: None,
// data_kind: Default::default(),
// });
// }
Ok(())
}
}

View File

@ -1,7 +1,10 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, bail, Result};
use crate::{ use crate::{
analysis::{cfa::AnalyzerState, read_u32}, analysis::{
cfa::{AnalyzerState, SectionAddress},
read_address,
},
obj::{ obj::{
ObjInfo, ObjSectionKind, ObjSplit, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjInfo, ObjSectionKind, ObjSplit, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags,
ObjSymbolKind, ObjSymbolKind,
@ -189,189 +192,230 @@ const SIGNATURES: &[(&str, &str)] = &[
]; ];
const POST_SIGNATURES: &[(&str, &str)] = &[ const POST_SIGNATURES: &[(&str, &str)] = &[
("RSOStaticLocateObject", include_str!("../../assets/signatures/RSOStaticLocateObject.yml")), ("RSOStaticLocateObject", include_str!("../../assets/signatures/RSOStaticLocateObject.yml")),
// ("GXInit", include_str!("../../assets/signatures/GXInit.yml")), ("GXInit", include_str!("../../assets/signatures/GXInit.yml")),
("__register_fragment", include_str!("../../assets/signatures/__register_fragment.yml")), ("__register_fragment", include_str!("../../assets/signatures/__register_fragment.yml")),
("__unregister_fragment", include_str!("../../assets/signatures/__unregister_fragment.yml")),
("__register_atexit", include_str!("../../assets/signatures/__register_atexit.yml")),
(
"__register_global_object",
include_str!("../../assets/signatures/__register_global_object.yml"),
),
]; ];
fn apply_signature_for_symbol(obj: &mut ObjInfo, name: &str, sig_str: &str) -> Result<()> {
let Some((_, symbol)) = obj.symbols.by_name(name)? else {
return Ok(());
};
let Some(section_index) = symbol.section else {
return Ok(());
};
let addr = symbol.address as u32;
let section = &obj.sections[section_index];
if let Some(signature) = check_signatures_str(section, addr, sig_str)? {
apply_signature(obj, SectionAddress::new(section_index, addr), &signature)?;
}
Ok(())
}
fn apply_ctors_signatures(obj: &mut ObjInfo) -> Result<()> {
let Some((_, symbol)) = obj.symbols.by_name("_ctors")? else {
return Ok(());
};
// First entry of ctors is __init_cpp_exceptions
let ctors_section_index =
symbol.section.ok_or_else(|| anyhow!("Missing _ctors symbol section"))?;
let ctors_section = &obj.sections[ctors_section_index];
// __init_cpp_exceptions_reference + null pointer
if ctors_section.size < 8 {
return Ok(());
}
let Some(target) = read_address(obj, ctors_section, symbol.address as u32).ok() else {
return Ok(());
};
let Some(signature) = check_signatures_str(
&obj.sections[target.section],
target.address,
include_str!("../../assets/signatures/__init_cpp_exceptions.yml"),
)?
else {
return Ok(());
};
let address = symbol.address;
apply_signature(obj, target, &signature)?;
obj.symbols.add(
ObjSymbol {
name: "__init_cpp_exceptions_reference".to_string(),
demangled_name: None,
address,
section: Some(ctors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global | ObjSymbolFlags::RelocationIgnore),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
if obj.sections[ctors_section_index].splits.for_address(address as u32).is_none() {
obj.add_split(ctors_section_index, address as u32, ObjSplit {
unit: "__init_cpp_exceptions.cpp".to_string(),
end: address as u32 + 4,
align: None,
common: false,
autogenerated: true,
})?;
}
Ok(())
}
fn apply_dtors_signatures(obj: &mut ObjInfo) -> Result<()> {
let Some((_, symbol)) = obj.symbols.by_name("_dtors")? else {
for symbol in obj.symbols.iter() {
println!("{:?} {:#010X} {}", symbol.section, symbol.address, symbol.name);
}
bail!("Missing _dtors symbol");
// return Ok(());
};
let dtors_section_index =
symbol.section.ok_or_else(|| anyhow!("Missing _dtors symbol section"))?;
let dtors_section = &obj.sections[dtors_section_index];
// __destroy_global_chain_reference + null pointer
if dtors_section.size < 8 {
return Ok(());
}
let address = symbol.address;
let dgc_target = read_address(obj, dtors_section, address as u32).ok();
let fce_target = read_address(obj, dtors_section, address as u32 + 4).ok();
let mut found_dgc = false;
let mut found_fce = false;
// First entry of dtors is __destroy_global_chain
if let Some(dgc_target) = dgc_target {
if let Some(signature) = check_signatures_str(
&obj.sections[dgc_target.section],
dgc_target.address,
include_str!("../../assets/signatures/__destroy_global_chain.yml"),
)? {
apply_signature(obj, dgc_target, &signature)?;
obj.add_symbol(
ObjSymbol {
name: "__destroy_global_chain_reference".to_string(),
demangled_name: None,
address,
section: Some(dtors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(
ObjSymbolFlags::Global | ObjSymbolFlags::RelocationIgnore,
),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
found_dgc = true;
} else {
log::warn!("Failed to match __destroy_global_chain signature ({:#010X})", dgc_target);
}
}
// Second entry of dtors is __fini_cpp_exceptions
if let Some(fce_target) = fce_target {
if let Some(signature) = check_signatures_str(
&obj.sections[fce_target.section],
fce_target.address,
include_str!("../../assets/signatures/__fini_cpp_exceptions.yml"),
)? {
apply_signature(obj, fce_target, &signature)?;
obj.add_symbol(
ObjSymbol {
name: "__fini_cpp_exceptions_reference".to_string(),
demangled_name: None,
address: address + 4,
section: Some(dtors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(
ObjSymbolFlags::Global | ObjSymbolFlags::RelocationIgnore,
),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
found_fce = true;
}
}
if found_dgc {
let mut end = address as u32 + 4;
if found_fce {
end += 4;
}
if obj.sections[dtors_section_index].splits.for_address(address as u32).is_none() {
obj.add_split(dtors_section_index, address as u32, ObjSplit {
unit: "__init_cpp_exceptions.cpp".to_string(),
end,
align: None,
common: false,
autogenerated: true,
})?;
}
}
Ok(())
}
fn apply_init_user_signatures(obj: &mut ObjInfo) -> Result<()> {
let Some((_, symbol)) = obj.symbols.by_name("__init_user")? else {
return Ok(());
};
let Some(section_index) = symbol.section else {
return Ok(());
};
// __init_user can be overridden, but we can still look for __init_cpp from it
let mut analyzer = AnalyzerState::default();
analyzer.process_function_at(obj, SectionAddress::new(section_index, symbol.address as u32))?;
for addr in analyzer.function_entries {
let section = &obj.sections[addr.section];
if let Some(signature) = check_signatures_str(
section,
addr.address,
include_str!("../../assets/signatures/__init_cpp.yml"),
)? {
apply_signature(obj, SectionAddress::new(section_index, addr.address), &signature)?;
break;
}
}
Ok(())
}
pub fn apply_signatures(obj: &mut ObjInfo) -> Result<()> { pub fn apply_signatures(obj: &mut ObjInfo) -> Result<()> {
let entry = obj.entry as u32; if let Some(entry) = obj.entry.map(|n| n as u32) {
let (entry_section_index, entry_section) = obj.sections.at_address(entry)?; let (entry_section_index, entry_section) = obj.sections.at_address(entry)?;
if let Some(signature) = check_signatures_str( if let Some(signature) = check_signatures_str(
entry_section, entry_section,
entry, entry,
include_str!("../../assets/signatures/__start.yml"), include_str!("../../assets/signatures/__start.yml"),
)? { )? {
apply_signature(obj, entry_section_index, entry, &signature)?; apply_signature(obj, SectionAddress::new(entry_section_index, entry), &signature)?;
}
} }
for &(name, sig_str) in SIGNATURES { for &(name, sig_str) in SIGNATURES {
if let Some((_, symbol)) = obj.symbols.by_name(name)? { apply_signature_for_symbol(obj, name, sig_str)?
let addr = symbol.address as u32;
let section_index =
symbol.section.ok_or_else(|| anyhow!("Symbol '{}' missing section", name))?;
let section = &obj.sections[section_index];
if let Some(signature) = check_signatures_str(section, addr, sig_str)? {
apply_signature(obj, section_index, addr, &signature)?;
}
}
}
if let Some((_, symbol)) = obj.symbols.by_name("__init_user")? {
// __init_user can be overridden, but we can still look for __init_cpp from it
let mut analyzer = AnalyzerState::default();
analyzer.process_function_at(obj, symbol.address as u32)?;
for addr in analyzer.function_entries {
let (section_index, section) = obj.sections.at_address(addr)?;
if let Some(signature) = check_signatures_str(
section,
addr,
include_str!("../../assets/signatures/__init_cpp.yml"),
)? {
apply_signature(obj, section_index, addr, &signature)?;
break;
}
}
}
if let Some((_, symbol)) = obj.symbols.by_name("_ctors")? {
// First entry of ctors is __init_cpp_exceptions
let ctors_section_index =
symbol.section.ok_or_else(|| anyhow!("Missing _ctors symbol section"))?;
let ctors_section = &obj.sections[ctors_section_index];
let target =
read_u32(&ctors_section.data, symbol.address as u32, ctors_section.address as u32)
.ok_or_else(|| anyhow!("Failed to read _ctors data"))?;
if target != 0 {
let (target_section_index, target_section) = obj.sections.at_address(target)?;
if let Some(signature) = check_signatures_str(
target_section,
target,
include_str!("../../assets/signatures/__init_cpp_exceptions.yml"),
)? {
let address = symbol.address;
apply_signature(obj, target_section_index, target, &signature)?;
obj.symbols.add(
ObjSymbol {
name: "__init_cpp_exceptions_reference".to_string(),
demangled_name: None,
address,
section: Some(ctors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
if obj.sections[ctors_section_index].splits.for_address(address as u32).is_none() {
obj.add_split(ctors_section_index, address as u32, ObjSplit {
unit: "__init_cpp_exceptions.cpp".to_string(),
end: address as u32 + 4,
align: None,
common: false,
autogenerated: true,
})?;
}
}
}
}
if let Some((_, symbol)) = obj.symbols.by_name("_dtors")? {
let dtors_section_index =
symbol.section.ok_or_else(|| anyhow!("Missing _dtors symbol section"))?;
let dtors_section = &obj.sections[dtors_section_index];
let address = symbol.address;
let section_address = dtors_section.address;
// First entry of dtors is __destroy_global_chain
let dgc_target = read_u32(&dtors_section.data, address as u32, section_address as u32)
.ok_or_else(|| anyhow!("Failed to read _dtors data"))?;
let fce_target = read_u32(&dtors_section.data, address as u32 + 4, section_address as u32)
.ok_or_else(|| anyhow!("Failed to read _dtors data"))?;
let mut found_dgc = false;
let mut found_fce = false;
if dgc_target != 0 {
let (target_section_index, target_section) = obj.sections.at_address(dgc_target)?;
if let Some(signature) = check_signatures_str(
target_section,
dgc_target,
include_str!("../../assets/signatures/__destroy_global_chain.yml"),
)? {
apply_signature(obj, target_section_index, dgc_target, &signature)?;
obj.add_symbol(
ObjSymbol {
name: "__destroy_global_chain_reference".to_string(),
demangled_name: None,
address,
section: Some(dtors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
found_dgc = true;
} else {
log::warn!(
"Failed to match __destroy_global_chain signature ({:#010X})",
dgc_target
);
}
}
// Second entry of dtors is __fini_cpp_exceptions
if fce_target != 0 {
let (target_section_index, target_section) = obj.sections.at_address(fce_target)?;
if let Some(signature) = check_signatures_str(
target_section,
fce_target,
include_str!("../../assets/signatures/__fini_cpp_exceptions.yml"),
)? {
apply_signature(obj, target_section_index, fce_target, &signature)?;
obj.add_symbol(
ObjSymbol {
name: "__fini_cpp_exceptions_reference".to_string(),
demangled_name: None,
address: address + 4,
section: Some(dtors_section_index),
size: 4,
size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Global.into()),
kind: ObjSymbolKind::Object,
align: None,
data_kind: Default::default(),
},
true,
)?;
found_fce = true;
}
}
if found_dgc {
let mut end = address as u32 + 4;
if found_fce {
end += 4;
}
if obj.sections[dtors_section_index].splits.for_address(address as u32).is_none() {
obj.add_split(dtors_section_index, address as u32, ObjSplit {
unit: "__init_cpp_exceptions.cpp".to_string(),
end,
align: None,
common: false,
autogenerated: true,
})?;
}
}
} }
apply_init_user_signatures(obj)?;
apply_ctors_signatures(obj)?;
apply_dtors_signatures(obj)?;
Ok(()) Ok(())
} }
pub fn apply_signatures_post(obj: &mut ObjInfo) -> Result<()> { pub fn apply_signatures_post(obj: &mut ObjInfo) -> Result<()> {
log::info!("Checking post CFA signatures..."); log::debug!("Checking post CFA signatures");
for &(_name, sig_str) in POST_SIGNATURES { for &(_name, sig_str) in POST_SIGNATURES {
let signatures = parse_signatures(sig_str)?; let signatures = parse_signatures(sig_str)?;
let mut found_signature = None; let mut found_signature = None;
@ -391,13 +435,10 @@ pub fn apply_signatures_post(obj: &mut ObjInfo) -> Result<()> {
} }
if let Some((symbol_index, signature)) = found_signature { if let Some((symbol_index, signature)) = found_signature {
let symbol = &obj.symbols[symbol_index]; let symbol = &obj.symbols[symbol_index];
let section_index = symbol let symbol_addr = SectionAddress::new(symbol.section.unwrap(), symbol.address as u32);
.section apply_signature(obj, symbol_addr, &signature)?;
.ok_or_else(|| anyhow!("Symbol '{}' missing section", symbol.name))?;
let address = symbol.address as u32;
apply_signature(obj, section_index, address, &signature)?;
} }
} }
log::info!("Done!"); log::debug!("Done!");
Ok(()) Ok(())
} }

View File

@ -8,24 +8,25 @@ use ppc750cl::{Ins, Opcode};
use crate::{ use crate::{
analysis::{ analysis::{
cfa::SectionAddress,
disassemble, disassemble,
executor::{ExecCbData, ExecCbResult, Executor}, executor::{ExecCbData, ExecCbResult, Executor},
uniq_jump_table_entries, uniq_jump_table_entries,
vm::{BranchTarget, StepResult, VM}, vm::{section_address_for, BranchTarget, StepResult, VM},
}, },
obj::{ObjInfo, ObjSection}, obj::{ObjInfo, ObjKind, ObjSection},
}; };
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct FunctionSlices { pub struct FunctionSlices {
pub blocks: BTreeMap<u32, u32>, pub blocks: BTreeMap<SectionAddress, Option<SectionAddress>>,
pub branches: BTreeMap<u32, Vec<u32>>, pub branches: BTreeMap<SectionAddress, Vec<SectionAddress>>,
pub function_references: BTreeSet<u32>, pub function_references: BTreeSet<SectionAddress>,
pub jump_table_references: BTreeMap<u32, u32>, pub jump_table_references: BTreeMap<SectionAddress, u32>,
pub prologue: Option<u32>, pub prologue: Option<SectionAddress>,
pub epilogue: Option<u32>, pub epilogue: Option<SectionAddress>,
// Either a block or tail call // Either a block or tail call
pub possible_blocks: BTreeSet<u32>, pub possible_blocks: BTreeSet<SectionAddress>,
pub has_conditional_blr: bool, pub has_conditional_blr: bool,
pub has_rfi: bool, pub has_rfi: bool,
pub finalized: bool, pub finalized: bool,
@ -38,7 +39,7 @@ pub enum TailCallResult {
Error(anyhow::Error), Error(anyhow::Error),
} }
type BlockRange = Range<u32>; type BlockRange = Range<SectionAddress>;
#[inline(always)] #[inline(always)]
fn next_ins(section: &ObjSection, ins: &Ins) -> Option<Ins> { disassemble(section, ins.addr + 4) } fn next_ins(section: &ObjSection, ins: &Ins) -> Option<Ins> { disassemble(section, ins.addr + 4) }
@ -72,33 +73,41 @@ fn check_sequence(
} }
impl FunctionSlices { impl FunctionSlices {
pub fn end(&self) -> u32 { self.blocks.last_key_value().map(|(_, &end)| end).unwrap_or(0) } pub fn end(&self) -> Option<SectionAddress> {
self.blocks.last_key_value().map(|(_, &end)| end).flatten()
pub fn start(&self) -> u32 {
self.blocks.first_key_value().map(|(&start, _)| start).unwrap_or(0)
} }
pub fn add_block_start(&mut self, addr: u32) -> bool { pub fn start(&self) -> Option<SectionAddress> {
self.blocks.first_key_value().map(|(&start, _)| start)
}
pub fn add_block_start(&mut self, addr: SectionAddress) -> bool {
// Slice previous block. // Slice previous block.
if let Some((_, end)) = self.blocks.range_mut(..addr).next_back() { if let Some((_, end)) = self.blocks.range_mut(..addr).next_back() {
let last_end = *end; if let Some(last_end) = *end {
if last_end > addr { if last_end > addr {
*end = addr; *end = Some(addr);
self.blocks.insert(addr, last_end); self.blocks.insert(addr, Some(last_end));
return false; return false;
}
} }
} }
// Otherwise, insert with no end. // Otherwise, insert with no end.
match self.blocks.entry(addr) { match self.blocks.entry(addr) {
btree_map::Entry::Vacant(e) => { btree_map::Entry::Vacant(e) => {
e.insert(0); e.insert(None);
true true
} }
btree_map::Entry::Occupied(_) => false, btree_map::Entry::Occupied(_) => false,
} }
} }
fn check_prologue(&mut self, section: &ObjSection, ins: &Ins) -> Result<()> { fn check_prologue(
&mut self,
section: &ObjSection,
addr: SectionAddress,
ins: &Ins,
) -> Result<()> {
#[inline(always)] #[inline(always)]
fn is_mflr(ins: &Ins) -> bool { fn is_mflr(ins: &Ins) -> bool {
// mfspr r0, LR // mfspr r0, LR
@ -117,17 +126,22 @@ impl FunctionSlices {
if check_sequence(section, ins, &[(&is_stwu, &is_mflr), (&is_mflr, &is_stw)])? { if check_sequence(section, ins, &[(&is_stwu, &is_mflr), (&is_mflr, &is_stw)])? {
if let Some(prologue) = self.prologue { if let Some(prologue) = self.prologue {
if prologue != ins.addr && prologue != ins.addr - 4 { if prologue != addr && prologue != addr - 4 {
bail!("Found duplicate prologue: {:#010X} and {:#010X}", prologue, ins.addr) bail!("Found duplicate prologue: {:#010X} and {:#010X}", prologue, addr)
} }
} else { } else {
self.prologue = Some(ins.addr); self.prologue = Some(addr);
} }
} }
Ok(()) Ok(())
} }
fn check_epilogue(&mut self, section: &ObjSection, ins: &Ins) -> Result<()> { fn check_epilogue(
&mut self,
section: &ObjSection,
addr: SectionAddress,
ins: &Ins,
) -> Result<()> {
#[inline(always)] #[inline(always)]
fn is_mtlr(ins: &Ins) -> bool { fn is_mtlr(ins: &Ins) -> bool {
// mtspr LR, r0 // mtspr LR, r0
@ -146,11 +160,11 @@ impl FunctionSlices {
if check_sequence(section, ins, &[(&is_mtlr, &is_addi), (&is_or, &is_mtlr)])? { if check_sequence(section, ins, &[(&is_mtlr, &is_addi), (&is_or, &is_mtlr)])? {
if let Some(epilogue) = self.epilogue { if let Some(epilogue) = self.epilogue {
if epilogue != ins.addr { if epilogue != addr {
bail!("Found duplicate epilogue: {:#010X} and {:#010X}", epilogue, ins.addr) bail!("Found duplicate epilogue: {:#010X} and {:#010X}", epilogue, addr)
} }
} else { } else {
self.epilogue = Some(ins.addr); self.epilogue = Some(addr);
} }
} }
Ok(()) Ok(())
@ -160,16 +174,16 @@ impl FunctionSlices {
&mut self, &mut self,
data: ExecCbData, data: ExecCbData,
obj: &ObjInfo, obj: &ObjInfo,
function_start: u32, function_start: SectionAddress,
function_end: Option<u32>, function_end: Option<SectionAddress>,
known_functions: &BTreeSet<u32>, known_functions: &BTreeSet<SectionAddress>,
) -> Result<ExecCbResult<bool>> { ) -> Result<ExecCbResult<bool>> {
let ExecCbData { executor, vm, result, section_index, section, ins, block_start } = data; let ExecCbData { executor, vm, result, ins_addr, section, ins, block_start } = data;
// Track discovered prologue(s) and epilogue(s) // Track discovered prologue(s) and epilogue(s)
self.check_prologue(section, ins) self.check_prologue(section, ins_addr, ins)
.with_context(|| format!("While processing {:#010X}", function_start))?; .with_context(|| format!("While processing {:#010X}", function_start))?;
self.check_epilogue(section, ins) self.check_epilogue(section, ins_addr, ins)
.with_context(|| format!("While processing {:#010X}", function_start))?; .with_context(|| format!("While processing {:#010X}", function_start))?;
if !self.has_conditional_blr && is_conditional_blr(ins) { if !self.has_conditional_blr && is_conditional_blr(ins) {
self.has_conditional_blr = true; self.has_conditional_blr = true;
@ -179,37 +193,37 @@ impl FunctionSlices {
} }
// If control flow hits a block we thought may be a tail call, // If control flow hits a block we thought may be a tail call,
// we know it isn't. // we know it isn't.
if self.possible_blocks.contains(&ins.addr) { if self.possible_blocks.contains(&ins_addr) {
self.possible_blocks.remove(&ins.addr); self.possible_blocks.remove(&ins_addr);
} }
match result { match result {
StepResult::Continue | StepResult::LoadStore { .. } => { StepResult::Continue | StepResult::LoadStore { .. } => {
let next_address = ins.addr + 4; let next_address = ins_addr + 4;
// If we already visited the next address, connect the blocks and end // If we already visited the next address, connect the blocks and end
if executor.visited(section_index, section.address as u32, next_address) { if executor.visited(section.address as u32, next_address) {
self.blocks.insert(block_start, next_address); self.blocks.insert(block_start, Some(next_address));
self.branches.insert(ins.addr, vec![next_address]); self.branches.insert(ins_addr, vec![next_address]);
Ok(ExecCbResult::EndBlock) Ok(ExecCbResult::EndBlock)
} else { } else {
Ok(ExecCbResult::Continue) Ok(ExecCbResult::Continue)
} }
} }
StepResult::Illegal => { StepResult::Illegal => {
log::debug!("Illegal instruction @ {:#010X}", ins.addr); log::debug!("Illegal instruction @ {:#010X}", ins_addr);
Ok(ExecCbResult::End(false)) Ok(ExecCbResult::End(false))
} }
StepResult::Jump(target) => match target { StepResult::Jump(target) => match target {
BranchTarget::Unknown => { BranchTarget::Unknown => {
// Likely end of function // Likely end of function
let next_addr = ins.addr + 4; let next_addr = ins_addr + 4;
self.blocks.insert(block_start, next_addr); self.blocks.insert(block_start, Some(next_addr));
// If this function has a prologue but no epilogue, and this // If this function has a prologue but no epilogue, and this
// instruction is a bctr, we can assume it's an unrecovered // instruction is a bctr, we can assume it's an unrecovered
// jump table and continue analysis. // jump table and continue analysis.
if self.prologue.is_some() && self.epilogue.is_none() { if self.prologue.is_some() && self.epilogue.is_none() {
log::debug!("Assuming unrecovered jump table {:#010X}", next_addr); log::debug!("Assuming unrecovered jump table {:#010X}", next_addr);
self.branches.insert(ins.addr, vec![next_addr]); self.branches.insert(ins_addr, vec![next_addr]);
if self.add_block_start(next_addr) { if self.add_block_start(next_addr) {
executor.push(next_addr, vm.clone_for_return(), true); executor.push(next_addr, vm.clone_for_return(), true);
} }
@ -217,14 +231,14 @@ impl FunctionSlices {
Ok(ExecCbResult::EndBlock) Ok(ExecCbResult::EndBlock)
} }
BranchTarget::Return => { BranchTarget::Return => {
self.blocks.insert(block_start, ins.addr + 4); self.blocks.insert(block_start, Some(ins_addr + 4));
Ok(ExecCbResult::EndBlock) Ok(ExecCbResult::EndBlock)
} }
BranchTarget::Address(addr) => { BranchTarget::Address(addr) => {
// End of block // End of block
self.blocks.insert(block_start, ins.addr + 4); self.blocks.insert(block_start, Some(ins_addr + 4));
self.branches.insert(ins.addr, vec![addr]); self.branches.insert(ins_addr, vec![addr]);
if addr == ins.addr { if addr == ins_addr {
// Infinite loop // Infinite loop
} else if addr >= function_start } else if addr >= function_start
&& matches!(function_end, Some(known_end) if addr < known_end) && matches!(function_end, Some(known_end) if addr < known_end)
@ -233,7 +247,7 @@ impl FunctionSlices {
if self.add_block_start(addr) { if self.add_block_start(addr) {
return Ok(ExecCbResult::Jump(addr)); return Ok(ExecCbResult::Jump(addr));
} }
} else if matches!(section.data_range(ins.addr, ins.addr + 4), Ok(data) if data == [0u8; 4]) } else if matches!(section.data_range(ins_addr.address, ins_addr.address + 4), Ok(data) if data == [0u8; 4])
{ {
// If this branch has zeroed padding after it, assume tail call. // If this branch has zeroed padding after it, assume tail call.
self.function_references.insert(addr); self.function_references.insert(addr);
@ -244,16 +258,16 @@ impl FunctionSlices {
} }
BranchTarget::JumpTable { address, size } => { BranchTarget::JumpTable { address, size } => {
// End of block // End of block
let next_address = ins.addr + 4; let next_address = ins_addr + 4;
self.blocks.insert(block_start, next_address); self.blocks.insert(block_start, Some(next_address));
let (mut entries, size) = uniq_jump_table_entries( let (mut entries, size) = uniq_jump_table_entries(
obj, obj,
address, address,
size, size,
ins.addr, ins_addr,
function_start, function_start,
function_end.unwrap_or_else(|| self.end()), function_end.or_else(|| self.end()),
)?; )?;
if entries.contains(&next_address) if entries.contains(&next_address)
&& !entries.iter().any(|addr| known_functions.contains(addr)) && !entries.iter().any(|addr| known_functions.contains(addr))
@ -266,7 +280,7 @@ impl FunctionSlices {
executor.push(addr, vm.clone_all(), true); executor.push(addr, vm.clone_all(), true);
} }
} }
self.branches.insert(ins.addr, branches); self.branches.insert(ins_addr, branches);
} else { } else {
// If the table doesn't contain the next address, // If the table doesn't contain the next address,
// it could be a function jump table instead // it could be a function jump table instead
@ -277,7 +291,7 @@ impl FunctionSlices {
}, },
StepResult::Branch(branches) => { StepResult::Branch(branches) => {
// End of block // End of block
self.blocks.insert(block_start, ins.addr + 4); self.blocks.insert(block_start, Some(ins_addr + 4));
let mut out_branches = vec![]; let mut out_branches = vec![];
for branch in branches { for branch in branches {
@ -296,12 +310,12 @@ impl FunctionSlices {
} }
} }
BranchTarget::JumpTable { .. } => { BranchTarget::JumpTable { .. } => {
bail!("Conditional jump table unsupported @ {:#010X}", ins.addr); bail!("Conditional jump table unsupported @ {:#010X}", ins_addr);
} }
} }
} }
if !out_branches.is_empty() { if !out_branches.is_empty() {
self.branches.insert(ins.addr, out_branches); self.branches.insert(ins_addr, out_branches);
} }
Ok(ExecCbResult::EndBlock) Ok(ExecCbResult::EndBlock)
} }
@ -311,10 +325,10 @@ impl FunctionSlices {
pub fn analyze( pub fn analyze(
&mut self, &mut self,
obj: &ObjInfo, obj: &ObjInfo,
start: u32, start: SectionAddress,
function_start: u32, function_start: SectionAddress,
function_end: Option<u32>, function_end: Option<SectionAddress>,
known_functions: &BTreeSet<u32>, known_functions: &BTreeSet<SectionAddress>,
) -> Result<bool> { ) -> Result<bool> {
if !self.add_block_start(start) { if !self.add_block_start(start) {
return Ok(true); return Ok(true);
@ -342,8 +356,15 @@ impl FunctionSlices {
// Visit trailing blocks // Visit trailing blocks
if let Some(known_end) = function_end { if let Some(known_end) = function_end {
while self.end() < known_end { loop {
executor.push(self.end(), VM::new_from_obj(obj), true); let Some(end) = self.end() else {
log::warn!("Trailing block analysis failed @ {:#010X}", function_start);
break;
};
if end >= known_end {
break;
}
executor.push(end, VM::new_from_obj(obj), true);
let result = executor.run(obj, |data| { let result = executor.run(obj, |data| {
self.instruction_callback( self.instruction_callback(
data, data,
@ -361,7 +382,7 @@ impl FunctionSlices {
// Sanity check // Sanity check
for (&start, &end) in &self.blocks { for (&start, &end) in &self.blocks {
ensure!(end != 0, "Failed to finalize block @ {start:#010X}"); ensure!(end.is_some(), "Failed to finalize block @ {start:#010X}");
} }
Ok(true) Ok(true)
@ -369,7 +390,11 @@ impl FunctionSlices {
pub fn can_finalize(&self) -> bool { self.possible_blocks.is_empty() } pub fn can_finalize(&self) -> bool { self.possible_blocks.is_empty() }
pub fn finalize(&mut self, obj: &ObjInfo, known_functions: &BTreeSet<u32>) -> Result<()> { pub fn finalize(
&mut self,
obj: &ObjInfo,
known_functions: &BTreeSet<SectionAddress>,
) -> Result<()> {
ensure!(!self.finalized, "Already finalized"); ensure!(!self.finalized, "Already finalized");
ensure!(self.can_finalize(), "Can't finalize"); ensure!(self.can_finalize(), "Can't finalize");
@ -384,49 +409,64 @@ impl FunctionSlices {
} }
} }
let end = self.end(); let Some(end) = self.end() else {
match (obj.sections.at_address(end), obj.sections.at_address(end - 4)) { bail!("Can't finalize function without known end: {:#010X?}", self.start())
(Ok((section_index, section)), Ok((other_section_index, _other_section))) };
if section_index == other_section_index => // TODO: rework to make compatible with relocatable objects
{ if obj.kind == ObjKind::Executable {
// FIXME this is real bad match (
if !self.has_conditional_blr { (end.section, &obj.sections[end.section]),
if let Some(ins) = disassemble(section, end - 4) { obj.sections.at_address(end.address - 4),
if ins.op == Opcode::B ) {
&& self.function_references.contains(&ins.branch_dest().unwrap()) ((section_index, section), Ok((other_section_index, _other_section)))
{ if section_index == other_section_index =>
for branches in self.branches.values() { {
if branches.len() > 1 // FIXME this is real bad
&& branches.contains(self.blocks.last_key_value().unwrap().0) if !self.has_conditional_blr {
if let Some(ins) = disassemble(section, end.address - 4) {
if ins.op == Opcode::B {
if let Some(target) = ins
.branch_dest()
.and_then(|addr| section_address_for(obj, end - 4, addr))
{ {
self.has_conditional_blr = true; if self.function_references.contains(&target) {
for branches in self.branches.values() {
if branches.len() > 1
&& branches.contains(
self.blocks.last_key_value().unwrap().0,
)
{
self.has_conditional_blr = true;
}
}
}
} }
} }
} }
} }
}
// MWCC optimization sometimes leaves an unreachable blr // MWCC optimization sometimes leaves an unreachable blr
// after generating a conditional blr in the function. // after generating a conditional blr in the function.
if self.has_conditional_blr if self.has_conditional_blr
&& matches!(disassemble(section, end - 4), Some(ins) if !ins.is_blr()) && matches!(disassemble(section, end.address - 4), Some(ins) if !ins.is_blr())
&& matches!(disassemble(section, end), Some(ins) if ins.is_blr()) && matches!(disassemble(section, end.address), Some(ins) if ins.is_blr())
&& !known_functions.contains(&end) && !known_functions.contains(&end)
{ {
log::trace!("Found trailing blr @ {:#010X}, merging with function", end); log::trace!("Found trailing blr @ {:#010X}, merging with function", end);
self.blocks.insert(end, end + 4); self.blocks.insert(end, Some(end + 4));
} }
// Some functions with rfi also include a trailing nop // Some functions with rfi also include a trailing nop
if self.has_rfi if self.has_rfi
&& matches!(disassemble(section, end), Some(ins) if is_nop(&ins)) && matches!(disassemble(section, end.address), Some(ins) if is_nop(&ins))
&& !known_functions.contains(&end) && !known_functions.contains(&end)
{ {
log::trace!("Found trailing nop @ {:#010X}, merging with function", end); log::trace!("Found trailing nop @ {:#010X}, merging with function", end);
self.blocks.insert(end, end + 4); self.blocks.insert(end, Some(end + 4));
}
} }
_ => {}
} }
_ => {}
} }
self.finalized = true; self.finalized = true;
@ -437,15 +477,20 @@ impl FunctionSlices {
pub fn check_tail_call( pub fn check_tail_call(
&mut self, &mut self,
obj: &ObjInfo, obj: &ObjInfo,
addr: u32, addr: SectionAddress,
function_start: u32, function_start: SectionAddress,
function_end: u32, function_end: Option<SectionAddress>,
known_functions: &BTreeSet<u32>, known_functions: &BTreeSet<SectionAddress>,
) -> TailCallResult { ) -> TailCallResult {
// If jump target is already a known block or within known function bounds, not a tail call. // If jump target is already a known block or within known function bounds, not a tail call.
if self.blocks.contains_key(&addr) || (addr >= function_start && addr < function_end) { if self.blocks.contains_key(&addr) {
return TailCallResult::Not; return TailCallResult::Not;
} }
if let Some(function_end) = function_end {
if addr >= function_start && addr < function_end {
return TailCallResult::Not;
}
}
// If there's a prologue in the current function, not a tail call. // If there's a prologue in the current function, not a tail call.
if self.prologue.is_some() { if self.prologue.is_some() {
return TailCallResult::Not; return TailCallResult::Not;
@ -455,20 +500,18 @@ impl FunctionSlices {
return TailCallResult::Is; return TailCallResult::Is;
} }
// If the jump target is in a different section, known tail call. // If the jump target is in a different section, known tail call.
let (_, target_section) = match obj.sections.at_address(addr) { if addr.section != function_start.section {
Ok(section) => section,
Err(e) => return TailCallResult::Error(e),
};
if !target_section.contains(function_start) {
return TailCallResult::Is; return TailCallResult::Is;
} }
// If the jump target has 0'd padding before it, known tail call. // If the jump target has 0'd padding before it, known tail call.
if matches!(target_section.data_range(addr - 4, addr), Ok(data) if data == [0u8; 4]) { let target_section = &obj.sections[addr.section];
if matches!(target_section.data_range(addr.address - 4, addr.address), Ok(data) if data == [0u8; 4])
{
return TailCallResult::Is; return TailCallResult::Is;
} }
// If we're not sure where the function ends yet, mark as possible tail call. // If we're not sure where the function ends yet, mark as possible tail call.
// let end = self.end(); // let end = self.end();
if function_end == 0 { if function_end.is_none() {
return TailCallResult::Possible; return TailCallResult::Possible;
} }
// If jump target is known to be a function, or there's a function in between // If jump target is known to be a function, or there's a function in between
@ -483,8 +526,7 @@ impl FunctionSlices {
function_references: self.function_references.clone(), function_references: self.function_references.clone(),
..Default::default() ..Default::default()
}; };
if let Ok(result) = if let Ok(result) = slices.analyze(obj, addr, function_start, function_end, known_functions)
slices.analyze(obj, addr, function_start, Some(function_end), known_functions)
{ {
// If analysis failed, assume tail call. // If analysis failed, assume tail call.
if !result { if !result {
@ -492,23 +534,28 @@ impl FunctionSlices {
return TailCallResult::Is; return TailCallResult::Is;
} }
// If control flow jumps below the entry point, not a tail call. // If control flow jumps below the entry point, not a tail call.
let start = slices.start(); let start = slices.start().unwrap();
if start < addr { if start < addr {
log::trace!("Tail call possibility eliminated: {:#010X} < {:#010X}", start, addr); log::trace!("Tail call possibility eliminated: {:#010X} < {:#010X}", start, addr);
return TailCallResult::Not; return TailCallResult::Not;
} }
// If control flow includes another possible tail call, we know both are not tail calls. // If control flow includes another possible tail call, we know both are not tail calls.
let end = slices.end(); if let Some(end) = slices.end() {
let other_blocks = // TODO idk if wrapping this is right
self.possible_blocks.range(start + 4..end).cloned().collect::<Vec<u32>>(); let other_blocks = self
if !other_blocks.is_empty() { .possible_blocks
for other_addr in other_blocks { .range(start + 4..end)
log::trace!("Logically eliminating {:#010X}", other_addr); .cloned()
self.possible_blocks.remove(&other_addr); .collect::<Vec<SectionAddress>>();
// self.add_block_start(oth); if !other_blocks.is_empty() {
for other_addr in other_blocks {
log::trace!("Logically eliminating {:#010X}", other_addr);
self.possible_blocks.remove(&other_addr);
// self.add_block_start(oth);
}
log::trace!("While analyzing {:#010X}", addr);
return TailCallResult::Not;
} }
log::trace!("While analyzing {:#010X}", addr);
return TailCallResult::Not;
} }
// If we discovered a function prologue, known tail call. // If we discovered a function prologue, known tail call.
if slices.prologue.is_some() { if slices.prologue.is_some() {
@ -524,7 +571,10 @@ impl FunctionSlices {
loop { loop {
let ((first_begin, first_end), (second_begin, second_end)) = let ((first_begin, first_end), (second_begin, second_end)) =
match (iter.next(), iter.peek()) { match (iter.next(), iter.peek()) {
(Some((&b1s, &b1e)), Some(&(&b2s, &b2e))) => ((b1s, b1e), (b2s, b2e)), (Some((&b1s, &Some(b1e))), Some(&(&b2s, &Some(b2e)))) => {
((b1s, b1e), (b2s, b2e))
}
(Some(_), Some(_)) => continue,
_ => break None, _ => break None,
}; };
if second_begin > first_end { if second_begin > first_end {

View File

@ -1,5 +1,5 @@
use std::{ use std::{
collections::{btree_map::Entry, BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
mem::take, mem::take,
}; };
@ -8,25 +8,26 @@ use ppc750cl::Opcode;
use crate::{ use crate::{
analysis::{ analysis::{
cfa::SectionAddress,
executor::{ExecCbData, ExecCbResult, Executor}, executor::{ExecCbData, ExecCbResult, Executor},
uniq_jump_table_entries, relocation_target_for, uniq_jump_table_entries,
vm::{is_store_op, BranchTarget, GprValue, StepResult, VM}, vm::{is_store_op, BranchTarget, GprValue, StepResult, VM},
}, },
obj::{ obj::{
ObjDataKind, ObjInfo, ObjReloc, ObjRelocKind, ObjSection, ObjSectionKind, ObjSymbol, ObjDataKind, ObjInfo, ObjKind, ObjReloc, ObjRelocKind, ObjSection, ObjSectionKind,
ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind,
}, },
}; };
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum Relocation { pub enum Relocation {
Ha(u32), Ha(SectionAddress),
Hi(u32), Hi(SectionAddress),
Lo(u32), Lo(SectionAddress),
Sda21(u32), Sda21(SectionAddress),
Rel14(u32), Rel14(SectionAddress),
Rel24(u32), Rel24(SectionAddress),
Absolute(u32), Absolute(SectionAddress),
} }
#[derive(Debug)] #[derive(Debug)]
@ -42,30 +43,29 @@ pub enum DataKind {
} }
pub struct Tracker { pub struct Tracker {
processed_functions: BTreeSet<u32>, processed_functions: BTreeSet<SectionAddress>,
sda2_base: u32, // r2 sda2_base: Option<u32>, // r2
sda_base: u32, // r13 sda_base: Option<u32>, // r13
pub relocations: BTreeMap<u32, Relocation>, pub relocations: BTreeMap<SectionAddress, Relocation>,
data_types: BTreeMap<u32, DataKind>, data_types: BTreeMap<SectionAddress, DataKind>,
stack_address: Option<u32>, stack_address: Option<u32>,
stack_end: Option<u32>, stack_end: Option<u32>,
db_stack_addr: Option<u32>, db_stack_addr: Option<u32>,
arena_lo: Option<u32>, arena_lo: Option<u32>,
arena_hi: Option<u32>, arena_hi: Option<u32>,
pub ignore_addresses: BTreeSet<u32>, pub known_relocations: BTreeSet<SectionAddress>,
pub known_relocations: BTreeSet<u32>,
stores_to: BTreeSet<u32>, // for determining data vs rodata, sdata(2)/sbss(2) stores_to: BTreeSet<SectionAddress>, // for determining data vs rodata, sdata(2)/sbss(2)
sda_to: BTreeSet<u32>, // for determining data vs sdata sda_to: BTreeSet<SectionAddress>, // for determining data vs sdata
hal_to: BTreeSet<u32>, // for determining data vs sdata hal_to: BTreeSet<SectionAddress>, // for determining data vs sdata
} }
impl Tracker { impl Tracker {
pub fn new(obj: &ObjInfo) -> Tracker { pub fn new(obj: &ObjInfo) -> Tracker {
Self { Self {
processed_functions: Default::default(), processed_functions: Default::default(),
sda2_base: obj.sda2_base.unwrap(), sda2_base: obj.sda2_base,
sda_base: obj.sda_base.unwrap(), sda_base: obj.sda_base,
relocations: Default::default(), relocations: Default::default(),
data_types: Default::default(), data_types: Default::default(),
stack_address: obj.stack_address, stack_address: obj.stack_address,
@ -81,7 +81,6 @@ impl Tracker {
.arena_lo .arena_lo
.or_else(|| obj.db_stack_addr.map(|db_stack_addr| (db_stack_addr + 0x1F) & !0x1F)), .or_else(|| obj.db_stack_addr.map(|db_stack_addr| (db_stack_addr + 0x1F) & !0x1F)),
arena_hi: Some(obj.arena_hi.unwrap_or(0x81700000)), arena_hi: Some(obj.arena_hi.unwrap_or(0x81700000)),
ignore_addresses: Default::default(),
known_relocations: Default::default(), known_relocations: Default::default(),
stores_to: Default::default(), stores_to: Default::default(),
sda_to: Default::default(), sda_to: Default::default(),
@ -98,20 +97,24 @@ impl Tracker {
.filter(|(_, s)| matches!(s.kind, ObjSectionKind::Data | ObjSectionKind::ReadOnlyData)) .filter(|(_, s)| matches!(s.kind, ObjSectionKind::Data | ObjSectionKind::ReadOnlyData))
{ {
log::debug!("Processing section {}, address {:#X}", section_index, section.address); log::debug!("Processing section {}, address {:#X}", section_index, section.address);
self.process_data(obj, section)?; self.process_data(obj, section_index, section)?;
} }
Ok(()) Ok(())
} }
fn process_code(&mut self, obj: &ObjInfo) -> Result<()> { fn process_code(&mut self, obj: &ObjInfo) -> Result<()> {
self.process_function_by_address(obj, obj.entry as u32)?; if let Some(entry) = obj.entry {
let (section_index, _) = obj.sections.at_address(entry as u32)?;
let entry_addr = SectionAddress::new(section_index, entry as u32);
self.process_function_by_address(obj, entry_addr)?;
}
for (section_index, _) in obj.sections.by_kind(ObjSectionKind::Code) { for (section_index, _) in obj.sections.by_kind(ObjSectionKind::Code) {
for (_, symbol) in obj for (_, symbol) in obj
.symbols .symbols
.for_section(section_index) .for_section(section_index)
.filter(|(_, symbol)| symbol.kind == ObjSymbolKind::Function && symbol.size_known) .filter(|(_, symbol)| symbol.kind == ObjSymbolKind::Function && symbol.size_known)
{ {
let addr = symbol.address as u32; let addr = SectionAddress::new(section_index, symbol.address as u32);
if !self.processed_functions.insert(addr) { if !self.processed_functions.insert(addr) {
continue; continue;
} }
@ -121,15 +124,14 @@ impl Tracker {
Ok(()) Ok(())
} }
fn process_function_by_address(&mut self, obj: &ObjInfo, addr: u32) -> Result<()> { fn process_function_by_address(&mut self, obj: &ObjInfo, addr: SectionAddress) -> Result<()> {
if self.processed_functions.contains(&addr) { if self.processed_functions.contains(&addr) {
return Ok(()); return Ok(());
} }
self.processed_functions.insert(addr); self.processed_functions.insert(addr);
let (section_index, _) = obj.sections.at_address(addr)?;
if let Some((_, symbol)) = obj if let Some((_, symbol)) = obj
.symbols .symbols
.at_section_address(section_index, addr) .at_section_address(addr.section, addr.address)
.find(|(_, symbol)| symbol.kind == ObjSymbolKind::Function && symbol.size_known) .find(|(_, symbol)| symbol.kind == ObjSymbolKind::Function && symbol.size_known)
{ {
self.process_function(obj, symbol)?; self.process_function(obj, symbol)?;
@ -143,13 +145,12 @@ impl Tracker {
&mut self, &mut self,
data: ExecCbData, data: ExecCbData,
obj: &ObjInfo, obj: &ObjInfo,
function_start: u32, function_start: SectionAddress,
function_end: u32, function_end: SectionAddress,
possible_missed_branches: &mut BTreeMap<u32, Box<VM>>, possible_missed_branches: &mut BTreeMap<SectionAddress, Box<VM>>,
) -> Result<ExecCbResult<()>> { ) -> Result<ExecCbResult<()>> {
let ExecCbData { executor, vm, result, section_index: _, section: _, ins, block_start: _ } = let ExecCbData { executor, vm, result, ins_addr, section: _, ins, block_start: _ } = data;
data; let is_function_addr = |addr: SectionAddress| addr >= function_start && addr < function_end;
let is_function_addr = |addr: u32| addr >= function_start && addr < function_end;
match result { match result {
StepResult::Continue => { StepResult::Continue => {
@ -159,26 +160,25 @@ impl Tracker {
let source = ins.field_rA(); let source = ins.field_rA();
let target = ins.field_rD(); let target = ins.field_rD();
if let GprValue::Constant(value) = vm.gpr[target].value { if let GprValue::Constant(value) = vm.gpr[target].value {
if self.is_valid_address(obj, ins.addr, value) { if let Some(value) = self.is_valid_address(obj, ins_addr, value) {
if (source == 2 if (source == 2
&& vm.gpr[2].value == GprValue::Constant(self.sda2_base)) && matches!(self.sda2_base, Some(v) if vm.gpr[2].value == GprValue::Constant(v)))
|| (source == 13 || (source == 13
&& vm.gpr[13].value == GprValue::Constant(self.sda_base)) && matches!(self.sda_base, Some(v) if vm.gpr[13].value == GprValue::Constant(v)))
{ {
self.relocations.insert(ins.addr, Relocation::Sda21(value)); self.relocations.insert(ins_addr, Relocation::Sda21(value));
self.sda_to.insert(value); self.sda_to.insert(value);
} else if let (Some(hi_addr), Some(lo_addr)) = } else if let (Some(hi_addr), Some(lo_addr)) =
(vm.gpr[target].hi_addr, vm.gpr[target].lo_addr) (vm.gpr[target].hi_addr, vm.gpr[target].lo_addr)
{ {
let hi_reloc = self.relocations.get(&hi_addr.get()).cloned(); let hi_reloc = self.relocations.get(&hi_addr).cloned();
if hi_reloc.is_none() { if hi_reloc.is_none() {
self.relocations debug_assert_ne!(value, SectionAddress::new(usize::MAX, 0));
.insert(hi_addr.get(), Relocation::Ha(value)); self.relocations.insert(hi_addr, Relocation::Ha(value));
} }
let lo_reloc = self.relocations.get(&lo_addr.get()).cloned(); let lo_reloc = self.relocations.get(&lo_addr).cloned();
if lo_reloc.is_none() { if lo_reloc.is_none() {
self.relocations self.relocations.insert(lo_addr, Relocation::Lo(value));
.insert(lo_addr.get(), Relocation::Lo(value));
} }
self.hal_to.insert(value); self.hal_to.insert(value);
} }
@ -189,19 +189,17 @@ impl Tracker {
Opcode::Ori => { Opcode::Ori => {
let target = ins.field_rA(); let target = ins.field_rA();
if let GprValue::Constant(value) = vm.gpr[target].value { if let GprValue::Constant(value) = vm.gpr[target].value {
if self.is_valid_address(obj, ins.addr, value) { if let Some(value) = self.is_valid_address(obj, ins_addr, value) {
if let (Some(hi_addr), Some(lo_addr)) = if let (Some(hi_addr), Some(lo_addr)) =
(vm.gpr[target].hi_addr, vm.gpr[target].lo_addr) (vm.gpr[target].hi_addr, vm.gpr[target].lo_addr)
{ {
let hi_reloc = self.relocations.get(&hi_addr.get()).cloned(); let hi_reloc = self.relocations.get(&hi_addr).cloned();
if hi_reloc.is_none() { if hi_reloc.is_none() {
self.relocations self.relocations.insert(hi_addr, Relocation::Hi(value));
.insert(hi_addr.get(), Relocation::Hi(value));
} }
let lo_reloc = self.relocations.get(&lo_addr.get()).cloned(); let lo_reloc = self.relocations.get(&lo_addr).cloned();
if lo_reloc.is_none() { if lo_reloc.is_none() {
self.relocations self.relocations.insert(lo_addr, Relocation::Lo(value));
.insert(lo_addr.get(), Relocation::Lo(value));
} }
self.hal_to.insert(value); self.hal_to.insert(value);
} }
@ -213,34 +211,38 @@ impl Tracker {
Ok(ExecCbResult::Continue) Ok(ExecCbResult::Continue)
} }
StepResult::LoadStore { address, source, source_reg } => { StepResult::LoadStore { address, source, source_reg } => {
if self.is_valid_address(obj, ins.addr, address) { if let Some(address) = self.is_valid_address(obj, ins_addr, address.address) {
if (source_reg == 2 && source.value == GprValue::Constant(self.sda2_base)) if (source_reg == 2
|| (source_reg == 13 && source.value == GprValue::Constant(self.sda_base)) && matches!(self.sda2_base, Some(v) if source.value == GprValue::Constant(v)))
|| (source_reg == 13
&& matches!(self.sda_base, Some(v) if source.value == GprValue::Constant(v)))
{ {
self.relocations.insert(ins.addr, Relocation::Sda21(address)); self.relocations.insert(ins_addr, Relocation::Sda21(address));
self.sda_to.insert(address); self.sda_to.insert(address);
} else { } else {
match (source.hi_addr, source.lo_addr) { match (source.hi_addr, source.lo_addr) {
(Some(hi_addr), None) => { (Some(hi_addr), None) => {
let hi_reloc = self.relocations.get(&hi_addr.get()).cloned(); let hi_reloc = self.relocations.get(&hi_addr).cloned();
if hi_reloc.is_none() { if hi_reloc.is_none() {
self.relocations.insert(hi_addr.get(), Relocation::Ha(address)); debug_assert_ne!(address, SectionAddress::new(usize::MAX, 0));
self.relocations.insert(hi_addr, Relocation::Ha(address));
} }
if hi_reloc.is_none() if hi_reloc.is_none()
|| matches!(hi_reloc, Some(Relocation::Ha(v)) if v == address) || matches!(hi_reloc, Some(Relocation::Ha(v)) if v == address)
{ {
self.relocations.insert(ins.addr, Relocation::Lo(address)); self.relocations.insert(ins_addr, Relocation::Lo(address));
} }
self.hal_to.insert(address); self.hal_to.insert(address);
} }
(Some(hi_addr), Some(lo_addr)) => { (Some(hi_addr), Some(lo_addr)) => {
let hi_reloc = self.relocations.get(&hi_addr.get()).cloned(); let hi_reloc = self.relocations.get(&hi_addr).cloned();
if hi_reloc.is_none() { if hi_reloc.is_none() {
self.relocations.insert(hi_addr.get(), Relocation::Ha(address)); debug_assert_ne!(address, SectionAddress::new(usize::MAX, 0));
self.relocations.insert(hi_addr, Relocation::Ha(address));
} }
let lo_reloc = self.relocations.get(&lo_addr.get()).cloned(); let lo_reloc = self.relocations.get(&lo_addr).cloned();
if lo_reloc.is_none() { if lo_reloc.is_none() {
self.relocations.insert(lo_addr.get(), Relocation::Lo(address)); self.relocations.insert(lo_addr, Relocation::Lo(address));
} }
self.hal_to.insert(address); self.hal_to.insert(address);
} }
@ -256,22 +258,22 @@ impl Tracker {
} }
StepResult::Illegal => bail!( StepResult::Illegal => bail!(
"Illegal instruction hit @ {:#010X} (function {:#010X}-{:#010X})", "Illegal instruction hit @ {:#010X} (function {:#010X}-{:#010X})",
ins.addr, ins_addr,
function_start, function_start,
function_end function_end
), ),
StepResult::Jump(target) => match target { StepResult::Jump(target) => match target {
BranchTarget::Unknown | BranchTarget::Return => Ok(ExecCbResult::EndBlock), BranchTarget::Unknown | BranchTarget::Return => Ok(ExecCbResult::EndBlock),
BranchTarget::Address(addr) => { BranchTarget::Address(addr) => {
let next_addr = ins.addr + 4; let next_addr = ins_addr + 4;
if next_addr < function_end { if next_addr < function_end {
possible_missed_branches.insert(ins.addr + 4, vm.clone_all()); possible_missed_branches.insert(ins_addr + 4, vm.clone_all());
} }
if is_function_addr(addr) { if is_function_addr(addr) {
Ok(ExecCbResult::Jump(addr)) Ok(ExecCbResult::Jump(addr))
} else { } else {
if ins.is_direct_branch() { if ins.is_direct_branch() {
self.relocations.insert(ins.addr, Relocation::Rel24(addr)); self.relocations.insert(ins_addr, Relocation::Rel24(addr));
} }
Ok(ExecCbResult::EndBlock) Ok(ExecCbResult::EndBlock)
} }
@ -281,9 +283,9 @@ impl Tracker {
obj, obj,
address, address,
size, size,
ins.addr, ins_addr,
function_start, function_start,
function_end, Some(function_end),
)?; )?;
for target in entries { for target in entries {
if is_function_addr(target) { if is_function_addr(target) {
@ -299,7 +301,7 @@ impl Tracker {
BranchTarget::Unknown | BranchTarget::Return => {} BranchTarget::Unknown | BranchTarget::Return => {}
BranchTarget::Address(addr) => { BranchTarget::Address(addr) => {
if branch.link || !is_function_addr(addr) { if branch.link || !is_function_addr(addr) {
self.relocations.insert(ins.addr, match ins.op { self.relocations.insert(ins_addr, match ins.op {
Opcode::B => Relocation::Rel24(addr), Opcode::B => Relocation::Rel24(addr),
Opcode::Bc => Relocation::Rel14(addr), Opcode::Bc => Relocation::Rel14(addr),
_ => continue, _ => continue,
@ -309,7 +311,7 @@ impl Tracker {
} }
} }
BranchTarget::JumpTable { .. } => { BranchTarget::JumpTable { .. } => {
bail!("Conditional jump table unsupported @ {:#010X}", ins.addr) bail!("Conditional jump table unsupported @ {:#010X}", ins_addr)
} }
} }
} }
@ -319,19 +321,18 @@ impl Tracker {
} }
pub fn process_function(&mut self, obj: &ObjInfo, symbol: &ObjSymbol) -> Result<()> { pub fn process_function(&mut self, obj: &ObjInfo, symbol: &ObjSymbol) -> Result<()> {
let function_start = symbol.address as u32; let Some(section_index) = symbol.section else {
let function_end = (symbol.address + symbol.size) as u32; bail!("Function '{}' missing section", symbol.name)
};
let function_start = SectionAddress::new(section_index, symbol.address as u32);
let function_end = function_start + symbol.size as u32;
// The compiler can sometimes create impossible-to-reach branches, // The compiler can sometimes create impossible-to-reach branches,
// but we still want to track them. // but we still want to track them.
let mut possible_missed_branches = BTreeMap::new(); let mut possible_missed_branches = BTreeMap::new();
let mut executor = Executor::new(obj); let mut executor = Executor::new(obj);
executor.push( executor.push(function_start, VM::new_with_base(self.sda2_base, self.sda_base), false);
symbol.address as u32,
VM::new_with_base(self.sda2_base, self.sda_base),
false,
);
loop { loop {
executor.run(obj, |data| -> Result<ExecCbResult<()>> { executor.run(obj, |data| -> Result<ExecCbResult<()>> {
self.instruction_callback( self.instruction_callback(
@ -348,11 +349,8 @@ impl Tracker {
} }
let mut added = false; let mut added = false;
for (addr, vm) in take(&mut possible_missed_branches) { for (addr, vm) in take(&mut possible_missed_branches) {
let (section_index, section) = match obj.sections.at_address(addr) { let section = &obj.sections[addr.section];
Ok(section) => section, if !executor.visited(section.address as u32, addr) {
Err(_) => continue,
};
if !executor.visited(section_index, section.address as u32, addr) {
executor.push(addr, vm, true); executor.push(addr, vm, true);
added = true; added = true;
} }
@ -364,11 +362,16 @@ impl Tracker {
Ok(()) Ok(())
} }
fn process_data(&mut self, obj: &ObjInfo, section: &ObjSection) -> Result<()> { fn process_data(
let mut addr = section.address as u32; &mut self,
obj: &ObjInfo,
section_index: usize,
section: &ObjSection,
) -> Result<()> {
let mut addr = SectionAddress::new(section_index, section.address as u32);
for chunk in section.data.chunks_exact(4) { for chunk in section.data.chunks_exact(4) {
let value = u32::from_be_bytes(chunk.try_into()?); let value = u32::from_be_bytes(chunk.try_into()?);
if self.is_valid_address(obj, addr, value) { if let Some(value) = self.is_valid_address(obj, addr, value) {
self.relocations.insert(addr, Relocation::Absolute(value)); self.relocations.insert(addr, Relocation::Absolute(value));
} }
addr += 4; addr += 4;
@ -376,36 +379,53 @@ impl Tracker {
Ok(()) Ok(())
} }
fn is_valid_address(&self, obj: &ObjInfo, from: u32, addr: u32) -> bool { fn is_valid_address(
if self.ignore_addresses.contains(&addr) { &self,
return false; obj: &ObjInfo,
} from: SectionAddress,
if let Some((&start, &end)) = obj.blocked_ranges.range(..=from).next_back() { addr: u32,
if from >= start && from < end { ) -> Option<SectionAddress> {
return false; if let Some((&start, &end)) = obj.blocked_ranges.range(..=from.address).next_back() {
if from.address >= start && from.address < end {
return None;
} }
} }
// Check for an existing relocation
if let Some(target) = relocation_target_for(obj, from, None).ok().flatten() {
if obj.kind == ObjKind::Executable {
debug_assert_eq!(target.address, addr);
}
return Some(target);
}
// Remainder of this function is for executable objects only
if obj.kind == ObjKind::Relocatable {
return None;
}
if self.known_relocations.contains(&from) { if self.known_relocations.contains(&from) {
return true; let section_index =
obj.sections.at_address(addr).ok().map(|(idx, _)| idx).unwrap_or(usize::MAX);
return Some(SectionAddress::new(section_index, addr));
} }
if self.stack_address == Some(addr) if self.stack_address == Some(addr)
|| self.stack_end == Some(addr) || self.stack_end == Some(addr)
|| self.db_stack_addr == Some(addr) || self.db_stack_addr == Some(addr)
|| self.arena_lo == Some(addr) || self.arena_lo == Some(addr)
|| self.arena_hi == Some(addr) || self.arena_hi == Some(addr)
|| self.sda2_base == addr || self.sda2_base == Some(addr)
|| self.sda_base == addr || self.sda_base == Some(addr)
{ {
return true; return Some(SectionAddress::new(usize::MAX, addr));
} }
// if addr > 0x80000000 && addr < 0x80003100 { // if addr > 0x80000000 && addr < 0x80003100 {
// return true; // return true;
// } // }
if let Ok((_, section)) = obj.sections.at_address(addr) { if let Ok((section_index, section)) = obj.sections.at_address(addr) {
// References to code sections will never be unaligned // References to code sections will never be unaligned
return section.kind != ObjSectionKind::Code || addr & 3 == 0; if section.kind != ObjSectionKind::Code || addr & 3 == 0 {
return Some(SectionAddress::new(section_index, addr));
}
} }
false None
} }
fn special_symbol( fn special_symbol(
@ -437,8 +457,8 @@ impl Tracker {
.or_else(|| check_symbol(self.arena_lo, "__ArenaLo")) .or_else(|| check_symbol(self.arena_lo, "__ArenaLo"))
.or_else(|| check_symbol(self.arena_hi, "__ArenaHi")) .or_else(|| check_symbol(self.arena_hi, "__ArenaHi"))
.or_else(|| check_symbol(self.db_stack_addr, "_db_stack_addr")) .or_else(|| check_symbol(self.db_stack_addr, "_db_stack_addr"))
.or_else(|| check_symbol(Some(self.sda2_base), "_SDA2_BASE_")) .or_else(|| check_symbol(self.sda2_base, "_SDA2_BASE_"))
.or_else(|| check_symbol(Some(self.sda_base), "_SDA_BASE_")) .or_else(|| check_symbol(self.sda_base, "_SDA_BASE_"))
} }
pub fn apply(&self, obj: &mut ObjInfo, replace: bool) -> Result<()> { pub fn apply(&self, obj: &mut ObjInfo, replace: bool) -> Result<()> {
@ -454,14 +474,14 @@ impl Tracker {
section.name = new_name; section.name = new_name;
} }
for (_, section) in obj.sections.iter_mut() { for (section_index, section) in obj.sections.iter_mut() {
if !section.section_known { if !section.section_known {
if section.kind == ObjSectionKind::Code { if section.kind == ObjSectionKind::Code {
apply_section_name(section, ".text"); apply_section_name(section, ".text");
continue; continue;
} }
let start = section.address as u32; let start = SectionAddress::new(section_index, section.address as u32);
let end = (section.address + section.size) as u32; let end = start + section.size as u32;
if self.sda_to.range(start..end).next().is_some() { if self.sda_to.range(start..end).next().is_some() {
if self.stores_to.range(start..end).next().is_some() { if self.stores_to.range(start..end).next().is_some() {
if section.kind == ObjSectionKind::Bss { if section.kind == ObjSectionKind::Bss {
@ -488,11 +508,6 @@ impl Tracker {
} }
} }
let mut relocation_maps = Vec::new();
for (_, section) in obj.sections.iter() {
relocation_maps.push(section.build_relocation_map()?);
}
for (addr, reloc) in &self.relocations { for (addr, reloc) in &self.relocations {
let addr = *addr; let addr = *addr;
let (reloc_kind, target) = match *reloc { let (reloc_kind, target) = match *reloc {
@ -516,89 +531,67 @@ impl Tracker {
DataKind::Double => ObjDataKind::Double, DataKind::Double => ObjDataKind::Double,
}) })
.unwrap_or_default(); .unwrap_or_default();
let (target_symbol, addend) = let (target_symbol, addend) = if let Some(symbol) =
if let Some(symbol) = self.special_symbol(obj, target, reloc_kind) { self.special_symbol(obj, target.address, reloc_kind)
(symbol, 0) {
} else { (symbol, 0)
let (target_section_index, _) = match obj.sections.iter().find(|&(_, s)| { } else if let Some((symbol_idx, symbol)) =
target >= s.address as u32 && target < (s.address + s.size) as u32 obj.symbols.for_relocation(target, reloc_kind)?
}) { {
Some(v) => v, let symbol_address = symbol.address;
None => continue, // TODO meh
}; if data_kind != ObjDataKind::Unknown
if let Some((symbol_idx, symbol)) = && symbol.data_kind == ObjDataKind::Unknown
obj.symbols.for_relocation(target, reloc_kind)? && symbol_address as u32 == target.address
{ {
let symbol_address = symbol.address; obj.symbols.replace(symbol_idx, ObjSymbol { data_kind, ..symbol.clone() })?;
// TODO meh
if data_kind != ObjDataKind::Unknown
&& symbol.data_kind == ObjDataKind::Unknown
&& symbol_address as u32 == target
{
obj.symbols
.replace(symbol_idx, ObjSymbol { data_kind, ..symbol.clone() })?;
}
(symbol_idx, target as i64 - symbol_address as i64)
} else {
// Create a new label
let symbol_idx = obj.symbols.add_direct(ObjSymbol {
name: format!("lbl_{:08X}", target),
demangled_name: None,
address: target as u64,
section: Some(target_section_index),
size: 0,
size_known: false,
flags: Default::default(),
kind: Default::default(),
align: None,
data_kind,
})?;
(symbol_idx, 0)
}
};
let reloc = ObjReloc {
kind: reloc_kind,
address: addr as u64,
target_symbol,
addend,
module: None,
};
let (section_index, section) =
match obj.sections.iter_mut().find(|(_, s)| s.contains(addr)) {
Some(v) => v,
None => bail!(
"Failed to locate source section for relocation @ {:#010X} {:#010X?}",
addr,
reloc
),
};
let reloc_map = &mut relocation_maps[section_index];
match reloc_map.entry(addr) {
Entry::Vacant(e) => {
e.insert(section.relocations.len());
section.relocations.push(reloc);
} }
Entry::Occupied(e) => { (symbol_idx, target.address as i64 - symbol_address as i64)
let reloc_symbol = &obj.symbols[reloc.target_symbol]; } else {
if reloc_symbol.name != "_unresolved" { // Create a new label
let v = &mut section.relocations[*e.get()]; let name = if obj.module_id == 0 {
let iter_symbol = &obj.symbols[v.target_symbol]; format!("lbl_{:08X}", target.address)
if iter_symbol.address as i64 + v.addend } else {
!= reloc_symbol.address as i64 + reloc.addend format!(
{ "lbl_{}_{}_{:X}",
bail!( obj.module_id,
"Conflicting relocations (target {:#010X}): {:#010X?} ({}) != {:#010X?} ({})", obj.sections[target.section].name.trim_start_matches('.'),
target, target.address
v, )
iter_symbol.name, };
reloc, let symbol_idx = obj.symbols.add_direct(ObjSymbol {
reloc_symbol.name name,
); demangled_name: None,
} address: target.address as u64,
if replace { section: Some(target.section),
*v = reloc; size: 0,
} size_known: false,
flags: Default::default(),
kind: Default::default(),
align: None,
data_kind,
})?;
(symbol_idx, 0)
};
let reloc = ObjReloc { kind: reloc_kind, target_symbol, addend, module: None };
let section = &mut obj.sections[addr.section];
if replace {
section.relocations.replace(addr.address, reloc);
} else if let Err(e) = section.relocations.insert(addr.address, reloc.clone()) {
let reloc_symbol = &obj.symbols[target_symbol];
if reloc_symbol.name != "_unresolved" {
let iter_symbol = &obj.symbols[e.value.target_symbol];
if iter_symbol.address as i64 + e.value.addend
!= reloc_symbol.address as i64 + addend
{
bail!(
"Conflicting relocations (target {:#010X}): {:#010X?} ({}) != {:#010X?} ({})",
target,
e.value,
iter_symbol.name,
reloc,
reloc_symbol.name
);
} }
} }
} }

View File

@ -2,7 +2,10 @@ use std::num::NonZeroU32;
use ppc750cl::{Argument, Ins, Opcode, GPR}; use ppc750cl::{Argument, Ins, Opcode, GPR};
use crate::obj::ObjInfo; use crate::{
analysis::{cfa::SectionAddress, relocation_target_for},
obj::{ObjInfo, ObjKind},
};
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub enum GprValue { pub enum GprValue {
@ -11,6 +14,8 @@ pub enum GprValue {
Unknown, Unknown,
/// GPR value is a constant /// GPR value is a constant
Constant(u32), Constant(u32),
/// GPR value is a known relocated address
Address(SectionAddress),
/// Comparison result (CR field) /// Comparison result (CR field)
ComparisonResult(u8), ComparisonResult(u8),
/// GPR value is within a range /// GPR value is within a range
@ -24,9 +29,9 @@ pub struct Gpr {
/// The current calculated value /// The current calculated value
pub value: GprValue, pub value: GprValue,
/// Address that loads the hi part of this GPR /// Address that loads the hi part of this GPR
pub hi_addr: Option<NonZeroU32>, pub hi_addr: Option<SectionAddress>,
/// Address that loads the lo part of this GPR /// Address that loads the lo part of this GPR
pub lo_addr: Option<NonZeroU32>, pub lo_addr: Option<SectionAddress>,
} }
impl Gpr { impl Gpr {
@ -36,16 +41,16 @@ impl Gpr {
self.lo_addr = None; self.lo_addr = None;
} }
fn set_hi(&mut self, value: GprValue, addr: u32) { fn set_hi(&mut self, value: GprValue, addr: SectionAddress) {
self.value = value; self.value = value;
self.hi_addr = NonZeroU32::new(addr); self.hi_addr = Some(addr);
self.lo_addr = None; self.lo_addr = None;
} }
fn set_lo(&mut self, value: GprValue, addr: u32, hi_gpr: Gpr) { fn set_lo(&mut self, value: GprValue, addr: SectionAddress, hi_gpr: Gpr) {
self.value = value; self.value = value;
self.hi_addr = hi_gpr.hi_addr; self.hi_addr = hi_gpr.hi_addr;
self.lo_addr = hi_gpr.lo_addr.or_else(|| NonZeroU32::new(addr)); self.lo_addr = Some(hi_gpr.lo_addr.unwrap_or(addr));
} }
} }
@ -80,9 +85,9 @@ pub enum BranchTarget {
/// Branch to LR /// Branch to LR
Return, Return,
/// Branch to address /// Branch to address
Address(u32), Address(SectionAddress),
/// Branch to jump table /// Branch to jump table
JumpTable { address: u32, size: Option<NonZeroU32> }, JumpTable { address: SectionAddress, size: Option<NonZeroU32> },
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
@ -100,7 +105,7 @@ pub enum StepResult {
/// Continue normally /// Continue normally
Continue, Continue,
/// Load from / store to /// Load from / store to
LoadStore { address: u32, source: Gpr, source_reg: u8 }, LoadStore { address: SectionAddress, source: Gpr, source_reg: u8 },
/// Hit illegal instruction /// Hit illegal instruction
Illegal, Illegal,
/// Jump without affecting VM state /// Jump without affecting VM state
@ -109,23 +114,40 @@ pub enum StepResult {
Branch(Vec<Branch>), Branch(Vec<Branch>),
} }
pub fn section_address_for(
obj: &ObjInfo,
ins_addr: SectionAddress,
target_addr: u32,
) -> Option<SectionAddress> {
if let Some(target) = relocation_target_for(obj, ins_addr, None).ok().flatten() {
return Some(target);
}
if obj.kind == ObjKind::Executable {
let (section_index, _) = obj.sections.at_address(target_addr).ok()?;
return Some(SectionAddress::new(section_index, target_addr));
}
// TODO: relative jumps within relocatable objects?
None
}
impl VM { impl VM {
#[inline] #[inline]
pub fn new() -> Box<Self> { Box::default() } pub fn new() -> Box<Self> { Box::default() }
#[inline] #[inline]
pub fn new_from_obj(obj: &ObjInfo) -> Box<Self> { pub fn new_from_obj(obj: &ObjInfo) -> Box<Self> {
match (obj.sda2_base, obj.sda_base) { Self::new_with_base(obj.sda2_base, obj.sda_base)
(Some(sda2_base), Some(sda_base)) => Self::new_with_base(sda2_base, sda_base),
_ => Self::new(),
}
} }
#[inline] #[inline]
pub fn new_with_base(sda2_base: u32, sda_base: u32) -> Box<Self> { pub fn new_with_base(sda2_base: Option<u32>, sda_base: Option<u32>) -> Box<Self> {
let mut vm = Self::new(); let mut vm = Self::new();
vm.gpr[2].value = GprValue::Constant(sda2_base); if let Some(value) = sda2_base {
vm.gpr[13].value = GprValue::Constant(sda_base); vm.gpr[2].value = GprValue::Constant(value);
}
if let Some(value) = sda_base {
vm.gpr[13].value = GprValue::Constant(value);
}
vm vm
} }
@ -157,7 +179,13 @@ impl VM {
#[inline] #[inline]
pub fn clone_all(&self) -> Box<Self> { Box::new(self.clone()) } pub fn clone_all(&self) -> Box<Self> { Box::new(self.clone()) }
pub fn step(&mut self, ins: &Ins) -> StepResult { pub fn step(&mut self, obj: &ObjInfo, ins_addr: SectionAddress, ins: &Ins) -> StepResult {
let relocation_target = relocation_target_for(obj, ins_addr, None).ok().flatten();
if let Some(_target) = relocation_target {
let _defs = ins.defs();
// TODO
}
match ins.op { match ins.op {
Opcode::Illegal => { Opcode::Illegal => {
return StepResult::Illegal; return StepResult::Illegal;
@ -189,7 +217,7 @@ impl VM {
}; };
if ins.field_rA() == 0 { if ins.field_rA() == 0 {
// lis rD, SIMM // lis rD, SIMM
self.gpr[ins.field_rD()].set_hi(value, ins.addr); self.gpr[ins.field_rD()].set_hi(value, ins_addr);
} else { } else {
self.gpr[ins.field_rD()].set_direct(value); self.gpr[ins.field_rD()].set_direct(value);
} }
@ -213,7 +241,7 @@ impl VM {
// li rD, SIMM // li rD, SIMM
self.gpr[ins.field_rD()].set_direct(value); self.gpr[ins.field_rD()].set_direct(value);
} else { } else {
self.gpr[ins.field_rD()].set_lo(value, ins.addr, self.gpr[ins.field_rA()]); self.gpr[ins.field_rD()].set_lo(value, ins_addr, self.gpr[ins.field_rA()]);
} }
} }
// ori rA, rS, UIMM // ori rA, rS, UIMM
@ -224,7 +252,7 @@ impl VM {
} }
_ => GprValue::Unknown, _ => GprValue::Unknown,
}; };
self.gpr[ins.field_rA()].set_lo(value, ins.addr, self.gpr[ins.field_rS()]); self.gpr[ins.field_rA()].set_lo(value, ins_addr, self.gpr[ins.field_rS()]);
} }
// or rA, rS, rB // or rA, rS, rB
Opcode::Or => { Opcode::Or => {
@ -304,24 +332,41 @@ impl VM {
let branch_target = match ins.op { let branch_target = match ins.op {
Opcode::Bcctr => { Opcode::Bcctr => {
match self.ctr { match self.ctr {
GprValue::Constant(value) => BranchTarget::Address(value), GprValue::Constant(value) => {
if let Some(target) = section_address_for(obj, ins_addr, value) {
BranchTarget::Address(target)
} else {
BranchTarget::Unknown
}
},
GprValue::LoadIndexed { address, max_offset } GprValue::LoadIndexed { address, max_offset }
// FIXME: avoids treating bctrl indirect calls as jump tables // FIXME: avoids treating bctrl indirect calls as jump tables
if !ins.field_LK() => { if !ins.field_LK() => {
BranchTarget::JumpTable { address, size: max_offset.and_then(|n| n.checked_add(4)) } if let Some(target) = section_address_for(obj, ins_addr, address) {
BranchTarget::JumpTable { address: target, size: max_offset.and_then(|n| n.checked_add(4)) }
} else {
BranchTarget::Unknown
}
} }
_ => BranchTarget::Unknown, _ => BranchTarget::Unknown,
} }
} }
Opcode::Bclr => BranchTarget::Return, Opcode::Bclr => BranchTarget::Return,
_ => BranchTarget::Address(ins.branch_dest().unwrap()), _ => {
let value = ins.branch_dest().unwrap();
if let Some(target) = section_address_for(obj, ins_addr, value) {
BranchTarget::Address(target)
} else {
BranchTarget::Unknown
}
}
}; };
// If branching with link, use function call semantics // If branching with link, use function call semantics
if ins.field_LK() { if ins.field_LK() {
return StepResult::Branch(vec![ return StepResult::Branch(vec![
Branch { Branch {
target: BranchTarget::Address(ins.addr + 4), target: BranchTarget::Address(ins_addr + 4),
link: false, link: false,
vm: self.clone_for_return(), vm: self.clone_for_return(),
}, },
@ -338,7 +383,7 @@ impl VM {
let mut branches = vec![ let mut branches = vec![
// Branch not taken // Branch not taken
Branch { Branch {
target: BranchTarget::Address(ins.addr + 4), target: BranchTarget::Address(ins_addr + 4),
link: false, link: false,
vm: self.clone_all(), vm: self.clone_all(),
}, },
@ -409,15 +454,17 @@ impl VM {
if is_update_op(op) { if is_update_op(op) {
self.gpr[source].set_lo( self.gpr[source].set_lo(
GprValue::Constant(address), GprValue::Constant(address),
ins.addr, ins_addr,
self.gpr[source], self.gpr[source],
); );
} }
result = StepResult::LoadStore { if let Some(target) = section_address_for(obj, ins_addr, address) {
address, result = StepResult::LoadStore {
source: self.gpr[source], address: target,
source_reg: source as u8, source: self.gpr[source],
}; source_reg: source as u8,
};
}
} else if is_update_op(op) { } else if is_update_op(op) {
self.gpr[source].set_direct(GprValue::Unknown); self.gpr[source].set_direct(GprValue::Unknown);
} }
@ -619,119 +666,119 @@ pub fn is_update_op(op: Opcode) -> bool {
// ) // )
// } // }
#[cfg(test)] // #[cfg(test)]
mod tests { // mod tests {
use super::*; // use super::*;
//
#[test] // #[test]
fn test_load_indexed_1() { // fn test_load_indexed_1() {
let mut vm = VM::new(); // let mut vm = VM::new();
assert_eq!(vm.step(&Ins::new(0x3cc08052, 0x803dfe28)), StepResult::Continue); // lis r6, -0x7fae // assert_eq!(vm.step(&Ins::new(0x3cc08052, 0x803dfe28)), StepResult::Continue); // lis r6, -0x7fae
assert_eq!(vm.step(&Ins::new(0x38c60e18, 0x803dfe30)), StepResult::Continue); // addi r6, r6, 0xe18 // assert_eq!(vm.step(&Ins::new(0x38c60e18, 0x803dfe30)), StepResult::Continue); // addi r6, r6, 0xe18
assert_eq!(vm.gpr[6].value, GprValue::Constant(0x80520e18)); // assert_eq!(vm.gpr[6].value, GprValue::Constant(0x80520e18));
assert_eq!(vm.step(&Ins::new(0x550066fa, 0x803dfe34)), StepResult::Continue); // rlwinm r0, r8, 12, 27, 29 // assert_eq!(vm.step(&Ins::new(0x550066fa, 0x803dfe34)), StepResult::Continue); // rlwinm r0, r8, 12, 27, 29
assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 28, step: 1 << 12 }); // assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 28, step: 1 << 12 });
assert_eq!(vm.step(&Ins::new(0x7d86002e, 0x803dfe3c)), StepResult::Continue); // lwzx r12, r6, r0 // assert_eq!(vm.step(&Ins::new(0x7d86002e, 0x803dfe3c)), StepResult::Continue); // lwzx r12, r6, r0
assert_eq!(vm.gpr[12].value, GprValue::LoadIndexed { // assert_eq!(vm.gpr[12].value, GprValue::LoadIndexed {
address: 0x80520e18, // address: 0x80520e18,
max_offset: NonZeroU32::new(28) // max_offset: NonZeroU32::new(28)
}); // });
assert_eq!(vm.step(&Ins::new(0x7d8903a6, 0x803dfe4c)), StepResult::Continue); // mtspr CTR, r12 // assert_eq!(vm.step(&Ins::new(0x7d8903a6, 0x803dfe4c)), StepResult::Continue); // mtspr CTR, r12
assert_eq!(vm.ctr, GprValue::LoadIndexed { // assert_eq!(vm.ctr, GprValue::LoadIndexed {
address: 0x80520e18, // address: 0x80520e18,
max_offset: NonZeroU32::new(28) // max_offset: NonZeroU32::new(28)
}); // });
assert_eq!( // assert_eq!(
vm.step(&Ins::new(0x4e800420, 0x803dfe50)), // bctr // vm.step(&Ins::new(0x4e800420, 0x803dfe50)), // bctr
StepResult::Jump(BranchTarget::JumpTable { // StepResult::Jump(BranchTarget::JumpTable {
address: 0x80520e18, // address: 0x80520e18,
size: NonZeroU32::new(32) // size: NonZeroU32::new(32)
}) // })
); // );
} // }
//
#[test] // #[test]
fn test_load_indexed_2() { // fn test_load_indexed_2() {
let mut vm = VM::new(); // let mut vm = VM::new();
assert_eq!(vm.step(&Ins::new(0x3c808057, 0x80465320)), StepResult::Continue); // lis r4, -0x7fa9 // assert_eq!(vm.step(&Ins::new(0x3c808057, 0x80465320)), StepResult::Continue); // lis r4, -0x7fa9
assert_eq!(vm.step(&Ins::new(0x54600e7a, 0x80465324)), StepResult::Continue); // rlwinm r0, r3, 1, 25, 29 // assert_eq!(vm.step(&Ins::new(0x54600e7a, 0x80465324)), StepResult::Continue); // rlwinm r0, r3, 1, 25, 29
assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 124, step: 2 }); // assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 124, step: 2 });
assert_eq!(vm.step(&Ins::new(0x38840f70, 0x80465328)), StepResult::Continue); // addi r4, r4, 0xf70 // assert_eq!(vm.step(&Ins::new(0x38840f70, 0x80465328)), StepResult::Continue); // addi r4, r4, 0xf70
assert_eq!(vm.gpr[4].value, GprValue::Constant(0x80570f70)); // assert_eq!(vm.gpr[4].value, GprValue::Constant(0x80570f70));
assert_eq!(vm.step(&Ins::new(0x7d84002e, 0x80465330)), StepResult::Continue); // lwzx r12, r4, r0 // assert_eq!(vm.step(&Ins::new(0x7d84002e, 0x80465330)), StepResult::Continue); // lwzx r12, r4, r0
assert_eq!(vm.gpr[12].value, GprValue::LoadIndexed { // assert_eq!(vm.gpr[12].value, GprValue::LoadIndexed {
address: 0x80570f70, // address: 0x80570f70,
max_offset: NonZeroU32::new(124) // max_offset: NonZeroU32::new(124)
}); // });
assert_eq!(vm.step(&Ins::new(0x7d8903a6, 0x80465340)), StepResult::Continue); // mtspr CTR, r12 // assert_eq!(vm.step(&Ins::new(0x7d8903a6, 0x80465340)), StepResult::Continue); // mtspr CTR, r12
assert_eq!(vm.ctr, GprValue::LoadIndexed { // assert_eq!(vm.ctr, GprValue::LoadIndexed {
address: 0x80570f70, // address: 0x80570f70,
max_offset: NonZeroU32::new(124) // max_offset: NonZeroU32::new(124)
}); // });
assert_eq!( // assert_eq!(
vm.step(&Ins::new(0x4e800420, 0x80465344)), // bctr // vm.step(&Ins::new(0x4e800420, 0x80465344)), // bctr
StepResult::Jump(BranchTarget::JumpTable { // StepResult::Jump(BranchTarget::JumpTable {
address: 0x80570f70, // address: 0x80570f70,
size: NonZeroU32::new(128) // size: NonZeroU32::new(128)
}) // })
); // );
} // }
//
#[test] // #[test]
fn test_load_indexed_3() { // fn test_load_indexed_3() {
let mut vm = VM::new(); // let mut vm = VM::new();
assert_eq!(vm.step(&Ins::new(0x28000127, 0x800ed458)), StepResult::Continue); // cmplwi r0, 0x127 // assert_eq!(vm.step(&Ins::new(0x28000127, 0x800ed458)), StepResult::Continue); // cmplwi r0, 0x127
assert_eq!(vm.cr[0], Cr { // assert_eq!(vm.cr[0], Cr {
signed: false, // signed: false,
left: GprValue::Unknown, // left: GprValue::Unknown,
right: GprValue::Constant(295), // right: GprValue::Constant(295),
}); // });
//
// When branch isn't taken, we know r0 is <= 295 // // When branch isn't taken, we know r0 is <= 295
let mut false_vm = vm.clone(); // let mut false_vm = vm.clone();
false_vm.gpr[0] = // false_vm.gpr[0] =
Gpr { value: GprValue::Range { min: 0, max: 295, step: 1 }, ..Default::default() }; // Gpr { value: GprValue::Range { min: 0, max: 295, step: 1 }, ..Default::default() };
// When branch is taken, we know r0 is > 295 // // When branch is taken, we know r0 is > 295
let mut true_vm = vm.clone(); // let mut true_vm = vm.clone();
true_vm.gpr[0] = Gpr { // true_vm.gpr[0] = Gpr {
value: GprValue::Range { min: 296, max: u32::MAX, step: 1 }, // value: GprValue::Range { min: 296, max: u32::MAX, step: 1 },
..Default::default() // ..Default::default()
}; // };
assert_eq!( // assert_eq!(
vm.step(&Ins::new(0x418160bc, 0x800ed45c)), // bgt 0x60bc // vm.step(&Ins::new(0x418160bc, 0x800ed45c)), // bgt 0x60bc
StepResult::Branch(vec![ // StepResult::Branch(vec![
Branch { // Branch {
target: BranchTarget::Address(0x800ed460), // target: BranchTarget::Address(0x800ed460),
link: false, // link: false,
vm: false_vm.clone() // vm: false_vm.clone()
}, // },
Branch { target: BranchTarget::Address(0x800f3518), link: false, vm: true_vm } // Branch { target: BranchTarget::Address(0x800f3518), link: false, vm: true_vm }
]) // ])
); // );
//
// Take the false branch // // Take the false branch
let mut vm = false_vm; // let mut vm = false_vm;
assert_eq!(vm.step(&Ins::new(0x3c608053, 0x800ed460)), StepResult::Continue); // lis r3, -0x7fad // assert_eq!(vm.step(&Ins::new(0x3c608053, 0x800ed460)), StepResult::Continue); // lis r3, -0x7fad
assert_eq!(vm.step(&Ins::new(0x5400103a, 0x800ed464)), StepResult::Continue); // rlwinm r0, r0, 0x2, 0x0, 0x1d // assert_eq!(vm.step(&Ins::new(0x5400103a, 0x800ed464)), StepResult::Continue); // rlwinm r0, r0, 0x2, 0x0, 0x1d
assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 1180, step: 4 }); // assert_eq!(vm.gpr[0].value, GprValue::Range { min: 0, max: 1180, step: 4 });
assert_eq!(vm.step(&Ins::new(0x3863ef6c, 0x800ed468)), StepResult::Continue); // subi r3, r3, 0x1094 // assert_eq!(vm.step(&Ins::new(0x3863ef6c, 0x800ed468)), StepResult::Continue); // subi r3, r3, 0x1094
assert_eq!(vm.gpr[3].value, GprValue::Constant(0x8052ef6c)); // assert_eq!(vm.gpr[3].value, GprValue::Constant(0x8052ef6c));
assert_eq!(vm.step(&Ins::new(0x7c63002e, 0x800ed46c)), StepResult::Continue); // lwzx r3, r3, r0 // assert_eq!(vm.step(&Ins::new(0x7c63002e, 0x800ed46c)), StepResult::Continue); // lwzx r3, r3, r0
assert_eq!(vm.gpr[3].value, GprValue::LoadIndexed { // assert_eq!(vm.gpr[3].value, GprValue::LoadIndexed {
address: 0x8052ef6c, // address: 0x8052ef6c,
max_offset: NonZeroU32::new(1180) // max_offset: NonZeroU32::new(1180)
}); // });
assert_eq!(vm.step(&Ins::new(0x7c6903a6, 0x800ed470)), StepResult::Continue); // mtspr CTR, r3 // assert_eq!(vm.step(&Ins::new(0x7c6903a6, 0x800ed470)), StepResult::Continue); // mtspr CTR, r3
assert_eq!(vm.ctr, GprValue::LoadIndexed { // assert_eq!(vm.ctr, GprValue::LoadIndexed {
address: 0x8052ef6c, // address: 0x8052ef6c,
max_offset: NonZeroU32::new(1180) // max_offset: NonZeroU32::new(1180)
}); // });
assert_eq!( // assert_eq!(
vm.step(&Ins::new(0x4e800420, 0x800ed474)), // bctr // vm.step(&Ins::new(0x4e800420, 0x800ed474)), // bctr
StepResult::Jump(BranchTarget::JumpTable { // StepResult::Jump(BranchTarget::JumpTable {
address: 0x8052ef6c, // address: 0x8052ef6c,
size: NonZeroU32::new(1184) // size: NonZeroU32::new(1184)
}) // })
); // );
} // }
} // }

View File

@ -3,9 +3,9 @@ use std::{
fs, fs,
fs::{DirBuilder, File}, fs::{DirBuilder, File},
io::Write, io::Write,
mem::take,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use std::mem::take;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use argp::FromArgs; use argp::FromArgs;
@ -14,9 +14,9 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
analysis::{ analysis::{
cfa::AnalyzerState, cfa::{AnalyzerState, SectionAddress},
objects::{detect_object_boundaries, detect_strings}, objects::{detect_object_boundaries, detect_strings},
pass::{AnalysisPass, FindSaveRestSleds, FindTRKInterruptVectorTable}, pass::{AnalysisPass, FindRelCtorsDtors, FindSaveRestSleds, FindTRKInterruptVectorTable},
signatures::{apply_signatures, apply_signatures_post}, signatures::{apply_signatures, apply_signatures_post},
tracker::Tracker, tracker::Tracker,
}, },
@ -285,7 +285,9 @@ fn info(args: InfoArgs) -> Result<()> {
} }
println!("{}:", obj.name); println!("{}:", obj.name);
println!("Entry point: {:#010X}", obj.entry); if let Some(entry) = obj.entry {
println!("Entry point: {:#010X}", entry);
}
println!("\nSections:"); println!("\nSections:");
println!("\t{: >10} | {: <10} | {: <10} | {: <10}", "Name", "Address", "Size", "File Off"); println!("\t{: >10} | {: <10} | {: <10} | {: <10}", "Name", "Address", "Size", "File Off");
for (_, section) in obj.sections.iter() { for (_, section) in obj.sections.iter() {
@ -330,19 +332,35 @@ fn verify_hash<P: AsRef<Path>>(path: P, hash_str: &str) -> Result<()> {
} }
fn update_symbols(obj: &mut ObjInfo, modules: &BTreeMap<u32, ObjInfo>) -> Result<()> { fn update_symbols(obj: &mut ObjInfo, modules: &BTreeMap<u32, ObjInfo>) -> Result<()> {
log::info!("Updating symbols for module {}", obj.module_id); log::debug!("Updating symbols for module {}", obj.module_id);
// Find all references to this module from other modules // Find all references to this module from other modules
for rel_reloc in obj for (source_module_id, rel_reloc) in obj
.unresolved_relocations .unresolved_relocations
.iter() .iter()
.chain(modules.iter().flat_map(|(_, obj)| obj.unresolved_relocations.iter())) .map(|r| (obj.module_id, r))
.filter(|r| r.module_id == obj.module_id) .chain(
modules
.iter()
.flat_map(|(_, obj)| obj.unresolved_relocations.iter().map(|r| (obj.module_id, r))),
)
.filter(|(_, r)| r.module_id == obj.module_id)
{ {
if source_module_id == obj.module_id {
// Skip if already resolved
let (_, source_section) = obj
.sections
.get_elf_index(rel_reloc.section as usize)
.ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.section))?;
if source_section.relocations.contains(rel_reloc.address) {
continue;
}
}
let (target_section_index, target_section) = obj let (target_section_index, target_section) = obj
.sections .sections
.get_elf_index(rel_reloc.target_section as usize) .get_elf_index(rel_reloc.target_section as usize)
.ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.section))?; .ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.target_section))?;
let target_symbol = obj let target_symbol = obj
.symbols .symbols
@ -357,7 +375,12 @@ fn update_symbols(obj: &mut ObjInfo, modules: &BTreeMap<u32, ObjInfo>) -> Result
symbol.name symbol.name
); );
} }
anyhow!("Multiple symbols found for {:#010X}", rel_reloc.addend) anyhow!(
"Multiple symbols found for {:#010X} while checking reloc {} {:?}",
rel_reloc.addend,
source_module_id,
rel_reloc
)
})?; })?;
if let Some((symbol_index, symbol)) = target_symbol { if let Some((symbol_index, symbol)) = target_symbol {
@ -376,13 +399,18 @@ fn update_symbols(obj: &mut ObjInfo, modules: &BTreeMap<u32, ObjInfo>) -> Result
rel_reloc.target_section, rel_reloc.target_section,
rel_reloc.addend rel_reloc.addend
); );
obj.symbols.add_direct(ObjSymbol { let name = if obj.module_id == 0 {
name: format!( format!("lbl_{:08X}", rel_reloc.addend)
"lbl_mod{}_{}_{:08X}", } else {
format!(
"lbl_{}_{}_{:X}",
obj.module_id, obj.module_id,
target_section.name.trim_start_matches('.'), target_section.name.trim_start_matches('.'),
rel_reloc.addend rel_reloc.addend
), )
};
obj.symbols.add_direct(ObjSymbol {
name,
demangled_name: None, demangled_name: None,
address: rel_reloc.addend as u64, address: rel_reloc.addend as u64,
section: Some(target_section_index), section: Some(target_section_index),
@ -404,10 +432,19 @@ fn create_relocations(
modules: &BTreeMap<u32, ObjInfo>, modules: &BTreeMap<u32, ObjInfo>,
dol_obj: &ObjInfo, dol_obj: &ObjInfo,
) -> Result<()> { ) -> Result<()> {
log::info!("Creating relocations for module {}", obj.module_id); log::debug!("Creating relocations for module {}", obj.module_id);
// Resolve all relocations in this module // Resolve all relocations in this module
for rel_reloc in take(&mut obj.unresolved_relocations) { for rel_reloc in take(&mut obj.unresolved_relocations) {
// Skip if already resolved
let (_, source_section) = obj
.sections
.get_elf_index(rel_reloc.section as usize)
.ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.section))?;
if source_section.relocations.contains(rel_reloc.address) {
continue;
}
let target_obj = if rel_reloc.module_id == 0 { let target_obj = if rel_reloc.module_id == 0 {
dol_obj dol_obj
} else if rel_reloc.module_id == obj.module_id { } else if rel_reloc.module_id == obj.module_id {
@ -432,7 +469,7 @@ fn create_relocations(
)? )?
}; };
if let Some((symbol_index, symbol)) = target_obj let Some((symbol_index, symbol)) = target_obj
.symbols .symbols
.at_section_address(target_section_index, rel_reloc.addend) .at_section_address(target_section_index, rel_reloc.addend)
.filter(|(_, s)| s.referenced_by(rel_reloc.kind)) .filter(|(_, s)| s.referenced_by(rel_reloc.kind))
@ -447,32 +484,31 @@ fn create_relocations(
} }
anyhow!("Multiple symbols found for {:#010X}", rel_reloc.addend) anyhow!("Multiple symbols found for {:#010X}", rel_reloc.addend)
})? })?
{ else {
// log::info!("Would create relocation to symbol {}", symbol.name);
let reloc = ObjReloc {
kind: rel_reloc.kind,
address: rel_reloc.address as u64 & !3,
target_symbol: symbol_index,
addend: rel_reloc.addend as i64 - symbol.address as i64,
module: if rel_reloc.module_id == obj.module_id {
None
} else {
Some(rel_reloc.module_id)
},
};
let (_, source_section) = obj
.sections
.get_elf_index_mut(rel_reloc.section as usize)
.ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.section))?;
source_section.relocations.push(reloc);
} else {
bail!( bail!(
"Couldn't find module {} symbol in section {} at {:#010X}", "Couldn't find module {} symbol in section {} at {:#010X}",
rel_reloc.module_id, rel_reloc.module_id,
rel_reloc.target_section, rel_reloc.target_section,
rel_reloc.addend rel_reloc.addend
); );
} };
// log::info!("Would create relocation to symbol {}", symbol.name);
let reloc = ObjReloc {
kind: rel_reloc.kind,
target_symbol: symbol_index,
addend: rel_reloc.addend as i64 - symbol.address as i64,
module: if rel_reloc.module_id == obj.module_id {
None
} else {
Some(rel_reloc.module_id)
},
};
let (_, source_section) = obj
.sections
.get_elf_index_mut(rel_reloc.section as usize)
.ok_or_else(|| anyhow!("Failed to locate REL section {}", rel_reloc.section))?;
source_section.relocations.insert(rel_reloc.address, reloc)?;
} }
Ok(()) Ok(())
@ -483,7 +519,7 @@ fn resolve_external_relocations(
modules: &BTreeMap<u32, ObjInfo>, modules: &BTreeMap<u32, ObjInfo>,
dol_obj: Option<&ObjInfo>, dol_obj: Option<&ObjInfo>,
) -> Result<()> { ) -> Result<()> {
log::info!("Resolving relocations for module {}", obj.module_id); log::debug!("Resolving relocations for module {}", obj.module_id);
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct RelocRef { struct RelocRef {
@ -493,7 +529,7 @@ fn resolve_external_relocations(
let mut reloc_to_symbol = HashMap::<RelocRef, usize>::new(); let mut reloc_to_symbol = HashMap::<RelocRef, usize>::new();
for (_section_index, section) in obj.sections.iter_mut() { for (_section_index, section) in obj.sections.iter_mut() {
for reloc in section.relocations.iter_mut() { for (_reloc_address, reloc) in section.relocations.iter_mut() {
if let Some(module_id) = reloc.module { if let Some(module_id) = reloc.module {
let reloc_ref = RelocRef { module_id, symbol_index: reloc.target_symbol }; let reloc_ref = RelocRef { module_id, symbol_index: reloc.target_symbol };
let symbol_idx = match reloc_to_symbol.entry(reloc_ref) { let symbol_idx = match reloc_to_symbol.entry(reloc_ref) {
@ -559,8 +595,11 @@ fn split(args: SplitArgs) -> Result<()> {
let mut modules = BTreeMap::<u32, ObjInfo>::new(); let mut modules = BTreeMap::<u32, ObjInfo>::new();
let mut module_ids = Vec::with_capacity(config.modules.len()); let mut module_ids = Vec::with_capacity(config.modules.len());
if !config.modules.is_empty() {
log::info!("Loading {} modules", config.modules.len());
}
for module_config in &config.modules { for module_config in &config.modules {
log::info!("Loading {}", module_config.object.display()); log::debug!("Loading {}", module_config.object.display());
if let Some(hash_str) = &module_config.hash { if let Some(hash_str) = &module_config.hash {
verify_hash(&module_config.object, hash_str)?; verify_hash(&module_config.object, hash_str)?;
} }
@ -593,25 +632,6 @@ fn split(args: SplitArgs) -> Result<()> {
log::info!("Performing signature analysis"); log::info!("Performing signature analysis");
apply_signatures(&mut obj)?; apply_signatures(&mut obj)?;
if !modules.is_empty() {
log::info!("Applying module relocations");
// Step 1: For each module, create any missing symbols (referenced from other modules) and set FORCEACTIVE
update_symbols(&mut obj, &modules)?;
for &module_id in &module_ids {
let mut module_obj = modules.remove(&module_id).unwrap();
update_symbols(&mut module_obj, &modules)?;
modules.insert(module_id, module_obj);
}
// Step 2: For each module, create relocations to symbols in other modules
for &module_id in &module_ids {
let mut module_obj = modules.remove(&module_id).unwrap();
create_relocations(&mut module_obj, &modules, &obj)?;
modules.insert(module_id, module_obj);
}
}
if !config.quick_analysis { if !config.quick_analysis {
log::info!("Detecting function boundaries"); log::info!("Detecting function boundaries");
state.detect_functions(&obj)?; state.detect_functions(&obj)?;
@ -631,43 +651,98 @@ fn split(args: SplitArgs) -> Result<()> {
apply_selfile(&mut obj, selfile)?; apply_selfile(&mut obj, selfile)?;
} }
if !modules.is_empty() {
log::info!("Analyzing modules");
let mut function_count = 0;
for &module_id in &module_ids {
log::info!("Analyzing module {}", module_id);
let module_obj = modules.get_mut(&module_id).unwrap();
let mut state = AnalyzerState::default();
state.detect_functions(module_obj)?;
function_count += state.function_slices.len();
FindRelCtorsDtors::execute(&mut state, module_obj)?;
state.apply(module_obj)?;
apply_signatures(module_obj)?;
apply_signatures_post(module_obj)?;
}
log::info!("Discovered {} functions in modules", function_count);
// Step 1: For each module, create any missing symbols (referenced from other modules) and set FORCEACTIVE
update_symbols(&mut obj, &modules)?;
for &module_id in &module_ids {
let mut module_obj = modules.remove(&module_id).unwrap();
update_symbols(&mut module_obj, &modules)?;
modules.insert(module_id, module_obj);
}
// Step 2: For each module, create relocations to symbols in other modules
for &module_id in &module_ids {
let mut module_obj = modules.remove(&module_id).unwrap();
create_relocations(&mut module_obj, &modules, &obj)?;
modules.insert(module_id, module_obj);
}
}
log::info!("Performing relocation analysis"); log::info!("Performing relocation analysis");
let mut tracker = Tracker::new(&obj); let mut tracker = Tracker::new(&obj);
tracker.process(&obj)?; tracker.process(&obj)?;
log::info!("Applying relocations"); log::info!("Applying relocations");
tracker.apply(&mut obj, false)?; tracker.apply(&mut obj, false)?;
if !modules.is_empty() {
resolve_external_relocations(&mut obj, &modules, None)?;
for &module_id in &module_ids {
let mut module_obj = modules.remove(&module_id).unwrap();
resolve_external_relocations(&mut module_obj, &modules, Some(&obj))?;
let mut tracker = Tracker::new(&module_obj);
tracker.process(&module_obj)?;
tracker.apply(&mut module_obj, false)?;
modules.insert(module_id, module_obj);
}
}
if config.detect_objects { if config.detect_objects {
log::info!("Detecting object boundaries"); log::info!("Detecting object boundaries");
detect_object_boundaries(&mut obj)?; detect_object_boundaries(&mut obj)?;
for module_obj in modules.values_mut() {
detect_object_boundaries(module_obj)?;
}
} }
if config.detect_strings { if config.detect_strings {
log::info!("Detecting strings"); log::info!("Detecting strings");
detect_strings(&mut obj)?; detect_strings(&mut obj)?;
for module_obj in modules.values_mut() {
detect_strings(module_obj)?;
}
} }
log::info!("Adjusting splits"); log::info!("Adjusting splits");
update_splits(&mut obj)?; update_splits(&mut obj)?;
for module_obj in modules.values_mut() {
update_splits(module_obj)?;
}
if !args.no_update { if !args.no_update {
log::info!("Writing configuration");
if let Some(symbols_path) = &config.symbols { if let Some(symbols_path) = &config.symbols {
write_symbols_file(symbols_path, &obj)?; write_symbols_file(symbols_path, &obj)?;
} }
if let Some(splits_path) = &config.splits { if let Some(splits_path) = &config.splits {
write_splits_file(splits_path, &obj)?; write_splits_file(splits_path, &obj, false)?;
} }
}
if !modules.is_empty() { for (config, &module_id) in config.modules.iter().zip(&module_ids) {
log::info!("Resolving module relocations"); let module_obj = modules.get(&module_id).unwrap();
if let Some(symbols_path) = &config.symbols {
resolve_external_relocations(&mut obj, &modules, None)?; write_symbols_file(symbols_path, module_obj)?;
for &module_id in &module_ids { }
let mut module_obj = modules.remove(&module_id).unwrap(); if let Some(splits_path) = &config.splits {
resolve_external_relocations(&mut module_obj, &modules, Some(&obj))?; write_splits_file(splits_path, module_obj, true)?;
modules.insert(module_id, module_obj); }
} }
} }
@ -721,22 +796,15 @@ fn split(args: SplitArgs) -> Result<()> {
// Split and write modules // Split and write modules
for (config, &module_id) in config.modules.iter().zip(&module_ids) { for (config, &module_id) in config.modules.iter().zip(&module_ids) {
let obj = modules.get(&module_id).unwrap(); let obj = modules.get_mut(&module_id).unwrap();
let out_dir = args.out_dir.join(format!("module_{}", module_id)); let out_dir = args.out_dir.join(format!("module_{}", module_id));
let asm_dir = out_dir.join("asm"); let asm_dir = out_dir.join("asm");
// let obj_dir = out_dir.join("obj"); // let obj_dir = out_dir.join("obj");
if !args.no_update { log::info!("Processing module {}", module_id);
if let Some(symbols_path) = &config.symbols {
write_symbols_file(symbols_path, obj)?;
}
if let Some(splits_path) = &config.splits {
write_splits_file(splits_path, obj)?;
}
}
log::info!("Writing disassembly"); // log::info!("Writing disassembly");
let filename = config.object.file_name().unwrap().to_str().unwrap(); let filename = config.object.file_name().unwrap().to_str().unwrap();
let out_path = asm_dir.join(asm_path_for_unit(filename)); let out_path = asm_dir.join(asm_path_for_unit(filename));
let mut w = buf_writer(&out_path)?; let mut w = buf_writer(&out_path)?;
@ -783,13 +851,14 @@ fn validate<P: AsRef<Path>>(obj: &ObjInfo, elf_file: P, state: &AnalyzerState) -
); );
} }
} }
let mut real_functions = BTreeMap::<u32, String>::new(); let mut real_functions = BTreeMap::<SectionAddress, String>::new();
for (section_index, _section) in real_obj.sections.by_kind(ObjSectionKind::Code) { for (section_index, _section) in real_obj.sections.by_kind(ObjSectionKind::Code) {
for (_symbol_idx, symbol) in real_obj.symbols.for_section(section_index) { for (_symbol_idx, symbol) in real_obj.symbols.for_section(section_index) {
real_functions.insert(symbol.address as u32, symbol.name.clone()); let symbol_addr = SectionAddress::new(section_index, symbol.address as u32);
match state.function_bounds.get(&(symbol.address as u32)) { real_functions.insert(symbol_addr, symbol.name.clone());
Some(&end) => { match state.function_bounds.get(&symbol_addr) {
if symbol.size > 0 && end != (symbol.address + symbol.size) as u32 { Some(&Some(end)) => {
if symbol.size > 0 && end != (symbol_addr + symbol.size as u32) {
log::warn!( log::warn!(
"Function {:#010X} ({}) ends at {:#010X}, expected {:#010X}", "Function {:#010X} ({}) ends at {:#010X}, expected {:#010X}",
symbol.address, symbol.address,
@ -799,6 +868,9 @@ fn validate<P: AsRef<Path>>(obj: &ObjInfo, elf_file: P, state: &AnalyzerState) -
); );
} }
} }
Some(_) => {
log::warn!("Function {:#010X} ({}) has no end", symbol.address, symbol.name);
}
None => { None => {
log::warn!( log::warn!(
"Function {:#010X} ({}) not discovered!", "Function {:#010X} ({}) not discovered!",
@ -810,14 +882,15 @@ fn validate<P: AsRef<Path>>(obj: &ObjInfo, elf_file: P, state: &AnalyzerState) -
} }
} }
for (&start, &end) in &state.function_bounds { for (&start, &end) in &state.function_bounds {
if end == 0 { let Some(end) = end else {
continue; continue;
} };
if !real_functions.contains_key(&start) { if !real_functions.contains_key(&start) {
let (real_addr, real_name) = real_functions.range(..start).next_back().unwrap(); let (real_addr, real_name) = real_functions.range(..start).next_back().unwrap();
log::warn!( log::warn!(
"Function {:#010X} not real (actually a part of {} @ {:#010X})", "Function {:#010X}..{:#010X} not real (actually a part of {} @ {:#010X})",
start, start,
end,
real_name, real_name,
real_addr real_addr
); );
@ -830,13 +903,10 @@ fn validate<P: AsRef<Path>>(obj: &ObjInfo, elf_file: P, state: &AnalyzerState) -
Some(v) => v, Some(v) => v,
None => continue, None => continue,
}; };
let real_map = real_section.build_relocation_map()?; for (real_addr, real_reloc) in real_section.relocations.iter() {
let obj_map = obj_section.build_relocation_map()?;
for (&real_addr, &real_reloc_idx) in &real_map {
let real_reloc = &real_section.relocations[real_reloc_idx];
let real_symbol = &real_obj.symbols[real_reloc.target_symbol]; let real_symbol = &real_obj.symbols[real_reloc.target_symbol];
let obj_reloc = match obj_map.get(&real_addr) { let obj_reloc = match obj_section.relocations.at(real_addr) {
Some(v) => &obj_section.relocations[*v], Some(v) => v,
None => { None => {
// Ignore GCC local jump branches // Ignore GCC local jump branches
if real_symbol.kind == ObjSymbolKind::Section if real_symbol.kind == ObjSymbolKind::Section
@ -886,10 +956,9 @@ fn validate<P: AsRef<Path>>(obj: &ObjInfo, elf_file: P, state: &AnalyzerState) -
continue; continue;
} }
} }
for (&obj_addr, &obj_reloc_idx) in &obj_map { for (obj_addr, obj_reloc) in obj_section.relocations.iter() {
let obj_reloc = &obj_section.relocations[obj_reloc_idx];
let obj_symbol = &obj.symbols[obj_reloc.target_symbol]; let obj_symbol = &obj.symbols[obj_reloc.target_symbol];
if !real_map.contains_key(&obj_addr) { if !real_section.relocations.contains(obj_addr) {
log::warn!( log::warn!(
"Relocation not real @ {:#010X} {:?} to {:#010X}+{:X} ({})", "Relocation not real @ {:#010X} {:?} to {:#010X}+{:X} ({})",
obj_addr, obj_addr,

View File

@ -124,7 +124,7 @@ fn config(args: ConfigArgs) -> Result<()> {
DirBuilder::new().recursive(true).create(&args.out_dir)?; DirBuilder::new().recursive(true).create(&args.out_dir)?;
write_symbols_file(args.out_dir.join("symbols.txt"), &obj)?; write_symbols_file(args.out_dir.join("symbols.txt"), &obj)?;
write_splits_file(args.out_dir.join("splits.txt"), &obj)?; write_splits_file(args.out_dir.join("splits.txt"), &obj, false)?;
Ok(()) Ok(())
} }

View File

@ -9,7 +9,7 @@ use argp::FromArgs;
use crate::{ use crate::{
analysis::{ analysis::{
cfa::AnalyzerState, cfa::{AnalyzerState, SectionAddress},
pass::{AnalysisPass, FindSaveRestSleds, FindTRKInterruptVectorTable}, pass::{AnalysisPass, FindSaveRestSleds, FindTRKInterruptVectorTable},
signatures::{apply_signatures, apply_signatures_post}, signatures::{apply_signatures, apply_signatures_post},
tracker::Tracker, tracker::Tracker,
@ -75,7 +75,6 @@ fn info(args: InfoArgs) -> Result<()> {
let map = map_file(args.rel_file)?; let map = map_file(args.rel_file)?;
let rel = process_rel(map_reader(&map))?; let rel = process_rel(map_reader(&map))?;
println!("Read REL module ID {}", rel.module_id); println!("Read REL module ID {}", rel.module_id);
// println!("REL: {:#?}", rel);
Ok(()) Ok(())
} }
@ -117,7 +116,7 @@ fn merge(args: MergeArgs) -> Result<()> {
data: mod_section.data.clone(), data: mod_section.data.clone(),
align: mod_section.align, align: mod_section.align,
elf_index: mod_section.elf_index, elf_index: mod_section.elf_index,
relocations: vec![], relocations: Default::default(),
original_address: mod_section.original_address, original_address: mod_section.original_address,
file_offset: mod_section.file_offset, file_offset: mod_section.file_offset,
section_known: mod_section.section_known, section_known: mod_section.section_known,
@ -160,8 +159,10 @@ fn merge(args: MergeArgs) -> Result<()> {
let (target_section_index, _) = obj.sections.at_address(target_addr)?; let (target_section_index, _) = obj.sections.at_address(target_addr)?;
let (symbol_idx, addend) = if let Some((symbol_idx, symbol)) = let (symbol_idx, addend) = if let Some((symbol_idx, symbol)) =
obj.symbols.for_relocation(target_addr, rel_reloc.kind)? obj.symbols.for_relocation(
{ SectionAddress::new(target_section_index, target_addr),
rel_reloc.kind,
)? {
(symbol_idx, target_addr as i64 - symbol.address as i64) (symbol_idx, target_addr as i64 - symbol.address as i64)
} else { } else {
// Create a new label // Create a new label
@ -179,13 +180,12 @@ fn merge(args: MergeArgs) -> Result<()> {
})?; })?;
(symbol_idx, 0) (symbol_idx, 0)
}; };
obj.sections[source_section_index].relocations.push(ObjReloc { obj.sections[source_section_index].relocations.insert(source_addr, ObjReloc {
kind: rel_reloc.kind, kind: rel_reloc.kind,
address: source_addr as u64,
target_symbol: symbol_idx, target_symbol: symbol_idx,
addend, addend,
module: None, module: None,
}); })?;
} }
} }
@ -218,12 +218,11 @@ fn merge(args: MergeArgs) -> Result<()> {
fn link_relocations(obj: &mut ObjInfo) -> Result<()> { fn link_relocations(obj: &mut ObjInfo) -> Result<()> {
for (_, section) in obj.sections.iter_mut() { for (_, section) in obj.sections.iter_mut() {
for reloc in &section.relocations { for (source_address, reloc) in section.relocations.iter() {
let source_address = reloc.address /*& !3*/;
let target_address = let target_address =
(obj.symbols[reloc.target_symbol].address as i64 + reloc.addend) as u32; (obj.symbols[reloc.target_symbol].address as i64 + reloc.addend) as u32;
let ins_ref = let ins_ref =
array_ref_mut!(section.data, (source_address - section.address) as usize, 4); array_ref_mut!(section.data, (source_address as u64 - section.address) as usize, 4);
let mut ins = u32::from_be_bytes(*ins_ref); let mut ins = u32::from_be_bytes(*ins_ref);
match reloc.kind { match reloc.kind {
ObjRelocKind::Absolute => { ObjRelocKind::Absolute => {

View File

@ -1,3 +1,4 @@
mod relocations;
mod sections; mod sections;
mod splits; mod splits;
mod symbols; mod symbols;
@ -9,8 +10,8 @@ use std::{
}; };
use anyhow::{anyhow, bail, ensure, Result}; use anyhow::{anyhow, bail, ensure, Result};
pub use relocations::{ObjReloc, ObjRelocKind, ObjRelocations};
pub use sections::{section_kind_for_section, ObjSection, ObjSectionKind, ObjSections}; pub use sections::{section_kind_for_section, ObjSection, ObjSectionKind, ObjSections};
use serde::{Deserialize, Serialize};
pub use splits::{ObjSplit, ObjSplits}; pub use splits::{ObjSplit, ObjSplits};
pub use symbols::{ pub use symbols::{
ObjDataKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, ObjSymbolScope, ObjDataKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, ObjSymbolScope,
@ -49,7 +50,7 @@ pub struct ObjInfo {
pub name: String, pub name: String,
pub symbols: ObjSymbols, pub symbols: ObjSymbols,
pub sections: ObjSections, pub sections: ObjSections,
pub entry: u64, pub entry: Option<u64>,
pub mw_comment: Option<MWComment>, pub mw_comment: Option<MWComment>,
// Linker generated // Linker generated
@ -75,27 +76,6 @@ pub struct ObjInfo {
pub unresolved_relocations: Vec<RelReloc>, pub unresolved_relocations: Vec<RelReloc>,
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum ObjRelocKind {
Absolute,
PpcAddr16Hi,
PpcAddr16Ha,
PpcAddr16Lo,
PpcRel24,
PpcRel14,
PpcEmbSda21,
}
#[derive(Debug, Clone)]
pub struct ObjReloc {
pub kind: ObjRelocKind,
pub address: u64,
pub target_symbol: SymbolIndex,
pub addend: i64,
/// If present, relocation against external module
pub module: Option<u32>,
}
impl ObjInfo { impl ObjInfo {
pub fn new( pub fn new(
kind: ObjKind, kind: ObjKind,
@ -110,7 +90,7 @@ impl ObjInfo {
name, name,
symbols: ObjSymbols::new(kind, symbols), symbols: ObjSymbols::new(kind, symbols),
sections: ObjSections::new(kind, sections), sections: ObjSections::new(kind, sections),
entry: 0, entry: None,
mw_comment: Default::default(), mw_comment: Default::default(),
sda2_base: None, sda2_base: None,
sda_base: None, sda_base: None,

109
src/obj/relocations.rs Normal file
View File

@ -0,0 +1,109 @@
use std::{
collections::{btree_map, BTreeMap},
error::Error,
fmt,
ops::RangeBounds,
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use crate::obj::SymbolIndex;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum ObjRelocKind {
Absolute,
PpcAddr16Hi,
PpcAddr16Ha,
PpcAddr16Lo,
PpcRel24,
PpcRel14,
PpcEmbSda21,
}
#[derive(Debug, Clone)]
pub struct ObjReloc {
pub kind: ObjRelocKind,
// pub address: u64,
pub target_symbol: SymbolIndex,
pub addend: i64,
/// If present, relocation against external module
pub module: Option<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct ObjRelocations {
relocations: BTreeMap<u32, ObjReloc>,
}
#[derive(Debug)]
pub struct ExistingRelocationError {
pub address: u32,
pub value: ObjReloc,
}
impl fmt::Display for ExistingRelocationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "relocation already exists at address {:#010X}", self.address)
}
}
impl Error for ExistingRelocationError {}
impl ObjRelocations {
pub fn new(relocations: Vec<(u32, ObjReloc)>) -> Result<Self, ExistingRelocationError> {
let mut map = BTreeMap::new();
for (address, reloc) in relocations {
let address = address & !3;
match map.entry(address) {
btree_map::Entry::Vacant(e) => e.insert(reloc),
btree_map::Entry::Occupied(e) => {
return Err(ExistingRelocationError { address, value: e.get().clone() })
}
};
}
Ok(Self { relocations: map })
}
pub fn len(&self) -> usize { self.relocations.len() }
pub fn insert(&mut self, address: u32, reloc: ObjReloc) -> Result<(), ExistingRelocationError> {
let address = address & !3;
match self.relocations.entry(address) {
btree_map::Entry::Vacant(e) => e.insert(reloc),
btree_map::Entry::Occupied(e) => {
return Err(ExistingRelocationError { address, value: e.get().clone() })
}
};
Ok(())
}
pub fn replace(&mut self, address: u32, reloc: ObjReloc) {
self.relocations.insert(address, reloc);
}
pub fn at(&self, address: u32) -> Option<&ObjReloc> { self.relocations.get(&address) }
pub fn at_mut(&mut self, address: u32) -> Option<&mut ObjReloc> {
self.relocations.get_mut(&address)
}
pub fn clone_map(&self) -> BTreeMap<u32, ObjReloc> { self.relocations.clone() }
pub fn is_empty(&self) -> bool { self.relocations.is_empty() }
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (u32, &ObjReloc)> {
self.relocations.iter().map(|(&addr, reloc)| (addr, reloc))
}
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = (u32, &mut ObjReloc)> {
self.relocations.iter_mut().map(|(&addr, reloc)| (addr, reloc))
}
pub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = (u32, &ObjReloc)>
where R: RangeBounds<u32> {
self.relocations.range(range).map(|(&addr, reloc)| (addr, reloc))
}
pub fn contains(&self, address: u32) -> bool { self.relocations.contains_key(&address) }
}

View File

@ -1,13 +1,13 @@
use std::{ use std::{
cmp::min, cmp::min,
collections::{btree_map, BTreeMap, Bound}, collections::Bound,
ops::{Index, IndexMut, Range, RangeBounds}, ops::{Index, IndexMut, Range, RangeBounds},
}; };
use anyhow::{anyhow, bail, ensure, Result}; use anyhow::{anyhow, bail, ensure, Result};
use itertools::Itertools; use itertools::Itertools;
use crate::obj::{ObjKind, ObjReloc, ObjSplit, ObjSplits, ObjSymbol}; use crate::obj::{ObjKind, ObjRelocations, ObjSplit, ObjSplits, ObjSymbol};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ObjSectionKind { pub enum ObjSectionKind {
@ -27,7 +27,7 @@ pub struct ObjSection {
pub align: u64, pub align: u64,
/// REL files reference the original ELF section indices /// REL files reference the original ELF section indices
pub elf_index: usize, pub elf_index: usize,
pub relocations: Vec<ObjReloc>, pub relocations: ObjRelocations,
pub original_address: u64, pub original_address: u64,
pub file_offset: u64, pub file_offset: u64,
pub section_known: bool, pub section_known: bool,
@ -175,34 +175,6 @@ impl ObjSection {
self.data_range(symbol.address as u32, symbol.address as u32 + symbol.size as u32) self.data_range(symbol.address as u32, symbol.address as u32 + symbol.size as u32)
} }
pub fn build_relocation_map(&self) -> Result<BTreeMap<u32, usize>> {
let mut relocations = BTreeMap::new();
for (idx, reloc) in self.relocations.iter().enumerate() {
let address = reloc.address as u32;
match relocations.entry(address) {
btree_map::Entry::Vacant(e) => {
e.insert(idx);
}
btree_map::Entry::Occupied(_) => bail!("Duplicate relocation @ {address:#010X}"),
}
}
Ok(relocations)
}
pub fn build_relocation_map_cloned(&self) -> Result<BTreeMap<u32, ObjReloc>> {
let mut relocations = BTreeMap::new();
for reloc in self.relocations.iter().cloned() {
let address = reloc.address as u32;
match relocations.entry(address) {
btree_map::Entry::Vacant(e) => {
e.insert(reloc);
}
btree_map::Entry::Occupied(_) => bail!("Duplicate relocation @ {address:#010X}"),
}
}
Ok(relocations)
}
#[inline] #[inline]
pub fn contains(&self, addr: u32) -> bool { pub fn contains(&self, addr: u32) -> bool {
(self.address..self.address + self.size).contains(&(addr as u64)) (self.address..self.address + self.size).contains(&(addr as u64))

View File

@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr}; use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::{ use crate::{
analysis::cfa::SectionAddress,
obj::{ObjKind, ObjRelocKind}, obj::{ObjKind, ObjRelocKind},
util::{config::is_auto_symbol, nested::NestedVec, split::is_linker_generated_label}, util::{config::is_auto_symbol, nested::NestedVec, split::is_linker_generated_label},
}; };
@ -34,6 +35,8 @@ flags! {
Common, Common,
Hidden, Hidden,
ForceActive, ForceActive,
/// Symbol isn't referenced by any relocations
RelocationIgnore,
} }
} }
@ -72,6 +75,9 @@ impl ObjSymbolFlagSet {
#[inline] #[inline]
pub fn is_force_active(&self) -> bool { self.0.contains(ObjSymbolFlags::ForceActive) } pub fn is_force_active(&self) -> bool { self.0.contains(ObjSymbolFlags::ForceActive) }
#[inline]
pub fn is_relocation_ignore(&self) -> bool { self.0.contains(ObjSymbolFlags::RelocationIgnore) }
#[inline] #[inline]
pub fn set_scope(&mut self, scope: ObjSymbolScope) { pub fn set_scope(&mut self, scope: ObjSymbolScope) {
match scope { match scope {
@ -194,9 +200,11 @@ impl ObjSymbols {
// Replace auto symbols with real symbols // Replace auto symbols with real symbols
(symbol.kind == ObjSymbolKind::Unknown && is_auto_symbol(&symbol.name)) (symbol.kind == ObjSymbolKind::Unknown && is_auto_symbol(&symbol.name))
}) })
} else { } else if self.obj_kind == ObjKind::Executable {
// TODO hmmm // TODO hmmm
self.iter_abs().find(|(_, symbol)| symbol.name == in_symbol.name) self.iter_abs().find(|(_, symbol)| symbol.name == in_symbol.name)
} else {
bail!("ABS symbol in relocatable object: {:?}", in_symbol);
}; };
let target_symbol_idx = if let Some((symbol_idx, existing)) = opt { let target_symbol_idx = if let Some((symbol_idx, existing)) = opt {
let size = let size =
@ -361,7 +369,7 @@ impl ObjSymbols {
where where
R: RangeBounds<u32>, R: RangeBounds<u32>,
{ {
debug_assert!(self.obj_kind == ObjKind::Executable); // debug_assert!(self.obj_kind == ObjKind::Executable);
self.symbols_by_address.range(range).map(|(k, v)| (*k, v.as_ref())) self.symbols_by_address.range(range).map(|(k, v)| (*k, v.as_ref()))
} }
@ -432,16 +440,19 @@ impl ObjSymbols {
// Try to find a previous sized symbol that encompasses the target // Try to find a previous sized symbol that encompasses the target
pub fn for_relocation( pub fn for_relocation(
&self, &self,
target_addr: u32, target_addr: SectionAddress,
reloc_kind: ObjRelocKind, reloc_kind: ObjRelocKind,
) -> Result<Option<(SymbolIndex, &ObjSymbol)>> { ) -> Result<Option<(SymbolIndex, &ObjSymbol)>> {
ensure!(self.obj_kind == ObjKind::Executable); // ensure!(self.obj_kind == ObjKind::Executable);
let mut result = None; let mut result = None;
for (_addr, symbol_idxs) in self.indexes_for_range(..=target_addr).rev() { for (_addr, symbol_idxs) in self.indexes_for_range(..=target_addr.address).rev() {
let mut symbols = symbol_idxs let mut symbols = symbol_idxs
.iter() .iter()
.map(|&idx| (idx, &self.symbols[idx])) .map(|&idx| (idx, &self.symbols[idx]))
.filter(|(_, sym)| sym.referenced_by(reloc_kind)) .filter(|(_, sym)| {
(sym.section.is_none() || sym.section == Some(target_addr.section))
&& sym.referenced_by(reloc_kind)
})
.collect_vec(); .collect_vec();
let (symbol_idx, symbol) = if symbols.len() == 1 { let (symbol_idx, symbol) = if symbols.len() == 1 {
symbols.pop().unwrap() symbols.pop().unwrap()
@ -480,12 +491,12 @@ impl ObjSymbols {
None => continue, None => continue,
} }
}; };
if symbol.address == target_addr as u64 { if symbol.address == target_addr.address as u64 {
result = Some((symbol_idx, symbol)); result = Some((symbol_idx, symbol));
break; break;
} }
if symbol.size > 0 { if symbol.size > 0 {
if symbol.address + symbol.size > target_addr as u64 { if symbol.address + symbol.size > target_addr.address as u64 {
result = Some((symbol_idx, symbol)); result = Some((symbol_idx, symbol));
} }
break; break;
@ -509,6 +520,10 @@ impl Index<SymbolIndex> for ObjSymbols {
impl ObjSymbol { impl ObjSymbol {
/// Whether this symbol can be referenced by the given relocation kind. /// Whether this symbol can be referenced by the given relocation kind.
pub fn referenced_by(&self, reloc_kind: ObjRelocKind) -> bool { pub fn referenced_by(&self, reloc_kind: ObjRelocKind) -> bool {
if self.flags.is_relocation_ignore() {
return false;
}
if is_linker_generated_label(&self.name) { if is_linker_generated_label(&self.name) {
// Linker generated labels will only be referenced by @ha/@h/@l relocations // Linker generated labels will only be referenced by @ha/@h/@l relocations
return matches!( return matches!(

View File

@ -62,7 +62,7 @@ pub fn write_asm<W: Write>(w: &mut W, obj: &ObjInfo) -> Result<()> {
} }
} }
let mut relocations = section.build_relocation_map_cloned()?; let mut relocations = section.relocations.clone_map();
// Generate local jump labels // Generate local jump labels
if section.kind == ObjSectionKind::Code { if section.kind == ObjSectionKind::Code {
@ -107,7 +107,6 @@ pub fn write_asm<W: Write>(w: &mut W, obj: &ObjInfo) -> Result<()> {
Opcode::Bc => ObjRelocKind::PpcRel14, Opcode::Bc => ObjRelocKind::PpcRel14,
_ => unreachable!(), _ => unreachable!(),
}, },
address: ins.addr as u64,
target_symbol: symbol_idx, target_symbol: symbol_idx,
addend: 0, addend: 0,
module: None, module: None,
@ -127,7 +126,7 @@ pub fn write_asm<W: Write>(w: &mut W, obj: &ObjInfo) -> Result<()> {
.iter() .iter()
.filter(|(_, s)| matches!(s.kind, ObjSectionKind::Data | ObjSectionKind::ReadOnlyData)) .filter(|(_, s)| matches!(s.kind, ObjSectionKind::Data | ObjSectionKind::ReadOnlyData))
{ {
for reloc in &section.relocations { for (reloc_address, reloc) in section.relocations.iter() {
if reloc.addend == 0 { if reloc.addend == 0 {
continue; continue;
} }
@ -137,7 +136,7 @@ pub fn write_asm<W: Write>(w: &mut W, obj: &ObjInfo) -> Result<()> {
None => continue, None => continue,
}; };
let target_section = obj.sections.get(target_section_idx).ok_or_else(|| { let target_section = obj.sections.get(target_section_idx).ok_or_else(|| {
anyhow!("Invalid relocation target section: {:#010X} {:?}", reloc.address, target) anyhow!("Invalid relocation target section: {:#010X} {:?}", reloc_address, target)
})?; })?;
let address = (target.address as i64 + reloc.addend) as u64; let address = (target.address as i64 + reloc.addend) as u64;
let vec = match section_entries[target_section_idx].entry(address as u32) { let vec = match section_entries[target_section_idx].entry(address as u32) {
@ -462,13 +461,13 @@ fn write_data<W: Write>(
} else { } else {
current_symbol_kind current_symbol_kind
}; };
if let Some((reloc_addr, r)) = reloc { if let Some((&reloc_addr, r)) = reloc {
if current_address == *reloc_addr { if current_address == reloc_addr {
reloc = reloc_iter.next(); reloc = reloc_iter.next();
match symbol_kind { match symbol_kind {
ObjSymbolKind::Object => { ObjSymbolKind::Object => {
current_address = current_address =
write_data_reloc(w, symbols, entries, r, section_entries)?; write_data_reloc(w, symbols, entries, reloc_addr, r, section_entries)?;
continue; continue;
} }
ObjSymbolKind::Function => { ObjSymbolKind::Function => {
@ -716,6 +715,7 @@ fn write_data_reloc<W: Write>(
w: &mut W, w: &mut W,
symbols: &[ObjSymbol], symbols: &[ObjSymbol],
_entries: &BTreeMap<u32, Vec<SymbolEntry>>, _entries: &BTreeMap<u32, Vec<SymbolEntry>>,
reloc_address: u32,
reloc: &ObjReloc, reloc: &ObjReloc,
section_entries: &[BTreeMap<u32, Vec<SymbolEntry>>], section_entries: &[BTreeMap<u32, Vec<SymbolEntry>>],
) -> Result<u32> { ) -> Result<u32> {
@ -736,18 +736,18 @@ fn write_data_reloc<W: Write>(
write!(w, ", ")?; write!(w, ", ")?;
write_symbol_name(w, &symbol.name)?; write_symbol_name(w, &symbol.name)?;
writeln!(w)?; writeln!(w)?;
return Ok((reloc.address + 4) as u32); return Ok(reloc_address + 4);
} }
} }
write!(w, "\t.4byte ")?; write!(w, "\t.4byte ")?;
write_reloc_symbol(w, symbols, reloc)?; write_reloc_symbol(w, symbols, reloc)?;
writeln!(w)?; writeln!(w)?;
Ok((reloc.address + 4) as u32) Ok(reloc_address + 4)
} }
_ => Err(anyhow!( _ => Err(anyhow!(
"Unsupported data relocation type {:?} @ {:#010X}", "Unsupported data relocation type {:?} @ {:#010X}",
reloc.kind, reloc.kind,
reloc.address reloc_address
)), )),
} }
} }

View File

@ -41,7 +41,7 @@ impl MWComment {
10 => [2, 4, 2, 1], 10 => [2, 4, 2, 1],
// Version 2.4.7 build 108 // Version 2.4.7 build 108
// (CodeWarrior for GameCube 2.7) // (CodeWarrior for GameCube 2.7)
11 => [2, 4, 7, 1], 11 | 13 => [2, 4, 7, 1],
// Version 4.1 build 60126 // Version 4.1 build 60126
// (CodeWarrior for GameCube 3.0 Alpha 3) // (CodeWarrior for GameCube 3.0 Alpha 3)
14 | 15 => [4, 0, 0, 1], 14 | 15 => [4, 0, 0, 1],
@ -84,7 +84,7 @@ impl MWComment {
// 0xB // 0xB
header.version = reader.read_u8()?; header.version = reader.read_u8()?;
ensure!( ensure!(
matches!(header.version, 8 | 10 | 11 | 14 | 15), matches!(header.version, 8 | 10 | 11 | 13 | 14 | 15),
"Unknown .comment section version: {}", "Unknown .comment section version: {}",
header.version header.version
); );
@ -219,7 +219,7 @@ pub fn read_comment_sym<R: Read>(r: &mut R) -> Result<CommentSym> {
ensure!(matches!(out.vis_flags, 0 | 0xD | 0xE), "Unknown vis_flags {}", out.vis_flags); ensure!(matches!(out.vis_flags, 0 | 0xD | 0xE), "Unknown vis_flags {}", out.vis_flags);
out.active_flags = r.read_u8()?; out.active_flags = r.read_u8()?;
ensure!( ensure!(
matches!(out.active_flags, 0 | 0x8 | 0x10), matches!(out.active_flags, 0 | 0x8 | 0x10 | 0x20),
"Unknown active_flags {}", "Unknown active_flags {}",
out.active_flags out.active_flags
); );

View File

@ -288,23 +288,20 @@ fn symbol_data_kind_from_str(s: &str) -> Option<ObjDataKind> {
} }
#[inline] #[inline]
pub fn write_splits_file<P: AsRef<Path>>(path: P, obj: &ObjInfo) -> Result<()> { pub fn write_splits_file<P: AsRef<Path>>(path: P, obj: &ObjInfo, all: bool) -> Result<()> {
let mut w = buf_writer(path)?; let mut w = buf_writer(path)?;
write_splits(&mut w, obj)?; write_splits(&mut w, obj, all)?;
w.flush()?; w.flush()?;
Ok(()) Ok(())
} }
pub fn write_splits<W: Write>(w: &mut W, obj: &ObjInfo) -> Result<()> { pub fn write_splits<W: Write>(w: &mut W, obj: &ObjInfo, all: bool) -> Result<()> {
let mut begin = true; writeln!(w, "Sections:")?;
for unit in obj.link_order.iter().filter(|unit| !unit.autogenerated) { for (_, section) in obj.sections.iter() {
if begin { write!(w, "\t{:<11} type:{:?} align:{:#X}", section.name, section.kind, section.align)?;
begin = false; }
} else { for unit in obj.link_order.iter().filter(|unit| all || !unit.autogenerated) {
writeln!(w)?; write!(w, "\n{}:", unit.name)?;
}
write!(w, "{}:", unit.name)?;
if let Some(comment_version) = unit.comment_version { if let Some(comment_version) = unit.comment_version {
write!(w, " comment:{}", comment_version)?; write!(w, " comment:{}", comment_version)?;
} }

View File

@ -270,7 +270,7 @@ pub fn process_dol<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
data: dol.virtual_data_at(dol_section.target, size)?.to_vec(), data: dol.virtual_data_at(dol_section.target, size)?.to_vec(),
align: 0, align: 0,
elf_index: 0, elf_index: 0,
relocations: vec![], relocations: Default::default(),
original_address: 0, original_address: 0,
file_offset: dol_section.offset as u64, file_offset: dol_section.offset as u64,
section_known: known, section_known: known,
@ -299,7 +299,7 @@ pub fn process_dol<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
data: vec![], data: vec![],
align: 0, align: 0,
elf_index: 0, elf_index: 0,
relocations: vec![], relocations: Default::default(),
original_address: 0, original_address: 0,
file_offset: 0, file_offset: 0,
section_known: false, section_known: false,
@ -333,7 +333,7 @@ pub fn process_dol<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
// Create object // Create object
let mut obj = let mut obj =
ObjInfo::new(ObjKind::Executable, ObjArchitecture::PowerPc, name, vec![], sections); ObjInfo::new(ObjKind::Executable, ObjArchitecture::PowerPc, name, vec![], sections);
obj.entry = dol.header.entry_point as u64; obj.entry = Some(dol.header.entry_point as u64);
// Generate _rom_copy_info symbol // Generate _rom_copy_info symbol
if let (Some(rom_copy_info_addr), Some(rom_copy_info_end)) = if let (Some(rom_copy_info_addr), Some(rom_copy_info_end)) =

View File

@ -1,6 +1,7 @@
use std::{ use std::{
collections::{hash_map, HashMap}, collections::{hash_map, HashMap},
io::Cursor, io::Cursor,
num::NonZeroU64,
path::Path, path::Path,
}; };
@ -90,7 +91,7 @@ pub fn process_elf<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
data: section.uncompressed_data()?.to_vec(), data: section.uncompressed_data()?.to_vec(),
align: section.align(), align: section.align(),
elf_index: section.index().0, elf_index: section.index().0,
relocations: vec![], relocations: Default::default(),
original_address: 0, // TODO load from abs symbol original_address: 0, // TODO load from abs symbol
file_offset: section.file_range().map(|(v, _)| v).unwrap_or_default(), file_offset: section.file_range().map(|(v, _)| v).unwrap_or_default(),
section_known: true, section_known: true,
@ -100,21 +101,25 @@ pub fn process_elf<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
let mw_comment = if let Some(comment_section) = obj_file.section_by_name(".comment") { let mw_comment = if let Some(comment_section) = obj_file.section_by_name(".comment") {
let data = comment_section.uncompressed_data()?; let data = comment_section.uncompressed_data()?;
let mut reader = Cursor::new(&*data); if data.is_empty() {
let header = None
MWComment::parse_header(&mut reader).context("While reading .comment section")?; } else {
log::debug!("Loaded .comment section header {:?}", header); let mut reader = Cursor::new(&*data);
let mut comment_syms = Vec::with_capacity(obj_file.symbols().count()); let header =
for symbol in obj_file.symbols() { MWComment::parse_header(&mut reader).context("While reading .comment section")?;
let comment_sym = read_comment_sym(&mut reader)?; log::debug!("Loaded .comment section header {:?}", header);
log::debug!("Symbol {:?} -> Comment {:?}", symbol, comment_sym); let mut comment_syms = Vec::with_capacity(obj_file.symbols().count());
comment_syms.push(comment_sym); for symbol in obj_file.symbols() {
let comment_sym = read_comment_sym(&mut reader)?;
log::debug!("Symbol {:?} -> Comment {:?}", symbol, comment_sym);
comment_syms.push(comment_sym);
}
ensure!(
data.len() - reader.position() as usize == 0,
".comment section data not fully read"
);
Some((header, comment_syms))
} }
ensure!(
data.len() - reader.position() as usize == 0,
".comment section data not fully read"
);
Some((header, comment_syms))
} else { } else {
None None
}; };
@ -285,14 +290,14 @@ pub fn process_elf<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
// Create a map of address -> file splits // Create a map of address -> file splits
for (file_name, section_addrs) in section_starts { for (file_name, section_addrs) in section_starts {
for (address, _) in section_addrs { for (address, _) in section_addrs {
let section = let Some(section) = sections.iter_mut().find(|s| s.contains(address as u32)) else {
sections.iter_mut().find(|s| s.contains(address as u32)).ok_or_else(|| { log::warn!(
anyhow!( "Failed to find section containing address {:#010X} in file {}",
"Failed to find section containing address {:#010X} in file {}", address,
address, file_name
file_name );
) continue;
})?; };
section.splits.push(address as u32, ObjSplit { section.splits.push(address as u32, ObjSplit {
unit: file_name.clone(), unit: file_name.clone(),
end: 0, // TODO end: 0, // TODO
@ -318,12 +323,12 @@ pub fn process_elf<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
else { else {
continue; continue;
}; };
out_section.relocations.push(reloc); out_section.relocations.insert(address as u32, reloc)?;
} }
} }
let mut obj = ObjInfo::new(kind, architecture, obj_name, symbols, sections); let mut obj = ObjInfo::new(kind, architecture, obj_name, symbols, sections);
obj.entry = obj_file.entry(); obj.entry = NonZeroU64::new(obj_file.entry()).map(|n| n.get());
obj.mw_comment = mw_comment.map(|(header, _)| header); obj.mw_comment = mw_comment.map(|(header, _)| header);
obj.sda2_base = sda2_base; obj.sda2_base = sda2_base;
obj.sda_base = sda_base; obj.sda_base = sda_base;
@ -585,7 +590,7 @@ pub fn write_elf(obj: &ObjInfo) -> Result<Vec<u8>> {
ObjKind::Relocatable => elf::ET_REL, ObjKind::Relocatable => elf::ET_REL,
}, },
e_machine: elf::EM_PPC, e_machine: elf::EM_PPC,
e_entry: obj.entry, e_entry: obj.entry.unwrap_or(0),
e_flags: elf::EF_PPC_EMB, e_flags: elf::EF_PPC_EMB,
})?; })?;
@ -627,8 +632,8 @@ pub fn write_elf(obj: &ObjInfo) -> Result<Vec<u8>> {
} }
writer.write_align_relocation(); writer.write_align_relocation();
ensure!(writer.len() == out_section.rela_offset); ensure!(writer.len() == out_section.rela_offset);
for reloc in &section.relocations { for (reloc_address, reloc) in section.relocations.iter() {
let mut r_offset = reloc.address; let mut r_offset = reloc_address as u64;
let r_type = match reloc.kind { let r_type = match reloc.kind {
ObjRelocKind::Absolute => { ObjRelocKind::Absolute => {
if r_offset & 3 == 0 { if r_offset & 3 == 0 {
@ -857,7 +862,5 @@ fn to_obj_reloc(
} }
_ => Err(anyhow!("Unhandled relocation symbol type {:?}", symbol.kind())), _ => Err(anyhow!("Unhandled relocation symbol type {:?}", symbol.kind())),
}?; }?;
let address = address & !3; // TODO hack: round down for instruction Ok(Some(ObjReloc { kind: reloc_kind, target_symbol, addend, module: None }))
let reloc_data = ObjReloc { kind: reloc_kind, address, target_symbol, addend, module: None };
Ok(Some(reloc_data))
} }

View File

@ -56,6 +56,7 @@ pub fn process_rel(mut reader: Reader) -> Result<ObjInfo> {
let mut sections = Vec::with_capacity(num_sections as usize); let mut sections = Vec::with_capacity(num_sections as usize);
reader.set_position(section_info_offset as u64); reader.set_position(section_info_offset as u64);
let mut found_text = false;
let mut total_bss_size = 0; let mut total_bss_size = 0;
for idx in 0..num_sections { for idx in 0..num_sections {
let offset = reader.read_u32::<BigEndian>()?; let offset = reader.read_u32::<BigEndian>()?;
@ -79,15 +80,20 @@ pub fn process_rel(mut reader: Reader) -> Result<ObjInfo> {
// println!("Section {} offset {:#X} size {:#X}", idx, offset, size); // println!("Section {} offset {:#X} size {:#X}", idx, offset, size);
let (name, kind, section_known) = if offset == 0 {
ensure!(total_bss_size == 0, "Multiple BSS sections in REL");
total_bss_size = size;
(".bss".to_string(), ObjSectionKind::Bss, true)
} else if exec {
ensure!(!found_text, "Multiple text sections in REL");
found_text = true;
(".text".to_string(), ObjSectionKind::Code, true)
} else {
(format!(".section{}", idx), ObjSectionKind::Data, false)
};
sections.push(ObjSection { sections.push(ObjSection {
name: format!(".section{}", idx), name,
kind: if offset == 0 { kind,
ObjSectionKind::Bss
} else if exec {
ObjSectionKind::Code
} else {
ObjSectionKind::Data
},
address: 0, address: 0,
size: size as u64, size: size as u64,
data, data,
@ -97,15 +103,12 @@ pub fn process_rel(mut reader: Reader) -> Result<ObjInfo> {
} }
.unwrap_or_default() as u64, .unwrap_or_default() as u64,
elf_index: idx as usize, elf_index: idx as usize,
relocations: vec![], relocations: Default::default(),
original_address: 0, original_address: 0,
file_offset: offset as u64, file_offset: offset as u64,
section_known: false, section_known,
splits: Default::default(), splits: Default::default(),
}); });
if offset == 0 {
total_bss_size += size;
}
} }
ensure!( ensure!(
total_bss_size == bss_size, total_bss_size == bss_size,
@ -212,7 +215,7 @@ pub fn process_rel(mut reader: Reader) -> Result<ObjInfo> {
unresolved_relocations.push(RelReloc { unresolved_relocations.push(RelReloc {
kind, kind,
section, section,
address, address: address & !3,
module_id: reloc_module_id, module_id: reloc_module_id,
target_section, target_section,
addend, addend,

View File

@ -103,7 +103,7 @@ pub fn process_rso<P: AsRef<Path>>(path: P) -> Result<ObjInfo> {
data, data,
align: 0, align: 0,
elf_index: idx as usize, elf_index: idx as usize,
relocations: vec![], relocations: Default::default(),
original_address: 0, original_address: 0,
file_offset: offset as u64, file_offset: offset as u64,
section_known: false, section_known: false,

View File

@ -10,10 +10,14 @@ use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use crate::{ use crate::{
analysis::tracker::{Relocation, Tracker}, analysis::{
cfa::SectionAddress,
tracker::{Relocation, Tracker},
},
array_ref, array_ref,
obj::{ obj::{
ObjInfo, ObjReloc, ObjRelocKind, ObjSection, ObjSymbol, ObjSymbolFlagSet, ObjSymbolKind, ObjInfo, ObjKind, ObjReloc, ObjRelocKind, ObjSection, ObjSymbol, ObjSymbolFlagSet,
ObjSymbolKind,
}, },
util::elf::process_elf, util::elf::process_elf,
}; };
@ -105,8 +109,13 @@ pub fn check_signatures(
Ok(None) Ok(None)
} }
pub fn apply_symbol(obj: &mut ObjInfo, target: u32, sig_symbol: &OutSymbol) -> Result<usize> { pub fn apply_symbol(
let mut target_section_index = obj.sections.at_address(target).ok().map(|(idx, _)| idx); obj: &mut ObjInfo,
target: SectionAddress,
sig_symbol: &OutSymbol,
) -> Result<usize> {
let mut target_section_index =
if target.section == usize::MAX { None } else { Some(target.section) };
if let Some(target_section_index) = target_section_index { if let Some(target_section_index) = target_section_index {
let target_section = &mut obj.sections[target_section_index]; let target_section = &mut obj.sections[target_section_index];
if !target_section.section_known { if !target_section.section_known {
@ -126,7 +135,7 @@ pub fn apply_symbol(obj: &mut ObjInfo, target: u32, sig_symbol: &OutSymbol) -> R
ObjSymbol { ObjSymbol {
name: sig_symbol.name.clone(), name: sig_symbol.name.clone(),
demangled_name, demangled_name,
address: target as u64, address: target.address as u64,
section: target_section_index, section: target_section_index,
size: sig_symbol.size as u64, size: sig_symbol.size as u64,
size_known: sig_symbol.size > 0 || sig_symbol.kind == ObjSymbolKind::Unknown, size_known: sig_symbol.size > 0 || sig_symbol.kind == ObjSymbolKind::Unknown,
@ -142,8 +151,7 @@ pub fn apply_symbol(obj: &mut ObjInfo, target: u32, sig_symbol: &OutSymbol) -> R
pub fn apply_signature( pub fn apply_signature(
obj: &mut ObjInfo, obj: &mut ObjInfo,
section_index: usize, addr: SectionAddress,
addr: u32,
signature: &FunctionSignature, signature: &FunctionSignature,
) -> Result<()> { ) -> Result<()> {
let in_symbol = &signature.symbols[signature.symbol]; let in_symbol = &signature.symbols[signature.symbol];
@ -157,7 +165,7 @@ pub fn apply_signature(
if reloc_addr < addr || reloc_addr >= addr + in_symbol.size { if reloc_addr < addr || reloc_addr >= addr + in_symbol.size {
continue; continue;
} }
let offset = reloc_addr - addr; let offset = reloc_addr.address - addr.address;
let sig_reloc = match signature.relocations.iter().find(|r| r.offset == offset) { let sig_reloc = match signature.relocations.iter().find(|r| r.offset == offset) {
Some(reloc) => reloc, Some(reloc) => reloc,
None => continue, None => continue,
@ -169,22 +177,23 @@ pub fn apply_signature(
| (&Relocation::Lo(addr), ObjRelocKind::PpcAddr16Lo) | (&Relocation::Lo(addr), ObjRelocKind::PpcAddr16Lo)
| (&Relocation::Rel24(addr), ObjRelocKind::PpcRel24) | (&Relocation::Rel24(addr), ObjRelocKind::PpcRel24)
| (&Relocation::Rel14(addr), ObjRelocKind::PpcRel14) | (&Relocation::Rel14(addr), ObjRelocKind::PpcRel14)
| (&Relocation::Sda21(addr), ObjRelocKind::PpcEmbSda21) => { | (&Relocation::Sda21(addr), ObjRelocKind::PpcEmbSda21) => SectionAddress::new(
(addr as i64 - sig_reloc.addend as i64) as u32 addr.section,
} (addr.address as i64 - sig_reloc.addend as i64) as u32,
),
_ => bail!("Relocation mismatch: {:?} != {:?}", reloc, sig_reloc.kind), _ => bail!("Relocation mismatch: {:?} != {:?}", reloc, sig_reloc.kind),
}; };
let sig_symbol = &signature.symbols[sig_reloc.symbol]; let sig_symbol = &signature.symbols[sig_reloc.symbol];
// log::info!("Processing relocation {:#010X} {:?} -> {:#010X} {:?}", reloc_addr, reloc, target, sig_symbol);
let target_symbol_idx = apply_symbol(obj, target, sig_symbol)?; let target_symbol_idx = apply_symbol(obj, target, sig_symbol)?;
let obj_reloc = ObjReloc { let obj_reloc = ObjReloc {
kind: sig_reloc.kind, kind: sig_reloc.kind,
address: reloc_addr as u64,
target_symbol: target_symbol_idx, target_symbol: target_symbol_idx,
addend: sig_reloc.addend as i64, addend: sig_reloc.addend as i64,
module: None, module: None,
}; };
// log::info!("Applying relocation {:#010X?}", obj_reloc); // log::info!("Applying relocation {:#010X?}", obj_reloc);
obj.sections[section_index].relocations.push(obj_reloc); obj.sections[addr.section].relocations.insert(reloc_addr.address, obj_reloc)?;
} }
for reloc in &signature.relocations { for reloc in &signature.relocations {
let addr = addr + reloc.offset; let addr = addr + reloc.offset;
@ -244,11 +253,12 @@ pub fn generate_signature<P: AsRef<Path>>(
let mut symbol_map: BTreeMap<usize, usize> = BTreeMap::new(); let mut symbol_map: BTreeMap<usize, usize> = BTreeMap::new();
let mut obj = process_elf(path)?; let mut obj = process_elf(path)?;
if obj.sda2_base.is_none() if obj.kind == ObjKind::Executable
|| obj.sda_base.is_none() && (obj.sda2_base.is_none()
|| obj.stack_address.is_none() || obj.sda_base.is_none()
|| obj.stack_end.is_none() || obj.stack_address.is_none()
|| obj.db_stack_addr.is_none() || obj.stack_end.is_none()
|| obj.db_stack_addr.is_none())
{ {
log::warn!( log::warn!(
"Failed to locate all abs symbols {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?}", "Failed to locate all abs symbols {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?} {:#010X?}",
@ -291,7 +301,6 @@ pub fn generate_signature<P: AsRef<Path>>(
// symbol.address, // symbol.address,
// symbol.address + symbol.size // symbol.address + symbol.size
// ); // );
let relocations = section.build_relocation_map()?;
let mut instructions = section.data[(symbol.address - section.address) as usize let mut instructions = section.data[(symbol.address - section.address) as usize
..(symbol.address - section.address + symbol.size) as usize] ..(symbol.address - section.address + symbol.size) as usize]
.chunks_exact(4) .chunks_exact(4)
@ -299,8 +308,7 @@ pub fn generate_signature<P: AsRef<Path>>(
.collect::<Vec<(u32, u32)>>(); .collect::<Vec<(u32, u32)>>();
for (idx, (ins, pat)) in instructions.iter_mut().enumerate() { for (idx, (ins, pat)) in instructions.iter_mut().enumerate() {
let addr = (symbol.address as usize + idx * 4) as u32; let addr = (symbol.address as usize + idx * 4) as u32;
if let Some(&reloc_idx) = relocations.get(&addr) { if let Some(reloc) = section.relocations.at(addr) {
let reloc = &section.relocations[reloc_idx];
let symbol_idx = match symbol_map.entry(reloc.target_symbol) { let symbol_idx = match symbol_map.entry(reloc.target_symbol) {
btree_map::Entry::Vacant(e) => { btree_map::Entry::Vacant(e) => {
let target = &obj.symbols[reloc.target_symbol]; let target = &obj.symbols[reloc.target_symbol];

View File

@ -3,40 +3,35 @@ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
}; };
use anyhow::{anyhow, bail, ensure, Result}; use anyhow::{anyhow, bail, ensure, Context, Result};
use itertools::Itertools; use itertools::Itertools;
use petgraph::{graph::NodeIndex, Graph}; use petgraph::{graph::NodeIndex, Graph};
use crate::{ use crate::{
array_ref, analysis::{cfa::SectionAddress, read_address, read_u32},
obj::{ obj::{
ObjArchitecture, ObjInfo, ObjKind, ObjReloc, ObjSection, ObjSectionKind, ObjSplit, ObjArchitecture, ObjInfo, ObjKind, ObjReloc, ObjRelocations, ObjSection, ObjSectionKind,
ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, ObjSymbolScope, ObjUnit, ObjSplit, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, ObjSymbolScope,
ObjUnit,
}, },
util::comment::MWComment, util::comment::MWComment,
}; };
/// Create splits for function pointers in the given section. /// Create splits for function pointers in the given section.
fn split_ctors_dtors( fn split_ctors_dtors(obj: &mut ObjInfo, start: SectionAddress, end: SectionAddress) -> Result<()> {
obj: &mut ObjInfo, let ctors_section = &obj.sections[start.section];
ctors_section_index: usize, let mut new_splits = BTreeMap::<SectionAddress, ObjSplit>::new();
start: u32,
end: u32,
) -> Result<()> {
let ctors_section = &obj.sections[ctors_section_index];
let mut new_splits = BTreeMap::<u32, (usize, ObjSplit)>::new();
let mut current_address = start; let mut current_address = start;
let mut referenced_symbols = vec![]; let mut referenced_symbols = vec![];
while current_address < end { while current_address < end {
let chunk = ctors_section.data_range(current_address, current_address + 4)?; let function_addr = read_address(obj, ctors_section, current_address.address)?;
let function_addr = u32::from_be_bytes(*array_ref!(chunk, 0, 4));
log::debug!("Found {} entry: {:#010X}", ctors_section.name, function_addr); log::debug!("Found {} entry: {:#010X}", ctors_section.name, function_addr);
let (text_section_index, text_section) = obj.sections.at_address(function_addr)?; let text_section = &obj.sections[function_addr.section];
let Some((function_symbol_idx, function_symbol)) = obj.symbols.kind_at_section_address( let Some((function_symbol_idx, function_symbol)) = obj.symbols.kind_at_section_address(
text_section_index, function_addr.section,
function_addr, function_addr.address,
ObjSymbolKind::Function, ObjSymbolKind::Function,
)? )?
else { else {
@ -44,8 +39,8 @@ fn split_ctors_dtors(
}; };
referenced_symbols.push(function_symbol_idx); referenced_symbols.push(function_symbol_idx);
let ctors_split = ctors_section.splits.for_address(current_address); let ctors_split = ctors_section.splits.for_address(current_address.address);
let function_split = text_section.splits.for_address(function_addr); let function_split = text_section.splits.for_address(function_addr.address);
let mut expected_unit = None; let mut expected_unit = None;
if let Some((_, ctors_split)) = ctors_split { if let Some((_, ctors_split)) = ctors_split {
@ -83,37 +78,31 @@ fn split_ctors_dtors(
ctors_section.name, ctors_section.name,
current_address current_address
); );
new_splits.insert( new_splits.insert(current_address, ObjSplit {
current_address, unit: unit.clone(),
(ctors_section_index, ObjSplit { end: current_address.address + 4,
unit: unit.clone(), align: None,
end: current_address + 4, common: false,
align: None, autogenerated: true,
common: false, });
autogenerated: true,
}),
);
} }
if function_split.is_none() { if function_split.is_none() {
log::debug!("Adding split for function @ {:#010X}", function_addr); log::debug!("Adding split for function @ {:#010X}", function_addr);
new_splits.insert( new_splits.insert(function_addr, ObjSplit {
function_addr, unit,
(text_section_index, ObjSplit { end: function_addr.address + function_symbol.size as u32,
unit, align: None,
end: function_addr + function_symbol.size as u32, common: false,
align: None, autogenerated: true,
common: false, });
autogenerated: true,
}),
);
} }
} }
current_address += 4; current_address += 4;
} }
for (addr, (section_index, split)) in new_splits { for (addr, split) in new_splits {
obj.add_split(section_index, addr, split)?; obj.add_split(addr.section, addr.address, split)?;
} }
// Hack to avoid deadstripping // Hack to avoid deadstripping
@ -125,18 +114,18 @@ fn split_ctors_dtors(
} }
/// Create splits for extabindex + extab entries. /// Create splits for extabindex + extab entries.
fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Result<()> { fn split_extabindex(obj: &mut ObjInfo, start: SectionAddress) -> Result<()> {
let section = &obj.sections[section_index]; let section = &obj.sections[start.section];
let mut new_splits = BTreeMap::<u32, (usize, ObjSplit)>::new(); let mut new_splits = BTreeMap::<SectionAddress, ObjSplit>::new();
let (_, eti_init_info) = obj let (_, eti_init_info) = obj
.symbols .symbols
.by_name("_eti_init_info")? .by_name("_eti_init_info")?
.ok_or_else(|| anyhow!("Failed to find _eti_init_info symbol"))?; .ok_or_else(|| anyhow!("Failed to find _eti_init_info symbol"))?;
ensure!( ensure!(
eti_init_info.section == Some(section_index), eti_init_info.section == Some(start.section),
"_eti_init_info symbol in the wrong section: {:?} != {}", "_eti_init_info symbol in the wrong section: {:?} != {}",
eti_init_info.section, eti_init_info.section,
section_index start.section
); );
let (extab_section_index, extab_section) = let (extab_section_index, extab_section) =
@ -144,11 +133,21 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
let mut current_address = start; let mut current_address = start;
let section_end = eti_init_info.address as u32; let section_end = eti_init_info.address as u32;
while current_address < section_end { while current_address.address < section_end {
let chunk = section.data_range(current_address, current_address + 12)?; let function_addr = read_address(obj, section, current_address.address)?;
let function_addr = u32::from_be_bytes(*array_ref!(chunk, 0, 4)); let function_size = read_u32(section, current_address.address + 4).with_context(|| {
let function_size = u32::from_be_bytes(*array_ref!(chunk, 4, 4)); format!(
let extab_addr = u32::from_be_bytes(*array_ref!(chunk, 8, 4)); "Failed to read extabindex entry function size @ {:#010X}",
current_address.address + 4
)
})?;
let extab_addr = read_address(obj, section, current_address.address + 8)?;
ensure!(
extab_addr.section == extab_section_index,
"extabindex entry @ {:#010X} has invalid extab address {:#010X}",
current_address,
extab_addr
);
log::debug!( log::debug!(
"Found extabindex entry: {:#010X} size {:#010X} extab {:#010X}", "Found extabindex entry: {:#010X} size {:#010X} extab {:#010X}",
function_addr, function_addr,
@ -157,8 +156,8 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
); );
let Some((_, eti_symbol)) = obj.symbols.kind_at_section_address( let Some((_, eti_symbol)) = obj.symbols.kind_at_section_address(
section_index, current_address.section,
current_address, current_address.address,
ObjSymbolKind::Object, ObjSymbolKind::Object,
)? )?
else { else {
@ -172,10 +171,10 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
12 12
); );
let (text_section_index, text_section) = obj.sections.at_address(function_addr)?; let text_section = &obj.sections[function_addr.section];
let Some((_, function_symbol)) = obj.symbols.kind_at_section_address( let Some((_, function_symbol)) = obj.symbols.kind_at_section_address(
text_section_index, function_addr.section,
function_addr, function_addr.address,
ObjSymbolKind::Function, ObjSymbolKind::Function,
)? )?
else { else {
@ -190,8 +189,8 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
); );
let Some((_, extab_symbol)) = obj.symbols.kind_at_section_address( let Some((_, extab_symbol)) = obj.symbols.kind_at_section_address(
extab_section_index, extab_addr.section,
extab_addr, extab_addr.address,
ObjSymbolKind::Object, ObjSymbolKind::Object,
)? )?
else { else {
@ -203,9 +202,9 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
extab_symbol.name extab_symbol.name
); );
let extabindex_split = section.splits.for_address(current_address); let extabindex_split = section.splits.for_address(current_address.address);
let extab_split = extab_section.splits.for_address(extab_addr); let extab_split = extab_section.splits.for_address(extab_addr.address);
let function_split = text_section.splits.for_address(function_addr); let function_split = text_section.splits.for_address(function_addr.address);
let mut expected_unit = None; let mut expected_unit = None;
if let Some((_, extabindex_split)) = extabindex_split { if let Some((_, extabindex_split)) = extabindex_split {
@ -257,52 +256,43 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
current_address, current_address,
end end
); );
new_splits.insert( new_splits.insert(current_address, ObjSplit {
current_address, unit: unit.clone(),
(section_index, ObjSplit { end: end.address,
unit: unit.clone(), align: None,
end, common: false,
align: None, autogenerated: true,
common: false, });
autogenerated: true,
}),
);
} }
if extab_split.is_none() { if extab_split.is_none() {
let end = extab_addr + extab_symbol.size as u32; let end = extab_addr + extab_symbol.size as u32;
log::debug!("Adding split for extab @ {:#010X}-{:#010X}", extab_addr, end); log::debug!("Adding split for extab @ {:#010X}-{:#010X}", extab_addr, end);
new_splits.insert( new_splits.insert(extab_addr, ObjSplit {
extab_addr, unit: unit.clone(),
(extab_section_index, ObjSplit { end: end.address,
unit: unit.clone(), align: None,
end, common: false,
align: None, autogenerated: true,
common: false, });
autogenerated: true,
}),
);
} }
if function_split.is_none() { if function_split.is_none() {
let end = function_addr + function_symbol.size as u32; let end = function_addr + function_symbol.size as u32;
log::debug!("Adding split for function @ {:#010X}-{:#010X}", function_addr, end); log::debug!("Adding split for function @ {:#010X}-{:#010X}", function_addr, end);
new_splits.insert( new_splits.insert(function_addr, ObjSplit {
function_addr, unit,
(text_section_index, ObjSplit { end: end.address,
unit, align: None,
end, common: false,
align: None, autogenerated: true,
common: false, });
autogenerated: true,
}),
);
} }
} }
current_address += 12; current_address += 12;
} }
for (addr, (section_index, split)) in new_splits { for (addr, split) in new_splits {
obj.add_split(section_index, addr, split)?; obj.add_split(addr.section, addr.address, split)?;
} }
Ok(()) Ok(())
@ -310,12 +300,16 @@ fn split_extabindex(obj: &mut ObjInfo, section_index: usize, start: u32) -> Resu
/// Create splits for gaps between existing splits. /// Create splits for gaps between existing splits.
fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> { fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
let mut new_splits = BTreeMap::<u32, (usize, ObjSplit)>::new(); let mut new_splits = BTreeMap::<SectionAddress, ObjSplit>::new();
for (section_index, section) in obj.sections.iter() { for (section_index, section) in obj.sections.iter() {
let mut current_address = section.address as u32; let mut current_address = SectionAddress::new(section_index, section.address as u32);
let section_end = end_for_section(obj, section_index)?; let section_end = end_for_section(obj, section_index)?;
let mut file_iter = section.splits.for_range(..section_end).peekable(); let mut file_iter = section
.splits
.for_range(..section_end.address)
.map(|(addr, split)| (SectionAddress::new(section_index, addr), split))
.peekable();
log::debug!( log::debug!(
"Checking splits for section {} ({:#010X}..{:#010X})", "Checking splits for section {} ({:#010X}..{:#010X})",
@ -331,9 +325,9 @@ fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
let (split_start, split_end) = match file_iter.peek() { let (split_start, split_end) = match file_iter.peek() {
Some(&(addr, split)) => { Some(&(addr, split)) => {
log::debug!("Found split {} ({:#010X}..{:#010X})", split.unit, addr, split.end); log::debug!("Found split {} ({:#010X}..{:#010X})", split.unit, addr, split.end);
(addr, split.end) (addr, SectionAddress::new(section_index, split.end))
} }
None => (section_end, 0), None => (section_end, SectionAddress::new(section_index, 0)),
}; };
ensure!( ensure!(
split_start >= current_address, split_start >= current_address,
@ -347,7 +341,7 @@ fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
let mut new_split_end = split_start; let mut new_split_end = split_start;
let symbols = obj let symbols = obj
.symbols .symbols
.for_section_range(section_index, current_address..split_start) .for_section_range(section_index, current_address.address..split_start.address)
.collect_vec(); .collect_vec();
let mut existing_symbols = HashSet::new(); let mut existing_symbols = HashSet::new();
for (_, symbol) in symbols { for (_, symbol) in symbols {
@ -357,7 +351,7 @@ fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
symbol.name, symbol.name,
symbol.address symbol.address
); );
new_split_end = symbol.address as u32; new_split_end.address = symbol.address as u32;
break; break;
} }
} }
@ -367,24 +361,25 @@ fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
current_address, current_address,
new_split_end new_split_end
); );
let unit = let unit = format!(
format!("{:08X}_{}", current_address, section.name.trim_start_matches('.')); "{:02}_{:08X}_{}",
new_splits.insert( current_address.section,
current_address, current_address.address,
(section_index, ObjSplit { section.name.trim_start_matches('.')
unit: unit.clone(),
end: new_split_end,
align: None,
common: false,
autogenerated: true,
}),
); );
new_splits.insert(current_address, ObjSplit {
unit: unit.clone(),
end: new_split_end.address,
align: None,
common: false,
autogenerated: true,
});
current_address = new_split_end; current_address = new_split_end;
continue; continue;
} }
file_iter.next(); file_iter.next();
if split_end > 0 { if split_end.address > 0 {
current_address = split_end; current_address = split_end;
} else { } else {
let mut file_end = section_end; let mut file_end = section_end;
@ -397,8 +392,8 @@ fn create_gap_splits(obj: &mut ObjInfo) -> Result<()> {
} }
// Add new splits // Add new splits
for (addr, (section_index, split)) in new_splits { for (addr, split) in new_splits {
obj.add_split(section_index, addr, split)?; obj.add_split(addr.section, addr.address, split)?;
} }
Ok(()) Ok(())
@ -427,10 +422,11 @@ fn update_common_splits(obj: &mut ObjInfo) -> Result<()> {
/// Final validation of splits. /// Final validation of splits.
fn validate_splits(obj: &ObjInfo) -> Result<()> { fn validate_splits(obj: &ObjInfo) -> Result<()> {
let mut last_split_end = 0; let mut last_split_end = SectionAddress::new(0, 0);
for (section_index, section, addr, split) in obj.sections.all_splits() { for (section_index, section, addr, split) in obj.sections.all_splits() {
let split_addr = SectionAddress::new(section_index, addr);
ensure!( ensure!(
addr >= last_split_end, split_addr >= last_split_end,
"Split {} {} {:#010X}..{:#010X} overlaps with previous split", "Split {} {} {:#010X}..{:#010X} overlaps with previous split",
split.unit, split.unit,
section.name, section.name,
@ -445,7 +441,7 @@ fn validate_splits(obj: &ObjInfo) -> Result<()> {
addr, addr,
split.end split.end
); );
last_split_end = split.end; last_split_end = SectionAddress::new(section_index, split.end);
if let Some((_, symbol)) = obj if let Some((_, symbol)) = obj
.symbols .symbols
@ -497,22 +493,26 @@ fn validate_splits(obj: &ObjInfo) -> Result<()> {
pub fn update_splits(obj: &mut ObjInfo) -> Result<()> { pub fn update_splits(obj: &mut ObjInfo) -> Result<()> {
// Create splits for extab and extabindex entries // Create splits for extab and extabindex entries
if let Some((section_index, section)) = obj.sections.by_name("extabindex")? { if let Some((section_index, section)) = obj.sections.by_name("extabindex")? {
let start = section.address as u32; let start = SectionAddress::new(section_index, section.address as u32);
split_extabindex(obj, section_index, start)?; split_extabindex(obj, start)?;
} }
// Create splits for .ctors entries // Create splits for .ctors entries
if let Some((section_index, section)) = obj.sections.by_name(".ctors")? { if let Some((section_index, section)) = obj.sections.by_name(".ctors")? {
let start = section.address as u32; let start = SectionAddress::new(section_index, section.address as u32);
let end = section.address as u32 + section.size as u32 - 4; let end = start + (section.size as u32 - 4);
split_ctors_dtors(obj, section_index, start, end)?; split_ctors_dtors(obj, start, end)?;
} }
// Create splits for .dtors entries // Create splits for .dtors entries
if let Some((section_index, section)) = obj.sections.by_name(".dtors")? { if let Some((section_index, section)) = obj.sections.by_name(".dtors")? {
let start = section.address as u32 + 4; // skip __destroy_global_chain_reference let mut start = SectionAddress::new(section_index, section.address as u32);
let end = section.address as u32 + section.size as u32 - 4; let end = start + (section.size as u32 - 4);
split_ctors_dtors(obj, section_index, start, end)?; if obj.kind == ObjKind::Executable {
// Skip __destroy_global_chain_reference
start += 4;
}
split_ctors_dtors(obj, start, end)?;
} }
// Create gap splits // Create gap splits
@ -644,13 +644,14 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
objects.push(split_obj); objects.push(split_obj);
} }
for (section_idx, section) in obj.sections.iter() { for (section_index, section) in obj.sections.iter() {
let mut current_address = section.address as u32; let mut current_address = SectionAddress::new(section_index, section.address as u32);
let section_end = end_for_section(obj, section_idx)?; let section_end = end_for_section(obj, section_index)?;
let mut file_iter = section.splits.for_range(current_address..section_end).peekable(); let mut file_iter = section
.splits
// Build address to relocation / address to symbol maps .for_range(current_address.address..section_end.address)
let relocations = section.build_relocation_map()?; .map(|(addr, split)| (SectionAddress::new(section_index, addr), split))
.peekable();
loop { loop {
if current_address >= section_end { if current_address >= section_end {
@ -710,40 +711,41 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
); );
// Collect relocations; target_symbol will be updated later // Collect relocations; target_symbol will be updated later
let out_relocations = relocations let out_relocations = section
.range(current_address..file_end) .relocations
.map(|(_, &idx)| { .range(current_address.address..file_end.address)
let o = &section.relocations[idx]; .map(|(addr, o)| {
ObjReloc { (addr - current_address.address, ObjReloc {
kind: o.kind, kind: o.kind,
address: o.address - current_address as u64,
target_symbol: o.target_symbol, target_symbol: o.target_symbol,
addend: o.addend, addend: o.addend,
module: o.module, module: o.module,
} })
}) })
.collect(); .collect_vec();
// Add section symbols // Add section symbols
let out_section_idx = file.sections.next_section_index(); let out_section_idx = file.sections.next_section_index();
let mut comm_addr = current_address; let mut comm_addr = current_address;
for (symbol_idx, symbol) in for (symbol_idx, symbol) in obj
obj.symbols.for_section_range(section_idx, current_address..file_end).filter( .symbols
|&(_, s)| s.section == Some(section_idx) && !is_linker_generated_label(&s.name), .for_section_range(section_index, current_address.address..file_end.address)
) .filter(|&(_, s)| {
s.section == Some(section_index) && !is_linker_generated_label(&s.name)
})
{ {
if symbol_idxs[symbol_idx].is_some() { if symbol_idxs[symbol_idx].is_some() {
continue; // should never happen? continue; // should never happen?
} }
if split.common && symbol.address as u32 > comm_addr { if split.common && symbol.address as u32 > comm_addr.address {
// HACK: Add padding for common bug // HACK: Add padding for common bug
file.symbols.add_direct(ObjSymbol { file.symbols.add_direct(ObjSymbol {
name: format!("pad_{:010X}", comm_addr), name: format!("pad_{:010X}", comm_addr),
demangled_name: None, demangled_name: None,
address: 0, address: 0,
section: None, section: None,
size: symbol.address - comm_addr as u64, size: symbol.address - comm_addr.address as u64,
size_known: true, size_known: true,
flags: ObjSymbolFlagSet(ObjSymbolFlags::Common.into()), flags: ObjSymbolFlagSet(ObjSymbolFlags::Common.into()),
kind: ObjSymbolKind::Object, kind: ObjSymbolKind::Object,
@ -751,12 +753,16 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
data_kind: Default::default(), data_kind: Default::default(),
})?; })?;
} }
comm_addr = (symbol.address + symbol.size) as u32; comm_addr.address = (symbol.address + symbol.size) as u32;
symbol_idxs[symbol_idx] = Some(file.symbols.add_direct(ObjSymbol { symbol_idxs[symbol_idx] = Some(file.symbols.add_direct(ObjSymbol {
name: symbol.name.clone(), name: symbol.name.clone(),
demangled_name: symbol.demangled_name.clone(), demangled_name: symbol.demangled_name.clone(),
address: if split.common { 4 } else { symbol.address - current_address as u64 }, address: if split.common {
4
} else {
symbol.address - current_address.address as u64
},
section: if split.common { None } else { Some(out_section_idx) }, section: if split.common { None } else { Some(out_section_idx) },
size: symbol.size, size: symbol.size,
size_known: symbol.size_known, size_known: symbol.size_known,
@ -780,11 +786,11 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
if !split.common { if !split.common {
let data = match section.kind { let data = match section.kind {
ObjSectionKind::Bss => vec![], ObjSectionKind::Bss => vec![],
_ => section.data[(current_address as u64 - section.address) as usize _ => section.data[(current_address.address as u64 - section.address) as usize
..(file_end as u64 - section.address) as usize] ..(file_end.address as u64 - section.address) as usize]
.to_vec(), .to_vec(),
}; };
let name = if let Some(name) = obj.named_sections.get(&current_address) { let name = if let Some(name) = obj.named_sections.get(&current_address.address) {
name.clone() name.clone()
} else { } else {
section.name.clone() section.name.clone()
@ -793,13 +799,14 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
name, name,
kind: section.kind, kind: section.kind,
address: 0, address: 0,
size: file_end as u64 - current_address as u64, size: file_end.address as u64 - current_address.address as u64,
data, data,
align, align,
elf_index: out_section_idx + 1, elf_index: out_section_idx + 1,
relocations: out_relocations, relocations: ObjRelocations::new(out_relocations)?,
original_address: current_address as u64, original_address: current_address.address as u64,
file_offset: section.file_offset + (current_address as u64 - section.address), file_offset: section.file_offset
+ (current_address.address as u64 - section.address),
section_known: true, section_known: true,
splits: Default::default(), splits: Default::default(),
}); });
@ -814,7 +821,7 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
for (obj_idx, out_obj) in objects.iter_mut().enumerate() { for (obj_idx, out_obj) in objects.iter_mut().enumerate() {
let symbol_idxs = &mut object_symbols[obj_idx]; let symbol_idxs = &mut object_symbols[obj_idx];
for (_section_index, section) in out_obj.sections.iter_mut() { for (_section_index, section) in out_obj.sections.iter_mut() {
for reloc in &mut section.relocations { for (reloc_address, reloc) in section.relocations.iter_mut() {
match symbol_idxs[reloc.target_symbol] { match symbol_idxs[reloc.target_symbol] {
Some(out_sym_idx) => { Some(out_sym_idx) => {
reloc.target_symbol = out_sym_idx; reloc.target_symbol = out_sym_idx;
@ -850,7 +857,7 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
else { else {
bail!( bail!(
"Bad extabindex relocation @ {:#010X}", "Bad extabindex relocation @ {:#010X}",
reloc.address + section.original_address reloc_address as u64 + section.original_address
); );
}; };
let target_section = &obj.sections.at_address(target_addr)?.1.name; let target_section = &obj.sections.at_address(target_addr)?.1.name;
@ -860,7 +867,7 @@ pub fn split_obj(obj: &ObjInfo) -> Result<Vec<ObjInfo>> {
\tTarget object: {}:{:#010X} ({})\n\ \tTarget object: {}:{:#010X} ({})\n\
\tTarget symbol: {:#010X} ({})\n\ \tTarget symbol: {:#010X} ({})\n\
This will cause the linker to crash.\n", This will cause the linker to crash.\n",
reloc.address + section.original_address, reloc_address as u64 + section.original_address,
section.name, section.name,
section.original_address, section.original_address,
out_obj.name, out_obj.name,
@ -999,7 +1006,7 @@ pub fn is_linker_generated_object(name: &str) -> bool {
} }
/// Locate the end address of a section when excluding linker generated objects /// Locate the end address of a section when excluding linker generated objects
pub fn end_for_section(obj: &ObjInfo, section_index: usize) -> Result<u32> { pub fn end_for_section(obj: &ObjInfo, section_index: usize) -> Result<SectionAddress> {
let section = obj let section = obj
.sections .sections
.get(section_index) .get(section_index)
@ -1011,7 +1018,7 @@ pub fn end_for_section(obj: &ObjInfo, section_index: usize) -> Result<u32> {
&& section.data[section.data.len() - 4..] == [0u8; 4] && section.data[section.data.len() - 4..] == [0u8; 4]
{ {
section_end -= 4; section_end -= 4;
return Ok(section_end); return Ok(SectionAddress::new(section_index, section_end));
} }
loop { loop {
let last_symbol = obj let last_symbol = obj
@ -1033,5 +1040,5 @@ pub fn end_for_section(obj: &ObjInfo, section_index: usize) -> Result<u32> {
_ => break, _ => break,
} }
} }
Ok(section_end) Ok(SectionAddress::new(section_index, section_end))
} }