diff options
Diffstat (limited to 'src/kernel/core')
-rw-r--r-- | src/kernel/core/kmain.c | 29 | ||||
-rw-r--r-- | src/kernel/core/sys.c | 34 |
2 files changed, 31 insertions, 32 deletions
diff --git a/src/kernel/core/kmain.c b/src/kernel/core/kmain.c index d09a1b5..dd66805 100644 --- a/src/kernel/core/kmain.c +++ b/src/kernel/core/kmain.c @@ -69,6 +69,9 @@ void kmain(multiboot_info_t *mbd, int32_t mb_magic) { // to allocate memory ; they just increment it of the allocated quantity void* kernel_data_end = (void*)&k_end_addr; + elf_shdr_t *elf_sections = (elf_shdr_t*)(mbd->elf_sec.addr + K_HIGHHALF_ADDR); + ASSERT(sizeof(elf_shdr_t) == mbd->elf_sec.size); + dbglog_setup(); dbg_printf("Hello, kernel world!\n"); @@ -95,6 +98,14 @@ void kmain(multiboot_info_t *mbd, int32_t mb_magic) { kernel_data_end = mod_end_pa; } + for (unsigned i = 0; i < mbd->elf_sec.num; i++) { + elf_sections[i].sh_addr += K_HIGHHALF_ADDR; + size_t section_end = elf_sections[i].sh_addr + elf_sections[i].sh_size; + void* section_end_pa = (void*)((section_end & 0xFFFFF000) + 0x1000); + if (section_end_pa > kernel_data_end) + kernel_data_end = section_end_pa; + } + gdt_init(); dbg_printf("GDT set up.\n"); idt_init(); dbg_printf("IDT set up.\n"); @@ -125,17 +136,13 @@ void kmain(multiboot_info_t *mbd, int32_t mb_magic) { setup_syscall_table(); dbg_printf("System calls setup ok.\n"); - // If we have a kernel map, load it now - for (unsigned i = 0; i < mbd->mods_count; i++) { - char* modname = (char*)mods[i].string; - size_t len = mods[i].mod_end - mods[i].mod_start; - - if (strlen(modname) > 4 && strcmp(modname + strlen(modname) - 4, ".map") == 0) { - // Copy data to somewhere safe, as it will be modified - void* dup_data = malloc(len + 1); - memcpy(dup_data, (void*)mods[i].mod_start, len); - - load_kernel_symbol_map((char*)dup_data, len); + // Look for kernel symbol table section + for (unsigned i = 0; i < mbd->elf_sec.num; i++) { + dbg_printf("0x%p section type %d addr 0x%p - 0x%p\n", + &elf_sections[i], elf_sections[i].sh_type, + elf_sections[i].sh_addr, elf_sections[i].sh_addr + elf_sections[i].sh_size); + if (elf_sections[i].sh_type == SHT_SYMTAB) { + load_kernel_symbol_table(&elf_sections[i], &elf_sections[elf_sections[i].sh_link]); } } diff --git a/src/kernel/core/sys.c b/src/kernel/core/sys.c index 725843e..2908868 100644 --- a/src/kernel/core/sys.c +++ b/src/kernel/core/sys.c @@ -3,6 +3,8 @@ #include <thread.h> #include <string.h> +#include <elf.h> + #include <btree.h> @@ -44,31 +46,21 @@ void sys_panic_assert(const char* assertion, const char* file, int line) { btree_t *kernel_symbol_map = 0; -void load_kernel_symbol_map(char* text, size_t len) { +void load_kernel_symbol_table(elf_shdr_t *sym, elf_shdr_t *str) { kernel_symbol_map = create_btree(id_key_cmp_fun, 0); ASSERT (kernel_symbol_map != 0); - dbg_printf("Loading kernel symbol map...\n"); - - char* it = text; - while (it < text + len) { - char* eol = it; - while (eol < text + len && *eol != 0 && *eol != '\n') eol++; - if (eol >= text + len) break; - *eol = 0; - - if (it[16] == '0' && it[17] == 'x' && it[34] == ' ' && it[49] == ' ') { - uint32_t addr = 0; - for (unsigned i = 18; i < 34; i++) { - addr *= 16; - if (it[i] >= '0' && it[i] <= '9') addr += it[i] - '0'; - if (it[i] >= 'a' && it[i] <= 'f') addr += it[i] - 'a' + 10; - } - btree_add(kernel_symbol_map, (void*)addr, it + 50); - } + dbg_printf("Loading kernel symbol table...\n"); - it = eol + 1; - } + + ASSERT(sym->sh_entsize == sizeof(elf_sym_t)); + unsigned nsym = sym->sh_size / sym->sh_entsize; + + elf_sym_t *st = (elf_sym_t*)sym->sh_addr; + const char* strtab = (const char*)(str->sh_addr); + for (unsigned j = 0; j < nsym; j++) { + btree_add(kernel_symbol_map, (void*)st[j].st_value, (void*)(strtab + st[j].st_name)); + } } void kernel_stacktrace(uint32_t ebp, uint32_t eip) { |