monistode_binutils/object_file/
relocations.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::sections::header::{RelocationTableHeader, SectionHeader};
use crate::serializable::*;
use crate::Address;

#[derive(Debug, Clone)]
pub struct Relocation {
    pub symbol: String,
    pub address: Address,
    pub relative: bool,
}

#[derive(Debug, Clone)]
struct RelocationEntry {
    section_id: u32,
    symbol_offset: usize,
    address: Address,
    relative: bool,
}

#[derive(Debug, Clone)]
pub struct RelocationTable {
    entries: Vec<RelocationEntry>,
    names: Vec<u8>,
}

impl RelocationTable {
    pub fn new() -> Self {
        RelocationTable {
            entries: Vec::new(),
            names: Vec::new(),
        }
    }

    pub fn add_relocation(&mut self, section_id: u32, relocation: Relocation) {
        let symbol_offset = self.names.len();
        self.names.extend(relocation.symbol.as_bytes());
        self.names.push(0); // null terminator

        self.entries.push(RelocationEntry {
            section_id,
            symbol_offset,
            address: relocation.address,
            relative: relocation.relative,
        });
    }

    pub fn serialize(&self) -> (SectionHeader, Vec<u8>) {
        let mut data = Vec::new();

        // Entries
        for entry in &self.entries {
            data.extend(entry.section_id.to_le_bytes());
            data.extend((entry.symbol_offset as u32).to_le_bytes());
            data.extend((entry.address.0 as u32).to_le_bytes());
            data.push(entry.relative as u8);
            data.push(0); // padding for alignment
            data.push(0);
            data.push(0);
        }

        // Names
        data.extend(&self.names);

        let header = SectionHeader::RelocationTable(RelocationTableHeader {
            entry_count: self.entries.len() as u32,
            names_length: self.names.len() as u32,
        });

        (header, data)
    }

    pub fn deserialize(
        header: &RelocationTableHeader,
        data: &[u8],
    ) -> Result<(usize, Self), SerializationError> {
        let required_size = (header.entry_count as usize * 16) + header.names_length as usize;
        if data.len() < required_size {
            return Err(SerializationError::DataTooShort);
        }

        let mut offset = 0;
        let mut entries = Vec::new();

        // Read entries
        for _ in 0..header.entry_count {
            if offset + 16 > data.len() {
                return Err(SerializationError::DataTooShort);
            }

            let section_id = u32::from_le_bytes([
                data[offset],
                data[offset + 1],
                data[offset + 2],
                data[offset + 3],
            ]);
            offset += 4;

            let symbol_offset = u32::from_le_bytes([
                data[offset],
                data[offset + 1],
                data[offset + 2],
                data[offset + 3],
            ]) as usize;
            offset += 4;

            let addr = u32::from_le_bytes([
                data[offset],
                data[offset + 1],
                data[offset + 2],
                data[offset + 3],
            ]) as usize;
            offset += 4;

            let relative = data[offset] != 0;
            offset += 4; // Skip padding bytes too

            if symbol_offset >= header.names_length as usize {
                return Err(SerializationError::InvalidData);
            }

            entries.push(RelocationEntry {
                section_id,
                symbol_offset,
                address: Address(addr),
                relative,
            });
        }

        // Read names
        if offset + header.names_length as usize > data.len() {
            return Err(SerializationError::DataTooShort);
        }
        let names = data[offset..offset + header.names_length as usize].to_vec();

        // Validate that all names are properly null-terminated
        if names.len() > 0 {
            if !names.iter().any(|&b| b == 0) {
                return Err(SerializationError::InvalidData);
            }
        }

        Ok((
            offset + header.names_length as usize,
            RelocationTable { entries, names },
        ))
    }

    pub fn get_relocations(&self, section_id: u32) -> Vec<Relocation> {
        self.entries
            .iter()
            .filter(|entry| entry.section_id == section_id)
            .map(|entry| {
                let mut symbol = String::new();
                let mut i = entry.symbol_offset as usize;
                while i < self.names.len() && self.names[i] != 0 {
                    symbol.push(self.names[i] as char);
                    i += 1;
                }
                Relocation {
                    symbol,
                    address: Address(entry.address.0),
                    relative: entry.relative,
                }
            })
            .collect()
    }
}