Skip to content

Commit 7a52185

Browse files
committed
Fix DWARF parser reporting as valid for any binary with a build id
1 parent 783a3cb commit 7a52185

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

rust/examples/dwarf/dwarf_import/src/helpers.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,22 @@ pub(crate) fn download_debug_info(view: &BinaryView) -> Result<Ref<BinaryView>,
432432
}
433433

434434

435-
pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<Ref<BinaryView>>, String> {
435+
pub(crate) fn find_local_debug_file(view: &BinaryView) -> Option<String> {
436436
let settings = Settings::new("");
437437
let debug_info_paths = settings.get_string_list("analysis.debugInfo.debugDirectories", Some(view), None);
438+
438439
if debug_info_paths.is_empty() {
439-
return Ok(None)
440+
return None
440441
}
441442

443+
let build_id = match get_build_id(view) {
444+
Ok(x) => x,
445+
Err(_) => return None,
446+
};
447+
442448
for debug_info_path in debug_info_paths.into_iter() {
443449
if let Ok(path) = PathBuf::from_str(&debug_info_path.to_string())
444450
{
445-
let build_id = get_build_id(view)?;
446451
let elf_path = path
447452
.join(&build_id[..2])
448453
.join(&build_id[2..])
@@ -452,25 +457,36 @@ pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<R
452457
.join(&build_id[..2])
453458
.join(format!("{}.debug", &build_id[2..]));
454459

455-
let options = "{\"analysis.debugInfo.internal\": false}";
456460
let final_path = if debug_ext_path.exists() {
457461
debug_ext_path
458462
}
459463
else if elf_path.exists() {
460464
elf_path
461465
}
462466
else {
463-
// No paths exist
464-
return Ok(None)
467+
// No paths exist in this dir, try the next one
468+
continue;
465469
};
466-
return Ok(
467-
binaryninja::load_with_options(
468-
final_path.to_string_lossy().to_string(),
469-
false,
470-
Some(options)
471-
)
472-
);
470+
return final_path
471+
.to_str()
472+
.and_then(|x| Some(x.to_string()));
473473
}
474474
}
475-
Ok(None)
475+
None
476+
}
477+
478+
479+
pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<Ref<BinaryView>>, String> {
480+
if let Some(debug_file_path) = find_local_debug_file(view) {
481+
Ok(
482+
binaryninja::load_with_options(
483+
debug_file_path,
484+
false,
485+
Some("{\"analysis.debugInfo.internal\": false}")
486+
)
487+
)
488+
}
489+
else {
490+
Ok(None)
491+
}
476492
}

rust/examples/dwarf/dwarf_import/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ struct DWARFParser;
271271
impl CustomDebugInfoParser for DWARFParser {
272272
fn is_valid(&self, view: &BinaryView) -> bool {
273273
dwarfreader::is_valid(view) ||
274-
dwarfreader::can_use_build_id(view) ||
275-
dwarfreader::can_use_debuginfod(view)
274+
dwarfreader::can_use_debuginfod(view) ||
275+
(dwarfreader::has_build_id_section(view) && helpers::find_local_debug_file(view).is_some())
276276
}
277277

278278
fn parse_info(
@@ -283,7 +283,7 @@ impl CustomDebugInfoParser for DWARFParser {
283283
progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
284284
) -> bool {
285285
let external_file = if !dwarfreader::is_valid(bv) {
286-
if dwarfreader::can_use_build_id(bv) {
286+
if dwarfreader::has_build_id_section(bv) {
287287
if let Ok(Some(debug_view)) = helpers::load_debug_info_for_build_id(bv) {
288288
Some(debug_view)
289289
}

rust/examples/dwarf/shared/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ pub fn is_raw_dwo_dwarf(view: &BinaryView) -> bool {
5454
}
5555

5656
pub fn can_use_debuginfod(view: &BinaryView) -> bool {
57-
can_use_build_id(view) &&
57+
has_build_id_section(view) &&
5858
Settings::new("")
5959
.get_bool("network.enableDebuginfod", Some(view), None)
6060
}
6161

62-
pub fn can_use_build_id(view: &BinaryView) -> bool {
62+
pub fn has_build_id_section(view: &BinaryView) -> bool {
6363
if let Ok(raw_view) = view.raw_view() {
6464
return raw_view.section_by_name(".note.gnu.build-id").is_ok()
6565
}

0 commit comments

Comments
 (0)