Skip to content

Commit f8d534d

Browse files
committed
Merge branch 'main' into fork/LagoLunatic/data-symbol-diff
# Conflicts: # objdiff-cli/src/cmd/report.rs
2 parents a9f69da + 6fb4bb8 commit f8d534d

File tree

15 files changed

+195
-80
lines changed

15 files changed

+195
-80
lines changed

Cargo.lock

Lines changed: 14 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

objdiff-cli/src/cmd/report.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,16 @@ fn report_object(
177177
.target_path
178178
.as_ref()
179179
.map(|p| {
180-
obj::read::read(p.as_ref(), diff_config).with_context(|| format!("Failed to open {p}"))
180+
obj::read::read(p.as_ref(), diff_config, diff::DiffSide::Target)
181+
.with_context(|| format!("Failed to open {p}"))
181182
})
182183
.transpose()?;
183184
let base = object
184185
.base_path
185186
.as_ref()
186187
.map(|p| {
187-
obj::read::read(p.as_ref(), diff_config).with_context(|| format!("Failed to open {p}"))
188+
obj::read::read(p.as_ref(), diff_config, diff::DiffSide::Base)
189+
.with_context(|| format!("Failed to open {p}"))
188190
})
189191
.transpose()?;
190192
let result =

objdiff-core/src/arch/mips.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rabbitizer::{
1212

1313
use crate::{
1414
arch::{Arch, RelocationOverride, RelocationOverrideTarget},
15-
diff::{DiffObjConfig, MipsAbi, MipsInstrCategory, display::InstructionPart},
15+
diff::{DiffObjConfig, DiffSide, MipsAbi, MipsInstrCategory, display::InstructionPart},
1616
obj::{
1717
InstructionArg, InstructionArgValue, InstructionRef, Relocation, RelocationFlags,
1818
ResolvedInstructionRef, ResolvedRelocation, Section, Symbol, SymbolFlag, SymbolFlagSet,
@@ -27,6 +27,7 @@ pub struct ArchMips {
2727
pub ri_gp_value: i32,
2828
pub paired_relocations: Vec<BTreeMap<u64, i64>>,
2929
pub ignored_symbols: BTreeSet<usize>,
30+
pub diff_side: DiffSide,
3031
}
3132

3233
const EF_MIPS_ABI: u32 = 0x0000F000;
@@ -38,7 +39,7 @@ const EF_MIPS_MACH_5900: u32 = 0x00920000;
3839
const R_MIPS15_S3: u32 = 119;
3940

4041
impl ArchMips {
41-
pub fn new(object: &object::File) -> Result<Self> {
42+
pub fn new(object: &object::File, diff_side: DiffSide) -> Result<Self> {
4243
let mut abi = Abi::O32;
4344
let mut isa_extension = None;
4445
match object.flags() {
@@ -124,7 +125,11 @@ impl ArchMips {
124125
let Ok(name) = obj_symbol.name() else { continue };
125126
if let Some(prefix) = name.strip_suffix(".NON_MATCHING") {
126127
ignored_symbols.insert(obj_symbol.index().0);
127-
if let Some(target_symbol) = object.symbol_by_name(prefix) {
128+
// Only remove the prefixless symbols if we are on the Base side of the diff,
129+
// to allow diffing against target objects that contain `.NON_MATCHING` markers.
130+
if diff_side == DiffSide::Base
131+
&& let Some(target_symbol) = object.symbol_by_name(prefix)
132+
{
128133
ignored_symbols.insert(target_symbol.index().0);
129134
}
130135
}
@@ -137,6 +142,7 @@ impl ArchMips {
137142
ri_gp_value,
138143
paired_relocations,
139144
ignored_symbols,
145+
diff_side,
140146
})
141147
}
142148

objdiff-core/src/arch/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use object::Endian as _;
1717

1818
use crate::{
1919
diff::{
20-
DiffObjConfig,
20+
DiffObjConfig, DiffSide,
2121
display::{ContextItem, HoverItem, InstructionPart},
2222
},
2323
obj::{
@@ -418,15 +418,18 @@ pub trait Arch: Any + Debug + Send + Sync {
418418
}
419419
}
420420

421-
pub fn new_arch(object: &object::File) -> Result<Box<dyn Arch>> {
421+
pub fn new_arch(object: &object::File, diff_side: DiffSide) -> Result<Box<dyn Arch>> {
422422
use object::Object as _;
423+
// Avoid unused warnings on non-mips builds
424+
let _ = diff_side;
425+
423426
Ok(match object.architecture() {
424427
#[cfg(feature = "ppc")]
425428
object::Architecture::PowerPc | object::Architecture::PowerPc64 => {
426429
Box::new(ppc::ArchPpc::new(object)?)
427430
}
428431
#[cfg(feature = "mips")]
429-
object::Architecture::Mips => Box::new(mips::ArchMips::new(object)?),
432+
object::Architecture::Mips => Box::new(mips::ArchMips::new(object, diff_side)?),
430433
#[cfg(feature = "x86")]
431434
object::Architecture::I386 | object::Architecture::X86_64 => {
432435
Box::new(x86::ArchX86::new(object)?)

objdiff-core/src/diff/display.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ pub enum HoverItem {
379379
}
380380

381381
pub fn symbol_context(obj: &Object, symbol_index: usize) -> Vec<ContextItem> {
382-
let symbol = &obj.symbols[symbol_index];
382+
let Some(symbol) = obj.symbols.get(symbol_index) else {
383+
return Vec::new();
384+
};
383385
let mut out = Vec::new();
384386
out.push(ContextItem::Copy { value: symbol.name.clone(), label: None });
385387
if let Some(name) = &symbol.demangled_name {
@@ -403,7 +405,9 @@ pub fn symbol_hover(
403405
addend: i64,
404406
override_color: Option<HoverItemColor>,
405407
) -> Vec<HoverItem> {
406-
let symbol = &obj.symbols[symbol_index];
408+
let Some(symbol) = obj.symbols.get(symbol_index) else {
409+
return Vec::new();
410+
};
407411
let addend_str = match addend.cmp(&0i64) {
408412
Ordering::Greater => format!("+{addend:x}"),
409413
Ordering::Less => format!("-{:x}", -addend),

objdiff-core/src/diff/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,11 @@ fn find_section(
818818
s.kind == section_kind && s.name == name && !matches.iter().any(|m| m.right == Some(i))
819819
})
820820
}
821+
822+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
823+
pub enum DiffSide {
824+
/// The target/expected side of the diff.
825+
Target,
826+
/// The base side of the diff.
827+
Base,
828+
}

objdiff-core/src/jobs/objdiff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use typed_path::Utf8PlatformPathBuf;
66

77
use crate::{
88
build::{BuildConfig, BuildStatus, run_make},
9-
diff::{DiffObjConfig, MappingConfig, ObjectDiff, diff_objs},
9+
diff::{DiffObjConfig, DiffSide, MappingConfig, ObjectDiff, diff_objs},
1010
jobs::{Job, JobContext, JobResult, JobState, start_job, update_status},
1111
obj::{Object, read},
1212
};
@@ -117,7 +117,7 @@ fn run_build(
117117
&cancel,
118118
)?;
119119
step_idx += 1;
120-
match read::read(target_path.as_ref(), &config.diff_obj_config) {
120+
match read::read(target_path.as_ref(), &config.diff_obj_config, DiffSide::Target) {
121121
Ok(obj) => Some(obj),
122122
Err(e) => {
123123
first_status = BuildStatus {
@@ -141,7 +141,7 @@ fn run_build(
141141
Some(base_path) if second_status.success => {
142142
update_status(context, format!("Loading base {base_path}"), step_idx, total, &cancel)?;
143143
step_idx += 1;
144-
match read::read(base_path.as_ref(), &config.diff_obj_config) {
144+
match read::read(base_path.as_ref(), &config.diff_obj_config, DiffSide::Base) {
145145
Ok(obj) => Some(obj),
146146
Err(e) => {
147147
second_status = BuildStatus {

objdiff-core/src/obj/read.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use object::{Object as _, ObjectSection as _, ObjectSymbol as _};
1212

1313
use crate::{
1414
arch::{Arch, RelocationOverride, RelocationOverrideTarget, new_arch},
15-
diff::DiffObjConfig,
15+
diff::{DiffObjConfig, DiffSide},
1616
obj::{
1717
FlowAnalysisResult, Object, Relocation, RelocationFlags, Section, SectionData, SectionFlag,
1818
SectionKind, Symbol, SymbolFlag, SymbolFlagSet, SymbolKind,
@@ -74,6 +74,14 @@ fn map_symbol(
7474
{
7575
flags |= SymbolFlag::Hidden;
7676
}
77+
if file.format() == object::BinaryFormat::Coff
78+
&& let Ok(name) = symbol.name()
79+
&& (name.starts_with("except_data_")
80+
|| name.starts_with("__unwind")
81+
|| name.starts_with("__catch"))
82+
{
83+
flags |= SymbolFlag::Hidden;
84+
}
7785

7886
let kind = match symbol.kind() {
7987
object::SymbolKind::Text => SymbolKind::Function,
@@ -967,21 +975,25 @@ fn do_combine_sections(
967975
}
968976

969977
#[cfg(feature = "std")]
970-
pub fn read(obj_path: &std::path::Path, config: &DiffObjConfig) -> Result<Object> {
978+
pub fn read(
979+
obj_path: &std::path::Path,
980+
config: &DiffObjConfig,
981+
diff_side: DiffSide,
982+
) -> Result<Object> {
971983
let (data, timestamp) = {
972984
let file = std::fs::File::open(obj_path)?;
973985
let timestamp = filetime::FileTime::from_last_modification_time(&file.metadata()?);
974986
(unsafe { memmap2::Mmap::map(&file) }?, timestamp)
975987
};
976-
let mut obj = parse(&data, config)?;
988+
let mut obj = parse(&data, config, diff_side)?;
977989
obj.path = Some(obj_path.to_path_buf());
978990
obj.timestamp = Some(timestamp);
979991
Ok(obj)
980992
}
981993

982-
pub fn parse(data: &[u8], config: &DiffObjConfig) -> Result<Object> {
994+
pub fn parse(data: &[u8], config: &DiffObjConfig, diff_side: DiffSide) -> Result<Object> {
983995
let obj_file = object::File::parse(data)?;
984-
let mut arch = new_arch(&obj_file)?;
996+
let mut arch = new_arch(&obj_file, diff_side)?;
985997
let split_meta = parse_split_meta(&obj_file)?;
986998
let (mut sections, section_indices) =
987999
map_sections(arch.as_ref(), &obj_file, split_meta.as_ref())?;

objdiff-core/tests/arch_arm.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ mod common;
66
#[cfg(feature = "arm")]
77
fn read_arm() {
88
let diff_config = diff::DiffObjConfig { ..Default::default() };
9-
let obj = obj::read::parse(include_object!("data/arm/LinkStateItem.o"), &diff_config).unwrap();
9+
let obj = obj::read::parse(
10+
include_object!("data/arm/LinkStateItem.o"),
11+
&diff_config,
12+
diff::DiffSide::Base,
13+
)
14+
.unwrap();
1015
insta::assert_debug_snapshot!(obj);
1116
let symbol_idx =
1217
obj.symbols.iter().position(|s| s.name == "_ZN13LinkStateItem12OnStateLeaveEi").unwrap();
@@ -20,7 +25,9 @@ fn read_arm() {
2025
#[cfg(feature = "arm")]
2126
fn read_thumb() {
2227
let diff_config = diff::DiffObjConfig { ..Default::default() };
23-
let obj = obj::read::parse(include_object!("data/arm/thumb.o"), &diff_config).unwrap();
28+
let obj =
29+
obj::read::parse(include_object!("data/arm/thumb.o"), &diff_config, diff::DiffSide::Base)
30+
.unwrap();
2431
insta::assert_debug_snapshot!(obj);
2532
let symbol_idx = obj
2633
.symbols
@@ -37,7 +44,12 @@ fn read_thumb() {
3744
#[cfg(feature = "arm")]
3845
fn combine_text_sections() {
3946
let diff_config = diff::DiffObjConfig { combine_text_sections: true, ..Default::default() };
40-
let obj = obj::read::parse(include_object!("data/arm/enemy300.o"), &diff_config).unwrap();
47+
let obj = obj::read::parse(
48+
include_object!("data/arm/enemy300.o"),
49+
&diff_config,
50+
diff::DiffSide::Base,
51+
)
52+
.unwrap();
4153
let symbol_idx = obj.symbols.iter().position(|s| s.name == "Enemy300Draw").unwrap();
4254
let diff = diff::code::no_diff_code(&obj, symbol_idx, &diff_config).unwrap();
4355
insta::assert_debug_snapshot!(diff.instruction_rows);

0 commit comments

Comments
 (0)