Migrate SectionIndex/SymbolIndex to u32

This halves the size of structs like SectionAddress.
This commit is contained in:
2024-10-04 20:40:50 -06:00
parent 1f4b452bd5
commit b184fee73f
24 changed files with 263 additions and 215 deletions

View File

@@ -16,12 +16,15 @@ use crate::{
vm::{BranchTarget, GprValue, StepResult, VM},
RelocationTarget,
},
obj::{ObjInfo, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind},
obj::{
ObjInfo, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind,
SectionIndex,
},
};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SectionAddress {
pub section: usize,
pub section: SectionIndex,
pub address: u32,
}
@@ -38,7 +41,7 @@ impl Display for SectionAddress {
}
impl SectionAddress {
pub fn new(section: usize, address: u32) -> Self { Self { section, address } }
pub fn new(section: SectionIndex, address: u32) -> Self { Self { section, address } }
pub fn offset(self, offset: i32) -> Self {
Self { section: self.section, address: self.address.wrapping_add_signed(offset) }
@@ -116,7 +119,7 @@ pub struct AnalyzerState {
pub functions: BTreeMap<SectionAddress, FunctionInfo>,
pub jump_tables: BTreeMap<SectionAddress, u32>,
pub known_symbols: BTreeMap<SectionAddress, Vec<ObjSymbol>>,
pub known_sections: BTreeMap<usize, String>,
pub known_sections: BTreeMap<SectionIndex, String>,
}
impl AnalyzerState {

View File

@@ -18,7 +18,7 @@ struct VisitedAddresses {
impl VisitedAddresses {
pub fn new(obj: &ObjInfo) -> Self {
let mut inner = Vec::with_capacity(obj.sections.len());
let mut inner = Vec::with_capacity(obj.sections.len() as usize);
for (_, section) in obj.sections.iter() {
if section.kind == ObjSectionKind::Code {
let size = (section.size / 4) as usize;
@@ -32,11 +32,13 @@ impl VisitedAddresses {
}
pub fn contains(&self, section_address: u32, address: SectionAddress) -> bool {
self.inner[address.section].contains(Self::bit_for(section_address, address.address))
self.inner[address.section as usize]
.contains(Self::bit_for(section_address, address.address))
}
pub fn insert(&mut self, section_address: u32, address: SectionAddress) {
self.inner[address.section].insert(Self::bit_for(section_address, address.address));
self.inner[address.section as usize]
.insert(Self::bit_for(section_address, address.address));
}
#[inline]

View File

@@ -6,7 +6,9 @@ use ppc750cl::Ins;
use crate::{
analysis::cfa::SectionAddress,
array_ref,
obj::{ObjInfo, ObjKind, ObjRelocKind, ObjSection, ObjSectionKind, ObjSymbolKind},
obj::{
ObjInfo, ObjKind, ObjRelocKind, ObjSection, ObjSectionKind, ObjSymbolKind, SectionIndex,
},
};
pub mod cfa;
@@ -36,11 +38,9 @@ fn read_unresolved_relocation_address(
address: u32,
reloc_kind: Option<ObjRelocKind>,
) -> Result<Option<RelocationTarget>> {
if let Some(reloc) = obj
.unresolved_relocations
.iter()
.find(|reloc| reloc.section as usize == section.elf_index && reloc.address == address)
{
if let Some(reloc) = obj.unresolved_relocations.iter().find(|reloc| {
reloc.section as SectionIndex == section.elf_index && reloc.address == address
}) {
if reloc.module_id != obj.module_id {
return Ok(Some(RelocationTarget::External));
}
@@ -48,7 +48,7 @@ fn read_unresolved_relocation_address(
ensure!(reloc.kind == reloc_kind);
}
let (target_section_index, target_section) =
obj.sections.get_elf_index(reloc.target_section as usize).ok_or_else(|| {
obj.sections.get_elf_index(reloc.target_section as SectionIndex).ok_or_else(|| {
anyhow!(
"Failed to find target section {} for unresolved relocation",
reloc.target_section

View File

@@ -1,7 +1,7 @@
use anyhow::Result;
use crate::{
obj::{ObjDataKind, ObjInfo, ObjSectionKind, ObjSymbolKind},
obj::{ObjDataKind, ObjInfo, ObjSectionKind, ObjSymbolKind, SymbolIndex},
util::split::is_linker_generated_label,
};
@@ -64,7 +64,7 @@ pub fn detect_objects(obj: &mut ObjInfo) -> Result<()> {
}
pub fn detect_strings(obj: &mut ObjInfo) -> Result<()> {
let mut symbols_set = Vec::<(usize, ObjDataKind, usize)>::new();
let mut symbols_set = Vec::<(SymbolIndex, ObjDataKind, usize)>::new();
for (section_index, section) in obj
.sections
.iter()

View File

@@ -7,7 +7,7 @@ use crate::{
analysis::cfa::{AnalyzerState, FunctionInfo, SectionAddress},
obj::{
ObjInfo, ObjKind, ObjRelocKind, ObjSectionKind, ObjSymbol, ObjSymbolFlagSet,
ObjSymbolFlags, ObjSymbolKind,
ObjSymbolFlags, ObjSymbolKind, SectionIndex,
},
};
@@ -147,14 +147,14 @@ impl AnalysisPass for FindRelCtorsDtors {
// And the section ends with a null pointer
while let Some(reloc) = obj.unresolved_relocations.iter().find(|reloc| {
reloc.module_id == obj.module_id
&& reloc.section == section.elf_index as u8
&& reloc.section as SectionIndex == section.elf_index
&& reloc.address == current_address
&& reloc.kind == ObjRelocKind::Absolute
}) {
let Some((target_section_index, target_section)) = obj
.sections
.iter()
.find(|(_, section)| section.elf_index == reloc.target_section as usize)
let Some((target_section_index, target_section)) =
obj.sections.iter().find(|(_, section)| {
section.elf_index == reloc.target_section as SectionIndex
})
else {
return false;
};

View File

@@ -19,7 +19,7 @@ use crate::{
},
obj::{
ObjDataKind, ObjInfo, ObjKind, ObjReloc, ObjRelocKind, ObjSection, ObjSectionKind,
ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind,
ObjSymbol, ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolKind, SectionIndex, SymbolIndex,
},
};
@@ -298,7 +298,7 @@ impl Tracker {
debug_assert_ne!(
value,
RelocationTarget::Address(SectionAddress::new(
usize::MAX,
SectionIndex::MAX,
0
))
);
@@ -359,7 +359,7 @@ impl Tracker {
debug_assert_ne!(
address,
RelocationTarget::Address(SectionAddress::new(
usize::MAX,
SectionIndex::MAX,
0
))
);
@@ -380,7 +380,7 @@ impl Tracker {
debug_assert_ne!(
address,
RelocationTarget::Address(SectionAddress::new(
usize::MAX,
SectionIndex::MAX,
0
))
);
@@ -464,7 +464,7 @@ impl Tracker {
{
(addr, is_function_addr(addr))
} else {
(SectionAddress::new(usize::MAX, 0), false)
(SectionAddress::new(SectionIndex::MAX, 0), false)
};
if branch.link || !is_fn_addr {
self.relocations.insert(ins_addr, match ins.op {
@@ -549,7 +549,7 @@ impl Tracker {
fn process_data(
&mut self,
obj: &ObjInfo,
section_index: usize,
section_index: SectionIndex,
section: &ObjSection,
) -> Result<()> {
let mut addr = SectionAddress::new(section_index, section.address as u32);
@@ -602,7 +602,7 @@ impl Tracker {
} else {
// Check known relocations (function signature matching)
if self.known_relocations.contains(&from) {
return Some(SectionAddress::new(usize::MAX, addr));
return Some(SectionAddress::new(SectionIndex::MAX, addr));
}
// Check special symbols
if self.stack_address == Some(addr)
@@ -613,7 +613,7 @@ impl Tracker {
|| self.sda2_base == Some(addr)
|| self.sda_base == Some(addr)
{
return Some(SectionAddress::new(usize::MAX, addr));
return Some(SectionAddress::new(SectionIndex::MAX, addr));
}
// Not valid
None
@@ -625,7 +625,7 @@ impl Tracker {
obj: &mut ObjInfo,
addr: u32,
reloc_kind: ObjRelocKind,
) -> Option<usize> {
) -> Option<SymbolIndex> {
if !matches!(
reloc_kind,
ObjRelocKind::PpcAddr16Ha | ObjRelocKind::PpcAddr16Lo
@@ -641,7 +641,7 @@ impl Tracker {
// return generate_special_symbol(obj, addr, &name).ok();
// }
// }
let mut check_symbol = |opt: Option<u32>, name: &str| -> Option<usize> {
let mut check_symbol = |opt: Option<u32>, name: &str| -> Option<SymbolIndex> {
if let Some(value) = opt {
if addr == value {
return generate_special_symbol(obj, value, name).ok();
@@ -866,7 +866,7 @@ fn data_kind_from_op(op: Opcode) -> DataKind {
}
}
fn generate_special_symbol(obj: &mut ObjInfo, addr: u32, name: &str) -> Result<usize> {
fn generate_special_symbol(obj: &mut ObjInfo, addr: u32, name: &str) -> Result<SymbolIndex> {
obj.add_symbol(
ObjSymbol {
name: name.to_string(),