From a06382c27e4c7b3358473de33f74b292be68bc3a Mon Sep 17 00:00:00 2001 From: OndrikB <38321117+OndrikB@users.noreply.github.com> Date: Fri, 27 Sep 2024 08:33:36 +0200 Subject: [PATCH] Disambiguate dummy symbols (#107) * Disambiguate dummy symbols * Small formatting improvement * Put HashMap logic into symbol creation --- objdiff-core/src/obj/read.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/objdiff-core/src/obj/read.rs b/objdiff-core/src/obj/read.rs index ad9804b..01b3a57 100644 --- a/objdiff-core/src/obj/read.rs +++ b/objdiff-core/src/obj/read.rs @@ -1,4 +1,10 @@ -use std::{collections::HashSet, fs, io::Cursor, mem::size_of, path::Path}; +use std::{ + collections::{HashMap, HashSet}, + fs, + io::Cursor, + mem::size_of, + path::Path, +}; use anyhow::{anyhow, bail, ensure, Context, Result}; use filetime::FileTime; @@ -147,6 +153,7 @@ fn symbols_by_section( obj_file: &File<'_>, section: &ObjSection, split_meta: Option<&SplitMeta>, + name_counts: &mut HashMap, ) -> Result> { let mut result = Vec::::new(); for symbol in obj_file.symbols() { @@ -179,8 +186,14 @@ fn symbols_by_section( } if result.is_empty() { // Dummy symbol for empty sections + *name_counts.entry(section.name.clone()).or_insert(0) += 1; + let current_count: u32 = *name_counts.get(§ion.name).unwrap(); result.push(ObjSymbol { - name: format!("[{}]", section.name), + name: if current_count > 1 { + format!("[{} ({})]", section.name, current_count) + } else { + format!("[{}]", section.name) + }, demangled_name: None, address: 0, section_address: 0, @@ -635,9 +648,15 @@ pub fn parse(data: &[u8], config: &DiffObjConfig) -> Result { let arch = new_arch(&obj_file)?; let split_meta = split_meta(&obj_file)?; let mut sections = filter_sections(&obj_file, split_meta.as_ref())?; + let mut name_counts: HashMap = HashMap::new(); for section in &mut sections { - section.symbols = - symbols_by_section(arch.as_ref(), &obj_file, section, split_meta.as_ref())?; + section.symbols = symbols_by_section( + arch.as_ref(), + &obj_file, + section, + split_meta.as_ref(), + &mut name_counts, + )?; section.relocations = relocations_by_section(arch.as_ref(), &obj_file, section, split_meta.as_ref())?; }