ld/dwarf: remove reader resets

DWARF reader never had to reverse. Readers are already copied to achieve this effect.
Plus the position that it reverses to might be questionable.
This commit is contained in:
occheung 2024-11-28 16:39:58 +08:00 committed by sb10q
parent fce61f7b8c
commit 71dec251e3
1 changed files with 11 additions and 18 deletions

View File

@ -27,23 +27,16 @@ pub const DW_EH_PE_indirect: u8 = 0x80;
pub struct DwarfReader<'a> {
pub slice: &'a [u8],
pub virt_addr: u32,
base_slice: &'a [u8],
base_virt_addr: u32,
}
impl<'a> DwarfReader<'a> {
pub fn new(slice: &[u8], virt_addr: u32) -> DwarfReader {
DwarfReader { slice, virt_addr, base_slice: slice, base_virt_addr: virt_addr }
DwarfReader { slice, virt_addr }
}
/// Creates a new instance from another instance of [DwarfReader], optionally removing any
/// offsets previously applied to the other instance.
pub fn from_reader(other: &DwarfReader<'a>, reset_offset: bool) -> DwarfReader<'a> {
if reset_offset {
DwarfReader::new(other.base_slice, other.base_virt_addr)
} else {
DwarfReader::new(other.slice, other.virt_addr)
}
/// Creates a new instance from another instance of [DwarfReader].
pub fn from_reader(other: &DwarfReader<'a>) -> DwarfReader<'a> {
DwarfReader::new(other.slice, other.virt_addr)
}
pub fn offset(&mut self, offset: u32) {
@ -228,7 +221,7 @@ impl<'a> EH_Frame<'a> {
/// Returns an [Iterator] over all Call Frame Information (CFI) records.
pub fn cfi_records(&self) -> CFI_Records<'a> {
let reader = DwarfReader::from_reader(&self.reader, true);
let reader = DwarfReader::from_reader(&self.reader);
let len = reader.slice.len();
CFI_Records { reader, available: len }
@ -259,7 +252,7 @@ impl<'a> CFI_Record<'a> {
0xFFFF_FFFF => unimplemented!(),
_ => {
let mut fde_reader = DwarfReader::from_reader(cie_reader, false);
let mut fde_reader = DwarfReader::from_reader(cie_reader);
fde_reader.offset(length);
fde_reader
}
@ -278,7 +271,7 @@ impl<'a> CFI_Record<'a> {
// Skip code/data alignment factors & return address register along the way as well
// We only tackle the case where 'z' and 'R' are part of the augmentation string, otherwise
// we cannot get the addresses to make .eh_frame_hdr
let mut aug_data_reader = DwarfReader::from_reader(cie_reader, false);
let mut aug_data_reader = DwarfReader::from_reader(cie_reader);
let mut aug_str_len = 0;
loop {
if aug_data_reader.read_u8() == b'\0' {
@ -321,7 +314,7 @@ impl<'a> CFI_Record<'a> {
/// Returns a [DwarfReader] initialized to the first Frame Description Entry (FDE) of this CFI
/// record.
pub fn get_fde_reader(&self) -> DwarfReader<'a> {
DwarfReader::from_reader(&self.fde_reader, false)
DwarfReader::from_reader(&self.fde_reader)
}
/// Returns an [Iterator] over all Frame Description Entries (FDEs).
@ -349,7 +342,7 @@ impl<'a> Iterator for CFI_Records<'a> {
return None;
}
let mut this_reader = DwarfReader::from_reader(&self.reader, false);
let mut this_reader = DwarfReader::from_reader(&self.reader);
// Remove the length of the header and the content from the counter
let length = self.reader.read_u32();
@ -362,7 +355,7 @@ impl<'a> Iterator for CFI_Records<'a> {
// Remove the length of the header and the content from the counter
self.available -= length + mem::size_of::<u32>();
let mut next_reader = DwarfReader::from_reader(&self.reader, false);
let mut next_reader = DwarfReader::from_reader(&self.reader);
next_reader.offset(length as u32);
let cie_ptr = self.reader.read_u32();
@ -410,7 +403,7 @@ impl<'a> Iterator for FDE_Records<'a> {
// Remove the length of the header and the content from the counter
self.available -= length + mem::size_of::<u32>();
let mut next_fde_reader = DwarfReader::from_reader(&self.reader, false);
let mut next_fde_reader = DwarfReader::from_reader(&self.reader);
next_fde_reader.offset(length as u32);
let cie_ptr = self.reader.read_u32();