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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
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::{bytes_of, Pod};
use crate::read::util::StringTable;
use crate::xcoff;

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.
///
/// Returned by [`FileHeader::symbols`].
#[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 string table used for the symbol names.
    #[inline]
    pub fn strings(&self) -> StringTable<'data, R> {
        self.strings
    }

    /// Iterate over the symbols.
    #[inline]
    pub fn iter<'table>(&'table self) -> SymbolIterator<'data, 'table, Xcoff, R> {
        SymbolIterator {
            symbols: self,
            index: 0,
        }
    }

    /// Empty symbol iterator.
    #[inline]
    pub(super) fn iter_none<'table>(&'table self) -> SymbolIterator<'data, 'table, Xcoff, R> {
        SymbolIterator {
            symbols: self,
            index: self.symbols.len(),
        }
    }

    /// 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 a file auxiliary symbol.
    pub fn aux_file(&self, index: usize, offset: usize) -> Result<&'data Xcoff::FileAux> {
        debug_assert!(self.symbol(index)?.has_aux_file());
        let aux_file = self.get::<Xcoff::FileAux>(index, offset)?;
        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()
    }
}

/// An iterator for symbol entries in an XCOFF file.
///
/// Yields the index and symbol structure for each symbol.
#[derive(Debug)]
pub struct SymbolIterator<'data, 'table, Xcoff, R = &'data [u8]>
where
    Xcoff: FileHeader,
    R: ReadRef<'data>,
{
    symbols: &'table SymbolTable<'data, Xcoff, R>,
    index: usize,
}

impl<'data, 'table, Xcoff: FileHeader, R: ReadRef<'data>> Iterator
    for SymbolIterator<'data, 'table, Xcoff, R>
{
    type Item = (SymbolIndex, &'data Xcoff::Symbol);

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.index;
        let symbol = self.symbols.symbol(index).ok()?;
        self.index += 1 + symbol.n_numaux() as usize;
        Some((SymbolIndex(index), symbol))
    }
}

/// A symbol table in an [`XcoffFile32`](super::XcoffFile32).
pub type XcoffSymbolTable32<'data, 'file, R = &'data [u8]> =
    XcoffSymbolTable<'data, 'file, xcoff::FileHeader32, R>;
/// A symbol table in an [`XcoffFile64`](super::XcoffFile64).
pub type XcoffSymbolTable64<'data, 'file, R = &'data [u8]> =
    XcoffSymbolTable<'data, 'file, xcoff::FileHeader64, R>;

/// A symbol table in an [`XcoffFile`].
#[derive(Debug, Clone, Copy)]
pub struct XcoffSymbolTable<'data, 'file, Xcoff, R = &'data [u8]>
where
    Xcoff: FileHeader,
    R: ReadRef<'data>,
{
    pub(super) 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.iter(),
        }
    }

    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 for the symbols in an [`XcoffFile32`](super::XcoffFile32).
pub type XcoffSymbolIterator32<'data, 'file, R = &'data [u8]> =
    XcoffSymbolIterator<'data, 'file, xcoff::FileHeader32, R>;
/// An iterator for the symbols in an [`XcoffFile64`](super::XcoffFile64).
pub type XcoffSymbolIterator64<'data, 'file, R = &'data [u8]> =
    XcoffSymbolIterator<'data, 'file, xcoff::FileHeader64, R>;

/// An iterator for the symbols in an [`XcoffFile`].
pub struct XcoffSymbolIterator<'data, 'file, Xcoff, R = &'data [u8]>
where
    Xcoff: FileHeader,
    R: ReadRef<'data>,
{
    pub(super) file: &'file XcoffFile<'data, Xcoff, R>,
    pub(super) symbols: SymbolIterator<'data, 'file, Xcoff, R>,
}

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, symbol) = self.symbols.next()?;
        Some(XcoffSymbol {
            file: self.file,
            symbols: self.symbols.symbols,
            index,
            symbol,
        })
    }
}

/// A symbol in an [`XcoffFile32`](super::XcoffFile32).
pub type XcoffSymbol32<'data, 'file, R = &'data [u8]> =
    XcoffSymbol<'data, 'file, xcoff::FileHeader32, R>;
/// A symbol in an [`XcoffFile64`](super::XcoffFile64).
pub type XcoffSymbol64<'data, 'file, R = &'data [u8]> =
    XcoffSymbol<'data, 'file, xcoff::FileHeader64, R>;

