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
use core::convert::TryInto;

use crate::elf;
use crate::endian;
use crate::read::{Bytes, Error, ReadError, Result};

use super::FileHeader;

/// An ELF attributes section.
///
/// This may be a GNU attributes section, or an architecture specific attributes section.
///
/// An attributes section contains a series of [`AttributesSubsection`].
///
/// Returned by [`SectionHeader::attributes`](super::SectionHeader::attributes)
/// and [`SectionHeader::gnu_attributes`](super::SectionHeader::gnu_attributes).
#[derive(Debug, Clone)]
pub struct AttributesSection<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    version: u8,
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> AttributesSection<'data, Elf> {
    /// Parse an ELF attributes section given the section data.
    pub fn new(endian: Elf::Endian, data: &'data [u8]) -> Result<Self> {
        let mut data = Bytes(data);

        // Skip the version field that is one byte long.
        // If the section is empty then the version doesn't matter.
        let version = data.read::<u8>().cloned().unwrap_or(b'A');

        Ok(AttributesSection {
            endian,
            version,
            data,
        })
    }

    /// Return the version of the attributes section.
    pub fn version(&self) -> u8 {
        self.version
    }

    /// Return an iterator over the subsections.
    pub fn subsections(&self) -> Result<AttributesSubsectionIterator<'data, Elf>> {
        // There is currently only one format version.
        if self.version != b'A' {
            return Err(Error("Unsupported ELF attributes section version"));
        }

        Ok(AttributesSubsectionIterator {
            endian: self.endian,
            data: self.data,
        })
    }
}

/// An iterator for the subsections in an [`AttributesSection`].
#[derive(Debug, Clone)]
pub struct AttributesSubsectionIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> AttributesSubsectionIterator<'data, Elf> {
    /// Return the next subsection.
    pub fn next(&mut self) -> Result<Option<AttributesSubsection<'data, Elf>>> {
        if self.data.is_empty() {
            return Ok(None);
        }

        let result = self.parse().map(Some);
        if result.is_err() {
            self.data = Bytes(&[]);
        }
        result
    }

    fn parse(&mut self) -> Result<AttributesSubsection<'data, Elf>> {
        // First read the subsection length.
        let mut data = self.data;
        let length = data
            .read::<endian::U32Bytes<Elf::Endian>>()
            .read_error("ELF attributes section is too short")?
            .get(self.endian);

        // Now read the entire subsection, updating self.data.
        let mut data = self
            .data
            .read_bytes(length as usize)
            .read_error("Invalid ELF attributes subsection length")?;
        // Skip the subsection length field.
        data.skip(4)
            .read_error("Invalid ELF attributes subsection length")?;

        // TODO: errors here should not prevent reading the next subsection.
        let vendor = data
            .read_string()
            .read_error("Invalid ELF attributes vendor")?;

        Ok(AttributesSubsection {
            endian: self.endian,
            length,
            vendor,
            data,
        })
    }
}

impl<'data, Elf: FileHeader> Iterator for AttributesSubsectionIterator<'data, Elf> {
    type Item = Result<AttributesSubsection<'data, Elf>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next().transpose()
    }
}

/// A subsection in an [`AttributesSection`].
///
/// A subsection is identified by a vendor name.  It contains a series of
/// [`AttributesSubsubsection`].
#[derive(Debug, Clone)]
pub struct AttributesSubsection<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    length: u32,
    vendor: &'data [u8],
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> AttributesSubsection<'data, Elf> {
    /// Return the length of the attributes subsection.
    pub fn length(&self) -> u32 {
        self.length
    }

    /// Return the vendor name of the attributes subsection.
    pub fn vendor(&self) -> &'data [u8] {
        self.vendor
    }

    /// Return an iterator over the sub-subsections.
    pub fn subsubsections(&self) -> AttributesSubsubsectionIterator<'data, Elf> {
        AttributesSubsubsectionIterator {
            endian: self.endian,
            data: self.data,
        }
    }
}

