Skip to content

Commit

Permalink
add shitfted attribute to til pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Dec 27, 2024
1 parent f4a7c6c commit 755cbc7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/til.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ impl TypeRaw {
Bitfield::read(input, type_flags).context("Type::Bitfield")?,
),

// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x480369

// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x480369
(flag::tf_complex::BT_COMPLEX, flag::tf_complex::BTMT_TYPEDEF) => Typedef::read(input)
.context("Type::Typedef")
Expand Down
43 changes: 30 additions & 13 deletions src/til/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::Result;

use crate::ida_reader::IdaGenericBufUnpack;
use crate::til::section::TILSectionHeader;
use crate::til::{Type, TypeRaw, TAH};
Expand All @@ -6,6 +8,7 @@ use crate::til::{Type, TypeRaw, TAH};
pub struct Pointer {
pub closure: PointerType,
pub modifier: Option<PointerModifier>,
pub shifted: Option<(Box<Type>, u32)>,
pub typ: Box<Type>,
}

Expand All @@ -14,12 +17,24 @@ impl Pointer {
til: &TILSectionHeader,
raw: PointerRaw,
fields: &mut impl Iterator<Item = Vec<u8>>,
) -> anyhow::Result<Self> {
) -> Result<Self> {
let shifted = raw
.shifted
.map(|(t, v)| -> Result<_> {
Ok((
// TODO if this type allow non typedef, this may consume fields
Type::new(til, *t, &mut vec![].into_iter()).map(Box::new)?,
v,
))
})
.transpose()?;
let typ = Type::new(til, *raw.typ, fields).map(Box::new)?;
Ok(Self {
// TODO forward fields to closure?
closure: PointerType::new(til, raw.closure)?,
modifier: raw.modifier,
typ: Type::new(til, *raw.typ, fields).map(Box::new)?,
shifted,
typ,
})
}
}
Expand All @@ -34,7 +49,7 @@ pub enum PointerType {
}

impl PointerType {
fn new(til: &TILSectionHeader, raw: PointerTypeRaw) -> anyhow::Result<Self> {
fn new(til: &TILSectionHeader, raw: PointerTypeRaw) -> Result<Self> {
match raw {
PointerTypeRaw::Closure(c) => {
// TODO subtype get the fields?
Expand Down Expand Up @@ -62,6 +77,7 @@ pub enum PointerModifier {
pub(crate) struct PointerRaw {
pub closure: PointerTypeRaw,
pub modifier: Option<PointerModifier>,
pub shifted: Option<(Box<TypeRaw>, u32)>,
pub typ: Box<TypeRaw>,
}

Expand All @@ -70,7 +86,7 @@ impl PointerRaw {
input: &mut impl IdaGenericBufUnpack,
header: &TILSectionHeader,
metadata: u8,
) -> anyhow::Result<Self> {
) -> Result<Self> {
use crate::til::flag::tattr_ptr::*;
use crate::til::flag::tf_ptr::*;
// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x478d67
Expand All @@ -88,11 +104,14 @@ impl PointerRaw {
let tah = TAH::read(&mut *input)?;
let typ = TypeRaw::read(&mut *input, header)?;
// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x459bc6
if tah.0 .0 & TAPTR_SHIFTED != 0 {
// TODO __shifted?
let _typ = TypeRaw::read(&mut *input, header)?;
let _value = input.read_de()?;
}
let shifted = (tah.0 .0 & TAPTR_SHIFTED != 0)
.then(|| -> Result<_> {
// TODO allow typedef only?
let typ = TypeRaw::read(&mut *input, header)?;
let value = input.read_de()?;
Ok((Box::new(typ), value))
})
.transpose()?;

// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x459bc6 print_til_type_att
let modifier = match tah.0 .0 & (TAPTR_RESTRICT | TAPTR_PTR64 | TAPTR_PTR32) {
Expand All @@ -112,6 +131,7 @@ impl PointerRaw {
Ok(Self {
closure,
modifier,
shifted,
typ: Box::new(typ),
})
}
Expand All @@ -128,10 +148,7 @@ pub(crate) enum PointerTypeRaw {
}

impl PointerTypeRaw {
fn read(
input: &mut impl IdaGenericBufUnpack,
header: &TILSectionHeader,
) -> anyhow::Result<Self> {
fn read(input: &mut impl IdaGenericBufUnpack, header: &TILSectionHeader) -> Result<Self> {
let closure_type = input.read_u8()?;
if closure_type == 0xFF {
// InnerRef fb47f2c2-3c08-4d40-b7ab-3c7736dce31d 0x473b5a
Expand Down
22 changes: 21 additions & 1 deletion src/tools/tilib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,13 @@ fn print_til_type_pointer(
Some(idb_rs::til::pointer::PointerModifier::Ptr64) => "__ptr64 ",
Some(idb_rs::til::pointer::PointerModifier::Restricted) => "__restricted ",
};
write!(fmt, "*{modifier}{}", name.unwrap_or(""))?;
write!(fmt, "*{modifier}")?;
if let Some((ty, value)) = &pointer.shifted {
write!(fmt, "__shifted(")?;
print_til_type_only(fmt, section, ty)?;
write!(fmt, ",{value:#X}) ")?;
}
write!(fmt, "{}", name.unwrap_or(""))?;
}
Ok(())
}
Expand Down Expand Up @@ -728,6 +734,20 @@ fn print_til_type_name(
write!(fmt, "{}{name}", if print_prefix { prefix } else { "" })
}

fn print_til_type_only(fmt: &mut impl Write, section: &TILSection, tinfo: &Type) -> Result<()> {
match &tinfo.type_variant {
TypeVariant::Typedef(Typedef::Name(name)) => {
write!(fmt, "{}", String::from_utf8_lossy(name))?;
}
TypeVariant::Typedef(Typedef::Ordinal(ord)) => {
let ty = section.get_ord(Id0TilOrd { ord: (*ord).into() }).unwrap();
write!(fmt, "{}", String::from_utf8_lossy(&ty.name))?;
}
_ => {}
};
Ok(())
}

fn print_til_type_len(
fmt: &mut impl Write,
section: &TILSection,
Expand Down

0 comments on commit 755cbc7

Please sign in to comment.