Skip to content

Commit a06382c

Browse files
authored
Disambiguate dummy symbols (#107)
* Disambiguate dummy symbols * Small formatting improvement * Put HashMap logic into symbol creation
1 parent e013638 commit a06382c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

objdiff-core/src/obj/read.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use std::{collections::HashSet, fs, io::Cursor, mem::size_of, path::Path};
1+
use std::{
2+
collections::{HashMap, HashSet},
3+
fs,
4+
io::Cursor,
5+
mem::size_of,
6+
path::Path,
7+
};
28

39
use anyhow::{anyhow, bail, ensure, Context, Result};
410
use filetime::FileTime;
@@ -147,6 +153,7 @@ fn symbols_by_section(
147153
obj_file: &File<'_>,
148154
section: &ObjSection,
149155
split_meta: Option<&SplitMeta>,
156+
name_counts: &mut HashMap<String, u32>,
150157
) -> Result<Vec<ObjSymbol>> {
151158
let mut result = Vec::<ObjSymbol>::new();
152159
for symbol in obj_file.symbols() {
@@ -179,8 +186,14 @@ fn symbols_by_section(
179186
}
180187
if result.is_empty() {
181188
// Dummy symbol for empty sections
189+
*name_counts.entry(section.name.clone()).or_insert(0) += 1;
190+
let current_count: u32 = *name_counts.get(&section.name).unwrap();
182191
result.push(ObjSymbol {
183-
name: format!("[{}]", section.name),
192+
name: if current_count > 1 {
193+
format!("[{} ({})]", section.name, current_count)
194+
} else {
195+
format!("[{}]", section.name)
196+
},
184197
demangled_name: None,
185198
address: 0,
186199
section_address: 0,
@@ -635,9 +648,15 @@ pub fn parse(data: &[u8], config: &DiffObjConfig) -> Result<ObjInfo> {
635648
let arch = new_arch(&obj_file)?;
636649
let split_meta = split_meta(&obj_file)?;
637650
let mut sections = filter_sections(&obj_file, split_meta.as_ref())?;
651+
let mut name_counts: HashMap<String, u32> = HashMap::new();
638652
for section in &mut sections {
639-
section.symbols =
640-
symbols_by_section(arch.as_ref(), &obj_file, section, split_meta.as_ref())?;
653+
section.symbols = symbols_by_section(
654+
arch.as_ref(),
655+
&obj_file,
656+
section,
657+
split_meta.as_ref(),
658+
&mut name_counts,
659+
)?;
641660
section.relocations =
642661
relocations_by_section(arch.as_ref(), &obj_file, section, split_meta.as_ref())?;
643662
}

0 commit comments

Comments
 (0)