ms_codeview/syms/
offset_segment.rs

1use super::*;
2
3/// Stores an `offset` and `segment` pair, in that order. This structure is directly embedded in
4/// on-disk structures.
5#[repr(C)]
6#[derive(IntoBytes, FromBytes, Immutable, KnownLayout, Unaligned, Default, Clone, Copy, Eq)]
7pub struct OffsetSegment {
8    /// The offset in bytes of a symbol within a segment.
9    pub offset: U32<LE>,
10
11    /// The segment (section) index.
12    pub segment: U16<LE>,
13}
14
15impl PartialEq for OffsetSegment {
16    #[inline]
17    fn eq(&self, other: &Self) -> bool {
18        self.as_u64() == other.as_u64()
19    }
20}
21
22impl PartialOrd for OffsetSegment {
23    #[inline]
24    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
25        Some(self.cmp(other))
26    }
27}
28
29impl Ord for OffsetSegment {
30    #[inline]
31    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
32        Ord::cmp(&self.as_u64(), &other.as_u64())
33    }
34}
35
36impl OffsetSegment {
37    /// The offset in bytes of a symbol within a segment.
38    #[inline]
39    pub fn offset(&self) -> u32 {
40        self.offset.get()
41    }
42
43    /// The segment (section) index.
44    #[inline]
45    pub fn segment(&self) -> u16 {
46        self.segment.get()
47    }
48
49    /// Combines the segment and offset into a tuple. The segment is the first element and the
50    /// offset is the second element. This order gives a sorting order that sorts by segment first.
51    #[inline]
52    pub fn as_tuple(&self) -> (u16, u32) {
53        (self.segment.get(), self.offset.get())
54    }
55
56    /// Combines the segment and offset into a single `u64` value, with the segment in the
57    /// higher-order bits. This allows for efficient comparisons.
58    #[inline]
59    pub fn as_u64(&self) -> u64 {
60        ((self.segment.get() as u64) << 32) | (self.offset.get() as u64)
61    }
62}
63
64impl std::fmt::Display for OffsetSegment {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "[{:04x}:{:08x}]", self.segment.get(), self.offset.get())
67    }
68}
69
70impl Debug for OffsetSegment {
71    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72        <Self as std::fmt::Display>::fmt(self, f)
73    }
74}
75
76impl std::hash::Hash for OffsetSegment {
77    #[inline]
78    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
79        state.write_u16(self.segment());
80        state.write_u32(self.offset());
81    }
82}