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
use alloc::vec::Vec;

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

use super::FileHeader;

/// A version index.
#[derive(Debug, Default, Clone, Copy)]
pub struct VersionIndex(pub u16);

impl VersionIndex {
    /// Return the version index.
    pub fn index(&self) -> u16 {
        self.0 & elf::VERSYM_VERSION
    }

    /// Return true if it is the local index.
    pub fn is_local(&self) -> bool {
        self.index() == elf::VER_NDX_LOCAL
    }

    /// Return true if it is the global index.
    pub fn is_global(&self) -> bool {
        self.index() == elf::VER_NDX_GLOBAL
    }

    /// Return the hidden flag.
    pub fn is_hidden(&self) -> bool {
        self.0 & elf::VERSYM_HIDDEN != 0
    }
}

/// A version definition or requirement.
///
/// This is derived from entries in the `SHT_GNU_verdef` and `SHT_GNU_verneed` sections.
#[derive(Debug, Default, Clone, Copy)]
pub struct Version<'data> {
    name: &'data [u8],
    hash: u32,
    // Used to keep track of valid indices in `VersionTable`.
    valid: bool,
}

impl<'data> Version<'data> {
    /// Return the version name.
    pub fn name(&self) -> &'data [u8] {
        self.name
    }

    /// Return hash of the version name.
    pub fn hash(&self) -> u32 {
        self.hash
    }
}

/// A table of version definitions and requirements.
///
/// It allows looking up the version information for a given symbol index.
///
/// This is derived from entries in the `SHT_GNU_versym`, `SHT_GNU_verdef` and `SHT_GNU_verneed` sections.
#[derive(Debug, Clone)]
pub struct VersionTable<'data, Elf: FileHeader> {
    symbols: &'data [elf::Versym<Elf::Endian>],
    versions: Vec<Version<'data>>,
}

impl<'data, Elf: FileHeader> Default for VersionTable<'data, Elf> {
    fn default() -> Self {
        VersionTable {
            symbols: &[],
            versions: Vec::new(),
        }
    }
}