/// A symbol in an [`XcoffFile`].
///
/// Most functionality is provided by the [`ObjectSymbol`] trait implementation.
#[derive(Debug, Clone, Copy)]
pub struct XcoffSymbol<'data, 'file, Xcoff, R = &'data [u8]>
where
    Xcoff: FileHeader,
    R: ReadRef<'data>,
{
    pub(super) 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]> {
        if self.symbol.has_aux_file() {
            // By convention the file name is in the first auxiliary entry.
            self.symbols
                .aux_file(self.index.0, 1)?
                .fname(self.symbols.strings)
        } else {
            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
            | xcoff::C_INFO => 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();
                if sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_CM {
                    return aux_csect.x_scnlen();
                }
            }
        }
        0
    }

    fn kind(&self) -> SymbolKind {
        if self.symbol.has_aux_csect() {
            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();
                if sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_CM {
                    return match aux_csect.x_smclas() {
                        xcoff::XMC_PR | xcoff::XMC_GL => SymbolKind::Text,
                        xcoff::XMC_RO | xcoff::XMC_RW | xcoff::XMC_TD | xcoff::XMC_BS => {
                            SymbolKind::Data
                        }
                        xcoff::XMC_TL | xcoff::XMC_UL => SymbolKind::Tls,
                        xcoff::XMC_DS | xcoff::XMC_TC0 | xcoff::XMC_TC => {
                            // `Metadata` might be a better kind for these if we had it.
                            SymbolKind::Data
                        }
                        _ => SymbolKind::Unknown,
                    };
                } else if sym_type == xcoff::XTY_LD {
                    // A function entry point. Neither `Text` nor `Label` are a good fit for this.
                    return SymbolKind::Text;
                } else if sym_type == xcoff::XTY_ER {
                    return SymbolKind::Unknown;
                }
            }
        }
        match self.symbol.n_sclass() {
            xcoff::C_NULL => SymbolKind::Null,
            xcoff::C_FILE => SymbolKind::File,
            _ => 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.n_scnum() <= 0 {
            return false;
        }
        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 sym_type = aux_csect.sym_type();
                sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_LD || sym_type == xcoff::XTY_CM
            } 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 => {
                    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, SymbolIndex> {
        let mut x_smtyp = 0;
        let mut x_smclas = 0;
        let mut containing_csect = None;
        if self.symbol.has_aux_csect() {
            if let Ok(aux_csect) = self
                .file
                .symbols
                .aux_csect(self.index.0, self.symbol.n_numaux() as usize)
            {
                x_smtyp = aux_csect.x_smtyp();
                x_smclas = aux_csect.x_smclas();
                if aux_csect.sym_type() == xcoff::XTY_LD {
                    containing_csect = Some(SymbolIndex(aux_csect.x_scnlen() as usize))
                }
            }
        }
        SymbolFlags::Xcoff {
            n_sclass: self.symbol.n_sclass(),
            x_smtyp,
            x_smclas,
            containing_csect,
        }
    }
}

/// A trait for generic access to [`xcoff::Symbol32`] and [`xcoff::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_offset(&self) -> Option<u32>;
    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
    }

    fn name_offset(&self) -> Option<u32> {
        Some(self.n_offset.get(BE))
    }

    /// 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
    }

    fn name_offset(&self) -> Option<u32> {
        if self.n_name[0] == 0 {
            let offset = u32::from_be_bytes(self.n_name[4..8].try_into().unwrap());
            Some(offset)
        } else {
            None
        }
    }

    /// Parse the symbol name for XCOFF32.
    fn name<'data, R: ReadRef<'data>>(
        &'data self,
        strings: StringTable<'data, R>,
    ) -> Result<&'data [u8]> {
        if let Some(offset) = self.name_offset() {
            // If the name starts with 0 then the last 4 bytes are a string table offset.
            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 [`xcoff::FileAux32`] and [`xcoff::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>;

    fn name_offset(&self) -> Option<u32> {
        let x_fname = self.x_fname();
        if x_fname[0] == 0 {
            Some(u32::from_be_bytes(x_fname[4..8].try_into().unwrap()))
        } else {
            None
        }
    }

    /// Parse the x_fname field, which may be an inline string or a string table offset.
    fn fname<'data, R: ReadRef<'data>>(
        &'data self,
        strings: StringTable<'data, R>,
    ) -> Result<&'data [u8]> {
        if let Some(offset) = self.name_offset() {
            // If the name starts with 0 then the last 4 bytes are a string table offset.
            strings
                .get(offset)
                .read_error("Invalid XCOFF symbol name offset")
        } else {
            // The name is inline and padded with nulls.
            let x_fname = self.x_fname();
            Ok(match memchr::memchr(b'\0', x_fname) {
                Some(end) => &x_fname[..end],
                None => x_fname,
            })
        }
    }
}

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 [`xcoff::CsectAux32`] and [`xcoff::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_stab(&self) -> Option<u32>;
    fn x_snstab(&self) -> Option<u16>;
    fn x_auxtype(&self) -> Option<u8>;

    fn alignment(&self) -> u8 {
        self.x_smtyp() >> 3
    }
    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_stab(&self) -> Option<u32> {
        None
    }

    fn x_snstab(&self) -> Option<u16> {
        None
    }

    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_stab(&self) -> Option<u32> {
        Some(self.x_stab.get(BE))
    }

    fn x_snstab(&self) -> Option<u16> {
        Some(self.x_snstab.get(BE))
    }

    fn x_auxtype(&self) -> Option<u8> {
        None
    }
}