Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPC: Guess reloc data type based on the instruction. #108

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion objdiff-core/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{borrow::Cow, collections::BTreeMap};
use std::{borrow::Cow, collections::BTreeMap, ffi::CStr};

use anyhow::{bail, Result};
use byteorder::ByteOrder;
use object::{Architecture, File, Object, ObjectSymbol, Relocation, RelocationFlags, Symbol};

use crate::{
diff::DiffObjConfig,
obj::{ObjIns, ObjReloc, ObjSection},
util::ReallySigned,
};

#[cfg(feature = "arm")]
Expand All @@ -17,6 +19,97 @@ pub mod ppc;
#[cfg(feature = "x86")]
pub mod x86;

/// Represents the type of data associated with an instruction
pub enum DataType {
Int8,
Int16,
Int32,
Int64,
Int128,
Float,
Double,
Bytes,
String,
}

impl DataType {
pub fn display_bytes<Endian: ByteOrder>(&self, bytes: &[u8]) -> Option<String> {
if self.required_len().is_some_and(|l| bytes.len() < l) {
return None;
}

match self {
DataType::Int8 => {
let i = i8::from_ne_bytes(bytes.try_into().unwrap());
if i < 0 {
format!("Int8: {:#x} ({:#x})", i, ReallySigned(i))
} else {
format!("Int8: {:#x}", i)
}
}
DataType::Int16 => {
let i = Endian::read_i16(bytes);
if i < 0 {
format!("Int16: {:#x} ({:#x})", i, ReallySigned(i))
} else {
format!("Int16: {:#x}", i)
}
}
DataType::Int32 => {
let i = Endian::read_i32(bytes);
if i < 0 {
format!("Int32: {:#x} ({:#x})", i, ReallySigned(i))
} else {
format!("Int32: {:#x}", i)
}
}
DataType::Int64 => {
let i = Endian::read_i64(bytes);
if i < 0 {
format!("Int64: {:#x} ({:#x})", i, ReallySigned(i))
} else {
format!("Int64: {:#x}", i)
}
}
DataType::Int128 => {
let i = Endian::read_i128(bytes);
if i < 0 {
format!("Int128: {:#x} ({:#x})", i, ReallySigned(i))
} else {
format!("Int128: {:#x}", i)
}
}
DataType::Float => {
format!("Float: {}", Endian::read_f32(bytes))
}
DataType::Double => {
format!("Double: {}", Endian::read_f64(bytes))
}
DataType::Bytes => {
format!("Bytes: {:#?}", bytes)
}
DataType::String => {
format!("String: {:?}", CStr::from_bytes_until_nul(bytes).ok()?)
}
}
.into()
}

fn required_len(&self) -> Option<usize> {
match self {
DataType::Int8 => Some(1),
DataType::Int16 => Some(2),
DataType::Int32 => Some(4),
DataType::Int64 => Some(8),
DataType::Int128 => Some(16),
DataType::Float => Some(4),
DataType::Double => Some(8),
DataType::Bytes => None,
DataType::String => None,
}
}
}

