<commit_before>//
//  SOS: the Stupid Operating System
//  by <NAME> (<EMAIL>)
//
//  Copyright (c) 2015-2016 <NAME>
//  Released under the terms of the MIT license. See `LICENSE` in the root
//  directory of this repository for more information.
//
use memory::PAddr;
use core::fmt;
use super::ElfResult;

// Distinguished section indices.
pub const SHN_UNDEF: u16        = 0;
pub const SHN_LORESERVE: u16    = 0xff00;
pub const SHN_LOPROC: u16       = 0xff00;
pub const SHN_HIPROC: u16       = 0xff1f;
pub const SHN_LOOS: u16         = 0xff20;
pub const SHN_HIOS: u16         = 0xff3f;
pub const SHN_ABS: u16          = 0xfff1;
pub const SHN_COMMON: u16       = 0xfff2;
pub const SHN_XINDEX: u16       = 0xffff;
pub const SHN_HIRESERVE: u16    = 0xffff;

pub const SHT_LOOS: u32   = 0x60000000;
pub const SHT_HIOS: u32   = 0x6fffffff;
pub const SHT_LOPROC: u32 = 0x70000000;
pub const SHT_HIPROC: u32 = 0x7fffffff;
pub const SHT_LOUSER: u32 = 0x80000000;
pub const SHT_HIUSER: u32 = 0xffffffff;

/// Represents an ELF section header
///
/// Refer to [Figure 4-8], "Section Header", from Chapter 4 of the ELF standard
/// for more information.
///
/// [Figure 4-8]: (http://www.sco.com/developers/gabi/latest/ch4.sheader.html#section_header)
#[derive(Clone, Copy, Debug)]
pub enum Header<'a> {
    ThirtyTwo(&'a HeaderRepr<u32>)
  , SixtyFour(&'a HeaderRepr<u64>)
}

/// Raw representation of an ELF section header in an ELF binary.
///
/// Refer to [Figure 4-8], "Section Header", from Chapter 4 of the ELF standard
/// for more information.
///
/// [Figure 4-8]: (http://www.sco.com/developers/gabi/latest/ch4.sheader.html#section_header)
//  TODO: add docs for all fields!
//          - eliza, 03/05/2017
#[derive(Debug)]
#[repr(C)]
pub struct HeaderRepr<Word> {
    /// This member specifies the name of the section.
    ///
    /// Its value is an index into the section header string table section,
    /// giving the location of a null-terminated string.
    //  TODO: this should be a Word, not a 32-bit word.
    //          - eliza, 03/05/2017
    name_offset: u32
  , /// This member categorizes the section's contents and semantics.
    ty: TypeRepr
  , flags: Flags
  , address: Word
  , offset: Word
  , length: Word
  , link: u32
  , info: u32
  , address_align: Word
  , entry_length: Word
}

pub trait AsHeader {
    fn as_header(&self) -> Header;
}

impl AsHeader for HeaderRepr<u32> {
    #[inline] fn as_header(&self) -> Header {
        Header::ThirtyTwo(self)
    }
}

impl AsHeader for HeaderRepr<u64> {
    #[inline] fn as_header(&self) -> Header {
        Header::SixtyFour(self)
    }
}


