summaryrefslogtreecommitdiff
path: root/lib/elf64.c
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2021-12-19 01:16:51 +0000
committerAda Christine <adachristine18@gmail.com>2021-12-19 01:16:51 +0000
commitbff63a4337eb3388617d230970c5eb2684d6a215 (patch)
treef40891f45918b23dac0f31888142136434c7fc8c /lib/elf64.c
parentd5f485a4f6801f58cbb2bc9d8e80c955c79ff044 (diff)
i don't know what i'm doing anymore
Diffstat (limited to 'lib/elf64.c')
-rw-r--r--lib/elf64.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/elf64.c b/lib/elf64.c
new file mode 100644
index 0000000..2dee395
--- /dev/null
+++ b/lib/elf64.c
@@ -0,0 +1,45 @@
+#include "elf.h"
+
+bool elf64_validate(Elf64_Ehdr *ehdr, unsigned type, unsigned machine)
+{
+ if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
+ ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
+ ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
+ ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
+ ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
+ ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
+ ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
+ ehdr->e_machine != machine ||
+ ehdr->e_type != type)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+size_t elf64_size(Elf64_Ehdr *ehdr, Elf64_Phdr *phdrs)
+{
+ size_t size = 0;
+
+ for (int i = 0; i < ehdr->e_phnum; i++)
+ {
+ if (phdrs[i].p_type != PT_LOAD)
+ {
+ continue;
+ }
+
+ if (phdrs[i].p_offset + phdrs[i].p_memsz > size)
+ {
+ size = phdrs[i].p_offset + phdrs[i].p_memsz;
+ if (phdrs[i].p_align > 1)
+ {
+ size = (size + phdrs[i].p_align - 1) &
+ ~(phdrs[i].p_align - 1);
+ }
+ }
+ }
+
+ return size;
+}
+