From 8e4925341452f7dc45b1e724816f133bcafbe224 Mon Sep 17 00:00:00 2001 From: phix33 <122955334+phix33@users.noreply.github.com> Date: Wed, 4 Nov 2026 19:24:38 +1100 Subject: [PATCH 1/6] [AUTOMATED] Keep a MIPS object's weak GOT entries as unfolded import slots --- .../src/loader/kuna_mipsreloc.rs | 94 +++++++++++++++---- .../src/loader/kuna_mipsreloc_tests.rs | 37 +++++++- .../kuna-analysis/src/loader/reloc_object.rs | 40 +++++++- .../kuna-analysis/src/loadimage_object.rs | 38 +++++++- decompiler/crates/kuna-base/src/xml.rs | 3 +- decompiler/crates/kuna-console/src/engine.rs | 19 ++++ docs/baseline-stages.json | 16 +++- docs/spec/01-program-prep.md | 18 +++- tests/stages/kuna-mipsgot-weak.xml | 48 ++++++++++ 9 files changed, 281 insertions(+), 32 deletions(-) create mode 100644 tests/stages/kuna-mipsgot-weak.xml diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs index cd2d3cbee8..3beb511bae 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs @@ -35,7 +35,10 @@ //! adds back the input `$gp` a partial link recorded in `.reginfo` or //! `.MIPS.options`. The GOT holds what the relocations put in it and nothing //! writes it, so the loader reports it foldable, and -//! `lw $t9, %call16(f)($gp); jalr $t9` calls `f`. +//! `lw $t9, %call16(f)($gp); jalr $t9` calls `f`. The exception is the entry of +//! an undefined weak symbol: the run-time loader fills it, with 0 when nothing +//! defines the symbol, so it is an import slot that holds 0 and is not folded, +//! and `if (f) f(x);` keeps its test. //! //! `R_MIPS_JALR` only says which function a `jalr` calls and is not applied. //! microMIPS, MIPS16, R6 PC-relative and TLS relocations are unsupported. @@ -44,7 +47,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::ops::Range; use object::read::{Object, ObjectSection, ObjectSymbol}; -use object::{Relocation, RelocationFlags, RelocationTarget, SectionIndex}; +use object::{Relocation, RelocationFlags, RelocationTarget, SectionIndex, SymbolIndex}; use super::reloc_apply::RelocationFailure; @@ -237,13 +240,16 @@ pub(super) struct Gp { capacity: u64, entries: BTreeMap, little_endian: bool, + /// The slot of each undefined weak symbol a GOT relocation names: the + /// run-time loader fills it, with 0 when nothing defines the symbol. + imports: BTreeMap, } impl Gp { /// A GOT of `entries` entries of `entry` bytes at `base`. pub(super) fn new(base: u64, entry: u64, entries: usize, little_endian: bool) -> Gp { let capacity = (entries as u64).min(GOT_WINDOW / entry); - Gp { base, entry, capacity, entries: BTreeMap::new(), little_endian } + Gp { base, entry, capacity, entries: BTreeMap::new(), little_endian, imports: BTreeMap::new() } } pub(super) fn gp(&self) -> u64 { @@ -255,6 +261,17 @@ impl Gp { self.base } + /// Each import slot `(symbol, slot)`: an undefined weak symbol's GOT entry, + /// which holds 0 until the run-time loader binds the symbol. + pub(super) fn imports(&self) -> impl Iterator + '_ { + self.imports.iter().map(|(&symbol, &slot)| (SymbolIndex(symbol), slot)) + } + + /// How many bytes one GOT entry spans. + pub(super) fn entry(&self) -> u64 { + self.entry + } + /// How many bytes the GOT spans. pub(super) fn size(&self) -> u64 { self.capacity * self.entry @@ -276,23 +293,41 @@ impl Gp { fn slot(&mut self, value: i128) -> Result { let mask = if self.entry == 8 { u64::MAX } else { 0xffff_ffff }; let value = (value as u64) & mask; - let next = self.entries.len() as u64; if let Some(&slot) = self.entries.get(&value) { return Ok(slot as i128 - self.gp() as i128); } + let slot = self.next_slot()?; + self.entries.insert(value, slot); + Ok(slot as i128 - self.gp() as i128) + } + + /// The `$gp` offset of `symbol`'s import slot, made on first use. + fn import(&mut self, symbol: SymbolIndex) -> Result { + let slot = match self.imports.get(&symbol.0) { + Some(&slot) => slot, + None => { + let slot = self.next_slot()?; + self.imports.insert(symbol.0, slot); + slot + } + }; + Ok(slot as i128 - self.gp() as i128) + } + + fn next_slot(&self) -> Result { + let next = (self.entries.len() + self.imports.len()) as u64; if next >= self.capacity { return Err(RelocationFailure::OutOfRange); } - let slot = self.base + next * self.entry; - self.entries.insert(value, slot); - Ok(slot as i128 - self.gp() as i128) + Ok(self.base + next * self.entry) } /// Apply a section's relocations to its bytes `data` laid at `base`: each /// expression in record order, reading an implicit addend from the bytes /// earlier expressions wrote. `lo_halves` is [`lo_halves`]'s, per record, - /// and `input_gp` the object's own recorded `$gp` ([`input_gp`]). A failed - /// expression writes nothing and reports each of its records. + /// `input_gp` the object's own recorded `$gp` ([`input_gp`]), and `weak` + /// tells an undefined weak symbol, whose GOT entry is an import slot. A + /// failed expression writes nothing and reports each of its records. pub(super) fn apply_section( &mut self, arch: object::Architecture, @@ -301,10 +336,11 @@ impl Gp { records: &[(u64, Relocation)], lo_halves: &[Option], input_gp: Result, + weak: &dyn Fn(RelocationTarget) -> bool, resolve: &mut dyn FnMut(RelocationTarget) -> Option, ) -> Applied { let mut applied = Applied::default(); - let input = Inputs { arch, base, input_gp }; + let input = Inputs { arch, base, input_gp, weak }; let (ops, bounds) = expressions(arch, records); for bound in bounds { let expression = &ops[bound]; @@ -353,7 +389,14 @@ impl Gp { let old = read(field, le); let place = input.base.wrapping_add(offset as u64); let first = &records[expression[0].record].1; - let first_target = resolve(first.target()).ok_or(RelocationFailure::UnresolvedTarget)?; + let import_of = |op: &Op, reloc: &Relocation| { + Some(reloc.target()).filter(|&t| op.special.is_none() && got_entry(op.op, false) == Some(true) && (input.weak)(t)) + }; + let first_import = import_of(&expression[0], first); + let first_target = match first_import { + Some(_) => Target { address: 0, local: false, gp_disp: false }, + None => resolve(first.target()).ok_or(RelocationFailure::UnresolvedTarget)?, + }; let mut value = if first.has_implicit_addend() { self.implicit(expression[0].op, old, first_target.local, lo_half)? as u64 } else { @@ -363,7 +406,9 @@ impl Gp { let mut callee = None; for (index, op) in expression.iter().enumerate() { let reloc = &records[op.record].1; + let import = if index == 0 { first_import } else { import_of(op, reloc) }; let (mut target, named) = match (index, op.special) { + (_, None) if import.is_some() => (Target { address: 0, local: false, gp_disp: false }, import), (0, _) => (first_target, Some(first.target())), (_, None) => (resolve(reloc.target()).ok_or(RelocationFailure::UnresolvedTarget)?, Some(reloc.target())), (_, Some(ssym)) => (self.special(ssym, place, input)?, None), @@ -372,7 +417,7 @@ impl Gp { target.address = target.address as u32 as i32 as i64 as u64; } let last = index + 1 == expression.len(); - let next = self.operate(op.op, target, value, place, input, last)?; + let next = self.operate(op.op, target, import, value, place, input, last)?; let symbol = match named { Some(t @ RelocationTarget::Symbol(_)) if !target.gp_disp => Origin::Symbol(t, 1), _ => Origin::Constant, @@ -428,8 +473,19 @@ impl Gp { /// The result of operation `op` on symbol `t` and addend `a`, as the field /// bits it would write; a later operation of the expression takes it as its /// addend. Range checks apply only to the last operation; PC32 always - /// keeps the low 32 bits of the displacement. - fn operate(&mut self, op: u32, t: Target, a: u64, place: u64, input: &Inputs, last: bool) -> Result { + /// keeps the low 32 bits of the displacement. A symbol's + /// GOT entry for `import`, an undefined weak symbol, is its import slot. + #[allow(clippy::too_many_arguments)] + fn operate( + &mut self, + op: u32, + t: Target, + import: Option, + a: u64, + place: u64, + input: &Inputs, + last: bool, + ) -> Result { let gp = self.gp(); let s = t.address; let sum = s.wrapping_add(a); @@ -439,7 +495,10 @@ impl Gp { return Err(RelocationFailure::UnresolvedTarget); } let page = |x: u64| x.wrapping_add(0x8000) & !0xffff; - let got = |this: &mut Self, value: u64| this.slot(i128::from(value as i64)).map(|v| v as u64); + let got = |this: &mut Self, value: u64| match import { + Some(RelocationTarget::Symbol(symbol)) => this.import(symbol).map(|v| v as u64), + _ => this.slot(i128::from(value as i64)).map(|v| v as u64), + }; Ok(match op { R16 => checked(sum, 16)?, R32 => sum & 0xffff_ffff, @@ -490,10 +549,13 @@ impl Gp { } /// What a section's expressions read besides their records. -struct Inputs { +struct Inputs<'a> { arch: object::Architecture, base: u64, input_gp: Result, + /// Whether a target is an undefined weak symbol, whose GOT entry is an + /// import slot rather than the address the layout gave it. + weak: &'a dyn Fn(RelocationTarget) -> bool, } /// One operation of an expression: its type, the record it came from, and diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs index b2ee1f2853..e69db93f95 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs @@ -57,7 +57,7 @@ fn apply_one( lo: Option, ) -> Result<(), RelocationFailure> { let records = [(0u64, reloc)]; - let applied = gp.apply_section(arch, field, place, &records, &[lo], Ok(0), &mut |_| Some(t)); + let applied = gp.apply_section(arch, field, place, &records, &[lo], Ok(0), &|_| false, &mut |_| Some(t)); applied.failures.first().map_or(Ok(()), |&(_, reason)| Err(reason)) } @@ -384,7 +384,7 @@ fn same_offset_records_chain_across_rel_and_rela_as_gnu_ld() { let mut gp = Gp::new(0x40_0000, 4, 0, le); let applied = gp.apply_section( Architecture::Mips64_N32, &mut field, 0x40_0000, &records, &[None; 3], - Ok(0), &mut |_| Some(target(0x100)), + Ok(0), &|_| false, &mut |_| Some(target(0x100)), ); assert_eq!(read(&field, le), 0x307); assert!(applied.failures.is_empty()); @@ -429,7 +429,8 @@ fn relocated_text_matches_the_gnu_linked_originals() { } /// Each GOT load reaches a slot holding what the GNU-linked GOT's slot holds; -/// the slots may be ordered differently, and every other bit matches. +/// the slots may be ordered differently, and every other bit matches. An +/// undefined weak symbol's slot holds 0, as GNU's does, and is an import slot. #[test] fn got_loads_reach_the_values_gnu_linked() { let mut stems = vec!["got-32-be".to_string()]; @@ -463,6 +464,7 @@ fn got_loads_reach_the_values_gnu_linked() { read_at(data, address - base, width as usize, le) }; let mut slots = 0; + let mut weak_slots = 0; for (offset, reloc) in file.section_by_name(".text").unwrap().relocations() { let RelocationFlags::Elf { r_type } = reloc.flags() else { unreachable!() }; let weak = match reloc.target() { @@ -471,7 +473,7 @@ fn got_loads_reach_the_values_gnu_linked() { }; // A split offset's high half is 0 in both small GOTs, so its low half // alone locates the slot; the `lui` halves are compared as other bits. - if weak || !matches!(r_type & 0xff, GOT16 | CALL16 | GOT_DISP | GOT_PAGE | GOT_LO16 | CALL_LO16) { + if !matches!(r_type & 0xff, GOT16 | CALL16 | GOT_DISP | GOT_PAGE | GOT_LO16 | CALL_LO16) { continue; } let ours = read_at(&actual, offset, 4, le); @@ -480,6 +482,9 @@ fn got_loads_reach_the_values_gnu_linked() { let gnu_slot = gnu_gp.wrapping_add(gnu as u16 as i16 as i64 as u64); let gnu_value = read_at(gnu_got.data().unwrap(), gnu_slot - gnu_got.address(), width as usize, le); assert_eq!(value(slot), gnu_value, "{stem} at {offset:#x}"); + let import = layout.import_slots.iter().any(|&(lo, hi)| slot >= lo && slot < hi); + assert_eq!(import, weak, "{stem} at {offset:#x}: only a weak symbol's slot is an import"); + weak_slots += usize::from(weak); for word in [&mut actual, &mut expected] { let at = offset as usize; let masked = read_at(word, offset, 4, le) & !0xffff; @@ -488,6 +493,8 @@ fn got_loads_reach_the_values_gnu_linked() { slots += 1; } assert!(slots >= 5, "{stem}"); + assert_eq!(weak_slots, 1, "{stem}: the weak `optional` slot"); + assert_eq!(layout.import_slots.len(), 1, "{stem}"); assert_eq!(actual, expected, "{stem}: bits outside the GOT offsets"); } } @@ -716,3 +723,25 @@ fn only_a_global_gp_is_the_linker_gp() { assert_eq!(read_at(&layout.segments[0].1, 0, 4, false), expected, "{scope:?}"); } } + +/// A GCC PIC object's weak function keeps one import slot, inside the GOT, for +/// both the test of its address and the call through it, named after the +/// function and holding 0. +#[test] +fn a_weak_functions_got_entry_is_a_named_import_slot() { + for stem in ["pic-gcc47-n32", "pic-gcc47-64", "pic-gcc73-n32", "pic-gcc73-64"] { + let bytes = fixture(&format!("{stem}.o")); + let file = object::File::parse(&*bytes).unwrap(); + let layout = layout(&bytes); + assert!(layout.diagnostics.is_empty(), "{stem}: {:?}", layout.diagnostics.report_lines()); + let [(lo, hi)] = layout.import_slots[..] else { panic!("{stem}: {:?}", layout.import_slots) }; + assert_eq!(hi - lo, if file.is_64() { 8 } else { 4 }); + let named: Vec<_> = layout.funcsyms.iter().filter(|(a, _)| *a == lo).map(|(_, n)| n.as_slice()).collect(); + assert_eq!(named, [b"optional".as_slice()], "{stem}"); + assert!(!layout.funcsyms.iter().any(|(a, n)| n == b"optional" && *a != lo), "{stem}: one name"); + let (base, data) = layout.segments.iter().find(|(b, d)| lo >= *b && hi <= b + d.len() as u64).unwrap(); + assert!(data[(lo - base) as usize..(hi - base) as usize].iter().all(|&b| b == 0), "{stem}"); + let (got_lo, got_hi) = layout.got_range.unwrap(); + assert!(got_lo <= lo && hi <= got_hi, "{stem}"); + } +} diff --git a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs index e1ec5d7638..c697088a54 100644 --- a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs +++ b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs @@ -254,10 +254,14 @@ pub struct RelocLayout { /// Half-open extent of allocated ELF COMMON storage, separate from extern slots. pub common_range: Option<(u64, u64)>, /// Half-open extent of a MIPS object's synthetic GOT: its entries hold what - /// the relocations put there and nothing else writes them. + /// the relocations put there and nothing else writes them, except its + /// [`Self::import_slots`]. pub got_range: Option<(u64, u64)>, /// A MIPS object's synthetic `$gp`. pub gp: Option, + /// Half-open import slots in the GOT: an undefined weak symbol's entry, + /// which the run-time loader fills and which therefore does not fold. + pub import_slots: Vec<(u64, u64)>, /// A PowerPC64 ELFv1 object's function descriptors: `(descriptor, entry, /// TOC)`, sorted by descriptor address. pub opd: Vec<(u64, u64, Option)>, @@ -378,6 +382,10 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL } let got_base = gp.as_ref().map(|g| g.base()); let input_gp = if mips { kuna_mipsreloc::input_gp(file) } else { Ok(0) }; + let weak = |target: RelocationTarget| match target { + RelocationTarget::Symbol(idx) => file.symbol_by_index(idx).is_ok_and(|s| s.is_undefined() && s.is_weak()), + _ => false, + }; // Externs (undefined referenced symbols) live in a synthetic area above the // laid-out sections — the angr extern-object analog. Allocated on demand. @@ -474,6 +482,7 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL &relocs, &lo_halves, input_gp, + &weak, &mut resolve, ); for (record, reason) in applied.failures { @@ -678,6 +687,34 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL } funcsizes.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1))); funcsizes.dedup_by_key(|s| s.0); + // A MIPS object's import slots: the GOT entry of each undefined weak symbol, + // which the run-time loader fills. One a call reaches is named after it, as + // an Import Address Table slot is, so `(*slot)(x)` calls the import while a + // test of the slot still reads it. + let mut import_slots = Vec::new(); + if let Some(g) = &gp { + for (idx, slot) in g.imports() { + import_slots.push((slot, slot + g.entry())); + let Ok(sym) = file.symbol_by_index(idx) else { continue }; + if !code_externs.contains(&idx) && sym.kind() != SymbolKind::Text { + continue; + } + if let Ok(n) = sym.name_bytes().map(<[u8]>::to_vec) { + if !n.is_empty() { + symbol_info.push(RelocSymbolInfo { + name: String::from_utf8_lossy(&n).into_owned(), + vma: slot, + section_index: None, + section_name: None, + section_offset: None, + binding: symbol_binding(&sym), + undefined: true, + }); + funcsyms.push((slot, n)); + } + } + } + } // Extern functions: name each external *code target* at its synthetic slot // so `call ext` renders by name instead of a bare address. A code target is // an undefined symbol reached through a PLT-relative reloc (reliable even for @@ -746,6 +783,7 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL common_range: common_allocator.range(), got_range, gp: gp.as_ref().map(kuna_mipsreloc::Gp::gp), + import_slots, opd: { opd_descriptors.sort_unstable(); opd_descriptors.dedup(); diff --git a/decompiler/crates/kuna-analysis/src/loadimage_object.rs b/decompiler/crates/kuna-analysis/src/loadimage_object.rs index f589443eb0..008a75578b 100644 --- a/decompiler/crates/kuna-analysis/src/loadimage_object.rs +++ b/decompiler/crates/kuna-analysis/src/loadimage_object.rs @@ -1212,6 +1212,7 @@ impl ObjectLoadImage { let layout = reloc_object::layout_relocatable(file, fmt); let got_range = layout.got_range; + let import_slots = layout.import_slots.clone(); let elfv1 = crate::loader::elfv1::Descriptors( layout .opd @@ -1364,11 +1365,12 @@ impl ObjectLoadImage { dynreloc_const: fpconst .const_ranges .into_iter() - .chain(got_range.map(|(lo, hi)| (lo, hi - 1))) + .chain(got_range.into_iter().flat_map(|range| outside(range, &import_slots))) .collect(), // A pre-link object has no Import Address Table: its external calls - // are relocations, resolved by the layout pass. - import_slots: Vec::new(), + // are relocations, resolved by the layout pass. A MIPS object's + // undefined weak symbols keep GOT entries the run-time loader fills. + import_slots, plt_local: Vec::new(), mips_linkage: crate::loader::kuna_mipsstubs::class_linkage(&file), elfv1, @@ -2115,6 +2117,25 @@ pub(crate) fn compose_language_id(arch: Architecture, endian: &str, model: Optio }) } +/// The inclusive pieces of the half-open `range` that none of the half-open +/// `holes` covers. +fn outside((lo, hi): (u64, u64), holes: &[(u64, u64)]) -> Vec<(u64, u64)> { + let mut holes: Vec<_> = holes.iter().copied().filter(|&(a, b)| a < hi && b > lo).collect(); + holes.sort_unstable(); + let mut out = Vec::new(); + let mut at = lo; + for (a, b) in holes { + if a > at { + out.push((at, a - 1)); + } + at = at.max(b); + } + if at < hi { + out.push((at, hi - 1)); + } + out +} + /// Every SLEIGH language id the **ELF** loader (`ElfFormat` + [`language_id_for`]) /// can produce, for both endiannesses, over the supported ELF machines. This is /// the exact set a real ELF resolves to today — exposed so the cross-crate @@ -3058,6 +3079,17 @@ mod tests { /// PE import slots retain their loader identity independently of section /// permissions. The side table, not CODE versus DATA, is what body /// selection can rely on while keeping the slot's function symbol. + /// A MIPS object's foldable GOT pieces exclude its import slots, which + /// may sit at either end or in the middle. + #[test] + fn got_ranges_leave_out_import_slots() { + assert_eq!(outside((0x100, 0x120), &[]), [(0x100, 0x11f)]); + assert_eq!(outside((0x100, 0x120), &[(0x108, 0x10c)]), [(0x100, 0x107), (0x10c, 0x11f)]); + assert_eq!(outside((0x100, 0x120), &[(0x11c, 0x120), (0x100, 0x104)]), [(0x104, 0x11b)]); + assert_eq!(outside((0x100, 0x108), &[(0x100, 0x104), (0x104, 0x108)]), []); + assert_eq!(outside((0x100, 0x108), &[(0x200, 0x208)]), [(0x100, 0x107)]); + } + #[test] fn pe_import_slots_are_reported_in_code_and_data_sections() { for (fixture, slot, name, executable) in [ diff --git a/decompiler/crates/kuna-base/src/xml.rs b/decompiler/crates/kuna-base/src/xml.rs index 5ef7f42d0f..055dc89372 100644 --- a/decompiler/crates/kuna-base/src/xml.rs +++ b/decompiler/crates/kuna-base/src/xml.rs @@ -2205,7 +2205,8 @@ mod tests { // and kuna-repframecopy / a by-value argument copied with rep movs reaches its call // and kuna-libcnames-win32 / a local takes the name of the Win32 call it meets // and kuna-libcnames-addr / a stack object takes the name of the library parameter its address meets - assert_eq!(count, 756, "corpus file count drifted"); + assert_eq!(count, 757, "corpus file count drifted"); + // and kuna-mipsgot-weak / unresolved weak GOT entries retain import-slot identity } /// ~20 representative SLEIGH spec files across varied processors diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 302c332ecd..4f57340c49 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -4030,6 +4030,24 @@ fn input_isa_stash( vec![("input_isa", explicit)] } +/// (kuna) An ELF image's import slots, which only a MIPS relocatable object +/// has (the GOT entry of an undefined weak function, which the run-time loader +/// fills). No pass reports them, so they are stashed under the PE/Mach-O +/// import-binding pass's id (`peimportcall`) and committed with its facts. +fn elf_import_slot_stash( + bytes: &[u8], + slots: &[(u64, u64)], +) -> Option<(&'static str, kuna_analysis::pass::AnalysisOutput)> { + use object::Object as _; + let elf = kuna_analysis::loadimage_object::parse_object(bytes) + .is_ok_and(|file| file.format() == object::BinaryFormat::Elf); + (elf && !slots.is_empty()).then(|| { + let mut output = kuna_analysis::pass::AnalysisOutput::default(); + output.externref = slots.to_vec(); + ("peimportcall", output) + }) +} + /// A program with nothing loaded beyond the engine: every stash empty, every /// declaration absent. Each bootstrap fills in what its loader supplies. fn empty_program( @@ -4738,6 +4756,7 @@ pub fn bootstrap_from_object_with_isa( sleigh.base().unwrap().translate(), ); pending_analysis.extend(input_isa_stash(input_context_paints)); + pending_analysis.extend(elf_import_slot_stash(&bytes, loader.import_slot_ranges())); // (kuna `dynrelocs`) The PT_GNU_RELRO-frozen dynamic-relocation slots, read // off the same loader while it is still in hand. `readonly_ranges` above diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index d2edbac94a..ef8f9e823d 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4350, + 4350 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -306,8 +306,11 @@ "data:CALLEEREGINPUTS #2: off, the method reached through a stub gets nothing", "data:CALLEEREGINPUTS #30: both, a GCC __cdecl member whose this is stored at [esp+4] behind the hidden return pointer gets no receiver", "data:CALLEEREGINPUTS #3: default, the constructor gets this and nothing else", + "data:CALLEEREGINPUTS #3: on, the constructor gets this and nothing else", "data:CALLEEREGINPUTS #4: default, the method reached through a stub gets this", + "data:CALLEEREGINPUTS #4: on, the method reached through a stub gets this", "data:CALLEEREGINPUTS #5: default, the constructed field is read back", + "data:CALLEEREGINPUTS #5: on, the constructed field is read back", "data:CALLEEREGINPUTS #6: off, forward hands obj_set only the stack argument", "data:CALLEEREGINPUTS #7: default, forward takes this in ECX", "data:CALLEEREGINPUTS #8: default, forward passes its own this through and keeps the stack argument", @@ -1232,6 +1235,7 @@ "data:KUNA-CATALOG #13: validated A32 inventory frames are the default", "data:KUNA-CATALOG #14: full ARM frames are explicitly enabled", "data:KUNA-CATALOG #15: every registry row plus eight individual queries", + "data:KUNA-CATALOG #15: every registry row plus six individual queries", "data:KUNA-CATALOG #16: standard C byte-array rendering is explicitly opt-in", "data:KUNA-CATALOG #17: standard C byte-array rendering exposes its live state", "data:KUNA-CATALOG #1: compareform documented with its post-DIV-2 default and P3 address", @@ -1240,6 +1244,7 @@ "data:KUNA-CATALOG #4: v850indirectbranch is a V850-gated default ()", "data:KUNA-CATALOG #5: each settable carries symptom-keyed use_when guidance for the operator", "data:KUNA-CATALOG #6: angr-derived features carry source_decompiler provenance (loweredswitch, namestyle, stackguard, branchflip, foldcallret, dedupvardecls, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, switchguardbound, switchsharedcase, switchmultipred, unrolledguard, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, securitycheck, structsynth, protoorder, calleevote, rustprobestack)", + "data:KUNA-CATALOG #7: change_kind structure-recovery tags the switch/array/loader/loop/tail-call reconstructors (loweredswitch, switchselector, constselectjump, arraystride, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, iteboolean, paramcopyhoist, guardarm, loopcondhoist, endptrbound, structsynth, structmerge, structheadless, devectorize, gomorestack, gowritebarrier, ptrauthtrap, rustprobestack)", "data:KUNA-CATALOG #7: change_kind structure-recovery tags the switch/array/loader/loop/tail-call reconstructors (loweredswitch, switchselector, constselectjump, arraystride, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, iteboolean, paramcopyhoist, guardarm, loopcondhoist, endptrbound, structsynth, structmerge, structheadless, devectorize, gomorestack, gowritebarrier, ptrauthtrap, rustprobestack, chkstkdarwin)", "data:KUNA-CATALOG #8: the opt-in-tool change_kind counts explicitly opted transformations ()", "data:KUNA-CATALOG #9: every settable carries a machine-readable change_kind so an LLM can judge default-on eligibility", @@ -1872,6 +1877,7 @@ "data:PTRAUTHTRAP #14: a guard that traps when the bits agree keeps its trap", "data:PTRAUTHTRAP #15: a value moved into x30 in an earlier block keeps its trap", "data:PTRAUTHTRAP #16: x30 loaded from a non-frame address keeps its trap", + "data:PTRAUTHTRAP #1: off keeps the bit test on the incoming return address", "data:PTRAUTHTRAP #1: off keeps the bit test on the incoming return address (and x30inverted keeps it in both)", "data:PTRAUTHTRAP #2: off keeps the trap call", "data:PTRAUTHTRAP #3: the tail call survives in both passes", @@ -3418,6 +3424,7 @@ "data:libcnames #3: with the option on the first descriptor is fd", "data:libcnames #4: the second open is fd_2, also after every rename", "data:libcnames #5: read's result is n and it reads from fd", + "data:libcnames #6: the buffer keeps the number it has with the option off, in every pass", "data:libcnames #6: the stack buffer read and write take by address is v4 with the option off", "data:libcnames #7: strlen's results are len and len_2 and malloc's is p", "data:libcnames #8: a value used inside an expression names nothing", @@ -3486,6 +3493,9 @@ "data:mipsgot-local #2: c_calls bumps counter", "data:mipsgot-local #2b: c_calls calls wfn and reads gdata", "data:mipsgot-local #3: no call through a GOT slot", + "data:mipsgot-weak #1: the weak function's address is tested", + "data:mipsgot-weak #2: the call binds to optional", + "data:mipsgot-weak #3: no unconditional call", "data:mipsreloc-chains #10: N32 REL R32 then LO16 keep the other half in both byte orders", "data:mipsreloc-chains #11: REL GPREL16, SUB, HI16 compose in both byte orders", "data:mipsreloc-chains #1: N32 PIC read_global reads global_value", @@ -4347,4 +4357,4 @@ ], "returncode": 0, "unit_footer": null -} \ No newline at end of file +} diff --git a/docs/spec/01-program-prep.md b/docs/spec/01-program-prep.md index 54c7a3681f..2096411d65 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -893,7 +893,13 @@ the section-flag translation, import resolution (§1.3), and extra constant rang resolves to `_gp` less the place (the PIC prologue adds `$t9`, the function's entry) and a global `_gp` or an undefined `__gnu_local_gp` to `_gp`. The GOT's entries hold what the relocations put in them and nothing writes them, so the loader reports its range foldable - (`dynreloc_const`, beside the MSVC FP constants), and + (`dynreloc_const`, beside the MSVC FP constants). The exception is the entry + of an undefined weak symbol, which the run-time loader fills with the + symbol's address, or 0 when nothing defines it: each such symbol gets its own + entry holding 0, reported as an import slot outside the foldable range and, + when a call reaches it, named after the symbol as an Import Address Table + slot is, so `if (f) f(x);` keeps its test. A direct call or address + relocation against the symbol still resolves to its synthetic extern. Otherwise `lw $t9, %call16(f)($gp); jalr $t9` decompiles to a call of `f` wherever `$t9` is seeded with the entry. `R_MIPS_JALR`, a hint naming the function a `jalr` calls, is not applied; microMIPS, MIPS16, R6 PC-relative and TLS relocations @@ -2638,9 +2644,13 @@ the resolved callee's no-return flag onto the prototype it hands `ActionDeindire `decompiler/crates/kuna-decomp/src/p0_knowledge/database.rs (Database::build_global_query)` dropped it, where upstream returns the callee's live `Funcdata`), which is what makes the deindirect schedule the restart whose re-flow plants the artificial halt. The -extra upstream Win32 no-return-name list remains on the PE/COFF arm only. Off, PE -and Mach-O import-slot calls render byte for byte as before; other formats are -unaffected either way. +extra upstream Win32 no-return-name list remains on the PE/COFF arm only. An ELF +image runs no such pass, but a MIPS relocatable object's loader makes import +slots, the GOT entry of each undefined weak function: the engine stashes the +loader's ranges under this pass's id +(`decompiler/crates/kuna-console/src/engine.rs (elf_import_slot_stash)`), so +they commit with that pass's facts and the call in `if (f) f(x);` binds to `f`. +Other images are unaffected. The `externref` mark also protects the slot until that resolution: P3 read-only folding cannot replace it with an on-disk thunk/name RVA, even if a hostile or single-section PE mapped the IAT executable and non-writable. diff --git a/tests/stages/kuna-mipsgot-weak.xml b/tests/stages/kuna-mipsgot-weak.xml new file mode 100644 index 0000000000..1d2522d7e6 --- /dev/null +++ b/tests/stages/kuna-mipsgot-weak.xml @@ -0,0 +1,48 @@ + + + +03e0000800000000 + + + +if \(dat_[0-9a-f]+\) + x = optional\(\); +return optional\( + From bd7233b5fc56ad4d0c8a78a20a48a3ae72bacc63 Mon Sep 17 00:00:00 2001 From: phix33 <122955334+phix33@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:18:14 +1100 Subject: [PATCH 2/6] [AUTOMATED] Unify weak import references and index slot lookups --- .../src/loader/kuna_mipsreloc_tests.rs | 26 ++++ .../kuna-analysis/src/loader/reloc_object.rs | 10 ++ .../kuna-analysis/src/loadimage_object.rs | 28 ++++ .../crates/kuna-cli/tests/integration.rs | 3 + .../kuna-cli/tests/mips_weak_imports.rs | 82 ++++++++++ decompiler/crates/kuna-console/src/engine.rs | 20 ++- .../kuna-console/src/kuna_importslots.rs | 82 ++++++++++ decompiler/crates/kuna-console/src/lib.rs | 1 + .../crates/kuna-console/tests/integration.rs | 3 + .../kuna-console/tests/mips_weak_imports.rs | 113 ++++++++++++++ .../kuna-decomp/src/p0_knowledge/database.rs | 38 +++++ docs/baseline-stages.json | 6 +- docs/spec/01-program-prep.md | 20 +++ tests/fixtures/mips-weak-imports/README.md | 15 ++ tests/fixtures/mips-weak-imports/build.py | 16 ++ .../identity-64-be-got-only.o | Bin 0 -> 1456 bytes .../mips-weak-imports/identity-64-be-mixed.o | Bin 0 -> 1728 bytes .../identity-64-le-got-only.o | Bin 0 -> 1456 bytes .../mips-weak-imports/identity-64-le-mixed.o | Bin 0 -> 1728 bytes .../identity-n32-be-got-only.o | Bin 0 -> 1056 bytes .../mips-weak-imports/identity-n32-be-mixed.o | Bin 0 -> 1232 bytes .../identity-n32-le-got-only.o | Bin 0 -> 1056 bytes .../mips-weak-imports/identity-n32-le-mixed.o | Bin 0 -> 1232 bytes tests/fixtures/mips-weak-imports/identity.S | 41 +++++ .../mips-weak-imports/provenance.json | 144 ++++++++++++++++++ tests/stages/kuna-mipsgot-weak.xml | 18 ++- 26 files changed, 659 insertions(+), 7 deletions(-) create mode 100644 decompiler/crates/kuna-cli/tests/mips_weak_imports.rs create mode 100644 decompiler/crates/kuna-console/src/kuna_importslots.rs create mode 100644 decompiler/crates/kuna-console/tests/mips_weak_imports.rs create mode 100644 tests/fixtures/mips-weak-imports/README.md create mode 100644 tests/fixtures/mips-weak-imports/build.py create mode 100644 tests/fixtures/mips-weak-imports/identity-64-be-got-only.o create mode 100644 tests/fixtures/mips-weak-imports/identity-64-be-mixed.o create mode 100644 tests/fixtures/mips-weak-imports/identity-64-le-got-only.o create mode 100644 tests/fixtures/mips-weak-imports/identity-64-le-mixed.o create mode 100644 tests/fixtures/mips-weak-imports/identity-n32-be-got-only.o create mode 100644 tests/fixtures/mips-weak-imports/identity-n32-be-mixed.o create mode 100644 tests/fixtures/mips-weak-imports/identity-n32-le-got-only.o create mode 100644 tests/fixtures/mips-weak-imports/identity-n32-le-mixed.o create mode 100644 tests/fixtures/mips-weak-imports/identity.S create mode 100644 tests/fixtures/mips-weak-imports/provenance.json diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs index e69db93f95..19c842eeeb 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs @@ -745,3 +745,29 @@ fn a_weak_functions_got_entry_is_a_named_import_slot() { assert!(got_lo <= lo && hi <= got_hi, "{stem}"); } } + + +#[test] +fn mixed_weak_references_share_one_function_without_changing_relocation_values() { + for abi in ["n32", "64"] { + for endian in ["be", "le"] { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.."); + let bytes = std::fs::read(root.join(format!("tests/fixtures/mips-weak-imports/identity-{abi}-{endian}-mixed.o"))).unwrap(); + let layout = layout(&bytes); + assert!(layout.diagnostics.is_empty()); + let names: Vec<_> = layout.funcsyms.iter().filter(|(_, name)| name == b"optional").collect(); + assert_eq!(names.len(), 1); + let [(alias, canonical)] = layout.function_address_aliases[..] else { panic!("one direct-address alias") }; + assert_ne!(alias, canonical); + assert_eq!(names[0].0, canonical); + assert_eq!(layout.import_slots, [(canonical, canonical + if abi == "64" { 8 } else { 4 })]); + let file = object::File::parse(&*bytes).unwrap(); + let sym = file.symbols().find(|s| s.name().ok() == Some("optional")).unwrap(); + assert_eq!(layout.extern_addr[&sym.index()], alias); + let rodata = file.section_by_name(".rodata").unwrap(); + let base = layout.section_vma[&rodata.index()]; + let data = &layout.segments.iter().find(|(addr, _)| *addr == base).unwrap().1; + assert_eq!(read_at(data, 0, if abi == "64" { 8 } else { 4 }, endian == "le"), alias); + } + } +} diff --git a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs index c697088a54..10869aa189 100644 --- a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs +++ b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs @@ -262,6 +262,8 @@ pub struct RelocLayout { /// Half-open import slots in the GOT: an undefined weak symbol's entry, /// which the run-time loader fills and which therefore does not fold. pub import_slots: Vec<(u64, u64)>, + /// Additional addresses of the same logical import: `(alias, canonical)`. + pub function_address_aliases: Vec<(u64, u64)>, /// A PowerPC64 ELFv1 object's function descriptors: `(descriptor, entry, /// TOC)`, sorted by descriptor address. pub opd: Vec<(u64, u64, Option)>, @@ -692,6 +694,8 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL // an Import Address Table slot is, so `(*slot)(x)` calls the import while a // test of the slot still reads it. let mut import_slots = Vec::new(); + let mut import_functions = HashMap::new(); + let mut function_address_aliases = Vec::new(); if let Some(g) = &gp { for (idx, slot) in g.imports() { import_slots.push((slot, slot + g.entry())); @@ -711,6 +715,7 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL undefined: true, }); funcsyms.push((slot, n)); + import_functions.insert(idx, slot); } } } @@ -723,6 +728,10 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL // `stdout`, referenced by PC32 to an `STT_OBJECT`) are addressed but // deliberately left unnamed here. for (sym_idx, addr) in &extern_order { + if let Some(&canonical) = import_functions.get(sym_idx) { + function_address_aliases.push((*addr, canonical)); + continue; + } let Ok(sym) = file.symbol_by_index(*sym_idx) else { continue; }; @@ -784,6 +793,7 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL got_range, gp: gp.as_ref().map(kuna_mipsreloc::Gp::gp), import_slots, + function_address_aliases, opd: { opd_descriptors.sort_unstable(); opd_descriptors.dedup(); diff --git a/decompiler/crates/kuna-analysis/src/loadimage_object.rs b/decompiler/crates/kuna-analysis/src/loadimage_object.rs index 008a75578b..1890ca451f 100644 --- a/decompiler/crates/kuna-analysis/src/loadimage_object.rs +++ b/decompiler/crates/kuna-analysis/src/loadimage_object.rs @@ -577,6 +577,7 @@ pub struct ObjectLoadImage { /// a whole-binary enumeration needs to keep them out of the decompile set. /// Empty for every non-PE image and for a relocatable object. import_slots: Vec<(u64, u64)>, + function_address_aliases: Vec<(u64, u64)>, /// (kuna `pltlocal`) `(stub, definition)` for each PLT stub the image's own /// dynamic relocations bind to a function it defines, ascending by stub. See /// [`crate::loader::kuna_pltlocal`]. @@ -1181,6 +1182,7 @@ impl ObjectLoadImage { const_ranges, dynreloc_const, import_slots, + function_address_aliases: Vec::new(), plt_local, mips_linkage, elfv1, @@ -1213,6 +1215,7 @@ impl ObjectLoadImage { let layout = reloc_object::layout_relocatable(file, fmt); let got_range = layout.got_range; let import_slots = layout.import_slots.clone(); + let function_address_aliases = layout.function_address_aliases.clone(); let elfv1 = crate::loader::elfv1::Descriptors( layout .opd @@ -1371,6 +1374,7 @@ impl ObjectLoadImage { // are relocations, resolved by the layout pass. A MIPS object's // undefined weak symbols keep GOT entries the run-time loader fills. import_slots, + function_address_aliases, plt_local: Vec::new(), mips_linkage: crate::loader::kuna_mipsstubs::class_linkage(&file), elfv1, @@ -1486,6 +1490,10 @@ impl ObjectLoadImage { /// holds a pointer the run-time loader writes. The engine reports these /// ranges so its whole-binary enumeration can decompile the image's code /// without inventing a body for every import. + pub fn function_address_aliases(&self) -> &[(u64, u64)] { + &self.function_address_aliases + } + pub fn import_slot_ranges(&self) -> &[(u64, u64)] { &self.import_slots } @@ -1802,6 +1810,10 @@ impl LoadImage for ObjectLoadImage { r.0 = r.0.wadd(badjust); r.1 = r.1.wadd(badjust); } + for r in self.function_address_aliases.iter_mut().chain(&mut self.import_slots) { + r.0 = r.0.wadd(badjust); + r.1 = r.1.wadd(badjust); + } for r in &mut self.plt_local { r.0 = r.0.wadd(badjust); r.1 = r.1.wadd(badjust); @@ -3081,6 +3093,22 @@ mod tests { /// selection can rely on while keeping the slot's function symbol. /// A MIPS object's foldable GOT pieces exclude its import slots, which /// may sit at either end or in the middle. + #[test] + fn adjusting_a_weak_import_moves_its_slots_and_all_function_addresses() { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../..") + .join("tests/fixtures/mips-weak-imports/identity-64-be-mixed.o"); + let mut image = ObjectLoadImage::open(path.to_str().unwrap()).unwrap(); + let slots = image.import_slot_ranges().to_vec(); + let aliases = image.function_address_aliases().to_vec(); + assert_eq!(slots.len(), 1); + assert_eq!(aliases.len(), 1); + image.attach_to_space(Rc::clone(manager().get_default_code_space().unwrap())); + image.adjust_vma(0x2000); + assert_eq!(image.import_slot_ranges(), slots.iter().map(|&(lo, hi)| (lo + 0x2000, hi + 0x2000)).collect::>()); + assert_eq!(image.function_address_aliases(), aliases.iter().map(|&(a, c)| (a + 0x2000, c + 0x2000)).collect::>()); + } + #[test] fn got_ranges_leave_out_import_slots() { assert_eq!(outside((0x100, 0x120), &[]), [(0x100, 0x11f)]); diff --git a/decompiler/crates/kuna-cli/tests/integration.rs b/decompiler/crates/kuna-cli/tests/integration.rs index 52c08050d6..9524613b2c 100644 --- a/decompiler/crates/kuna-cli/tests/integration.rs +++ b/decompiler/crates/kuna-cli/tests/integration.rs @@ -846,3 +846,6 @@ mod e500_arguments; #[path = "cvf_trace.rs"] mod cvf_trace; + +#[path = "mips_weak_imports.rs"] +mod mips_weak_imports; diff --git a/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs new file mode 100644 index 0000000000..95b7626dd2 --- /dev/null +++ b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs @@ -0,0 +1,82 @@ +//! Execute weak-call output with the original declared ABI and both bindings. +use crate::common; +use std::collections::BTreeSet; +use std::process::Command; + +#[test] +#[cfg(target_os = "linux")] +fn declared_weak_calls_preserve_null_guards_arguments_and_results() { + if common::process::optional_output(Command::new("cc").arg("--version")).is_none() { + return; + } + for compiler in ["47", "73"] { + for abi in ["n32", "64"] { + let image = common::repo_root().join(format!( + "tests/fixtures/octeon-relocations/pic-gcc{compiler}-{abi}.o" + )); + let out = + common::process::required_output(Command::new(env!("CARGO_BIN_EXE_kuna")).args([ + "decompile", + image.to_str().unwrap(), + "call_optional", + "--assert-strict", + "--assert", + "prototype call_optional unsigned int call_optional(unsigned int x)", + "--assert", + "prototype optional unsigned int optional(unsigned int x)", + ])); + let code = String::from_utf8(out.stdout).unwrap(); + let slots: BTreeSet<_> = code + .split(|c: char| !c.is_ascii_alphanumeric() && c != '_') + .filter(|name| name.starts_with("dat_")) + .collect(); + assert_eq!(slots.len(), 1, "one weak import slot: {code}"); + for bound in [false, true] { + let source = common::scratch_file("weak-call", "c"); + let driver = source.with_extension("driver.c"); + let executable = source.with_extension("exe"); + let declarations = slots + .iter() + .map(|name| format!("extern uintptr_t {name};\n")) + .collect::(); + std::fs::write(&source, format!("#include \nextern unsigned int optional(unsigned int) __attribute__((weak));\n{declarations}{code}")).unwrap(); + let definitions = slots + .iter() + .map(|name| format!("uintptr_t {name};\n")) + .collect::(); + let initialize = slots + .iter() + .map(|name| format!("{name}=(uintptr_t)optional;\n")) + .collect::(); + let helper = if bound { + "unsigned int optional(unsigned int x) { calls++; last=x; return x^0x12345678u; }" + } else { + "" + }; + let expected = if bound { "0x12345678u" } else { "0x42u" }; + let count = if bound { "i+1" } else { "0" }; + let argument = if bound { " || last!=x" } else { "" }; + std::fs::write(&driver, format!("#include \nextern unsigned int optional(unsigned int) __attribute__((weak));\nunsigned int call_optional(unsigned int);\nunsigned calls,last;\n{definitions}{helper}\nint main(void) {{ {initialize}for(uint32_t i=0;i<10000;i++) {{uint32_t x=i*2654435761u^0x89123456u; uint32_t y=call_optional(x); if(y!=(x^{expected}) || calls!={count}{argument}) return 1; }} return 0; }}\n")).unwrap(); + common::process::required_output( + Command::new("cc") + .args([ + "-std=c11", + "-O2", + "-fno-lto", + "-Wall", + "-Werror", + "-Wuninitialized", + ]) + .arg(&source) + .arg(&driver) + .arg("-o") + .arg(&executable), + ); + common::process::required_output(&mut Command::new(&executable)); + for file in [source, driver, executable] { + std::fs::remove_file(file).unwrap(); + } + } + } + } +} diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 4f57340c49..2f82bdd335 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -708,7 +708,8 @@ pub struct ConsoleProgram { /// point of them; the slot is still a pointer word rather than a function /// entry, which is what [`Self::function_entries_executable`] reads this for. /// Empty on the XML datatest path and for every non-PE image. - import_slots: Vec<(u64, u64)>, + import_slots: crate::kuna_importslots::ImportSlots, + function_address_aliases: BTreeMap, /// (kuna, issue #666) Does this container decorate a C identifier with a /// leading `_` in its symbol table? /// @@ -1236,7 +1237,7 @@ impl ConsoleProgram { /// (`ObjectLoadImage::import_slot_ranges`). Always false on the XML datatest /// path and for a raw image. pub fn is_import_slot(&self, vma: u64) -> bool { - self.import_slots.iter().any(|&(lo, hi)| vma >= lo && vma < hi) + self.import_slots.contains(vma) } /// Whether `vma` may be treated as the start of a function body. @@ -1351,6 +1352,9 @@ impl ConsoleProgram { /// already knows, so an address that is not a discovered function start is /// still `None` and the caller keeps decompiling exactly where it was asked. pub fn find_entry_at(&self, vma: u64) -> Option { + let vma = if self.is_declared_entry(vma) { vma } else { + self.function_address_aliases.get(&vma).copied().unwrap_or(vma) + }; let want = self.thumb_normalized(vma); if !self.anonymous_entries.is_empty() { return self.find_compact_entry_at(want); @@ -2373,6 +2377,7 @@ impl ConsoleProgram { // does not overlap `self.symbols`. let records: Vec<(String, Address)> = self.symbols.iter().map(|s| (s.name.clone(), s.addr.clone())).collect(); + let aliases = self.function_address_aliases.clone(); let arch = self.arch_mut(); for (name, addr) in records { let (scope, basename) = arch @@ -2384,6 +2389,10 @@ impl ConsoleProgram { } arch.symboltab.add_function(scope, &addr, &basename, min_size, type_code.clone())?; } + let code = arch.manage().get_default_code_space().expect("loaded code space").clone(); + for (alias, canonical) in aliases { + arch.symboltab.add_function_alias(&Address::new(code.clone(), canonical), &Address::new(code.clone(), alias))?; + } Ok(()) } @@ -4092,7 +4101,8 @@ fn empty_program( assertion_outcomes: Vec::new(), pending_prototypes: BTreeMap::new(), raw_address_units: None, - import_slots: Vec::new(), + import_slots: Default::default(), + function_address_aliases: BTreeMap::new(), symbol_underscore_prefix: false, load_notes: Vec::new(), elf_functions: Default::default(), @@ -4771,6 +4781,7 @@ pub fn bootstrap_from_object_with_isa( // FunctionSymbol at each of them for call naming, and the whole-binary // enumeration needs to know those addresses hold a pointer, not a body. let import_slots: Vec<(u64, u64)> = loader.import_slot_ranges().to_vec(); + let function_address_aliases = loader.function_address_aliases().to_vec(); // (kuna `pltlocal`) The PLT stubs the image's own relocations bind to a // function it defines, so a call through one is built at the definition. let plt_local: Vec<(u64, u64)> = loader.plt_local_targets().to_vec(); @@ -4968,7 +4979,8 @@ pub fn bootstrap_from_object_with_isa( prog.analysis_image = Some((path.to_string(), bytes, proof_memo)); prog.loader_data_objects = loader_data_objects; prog.loader_data_exports = loader_data_exports; - prog.import_slots = import_slots; + prog.import_slots = crate::kuna_importslots::ImportSlots::new(import_slots); + prog.function_address_aliases = function_address_aliases.into_iter().collect(); prog.symbol_underscore_prefix = symbol_underscore_prefix; if let Ok(loc) = prog.arch().get_register_varnode(b"r2") { if loc.size == 8 { diff --git a/decompiler/crates/kuna-console/src/kuna_importslots.rs b/decompiler/crates/kuna-console/src/kuna_importslots.rs new file mode 100644 index 0000000000..9e5a06417f --- /dev/null +++ b/decompiler/crates/kuna-console/src/kuna_importslots.rs @@ -0,0 +1,82 @@ +//! Indexed half-open import storage, independent of whether a slot lies in code. + +#[derive(Default)] +pub(crate) struct ImportSlots(Vec<(u64, u64)>); + +impl ImportSlots { + pub(crate) fn new(mut ranges: Vec<(u64, u64)>) -> Self { + ranges.retain(|&(lo, hi)| lo < hi); + ranges.sort_unstable(); + let mut merged: Vec<(u64, u64)> = Vec::with_capacity(ranges.len()); + for (lo, hi) in ranges { + if let Some(last) = merged.last_mut().filter(|last| lo <= last.1) { + last.1 = last.1.max(hi); + } else { + merged.push((lo, hi)); + } + } + debug_assert!(merged.windows(2).all(|pair| pair[0].1 < pair[1].0)); + Self(merged) + } + + pub(crate) fn contains(&self, address: u64) -> bool { + let end = self.0.partition_point(|&(lo, _)| { + #[cfg(test)] + COMPARISONS.with(|count| count.set(count.get() + 1)); + lo <= address + }); + end != 0 && address < self.0[end - 1].1 + } +} + +#[cfg(test)] +thread_local! { + static COMPARISONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn normalization_preserves_half_open_membership() { + let ranges = vec![ + (80, 90), + (12, 16), + (8, 12), + (10, 15), + (9, 10), + (7, 7), + (30, 20), + (0, 1), + (u64::MAX - 3, u64::MAX), + ]; + let slots = ImportSlots::new(ranges.clone()); + for address in (0..100).chain([u64::MAX - 4, u64::MAX - 3, u64::MAX - 1, u64::MAX]) { + assert_eq!( + slots.contains(address), + ranges.iter().any(|&(lo, hi)| lo <= address && address < hi), + "{address:#x}" + ); + } + assert!(!ImportSlots::default().contains(0)); + } + + #[test] + fn large_positive_and_negative_queries_use_logarithmic_comparisons() { + let slots = ImportSlots::new((0..10_000).map(|i| (4 * i + 1, 4 * i + 3)).collect()); + for i in 0..10_000 { + for (address, expected) in [(4 * i, false), (4 * i + 2, true)] { + COMPARISONS.with(|count| count.set(0)); + assert_eq!(slots.contains(address), expected); + COMPARISONS.with(|count| { + assert!( + (1..=16).contains(&count.get()), + "{} comparisons", + count.get() + ) + }); + } + } + } +} diff --git a/decompiler/crates/kuna-console/src/lib.rs b/decompiler/crates/kuna-console/src/lib.rs index 974066d1ae..3bb911e359 100644 --- a/decompiler/crates/kuna-console/src/lib.rs +++ b/decompiler/crates/kuna-console/src/lib.rs @@ -20,6 +20,7 @@ pub mod interface; pub mod ifaceterm; pub mod ifacedecomp; mod entry_selector; +mod kuna_importslots; pub mod engine; pub mod decompile_step; pub mod project; diff --git a/decompiler/crates/kuna-console/tests/integration.rs b/decompiler/crates/kuna-console/tests/integration.rs index 4921900c86..f6868ec359 100644 --- a/decompiler/crates/kuna-console/tests/integration.rs +++ b/decompiler/crates/kuna-console/tests/integration.rs @@ -543,3 +543,6 @@ mod verify_rustfmtio; #[path = "verify_rustprobestack.rs"] mod verify_rustprobestack; + +#[path = "mips_weak_imports.rs"] +mod mips_weak_imports; diff --git a/decompiler/crates/kuna-console/tests/mips_weak_imports.rs b/decompiler/crates/kuna-console/tests/mips_weak_imports.rs new file mode 100644 index 0000000000..affd103148 --- /dev/null +++ b/decompiler/crates/kuna-console/tests/mips_weak_imports.rs @@ -0,0 +1,113 @@ +//! One weak import keeps one logical symbol across GOT and direct relocations. +use kuna_analysis::loadimage_object::ObjectLoadImage; +use kuna_base::address::Address; +use kuna_console::{ + assertions, assertsyntax, + engine::{bootstrap_from_object, EntrySelector}, +}; +use std::path::PathBuf; + +#[test] +fn mixed_and_got_only_imports_keep_one_identity_and_share_edited_prototypes() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../.."); + for abi in ["n32", "64"] { + for endian in ["be", "le"] { + for form in ["mixed", "got-only"] { + let path = root.join(format!( + "tests/fixtures/mips-weak-imports/identity-{abi}-{endian}-{form}.o" + )); + let target = match (abi, endian) { + ("n32", "le") => "MIPS:LE:64:64-32addr:n32", + (_, "le") => "MIPS:LE:64:default", + _ => "", + }; + let bytes = std::fs::read(&path).unwrap(); + let image = + ObjectLoadImage::from_bytes_with_target(path.to_str().unwrap(), &bytes, target) + .unwrap(); + let aliases = image.function_address_aliases().to_vec(); + assert_eq!(aliases.len(), usize::from(form == "mixed")); + let mut prog = bootstrap_from_object( + path.to_str().unwrap(), + target, + &[root.join("specs").to_string_lossy().into_owned()], + ) + .unwrap(); + prog.commit_pending_analysis().unwrap(); + let entries = prog.function_entries_canonical(); + assert_eq!( + entries.iter().filter(|e| e.name == "optional").count(), + 1, + "{path:?}" + ); + let entry = prog + .resolve_entry(&EntrySelector::Name("optional".into())) + .unwrap(); + assert!(prog.is_import_slot(entry.addr.get_offset())); + assert!(!prog + .function_entries_executable() + .iter() + .any(|e| e.name == "optional")); + let mut addresses = vec![entry.addr.clone()]; + for &(alias, canonical) in &aliases { + assert_eq!(canonical, entry.addr.get_offset()); + assert_eq!( + prog.resolve_entry(&EntrySelector::Numeric(alias)) + .unwrap() + .addr, + entry.addr + ); + addresses.push(Address::new(entry.addr.get_space().unwrap().clone(), alias)); + } + let (symbol, _) = prog + .arch() + .symboltab + .find_function_across_scopes(&entry.addr) + .unwrap(); + for address in &addresses { + assert_eq!( + prog.arch() + .symboltab + .find_function_across_scopes(address) + .unwrap() + .0, + symbol + ); + } + for (selector, declaration, count) in [ + ( + "optional".to_string(), + "unsigned int optional(unsigned int x)", + 1, + ), + ( + format!("0x{:x}", addresses.last().unwrap().get_offset()), + "unsigned int optional(unsigned int x,unsigned int y)", + 2, + ), + ] { + prog.set_assertions(vec![assertsyntax::parse_directive(&format!( + "prototype {selector} {declaration}" + )) + .unwrap()]); + assertions::apply_program_scoped(&mut prog); + assert!( + prog.assertion_outcomes() + .iter() + .all(|o| o.status == "applied"), + "{:?}", + prog.assertion_outcomes() + ); + let context = prog.arch().build_arch_handle(); + for address in &addresses { + let (name, _, proto) = context + .query_function(address) + .expect("shared import lookup"); + assert_eq!(name, "optional"); + assert_eq!(proto.num_params(), count, "{path:?}: {address:?}"); + } + } + } + } + } +} diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/database.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/database.rs index 46dc46a570..7be3890b3e 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/database.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/database.rs @@ -2252,6 +2252,20 @@ impl Database { Ok(sid) } + /// Map another address to the same function identity and declared prototype. + pub fn add_function_alias(&mut self, entry: &Address, alias: &Address) -> KunaResult<()> { + self.kuna_gen += 1; + let (symbol, scope) = self.find_function_across_scopes(entry) + .ok_or_else(|| KunaError::lowlevel("Function alias has no canonical function"))?; + if let Some((other, _)) = self.find_function_across_scopes(alias) { + return if other == symbol { Ok(()) } else { + Err(KunaError::lowlevel("Function alias overlaps a different function")) + }; + } + self.add_map_point(scope, symbol, alias, &Address::new_invalid())?; + Ok(()) + } + /// C++ `ScopeInternal::setAttribute` (`database.cc:2228-2235`): OR the /// lockable boolean attributes onto a symbol (the rest are masked out), then /// recompute its `size_typelock`. The console `map` commands lock the symbols @@ -5570,6 +5584,30 @@ mod tests { /// The per-function inline / noreturn / inject-id flags round-trip through the /// FunctionSymbol kind (the parked-FuncProto facts `OptionInline` / /// `OptionNoReturn` / `IfcFixupApply` set and `FlowInfo::queryCall` reads). + #[test] + fn function_aliases_share_identity_without_merging_distinct_same_named_functions() { + let m=build_manager(); + let (mut db,g)=db_with_global(m.num_spaces()); + let a=Address::new(space(&m,2),0x1000); + let b=Address::new(space(&m,2),0x2000); + let alias=Address::new(space(&m,2),0x3000); + let first=db.add_function(g,&a,"optional",1,dt(1)).unwrap(); + let second=db.add_function(g,&b,"optional",1,dt(1)).unwrap(); + let generation=db.kuna_generation(); + db.add_function_alias(&a,&alias).unwrap(); + assert!(db.kuna_generation()>generation); + assert_eq!(db.find_by_name(g,"optional").len(),2); + assert_eq!(db.find_function_across_scopes(&alias),Some((first,g))); + assert!(db.add_function_alias(&a,&b).is_err()); + assert_eq!(db.find_function_across_scopes(&b),Some((second,g))); + let mut pieces=crate::fspec::PrototypePieces::default(); + pieces.name="optional".into(); + pieces.intypes=vec![dt(4)]; + db.set_function_proto_pieces(first,pieces); + assert_eq!(db.function_proto_pieces(g,&alias).unwrap().intypes.len(),1); + assert!(db.function_proto_pieces(g,&b).is_none()); + } + #[test] fn function_inline_noreturn_injectid_roundtrip() { let m = build_manager(); diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index ef8f9e823d..89c685cf61 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4350, - 4350 + 4352, + 4352 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -3496,6 +3496,8 @@ "data:mipsgot-weak #1: the weak function's address is tested", "data:mipsgot-weak #2: the call binds to optional", "data:mipsgot-weak #3: no unconditional call", + "data:mipsgot-weak #4: mixed direct and GOT references retain one import name", + "data:mipsgot-weak #5: a declared import is never ambiguous", "data:mipsreloc-chains #10: N32 REL R32 then LO16 keep the other half in both byte orders", "data:mipsreloc-chains #11: REL GPREL16, SUB, HI16 compose in both byte orders", "data:mipsreloc-chains #1: N32 PIC read_global reads global_value", diff --git a/docs/spec/01-program-prep.md b/docs/spec/01-program-prep.md index 2096411d65..f2d1b8a2a9 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -10214,3 +10214,23 @@ Without a callee prototype, a preserved incoming argument register cannot establish import arity: calls with and without that argument can have identical instructions. A declared prototype supplies that missing contract (`kuna_gotimportproto.rs`, §4). + + +### Weak import identity and slot membership + +An undefined weak MIPS import has one FunctionSymbol, anchored at its GOT slot. +A direct relocation keeps its original synthetic extern address and is recorded +as another address of that same symbol. The loader carries the SymbolIndex-based +association through bootstrap; the database maps both addresses to one symbol, +so name selection, renamed imports and edited prototypes do not split between +two independent functions. Distinct ELF symbols are not merged by their spelling. +The canonical inventory contains the import once; explicit alias-address lookup +resolves the same entry. Import pointer storage remains bodyless unless a caller +explicitly declares a function there. + +Import storage extents are normalized once at bootstrap: empty extents are +removed and overlapping/adjacent half-open ranges are merged in address order. +Membership queries use a binary search, including interior and missing addresses. +Executable-function enumeration therefore avoids a full slot-vector scan per +candidate, while preserving PE/Mach-O import slots inside executable sections and +caller-declared-entry precedence. diff --git a/tests/fixtures/mips-weak-imports/README.md b/tests/fixtures/mips-weak-imports/README.md new file mode 100644 index 0000000000..41960c86ee --- /dev/null +++ b/tests/fixtures/mips-weak-imports/README.md @@ -0,0 +1,15 @@ +# Weak MIPS import identity + +Original authored GAS source, no vendor code. `identity.S` loads one undefined +weak function through its GOT entry; the mixed variant also reads a read-only +pointer initialized by a direct R_MIPS_32/R_MIPS_64 relocation to that same ELF +symbol. The GOT-only variant has no direct reference. Both restore their ABI +GOT base before returning. N32/N64 and both byte orders are included. + +Rebuild with `python3 build.py --assembler /path/to/mips64-octeon-linux-gnu-as`. +The recorded GNU assembler is 2.30 from the local SDK; provenance records source, +tool and output hashes and exact commands. Tests use an explicit generic MIPS +LE target because automatic little-endian Octeon selection is intentionally +unavailable. Equivalent original review programs were GNU-linked and executed +under the pinned QEMU Octeon68XX reference in both BE ABIs; LE is static loader +and public-assertion evidence only. diff --git a/tests/fixtures/mips-weak-imports/build.py b/tests/fixtures/mips-weak-imports/build.py new file mode 100644 index 0000000000..48a1878f14 --- /dev/null +++ b/tests/fixtures/mips-weak-imports/build.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Rebuild original weak-import identity objects; explicit GAS ABI/endian inputs.""" +import argparse, hashlib, json, subprocess +from pathlib import Path +p=argparse.ArgumentParser();p.add_argument('--assembler',required=True);a=p.parse_args() +root=Path(__file__).resolve().parent +records=[] +for abi in ('n32','64'): + for endian in ('be','le'): + for mixed in (0,1): + name=f"identity-{abi}-{endian}-{'mixed' if mixed else 'got-only'}.o" + argv=[a.assembler,'-EB' if endian=='be' else '-EL','-march=octeon','-mabi='+abi,'--defsym','ABI64='+str(int(abi=='64')),'--defsym','MIXED='+str(mixed),'-o',name,'identity.S'] + subprocess.run(argv,cwd=root,check=True) + records.append({'command':[Path(a.assembler).name,*argv[1:]],'output':name,'sha256':hashlib.sha256((root/name).read_bytes()).hexdigest()}) +manifest={'source':'identity.S','source_sha256':hashlib.sha256((root/'identity.S').read_bytes()).hexdigest(),'assembler_version':subprocess.check_output([a.assembler,'--version'],text=True).splitlines()[0],'assembler_sha256':hashlib.sha256(Path(a.assembler).read_bytes()).hexdigest(),'records':records} +(root/'provenance.json').write_text(json.dumps(manifest,indent=2)+'\n') diff --git a/tests/fixtures/mips-weak-imports/identity-64-be-got-only.o b/tests/fixtures/mips-weak-imports/identity-64-be-got-only.o new file mode 100644 index 0000000000000000000000000000000000000000..33e2eea4f5eedfd327944ac9d6c5169ffdec4203 GIT binary patch literal 1456 zcmbtS%}OId5U!c1*^Mi!?nPJ+viQfMje>X*b`%u@s|a4acGlQ@g+T1FyuTiWURcf|Ot$D8NxnRCWiH4{AIk~hMncV0!kuEaR2}S literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-64-be-mixed.o b/tests/fixtures/mips-weak-imports/identity-64-be-mixed.o new file mode 100644 index 0000000000000000000000000000000000000000..1cc32bb10bf6326a0aec706d0d3d3639b9858e55 GIT binary patch literal 1728 zcmbtU&ubGw6n?vDo2sd%twKN#Dzp}iCQJ$m$_ z;2+`9qeqYa0YdXHc<>y*w>xh$JKMeJgW31qH}88tW+r*J_3Wwddz6buv#>27-+@sJ zPd-qc!W0YXGR@UazArtzL3HN}c0Z5qot+Y$y(jwh5;2dP%d4B@)o8R-8>M;4TS>X} zk*JYbRo~F}krKPpCZf>O2EZ%(hev6r^(l_467a^q&8D5BUV(E_J1)-){M|?2)33#fqrM7sbgU`pDy=B!BnbuE?Va5q8%sRZ>AmW!eYnGJOhL+v2N?%% zE>K#>z0Q%25~=z>`XHTz0prVgv%7~Se7C;vrJkYZ=f(ammgEZ=n27()*xV=IpX3qE_=~}K{Kv$(O88A@ zL;7WyZMSa;|B)Hq4mAH?%YU|A{{nFL-ZL8c+#k!WOyys@{yfks{!hag=FH5<`F{bB Cv{%Of literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-64-le-got-only.o b/tests/fixtures/mips-weak-imports/identity-64-le-got-only.o new file mode 100644 index 0000000000000000000000000000000000000000..8a9f102e63cfa0df96eceea4dd7b22978453354c GIT binary patch literal 1456 zcmbtTO-ox*5Irx~uYS?iZUkM3)DMb2S}0u!eTs^KDg`%gZ)$1-i3xdE&{d^dm)*3` zAK_2%pEQ5LMc3)fyZ0n7*R0xsyqR-m&Yig*`MUdIuaYF7C78hwkp*rlKAy()RaS%< z9^(;~Z?6onunGKH-RS+4_}lLU+w03cm>Kj0k6NXvzL-1sZ->#Q)2CNxky=VUa{X0^&ZI zq@MhtB)Ar9)ZdgrDdzhmbtJ)`e!opjPknq|aWBQ;swge|Z9t1bpzmIP@%(zP@!6}i Z7j*hQ$`<#3F3EBF{V>$0vRL9+{}+K%N^k%G literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-64-le-mixed.o b/tests/fixtures/mips-weak-imports/identity-64-le-mixed.o new file mode 100644 index 0000000000000000000000000000000000000000..699eac7e291b07d3ac03ef6ca47b5a319b0ea68b GIT binary patch literal 1728 zcmbVLOKTKC5dLO6kM$8vqQZh6R7eaG2LqCm;3OJh!AKyN9PM?-WMPwCW+ovy8RF5S z7X^QW#~eL+^alv;U-000t(vad-RT`KDVXW1ullP;SNHpkS1&6;0F?k$3@t^0$T)o( zA5WuPjc^S!SonTQo;y#0uMZyezL0+Q8~8xq+h2>b&nedW@ zlS5~Fu{p`{?_ yV)^s(do8P-sXv6jB?NxQ{Q3DW5$4aHsWqwp9vdU#=bs~A8ULs7^&V&lpZ^ckAy>u# literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-n32-be-got-only.o b/tests/fixtures/mips-weak-imports/identity-n32-be-got-only.o new file mode 100644 index 0000000000000000000000000000000000000000..de33f92dce5b6d777acd53e06e0aedb66ea1e624 GIT binary patch literal 1056 zcma)4yGq1R5S{G$e&DMSv{3Pd$VEXc#9|c{7E}Z+tc1H5*M)tNTtO?vN>H${5-j`# zKfzDgU$C%V&rC8gD~JwEX3pGmXHF)$-rn8uG9D%7Q5W)5A(Okgr#V^?#ys^=Z^A{A zy?LKmA19i7LH^vBe7q%E+$1kcjhSpidZp>~MAX(&NI))=K?}h4xFi}j00xTH3sKbB z#^*mRrM3Bi!_IGl-~{4wiYvof`T?|P16D241Gpn;h6Fe@OEB-QKjusiNAnOOo8qvS zKi=M=jm`Y(3Wc`fQ?2ewwW=)(2lIQhCudCK8j^$imBj>ugTUR9he zkO^H7#dFO9zif^3FCCdv>nxc<7?KJ6VySvoBeR!3I5f6UIxE{E>h;rr48DZ1&IP5y zh4aH$+&{wvc)Wkrm_xnP)V`!QbIMv2#DZ$9vLuMe0E=@lMsW-w+CEUIX|Hq)$ literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-n32-be-mixed.o b/tests/fixtures/mips-weak-imports/identity-n32-be-mixed.o new file mode 100644 index 0000000000000000000000000000000000000000..0218961d5564c3e46350531243a83ec71d2f92bf GIT binary patch literal 1232 zcma)6O-md>5UrV2Hy>)uhae<}81RE2g8|7Q#k}UhmY{ieL(=>s9sZ+Sk>?eqDR>ItT+PERY+(ULox6 z9ml5%S_=+!D~DKhPT;z#=K7NZnaDU^-ewICXS@ZfQ~1QjJ0XA zwrk_e{o4O7k3rYXaUi*PmE(b+?+oKMAN68uKwJa5B!o_qX8XY(A-=d6=Q4cc!3 z1~_uBQF& QqTumD{v&-<5~Ab%13lqY9RL6T literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-n32-le-got-only.o b/tests/fixtures/mips-weak-imports/identity-n32-le-got-only.o new file mode 100644 index 0000000000000000000000000000000000000000..d4fefb30e508fd518eccd69f398fae73102fd5f3 GIT binary patch literal 1056 zcma)4O-md>5PjpAxax)&a}o5Q;$}lw8U@KA} z>NA=2^T62_OzM){e^i!4eQR9w$-%U(voNhRg-N1rZ`c{ZG#hW<8Qbc0`nJn_+)iNl z62>~0^jhaGO1aPfVv|C7E1j`UzDrZc%z|~AoV`vH{9w_0`bC=5%UYTCY#Qqe#*BSv zzZ#QrH~-S@QN!6{{(~8$pWdw`^sZl#zwmV5{-SFz@Bf?EjD5&C1gEt7$z+rT{9My? ucFAcE@j9F2Z)mqP`CY`MpVY}c=~+n;vrz6-q5YmP;p@g+I8zb5>V5;Twn*s! literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/identity-n32-le-mixed.o b/tests/fixtures/mips-weak-imports/identity-n32-le-mixed.o new file mode 100644 index 0000000000000000000000000000000000000000..fd15e2e0cc4b97bb029442240d95db774d129e35 GIT binary patch literal 1232 zcma)5O=}ZT6g`uPHh$N(f}o35=m$~;h2WyNNUIeBRtnt+t|n$0gJ~wrOhs3gb|Wab zaHHU&OMik37ygv|g)Usjb0%}0*MR5=ciy@8oqOM%`zCKUA3yRu4+al4lpb(g0haOp zpq>j>a2c0Ony-sL4}9R}ecC2pxkyIs4MkVJVj#6=-KNmi#6K`2_x2c}= zt?RMO$Jq(}>27?S$84dv!*z!JTz4{ViElNZZa&2NM)O`BY4|E?^SKux9%S9PANC}v z(xy02LELJHTSw~DG~%Waw=Sp2XNzl{BcChUmG+v`dd03jNZ;&bVGBW;CFAvlu_Wq+ zK^DEv5VXTAM9@l81WDBC_IG0hTg~mAAZ&Gadtrz1K|9eVQ<8Wr=M@KGmL=WReio%{ z;6E`>o$lY>sX?yU8uX6B4VvcDpb0;iq+wp5DPMaU=~Cg_mocF4!s@Lus9F9?cb6LW zHsCj&L-p(~6tKHlBfstF-u*?_WW4;|`9G!aLhm8$(4@0-kI8kG!T1vf+_|hUL37Yt zq_Md?BmXrT#awcHclear architecture load file tests/fixtures/octeon-relocations/pic-gcc73-n32.o read symbols + map prototype call_optional unsigned int call_optional(unsigned int x); + map prototype optional unsigned int optional(unsigned int x); lo fu call_optional decompile print C clear architecture load file tests/fixtures/octeon-relocations/pic-gcc47-64.o read symbols + map prototype call_optional unsigned int call_optional(unsigned int x); + map prototype optional unsigned int optional(unsigned int x); lo fu call_optional decompile print C + clear architecture + load file tests/fixtures/mips-weak-imports/identity-n32-be-mixed.o + read symbols + map prototype optional unsigned int optional(unsigned int x); + lo fu samedecompileprint C + clear architecture + load file tests/fixtures/mips-weak-imports/identity-64-be-mixed.o + read symbols + map prototype optional unsigned int optional(unsigned int x); + lo fu samedecompileprint C quit if \(dat_[0-9a-f]+\) - x = optional\(\); +optional\(x\); return optional\( +return dat_[0-9a-f]+ == optional; +ambiguous From a967373538bba41da49e1a51a8c6d8bcb4f4906f Mon Sep 17 00:00:00 2001 From: phix33 <122955334+phix33@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:39:23 +1100 Subject: [PATCH 3/6] [AUTOMATED] Keep a weak GOT_PAGE reference as the unfolded import slot --- .../src/loader/kuna_mipsreloc.rs | 7 +- .../src/loader/kuna_mipsreloc_tests.rs | 47 +++++++++++++ .../kuna-analysis/src/loadimage_object.rs | 20 +++--- .../kuna-cli/tests/mips_weak_imports.rs | 40 +++++++---- docs/spec/01-program-prep.md | 4 +- tests/fixtures/mips-weak-imports/README.md | 3 + tests/fixtures/mips-weak-imports/build.py | 6 +- tests/fixtures/mips-weak-imports/page-64-be.o | Bin 0 -> 1464 bytes tests/fixtures/mips-weak-imports/page-64-le.o | Bin 0 -> 1464 bytes .../fixtures/mips-weak-imports/page-n32-be.o | Bin 0 -> 1064 bytes .../fixtures/mips-weak-imports/page-n32-le.o | Bin 0 -> 1064 bytes tests/fixtures/mips-weak-imports/page.S | 27 ++++++++ .../mips-weak-imports/provenance.json | 62 ++++++++++++++++++ 13 files changed, 189 insertions(+), 27 deletions(-) create mode 100644 tests/fixtures/mips-weak-imports/page-64-be.o create mode 100644 tests/fixtures/mips-weak-imports/page-64-le.o create mode 100644 tests/fixtures/mips-weak-imports/page-n32-be.o create mode 100644 tests/fixtures/mips-weak-imports/page-n32-le.o create mode 100644 tests/fixtures/mips-weak-imports/page.S diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs index 3beb511bae..54679ea6ca 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs @@ -38,7 +38,9 @@ //! `lw $t9, %call16(f)($gp); jalr $t9` calls `f`. The exception is the entry of //! an undefined weak symbol: the run-time loader fills it, with 0 when nothing //! defines the symbol, so it is an import slot that holds 0 and is not folded, -//! and `if (f) f(x);` keeps its test. +//! and `if (f) f(x);` keeps its test. A `GOT_PAGE` against such a symbol +//! reaches the same slot and its `GOT_OFST` is the addend alone, as GNU ld +//! decays the pair for a symbol that does not bind locally. //! //! `R_MIPS_JALR` only says which function a `jalr` calls and is not applied. //! microMIPS, MIPS16, R6 PC-relative and TLS relocations are unsupported. @@ -390,7 +392,7 @@ impl Gp { let place = input.base.wrapping_add(offset as u64); let first = &records[expression[0].record].1; let import_of = |op: &Op, reloc: &Relocation| { - Some(reloc.target()).filter(|&t| op.special.is_none() && got_entry(op.op, false) == Some(true) && (input.weak)(t)) + Some(reloc.target()).filter(|&t| op.special.is_none() && (got_entry(op.op, false).is_some() || op.op == GOT_OFST) && (input.weak)(t)) }; let first_import = import_of(&expression[0], first); let first_target = match first_import { @@ -542,6 +544,7 @@ impl Gp { GOT16 | CALL16 | GOT_DISP => checked(got(self, sum)?, 16)?, GOT_HI16 | CALL_HI16 => got(self, sum)?.wrapping_add(0x8000) >> 16 & 0xffff, GOT_LO16 | CALL_LO16 => got(self, sum)? & 0xffff, + GOT_OFST if import.is_some() => checked(a, 16)?, GOT_OFST => checked(sum.wrapping_sub(page(sum)), 16)?, _ => return Err(RelocationFailure::Unsupported), }) diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs index 19c842eeeb..ba1c33d181 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs @@ -747,6 +747,53 @@ fn a_weak_functions_got_entry_is_a_named_import_slot() { } +/// A weak function reached through `GOT_PAGE`/`GOT_OFST` takes the import slot +/// GNU ld's global GOT entry is, with a zero offset; a local symbol's page +/// entry stays in the foldable GOT. +#[test] +fn a_weak_got_page_reaches_the_import_slot_with_a_zero_offset() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.."); + for abi in ["n32", "64"] { + for endian in ["be", "le"] { + let le = endian == "le"; + let width = if abi == "64" { 8 } else { 4 }; + for (stem, weak_pages) in [(format!("page-{abi}-{endian}"), 1), (format!("identity-{abi}-{endian}-mixed"), 0)] { + let bytes = std::fs::read(root.join(format!("tests/fixtures/mips-weak-imports/{stem}.o"))).unwrap(); + let file = object::File::parse(&*bytes).unwrap(); + let layout = layout(&bytes); + assert!(layout.diagnostics.is_empty(), "{stem}: {:?}", layout.diagnostics.report_lines()); + let [(lo, hi)] = layout.import_slots[..] else { panic!("{stem}: {:?}", layout.import_slots) }; + assert_eq!(hi - lo, width, "{stem}"); + let gp = layout.got_range.unwrap().0 + GP_BIAS; + let text = file.section_by_name(".text").unwrap(); + let base = layout.section_vma[&text.index()]; + let laid = &layout.segments.iter().find(|(a, _)| *a == base).unwrap().1; + let mut seen = 0; + for (offset, reloc) in text.relocations() { + let RelocationFlags::Elf { r_type } = reloc.flags() else { unreachable!() }; + let weak = match reloc.target() { + RelocationTarget::Symbol(i) => file.symbol_by_index(i).is_ok_and(|s| s.is_undefined() && s.is_weak()), + _ => false, + }; + let field = read_at(laid, offset, 4, le) as u16; + match r_type & 0xff { + GOT_PAGE => { + let slot = gp.wrapping_add(field as i16 as i64 as u64); + assert_eq!(slot == lo, weak, "{stem} at {offset:#x}"); + seen += usize::from(weak); + } + GOT_OFST if weak => assert_eq!(field, 0, "{stem} at {offset:#x}"), + _ => {} + } + } + assert_eq!(seen, weak_pages, "{stem}"); + let (segment, data) = layout.segments.iter().find(|(b, d)| lo >= *b && hi <= b + d.len() as u64).unwrap(); + assert!(data[(lo - segment) as usize..(hi - segment) as usize].iter().all(|&b| b == 0), "{stem}"); + } + } + } +} + #[test] fn mixed_weak_references_share_one_function_without_changing_relocation_values() { for abi in ["n32", "64"] { diff --git a/decompiler/crates/kuna-analysis/src/loadimage_object.rs b/decompiler/crates/kuna-analysis/src/loadimage_object.rs index 1890ca451f..2cfed6e9d9 100644 --- a/decompiler/crates/kuna-analysis/src/loadimage_object.rs +++ b/decompiler/crates/kuna-analysis/src/loadimage_object.rs @@ -1490,14 +1490,16 @@ impl ObjectLoadImage { /// holds a pointer the run-time loader writes. The engine reports these /// ranges so its whole-binary enumeration can decompile the image's code /// without inventing a body for every import. - pub fn function_address_aliases(&self) -> &[(u64, u64)] { - &self.function_address_aliases - } - pub fn import_slot_ranges(&self) -> &[(u64, u64)] { &self.import_slots } + /// (kuna) `(alias, canonical)` VMA pairs for one function the image names + /// at two addresses, such as a weak import's direct address and its slot. + pub fn function_address_aliases(&self) -> &[(u64, u64)] { + &self.function_address_aliases + } + /// The loader's function symbols as `(load_vma, name)` pairs — the SAME list /// `getNextSymbol` yields, but already **rebased** to the load VMA (for an /// `ET_REL` `.o` the raw `object` symbol address is section-relative; the @@ -3088,11 +3090,6 @@ mod tests { } } - /// PE import slots retain their loader identity independently of section - /// permissions. The side table, not CODE versus DATA, is what body - /// selection can rely on while keeping the slot's function symbol. - /// A MIPS object's foldable GOT pieces exclude its import slots, which - /// may sit at either end or in the middle. #[test] fn adjusting_a_weak_import_moves_its_slots_and_all_function_addresses() { let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -3109,6 +3106,8 @@ mod tests { assert_eq!(image.function_address_aliases(), aliases.iter().map(|&(a, c)| (a + 0x2000, c + 0x2000)).collect::>()); } + /// A MIPS object's foldable GOT pieces exclude its import slots, which + /// may sit at either end or in the middle. #[test] fn got_ranges_leave_out_import_slots() { assert_eq!(outside((0x100, 0x120), &[]), [(0x100, 0x11f)]); @@ -3118,6 +3117,9 @@ mod tests { assert_eq!(outside((0x100, 0x108), &[(0x200, 0x208)]), [(0x100, 0x107)]); } + /// PE import slots retain their loader identity independently of section + /// permissions. The side table, not CODE versus DATA, is what body + /// selection can rely on while keeping the slot's function symbol. #[test] fn pe_import_slots_are_reported_in_code_and_data_sections() { for (fixture, slot, name, executable) in [ diff --git a/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs index 95b7626dd2..ab53eda39f 100644 --- a/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs +++ b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs @@ -9,22 +9,34 @@ fn declared_weak_calls_preserve_null_guards_arguments_and_results() { if common::process::optional_output(Command::new("cc").arg("--version")).is_none() { return; } + let mut images = Vec::new(); for compiler in ["47", "73"] { for abi in ["n32", "64"] { - let image = common::repo_root().join(format!( - "tests/fixtures/octeon-relocations/pic-gcc{compiler}-{abi}.o" - )); - let out = - common::process::required_output(Command::new(env!("CARGO_BIN_EXE_kuna")).args([ - "decompile", - image.to_str().unwrap(), - "call_optional", - "--assert-strict", - "--assert", - "prototype call_optional unsigned int call_optional(unsigned int x)", - "--assert", - "prototype optional unsigned int optional(unsigned int x)", - ])); + images.push((format!("octeon-relocations/pic-gcc{compiler}-{abi}.o"), None)); + } + } + for (abi, little) in [("n32", "MIPS:LE:64:64-32addr"), ("64", "MIPS:LE:64:default")] { + images.push((format!("mips-weak-imports/page-{abi}-be.o"), None)); + images.push((format!("mips-weak-imports/page-{abi}-le.o"), Some(little))); + } + { + for (name, target) in images { + let image = common::repo_root().join("tests/fixtures").join(name); + let mut command = Command::new(env!("CARGO_BIN_EXE_kuna")); + command.args([ + "decompile", + image.to_str().unwrap(), + "call_optional", + "--assert-strict", + "--assert", + "prototype call_optional unsigned int call_optional(unsigned int x)", + "--assert", + "prototype optional unsigned int optional(unsigned int x)", + ]); + if let Some(target) = target { + command.args(["--target", target]); + } + let out = common::process::required_output(&mut command); let code = String::from_utf8(out.stdout).unwrap(); let slots: BTreeSet<_> = code .split(|c: char| !c.is_ascii_alphanumeric() && c != '_') diff --git a/docs/spec/01-program-prep.md b/docs/spec/01-program-prep.md index f2d1b8a2a9..5ae102cdc1 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -898,7 +898,9 @@ the section-flag translation, import resolution (§1.3), and extra constant rang symbol's address, or 0 when nothing defines it: each such symbol gets its own entry holding 0, reported as an import slot outside the foldable range and, when a call reaches it, named after the symbol as an Import Address Table - slot is, so `if (f) f(x);` keeps its test. A direct call or address + slot is, so `if (f) f(x);` keeps its test. A `GOT_PAGE` against the symbol + reaches that same entry and its `GOT_OFST` is the addend alone, the decay GNU + ld applies to a symbol that does not bind locally. A direct call or address relocation against the symbol still resolves to its synthetic extern. Otherwise `lw $t9, %call16(f)($gp); jalr $t9` decompiles to a call of `f` wherever `$t9` is seeded with the entry. `R_MIPS_JALR`, a hint naming the function a `jalr` diff --git a/tests/fixtures/mips-weak-imports/README.md b/tests/fixtures/mips-weak-imports/README.md index 41960c86ee..48adf89e56 100644 --- a/tests/fixtures/mips-weak-imports/README.md +++ b/tests/fixtures/mips-weak-imports/README.md @@ -5,6 +5,9 @@ weak function through its GOT entry; the mixed variant also reads a read-only pointer initialized by a direct R_MIPS_32/R_MIPS_64 relocation to that same ELF symbol. The GOT-only variant has no direct reference. Both restore their ABI GOT base before returning. N32/N64 and both byte orders are included. +`page.S` tests the same weak function through `%got_page`/`%got_ofst` and tail +calls it. GNU ld 2.30 `-shared` gives that pair one global GOT entry with +initial value 0 and leaves the `GOT_OFST` field 0, in both ABIs. Rebuild with `python3 build.py --assembler /path/to/mips64-octeon-linux-gnu-as`. The recorded GNU assembler is 2.30 from the local SDK; provenance records source, diff --git a/tests/fixtures/mips-weak-imports/build.py b/tests/fixtures/mips-weak-imports/build.py index 48a1878f14..49a7cf0381 100644 --- a/tests/fixtures/mips-weak-imports/build.py +++ b/tests/fixtures/mips-weak-imports/build.py @@ -12,5 +12,9 @@ argv=[a.assembler,'-EB' if endian=='be' else '-EL','-march=octeon','-mabi='+abi,'--defsym','ABI64='+str(int(abi=='64')),'--defsym','MIXED='+str(mixed),'-o',name,'identity.S'] subprocess.run(argv,cwd=root,check=True) records.append({'command':[Path(a.assembler).name,*argv[1:]],'output':name,'sha256':hashlib.sha256((root/name).read_bytes()).hexdigest()}) -manifest={'source':'identity.S','source_sha256':hashlib.sha256((root/'identity.S').read_bytes()).hexdigest(),'assembler_version':subprocess.check_output([a.assembler,'--version'],text=True).splitlines()[0],'assembler_sha256':hashlib.sha256(Path(a.assembler).read_bytes()).hexdigest(),'records':records} + name=f"page-{abi}-{endian}.o" + argv=[a.assembler,'-EB' if endian=='be' else '-EL','-march=octeon','-mabi='+abi,'--defsym','ABI64='+str(int(abi=='64')),'-o',name,'page.S'] + subprocess.run(argv,cwd=root,check=True) + records.append({'command':[Path(a.assembler).name,*argv[1:]],'output':name,'sha256':hashlib.sha256((root/name).read_bytes()).hexdigest()}) +manifest={'source':'identity.S','source_sha256':hashlib.sha256((root/'identity.S').read_bytes()).hexdigest(),'page_source':'page.S','page_source_sha256':hashlib.sha256((root/'page.S').read_bytes()).hexdigest(),'assembler_version':subprocess.check_output([a.assembler,'--version'],text=True).splitlines()[0],'assembler_sha256':hashlib.sha256(Path(a.assembler).read_bytes()).hexdigest(),'records':records} (root/'provenance.json').write_text(json.dumps(manifest,indent=2)+'\n') diff --git a/tests/fixtures/mips-weak-imports/page-64-be.o b/tests/fixtures/mips-weak-imports/page-64-be.o new file mode 100644 index 0000000000000000000000000000000000000000..acb4be4b8e638912a84ba1978d11bf2ccdfec8ec GIT binary patch literal 1464 zcmbtTK}#D!6#jN&Ok*vLrGlUb5z&Sn8m|Fe^6at0-pZ@3O|RhuD$||8c+#PeESVG1n1QPhSkDytcy4vQhZRU zvWmAFYvBh#DtB4n`_zImiZ&~raf86)qxNw;acPS8CmH3B{@TsV_fb&>ivaQe)FX0A za$HZm%;wT8GMynTAigw#=R3$->B;Rm*C}asC7;Q7qqe#B28TKf_d8wF>a=y}r1AV^ z6c_USxo;WMxA+S2Gqj%4a*Z+b^C_vKlg~!ip(4{u{3nTe%}^`T{9>RQ+GwbH6hUoo z?(V5%L_xaV`WWgXg55?Bimq2_V|uOnv1vwWr~eI>NkHyBe?Ng07jGf>9K;qWkarLW znRxGy#Rufew4{&R>jH7l9G;#+#`+mF25$eCM2p^i>R)$(M=VVu^lN3TZ_#Ro9rxq! zc}Mjx?0&xfx8?aeU6Xm4-=QVv;^uHX$P*UJc{AMWj7HZJ@&=Z7&xlReGy7)xlFjp?^I+$jZ)SF8XZM}H+}SEc5mX{nkXy)M?oo3dX!Uu2`a za@BxV^!$9zS2>R4TBhz~3Q!>Yvhs-gC-0qYkaoRHzf;s}r8ai04-RqDI*Yi`?<0P- zy}K7@EvjW3?YF5tB(vM>A*Sn%t#iG0D1i6;*ZjwndjsgwKc%htk^{d2>!MX@ zkp4d}$nh}C*ATD2i>2pOr{*jFnjE^9#;_hv8WULGtx|gg{*1>RwN(!Vg!2k(u}x>6 rk)Og;B!S;I-(lE4bK&ec^_S%3KLoS0XT3#SQU0zpb>Oq|CG7taIOa{7 literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/page-n32-be.o b/tests/fixtures/mips-weak-imports/page-n32-be.o new file mode 100644 index 0000000000000000000000000000000000000000..3cb81e0a63580da4f5acb98c269ab75b83558f78 GIT binary patch literal 1064 zcma)4&r4fD5T2LBUol{m3W6SDF)AVpf=DSIo&m*xiqN~d(HH~IB)qqRJvFqKLJvK7 zQSguOPsq_fBgbBQ=sEpn-HL`(O;cis8l--#AVM0uZvKK-B)`H#gkb&KhO%}W+{flu;9&T~VA z=$FJ#rH~Syp~P!@+JZ7sYh2k5M>6am6sPyzU)Kq;Vybu&t1FcN;+K@WaMw6;U5vj0Rt2fS@r1r~)UzD!Y&jVe9 zy>%9n!V4&EOjxg6nQD|2d*@LAkKaN1^?`Ybpnof~*m(}V)yVsa^8{-Qi{shHvt3Md zdCN9*S0vvmj6A2ecx$b=H+(-UZR|2-row)igqKqTKJu*>@c{fG&PSl)jSzDnf) literal 0 HcmV?d00001 diff --git a/tests/fixtures/mips-weak-imports/page-n32-le.o b/tests/fixtures/mips-weak-imports/page-n32-le.o new file mode 100644 index 0000000000000000000000000000000000000000..ba40eb5648313aa0e2fee2b47b29c86288fc6707 GIT binary patch literal 1064 zcma)4K}#D!6#mxLCbfcTQ7F`dmZnN60|hA}9@c=;Ko!Beby8yttV!66P)`PXDR}VU zrGh`gpWxA-k)u}+J*VH>-AAS^V&5|JzWLtwX5PM;leN$5K@dPKKoP?g@M#2?$60SN z6)a!^&s3WF@=Y%UW>$c!`MLfj@##5Gc{7HnRKfS9@@<52A7K%*j3LMUuw2?r^JjpI z6#O$qBRo1B%XzJ7b&^D*b%dZmFq-b^r>ZXF?gxG>{NvBKIX~Y*jQUGP_4JGm@#L?# z)G)M=y;Ft_!3&-@F%09i;MGd?<08J=IR4V^xMsU$W1;2b!c##Rewo3P*D8wSR3lh5 zg0->|@zgjc@f_1RhUp$3I$NW6N#+j}CH2@ESO4Z<_N=oowQd(CsW+Of{Wi=-b!*$$ zT5~_P4f3761co Date: Sun, 15 Nov 2026 00:04:57 +1100 Subject: [PATCH 4/6] [AUTOMATED] Stash import slots only for a relocatable object --- decompiler/crates/kuna-console/src/engine.rs | 46 +++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 2f82bdd335..0b079e83d5 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -4039,18 +4039,16 @@ fn input_isa_stash( vec![("input_isa", explicit)] } -/// (kuna) An ELF image's import slots, which only a MIPS relocatable object -/// has (the GOT entry of an undefined weak function, which the run-time loader -/// fills). No pass reports them, so they are stashed under the PE/Mach-O -/// import-binding pass's id (`peimportcall`) and committed with its facts. -fn elf_import_slot_stash( - bytes: &[u8], +/// (kuna) A relocatable object's import slots: the GOT entry of each undefined +/// weak symbol of a MIPS object, which the run-time loader fills. No pass +/// reports them, so they are stashed under the PE/Mach-O import-binding pass's +/// id (`peimportcall`) and committed with its facts. A linked image's slots +/// belong to its own passes and their options. +fn object_import_slot_stash( + relocatable: bool, slots: &[(u64, u64)], ) -> Option<(&'static str, kuna_analysis::pass::AnalysisOutput)> { - use object::Object as _; - let elf = kuna_analysis::loadimage_object::parse_object(bytes) - .is_ok_and(|file| file.format() == object::BinaryFormat::Elf); - (elf && !slots.is_empty()).then(|| { + (relocatable && !slots.is_empty()).then(|| { let mut output = kuna_analysis::pass::AnalysisOutput::default(); output.externref = slots.to_vec(); ("peimportcall", output) @@ -4766,7 +4764,10 @@ pub fn bootstrap_from_object_with_isa( sleigh.base().unwrap().translate(), ); pending_analysis.extend(input_isa_stash(input_context_paints)); - pending_analysis.extend(elf_import_slot_stash(&bytes, loader.import_slot_ranges())); + pending_analysis.extend(object_import_slot_stash( + !loader.reloc_sections().is_empty(), + loader.import_slot_ranges(), + )); // (kuna `dynrelocs`) The PT_GNU_RELRO-frozen dynamic-relocation slots, read // off the same loader while it is still in hand. `readonly_ranges` above @@ -6527,6 +6528,29 @@ mod tests { use super::{entry_name_rank, is_vtable_slot_name, FixedRefs, WholeOp}; + /// A linked ELF reports its GOT import slots too; only a relocatable + /// object's are committed outside the passes that own a linked image's. + #[test] + fn only_a_relocatable_objects_import_slots_are_stashed() { + let root = concat!(env!("CARGO_MANIFEST_DIR"), "/../../.."); + let linked = format!("{root}/decompiler/crates/kuna-analysis/tests/fixtures/elf_gotdata_x86_64.so"); + let object = format!("{root}/tests/fixtures/mips-weak-imports/page-64-be.o"); + for (path, relocatable) in [(linked, false), (object, true)] { + let image = kuna_analysis::loadimage_object::ObjectLoadImage::open(&path).unwrap(); + assert!(!image.import_slot_ranges().is_empty(), "{path}"); + assert_eq!(!image.reloc_sections().is_empty(), relocatable, "{path}"); + let stash = super::object_import_slot_stash( + !image.reloc_sections().is_empty(), + image.import_slot_ranges(), + ); + assert_eq!(stash.is_some(), relocatable, "{path}"); + if let Some((pass, output)) = stash { + assert_eq!(pass, "peimportcall"); + assert_eq!(output.externref, image.import_slot_ranges()); + } + } + } + /// A throwaway `(ram, constant)` space pair; `ram` stands in for the default /// data space, exactly as `listing::xrefs`'s own tests build it. fn spaces() -> (Rc, Rc) { From 3a74ce92c84a2e388dd49c3940764d11618a9f91 Mon Sep 17 00:00:00 2001 From: phix33 <122955334+phix33@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:32:18 +1100 Subject: [PATCH 5/6] [AUTOMATED] Preserve nullable weak addresses across relocation forms --- .../src/loader/kuna_nullable_reloc.rs | 215 +++++++++++ .../crates/kuna-analysis/src/loader/mod.rs | 2 + .../kuna-analysis/src/loader/reloc_object.rs | 27 ++ .../kuna-analysis/src/loadimage_object.rs | 13 + decompiler/crates/kuna-base/src/xml.rs | 2 +- .../crates/kuna-cli/tests/integration.rs | 3 + .../kuna-cli/tests/nullable_relocations.rs | 252 ++++++++++++ decompiler/crates/kuna-console/src/engine.rs | 18 + .../kuna-decomp/src/infra/architecture.rs | 6 + .../crates/kuna-decomp/src/p2_lift/flow.rs | 16 + .../src/p2_lift/kuna_nullable_reloc.rs | 328 ++++++++++++++++ .../crates/kuna-decomp/src/p2_lift/mod.rs | 2 + .../src/p3_dataflow/ruleaction_4.rs | 19 +- .../src/p9_emit/coreaction_render.rs | 3 +- .../crates/kuna-decomp/src/p9_emit/printc.rs | 6 +- .../kuna-decomp/src/substrate/context.rs | 4 + .../src/substrate/funcdata_varnode.rs | 37 ++ .../crates/kuna-decomp/src/substrate/op.rs | 6 + .../kuna-decomp/src/substrate/varnode.rs | 9 + docs/baseline-stages.json | 17 +- docs/spec/01-program-prep.md | 27 +- tests/fixtures/nullable-relocations/README.md | 25 ++ .../nullable-relocations/address-64.elf | Bin 0 -> 2752 bytes .../nullable-relocations/address-n32.elf | Bin 0 -> 1784 bytes .../address-x86-64-large.elf | Bin 0 -> 1696 bytes .../nullable-relocations/address-x86-64.elf | Bin 0 -> 1696 bytes tests/fixtures/nullable-relocations/address.c | 6 + tests/fixtures/nullable-relocations/build.py | 72 ++++ .../nullable-relocations/mixed-64.elf | Bin 0 -> 3808 bytes .../nullable-relocations/mixed-n32.elf | Bin 0 -> 2668 bytes .../nullable-relocations/provenance.json | 360 ++++++++++++++++++ .../nullable-relocations/readonly-64.elf | Bin 0 -> 2104 bytes .../nullable-relocations/readonly-mips.S | 46 +++ .../nullable-relocations/readonly-n32.elf | Bin 0 -> 1584 bytes .../nullable-relocations/readonly-x86-64.elf | Bin 0 -> 1320 bytes .../fixtures/nullable-relocations/readonly.S | 27 ++ .../nullable-relocations/rip-x86-64.elf | Bin 0 -> 1112 bytes tests/fixtures/nullable-relocations/rip.S | 36 ++ .../fixtures/nullable-relocations/weak-64.elf | Bin 0 -> 2632 bytes .../nullable-relocations/weak-n32.elf | Bin 0 -> 1740 bytes .../weak-x86-64-large.elf | Bin 0 -> 1664 bytes .../nullable-relocations/weak-x86-64.elf | Bin 0 -> 1688 bytes tests/fixtures/nullable-relocations/weak.c | 8 + tests/stages/kuna-mipsgot-weak.xml | 2 +- tests/stages/kuna-nullable-relocations.xml | 72 ++++ 45 files changed, 1646 insertions(+), 20 deletions(-) create mode 100644 decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs create mode 100644 decompiler/crates/kuna-cli/tests/nullable_relocations.rs create mode 100644 decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs create mode 100644 tests/fixtures/nullable-relocations/README.md create mode 100644 tests/fixtures/nullable-relocations/address-64.elf create mode 100644 tests/fixtures/nullable-relocations/address-n32.elf create mode 100644 tests/fixtures/nullable-relocations/address-x86-64-large.elf create mode 100644 tests/fixtures/nullable-relocations/address-x86-64.elf create mode 100644 tests/fixtures/nullable-relocations/address.c create mode 100644 tests/fixtures/nullable-relocations/build.py create mode 100644 tests/fixtures/nullable-relocations/mixed-64.elf create mode 100644 tests/fixtures/nullable-relocations/mixed-n32.elf create mode 100644 tests/fixtures/nullable-relocations/provenance.json create mode 100644 tests/fixtures/nullable-relocations/readonly-64.elf create mode 100644 tests/fixtures/nullable-relocations/readonly-mips.S create mode 100644 tests/fixtures/nullable-relocations/readonly-n32.elf create mode 100644 tests/fixtures/nullable-relocations/readonly-x86-64.elf create mode 100644 tests/fixtures/nullable-relocations/readonly.S create mode 100644 tests/fixtures/nullable-relocations/rip-x86-64.elf create mode 100644 tests/fixtures/nullable-relocations/rip.S create mode 100644 tests/fixtures/nullable-relocations/weak-64.elf create mode 100644 tests/fixtures/nullable-relocations/weak-n32.elf create mode 100644 tests/fixtures/nullable-relocations/weak-x86-64-large.elf create mode 100644 tests/fixtures/nullable-relocations/weak-x86-64.elf create mode 100644 tests/fixtures/nullable-relocations/weak.c create mode 100644 tests/stages/kuna-nullable-relocations.xml diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs new file mode 100644 index 0000000000..56ecce403a --- /dev/null +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs @@ -0,0 +1,215 @@ +//! Preserve nullable symbol identity separately from a pre-link display address. +use super::kuna_mipsreloc; +use kuna_decomp::kuna_nullable_reloc::{NullableRelocation, Operand}; +use object::{Object, ObjectSection, ObjectSymbol, RelocationTarget, SectionIndex, SymbolIndex}; +use std::collections::HashMap; + +pub(super) fn references( + file: &object::File<'_>, + vmas: &HashMap, + externs: &HashMap, + slots: &mut HashMap, + cursor: &mut u64, +) -> (Vec, Vec<(SymbolIndex, u64)>) { + let arch = file.architecture(); + let mips = matches!( + arch, + object::Architecture::Mips + | object::Architecture::Mips64 + | object::Architecture::Mips64_N32 + ); + if !mips && arch != object::Architecture::X86_64 { + return (Vec::new(), Vec::new()); + } + let pointer_size = if file.is_64() { 8 } else { 4 }; + let mut out = Vec::new(); + let mut allocated = Vec::new(); + for section in file.sections() { + let text = section.kind() == object::SectionKind::Text; + if !text + && !matches!( + section.kind(), + object::SectionKind::ReadOnlyData | object::SectionKind::ReadOnlyString + ) + { + continue; + } + let Some(&base) = vmas.get(§ion.index()) else { + continue; + }; + let data = section.data().unwrap_or(&[]); + let relocs: Vec<_> = section.relocations().collect(); + let lows = kuna_mipsreloc::lo_halves(file, &relocs, data, file.is_little_endian()); + for (i, (offset, reloc)) in relocs.iter().enumerate() { + let RelocationTarget::Symbol(idx) = reloc.target() else { + continue; + }; + let Ok(symbol) = file.symbol_by_index(idx) else { + continue; + }; + if !symbol.is_undefined() || !symbol.is_weak() { + continue; + } + let Some(&value) = externs.get(&idx) else { + continue; + }; + let Some(kind) = kuna_mipsreloc::r_type_of(reloc) else { + continue; + }; + let mut addend = reloc.addend(); + if !text { + let pointer = if mips { + kind == if pointer_size == 8 { 18 } else { 2 } + } else { + kind == 1 + }; + if !pointer { + continue; + } + if reloc.has_implicit_addend() { + let Some(bytes) = + data.get(*offset as usize..*offset as usize + pointer_size as usize) + else { + continue; + }; + addend = if file.is_little_endian() { + bytes.iter().rev().fold(0u64, |a, &b| (a << 8) | b as u64) + } else { + bytes.iter().fold(0u64, |a, &b| (a << 8) | b as u64) + } as i64; + } + let slot = *slots.entry(idx).or_insert_with(|| { + *cursor = (*cursor + pointer_size as u64 - 1) & !(pointer_size as u64 - 1); + let slot = *cursor; + *cursor += pointer_size as u64; + allocated.push((idx, slot)); + slot + }); + out.push(NullableRelocation { + field: base + offset, + slot, + symbol: value, + addend, + pointer_size, + operand: Operand::DataPointer, + shift: 0, + bias: 0, + patched: 0, + }); + continue; + } + let (operand, shift, bias, patched) = if mips { + if matches!(kind, 4 | 10) { + let mut reference = unsupported(base + offset, value, pointer_size); + reference.operand = Operand::CodeReference; + out.push(reference); + continue; + } + if kuna_mipsreloc::targets_code(kind) + || matches!(kind & 0xff, 0 | 9 | 19 | 20 | 21 | 22 | 23 | 37) + { + continue; + } + let word = data + .get(*offset as usize..*offset as usize + 4) + .map(|bytes| { + let bytes: [u8; 4] = bytes.try_into().unwrap(); + if file.is_little_endian() { + u32::from_le_bytes(bytes) + } else { + u32::from_be_bytes(bytes) + } + }); + let Some(word) = word else { continue }; + let (shift, bias) = match kind { + 5 => (16, 0x8000), + 6 => (0, 0), + 28 if pointer_size == 8 => (32, 0x80008000), + 29 if pointer_size == 8 => (48, 0x800080008000), + _ => { + out.push(unsupported(base + offset, value, pointer_size)); + continue; + } + }; + if reloc.has_implicit_addend() { + addend = match kind { + 5 => ((word as u16 as i64) << 16) + lows[i].unwrap_or(0), + 6 => word as u16 as i16 as i64, + _ => { + out.push(unsupported(base + offset, value, pointer_size)); + continue; + } + }; + } + let operand = match word >> 26 { + 15 => Operand::MipsLui, + 8 | 9 | 10 | 11 | 24 | 25 | 32..=63 => Operand::MipsSigned, + 12..=14 => Operand::MipsUnsigned, + _ => Operand::Unsupported, + }; + let patched = + value.wrapping_add(addend as u64).wrapping_add(bias) >> shift & 0xffff; + (operand, shift, bias, patched) + } else { + if kind == 4 + || (kind == 2 + && offset + .checked_sub(1) + .and_then(|p| data.get(p as usize)) + .is_some_and(|&b| matches!(b, 0xe8 | 0xe9))) + { + let mut reference = unsupported(base + offset, value, pointer_size); + reference.operand = Operand::CodeReference; + out.push(reference); + continue; + } + if reloc.has_implicit_addend() { + out.push(unsupported(base + offset, value, pointer_size)); + continue; + } + match kind { + 1 | 10 | 11 => (Operand::Absolute, 0, 0, 0), + 2 => (Operand::PcRelative, 0, 0, 0), + _ => { + out.push(unsupported(base + offset, value, pointer_size)); + continue; + } + } + }; + let slot = *slots.entry(idx).or_insert_with(|| { + *cursor = (*cursor + pointer_size as u64 - 1) & !(pointer_size as u64 - 1); + let slot = *cursor; + *cursor += pointer_size as u64; + allocated.push((idx, slot)); + slot + }); + out.push(NullableRelocation { + field: base + offset, + slot, + symbol: value, + addend, + pointer_size, + operand, + shift, + bias, + patched, + }); + } + } + out.sort_by_key(|r| r.field); + (out, allocated) +} + +fn unsupported(field: u64, symbol: u64, pointer_size: i32) -> NullableRelocation { + NullableRelocation { + field, + symbol, + pointer_size, + slot: 0, + addend: 0, + operand: Operand::Unsupported, + shift: 0, + bias: 0, + patched: 0, + } +} diff --git a/decompiler/crates/kuna-analysis/src/loader/mod.rs b/decompiler/crates/kuna-analysis/src/loader/mod.rs index b6794351df..eafeb140ac 100644 --- a/decompiler/crates/kuna-analysis/src/loader/mod.rs +++ b/decompiler/crates/kuna-analysis/src/loader/mod.rs @@ -134,3 +134,5 @@ pub mod kuna_realmodecs; pub mod kuna_mzdgroup; pub mod kuna_mipsstubs; + +mod kuna_nullable_reloc; diff --git a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs index 10869aa189..e0c391ad41 100644 --- a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs +++ b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs @@ -262,6 +262,7 @@ pub struct RelocLayout { /// Half-open import slots in the GOT: an undefined weak symbol's entry, /// which the run-time loader fills and which therefore does not fold. pub import_slots: Vec<(u64, u64)>, + pub nullable_relocations: Vec, /// Additional addresses of the same logical import: `(alias, canonical)`. pub function_address_aliases: Vec<(u64, u64)>, /// A PowerPC64 ELFv1 object's function descriptors: `(descriptor, entry, @@ -720,6 +721,24 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL } } } + let weak_storage_start = extern_cursor; + let mut nullable_slots: HashMap<_, _> = gp.as_ref() + .map(|g| g.imports().collect()).unwrap_or_default(); + let (nullable_relocations, allocated) = super::kuna_nullable_reloc::references( + file, &vma_of, &extern_of, &mut nullable_slots, &mut extern_cursor); + for (idx, slot) in allocated { + let Ok(sym) = file.symbol_by_index(idx) else { continue }; + import_slots.push((slot, slot + if file.is_64() { 8 } else { 4 })); + if !code_externs.contains(&idx) && sym.kind() != SymbolKind::Text { continue; } + let Ok(name) = sym.name_bytes() else { continue }; + symbol_info.push(RelocSymbolInfo { + name: String::from_utf8_lossy(name).into_owned(), vma: slot, + section_index: None, section_name: None, section_offset: None, + binding: symbol_binding(&sym), undefined: true, + }); + funcsyms.push((slot, name.to_vec())); + import_functions.insert(idx, slot); + } // Extern functions: name each external *code target* at its synthetic slot // so `call ext` renders by name instead of a bare address. A code target is // an undefined symbol reached through a PLT-relative reloc (reliable even for @@ -757,6 +776,13 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL let mut segments: Vec<(u64, Vec)> = laid.iter().map(|l| (l.vma, l.data.clone())).collect(); let mut sections: Vec<(u64, u64, u32)> = laid.iter().map(|l| (l.vma, l.size, l.flags)).collect(); + if extern_cursor > weak_storage_start { + segments.push((weak_storage_start, vec![0; (extern_cursor - weak_storage_start) as usize])); + sections.push((weak_storage_start, extern_cursor - weak_storage_start, + fmt.section_bits(".kuna.weak", SectionKind::Data, object::SectionFlags::Elf { + sh_flags: u64::from(object::elf::SHF_ALLOC | object::elf::SHF_WRITE), + }))); + } let got_range = match (&gp, got_base) { (Some(g), Some(base)) if g.size() != 0 => { segments.push((base, g.bytes())); @@ -793,6 +819,7 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL got_range, gp: gp.as_ref().map(kuna_mipsreloc::Gp::gp), import_slots, + nullable_relocations, function_address_aliases, opd: { opd_descriptors.sort_unstable(); diff --git a/decompiler/crates/kuna-analysis/src/loadimage_object.rs b/decompiler/crates/kuna-analysis/src/loadimage_object.rs index 2cfed6e9d9..9af8db73ba 100644 --- a/decompiler/crates/kuna-analysis/src/loadimage_object.rs +++ b/decompiler/crates/kuna-analysis/src/loadimage_object.rs @@ -577,6 +577,7 @@ pub struct ObjectLoadImage { /// a whole-binary enumeration needs to keep them out of the decompile set. /// Empty for every non-PE image and for a relocatable object. import_slots: Vec<(u64, u64)>, + nullable_relocations: Vec, function_address_aliases: Vec<(u64, u64)>, /// (kuna `pltlocal`) `(stub, definition)` for each PLT stub the image's own /// dynamic relocations bind to a function it defines, ascending by stub. See @@ -1182,6 +1183,7 @@ impl ObjectLoadImage { const_ranges, dynreloc_const, import_slots, + nullable_relocations: Vec::new(), function_address_aliases: Vec::new(), plt_local, mips_linkage, @@ -1215,6 +1217,7 @@ impl ObjectLoadImage { let layout = reloc_object::layout_relocatable(file, fmt); let got_range = layout.got_range; let import_slots = layout.import_slots.clone(); + let nullable_relocations = layout.nullable_relocations.clone(); let function_address_aliases = layout.function_address_aliases.clone(); let elfv1 = crate::loader::elfv1::Descriptors( layout @@ -1374,6 +1377,7 @@ impl ObjectLoadImage { // are relocations, resolved by the layout pass. A MIPS object's // undefined weak symbols keep GOT entries the run-time loader fills. import_slots, + nullable_relocations, function_address_aliases, plt_local: Vec::new(), mips_linkage: crate::loader::kuna_mipsstubs::class_linkage(&file), @@ -1490,6 +1494,10 @@ impl ObjectLoadImage { /// holds a pointer the run-time loader writes. The engine reports these /// ranges so its whole-binary enumeration can decompile the image's code /// without inventing a body for every import. + pub fn nullable_relocations(&self) -> &[kuna_decomp::kuna_nullable_reloc::NullableRelocation] { + &self.nullable_relocations + } + pub fn import_slot_ranges(&self) -> &[(u64, u64)] { &self.import_slots } @@ -1812,6 +1820,11 @@ impl LoadImage for ObjectLoadImage { r.0 = r.0.wadd(badjust); r.1 = r.1.wadd(badjust); } + for r in &mut self.nullable_relocations { + r.field = r.field.wadd(badjust); + r.symbol = r.symbol.wadd(badjust); + if r.slot != 0 { r.slot = r.slot.wadd(badjust); } + } for r in self.function_address_aliases.iter_mut().chain(&mut self.import_slots) { r.0 = r.0.wadd(badjust); r.1 = r.1.wadd(badjust); diff --git a/decompiler/crates/kuna-base/src/xml.rs b/decompiler/crates/kuna-base/src/xml.rs index 055dc89372..52e765e42e 100644 --- a/decompiler/crates/kuna-base/src/xml.rs +++ b/decompiler/crates/kuna-base/src/xml.rs @@ -2205,8 +2205,8 @@ mod tests { // and kuna-repframecopy / a by-value argument copied with rep movs reaches its call // and kuna-libcnames-win32 / a local takes the name of the Win32 call it meets // and kuna-libcnames-addr / a stack object takes the name of the library parameter its address meets - assert_eq!(count, 757, "corpus file count drifted"); // and kuna-mipsgot-weak / unresolved weak GOT entries retain import-slot identity + assert_eq!(count, 758, "corpus file count drifted"); } /// ~20 representative SLEIGH spec files across varied processors diff --git a/decompiler/crates/kuna-cli/tests/integration.rs b/decompiler/crates/kuna-cli/tests/integration.rs index 9524613b2c..e7354645b3 100644 --- a/decompiler/crates/kuna-cli/tests/integration.rs +++ b/decompiler/crates/kuna-cli/tests/integration.rs @@ -849,3 +849,6 @@ mod cvf_trace; #[path = "mips_weak_imports.rs"] mod mips_weak_imports; + +#[path = "nullable_relocations.rs"] +mod nullable_relocations; diff --git a/decompiler/crates/kuna-cli/tests/nullable_relocations.rs b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs new file mode 100644 index 0000000000..1add17d2f9 --- /dev/null +++ b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs @@ -0,0 +1,252 @@ +//! Nullable identities survive address fragments, addends and ordinary import calls. +use crate::common; +use std::collections::BTreeSet; +use std::process::Command; + +fn decompile(root: &std::path::Path, file: &str, name: &str, prototype: &str) -> String { + let (code, error, rc) = common::run_kuna(&[ + "decompile", + root.join(file).to_str().unwrap(), + name, + "--assert-strict", + "--assert", + &format!("prototype {name} {prototype}"), + "--assert", + "prototype optional int optional(int value)", + ]); + assert_eq!(rc, 0, "{error}"); + assert!(!code.contains("Nullable weak"), "{code}"); + code +} + +#[test] +#[cfg(all(target_os = "linux", target_pointer_width = "64"))] +fn weak_imports_remain_nullable_across_non_pic_and_mixed_forms() { + if common::process::optional_output(Command::new("cc").arg("--version")).is_none() { + return; + } + let root = common::repo_root().join("tests/fixtures/nullable-relocations"); + let manifest: serde_json::Value = + serde_json::from_slice(&std::fs::read(root.join("provenance.json")).unwrap()).unwrap(); + for object in manifest["objects"].as_array().unwrap() { + let file = object["file"].as_str().unwrap(); + let abi = object["abi"].as_str().unwrap(); + let kind = object["kind"].as_str().unwrap(); + let wide = matches!(abi, "64" | "x86-64-large" | "x86-64-rip" | "x86-64-rodata"); + let word = if wide { "uint64_t" } else { "uint32_t" }; + let mut functions = Vec::new(); + if kind == "address" { + let output = if abi == "n32" { + "unsigned int" + } else { + "unsigned long" + }; + for name in [ + "address_value", + "address_offset", + "address_negative", + "unrelated_literal", + ] { + functions.push((name.to_string(), format!("{output} {name}(void)"))); + } + } else { + for prefix in if kind == "mixed" { + vec!["", "pic_"] + } else { + vec![""] + } { + for (name, args) in [ + ("guard", "int value"), + ("offset_guard", "void"), + ("defined_guard", "int value"), + ("present", "int value"), + ] { + functions.push(( + format!("{prefix}{name}"), + format!("int {prefix}{name}({args})"), + )); + } + } + } + let codes: Vec<_> = functions + .iter() + .map(|(name, prototype)| decompile(&root, file, name, prototype)) + .collect(); + let code = codes.join("\n"); + let names: BTreeSet<_> = code + .split(|c: char| !c.is_ascii_alphanumeric() && c != '_') + .filter(|name| { + name.starts_with("dat_") && name[4..].chars().all(|c| c.is_ascii_hexdigit()) + }) + .collect(); + let source = common::scratch_file("nullable-relocations", "c"); + let driver = source.with_extension("driver.c"); + let executable = source.with_extension("exe"); + let globals: String = names + .iter() + .map(|name| format!("extern {word} {name};\n")) + .collect(); + let declarations: String = codes + .iter() + .map(|code| format!("{};\n", code.split_once('{').unwrap().0.trim())) + .collect(); + std::fs::write(&source, format!("#include \n#include \nextern int optional(int) __attribute__((weak));\n{globals}{declarations}{code}")).unwrap(); + let definitions: String = names + .iter() + .map(|name| format!("{word} {name};\n")) + .collect(); + let setters: String = names + .iter() + .map(|name| format!("{name}=({word})bits;")) + .collect(); + let bindings = if kind == "address" { + vec![0] + } else { + vec![0, 1] + }; + for bound in bindings { + let body = if kind == "address" { + let values = if abi == "x86-64" { + "0x8000,0x400000,0x7fff7ff0" + } else if abi == "x86-64-rip" { + "0,1,0x7fff,0x8000,0x400000,0x7fff7ff0" + } else if abi == "n32" { + "0,1,0x7fff,0x8000,0x400000,0x7fff7ff0,0x80000000,0xfffffff0" + } else { + "0,1,0x7fff,0x8000,0x400000,0x7fffffffffffffff,0x8000000000000000,0xffffffff80007ff0,0xffffffffffffffff" + }; + let result = if abi == "n32" { "uint32_t" } else { "uint64_t" }; + format!("int main(void){{uint64_t values[]={{{values}}};for(unsigned i=0;i\n{declarations}{definitions}{body}\n"), + ) + .unwrap(); + for opt in ["-O0", "-O2"] { + common::process::required_output( + Command::new("cc") + .args([ + "-std=c11", + "-no-pie", + opt, + "-Wall", + "-Wextra", + "-Werror", + "-Wno-parentheses", + "-Wuninitialized", + "-fno-lto", + "-fsanitize=undefined", + "-fno-sanitize-recover=undefined", + &format!("-DBOUND={bound}"), + ]) + .arg(&source) + .arg(&driver) + .arg("-Wl,--section-start=.weakbinding=0x407ff0") + .arg("-o") + .arg(&executable), + ); + let output = common::process::output_with_timeout( + &mut Command::new(&executable), + std::time::Duration::from_secs(10), + std::time::Duration::from_millis(10), + ) + .unwrap(); + assert!( + output.status.success(), + "{file}: {code}\n{}", + String::from_utf8_lossy(&output.stderr) + ); + } + } + for path in [source, driver, executable] { + std::fs::remove_file(path).unwrap(); + } + } +} + +#[test] +fn unmatched_weak_operands_report_the_missing_model() { + let root = common::repo_root().join("tests/fixtures/nullable-relocations"); + let (code, error, rc) = common::run_kuna(&[ + "decompile", + root.join("rip-x86-64.elf").to_str().unwrap(), + "unsupported_offset", + ]); + assert_eq!(rc, 0, "{error}"); + assert!( + code.contains("Nullable weak relocation operand could not be modeled"), + "{code}" + ); +} + +#[test] +#[cfg(all(target_os = "linux", target_pointer_width = "64"))] +fn readonly_and_got_weak_references_share_identity() { + if common::process::optional_output(Command::new("cc").arg("--version")).is_none() { + return; + } + for abi in ["n32", "64"] { + let image = common::repo_root().join(format!( + "tests/fixtures/mips-weak-imports/identity-{abi}-be-mixed.o" + )); + let (code, error, rc) = common::run_kuna(&[ + "decompile", + image.to_str().unwrap(), + "same", + "--assert-strict", + "--assert", + "prototype same unsigned int same(void)", + "--assert", + "prototype optional int optional(int value)", + ]); + assert_eq!(rc, 0, "{error}"); + // One immutable weak binding on both sides makes this equality true, + // including the unbound zero case. No arbitrary synthetic address fits. + assert!( + code.contains("return 1;") || code.contains("return true;"), + "{code}" + ); + let source = common::scratch_file("weak-identity", "c"); + let exe = source.with_extension("exe"); + std::fs::write( + &source, + format!("#include \n{code}\nint main(void){{return same()!=1;}}\n"), + ) + .unwrap(); + for opt in ["-O0", "-O2"] { + common::process::required_output( + Command::new("cc") + .args([ + opt, + "-Wall", + "-Wextra", + "-Werror", + "-fsanitize=undefined", + "-fno-sanitize-recover=undefined", + ]) + .arg(&source) + .arg("-o") + .arg(&exe), + ); + let output = common::process::output_with_timeout( + &mut Command::new(&exe), + std::time::Duration::from_secs(5), + std::time::Duration::from_millis(10), + ) + .unwrap(); + assert!(output.status.success()); + } + for path in [source, exe] { + std::fs::remove_file(path).unwrap(); + } + } +} diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 0b079e83d5..d892184da7 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -4764,6 +4764,24 @@ pub fn bootstrap_from_object_with_isa( sleigh.base().unwrap().translate(), ); pending_analysis.extend(input_isa_stash(input_context_paints)); + let mut nullable = std::collections::BTreeMap::>::new(); + for record in loader.nullable_relocations() { + nullable.entry(record.field).or_default().push(record.clone()); + } + let mut literals = std::collections::BTreeSet::new(); + for record in loader.nullable_relocations() { + literals.extend((0..16).map(|byte| record.symbol.wrapping_add(byte))); + if record.slot != 0 { literals.extend((0..record.pointer_size as u64).map(|byte| record.slot + byte)); } + } + if !loader.reloc_sections().is_empty() { + for &(lo, hi) in loader.import_slot_ranges() { literals.extend(lo..hi); } + for &(alias, slot) in loader.function_address_aliases() { + literals.extend((0..16).map(|byte| alias.wrapping_add(byte))); + literals.extend((0..8).map(|byte| slot.wrapping_add(byte))); + } + } + sleigh.base_mut().unwrap().nullable_literal_values = std::rc::Rc::new(literals); + sleigh.base_mut().unwrap().nullable_relocations = std::rc::Rc::new(nullable); pending_analysis.extend(object_import_slot_stash( !loader.reloc_sections().is_empty(), loader.import_slot_ranges(), diff --git a/decompiler/crates/kuna-decomp/src/infra/architecture.rs b/decompiler/crates/kuna-decomp/src/infra/architecture.rs index bfdda9a670..ad3d0ff146 100644 --- a/decompiler/crates/kuna-decomp/src/infra/architecture.rs +++ b/decompiler/crates/kuna-decomp/src/infra/architecture.rs @@ -270,6 +270,8 @@ pub struct Architecture { pub loader_transfer_sites: Rc>, /// MIPS GOT import entries identified by the loader, never ordinary definitions. pub loader_mips_imports: Rc>, + pub nullable_relocations: Rc>>, + pub nullable_literal_values: Rc>, // --- Configuration data (architecture.hh:170-208) --------------------- /// How many levels to let parameter trims recurse (C++ `trim_recurse_max`). @@ -2236,6 +2238,8 @@ impl Architecture { loader_mips_t9: None, loader_transfer_sites: Rc::default(), loader_mips_imports: Rc::default(), + nullable_relocations: Rc::default(), + nullable_literal_values: Rc::default(), symbol_snapshots: RefCell::new(SymbolSnapshots::default()), kuna_snapshot_cache: std::env::var_os("KUNA_NO_SYMBOL_SNAPSHOT_CACHE").is_none(), @@ -3629,6 +3633,8 @@ impl Architecture { ctx.callee_protos = callee_protos; ctx.loader_transfer_sites = Rc::clone(&self.loader_transfer_sites); ctx.loader_mips_imports = Rc::clone(&self.loader_mips_imports); + ctx.nullable_relocations = Rc::clone(&self.nullable_relocations); + ctx.nullable_literal_values = Rc::clone(&self.nullable_literal_values); // (kuna) The convention each of those callees was DECLARED under, keyed // the same way the pieces are (entry address). The declaration names the // function, the parked pieces carry that name, and the read side diff --git a/decompiler/crates/kuna-decomp/src/p2_lift/flow.rs b/decompiler/crates/kuna-decomp/src/p2_lift/flow.rs index 4fbedcce45..0b0ee15074 100644 --- a/decompiler/crates/kuna-decomp/src/p2_lift/flow.rs +++ b/decompiler/crates/kuna-decomp/src/p2_lift/flow.rs @@ -2016,6 +2016,14 @@ truncating the fall-through here" self.maxaddr = endaddr.clone(); } + if !self.data.get_arch().nullable_relocations.is_empty() { + let first = if emptyflag { self.dead_head() } else { marker.and_then(|op| self.dead_next(op)) }; + let mut ops = Vec::new(); + let mut cursor = first; + while let Some(op) = cursor { ops.push(op); cursor = self.dead_next(op); } + crate::kuna_nullable_reloc::materialize(&mut self.data, curaddr, step, &ops); + } + // oiter points at first new op: beginDead() if was empty, else ++marker. let first_new: Option = if emptyflag { self.dead_head() @@ -2893,6 +2901,14 @@ truncating the fall-through here" entry = info.entry; } } + let numeric = self.data.obank().get(op).and_then(|o| o.get_in(0)) + .and_then(|vn| self.data.vbank().get(vn)).is_some_and(|v| v.is_synthetic_literal()); + if numeric { + let target = self.data.new_constant(entry.get_addr_size() as i32, entry.get_offset()); + self.data.op_set_input(op, target, 0)?; + self.data.op_set_opcode_code(op, OpCode::CPUI_CALLIND); + return self.setup_callind_specs(op); + } self.build_call_specs(op, entry.clone(), false)?; self.qlst_count += 1; // While weaving a call-fixup body, a nested direct CALL to the SAME diff --git a/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs b/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs new file mode 100644 index 0000000000..e66176b8d6 --- /dev/null +++ b/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs @@ -0,0 +1,328 @@ +//! Keep an undefined weak address symbolic at its exact relocated operand. +use crate::context::{OpId, VarnodeId}; +use crate::dtype::TypeFactory; +use crate::funcdata::Funcdata; +use kuna_base::address::{calc_mask, Address}; +use kuna_num::opcodes::OpCode; + +#[derive(Clone, Copy, Debug)] +pub enum Operand { + Unsupported, + CodeReference, + DataPointer, + MipsLui, + MipsSigned, + MipsUnsigned, + Absolute, + PcRelative, +} + +#[derive(Clone, Debug)] +pub struct NullableRelocation { + pub field: u64, + pub slot: u64, + pub symbol: u64, + pub addend: i64, + pub pointer_size: i32, + pub operand: Operand, + pub shift: u32, + pub bias: u64, + pub patched: u64, +} + +/// Run before xrefs or SSA discard the instruction/operand provenance. The +/// supported operand forms are explicit; an unmatched form remains diagnosed. +pub(crate) fn materialize(data: &mut Funcdata, address: &Address, step: i32, ops: &[OpId]) { + // SLEIGH can emit a direct memory varnode for a fixed address rather than + // a LOAD (notably x86 RIP-relative reads). Preserve those reads here; + // computed addresses are handled when RuleLoadVarnode materializes them. + for &id in ops { + let count = data.obank().get(id).unwrap().num_input(); + for slot in 0..count { + let Some(vn) = data + .obank() + .get(id) + .unwrap() + .get_in(slot) + .and_then(|v| data.vbank().get(v)) + else { + continue; + }; + if address + .get_space() + .is_none_or(|s| s.get_index() != vn.get_space().get_index()) + { + continue; + } + let record = data + .get_arch() + .nullable_relocations + .get(&vn.get_offset()) + .and_then(|records| { + records.iter().find(|r| { + r.pointer_size == vn.get_size() && matches!(r.operand, Operand::DataPointer) + }) + }) + .cloned(); + let Some(record) = record else { continue }; + let space = address.get_space().unwrap().clone(); + let space_id = data.new_varnode_space(&space); + let location = data.new_constant(space.get_addr_size() as i32, record.slot); + data.vbank_mut() + .get_mut(location) + .unwrap() + .set_synthetic_literal(false); + let mut value = emit( + data, + id, + OpCode::CPUI_LOAD, + record.pointer_size, + &[space_id, location], + ); + if record.addend != 0 { + let addend = data.new_constant(record.pointer_size, record.addend as u64); + value = emit( + data, + id, + OpCode::CPUI_INT_ADD, + record.pointer_size, + &[value, addend], + ); + } + data.op_set_input(id, value, slot) + .expect("read-only weak pointer"); + } + } + let end = address.get_offset().saturating_add(step as u64); + let records: Vec<_> = data + .get_arch() + .nullable_relocations + .range(address.get_offset()..end) + .flat_map(|(_, refs)| refs.iter().cloned()) + .collect(); + for record in records { + if matches!(record.operand, Operand::CodeReference) { + let mut found = false; + for &id in ops { + let op = data.obank().get(id).unwrap(); + if matches!( + op.code(), + OpCode::CPUI_CALL | OpCode::CPUI_BRANCH | OpCode::CPUI_CBRANCH + ) { + if let Some(vn) = op.get_in(0).filter(|&vn| { + data.vbank() + .get(vn) + .is_some_and(|v| v.get_addr().get_offset() == record.symbol) + }) { + data.vbank_mut() + .get_mut(vn) + .unwrap() + .set_synthetic_literal(false); + found = true; + } + } + } + if !found { + data.warning( + "Nullable weak code relocation could not be modeled", + address, + ); + } + continue; + } + let mut matches = Vec::new(); + for &id in ops { + let Some(op) = data.obank().get(id) else { + continue; + }; + for slot in 0..op.num_input() { + let Some(vn) = op.get_in(slot).and_then(|v| data.vbank().get(v)) else { + continue; + }; + if !vn.is_constant() || !(1..=8).contains(&vn.get_size()) { + continue; + } + let expected = match record.operand { + Operand::MipsLui + if op.code() == OpCode::CPUI_INT_LEFT + && slot == 0 + && op + .get_in(1) + .and_then(|v| data.vbank().get(v)) + .is_some_and(|v| v.is_constant() && v.get_offset() == 16) => + { + Some(record.patched) + } + Operand::MipsSigned + if slot == 1 + && matches!( + op.code(), + OpCode::CPUI_INT_ADD + | OpCode::CPUI_INT_LESS + | OpCode::CPUI_INT_SLESS + ) => + { + Some(record.patched as u16 as i16 as i64 as u64) + } + Operand::MipsUnsigned + if slot == 1 + && matches!( + op.code(), + OpCode::CPUI_INT_OR | OpCode::CPUI_INT_AND | OpCode::CPUI_INT_XOR + ) => + { + Some(record.patched) + } + Operand::Absolute if op.code() == OpCode::CPUI_COPY && slot == 0 => { + Some(record.symbol.wrapping_add(record.addend as u64)) + } + Operand::PcRelative + if slot == 0 && op.code() == OpCode::CPUI_COPY + || matches!(record.operand, Operand::PcRelative) + && slot == 1 + && matches!(op.code(), OpCode::CPUI_LOAD | OpCode::CPUI_STORE) => + { + Some( + record + .symbol + .wrapping_add(record.addend as u64) + .wrapping_add(end - record.field), + ) + } + _ => None, + }; + if expected + .is_some_and(|expected| expected & calc_mask(vn.get_size()) == vn.get_offset()) + { + matches.push((id, slot, vn.get_size())); + } + } + } + if matches.len() != 1 { + data.warning("Nullable weak relocation operand could not be modeled; address guards may be incomplete", address); + continue; + } + let (before, slot, size) = matches[0]; + let space = address.get_space().unwrap().clone(); + let space_id = data.new_varnode_space(&space); + let location = data.new_constant(space.get_addr_size() as i32, record.slot); + data.vbank_mut() + .get_mut(location) + .unwrap() + .set_synthetic_literal(false); + let mut value = emit( + data, + before, + OpCode::CPUI_LOAD, + record.pointer_size, + &[space_id, location], + ); + let mut addend = record.addend as u64; + if matches!(record.operand, Operand::PcRelative) { + addend = addend.wrapping_add(end - record.field); + } + addend = addend.wrapping_add(record.bias); + if addend != 0 { + let amount = data.new_constant(record.pointer_size, addend); + value = emit( + data, + before, + OpCode::CPUI_INT_ADD, + record.pointer_size, + &[value, amount], + ); + } + if record.shift != 0 { + let count = data.new_constant(4, record.shift as u64); + value = emit( + data, + before, + OpCode::CPUI_INT_RIGHT, + record.pointer_size, + &[value, count], + ); + } + if matches!( + record.operand, + Operand::MipsLui | Operand::MipsSigned | Operand::MipsUnsigned + ) { + let zero = data.new_constant(4, 0); + value = emit(data, before, OpCode::CPUI_SUBPIECE, 2, &[value, zero]); + if size > 2 { + let code = if matches!(record.operand, Operand::MipsSigned) { + OpCode::CPUI_INT_SEXT + } else { + OpCode::CPUI_INT_ZEXT + }; + value = emit(data, before, code, size, &[value]); + } + } else if size < record.pointer_size { + let zero = data.new_constant(4, 0); + value = emit(data, before, OpCode::CPUI_SUBPIECE, size, &[value, zero]); + } else if size > record.pointer_size { + value = emit(data, before, OpCode::CPUI_INT_ZEXT, size, &[value]); + } + data.op_set_input(before, value, slot) + .expect("relocation operand"); + if matches!( + record.operand, + Operand::MipsLui | Operand::MipsSigned | Operand::MipsUnsigned + ) { + for &id in ops { + let op = data.obank().get(id).unwrap(); + if !matches!( + op.code(), + OpCode::CPUI_INT_LEFT + | OpCode::CPUI_INT_SEXT + | OpCode::CPUI_INT_ZEXT + | OpCode::CPUI_INT_ADD + | OpCode::CPUI_COPY + ) { + continue; + } + if let Some(out) = op.get_out() { + if let Some(types) = data.get_arch().types_rc() { + let size = data.vbank().get(out).unwrap().get_size(); + if let Ok(ty) = types.get_base(size, crate::dtype::type_metatype::TYPE_UINT) + { + data.vbank_mut() + .get_mut(out) + .unwrap() + .update_type_locked(ty, true, true); + } + } + } + } + } + } +} + +fn emit( + data: &mut Funcdata, + before: OpId, + code: OpCode, + size: i32, + inputs: &[VarnodeId], +) -> VarnodeId { + let op = data.new_op( + inputs.len() as i32, + data.obank().get(before).unwrap().get_addr().clone(), + ); + data.op_set_opcode_code(op, code); + data.op_set_all_input(op, inputs) + .expect("symbolic expression"); + let out = data.new_unique_out(size, op).expect("symbolic output"); + if code != OpCode::CPUI_LOAD { + if let Some(types) = data.get_arch().types_rc() { + let meta = crate::dtype::type_metatype::TYPE_UINT; + if let Ok(ty) = types.get_base(size, meta) { + data.vbank_mut() + .get_mut(out) + .unwrap() + .update_type_locked(ty, true, true); + } + } + } + data.obank_mut().insert_before_dead(op, before); + out +} diff --git a/decompiler/crates/kuna-decomp/src/p2_lift/mod.rs b/decompiler/crates/kuna-decomp/src/p2_lift/mod.rs index 41ccae3089..f34f1f6e6f 100644 --- a/decompiler/crates/kuna-decomp/src/p2_lift/mod.rs +++ b/decompiler/crates/kuna-decomp/src/p2_lift/mod.rs @@ -67,3 +67,5 @@ pub(crate) mod kuna_highslice_guard; pub mod kuna_stubtailcall; // (kuna) a loader-proven lazy-binding stub flows as a tail call pub mod kuna_ppcpcread; + +pub mod kuna_nullable_reloc; diff --git a/decompiler/crates/kuna-decomp/src/p3_dataflow/ruleaction_4.rs b/decompiler/crates/kuna-decomp/src/p3_dataflow/ruleaction_4.rs index 04d64f3e02..47c5888d1a 100644 --- a/decompiler/crates/kuna-decomp/src/p3_dataflow/ruleaction_4.rs +++ b/decompiler/crates/kuna-decomp/src/p3_dataflow/ruleaction_4.rs @@ -279,10 +279,23 @@ impl Rule for RuleLoadVarnode { }; let size = size_of(data, out_vn(data, op)); let offoff = kuna_base::space::AddrSpace::address_to_byte(offoff, baseoff.get_word_size()); - let newvn = data.new_varnode_space_off(size, Rc::clone(&baseoff), offoff); + // A read-only relocated pointer is the weak binding plus its addend, + // not the synthetic display address stored in its pre-link bytes. + let nullable = data.get_arch().nullable_relocations.get(&offoff) + .filter(|_| data.get_address().get_space().is_some_and(|s| s.get_index() == baseoff.get_index())) + .and_then(|records| records.iter().find(|r| r.pointer_size == size + && matches!(r.operand, crate::kuna_nullable_reloc::Operand::DataPointer))).cloned(); + let offset = nullable.as_ref().map_or(offoff, |r| r.slot); + let newvn = data.new_varnode_space_off(size, Rc::clone(&baseoff), offset); data.op_set_input(op, newvn, 0).expect("RuleLoadVarnode: opSetInput"); - data.op_remove_input(op, 1); - set_opcode(data, op, OpCode::CPUI_COPY); + if let Some(record) = nullable.filter(|r| r.addend != 0) { + let addend = data.new_constant(size, record.addend as u64); + data.op_set_input(op, addend, 1).expect("weak pointer addend"); + set_opcode(data, op, OpCode::CPUI_INT_ADD); + } else { + data.op_remove_input(op, 1); + set_opcode(data, op, OpCode::CPUI_COPY); + } crate::p3_dataflow::kuna_stackreadscope::mark_stack_read(data, op); // C++ ruleaction.cc:4316-4324 — the spacebase-placeholder tail. When the // converted COPY output is the stack-pointer placeholder the call set up, diff --git a/decompiler/crates/kuna-decomp/src/p9_emit/coreaction_render.rs b/decompiler/crates/kuna-decomp/src/p9_emit/coreaction_render.rs index 43c0113b5e..c1e33c359e 100644 --- a/decompiler/crates/kuna-decomp/src/p9_emit/coreaction_render.rs +++ b/decompiler/crates/kuna-decomp/src/p9_emit/coreaction_render.rs @@ -792,7 +792,7 @@ impl Action for ActionConstantPtr { if offset == 0 { continue; // Never make constant 0 into spacebase } - if is_ptr_check { + if is_ptr_check || data.vbank().get(vn).unwrap().is_synthetic_literal() { continue; // Have we checked this variable before } if has_no_descend { @@ -1312,6 +1312,7 @@ impl Action for ActionDeindirect { } } + if data.vbank().get(vn).is_some_and(|v| v.is_synthetic_literal()) { continue; } let v = match data.vbank().get(vn) { Some(v) => v, None => continue, diff --git a/decompiler/crates/kuna-decomp/src/p9_emit/printc.rs b/decompiler/crates/kuna-decomp/src/p9_emit/printc.rs index 72ac9e892d..62c7cc0a2f 100644 --- a/decompiler/crates/kuna-decomp/src/p9_emit/printc.rs +++ b/decompiler/crates/kuna-decomp/src/p9_emit/printc.rs @@ -10588,7 +10588,7 @@ impl PrintC { if matches!(ct.get_metatype(), TYPE_PTR | TYPE_PTRREL) { // (kuna `elemptr`) A pointer the rule gave `char *` says the bytes // are indexed, never that they end at a zero byte. - if off != 0 && !crate::kuna_elemptr::reaches_element_pointer(fd, vn) { + if !v.is_synthetic_literal() && off != 0 && !crate::kuna_elemptr::reaches_element_pointer(fd, vn) { if let Some(sub) = ct.get_ptr_to() { // (kuna `cfstrings`) An entry reads as its string whatever its type. let sub = if sub.is_char_print() { Some(sub) } else { cfstring_char(arch, off) }; @@ -10645,7 +10645,7 @@ impl PrintC { )); return; } - if self.out_lang == crate::kuna_lang::OutLang::C + if !v.is_synthetic_literal() && self.out_lang == crate::kuna_lang::OutLang::C && self.options.global_ref && ct.get_metatype() == crate::dtype::type_metatype::TYPE_PTR && ct.get_word_size() == Some(1) @@ -10668,7 +10668,7 @@ impl PrintC { return; } } - if let Some(addr) = self.globalref.decide(fd, arch, vn, op, off, &ct) { + if let Some(addr) = (!v.is_synthetic_literal()).then(|| self.globalref.decide(fd, arch, vn, op, off, &ct)).flatten() { self.push_global_ref_ir(arch, addr, op, vn); return; } diff --git a/decompiler/crates/kuna-decomp/src/substrate/context.rs b/decompiler/crates/kuna-decomp/src/substrate/context.rs index d3ee7e42e7..21b693ea5f 100644 --- a/decompiler/crates/kuna-decomp/src/substrate/context.rs +++ b/decompiler/crates/kuna-decomp/src/substrate/context.rs @@ -1464,6 +1464,8 @@ pub struct ArchContext { pub loader_transfer_sites: Rc>, /// MIPS GOT import entries identified by the loader, never ordinary definitions. pub loader_mips_imports: Rc>, + pub nullable_relocations: Rc>>, + pub nullable_literal_values: Rc>, /// The calling convention each declared callee was declared under, keyed the /// same way [`Self::callee_protos`] is. Snapshotted at `build_arch_handle` /// from the architecture's declared-convention map and read back by @@ -1754,6 +1756,8 @@ impl ArchContext { callee_protos: Rc::new(Vec::new()), loader_transfer_sites: Rc::default(), loader_mips_imports: Rc::default(), + nullable_relocations: Rc::default(), + nullable_literal_values: Rc::default(), // No tracked registers until build_arch_handle snapshots the context DB. tracked_sets: kuna_base::partmap::PartMap::default(), } diff --git a/decompiler/crates/kuna-decomp/src/substrate/funcdata_varnode.rs b/decompiler/crates/kuna-decomp/src/substrate/funcdata_varnode.rs index 4efd93dc83..26c348533d 100644 --- a/decompiler/crates/kuna-decomp/src/substrate/funcdata_varnode.rs +++ b/decompiler/crates/kuna-decomp/src/substrate/funcdata_varnode.rs @@ -285,6 +285,9 @@ impl Funcdata { let ct = Self::type_base_unknown(s); let caddr = self.get_arch().get_constant(constant_val); let vn = self.vbank_mut().create(s, caddr, ct); + if self.get_arch().nullable_literal_values.contains(&constant_val) { + self.vbank_mut().get_mut(vn).unwrap().set_synthetic_literal(true); + } self.assign_high(vn); vn } @@ -335,6 +338,9 @@ impl Funcdata { } // uint4 vflags=0; entry = localmap->queryProperties(...); ... -- STUB(W4) self.set_varnode_properties(vn); + if m.is_constant() && self.get_arch().nullable_literal_values.contains(&m.get_offset()) { + self.vbank_mut().get_mut(vn).unwrap().set_synthetic_literal(true); + } vn } @@ -651,6 +657,9 @@ impl Funcdata { let ct = Self::type_base_unknown(1); let vn = self.vbank_mut().create(1, m.clone(), ct); self.vbank_mut().get_mut(vn).expect("new_code_ref: stale vn").set_annotation(); + if self.get_arch().nullable_literal_values.contains(&m.get_offset()) { + self.vbank_mut().get_mut(vn).unwrap().set_synthetic_literal(true); + } self.assign_high(vn); vn } @@ -818,6 +827,7 @@ impl Funcdata { (v.get_size(), v.get_addr().clone(), Rc::clone(v.get_type()), v.get_flags()) }; let cloned = self.clone_varnode_fields(size, addr, ct, flags); + if self.vbank().get(vn).unwrap().is_synthetic_literal() { self.vbank_mut().get_mut(cloned).unwrap().set_synthetic_literal(true); } let transport = self.kuna_is_call_transport(vn); self.kuna_inherit_storage(cloned, vn); if transport { @@ -837,6 +847,7 @@ impl Funcdata { (v.get_size(), v.get_addr().clone(), Rc::clone(v.get_type()), v.get_flags()) }; let cloned = self.clone_varnode_fields(size, addr, ct, flags); + if src.vbank().get(vn).unwrap().is_synthetic_literal() { self.vbank_mut().get_mut(cloned).unwrap().set_synthetic_literal(true); } self.kuna_extend_storage_sources(cloned, src.kuna_storage_sources(vn).iter().cloned()); if src.kuna_is_call_transport(vn) { self.kuna_mark_call_transport(cloned); @@ -3607,6 +3618,32 @@ mod tests { assert_eq!(fd.num_varnodes(), 1); } + #[test] + fn synthetic_import_literals_survive_all_constructors_and_clones() { + let mut context = ArchContext::new(build_manager()); + context.nullable_literal_values = Rc::new([0x402000].into_iter().collect()); + let ram = context.manage().get_space_by_name("ram").unwrap().clone(); + let address = Address::new(ram.clone(), 0x1000); + let mut fd = Funcdata::new("literal", "literal", Rc::new(context), address, + 0x10000000, 0x40).unwrap(); + let constant_address = fd.get_arch().get_constant(0x402000); + let a = fd.new_constant(8, 0x402000); + let b = fd.new_varnode(8, &constant_address, None); + let c = fd.new_code_ref(&Address::new(ram, 0x402000)); + for original in [a, b, c] { + assert!(fd.vbank().get(original).unwrap().is_synthetic_literal()); + let cloned = fd.clone_varnode(original); + assert!(fd.vbank().get(cloned).unwrap().is_synthetic_literal()); + let mut other = build_fd(); + let cloned = other.clone_varnode_from(&fd, original); + assert!(other.vbank().get(cloned).unwrap().is_synthetic_literal()); + } + let ordinary = fd.new_constant(8, 0x403000); + assert!(!fd.vbank().get(ordinary).unwrap().is_synthetic_literal()); + fd.vbank_mut().get_mut(c).unwrap().set_synthetic_literal(false); + assert!(!fd.vbank().get(c).unwrap().is_synthetic_literal()); + } + #[test] fn new_unique_advances_uniq_offset_and_create_index() { let mut fd = build_fd(); diff --git a/decompiler/crates/kuna-decomp/src/substrate/op.rs b/decompiler/crates/kuna-decomp/src/substrate/op.rs index eef82a1b60..693970f4ec 100644 --- a/decompiler/crates/kuna-decomp/src/substrate/op.rs +++ b/decompiler/crates/kuna-decomp/src/substrate/op.rs @@ -1837,6 +1837,12 @@ impl PcodeOpBank { self.deadlist.insert_after(&mut self.arena, ListKind::Insert, prev, op); } + /// Insert a newly emitted raw operation before another raw operation. + pub fn insert_before_dead(&mut self, op: OpId, before: OpId) { + assert!(op != before && self.on_dead_list(op) && self.on_dead_list(before)); + self.deadlist.splice_range(&mut self.arena, ListKind::Insert, op, op, Some(before)); + } + /// Move the inclusive sequence `[firstop, lastop]` to right after `prev` in /// the dead list (C++ `moveSequenceDead`, `op.cc:1086`). /// diff --git a/decompiler/crates/kuna-decomp/src/substrate/varnode.rs b/decompiler/crates/kuna-decomp/src/substrate/varnode.rs index fe446a3d34..114a86252f 100644 --- a/decompiler/crates/kuna-decomp/src/substrate/varnode.rs +++ b/decompiler/crates/kuna-decomp/src/substrate/varnode.rs @@ -161,6 +161,8 @@ pub mod addl_flags { /// Varnode's HighVariable turns out to span foreign members /// (`p5_types/kuna_sitetypelock.rs`). pub const site_locked: uint2 = 0x4000; + /// A program literal that coincides with a synthetic extern display address. + pub const synthetic_literal: uint2 = 0x8000; } /// Number of inline `OpId` slots a `descend` list keeps before spilling to the @@ -976,6 +978,13 @@ impl Varnode { pub fn is_ptr_check(&self) -> bool { (self.addlflags & addl_flags::ptrcheck) != 0 } + pub(crate) fn is_synthetic_literal(&self) -> bool { + self.addlflags & addl_flags::synthetic_literal != 0 + } + pub(crate) fn set_synthetic_literal(&mut self, value: bool) { + if value { self.addlflags |= addl_flags::synthetic_literal; } + else { self.addlflags &= !addl_flags::synthetic_literal; } + } /// C++ `isPtrFlow`. pub fn is_ptr_flow(&self) -> bool { (self.addlflags & addl_flags::ptrflow) != 0 diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index 89c685cf61..f5f3481b4e 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4352, - 4352 + 4349, + 4349 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -306,11 +306,8 @@ "data:CALLEEREGINPUTS #2: off, the method reached through a stub gets nothing", "data:CALLEEREGINPUTS #30: both, a GCC __cdecl member whose this is stored at [esp+4] behind the hidden return pointer gets no receiver", "data:CALLEEREGINPUTS #3: default, the constructor gets this and nothing else", - "data:CALLEEREGINPUTS #3: on, the constructor gets this and nothing else", "data:CALLEEREGINPUTS #4: default, the method reached through a stub gets this", - "data:CALLEEREGINPUTS #4: on, the method reached through a stub gets this", "data:CALLEEREGINPUTS #5: default, the constructed field is read back", - "data:CALLEEREGINPUTS #5: on, the constructed field is read back", "data:CALLEEREGINPUTS #6: off, forward hands obj_set only the stack argument", "data:CALLEEREGINPUTS #7: default, forward takes this in ECX", "data:CALLEEREGINPUTS #8: default, forward passes its own this through and keeps the stack argument", @@ -1235,7 +1232,6 @@ "data:KUNA-CATALOG #13: validated A32 inventory frames are the default", "data:KUNA-CATALOG #14: full ARM frames are explicitly enabled", "data:KUNA-CATALOG #15: every registry row plus eight individual queries", - "data:KUNA-CATALOG #15: every registry row plus six individual queries", "data:KUNA-CATALOG #16: standard C byte-array rendering is explicitly opt-in", "data:KUNA-CATALOG #17: standard C byte-array rendering exposes its live state", "data:KUNA-CATALOG #1: compareform documented with its post-DIV-2 default and P3 address", @@ -1244,7 +1240,6 @@ "data:KUNA-CATALOG #4: v850indirectbranch is a V850-gated default ()", "data:KUNA-CATALOG #5: each settable carries symptom-keyed use_when guidance for the operator", "data:KUNA-CATALOG #6: angr-derived features carry source_decompiler provenance (loweredswitch, namestyle, stackguard, branchflip, foldcallret, dedupvardecls, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, switchguardbound, switchsharedcase, switchmultipred, unrolledguard, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, securitycheck, structsynth, protoorder, calleevote, rustprobestack)", - "data:KUNA-CATALOG #7: change_kind structure-recovery tags the switch/array/loader/loop/tail-call reconstructors (loweredswitch, switchselector, constselectjump, arraystride, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, iteboolean, paramcopyhoist, guardarm, loopcondhoist, endptrbound, structsynth, structmerge, structheadless, devectorize, gomorestack, gowritebarrier, ptrauthtrap, rustprobestack)", "data:KUNA-CATALOG #7: change_kind structure-recovery tags the switch/array/loader/loop/tail-call reconstructors (loweredswitch, switchselector, constselectjump, arraystride, noreturn_propagate, loopbreak_recovery, gotoreduce, crossjumprevert, taildup, dedupitetail, returndup, iteregion, iteexpr, tailcalljump, regionstructure, regionlooprefine, regionedgeorder, ifelseflatten, earlyreturn, switchreturn, condfold, iteboolean, paramcopyhoist, guardarm, loopcondhoist, endptrbound, structsynth, structmerge, structheadless, devectorize, gomorestack, gowritebarrier, ptrauthtrap, rustprobestack, chkstkdarwin)", "data:KUNA-CATALOG #8: the opt-in-tool change_kind counts explicitly opted transformations ()", "data:KUNA-CATALOG #9: every settable carries a machine-readable change_kind so an LLM can judge default-on eligibility", @@ -1877,7 +1872,6 @@ "data:PTRAUTHTRAP #14: a guard that traps when the bits agree keeps its trap", "data:PTRAUTHTRAP #15: a value moved into x30 in an earlier block keeps its trap", "data:PTRAUTHTRAP #16: x30 loaded from a non-frame address keeps its trap", - "data:PTRAUTHTRAP #1: off keeps the bit test on the incoming return address", "data:PTRAUTHTRAP #1: off keeps the bit test on the incoming return address (and x30inverted keeps it in both)", "data:PTRAUTHTRAP #2: off keeps the trap call", "data:PTRAUTHTRAP #3: the tail call survives in both passes", @@ -3424,7 +3418,6 @@ "data:libcnames #3: with the option on the first descriptor is fd", "data:libcnames #4: the second open is fd_2, also after every rename", "data:libcnames #5: read's result is n and it reads from fd", - "data:libcnames #6: the buffer keeps the number it has with the option off, in every pass", "data:libcnames #6: the stack buffer read and write take by address is v4 with the option off", "data:libcnames #7: strlen's results are len and len_2 and malloc's is p", "data:libcnames #8: a value used inside an expression names nothing", @@ -3570,6 +3563,10 @@ "data:noreturn_extern #1: neighbour call leaks past the no-return call (pass 1 off only)", "data:noreturn_extern #3: __stack_chk_fail is the bounding call (both passes)", "data:null syntax is not a recovered identifier", + "data:nullable-relocations #1: weak binding is tested", + "data:nullable-relocations #2: imported call keeps its argument", + "data:nullable-relocations #3: unrelated literal stays numeric", + "data:nullable-relocations #4: every tested operand is supported", "data:octeoneffects #10: only off forwards a neighboring stack value across zeroing", "data:octeoneffects #11: both zero operations retain neighboring stores in both guarded modes", "data:octeoneffects #12: zeroing through an incoming pointer keeps a spill in the frame's top cache block", @@ -4359,4 +4356,4 @@ ], "returncode": 0, "unit_footer": null -} +} \ No newline at end of file diff --git a/docs/spec/01-program-prep.md b/docs/spec/01-program-prep.md index 5ae102cdc1..6a6ac50dad 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -2650,9 +2650,34 @@ extra upstream Win32 no-return-name list remains on the PE/COFF arm only. An ELF image runs no such pass, but a MIPS relocatable object's loader makes import slots, the GOT entry of each undefined weak function: the engine stashes the loader's ranges under this pass's id -(`decompiler/crates/kuna-console/src/engine.rs (elf_import_slot_stash)`), so +(`decompiler/crates/kuna-console/src/engine.rs (object_import_slot_stash)`), so they commit with that pass's facts and the call in `if (f) f(x);` binds to `f`. Other images are unaffected. +Undefined weak MIPS and x86-64 code-address operands in relocatable ELF objects +also retain their nullable identity before SSA. The relocation walker +`decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs (references)` +records exact instruction fields and one canonical dynamic slot per symbol. +`decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs (materialize)` +replaces only a uniquely matched supported operand with a slot load and the +relocation's arithmetic. MIPS HI16/LO16/HIGHER/HIGHEST preserve rounding and +signed immediates; x86-64 absolute and PC-relative address forms preserve addends +and instruction-end bias. Mixed GOT and ordinary forms share the binding. +Full-pointer weak relocations in read-only data retain the same binding and +addend when `RuleLoadVarnode` materializes the read, so read-only folding cannot +turn a genuine weak pointer into an unrelated numeric literal. Writable data, +partial pointer reads, other architectures and relocation families outside these +forms are not covered. Unmatched or unsupported code operands diagnose. +The binding represents the linker's weak-symbol choice, not arbitrary concurrent +mutation of an import slot. + +Synthetic display addresses are not proof that an unrelated integer names the +import. A per-varnode literal marker protects coincidentally equal constants +through construction and cloning from pointer/name inference. Exact relocated +code operands explicitly clear it, and internal slot loads use their real slot +address. This keeps fixed numeric addresses and real imported calls distinct. +The nullable-relocations fixtures cover zero and bound imports, addends across +signed-low boundaries, mixed forms, defined functions and unrelated literals. + The `externref` mark also protects the slot until that resolution: P3 read-only folding cannot replace it with an on-disk thunk/name RVA, even if a hostile or single-section PE mapped the IAT executable and non-writable. diff --git a/tests/fixtures/nullable-relocations/README.md b/tests/fixtures/nullable-relocations/README.md new file mode 100644 index 0000000000..d47b6c1ba4 --- /dev/null +++ b/tests/fixtures/nullable-relocations/README.md @@ -0,0 +1,25 @@ +# Nullable relocations + +Original C and x86-64 assembly, with sources and compiler/tool identities in +`provenance.json`. No vendor implementation is included. ELF files are relocatable +objects. `weak.c` covers null guards, imported calls, argument preservation, and +defined-symbol controls. `address.c` returns weak addresses with zero/positive/ +negative addends, and an unrelated fixed literal equal to a synthetic extern +address. `rip.S` covers x86 RIP-relative address fields. `readonly.S` and `readonly-mips.S` check full-width read-only weak pointer fields +and their addends. `rip.S:unsupported_offset` is a diagnosed operand near miss. +Mixed N32/N64 objects +combine PIC and ordinary address spellings of the same symbol. + +Build with `build.py --cc ... --mips-cc ... --linker ... --artifacts ...`. +The CLI regression replays emitted C at O0/O2 with UBSan. Guard inputs are in +[-5000,4999], with zero and bound imports and exact call/argument counts. Address +bit patterns include signed-low carry boundaries and full 64-bit high bits. +The x86 small-model address object has negative R_X86_64_32 addends, so its +address-replay bindings keep every relocated field within its representable +range. The guard object separately exercises the unbound zero case. The large-model +guard declares STT_FUNC explicitly because its absolute relocations alone do +not distinguish functions from data; the ordinary forms retain STT_NOTYPE and +use actual code-relocation evidence. + +Original-machine link/execution qualification is separate from emitted-C replay; +unsupported relocation/operand forms must diagnose and are not semantic passes. diff --git a/tests/fixtures/nullable-relocations/address-64.elf b/tests/fixtures/nullable-relocations/address-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..7fcfbc63f9329f26ced39f879bd265c4ff139150 GIT binary patch literal 2752 zcmb`IUu)Dr5WpwtU8}aWSMfoxrJ;ya(WS+rA_ZfsR-u++75di8HM#4-{Uga0714VX z1o6?2;iDhLM?XS+@cEocX41WF@}eEso0;G2%x*T5y^l|}p4zqrnOJZRZ4G1*G{y&m z&lqzP=E5vg)@*>aF#vxZ#{qJ?vNrY$Yiv|*)2uUyyHw9#cc>g#ONoL3z*K4#{kk@8 z4R;1*o>A~jS^hj)3z>J?IGgz9#>N9@X>&!{2#9Ov*iL1;4^j$1U%gg8IJ-FM#MqyA`X+g$YyeQ6&Zc^=6pIwV)*JujcR2dW z;4br?!Kn*Q=Zj#dT zLr`r$sP!8PLw8p}9dz66po4{y3BlohJ@ncED7;ye7sp|<){ldTH~F8+!F;)I%sz9u zHl2embmk&g(UAMc6`1N0&$Ar#>AlK2)bAv82Vch_+`Qku)1(~H*QXwqdjFEY(o1`h zikN*Q>n|C5ko9Qa4>A8H$Ibko%;`CGk$mF2IOO^DIbs$C}xuVanYx%MsCuRBOcPRO!V=dn}KkE}063uS*zRB<6D0=>{ zoKQtg&oBRb`tyJ24u^kuo}ugt1JM$f{gY3&v2U4XJE>)TLrwP=-N7XK`t%=d_H8l= b@eelWe$qu>e-;A;`Q>9y`l2VJo#p=pgjuL| literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/address-n32.elf b/tests/fixtures/nullable-relocations/address-n32.elf new file mode 100644 index 0000000000000000000000000000000000000000..0828a269b759ffa3ca4ab2a50a11a38e913667dc GIT binary patch literal 1784 zcmb_cO=}cE5Utr=O*9*mhzCI>qliS2CNY8#Jd7q90~&%+@D^t?y_?1TVx~7}5Ztp~ zJ@^wmdh`c)@MriJJa}H;>zN)V!-6?jP~GoURo8S?ch84MPak^~Pjc~O8n|Gn?TkLj zRk>>;mSjQZ^H_lU6_M&MnZEuowVr?nX}oV2j6`bb%JLXgm>SkonWwO)a;)W7FfLB? zq{jc_-&|i`^OrWaU*7cZsa3U{=C(cYj-HG6e$ktm<)>_)E~aj&XEWn&Pp}0OY%hwo z2Ox8;-q4=RRE>QpOWvQFjDHuM2+n=9gCAQDa_kJi&y?2Bl(;(+YexL(l7Fhi&zATs zc%JiG>b#J})X%5;rJ4P5dU$l=Hf-|PwVxRW$*y1JHGmNlV zTxTo3$T^1toT|gGQLe+6MR@(KphkBg`{*Y@fiQTzRFSND*_=F}6jDWR&(p;(QGli8H^A!xPJeKI$NwvpT#DaDLkfze{-oB@UpF z>SqjYe|*#Qaeqhs@%RtsOuVlE^NwM5`{V3-NAq_m!9QTbuBe9|Bv`eJhzGIYLD^<^-*y*wlO=g6Z7=!} z_A7YvBlKA4#g9|*=B3UrJCjY*2n7f7X685Zo4hyklCQ7czMgR$QgP@xO+7`4wq~cc zpP4>grA7L^bmvdwKx0Sa^Y{nk`gBAa>+5UogUwbN+SIK-^&We*)%vqE^Xm@<(aLjd zEc0`LpN)5$G(&UQD+zAG$$-SuCO^nUm&};{`zhfv3V>1Y>=`;s@Yhva@E@mtDpS8{e(9JUb zZ35gogP&yL3eNMzjB|M+mg3rnMhSPt|5U=Y59B3#LmRYM+jydTYT;a-HM^F)PGAG_ zT1i4)I~op6$XFOzEHqBK~%mdZRcRnb_`;msK#mW;YIoMt1Oj%4Irws{Pe! zz8Gr?h}OO+yut>)6JG(*4Qa0S5BCG_S&;ZG(U?mzysVI0lt=8KX7NrSeqKKA zH)`hd1DQqn_a|iborKWgVmY|1-qR4hGX;m6z?L^e_&y8vvs;?{}gGlftqFh zPf6%$qUbXJGvihB8%x10;bDH@DqrVU^=?pVa?nFKEchQ`MrHmb!C9S>W8r@|g`Vy8 VHIA2O56ZFZfAaU37Ll?!@E3pzfnWdt literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/address-x86-64.elf b/tests/fixtures/nullable-relocations/address-x86-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..a3bc59b97660f79049b33c6bd0355aa051472b1b GIT binary patch literal 1696 zcmbtU&1w`u5Uxq0i5f{1142Xx!9Os=uBe9`WHD-jhzBv?LD|gi_GTA%XO@|skmM2{ zA+O-gNAMWP#m9+w^AhWut(u+m?m@JmyXyP8s=B+XXAWMzc{SyEq~g&t8X;wgwx&mR zEjMd)nda%&!p+~`fsfk@w_ea+V!uYmw7#~s>fhgNWRcDM>XYD6P+6%y&2n6QAWXg$ zPcu9gd7R_1{&th5h>pPNv97TV@%G=5x4Y;q&(F>s@mU40>{VCrR?{? z;RKE;)NF=+o{lXrF+HK8mzcjj!5R0sTJqPkz!yBu4pF|!Hh3Q-i}KIe2JhaJ!d5FW zX&N*s9@uUig*~O}J7F&~1&Z74)Yt+>W;?Xq9Ya}^m|kd2tKREclW;r)eSu(q?FsM_ zCYFN#l0&*PR3-j27hibpFM3?aW3iOiI&3(+FZiRwYaJ*^_xd($u(ruichthY0&8|H z1+CDA6g1M5f@a+Bn~1q6wkF`Wx02eh`C0IGlm&w%9+<@LQIKaaoq9V7`$kUif6C`@ zysh(2nwsEMhu69&z03x_lbRBuH>9{)KlBIt%*pz7(RYO}_Y_O}vhFJ6D9b|z$oa4F zc;Cob^p9`e;;D&e!t!YhBL5hMT09v>uVWe<>yZaLKgxgHLN91+Q z6V&a$P28`~h%2B%0fnk7Xjd>)I#-ZNZhB%_(v#oIQ2J2KJAS~;SmrR6Ig-j2WG*|+ zxJy^WqekAS(e;Pzr|sX6gW0C<9ZuJGMGk%t!T3Y%-(~m!jdczX!<_mJ=;tm%DwUA$Z8y|I($Xi~6(o;|;;Jn@76 zf@QOtw`}@vST?&n6GrI2Welxtf4_NMLM(G_S(N$l?EG$~d_T1~GA>UJ=0|T(F@~|% z>|peBmQDY-21a;m<$ ztxon(hHtc=@Fkq@=LCOY{yP|~-EJRQwRj$1_V{W#*r3Gw0~}vvuYf1+DWJJijwOK) z9Ru0`x8Hv!1YCWe9ng&*aT#sIkM`jw6S$X=_CL?T&z%Y2n8k5ymE`e!)8a7|q5oBD{72fsx)<`=aFlPwFKgc-^sL1nYd#}ihryTriZs?% zmbPlORv0bSw}WO?fkrcIg`1IVHKMhx&0t-@uDcr6);7bcPS7KE)hp zyD-Sy^SZ0~1v?o1J+$$Jy8DXv*8KyVE3sFuxtV^g7I!~wzDI-$c3_t}fM@!A2;4HHZT7D=h@AY;A1tBJlBR z>IYHOT&rwHp~-Gko8oU&!^(D@W!~n;_5$R!hR8)dj=TP%<~gSh-#3N)BJ$O}4`}jD zd|5?_l(aU=hiP`b&JLg6vGk-SjrMQ0HQ|AAXU7!EG eIvX9wo9Ezy?tN^C)^>f~qkZy6%)bBJFaLin-0#`| literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/mixed-n32.elf b/tests/fixtures/nullable-relocations/mixed-n32.elf new file mode 100644 index 0000000000000000000000000000000000000000..6b3aa5e1d73eac009416bf9e9b2e2382d0265994 GIT binary patch literal 2668 zcmc&#O=whC6h3cebVg%hek@F}#V3r!iabISsFWb1Nvka?Qd7u6$h^tCnGDVRbmpav zKVYakEwoDoK^I+g>C%N#7hQJYqKhsH$+ElZLU8x`efQn-@)8YpQRsp5?sx9}?sxA0 zyYb1nPo0b-j#zV~0F;Cx`Cr#=%MqC}?MG!qhDFMmj7SzWyOLf2Ya;L2I91&Edm7Br zqR4@YWJ}*kKD%LZ#2+xHAyzsKLDU~VEe=cY2DBW15Sj`h_^x z4><#a?{f&OD-yt5^Ns6{r!XOTWda!Yg2uWzgSOVmwj3pJG{>CG4h_%V%gx`<%dkggvmKylY)scUCo;C%Ru+K~8*_n`gZr0o z&fzMJjWc7ScSnQ(Tti`C4d5cc~uUT9M z{@G&A@3F-XfsxB-J_3e~sJ@HirnoFJsJMdHN%0!?Lh%EvmExxuv*HcRH^yH7N-OlM z;`PH`TrMd(|JlV$o?mU&m;FXxyk5{~w(A{&W#0>f)zHFVX`$ZpTY-2rKlH_`_Q784 zv|2o)9v)5455r!wx)KIP2EDtro_MWVP+e&-!-Yw`Jo4}CU{~ZAs5&41)r+aF1sfj& zkNc;*b35?3FMJ<#uWP{1;cCMcOrp%e%W=F5d+%HZ9)jc9F>U)$&cLZ>^E&+J{RLN} z2JIv~rMG((aRp92o=vtwA(%mK?x$7S->@m)d<=l(X{JUG;wfWwSlDaPi@HoD)5 zqL-WXnF_GxVl_FmZ{UgnGu1Cm}#k7s3?kymB oP6c2;oNqq$I9~oQ?3w^c?ioIx6IoNe5yUB8p6BA0l297nKbI5clmGw# literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/provenance.json b/tests/fixtures/nullable-relocations/provenance.json new file mode 100644 index 0000000000..7527fa1434 --- /dev/null +++ b/tests/fixtures/nullable-relocations/provenance.json @@ -0,0 +1,360 @@ +{ + "objects": [ + { + "file": "weak-n32.elf", + "abi": "n32", + "kind": "weak", + "source": "weak.c", + "sha256": "df4301a7ac4f5a5fbe0a742a5d32f280c93b0323f77f4d2ee336696a95820b72", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "weak-n32.elf", + "-mabi=n32", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "address-n32.elf", + "abi": "n32", + "kind": "address", + "source": "address.c", + "sha256": "18acdb6927c40abc33f67d89d70a80dd9eab667885ac9147ad5015267dbc926c", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "address.c", + "-o", + "address-n32.elf", + "-mabi=n32", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "dfe3ae46200121b4324b2342b5d6b1a38ebc258daab294e33bc5bb8e2e16a84c" + }, + { + "file": "readonly-n32.elf", + "abi": "n32", + "kind": "address", + "source": "readonly-mips.S", + "sha256": "4fd353a2526a70ab7de6d6a2ad28055e73a68b0135622cb2c055e098b58d7834", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-c", + "readonly-mips.S", + "-o", + "readonly-n32.elf", + "-mabi=n32", + "-march=octeon", + "-mabicalls", + "-fPIC", + "-DABI64=0" + ] + ], + "source_sha256": "90f4191c2613f0870dbfa6f6512799d31eb11159dbb699a311217ba77a9baec8" + }, + { + "file": "mixed-n32.elf", + "abi": "n32", + "kind": "mixed", + "source": "weak.c", + "sha256": "f3ed167ffffaf081faca95b69b1649bd3218882141a3dee5285ea5860293e36b", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "pic-n32.o", + "-mabi=n32", + "-march=octeon", + "-mabicalls", + "-fPIC", + "-G0", + "-Dguard=pic_guard", + "-Doffset_guard=pic_offset_guard", + "-Dpresent=pic_present", + "-Ddefined_guard=pic_defined_guard" + ], + [ + "ld", + "-r", + "-m", + "elf32btsmipn32", + "-o", + "mixed-n32.elf", + "weak-n32.elf", + "pic-n32.o" + ] + ], + "link_diagnostics": "ld: pic-n32.o: warning: linking abicalls files with non-abicalls files\n", + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "weak-64.elf", + "abi": "64", + "kind": "weak", + "source": "weak.c", + "sha256": "95747def5397668fe0854fae084dacfd98660198df445f7b8a27a1b1f8d59868", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "weak-64.elf", + "-mabi=64", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "address-64.elf", + "abi": "64", + "kind": "address", + "source": "address.c", + "sha256": "c57b9f8c6f504072b5a0c403e5ef4378ecf058b1d153af9c04e603c6a11dd14d", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "address.c", + "-o", + "address-64.elf", + "-mabi=64", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "dfe3ae46200121b4324b2342b5d6b1a38ebc258daab294e33bc5bb8e2e16a84c" + }, + { + "file": "readonly-64.elf", + "abi": "64", + "kind": "address", + "source": "readonly-mips.S", + "sha256": "c14490a05bb990daa6c5ff095642b854135268829059a5b24bc275344a26fc04", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-c", + "readonly-mips.S", + "-o", + "readonly-64.elf", + "-mabi=64", + "-march=octeon", + "-mabicalls", + "-fPIC", + "-DABI64=1" + ] + ], + "source_sha256": "90f4191c2613f0870dbfa6f6512799d31eb11159dbb699a311217ba77a9baec8" + }, + { + "file": "mixed-64.elf", + "abi": "64", + "kind": "mixed", + "source": "weak.c", + "sha256": "0b953d0b7c57d44d9f03b98bc876fb41df3e17a340b3a17d644687356fad6787", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "pic-64.o", + "-mabi=64", + "-march=octeon", + "-mabicalls", + "-fPIC", + "-G0", + "-Dguard=pic_guard", + "-Doffset_guard=pic_offset_guard", + "-Dpresent=pic_present", + "-Ddefined_guard=pic_defined_guard" + ], + [ + "ld", + "-r", + "-m", + "elf64btsmip", + "-o", + "mixed-64.elf", + "weak-64.elf", + "pic-64.o" + ] + ], + "link_diagnostics": "ld: pic-64.o: warning: linking abicalls files with non-abicalls files\n", + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "weak-x86-64.elf", + "abi": "x86-64", + "kind": "weak", + "source": "weak.c", + "sha256": "8bba3c940ed26a4b15a0ae4f93196e30f7348078d28eb435a152136b3e574ef7", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "weak-x86-64.elf", + "-fno-pic", + "-fno-pie" + ] + ], + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "address-x86-64.elf", + "abi": "x86-64", + "kind": "address", + "source": "address.c", + "sha256": "8c5d628ca6d7692f175ea424951c29f20a75b6d97b093592927b8439f63f3aab", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-c", + "address.c", + "-o", + "address-x86-64.elf", + "-fno-pic", + "-fno-pie" + ] + ], + "source_sha256": "dfe3ae46200121b4324b2342b5d6b1a38ebc258daab294e33bc5bb8e2e16a84c" + }, + { + "file": "weak-x86-64-large.elf", + "abi": "x86-64-large", + "kind": "weak", + "source": "weak.c", + "sha256": "26821cea0f6830eed10ac63a617d6eb51fad8ad9e38ec23acda4efc3c0f75a81", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-c", + "weak.c", + "-o", + "weak-x86-64-large.elf", + "-fno-pic", + "-fno-pie", + "-mcmodel=large", + "-DTYPE_HINT" + ] + ], + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, + { + "file": "address-x86-64-large.elf", + "abi": "x86-64-large", + "kind": "address", + "source": "address.c", + "sha256": "03c6d747e2ed91aace5014d13899d12ad22df6b437454dbb4f71b1aab85bf7ee", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-c", + "address.c", + "-o", + "address-x86-64-large.elf", + "-fno-pic", + "-fno-pie", + "-mcmodel=large" + ] + ], + "source_sha256": "dfe3ae46200121b4324b2342b5d6b1a38ebc258daab294e33bc5bb8e2e16a84c" + }, + { + "file": "rip-x86-64.elf", + "abi": "x86-64-rip", + "kind": "address", + "source": "rip.S", + "sha256": "9bed48b85388426fe720833c58c7b32bb7e2053cd5452bd1e05f1c6e73eed79b", + "commands": [ + [ + "cc", + "-c", + "rip.S", + "-o", + "rip-x86-64.elf" + ] + ], + "source_sha256": "8700c0fe9395dc3c95408f7fc6076289c119bb847960835e271d9fd09fabce7e" + }, + { + "file": "readonly-x86-64.elf", + "abi": "x86-64-rodata", + "kind": "address", + "source": "readonly.S", + "sha256": "f7a8fbde35c1d38bd5fdd52998584b431ae6150ebd2911d3fe5db45db328f035", + "commands": [ + [ + "cc", + "-c", + "readonly.S", + "-o", + "readonly-x86-64.elf" + ] + ], + "source_sha256": "a16aee10bc233a318d8095aa9b2a0e8043fcfea5bd82c7188c4dc899d99c0f40" + } + ], + "tools": { + "cc": { + "name": "cc", + "sha256": "b5f1b773a7c733738352000c92a077dc5852a1a2fc6d836b1e411be1e9ec5f88", + "version": "cc (Ubuntu 15.2.0-16ubuntu1) 15.2.0" + }, + "mips_cc": { + "name": "mips64-octeon-linux-gnu-gcc", + "sha256": "5d26f5ed013593e77d0237dac6f3179f68cdbb681f8c20d7948444b7ff3e0b49", + "version": "mips64-octeon-linux-gnu-gcc (GCC) 7.3.0" + }, + "linker": { + "name": "ld", + "sha256": "2865846ece2642439cfae404ef930bfc5494c4babd2347757878b8f0f8f761cf", + "version": "GNU ld (GNU Binutils) 2.47.20260726" + } + } +} diff --git a/tests/fixtures/nullable-relocations/readonly-64.elf b/tests/fixtures/nullable-relocations/readonly-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..b1b693a4882bafc1db72837aa03f740f44471035 GIT binary patch literal 2104 zcmb`HPm9w)6u@87?pC{hT*ZY2Jrvp1B5puf!NY=aL9MV#VJ{wpbxNk)LTpPiEqbze z^&)r@{2(6tK?HmB3wW;YP3EPOG`Xk`Chxu9%zN`@UXl+_PM^B21F1N032h6cGiX%5 zPd`Boe9e5=hONqUc5r+X;LbOI%Gdef&yN5&rNw zB@=_|*E7I4$ZI9MUchVDR?oGM>vktnKB`Pxo4f z^%YL_(cNChVGH@hcWsDv6URnD{(G9Q=08GSl>edOoS*xCihQ#me_Qj_{I5##zs}{S zbFyrsfvw62?QiiZPg0!b3oXdK`2AZYI;d5Y=nJ-}Ii8a56nKG90=!NfgZJWj`^-!F z#GdH%yP@czI|?H3bX+t{V(?q^gi=wzGgdO@I{rmBFqpsF{P*IU#8abL+4pq=A-z{5 zzJ8_;cb=F|1N8NQEE(rzs8KrjMvQ2 z<$iRYM;L$C%;)2O8BXW4h3h5$5(nRly`~&5@0-@sJI?XB{TV~q{uFYx%iRE0N-o(NGXM6w4$n$+q!odA(jJDCN T?ESAIEQznSOaQhxBWe5}WJ`qN literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/readonly-mips.S b/tests/fixtures/nullable-relocations/readonly-mips.S new file mode 100644 index 0000000000..0746322618 --- /dev/null +++ b/tests/fixtures/nullable-relocations/readonly-mips.S @@ -0,0 +1,46 @@ +.abicalls +.set noreorder +.text +.macro POINTER name, byte +.globl \name +.type \name,@function +\name: +.cpsetup $25,$24,\name +.if ABI64 + ld $2,%got_page(saved)($28) + ld $2,%got_ofst(saved+\byte)($2) +.else + lw $2,%got_page(saved)($28) + lw $2,%got_ofst(saved+\byte)($2) +.endif +.cpreturn + jr $31 + nop +.size \name,.-\name +.endm +POINTER address_value,0 +.if ABI64 +POINTER address_offset,8 +POINTER address_negative,16 +.else +POINTER address_offset,4 +POINTER address_negative,8 +.endif +.globl unrelated_literal +.type unrelated_literal,@function +unrelated_literal: + lui $2,0x40 + ori $2,$2,0x2000 + jr $31 + nop +.size unrelated_literal,.-unrelated_literal +.weak optional +.type optional,@function +.section .rodata +.balign 8 +saved: +.if ABI64 + .dword optional,optional+0x8000,optional-0x8000 +.else + .word optional,optional+0x8000,optional-0x8000 +.endif diff --git a/tests/fixtures/nullable-relocations/readonly-n32.elf b/tests/fixtures/nullable-relocations/readonly-n32.elf new file mode 100644 index 0000000000000000000000000000000000000000..19882d637c36a37a646a127cdb2d8c283f370501 GIT binary patch literal 1584 zcmb_c&ubGw6n?u&t+q8rQ4sVHrL{%MLZO0453%5nLX|)-f-FC>J0Z2VcUvUBT*nupx?j%; zZ-*h5L9|fiyfdAs9)e!#XG449#X9T%2rQoRKzvG%fNSwJ+)D?npN*w9tHJs?Ck8>h zcqU##EVmTIJ9QgK}9vOFBAbf{gF_j;u^ zF3faanPjACm=!uzM#qO~VsxQW$_FONGY|khSdtpKAJ;hvFqiY94^G295e;+cK@;+; zh#8m{_?(OlqGj<1cYB$O|E8w_cN*s6j~ng$?^pg@<37=f_=CH>%*DUsX~3O^x%j{E z{9jf6d_JIot7(DfF&}e)wMICbfk)-LBcr;QO);Ne3**K|M!z)_#mXp(x@AdGq5DbJ z%PD%c*Ext(H|eFSkNhAmcoAEQd@jdTkCZV*(j6LI@?-xQEhzbJ%lX>iV+;Y_m&FbI zav$3^q7Xd^THwnVyr0)rFz!?_uDQjenq?BdO(eK)f75LZ=Cw(0C)gwR3ehdmCWfGT=ky#2uP-%}VZU4M3u~k=FZ0>#C*lgW v1m90;@}CK9z?x$W?k)dMupj3xU`X#kZJ8Yqf%siU!qe5hB7P$x%vSdcOuv6* literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/readonly-x86-64.elf b/tests/fixtures/nullable-relocations/readonly-x86-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..38f6faac92dc44d91c06f5a444dba462718b4f46 GIT binary patch literal 1320 zcmbtTJx{|x40S2rW#D6}goG*vCK`zaF;!WB4ya;brCiCCR)hrQu2q>}MB)!IBJ~gO zgOK2yv#-%L3t*{d`_<3(y(G`47iSBV3b0k61+$D%fWg8n4)ZXC8mvS6Z3(lf@86*j z!Za`Z)nRAnJ?D5;n8#O4)&5*1z^1{r3pZJ%nmxifXWt|od1#EcOpJZ%ISQ~$9+Vi% z2+~JAOcN1zv|{eUCfJ?Ear6_s6e{`P>)3 z1OHCgdxA%a`L$)cBaQhV)ML$f)X1ODIF&hfLW3?HVgrBvmVtfkv9eBrb}Y@EKK}=9 Cm|DdE literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/readonly.S b/tests/fixtures/nullable-relocations/readonly.S new file mode 100644 index 0000000000..eeeab2dac7 --- /dev/null +++ b/tests/fixtures/nullable-relocations/readonly.S @@ -0,0 +1,27 @@ +.text +.weak optional +.type optional,@function +.globl address_value +.type address_value,@function +address_value: mov saved(%rip),%rax + ret +.size address_value,.-address_value +.globl address_offset +.type address_offset,@function +address_offset: mov saved+8(%rip),%rax + ret +.size address_offset,.-address_offset +.globl address_negative +.type address_negative,@function +address_negative: mov saved+16(%rip),%rax + ret +.size address_negative,.-address_negative +.globl unrelated_literal +.type unrelated_literal,@function +unrelated_literal: mov $0x402000,%eax + ret +.size unrelated_literal,.-unrelated_literal +.section .rodata +.balign 8 +saved:.quad optional,optional+0x8000,optional-0x8000 +.section .note.GNU-stack,"",@progbits diff --git a/tests/fixtures/nullable-relocations/rip-x86-64.elf b/tests/fixtures/nullable-relocations/rip-x86-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..b655adc3114456f83911aef9396a5a10af46a8a8 GIT binary patch literal 1112 zcmbtSy-veG40g+33rm5e5)vv5us|d>CKLvKgb*7eDpzu)6(Naom#R!1nRpK#ft?rN zfsnvX>^40*Ah2}#?C<>9zPpT0E>EkK3W!zU2quxR0L|*e_jA{WI@I9&Wr3o17vG@e z6Z@R+=7O0Y>a{&%XBoM*(j+y`X)-Ud;4bqG&J+HSdD4+B762EBRDY9W$%im|A7MUzb;*>a6VXryvN7qSb z|NJU!#pOo@vHpWE^0$~L-PC(|=IPO@4tU0<1@^?F2|cTa7Q8@N1zyJ(@S@mi@9gS& z-&l3`z-9jpEuKs6TMGK@>b>!UHNz&k!nZ_Op?NBlvqa?#QKQado$+k3Ots?5l`3U#ex|AHaS$*3uo|}Tfh~L}Qz+9$ zVFh-}>*Rp5-?VUNDab{KMAt$o?OVxkXpT%1+9G8Sk^gm;7NXf%;s5A2H#Q!YmNvIu z-zwc#ch%}?o&rSY{Y!E_EI2c>NJm9IAPb2LbZY`?UeX-8Z7mB!A3>;FCy-7*qNB%* z&9!>tpyN|H)Gv*k9G(10QnFKGtr<^@$6zic&V>|wIt7eL_j?S9YgP`}w6dH!B& z><$|=7#h!ohB}QeP!X&*MWua zk{Z41`fiQ6KMeW4NJFpVD&ITsNi|&GB~=@ZNWFNry`#eR%gn8{n;o~s)4*lW7?SEX zyxO>>SaolWRK4Hr;$VaZ-u_;5=yp9)eB+ht`@?o^?0ciw*3LaCv8TKPcAqLbYE^S# zsJYNp7UcX?1PpohSkHemM_(n6ykkYi77g5fcaB(>L}vbFHjsGd@xQ}O#+gPC``jG? zXTm)(N%CPokJ$gVR@(l*G>(0mB>up6ct~AL8_35RIMiI2)Eo0H9x|^0IHlZAEmxVN z{_>8R`F9yl$?w1~iA?(@8_0Y$ZXEgB_fYJ?eZ$|XpL3w&eTm17`VF|xBW3>+s4u9> z1N)cXBXj>B`zI%VS(iZ3a?JAvJ6XTTkzqVYM*BJ68OQsDHxFdy$8V>7ZUZBRe`vw} U|6t~yPxqgY`hC-Jghl@U0Bj6>v;Y7A literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/weak-n32.elf b/tests/fixtures/nullable-relocations/weak-n32.elf new file mode 100644 index 0000000000000000000000000000000000000000..9baa349703983be2dbed12dd45960bac90951f69 GIT binary patch literal 1740 zcmb_dU279T6up~7TW!-1@j>vTD+H-hhH3>%=|gO*rJzNuf^Y4nyGcWv4>lXrA{czs zKj2UB(MNxRKf}M^gKt7TXEHY(7eOC9aQB{j@0r<|JG05#2ag^Gg+N9KWE|W}lq!P{ za$W9P#k|bQ%t#BeS`?}5i(K53V&$ET6~Ec>!w2gH)Yn9qUs#iX9=Zwj1CcBKe5brx zz);LCXI#!()2Yg1SHb7JI8XmieRF+%C7j>fett8&XO_(3@GZ6{!SPcG-pmCPWxmSx z>5^MS=sa}`9w%k4N&>L=1ar?UbGiEnrW@_PJ(;l*`;y0~KP5T&?iV2*)tE>8+js<} zoc>%cp2)>|CN_=nXLI$jTzozkmk}G63oi`Ur+b%hSeS()opJSR;ES+v7vBJ19>p({ z=+Nv-r<*l9?dV84^?EPKb{qXDjis9=y`-Ho-{1Ub-V@ztd{5 zDHsVwa zAOj8HxesCs=Iyt*jCc{o{qkGU{qBH!^99uS&{_>ia#y!Hd=?|HM(aB7_7Q9#j$sPE zmpFuxS7AW$01sUszkSwt9><-NlOOO7!6%Rzjo@{C^x8Y7zrd)QhH)Ow$C$q}dS;-7 ZuXqgqX+Qv-kG}<;uE_IvPl1Eg{Q{B?a}EFi literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/weak-x86-64-large.elf b/tests/fixtures/nullable-relocations/weak-x86-64-large.elf new file mode 100644 index 0000000000000000000000000000000000000000..262da42f73fb9fcc910c9855b93773f7d90ec671 GIT binary patch literal 1664 zcmbtUPiqrV5TETIliJEw@gP*%#e;vqJ`zz6J;*}U1Q8LT;6bUI_QL#M(!>nLj&q*WkES`|hs!({cAx?taNJc36;zf81J}@}af4 zx#8X3>SUqGyygRSPc_z?53|B=-eCk_4y!EVn#a|8u>~^#Ibm|#t7wxrA6|j8zwE3m z)#@jBLE>C5BIla024mM)6ZV+xdI@`NVeC#R>g62#Jevw2Fk8`S+T%t=vO?@iDPPWh znQ@Ofa_&{eea7YNHyM9v;qYDud#VdjY<8nC7(&$Vr`l}qWI@t{IMJyN4fJ$>H`F~w zsl)}YL-&-P6GZ!v*tl1qKyxYRrg^|$;-_Uw$g6F7zMqs7LW8LY>*pG%v3?>A>vF{O04mH#5oGhff|&l}aE~f(J0N1O?cb z9_c}$25=tc;g`4c`wQ94_qmzRC@s#HD+l;M%DGy2Mv|}^W0zPGw#ItJ!ZsX?%~OJUIRihBPB{>mp3rH9_(cns zJJQ)cvP%~bpm?7ha_=R^1IFd<*BO82;P6_9JE{%Iz-%XR*n=dBGHo`uhGE))L8>zy z8|dh0JJuaWsRZ?%Xcn3JE_96*7xx&aSmHlN$K*4P^P?G_&|TI9_t|bbc)<1x2bXt( z%J%vuY@syivFwVL>Rzaf-Zh{)p$S2?vJ6x^>G$c)Do%`6xLeDa3EMktZ^c73NRxq1 z%^s+tg6?icY1r3%gZ~pgrqk}6b;<;6%l?U*_SB|DG^oFU4U)~xmHyFu=p5OPD;!UI zLb&{n7=}-xIFe#*x{U_a{MYG$#XT o4w+`;=AUC6#TZrieED69lgo+;^uOfJo}Aw~zjqROtho7q0Wr^is{jB1 literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/weak.c b/tests/fixtures/nullable-relocations/weak.c new file mode 100644 index 0000000000..2fb5310e50 --- /dev/null +++ b/tests/fixtures/nullable-relocations/weak.c @@ -0,0 +1,8 @@ +extern int optional(int) __attribute__((weak)); +#ifdef TYPE_HINT +__asm__(".type optional,@function"); +#endif +int guard(int value){return optional?optional(value):value+7;} +int offset_guard(void){return (unsigned long)optional+0x8000UL==0x8000UL;} +int present(int value){return value+11;} +int defined_guard(int value){return present?present(value):value+7;} diff --git a/tests/stages/kuna-mipsgot-weak.xml b/tests/stages/kuna-mipsgot-weak.xml index d2d7db469f..0c553ad052 100644 --- a/tests/stages/kuna-mipsgot-weak.xml +++ b/tests/stages/kuna-mipsgot-weak.xml @@ -59,6 +59,6 @@ if \(dat_[0-9a-f]+\) optional\(x\); return optional\( -return dat_[0-9a-f]+ == optional; +return (dat_[0-9a-f]+ == optional|1|true); ambiguous diff --git a/tests/stages/kuna-nullable-relocations.xml b/tests/stages/kuna-nullable-relocations.xml new file mode 100644 index 0000000000..e26a21859c --- /dev/null +++ b/tests/stages/kuna-nullable-relocations.xml @@ -0,0 +1,72 @@ + + +03e0000800000000 + + + + +if [^\n]*dat_[0-9a-f]+ +optional\(value\) +return 0x402000; +Nullable weak.*could not be modeled + From 50ed7587ff5e8dd3b589e54c762874ea99fdab2f Mon Sep 17 00:00:00 2001 From: phix33 <122955334+phix33@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:57:47 +1100 Subject: [PATCH 6/6] [AUTOMATED] Fix weak import slot storage, entry address and absolute operands --- .../src/loader/kuna_commonalloc.rs | 20 ++ .../src/loader/kuna_mipsreloc.rs | 14 +- .../src/loader/kuna_mipsreloc_tests.rs | 29 +-- .../src/loader/kuna_nullable_reloc.rs | 111 +++++++---- .../kuna-analysis/src/loader/reloc_object.rs | 82 ++++---- .../kuna-analysis/tests/common_allocation.rs | 181 ++++++++++++++++++ .../kuna-cli/tests/nullable_relocations.rs | 167 +++++++++++++++- decompiler/crates/kuna-console/src/engine.rs | 4 +- .../kuna-console/tests/mips_weak_imports.rs | 9 +- .../src/p2_lift/kuna_nullable_reloc.rs | 27 ++- docs/baseline-stages.json | 8 +- docs/spec/01-program-prep.md | 35 +++- tests/fixtures/nullable-relocations/README.md | 12 +- tests/fixtures/nullable-relocations/build.py | 13 ++ .../nullable-relocations/common-64.elf | Bin 0 -> 2872 bytes .../nullable-relocations/common-n32.elf | Bin 0 -> 1812 bytes .../nullable-relocations/common-x86-64.elf | Bin 0 -> 1752 bytes tests/fixtures/nullable-relocations/common.c | 6 + .../nullable-relocations/provenance.json | 96 +++++++++- .../nullable-relocations/rip-x86-64.elf | Bin 1112 -> 1112 bytes tests/fixtures/nullable-relocations/rip.S | 8 +- .../nullable-relocations/weak-x86-64-pic.elf | Bin 0 -> 1760 bytes tests/stages/kuna-nullable-relocations.xml | 45 ++++- 23 files changed, 741 insertions(+), 126 deletions(-) create mode 100644 tests/fixtures/nullable-relocations/common-64.elf create mode 100644 tests/fixtures/nullable-relocations/common-n32.elf create mode 100644 tests/fixtures/nullable-relocations/common-x86-64.elf create mode 100644 tests/fixtures/nullable-relocations/common.c create mode 100644 tests/fixtures/nullable-relocations/weak-x86-64-pic.elf diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_commonalloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_commonalloc.rs index a4be9562b6..7956fa2a50 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_commonalloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_commonalloc.rs @@ -19,6 +19,14 @@ impl CommonAllocator { } } + /// Keep `size` bytes between the extern slots and COMMON storage for other + /// synthetic slots, before anything is allocated. Returns where they end. + pub(super) fn reserve(&mut self, size: u64) -> Option { + debug_assert!(self.first.is_none()); + self.cursor = self.cursor.and_then(|cursor| cursor.checked_add(size)); + self.cursor + } + pub(super) fn range(&self) -> Option<(u64, u64)> { Some((self.first?, self.cursor?)) } @@ -68,6 +76,18 @@ mod tests { assert_eq!(CommonAllocator::new(u64::MAX, 1, true).allocate(1, 1), None); } + #[test] + fn reserved_slots_end_where_common_storage_begins() { + let mut allocator = CommonAllocator::new(0x402000, 1, true); + assert_eq!(allocator.reserve(0), Some(0x402010)); + assert_eq!(allocator.reserve(24), Some(0x402028)); + assert_eq!(allocator.allocate(4, 4), Some(0x402028)); + assert_eq!(allocator.range(), Some((0x402028, 0x40202c))); + let mut full = CommonAllocator::new(u64::MAX - 15, 0, true); + assert_eq!(full.reserve(16), None); + assert_eq!(full.allocate(1, 1), None); + } + #[test] fn zero_size_symbols_still_have_distinct_addresses() { let mut allocator = CommonAllocator::new(0x402000, 0, false); diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs index 54679ea6ca..c42c11d89d 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs @@ -40,7 +40,9 @@ //! defines the symbol, so it is an import slot that holds 0 and is not folded, //! and `if (f) f(x);` keeps its test. A `GOT_PAGE` against such a symbol //! reaches the same slot and its `GOT_OFST` is the addend alone, as GNU ld -//! decays the pair for a symbol that does not bind locally. +//! decays the pair for a symbol that does not bind locally. The symbol is still +//! resolved, so it takes the synthetic extern address every undefined symbol +//! has and the function stays entered there. //! //! `R_MIPS_JALR` only says which function a `jalr` calls and is not applied. //! microMIPS, MIPS16, R6 PC-relative and TLS relocations are unsupported. @@ -396,7 +398,10 @@ impl Gp { }; let first_import = import_of(&expression[0], first); let first_target = match first_import { - Some(_) => Target { address: 0, local: false, gp_disp: false }, + Some(import) => { + resolve(import); + Target { address: 0, local: false, gp_disp: false } + } None => resolve(first.target()).ok_or(RelocationFailure::UnresolvedTarget)?, }; let mut value = if first.has_implicit_addend() { @@ -410,7 +415,10 @@ impl Gp { let reloc = &records[op.record].1; let import = if index == 0 { first_import } else { import_of(op, reloc) }; let (mut target, named) = match (index, op.special) { - (_, None) if import.is_some() => (Target { address: 0, local: false, gp_disp: false }, import), + (_, None) if import.is_some() => { + import.and_then(&mut *resolve); + (Target { address: 0, local: false, gp_disp: false }, import) + } (0, _) => (first_target, Some(first.target())), (_, None) => (resolve(reloc.target()).ok_or(RelocationFailure::UnresolvedTarget)?, Some(reloc.target())), (_, Some(ssym)) => (self.special(ssym, place, input)?, None), diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs index ba1c33d181..431f9bb785 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc_tests.rs @@ -725,10 +725,11 @@ fn only_a_global_gp_is_the_linker_gp() { } /// A GCC PIC object's weak function keeps one import slot, inside the GOT, for -/// both the test of its address and the call through it, named after the -/// function and holding 0. +/// both the test of its address and the call through it, holding 0. The +/// function is named once, at its synthetic extern address, and the slot is +/// another address of it. #[test] -fn a_weak_functions_got_entry_is_a_named_import_slot() { +fn a_weak_functions_got_entry_is_an_import_slot_of_its_extern() { for stem in ["pic-gcc47-n32", "pic-gcc47-64", "pic-gcc73-n32", "pic-gcc73-64"] { let bytes = fixture(&format!("{stem}.o")); let file = object::File::parse(&*bytes).unwrap(); @@ -736,9 +737,13 @@ fn a_weak_functions_got_entry_is_a_named_import_slot() { assert!(layout.diagnostics.is_empty(), "{stem}: {:?}", layout.diagnostics.report_lines()); let [(lo, hi)] = layout.import_slots[..] else { panic!("{stem}: {:?}", layout.import_slots) }; assert_eq!(hi - lo, if file.is_64() { 8 } else { 4 }); - let named: Vec<_> = layout.funcsyms.iter().filter(|(a, _)| *a == lo).map(|(_, n)| n.as_slice()).collect(); - assert_eq!(named, [b"optional".as_slice()], "{stem}"); - assert!(!layout.funcsyms.iter().any(|(a, n)| n == b"optional" && *a != lo), "{stem}: one name"); + let symbol = file.symbols().find(|s| s.name().ok() == Some("optional")).unwrap(); + let entry = layout.extern_addr[&symbol.index()]; + let (extern_lo, extern_hi) = layout.extern_range.unwrap(); + assert!(extern_lo <= entry && entry < extern_hi, "{stem}"); + let named: Vec<_> = layout.funcsyms.iter().filter(|(_, n)| n == b"optional").map(|(a, _)| *a).collect(); + assert_eq!(named, [entry], "{stem}: one name, at the extern address"); + assert_eq!(layout.function_address_aliases, [(lo, entry)], "{stem}"); let (base, data) = layout.segments.iter().find(|(b, d)| lo >= *b && hi <= b + d.len() as u64).unwrap(); assert!(data[(lo - base) as usize..(hi - base) as usize].iter().all(|&b| b == 0), "{stem}"); let (got_lo, got_hi) = layout.got_range.unwrap(); @@ -804,17 +809,17 @@ fn mixed_weak_references_share_one_function_without_changing_relocation_values() assert!(layout.diagnostics.is_empty()); let names: Vec<_> = layout.funcsyms.iter().filter(|(_, name)| name == b"optional").collect(); assert_eq!(names.len(), 1); - let [(alias, canonical)] = layout.function_address_aliases[..] else { panic!("one direct-address alias") }; - assert_ne!(alias, canonical); - assert_eq!(names[0].0, canonical); - assert_eq!(layout.import_slots, [(canonical, canonical + if abi == "64" { 8 } else { 4 })]); + let [(slot, entry)] = layout.function_address_aliases[..] else { panic!("one import-slot alias") }; + assert_ne!(slot, entry); + assert_eq!(names[0].0, entry); + assert_eq!(layout.import_slots, [(slot, slot + if abi == "64" { 8 } else { 4 })]); let file = object::File::parse(&*bytes).unwrap(); let sym = file.symbols().find(|s| s.name().ok() == Some("optional")).unwrap(); - assert_eq!(layout.extern_addr[&sym.index()], alias); + assert_eq!(layout.extern_addr[&sym.index()], entry); let rodata = file.section_by_name(".rodata").unwrap(); let base = layout.section_vma[&rodata.index()]; let data = &layout.segments.iter().find(|(addr, _)| *addr == base).unwrap().1; - assert_eq!(read_at(data, 0, if abi == "64" { 8 } else { 4 }, endian == "le"), alias); + assert_eq!(read_at(data, 0, if abi == "64" { 8 } else { 4 }, endian == "le"), entry); } } } diff --git a/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs b/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs index 56ecce403a..1864a0f4e3 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs @@ -4,20 +4,64 @@ use kuna_decomp::kuna_nullable_reloc::{NullableRelocation, Operand}; use object::{Object, ObjectSection, ObjectSymbol, RelocationTarget, SectionIndex, SymbolIndex}; use std::collections::HashMap; +fn is_mips(arch: object::Architecture) -> bool { + matches!( + arch, + object::Architecture::Mips + | object::Architecture::Mips64 + | object::Architecture::Mips64_N32 + ) +} + +/// The most storage [`references`] can take: one pointer per undefined weak +/// symbol. The layout keeps this much clear of every other synthetic address. +pub(super) fn reserved_bytes(file: &object::File<'_>) -> u64 { + let arch = file.architecture(); + if !is_mips(arch) && arch != object::Architecture::X86_64 { + return 0; + } + let weak = file.symbols().filter(|s| s.is_undefined() && s.is_weak()).count() as u64; + weak.saturating_mul(if file.is_64() { 8 } else { 4 }) +} + +/// `symbol`'s slot, made on first use inside `cursor..limit`. +fn slot_for( + slots: &mut HashMap, + allocated: &mut Vec<(SymbolIndex, u64)>, + symbol: SymbolIndex, + cursor: &mut u64, + limit: u64, + pointer_size: u64, +) -> Option { + if let Some(&slot) = slots.get(&symbol) { + return Some(slot); + } + let slot = cursor.checked_add(pointer_size - 1)? & !(pointer_size - 1); + let end = slot.checked_add(pointer_size).filter(|&end| end <= limit)?; + *cursor = end; + slots.insert(symbol, slot); + allocated.push((symbol, slot)); + Some(slot) +} + +fn bytes_at(data: &[u8], offset: u64, len: usize) -> Option<&[u8]> { + let start = usize::try_from(offset).ok()?; + data.get(start..start.checked_add(len)?) +} + +/// Each weak relocation the engine keeps nullable, and each slot allocated for +/// one in `cursor..limit`. A field or slot that cannot be represented is skipped +/// or left unsupported, never wrapped. pub(super) fn references( file: &object::File<'_>, vmas: &HashMap, externs: &HashMap, slots: &mut HashMap, cursor: &mut u64, + limit: u64, ) -> (Vec, Vec<(SymbolIndex, u64)>) { let arch = file.architecture(); - let mips = matches!( - arch, - object::Architecture::Mips - | object::Architecture::Mips64 - | object::Architecture::Mips64_N32 - ); + let mips = is_mips(arch); if !mips && arch != object::Architecture::X86_64 { return (Vec::new(), Vec::new()); } @@ -56,6 +100,9 @@ pub(super) fn references( let Some(kind) = kuna_mipsreloc::r_type_of(reloc) else { continue; }; + let Some(field) = base.checked_add(*offset) else { + continue; + }; let mut addend = reloc.addend(); if !text { let pointer = if mips { @@ -67,9 +114,7 @@ pub(super) fn references( continue; } if reloc.has_implicit_addend() { - let Some(bytes) = - data.get(*offset as usize..*offset as usize + pointer_size as usize) - else { + let Some(bytes) = bytes_at(data, *offset, pointer_size as usize) else { continue; }; addend = if file.is_little_endian() { @@ -78,15 +123,13 @@ pub(super) fn references( bytes.iter().fold(0u64, |a, &b| (a << 8) | b as u64) } as i64; } - let slot = *slots.entry(idx).or_insert_with(|| { - *cursor = (*cursor + pointer_size as u64 - 1) & !(pointer_size as u64 - 1); - let slot = *cursor; - *cursor += pointer_size as u64; - allocated.push((idx, slot)); - slot - }); + let Some(slot) = + slot_for(slots, &mut allocated, idx, cursor, limit, pointer_size as u64) + else { + continue; + }; out.push(NullableRelocation { - field: base + offset, + field, slot, symbol: value, addend, @@ -100,7 +143,7 @@ pub(super) fn references( } let (operand, shift, bias, patched) = if mips { if matches!(kind, 4 | 10) { - let mut reference = unsupported(base + offset, value, pointer_size); + let mut reference = unsupported(field, value, pointer_size); reference.operand = Operand::CodeReference; out.push(reference); continue; @@ -110,8 +153,7 @@ pub(super) fn references( { continue; } - let word = data - .get(*offset as usize..*offset as usize + 4) + let word = bytes_at(data, *offset, 4) .map(|bytes| { let bytes: [u8; 4] = bytes.try_into().unwrap(); if file.is_little_endian() { @@ -127,7 +169,7 @@ pub(super) fn references( 28 if pointer_size == 8 => (32, 0x80008000), 29 if pointer_size == 8 => (48, 0x800080008000), _ => { - out.push(unsupported(base + offset, value, pointer_size)); + out.push(unsupported(field, value, pointer_size)); continue; } }; @@ -136,7 +178,7 @@ pub(super) fn references( 5 => ((word as u16 as i64) << 16) + lows[i].unwrap_or(0), 6 => word as u16 as i16 as i64, _ => { - out.push(unsupported(base + offset, value, pointer_size)); + out.push(unsupported(field, value, pointer_size)); continue; } }; @@ -155,36 +197,35 @@ pub(super) fn references( || (kind == 2 && offset .checked_sub(1) - .and_then(|p| data.get(p as usize)) - .is_some_and(|&b| matches!(b, 0xe8 | 0xe9))) + .and_then(|p| bytes_at(data, p, 1)) + .is_some_and(|b| matches!(b[0], 0xe8 | 0xe9))) { - let mut reference = unsupported(base + offset, value, pointer_size); + let mut reference = unsupported(field, value, pointer_size); reference.operand = Operand::CodeReference; out.push(reference); continue; } if reloc.has_implicit_addend() { - out.push(unsupported(base + offset, value, pointer_size)); + out.push(unsupported(field, value, pointer_size)); continue; } match kind { 1 | 10 | 11 => (Operand::Absolute, 0, 0, 0), 2 => (Operand::PcRelative, 0, 0, 0), _ => { - out.push(unsupported(base + offset, value, pointer_size)); + out.push(unsupported(field, value, pointer_size)); continue; } } }; - let slot = *slots.entry(idx).or_insert_with(|| { - *cursor = (*cursor + pointer_size as u64 - 1) & !(pointer_size as u64 - 1); - let slot = *cursor; - *cursor += pointer_size as u64; - allocated.push((idx, slot)); - slot - }); + let Some(slot) = + slot_for(slots, &mut allocated, idx, cursor, limit, pointer_size as u64) + else { + out.push(unsupported(field, value, pointer_size)); + continue; + }; out.push(NullableRelocation { - field: base + offset, + field, slot, symbol: value, addend, diff --git a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs index e0c391ad41..28b58ee953 100644 --- a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs +++ b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs @@ -259,11 +259,14 @@ pub struct RelocLayout { pub got_range: Option<(u64, u64)>, /// A MIPS object's synthetic `$gp`. pub gp: Option, - /// Half-open import slots in the GOT: an undefined weak symbol's entry, - /// which the run-time loader fills and which therefore does not fold. + /// Half-open import slots: an undefined weak symbol's GOT entry or the + /// word allocated for its other address forms, which the run-time loader + /// fills and which therefore does not fold. Disjoint from the extern slots + /// and from [`Self::common_range`]. pub import_slots: Vec<(u64, u64)>, pub nullable_relocations: Vec, - /// Additional addresses of the same logical import: `(alias, canonical)`. + /// Additional addresses of the same logical import, `(alias, canonical)`: + /// its import slot, beside the synthetic extern address it is entered at. pub function_address_aliases: Vec<(u64, u64)>, /// A PowerPC64 ELFv1 object's function descriptors: `(descriptor, entry, /// TOC)`, sorted by descriptor address. @@ -400,6 +403,9 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL file.symbols().filter(|symbol| symbol.section() == SymbolSection::Undefined).count(), file.is_64(), ); + let weak_limit = common_allocator + .reserve(super::kuna_nullable_reloc::reserved_bytes(file)) + .unwrap_or(0); // Externs reached through a PLT-relative relocation (`R_X86_64_PLT32`) or a // branch/call instruction field are *code targets* — even when the undefined // symbol is `STT_NOTYPE` (object `SymbolKind::Unknown`), which gcc emits for @@ -690,54 +696,52 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL } funcsizes.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1))); funcsizes.dedup_by_key(|s| s.0); - // A MIPS object's import slots: the GOT entry of each undefined weak symbol, - // which the run-time loader fills. One a call reaches is named after it, as - // an Import Address Table slot is, so `(*slot)(x)` calls the import while a - // test of the slot still reads it. + // An undefined weak symbol's import slot: its MIPS GOT entry, or the word + // allocated here for its other address forms, which the run-time loader + // fills. The function stays at its synthetic extern address, where direct + // references reach it, and the slot is another address of that function, so + // `(*slot)(x)` calls the import while a test of the slot still reads it. let mut import_slots = Vec::new(); - let mut import_functions = HashMap::new(); let mut function_address_aliases = Vec::new(); + let weak_storage_start = extern_cursor; + let mut nullable_slots: HashMap<_, _> = gp.as_ref() + .map(|g| g.imports().collect()).unwrap_or_default(); + let mut function_slots: Vec<(SymbolIndex, u64)> = Vec::new(); if let Some(g) = &gp { for (idx, slot) in g.imports() { import_slots.push((slot, slot + g.entry())); - let Ok(sym) = file.symbol_by_index(idx) else { continue }; - if !code_externs.contains(&idx) && sym.kind() != SymbolKind::Text { - continue; - } - if let Ok(n) = sym.name_bytes().map(<[u8]>::to_vec) { - if !n.is_empty() { - symbol_info.push(RelocSymbolInfo { - name: String::from_utf8_lossy(&n).into_owned(), - vma: slot, - section_index: None, - section_name: None, - section_offset: None, - binding: symbol_binding(&sym), - undefined: true, - }); - funcsyms.push((slot, n)); - import_functions.insert(idx, slot); - } - } + function_slots.push((idx, slot)); } } - let weak_storage_start = extern_cursor; - let mut nullable_slots: HashMap<_, _> = gp.as_ref() - .map(|g| g.imports().collect()).unwrap_or_default(); let (nullable_relocations, allocated) = super::kuna_nullable_reloc::references( - file, &vma_of, &extern_of, &mut nullable_slots, &mut extern_cursor); + file, &vma_of, &extern_of, &mut nullable_slots, &mut extern_cursor, weak_limit); for (idx, slot) in allocated { - let Ok(sym) = file.symbol_by_index(idx) else { continue }; import_slots.push((slot, slot + if file.is_64() { 8 } else { 4 })); - if !code_externs.contains(&idx) && sym.kind() != SymbolKind::Text { continue; } + function_slots.push((idx, slot)); + } + for (idx, slot) in function_slots { + let Ok(sym) = file.symbol_by_index(idx) else { continue }; + if !code_externs.contains(&idx) && sym.kind() != SymbolKind::Text { + continue; + } let Ok(name) = sym.name_bytes() else { continue }; + if name.is_empty() { + continue; + } + if let Some(&entry) = extern_of.get(&idx) { + function_address_aliases.push((slot, entry)); + continue; + } symbol_info.push(RelocSymbolInfo { - name: String::from_utf8_lossy(name).into_owned(), vma: slot, - section_index: None, section_name: None, section_offset: None, - binding: symbol_binding(&sym), undefined: true, + name: String::from_utf8_lossy(name).into_owned(), + vma: slot, + section_index: None, + section_name: None, + section_offset: None, + binding: symbol_binding(&sym), + undefined: true, }); funcsyms.push((slot, name.to_vec())); - import_functions.insert(idx, slot); } // Extern functions: name each external *code target* at its synthetic slot // so `call ext` renders by name instead of a bare address. A code target is @@ -747,10 +751,6 @@ pub fn layout_relocatable(file: &object::File, fmt: &dyn ObjectFormat) -> RelocL // `stdout`, referenced by PC32 to an `STT_OBJECT`) are addressed but // deliberately left unnamed here. for (sym_idx, addr) in &extern_order { - if let Some(&canonical) = import_functions.get(sym_idx) { - function_address_aliases.push((*addr, canonical)); - continue; - } let Ok(sym) = file.symbol_by_index(*sym_idx) else { continue; }; diff --git a/decompiler/crates/kuna-analysis/tests/common_allocation.rs b/decompiler/crates/kuna-analysis/tests/common_allocation.rs index 856cbbcbc5..55d9b0bdb4 100644 --- a/decompiler/crates/kuna-analysis/tests/common_allocation.rs +++ b/decompiler/crates/kuna-analysis/tests/common_allocation.rs @@ -160,3 +160,184 @@ fn common_sizes_alignment_and_repeated_references_hold_across_architectures() { ); } } + +/// One object with `weak` undefined weak functions, each named by a full +/// read-only pointer, `unused` more that nothing names, `common` COMMON +/// objects and one ordinary extern. +fn weak_and_common_object( + architecture: Architecture, + endian: Endianness, + width: usize, + weak: usize, + unused: usize, + common: usize, +) -> Vec { + let mut object = write::Object::new(BinaryFormat::Elf, architecture, endian); + let rodata = object.section_id(write::StandardSection::ReadOnlyData); + let data = object.section_id(write::StandardSection::Data); + object.append_section_data(rodata, &vec![0; width * weak.max(1)], width as u64); + object.append_section_data(data, &vec![0; width * (common + 1)], width as u64); + let pointer = RelocationFlags::Generic { + kind: RelocationKind::Absolute, + encoding: RelocationEncoding::Generic, + size: (width * 8) as u8, + }; + let add = |object: &mut write::Object, name: String, size, value, section, kind, weak| { + object.add_symbol(write::Symbol { + name: name.into_bytes(), + size, + value, + section, + kind, + scope: SymbolScope::Linkage, + weak, + flags: SymbolFlags::None, + }) + }; + let undefined = write::SymbolSection::Undefined; + let callee = add(&mut object, "callee".into(), 0, 0, undefined, SymbolKind::Text, false); + let mut relocations = vec![(data, 0, callee)]; + for index in 0..weak + unused { + let symbol = add(&mut object, format!("optional{index}"), 0, 0, undefined, SymbolKind::Text, true); + if index < weak { + relocations.push((rodata, index * width, symbol)); + } + } + for index in 0..common { + let (size, alignment) = if index % 2 == 0 { (4, 4) } else { (24, 16) }; + let symbol = add( + &mut object, + format!("shared{index}"), + size, + alignment, + write::SymbolSection::Common, + SymbolKind::Data, + false, + ); + relocations.push((data, (index + 1) * width, symbol)); + } + for (section, offset, symbol) in relocations { + object + .add_relocation( + section, + write::Relocation { offset: offset as u64, symbol, addend: 0, flags: pointer }, + ) + .unwrap(); + } + object.write().unwrap() +} + +/// Extern slots, weak import slots and COMMON storage never share an address, +/// and COMMON storage moves only by the slots an architecture can allocate. +#[test] +fn weak_import_slots_stay_clear_of_extern_slots_and_common_storage() { + for (architecture, endian, width, slotted) in [ + (Architecture::X86_64, Endianness::Little, 8, true), + (Architecture::Mips, Endianness::Big, 4, true), + (Architecture::Mips, Endianness::Little, 4, true), + (Architecture::Mips64, Endianness::Big, 8, true), + (Architecture::Mips64, Endianness::Little, 8, true), + (Architecture::Aarch64, Endianness::Little, 8, false), + (Architecture::PowerPc, Endianness::Big, 4, false), + (Architecture::I386, Endianness::Little, 4, false), + ] { + for (weak, unused, common) in [(1, 0, 2), (3, 2, 5), (0, 2, 3), (1000, 0, 1000)] { + let bytes = weak_and_common_object(architecture, endian, width, weak, unused, common); + let file = object::File::parse(&*bytes).unwrap(); + let fmt = kuna_analysis::loader::format::detect(&file).unwrap(); + let layout = layout_relocatable(&file, fmt.as_ref()); + let case = format!("{architecture:?} {endian:?} {weak}+{unused} weak, {common} common"); + assert!(layout.diagnostics.is_empty(), "{case}: {:?}", layout.diagnostics.report_lines()); + let mut externs = Vec::new(); + let mut commons = Vec::new(); + let (mut undefined, mut weak_undefined) = (0u64, 0u64); + for symbol in file.symbols() { + undefined += u64::from(symbol.is_undefined()); + weak_undefined += u64::from(symbol.is_undefined() && symbol.is_weak()); + let Some(&address) = layout.extern_addr.get(&symbol.index()) else { continue }; + if symbol.is_common() { + commons.push((address, address + symbol.size())); + } else { + externs.push((address, address + 16)); + } + } + assert_eq!(externs.len(), weak + 1, "{case}"); + assert_eq!(commons.len(), common, "{case}"); + assert_eq!(layout.import_slots.len(), if slotted { weak } else { 0 }, "{case}"); + assert!(layout.import_slots.iter().all(|&(lo, hi)| hi - lo == width as u64), "{case}"); + let base = externs.iter().map(|range| range.0).min().unwrap(); + let reserved = if slotted { weak_undefined * width as u64 } else { 0 }; + let first_common = commons.iter().map(|range| range.0).min().unwrap(); + assert_eq!(first_common, (base + 16 * undefined + reserved + 3) & !3, "{case}"); + assert_eq!(layout.common_range.unwrap().0, first_common, "{case}"); + assert!(layout.import_slots.iter().all(|&(_, hi)| hi <= first_common), "{case}"); + let mut ranges: Vec<_> = externs + .iter() + .chain(&commons) + .chain(&layout.import_slots) + .copied() + .collect(); + ranges.sort_unstable(); + assert!(ranges.windows(2).all(|pair| pair[0].1 <= pair[1].0), "{case}: {ranges:x?}"); + let slots: Vec<_> = layout.import_slots.iter().map(|&(lo, _)| lo).collect(); + for &(slot, entry) in &layout.function_address_aliases { + assert!(slots.contains(&slot), "{case}"); + assert!(externs.contains(&(entry, entry + 16)), "{case}"); + assert_eq!(layout.funcsyms.iter().filter(|(address, _)| *address == entry).count(), 1, "{case}"); + } + assert!(!layout.funcsyms.iter().any(|(address, _)| slots.contains(address)), "{case}"); + } + } +} + +/// A weak relocation whose offset cannot be an address in the section is +/// skipped; the relocation beside it still gets its slot. +#[test] +fn an_unrepresentable_weak_relocation_offset_is_skipped() { + for (architecture, endian, width) in [ + (Architecture::X86_64, Endianness::Little, 8usize), + (Architecture::Mips64, Endianness::Big, 8), + (Architecture::Mips, Endianness::Big, 4), + ] { + for section in [write::StandardSection::ReadOnlyData, write::StandardSection::Text] { + for offset in [u64::MAX - 0xff, u64::MAX, u64::MAX - width as u64 + 1] { + let mut object = write::Object::new(BinaryFormat::Elf, architecture, endian); + let rodata = object.section_id(write::StandardSection::ReadOnlyData); + let target = object.section_id(section); + object.append_section_data(rodata, &vec![0; width], width as u64); + if target != rodata { + object.append_section_data(target, &[0; 16], 4); + } + let symbol = object.add_symbol(write::Symbol { + name: b"optional".to_vec(), + size: 0, + value: 0, + section: write::SymbolSection::Undefined, + kind: SymbolKind::Text, + scope: SymbolScope::Linkage, + weak: true, + flags: SymbolFlags::None, + }); + let flags = RelocationFlags::Generic { + kind: RelocationKind::Absolute, + encoding: RelocationEncoding::Generic, + size: (width * 8) as u8, + }; + for (section, offset) in [(rodata, 0), (target, offset)] { + object + .add_relocation(section, write::Relocation { offset, symbol, addend: 0, flags }) + .unwrap(); + } + let bytes = object.write().unwrap(); + let file = object::File::parse(&*bytes).unwrap(); + let fmt = kuna_analysis::loader::format::detect(&file).unwrap(); + let layout = layout_relocatable(&file, fmt.as_ref()); + let case = format!("{architecture:?} {section:?} {offset:#x}"); + assert_eq!(layout.import_slots.len(), 1, "{case}"); + let rodata_vma = layout.section_info.iter().find(|s| s.name == ".rodata").unwrap().vma; + let fields: Vec<_> = layout.nullable_relocations.iter().map(|r| r.field).collect(); + assert_eq!(fields, [rodata_vma], "{case}"); + } + } + } +} diff --git a/decompiler/crates/kuna-cli/tests/nullable_relocations.rs b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs index 1add17d2f9..4aa943adb6 100644 --- a/decompiler/crates/kuna-cli/tests/nullable_relocations.rs +++ b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs @@ -32,6 +32,9 @@ fn weak_imports_remain_nullable_across_non_pic_and_mixed_forms() { let file = object["file"].as_str().unwrap(); let abi = object["abi"].as_str().unwrap(); let kind = object["kind"].as_str().unwrap(); + if matches!(kind, "common" | "diagnosed") { + continue; + } let wide = matches!(abi, "64" | "x86-64-large" | "x86-64-rip" | "x86-64-rodata"); let word = if wide { "uint64_t" } else { "uint32_t" }; let mut functions = Vec::new(); @@ -49,6 +52,12 @@ fn weak_imports_remain_nullable_across_non_pic_and_mixed_forms() { ] { functions.push((name.to_string(), format!("{output} {name}(void)"))); } + if abi == "x86-64-rip" { + functions.push(( + "register_offset".to_string(), + "unsigned long register_offset(unsigned long base)".to_string(), + )); + } } else { for prefix in if kind == "mixed" { vec!["", "pic_"] @@ -116,7 +125,12 @@ fn weak_imports_remain_nullable_across_non_pic_and_mixed_forms() { "0,1,0x7fff,0x8000,0x400000,0x7fffffffffffffff,0x8000000000000000,0xffffffff80007ff0,0xffffffffffffffff" }; let result = if abi == "n32" { "uint32_t" } else { "uint64_t" }; - format!("int main(void){{uint64_t values[]={{{values}}};for(unsigned i=0;i = inventory + .as_array() + .or_else(|| inventory["functions"].as_array()) + .unwrap() + .iter() + .filter(|row| row["name"] == "optional") + .collect(); + assert_eq!(listed.len(), 1, "{file}: {inventory}"); + + let (answer, error, rc) = common::run_kuna(&["xrefs", path, "--to", "optional", "--json"]); + assert_eq!(rc, 0, "{file}: {error}"); + let answer: serde_json::Value = serde_json::from_str(&answer).unwrap(); + let entry = answer["target"]["address"].as_u64().unwrap(); + assert_eq!(listed[0]["address"].as_u64(), Some(entry), "{file}"); + let mut callers: Vec<_> = answer["xrefs"] + .as_array() + .unwrap() + .iter() + .map(|row| { + assert!(matches!(row["kind"].as_str(), Some("call" | "jump")), "{file}: {row}"); + assert_eq!(row["to_address"].as_u64(), Some(entry), "{file}: {row}"); + row["from_function"]["name"].as_str().unwrap().to_string() + }) + .collect(); + callers.sort(); + assert_eq!(callers, ["call_only", "guard"], "{file}: {answer}"); + + let (graph, error, rc) = common::run_kuna(&["decompile-graph", path]); + assert_eq!(rc, 0, "{file}: {error}"); + let graph: serde_json::Value = serde_json::from_str(&graph).unwrap(); + let address = |name: &str| { + let rows: Vec<_> = graph["functions"] + .as_array() + .unwrap() + .iter() + .filter(|row| row["name"] == name) + .collect(); + assert_eq!(rows.len(), 1, "{file}: {name}"); + (rows[0]["address"].as_u64().unwrap(), rows[0]["kind"].as_str().unwrap().to_string()) + }; + let (optional, kind) = address("optional"); + assert_eq!((optional, kind.as_str()), (entry, "external"), "{file}"); + for caller in ["call_only", "guard"] { + let (caller_address, _) = address(caller); + assert!( + graph["edges"].as_array().unwrap().iter().any(|edge| { + edge["callerAddress"].as_u64() == Some(caller_address) + && edge["calleeAddress"].as_u64() == Some(optional) + }), + "{file}: no edge {caller} -> optional" + ); + } + + let word = if abi == "n32" { "uint32_t" } else { "uint64_t" }; + let candidate = if abi == "n32" { "unsigned int" } else { "unsigned long" }; + let functions = [ + ("guard", "int guard(int value)".to_string()), + ("call_only", "int call_only(int value)".to_string()), + ("same", format!("int same({candidate} candidate)")), + ]; + let codes: Vec<_> = functions + .iter() + .map(|(name, prototype)| decompile(&root, &file, name, prototype)) + .collect(); + assert!(codes[0].contains("counter += value;"), "{file}: {}", codes[0]); + assert!(codes[0].contains("limit + 7"), "{file}: {}", codes[0]); + let code = codes.join("\n"); + assert!(!code.contains("CONCAT"), "{file}: {code}"); + let slots: BTreeSet<_> = code + .split(|c: char| !c.is_ascii_alphanumeric() && c != '_') + .filter(|name| { + name.starts_with("dat_") && name[4..].chars().all(|c| c.is_ascii_hexdigit()) + }) + .collect(); + assert_eq!(slots.len(), 1, "{file}: one weak slot: {code}"); + let slot = slots.first().unwrap(); + if !compiler { + continue; + } + let source = common::scratch_file("weak-common", "c"); + let driver = source.with_extension("driver.c"); + let executable = source.with_extension("exe"); + let declarations: String = codes + .iter() + .map(|code| format!("{};\n", code.split_once('{').unwrap().0.trim())) + .collect(); + std::fs::write(&source, format!("#include \n#include \nextern int optional(int) __attribute__((weak));\nextern int counter, limit;\nextern {word} {slot};\n{declarations}{code}")).unwrap(); + std::fs::write(&driver, format!("#include \n{declarations}int counter, limit;\n{word} {slot};\nint calls, seen;\n#if BOUND\n__attribute__((section(\".weakbinding\"))) int optional(int x){{calls++;seen=x;return x+11;}}\n#else\nextern int optional(int) __attribute__((weak));\n#endif\nint main(void){{uintptr_t bits=(uintptr_t)optional;{slot}=({word})bits;int sum=0;for(int x=-5000;x<5000;x++){{limit=3*x;sum+=x;if(guard(x)!=(BOUND?x+11:3*x+7)||counter!=sum||limit!=3*x)return 1;if(same(({candidate})bits)!=1||same(({candidate})(bits+16))!=0)return 2;if(BOUND&&call_only(x)!=x+12)return 3;}}return calls!=(BOUND?20000:0)||(BOUND&&seen!=4999);}}\n")).unwrap(); + for bound in [0, 1] { + for opt in ["-O0", "-O2"] { + common::process::required_output( + Command::new("cc") + .args([ + "-std=c11", + "-no-pie", + opt, + "-Wall", + "-Wextra", + "-Werror", + "-Wno-parentheses", + "-Wno-sign-compare", + "-Wuninitialized", + "-fno-lto", + "-fsanitize=undefined", + "-fno-sanitize-recover=undefined", + &format!("-DBOUND={bound}"), + ]) + .arg(&source) + .arg(&driver) + .arg("-Wl,--section-start=.weakbinding=0x407ff0") + .arg("-o") + .arg(&executable), + ); + let output = common::process::output_with_timeout( + &mut Command::new(&executable), + std::time::Duration::from_secs(10), + std::time::Duration::from_millis(10), + ) + .unwrap(); + assert!( + output.status.success(), + "{file} bound={bound} {opt}: {code}\n{:?}", + output.status + ); + } + } + for path in [source, driver, executable] { + std::fs::remove_file(path).unwrap(); + } + } +} diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index d892184da7..fa5751c177 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -4775,8 +4775,8 @@ pub fn bootstrap_from_object_with_isa( } if !loader.reloc_sections().is_empty() { for &(lo, hi) in loader.import_slot_ranges() { literals.extend(lo..hi); } - for &(alias, slot) in loader.function_address_aliases() { - literals.extend((0..16).map(|byte| alias.wrapping_add(byte))); + for &(slot, entry) in loader.function_address_aliases() { + literals.extend((0..16).map(|byte| entry.wrapping_add(byte))); literals.extend((0..8).map(|byte| slot.wrapping_add(byte))); } } diff --git a/decompiler/crates/kuna-console/tests/mips_weak_imports.rs b/decompiler/crates/kuna-console/tests/mips_weak_imports.rs index affd103148..f79c02850b 100644 --- a/decompiler/crates/kuna-console/tests/mips_weak_imports.rs +++ b/decompiler/crates/kuna-console/tests/mips_weak_imports.rs @@ -1,4 +1,5 @@ -//! One weak import keeps one logical symbol across GOT and direct relocations. +//! One weak import keeps one logical symbol, entered at its synthetic extern +//! address, across its import slot and direct relocations. use kuna_analysis::loadimage_object::ObjectLoadImage; use kuna_base::address::Address; use kuna_console::{ @@ -26,7 +27,7 @@ fn mixed_and_got_only_imports_keep_one_identity_and_share_edited_prototypes() { ObjectLoadImage::from_bytes_with_target(path.to_str().unwrap(), &bytes, target) .unwrap(); let aliases = image.function_address_aliases().to_vec(); - assert_eq!(aliases.len(), usize::from(form == "mixed")); + assert_eq!(aliases.len(), 1, "{path:?}: the import slot"); let mut prog = bootstrap_from_object( path.to_str().unwrap(), target, @@ -43,7 +44,8 @@ fn mixed_and_got_only_imports_keep_one_identity_and_share_edited_prototypes() { let entry = prog .resolve_entry(&EntrySelector::Name("optional".into())) .unwrap(); - assert!(prog.is_import_slot(entry.addr.get_offset())); + assert!(!prog.is_import_slot(entry.addr.get_offset())); + assert!(!prog.vma_bytes_mapped(entry.addr.get_offset())); assert!(!prog .function_entries_executable() .iter() @@ -51,6 +53,7 @@ fn mixed_and_got_only_imports_keep_one_identity_and_share_edited_prototypes() { let mut addresses = vec![entry.addr.clone()]; for &(alias, canonical) in &aliases { assert_eq!(canonical, entry.addr.get_offset()); + assert!(prog.is_import_slot(alias)); assert_eq!( prog.resolve_entry(&EntrySelector::Numeric(alias)) .unwrap() diff --git a/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs b/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs index e66176b8d6..58cbc5c938 100644 --- a/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs +++ b/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs @@ -100,6 +100,7 @@ pub(crate) fn materialize(data: &mut Funcdata, address: &Address, step: i32, ops .range(address.get_offset()..end) .flat_map(|(_, refs)| refs.iter().cloned()) .collect(); + let alone = records.len() == 1; for record in records { if matches!(record.operand, Operand::CodeReference) { let mut found = false; @@ -173,8 +174,14 @@ pub(crate) fn materialize(data: &mut Funcdata, address: &Address, step: i32, ops { Some(record.patched) } - Operand::Absolute if op.code() == OpCode::CPUI_COPY && slot == 0 => { - Some(record.symbol.wrapping_add(record.addend as u64)) + Operand::Absolute + if op.code() == OpCode::CPUI_COPY && slot == 0 + || alone && vn.get_size() >= 4 => + { + let value = record.symbol.wrapping_add(record.addend as u64); + (op.code() == OpCode::CPUI_COPY + || value != end && value != address.get_offset()) + .then_some(value) } Operand::PcRelative if slot == 0 && op.code() == OpCode::CPUI_COPY @@ -198,11 +205,19 @@ pub(crate) fn materialize(data: &mut Funcdata, address: &Address, step: i32, ops } } } - if matches.len() != 1 { + // An instruction's one immediate can reach several p-code operations + // (a compare's subtraction and flags). With no other weak field in the + // instruction they are that one operand, read once before all of them. + let shared = matches.len() > 1 + && alone + && matches!(record.operand, Operand::Absolute) + && matches.iter().all(|m| m.2 == matches[0].2); + if matches.len() != 1 && !shared { data.warning("Nullable weak relocation operand could not be modeled; address guards may be incomplete", address); continue; } let (before, slot, size) = matches[0]; + let before = if shared { ops[0] } else { before }; let space = address.get_space().unwrap().clone(); let space_id = data.new_varnode_space(&space); let location = data.new_constant(space.get_addr_size() as i32, record.slot); @@ -262,6 +277,12 @@ pub(crate) fn materialize(data: &mut Funcdata, address: &Address, step: i32, ops } else if size > record.pointer_size { value = emit(data, before, OpCode::CPUI_INT_ZEXT, size, &[value]); } + if shared { + for &(id, slot, _) in &matches { + data.op_set_input(id, value, slot).expect("relocation operand"); + } + continue; + } data.op_set_input(before, value, slot) .expect("relocation operand"); if matches!( diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index f5f3481b4e..9c0fa9bcbd 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4349, - 4349 + 4353, + 4353 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -3567,6 +3567,10 @@ "data:nullable-relocations #2: imported call keeps its argument", "data:nullable-relocations #3: unrelated literal stays numeric", "data:nullable-relocations #4: every tested operand is supported", + "data:nullable-relocations #5: a COMMON object written beside a weak slot keeps its name", + "data:nullable-relocations #6: a COMMON object read beside a weak slot keeps its name", + "data:nullable-relocations #7: no COMMON object is read as the slot", + "data:nullable-relocations #8: a compared weak address reads the slot", "data:octeoneffects #10: only off forwards a neighboring stack value across zeroing", "data:octeoneffects #11: both zero operations retain neighboring stores in both guarded modes", "data:octeoneffects #12: zeroing through an incoming pointer keeps a spill in the frame's top cache block", diff --git a/docs/spec/01-program-prep.md b/docs/spec/01-program-prep.md index 6a6ac50dad..00bf2c65a0 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -901,7 +901,8 @@ the section-flag translation, import resolution (§1.3), and extra constant rang slot is, so `if (f) f(x);` keeps its test. A `GOT_PAGE` against the symbol reaches that same entry and its `GOT_OFST` is the addend alone, the decay GNU ld applies to a symbol that does not bind locally. A direct call or address - relocation against the symbol still resolves to its synthetic extern. Otherwise + relocation against the symbol still resolves to its synthetic extern, the + address the function is entered at. Otherwise `lw $t9, %call16(f)($gp); jalr $t9` decompiles to a call of `f` wherever `$t9` is seeded with the entry. `R_MIPS_JALR`, a hint naming the function a `jalr` calls, is not applied; microMIPS, MIPS16, R6 PC-relative and TLS relocations @@ -2657,11 +2658,22 @@ Undefined weak MIPS and x86-64 code-address operands in relocatable ELF objects also retain their nullable identity before SSA. The relocation walker `decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs (references)` records exact instruction fields and one canonical dynamic slot per symbol. +The layout keeps one pointer per undefined weak symbol clear between the +synthetic extern slots and ELF COMMON storage, and a slot is allocated only +inside that reservation, so a slot never shares an address with a COMMON object +or an extern; a field address or slot that cannot be represented is skipped or +diagnosed, never wrapped. Other architectures reserve nothing and keep their +COMMON addresses. `decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs (materialize)` replaces only a uniquely matched supported operand with a slot load and the relocation's arithmetic. MIPS HI16/LO16/HIGHER/HIGHEST preserve rounding and signed immediates; x86-64 absolute and PC-relative address forms preserve addends -and instruction-end bias. Mixed GOT and ordinary forms share the binding. +and instruction-end bias. An x86-64 absolute field is one operand wherever its +value appears in its instruction, provided the instruction has no other weak +field and the value is not the instruction's own address or end: a register +displacement takes the slot read in place, and an immediate that reaches several +operations, as a compare's subtraction and flags do, takes one read made before +the instruction. Mixed GOT and ordinary forms share the binding. Full-pointer weak relocations in read-only data retain the same binding and addend when `RuleLoadVarnode` materializes the read, so read-only folding cannot turn a genuine weak pointer into an unrelated numeric literal. Writable data, @@ -10245,13 +10257,18 @@ instructions. A declared prototype supplies that missing contract ### Weak import identity and slot membership -An undefined weak MIPS import has one FunctionSymbol, anchored at its GOT slot. -A direct relocation keeps its original synthetic extern address and is recorded -as another address of that same symbol. The loader carries the SymbolIndex-based -association through bootstrap; the database maps both addresses to one symbol, -so name selection, renamed imports and edited prototypes do not split between -two independent functions. Distinct ELF symbols are not merged by their spelling. -The canonical inventory contains the import once; explicit alias-address lookup +An undefined weak import has one FunctionSymbol, entered at its synthetic extern +address: the address every undefined function has, which direct calls and +address relocations reach and which a GOT relocation against the symbol +allocates as it does for any other symbol. Its import slot, the GOT entry or the +word allocated for its other address forms, is recorded as another address of +that same symbol. The function inventory, a cross-reference query by name and +the call graph therefore find the import and its direct references where they +would without a slot. The loader carries the SymbolIndex-based association +through bootstrap; the database maps both addresses to one symbol, so name +selection, renamed imports and edited prototypes do not split between two +independent functions. Distinct ELF symbols are not merged by their spelling. +The canonical inventory contains the import once; a lookup at the slot address resolves the same entry. Import pointer storage remains bodyless unless a caller explicitly declares a function there. diff --git a/tests/fixtures/nullable-relocations/README.md b/tests/fixtures/nullable-relocations/README.md index d47b6c1ba4..a3d5f77109 100644 --- a/tests/fixtures/nullable-relocations/README.md +++ b/tests/fixtures/nullable-relocations/README.md @@ -5,10 +5,14 @@ Original C and x86-64 assembly, with sources and compiler/tool identities in objects. `weak.c` covers null guards, imported calls, argument preservation, and defined-symbol controls. `address.c` returns weak addresses with zero/positive/ negative addends, and an unrelated fixed literal equal to a synthetic extern -address. `rip.S` covers x86 RIP-relative address fields. `readonly.S` and `readonly-mips.S` check full-width read-only weak pointer fields -and their addends. `rip.S:unsupported_offset` is a diagnosed operand near miss. -Mixed N32/N64 objects -combine PIC and ordinary address spellings of the same symbol. +address. `rip.S` covers x86 RIP-relative address fields and, in +`register_offset`, an absolute displacement on a register. `readonly.S` and +`readonly-mips.S` check full-width read-only weak pointer fields and their +addends. Mixed N32/N64 objects combine PIC and ordinary address spellings of the +same symbol. `common.c` puts two COMMON objects and direct calls beside the weak +function, and compares an argument with its address through one immediate. +`weak-x86-64-pic.elf` is `weak.c` built `-fPIC`: its `GOTPCREL` operands are not +modeled and must diagnose. Build with `build.py --cc ... --mips-cc ... --linker ... --artifacts ...`. The CLI regression replays emitted C at O0/O2 with UBSan. Guard inputs are in diff --git a/tests/fixtures/nullable-relocations/build.py b/tests/fixtures/nullable-relocations/build.py index 75859de78b..1087cb2761 100644 --- a/tests/fixtures/nullable-relocations/build.py +++ b/tests/fixtures/nullable-relocations/build.py @@ -48,6 +48,19 @@ result = subprocess.run(link, check=True, capture_output=True, text=True) rows.append({'file': name, 'abi': abi, 'kind': 'mixed', 'source': 'weak.c', 'sha256': sha(r / name), 'commands': [command, link], 'link_diagnostics': result.stderr}) +for abi in ['n32', '64', 'x86-64']: + mips = abi != 'x86-64' + cc = a.mips_cc if mips else a.cc + flags = ['-mabi=' + abi, '-march=octeon', '-mno-abicalls', '-fno-pic', '-G0'] if mips else ['-fno-pic', '-fno-pie'] + name = 'common-' + abi + '.elf' + command = [cc, '-O2', '-fno-stack-protector', '-fcommon', '-c', 'common.c', '-o', name, *flags] + subprocess.run(command, cwd=r, check=True) + rows.append({'file': name, 'abi': abi, 'kind': 'common', 'source': 'common.c', + 'sha256': sha(r / name), 'commands': [[Path(cc).name, *command[1:]]]}) +command = [a.cc, '-O2', '-fno-stack-protector', '-fPIC', '-c', 'weak.c', '-o', 'weak-x86-64-pic.elf'] +subprocess.run(command, cwd=r, check=True) +rows.append({'file': 'weak-x86-64-pic.elf', 'abi': 'x86-64-pic', 'kind': 'diagnosed', 'source': 'weak.c', + 'sha256': sha(r / 'weak-x86-64-pic.elf'), 'commands': [[Path(a.cc).name, *command[1:]]]}) for source, name, abi in [('rip.S', 'rip-x86-64.elf', 'x86-64-rip'), ('readonly.S', 'readonly-x86-64.elf', 'x86-64-rodata')]: command = [a.cc, '-c', source, '-o', name] diff --git a/tests/fixtures/nullable-relocations/common-64.elf b/tests/fixtures/nullable-relocations/common-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..c3d06b465edc6f04d2786c766d11388232dd82c2 GIT binary patch literal 2872 zcmbVO&1(};5T8vythKFM>7k&~EfkGn7sXOV0>(C41udcoJqWsKwrL<=Bpa|&w8w%) zdhl*9iU$w<2Z}fGPw1`w2~`k!TYt0rCfU61UOMnHGr#$KZ)e};{r$%eQYnMlWY7TQ z2++1b=s(){09!V|vNTF&riUogM^sn_?+4y9o+g97tBBPi5KXFC`#hw}P*n9wc6Y`u+z|SP$^31x- z$j=jcjDQETrW=3{>KN<5hhy-;bI13~&%zuf|F80w{V^Z66sWPu1pmvC{LyE8BZ6zv-zpf)#y8oEM_2je+n4;>veA@_-BBh#nSU<6|VQ0 zQ@Ec0slxSsUIXWT_?)6thZ2YL^NQQ=mcsRZwiT}T^GV@)KY)X0fUB&FENFb6=y(>` zXbjHv*cYSLMSRXtT;}qnhHsMHT&s9a9^7k$d{v}|TXt;EedCc`a6E_Xe6vaRlSfaV z+u^v1=HyE&Wv2)=2&oquWLFAqzE!kgnw=q=r{1k1QQ+XNzFKMUT-&(GY{&B&rF_eC zn_XKtv0@=o?l=A1vnuPZ;==gMh0a5eImrqb@?Pa<{DRK&A$9mYW8!=J5|-e&lDg=y zOY11n`quzUzLUh?sH=MrN%T3A`sq&TK$3dg_g%!xcUg-!cz;a7L#@*< x-{O}_hmx@i&iA$a?Tgmu{{;HnhGEpRuY_5}g+=T0J(m#Qzp4U0N}%HH`2UG2oo4_5 literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/common-n32.elf b/tests/fixtures/nullable-relocations/common-n32.elf new file mode 100644 index 0000000000000000000000000000000000000000..c3c19c963112a85e8ed5bbef65380ea9f6dd3985 GIT binary patch literal 1812 zcma)6L2DCH5S~q9TWymj(nCR|TPU`QEUgtS#lvc=wNQ&#g&c&sNj7P4vl})Wutl&3 zPwEfwrr@;)e}KiSKSF4Kf(nAS^_#bE)@?32FgxGOd~Y%{`?4?ZY}_?+2Bl)q2yiA) z@b=%(G+k4SIjT@O9dU?j+|xN#?-rc#mfu%zXSl$#4oW(6f#M(8B* z2YlJH3gi6HKwiCXi6MVRFkARV7>O;NP5R?GxsO7H;-9dxFEZgkrlHyVEPURjsn0I} zSD|I)*Mm+c@U1$v`cBxOdeHYHH>98&wFBSr$ZL1n5!D^f+YWqhmwHae#oi7JBeTYT-6 zpuWPqj}48hWc7ACkyC?>!o=O%LvTD5^S)to%`^|u?`|A(3ET`6P zdQMBl!R|IfvN{d7)^BlYbMY!!yk*x%5mnFK*=~lsM+-OAa-t|~*ZPs$(@p-TF~Q^e zD!a3RyHF9nCoJcnyi*$r$xq<%3R;5ldCu`C=A!m-?Ua1dR*cl+{~~{){>58BGu)f( zp6lTbq{21WUj~oAUBdgS<1k8uw+WB?6*&fYDh{mNn{|!tB$s~&I|Jo;`Tr*KZUJZN z88-Z^NUnw;ho^@BG`cJLaLDVFHXp`Mq*IKF8lGd1rvRZ+KjXif|p43N*vZAH#nQ6;ZrD D5s`}# literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/common-x86-64.elf b/tests/fixtures/nullable-relocations/common-x86-64.elf new file mode 100644 index 0000000000000000000000000000000000000000..89e20d3ebcb484862e33c786cd549564b6555b44 GIT binary patch literal 1752 zcmbVML2DCH5T0$TiCUXf6e<-PJ!loXG@=CyN}y_2DN=-jJxJN4o3^;w4Vw)#UR0td zFFknh>c8+9s0V+7M~@Y}s5cSfd@nP0vqpptynNq$J2UUSncY0QvvhaJvPjLMTeR<~ zO7wPkpO*|R(Qz83AGxzX-@~_V;@5fAiATSH<))qkULV45yOY_i=B93wiFevxvwM)8 z_J?e{W9?R}c5dn=6!W~;ab9%cnm;zA(K~ZU%9)P`qwmwP$CFb4}wHFNs1U z9G>Te?G_)b&=64vnjZTkbC~t?Az9mbYjQM`-QkNO=ZtwmbXgFwv%*gSow5AAv8;)8 zL?0LY7odkD^vzr&9Wy+j!$&#jL_*iI;CzL20rpSx8mI4F+!3aDj`<+{BI~H%cQ4k8 zqAxST{@kk))@os3S7@W@M(Y&Te`p3 zl&*atyV0t}ZkaKTdbp`9@_g5hz3rInbvJg&E;kxvqk&$)S`fyb&F|z)BX%oK#NP;- zc0CH~UKF>;HWj?hwQAJsqMq{LSm0E=gXn3}O5(Ulk$FDlPqL3bmQwn%G;v3C%o+P+ z#h(zoAi92kBGWr1DMXE=U10|`51Nnn8Z~vUOpWRO_#Tp``zy}#L*>c1k4Y!N)3NvY zS%z2gokO_h7vLmJ`=?Tw32LVE_axwv1`4M0AG1DaexIeFBYBt~ay4J)H)wC9l;onH l;IQF4#EjDUM?`1nvkBRMu222D*Z3bw&mJk$BKrTy{|zNVoaF!j literal 0 HcmV?d00001 diff --git a/tests/fixtures/nullable-relocations/common.c b/tests/fixtures/nullable-relocations/common.c new file mode 100644 index 0000000000..10aee6ff6f --- /dev/null +++ b/tests/fixtures/nullable-relocations/common.c @@ -0,0 +1,6 @@ +extern int optional(int) __attribute__((weak)); +int counter; +int limit; +int guard(int value){counter+=value;return optional?optional(value):limit+7;} +int call_only(int value){return optional(value)+1;} +int same(int(*candidate)(int)){return candidate==optional;} diff --git a/tests/fixtures/nullable-relocations/provenance.json b/tests/fixtures/nullable-relocations/provenance.json index 7527fa1434..afc60264f8 100644 --- a/tests/fixtures/nullable-relocations/provenance.json +++ b/tests/fixtures/nullable-relocations/provenance.json @@ -305,12 +305,104 @@ ], "source_sha256": "dfe3ae46200121b4324b2342b5d6b1a38ebc258daab294e33bc5bb8e2e16a84c" }, + { + "file": "common-n32.elf", + "abi": "n32", + "kind": "common", + "source": "common.c", + "sha256": "85b8e2e5d8ec3bc5d2cfa1ce469935b24ef13516a597daf8b285e8bb6b8a9d12", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-fcommon", + "-c", + "common.c", + "-o", + "common-n32.elf", + "-mabi=n32", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "0182d41a2219e26628d3c564e3abe4c272b048c85d6b8bc770f5a8da74e4c2bf" + }, + { + "file": "common-64.elf", + "abi": "64", + "kind": "common", + "source": "common.c", + "sha256": "dcfb90c721bf907d47b099d357b661873d84cc4db65b15148e95173431302039", + "commands": [ + [ + "mips64-octeon-linux-gnu-gcc", + "-O2", + "-fno-stack-protector", + "-fcommon", + "-c", + "common.c", + "-o", + "common-64.elf", + "-mabi=64", + "-march=octeon", + "-mno-abicalls", + "-fno-pic", + "-G0" + ] + ], + "source_sha256": "0182d41a2219e26628d3c564e3abe4c272b048c85d6b8bc770f5a8da74e4c2bf" + }, + { + "file": "common-x86-64.elf", + "abi": "x86-64", + "kind": "common", + "source": "common.c", + "sha256": "624cf27f8ca8932b83a7c2221a890dc6a44114e08c19320cb287d4e9dca85ec1", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-fcommon", + "-c", + "common.c", + "-o", + "common-x86-64.elf", + "-fno-pic", + "-fno-pie" + ] + ], + "source_sha256": "0182d41a2219e26628d3c564e3abe4c272b048c85d6b8bc770f5a8da74e4c2bf" + }, + { + "file": "weak-x86-64-pic.elf", + "abi": "x86-64-pic", + "kind": "diagnosed", + "source": "weak.c", + "sha256": "ae34639381fa33c46d9d915170b6cbcd20211ec601282eed52ec6c383351630a", + "commands": [ + [ + "cc", + "-O2", + "-fno-stack-protector", + "-fPIC", + "-c", + "weak.c", + "-o", + "weak-x86-64-pic.elf" + ] + ], + "source_sha256": "5f373e2b102ae3a6dbcfb0815615926ad05e1dd0b9ffe33fae5d306c34824008" + }, { "file": "rip-x86-64.elf", "abi": "x86-64-rip", "kind": "address", "source": "rip.S", - "sha256": "9bed48b85388426fe720833c58c7b32bb7e2053cd5452bd1e05f1c6e73eed79b", + "sha256": "a875a0aab1fdd54a880900527a11cf2dfc19e8b53bc13824bd1cd8338a9cc6a0", "commands": [ [ "cc", @@ -320,7 +412,7 @@ "rip-x86-64.elf" ] ], - "source_sha256": "8700c0fe9395dc3c95408f7fc6076289c119bb847960835e271d9fd09fabce7e" + "source_sha256": "71a4f1ff355fd8c8cc66fdabc88c593113435abd305b36bb8cf361ced3aacb88" }, { "file": "readonly-x86-64.elf", diff --git a/tests/fixtures/nullable-relocations/rip-x86-64.elf b/tests/fixtures/nullable-relocations/rip-x86-64.elf index b655adc3114456f83911aef9396a5a10af46a8a8..faf12679feaac801f41e10f656e04745c5bbfc4b 100644 GIT binary patch delta 40 vcmcb?af4$+6r)g4YIw}{ delta 40 vcmcb?af4$+6r)gSUU6wbL4Hw5YD#>5T3T^x$>to!9wx@viG{+Gd02b^L#Pg& diff --git a/tests/fixtures/nullable-relocations/rip.S b/tests/fixtures/nullable-relocations/rip.S index d8498e9dcf..fc5f4fefea 100644 --- a/tests/fixtures/nullable-relocations/rip.S +++ b/tests/fixtures/nullable-relocations/rip.S @@ -28,9 +28,9 @@ unrelated_literal: .section .note.GNU-stack,"",@progbits .text -.globl unsupported_offset -.type unsupported_offset,@function -unsupported_offset: +.globl register_offset +.type register_offset,@function +register_offset: leaq optional(%rdi),%rax ret -.size unsupported_offset,.-unsupported_offset +.size register_offset,.-register_offset diff --git a/tests/fixtures/nullable-relocations/weak-x86-64-pic.elf b/tests/fixtures/nullable-relocations/weak-x86-64-pic.elf new file mode 100644 index 0000000000000000000000000000000000000000..aa087d666d728a29f88cfe5140e599896ed1df6a GIT binary patch literal 1760 zcmbu9&1(};5WpvGHMOmz77s$D*^^qZk3`f%QQ3av0|{2N0R_vtNj6Psk`0>|Y`u8U zYr%sTZ~iF({{fF4y?PXHD%6?HO!9baFFNp;ncsZ8ee+)OrcfzPrBWc0f(J11gaUk; zp4e_|x^NK|;Adv#SNZMznCQ9hq%+xVfRj@Cb0+J?@%&ISG9QLWq1jZK?7O&z>Q74Z zNGE>W1xj06>&}g8ZP3*NCx1uXQn|JK-9hZ6{Y}&#N=}?qfL@} z{RC1+i>c*>^z3^)Ak|!rBNI(5$83o;v2C_z4fc%f3#`vlG|L(IahepLPM9?9wPbMl zM%tUc#g-KWjt$Yjj*I$8OwMzgxr>@a&U6oL%6}YFfbqFr1^%I`gIB8TY;IJ%onrB6 zVb|N;*sK&h2z&Yxb;`cL%B$g}ors$Dra4LCb4Kekb5B z_&?=fIlaO2RvVZ7=N7l$*fPdx_O6xJFy$sX z)bn5K^H@tgbGS^A{pojNitI0X9fM+}TC7Fc^?Mjdy1y?8_eZr-FZY+UH|T?X+W-;1!{a +addends, independent fixed literals and exact call counts at O0/O2. + +The common-* objects (common.c) hold two COMMON objects beside the weak +function. The slot allocated for the weak address once started where COMMON +storage starts, so `counter += value` printed as a write of the slot and the +test read both objects; slot space is now reserved ahead of COMMON storage. +`same` compares its argument with the weak address through one immediate that +reaches the compare's subtraction and flags, which reads the slot once. --> -if [^\n]*dat_[0-9a-f]+ -optional\(value\) +if [^\n]*dat_[0-9a-f]+ +optional\(value\) return 0x402000; Nullable weak.*could not be modeled +counter \+= value; +return limit \+ 7; +CONCAT|dat_[0-9a-f]+ \+= +candidate == dat_[0-9a-f]+;