use crate::elf::constants::{
SHF_ALLOC, SHF_EXECINSTR, SHF_INFO_LINK, SHF_TLS, SHF_WRITE, SHT_DYNAMIC,
SHT_DYNSYM, SHT_FINI_ARRAY, SHT_GNU_HASH, SHT_GNU_VERNEED, SHT_GNU_VERSYM,
SHT_HASH, SHT_INIT_ARRAY, SHT_NOBITS, SHT_NOTE, SHT_PREINIT_ARRAY,
SHT_PROGBITS, SHT_RELA, SHT_STRTAB,
};
const SYM_SIZE: u64 = 24;
const RELA_SIZE: u64 = 24;
const DYN_SIZE: u64 = 16;
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Sect {
Interp,
Note,
BuildId,
Init,
Text,
Fini,
Plt,
Rodata,
EhFrame,
EhFrameHdr,
GnuHash,
Hash,
Dynsym,
Dynstr,
Versym,
Verneed,
RelaDyn,
RelaPlt,
Dynamic,
Got,
DataRelRo,
PreinitArray,
InitArray,
FiniArray,
GotPlt,
Data,
Tdata,
Tbss,
Bss,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Link {
None,
Dynsym,
Dynstr,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Info {
Zero,
FirstGlobal,
VerneedCount,
GotPlt,
}
pub const MEASURED: u64 = 0;
#[derive(Copy, Clone)]
pub struct Spec {
pub name: &'static [u8],
pub sh_type: u32,
pub flags: u64,
pub align: u64,
pub entsize: u64,
pub link: Link,
pub info: Info,
}
const fn plain(
name: &'static [u8],
sh_type: u32,
flags: u64,
align: u64,
) -> Spec {
Spec {
name,
sh_type,
flags,
align,
entsize: 0,
link: Link::None,
info: Info::Zero,
}
}
pub const TABLE: [(Sect, Spec); 29] = [
(Sect::Interp, plain(b".interp", SHT_PROGBITS, SHF_ALLOC, 1)),
(
Sect::BuildId,
plain(b".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, 4),
),
(Sect::Note, plain(b".note", SHT_NOTE, SHF_ALLOC, MEASURED)),
(
Sect::Init,
plain(b".init", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, MEASURED),
),
(
Sect::Text,
plain(b".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, MEASURED),
),
(
Sect::Fini,
plain(b".fini", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, MEASURED),
),
(
Sect::Plt,
plain(b".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 16),
),
(
Sect::Rodata,
plain(b".rodata", SHT_PROGBITS, SHF_ALLOC, MEASURED),
),
(
Sect::EhFrame,
plain(b".eh_frame", SHT_PROGBITS, SHF_ALLOC, MEASURED),
),
(
Sect::EhFrameHdr,
plain(b".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, 4),
),
(
Sect::GnuHash,
Spec {
name: b".gnu.hash",
sh_type: SHT_GNU_HASH,
flags: SHF_ALLOC,
align: 8,
entsize: 0,
link: Link::Dynsym,
info: Info::Zero,
},
),
(
Sect::Hash,
Spec {
name: b".hash",
sh_type: SHT_HASH,
flags: SHF_ALLOC,
align: 4,
entsize: 4,
link: Link::Dynsym,
info: Info::Zero,
},
),
(
Sect::Dynsym,
Spec {
name: b".dynsym",
sh_type: SHT_DYNSYM,
flags: SHF_ALLOC,
align: 8,
entsize: SYM_SIZE,
link: Link::Dynstr,
info: Info::FirstGlobal,
},
),
(Sect::Dynstr, plain(b".dynstr", SHT_STRTAB, SHF_ALLOC, 1)),
(
Sect::Versym,
Spec {
name: b".gnu.version",
sh_type: SHT_GNU_VERSYM,
flags: SHF_ALLOC,
align: 2,
entsize: 2,
link: Link::Dynsym,
info: Info::Zero,
},
),
(
Sect::Verneed,
Spec {
name: b".gnu.version_r",
sh_type: SHT_GNU_VERNEED,
flags: SHF_ALLOC,
align: 4,
entsize: 0,
link: Link::Dynstr,
info: Info::VerneedCount,
},
),
(
Sect::RelaDyn,
Spec {
name: b".rela.dyn",
sh_type: SHT_RELA,
flags: SHF_ALLOC,
align: 8,
entsize: RELA_SIZE,
link: Link::Dynsym,
info: Info::Zero,
},
),
(
Sect::RelaPlt,
Spec {
name: b".rela.plt",
sh_type: SHT_RELA,
flags: SHF_ALLOC | SHF_INFO_LINK,
align: 8,
entsize: RELA_SIZE,
link: Link::Dynsym,
info: Info::GotPlt,
},
),
(
Sect::Dynamic,
Spec {
name: b".dynamic",
sh_type: SHT_DYNAMIC,
flags: SHF_ALLOC | SHF_WRITE,
align: 8,
entsize: DYN_SIZE,
link: Link::Dynstr,
info: Info::Zero,
},
),
(
Sect::Got,
plain(b".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 8),
),
(
Sect::DataRelRo,
plain(
b".data.rel.ro",
SHT_PROGBITS,
SHF_ALLOC | SHF_WRITE,
MEASURED,
),
),
(
Sect::PreinitArray,
plain(
b".preinit_array",
SHT_PREINIT_ARRAY,
SHF_ALLOC | SHF_WRITE,
MEASURED,
),
),
(
Sect::InitArray,
plain(
b".init_array",
SHT_INIT_ARRAY,
SHF_ALLOC | SHF_WRITE,
MEASURED,
),
),
(
Sect::FiniArray,
plain(
b".fini_array",
SHT_FINI_ARRAY,
SHF_ALLOC | SHF_WRITE,
MEASURED,
),
),
(
Sect::GotPlt,
plain(b".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 8),
),
(
Sect::Data,
plain(b".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, MEASURED),
),
(
Sect::Tdata,
plain(
b".tdata",
SHT_PROGBITS,
SHF_ALLOC | SHF_WRITE | SHF_TLS,
MEASURED,
),
),
(
Sect::Tbss,
plain(
b".tbss",
SHT_NOBITS,
SHF_ALLOC | SHF_WRITE | SHF_TLS,
MEASURED,
),
),
(
Sect::Bss,
plain(b".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE, MEASURED),
),
];
pub const CONTENT: [Sect; 15] = [
Sect::Note,
Sect::Init,
Sect::Text,
Sect::Fini,
Sect::Rodata,
Sect::EhFrame,
Sect::Got,
Sect::DataRelRo,
Sect::PreinitArray,
Sect::InitArray,
Sect::FiniArray,
Sect::Data,
Sect::Tdata,
Sect::Tbss,
Sect::Bss,
];
pub const fn file_backed(sect: Sect) -> bool {
!matches!(sect, Sect::Bss | Sect::Tbss)
}
pub const fn writable(spec: &Spec) -> bool {
spec.flags & SHF_WRITE != 0
}
pub const fn relro(sect: Sect) -> bool {
matches!(
sect,
Sect::Dynamic
| Sect::Got
| Sect::DataRelRo
| Sect::PreinitArray
| Sect::InitArray
| Sect::FiniArray
)
}
pub const fn executable(spec: &Spec) -> bool {
spec.flags & SHF_EXECINSTR != 0
}
pub fn code() -> impl Iterator<Item = Sect> {
CONTENT
.into_iter()
.filter(|s| TABLE.iter().any(|(t, spec)| t == s && executable(spec)))
}