pelite\pe64/
tls.rs

1/*!
2TLS Directory.
3
4# Examples
5
6```
7# #![allow(unused_variables)]
8use pelite::pe64::{Pe, PeFile};
9
10# #[allow(dead_code)]
11fn example(file: PeFile<'_>) -> pelite::Result<()> {
12	// Access the TLS directory
13	let tls = file.tls()?;
14
15	// Access the initialized thread local data
16	let raw_data = tls.raw_data()?;
17
18	// Access the TLS slot
19	let slot = tls.slot()?;
20
21	// Access the TLS callbacks
22	let callbacks = tls.callbacks()?;
23
24	Ok(())
25}
26```
27*/
28
29use std::fmt;
30
31use crate::{Error, Result};
32
33use super::image::*;
34use super::Pe;
35
36//----------------------------------------------------------------
37
38/// TLS Directory.
39///
40/// For more information see the [module-level documentation](index.html).
41#[derive(Copy, Clone)]
42pub struct Tls<'a, P> {
43	pe: P,
44	image: &'a IMAGE_TLS_DIRECTORY,
45}
46impl<'a, P: Pe<'a>> Tls<'a, P> {
47	pub(crate) fn try_from(pe: P) -> Result<Tls<'a, P>> {
48		let datadir = pe.data_directory().get(IMAGE_DIRECTORY_ENTRY_TLS).ok_or(Error::Bounds)?;
49		let image = pe.derva(datadir.VirtualAddress)?;
50		Ok(Tls { pe, image })
51	}
52	/// Gets the PE instance.
53	pub fn pe(&self) -> P {
54		self.pe
55	}
56	/// Returns the underlying TLS directory image.
57	pub fn image(&self) -> &'a IMAGE_TLS_DIRECTORY {
58		self.image
59	}
60	/// Gets the raw TLS initialization data.
61	pub fn raw_data(&self) -> Result<&'a [u8]> {
62		if self.image.StartAddressOfRawData > self.image.EndAddressOfRawData {
63			return Err(Error::Invalid);
64		}
65		// FIXME! truncation warning on 32bit...
66		let len = (self.image.EndAddressOfRawData - self.image.StartAddressOfRawData) as usize;
67		self.pe.deref_slice(self.image.StartAddressOfRawData.into(), len)
68	}
69	/// Gets the TLS slot location.
70	pub fn slot(&self) -> Result<&'a u32> {
71		self.pe.deref(self.image.AddressOfIndex.into())
72	}
73	/// Gets the TLS initialization callbacks.
74	pub fn callbacks(&self) -> Result<&'a [Va]> {
75		self.pe.deref_slice_s(self.image.AddressOfCallBacks.into(), 0)
76	}
77}
78impl<'a, P: Pe<'a>> fmt::Debug for Tls<'a, P> {
79	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80		f.debug_struct("Tls")
81			.field("raw_data.len", &format_args!("{:?}", self.raw_data().map(|raw_data| raw_data.len())))
82			.field("callbacks.len", &format_args!("{:?}", &self.callbacks().map(|cbs| cbs.len())))
83			.finish()
84	}
85}
86
87//----------------------------------------------------------------
88
89#[cfg(feature = "serde")]
90mod serde {
91	use crate::util::serde_helper::*;
92	use super::{Pe, Tls};
93
94	impl<'a, P: Pe<'a>> Serialize for Tls<'a, P> {
95		fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
96			let is_human_readable = serializer.is_human_readable();
97			let mut state = serializer.serialize_struct("Tls", 2)?;
98			if cfg!(feature = "data-encoding") && is_human_readable {
99				#[cfg(feature = "data-encoding")]
100				state.serialize_field("raw_data",
101					&self.raw_data().ok().map(|data| data_encoding::BASE64.encode(data)))?;
102			}
103			else {
104				state.serialize_field("raw_data", &self.raw_data().ok())?;
105			}
106			state.serialize_field("callbacks", &self.callbacks().ok())?;
107			state.end()
108		}
109	}
110}
111
112//----------------------------------------------------------------
113
114#[cfg(test)]
115pub(crate) fn test<'a, P: Pe<'a>>(pe: P) -> Result<()> {
116	let tls = pe.tls()?;
117	let _ = format!("{:?}", tls);
118	let _raw_data = tls.raw_data();
119	let _slot = tls.slot();
120	let _callbacks = tls.callbacks();
121	Ok(())
122}