summary refs log tree commit diff
path: root/src/structs.rs
diff options
context:
space:
mode:
authortzlil <tzlils@protonmail.com>2023-03-22 01:08:16 +0200
committertzlil <tzlils@protonmail.com>2023-03-22 01:08:16 +0200
commit2246c9e8a73fee10dc8ffcfe178538ff678df8bc (patch)
tree0b39a8bef7b584b2559dc916d63de7e0e2baae5f /src/structs.rs
parentf9c58b9d960124a0e84a399333c19f1fa8e4b838 (diff)
add program headers
Diffstat (limited to 'src/structs.rs')
-rw-r--r--src/structs.rs133
1 files changed, 131 insertions, 2 deletions
diff --git a/src/structs.rs b/src/structs.rs
index f5c8140..673ae35 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -323,7 +323,7 @@ pub struct ElfSectionHeader {
 impl std::fmt::Display for ElfSectionHeader {
     fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
         formatter.write_fmt(format_args!(
-            "{: <16} {:0>16x} {:0>8x}\n       {:0>16x} {:0>16x} {: ^10} {: <5} {: <5} {}",
+            "{: <16} {:0>16x} {:0>8x}\n       {:0>16x} {:0>16x}   {: <10} {: <5} {: <5} {}",
             self.r#type,
             self.addr,
             self.offset,
@@ -451,7 +451,31 @@ bitflags! {
 // https://github.com/llvm-mirror/llvm/blob/2c4ca6832fa6b306ee6a7010bfb80a3f2596f824/tools/llvm-readobj/ELFDumper.cpp#L1176
 impl std::fmt::Display for ElfSectionFlags {
     fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
-        "  A".fmt(formatter);
+        format!(
+            "{}{}{}",
+            {
+                if self.contains(ElfSectionFlags::WRITE) {
+                    "W"
+                } else {
+                    ""
+                }
+            },
+            {
+                if self.contains(ElfSectionFlags::ALLOC) {
+                    "A"
+                } else {
+                    ""
+                }
+            },
+            {
+                if self.contains(ElfSectionFlags::EXEC) {
+                    "X"
+                } else {
+                    ""
+                }
+            }
+        )
+        .fmt(formatter);
         Ok(())
     }
 }
@@ -502,3 +526,108 @@ pub struct ElfSymbol {
     /// size.
     pub size: u64,
 }
+
+#[derive(Debug)]
+#[repr(C)]
+pub struct ElfProgramHeader {
+    pub r#type: ElfProgramType,
+    pub flags: ElfProgramFlags,
+    pub offset: u64,
+    pub vaddr: u64,
+    pub paddr: u64,
+    pub filesz: u64,
+    pub memsz: u64,
+    pub align: u64,
+}
+
+impl std::fmt::Display for ElfProgramHeader {
+    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+        formatter.write_fmt(format_args!(
+            "{: <14} {:#016x} {:#016x} {:#016x}\n                 {:#016x} {:#016x}   {: <10} {:#08x}",
+            self.r#type,
+			self.offset,
+            self.vaddr,
+            self.paddr,
+            self.filesz,
+            self.memsz,
+            self.flags,
+            self.align,
+        ))?;
+        Ok(())
+    }
+}
+
+#[derive(PartialEq,Debug)]
+#[repr(u32)]
+pub enum ElfProgramType {
+    /// This value marks the section header as inactive.
+    /// It does not have an associated section.  Other
+    /// members of the section header have undefined
+    /// values.
+    PHDR = 6,
+    Interp = 3,
+    Load = 1,
+    Dynamic = 2,
+    Note = 4,
+
+    GnuProperty = 0x6474E553,
+    GnuEhFrame = 0x6474E550,
+    GnuStack = 0x6474E551,
+    GnuRelro = 0x6474E552,
+}
+
+impl std::fmt::Display for ElfProgramType {
+    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+        match self {
+            ElfProgramType::PHDR => "PHDR".fmt(formatter),
+            ElfProgramType::Interp => "INTERP".fmt(formatter),
+            ElfProgramType::Note => "NOTE".fmt(formatter),
+            ElfProgramType::Load => "LOAD".fmt(formatter),
+            ElfProgramType::Dynamic => "DYNAMIC".fmt(formatter),
+            ElfProgramType::GnuProperty => "GNU_PROPERTY".fmt(formatter),
+            ElfProgramType::GnuEhFrame => "GNU_EH_FRAME".fmt(formatter),
+            ElfProgramType::GnuStack => "GNU_STACK".fmt(formatter),
+            ElfProgramType::GnuRelro => "GNU_RELRO".fmt(formatter),
+        }
+    }
+}
+
+bitflags! {
+    #[derive(Debug)]
+    pub struct ElfProgramFlags: u32 {
+        const EXEC = 0x1;
+        const WRITE = 0x2;
+        const READ = 0x4;
+    }
+}
+
+impl std::fmt::Display for ElfProgramFlags {
+    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+        format!(
+            "{}{}{}",
+            {
+                if self.contains(ElfProgramFlags::READ) {
+                    "R"
+                } else {
+                    " "
+                }
+            },
+            {
+                if self.contains(ElfProgramFlags::WRITE) {
+                    "W"
+                } else {
+                    " "
+                }
+            },
+            {
+                if self.contains(ElfProgramFlags::EXEC) {
+                    "E"
+                } else {
+                    " "
+                }
+            }
+        )
+        .fmt(formatter);
+        Ok(())
+    }
+}
\ No newline at end of file