Skip to content

Commit 9caec5d

Browse files
committed
symcheck: Store the section name in SymInfo if available
Currently `SymInfo` stores a `Section`, which is just an index: SymInfo { section: Section( SectionIndex( 539, ), ), ... }, Look up and store the section name instead if possible, with a fallback to the `Section` debug printing. This makes output more clear and will allow us to filter by section name.
1 parent 87a66ec commit 9caec5d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

crates/symbol-check/src/main.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::process::{Command, Stdio};
99

1010
use object::read::archive::{ArchiveFile, ArchiveMember};
1111
use object::{
12-
File as ObjFile, Object, ObjectSymbol, Symbol, SymbolKind, SymbolScope, SymbolSection,
12+
File as ObjFile, Object, ObjectSection, ObjectSymbol, Symbol, SymbolKind, SymbolScope,
1313
};
1414
use serde_json::Value;
1515

@@ -154,7 +154,7 @@ struct SymInfo {
154154
name: String,
155155
kind: SymbolKind,
156156
scope: SymbolScope,
157-
section: SymbolSection,
157+
section: String,
158158
is_undefined: bool,
159159
is_global: bool,
160160
is_local: bool,
@@ -165,12 +165,22 @@ struct SymInfo {
165165
}
166166

167167
impl SymInfo {
168-
fn new(sym: &Symbol, member: &ArchiveMember) -> Self {
168+
fn new(sym: &Symbol, obj: &ObjFile, member: &ArchiveMember) -> Self {
169+
// Include the section name if possible. Fall back to the `Section` debug impl if not.
170+
let section = sym.section();
171+
let section_name = sym
172+
.section()
173+
.index()
174+
.and_then(|idx| obj.section_by_index(idx).ok())
175+
.and_then(|sec| sec.name().ok())
176+
.map(ToString::to_string)
177+
.unwrap_or_else(|| format!("{section:?}"));
178+
169179
Self {
170180
name: sym.name().expect("missing name").to_owned(),
171181
kind: sym.kind(),
172182
scope: sym.scope(),
173-
section: sym.section(),
183+
section: section_name,
174184
is_undefined: sym.is_undefined(),
175185
is_global: sym.is_global(),
176186
is_local: sym.is_local(),
@@ -192,13 +202,13 @@ fn verify_no_duplicates(archive: &Archive) {
192202
let mut dups = Vec::new();
193203
let mut found_any = false;
194204

195-
archive.for_each_symbol(|symbol, member| {
205+
archive.for_each_symbol(|symbol, obj, member| {
196206
// Only check defined globals
197207
if !symbol.is_global() || symbol.is_undefined() {
198208
return;
199209
}
200210

201-
let sym = SymInfo::new(&symbol, member);
211+
let sym = SymInfo::new(&symbol, obj, member);
202212

203213
// x86-32 includes multiple copies of thunk symbols
204214
if sym.name.starts_with("__x86.get_pc_thunk") {
@@ -244,15 +254,15 @@ fn verify_core_symbols(archive: &Archive) {
244254
let mut undefined = Vec::new();
245255
let mut has_symbols = false;
246256

247-
archive.for_each_symbol(|symbol, member| {
257+
archive.for_each_symbol(|symbol, obj, member| {
248258
has_symbols = true;
249259

250260
// Find only symbols from `core`
251261
if !symbol.name().unwrap().contains("_ZN4core") {
252262
return;
253263
}
254264

255-
let sym = SymInfo::new(&symbol, member);
265+
let sym = SymInfo::new(&symbol, obj, member);
256266
if sym.is_undefined {
257267
undefined.push(sym);
258268
} else {
@@ -304,9 +314,9 @@ impl Archive {
304314
}
305315

306316
/// For a given archive, do something with each symbol.
307-
fn for_each_symbol(&self, mut f: impl FnMut(Symbol, &ArchiveMember)) {
317+
fn for_each_symbol(&self, mut f: impl FnMut(Symbol, &ObjFile, &ArchiveMember)) {
308318
self.for_each_object(|obj, member| {
309-
obj.symbols().for_each(|sym| f(sym, member));
319+
obj.symbols().for_each(|sym| f(sym, &obj, member));
310320
});
311321
}
312322
}

0 commit comments

Comments
 (0)