bitflags! {
    pub flags Flags: usize {
        // Flags (SectionHeader::flags)
        const SHF_WRITE            =        0x1
      , const SHF_ALLOC            =        0x2
      , const SHF_EXECINSTR        =        0x4<commit_msg>feat(elf): add fmt::Display implementation for ELF sections<commit_after>//
//  SOS: the Stupid Operating System
//  by <NAME> (<EMAIL>)
//
//  Copyright (c) 2015-2016 <NAME>
//  Released under the terms of the MIT license. See `LICENSE` in the root
//  directory of this repository for more information.
//
use memory::PAddr;
use core::fmt;
use super::ElfResult;

// Distinguished section indices.
pub const SHN_UNDEF: u16        = 0;
pub const SHN_LORESERVE: u16    = 0xff00;
pub const SHN_LOPROC: u16       = 0xff00;
pub const SHN_HIPROC: u16       = 0xff1f;
pub const SHN_LOOS: u16         = 0xff20;
pub const SHN_HIOS: u16         = 0xff3f;
pub const SHN_ABS: u16          = 0xfff1;
pub const SHN_COMMON: u16       = 0xfff2;
pub const SHN_XINDEX: u16       = 0xffff;
pub const SHN_HIRESERVE: u16    = 0xffff;

pub const SHT_LOOS: u32   = 0x60000000;
pub const SHT_HIOS: u32   = 0x6fffffff;
pub const SHT_LOPROC: u32 = 0x70000000;
pub const SHT_HIPROC: u32 = 0x7fffffff;
pub const SHT_LOUSER: u32 = 0x80000000;
pub const SHT_HIUSER: u32 = 0xffffffff;

/// Represents an ELF section header
///
/// Refer to [Figure 4-8], "Section Header", from Chapter 4 of the ELF standard
/// for more information.
///
/// [Figure 4-8]: (http://www.sco.com/developers/gabi/latest/ch4.sheader.html#section_header)
#[derive(Clone, Copy, Debug)]
pub enum Header<'a> {
    ThirtyTwo(&'a HeaderRepr<u32>)
  , SixtyFour(&'a HeaderRepr<u64>)
}

impl<'a> fmt::Display for Header<'a> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // TODO: do we want to actually get the section's name from the string
        //       table and display it here?
        //          - eliza, 03/05/2017
        // TODO: do we want to print the header's flags, or would that make the //       format too long?
        //          - eliza, 03/05/2017
        write!(f, "ELF {:?} section at {:#08x} to {:#08x}"
              , self.get_type(), self.addr(), self.end_addr())
    }
}

/// Raw representation of an ELF section header in an ELF binary.
///
/// Refer to [Figure 4-8], "Section Header", from Chapter 4 of the ELF standard
/// for more information.
///
/// [Figure 4-8]: (http://www.sco.com/developers/gabi/latest/ch4.sheader.html#section_header)
//  TODO: add docs for all fields!
//          - eliza, 03/05/2017
#[derive(Debug)]
#[repr(C)]
pub struct HeaderRepr<Word> {
    /// This member specifies the name of the section.
    ///
    /// Its value is an index into the section header string table section,
    /// giving the location of a null-terminated string.
    //  TODO: this should be a Word, not a 32-bit word.
    //          - eliza, 03/05/2017
    name_offset: u32
  , /// This member categorizes the section's contents and semantics.
    ty: TypeRepr
  , flags: Flags
  , address: Word
  , offset: Word
  , length: Word
  , link: u32
  , info: u32
  , address_align: Word
  , entry_length: Word
}

// TODO: does this need to be a trait? could we make this faster by simplifying
//       dispatch here?
//         - eliza, 03/05/2017
pub trait AsHeader {
    fn as_header(&self) -> Header;
}

impl AsHeader for HeaderRepr<u32> {
    #[inline] fn as_header(&self) -> Header {
        Header::ThirtyTwo(self)
    }
}

impl AsHeader for HeaderRepr<u64> {
    #[inline] fn as_header(&self) -> Header {
        Header::SixtyFour(self)
    }
}


bitflags! {
    // TODO: add documentation to the flags
    //          - eliza, 03/05/2017
    pub flags Flags: usize {
        // Flags (SectionHeader::flags)
        const SHF_WRITE            =        0x1
      , const SHF_ALLOC            =        0x2
      , const SHF_EXECINSTR        =        0x4
{'fraction_non_alphanumeric': 0.12260242442841798, 'fraction_numerical': 0.049102347705999694, 'mean_word_length': 2.5005370569280343, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6392812', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2279, 'n_words': 704}