/// An iterator for the sub-subsections in an [`AttributesSubsection`].
#[derive(Debug, Clone)]
pub struct AttributesSubsubsectionIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> AttributesSubsubsectionIterator<'data, Elf> {
    /// Return the next sub-subsection.
    pub fn next(&mut self) -> Result<Option<AttributesSubsubsection<'data>>> {
        if self.data.is_empty() {
            return Ok(None);
        }

        let result = self.parse().map(Some);
        if result.is_err() {
            self.data = Bytes(&[]);
        }
        result
    }

    fn parse(&mut self) -> Result<AttributesSubsubsection<'data>> {
        // The format of a sub-section looks like this:
        //
        // <file-tag> <size> <attribute>*
        // | <section-tag> <size> <section-number>* 0 <attribute>*
        // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
        let mut data = self.data;
        let tag = *data
            .read::<u8>()
            .read_error("ELF attributes subsection is too short")?;
        let length = data
            .read::<endian::U32Bytes<Elf::Endian>>()
            .read_error("ELF attributes subsection is too short")?
            .get(self.endian);

        // Now read the entire sub-subsection, updating self.data.
        let mut data = self
            .data
            .read_bytes(length as usize)
            .read_error("Invalid ELF attributes sub-subsection length")?;
        // Skip the tag and sub-subsection size field.
        data.skip(1 + 4)
            .read_error("Invalid ELF attributes sub-subsection length")?;

        // TODO: errors here should not prevent reading the next sub-subsection.
        let indices = if tag == elf::Tag_Section || tag == elf::Tag_Symbol {
            data.read_string()
                .map(Bytes)
                .read_error("Missing ELF attributes sub-subsection indices")?
        } else if tag == elf::Tag_File {
            Bytes(&[])
        } else {
            return Err(Error("Unimplemented ELF attributes sub-subsection tag"));
        };

        Ok(AttributesSubsubsection {
            tag,
            length,
            indices,
            data,
        })
    }
}

impl<'data, Elf: FileHeader> Iterator for AttributesSubsubsectionIterator<'data, Elf> {
    type Item = Result<AttributesSubsubsection<'data>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next().transpose()
    }
}

/// A sub-subsection in an [`AttributesSubsection`].
///
/// A sub-subsection is identified by a tag.  It contains an optional series of indices,
/// followed by a series of attributes.
#[derive(Debug, Clone)]
pub struct AttributesSubsubsection<'data> {
    tag: u8,
    length: u32,
    indices: Bytes<'data>,
    data: Bytes<'data>,
}

impl<'data> AttributesSubsubsection<'data> {
    /// Return the tag of the attributes sub-subsection.
    pub fn tag(&self) -> u8 {
        self.tag
    }

    /// Return the length of the attributes sub-subsection.
    pub fn length(&self) -> u32 {
        self.length
    }

    /// Return the data containing the indices.
    pub fn indices_data(&self) -> &'data [u8] {
        self.indices.0
    }

    /// Return the indices.
    ///
    /// This will be section indices if the tag is `Tag_Section`,
    /// or symbol indices if the tag is `Tag_Symbol`,
    /// and otherwise it will be empty.
    pub fn indices(&self) -> AttributeIndexIterator<'data> {
        AttributeIndexIterator { data: self.indices }
    }

    /// Return the data containing the attributes.
    pub fn attributes_data(&self) -> &'data [u8] {
        self.data.0
    }

    /// Return a parser for the data containing the attributes.
    pub fn attributes(&self) -> AttributeReader<'data> {
        AttributeReader { data: self.data }
    }
}

/// An iterator over the indices in an [`AttributesSubsubsection`].
#[derive(Debug, Clone)]
pub struct AttributeIndexIterator<'data> {
    data: Bytes<'data>,
}

impl<'data> AttributeIndexIterator<'data> {
    /// Parse the next index.
    pub fn next(&mut self) -> Result<Option<u32>> {
        if self.data.is_empty() {
            return Ok(None);
        }

        let result = self.parse().map(Some);
        if result.is_err() {
            self.data = Bytes(&[]);
        }
        result
    }

    fn parse(&mut self) -> Result<u32> {
        let err = "Invalid ELF attribute index";
        self.data
            .read_uleb128()
            .read_error(err)?
            .try_into()
            .map_err(|_| ())
            .read_error(err)
    }
}

impl<'data> Iterator for AttributeIndexIterator<'data> {
    type Item = Result<u32>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next().transpose()
    }
}

/// A parser for the attributes in an [`AttributesSubsubsection`].
///
/// The parser relies on the caller to know the format of the data for each attribute tag.
#[derive(Debug, Clone)]
pub struct AttributeReader<'data> {
    data: Bytes<'data>,
}

impl<'data> AttributeReader<'data> {
    /// Parse a tag.
    pub fn read_tag(&mut self) -> Result<Option<u64>> {
        if self.data.is_empty() {
            return Ok(None);
        }
        let err = "Invalid ELF attribute tag";
        self.data.read_uleb128().read_error(err).map(Some)
    }

    /// Parse an integer value.
    pub fn read_integer(&mut self) -> Result<u64> {
        let err = "Invalid ELF attribute integer value";
        self.data.read_uleb128().read_error(err)
    }

    /// Parse a string value.
    pub fn read_string(&mut self) -> Result<&'data [u8]> {
        let err = "Invalid ELF attribute string value";
        self.data.read_string().read_error(err)
    }
}