macho_core/model/
relocation.rs1use std::fmt;
2
3#[derive(Debug, Clone, Copy)]
5pub enum Relocation {
6 Standard(StandardRelocation),
8 Scattered(ScatteredRelocation),
10}
11
12impl Relocation {
13 pub fn address(&self) -> u32 {
15 match self {
16 Self::Standard(r) => r.address,
17 Self::Scattered(r) => r.address,
18 }
19 }
20
21 pub fn reloc_type(&self) -> u8 {
23 match self {
24 Self::Standard(r) => r.reloc_type,
25 Self::Scattered(r) => r.reloc_type,
26 }
27 }
28
29 pub fn length(&self) -> u8 {
31 match self {
32 Self::Standard(r) => r.length,
33 Self::Scattered(r) => r.length,
34 }
35 }
36
37 pub fn pc_relative(&self) -> bool {
39 match self {
40 Self::Standard(r) => r.pc_relative,
41 Self::Scattered(r) => r.pc_relative,
42 }
43 }
44
45 pub fn is_scattered(&self) -> bool {
47 matches!(self, Self::Scattered(_))
48 }
49}
50
51impl fmt::Display for Relocation {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 Self::Standard(r) => write!(
55 f,
56 "addr={:#x} type={} len={} pcrel={} extern={} sym={}",
57 r.address, r.reloc_type, r.length, r.pc_relative, r.is_extern, r.symbol_num
58 ),
59 Self::Scattered(r) => write!(
60 f,
61 "SCATTERED addr={:#x} type={} len={} pcrel={} value={:#x}",
62 r.address, r.reloc_type, r.length, r.pc_relative, r.value
63 ),
64 }
65 }
66}
67
68#[derive(Debug, Clone, Copy)]
69pub struct StandardRelocation {
71 pub address: u32,
73 pub symbol_num: u32,
75 pub pc_relative: bool,
77 pub length: u8,
79 pub is_extern: bool,
81 pub reloc_type: u8,
83}
84
85#[derive(Debug, Clone, Copy)]
86pub struct ScatteredRelocation {
88 pub reloc_type: u8,
90 pub length: u8,
92 pub pc_relative: bool,
94 pub address: u32,
96 pub value: i32,
98}