Skip to content

Commit 10cb939

Browse files
committed
Handle compressed debug sections in ELF files
ELF files allow debug info sections to be compressed. The libbacktrace backed supported these compressed sections, but the Gimli backend did not. This commit adds that support to the Gimli backend. In my tests these debug info sections do not obey the alignment requirements that the object crate expects for the gABI compression header (nor can I find a source documenting any alignment requirements), so this commit additionally enables the "unaligned" feature in the upcoming version of the object crate. There is a bit of unsafe to ensure the lifetime of the decompressed sections matches the lifetime of the mmap'd file. I don't think there is a way around this unsafe code, unless we are willing to ditch Gimli's EndianSlice for an (apparently slower) EndianReader backed by a Cow<[u8]>. Fix #342.
1 parent d4f24b1 commit 10cb939

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

.github/workflows/main.yml

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ jobs:
7878
- run: cargo test --features gimli-symbolize --manifest-path crates/without_debuginfo/Cargo.toml
7979
- run: cargo test --manifest-path crates/line-tables-only/Cargo.toml --features libbacktrace
8080
- run: cargo test --manifest-path crates/line-tables-only/Cargo.toml --features gimli-symbolize
81+
- run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gabi" cargo test --features gimli-symbolize
82+
if: contains(matrix.os, 'ubuntu')
83+
- run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gnu" cargo test --features gimli-symbolize
84+
if: contains(matrix.os, 'ubuntu')
8185

8286
windows_arm64:
8387
name: Windows AArch64

Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ compiler_builtins = { version = '0.1.2', optional = true }
3939
# Optional dependencies enabled through the `gimli-symbolize` feature, do not
4040
# use these features directly.
4141
addr2line = { version = "0.12.0", optional = true, default-features = false }
42+
flate2 = { version = "1.0.14", optional = true }
4243
[dependencies.object]
43-
version = "0.19"
44+
git = "https://github.com/gimli-rs/object.git"
4445
optional = true
4546
default-features = false
46-
features = ['read_core', 'elf', 'macho', 'pe']
47+
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned']
4748

4849
[target.'cfg(windows)'.dependencies]
4950
winapi = { version = "0.3.3", optional = true }
@@ -71,7 +72,7 @@ std = []
7172
# be affected by feature selection here. Also note that it's highly unlikely you
7273
# want to configure this. If you're having trouble getting backtraces it's
7374
# likely best to open an issue.
74-
gimli-symbolize = ["addr2line", "object", "std"]
75+
gimli-symbolize = ["addr2line", "flate2", "object", "std"]
7576
libbacktrace = ["backtrace-sys/backtrace-sys"]
7677

7778
#=======================================

src/symbolize/gimli/elf.rs

+62-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::{Mapping, Path, Vec};
2-
use object::read::elf::{FileHeader, SectionHeader, SectionTable, Sym};
1+
use super::{Mapping, Mmap, Path, Vec};
2+
use object::elf::{ELFCOMPRESS_ZLIB, SHF_COMPRESSED};
3+
use object::read::elf::{CompressionHeader, FileHeader, SectionHeader, SectionTable, Sym};
34
use object::read::StringTable;
4-
use object::{Bytes, NativeEndian};
5+
use object::{BigEndian, Bytes, NativeEndian};
56

67
#[cfg(target_pointer_width = "32")]
78
type Elf = object::elf::FileHeader32<NativeEndian>;
@@ -28,16 +29,16 @@ pub struct Object<'a> {
2829
/// We could use a literal instead, but this helps ensure correctness.
2930
endian: NativeEndian,
3031
/// The entire file data.
31-
data: Bytes<'a>,
32+
mmap: &'a Mmap,
3233
sections: SectionTable<'a, Elf>,
3334
strings: StringTable<'a>,
3435
/// List of pre-parsed and sorted symbols by base address.
3536
syms: Vec<ParsedSym>,
3637
}
3738

