Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(debuginfo): Decompress GNU compressed debug sections correctly #192

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions debuginfo/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,32 @@ impl<'d> ElfObject<'d> {

/// Decompresses the given compressed section data, if supported.
fn decompress_section(&self, section_data: &[u8]) -> Option<Vec<u8>> {
let container = self.elf.header.container().ok()?;
let endianness = self.elf.header.endianness().ok()?;
let context = Ctx::new(container, endianness);
let (size, compressed) = if section_data.starts_with(b"ZLIB") {
// The GNU compression header is a 4 byte magic "ZLIB", followed by an 8-byte big-endian
// size prefix of the decompressed data. This adds up to 12 bytes of GNU header.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused by the last sentence here because the "up to" made it sound like it can be smaller while the code didn't allow for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, saw this too late. I can clarify this on master, if you like.

In my mind it read as "the numbers are adding up" to a sum :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either way is fine

if section_data.len() < 12 {
return None;
}

let compression = CompressionHeader::parse(&section_data, 0, context).ok()?;
if compression.ch_type != ELFCOMPRESS_ZLIB {
return None;
}
let mut size_bytes = [0; 8];
size_bytes.copy_from_slice(&section_data[4..12]);

(u64::from_be_bytes(size_bytes), &section_data[12..])
} else {
let container = self.elf.header.container().ok()?;
let endianness = self.elf.header.endianness().ok()?;
let context = Ctx::new(container, endianness);

let compression = CompressionHeader::parse(&section_data, 0, context).ok()?;
if compression.ch_type != ELFCOMPRESS_ZLIB {
return None;
}

let compressed = &section_data[CompressionHeader::size(context)..];
(compression.ch_size, compressed)
};

let compressed = &section_data[CompressionHeader::size(context)..];
let mut decompressed = Vec::with_capacity(compression.ch_size as usize);
let mut decompressed = Vec::with_capacity(size as usize);
Decompress::new(true)
.decompress_vec(compressed, &mut decompressed, FlushDecompress::Finish)
.ok()?;
Expand Down