impl<'data, Elf: FileHeader> VersionTable<'data, Elf> {
    /// Parse the version sections.
    pub fn parse<R: ReadRef<'data>>(
        endian: Elf::Endian,
        versyms: &'data [elf::Versym<Elf::Endian>],
        verdefs: Option<VerdefIterator<'data, Elf>>,
        verneeds: Option<VerneedIterator<'data, Elf>>,
        strings: StringTable<'data, R>,
    ) -> Result<Self> {
        let mut max_index = 0;
        if let Some(mut verdefs) = verdefs.clone() {
            while let Some((verdef, _)) = verdefs.next()? {
                if verdef.vd_flags.get(endian) & elf::VER_FLG_BASE != 0 {
                    continue;
                }
                let index = verdef.vd_ndx.get(endian) & elf::VERSYM_VERSION;
                if max_index < index {
                    max_index = index;
                }
            }
        }
        if let Some(mut verneeds) = verneeds.clone() {
            while let Some((_, mut vernauxs)) = verneeds.next()? {
                while let Some(vernaux) = vernauxs.next()? {
                    let index = vernaux.vna_other.get(endian) & elf::VERSYM_VERSION;
                    if max_index < index {
                        max_index = index;
                    }
                }
            }
        }

        // Indices should be sequential, but this could be up to
        // 32k * size_of::<Version>() if max_index is bad.
        let mut versions = vec![Version::default(); max_index as usize + 1];

        if let Some(mut verdefs) = verdefs {
            while let Some((verdef, mut verdauxs)) = verdefs.next()? {
                if verdef.vd_flags.get(endian) & elf::VER_FLG_BASE != 0 {
                    continue;
                }
                let index = verdef.vd_ndx.get(endian) & elf::VERSYM_VERSION;
                if index <= elf::VER_NDX_GLOBAL {
                    // TODO: return error?
                    continue;
                }
                if let Some(verdaux) = verdauxs.next()? {
                    versions[usize::from(index)] = Version {
                        name: verdaux.name(endian, strings)?,
                        hash: verdef.vd_hash.get(endian),
                        valid: true,
                    };
                }
            }
        }
        if let Some(mut verneeds) = verneeds {
            while let Some((_, mut vernauxs)) = verneeds.next()? {
                while let Some(vernaux) = vernauxs.next()? {
                    let index = vernaux.vna_other.get(endian) & elf::VERSYM_VERSION;
                    if index <= elf::VER_NDX_GLOBAL {
                        // TODO: return error?
                        continue;
                    }
                    versions[usize::from(index)] = Version {
                        name: vernaux.name(endian, strings)?,
                        hash: vernaux.vna_hash.get(endian),
                        valid: true,
                    };
                }
            }
        }

        Ok(VersionTable {
            symbols: versyms,
            versions,
        })
    }

    /// Return true if the version table is empty.
    pub fn is_empty(&self) -> bool {
        self.symbols.is_empty()
    }

    /// Return version index for a given symbol index.
    pub fn version_index(&self, endian: Elf::Endian, index: usize) -> VersionIndex {
        let version_index = match self.symbols.get(index) {
            Some(x) => x.0.get(endian),
            // Ideally this would be VER_NDX_LOCAL for undefined symbols,
            // but currently there are no checks that need this distinction.
            None => elf::VER_NDX_GLOBAL,
        };
        VersionIndex(version_index)
    }

    /// Return version information for a given symbol version index.
    ///
    /// Returns `Ok(None)` for local and global versions.
    /// Returns `Err(_)` if index is invalid.
    pub fn version(&self, index: VersionIndex) -> Result<Option<&Version<'data>>> {
        if index.index() <= elf::VER_NDX_GLOBAL {
            return Ok(None);
        }
        self.versions
            .get(usize::from(index.index()))
            .filter(|version| version.valid)
            .read_error("Invalid ELF symbol version index")
            .map(Some)
    }

    /// Return true if the given symbol index satisifies the requirements of `need`.
    ///
    /// Returns false for any error.
    ///
    /// Note: this function hasn't been fully tested and is likely to be incomplete.
    pub fn matches(&self, endian: Elf::Endian, index: usize, need: Option<&Version>) -> bool {
        let version_index = self.version_index(endian, index);
        let def = match self.version(version_index) {
            Ok(def) => def,
            Err(_) => return false,
        };
        match (def, need) {
            (Some(def), Some(need)) => need.hash == def.hash && need.name == def.name,
            (None, Some(_need)) => {
                // Version must be present if needed.
                false
            }
            (Some(_def), None) => {
                // For a dlsym call, use the newest version.
                // TODO: if not a dlsym call, then use the oldest version.
                !version_index.is_hidden()
            }
            (None, None) => true,
        }
    }
}

/// An iterator over the entries in an ELF `SHT_GNU_verdef` section.
#[derive(Debug, Clone)]
pub struct VerdefIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> VerdefIterator<'data, Elf> {
    pub(super) fn new(endian: Elf::Endian, data: &'data [u8]) -> Self {
        VerdefIterator {
            endian,
            data: Bytes(data),
        }
    }

    /// Return the next `Verdef` entry.
    pub fn next(
        &mut self,
    ) -> Result<Option<(&'data elf::Verdef<Elf::Endian>, VerdauxIterator<'data, Elf>)>> {
        if self.data.is_empty() {
            return Ok(None);
        }

        let verdef = self
            .data
            .read_at::<elf::Verdef<_>>(0)
            .read_error("ELF verdef is too short")?;

        let mut verdaux_data = self.data;
        verdaux_data
            .skip(verdef.vd_aux.get(self.endian) as usize)
            .read_error("Invalid ELF vd_aux")?;
        let verdaux =
            VerdauxIterator::new(self.endian, verdaux_data.0, verdef.vd_cnt.get(self.endian));

        let next = verdef.vd_next.get(self.endian);
        if next != 0 {
            self.data
                .skip(next as usize)
                .read_error("Invalid ELF vd_next")?;
        } else {
            self.data = Bytes(&[]);
        }
        Ok(Some((verdef, verdaux)))
    }
}

/// An iterator over the auxiliary records for an entry in an ELF `SHT_GNU_verdef` section.
#[derive(Debug, Clone)]
pub struct VerdauxIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
    count: u16,
}

impl<'data, Elf: FileHeader> VerdauxIterator<'data, Elf> {
    pub(super) fn new(endian: Elf::Endian, data: &'data [u8], count: u16) -> Self {
        VerdauxIterator {
            endian,
            data: Bytes(data),
            count,
        }
    }

