Skip to content

Commit

Permalink
fix(dynamic): sort the shared library list (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 18, 2024
1 parent 580c9ec commit fd2f7d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
36 changes: 32 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use elf::{endian::AnyEndian, ElfBytes};
use heh::app::Application as Heh;
use heh::decoder::Encoding;
use lddtree::{DependencyAnalyzer, DependencyTree};
use lddtree::DependencyAnalyzer;
use ratatui::text::Line;
use rust_strings::BytesConfig;
use std::{
Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct Analyzer<'a> {
/// System calls.
pub system_calls: Vec<Line<'a>>,
/// Library dependencies.
pub dependencies: DependencyTree,
pub dependencies: Vec<(String, String)>,
}

impl Debug for Analyzer<'_> {
Expand All @@ -67,8 +67,8 @@ impl<'a> Analyzer<'a> {
let elf = Elf::try_from(elf_bytes)?;
let heh = Heh::new(file_info.open_file()?, Encoding::Ascii, 0)
.map_err(|e| Error::HexdumpError(e.to_string()))?;
let dependencies = DependencyAnalyzer::default().analyze(file_info.path)?;
Ok(Self {
dependencies: Self::extract_libs(&file_info)?,
files,
file: file_info,
elf,
Expand All @@ -77,10 +77,38 @@ impl<'a> Analyzer<'a> {
heh,
tracer: TraceData::default(),
system_calls: Vec::new(),
dependencies,
})
}

/// Extracts the library dependencies.
pub fn extract_libs(file_info: &FileInfo<'a>) -> Result<Vec<(String, String)>> {
let mut dependencies = DependencyAnalyzer::default()
.analyze(file_info.path)?
.libraries
.clone()
.into_iter()
.map(|(name, lib)| {
(
name.to_string(),
lib.realpath
.unwrap_or(lib.path)
.to_string_lossy()
.to_string(),
)
})
.collect::<Vec<(String, String)>>();
dependencies.sort_by(|a, b| {
let lib_condition1 = a.0.starts_with("lib");
let lib_condition2 = b.0.starts_with("lib");
match (lib_condition1, lib_condition2) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.0.cmp(&b.0),
}
});
Ok(dependencies)
}

/// Returns the sequences of printable characters.
pub fn extract_strings(&mut self, event_sender: mpsc::Sender<Event>) {
let config = BytesConfig::new(self.file.bytes.to_vec()).with_min_length(self.strings_len);
Expand Down
11 changes: 1 addition & 10 deletions src/tui/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,9 @@ impl<'a> State<'a> {
self.list = SelectableList::with_items(
self.analyzer
.dependencies
.libraries
.clone()
.into_iter()
.map(|(name, lib)| {
vec![
name.to_string(),
lib.realpath
.unwrap_or(lib.path)
.to_string_lossy()
.to_string(),
]
})
.map(|(name, lib)| vec![name, lib])
.collect(),
);
}
Expand Down

0 comments on commit fd2f7d2

Please sign in to comment.