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

Commit

Permalink
squash
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Jul 3, 2024
1 parent ee03a06 commit 7c70307
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,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 @@ -230,16 +232,22 @@ 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()])
}
let data = if self.sbpf_version == SBPFVersion::V1 {
let (ro_offset, ro_section) = match &self.ro_section {
Section::Owned(offset, data) => (*offset, data.as_slice()),
Section::Borrowed(offset, byte_range) => {
(*offset, &self.elf_bytes.as_slice()[byte_range.clone()])
}
};
let offset = self
.text_section_vaddr
.saturating_sub(ebpf::MM_PROGRAM_START)
.saturating_sub(ro_offset as u64) as usize;
&ro_section[offset..offset.saturating_add(self.text_section_range.len())]
} else {
&self.elf_bytes.as_slice()[self.text_section_range.clone()]
};
(
ebpf::MM_PROGRAM_START.saturating_add(offset as u64),
section,
)
(self.text_section_vaddr, data)
}

/// Get the concatenated read-only sections (including the text section)
Expand All @@ -265,9 +273,10 @@ 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,
if self.sbpf_version == SBPFVersion::V1 {
self.text_section_range.start as u64
} else {
self.text_section_range.start as u64
}
}

Expand Down Expand Up @@ -329,7 +338,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 @@ -438,10 +448,8 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section,
text_section: Section::Borrowed(
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

0 comments on commit 7c70307

Please sign in to comment.