@@ -2,18 +2,32 @@ use std::path::Path;
2
2
3
3
use object:: { self , Object , ObjectSymbol , SymbolIterator } ;
4
4
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** impose any filters on the specific dynamic
7
+ /// symbols, e.g. if they are global or local, if they are defined or not, and in which section the
8
+ /// dynamic symbols reside in.
9
+ #[ track_caller]
10
+ pub fn exported_dynamic_symbol_names < ' file > ( file : & ' file object:: File < ' file > ) -> Vec < & ' file str > {
11
+ file. exports ( )
12
+ . unwrap ( )
13
+ . into_iter ( )
14
+ . filter_map ( |sym| std:: str:: from_utf8 ( sym. name ( ) ) . ok ( ) )
15
+ . collect ( )
16
+ }
17
+
18
+ /// Iterate through the symbols in an object file. See [`object::Object::symbols`].
8
19
///
9
20
/// Panics if `path` is not a valid object file readable by the current user.
21
+ #[ track_caller]
10
22
pub fn with_symbol_iter < P , F , R > ( path : P , func : F ) -> R
11
23
where
12
24
P : AsRef < Path > ,
13
25
F : FnOnce ( & mut SymbolIterator < ' _ , ' _ > ) -> R ,
14
26
{
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( ) ) ) ;
17
31
let mut iter = f. symbols ( ) ;
18
32
func ( & mut iter)
19
33
}
24
38
/// `path` contain a substring listed in `substrings`.
25
39
///
26
40
/// Panics if `path` is not a valid object file readable by the current user.
41
+ #[ track_caller]
27
42
pub fn any_symbol_contains ( path : impl AsRef < Path > , substrings : & [ & str ] ) -> bool {
28
43
with_symbol_iter ( path, |syms| {
29
44
for sym in syms {
0 commit comments