使用c写的一个elf文件解析器

时间:2021-08-04 04:29:34

2018-02-28

  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 
  4 #include"/usr/include/elf.h"
  5 
  6 int main(int argc, char* argv[])
  7 {
  8     if(argc < 2)
  9     {
 10         printf("invalid arguments\n");
 11         exit(0);
 12     }
 13     
 14     int shnum, a;
 15     FILE *fp;
 16     Elf64_Ehdr elf_head;
 17     fp = fopen(argv[1], "r");
 18     if(NULL == fp)
 19     {
 20         printf("open file fail\n");
 21         exit(0);
 22     }
 23     a = fread(&elf_head, sizeof(Elf64_Ehdr), 1, fp);
 24     if(a != 0)
 25         printf("READ OK \n");
 26     else
 27     {
 28         printf("READ ERROR\n");
 29         exit(0);
 30     }
 31 
 32     
 33     if(elf_head.e_ident[0] != 0x7F || elf_head.e_ident[1] != 'E' || elf_head.e_ident[2] != 'L' || elf_head.e_ident[3] != 'F')
 34     {
 35         printf("Not a ELF format file\n");
 36         exit(0);
 37     }
 38     printf("MAIGC NUMBER:\n");
 39     for(shnum = 0; shnum < 16; shnum++)
 40     {
 41         printf("%x ",elf_head.e_ident[shnum]);
 42     }
 43     printf("\n");
 44     printf("%hx\n", elf_head.e_type);
 45     printf("%hx\n", elf_head.e_machine);
 46     printf("%x\n",  elf_head.e_version);
 47     printf("%lx\n", elf_head.e_entry);
 48     printf("%lx\n", elf_head.e_phoff);
 49     printf("%lx\n", elf_head.e_shoff);
 50     printf("%x\n",  elf_head.e_flags);
 51     printf("%hx\n", elf_head.e_ehsize);
 52     printf("%hx\n", elf_head.e_phentsize);
 53     printf("%hx\n", elf_head.e_phnum);
 54     printf("%hx\n", elf_head.e_shentsize);
 55     printf("%hx\n", elf_head.e_shnum);
 56     printf("%hx\n", elf_head.e_shstrndx);
 57 
 58     Elf64_Shdr *shdr = (Elf64_Shdr*)malloc(sizeof(Elf64_Shdr) * elf_head.e_shnum);
 59     if(shdr == NULL)
 60     {
 61         printf("shdr malloc failed\n");
 62         exit(0);
 63     }
 64     a = fseek(fp, elf_head.e_shoff, SEEK_SET);
 65     if(a == 0)
 66         printf("\nshdr fseek OK\n");
 67     else
 68     {
 69         printf("shdr fseek ERROR\n");
 70         exit(0);
 71     }
 72     a = fread(shdr, sizeof(Elf64_Shdr) * elf_head.e_shnum, 1, fp);
 73     if(a != 0)
 74         printf("READ OK \n");
 75     else
 76     {
 77         printf("READ ERROR\n");
 78         exit(0);
 79     }
 80 
 81     rewind(fp);
 82     fseek(fp, shdr[elf_head.e_shstrndx].sh_offset, SEEK_SET);
 83     if(a != 0)
 84         printf("shstrtab fseek OK \n");
 85     else
 86     {
 87         printf("shstrtab fseek ERROR\n");
 88         exit(0);
 89     }
 90     char shstrtab[shdr[elf_head.e_shstrndx].sh_size];
 91     char *temp = shstrtab;
 92     a = fread(shstrtab, shdr[elf_head.e_shstrndx].sh_size, 1, fp);   
 93     if(a != 0)
 94     printf("READ OK\n");
 95     else
 96     {
 97     printf("READ ERROR\n");
 98     exit(0);
 99     }
100     printf("\n\n各段section信息:\n\n");
101     for(shnum = 0; shnum < elf_head.e_shnum; shnum++)
102     {
103         temp = shstrtab;
104         temp = temp + shdr[shnum].sh_name;
105         printf("sh_name: %s \n", temp);
106         printf("sh_type: %x \n", shdr[shnum].sh_type);
107         printf("sh_flags: %x \n", shdr[shnum].sh_flags);
108         printf("sh_addr: %x \n", shdr[shnum].sh_addr);
109         printf("sh_offset: %x \n", shdr[shnum].sh_offset);
110         printf("sh_size: %x \n", shdr[shnum].sh_size);
111         printf("sh_link: %x \n", shdr[shnum].sh_link);
112         printf("sh_info: %x \n", shdr[shnum].sh_info);
113         printf("sh_addralign: %x \n", shdr[shnum].sh_addralign);
114         printf("sh_entsize: %x \n\n\n", shdr[shnum].sh_entsize);
115     }
116 
117     free(shdr);
118     fclose(fp);
119     shdr = NULL; 
120     fp = NULL;
121 
122   return 0;
123 }