From e998012a188679f2e05f06ef105de0de19c33855 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 7 May 2023 12:23:13 +0200 Subject: [PATCH] size works --- elven-forest/src/main.rs | 6 ++++-- elven-forest/src/size.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/elven-forest/src/main.rs b/elven-forest/src/main.rs index 39aa4f0..6b45dc4 100644 --- a/elven-forest/src/main.rs +++ b/elven-forest/src/main.rs @@ -39,6 +39,10 @@ fn main() -> anyhow::Result<()> { let opts = Opts::parse(); for obj in &opts.files { + if opts.files.len() > 1 { + println!("{}", obj.display()); + } + print_file(&opts, obj).with_context(|| format!("Failed to print {}", obj.display()))?; } @@ -103,8 +107,6 @@ struct DynTable { } fn print_file(opts: &Opts, path: &Path) -> anyhow::Result<()> { - println!("{}", path.display()); - let file = File::open(path)?; let mmap = unsafe { Mmap::map(&file) }?; diff --git a/elven-forest/src/size.rs b/elven-forest/src/size.rs index 4c7fa7d..b7415fb 100644 --- a/elven-forest/src/size.rs +++ b/elven-forest/src/size.rs @@ -5,7 +5,6 @@ pub fn analyze_text_bloat(elf: ElfReader<'_>) -> Result<()> { let text = elf .section_header_by_name(b".text") .context(".text not found")?; - dbg!(text.size); let syms = elf.symbols().context("symbols not found")?; @@ -49,24 +48,43 @@ fn symbol_components(sym: &str) -> Result { if demangled.starts_with('<') { let qpath = parse_qpath(&demangled).context("invalid qpath")?; + let components = qpath_components(qpath)?; // qpath - return Ok(demangled); + return Ok(components.join(",")); } else { // normal path let components = demangled.split("::").collect::>(); - let path = components.join(";"); + let path = components.join(","); return Ok(path); } } -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] struct QPath<'a> { qself: &'a str, trait_: &'a str, pathy_bit: &'a str, } +fn qpath_components(qpath: QPath<'_>) -> Result> { + if qpath.qself.starts_with('<') { + let sub_qpath = parse_qpath(qpath.qself)?; + let mut sub_components = qpath_components(sub_qpath)?; + sub_components.extend(qpath.pathy_bit.split("::")); + Ok(sub_components) + } else { + Ok(qpath + .qself + .split("::") + .chain(qpath.pathy_bit.split("::")) + .collect()) + } +} + +// FIXME: Apparently the symbol `std::os::linux::process:: for std::os::fd::owned::OwnedFd>::from` exists in std +// I have no clue what to do about that. + fn parse_qpath(s: &str) -> Result> { let mut chars = s.char_indices().skip(1); let mut angle_brackets = 1u64; @@ -145,13 +163,13 @@ mod tests { #[test] fn path_debug_helper() { - // <::fmt::DebugHelper as core::fmt::Debug>::fmt::hc586615181f69e94 + // <::fmt::DebugHelper as core::fmt::Debug>::fmt::h4f87ac80fb33df05 let sym = "_ZN106_$LT$$LT$std..path..Iter$u20$as$u20$core..fmt..Debug$GT$..fmt..DebugHelper$u20$as$u20$core..fmt..Debug$GT$3fmt17h4f87ac80fb33df05E"; let components = symbol_components(sym).unwrap(); assert_eq!( components, - "std;path;Iter;fmt;DebugHelper;fmt;hc586615181f69e94" + "std,path,Iter,fmt,DebugHelper,fmt,h4f87ac80fb33df05" ) } }