Skip to content

Commit

Permalink
Separates the text section from read-only sections in internal struct…
Browse files Browse the repository at this point in the history
…ures.

Removes SectionInfo.
  • Loading branch information
Lichtso committed Feb 27, 2024
1 parent 179a0f9 commit db0e38f
Showing 1 changed file with 26 additions and 63 deletions.
89 changes: 26 additions & 63 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,6 @@ impl BpfRelocationType {
}
}

#[derive(Debug, PartialEq)]
struct SectionInfo {
name: String,
vaddr: u64,
offset_range: Range<usize>,
}
impl SectionInfo {
fn mem_size(&self) -> usize {
mem::size_of::<Self>().saturating_add(self.name.capacity())
}
}

#[derive(Debug, PartialEq)]
pub(crate) enum Section {
/// Owned section data.
Expand All @@ -210,8 +198,8 @@ pub struct Executable<C: ContextObject> {
sbpf_version: SBPFVersion,
/// Read-only section
ro_section: Section,
/// Text section info
text_section_info: SectionInfo,
/// Text section
text_section: Section,
/// Address of the entry point
entry_pc: usize,
/// Call resolution map (hash, pc, name)
Expand All @@ -236,21 +224,15 @@ impl<C: ContextObject> Executable<C> {

/// Get the .text section virtual address and bytes
pub fn get_text_bytes(&self) -> (u64, &[u8]) {
let (ro_offset, ro_section) = match &self.ro_section {
Section::Owned(offset, data) => (*offset, data.as_slice()),
let (offset, section) = match &self.text_section {
Section::Owned(_offset, _data) => unreachable!(),
Section::Borrowed(offset, byte_range) => {
(*offset, &self.elf_bytes.as_slice()[byte_range.clone()])
}
};

let offset = self
.text_section_info
.vaddr
.saturating_sub(ebpf::MM_PROGRAM_START)
.saturating_sub(ro_offset as u64) as usize;
(
self.text_section_info.vaddr,
&ro_section[offset..offset.saturating_add(self.text_section_info.offset_range.len())],
ebpf::MM_PROGRAM_START.saturating_add(offset as u64),
section,
)
}

Expand All @@ -274,10 +256,13 @@ impl<C: ContextObject> Executable<C> {
self.entry_pc
}

/// Get the text section offset
/// Get the text section offset in the ELF file
#[cfg(feature = "debugger")]
pub fn get_text_section_offset(&self) -> u64 {
self.text_section_info.offset_range.start as u64
match &self.text_section {
Section::Owned(_offset, _data) => unreachable!(),
Section::Borrowed(_offset, byte_range) => byte_range.start as u64,
}
}

/// Get the loader built-in program
Expand Down Expand Up @@ -323,8 +308,6 @@ impl<C: ContextObject> Executable<C> {
mut function_registry: FunctionRegistry<usize>,
) -> Result<Self, ElfError> {
let elf_bytes = AlignedMemory::from_slice(text_bytes);
let config = loader.get_config();
let enable_symbol_and_section_labels = config.enable_symbol_and_section_labels;
let entry_pc = if let Some((_name, pc)) = function_registry.lookup_by_name(b"entrypoint") {
pc
} else {
Expand All @@ -340,15 +323,7 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section: Section::Borrowed(0, 0..text_bytes.len()),
text_section_info: SectionInfo {
name: if enable_symbol_and_section_labels {
".text".to_string()
} else {
String::default()
},
vaddr: ebpf::MM_PROGRAM_START,
offset_range: 0..text_bytes.len(),
},
text_section: Section::Borrowed(0, 0..text_bytes.len()),
entry_pc,
function_registry,
loader,
Expand Down Expand Up @@ -394,32 +369,19 @@ impl<C: ContextObject> Executable<C> {

// calculate the text section info
let text_section = elf.section(b".text")?;
let text_section_info = SectionInfo {
name: if config.enable_symbol_and_section_labels {
elf.section_name(text_section.sh_name())
.and_then(|name| std::str::from_utf8(name).ok())
.unwrap_or(".text")
.to_string()
} else {
String::default()
},
vaddr: if sbpf_version.enable_elf_vaddr()
&& text_section.sh_addr() >= ebpf::MM_PROGRAM_START
{
text_section.sh_addr()
} else {
text_section
.sh_addr()
.saturating_add(ebpf::MM_PROGRAM_START)
},
offset_range: text_section.file_range().unwrap_or_default(),
let text_section_vaddr = if sbpf_version.enable_elf_vaddr()
&& text_section.sh_addr() >= ebpf::MM_PROGRAM_START
{
text_section.sh_addr()
} else {
text_section
.sh_addr()
.saturating_add(ebpf::MM_PROGRAM_START)
};
let vaddr_end = if sbpf_version.reject_rodata_stack_overlap() {
text_section_info
.vaddr
.saturating_add(text_section.sh_size())
text_section_vaddr.saturating_add(text_section.sh_size())
} else {
text_section_info.vaddr
text_section_vaddr
};
if (config.reject_broken_elfs
&& !sbpf_version.enable_elf_vaddr()
Expand Down Expand Up @@ -470,7 +432,10 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section,
text_section_info,
text_section: Section::Borrowed(
text_section_vaddr.saturating_sub(ebpf::MM_PROGRAM_START) as usize,
text_section.file_range().unwrap_or_default(),
),
entry_pc,
function_registry,
loader,
Expand All @@ -492,8 +457,6 @@ impl<C: ContextObject> Executable<C> {
Section::Owned(_, data) => data.capacity(),
Section::Borrowed(_, _) => 0,
})
// text section info
.saturating_add(self.text_section_info.mem_size())
// bpf functions
.saturating_add(self.function_registry.mem_size());

Expand Down

0 comments on commit db0e38f

Please sign in to comment.