-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: add an elf module and an helper to read a .BTF section from an…
… ELF file This helpers is useful to extract .BTF ELF sections from kernel binaries, e.g. vmlinux and .ko files. Signed-off-by: Antoine Tenart <[email protected]>
- Loading branch information
Showing
5 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::{ | ||
fs::{self, File}, | ||
path::Path, | ||
}; | ||
|
||
use anyhow::{anyhow, bail, Result}; | ||
use elf::{endian::AnyEndian, ElfStream}; | ||
|
||
/// Extract raw BTF data from the .BTF elf section of the given file. Output can | ||
/// be used to fed `from_bytes` constructors in this library. | ||
pub fn extract_btf_from_file<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> { | ||
let file = File::open(&path) | ||
.map_err(|e| anyhow!("Could not open {}: {e}", path.as_ref().display()))?; | ||
let mut elf = ElfStream::<AnyEndian, _>::open_stream(file)?; | ||
|
||
let btf_hdr = match elf.section_header_by_name(".BTF")? { | ||
Some(hdr) => *hdr, | ||
None => bail!("No BTF section in {}", path.as_ref().display()), | ||
}; | ||
|
||
let (btf, chdr) = elf.section_data(&btf_hdr)?; | ||
if chdr.is_some() { | ||
bail!( | ||
"Compressed BTF sections are not supported ({})", | ||
path.as_ref().display() | ||
); | ||
} | ||
|
||
Ok(btf.to_vec()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
//! use cases. | ||
pub mod collection; | ||
#[cfg(feature = "elf")] | ||
pub mod elf; |