sbpf_disassembler/
section_header.rs

1use {
2    crate::{errors::DisassemblerError, section_header_entry::SectionHeaderEntry},
3    object::{Endianness, read::elf::ElfFile64},
4    serde::{Deserialize, Serialize},
5    std::fmt::{Debug, Display},
6};
7
8#[allow(non_camel_case_types)]
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[repr(u32)]
11pub enum SectionHeaderType {
12    SHT_NULL = 0x00,           // Section header table entry unused
13    SHT_PROGBITS = 0x01,       // Program data
14    SHT_SYMTAB = 0x02,         // Symbol table
15    SHT_STRTAB = 0x03,         // String table
16    SHT_RELA = 0x04,           // Relocation entries with addends
17    SHT_HASH = 0x05,           // Symbol hash table
18    SHT_DYNAMIC = 0x06,        // Dynamic linking information
19    SHT_NOTE = 0x07,           // Notes
20    SHT_NOBITS = 0x08,         // Program space with no data (bss)
21    SHT_REL = 0x09,            // Relocation entries, no addends
22    SHT_SHLIB = 0x0A,          // Reserved
23    SHT_DYNSYM = 0x0B,         // Dynamic linker symbol table
24    SHT_INIT_ARRAY = 0x0E,     // Array of constructors
25    SHT_FINI_ARRAY = 0x0F,     // Array of destructors
26    SHT_PREINIT_ARRAY = 0x10,  // Array of pre-constructors
27    SHT_GROUP = 0x11,          // Section group
28    SHT_SYMTAB_SHNDX = 0x12,   // Extended section indices
29    SHT_NUM = 0x13,            // Number of defined types.
30    SHT_GNU_HASH = 0x6ffffff6, // GNU Hash
31}
32
33impl TryFrom<u32> for SectionHeaderType {
34    type Error = DisassemblerError;
35
36    fn try_from(value: u32) -> Result<Self, Self::Error> {
37        Ok(match value {
38            0x00 => Self::SHT_NULL,
39            0x01 => Self::SHT_PROGBITS,
40            0x02 => Self::SHT_SYMTAB,
41            0x03 => Self::SHT_STRTAB,
42            0x04 => Self::SHT_RELA,
43            0x05 => Self::SHT_HASH,
44            0x06 => Self::SHT_DYNAMIC,
45            0x07 => Self::SHT_NOTE,
46            0x08 => Self::SHT_NOBITS,
47            0x09 => Self::SHT_REL,
48            0x0A => Self::SHT_SHLIB,
49            0x0B => Self::SHT_DYNSYM,
50            0x0E => Self::SHT_INIT_ARRAY,
51            0x0F => Self::SHT_FINI_ARRAY,
52            0x10 => Self::SHT_PREINIT_ARRAY,
53            0x11 => Self::SHT_GROUP,
54            0x12 => Self::SHT_SYMTAB_SHNDX,
55            0x13 => Self::SHT_NUM,
56            0x6ffffff6 => Self::SHT_GNU_HASH,
57            _ => return Err(DisassemblerError::InvalidSectionHeaderType),
58        })
59    }
60}
61
62impl Display for SectionHeaderType {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        f.write_str(Into::<&str>::into(self.clone()))
65    }
66}
67
68impl From<SectionHeaderType> for &str {
69    fn from(val: SectionHeaderType) -> Self {
70        match val {
71            SectionHeaderType::SHT_NULL => "SHT_NULL",
72            SectionHeaderType::SHT_PROGBITS => "SHT_PROGBITS",
73            SectionHeaderType::SHT_SYMTAB => "SHT_SYMTAB",
74            SectionHeaderType::SHT_STRTAB => "SHT_STRTAB",
75            SectionHeaderType::SHT_RELA => "SHT_RELA",
76            SectionHeaderType::SHT_HASH => "SHT_HASH",
77            SectionHeaderType::SHT_DYNAMIC => "SHT_DYNAMIC",
78            SectionHeaderType::SHT_NOTE => "SHT_NOTE",
79            SectionHeaderType::SHT_NOBITS => "SHT_NOBITS",
80            SectionHeaderType::SHT_REL => "SHT_REL",
81            SectionHeaderType::SHT_SHLIB => "SHT_SHLIB",
82            SectionHeaderType::SHT_DYNSYM => "SHT_DYNSYM",
83            SectionHeaderType::SHT_INIT_ARRAY => "SHT_INIT_ARRAY",
84            SectionHeaderType::SHT_FINI_ARRAY => "SHT_FINI_ARRAY",
85            SectionHeaderType::SHT_PREINIT_ARRAY => "SHT_PREINIT_ARRAY",
86            SectionHeaderType::SHT_GROUP => "SHT_GROUP",
87            SectionHeaderType::SHT_SYMTAB_SHNDX => "SHT_SYMTAB_SHNDX",
88            SectionHeaderType::SHT_NUM => "SHT_NUM",
89            SectionHeaderType::SHT_GNU_HASH => "SHT_GNU_HASH",
90        }
91    }
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct SectionHeader {
96    pub sh_name: u32, // An offset to a string in the .shstrtab section that represents the name of this section.
97    pub sh_type: SectionHeaderType, // Identifies the type of this header.
98    pub sh_flags: u64, // Identifies the attributes of the section.
99    pub sh_addr: u64, // Virtual address of the section in memory, for sections that are loaded.
100    pub sh_offset: u64, // Offset of the section in the file image.
101    pub sh_size: u64, // Size in bytes of the section in the file image. May be 0.
102    pub sh_link: u32, // Contains the section index of an associated section. This field is used for several purposes, depending on the type of section.
103    pub sh_info: u32, // Contains extra information about the section. This field is used for several purposes, depending on the type of section.
104    pub sh_addralign: u64, // Contains the required alignment of the section. This field must be a power of two.
105    pub sh_entsize: u64, // Contains the size, in bytes, of each entry, for sections that contain fixed-size entries. Otherwise, this field contains zero.
106}
107
108impl SectionHeader {
109    pub fn from_elf_file(
110        elf_file: &ElfFile64<Endianness>,
111    ) -> Result<(Vec<Self>, Vec<SectionHeaderEntry>), DisassemblerError> {
112        let endian = elf_file.endian();
113        let section_headers_data: Vec<_> = elf_file.elf_section_table().iter().collect();
114
115        let mut section_headers = Vec::new();
116        for sh in section_headers_data.iter() {
117            let sh_name = sh.sh_name.get(endian);
118            let sh_type = SectionHeaderType::try_from(sh.sh_type.get(endian))?;
119            let sh_flags = sh.sh_flags.get(endian);
120            let sh_addr = sh.sh_addr.get(endian);
121            let sh_offset = sh.sh_offset.get(endian);
122            let sh_size = sh.sh_size.get(endian);
123            let sh_link = sh.sh_link.get(endian);
124            let sh_info = sh.sh_info.get(endian);
125            let sh_addralign = sh.sh_addralign.get(endian);
126            let sh_entsize = sh.sh_entsize.get(endian);
127
128            section_headers.push(SectionHeader {
129                sh_name,
130                sh_type,
131                sh_flags,
132                sh_addr,
133                sh_offset,
134                sh_size,
135                sh_link,
136                sh_info,
137                sh_addralign,
138                sh_entsize,
139            });
140        }
141
142        let elf_header = elf_file.elf_header();
143        let e_shstrndx = elf_header.e_shstrndx.get(endian);
144        let shstrndx = &section_headers[e_shstrndx as usize];
145        let shstrndx_value = elf_file.data()
146            [shstrndx.sh_offset as usize..shstrndx.sh_offset as usize + shstrndx.sh_size as usize]
147            .to_vec();
148
149        let mut indices: Vec<u32> = section_headers.iter().map(|h| h.sh_name).collect();
150        indices.push(shstrndx.sh_size as u32);
151        indices.sort_unstable();
152
153        let section_header_entries = section_headers
154            .iter()
155            .map(|s| {
156                let current_offset = s.sh_name as usize;
157                let next_index = indices.binary_search(&s.sh_name).unwrap() + 1;
158                let next_offset = *indices
159                    .get(next_index)
160                    .ok_or(DisassemblerError::InvalidString)?
161                    as usize;
162
163                let label = String::from_utf8(shstrndx_value[current_offset..next_offset].to_vec())
164                    .unwrap_or("default".to_string());
165
166                let data = elf_file.data()
167                    [s.sh_offset as usize..s.sh_offset as usize + s.sh_size as usize]
168                    .to_vec();
169
170                SectionHeaderEntry::new(label, s.sh_offset as usize, data)
171            })
172            .collect::<Result<Vec<_>, _>>()?;
173
174        Ok((section_headers, section_header_entries))
175    }
176
177    pub fn to_bytes(&self) -> Vec<u8> {
178        let mut b = self.sh_name.to_le_bytes().to_vec();
179        b.extend_from_slice(&(self.sh_type.clone() as u32).to_le_bytes());
180        b.extend_from_slice(&self.sh_flags.to_le_bytes());
181        b.extend_from_slice(&self.sh_addr.to_le_bytes());
182        b.extend_from_slice(&self.sh_offset.to_le_bytes());
183        b.extend_from_slice(&self.sh_size.to_le_bytes());
184        b.extend_from_slice(&self.sh_link.to_le_bytes());
185        b.extend_from_slice(&self.sh_info.to_le_bytes());
186        b.extend_from_slice(&self.sh_addralign.to_le_bytes());
187        b.extend_from_slice(&self.sh_entsize.to_le_bytes());
188        b
189    }
190}
191
192#[cfg(test)]
193mod tests {
194    use {super::*, crate::program::Program, hex_literal::hex};
195
196    #[test]
197    fn test_section_headers() {
198        let program = Program::from_bytes(&hex!("7F454C460201010000000000000000000300F700010000002001000000000000400000000000000028020000000000000000000040003800030040000600050001000000050000002001000000000000200100000000000020010000000000003000000000000000300000000000000000100000000000000100000004000000C001000000000000C001000000000000C0010000000000003C000000000000003C000000000000000010000000000000020000000600000050010000000000005001000000000000500100000000000070000000000000007000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007912A000000000007911182900000000B7000000010000002D21010000000000B70000000000000095000000000000001E0000000000000004000000000000000600000000000000C0010000000000000B0000000000000018000000000000000500000000000000F0010000000000000A000000000000000C00000000000000160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000120001002001000000000000300000000000000000656E747279706F696E7400002E74657874002E64796E737472002E64796E73796D002E64796E616D6963002E73687374727461620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010000000600000000000000200100000000000020010000000000003000000000000000000000000000000008000000000000000000000000000000170000000600000003000000000000005001000000000000500100000000000070000000000000000400000000000000080000000000000010000000000000000F0000000B0000000200000000000000C001000000000000C001000000000000300000000000000004000000010000000800000000000000180000000000000007000000030000000200000000000000F001000000000000F0010000000000000C00000000000000000000000000000001000000000000000000000000000000200000000300000000000000000000000000000000000000FC010000000000002A00000000000000000000000000000001000000000000000000000000000000")).unwrap();
199
200        // Verify we have the expected number of section headers.
201        assert_eq!(program.section_headers.len(), 6);
202        assert_eq!(program.section_header_entries.len(), 6);
203    }
204
205    #[test]
206    fn test_section_header_type_conversions() {
207        // Test all valid TryFrom conversions.
208        assert!(matches!(
209            SectionHeaderType::try_from(0x00),
210            Ok(SectionHeaderType::SHT_NULL)
211        ));
212        assert!(matches!(
213            SectionHeaderType::try_from(0x01),
214            Ok(SectionHeaderType::SHT_PROGBITS)
215        ));
216        assert!(matches!(
217            SectionHeaderType::try_from(0x02),
218            Ok(SectionHeaderType::SHT_SYMTAB)
219        ));
220        assert!(matches!(
221            SectionHeaderType::try_from(0x03),
222            Ok(SectionHeaderType::SHT_STRTAB)
223        ));
224        assert!(matches!(
225            SectionHeaderType::try_from(0x04),
226            Ok(SectionHeaderType::SHT_RELA)
227        ));
228        assert!(matches!(
229            SectionHeaderType::try_from(0x05),
230            Ok(SectionHeaderType::SHT_HASH)
231        ));
232        assert!(matches!(
233            SectionHeaderType::try_from(0x06),
234            Ok(SectionHeaderType::SHT_DYNAMIC)
235        ));
236        assert!(matches!(
237            SectionHeaderType::try_from(0x07),
238            Ok(SectionHeaderType::SHT_NOTE)
239        ));
240        assert!(matches!(
241            SectionHeaderType::try_from(0x08),
242            Ok(SectionHeaderType::SHT_NOBITS)
243        ));
244        assert!(matches!(
245            SectionHeaderType::try_from(0x09),
246            Ok(SectionHeaderType::SHT_REL)
247        ));
248        assert!(matches!(
249            SectionHeaderType::try_from(0x0A),
250            Ok(SectionHeaderType::SHT_SHLIB)
251        ));
252        assert!(matches!(
253            SectionHeaderType::try_from(0x0B),
254            Ok(SectionHeaderType::SHT_DYNSYM)
255        ));
256        assert!(matches!(
257            SectionHeaderType::try_from(0x0E),
258            Ok(SectionHeaderType::SHT_INIT_ARRAY)
259        ));
260        assert!(matches!(
261            SectionHeaderType::try_from(0x0F),
262            Ok(SectionHeaderType::SHT_FINI_ARRAY)
263        ));
264        assert!(matches!(
265            SectionHeaderType::try_from(0x10),
266            Ok(SectionHeaderType::SHT_PREINIT_ARRAY)
267        ));
268        assert!(matches!(
269            SectionHeaderType::try_from(0x11),
270            Ok(SectionHeaderType::SHT_GROUP)
271        ));
272        assert!(matches!(
273            SectionHeaderType::try_from(0x12),
274            Ok(SectionHeaderType::SHT_SYMTAB_SHNDX)
275        ));
276        assert!(matches!(
277            SectionHeaderType::try_from(0x13),
278            Ok(SectionHeaderType::SHT_NUM)
279        ));
280        assert!(matches!(
281            SectionHeaderType::try_from(0x6ffffff6),
282            Ok(SectionHeaderType::SHT_GNU_HASH)
283        ));
284
285        // Test invalid value
286        assert!(SectionHeaderType::try_from(0xFF).is_err());
287    }
288
289    #[test]
290    fn test_section_header_type_to_str() {
291        // Test all Into<&str> conversions.
292        assert_eq!(<&str>::from(SectionHeaderType::SHT_NULL), "SHT_NULL");
293        assert_eq!(
294            <&str>::from(SectionHeaderType::SHT_PROGBITS),
295            "SHT_PROGBITS"
296        );
297        assert_eq!(<&str>::from(SectionHeaderType::SHT_SYMTAB), "SHT_SYMTAB");
298        assert_eq!(<&str>::from(SectionHeaderType::SHT_STRTAB), "SHT_STRTAB");
299        assert_eq!(<&str>::from(SectionHeaderType::SHT_RELA), "SHT_RELA");
300        assert_eq!(<&str>::from(SectionHeaderType::SHT_HASH), "SHT_HASH");
301        assert_eq!(<&str>::from(SectionHeaderType::SHT_DYNAMIC), "SHT_DYNAMIC");
302        assert_eq!(<&str>::from(SectionHeaderType::SHT_NOTE), "SHT_NOTE");
303        assert_eq!(<&str>::from(SectionHeaderType::SHT_NOBITS), "SHT_NOBITS");
304        assert_eq!(<&str>::from(SectionHeaderType::SHT_REL), "SHT_REL");
305        assert_eq!(<&str>::from(SectionHeaderType::SHT_SHLIB), "SHT_SHLIB");
306        assert_eq!(<&str>::from(SectionHeaderType::SHT_DYNSYM), "SHT_DYNSYM");
307        assert_eq!(
308            <&str>::from(SectionHeaderType::SHT_INIT_ARRAY),
309            "SHT_INIT_ARRAY"
310        );
311        assert_eq!(
312            <&str>::from(SectionHeaderType::SHT_FINI_ARRAY),
313            "SHT_FINI_ARRAY"
314        );
315        assert_eq!(
316            <&str>::from(SectionHeaderType::SHT_PREINIT_ARRAY),
317            "SHT_PREINIT_ARRAY"
318        );
319        assert_eq!(<&str>::from(SectionHeaderType::SHT_GROUP), "SHT_GROUP");
320        assert_eq!(
321            <&str>::from(SectionHeaderType::SHT_SYMTAB_SHNDX),
322            "SHT_SYMTAB_SHNDX"
323        );
324        assert_eq!(<&str>::from(SectionHeaderType::SHT_NUM), "SHT_NUM");
325        assert_eq!(
326            <&str>::from(SectionHeaderType::SHT_GNU_HASH),
327            "SHT_GNU_HASH"
328        );
329    }
330
331    #[test]
332    fn test_section_header_type_display() {
333        assert_eq!(SectionHeaderType::SHT_PROGBITS.to_string(), "SHT_PROGBITS");
334        assert_eq!(SectionHeaderType::SHT_DYNAMIC.to_string(), "SHT_DYNAMIC");
335    }
336
337    #[test]
338    fn test_section_header_to_bytes() {
339        let header = SectionHeader {
340            sh_name: 1,
341            sh_type: SectionHeaderType::SHT_PROGBITS,
342            sh_flags: 6,
343            sh_addr: 0x120,
344            sh_offset: 0x120,
345            sh_size: 48,
346            sh_link: 0,
347            sh_info: 0,
348            sh_addralign: 8,
349            sh_entsize: 0,
350        };
351
352        let bytes = header.to_bytes();
353        assert_eq!(bytes.len(), 64);
354
355        // Check first few fields.
356        assert_eq!(
357            u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
358            1
359        );
360        assert_eq!(
361            u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
362            1
363        );
364    }
365}