Crates.io | ld-so-cache |
lib.rs | ld-so-cache |
version | 0.1.0 |
created_at | 2025-06-11 02:09:36.87137+00 |
updated_at | 2025-06-11 02:09:36.87137+00 |
description | A parser for glibc ld.so.cache files |
homepage | |
repository | https://gitlab.com/piperswe/ld-so-cache |
max_upload_size | |
id | 1708006 |
size | 2,682,251 |
A comprehensive Rust parser for Linux ld.so.cache
files with 100% compatibility with ldconfig
output.
The ld.so.cache
file is used by the Linux dynamic linker to quickly locate shared libraries. This crate provides a complete parser that can read both legacy (ld.so-1.7.0) and modern (glibc) cache formats, extracting library names, paths, and hardware capability information.
ldconfig -p
across many major distributionsThis parser has been tested against real cache files from:
All tests show exact matches with ldconfig -p
output.
use ld_so_cache::parsers::parse_ld_cache;
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse the system's ld.so.cache file
let data = fs::read("/etc/ld.so.cache")?;
let cache = parse_ld_cache(&data)?;
// Extract all library entries
let entries = cache.get_entries()?;
for entry in entries {
println!("{} -> {}", entry.library_name, entry.library_path);
// Check if it's an ELF library
if entry.flags & 1 != 0 {
println!(" ELF library");
}
// Display hardware capabilities if present
if let Some(hwcap) = entry.hwcap {
let isa_level = (hwcap >> 52) & 0x3ff;
if isa_level > 0 {
println!(" Requires ISA level: {}", isa_level);
}
}
}
Ok(())
}
The crate includes a CLI tool for inspecting cache files:
# Parse the system cache
ld-cache-parser
# Parse a specific cache file
ld-cache-parser -f /path/to/ld.so.cache
# Show detailed information including hardware capabilities
ld-cache-parser --verbose
# Output in JSON format
ld-cache-parser --format json
# Show only cache statistics
ld-cache-parser --stats
The old format provides basic library name to path mappings:
use ld_so_cache::OldFileEntry;
let entry = OldFileEntry {
flags: 1, // ELF library
key: 0, // Offset to library name
value: 10, // Offset to library path
};
The new format includes hardware capabilities and extensions:
use ld_so_cache::NewFileEntry;
let entry = NewFileEntry {
flags: 1,
key: 0,
value: 10,
osversion_unused: 0,
hwcap: 0x0020_0000_0000_1234, // Hardware capabilities
};
// Extract ISA level (bits 52-61)
let isa_level = (entry.hwcap >> 52) & 0x3ff;
// Check for extension flag (bit 62)
let has_extensions = entry.hwcap & (1u64 << 62) != 0;
// Get CPU features (bits 0-51)
let cpu_features = entry.hwcap & ((1u64 << 52) - 1);
The parser correctly decodes hardware capability information:
The library provides comprehensive error handling:
use ld_so_cache::{CacheError, parsers::parse_ld_cache};
match parse_ld_cache(&data) {
Ok(cache) => {
// Process cache entries
match cache.get_entries() {
Ok(entries) => println!("Found {} libraries", entries.len()),
Err(CacheError::InvalidStringOffset(offset)) => {
eprintln!("Corrupted string table at offset {}", offset);
}
Err(e) => eprintln!("Error extracting entries: {}", e),
}
}
Err(CacheError::InvalidMagic) => {
eprintln!("Not a valid ld.so.cache file");
}
Err(CacheError::TruncatedFile) => {
eprintln!("Cache file is incomplete");
}
Err(e) => eprintln!("Parse error: {}", e),
}
The library supports JSON serialization with proper formatting:
{
"library_name": "libc.so.6",
"library_path": "/lib/x86_64-linux-gnu/libc.so.6",
"flags": 769,
"hwcap": "0x0000000000001000"
}
Add this to your Cargo.toml
:
[dependencies]
ld-so-cache = "0.1"
Or install the CLI tool:
cargo install ld-so-cache
Full API documentation is available at docs.rs.
The library includes extensive documentation with examples for all public APIs.
The crate includes comprehensive tests:
ldconfig -p
outputRun tests with:
cargo test
This project is licensed under the CC0 License - see the LICENSE.md
file for details.
This library was built with the help of Claude Code. This README was significantly written by Claude. I wanted to get this quickly built, so I took a shortcut.