Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SDWORD load_module(UDWORD mod_start, UDWORD mod_end)
- {
- struct kmod *new_mod;
- struct elf_header *eh;
- struct elf_section_header *sh;
- struct elf_symtab_entry *se;
- struct elf_rel *rel;
- CSTRING strtab;
- UDWORD i, j, mod_mem_size, mod_mem_addr, cur_addr;
- UDWORD init_addr = 0, destroy_addr = 0;
- UDWORD *sh_off_table;
- UDWORD rel_section, sym_addr;
- eh = (struct elf_header*)vmm_find_kernel_pages(VMM_NUM_PAGES(mod_end - mod_start));
- if (!eh) {
- return -1;
- }
- if (vmm_map_range(current_thread->p->context, (UDWORD)eh, mod_start, \
- VMM_PTE_P, VMM_NUM_PAGES(mod_end - mod_start)) < 0)
- {
- return -1;
- }
- if (eh->e_ident[EI_MAG0] != ELFMAG0 || eh->e_ident[EI_MAG1] != ELFMAG1 || \
- eh->e_ident[EI_MAG2] != ELFMAG2 || eh->e_ident[EI_MAG3] != ELFMAG3 || \
- eh->e_ident[EI_CLASS] != ELFCLASS32 || eh->e_ident[EI_DATA] != \
- ELFDATA2LSB || eh->e_ident[EI_VERSION] != EV_CURRENT || \
- eh->e_machine != EM_386 || eh->e_version != EV_CURRENT || \
- eh->e_type != ET_REL || sizeof(struct elf_section_header) != \
- eh->e_shentsize || sizeof(struct elf_header) != eh->e_ehsize)
- {
- vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
- return -1;
- }
- sh = (struct elf_section_header*)((UDWORD)eh + eh->e_shoff);
- for (i = 0; i < eh->e_shnum; i++) {
- if (sh[i].sh_type == SHT_PROGBITS || sh[i].sh_type == SHT_NOBITS) {
- mod_mem_size += sh[i].sh_size;
- }
- }
- mod_mem_addr = vmm_alloc_kernel_pages(VMM_NUM_PAGES(mod_mem_size));
- if (!mod_mem_addr) {
- vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
- return -1;
- }
- sh_off_table = (UDWORD*)vmm_alloc_kernel_pages(VMM_NUM_PAGES(eh->e_shnum << 2));
- if (!sh_off_table) {
- vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
- vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
- return -1;
- }
- setdwords(sh_off_table, 0, eh->e_shnum);
- cur_addr = mod_mem_addr;
- for (i = 0; i < eh->e_shnum; i++) {
- if (sh[i].sh_type == SHT_PROGBITS) {
- sh_off_table[i] = cur_addr;
- copybytes((void*)cur_addr, (const void*)((UDWORD)eh + \
- sh[i].sh_offset), sh[i].sh_size);
- cur_addr += sh[i].sh_size;
- } else if (sh[i].sh_type == SHT_NOBITS) {
- sh_off_table[i] = cur_addr;
- setbytes((void*)cur_addr, 0, sh[i].sh_size);
- cur_addr += sh[i].sh_size;
- }
- }
- for (i = 0; i < eh->e_shnum; i++) {
- if (sh[i].sh_type == SHT_REL) {
- rel = (struct elf_rel*)((UDWORD)eh + sh[i].sh_offset);
- se = (struct elf_symtab_entry*)((UDWORD)eh + sh[sh[i].sh_link].sh_offset);
- strtab = (CSTRING)((UDWORD)eh + sh[sh[sh[i].sh_link].sh_link].sh_offset);
- rel_section = sh[i].sh_info;
- for (j = 0; j < (sh[i].sh_size / sh[i].sh_entsize); j++) {
- if (ELF32_ST_TYPE(se[ELF32_R_SYM(rel[j].r_info)].st_info) == STT_SECTION) {
- sym_addr = sh_off_table[se[ELF32_R_SYM(rel[j].r_info)].st_shndx];
- } else {
- CSTRING symname = &(strtab[se[ELF32_R_SYM(rel[j].r_info)].st_name]);
- //TODO: Symboltabelle
- if (streq(symname, "current_thread")) {
- sym_addr = (UDWORD)¤t_thread;
- } else if (streq(symname, "vmm_map")) {
- sym_addr = (UDWORD)&vmm_map;
- } else if (streq(symname, "vmm_find_kernel_pages")) {
- sym_addr = (UDWORD)&vmm_find_kernel_pages;
- } else {
- sym_addr = 0;
- }
- }
- if (ELF32_R_TYPE(rel[j].r_info) == R_386_PC32) {
- UDWORD dist = sym_addr - sh_off_table[rel_section] - rel[j].r_offset;
- *((UDWORD*)(sh_off_table[rel_section] + rel[j].r_offset)) += dist;
- } else if (ELF32_R_TYPE(rel[j].r_info) == R_386_32) {
- *((UDWORD*)(sh_off_table[rel_section] + rel[j].r_offset)) += sym_addr;
- }
- }
- }
- }
- for (i = 0; i < eh->e_shnum; i++) {
- if (sh[i].sh_type == SHT_SYMTAB) {
- break;
- }
- }
- if (i == eh->e_shnum) {
- vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
- vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
- return -1;
- }
- strtab = (CSTRING)((UDWORD)eh + sh[sh[i].sh_link].sh_offset);
- se = (struct elf_symtab_entry*)((UDWORD)eh + sh[i].sh_offset);
- j = sh[i].sh_size / sh[i].sh_entsize;
- for (i = 0; i < j; i++) {
- if (streq(strtab + se[i].st_name, "mod_init")) {
- init_addr = sh_off_table[se[i].st_shndx] + se[i].st_value;
- } else if (streq(strtab + se[i].st_name, "mod_destroy")) {
- destroy_addr = sh_off_table[se[i].st_shndx] + se[i].st_value;
- }
- }
- vmm_map_range(current_thread->p->context, (UDWORD)sh_off_table, 0, 0, VMM_NUM_PAGES(eh->e_shnum << 2));
- vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
- new_mod = (struct kmod*)vmm_alloc_kernel_pages(1);
- if (!new_mod) {
- vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
- return -1;
- }
- new_mod->virt_start = mod_mem_addr;
- new_mod->virt_end = mod_mem_addr + (VMM_NUM_PAGES(mod_mem_size) << VMM_PAGE_SHIFT);
- new_mod->mod_init = (void*)init_addr;
- new_mod->mod_destroy = (void*)destroy_addr;
- if (new_mod->mod_init) {
- if (new_mod->mod_init() < 0) {
- vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
- vmm_free_kernel_pages((UDWORD)new_mod, 1);
- return -1;
- }
- }
- lock(&mod_list_lock);
- new_mod->next = mod_list;
- mod_list = new_mod;
- unlock(&mod_list_lock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment