From f7f6bcfa9b4da67d84c3554a75b8e036869ccc1d Mon Sep 17 00:00:00 2001 From: Daniel Thaler Date: Thu, 31 Oct 2024 20:13:44 +0100 Subject: [PATCH] Complain if an elf file contains no debug info If an elf file was stripped of debug info, then a2ltool would previously still accept it, and remove all data from the a2l file during an update. Now an elf file without debug info is rejected. --- src/dwarf/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/dwarf/mod.rs b/src/dwarf/mod.rs index cb14737..cf69501 100644 --- a/src/dwarf/mod.rs +++ b/src/dwarf/mod.rs @@ -109,8 +109,17 @@ impl DebugData { pub(crate) fn load(filename: &OsStr, verbose: bool) -> Result { let filedata = load_filedata(filename)?; let elffile = load_elf_file(&filename.to_string_lossy(), &filedata)?; + + if elffile.sections().find(|section| section.name() == Ok(".debug_info")).is_none() { + return Err(format!("Error: {} does not contain DWARF2+ debug info. The section .debug_info is missing.", filename.to_string_lossy())); + } + let dwarf = load_dwarf(&elffile)?; + if !verify_dwarf_compile_units(&dwarf) { + return Err(format!("Error: {} does not contain DWARF2+ debug info - zero compile units contain debug info.", filename.to_string_lossy())); + } + let sections = get_elf_sections(&elffile); let dbg_reader = DebugDataReader { @@ -187,6 +196,17 @@ fn load_dwarf<'data>( gimli::Dwarf::load(loader) } +// verify that the dwarf data is valid +fn verify_dwarf_compile_units(dwarf: &gimli::Dwarf) -> bool { + let mut units_iter = dwarf.debug_info.units(); + let mut units_count = 0; + while let Ok(Some(_)) = units_iter.next() { + units_count += 1; + } + + units_count > 0 +} + // get a section from the elf file. // returns a slice referencing the section data if it exists, or an empty slice otherwise fn get_file_section_reader<'data>(