Skip to content

Commit 1e44f73

Browse files
committed
Add simple symbol search & handle symbol relocation addends
1 parent b7b177a commit 1e44f73

File tree

6 files changed

+78
-45
lines changed

6 files changed

+78
-45
lines changed

src/diff.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ fn resolve_branches(vec: &mut [ObjInsDiff]) {
181181
}
182182
}
183183

184+
fn address_eq(left: &ObjSymbol, right: &ObjSymbol) -> bool {
185+
left.address as i64 + left.addend == right.address as i64 + right.addend
186+
}
187+
184188
fn reloc_eq(left_reloc: Option<&ObjReloc>, right_reloc: Option<&ObjReloc>) -> bool {
185189
if let (Some(left), Some(right)) = (left_reloc, right_reloc) {
186190
if left.kind != right.kind {
@@ -190,7 +194,7 @@ fn reloc_eq(left_reloc: Option<&ObjReloc>, right_reloc: Option<&ObjReloc>) -> bo
190194
match (&left.target_section, &right.target_section) {
191195
(Some(sl), Some(sr)) => {
192196
// Match if section and name or address match
193-
sl == sr && (name_matches || left.target.address == right.target.address)
197+
sl == sr && (name_matches || address_eq(&left.target, &right.target))
194198
}
195199
(Some(_), None) => false,
196200
(None, Some(_)) => {

src/obj/elf.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn to_obj_section_kind(kind: SectionKind) -> ObjSectionKind {
2222
}
2323
}
2424

25-
fn to_obj_symbol(obj_file: &File<'_>, symbol: &Symbol<'_, '_>) -> Result<ObjSymbol> {
25+
fn to_obj_symbol(obj_file: &File<'_>, symbol: &Symbol<'_, '_>, addend: i64) -> Result<ObjSymbol> {
2626
let mut name = symbol.name().context("Failed to process symbol name")?;
2727
if name.is_empty() {
2828
println!("Found empty sym: {:?}", symbol);
@@ -56,6 +56,7 @@ fn to_obj_symbol(obj_file: &File<'_>, symbol: &Symbol<'_, '_>) -> Result<ObjSymb
5656
size: symbol.size(),
5757
size_known: symbol.size() != 0,
5858
flags,
59+
addend,
5960
diff_symbol: None,
6061
instructions: vec![],
6162
match_percent: 0.0,
@@ -118,7 +119,7 @@ fn symbols_by_section(obj_file: &File<'_>, section: &ObjSection) -> Result<Vec<O
118119
continue;
119120
}
120121
}
121-
result.push(to_obj_symbol(obj_file, &symbol)?);
122+
result.push(to_obj_symbol(obj_file, &symbol, 0)?);
122123
}
123124
}
124125
}
@@ -140,7 +141,7 @@ fn common_symbols(obj_file: &File<'_>) -> Result<Vec<ObjSymbol>> {
140141
let mut result = Vec::<ObjSymbol>::new();
141142
for symbol in obj_file.symbols() {
142143
if symbol.is_common() {
143-
result.push(to_obj_symbol(obj_file, &symbol)?);
144+
result.push(to_obj_symbol(obj_file, &symbol, 0)?);
144145
}
145146
}
146147
Ok(result)
@@ -169,21 +170,22 @@ fn find_section_symbol(
169170
}
170171
continue;
171172
}
172-
return to_obj_symbol(obj_file, &symbol);
173+
return to_obj_symbol(obj_file, &symbol, 0);
173174
}
174175
let (name, offset) = closest_symbol
175176
.and_then(|s| s.name().map(|n| (n, s.address())).ok())
176177
.or_else(|| section.name().map(|n| (n, section.address())).ok())
177178
.unwrap_or(("<unknown>", 0));
178179
let offset_addr = address - offset;
179180
Ok(ObjSymbol {
180-
name: if offset_addr > 0 { format!("{}+{:#X}", name, address) } else { name.to_string() },
181+
name: name.to_string(),
181182
demangled_name: None,
182183
address,
183184
section_address: address - section.address(),
184185
size: 0,
185186
size_known: false,
186187
flags: Default::default(),
188+
addend: offset_addr as i64,
187189
diff_symbol: None,
188190
instructions: vec![],
189191
match_percent: 0.0,
@@ -257,7 +259,7 @@ fn relocations_by_section(
257259
// println!("Reloc: {:?}, symbol: {:?}", reloc, symbol);
258260
let target = match symbol.kind() {
259261
SymbolKind::Text | SymbolKind::Data | SymbolKind::Unknown => {
260-
to_obj_symbol(obj_file, &symbol)
262+
to_obj_symbol(obj_file, &symbol, reloc.addend())
261263
}
262264
SymbolKind::Section => {
263265
let addend = if reloc.has_implicit_addend() {

src/obj/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub struct ObjSymbol {
101101
pub size: u64,
102102
pub size_known: bool,
103103
pub flags: ObjSymbolFlagSet,
104+
pub addend: i64,
104105

105106
// Diff
106107
pub diff_symbol: Option<String>,

src/views/function_diff.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::default::Default;
22

33
use cwdemangle::demangle;
4-
use egui::{text::LayoutJob, Color32, FontFamily, FontId, Label, Sense, TextFormat};
4+
use egui::{text::LayoutJob, Color32, Label, Sense};
55
use egui_extras::{Size, StripBuilder, TableBuilder};
66
use ppc750cl::Argument;
77

@@ -11,52 +11,50 @@ use crate::{
1111
ObjInfo, ObjIns, ObjInsArg, ObjInsArgDiff, ObjInsDiff, ObjInsDiffKind, ObjReloc,
1212
ObjRelocKind, ObjSymbol,
1313
},
14-
views::symbol_diff::match_color_for_symbol,
14+
views::{symbol_diff::match_color_for_symbol, write_text, COLOR_RED, FONT_SIZE},
1515
};
1616

17-
const FONT_SIZE: f32 = 14.0;
18-
const FONT_ID: FontId = FontId::new(FONT_SIZE, FontFamily::Monospace);
19-
20-
const COLOR_RED: Color32 = Color32::from_rgb(200, 40, 41);
21-
22-
fn write_text(str: &str, color: Color32, job: &mut LayoutJob) {
23-
job.append(str, 0.0, TextFormat { font_id: FONT_ID, color, ..Default::default() });
17+
fn write_reloc_name(reloc: &ObjReloc, color: Color32, job: &mut LayoutJob) {
18+
let name = reloc.target.demangled_name.as_ref().unwrap_or(&reloc.target.name);
19+
write_text(name, Color32::LIGHT_GRAY, job);
20+
if reloc.target.addend != 0 {
21+
write_text(&format!("+{:X}", reloc.target.addend), color, job);
22+
}
2423
}
2524

26-
fn write_reloc(reloc: &ObjReloc, job: &mut LayoutJob) {
27-
let name = reloc.target.demangled_name.as_ref().unwrap_or(&reloc.target.name);
25+
fn write_reloc(reloc: &ObjReloc, color: Color32, job: &mut LayoutJob) {
2826
match reloc.kind {
2927
ObjRelocKind::PpcAddr16Lo => {
30-
write_text(name, Color32::LIGHT_GRAY, job);
31-
write_text("@l", Color32::GRAY, job);
28+
write_reloc_name(reloc, color, job);
29+
write_text("@l", color, job);
3230
}
3331
ObjRelocKind::PpcAddr16Hi => {
34-
write_text(name, Color32::LIGHT_GRAY, job);
35-
write_text("@h", Color32::GRAY, job);
32+
write_reloc_name(reloc, color, job);
33+
write_text("@h", color, job);
3634
}
3735
ObjRelocKind::PpcAddr16Ha => {
38-
write_text(name, Color32::LIGHT_GRAY, job);
39-
write_text("@ha", Color32::GRAY, job);
36+
write_reloc_name(reloc, color, job);
37+
write_text("@ha", color, job);
4038
}
4139
ObjRelocKind::PpcEmbSda21 => {
42-
write_text(name, Color32::LIGHT_GRAY, job);
43-
write_text("@sda21", Color32::GRAY, job);
40+
write_reloc_name(reloc, color, job);
41+
write_text("@sda21", color, job);
4442
}
4543
ObjRelocKind::MipsHi16 => {
46-
write_text("%hi(", Color32::GRAY, job);
47-
write_text(name, Color32::LIGHT_GRAY, job);
48-
write_text(")", Color32::GRAY, job);
44+
write_text("%hi(", color, job);
45+
write_reloc_name(reloc, color, job);
46+
write_text(")", color, job);
4947
}
5048
ObjRelocKind::MipsLo16 => {
51-
write_text("%lo(", Color32::GRAY, job);
52-
write_text(name, Color32::LIGHT_GRAY, job);
53-
write_text(")", Color32::GRAY, job);
49+
write_text("%lo(", color, job);
50+
write_reloc_name(reloc, color, job);
51+
write_text(")", color, job);
5452
}
5553
ObjRelocKind::Absolute
5654
| ObjRelocKind::PpcRel24
5755
| ObjRelocKind::PpcRel14
5856
| ObjRelocKind::Mips26 => {
59-
write_text(name, Color32::LIGHT_GRAY, job);
57+
write_reloc_name(reloc, color, job);
6058
}
6159
};
6260
}
@@ -113,10 +111,10 @@ fn write_ins(
113111
}
114112
},
115113
ObjInsArg::Reloc => {
116-
write_reloc(ins.reloc.as_ref().unwrap(), job);
114+
write_reloc(ins.reloc.as_ref().unwrap(), base_color, job);
117115
}
118116
ObjInsArg::RelocWithBase => {
119-
write_reloc(ins.reloc.as_ref().unwrap(), job);
117+
write_reloc(ins.reloc.as_ref().unwrap(), base_color, job);
120118
write_text("(", base_color, job);
121119
writing_offset = true;
122120
continue;

src/views/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
use egui::{text::LayoutJob, Color32, FontFamily, FontId, TextFormat};
2+
13
pub(crate) mod config;
24
pub(crate) mod function_diff;
35
pub(crate) mod jobs;
46
pub(crate) mod symbol_diff;
7+
8+
const FONT_SIZE: f32 = 14.0;
9+
const FONT_ID: FontId = FontId::new(FONT_SIZE, FontFamily::Monospace);
10+
11+
const COLOR_RED: Color32 = Color32::from_rgb(200, 40, 41);
12+
13+
fn write_text(str: &str, color: Color32, job: &mut LayoutJob) {
14+
job.append(str, 0.0, TextFormat { font_id: FONT_ID, color, ..Default::default() });
15+
}

src/views/symbol_diff.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use egui::{
2-
text::LayoutJob, CollapsingHeader, Color32, FontFamily, FontId, Rgba, ScrollArea,
3-
SelectableLabel, TextFormat, Ui, Widget,
2+
text::LayoutJob, CollapsingHeader, Color32, Rgba, ScrollArea,
3+
SelectableLabel, Ui, Widget,
44
};
55
use egui_extras::{Size, StripBuilder};
66

77
use crate::{
88
app::{View, ViewState},
99
jobs::objdiff::BuildStatus,
1010
obj::{ObjInfo, ObjSymbol, ObjSymbolFlags},
11+
views::write_text,
1112
};
1213

1314
pub fn match_color_for_symbol(symbol: &ObjSymbol) -> Color32 {
@@ -20,13 +21,6 @@ pub fn match_color_for_symbol(symbol: &ObjSymbol) -> Color32 {
2021
}
2122
}
2223

23-
const FONT_SIZE: f32 = 14.0;
24-
const FONT_ID: FontId = FontId::new(FONT_SIZE, FontFamily::Monospace);
25-
26-
fn write_text(str: &str, color: Color32, job: &mut LayoutJob) {
27-
job.append(str, 0.0, TextFormat { font_id: FONT_ID, color, ..Default::default() });
28-
}
29-
3024
fn symbol_context_menu_ui(ui: &mut Ui, symbol: &ObjSymbol) {
3125
ui.scope(|ui| {
3226
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
@@ -104,14 +98,28 @@ fn symbol_ui(
10498
}
10599
}
106100

101+
fn symbol_matches_search(symbol: &ObjSymbol, search_str: &str) -> bool {
102+
search_str.is_empty()
103+
|| symbol.name.contains(search_str)
104+
|| symbol
105+
.demangled_name
106+
.as_ref()
107+
.map(|s| s.to_ascii_lowercase().contains(search_str))
108+
.unwrap_or(false)
109+
}
110+
107111
fn symbol_list_ui(
108112
ui: &mut Ui,
109113
obj: &ObjInfo,
110114
highlighted_symbol: &mut Option<String>,
111115
selected_symbol: &mut Option<String>,
112116
current_view: &mut View,
113117
reverse_function_order: bool,
118+
search: &mut String,
114119
) {
120+
ui.text_edit_singleline(search);
121+
let lower_search = search.to_ascii_lowercase();
122+
115123
ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| {
116124
ui.scope(|ui| {
117125
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
@@ -131,6 +139,9 @@ fn symbol_list_ui(
131139
.show(ui, |ui| {
132140
if section.name == ".text" && reverse_function_order {
133141
for symbol in section.symbols.iter().rev() {
142+
if !symbol_matches_search(symbol, &lower_search) {
143+
continue;
144+
}
134145
symbol_ui(
135146
ui,
136147
symbol,
@@ -141,6 +152,9 @@ fn symbol_list_ui(
141152
}
142153
} else {
143154
for symbol in &section.symbols {
155+
if !symbol_matches_search(symbol, &lower_search) {
156+
continue;
157+
}
144158
symbol_ui(
145159
ui,
146160
symbol,
@@ -168,11 +182,12 @@ fn build_log_ui(ui: &mut Ui, status: &BuildStatus) {
168182
}
169183

170184
pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) {
171-
if let (Some(result), highlighted_symbol, selected_symbol, current_view) = (
185+
if let (Some(result), highlighted_symbol, selected_symbol, current_view, search) = (
172186
&view_state.build,
173187
&mut view_state.highlighted_symbol,
174188
&mut view_state.selected_symbol,
175189
&mut view_state.current_view,
190+
&mut view_state.search,
176191
) {
177192
StripBuilder::new(ui).size(Size::exact(40.0)).size(Size::remainder()).vertical(
178193
|mut strip| {
@@ -223,6 +238,7 @@ pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) {
223238
selected_symbol,
224239
current_view,
225240
view_state.reverse_fn_order,
241+
search,
226242
);
227243
});
228244
}
@@ -241,6 +257,7 @@ pub fn symbol_diff_ui(ui: &mut Ui, view_state: &mut ViewState) {
241257
selected_symbol,
242258
current_view,
243259
view_state.reverse_fn_order,
260+
search,
244261
);
245262
});
246263
}

0 commit comments

Comments
 (0)