From 7a686f5d9995f587fa1df7ac3257cb11cf803df8 Mon Sep 17 00:00:00 2001 From: Paulo Matias Date: Sun, 15 Nov 2026 07:49:23 -0300 Subject: [PATCH 1/3] [AUTOMATED] Seed r12 with the entry address at PowerPC64 ELFv2 global entries - The ELFv2 ABI passes a function's own address in r12 to its global entry, which derives the TOC pointer from it (addis r2,r12,hi; addi r2,r2,lo). Without that fact r2 was an unknown input and TOC-relative globals printed as offsets from an uninitialised register. - New P1 pass ppc64r12 (the analog of mips_gp's t9 seed): r12 = entry at each defined STT_FUNC whose st_other declares a distinct local entry. - CLI test ppc64_global_entry_toc with a 1.4 KB clang fixture; prototype_call_mechanisms' `on` assertion now checks the clobber, since the stored TOC resolves to a constant instead of printing as r2save. --- .../kuna-analysis/src/loader/kuna_ppc64r12.rs | 66 ++++++++++++++++++ .../crates/kuna-analysis/src/loader/mod.rs | 1 + decompiler/crates/kuna-analysis/src/passes.rs | 2 + .../tests/fixtures/publish_toc_ppc64le | Bin 0 -> 1392 bytes .../crates/kuna-cli/tests/integration.rs | 2 + .../kuna-cli/tests/ppc64_global_entry_toc.rs | 20 ++++++ .../tests/prototype_call_mechanisms.rs | 4 +- 7 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs create mode 100755 decompiler/crates/kuna-analysis/tests/fixtures/publish_toc_ppc64le create mode 100644 decompiler/crates/kuna-cli/tests/ppc64_global_entry_toc.rs diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs new file mode 100644 index 0000000000..fdf3c634a2 --- /dev/null +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs @@ -0,0 +1,66 @@ +//! (kuna `ppc64r12`) PPC64 ELFv2 global entries hold their own address in `r12`. +//! +//! The OpenPOWER ELFv2 ABI requires a caller that enters a function at its +//! *global* entry (an indirect call, a PLT stub, a cross-module call) to pass the +//! entry address in `r12`. The global entry derives the TOC pointer from it, +//! `addis r2,r12,hi; addi r2,r2,lo`, before falling into the local entry. Without +//! that fact `r2` is an unknown function input and every TOC-relative global +//! access prints as an offset from an uninitialised register +//! (`*(T *)(r12 + 0x22068) = p`), which also does not recompile. +//! +//! The pass seeds `r12 = entry` at each defined function whose symbol declares a +//! distinct local entry (`st_other`). That annotation is what says the function +//! begins with the `r12`-based TOC setup; a function without one keeps `r2` as +//! received. The kuna analog of the MIPS `t9 = entry` seed in +//! [`super::mips_markers`]. + +use object::read::{Object, ObjectSymbol}; +use object::{SymbolFlags, SymbolKind}; + +use crate::pass::{AnalysisCtx, AnalysisOutput, AnalysisPass, Phase, TrackedRegFact}; + +/// The ELFv2 global-entry address register. +const R12: &str = "r12"; + +/// One `r12 = entry` fact per PPC64 function symbol with a distinct local entry. +fn scan_global_entries(file: &object::File) -> AnalysisOutput { + let mut out = AnalysisOutput::default(); + if file.architecture() != object::Architecture::PowerPc64 + || !matches!(file.format(), object::BinaryFormat::Elf) + { + return out; + } + for sym in file.symbols().chain(file.dynamic_symbols()) { + if sym.kind() != SymbolKind::Text || sym.is_undefined() || sym.address() == 0 { + continue; + } + let SymbolFlags::Elf { st_other, .. } = sym.flags() else { continue }; + // Bits 5-7 of st_other: 2..=6 name a local entry 4..64 bytes in. + if !matches!((st_other >> 5) & 7, 2..=6) { + continue; + } + out.tracked_regs.push(TrackedRegFact { func_addr: sym.address(), reg: R12, value: sym.address() }); + } + out +} + +/// Seeds `r12` at PPC64 ELFv2 global entries; empty on every other image. +#[derive(Clone, Copy, Default)] +pub struct Ppc64GlobalEntryPass; + +impl AnalysisPass for Ppc64GlobalEntryPass { + fn phase(&self) -> Phase { + Phase::P1 + } + + fn id(&self) -> &'static str { + "ppc64r12" + } + + fn run(&self, ctx: &AnalysisCtx) -> AnalysisOutput { + if ctx.arch.get_register_varnode(R12.as_bytes()).is_err() { + return AnalysisOutput::default(); + } + scan_global_entries(ctx.file) + } +} diff --git a/decompiler/crates/kuna-analysis/src/loader/mod.rs b/decompiler/crates/kuna-analysis/src/loader/mod.rs index b6794351df..733ecd476a 100644 --- a/decompiler/crates/kuna-analysis/src/loader/mod.rs +++ b/decompiler/crates/kuna-analysis/src/loader/mod.rs @@ -47,6 +47,7 @@ pub mod kuna_globalref; pub mod elfv1; mod elfv1_imports; pub mod kuna_ppc32_imports; +pub mod kuna_ppc64r12; mod kuna_ppc32picimports; pub use kuna_ppc32picimports::{ImportProofMemo, ImportProofScope}; // (kuna) ELF section-table tolerance: an image whose section headers are diff --git a/decompiler/crates/kuna-analysis/src/passes.rs b/decompiler/crates/kuna-analysis/src/passes.rs index 0f5219fbd8..4d79e5865d 100644 --- a/decompiler/crates/kuna-analysis/src/passes.rs +++ b/decompiler/crates/kuna-analysis/src/passes.rs @@ -280,6 +280,8 @@ pub fn passes_for(compiler: Compiler, format: object::BinaryFormat) -> VecKtc#r5fn}q6{vS`Vk?Q7IFW4-JvHUTPvgjm zf6#l5IWn`JXBL-31uMmG_U*iRyX$d&ABD%h=MjcaYb3LK&s!F*DGweKzeIs1?a~52 zm#9JWJfF2(x!xp#&f=|>1~$qx>mfR4*nFN_yi*;JtfG9*anil6W1m2y1%a+k_Bv7B z#Ph8W5c^twuLT@G(M?r1^=7ONh-~QLclNi4<$V9^r{(FCb>jEh;ZwM8#s|9|-Rw8U z@zl`x#H}^?Q=Xf`Uivf1_DmRlGoL;OAA=og_oK99u97V84O5V=_=R{yINztLuI9*j zRxiA&kBst*j4L$9_gUU!b#FnDTo)8{hWuy`2ZJQ#pp`{wyL;Z3FwZH-l72QkA4PH8 zOFISI&M3tf;t6AJzt%70 Date: Sun, 15 Nov 2026 08:39:21 -0300 Subject: [PATCH 2/3] Skip the r12 seed on images whose e_flags declare ELFv1 - st_other entry bits mean nothing under ELFv1; the ELFv1 descriptor reader already gates on e_flags & 3. --- decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs index fdf3c634a2..ed5396dbed 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_ppc64r12.rs @@ -25,8 +25,10 @@ const R12: &str = "r12"; /// One `r12 = entry` fact per PPC64 function symbol with a distinct local entry. fn scan_global_entries(file: &object::File) -> AnalysisOutput { let mut out = AnalysisOutput::default(); + // An image whose e_flags declare ELFv1 (1) gives st_other no entry meaning. if file.architecture() != object::Architecture::PowerPc64 || !matches!(file.format(), object::BinaryFormat::Elf) + || matches!(file.flags(), object::FileFlags::Elf { e_flags, .. } if e_flags & 3 == 1) { return out; } From ea25772be201f44380c0c2cd7209964e625c0617 Mon Sep 17 00:00:00 2001 From: Paulo Matias Date: Sun, 15 Nov 2026 08:49:36 -0300 Subject: [PATCH 3/3] Pin the TOC constant the r2Save store writes under callmechanisms on - The on arm now checks the store's RHS is the constant the return reads back, not only that the local is clobbered. --- .../kuna-cli/tests/prototype_call_mechanisms.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/decompiler/crates/kuna-cli/tests/prototype_call_mechanisms.rs b/decompiler/crates/kuna-cli/tests/prototype_call_mechanisms.rs index 634d3cae94..c2839bc9a0 100644 --- a/decompiler/crates/kuna-cli/tests/prototype_call_mechanisms.rs +++ b/decompiler/crates/kuna-cli/tests/prototype_call_mechanisms.rs @@ -392,14 +392,18 @@ fn default_keeps_a_local_in_the_elfv1_toc_slot_of_an_elfv2_frame() { let kept = code .lines() .any(|line| line.trim().starts_with("return ") && line.contains(product.as_str())); - (kept, code) + (kept, code, product) }; - let (kept, off) = returned(Some("off")); + let (kept, off, _) = returned(Some("off")); assert!(kept && !off.contains("r2save"), "{off}"); - let (kept, code) = returned(Some("on")); + let (kept, code, product) = returned(Some("on")); // `on` stores the TOC over the local; the global entry's `r12` resolves - // that TOC to a constant, so the store no longer prints as `r2save`. + // that TOC to a constant, so the store prints as that constant (not + // `r2save`) and the return reads it back. assert!(!kept, "{code}"); + let toc = code.lines().find_map(|line| line.trim().strip_prefix(&format!("{product} = 0x"))) + .and_then(|rest| rest.strip_suffix(';')).unwrap_or_else(|| panic!("{code}")); + assert!(code.contains(&format!("+ 0x{toc};")) && !code.contains("r2save"), "{code}"); for value in [None, Some("auto")] { assert_eq!(returned(value).1, off, "{value:?}"); }