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
use core::fmt::Debug;
use core::{iter, slice, str};

use crate::elf;
use crate::endian::{Endianness, U32Bytes};
use crate::read::{self, ComdatKind, ObjectComdat, ReadError, ReadRef, SectionIndex, SymbolIndex};

use super::{ElfFile, FileHeader, SectionHeader, Sym};

/// An iterator for the COMDAT section groups in an [`ElfFile32`](super::ElfFile32).
pub type ElfComdatIterator32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdatIterator<'data, 'file, elf::FileHeader32<Endian>, R>;
/// An iterator for the COMDAT section groups in an [`ElfFile64`](super::ElfFile64).
pub type ElfComdatIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdatIterator<'data, 'file, elf::FileHeader64<Endian>, R>;

/// An iterator for the COMDAT section groups in an [`ElfFile`].
#[derive(Debug)]
pub struct ElfComdatIterator<'data, 'file, Elf, R = &'data [u8]>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    file: &'file ElfFile<'data, Elf, R>,
    iter: iter::Enumerate<slice::Iter<'data, Elf::SectionHeader>>,
}

impl<'data, 'file, Elf, R> ElfComdatIterator<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    pub(super) fn new(
        file: &'file ElfFile<'data, Elf, R>,
    ) -> ElfComdatIterator<'data, 'file, Elf, R> {
        let mut iter = file.sections.iter().enumerate();
        iter.next(); // Skip null section.
        ElfComdatIterator { file, iter }
    }
}

impl<'data, 'file, Elf, R> Iterator for ElfComdatIterator<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    type Item = ElfComdat<'data, 'file, Elf, R>;

    fn next(&mut self) -> Option<Self::Item> {
        for (_index, section) in self.iter.by_ref() {
            if let Some(comdat) = ElfComdat::parse(self.file, section) {
                return Some(comdat);
            }
        }
        None
    }
}

/// A COMDAT section group in an [`ElfFile32`](super::ElfFile32).
pub type ElfComdat32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdat<'data, 'file, elf::FileHeader32<Endian>, R>;
/// A COMDAT section group in an [`ElfFile64`](super::ElfFile64).
pub type ElfComdat64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdat<'data, 'file, elf::FileHeader64<Endian>, R>;

/// A COMDAT section group in an [`ElfFile`].
///
/// Most functionality is provided by the [`ObjectComdat`] trait implementation.
#[derive(Debug)]
pub struct ElfComdat<'data, 'file, Elf, R = &'data [u8]>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    file: &'file ElfFile<'data, Elf, R>,
    section: &'data Elf::SectionHeader,
    sections: &'data [U32Bytes<Elf::Endian>],
}

impl<'data, 'file, Elf, R> ElfComdat<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    fn parse(
        file: &'file ElfFile<'data, Elf, R>,
        section: &'data Elf::SectionHeader,
    ) -> Option<ElfComdat<'data, 'file, Elf, R>> {
        let (flag, sections) = section.group(file.endian, file.data).ok()??;
        if flag != elf::GRP_COMDAT {
            return None;
        }
        Some(ElfComdat {
            file,
            section,
            sections,
        })
    }

    /// Get the ELF file containing this COMDAT section group.
    pub fn elf_file(&self) -> &'file ElfFile<'data, Elf, R> {
        self.file
    }

    /// Get the raw ELF section header for the COMDAT section group.
    pub fn elf_section_header(&self) -> &'data Elf::SectionHeader {
        self.section
    }
}

impl<'data, 'file, Elf, R> read::private::Sealed for ElfComdat<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
}

impl<'data, 'file, Elf, R> ObjectComdat<'data> for ElfComdat<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    type SectionIterator = ElfComdatSectionIterator<'data, 'file, Elf, R>;

    #[inline]
    fn kind(&self) -> ComdatKind {
        ComdatKind::Any
    }

    #[inline]
    fn symbol(&self) -> SymbolIndex {
        SymbolIndex(self.section.sh_info(self.file.endian) as usize)
    }

    fn name_bytes(&self) -> read::Result<&'data [u8]> {
        // FIXME: check sh_link
        let index = self.symbol();
        let symbol = self.file.symbols.symbol(index)?;
        symbol.name(self.file.endian, self.file.symbols.strings())
    }

    fn name(&self) -> read::Result<&'data str> {
        let name = self.name_bytes()?;
        str::from_utf8(name)
            .ok()
            .read_error("Non UTF-8 ELF COMDAT name")
    }

    fn sections(&self) -> Self::SectionIterator {
        ElfComdatSectionIterator {
            file: self.file,
            sections: self.sections.iter(),
        }
    }
}

/// An iterator for the sections in a COMDAT section group in an [`ElfFile32`](super::ElfFile32).
pub type ElfComdatSectionIterator32<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdatSectionIterator<'data, 'file, elf::FileHeader32<Endian>, R>;
/// An iterator for the sections in a COMDAT section group in an [`ElfFile64`](super::ElfFile64).
pub type ElfComdatSectionIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> =
    ElfComdatSectionIterator<'data, 'file, elf::FileHeader64<Endian>, R>;

/// An iterator for the sections in a COMDAT section group in an [`ElfFile`].
#[derive(Debug)]
pub struct ElfComdatSectionIterator<'data, 'file, Elf, R = &'data [u8]>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    file: &'file ElfFile<'data, Elf, R>,
    sections: slice::Iter<'data, U32Bytes<Elf::Endian>>,
}

impl<'data, 'file, Elf, R> Iterator for ElfComdatSectionIterator<'data, 'file, Elf, R>
where
    Elf: FileHeader,
    R: ReadRef<'data>,
{
    type Item = SectionIndex;

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.sections.next()?;
        Some(SectionIndex(index.get(self.file.endian) as usize))
    }
}