Skip to content

Commit

Permalink
[Kernel] Persist loaded ELF string and symbol tables
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Dec 26, 2022
1 parent b12a3c6 commit 8dc7990
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 4 additions & 2 deletions kernel/kernel/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ bool append(char** buf_head, int32_t* buf_size, const char* format, ...) {
}

bool symbolicate_and_append__user_mode_frame(int frame_idx, uintptr_t* frame_addr, char** buf_head, int32_t* buf_size) {
// If we've traversed to the NULL page, we're probably out of stack frames
return symbolicate_and_append(frame_idx, frame_addr, buf_head, buf_size);
}

bool symbolicate_and_append(int frame_idx, uintptr_t* frame_addr, char** buf_head, int32_t* buf_size) {
printf("symbolicate(%d, 0x%x)\n", frame_idx, frame_addr);
printf("symbolicate(%d, 0x%x) = ", frame_idx, frame_addr);

if (frame_addr < PAGE_SIZE) {
// If we've traversed to the NULL page, we're probably out of stack frames
printf("NULL\n");
return false;
}

Expand Down Expand Up @@ -134,6 +135,7 @@ bool symbolicate_and_append(int frame_idx, uintptr_t* frame_addr, char** buf_hea
*/
}

printf("[%02d] 0x%p %s\n", frame_idx, (uintptr_t)frame_addr, symbol);
bool can_append_more = append(buf_head, buf_size, "[%02d] 0x%p %s\n", frame_idx, (uintptr_t)frame_addr, symbol);
if (!can_append_more || found_program_start) {
return false;
Expand Down
7 changes: 6 additions & 1 deletion kernel/kernel/multitasking/tasks/task_small.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ void _thread_destroy(task_small_t* thread) {

// Free kernel stack
//printf("Free kernel stack 0x%p\n", thread->kernel_stack_malloc_head);
kfree(thread->kernel_stack_malloc_head);
kfree((void*)thread->kernel_stack_malloc_head);

// Free the string table and symbol table that were copied to the heap
// TODO(PT): These are only heap copies when the underlying program was loaded from an ELF
kfree((void*)thread->elf_symbol_table.strtab);
kfree((void*)thread->elf_symbol_table.symtab);

if (!thread->is_thread) {
// Free AMC service if there is one
Expand Down
19 changes: 15 additions & 4 deletions kernel/kernel/util/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void _record_elf_symbol_table(void* buf, elf_t* elf) {
elf->strtabsz = shdr->size;
}
if (!strcmp(name, ".symtab")) {
elf->symtab = (const char*)((uint8_t*)buf + shdr->offset);
elf->symtab = (elf_symbol_t *)((uint8_t *) buf + shdr->offset);
elf->symtabsz = shdr->size;
}
}
Expand All @@ -171,9 +171,20 @@ void elf_load_buffer(char* program_name, char** argv, uint8_t* buf, uint32_t buf
task_assert(elf_validate_header(hdr), "ELF header validation failed", NULL);

task_small_t* current_task = tasking_get_task_with_pid(getpid());

char* string_table = elf_get_string_table(hdr, buf_size);
_record_elf_symbol_table(buf, &current_task->elf_symbol_table);
char* string_table = elf_get_string_table(hdr, buf_size);

// Copy the strings/symbols data to the heap, since we're freeing the underlying ELF buffer later on
elf_t symbols_data;
_record_elf_symbol_table(buf, &symbols_data);
void* string_table_copy = kmalloc(symbols_data.strtabsz);
memcpy(string_table_copy, symbols_data.strtab, symbols_data.strtabsz);
current_task->elf_symbol_table.strtabsz = symbols_data.strtabsz;
current_task->elf_symbol_table.strtab = string_table_copy;

void* symbol_table_copy = kmalloc(symbols_data.symtabsz);
memcpy(symbol_table_copy, symbols_data.symtab, symbols_data.symtabsz);
current_task->elf_symbol_table.symtabsz = symbols_data.symtabsz;
current_task->elf_symbol_table.symtab = symbol_table_copy;

uintptr_t prog_break = 0;
uintptr_t bss_loc = 0;
Expand Down

0 comments on commit 8dc7990

Please sign in to comment.