Guest User

Untitled

a guest
May 5th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. SDWORD load_module(UDWORD mod_start, UDWORD mod_end)
  2. {
  3.     struct kmod *new_mod;
  4.     struct elf_header *eh;
  5.     struct elf_section_header *sh;
  6.     struct elf_symtab_entry *se;
  7.     struct elf_rel *rel;
  8.     CSTRING strtab;
  9.     UDWORD i, j, mod_mem_size, mod_mem_addr, cur_addr;
  10.     UDWORD init_addr = 0, destroy_addr = 0;
  11.     UDWORD *sh_off_table;
  12.     UDWORD rel_section, sym_addr;
  13.     eh = (struct elf_header*)vmm_find_kernel_pages(VMM_NUM_PAGES(mod_end - mod_start));
  14.     if (!eh) {
  15.     return -1;
  16.     }
  17.     if (vmm_map_range(current_thread->p->context, (UDWORD)eh, mod_start, \
  18.     VMM_PTE_P, VMM_NUM_PAGES(mod_end - mod_start)) < 0)
  19.     {
  20.     return -1;
  21.     }
  22.     if (eh->e_ident[EI_MAG0] != ELFMAG0 || eh->e_ident[EI_MAG1] != ELFMAG1 || \
  23.     eh->e_ident[EI_MAG2] != ELFMAG2 || eh->e_ident[EI_MAG3] != ELFMAG3 || \
  24.     eh->e_ident[EI_CLASS] != ELFCLASS32 || eh->e_ident[EI_DATA] != \
  25.     ELFDATA2LSB || eh->e_ident[EI_VERSION] != EV_CURRENT || \
  26.     eh->e_machine != EM_386 || eh->e_version != EV_CURRENT || \
  27.     eh->e_type != ET_REL || sizeof(struct elf_section_header) != \
  28.     eh->e_shentsize || sizeof(struct elf_header) != eh->e_ehsize)
  29.     {
  30.     vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
  31.     return -1;
  32.     }
  33.     sh = (struct elf_section_header*)((UDWORD)eh + eh->e_shoff);
  34.     for (i = 0; i < eh->e_shnum; i++) {
  35.     if (sh[i].sh_type == SHT_PROGBITS || sh[i].sh_type == SHT_NOBITS) {
  36.         mod_mem_size += sh[i].sh_size;
  37.     }
  38.     }
  39.     mod_mem_addr = vmm_alloc_kernel_pages(VMM_NUM_PAGES(mod_mem_size));
  40.     if (!mod_mem_addr) {
  41.     vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
  42.     return -1;
  43.     }
  44.     sh_off_table = (UDWORD*)vmm_alloc_kernel_pages(VMM_NUM_PAGES(eh->e_shnum << 2));
  45.     if (!sh_off_table) {
  46.     vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
  47.     vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
  48.     return -1;
  49.     }
  50.     setdwords(sh_off_table, 0, eh->e_shnum);
  51.     cur_addr = mod_mem_addr;
  52.     for (i = 0; i < eh->e_shnum; i++) {
  53.     if (sh[i].sh_type == SHT_PROGBITS) {
  54.         sh_off_table[i] = cur_addr;
  55.         copybytes((void*)cur_addr, (const void*)((UDWORD)eh + \
  56.         sh[i].sh_offset), sh[i].sh_size);
  57.         cur_addr += sh[i].sh_size;
  58.     } else if (sh[i].sh_type == SHT_NOBITS) {
  59.         sh_off_table[i] = cur_addr;
  60.         setbytes((void*)cur_addr, 0, sh[i].sh_size);
  61.         cur_addr += sh[i].sh_size;
  62.     }
  63.     }
  64.     for (i = 0; i < eh->e_shnum; i++) {
  65.     if (sh[i].sh_type == SHT_REL) {
  66.         rel = (struct elf_rel*)((UDWORD)eh + sh[i].sh_offset);
  67.         se = (struct elf_symtab_entry*)((UDWORD)eh + sh[sh[i].sh_link].sh_offset);
  68.         strtab = (CSTRING)((UDWORD)eh + sh[sh[sh[i].sh_link].sh_link].sh_offset);
  69.         rel_section = sh[i].sh_info;
  70.         for (j = 0; j < (sh[i].sh_size / sh[i].sh_entsize); j++) {
  71.         if (ELF32_ST_TYPE(se[ELF32_R_SYM(rel[j].r_info)].st_info) == STT_SECTION) {
  72.             sym_addr = sh_off_table[se[ELF32_R_SYM(rel[j].r_info)].st_shndx];
  73.         } else {
  74.             CSTRING symname = &(strtab[se[ELF32_R_SYM(rel[j].r_info)].st_name]);
  75.             //TODO: Symboltabelle
  76.             if (streq(symname, "current_thread")) {
  77.             sym_addr = (UDWORD)&current_thread;
  78.             } else if (streq(symname, "vmm_map")) {
  79.             sym_addr = (UDWORD)&vmm_map;
  80.             } else if (streq(symname, "vmm_find_kernel_pages")) {
  81.             sym_addr = (UDWORD)&vmm_find_kernel_pages;
  82.             } else {
  83.             sym_addr = 0;
  84.             }
  85.         }
  86.         if (ELF32_R_TYPE(rel[j].r_info) == R_386_PC32) {
  87.             UDWORD dist = sym_addr - sh_off_table[rel_section] - rel[j].r_offset;
  88.             *((UDWORD*)(sh_off_table[rel_section] + rel[j].r_offset)) += dist;
  89.         } else if (ELF32_R_TYPE(rel[j].r_info) == R_386_32) {
  90.             *((UDWORD*)(sh_off_table[rel_section] + rel[j].r_offset)) += sym_addr;
  91.         }
  92.         }
  93.     }
  94.     }
  95.     for (i = 0; i < eh->e_shnum; i++) {
  96.     if (sh[i].sh_type == SHT_SYMTAB) {
  97.         break;
  98.     }
  99.     }
  100.     if (i == eh->e_shnum) {
  101.     vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
  102.     vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
  103.     return -1;
  104.     }
  105.     strtab = (CSTRING)((UDWORD)eh + sh[sh[i].sh_link].sh_offset);
  106.     se = (struct elf_symtab_entry*)((UDWORD)eh + sh[i].sh_offset);
  107.     j = sh[i].sh_size / sh[i].sh_entsize;
  108.     for (i = 0; i < j; i++) {
  109.     if (streq(strtab + se[i].st_name, "mod_init")) {
  110.         init_addr = sh_off_table[se[i].st_shndx] + se[i].st_value;
  111.     } else if (streq(strtab + se[i].st_name, "mod_destroy")) {
  112.         destroy_addr = sh_off_table[se[i].st_shndx] + se[i].st_value;
  113.     }
  114.     }
  115.     vmm_map_range(current_thread->p->context, (UDWORD)sh_off_table, 0, 0, VMM_NUM_PAGES(eh->e_shnum << 2));
  116.     vmm_map_range(current_thread->p->context, (UDWORD)eh, 0, 0, VMM_NUM_PAGES(mod_end - mod_start));
  117.     new_mod = (struct kmod*)vmm_alloc_kernel_pages(1);
  118.     if (!new_mod) {
  119.     vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
  120.     return -1;
  121.     }
  122.     new_mod->virt_start = mod_mem_addr;
  123.     new_mod->virt_end = mod_mem_addr + (VMM_NUM_PAGES(mod_mem_size) << VMM_PAGE_SHIFT);
  124.     new_mod->mod_init = (void*)init_addr;
  125.     new_mod->mod_destroy = (void*)destroy_addr;
  126.     if (new_mod->mod_init) {
  127.     if (new_mod->mod_init() < 0) {
  128.         vmm_free_kernel_pages(mod_mem_addr, VMM_NUM_PAGES(mod_mem_size));
  129.         vmm_free_kernel_pages((UDWORD)new_mod, 1);
  130.         return -1;
  131.     }
  132.     }
  133.     lock(&mod_list_lock);
  134.     new_mod->next = mod_list;
  135.     mod_list = new_mod;
  136.     unlock(&mod_list_lock);
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment