Skip to content

Commit 05a6289

Browse files
committed
Fix crash when exporting DWARF fails
1 parent f3c7443 commit 05a6289

File tree

1 file changed

+14
-10
lines changed
  • plugins/dwarf/dwarf_export/src

1 file changed

+14
-10
lines changed

plugins/dwarf/dwarf_export/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ fn write_dwarf<T: gimli::Endianity>(
657657
arch: Architecture,
658658
endian: T,
659659
dwarf: &mut DwarfUnit,
660-
) {
660+
) -> Result<(), String> {
661661
// TODO : Look in to other options (mangling, flags, etc (see Object::new))
662662
let mut out_object = write::Object::new(
663663
BinaryFormat::Elf,
@@ -671,7 +671,7 @@ fn write_dwarf<T: gimli::Endianity>(
671671

672672
// Finally, write the DWARF data to the sections.
673673
let mut sections = Sections::new(EndianVec::new(endian));
674-
dwarf.write(&mut sections).unwrap();
674+
dwarf.write(&mut sections).map_err(|e| e.to_string())?;
675675

676676
sections
677677
.for_each(|input_id, input_data| {
@@ -685,15 +685,14 @@ fn write_dwarf<T: gimli::Endianity>(
685685
// Write data to section in output object
686686
let out_section = out_object.section_mut(output_id);
687687
if out_section.is_bss() {
688-
panic!("Please report this as a bug: output section is bss");
688+
return Err("Please report this as a bug: output section is bss".to_string());
689689
} else {
690690
out_section.set_data(input_data.clone().into_vec(), 1);
691691
}
692692
// out_section.flags = in_section.flags(); // TODO
693693

694-
Ok::<(), ()>(())
695-
})
696-
.unwrap();
694+
Ok(())
695+
})?;
697696

698697
if let Ok(out_data) = out_object.write() {
699698
if let Err(err) = fs::write(file_path, out_data) {
@@ -704,6 +703,8 @@ fn write_dwarf<T: gimli::Endianity>(
704703
} else {
705704
error!("Failed to write DWARF with requested settings");
706705
}
706+
707+
Ok(())
707708
}
708709

709710
fn export_dwarf(bv: &BinaryView) {
@@ -771,11 +772,14 @@ fn export_dwarf(bv: &BinaryView) {
771772
// TODO: Export all symbols instead of just data vars?
772773
// TODO: Sections? Segments?
773774

774-
if bv.default_endianness() == binaryninja::Endianness::LittleEndian {
775-
write_dwarf(&save_loc_path, arch, gimli::LittleEndian, &mut dwarf);
776-
} else {
777-
write_dwarf(&save_loc_path, arch, gimli::BigEndian, &mut dwarf);
775+
let endianness = match bv.default_endianness() {
776+
binaryninja::Endianness::LittleEndian => gimli::RunTimeEndian::Little,
777+
binaryninja::Endianness::BigEndian => gimli::RunTimeEndian::Big,
778778
};
779+
780+
if let Err(e) = write_dwarf(&save_loc_path, arch, endianness, &mut dwarf) {
781+
error!("Error writing DWARF: {}", e);
782+
}
779783
}
780784

781785
struct MyCommand;

0 commit comments

Comments
 (0)