Skip to main content

macho_core/model/
segment.rs

1use crate::format::constants::{SegmentFlags, VmProtection};
2use crate::model::addr::{ThinFileOffset, Va};
3use crate::model::names::SegmentName;
4use crate::model::section::Section;
5
6#[derive(Debug, Clone)]
7/// The Segment type.
8pub struct Segment {
9    pub(crate) name: SegmentName,
10    pub(crate) vm_addr: Va,
11    pub(crate) vm_size: u64,
12    pub(crate) file_offset: ThinFileOffset,
13    pub(crate) file_size: u64,
14    pub(crate) max_prot: VmProtection,
15    pub(crate) init_prot: VmProtection,
16    pub(crate) flags: SegmentFlags,
17    pub(crate) sections: Vec<Section>,
18}
19
20impl Segment {
21    /// Fixed-width segment name.
22    pub const fn name(&self) -> &SegmentName {
23        &self.name
24    }
25    /// Segment virtual start address.
26    pub const fn vm_addr(&self) -> Va {
27        self.vm_addr
28    }
29    /// Segment virtual size in bytes.
30    pub const fn vm_size(&self) -> u64 {
31        self.vm_size
32    }
33    /// Slice-relative start of file-backed bytes.
34    pub const fn file_offset(&self) -> ThinFileOffset {
35        self.file_offset
36    }
37    /// File-backed length in bytes.
38    pub const fn file_size(&self) -> u64 {
39        self.file_size
40    }
41    /// Maximum virtual-memory protections.
42    pub const fn max_prot(&self) -> VmProtection {
43        self.max_prot
44    }
45    /// Initial virtual-memory protections.
46    pub const fn init_prot(&self) -> VmProtection {
47        self.init_prot
48    }
49    /// Parsed segment flags.
50    pub const fn flags(&self) -> SegmentFlags {
51        self.flags
52    }
53    /// Validated sections in load-command order.
54    pub fn sections(&self) -> &[Section] {
55        &self.sections
56    }
57}