goblin/mach/
relocation.rsuse crate::mach;
use core::fmt;
use scroll::{IOread, IOwrite, Pread, Pwrite, SizeWith};
#[derive(Copy, Clone, Pread, Pwrite, IOwrite, SizeWith, IOread)]
#[repr(C)]
pub struct RelocationInfo {
pub r_address: i32,
pub r_info: u32,
}
pub const SIZEOF_RELOCATION_INFO: usize = 8;
impl RelocationInfo {
#[inline]
pub fn r_symbolnum(self) -> usize {
(self.r_info & 0x00ff_ffffu32) as usize
}
#[inline]
pub fn r_pcrel(self) -> u8 {
((self.r_info & 0x0100_0000u32) >> 24) as u8
}
#[inline]
pub fn r_length(self) -> u8 {
((self.r_info & 0x0600_0000u32) >> 25) as u8
}
#[inline]
pub fn r_extern(self) -> u8 {
((self.r_info & 0x0800_0000) >> 27) as u8
}
#[inline]
pub fn r_type(self) -> u8 {
((self.r_info & 0xf000_0000) >> 28) as u8
}
#[inline]
pub fn is_extern(self) -> bool {
self.r_extern() == 1
}
#[inline]
pub fn is_pic(self) -> bool {
self.r_pcrel() > 0
}
pub fn to_str(self, cputype: mach::cputype::CpuType) -> &'static str {
reloc_to_str(self.r_type(), cputype)
}
}
pub const R_ABS: u8 = 0;
impl fmt::Debug for RelocationInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("RelocationInfo")
.field("r_address", &format_args!("{:#x}", &self.r_address))
.field("r_info", &format_args!("{:#x}", &self.r_info))
.field("r_symbolnum", &format_args!("{:#x}", &self.r_symbolnum()))
.field("r_pcrel", &(self.r_pcrel()))
.field("r_length", &self.r_length())
.field("r_extern", &self.r_extern())
.field("r_type", &self.r_type())
.finish()
}
}
pub type RelocType = u8;
pub const X86_64_RELOC_UNSIGNED: RelocType = 0;
pub const X86_64_RELOC_SIGNED: RelocType = 1;
pub const X86_64_RELOC_BRANCH: RelocType = 2;
pub const X86_64_RELOC_GOT_LOAD: RelocType = 3;
pub const X86_64_RELOC_GOT: RelocType = 4;
pub const X86_64_RELOC_SUBTRACTOR: RelocType = 5;
pub const X86_64_RELOC_SIGNED_1: RelocType = 6;
pub const X86_64_RELOC_SIGNED_2: RelocType = 7;
pub const X86_64_RELOC_SIGNED_4: RelocType = 8;
pub const X86_64_RELOC_TLV: RelocType = 9;
pub const GENERIC_RELOC_VANILLA: RelocType = 0;
pub const GENERIC_RELOC_PAIR: RelocType = 1;
pub const GENERIC_RELOC_SECTDIFF: RelocType = 2;
pub const GENERIC_RELOC_PB_LA_PTR: RelocType = 3;
pub const GENERIC_RELOC_LOCAL_SECTDIFF: RelocType = 4;
pub const GENERIC_RELOC_TLV: RelocType = 5;
pub const ARM_RELOC_VANILLA: RelocType = GENERIC_RELOC_VANILLA;
pub const ARM_RELOC_PAIR: RelocType = GENERIC_RELOC_PAIR;
pub const ARM_RELOC_SECTDIFF: RelocType = GENERIC_RELOC_SECTDIFF;
pub const ARM_RELOC_LOCAL_SECTDIFF: RelocType = 3;
pub const ARM_RELOC_PB_LA_PTR: RelocType = 4;
pub const ARM_RELOC_BR24: RelocType = 5;
pub const ARM_THUMB_RELOC_BR22: RelocType = 6;
pub const ARM_THUMB_32BIT_BRANCH: RelocType = 7;
pub const ARM_RELOC_HALF: RelocType = 8;
pub const ARM_RELOC_HALF_SECTDIFF: RelocType = 9;
pub const ARM64_RELOC_UNSIGNED: RelocType = 0;
pub const ARM64_RELOC_SUBTRACTOR: RelocType = 1;
pub const ARM64_RELOC_BRANCH26: RelocType = 2;
pub const ARM64_RELOC_PAGE21: RelocType = 3;
pub const ARM64_RELOC_PAGEOFF12: RelocType = 4;
pub const ARM64_RELOC_GOT_LOAD_PAGE21: RelocType = 5;
pub const ARM64_RELOC_GOT_LOAD_PAGEOFF12: RelocType = 6;
pub const ARM64_RELOC_POINTER_TO_GOT: RelocType = 7;
pub const ARM64_RELOC_TLVP_LOAD_PAGE21: RelocType = 8;
pub const ARM64_RELOC_TLVP_LOAD_PAGEOFF12: RelocType = 9;
pub const ARM64_RELOC_ADDEND: RelocType = 10;
pub fn reloc_to_str(reloc: RelocType, cputype: mach::cputype::CpuType) -> &'static str {
use crate::mach::constants::cputype::*;
match cputype {
CPU_TYPE_ARM64 | CPU_TYPE_ARM64_32 => match reloc {
ARM64_RELOC_UNSIGNED => "ARM64_RELOC_UNSIGNED",
ARM64_RELOC_SUBTRACTOR => "ARM64_RELOC_SUBTRACTOR",
ARM64_RELOC_BRANCH26 => "ARM64_RELOC_BRANCH26",
ARM64_RELOC_PAGE21 => "ARM64_RELOC_PAGE21",
ARM64_RELOC_PAGEOFF12 => "ARM64_RELOC_PAGEOFF12",
ARM64_RELOC_GOT_LOAD_PAGE21 => "ARM64_RELOC_GOT_LOAD_PAGE21",
ARM64_RELOC_GOT_LOAD_PAGEOFF12 => "ARM64_RELOC_GOT_LOAD_PAGEOFF12",
ARM64_RELOC_POINTER_TO_GOT => "ARM64_RELOC_POINTER_TO_GOT",
ARM64_RELOC_TLVP_LOAD_PAGE21 => "ARM64_RELOC_TLVP_LOAD_PAGE21",
ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
ARM64_RELOC_ADDEND => "ARM64_RELOC_ADDEND",
_ => "UNKNOWN",
},
CPU_TYPE_X86_64 => match reloc {
X86_64_RELOC_UNSIGNED => "X86_64_RELOC_UNSIGNED",
X86_64_RELOC_SIGNED => "X86_64_RELOC_SIGNED",
X86_64_RELOC_BRANCH => "X86_64_RELOC_BRANCH",
X86_64_RELOC_GOT_LOAD => "X86_64_RELOC_GOT_LOAD",
X86_64_RELOC_GOT => "X86_64_RELOC_GOT",
X86_64_RELOC_SUBTRACTOR => "X86_64_RELOC_SUBTRACTOR",
X86_64_RELOC_SIGNED_1 => "X86_64_RELOC_SIGNED_1",
X86_64_RELOC_SIGNED_2 => "X86_64_RELOC_SIGNED_2",
X86_64_RELOC_SIGNED_4 => "X86_64_RELOC_SIGNED_4",
X86_64_RELOC_TLV => "X86_64_RELOC_TLV",
_ => "UNKNOWN",
},
CPU_TYPE_ARM => match reloc {
ARM_RELOC_VANILLA => "ARM_RELOC_VANILLA",
ARM_RELOC_PAIR => "ARM_RELOC_PAIR",
ARM_RELOC_SECTDIFF => "ARM_RELOC_SECTDIFF",
ARM_RELOC_LOCAL_SECTDIFF => "ARM_RELOC_LOCAL_SECTDIFF",
ARM_RELOC_PB_LA_PTR => "ARM_RELOC_PB_LA_PTR",
ARM_RELOC_BR24 => "ARM_RELOC_BR24",
ARM_THUMB_RELOC_BR22 => "ARM_THUMB_RELOC_BR22",
ARM_THUMB_32BIT_BRANCH => "ARM_THUMB_32BIT_BRANCH",
ARM_RELOC_HALF => "ARM_RELOC_HALF",
ARM_RELOC_HALF_SECTDIFF => "ARM_RELOC_HALF_SECTDIFF",
_ => "UNKNOWN",
},
CPU_TYPE_X86 => match reloc {
GENERIC_RELOC_VANILLA => "GENERIC_RELOC_VANILLA",
GENERIC_RELOC_PAIR => "GENERIC_RELOC_PAIR",
GENERIC_RELOC_SECTDIFF => "GENERIC_RELOC_SECTDIFF",
GENERIC_RELOC_PB_LA_PTR => "GENERIC_RELOC_PB_LA_PTR",
GENERIC_RELOC_LOCAL_SECTDIFF => "GENERIC_RELOC_LOCAL_SECTDIFF",
GENERIC_RELOC_TLV => "GENERIC_RELOC_TLV",
_ => "UNKNOWN",
},
_ => "BAD_CPUTYPE",
}
}