]> git.proxmox.com Git - rustc.git/blob - vendor/object-0.22.0/src/read/coff/symbol.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / vendor / object-0.22.0 / src / read / coff / symbol.rs
1 use alloc::fmt;
2 use alloc::vec::Vec;
3 use core::convert::TryInto;
4 use core::str;
5
6 use super::{CoffCommon, SectionTable};
7 use crate::endian::{LittleEndian as LE, U32Bytes};
8 use crate::pe;
9 use crate::pod::{Bytes, Pod};
10 use crate::read::util::StringTable;
11 use crate::read::{
12 self, ObjectSymbol, ObjectSymbolTable, ReadError, Result, SectionIndex, SymbolFlags,
13 SymbolIndex, SymbolKind, SymbolMap, SymbolMapEntry, SymbolScope, SymbolSection,
14 };
15
16 /// A table of symbol entries in a COFF or PE file.
17 ///
18 /// Also includes the string table used for the symbol names.
19 #[derive(Debug)]
20 pub struct SymbolTable<'data> {
21 symbols: &'data [pe::ImageSymbolBytes],
22 strings: StringTable<'data>,
23 }
24
25 impl<'data> SymbolTable<'data> {
26 /// Read the symbol table.
27 pub fn parse(header: &pe::ImageFileHeader, mut data: Bytes<'data>) -> Result<Self> {
28 // The symbol table may not be present.
29 let symbol_offset = header.pointer_to_symbol_table.get(LE) as usize;
30 let (symbols, strings) = if symbol_offset != 0 {
31 data.skip(symbol_offset)
32 .read_error("Invalid COFF symbol table offset")?;
33 let symbols = data
34 .read_slice(header.number_of_symbols.get(LE) as usize)
35 .read_error("Invalid COFF symbol table size")?;
36
37 // Note: don't update data when reading length; the length includes itself.
38 let length = data
39 .read_at::<U32Bytes<_>>(0)
40 .read_error("Missing COFF string table")?
41 .get(LE);
42 let strings = data
43 .read_bytes(length as usize)
44 .read_error("Invalid COFF string table length")?;
45
46 (symbols, strings)
47 } else {
48 (&[][..], Bytes(&[]))
49 };
50
51 Ok(SymbolTable {
52 symbols,
53 strings: StringTable::new(strings),
54 })
55 }
56
57 /// Return the string table used for the symbol names.
58 #[inline]
59 pub fn strings(&self) -> StringTable<'data> {
60 self.strings
61 }
62
63 /// Return true if the symbol table is empty.
64 #[inline]
65 pub fn is_empty(&self) -> bool {
66 self.symbols.is_empty()
67 }
68
69 /// The number of symbols.
70 #[inline]
71 pub fn len(&self) -> usize {
72 self.symbols.len()
73 }
74
75 /// Return the symbol table entry at the given index.
76 #[inline]
77 pub fn symbol(&self, index: usize) -> Result<&'data pe::ImageSymbol> {
78 self.get::<pe::ImageSymbol>(index)
79 }
80
81 /// Return the symbol table entry or auxiliary record at the given index.
82 pub fn get<T: Pod>(&self, index: usize) -> Result<&'data T> {
83 let bytes = self
84 .symbols
85 .get(index)
86 .read_error("Invalid COFF symbol index")?;
87 Bytes(&bytes.0[..])
88 .read()
89 .read_error("Invalid COFF symbol data")
90 }
91
92 /// Construct a map from addresses to a user-defined map entry.
93 pub fn map<Entry: SymbolMapEntry, F: Fn(&'data pe::ImageSymbol) -> Option<Entry>>(
94 &self,
95 f: F,
96 ) -> SymbolMap<Entry> {
97 let mut symbols = Vec::with_capacity(self.symbols.len());
98 let mut i = 0;
99 while let Ok(symbol) = self.symbol(i) {
100 i += 1 + symbol.number_of_aux_symbols as usize;
101 if !symbol.is_definition() {
102 continue;
103 }
104 if let Some(entry) = f(symbol) {
105 symbols.push(entry);
106 }
107 }
108 SymbolMap::new(symbols)
109 }
110 }
111
112 impl pe::ImageSymbol {
113 /// Parse a COFF symbol name.
114 ///
115 /// `strings` must be the string table used for symbol names.
116 pub fn name<'data>(&'data self, strings: StringTable<'data>) -> Result<&'data [u8]> {
117 if self.name[0] == 0 {
118 // If the name starts with 0 then the last 4 bytes are a string table offset.
119 let offset = u32::from_le_bytes(self.name[4..8].try_into().unwrap());
120 strings
121 .get(offset)
122 .read_error("Invalid COFF symbol name offset")
123 } else {
124 // The name is inline and padded with nulls.
125 Ok(match self.name.iter().position(|&x| x == 0) {
126 Some(end) => &self.name[..end],
127 None => &self.name[..],
128 })
129 }
130 }
131
132 /// Return the symbol address.
133 ///
134 /// This takes into account the image base and the section address.
135 pub fn address(&self, image_base: u64, sections: &SectionTable) -> Result<u64> {
136 let section_number = self.section_number.get(LE) as usize;
137 let section = sections.section(section_number)?;
138 let virtual_address = u64::from(section.virtual_address.get(LE));
139 let value = u64::from(self.value.get(LE));
140 Ok(image_base + virtual_address + value)
141 }
142
143 /// Return true if the symbol is a definition of a function or data object.
144 pub fn is_definition(&self) -> bool {
145 let section_number = self.section_number.get(LE);
146 if section_number == pe::IMAGE_SYM_UNDEFINED {
147 return false;
148 }
149 match self.storage_class {
150 pe::IMAGE_SYM_CLASS_STATIC => {
151 if self.value.get(LE) == 0 && self.number_of_aux_symbols > 0 {
152 // This is a section symbol.
153 false
154 } else {
155 true
156 }
157 }
158 pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => true,
159 _ => false,
160 }
161 }
162 }
163
164 /// A symbol table of a `CoffFile`.
165 #[derive(Debug, Clone, Copy)]
166 pub struct CoffSymbolTable<'data, 'file>
167 where
168 'data: 'file,
169 {
170 pub(crate) file: &'file CoffCommon<'data>,
171 }
172
173 impl<'data, 'file> read::private::Sealed for CoffSymbolTable<'data, 'file> {}
174
175 impl<'data, 'file> ObjectSymbolTable<'data> for CoffSymbolTable<'data, 'file> {
176 type Symbol = CoffSymbol<'data, 'file>;
177 type SymbolIterator = CoffSymbolIterator<'data, 'file>;
178
179 fn symbols(&self) -> Self::SymbolIterator {
180 CoffSymbolIterator {
181 file: self.file,
182 index: 0,
183 }
184 }
185
186 fn symbol_by_index(&self, index: SymbolIndex) -> Result<Self::Symbol> {
187 let symbol = self.file.symbols.symbol(index.0)?;
188 Ok(CoffSymbol {
189 file: self.file,
190 index,
191 symbol,
192 })
193 }
194 }
195
196 /// An iterator over the symbols of a `CoffFile`.
197 pub struct CoffSymbolIterator<'data, 'file>
198 where
199 'data: 'file,
200 {
201 pub(crate) file: &'file CoffCommon<'data>,
202 pub(crate) index: usize,
203 }
204
205 impl<'data, 'file> fmt::Debug for CoffSymbolIterator<'data, 'file> {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207 f.debug_struct("CoffSymbolIterator").finish()
208 }
209 }
210
211 impl<'data, 'file> Iterator for CoffSymbolIterator<'data, 'file> {
212 type Item = CoffSymbol<'data, 'file>;
213
214 fn next(&mut self) -> Option<Self::Item> {
215 let index = self.index;
216 let symbol = self.file.symbols.symbol(index).ok()?;
217 self.index += 1 + symbol.number_of_aux_symbols as usize;
218 Some(CoffSymbol {
219 file: self.file,
220 index: SymbolIndex(index),
221 symbol,
222 })
223 }
224 }
225
226 /// A symbol of a `CoffFile`.
227 #[derive(Debug, Clone, Copy)]
228 pub struct CoffSymbol<'data, 'file>
229 where
230 'data: 'file,
231 {
232 pub(crate) file: &'file CoffCommon<'data>,
233 pub(crate) index: SymbolIndex,
234 pub(crate) symbol: &'data pe::ImageSymbol,
235 }
236
237 impl<'data, 'file> read::private::Sealed for CoffSymbol<'data, 'file> {}
238
239 impl<'data, 'file> ObjectSymbol<'data> for CoffSymbol<'data, 'file> {
240 #[inline]
241 fn index(&self) -> SymbolIndex {
242 self.index
243 }
244
245 fn name(&self) -> read::Result<&'data str> {
246 let name = if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_FILE {
247 // The file name is in the following auxiliary symbol.
248 if self.symbol.number_of_aux_symbols > 0 {
249 let s = self
250 .file
251 .symbols
252 .get::<pe::ImageSymbolBytes>(self.index.0 + 1)?;
253 // The name is padded with nulls.
254 match s.0.iter().position(|&x| x == 0) {
255 Some(end) => &s.0[..end],
256 None => &s.0[..],
257 }
258 } else {
259 &[][..]
260 }
261 } else {
262 self.symbol.name(self.file.symbols.strings())?
263 };
264 str::from_utf8(name)
265 .ok()
266 .read_error("Non UTF-8 COFF symbol name")
267 }
268
269 fn address(&self) -> u64 {
270 // Only return an address for storage classes that we know use an address.
271 match self.symbol.storage_class {
272 pe::IMAGE_SYM_CLASS_STATIC
273 | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL
274 | pe::IMAGE_SYM_CLASS_LABEL => {}
275 pe::IMAGE_SYM_CLASS_EXTERNAL => {
276 if self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED {
277 // Undefined or common data, neither of which have an address.
278 return 0;
279 }
280 }
281 _ => return 0,
282 }
283 self.symbol
284 .address(self.file.image_base, &self.file.sections)
285 .unwrap_or(0)
286 }
287
288 fn size(&self) -> u64 {
289 match self.symbol.storage_class {
290 pe::IMAGE_SYM_CLASS_STATIC => {
291 // Section symbols may duplicate the size from the section table.
292 if self.symbol.value.get(LE) == 0 && self.symbol.number_of_aux_symbols > 0 {
293 if let Ok(aux) = self
294 .file
295 .symbols
296 .get::<pe::ImageAuxSymbolSection>(self.index.0 + 1)
297 {
298 u64::from(aux.length.get(LE))
299 } else {
300 0
301 }
302 } else {
303 0
304 }
305 }
306 pe::IMAGE_SYM_CLASS_EXTERNAL => {
307 if self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED {
308 // For undefined symbols, symbol.value is 0 and the size is 0.
309 // For common data, symbol.value is the size.
310 u64::from(self.symbol.value.get(LE))
311 } else if self.symbol.derived_type() == pe::IMAGE_SYM_DTYPE_FUNCTION
312 && self.symbol.number_of_aux_symbols > 0
313 {
314 // Function symbols may have a size.
315 if let Ok(aux) = self
316 .file
317 .symbols
318 .get::<pe::ImageAuxSymbolFunction>(self.index.0 + 1)
319 {
320 u64::from(aux.total_size.get(LE))
321 } else {
322 0
323 }
324 } else {
325 0
326 }
327 }
328 // Most symbols don't have sizes.
329 _ => 0,
330 }
331 }
332
333 fn kind(&self) -> SymbolKind {
334 let derived_kind = if self.symbol.derived_type() == pe::IMAGE_SYM_DTYPE_FUNCTION {
335 SymbolKind::Text
336 } else {
337 SymbolKind::Data
338 };
339 match self.symbol.storage_class {
340 pe::IMAGE_SYM_CLASS_STATIC => {
341 if self.symbol.value.get(LE) == 0 && self.symbol.number_of_aux_symbols > 0 {
342 SymbolKind::Section
343 } else {
344 derived_kind
345 }
346 }
347 pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => derived_kind,
348 pe::IMAGE_SYM_CLASS_SECTION => SymbolKind::Section,
349 pe::IMAGE_SYM_CLASS_FILE => SymbolKind::File,
350 pe::IMAGE_SYM_CLASS_LABEL => SymbolKind::Label,
351 _ => SymbolKind::Unknown,
352 }
353 }
354
355 fn section(&self) -> SymbolSection {
356 match self.symbol.section_number.get(LE) {
357 pe::IMAGE_SYM_UNDEFINED => {
358 if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL
359 && self.symbol.value.get(LE) == 0
360 {
361 SymbolSection::Undefined
362 } else {
363 SymbolSection::Common
364 }
365 }
366 pe::IMAGE_SYM_ABSOLUTE => SymbolSection::Absolute,
367 pe::IMAGE_SYM_DEBUG => {
368 if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_FILE {
369 SymbolSection::None
370 } else {
371 SymbolSection::Unknown
372 }
373 }
374 index if index > 0 => SymbolSection::Section(SectionIndex(index as usize)),
375 _ => SymbolSection::Unknown,
376 }
377 }
378
379 #[inline]
380 fn is_undefined(&self) -> bool {
381 self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL
382 && self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED
383 && self.symbol.value.get(LE) == 0
384 }
385
386 #[inline]
387 fn is_definition(&self) -> bool {
388 self.symbol.is_definition()
389 }
390
391 #[inline]
392 fn is_common(&self) -> bool {
393 self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL
394 && self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED
395 && self.symbol.value.get(LE) != 0
396 }
397
398 #[inline]
399 fn is_weak(&self) -> bool {
400 self.symbol.storage_class == pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL
401 }
402
403 #[inline]
404 fn scope(&self) -> SymbolScope {
405 match self.symbol.storage_class {
406 pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => {
407 // TODO: determine if symbol is exported
408 SymbolScope::Linkage
409 }
410 _ => SymbolScope::Compilation,
411 }
412 }
413
414 #[inline]
415 fn is_global(&self) -> bool {
416 match self.symbol.storage_class {
417 pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => true,
418 _ => false,
419 }
420 }
421
422 #[inline]
423 fn is_local(&self) -> bool {
424 !self.is_global()
425 }
426
427 fn flags(&self) -> SymbolFlags<SectionIndex> {
428 if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_STATIC
429 && self.symbol.value.get(LE) == 0
430 && self.symbol.number_of_aux_symbols > 0
431 {
432 if let Ok(aux) = self
433 .file
434 .symbols
435 .get::<pe::ImageAuxSymbolSection>(self.index.0 + 1)
436 {
437 // TODO: use high_number for bigobj
438 let number = aux.number.get(LE) as usize;
439 return SymbolFlags::CoffSection {
440 selection: aux.selection,
441 associative_section: if number == 0 {
442 None
443 } else {
444 Some(SectionIndex(number))
445 },
446 };
447 }
448 }
449 SymbolFlags::None
450 }
451 }