elf_utilities/segment/
segment_type.rs

1//! Type definitions for segment types.
2
3use crate::*;
4
5#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
6pub enum Type {
7    /// Program header table entry unused
8    Null,
9    /// Loadable program segment
10    Load,
11    /// dynamic linking information
12    Dynamic,
13    /// Program interpreter
14    Interp,
15    /// Auxiliary information
16    Note,
17    /// Reserved
18    ShLib,
19    /// Entry for header table itself
20    Phdr,
21    /// Thread-local storage segment
22    TLS,
23    /// Number of defined types
24    Num,
25    /// GCC .eh_frame_hdr segment
26    GNUEHFrame,
27    /// Indicates stack executability
28    GNUStack,
29    /// Read-only after relocation
30    GNURelRO,
31    /// User-defined values
32    Any(Elf64Word),
33}
34
35impl Type {
36    pub fn to_bytes(&self) -> Elf64Word {
37        match self {
38            Self::Null => 0,
39            Self::Load => 1,
40            Self::Dynamic => 2,
41            Self::Interp => 3,
42            Self::Note => 4,
43            Self::ShLib => 5,
44            Self::Phdr => 6,
45            Self::TLS => 7,
46            Self::Num => 8,
47            Self::GNUEHFrame => 0x6474e550,
48            Self::GNUStack => 0x6474e551,
49            Self::GNURelRO => 0x6474e552,
50            Self::Any(c) => *c,
51        }
52    }
53}
54
55impl From<Elf64Word> for Type {
56    fn from(bytes: Elf64Word) -> Self {
57        match bytes {
58            0 => Self::Null,
59            1 => Self::Load,
60            2 => Self::Dynamic,
61            3 => Self::Interp,
62            4 => Self::Note,
63            5 => Self::ShLib,
64            6 => Self::Phdr,
65            7 => Self::TLS,
66            8 => Self::Num,
67            0x6474e550 => Self::GNUEHFrame,
68            0x6474e551 => Self::GNUStack,
69            0x6474e552 => Self::GNURelRO,
70            _ => Self::Any(bytes),
71        }
72    }
73}