1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
use alloc::fmt;
use core::convert::TryInto;
use core::fmt::Debug;
use core::marker::PhantomData;
use core::str;
use crate::endian::{BigEndian as BE, U32Bytes};
use crate::pod::Pod;
use crate::read::util::StringTable;
use crate::{bytes_of, xcoff, Object, ObjectSection, SectionKind};
use crate::read::{
self, Bytes, Error, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, Result, SectionIndex,
SymbolFlags, SymbolIndex, SymbolKind, SymbolScope, SymbolSection,
};
use super::{FileHeader, XcoffFile};
/// A table of symbol entries in an XCOFF file.
///
/// Also includes the string table used for the symbol names.
#[derive(Debug)]
pub struct SymbolTable<'data, Xcoff, R = &'data [u8]>
where
Xcoff: FileHeader,
R: ReadRef<'data>,
{
symbols: &'data [xcoff::SymbolBytes],
strings: StringTable<'data, R>,
header: PhantomData<Xcoff>,
}
impl<'data, Xcoff, R> Default for SymbolTable<'data, Xcoff, R>
where
Xcoff: FileHeader,
R: ReadRef<'data>,
{
fn default() -> Self {
Self {
symbols: &[],
strings: StringTable::default(),
header: PhantomData,
}
}
}
impl<'data, Xcoff, R> SymbolTable<'data, Xcoff, R>
where
Xcoff: FileHeader,
R: ReadRef<'data>,
{
/// Parse the symbol table.
pub fn parse(header: Xcoff, data: R) -> Result<Self> {
let mut offset = header.f_symptr().into();
let (symbols, strings) = if offset != 0 {
let symbols = data
.read_slice(&mut offset, header.f_nsyms() as usize)
.read_error("Invalid XCOFF symbol table offset or size")?;
// Parse the string table.
// Note: don't update data when reading length; the length includes itself.
let length = data
.read_at::<U32Bytes<_>>(offset)
.read_error("Missing XCOFF string table")?
.get(BE);
let str_end = offset
.checked_add(length as u64)
.read_error("Invalid XCOFF string table length")?;
let strings = StringTable::new(data, offset, str_end);
(symbols, strings)
} else {
(&[][..], StringTable::default())
};
Ok(SymbolTable {
symbols,
strings,
header: PhantomData,
})
}
/// Return the symbol entry at the given index and offset.
pub fn get<T: Pod>(&self, index: usize, offset: usize) -> Result<&'data T> {
let entry = index
.checked_add(offset)
.and_then(|x| self.symbols.get(x))
.read_error("Invalid XCOFF symbol index")?;
let bytes = bytes_of(entry);
Bytes(bytes).read().read_error("Invalid XCOFF symbol data")
}
/// Return the symbol at the given index.
pub fn symbol(&self, index: usize) -> Result<&'data Xcoff::Symbol> {
self.get::<Xcoff::Symbol>(index, 0)
}
/// Return the file auxiliary symbol.
pub fn aux_file(&self, index: usize) -> Result<&'data Xcoff::FileAux> {
debug_assert!(self.symbol(index)?.has_aux_file());
let aux_file = self.get::<Xcoff::FileAux>(index, 1)?;
if let Some(aux_type) = aux_file.x_auxtype() {
if aux_type != xcoff::AUX_FILE {
return Err(Error("Invalid index for file auxiliary symbol."));
}
}
Ok(aux_file)
}
/// Return the csect auxiliary symbol.
pub fn aux_csect(&self, index: usize, offset: usize) -> Result<&'data Xcoff::CsectAux> {
debug_assert!(self.symbol(index)?.has_aux_csect());
let aux_csect = self.get::<Xcoff::CsectAux>(index, offset)?;
if let Some(aux_type) = aux_csect.x_auxtype() {
if aux_type != xcoff::AUX_CSECT {
return Err(Error("Invalid index/offset for csect auxiliary symbol."));
}
}
Ok(aux_csect)
}
/// Return true if the symbol table is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.symbols.is_empty()
}
/// The number of symbol table entries.
///
/// This includes auxiliary symbol table entries.
#[inline]
pub fn len(&self) -> usize {
self.symbols.len()
}
}
/// A symbol table of an `XcoffFile32`.
pub type XcoffSymbolTable32<'data, 'file, R = &'data [u8]> =
XcoffSymbolTable<'data, 'file, xcoff::FileHeader32, R>;
/// A symbol table of an `XcoffFile64`.
pub type XcoffSymbolTable64<'data, 'file, R = &'data [u8]> =
XcoffSymbolTable<'data, 'file, xcoff::FileHeader64, R>;
/// A symbol table of an `XcoffFile`.
#[derive(Debug, Clone, Copy)]
pub struct XcoffSymbolTable<'data, 'file, Xcoff, R = &'data [u8]>
where
'data: 'file,
Xcoff: FileHeader,
R: ReadRef<'data>,
{
pub(crate) file: &'file XcoffFile<'data, Xcoff, R>,
pub(super) symbols: &'file SymbolTable<'data, Xcoff, R>,
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> read::private::Sealed
for XcoffSymbolTable<'data, 'file, Xcoff, R>
{
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbolTable<'data>
for XcoffSymbolTable<'data, 'file, Xcoff, R>
{
type Symbol = XcoffSymbol<'data, 'file, Xcoff, R>;
type SymbolIterator = XcoffSymbolIterator<'data, 'file, Xcoff, R>;
fn symbols(&self) -> Self::SymbolIterator {
XcoffSymbolIterator {
file: self.file,
symbols: self.symbols,
index: 0,
}
}
fn symbol_by_index(&self, index: SymbolIndex) -> read::Result<Self::Symbol> {
let symbol = self.symbols.symbol(index.0)?;
Ok(XcoffSymbol {
file: self.file,
symbols: self.symbols,
index,
symbol,
})
}
}
/// An iterator over the symbols of an `XcoffFile32`.
pub type XcoffSymbolIterator32<'data, 'file, R = &'data [u8]> =
XcoffSymbolIterator<'data, 'file, xcoff::FileHeader32, R>;
/// An iterator over the symbols of an `XcoffFile64`.
pub type XcoffSymbolIterator64<'data, 'file, R = &'data [u8]> =
XcoffSymbolIterator<'data, 'file, xcoff::FileHeader64, R>;
/// An iterator over the symbols of an `XcoffFile`.
pub struct XcoffSymbolIterator<'data, 'file, Xcoff, R = &'data [u8]>
where
'data: 'file,
Xcoff: FileHeader,
R: ReadRef<'data>,
{
pub(crate) file: &'file XcoffFile<'data, Xcoff, R>,
pub(super) symbols: &'file SymbolTable<'data, Xcoff, R>,
pub(super) index: usize,
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> fmt::Debug
for XcoffSymbolIterator<'data, 'file, Xcoff, R>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("XcoffSymbolIterator").finish()
}
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> Iterator
for XcoffSymbolIterator<'data, 'file, Xcoff, R>
{
type Item = XcoffSymbol<'data, 'file, Xcoff, R>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
let symbol = self.symbols.symbol(index).ok()?;
// TODO: skip over the auxiliary symbols for now.
self.index += 1 + symbol.n_numaux() as usize;
Some(XcoffSymbol {
file: self.file,
symbols: self.symbols,
index: SymbolIndex(index),
symbol,
})
}
}
/// A symbol of an `XcoffFile32`.
pub type XcoffSymbol32<'data, 'file, R = &'data [u8]> =
XcoffSymbol<'data, 'file, xcoff::FileHeader32, R>;
/// A symbol of an `XcoffFile64`.
pub type XcoffSymbol64<'data, 'file, R = &'data [u8]> =
XcoffSymbol<'data, 'file, xcoff::FileHeader64, R>;
/// A symbol of an `XcoffFile`.
#[derive(Debug, Clone, Copy)]
pub struct XcoffSymbol<'data, 'file, Xcoff, R = &'data [u8]>
where
'data: 'file,
Xcoff: FileHeader,
R: ReadRef<'data>,
{
pub(crate) file: &'file XcoffFile<'data, Xcoff, R>,
pub(super) symbols: &'file SymbolTable<'data, Xcoff, R>,
pub(super) index: SymbolIndex,
pub(super) symbol: &'data Xcoff::Symbol,
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> read::private::Sealed
for XcoffSymbol<'data, 'file, Xcoff, R>
{
}
impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data>
for XcoffSymbol<'data, 'file, Xcoff, R>
{
#[inline]
fn index(&self) -> SymbolIndex {
self.index
}
fn name_bytes(&self) -> Result<&'data [u8]> {
self.symbol.name(self.symbols.strings)
}
fn name(&self) -> Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
.read_error("Non UTF-8 XCOFF symbol name")
}
#[inline]
fn address(&self) -> u64 {
match self.symbol.n_sclass() {
// Relocatable address.
xcoff::C_EXT
| xcoff::C_WEAKEXT
| xcoff::C_HIDEXT
| xcoff::C_FCN
| xcoff::C_BLOCK
| xcoff::C_STAT => self.symbol.n_value().into(),
_ => 0,
}
}
#[inline]
fn size(&self) -> u64 {
if self.symbol.has_aux_csect() {
// XCOFF32 must have the csect auxiliary entry as the last auxiliary entry.
// XCOFF64 doesn't require this, but conventionally does.
if let Ok(aux_csect) = self
.file
.symbols
.aux_csect(self.index.0, self.symbol.n_numaux() as usize)
{
let sym_type = aux_csect.sym_type() & 0x07;
if sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_CM {
aux_csect.x_scnlen()
} else {
0
}
} else {
0
}
} else {
0
}
}
fn kind(&self) -> SymbolKind {
match self.symbol.n_sclass() {
xcoff::C_FILE => SymbolKind::File,
xcoff::C_NULL => SymbolKind::Null,
_ => self
.file
.section_by_index(SectionIndex((self.symbol.n_scnum() - 1) as usize))
.map(|section| match section.kind() {
SectionKind::Data | SectionKind::UninitializedData => SymbolKind::Data,
SectionKind::UninitializedTls | SectionKind::Tls => SymbolKind::Tls,
SectionKind::Text => SymbolKind::Text,
_ => SymbolKind::Unknown,
})
.unwrap_or(SymbolKind::Unknown),
}
}
fn section(&self) -> SymbolSection {
match self.symbol.n_scnum() {
xcoff::N_ABS => SymbolSection::Absolute,
xcoff::N_UNDEF => SymbolSection::Undefined,
xcoff::N_DEBUG => SymbolSection::None,
index if index > 0 => SymbolSection::Section(SectionIndex(index as usize)),
_ => SymbolSection::Unknown,
}
}
#[inline]
fn is_undefined(&self) -> bool {
self.symbol.is_undefined()
}
/// Return true if the symbol is a definition of a function or data object.
#[inline]
fn is_definition(&self) -> bool {
if self.symbol.has_aux_csect() {
if let Ok(aux_csect) = self
.symbols
.aux_csect(self.index.0, self.symbol.n_numaux() as usize)
{
let smclas = aux_csect.x_smclas();
self.symbol.n_scnum() != xcoff::N_UNDEF
&& (smclas == xcoff::XMC_PR
|| smclas == xcoff::XMC_RW
|| smclas == xcoff::XMC_RO)
} else {
false
}
} else {
false
}
}
#[inline]
fn is_common(&self) -> bool {
self.symbol.n_sclass() == xcoff::C_EXT && self.symbol.n_scnum() == xcoff::N_UNDEF
}
#[inline]
fn is_weak(&self) -> bool {
self.symbol.n_sclass() == xcoff::C_WEAKEXT
}
fn scope(&self) -> SymbolScope {
if self.symbol.n_scnum() == xcoff::N_UNDEF {
SymbolScope::Unknown
} else {
match self.symbol.n_sclass() {
xcoff::C_EXT | xcoff::C_WEAKEXT | xcoff::C_HIDEXT => {
let visibility = self.symbol.n_type() & xcoff::SYM_V_MASK;
if visibility == xcoff::SYM_V_HIDDEN {
SymbolScope::Linkage
} else {
SymbolScope::Dynamic
}
}
_ => SymbolScope::Compilation,
}
}
}
#[inline]
fn is_global(&self) -> bool {
match self.symbol.n_sclass() {
xcoff::C_EXT | xcoff::C_WEAKEXT => true,
_ => false,
}
}
#[inline]
fn is_local(&self) -> bool {
!self.is_global()
}
#[inline]
fn flags(&self) -> SymbolFlags<SectionIndex> {
SymbolFlags::None
}
}
/// A trait for generic access to `Symbol32` and `Symbol64`.
#[allow(missing_docs)]
pub trait Symbol: Debug + Pod {
type Word: Into<u64>;
fn n_value(&self) -> Self::Word;
fn n_scnum(&self) -> i16;
fn n_type(&self) -> u16;
fn n_sclass(&self) -> u8;
fn n_numaux(&self) -> u8;
fn name<'data, R: ReadRef<'data>>(
&'data self,
strings: StringTable<'data, R>,
) -> Result<&'data [u8]>;
/// Return true if the symbol is undefined.
#[inline]
fn is_undefined(&self) -> bool {
let n_sclass = self.n_sclass();
(n_sclass == xcoff::C_EXT || n_sclass == xcoff::C_WEAKEXT)
&& self.n_scnum() == xcoff::N_UNDEF
}
/// Return true if the symbol has file auxiliary entry.
fn has_aux_file(&self) -> bool {
self.n_numaux() > 0 && self.n_sclass() == xcoff::C_FILE
}
/// Return true if the symbol has csect auxiliary entry.
///
/// A csect auxiliary entry is required for each symbol table entry that has
/// a storage class value of C_EXT, C_WEAKEXT, or C_HIDEXT.
fn has_aux_csect(&self) -> bool {
let sclass = self.n_sclass();
self.n_numaux() > 0
&& (sclass == xcoff::C_EXT || sclass == xcoff::C_WEAKEXT || sclass == xcoff::C_HIDEXT)
}
}
impl Symbol for xcoff::Symbol64 {
type Word = u64;
fn n_value(&self) -> Self::Word {
self.n_value.get(BE)
}
fn n_scnum(&self) -> i16 {
self.n_scnum.get(BE)
}
fn n_type(&self) -> u16 {
self.n_type.get(BE)
}
fn n_sclass(&self) -> u8 {
self.n_sclass
}
fn n_numaux(&self) -> u8 {
self.n_numaux
}
/// Parse the symbol name for XCOFF64.
fn name<'data, R: ReadRef<'data>>(
&'data self,
strings: StringTable<'data, R>,
) -> Result<&'data [u8]> {
strings
.get(self.n_offset.get(BE))
.read_error("Invalid XCOFF symbol name offset")
}
}
impl Symbol for xcoff::Symbol32 {
type Word = u32;
fn n_value(&self) -> Self::Word {
self.n_value.get(BE)
}
fn n_scnum(&self) -> i16 {
self.n_scnum.get(BE)
}
fn n_type(&self) -> u16 {
self.n_type.get(BE)
}
fn n_sclass(&self) -> u8 {
self.n_sclass
}
fn n_numaux(&self) -> u8 {
self.n_numaux
}
/// Parse the symbol name for XCOFF32.
fn name<'data, R: ReadRef<'data>>(
&'data self,
strings: StringTable<'data, R>,
) -> Result<&'data [u8]> {
if self.n_name[0] == 0 {
// If the name starts with 0 then the last 4 bytes are a string table offset.
let offset = u32::from_be_bytes(self.n_name[4..8].try_into().unwrap());
strings
.get(offset)
.read_error("Invalid XCOFF symbol name offset")
} else {
// The name is inline and padded with nulls.
Ok(match memchr::memchr(b'\0', &self.n_name) {
Some(end) => &self.n_name[..end],
None => &self.n_name,
})
}
}
}
/// A trait for generic access to `FileAux32` and `FileAux64`.
#[allow(missing_docs)]
pub trait FileAux: Debug + Pod {
fn x_fname(&self) -> &[u8; 8];
fn x_ftype(&self) -> u8;
fn x_auxtype(&self) -> Option<u8>;
}
impl FileAux for xcoff::FileAux64 {
fn x_fname(&self) -> &[u8; 8] {
&self.x_fname
}
fn x_ftype(&self) -> u8 {
self.x_ftype
}
fn x_auxtype(&self) -> Option<u8> {
Some(self.x_auxtype)
}
}
impl FileAux for xcoff::FileAux32 {
fn x_fname(&self) -> &[u8; 8] {
&self.x_fname
}
fn x_ftype(&self) -> u8 {
self.x_ftype
}
fn x_auxtype(&self) -> Option<u8> {
None
}
}
/// A trait for generic access to `CsectAux32` and `CsectAux64`.
#[allow(missing_docs)]
pub trait CsectAux: Debug + Pod {
fn x_scnlen(&self) -> u64;
fn x_parmhash(&self) -> u32;
fn x_snhash(&self) -> u16;
fn x_smtyp(&self) -> u8;
fn x_smclas(&self) -> u8;
fn x_auxtype(&self) -> Option<u8>;
fn sym_type(&self) -> u8 {
self.x_smtyp() & 0x07
}
}
impl CsectAux for xcoff::CsectAux64 {
fn x_scnlen(&self) -> u64 {
self.x_scnlen_lo.get(BE) as u64 | ((self.x_scnlen_hi.get(BE) as u64) << 32)
}
fn x_parmhash(&self) -> u32 {
self.x_parmhash.get(BE)
}
fn x_snhash(&self) -> u16 {
self.x_snhash.get(BE)
}
fn x_smtyp(&self) -> u8 {
self.x_smtyp
}
fn x_smclas(&self) -> u8 {
self.x_smclas
}
fn x_auxtype(&self) -> Option<u8> {
Some(self.x_auxtype)
}
}
impl CsectAux for xcoff::CsectAux32 {
fn x_scnlen(&self) -> u64 {
self.x_scnlen.get(BE) as u64
}
fn x_parmhash(&self) -> u32 {
self.x_parmhash.get(BE)
}
fn x_snhash(&self) -> u16 {
self.x_snhash.get(BE)
}
fn x_smtyp(&self) -> u8 {
self.x_smtyp
}
fn x_smclas(&self) -> u8 {
self.x_smclas
}
fn x_auxtype(&self) -> Option<u8> {
None
}
}
|