From a977e6ba9d0be973a286db2fa51c99897431da81 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 29 Nov 2023 18:49:40 -0800 Subject: [PATCH] read: add Object::symbol_by_name() and Object::symbol_by_name_bytes() --- src/read/traits.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/read/traits.rs b/src/read/traits.rs index d35b0b0c..0f533e8e 100644 --- a/src/read/traits.rs +++ b/src/read/traits.rs @@ -126,6 +126,17 @@ pub trait Object<'data: 'file, 'file>: read::private::Sealed { /// For Mach-O files, this does not include STAB entries. fn symbols(&'file self) -> Self::SymbolIterator; + /// Get the symbol named `symbol_name`, if the symbol exists. + fn symbol_by_name(&'file self, symbol_name: &str) -> Option { + self.symbol_by_name_bytes(symbol_name.as_bytes()) + } + + /// Like [`Self::symbol_by_name`], but allows names that are not UTF-8. + fn symbol_by_name_bytes(&'file self, symbol_name: &[u8]) -> Option { + self.symbols() + .find(|sym| sym.name_bytes() == Ok(symbol_name)) + } + /// Get the dynamic linking symbol table, if any. /// /// Only ELF has a separate dynamic linking symbol table.