Expand description
Reader, writer, and Linux ELF/DWARF converter for LLVM GSYM, in safe Rust.
GSYM maps an instruction address to a function name, a source file and line, and the chain of inlined calls at that address. It stores nothing else, so a GSYM file is far smaller than the DWARF it was built from, and its sorted address index can be memory-mapped and queried without parsing the rest of the file.
§Quick start
Build a file in memory and resolve an address:
use gsym::{AddressRange, FileEntry, Function, Gsym, GsymBuilder, LineEntry};
let mut builder = GsymBuilder::new().base_address(0x4000);
let source = builder.add_file(FileEntry::new(b"src", b"main.rs"))?;
builder.add_function(Function {
lines: vec![LineEntry::new(0x4010, source, 12)],
..Function::new(AddressRange::new(0x4010, 0x4020), b"example")
})?;
let bytes = builder.to_bytes()?;
let gsym = Gsym::parse(&bytes)?;
let hit = gsym.lookup(0x4014)?.expect("address is covered");
assert_eq!(hit.frames()[0].name, b"example");
assert_eq!(hit.frames()[0].line, 12);
assert_eq!(hit.frames()[0].offset, 4);Read a file written by this crate or by llvm-gsymutil:
use gsym::Gsym;
let gsym = Gsym::open("app.gsym")?;
if let Some(hit) = gsym.lookup(0x401120)? {
for frame in hit.frames() {
println!(
"{}{} at {}:{}",
String::from_utf8_lossy(frame.name),
if frame.inlined { " (inlined)" } else { "" },
String::from_utf8_lossy(frame.basename),
frame.line,
);
}
}Build one from an executable’s own DWARF (convert feature, on by default):
use gsym::convert::ElfConverter;
let report = ElfConverter::default().convert_path("./app")?;
std::fs::write("./app.gsym", report.builder.to_bytes()?)?;§Choosing an entry point
| To … | Use | Notes |
|---|---|---|
| query a file on disk | Gsym::open | safe; reads one owned snapshot |
| query bytes you already hold | Gsym::parse | any AsRef<[u8]>, borrowed or owned, no copy |
| query a large file without reading it all | MappedGsym::map | mmap feature; unsafe |
| create a file | GsymBuilder | deterministic output from semantic records |
| import ELF and DWARF | convert::ElfConverter | convert feature |
| change version or byte order | Gsym::transcode | or DecodedGsym to edit in between |
| split into shards | DecodedGsym::segments | size-bounded, independently readable |
| check an untrusted file | Gsym::verify | checks the whole file up front |
All readers are the same type, Gsym<D> over different byte
storage, so the query API does not change with the choice.
§Usage notes
-
Addresses are unslid. A GSYM file records the virtual addresses of the ELF image it was built from, so an address captured from a running PIE executable or shared object must have its load bias subtracted first. Skipping that step is the usual cause of an empty or wrong result; see
docs::symbolication. -
Names and paths are bytes. The format stores raw bytes and this crate preserves them, so results are
&[u8]. UseString::from_utf8_lossyto display them, orstr::from_utf8when invalid data should be reported. -
Version 1 is the default and is what current tooling reads. Version 2 raises v1’s 4 GiB offset limits and its 20-byte build-ID limit but needs LLVM 23 or newer, so it must be requested with
GsymVersion::V2. -
Lookups take
&selfand keep no interior state. There is no global cache and no lock, so one reader can be shared across threads. The one reusable buffer,LookupScratch, belongs to the caller.
§Feature flags
| Feature | Default | Adds |
|---|---|---|
mmap | yes | MappedGsym, a reader backed by a read-only memory map |
convert | yes | the convert module: Linux ELF and DWARF import |
debuginfod | yes | debuginfod network lookup during conversion |
The codec needs neither default feature:
[dependencies]
gsym-rs = { version = "0.1", default-features = false }§Errors
Fallible entry points return Result<T>. Its error type is
Error, which covers malformed input, model violations, exceeded limits,
I/O, and, with convert, ELF and DWARF diagnostics. It is
#[non_exhaustive], so a match on it needs a fallback arm.
Lookup validates only the records it reads, so an untrusted file is worth
one Gsym::verify at load time.
§Guides
docs::symbolication: unslid addresses, reading frames, storage choices, performance, threading.docs::cookbook: worked examples for every part of the API.docs::conversion: building GSYM from Linux ELF files and their DWARF.docs::format: the on-disk format, version 1 and version 2.
Modules§
Structs§
- Address
Range - A half-open virtual-address range,
[start, end). - Builder
Options - Finalization policy used by
GsymBuilder. - Call
Site - Metadata for a call instruction’s return address.
- Call
Site Flags - Forward-compatible GSYM call-site flag bits.
- Decoded
Gsym - An owned, version-independent representation of a complete GSYM file.
- File
Entry - A source file split into directory and basename, as GSYM stores it.
- File
Index - An index into a GSYM file table.
- Frame
Lookup Options - Controls which optional records frame visitation inspects.
- Function
- Owned semantic function data accepted by a GSYM builder.
- Function
Ref - Borrowed view of one indexed
FunctionInforecord. - Functions
- Iterator over indexed functions in address-table order.
- Gsym
- Parsed GSYM data backed by caller-selected byte storage.
- Gsym
Builder - Version-independent, deterministic GSYM construction API.
- Gsym
Segment - One independently readable shard of a segmented GSYM image.
- Header
- Borrowed metadata from a parsed GSYM header.
- Inline
Node - Recursive inline-call information for a function.
- Line
Entry - One address-to-source-row mapping.
- Lookup
- Borrowed symbolication result for one address.
- Lookup
Frame - One source frame returned by address lookup.
- Lookup
Options - Controls which optional records an allocating lookup inspects.
- Lookup
Scratch - Reusable buffer for allocation-free address lookup.
- Mapped
Bytes mmap - Opaque read-only mapping used by
MappedGsym. - Parser
Error convert - Opaque source error from an ELF or DWARF parser.
- Transcode
Options - Output choices for semantic GSYM transcoding.
- Verify
Report - Aggregate counts produced by full-file verification.
- Writer
Options - Settings controlling deterministic GSYM encoding.
Enums§
- Companion
Mismatch convert - Property by which a companion ELF disagrees with its linked image.
- ElfInput
Kind convert - Identifies an ELF input in conversion diagnostics.
- Endian
- Byte order used for fixed-width integers in a GSYM file.
- Error
- Errors produced while parsing, building, or writing GSYM data.
- Function
SetPolicy - How finalization treats functions that share an address range.
- Gsym
Version - GSYM encoding version.
Type Aliases§
- Mapped
Gsym mmap - GSYM reader backed directly by a read-only memory map.
- Result
- Crate-wide result type using
Error.