forked from encounter/objdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.rs
More file actions
351 lines (319 loc) · 11.9 KB
/
diff.rs
File metadata and controls
351 lines (319 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#![allow(clippy::needless_lifetimes)] // Generated serde code
use alloc::{
string::{String, ToString},
vec,
vec::Vec,
};
use core::fmt::Write;
use anyhow::Result;
use crate::{
diff::{self, DiffObjConfig, display::InstructionPart},
obj::{self, Object, SymbolFlag},
};
// Protobuf diff types
include!(concat!(env!("OUT_DIR"), "/objdiff.diff.rs"));
#[cfg(feature = "serde")]
include!(concat!(env!("OUT_DIR"), "/objdiff.diff.serde.rs"));
impl DiffResult {
pub fn new(
left: Option<(&Object, &diff::ObjectDiff)>,
right: Option<(&Object, &diff::ObjectDiff)>,
diff_config: &DiffObjConfig,
) -> Result<Self> {
Ok(Self {
left: left.map(|(obj, diff)| DiffObject::new(obj, diff, diff_config)).transpose()?,
right: right.map(|(obj, diff)| DiffObject::new(obj, diff, diff_config)).transpose()?,
})
}
}
impl DiffObject {
pub fn new(obj: &Object, diff: &diff::ObjectDiff, diff_config: &DiffObjConfig) -> Result<Self> {
let mut sections = Vec::with_capacity(obj.sections.len());
for (section_idx, section) in obj.sections.iter().enumerate() {
let section_diff = &diff.sections[section_idx];
sections.push(DiffSection::new(obj, section, section_diff));
}
let mut symbols = Vec::with_capacity(obj.symbols.len());
for (symbol_idx, symbol) in obj.symbols.iter().enumerate() {
let symbol_diff = &diff.symbols[symbol_idx];
symbols.push(DiffSymbol::new(obj, symbol_idx, symbol, symbol_diff, diff_config)?);
}
Ok(Self { sections, symbols })
}
}
impl DiffSection {
pub fn new(obj: &Object, section: &obj::Section, section_diff: &diff::SectionDiff) -> Self {
Self {
name: section.name.clone(),
kind: DiffSectionKind::from(section.kind) as i32,
size: section.size,
address: section.address,
match_percent: section_diff.match_percent,
data_diff: section_diff.data_diff.iter().map(DiffDataSegment::from).collect(),
reloc_diff: section_diff
.reloc_diff
.iter()
.map(|r| DiffDataRelocation::new(obj, r))
.collect(),
}
}
}
impl From<obj::SectionKind> for DiffSectionKind {
fn from(value: obj::SectionKind) -> Self {
match value {
obj::SectionKind::Unknown => DiffSectionKind::SectionUnknown,
obj::SectionKind::Code => DiffSectionKind::SectionCode,
obj::SectionKind::Data => DiffSectionKind::SectionData,
obj::SectionKind::Bss => DiffSectionKind::SectionBss,
obj::SectionKind::Common => DiffSectionKind::SectionCommon,
}
}
}
impl DiffSymbol {
pub fn new(
obj: &Object,
symbol_idx: usize,
symbol: &obj::Symbol,
symbol_diff: &diff::SymbolDiff,
diff_config: &DiffObjConfig,
) -> Result<Self> {
// Convert instruction rows
let instructions = symbol_diff
.instruction_rows
.iter()
.map(|row| DiffInstructionRow::new(obj, symbol_idx, row, diff_config))
.collect::<Result<Vec<_>>>()?;
// Convert data diff - flatten DataDiffRow segments into a single list
let data_diff: Vec<DiffDataSegment> = symbol_diff
.data_rows
.iter()
.flat_map(|row| row.segments.iter().map(DiffDataSegment::from))
.collect();
Ok(Self {
// Symbol metadata
name: symbol.name.clone(),
demangled_name: symbol.demangled_name.clone(),
address: symbol.address,
size: symbol.size,
flags: symbol_flags(&symbol.flags),
kind: DiffSymbolKind::from(symbol.kind) as i32,
// Diff information
target_symbol: symbol_diff.target_symbol.map(|i| i as u32),
match_percent: symbol_diff.match_percent,
instructions,
data_diff,
})
}
}
impl From<obj::SymbolKind> for DiffSymbolKind {
fn from(value: obj::SymbolKind) -> Self {
match value {
obj::SymbolKind::Unknown => DiffSymbolKind::SymbolUnknown,
obj::SymbolKind::Function => DiffSymbolKind::SymbolFunction,
obj::SymbolKind::Object => DiffSymbolKind::SymbolObject,
obj::SymbolKind::Section => DiffSymbolKind::SymbolSection,
}
}
}
fn symbol_flags(flags: &obj::SymbolFlagSet) -> Option<DiffSymbolFlags> {
Some(DiffSymbolFlags {
global: flags.contains(SymbolFlag::Global),
local: flags.contains(SymbolFlag::Local),
weak: flags.contains(SymbolFlag::Weak),
common: flags.contains(SymbolFlag::Common),
hidden: flags.contains(SymbolFlag::Hidden),
ignored: flags.contains(SymbolFlag::Ignored),
size_inferred: flags.contains(SymbolFlag::SizeInferred),
})
}
impl DiffInstructionRow {
pub fn new(
obj: &Object,
symbol_idx: usize,
row: &diff::InstructionDiffRow,
diff_config: &DiffObjConfig,
) -> Result<Self> {
let instruction = if let Some(ins_ref) = row.ins_ref {
let resolved = obj.resolve_instruction_ref(symbol_idx, ins_ref);
resolved.map(|r| DiffInstruction::new(obj, r, diff_config)).transpose()?
} else {
None
};
let arg_diff =
row.arg_diff.iter().map(|d| DiffInstructionArgDiff { diff_index: d.get() }).collect();
Ok(Self { diff_kind: DiffKind::from(row.kind) as i32, instruction, arg_diff })
}
}
impl DiffInstruction {
pub fn new(
obj: &Object,
resolved: obj::ResolvedInstructionRef,
diff_config: &DiffObjConfig,
) -> Result<Self> {
let mut formatted = String::new();
let mut parts = vec![];
let separator = diff_config.separator();
// Use the arch's display_instruction to get formatted parts
obj.arch.display_instruction(resolved, diff_config, &mut |part| {
write_instruction_part(&mut formatted, &part, separator, resolved.relocation);
parts
.push(DiffInstructionPart { part: Some(diff_instruction_part::Part::from(&part)) });
Ok(())
})?;
let relocation = resolved.relocation.map(|r| DiffRelocation::new(obj, r));
let line_number = resolved
.section
.line_info
.range(..=resolved.ins_ref.address)
.last()
.map(|(_, &line)| line);
Ok(Self {
address: resolved.ins_ref.address,
size: resolved.ins_ref.size as u32,
formatted,
parts,
relocation,
branch_dest: resolved.ins_ref.branch_dest,
line_number,
})
}
}
fn write_instruction_part(
out: &mut String,
part: &InstructionPart,
separator: &str,
reloc: Option<obj::ResolvedRelocation>,
) {
match part {
InstructionPart::Basic(s) => out.push_str(s),
InstructionPart::Opcode(s, _) => {
out.push_str(s);
out.push(' ');
}
InstructionPart::Arg(arg) => match arg {
obj::InstructionArg::Value(v) => {
let _ = write!(out, "{}", v);
}
obj::InstructionArg::Reloc => {
if let Some(resolved) = reloc {
out.push_str(&resolved.symbol.name);
if resolved.relocation.addend != 0 {
if resolved.relocation.addend < 0 {
let _ = write!(out, "-{:#x}", -resolved.relocation.addend);
} else {
let _ = write!(out, "+{:#x}", resolved.relocation.addend);
}
}
}
}
obj::InstructionArg::BranchDest(dest) => {
let _ = write!(out, "{:#x}", dest);
}
},
InstructionPart::Separator => out.push_str(separator),
}
}
impl diff_instruction_part::Part {
fn from(part: &InstructionPart) -> Self {
match part {
InstructionPart::Basic(s) => diff_instruction_part::Part::Basic(s.to_string()),
InstructionPart::Opcode(mnemonic, opcode) => {
diff_instruction_part::Part::Opcode(DiffOpcode {
mnemonic: mnemonic.to_string(),
opcode: *opcode as u32,
})
}
InstructionPart::Arg(arg) => {
diff_instruction_part::Part::Arg(DiffInstructionArg::from(arg))
}
InstructionPart::Separator => diff_instruction_part::Part::Separator(true),
}
}
}
impl From<&obj::InstructionArg<'_>> for DiffInstructionArg {
fn from(arg: &obj::InstructionArg) -> Self {
let arg = match arg {
obj::InstructionArg::Value(v) => match v {
obj::InstructionArgValue::Signed(v) => diff_instruction_arg::Arg::Signed(*v),
obj::InstructionArgValue::Unsigned(v) => diff_instruction_arg::Arg::Unsigned(*v),
obj::InstructionArgValue::Opaque(v) => {
diff_instruction_arg::Arg::Opaque(v.to_string())
}
},
obj::InstructionArg::Reloc => diff_instruction_arg::Arg::Reloc(true),
obj::InstructionArg::BranchDest(dest) => diff_instruction_arg::Arg::BranchDest(*dest),
};
DiffInstructionArg { arg: Some(arg) }
}
}
impl DiffRelocation {
pub fn new(obj: &Object, resolved: obj::ResolvedRelocation) -> Self {
let type_val = relocation_type(resolved.relocation.flags);
let type_name = obj
.arch
.reloc_name(resolved.relocation.flags)
.map(|s| s.to_string())
.unwrap_or_default();
Self {
r#type: type_val,
type_name,
target_symbol: resolved.relocation.target_symbol as u32,
addend: resolved.relocation.addend,
}
}
}
fn relocation_type(flags: obj::RelocationFlags) -> u32 {
match flags {
obj::RelocationFlags::Elf(r_type) => r_type,
obj::RelocationFlags::Coff(typ) => typ as u32,
}
}
impl From<diff::InstructionDiffKind> for DiffKind {
fn from(value: diff::InstructionDiffKind) -> Self {
match value {
diff::InstructionDiffKind::None => DiffKind::DiffNone,
diff::InstructionDiffKind::OpMismatch => DiffKind::DiffOpMismatch,
diff::InstructionDiffKind::ArgMismatch => DiffKind::DiffArgMismatch,
diff::InstructionDiffKind::Replace => DiffKind::DiffReplace,
diff::InstructionDiffKind::Delete => DiffKind::DiffDelete,
diff::InstructionDiffKind::Insert => DiffKind::DiffInsert,
}
}
}
impl From<diff::DataDiffKind> for DiffKind {
fn from(value: diff::DataDiffKind) -> Self {
match value {
diff::DataDiffKind::None => DiffKind::DiffNone,
diff::DataDiffKind::Replace => DiffKind::DiffReplace,
diff::DataDiffKind::Delete => DiffKind::DiffDelete,
diff::DataDiffKind::Insert => DiffKind::DiffInsert,
}
}
}
impl From<&diff::DataDiff> for DiffDataSegment {
fn from(value: &diff::DataDiff) -> Self {
Self {
kind: DiffKind::from(value.kind) as i32,
data: value.data.clone(),
size: value.size as u64,
}
}
}
impl DiffDataRelocation {
pub fn new(obj: &Object, value: &diff::DataRelocationDiff) -> Self {
let type_val = relocation_type(value.reloc.flags);
let type_name =
obj.arch.reloc_name(value.reloc.flags).map(|s| s.to_string()).unwrap_or_default();
Self {
relocation: Some(DiffRelocation {
r#type: type_val,
type_name,
target_symbol: value.reloc.target_symbol as u32,
addend: value.reloc.addend,
}),
kind: DiffKind::from(value.kind) as i32,
start: value.range.start,
end: value.range.end,
}
}
}