Skip to content

Commit d45c96b

Browse files
rbranemesare
authored andcommitted
[IDB Import] Fix base address translation
Previously we assumed the original base address was used, however this causes issues now that we automatically rebase lower address spaced binaries.
1 parent 93b86d9 commit d45c96b

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

plugins/idb_import/src/lib.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,22 @@ fn parse_id0_section_info<K: IDAKind>(
232232
debug_file: &BinaryView,
233233
id0: &ID0Section<K>,
234234
) -> Result<()> {
235-
let version = match id0.ida_info()? {
236-
idb_rs::id0::IDBParam::V1(IDBParam1 { version, .. })
237-
| idb_rs::id0::IDBParam::V2(IDBParam2 { version, .. }) => version,
235+
let (version, idb_baseaddr) = match id0.ida_info()? {
236+
idb_rs::id0::IDBParam::V1(IDBParam1 {
237+
version, baseaddr, ..
238+
})
239+
| idb_rs::id0::IDBParam::V2(IDBParam2 {
240+
version, baseaddr, ..
241+
}) => (version, baseaddr.into_u64()),
238242
};
239243

240-
for (addr, info) in get_info(id0, version)? {
244+
let bv_baseaddr = bv.start();
245+
// just addr this value to the address to translate from ida to bn
246+
// NOTE this delta could wrapp here and while using translating
247+
let addr_delta = bv_baseaddr.wrapping_sub(idb_baseaddr);
248+
249+
for (idb_addr, info) in get_info(id0, version)? {
250+
let addr = addr_delta.wrapping_add(idb_addr.into_u64());
241251
// just in case we change this struct in the future, this line will for us to review this code
242252
// TODO merge this data with folder locations
243253
let AddrInfo {
@@ -246,11 +256,8 @@ fn parse_id0_section_info<K: IDAKind>(
246256
ty,
247257
} = info;
248258
// TODO set comments to address here
249-
for function in &bv.functions_containing(addr.into_u64()) {
250-
function.set_comment_at(
251-
addr.into_u64(),
252-
&String::from_utf8_lossy(&comments.join(&b"\n"[..])),
253-
);
259+
for function in &bv.functions_containing(addr) {
260+
function.set_comment_at(addr, &String::from_utf8_lossy(&comments.join(&b"\n"[..])));
254261
}
255262

256263
let bnty = ty
@@ -282,7 +289,7 @@ fn parse_id0_section_info<K: IDAKind>(
282289
None,
283290
label.map(|x| x.to_string()),
284291
bnty,
285-
Some(addr.into_u64()),
292+
Some(addr),
286293
None,
287294
vec![],
288295
vec![],
@@ -292,7 +299,7 @@ fn parse_id0_section_info<K: IDAKind>(
292299
}
293300
(label, Some(_ty), Some(bnty)) => {
294301
let label: Option<&str> = label.as_ref().map(|x| x.as_ref());
295-
if !debug_info.add_data_variable(addr.into_u64(), &bnty, label, &[]) {
302+
if !debug_info.add_data_variable(addr, &bnty, label, &[]) {
296303
error!("Unable to add the type at {addr:#x}")
297304
}
298305
}
@@ -302,7 +309,7 @@ fn parse_id0_section_info<K: IDAKind>(
302309
// TODO how to add a label without a type associacted with it?
303310
if let Some(name) = label {
304311
if !debug_info.add_data_variable(
305-
addr.into_u64(),
312+
addr,
306313
&binaryninja::types::Type::void(),
307314
Some(&name),
308315
&[],
@@ -314,7 +321,7 @@ fn parse_id0_section_info<K: IDAKind>(
314321
(Some(name), None, None) => {
315322
// TODO how to add a label without a type associacted with it?
316323
if !debug_info.add_data_variable(
317-
addr.into_u64(),
324+
addr,
318325
&binaryninja::types::Type::void(),
319326
Some(&name),
320327
&[],

0 commit comments

Comments
 (0)