monistode_binutils/object_file/sections/
text.rs1use super::header::TextSectionHeader;
2use crate::object_file::relocations::Relocation;
3use crate::serializable::SerializationError;
4use crate::symbols::Symbol;
5use bitvec::prelude::*;
6
7#[derive(Debug, Clone)]
8pub struct TextSection {
9 pub data: BitVec,
10 pub symbols: Vec<Symbol>,
11 pub relocations: Vec<Relocation>,
12}
13
14impl TextSection {
15 pub fn new(data: BitVec, symbols: Vec<Symbol>, relocations: Vec<Relocation>) -> Self {
16 TextSection {
17 data,
18 symbols,
19 relocations,
20 }
21 }
22
23 pub fn serialize(&self) -> Vec<u8> {
24 let mut bytes = Vec::new();
25 for i in 0..((self.data.len() + 7) / 8) {
26 let mut byte = 0u8;
27 for j in 0..8 {
28 if i * 8 + j < self.data.len() && self.data[i * 8 + j] {
29 byte |= 1 << j;
30 }
31 }
32 bytes.push(byte);
33 }
34 bytes
35 }
36
37 pub fn deserialize(
38 header: &TextSectionHeader,
39 data: &[u8],
40 symbols: Vec<Symbol>,
41 relocations: Vec<Relocation>,
42 ) -> Result<(usize, Self), SerializationError> {
43 let required_bytes = (header.bit_length as usize + 7) / 8;
44 if data.len() < required_bytes {
45 return Err(SerializationError::DataTooShort);
46 }
47
48 let mut bits = BitVec::new();
49 for i in 0..header.bit_length as usize {
50 let bit = data[i / 8] & (1 << (i % 8)) != 0;
51 bits.push(bit);
52 }
53 let bytes_read = (header.bit_length + 7) as usize / 8;
54 Ok((
55 bytes_read,
56 TextSection {
57 data: bits,
58 symbols,
59 relocations,
60 },
61 ))
62 }
63}