OSDN Git Service

projects clean up 1
[pinoc/pinoc.git] / test / elf_analyze_test / elf_analyze_test / elf.cpp
diff --git a/test/elf_analyze_test/elf_analyze_test/elf.cpp b/test/elf_analyze_test/elf_analyze_test/elf.cpp
new file mode 100644 (file)
index 0000000..3592b68
--- /dev/null
@@ -0,0 +1,237 @@
+
+#include "elf.h"
+#include "stdio.h"
+#include "stdlib.h"
+#include "string.h"
+
+static unsigned char m_magic[4];
+static unsigned char m_fileclass;
+static unsigned char m_endian;
+static unsigned char m_elf_version;
+static unsigned char m_os_abi;
+static unsigned char m_abi_version;
+static unsigned short m_e_type;
+static unsigned short m_e_machine;
+static unsigned long m_e_version;
+static unsigned long m_e_entry;
+static unsigned long m_e_phoff;
+static unsigned long m_e_shoff;
+static unsigned long m_e_flags;
+static unsigned short m_e_ehsize;
+static unsigned short m_e_phentsize;
+static unsigned short m_e_phnum;
+static unsigned short m_e_shentsize;
+static unsigned short m_e_shnum;
+static unsigned short m_e_shstrndx;
+static struct Elf32_Phdr* m_program_hed;
+
+// 32bit\82Ü\82Å
+static long littletobig(unsigned char* buff, char size)
+{
+       unsigned long tmp = 0;
+       unsigned long scale = 1;
+       int i;
+       
+       for(i = size - 1; i >= 0; i--)
+       {
+               tmp += buff[i] * scale;
+               scale *= 0x100;
+       }
+
+       //tmp += buff[0] * 0x1000000;
+       //tmp += buff[1] * 0x10000;
+       //tmp += buff[2] * 0x100;
+       //tmp += buff[3];
+
+       //printf("ltob 0x%x\n", tmp);
+
+       return tmp;
+}
+
+void elf_status()
+{
+       int i;
+       struct Elf32_Phdr* elf_program_ent;
+
+       printf("-------- elf_status --------\n");
+
+       printf("---- ELF Header ----\n");
+       // \83}\83W\83b\83N
+       printf("magic : %x%c%c%c\n", m_magic[0], m_magic[1], m_magic[2], m_magic[3]);
+       printf("\83t\83@\83C\83\8b\83N\83\89\83X : %d\n", m_fileclass);
+       printf("\83G\83\93\83f\83B\83A\83\93 : %d\n", m_endian);
+       printf("elf version : %d\n", m_elf_version);
+       printf("OS ABI : %d\n", m_os_abi);
+       printf("ABI version : %d\n", m_abi_version);
+       // e_ident[9...15]      \96¢\8eg\97p
+       printf("\8dÄ\94z\92u\89Â\94\\82â\8eÀ\8ds\89Â\94\\93\99\82ÌELF\82Ì\8c`\8e®\82ð\95\\82·\81B : 0x%x\n", m_e_type);
+       printf("\83t\83@\83C\83\8b\82Å\97v\8b\81\82³\82ê\82é\83A\81[\83L\83e\83N\83`\83\83 : 0x%x\n", m_e_machine);
+       printf("ELF\83t\83H\81[\83}\83b\83g\82Ì\83o\81[\83W\83\87\83\93\82Å\8c»\8dÝ\82Í1 : 0x%x\n", m_e_version);
+       printf("\83v\83\8d\83O\83\89\83\80\82Ì\83G\83\93\83g\83\8a\83|\83C\83\93\83g\81B\8ew\92è\96³\82µ\82È\82ç0 : 0x%x\n", m_e_entry);
+       printf("\83v\83\8d\83O\83\89\83\80\83w\83b\83_\83e\81[\83u\83\8b\82Ì\83t\83@\83C\83\8b\90æ\93ª\82©\82ç\82Ì\83I\83t\83Z\83b\83g : %dbyte\n", m_e_phoff);
+       printf("\8eÀ\8ds\8e\9e\82É\82Í\8eg\82í\82ê\82È\82¢ : 0x%x\n", m_e_shoff);
+       printf("\83v\83\8d\83Z\83b\83T\8cÅ\97L\82Ì\83t\83\89\83O : 0x%x\n", m_e_flags);
+       printf("ELF\83w\83b\83_\82Ì\83T\83C\83Y : %dbyte\n", m_e_ehsize);
+       printf("\83v\83\8d\83O\83\89\83\80\83w\83b\83_\83e\81[\83u\83\8b\82Ì1\97v\91f\82 \82½\82è\82Ì\83T\83C\83Y : %dbyte\n", m_e_phentsize);
+       printf("\83v\83\8d\83O\83\89\83\80\83w\83b\83_\83e\81[\83u\83\8b\82Ì\97v\91f\90\94 : %d\8cÂ\n", m_e_phnum);
+       printf("\8eÀ\8ds\8e\9e\82É\82Í\8eg\82í\82ê\82È\82¢ : 0x%x\n", m_e_shentsize);
+       printf("\8eÀ\8ds\8e\9e\82É\82Í\8eg\82í\82ê\82È\82¢ : 0x%x\n", m_e_shnum);
+       printf("\8eÀ\8ds\8e\9e\82É\82Í\8eg\82í\82ê\82È\82¢ : 0x%x\n", m_e_shstrndx);
+
+       printf("---- Program Header ----\n");
+       elf_program_ent = m_program_hed;
+       // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\81[\82Ì\8cÂ\90\94\95ª\8cJ\82è\95Ô\82·
+       for(i = 0; i < m_e_phnum; i++)
+       {
+               printf("\81¡ Program Header %d\n", i+1);
+               
+               printf("1.\83G\83\93\83g\83\8a\82Ì\83^\83C\83v : 0x%x\n", elf_program_ent->p_type);
+               printf("2.\91Î\89\9e\82·\82é\83Z\83O\83\81\83\93\83g\82Ì\83t\83@\83C\83\8b\90æ\93ª\82©\82ç\82Ì\83I\83t\83Z\83b\83g : %dbyte\n", elf_program_ent->p_offset);
+               printf("3.\83\81\83\82\83\8a\8fã\82Å\82Ì\83Z\83O\83\81\83\93\83g\82Ì\91æ\88ê\83o\83C\83g\82Ì\89¼\91z\83A\83h\83\8c\83X : 0x%x\n", elf_program_ent->p_vaddr);
+               printf("4.\95¨\97\9d\94Ô\92n\8ew\92è\82ª\93K\90Ø\82È\83V\83X\83e\83\80\82Ì\88×\82É\97\\96ñ : 0x%x\n", elf_program_ent->p_paddr);
+               printf("5.\91Î\89\9e\82·\82é\83Z\83O\83\81\83\93\83g\82Ì\83t\83@\83C\83\8b\82Å\82Ì\83T\83C\83Y\81B0\82à\89 : %dbyte\n", elf_program_ent->p_filesz);
+               printf("6.\91Î\89\9e\82·\82é\83Z\83O\83\81\83\93\83g\82Ì\83\81\83\82\83\8a\8fã\82É\93W\8aJ\82³\82ê\82½\8e\9e\82Ì\83T\83C\83Y\81B0\82à\89 : %dbyte\n", elf_program_ent->p_memsz);
+               printf("7.\91Î\89\9e\82·\82é\83Z\83O\83\81\83\93\83g\82É\93K\90Ø\82È\83t\83\89\83O : 0x%x\n", elf_program_ent->p_flags);
+               printf("8.p_offset\81u2.\81v\82Æp_vaddr\81u3.\81v\82ð\82±\82Ì\92l\82Å\8a\84\82Á\82½\97]\82è\82Í\93\99\82µ\82¢ : 0x%x\n", elf_program_ent->p_align);
+
+               elf_program_ent = elf_program_ent->next;
+       }
+}
+
+int elf_check(struct Elf32_Ehdr* buff)
+{
+       //e_ident[0]    magic   0x7f
+       //e_ident[1]    magic   'E'
+       //e_ident[2]    magic   'L'
+       //e_ident[3]    magic   'F'
+       //e_ident[4]    \83t\83@\83C\83\8b\82Ì\83N\83\89\83X        0=invalid, 1=32bit, 2=64bit
+       //e_ident[5]    \83f\81[\83^\82Ì\95\84\8d\86\89»\95û\8e®\81i\83G\83\93\83f\83B\83A\83\93\93\99\81j    0=invalid, 1=LSB, 2=MSB
+       //e_ident[6]    ELF\83w\83b\83_\82Ì\83o\81[\83W\83\87\83\93   \8c»\8dÝ\82Í1
+       //e_ident[7]    \83t\83@\83C\83\8b\82ª\91Î\8fÛ\82Æ\82·\82éOS\82ÆABI     
+       //e_ident[8]    \83t\83@\83C\83\8b\82ª\91Î\8fÛ\82Æ\82·\82éABI\82Ì\83o\81[\83W\83\87\83\93     
+       //e_ident[9...15]       \96¢\8eg\97p
+       
+       int check_flg = 0;
+       
+       printf("-------- elf_check --------\n");
+       
+       // magic\83R\81[\83h\82ª\8aÔ\88á\82Á\82Ä\82¢\82ê\82ÎELF\83t\83@\83C\83\8b\82Å\82Í\82È\82¢\82Æ\94»\92f
+       if(buff->chare_ident[0] != 0x7F) check_flg = 1;
+       if(buff->chare_ident[1] != 'E') check_flg = 1;
+       if(buff->chare_ident[2] != 'L') check_flg = 1;
+       if(buff->chare_ident[3] != 'F') check_flg = 1;
+
+       if(check_flg)
+       {
+               printf("\90³\82µ\82¢ELF\83t\83@\83C\83\8b\83t\83H\81[\83}\83b\83g\82Å\82Í\82 \82è\82Ü\82¹\82ñ\n");
+               return 1;
+       }
+       printf("\90³\82µ\82¢ELF\83t\83@\83C\83\8b\83t\83H\81[\83}\83b\83g\82Å\82·\n");
+       return 0;
+}
+
+
+// elf\83t\83@\83C\83\8b\82ð\89ð\90Í\82µ\82Ä\8f\89\8aú\89»\82·\82é
+int elf_init(char* buff)
+{
+       int i;
+       struct Elf32_Phdr* ent,* ent_tmp;
+       struct Elf32_Ehdr* elf_header;
+       struct Elf32_Phdr* elf_program;
+
+       printf("-------- elf_init --------\n");
+       elf_header = (struct Elf32_Ehdr*)buff;
+       if(elf_check(elf_header))
+               return 1;
+
+       m_magic[0] = elf_header->chare_ident[0];
+       m_magic[0] = elf_header->chare_ident[1];
+       m_magic[0] = elf_header->chare_ident[2];
+       m_magic[0] = elf_header->chare_ident[3];
+       m_fileclass = elf_header->chare_ident[4];
+       m_endian =  elf_header->chare_ident[5];
+       m_elf_version = elf_header->chare_ident[6];
+       m_os_abi = elf_header->chare_ident[7];
+       m_abi_version = elf_header->chare_ident[8];
+       // e_ident[9...15]      \96¢\8eg\97p
+       m_e_type = littletobig((unsigned char*)&elf_header->e_type, 2);
+       m_e_machine = littletobig((unsigned char*)&elf_header->e_machine, 2);
+       m_e_version = littletobig((unsigned char*)&elf_header->e_version, 4);
+       m_e_entry = littletobig((unsigned char*)&elf_header->e_entry, 4);
+       m_e_phoff = littletobig((unsigned char*)&elf_header->e_phoff, 4);
+       m_e_shoff = littletobig((unsigned char*)&elf_header->e_shoff, 4);
+       m_e_flags = littletobig((unsigned char*)&elf_header->e_flags, 4);
+       m_e_ehsize =  littletobig((unsigned char*)&elf_header->e_ehsize, 2);
+       m_e_phentsize = littletobig((unsigned char*)&elf_header->e_phentsize, 2);
+       m_e_phnum =  littletobig((unsigned char*)&elf_header->e_phnum, 2);
+       m_e_shentsize = littletobig((unsigned char*)&elf_header->e_shentsize, 2);
+       m_e_shnum = littletobig((unsigned char*)&elf_header->e_shnum, 2);
+       m_e_shstrndx = littletobig((unsigned char*)&elf_header->e_shstrndx, 2);
+
+       // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\82Ì\90\94\82Í\95s\92è\82È\82Ì\82Å\83\8a\83X\83g\8d\\91¢\82Å\95Û\8e\9d\82·\82é
+       ent = (struct Elf32_Phdr*)malloc(sizeof(struct Elf32_Phdr));
+       m_program_hed = ent;
+
+       // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\81[\82Ì\8cÂ\90\94\95ª\8cJ\82è\95Ô\82·
+       for(i = 0; i < m_e_phnum; i++)
+       {
+               // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\82Ì\8aJ\8en\94Ô\92n\8eæ\93¾
+               elf_program = (struct Elf32_Phdr*)&buff[ m_e_phoff + m_e_phentsize * i ];
+               
+               // \83w\83b\83_\82Ì\93à\97e\82ð\95Û\91
+               ent->p_type = littletobig((unsigned char*)&elf_program->p_type, 4);
+               ent->p_offset = littletobig((unsigned char*)&elf_program->p_offset, 4);
+               ent->p_vaddr = littletobig((unsigned char*)&elf_program->p_vaddr, 4);
+               ent->p_paddr = littletobig((unsigned char*)&elf_program->p_paddr, 4);
+               ent->p_filesz = littletobig((unsigned char*)&elf_program->p_filesz, 4);
+               ent->p_memsz = littletobig((unsigned char*)&elf_program->p_memsz, 4);
+               ent->p_flags = littletobig((unsigned char*)&elf_program->p_flags, 4);
+               ent->p_align = littletobig((unsigned char*)&elf_program->p_align, 4);
+               ent->next = 0x00; // \83\8a\83X\83g\82Ì\8fI\92[\83R\81[\83h 0x00
+
+               // \83\8a\83X\83g\8d\\91¢\82Å\95Û\91\82·\82é
+               ent_tmp = ent;
+               ent = (struct Elf32_Phdr*)malloc(sizeof(struct Elf32_Phdr));
+               ent_tmp->next = ent;
+       }
+
+
+       return 0;
+}
+
+void* elf_devlop(char* buff)
+{
+       int i;
+       long virtual_space_cnt;
+       void* entry_point;
+       struct Elf32_Phdr* ent;
+
+       int test_buff = (int)(char*)malloc(1000);       // \83e\83X\83g\83o\83b\83t\83@\81uvisual studio\8fã\82Å\82Ì\82Ý\95K\97v\81v
+
+       // elf_\83w\83b\83_\81[\82É\83G\83\93\83g\83\8a\83|\83C\83\93\83g\82ª\8f\91\82¢\82Ä\82 \82Á\82½\8fê\8d\87\82Í\97D\90æ\93I\82É\93K\89\9e
+       if(m_e_entry)
+       {
+               entry_point = (void*)m_e_entry;
+       }
+       else // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\82Ì\8aJ\8en\94Ô\92n\82ð\93K\89\9e
+       {
+               entry_point = (void*)m_program_hed->p_vaddr;
+       }
+
+       ent = m_program_hed;
+
+       // \83v\83\8d\83O\83\89\83\80\83w\83b\83_\82Ì\97v\91f\82ð\91S\82Ä\93W\8aJ\82·\82é
+       for(i = 0; i < m_e_phnum; i++)
+       {
+               virtual_space_cnt = ent->p_memsz - ent->p_filesz;
+               memcpy((void*)(test_buff+(ent->p_vaddr)), &buff[ent->p_offset], ent->p_filesz);
+               memset((void*)(test_buff+(ent->p_vaddr) + ent->p_filesz), 0x00, virtual_space_cnt);
+               //memcpy((void*)((ent->p_vaddr)), &buff[ent->p_offset], ent->p_filesz);
+               //memset((void*)((ent->p_vaddr) + ent->p_filesz), 0x00, virtual_space_cnt);
+               ent = ent->next;
+       }
+
+       
+
+       return entry_point;
+}
\ No newline at end of file