Skip to content

Commit

Permalink
Fix finding BSS sections (benfred#624)
Browse files Browse the repository at this point in the history
* Fix finding BSS sections

There can be multiple NOBITS sections in an ELF binary - and we were taking
the first one as being the BSS section. In certain cases this doesn't work
(including the latest python 3.12 binary in conda) - since there can
be '.tbss' or '.sbss' sections instead.

Fix by performing an additional filter step using the section name.

* fix windows build with newer indicatif

Granulate backport notes: We only cherry-picked the changes to code and not dependencies, as the current versions of packages are enough to use the new code.
  • Loading branch information
benfred authored and michelhe committed Dec 20, 2023
1 parent 6d4cee6 commit 3ecf437
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/binary_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,25 @@ pub fn parse_binary(_pid: remoteprocess::Pid, filename: &Path, addr: u64, size:
}

Object::Elf(elf) => {
let bss_header = elf.section_headers
let strtab = elf.shdr_strtab;
let bss_header = elf
.section_headers
.iter()
.find(|ref header| header.sh_type == goblin::elf::section_header::SHT_NOBITS)
.ok_or_else(|| format_err!("Failed to find BSS section header in {}", filename.display()))?;
// filter down to things that are both NOBITS sections and are named .bss
.filter(|header| header.sh_type == goblin::elf::section_header::SHT_NOBITS)
.filter(|header| {
strtab
.get_at(header.sh_name)
.map_or(true, |name| name == ".bss")
})
// if we have multiple sections here, take the largest
.max_by_key(|header| header.sh_size)
.ok_or_else(|| {
format_err!(
"Failed to find BSS section header in {}",
filename.display()
)
})?;

let program_header = elf.program_headers
.iter()
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
// The spinner on windows doesn't look great: was replaced by a [?] character at least on
// my system. Replace unicode spinners with just how many seconds have elapsed
#[cfg(windows)]
progress.set_style(indicatif::ProgressStyle::default_spinner().template("[{elapsed}] {msg}"));
progress.set_style(
indicatif::ProgressStyle::default_spinner()
.template("[{elapsed}] {msg}")
.unwrap(),
);
progress
}
};
Expand Down

0 comments on commit 3ecf437

Please sign in to comment.