Debian Codegraph @

third_party/rust/goblin/src/pe/tls.rs

raw
1 use crate::error;
2 use alloc::vec::Vec;
3 use scroll::{Pread, Pwrite, SizeWith};
4
5 use crate::pe::data_directories;
6 use crate::pe::options;
7 use crate::pe::section_table;
8 use crate::pe::utils;
9
10 /// Represents the TLS directory `IMAGE_TLS_DIRECTORY64`.
11 #[repr(C)]
12 #[derive(Debug, PartialEq, Copy, Clone, Default, Pread, Pwrite, SizeWith)]
13 pub struct ImageTlsDirectory {
14 /// The starting address of the TLS raw data.
15 // NOTE: `u32` for 32-bit binaries, `u64` for 64-bit binaries.
16 pub start_address_of_raw_data: u64,
17 /// The ending address of the TLS raw data.
18 // NOTE: `u32` for 32-bit binaries, `u64` for 64-bit binaries.
19 pub end_address_of_raw_data: u64,
20 /// The address of the TLS index.
21 // NOTE: `u32` for 32-bit binaries, `u64` for 64-bit binaries.
22 pub address_of_index: u64,
23 /// The address of the TLS callback functions.
24 ///
25 /// Terminated by a null pointer.
26 // NOTE: `u32` for 32-bit binaries, `u64` for 64-bit binaries.
27 pub address_of_callbacks: u64,
28 /// The size of the zero fill.
29 pub size_of_zero_fill: u32,
30 /// The characteristics of the TLS.
31 pub characteristics: u32,
32 }
33
34 /// TLS information.
35 #[derive(Debug, Clone, PartialEq, Default)]
36 pub struct TlsData<'a> {
37 /// TLS directory.
38 pub image_tls_directory: ImageTlsDirectory,
39 /// Raw data of the TLS.
40 pub raw_data: Option<&'a [u8]>,
41 /// TLS index.
42 pub slot: Option<u32>,
43 /// TLS callbacks.
44 pub callbacks: Vec<u64>,
45 }
46
47 impl ImageTlsDirectory {
48 pub fn parse<T: Sized>(
49 bytes: &[u8],
50 dd: data_directories::DataDirectory,
51 sections: &[section_table::SectionTable],
52 file_alignment: u32,
53 ) -> error::Result<Self> {
54 Self::parse_with_opts::<T>(
55 bytes,
56 dd,
57 sections,
58 file_alignment,
59 &options::ParseOptions::default(),
60 )
61 }
62
63 pub fn parse_with_opts<T: Sized>(
64 bytes: &[u8],
65 dd: data_directories::DataDirectory,
66 sections: &[section_table::SectionTable],
67 file_alignment: u32,
68 opts: &options::ParseOptions,
69 ) -> error::Result<Self> {
70 let rva = dd.virtual_address as usize;
71 let mut offset =
72 utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
73 error::Error::Malformed(format!(
74 "Cannot map ImageTlsDirectory rva {:#x} into offset",
75 rva
76 ))
77 })?;
78
79 let is_64 = core::mem::size_of::<T>() == 8;
80
81 let start_address_of_raw_data = if is_64 {
82 bytes.gread_with::<u64>(&mut offset, scroll::LE)?
83 } else {
84 bytes.gread_with::<u32>(&mut offset, scroll::LE)? as u64
85 };
86 let end_address_of_raw_data = if is_64 {
87 bytes.gread_with::<u64>(&mut offset, scroll::LE)?
88 } else {
89 bytes.gread_with::<u32>(&mut offset, scroll::LE)? as u64
90 };
91 let address_of_index = if is_64 {
92 bytes.gread_with::<u64>(&mut offset, scroll::LE)?
93 } else {
94 bytes.gread_with::<u32>(&mut offset, scroll::LE)? as u64
95 };
96 let address_of_callbacks = if is_64 {
97 bytes.gread_with::<u64>(&mut offset, scroll::LE)?
98 } else {
99 bytes.gread_with::<u32>(&mut offset, scroll::LE)? as u64
100 };
101 let size_of_zero_fill = bytes.gread_with::<u32>(&mut offset, scroll::LE)?;
102 let characteristics = bytes.gread_with::<u32>(&mut offset, scroll::LE)?;
103
104 let itd = Self {
105 start_address_of_raw_data,
106 end_address_of_raw_data,
107 address_of_index,
108 address_of_callbacks,
109 size_of_zero_fill,
110 characteristics,
111 };
112
113 Ok(itd)
114 }
115 }
116
117 impl<'a> TlsData<'a> {
118 pub fn parse<T: Sized>(
119 bytes: &'a [u8],
120 image_base: usize,
121 dd: &data_directories::DataDirectory,
122 sections: &[section_table::SectionTable],
123 file_alignment: u32,
124 ) -> error::Result<Option<Self>> {
125 Self::parse_with_opts::<T>(
126 bytes,
127 image_base,
128 dd,
129 sections,
130 file_alignment,
131 &options::ParseOptions::default(),
132 )
133 }
134
135 pub fn parse_with_opts<T: Sized>(
136 bytes: &'a [u8],
137 image_base: usize,
138 dd: &data_directories::DataDirectory,
139 sections: &[section_table::SectionTable],
140 file_alignment: u32,
141 opts: &options::ParseOptions,
142 ) -> error::Result<Option<Self>> {
143 let mut raw_data = None;
144 let mut slot = None;
145 let mut callbacks = Vec::new();
146
147 let is_64 = core::mem::size_of::<T>() == 8;
148
149 let itd =
150 ImageTlsDirectory::parse_with_opts::<T>(bytes, *dd, sections, file_alignment, opts)?;
151
152 // Parse the raw data if any
153 if itd.end_address_of_raw_data != 0 && itd.start_address_of_raw_data != 0 {
154 if itd.start_address_of_raw_data > itd.end_address_of_raw_data {
155 return Err(error::Error::Malformed(format!(
156 "tls start_address_of_raw_data ({:#x}) is greater than end_address_of_raw_data ({:#x})",
157 itd.start_address_of_raw_data,
158 itd.end_address_of_raw_data
159 )));
160 }
161
162 if (itd.start_address_of_raw_data as usize) < image_base {
163 return Err(error::Error::Malformed(format!(
164 "tls start_address_of_raw_data ({:#x}) is less than image base ({:#x})",
165 itd.start_address_of_raw_data, image_base
166 )));
167 }
168
169 // VA to RVA
170 let rva = itd.start_address_of_raw_data as usize - image_base;
171 let size = itd.end_address_of_raw_data - itd.start_address_of_raw_data;
172 let offset =
173 utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
174 error::Error::Malformed(format!(
175 "cannot map tls start_address_of_raw_data rva ({:#x}) into offset",
176 rva
177 ))
178 })?;
179 raw_data = Some(&bytes[offset..offset + size as usize]);
180 }
181
182 // Parse the index if any
183 if itd.address_of_index != 0 {
184 if (itd.address_of_index as usize) < image_base {
185 return Err(error::Error::Malformed(format!(
186 "tls address_of_index ({:#x}) is less than image base ({:#x})",
187 itd.address_of_index, image_base
188 )));
189 }
190
191 // VA to RVA
192 let rva = itd.address_of_index as usize - image_base;
193 let offset = utils::find_offset(rva, sections, file_alignment, opts);
194 slot = offset.and_then(|x| bytes.pread_with::<u32>(x, scroll::LE).ok());
195 }
196
197 // Parse the callbacks if any
198 if itd.address_of_callbacks != 0 {
199 if (itd.address_of_callbacks as usize) < image_base {
200 return Err(error::Error::Malformed(format!(
201 "tls address_of_callbacks ({:#x}) is less than image base ({:#x})",
202 itd.address_of_callbacks, image_base
203 )));
204 }
205
206 // VA to RVA
207 let rva = itd.address_of_callbacks as usize - image_base;
208 let offset =
209 utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
210 error::Error::Malformed(format!(
211 "cannot map tls address_of_callbacks rva ({:#x}) into offset",
212 rva
213 ))
214 })?;
215 let mut i = 0;
216 // Read the callbacks until we find a null terminator
217 loop {
218 let callback: u64 = if is_64 {
219 bytes.pread_with::<u64>(offset + i * 8, scroll::LE)?
220 } else {
221 bytes.pread_with::<u32>(offset + i * 4, scroll::LE)? as u64
222 };
223 if callback == 0 {
224 break;
225 }
226 // Each callback is an VA so convert it to RVA
227 let callback_rva = callback as usize - image_base;
228 // Check if the callback is in the image
229 if utils::find_offset(callback_rva, sections, file_alignment, opts).is_none() {
230 return Err(error::Error::Malformed(format!(
231 "cannot map tls callback ({:#x})",
232 callback
233 )));
234 }
235 callbacks.push(callback);
236 i += 1;
237 }
238 }
239
240 Ok(Some(TlsData {
241 image_tls_directory: itd,
242 raw_data,
243 slot,
244 callbacks,
245 }))
246 }
247 }