use alloc::vec::Vec;
use crate::error::{Error, Result};
use crate::header::Machine;
use crate::ident::{ElfClass, Endian};
use crate::parse_utils::{array4, array8};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RelocationEntry {
pub offset: u64,
pub symbol: u32,
pub r_type: u32,
pub addend: Option<i64>,
}
impl RelocationEntry {
#[must_use]
pub const fn offset(&self) -> u64 {
self.offset
}
#[must_use]
pub const fn symbol(&self) -> u32 {
self.symbol
}
#[must_use]
pub const fn r_type(&self) -> u32 {
self.r_type
}
#[must_use]
pub const fn addend(&self) -> Option<i64> {
self.addend
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RelocationSection {
pub entries: Vec<RelocationEntry>,
pub is_rela: bool,
}
impl RelocationSection {
pub fn parse(data: &[u8], is_rela: bool, endian: Endian, class: ElfClass) -> Result<Self> {
let entry_size = match (class, is_rela) {
(ElfClass::Elf64, false) => 16usize,
(ElfClass::Elf64, true) => 24,
(ElfClass::Elf32, false) => 8,
(ElfClass::Elf32, true) => 12,
};
if !data.len().is_multiple_of(entry_size) {
return Err(Error::invalid_section(
"relocation table is not a whole number of entries",
));
}
let mut entries = Vec::with_capacity(data.len() / entry_size);
for entry in data.chunks_exact(entry_size) {
let (offset, info) = match class {
ElfClass::Elf64 => (endian.u64(array8(entry, 0)?), endian.u64(array8(entry, 8)?)),
ElfClass::Elf32 => (
u64::from(endian.u32(array4(entry, 0)?)),
u64::from(endian.u32(array4(entry, 4)?)),
),
};
let addend = if is_rela {
Some(match class {
ElfClass::Elf64 => endian.i64(array8(entry, 16)?),
ElfClass::Elf32 => i64::from(endian.i32(array4(entry, 8)?)),
})
} else {
None
};
entries.push(RelocationEntry {
offset,
symbol: split_symbol(class, info),
r_type: split_type(class, info),
addend,
});
}
Ok(Self { entries, is_rela })
}
#[must_use]
pub fn entries(&self) -> &[RelocationEntry] {
&self.entries
}
#[must_use]
pub const fn is_rela(&self) -> bool {
self.is_rela
}
}
const fn split_symbol(class: ElfClass, info: u64) -> u32 {
match class {
ElfClass::Elf64 => crate::low32(info >> 32),
ElfClass::Elf32 => crate::low32(info >> 8),
}
}
const fn split_type(class: ElfClass, info: u64) -> u32 {
match class {
ElfClass::Elf64 => crate::low32(info & 0xffff_ffff),
ElfClass::Elf32 => crate::low32(info & 0xff),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RelocKind {
Relative,
GlobalData,
JumpSlot,
Absolute,
Copy,
Tls,
Other,
}
#[must_use]
pub const fn relocation_kind(machine: Machine, r_type: u32) -> RelocKind {
match machine.value() {
62 => match r_type {
8 => RelocKind::Relative,
6 => RelocKind::GlobalData,
7 => RelocKind::JumpSlot,
1 => RelocKind::Absolute,
5 => RelocKind::Copy,
16..=18 => RelocKind::Tls,
_ => RelocKind::Other,
},
3 => match r_type {
8 => RelocKind::Relative,
6 => RelocKind::GlobalData,
7 => RelocKind::JumpSlot,
1 => RelocKind::Absolute,
5 => RelocKind::Copy,
14 | 15 | 35 | 36 | 37 => RelocKind::Tls,
_ => RelocKind::Other,
},
_ => RelocKind::Other,
}
}
#[must_use]
pub const fn relocation_width(machine: Machine, r_type: u32) -> Option<u8> {
match machine.value() {
62 => match r_type {
8 | 6 | 7 | 1 | 5 | 16 | 17 | 18 => Some(8),
2 | 10 | 11 => Some(4),
_ => None,
},
3 => match r_type {
8 | 6 | 7 | 1 | 5 => Some(4),
_ => None,
},
_ => None,
}
}