diff options
| author | 许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com> | 2025-01-20 19:13:16 +0800 |
|---|---|---|
| committer | Jieyou Xu <jieyouxu@outlook.com> | 2025-01-31 04:29:40 +0000 |
| commit | 0bc1b4f4f6ace5b5e2a7676e498343cfeafab136 (patch) | |
| tree | 2061b26632bd669a6aa0ef9971acb22e52dd4ef1 | |
| parent | c15c9702d6cf1fe36bd8bef7176a98137d6028b5 (diff) | |
| download | rust-0bc1b4f4f6ace5b5e2a7676e498343cfeafab136.tar.gz rust-0bc1b4f4f6ace5b5e2a7676e498343cfeafab136.zip | |
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 <binarycat@envs.net>
| -rw-r--r-- | src/tools/run-make-support/src/symbols.rs | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/tools/run-make-support/src/symbols.rs b/src/tools/run-make-support/src/symbols.rs index fd0c866bcc9..e4d244e14a4 100644 --- a/src/tools/run-make-support/src/symbols.rs +++ b/src/tools/run-make-support/src/symbols.rs @@ -2,28 +2,44 @@ use std::path::Path; use object::{self, Object, ObjectSymbol, SymbolIterator}; -/// Iterate through the symbols in an object file. -/// -/// Uses a callback because `SymbolIterator` does not own its data. +/// Given an [`object::File`], find the exported dynamic symbol names via +/// [`object::Object::exports`]. This does not distinguish between which section the symbols appear +/// in. +#[track_caller] +pub fn exported_dynamic_symbol_names<'file>(file: &'file object::File<'file>) -> Vec<&'file str> { + file.exports() + .unwrap() + .into_iter() + .filter_map(|sym| std::str::from_utf8(sym.name()).ok()) + .collect() +} + +/// Iterate through the symbols in an object file. See [`object::Object::symbols`]. /// -/// Panics if `path` is not a valid object file readable by the current user. +/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be +/// parsed as a recognized object file. +#[track_caller] pub fn with_symbol_iter<P, F, R>(path: P, func: F) -> R where P: AsRef<Path>, F: FnOnce(&mut SymbolIterator<'_, '_>) -> R, { - let raw_bytes = crate::fs::read(path); - let f = object::File::parse(raw_bytes.as_slice()).expect("unable to parse file"); + let path = path.as_ref(); + let blob = crate::fs::read(path); + let f = object::File::parse(&*blob) + .unwrap_or_else(|e| panic!("failed to parse `{}`: {e}", path.display())); let mut iter = f.symbols(); func(&mut iter) } /// Check an object file's symbols for substrings. /// -/// Returns `true` if any of the symbols found in the object file at -/// `path` contain a substring listed in `substrings`. +/// Returns `true` if any of the symbols found in the object file at `path` contain a substring +/// listed in `substrings`. /// -/// Panics if `path` is not a valid object file readable by the current user. +/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be +/// parsed as a recognized object file. +#[track_caller] pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool { with_symbol_iter(path, |syms| { for sym in syms { |