    /// Return the next `Verdaux` entry.
    pub fn next(&mut self) -> Result<Option<&'data elf::Verdaux<Elf::Endian>>> {
        if self.count == 0 {
            return Ok(None);
        }

        let verdaux = self
            .data
            .read_at::<elf::Verdaux<_>>(0)
            .read_error("ELF verdaux is too short")?;

        self.data
            .skip(verdaux.vda_next.get(self.endian) as usize)
            .read_error("Invalid ELF vda_next")?;
        self.count -= 1;
        Ok(Some(verdaux))
    }
}

/// An iterator over the entries in an ELF `SHT_GNU_verneed` section.
#[derive(Debug, Clone)]
pub struct VerneedIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
}

impl<'data, Elf: FileHeader> VerneedIterator<'data, Elf> {
    pub(super) fn new(endian: Elf::Endian, data: &'data [u8]) -> Self {
        VerneedIterator {
            endian,
            data: Bytes(data),
        }
    }

    /// Return the next `Verneed` entry.
    pub fn next(
        &mut self,
    ) -> Result<
        Option<(
            &'data elf::Verneed<Elf::Endian>,
            VernauxIterator<'data, Elf>,
        )>,
    > {
        if self.data.is_empty() {
            return Ok(None);
        }

        let verneed = self
            .data
            .read_at::<elf::Verneed<_>>(0)
            .read_error("ELF verneed is too short")?;

        let mut vernaux_data = self.data;
        vernaux_data
            .skip(verneed.vn_aux.get(self.endian) as usize)
            .read_error("Invalid ELF vn_aux")?;
        let vernaux =
            VernauxIterator::new(self.endian, vernaux_data.0, verneed.vn_cnt.get(self.endian));

        let next = verneed.vn_next.get(self.endian);
        if next != 0 {
            self.data
                .skip(next as usize)
                .read_error("Invalid ELF vn_next")?;
        } else {
            self.data = Bytes(&[]);
        }
        Ok(Some((verneed, vernaux)))
    }
}

/// An iterator over the auxiliary records for an entry in an ELF `SHT_GNU_verneed` section.
#[derive(Debug, Clone)]
pub struct VernauxIterator<'data, Elf: FileHeader> {
    endian: Elf::Endian,
    data: Bytes<'data>,
    count: u16,
}

impl<'data, Elf: FileHeader> VernauxIterator<'data, Elf> {
    pub(super) fn new(endian: Elf::Endian, data: &'data [u8], count: u16) -> Self {
        VernauxIterator {
            endian,
            data: Bytes(data),
            count,
        }
    }

    /// Return the next `Vernaux` entry.
    pub fn next(&mut self) -> Result<Option<&'data elf::Vernaux<Elf::Endian>>> {
        if self.count == 0 {
            return Ok(None);
        }

        let vernaux = self
            .data
            .read_at::<elf::Vernaux<_>>(0)
            .read_error("ELF vernaux is too short")?;

        self.data
            .skip(vernaux.vna_next.get(self.endian) as usize)
            .read_error("Invalid ELF vna_next")?;
        self.count -= 1;
        Ok(Some(vernaux))
    }
}

impl<Endian: endian::Endian> elf::Verdaux<Endian> {
    /// Parse the version name from the string table.
    pub fn name<'data, R: ReadRef<'data>>(
        &self,
        endian: Endian,
        strings: StringTable<'data, R>,
    ) -> Result<&'data [u8]> {
        strings
            .get(self.vda_name.get(endian))
            .read_error("Invalid ELF vda_name")
    }
}

impl<Endian: endian::Endian> elf::Verneed<Endian> {
    /// Parse the file from the string table.
    pub fn file<'data, R: ReadRef<'data>>(
        &self,
        endian: Endian,
        strings: StringTable<'data, R>,
    ) -> Result<&'data [u8]> {
        strings
            .get(self.vn_file.get(endian))
            .read_error("Invalid ELF vn_file")
    }
}

impl<Endian: endian::Endian> elf::Vernaux<Endian> {
    /// Parse the version name from the string table.
    pub fn name<'data, R: ReadRef<'data>>(
        &self,
        endian: Endian,
        strings: StringTable<'data, R>,
    ) -> Result<&'data [u8]> {
        strings
            .get(self.vna_name.get(endian))
            .read_error("Invalid ELF vna_name")
    }
}