Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Refactor - dissolve text section enum #581

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ pub struct Executable<C: ContextObject> {
sbpf_version: SBPFVersion,
/// Read-only section
ro_section: Section,
/// Text section
text_section: Section,
/// Text section virtual address
text_section_vaddr: u64,
/// Text section range in `elf_bytes`
text_section_range: Range<usize>,
/// Address of the entry point
entry_pc: usize,
/// Call resolution map (hash, pc, name)
Expand All @@ -263,16 +265,9 @@ impl<C: ContextObject> Executable<C> {

/// Get the .text section virtual address and bytes
pub fn get_text_bytes(&self) -> (u64, &[u8]) {
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()])
}
};
(
// The offset field is relative to `MM_PROGRAM_START`
ebpf::MM_PROGRAM_START.saturating_add(offset as u64),
section,
self.text_section_vaddr,
&self.elf_bytes.as_slice()[self.text_section_range.clone()],
)
}

Expand All @@ -299,10 +294,7 @@ impl<C: ContextObject> Executable<C> {
/// Get the text section offset in the ELF file
#[cfg(feature = "debugger")]
pub fn get_text_section_offset(&self) -> u64 {
match &self.text_section {
Section::Owned(_offset, _data) => unreachable!(),
Section::Borrowed(_offset, byte_range) => byte_range.start as u64,
}
self.text_section_range.start as u64
}

/// Get the loader built-in program
Expand Down Expand Up @@ -363,7 +355,8 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section: Section::Borrowed(0, 0..text_bytes.len()),
text_section: Section::Borrowed(0, 0..text_bytes.len()),
text_section_vaddr: ebpf::MM_PROGRAM_START,
text_section_range: 0..text_bytes.len(),
entry_pc,
function_registry,
loader,
Expand Down Expand Up @@ -466,11 +459,8 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section,
text_section: Section::Borrowed(
// The offset field is relative to `MM_PROGRAM_START`
text_section_vaddr.saturating_sub(ebpf::MM_PROGRAM_START) as usize,
text_section.file_range().unwrap_or_default(),
),
text_section_vaddr,
text_section_range: text_section.file_range().unwrap_or_default(),
entry_pc,
function_registry,
loader,
Expand Down