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 cd2d3cbee8..c42c11d89d 100644 --- a/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_mipsreloc.rs @@ -35,7 +35,14 @@ //! 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. 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. 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. @@ -44,7 +51,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 +244,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 +265,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 +297,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 +340,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 +393,17 @@ 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).is_some() || op.op == GOT_OFST) && (input.weak)(t)) + }; + let first_import = import_of(&expression[0], first); + let first_target = match first_import { + 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() { self.implicit(expression[0].op, old, first_target.local, lo_half)? as u64 } else { @@ -363,7 +413,12 @@ 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() => { + 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), @@ -372,7 +427,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 +483,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 +505,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, @@ -483,6 +552,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), }) @@ -490,10 +560,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..431f9bb785 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,103 @@ 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, 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_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(); + 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 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(); + assert!(got_lo <= lo && hi <= got_hi, "{stem}"); + } +} + + +/// 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"] { + 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 [(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()], 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"), 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 new file mode 100644 index 0000000000..1864a0f4e3 --- /dev/null +++ b/decompiler/crates/kuna-analysis/src/loader/kuna_nullable_reloc.rs @@ -0,0 +1,256 @@ +//! 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; + +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 = is_mips(arch); + 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 Some(field) = base.checked_add(*offset) 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) = bytes_at(data, *offset, 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 Some(slot) = + slot_for(slots, &mut allocated, idx, cursor, limit, pointer_size as u64) + else { + continue; + }; + out.push(NullableRelocation { + field, + 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(field, 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 = bytes_at(data, *offset, 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(field, 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(field, 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| bytes_at(data, p, 1)) + .is_some_and(|b| matches!(b[0], 0xe8 | 0xe9))) + { + let mut reference = unsupported(field, value, pointer_size); + reference.operand = Operand::CodeReference; + out.push(reference); + continue; + } + if reloc.has_implicit_addend() { + 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(field, value, pointer_size)); + continue; + } + } + }; + 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, + 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 e1ec5d7638..28b58ee953 100644 --- a/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs +++ b/decompiler/crates/kuna-analysis/src/loader/reloc_object.rs @@ -254,10 +254,20 @@ 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: 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)`: + /// 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. pub opd: Vec<(u64, u64, Option)>, @@ -378,6 +388,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. @@ -389,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 @@ -474,6 +491,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 +696,53 @@ 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); + // 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 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())); + function_slots.push((idx, slot)); + } + } + let (nullable_relocations, allocated) = super::kuna_nullable_reloc::references( + file, &vma_of, &extern_of, &mut nullable_slots, &mut extern_cursor, weak_limit); + for (idx, slot) in allocated { + import_slots.push((slot, slot + if file.is_64() { 8 } else { 4 })); + 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, + }); + funcsyms.push((slot, name.to_vec())); + } // 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 @@ -711,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())); @@ -746,6 +818,9 @@ 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, + nullable_relocations, + 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 f589443eb0..9af8db73ba 100644 --- a/decompiler/crates/kuna-analysis/src/loadimage_object.rs +++ b/decompiler/crates/kuna-analysis/src/loadimage_object.rs @@ -577,6 +577,8 @@ 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 /// [`crate::loader::kuna_pltlocal`]. @@ -1181,6 +1183,8 @@ impl ObjectLoadImage { const_ranges, dynreloc_const, import_slots, + nullable_relocations: Vec::new(), + function_address_aliases: Vec::new(), plt_local, mips_linkage, elfv1, @@ -1212,6 +1216,9 @@ 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 .opd @@ -1364,11 +1371,14 @@ 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, + nullable_relocations, + function_address_aliases, plt_local: Vec::new(), mips_linkage: crate::loader::kuna_mipsstubs::class_linkage(&file), elfv1, @@ -1484,10 +1494,20 @@ 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 } + /// (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 @@ -1800,6 +1820,15 @@ 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); + } for r in &mut self.plt_local { r.0 = r.0.wadd(badjust); r.1 = r.1.wadd(badjust); @@ -2115,6 +2144,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 @@ -3055,6 +3103,33 @@ mod tests { } } + #[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::>()); + } + + /// 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)]); + } + /// 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. 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-base/src/xml.rs b/decompiler/crates/kuna-base/src/xml.rs index 5ef7f42d0f..52e765e42e 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"); + // 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 52c08050d6..e7354645b3 100644 --- a/decompiler/crates/kuna-cli/tests/integration.rs +++ b/decompiler/crates/kuna-cli/tests/integration.rs @@ -846,3 +846,9 @@ mod e500_arguments; #[path = "cvf_trace.rs"] 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/mips_weak_imports.rs b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs new file mode 100644 index 0000000000..ab53eda39f --- /dev/null +++ b/decompiler/crates/kuna-cli/tests/mips_weak_imports.rs @@ -0,0 +1,94 @@ +//! 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; + } + let mut images = Vec::new(); + for compiler in ["47", "73"] { + for abi in ["n32", "64"] { + 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 != '_') + .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-cli/tests/nullable_relocations.rs b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs new file mode 100644 index 0000000000..4aa943adb6 --- /dev/null +++ b/decompiler/crates/kuna-cli/tests/nullable_relocations.rs @@ -0,0 +1,413 @@ +//! 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(); + 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(); + 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)"))); + } + 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_"] + } 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" }; + let register = if abi == "x86-64-rip" { + "||register_offset(0)!=bits||register_offset(0x12345)!=bits+0x12345u" + } else { + "" + }; + 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("weak-x86-64-pic.elf").to_str().unwrap(), + "guard", + ]); + 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(); + } + } +} + +/// A weak slot shares no storage with COMMON objects, and the import stays +/// where direct references and by-name queries find it. +#[test] +#[cfg(all(target_os = "linux", target_pointer_width = "64"))] +fn weak_slots_leave_common_storage_and_direct_references_intact() { + let root = common::repo_root().join("tests/fixtures/nullable-relocations"); + let compiler = common::process::optional_output(Command::new("cc").arg("--version")).is_some(); + for abi in ["n32", "64", "x86-64"] { + let file = format!("common-{abi}.elf"); + let path = root.join(&file); + let path = path.to_str().unwrap(); + + let (inventory, error, rc) = common::run_kuna(&["functions", path, "--json"]); + assert_eq!(rc, 0, "{file}: {error}"); + let inventory: serde_json::Value = serde_json::from_str(&inventory).unwrap(); + let listed: Vec<_> = 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 302c332ecd..fa5751c177 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(()) } @@ -4030,6 +4039,22 @@ fn input_isa_stash( vec![("input_isa", explicit)] } +/// (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)> { + (relocatable && !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( @@ -4074,7 +4099,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(), @@ -4738,6 +4764,28 @@ 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 &(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))); + } + } + 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(), + )); // (kuna `dynrelocs`) The PT_GNU_RELRO-frozen dynamic-relocation slots, read // off the same loader while it is still in hand. `readonly_ranges` above @@ -4752,6 +4800,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(); @@ -4949,7 +4998,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 { @@ -6496,6 +6546,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) { 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..f79c02850b --- /dev/null +++ b/decompiler/crates/kuna-console/tests/mips_weak_imports.rs @@ -0,0 +1,116 @@ +//! 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::{ + 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(), 1, "{path:?}: the import slot"); + 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.vma_bytes_mapped(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!(prog.is_import_slot(alias)); + 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/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/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/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..58cbc5c938 --- /dev/null +++ b/decompiler/crates/kuna-decomp/src/p2_lift/kuna_nullable_reloc.rs @@ -0,0 +1,349 @@ +//! 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(); + let alone = records.len() == 1; + 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 + || 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 + || 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())); + } + } + } + // 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); + 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]); + } + 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!( + 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 d2edbac94a..9c0fa9bcbd 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4353, + 4353 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -3486,6 +3486,11 @@ "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: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", @@ -3558,6 +3563,14 @@ "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: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 54c7a3681f..00bf2c65a0 100644 --- a/docs/spec/01-program-prep.md +++ b/docs/spec/01-program-prep.md @@ -893,7 +893,16 @@ 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 `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, 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 @@ -2638,9 +2647,49 @@ 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 (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. +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. 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, +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. @@ -10204,3 +10253,28 @@ 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 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. + +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..48adf89e56 --- /dev/null +++ b/tests/fixtures/mips-weak-imports/README.md @@ -0,0 +1,18 @@ +# 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. +`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, +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..49a7cf0381 --- /dev/null +++ b/tests/fixtures/mips-weak-imports/build.py @@ -0,0 +1,20 @@ +#!/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()}) + 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/identity-64-be-got-only.o b/tests/fixtures/mips-weak-imports/identity-64-be-got-only.o new file mode 100644 index 0000000000..33e2eea4f5 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-64-be-got-only.o differ 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 0000000000..1cc32bb10b Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-64-be-mixed.o differ 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 0000000000..8a9f102e63 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-64-le-got-only.o differ 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 0000000000..699eac7e29 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-64-le-mixed.o differ 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 0000000000..de33f92dce Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-n32-be-got-only.o differ 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 0000000000..0218961d55 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-n32-be-mixed.o differ 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 0000000000..d4fefb30e5 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-n32-le-got-only.o differ 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 0000000000..fd15e2e0cc Binary files /dev/null and b/tests/fixtures/mips-weak-imports/identity-n32-le-mixed.o differ diff --git a/tests/fixtures/mips-weak-imports/identity.S b/tests/fixtures/mips-weak-imports/identity.S new file mode 100644 index 0000000000..be98c73cca --- /dev/null +++ b/tests/fixtures/mips-weak-imports/identity.S @@ -0,0 +1,41 @@ +.abicalls +.set noreorder +.text +.globl same +.type same,@function +same: +.cpsetup $25,$24,same +.if ABI64 + ld $2,%got_disp(optional)($28) +.else + lw $2,%got_disp(optional)($28) +.endif +.if MIXED +.if ABI64 + ld $3,%got_page(saved)($28) + ld $3,%got_ofst(saved)($3) +.else + lw $3,%got_page(saved)($28) + lw $3,%got_ofst(saved)($3) +.endif + xor $2,$2,$3 + sltiu $2,$2,1 +.else + sltiu $2,$2,1 +.endif +.cpreturn + jr $31 + nop +.size same,.-same +.weak optional +.type optional,@function +.if MIXED +.section .rodata +.align 3 +saved: +.if ABI64 + .dword optional +.else + .word optional +.endif +.endif 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 0000000000..acb4be4b8e Binary files /dev/null and b/tests/fixtures/mips-weak-imports/page-64-be.o differ diff --git a/tests/fixtures/mips-weak-imports/page-64-le.o b/tests/fixtures/mips-weak-imports/page-64-le.o new file mode 100644 index 0000000000..254674b91d Binary files /dev/null and b/tests/fixtures/mips-weak-imports/page-64-le.o differ 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 0000000000..3cb81e0a63 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/page-n32-be.o differ 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 0000000000..ba40eb5648 Binary files /dev/null and b/tests/fixtures/mips-weak-imports/page-n32-le.o differ diff --git a/tests/fixtures/mips-weak-imports/page.S b/tests/fixtures/mips-weak-imports/page.S new file mode 100644 index 0000000000..3fe1c2e4ac --- /dev/null +++ b/tests/fixtures/mips-weak-imports/page.S @@ -0,0 +1,27 @@ +.abicalls +.set noreorder +.text +.globl call_optional +.type call_optional,@function +call_optional: +.cpsetup $25,$24,call_optional +.if ABI64 + ld $2,%got_page(optional)($28) + daddiu $2,$2,%got_ofst(optional) +.else + lw $2,%got_page(optional)($28) + addiu $2,$2,%got_ofst(optional) +.endif + beqz $2,1f + xori $3,$4,0x42 + or $25,$2,$0 +.cpreturn + jr $25 + nop +1: +.cpreturn + jr $31 + or $2,$3,$0 +.size call_optional,.-call_optional +.weak optional +.type optional,@function diff --git a/tests/fixtures/mips-weak-imports/provenance.json b/tests/fixtures/mips-weak-imports/provenance.json new file mode 100644 index 0000000000..8d4f1b7c37 --- /dev/null +++ b/tests/fixtures/mips-weak-imports/provenance.json @@ -0,0 +1,206 @@ +{ + "source": "identity.S", + "source_sha256": "24123e1abe538f4c489c9d0c12d1da8af19621bfbcc16d86b06a54dc3e248432", + "page_source": "page.S", + "page_source_sha256": "309521b0710a3254ca8ed636f76af956e7080d142ee3d6b3009f468c5ead5821", + "assembler_version": "GNU assembler (GNU Binutils) 2.30", + "assembler_sha256": "96b3d7b45a26a3b53aba77e56a372d30c8fa92f06092a2ca78a39d1e97ad465a", + "records": [ + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "--defsym", + "MIXED=0", + "-o", + "identity-n32-be-got-only.o", + "identity.S" + ], + "output": "identity-n32-be-got-only.o", + "sha256": "562f79401c225ba1e853613b94861a04d48f7a3d96cbd0fe8b904d78bcce116b" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "--defsym", + "MIXED=1", + "-o", + "identity-n32-be-mixed.o", + "identity.S" + ], + "output": "identity-n32-be-mixed.o", + "sha256": "691c0b5c16226274ad6b957682208cc56017259c1f79cfab97cdd1df286d3e35" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "-o", + "page-n32-be.o", + "page.S" + ], + "output": "page-n32-be.o", + "sha256": "5e73212fb6196c279b53338ae580e050b75060a0fb4fb5f0f30f9efdc219dcd5" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "--defsym", + "MIXED=0", + "-o", + "identity-n32-le-got-only.o", + "identity.S" + ], + "output": "identity-n32-le-got-only.o", + "sha256": "41ff824e07c0054c7019def17df2c41c3c1935d66835080b0ca6f812bd56057e" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "--defsym", + "MIXED=1", + "-o", + "identity-n32-le-mixed.o", + "identity.S" + ], + "output": "identity-n32-le-mixed.o", + "sha256": "386583815a9563c8a29129503fbc9ad65f1ad9c3b88b945e01232ed4c74d4196" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=n32", + "--defsym", + "ABI64=0", + "-o", + "page-n32-le.o", + "page.S" + ], + "output": "page-n32-le.o", + "sha256": "69103fb1d50c1f32869358654bf3243efbde7403f76cec935cf773ad54774704" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "--defsym", + "MIXED=0", + "-o", + "identity-64-be-got-only.o", + "identity.S" + ], + "output": "identity-64-be-got-only.o", + "sha256": "d7a5f9695777055255030238c8b04eeabd0d69ec52473c21817e6670e4597ecf" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "--defsym", + "MIXED=1", + "-o", + "identity-64-be-mixed.o", + "identity.S" + ], + "output": "identity-64-be-mixed.o", + "sha256": "7504eae1c82ba833c08d961c93011478c986639b48335a5277625d6208d51f7a" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EB", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "-o", + "page-64-be.o", + "page.S" + ], + "output": "page-64-be.o", + "sha256": "facec478f64a46d40a8a2f4128d4c094f1daf27ae63f6c0791c29370e47b437b" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "--defsym", + "MIXED=0", + "-o", + "identity-64-le-got-only.o", + "identity.S" + ], + "output": "identity-64-le-got-only.o", + "sha256": "461fab6d2934d643c99a757b2ffce6553ef52c6b8e63c32ef6d8f8a27ce47462" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "--defsym", + "MIXED=1", + "-o", + "identity-64-le-mixed.o", + "identity.S" + ], + "output": "identity-64-le-mixed.o", + "sha256": "4b41e62ea540d5acc022992beeeaea5780b0319a5a83ccf6bdeb97ee19d60ef3" + }, + { + "command": [ + "mips64-octeon-linux-gnu-as", + "-EL", + "-march=octeon", + "-mabi=64", + "--defsym", + "ABI64=1", + "-o", + "page-64-le.o", + "page.S" + ], + "output": "page-64-le.o", + "sha256": "446e42a7c833c3ef01ef0fb7393bd0706af4f4e18f750dd62708a6920d0ed527" + } + ] +} diff --git a/tests/fixtures/nullable-relocations/README.md b/tests/fixtures/nullable-relocations/README.md new file mode 100644 index 0000000000..a3d5f77109 --- /dev/null +++ b/tests/fixtures/nullable-relocations/README.md @@ -0,0 +1,29 @@ +# 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 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 +[-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 0000000000..7fcfbc63f9 Binary files /dev/null and b/tests/fixtures/nullable-relocations/address-64.elf differ diff --git a/tests/fixtures/nullable-relocations/address-n32.elf b/tests/fixtures/nullable-relocations/address-n32.elf new file mode 100644 index 0000000000..0828a269b7 Binary files /dev/null and b/tests/fixtures/nullable-relocations/address-n32.elf differ diff --git a/tests/fixtures/nullable-relocations/address-x86-64-large.elf b/tests/fixtures/nullable-relocations/address-x86-64-large.elf new file mode 100644 index 0000000000..b7e3118f21 Binary files /dev/null and b/tests/fixtures/nullable-relocations/address-x86-64-large.elf differ 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 0000000000..a3bc59b976 Binary files /dev/null and b/tests/fixtures/nullable-relocations/address-x86-64.elf differ diff --git a/tests/fixtures/nullable-relocations/address.c b/tests/fixtures/nullable-relocations/address.c new file mode 100644 index 0000000000..a8635cd126 --- /dev/null +++ b/tests/fixtures/nullable-relocations/address.c @@ -0,0 +1,6 @@ +extern int optional(int) __attribute__((weak)); +__asm__(".type optional,@function"); +unsigned long address_value(void){return (unsigned long)optional;} +unsigned long address_offset(void){return (unsigned long)optional+0x8000UL;} +unsigned long address_negative(void){return (unsigned long)optional-0x8000UL;} +unsigned long unrelated_literal(void){return 0x402000UL;} diff --git a/tests/fixtures/nullable-relocations/build.py b/tests/fixtures/nullable-relocations/build.py new file mode 100644 index 0000000000..1087cb2761 --- /dev/null +++ b/tests/fixtures/nullable-relocations/build.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +"""Build original weak relocations without linked/vendor implementations.""" +import argparse +import hashlib +import json +from pathlib import Path +import re +import subprocess + +p = argparse.ArgumentParser() +for name in ('cc', 'mips_cc', 'linker', 'artifacts'): + p.add_argument('--' + name.replace('_', '-'), required=True) +a = p.parse_args() +r = Path(__file__).resolve().parent +artifacts = Path(a.artifacts).resolve() +artifacts.mkdir(parents=True, exist_ok=True) +sha = lambda p: hashlib.sha256(p.read_bytes()).hexdigest() +rows = [] +for abi in ['n32', '64', 'x86-64', 'x86-64-large']: + mips = abi in ['n32', '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'] + if abi.endswith('large'): + flags += ['-mcmodel=large'] + for source in ['weak.c', 'address.c']: + name = source[:-2] + '-' + abi + '.elf' + extra = ['-DTYPE_HINT'] if abi.endswith('large') and source == 'weak.c' else [] + command = [cc, '-O2', '-fno-stack-protector', '-c', source, '-o', name, *flags, *extra] + subprocess.run(command, cwd=r, check=True) + rows.append({'file': name, 'abi': abi, 'kind': source[:-2], 'source': source, + 'sha256': sha(r / name), 'commands': [[Path(cc).name, *command[1:]]]}) + if mips: + name = 'readonly-' + abi + '.elf' + command = [cc, '-c', 'readonly-mips.S', '-o', name, '-mabi=' + abi, + '-march=octeon', '-mabicalls', '-fPIC', '-DABI64=' + ('1' if abi == '64' else '0')] + subprocess.run(command, cwd=r, check=True) + rows.append({'file': name, 'abi': abi, 'kind': 'address', 'source': 'readonly-mips.S', + 'sha256': sha(r / name), 'commands': [command]}) + pic = artifacts / ('pic-' + abi + '.o') + command = [cc, '-O2', '-fno-stack-protector', '-c', str(r / 'weak.c'), '-o', str(pic), + '-mabi=' + abi, '-march=octeon', '-mabicalls', '-fPIC', '-G0', + '-Dguard=pic_guard', '-Doffset_guard=pic_offset_guard', + '-Dpresent=pic_present', '-Ddefined_guard=pic_defined_guard'] + subprocess.run(command, check=True) + name = 'mixed-' + abi + '.elf' + link = [a.linker, '-r', '-m', 'elf32btsmipn32' if abi == 'n32' else 'elf64btsmip', + '-o', str(r / name), str(r / ('weak-' + abi + '.elf')), str(pic)] + 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] + subprocess.run(command, cwd=r, check=True) + rows.append({'file': name, 'abi': abi, 'kind': 'address', 'source': source, + 'sha256': sha(r / name), 'commands': [[Path(a.cc).name, *command[1:]]]}) +def portable(argument): + argument = str(argument) + for prefix in [str(r) + '/', str(artifacts) + '/']: + if argument.startswith(prefix): return argument[len(prefix):] + return argument +for row in rows: + row['commands'] = [[Path(command[0]).name, *map(portable, command[1:])] for command in row['commands']] + if 'link_diagnostics' in row: + row['link_diagnostics'] = re.sub(r'(? + + +03e0000800000000 + + + +if \(dat_[0-9a-f]+\) +optional\(x\); +return 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..5908fb2a0b --- /dev/null +++ b/tests/stages/kuna-nullable-relocations.xml @@ -0,0 +1,111 @@ + + +03e0000800000000 + + + + +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]+; +