Skip to main content

ms_pdb/
coff_groups.rs

1use std::cmp::Ordering;
2
3use ms_codeview::syms::OffsetSegment;
4
5/// Contains a list of COFF groups.
6#[derive(Clone, Default, Debug)]
7pub struct CoffGroups {
8    /// The COFF groups
9    pub vec: Vec<CoffGroup>,
10}
11
12/// Describes a single COFF group.
13///
14/// A COFF group is a contiguous region within a COFF section. For this reason, they are sometimes
15/// called "subsections".
16#[derive(Clone, Debug)]
17pub struct CoffGroup {
18    /// Name of the section
19    pub name: String,
20    /// Bit flags
21    pub characteristics: u32,
22    /// The location where this COFF group begins. The COFF group is contained entirely within
23    /// a single COFF section.
24    pub offset_segment: OffsetSegment,
25    /// The size in bytes of the COFF group.
26    pub size: u32,
27}
28
29impl CoffGroups {
30    /// Find the COFF group which contains `offset_segment`.
31    pub fn find_group_at(&self, offset_segment: OffsetSegment) -> Option<&CoffGroup> {
32        let seg = offset_segment.segment.get();
33        let off = offset_segment.offset.get();
34
35        match self.vec.binary_search_by(|g| {
36            let g_seg: u16 = g.offset_segment.segment();
37            let c = g_seg.cmp(&seg);
38            if c.is_ne() {
39                return c;
40            }
41
42            let g_off = g.offset_segment.offset();
43            if off < g_off {
44                return Ordering::Greater;
45            }
46
47            let offset_within_group = off - g_off;
48            if offset_within_group < g.size {
49                Ordering::Equal
50            } else {
51                Ordering::Less
52            }
53        }) {
54            Ok(i) => Some(&self.vec[i]),
55            Err(_) => None,
56        }
57    }
58}