From dd05a3392eee6e5f27b1cf836d6a436dfd89b70b Mon Sep 17 00:00:00 2001 From: Charlotte Delenk Date: Thu, 14 Oct 2021 09:55:37 +0200 Subject: [PATCH] decompiled __set_debug_bba and __get_debug_bba --- .gitignore | 3 ++- data/objects.txt | 17 +++-------------- data/ranges.csv | 11 +++++------ data/symbols.csv | 1 + rules.ninja | 3 +++ sdk/rvl/os/__start.c | 8 ++++++-- src/bin/buildgen.rs | 43 ++++++++++++++++++++++++++++++++++++++----- src/dol/mod.rs | 1 + src/ninja/mod.rs | 23 +++++++++++++++++++++++ src/range.rs | 3 +++ 10 files changed, 85 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 77c218fe..56bbb817 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /build.ninja /build .ninja_log -.ninja_deps \ No newline at end of file +.ninja_deps +.~lock.*# \ No newline at end of file diff --git a/data/objects.txt b/data/objects.txt index 13d8799c..249ce990 100644 --- a/data/objects.txt +++ b/data/objects.txt @@ -1,15 +1,4 @@ -build/obj/init_80004000_80006310.o +build/obj/0.o build/obj/sdk/rvl/os/__start.o -build/obj/init_8000661c_80006684.o -build/obj/extab_80006740_800068cc.o -build/obj/extabindex_800068e0_800069e4.o -build/obj/text_80006a00_80406244.o -build/obj/ctors_80406260_8040652c.o -build/obj/dtors_80406544_80406548.o -build/obj/rodata_80406560_80421030.o -build/obj/data_80421040_804966fc.o -build/obj/bss_80496700_8055640c.o -build/obj/sdata_80556420_8055c6d0.o -build/obj/sbss_8055c6e0_8055df74.o -build/obj/sdata2_8055df80_805643b0.o -build/obj/sbss2_805643c0_805643fc.o +build/obj/2.o +build/obj/3.o \ No newline at end of file diff --git a/data/ranges.csv b/data/ranges.csv index fbeb43c1..4863307c 100644 --- a/data/ranges.csv +++ b/data/ranges.csv @@ -1,6 +1,5 @@ -StartAddress,EndAddress,Section,File,function -80006310,8000661c,.init,__start.o,__start -80006684,80006728,.init,linker generated,Symbol is generated by the linker automatically -8040652c,80406530,.ctors,linker generated,Symbol is generated by the linker automatically -80406540,80406544,.dtors,linker generated,Symbol is generated by the linker automatically -80406548,8040654c,.dtors,linker generated,Symbol is generated by the linker automatically \ No newline at end of file +.init start,.init end,extab start,extab end,extabindex start,extabindex end,.text start,.text end,.ctors start,.ctors end,.dtors start,.dtors end,.rodata start,.rodata end,.data start,.data end,.bss start,.bss end,.sdata start,.sdata end,.sbss start,.sbss end,.sdata2 start,.sdata2 end,.sbss2 start,.sbss2 end,File +800062f0,8000661c,,,,,,,,,,,,,,,,,,,8055c878,8055c879,,,,,sdk/rvl/os/__start.o +,,,,,,,,80406540,80406544,,,,,,,,,,,,,,,,,Linker generated symbol file +80006684,80006728,,,,,,,8040652c,80406530,80406548,8040654c,,,,,,,,,,,,,,,Linker generated symbol file +80006728,80006728,800068cc,800068cc,800069e4,800069e4,80406244,80406244,80406530,80406530,8040654c,8040654c,80421030,80421030,804966fc,804966fc,8055640c,8055640c,8055c6d0,8055c6d0,8055df74,8055df74,805643b0,805643b0,805643fc,805643fc,End of File diff --git a/data/symbols.csv b/data/symbols.csv index c587783d..610b11e2 100644 --- a/data/symbols.csv +++ b/data/symbols.csv @@ -19,3 +19,4 @@ Address,SymbolName 80028530,__init_user 800285a0,exit 80174ad8,main +8055c878,Debug_BBA diff --git a/rules.ninja b/rules.ninja index c3ee46aa..3529b447 100644 --- a/rules.ninja +++ b/rules.ninja @@ -17,3 +17,6 @@ rule cc rule elf2dol command = cargo run --quiet --release --bin elf2dol -- $in $out + +rule mergeobj + command = ld.lld -relocatable $in -o $out diff --git a/sdk/rvl/os/__start.c b/sdk/rvl/os/__start.c index b70eab95..29bada2b 100644 --- a/sdk/rvl/os/__start.c +++ b/sdk/rvl/os/__start.c @@ -1,11 +1,9 @@ void __init_registers(void); void __init_data(void); void __init_hardware(void); -void __set_debug_bba(void); void InitMetroTRK(void); void OSInit(void); void __check_pad3(void); -void __get_debug_bba(void); void InitMetroTRK_BBA(void); void __init_user(void); int main(int argc, char **argv); @@ -20,6 +18,12 @@ extern int _SDA_BASE_; extern int _rom_copy_info; extern int _bss_init_info; +static char Debug_BBA; + +__declspec(section ".init") void __set_debug_bba() { Debug_BBA = 1; } + +__declspec(section ".init") char __get_debug_bba() { return Debug_BBA; } + __declspec(section ".init") asm void start() { // clang-format off nofralloc; diff --git a/src/bin/buildgen.rs b/src/bin/buildgen.rs index 50f3ac16..ecb46237 100644 --- a/src/bin/buildgen.rs +++ b/src/bin/buildgen.rs @@ -36,15 +36,18 @@ fn main() -> Result<()> { for result in rdr.records() { let result = result?; - ranges.remove_range( - parse_hex(result.get(0).unwrap())?, - parse_hex(result.get(1).unwrap())?, - ); + for i in 0..13 { + if &result[2 * i] == "" { + continue; + } + ranges.remove_range(parse_hex(&result[2 * i])?, parse_hex(&result[2 * i + 1])?); + } } ranges.merge_ranges(); - for (start, end) in ranges.into_iter() { + for (start, end) in ranges.iter() { + let (start, end) = (*start, *end); let objfile = format!( "build/obj/{}_{:08x}_{:08x}.o", dolfile.get_section_name(start), @@ -54,6 +57,36 @@ fn main() -> Result<()> { ninja.emit_extract("bin/donut/donut.dol", &objfile, start, end)?; } + let mut rdr = csv::Reader::from_reader(BufReader::new(File::open("data/ranges.csv")?)); + + for (i, result) in rdr.records().enumerate() { + let result = result?; + let mut start_addrs = Vec::new(); + for i in 0..13 { + if &result[2 * i] == "" { + continue; + } + start_addrs.push(parse_hex(&result[2 * i])?); + } + let mut to_merge = Vec::new(); + for (start, end) in ranges.iter() { + let (start, end) = (*start, *end); + for start_addr in start_addrs.iter() { + if *start_addr == end { + to_merge.push(format!( + "build/obj/{}_{:08x}_{:08x}.o", + dolfile.get_section_name(start), + start, + end + )); + } + } + } + if !to_merge.is_empty() { + ninja.emit_merge(format!("build/obj/{}.o", i), &to_merge)?; + } + } + for source in SOURCES.iter() { ninja.emit_cc(source.0, source.1)?; } diff --git a/src/dol/mod.rs b/src/dol/mod.rs index 1f4ec041..90c664d2 100644 --- a/src/dol/mod.rs +++ b/src/dol/mod.rs @@ -89,6 +89,7 @@ pub struct Dol { section_sizes: [u32; 18], bss_address: u32, bss_size: u32, + #[allow(dead_code)] entry_point: u32, reader: R, } diff --git a/src/ninja/mod.rs b/src/ninja/mod.rs index 679772c6..97937b61 100644 --- a/src/ninja/mod.rs +++ b/src/ninja/mod.rs @@ -85,6 +85,29 @@ impl NinjaFile { self.emit_mkdir(parent)?; Ok(()) } + + pub fn emit_merge( + &mut self, + outfile: impl AsRef, + objs: &[impl AsRef], + ) -> Result<()> { + let outfile = outfile.as_ref(); + if self.emitted_targets.contains(outfile) { + return Ok(()); + } + self.emitted_targets.insert(outfile.to_owned()); + let parent = outfile.parent().unwrap(); + self.writer + .write_all(format!("build {}: mergeobj ", outfile.display()).as_bytes())?; + for object in objs.iter() { + self.writer + .write_all(format!("{} ", object.as_ref().display()).as_bytes())?; + } + self.writer.write_all(b"\n")?; + self.emit_mkdir(parent)?; + Ok(()) + } + pub fn emit_genlcf(&mut self, outfile: impl AsRef) -> Result<()> { let outfile = outfile.as_ref(); if self.emitted_targets.contains(outfile) { diff --git a/src/range.rs b/src/range.rs index 4b76ce00..ef00b92f 100644 --- a/src/range.rs +++ b/src/range.rs @@ -66,4 +66,7 @@ impl Ranges { pub fn into_iter(self) -> impl Iterator { self.range_list.into_iter() } + pub fn iter(&self) -> impl Iterator { + self.range_list.iter() + } }