1use alloc::vec::Vec;
2use core::fmt::Debug;
34use crate::endian::{LittleEndian as LE, U16Bytes, U32Bytes};
5use crate::pe;
6use crate::read::{ByteString, Bytes, Error, ReadError, ReadRef, Result};
78/// Where an export is pointing to.
9#[derive(Clone, Copy)]
10pub enum ExportTarget<'data> {
11/// The address of the export, relative to the image base.
12Address(u32),
13/// Forwarded to an export ordinal in another DLL.
14 ///
15 /// This gives the name of the DLL, and the ordinal.
16ForwardByOrdinal(&'data [u8], u32),
17/// Forwarded to an export name in another DLL.
18 ///
19 /// This gives the name of the DLL, and the export name.
20ForwardByName(&'data [u8], &'data [u8]),
21}
2223impl<'data> ExportTarget<'data> {
24/// Returns true if the target is an address.
25pub fn is_address(&self) -> bool {
26match self {
27 ExportTarget::Address(_) => true,
28_ => false,
29 }
30 }
3132/// Returns true if the export is forwarded to another DLL.
33pub fn is_forward(&self) -> bool {
34 !self.is_address()
35 }
36}
3738/// An export from a PE file.
39///
40/// There are multiple kinds of PE exports (with or without a name, and local or forwarded).
41#[derive(Clone, Copy)]
42pub struct Export<'data> {
43/// The ordinal of the export.
44 ///
45 /// These are sequential, starting at a base specified in the DLL.
46pub ordinal: u32,
47/// The name of the export, if known.
48pub name: Option<&'data [u8]>,
49/// The target of this export.
50pub target: ExportTarget<'data>,
51}
5253impl<'a> Debug for Export<'a> {
54fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
55 f.debug_struct("Export")
56 .field("ordinal", &self.ordinal)
57 .field("name", &self.name.map(ByteString))
58 .field("target", &self.target)
59 .finish()
60 }
61}
6263impl<'a> Debug for ExportTarget<'a> {
64fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
65match self {
66 ExportTarget::Address(address) => write!(f, "Address({:#x})", address),
67 ExportTarget::ForwardByOrdinal(library, ordinal) => write!(
68 f,
69"ForwardByOrdinal({:?}.#{})",
70 ByteString(library),
71 ordinal
72 ),
73 ExportTarget::ForwardByName(library, name) => write!(
74 f,
75"ForwardByName({:?}.{:?})",
76 ByteString(library),
77 ByteString(name)
78 ),
79 }
80 }
81}
8283/// A partially parsed PE export table.
84///
85/// Returned by [`DataDirectories::export_table`](super::DataDirectories::export_table).
86#[derive(Debug, Clone)]
87pub struct ExportTable<'data> {
88 data: Bytes<'data>,
89 virtual_address: u32,
90 directory: &'data pe::ImageExportDirectory,
91 addresses: &'data [U32Bytes<LE>],
92 names: &'data [U32Bytes<LE>],
93 name_ordinals: &'data [U16Bytes<LE>],
94}
9596impl<'data> ExportTable<'data> {
97/// Parse the export table given its section data and address.
98pub fn parse(data: &'data [u8], virtual_address: u32) -> Result<Self> {
99let directory = Self::parse_directory(data)?;
100let data = Bytes(data);
101102let mut addresses = &[][..];
103let address_of_functions = directory.address_of_functions.get(LE);
104if address_of_functions != 0 {
105 addresses = data
106 .read_slice_at::<U32Bytes<_>>(
107 address_of_functions.wrapping_sub(virtual_address) as usize,
108 directory.number_of_functions.get(LE) as usize,
109 )
110 .read_error("Invalid PE export address table")?;
111 }
112113let mut names = &[][..];
114let mut name_ordinals = &[][..];
115let address_of_names = directory.address_of_names.get(LE);
116let address_of_name_ordinals = directory.address_of_name_ordinals.get(LE);
117if address_of_names != 0 {
118if address_of_name_ordinals == 0 {
119return Err(Error("Missing PE export ordinal table"));
120 }
121122let number = directory.number_of_names.get(LE) as usize;
123 names = data
124 .read_slice_at::<U32Bytes<_>>(
125 address_of_names.wrapping_sub(virtual_address) as usize,
126 number,
127 )
128 .read_error("Invalid PE export name pointer table")?;
129 name_ordinals = data
130 .read_slice_at::<U16Bytes<_>>(
131 address_of_name_ordinals.wrapping_sub(virtual_address) as usize,
132 number,
133 )
134 .read_error("Invalid PE export ordinal table")?;
135 }
136137Ok(ExportTable {
138 data,
139 virtual_address,
140 directory,
141 addresses,
142 names,
143 name_ordinals,
144 })
145 }
146147/// Parse the export directory given its section data.
148pub fn parse_directory(data: &'data [u8]) -> Result<&'data pe::ImageExportDirectory> {
149 data.read_at::<pe::ImageExportDirectory>(0)
150 .read_error("Invalid PE export dir size")
151 }
152153/// Returns the header of the export table.
154pub fn directory(&self) -> &'data pe::ImageExportDirectory {
155self.directory
156 }
157158/// Returns the base value of ordinals.
159 ///
160 /// Adding this to an address index will give an ordinal.
161pub fn ordinal_base(&self) -> u32 {
162self.directory.base.get(LE)
163 }
164165/// Returns the unparsed address table.
166 ///
167 /// An address table entry may be a local address, or the address of a forwarded export entry.
168 /// See [`Self::is_forward`] and [`Self::target_from_address`].
169pub fn addresses(&self) -> &'data [U32Bytes<LE>] {
170self.addresses
171 }
172173/// Returns the unparsed name pointer table.
174 ///
175 /// A name pointer table entry can be used with [`Self::name_from_pointer`].
176pub fn name_pointers(&self) -> &'data [U32Bytes<LE>] {
177self.names
178 }
179180/// Returns the unparsed ordinal table.
181 ///
182 /// An ordinal table entry is a 0-based index into the address table.
183 /// See [`Self::address_by_index`] and [`Self::target_by_index`].
184pub fn name_ordinals(&self) -> &'data [U16Bytes<LE>] {
185self.name_ordinals
186 }
187188/// Returns an iterator for the entries in the name pointer table and ordinal table.
189 ///
190 /// A name pointer table entry can be used with [`Self::name_from_pointer`].
191 ///
192 /// An ordinal table entry is a 0-based index into the address table.
193 /// See [`Self::address_by_index`] and [`Self::target_by_index`].
194pub fn name_iter(&self) -> impl Iterator<Item = (u32, u16)> + 'data {
195self.names
196 .iter()
197 .map(|x| x.get(LE))
198 .zip(self.name_ordinals.iter().map(|x| x.get(LE)))
199 }
200201/// Returns the export address table entry at the given address index.
202 ///
203 /// This may be a local address, or the address of a forwarded export entry.
204 /// See [`Self::is_forward`] and [`Self::target_from_address`].
205 ///
206 /// `index` is a 0-based index into the export address table.
207pub fn address_by_index(&self, index: u32) -> Result<u32> {
208Ok(self
209.addresses
210 .get(index as usize)
211 .read_error("Invalid PE export address index")?
212.get(LE))
213 }
214215/// Returns the export address table entry at the given ordinal.
216 ///
217 /// This may be a local address, or the address of a forwarded export entry.
218 /// See [`Self::is_forward`] and [`Self::target_from_address`].
219pub fn address_by_ordinal(&self, ordinal: u32) -> Result<u32> {
220self.address_by_index(ordinal.wrapping_sub(self.ordinal_base()))
221 }
222223/// Returns the target of the export at the given address index.
224 ///
225 /// `index` is a 0-based index into the export address table.
226pub fn target_by_index(&self, index: u32) -> Result<ExportTarget<'data>> {
227self.target_from_address(self.address_by_index(index)?)
228 }
229230/// Returns the target of the export at the given ordinal.
231pub fn target_by_ordinal(&self, ordinal: u32) -> Result<ExportTarget<'data>> {
232self.target_from_address(self.address_by_ordinal(ordinal)?)
233 }
234235/// Convert an export address table entry into a target.
236pub fn target_from_address(&self, address: u32) -> Result<ExportTarget<'data>> {
237Ok(if let Some(forward) = self.forward_string(address)? {
238let i = forward
239 .iter()
240 .position(|x| *x == b'.')
241 .read_error("Missing PE forwarded export separator")?;
242let library = &forward[..i];
243match &forward[i + 1..] {
244 [b'#', digits @ ..] => {
245let ordinal =
246 parse_ordinal(digits).read_error("Invalid PE forwarded export ordinal")?;
247 ExportTarget::ForwardByOrdinal(library, ordinal)
248 }
249 [] => {
250return Err(Error("Missing PE forwarded export name"));
251 }
252 name => ExportTarget::ForwardByName(library, name),
253 }
254 } else {
255 ExportTarget::Address(address)
256 })
257 }
258259fn forward_offset(&self, address: u32) -> Option<usize> {
260let offset = address.wrapping_sub(self.virtual_address) as usize;
261if offset < self.data.len() {
262Some(offset)
263 } else {
264None
265}
266 }
267268/// Return true if the export address table entry is a forward.
269pub fn is_forward(&self, address: u32) -> bool {
270self.forward_offset(address).is_some()
271 }
272273/// Return the forward string if the export address table entry is a forward.
274pub fn forward_string(&self, address: u32) -> Result<Option<&'data [u8]>> {
275if let Some(offset) = self.forward_offset(address) {
276self.data
277 .read_string_at(offset)
278 .read_error("Invalid PE forwarded export address")
279 .map(Some)
280 } else {
281Ok(None)
282 }
283 }
284285/// Convert an export name pointer table entry into a name.
286pub fn name_from_pointer(&self, name_pointer: u32) -> Result<&'data [u8]> {
287let offset = name_pointer.wrapping_sub(self.virtual_address);
288self.data
289 .read_string_at(offset as usize)
290 .read_error("Invalid PE export name pointer")
291 }
292293/// Returns the parsed exports in this table.
294pub fn exports(&self) -> Result<Vec<Export<'data>>> {
295// First, let's list all exports.
296let mut exports = Vec::new();
297let ordinal_base = self.ordinal_base();
298for (i, address) in self.addresses.iter().enumerate() {
299// Convert from an array index to an ordinal.
300let ordinal = ordinal_base.wrapping_add(i as u32);
301let target = self.target_from_address(address.get(LE))?;
302 exports.push(Export {
303 ordinal,
304 target,
305// Might be populated later.
306name: None,
307 });
308 }
309310// Now, check whether some (or all) of them have an associated name.
311 // `ordinal_index` is a 0-based index into `addresses`.
312for (name_pointer, ordinal_index) in self.name_iter() {
313let name = self.name_from_pointer(name_pointer)?;
314 exports
315 .get_mut(ordinal_index as usize)
316 .read_error("Invalid PE export ordinal")?
317.name = Some(name);
318 }
319320Ok(exports)
321 }
322}
323324fn parse_ordinal(digits: &[u8]) -> Option<u32> {
325if digits.is_empty() {
326return None;
327 }
328let mut result: u32 = 0;
329for &c in digits {
330let x = (c as char).to_digit(10)?;
331 result = result.checked_mul(10)?.checked_add(x)?;
332 }
333Some(result)
334}