Skip to content

Commit 0bc1b4f

Browse files
run-make-support: add object-based symbol helpers
- `dynamic_symbol_names` - `text_section_global_dynamic_symbol_names` - `global_undefined_dynamic_symbol_names` Also add some missing `#[track_caller]` attributes. Co-authored-by: binarycat <[email protected]>
1 parent c15c970 commit 0bc1b4f

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

Diff for: src/tools/run-make-support/src/symbols.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,44 @@ use std::path::Path;
22

33
use object::{self, Object, ObjectSymbol, SymbolIterator};
44

5-
/// Iterate through the symbols in an object file.
6-
///
7-
/// Uses a callback because `SymbolIterator` does not own its data.
5+
/// Given an [`object::File`], find the exported dynamic symbol names via
6+
/// [`object::Object::exports`]. This does not distinguish between which section the symbols appear
7+
/// in.
8+
#[track_caller]
9+
pub fn exported_dynamic_symbol_names<'file>(file: &'file object::File<'file>) -> Vec<&'file str> {
10+
file.exports()
11+
.unwrap()
12+
.into_iter()
13+
.filter_map(|sym| std::str::from_utf8(sym.name()).ok())
14+
.collect()
15+
}
16+
17+
/// Iterate through the symbols in an object file. See [`object::Object::symbols`].
818
///
9-
/// Panics if `path` is not a valid object file readable by the current user.
19+
/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be
20+
/// parsed as a recognized object file.
21+
#[track_caller]
1022
pub fn with_symbol_iter<P, F, R>(path: P, func: F) -> R
1123
where
1224
P: AsRef<Path>,
1325
F: FnOnce(&mut SymbolIterator<'_, '_>) -> R,
1426
{
15-
let raw_bytes = crate::fs::read(path);
16-
let f = object::File::parse(raw_bytes.as_slice()).expect("unable to parse file");
27+
let path = path.as_ref();
28+
let blob = crate::fs::read(path);
29+
let f = object::File::parse(&*blob)
30+
.unwrap_or_else(|e| panic!("failed to parse `{}`: {e}", path.display()));
1731
let mut iter = f.symbols();
1832
func(&mut iter)
1933
}
2034

2135
/// Check an object file's symbols for substrings.
2236
///
23-
/// Returns `true` if any of the symbols found in the object file at
24-
/// `path` contain a substring listed in `substrings`.
37+
/// Returns `true` if any of the symbols found in the object file at `path` contain a substring
38+
/// listed in `substrings`.
2539
///
26-
/// Panics if `path` is not a valid object file readable by the current user.
40+
/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be
41+
/// parsed as a recognized object file.
42+
#[track_caller]
2743
pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool {
2844
with_symbol_iter(path, |syms| {
2945
for sym in syms {

0 commit comments

Comments
 (0)