summaryrefslogtreecommitdiff
path: root/kernel/common/elf.c
diff options
context:
space:
mode:
authorNathan Lee <me@nwlee.tech>2026-08-12 17:44:17 -0500
committerNathan Lee <me@nwlee.tech>2026-08-12 17:44:17 -0500
commit2c0efd93530c472e96ff3529aee983b53f3b78d9 (patch)
treecc36e7334e162ba67df79333e267ac93b4d984b9 /kernel/common/elf.c
parent640a81b3ee366c651286efd1b80b37992cd53282 (diff)
rework project structure
Diffstat (limited to 'kernel/common/elf.c')
-rw-r--r--kernel/common/elf.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/kernel/common/elf.c b/kernel/common/elf.c
new file mode 100644
index 0000000..871eee7
--- /dev/null
+++ b/kernel/common/elf.c
@@ -0,0 +1,40 @@
+#include <sys/elf.h>
+
+int elf_chk_header(struct elf_header *header) {
+ uint32_t magic_chk;
+ memcpy(&magic_chk, header->e_ident, sizeof(uint32_t));
+ if (magic_chk != ltohi(ELF_MAGIC_LE)) {
+ return -1;
+ }
+
+ if (header->e_ident[EI_CLASS] != ELFHOSTCLASS) {
+ return -1;
+ }
+
+ if (header->e_ident[EI_DATA] != ELFHOSTDATA) {
+ return -1;
+ }
+
+ if (header->e_ident[EI_VERSION] != EV_CURRENT) {
+ return -1;
+ }
+
+ return 0;
+}
+
+struct elf_section_header *elf_get_section(struct elf_header *hdr, void *data, uint32_t index) {
+ uint8_t *base = (uint8_t *)data;
+
+ if (index >= hdr->e_shnum) {
+ return 0;
+ }
+
+ return (struct elf_section_header *)(base
+ + hdr->e_shoff
+ + index * hdr->e_shentsize);
+}
+
+char *elf_get_section_name(struct elf_header *hdr, struct elf_section_header *section, void *data) {
+ struct elf_section_header *shstrtab = elf_get_section(hdr, data, hdr->e_shstrndx);
+ return (char *)((uintptr_t)section->sh_name + (uintptr_t)shstrtab->sh_offset + (uintptr_t)data);
+}