#include "elf.h" #include "elf_struct.h" #include "tty.h" #include "task.h" static void print(const char*msg,u32 val) { con->putval(msg,val); } static void print(const char*msg,void* val) { con->putval(msg,val); } static void print(const char*msg) { con->print(msg); } void printProgramHeader(Elf32_Ehdr*header,Elf32_Phdr* ph,Task*t) { // Elf32_Word p_type; /* Segment type */ if(ph->p_type==PT_LOAD) { print("LOAD\n"); } else { print("Not a loadable section - not bothering.\n"); return; } // print("p_type:",ph->p_type); print("p_offset:",ph->p_offset); // print("p_vaddr:",ph->p_vaddr); // print("p_paddr:",ph->p_paddr); // print("p_filesz:",ph->p_filesz); // print("p_memsz:",ph->p_memsz); // print("p_flags:",ph->p_flags); // print("p_align:",ph->p_align); // map the pages it needs. for(u32 start_map=ph->p_vaddr;start_map<=ph->p_vaddr+ph->p_memsz;start_map+=0x1000) { t->page_dir.map_anon(start_map,1,1); // con->putval("Mapping page at ",start_map); } print("header:",header); print("offset:",ph->p_offset); void* src=reinterpret_cast(ph->p_offset+(u32)header); void* dest=reinterpret_cast(ph->p_vaddr); print("Copying from ",src); print("To ",dest); // print("Size:",ph->p_memsz); memcpy(dest,src,ph->p_filesz); if(ph->p_fileszp_memsz) { print("Memsize doesn't match filesize, zeroing the rest.\n"); memset((void*)((u32)dest+ph->p_filesz),0,ph->p_memsz-ph->p_filesz); } // print("\nBINDUMP:\n"); // UNUSED u8* temp=(u8*)src; // for(u32 i=0;ip_memsz/2;++i) { // con->puthexbyte(temp[i]); // } // print("\n"); // PANIC("TEST"); // print("DONE!"); // Elf32_Off p_offset; /* Segment file offset */ // Elf32_Addr p_vaddr; /* Segment virtual address */ // Elf32_Addr p_paddr; /* Segment physical address */ // Elf32_Word p_filesz; /* Segment size in file */ // Elf32_Word p_memsz; /* Segment size in memory */ // Elf32_Word p_flags; /* Segment flags */ // Elf32_Word p_align; } #include "interrupts.h" void finish_off(Elf32_Ehdr*head,Task* t) { t->regs.eip=head->e_entry; t->page_dir.map_anon(0xA0000000,1,1); t->regs.useresp=0xA0000FFC; t->regs.ebp=0xA0000FFC; // IRQHandler::dump_registers(t->regs); t->page_dir.switch_to_this(); // regs.print(); } void printSectionHeader(Elf32_Shdr* sh) { // sh_name; /* Section name (string tbl index) */ print("sh_name:",sh->sh_name); print("sh_type:",sh->sh_type); print("sh_flags:",sh->sh_flags); print("sh_addr:",sh->sh_addr); print("sh_offset:",sh->sh_offset); print("sh_size:",sh->sh_size); print("sh_link:",sh->sh_link); print("sh_info:",sh->sh_info); print("sh_addralign:",sh->sh_addralign); print("sh_entsize:",sh->sh_entsize); // sh_type; /* Section type */ // Elf32_Word sh_flags; /* Section flags */ // Elf32_Addr sh_addr; /* Section virtual addr at execution */ // Elf32_Off sh_offset; /* Section file offset */ // Elf32_Word sh_size; /* Section size in bytes */ // Elf32_Word sh_link; /* Link to another section */ // Elf32_Word sh_info; /* Additional section information */ // Elf32_Word sh_addralign; /* Section alignment */ // Elf32_Word sh_entsize; } void parseElf(u32 start,Task *t) { // unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ // Elf64_Half e_type; /* Object file type */ // Elf64_Half e_machine; /* Architecture */ // Elf64_Word e_version; /* Object file version */ // Elf64_Addr e_entry; /* Entry point virtual address */ // Elf64_Off e_phoff; /* Program header table file offset */ // Elf64_Off e_shoff; /* Section header table file offset */ // Elf64_Word e_flags; /* Processor-specific flags */ // Elf64_Half e_ehsize; /* ELF header size in bytes */ // Elf64_Half e_phentsize; /* Program header table entry size */ // Elf64_Half e_phnum; /* Program header table entry count */ // Elf64_Half e_shentsize; /* Section header table entry size */ // Elf64_Half e_shnum; /* Section header table entry count */ // Elf64_Half e_shstrndx; /* Section header string table index */ Elf32_Ehdr* header=reinterpret_cast(start); print("Entry point:",header->e_entry); Elf32_Phdr* pheader=reinterpret_cast(start+header->e_phoff); // Elf32_Shdr* sheader=reinterpret_cast(start+header->e_shoff); // con->putval("Section header offset:",sheader); // con->putval("Program header offset:",pheader); // print("pheader count:",header->e_phnum); // print("pheader size:",header->e_phentsize); // print("sizeof(Phdr):",sizeof(Elf32_Phdr)); //printSectionHeader(sheader); for(int i=0;ie_phnum;++i) { // print("Program header ",i); printProgramHeader(header,pheader+i,t); } finish_off(header,t); }