pelf 0.1.5

A library for parsing/generating/analyzing ELF
Documentation
use crate::Elf64Word;
/// section header table entry is unused
pub const ELF_SECTION_HEADER_TYPE_NULL: Elf64Word = 0;
/// holds program data.
pub const ELF_SECTION_HEADER_TYPE_PROGBITS: Elf64Word = 1;
/// a symbol table.
pub const ELF_SECTION_HEADER_TYPE_SYMTAB: Elf64Word = 2;
/// a string table.
pub const ELF_SECTION_HEADER_TYPE_STRTAB: Elf64Word = 3;
/// relocation entries with addends.
pub const ELF_SECTION_HEADER_TYPE_RELA: Elf64Word = 4;
/// a symbol hash table.
pub const ELF_SECTION_HEADER_TYPE_HASH: Elf64Word = 5;
/// dynamic linking information.
pub const ELF_SECTION_HEADER_TYPE_DYNAMIC: Elf64Word = 6;
/// notes.
pub const ELF_SECTION_HEADER_TYPE_NOTE: Elf64Word = 7;
/// program space with no data(bss).
pub const ELF_SECTION_HEADER_TYPE_NOBITS: Elf64Word = 8;
/// relocation entries with no addends.
pub const ELF_SECTION_HEADER_TYPE_REL: Elf64Word = 9;
/// reserved.
pub const ELF_SECTION_HEADER_TYPE_SHLIB: Elf64Word = 10;
/// dynamic linker symbol table.
pub const ELF_SECTION_HEADER_TYPE_DYNSYM: Elf64Word = 11;
/// array of constructors.
pub const ELF_SECTION_HEADER_TYPE_INIT_ARRAY: Elf64Word = 14;
/// array of destructors.
pub const ELF_SECTION_HEADER_TYPE_FINI_ARRAY: Elf64Word = 15;
/// array of pre-constructors.
pub const ELF_SECTION_HEADER_TYPE_PREINIT_ARRAY: Elf64Word = 16;
/// section group
pub const ELF_SECTION_HEADER_TYPE_GROUP: Elf64Word = 17;
/// extended section indices
pub const ELF_SECTION_HEADER_TYPE_SYMTAB_SHNDX: Elf64Word = 18;
/// number of predefined types
pub const ELF_SECTION_HEADER_TYPE_NUM: Elf64Word = 19;
/// GNU-style hash table.
pub const ELF_SECTION_HEADER_TYPE_GNU_HASH: Elf64Word = 0x6ffffff6;
/// VERNEED.
pub const ELF_SECTION_HEADER_TYPE_VERNEED: Elf64Word = 0x6ffffffe;
/// VERSYM.
pub const ELF_SECTION_HEADER_TYPE_VERSYM: Elf64Word = 0x6fffffff;

/// the elf section header type.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum SectionHeaderType {
    /// section header table entry is unused
    Null,
    /// holds program data.
    ProgBits,
    /// a symbol table.
    SymTab,
    /// a string table.
    StrTab,
    /// relocation entries with addends.
    Rela,
    /// a symbol hash table.
    Hash,
    /// dynamic linking information.
    Dynamic,
    /// notes.
    Note,
    /// program space with no data(bss).
    NoBits,
    /// relocation entries with no addends.
    Rel,
    /// reserved.
    ShLib,
    /// dynamic linker symbol table.
    DynSym,
    /// array of constructors.
    InitArray,
    /// array of destructors.
    FiniArray,
    /// array of pre-constructors.
    PreinitArray,
    /// section group
    Group,
    /// extended section indices
    SymTabShndx,
    /// number of predefined types
    Num,
    /// GNU-style hash table.
    GnuHash,
    /// VERSYM.
    VerSym,
    /// VERNEED.
    VerNeed,
    /// custom value.
    Any(Elf64Word),
}

impl Default for SectionHeaderType {
    fn default() -> Self {
        Self::Null
    }
}

impl std::fmt::Display for SectionHeaderType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            SectionHeaderType::Null => "Null".to_string(),
            SectionHeaderType::ProgBits => "ProgBits".to_string(),
            SectionHeaderType::SymTab => "SymTab".to_string(),
            SectionHeaderType::StrTab => "StrTab".to_string(),
            SectionHeaderType::Rela => "Rela".to_string(),
            SectionHeaderType::Hash => "Hash".to_string(),
            SectionHeaderType::Dynamic => "Dynamic".to_string(),
            SectionHeaderType::Note => "Note".to_string(),
            SectionHeaderType::NoBits => "NoBits".to_string(),
            SectionHeaderType::Rel => "Rel".to_string(),
            SectionHeaderType::ShLib => "ShLib".to_string(),
            SectionHeaderType::DynSym => "DynSym".to_string(),
            SectionHeaderType::InitArray => "InitArray".to_string(),
            SectionHeaderType::FiniArray => "FiniArray".to_string(),
            SectionHeaderType::PreinitArray => "PreinitArray".to_string(),
            SectionHeaderType::Group => "Group".to_string(),
            SectionHeaderType::SymTabShndx => "SymTabShndx".to_string(),
            SectionHeaderType::GnuHash => "GNU Hash".to_string(),
            SectionHeaderType::VerSym => "VerSym".to_string(),
            SectionHeaderType::VerNeed => "VerNeed".to_string(),
            SectionHeaderType::Num => format!("Unknown!({})", ELF_SECTION_HEADER_TYPE_NUM),
            SectionHeaderType::Any(v) => format!("Unknown!({})", v),
        };
        write!(f, "{}", s)
    }
}

impl From<Elf64Word> for SectionHeaderType {
    fn from(value: Elf64Word) -> Self {
        match value {
            ELF_SECTION_HEADER_TYPE_NULL => Self::Null,
            ELF_SECTION_HEADER_TYPE_PROGBITS => Self::ProgBits,
            ELF_SECTION_HEADER_TYPE_SYMTAB => Self::SymTab,
            ELF_SECTION_HEADER_TYPE_STRTAB => Self::StrTab,
            ELF_SECTION_HEADER_TYPE_RELA => Self::Rela,
            ELF_SECTION_HEADER_TYPE_HASH => Self::Hash,
            ELF_SECTION_HEADER_TYPE_DYNAMIC => Self::Dynamic,
            ELF_SECTION_HEADER_TYPE_NOTE => Self::Note,
            ELF_SECTION_HEADER_TYPE_NOBITS => Self::NoBits,
            ELF_SECTION_HEADER_TYPE_REL => Self::Rel,
            ELF_SECTION_HEADER_TYPE_SHLIB => Self::ShLib,
            ELF_SECTION_HEADER_TYPE_DYNSYM => Self::DynSym,
            ELF_SECTION_HEADER_TYPE_INIT_ARRAY => Self::InitArray,
            ELF_SECTION_HEADER_TYPE_FINI_ARRAY => Self::FiniArray,
            ELF_SECTION_HEADER_TYPE_PREINIT_ARRAY => Self::PreinitArray,
            ELF_SECTION_HEADER_TYPE_GROUP => Self::Group,
            ELF_SECTION_HEADER_TYPE_SYMTAB_SHNDX => Self::SymTabShndx,
            ELF_SECTION_HEADER_TYPE_GNU_HASH => Self::GnuHash,
            ELF_SECTION_HEADER_TYPE_VERSYM => Self::VerSym,
            ELF_SECTION_HEADER_TYPE_VERNEED => Self::VerNeed,
            ELF_SECTION_HEADER_TYPE_NUM => Self::Num,
            _ => Self::Any(value),
        }
    }
}