3839
impl<'a> Object<'a> {
39-
fn parse(data: &'a [u8]) -> Option<Object<'a>> {
40-
let data = object::Bytes(data);
40+
fn parse(mmap: &'a Mmap) -> Option<Object<'a>> {
41+
let data = object::Bytes(mmap);
4142
let elf = Elf::parse(data).ok()?;
4243
let endian = elf.endian().ok()?;
4344
let sections = elf.sections(endian, data).ok()?;
@@ -80,22 +81,59 @@ impl<'a> Object<'a> {
8081
syms.sort_unstable_by_key(|s| s.address);
8182
Some(Object {
8283
endian,
83-
data,
84+
mmap,
8485
sections,
8586
strings,
8687
syms,
8788
})
8889
}
8990

9091
pub fn section(&self, name: &str) -> Option<&'a [u8]> {
91-
Some(
92-
self.sections
93-
.section_by_name(self.endian, name.as_bytes())?
94-
.1
95-
.data(self.endian, self.data)
96-
.ok()?
97-
.0,
98-
)
92+
if let Some(section) = self.section_header(name) {
93+
let mut data = section.data(self.endian, self.data()).ok()?;
94+
95+
// Check for DWARF-standard (gABI) compression, i.e., as generated
96+
// by ld's `--compress-debug-sections=zlib-gabi` flag.
97+
let flags: u64 = section.sh_flags(self.endian).into();
98+
if (flags & u64::from(SHF_COMPRESSED)) == 0 {
99+
// Not compressed.
100+
return Some(data.0);
101+
}
102+
103+
let header = data.read::<<Elf as FileHeader>::CompressionHeader>().ok()?;
104+
if header.ch_type(self.endian) != ELFCOMPRESS_ZLIB {
105+
// Zlib compression is the only known type.
106+
return None;
107+
}
108+
let size = header.ch_size(self.endian) as usize;
109+
let buf = decompress_zlib(data.0, size)?;
110+
return Some(self.mmap.stash(buf));
111+
}
112+
113+
// Check for the nonstandard GNU compression format, i.e., as generated
114+
// by ld's `--compress-debug-sections=zlib-gnu` flag.
115+
let zdebug_name = format!(".zdebug_{}", &name[7..]);
116+
if let Some(section) = self.section_header(&zdebug_name) {
117+
let mut data = section.data(self.endian, self.data()).ok()?;
118+
if data.read_bytes(8).ok()?.0 != b"ZLIB\0\0\0\0" {
119+
return None;
120+
}
121+
let size = data.read::<object::U32Bytes<_>>().ok()?.get(BigEndian);
122+
let buf = decompress_zlib(data.0, size as usize)?;
123+
return Some(self.mmap.stash(buf));
124+
}
125+
126+
None
127+
}
128+
129+
fn section_header(&self, name: &str) -> Option<&<Elf as FileHeader>::SectionHeader> {
130+
self.sections
131+
.section_by_name(self.endian, name.as_bytes())
132+
.map(|(_index, section)| section)
133+
}
134+
135+
fn data(&self) -> Bytes<'a> {
136+
object::Bytes(&self.mmap)
99137
}
100138

101139
pub fn search_symtab<'b>(&'b self, addr: u64) -> Option<&'b [u8]> {
@@ -112,3 +150,12 @@ impl<'a> Object<'a> {
112150
}
113151
}
114152
}
153+
154+
fn decompress_zlib(data: &[u8], size: usize) -> Option<Vec<u8>> {
155+
let mut buf = Vec::with_capacity(size);
156+
let header_expected = true;
157+
flate2::Decompress::new(header_expected)
158+
.decompress_vec(data, &mut buf, flate2::FlushDecompress::Finish)
159+
.ok()?;
160+
Some(buf)
161+
}

src/symbolize/gimli/mmap_unix.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use std::cell::UnsafeCell;
12
use std::fs::File;
23
use std::ops::Deref;
34
use std::os::unix::prelude::*;
45
use std::ptr;
56
use std::slice;
7+
use std::vec::Vec;
68

79
pub struct Mmap {
810
ptr: *mut libc::c_void,
911
len: usize,
12+
/// Additional byte vectors that need to live as long as the mmap.
13+
buffers: UnsafeCell<Vec<Vec<u8>>>,
1014
}
1115

1216
impl Mmap {
@@ -22,7 +26,24 @@ impl Mmap {
2226
if ptr == libc::MAP_FAILED {
2327
return None;
2428
}
25-
Some(Mmap { ptr, len })
29+
Some(Mmap {
30+
ptr,
31+
len,
32+
buffers: UnsafeCell::new(vec![]),
33+
})
34+
}
35+
36+
/// Takes ownership of `buf` and returns a reference to its contents that
37+
/// lives as long as this `Mmap` does.
38+
pub fn stash(&self, buf: Vec<u8>) -> &[u8] {
39+
// SAFETY: this is the only function that ever constructs a mutable
40+
// reference to `self.buffers`.
41+
let buffers = unsafe { &mut *self.buffers.get() };
42+
let i = buffers.len();
43+
buffers.push(buf);
44+
// SAFETY: we never remove elements from `self.buffers`, so a reference
45+
// to the data inside any buffer will live as long as `Mmap` does.
46+
&buffers[i]
2647
}
2748
}
2849

0 commit comments

Comments
 (0)