From 0d5b7b0fa9911fec199201ca2d2b307f3df5b8d8 Mon Sep 17 00:00:00 2001 From: ohchase Date: Sun, 11 Aug 2024 09:05:24 -0400 Subject: [PATCH] patch collect_modules bug --- examples/dump_plt.rs | 1 + src/lib.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/dump_plt.rs b/examples/dump_plt.rs index 1fbb6f7..fd88ae4 100644 --- a/examples/dump_plt.rs +++ b/examples/dump_plt.rs @@ -3,6 +3,7 @@ use plt_rs::{collect_modules, DynamicLibrary, RelocationTable}; fn main() -> Result<()> { let entries = collect_modules(); + println!("collected modules"); for entry in entries.into_iter() { println!("[{:?}] Addr: {:#X?}", entry.name(), entry.addr()); diff --git a/src/lib.rs b/src/lib.rs index fe7a0ee..f6d32a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -505,7 +505,8 @@ impl<'a> DynamicLibrary<'a> { symbols .resolve_name(e.symbol_index() as usize, string_table) .map(|s| (e, s)) - }).find(|(_, s)| s.eq(symbol_name)) + }) + .find(|(_, s)| s.eq(symbol_name)) .map(|(target_function, _)| target_function) { return Some(symbol); @@ -519,7 +520,8 @@ impl<'a> DynamicLibrary<'a> { symbols .resolve_name(e.symbol_index() as usize, string_table) .map(|s| (e, s)) - }).find(|(_, s)| s.eq(symbol_name)) + }) + .find(|(_, s)| s.eq(symbol_name)) .map(|(target_function, _)| target_function) { return Some(symbol); @@ -630,6 +632,12 @@ pub fn collect_modules<'a>() -> Vec> { // We have to copy sthe `dl_phdr_info` struct out, as the same memory buffer is used for // each entry during the iteration process. Otherwise we could have used a vector of // pointers. + println!("{} {}", dl_info.dlpi_addr, dl_info.dlpi_phnum); + + if dl_info.dlpi_phnum == 0 { + return; + } + let program_headers = unsafe { std::slice::from_raw_parts(dl_info.dlpi_phdr, dl_info.dlpi_phnum as usize) }; objs.push(LoadedLibrary {