From 6d034adc5a2f14a9dda0d52a338306d6a4669fb0 Mon Sep 17 00:00:00 2001 From: Andy-Python-Programmer Date: Fri, 10 Sep 2021 18:02:54 +1000 Subject: [PATCH] multiboot2: implement ELF sections tag Signed-off-by: Andy-Python-Programmer --- stage23/lib/elf.c | 22 ++++++++++++++++------ stage23/lib/elf.h | 5 +++-- stage23/protos/multiboot2.c | 16 +++++++++++++--- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/stage23/lib/elf.c b/stage23/lib/elf.c index 706ec73f..60463e7d 100644 --- a/stage23/lib/elf.c +++ b/stage23/lib/elf.c @@ -249,30 +249,40 @@ int elf64_load_section(uint8_t *elf, void *buffer, const char *name, size_t limi /// SAFETY: The caller must ensure that the provided `elf` is a valid 64-bit /// ELF file. -void elf64_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info* info) { - struct elf64_hdr hdr; - memcpy(&hdr, elf + (0), sizeof(struct elf32_hdr)); +void elf64_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info** _info) { + struct elf_section_hdr_info* info = ext_mem_alloc(sizeof(struct elf_section_hdr_info)); - info->section_hdr_size = hdr.sh_num * hdr.shdr_size; + struct elf64_hdr hdr; + memcpy(&hdr, elf + (0), sizeof(struct elf64_hdr)); + + info->num = hdr.sh_num; info->section_entry_size = hdr.shdr_size; + info->section_hdr_size = info->num * info->section_entry_size; info->str_section_idx = hdr.shstrndx; info->section_hdrs = ext_mem_alloc(info->section_hdr_size); memcpy(info->section_hdrs, elf + (hdr.shoff), info->section_hdr_size); + + *_info = info; } /// SAFETY: The caller must ensure that the provided `elf` is a valid 64-bit /// ELF file. -void elf32_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info* info) { +void elf32_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info** _info) { + struct elf_section_hdr_info* info = ext_mem_alloc(sizeof(struct elf_section_hdr_info)); + struct elf32_hdr hdr; memcpy(&hdr, elf + (0), sizeof(struct elf32_hdr)); - info->section_hdr_size = hdr.sh_num * hdr.shdr_size; + info->num = hdr.sh_num; info->section_entry_size = hdr.shdr_size; + info->section_hdr_size = info->num * info->section_entry_size; info->str_section_idx = hdr.shstrndx; info->section_hdrs = ext_mem_alloc(info->section_hdr_size); memcpy(info->section_hdrs, elf + (hdr.shoff), info->section_hdr_size); + + *_info = info; } int elf32_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit) { diff --git a/stage23/lib/elf.h b/stage23/lib/elf.h index 31f01ba5..d57be297 100644 --- a/stage23/lib/elf.h +++ b/stage23/lib/elf.h @@ -21,6 +21,7 @@ struct elf_section_hdr_info { uint32_t section_hdr_size; uint32_t section_entry_size; uint32_t str_section_idx; + uint32_t num; void* section_hdrs; }; @@ -28,10 +29,10 @@ int elf_bits(uint8_t *elf); int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr, struct elf_range **ranges, uint64_t *ranges_count); int elf64_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit, uint64_t slide); -void elf64_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info* info); +void elf64_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info** info); int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t alloc_type); int elf32_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit); -void elf32_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info* info); +void elf32_section_hdr_info(uint8_t *elf, struct elf_section_hdr_info** info); #endif diff --git a/stage23/protos/multiboot2.c b/stage23/protos/multiboot2.c index 35bde0a5..e070ee37 100644 --- a/stage23/protos/multiboot2.c +++ b/stage23/protos/multiboot2.c @@ -98,7 +98,7 @@ void multiboot2_load(char *config, char* cmdline) { uint32_t kernel_top; int bits = elf_bits(kernel); - struct elf_section_hdr_info section_hdr_info; + struct elf_section_hdr_info* section_hdr_info; switch (bits) { case 32: @@ -122,7 +122,7 @@ void multiboot2_load(char *config, char* cmdline) { panic("multiboot1: invalid ELF file bitness"); } - print("multiboot2: found kernel entry point at: %X\n", entry_point); + print("multiboot2: found kernel entry point at: %x\n", entry_point); // Iterate through the entries... for (struct multiboot_header_tag* tag = (struct multiboot_header_tag*)(header + 1); @@ -174,7 +174,17 @@ void multiboot2_load(char *config, char* cmdline) { // Create ELF info tag ////////////////////////////////////////////// { - // ADD ME + uint32_t size = sizeof(struct multiboot_tag_elf_sections) + section_hdr_info->section_hdr_size; + struct multiboot_tag_elf_sections* tag = (struct multiboot_tag_elf_sections*)push_boot_param(NULL, size); + + tag->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS; + tag->size = size; + + tag->num = section_hdr_info->num; + tag->entsize = section_hdr_info->section_entry_size; + tag->shndx = section_hdr_info->str_section_idx; + + memcpy(tag->sections, section_hdr_info->section_hdrs, section_hdr_info->section_hdr_size); } #if uefi == 1