Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/examples/decompile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
eprintln!("Function count: {}", bv.functions().len());

for func in &bv.functions() {
decompile_to_c(bv.as_ref(), func.as_ref());
decompile_to_c(bv.as_ref(), func);
}

binaryninja::headless::shutdown();
Expand Down
37 changes: 18 additions & 19 deletions rust/examples/dwarf/dwarf_export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use binaryninja::{
interaction,
interaction::{FormResponses, FormResponses::Index},
logger::init,
rc::Ref,
string::BnString,
symbol::SymbolType,
types::{Conf, MemberAccess, StructureType, Type, TypeClass},
Expand All @@ -27,12 +26,12 @@ fn export_type(
name: String,
t: &Type,
bv: &BinaryView,
defined_types: &mut Vec<(Ref<Type>, UnitEntryId)>,
defined_types: &mut Vec<(Type, UnitEntryId)>,
dwarf: &mut DwarfUnit,
) -> Option<UnitEntryId> {
if let Some((_, die)) = defined_types
.iter()
.find(|(defined_type, _)| defined_type.as_ref() == t)
.find(|(defined_type, _)| defined_type == t)
{
return Some(*die);
}
Expand Down Expand Up @@ -165,7 +164,7 @@ fn export_type(

if let Some(target_die_uid) = export_type(
format!("{}", struct_member.ty.contents),
struct_member.ty.contents.as_ref(),
&struct_member.ty.contents,
bv,
defined_types,
dwarf,
Expand Down Expand Up @@ -373,12 +372,12 @@ fn export_type(
fn export_types(
bv: &BinaryView,
dwarf: &mut DwarfUnit,
defined_types: &mut Vec<(Ref<Type>, UnitEntryId)>,
defined_types: &mut Vec<(Type, UnitEntryId)>,
) {
for t in &bv.types() {
export_type(
t.name().to_string(),
&t.type_object(),
t.type_object(),
bv,
defined_types,
dwarf,
Expand All @@ -389,7 +388,7 @@ fn export_types(
fn export_functions(
bv: &BinaryView,
dwarf: &mut DwarfUnit,
defined_types: &mut Vec<(Ref<Type>, UnitEntryId)>,
defined_types: &mut Vec<(Type, UnitEntryId)>,
) {
let entry_point = bv.entry_point_function();

Expand All @@ -408,7 +407,7 @@ fn export_functions(
// TODO : (DW_AT_main_subprogram VS DW_TAG_entry_point)
// TODO : This attribute seems maybe usually unused?
if let Ok(entry_point_function) = &entry_point {
if entry_point_function.as_ref() == function.as_ref() {
if entry_point_function == function {
dwarf
.unit
.get_mut(function_die_uid)
Expand Down Expand Up @@ -459,7 +458,7 @@ fn export_functions(
if function.return_type().contents.type_class() != TypeClass::VoidTypeClass {
if let Some(return_type_die_uid) = export_type(
format!("{}", function.return_type().contents),
function.return_type().contents.as_ref(),
&function.return_type().contents,
bv,
defined_types,
dwarf,
Expand Down Expand Up @@ -516,19 +515,19 @@ fn export_functions(
fn export_data_vars(
bv: &BinaryView,
dwarf: &mut DwarfUnit,
defined_types: &mut Vec<(Ref<Type>, UnitEntryId)>,
defined_types: &mut Vec<(Type, UnitEntryId)>,
) {
let root = dwarf.unit.root();

for data_variable in &bv.data_variables() {
if let Some(symbol) = data_variable.symbol(bv) {
if symbol.sym_type() == SymbolType::External {
continue;
} else if symbol.sym_type() == SymbolType::Function {
continue;
} else if symbol.sym_type() == SymbolType::ImportedFunction {
continue;
} else if symbol.sym_type() == SymbolType::LibraryFunction {
if matches!(
symbol.sym_type(),
SymbolType::External
| SymbolType::Function
| SymbolType::ImportedFunction
| SymbolType::LibraryFunction
) {
continue;
}
}
Expand Down Expand Up @@ -567,7 +566,7 @@ fn export_data_vars(

if let Some(target_die_uid) = export_type(
format!("{}", data_variable.t.contents),
data_variable.t.contents.as_ref(),
&data_variable.t.contents,
bv,
defined_types,
dwarf,
Expand Down Expand Up @@ -756,7 +755,7 @@ fn export_dwarf(bv: &BinaryView) {
);

// Everything has types, so we need to track what is already defined globally as to not duplicate type entries
let mut defined_types: Vec<(Ref<Type>, UnitEntryId)> = vec![];
let mut defined_types: Vec<(Type, UnitEntryId)> = vec![];
export_types(bv, &mut dwarf, &mut defined_types);
export_functions(bv, &mut dwarf, &mut defined_types);
export_data_vars(bv, &mut dwarf, &mut defined_types);
Expand Down
55 changes: 21 additions & 34 deletions rust/examples/dwarf/dwarf_import/src/die_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ use crate::dwarfdebuginfo::{DebugInfoBuilder, DebugInfoBuilderContext, TypeUID};
use crate::helpers::*;
use crate::types::get_type;

use binaryninja::{
rc::*,
types::{EnumerationBuilder, FunctionParameter, ReferenceType, Type, TypeBuilder},
};
use binaryninja::types::{EnumerationBuilder, FunctionParameter, ReferenceType, Type, TypeBuilder};

use gimli::{constants, AttributeValue::Encoding, DebuggingInformationEntry, Reader, Unit};

pub(crate) fn handle_base_type<R: Reader<Offset = usize>>(
unit: &Unit<R>,
entry: &DebuggingInformationEntry<R>,
debug_info_builder_context: &DebugInfoBuilderContext<R>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All base types have:
// DW_AT_encoding (our concept of type_class)
// DW_AT_byte_size and/or DW_AT_bit_size
Expand Down Expand Up @@ -73,7 +70,7 @@ pub(crate) fn handle_enum<R: Reader<Offset = usize>>(
unit: &Unit<R>,
entry: &DebuggingInformationEntry<R>,
debug_info_builder_context: &DebugInfoBuilderContext<R>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All base types have:
// DW_AT_byte_size
// *DW_AT_name
Expand Down Expand Up @@ -132,7 +129,7 @@ pub(crate) fn handle_typedef(
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
typedef_name: String,
) -> (Option<Ref<Type>>, bool) {
) -> (Option<Type>, bool) {
// All base types have:
// DW_AT_name
// *DW_AT_type
Expand All @@ -159,7 +156,7 @@ pub(crate) fn handle_pointer<R: Reader<Offset = usize>>(
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
reference_type: ReferenceType,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All pointer types have:
// DW_AT_type
// *DW_AT_byte_size
Expand All @@ -174,15 +171,15 @@ pub(crate) fn handle_pointer<R: Reader<Offset = usize>>(
if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1;
Some(Type::pointer_of_width(
parent_type.as_ref(),
&parent_type,
pointer_size,
false,
false,
Some(reference_type),
))
} else {
Some(Type::pointer_of_width(
Type::void().as_ref(),
&Type::void(),
pointer_size,
false,
false,
Expand All @@ -192,15 +189,15 @@ pub(crate) fn handle_pointer<R: Reader<Offset = usize>>(
} else if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1;
Some(Type::pointer_of_width(
parent_type.as_ref(),
&parent_type,
debug_info_builder_context.default_address_size(),
false,
false,
Some(reference_type),
))
} else {
Some(Type::pointer_of_width(
Type::void().as_ref(),
&Type::void(),
debug_info_builder_context.default_address_size(),
false,
false,
Expand All @@ -214,7 +211,7 @@ pub(crate) fn handle_array<R: Reader<Offset = usize>>(
entry: &DebuggingInformationEntry<R>,
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All array types have:
// DW_AT_type
// *DW_AT_name
Expand All @@ -234,22 +231,16 @@ pub(crate) fn handle_array<R: Reader<Offset = usize>>(
let mut children = tree.root().unwrap().children();

// TODO : This is currently applying the size in reverse order
let mut result_type: Option<Ref<Type>> = None;
let mut result_type: Option<Type> = None;
while let Ok(Some(child)) = children.next() {
if let Some(inner_type) = result_type {
result_type = Some(Type::array(
inner_type.as_ref(),
get_subrange_size(child.entry()),
));
result_type = Some(Type::array(&inner_type, get_subrange_size(child.entry())));
} else {
result_type = Some(Type::array(
parent_type.as_ref(),
get_subrange_size(child.entry()),
));
result_type = Some(Type::array(&parent_type, get_subrange_size(child.entry())));
}
}

result_type.map_or(Some(Type::array(parent_type.as_ref(), 0)), Some)
result_type.map_or(Some(Type::array(&parent_type, 0)), Some)
} else {
None
}
Expand All @@ -261,7 +252,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>(
debug_info_builder_context: &DebugInfoBuilderContext<R>,
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All subroutine types have:
// *DW_AT_name
// *DW_AT_type (if not provided, void)
Expand Down Expand Up @@ -301,11 +292,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>(
name.clone(),
Type::named_type_from_type(
name,
&Type::function::<String, &binaryninja::types::Type>(
return_type.as_ref(),
&[],
false,
),
&Type::function::<String, &binaryninja::types::Type>(&return_type, &[], false),
),
false,
);
Expand Down Expand Up @@ -343,7 +330,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>(
}

Some(Type::function(
return_type.as_ref(),
&return_type,
&parameters,
variable_arguments,
))
Expand All @@ -352,7 +339,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>(
pub(crate) fn handle_const(
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All const types have:
// ?DW_AT_allocated
// ?DW_AT_associated
Expand All @@ -363,7 +350,7 @@ pub(crate) fn handle_const(

if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1;
Some((*parent_type).to_builder().set_const(true).finalize())
Some(parent_type.to_builder().set_const(true).finalize())
} else {
Some(TypeBuilder::void().set_const(true).finalize())
}
Expand All @@ -372,7 +359,7 @@ pub(crate) fn handle_const(
pub(crate) fn handle_volatile(
debug_info_builder: &mut DebugInfoBuilder,
entry_type: Option<TypeUID>,
) -> Option<Ref<Type>> {
) -> Option<Type> {
// All const types have:
// ?DW_AT_allocated
// ?DW_AT_associated
Expand All @@ -383,7 +370,7 @@ pub(crate) fn handle_volatile(

if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1;
Some((*parent_type).to_builder().set_volatile(true).finalize())
Some(parent_type.to_builder().set_volatile(true).finalize())
} else {
Some(TypeBuilder::void().set_volatile(true).finalize())
}
Expand Down
Loading