linux_bootloader/
pe_section.rs

1// Clippy doesn't like the lifetimes, but rustc wants them. 🤷
2#![allow(clippy::needless_lifetimes)]
3// Clippy doesn't understand that we exit with ? from the closure in
4// and_then below and this can't be expressed with map.
5#![allow(clippy::bind_instead_of_map)]
6
7use alloc::{borrow::ToOwned, string::String};
8use goblin::pe::section_table::SectionTable;
9
10/// Extracts the data of a section in a loaded PE file
11/// based on the section table.
12pub fn pe_section_data<'a>(pe_data: &'a [u8], section: &SectionTable) -> Option<&'a [u8]> {
13    let section_start: usize = section.virtual_address.try_into().ok()?;
14
15    assert!(section.virtual_size <= section.size_of_raw_data);
16    let section_end: usize = section_start + usize::try_from(section.virtual_size).ok()?;
17
18    Some(&pe_data[section_start..section_end])
19}
20
21/// Extracts the data of a section of a loaded PE file
22/// based on the section name.
23pub fn pe_section<'a>(pe_data: &'a [u8], section_name: &str) -> Option<&'a [u8]> {
24    let pe_binary = goblin::pe::PE::parse(pe_data).ok()?;
25
26    pe_binary
27        .sections
28        .iter()
29        .find(|s| s.name().map(|n| n == section_name).unwrap_or(false))
30        .and_then(|s| pe_section_data(pe_data, s))
31}
32
33/// Extracts the data of a section of a loaded PE image and returns it as a string.
34pub fn pe_section_as_string<'a>(pe_data: &'a [u8], section_name: &str) -> Option<String> {
35    pe_section(pe_data, section_name).map(|data| core::str::from_utf8(data).unwrap().to_owned())
36}