pub trait ObjArch: Send + Sync {
fn process_code(
&self,
Expand All @@ -42,6 +135,12 @@ pub trait ObjArch: Send + Sync {

fn symbol_address(&self, symbol: &Symbol) -> u64 { symbol.address() }

fn guess_data_type(&self, _instruction: &ObjIns) -> Option<DataType> { None }

fn display_data_type(&self, _ty: DataType, bytes: &[u8]) -> Option<String> {
Some(format!("Bytes: {:#x?}", bytes))
}
SquareMan marked this conversation as resolved.
Show resolved Hide resolved

// Downcast methods
#[cfg(feature = "ppc")]
fn ppc(&self) -> Option<&ppc::ObjArchPpc> { None }
Expand Down
31 changes: 30 additions & 1 deletion objdiff-core/src/arch/ppc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{borrow::Cow, collections::BTreeMap};

use anyhow::{bail, ensure, Result};
use byteorder::BigEndian;
use cwextab::{decode_extab, ExceptionTableData};
use object::{
elf, File, Object, ObjectSection, ObjectSymbol, Relocation, RelocationFlags, RelocationTarget,
Expand All @@ -9,7 +10,7 @@ use object::{
use ppc750cl::{Argument, InsIter, GPR};

use crate::{
arch::{ObjArch, ProcessCodeResult},
arch::{DataType, ObjArch, ProcessCodeResult},
diff::DiffObjConfig,
obj::{ObjIns, ObjInsArg, ObjInsArgValue, ObjReloc, ObjSection, ObjSymbol},
};
Expand Down Expand Up @@ -186,6 +187,34 @@ impl ObjArch for ObjArchPpc {
}
}

fn guess_data_type(&self, instruction: &ObjIns) -> Option<super::DataType> {
// Always shows the first string of the table. Not ideal, but it's really hard to find
// the actual string being referenced.
if instruction.reloc.as_ref().is_some_and(|r| r.target.name.starts_with("@stringBase")) {
return Some(DataType::String);
}

match instruction.mnemonic.as_str() {
SquareMan marked this conversation as resolved.
Show resolved Hide resolved
"lbz" | "lbzu" | "lbzux" | "lbzx" => Some(DataType::Int8),
"lhz" | "lhzu" | "lhzux" | "lhzx" => Some(DataType::Int16),
"lha" | "lhau" | "lhaux" | "lhax" => Some(DataType::Int16),
"lwz" | "lwzu" | "lwzux" | "lwzx" => Some(DataType::Int32),
"lfs" | "lfsu" | "lfsux" | "lfsx" => Some(DataType::Float),
"lfd" | "lfdu" | "lfdux" | "lfdx" => Some(DataType::Double),

"stb" | "stbu" | "stbux" | "stbx" => Some(DataType::Int8),
"sth" | "sthu" | "sthux" | "sthx" => Some(DataType::Int16),
"stw" | "stwu" | "stwux" | "stwx" => Some(DataType::Int32),
"stfs" | "stfsu" | "stfsux" | "stfsx" => Some(DataType::Float),
"stfd" | "stfdu" | "stfdux" | "stfdx" => Some(DataType::Double),
_ => None,
}
}

fn display_data_type(&self, ty: DataType, bytes: &[u8]) -> Option<String> {
ty.display_bytes::<BigEndian>(bytes)
}

fn ppc(&self) -> Option<&ObjArchPpc> { Some(self) }
}

Expand Down
1 change: 1 addition & 0 deletions objdiff-core/src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub struct ObjSymbol {
pub virtual_address: Option<u64>,
/// Original index in object symbol table
pub original_index: Option<usize>,
pub bytes: Vec<u8>,
}

pub struct ObjInfo {
Expand Down
14 changes: 14 additions & 0 deletions objdiff-core/src/obj/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ fn to_obj_symbol(
let virtual_address = split_meta
.and_then(|m| m.virtual_addresses.as_ref())
.and_then(|v| v.get(symbol.index().0).cloned());

let bytes = symbol
.section_index()
.and_then(|idx| obj_file.section_by_index(idx).ok())
.and_then(|section| section.data().ok())
.and_then(|data| {
data.get(section_address as usize..(section_address + symbol.size()) as usize)
})
.unwrap_or(&[]);

Ok(ObjSymbol {
name: name.to_string(),
demangled_name,
Expand All @@ -89,6 +99,7 @@ fn to_obj_symbol(
addend,
virtual_address,
original_index: Some(symbol.index().0),
bytes: bytes.to_vec(),
})
}

Expand Down Expand Up @@ -179,6 +190,7 @@ fn symbols_by_section(
addend: 0,
virtual_address: None,
original_index: None,
bytes: Vec::new(),
});
}
Ok(result)
Expand Down Expand Up @@ -239,6 +251,7 @@ fn find_section_symbol(
addend: offset_addr as i64,
virtual_address: None,
original_index: None,
bytes: Vec::new(),
})
}

Expand Down Expand Up @@ -521,6 +534,7 @@ fn update_combined_symbol(symbol: ObjSymbol, address_change: i64) -> Result<ObjS
None
},
original_index: symbol.original_index,
bytes: symbol.bytes,
})
}

Expand Down
6 changes: 6 additions & 0 deletions objdiff-gui/src/views/function_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ fn ins_hover_ui(
appearance.highlight_color,
format!("Size: {:x}", reloc.target.size),
);
if let Some(s) = arch
.guess_data_type(ins)
.and_then(|ty| arch.display_data_type(ty, &reloc.target.bytes))
{
ui.colored_label(appearance.highlight_color, s);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this to the context menu as well? So you can copy the float value. Would require splitting out the label and value for display

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one of the things I wanted to do in a followup. It might be a bit before I can get the time again to figure it out.

} else {
ui.colored_label(appearance.highlight_color, "Extern".to_string());
}
Expand Down