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
//! Type definitions for section header types.

use crate::*;

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Type {
    /// Section header table entry unused
    Null,
    /// Program data
    ProgBits,
    /// Symbol table
    SymTab,
    /// String table
    StrTab,
    /// Relocation entries with addends
    Rela,
    /// Symbol hash table
    Hash,
    /// Dynamic linking information
    Dynamic,
    /// Notes
    Note,
    /// Program space with no data(.bss)
    NoBits,
    /// Relocation entries, no addends
    Rel,
    /// Reserved
    ShLib,
    /// Dynamic linker symbol table
    DynSym,
    /// Array of constructors
    InitArray,
    /// Array of destructors
    FiniArray,
    /// Array of preconstructors
    PreInitArray,
    /// Section group
    Group,
    /// Extended section indices
    SymTabShNdx,
    /// Number of defined types
    Num,
    Any(Elf64Word),
}

impl Type {
    pub fn to_bytes(&self) -> Elf64Word {
        match self {
            Self::Null => 0,
            Self::ProgBits => 1,
            Self::SymTab => 2,
            Self::StrTab => 3,
            Self::Rela => 4,
            Self::Hash => 5,
            Self::Dynamic => 6,
            Self::Note => 7,
            Self::NoBits => 8,
            Self::Rel => 9,
            Self::ShLib => 10,
            Self::DynSym => 11,
            Self::InitArray => 14,
            Self::FiniArray => 15,
            Self::PreInitArray => 16,
            Self::Group => 17,
            Self::SymTabShNdx => 18,
            Self::Num => 19,
            Self::Any(c) => *c,
        }
    }
}

impl From<Elf64Word> for Type {
    fn from(bytes: Elf64Word) -> Self {
        match bytes {
            0 => Self::Null,
            1 => Self::ProgBits,
            2 => Self::SymTab,
            3 => Self::StrTab,
            4 => Self::Rela,
            5 => Self::Hash,
            6 => Self::Dynamic,
            7 => Self::Note,
            8 => Self::NoBits,
            9 => Self::Rel,
            10 => Self::ShLib,
            11 => Self::DynSym,
            14 => Self::InitArray,
            15 => Self::FiniArray,
            16 => Self::PreInitArray,
            17 => Self::Group,
            18 => Self::SymTabShNdx,
            19 => Self::Num,
            _ => Self::Any(bytes),
        }
    }
}