Get elf sections offsets

折月煮酒 提交于 2019-12-04 08:27:36

I think you can use sh_offset and shdr[i].sh_size:

void    print_section(Elf64_Shdr *shdr, char *strTab, int shNum, uint8_t *data)
{
  int   i;  

  for(i = 0; i < shNum; i++) {
    size_t k;
     printf("%02d: %s Offset %lx\n", i, &strTab[shdr[i].sh_name], 
        shdr[i].sh_offset);
     for (k = shdr[i].sh_offset; k < shdr[i].sh_offset + shdr[i].sh_size; k++) {
       printf("%x", data[k]);
     }   
     printf("\n");
     for (k = shdr[i].sh_offset; k < shdr[i].sh_offset + shdr[i].sh_size; k++) {
       printf("%c", data[k]);
     }   
     printf("\n");
  }
}

And call it like this:

print_section(shdr, strtab, elf->e_shnum, (uint8_t*)data);

One way to get the virtual address (offset):

Elf64_Phdr *ph = (Elf64_Phdr *) ((uint8_t *) data + elf->e_phoff);
printf("Virtual address offset: %lx\n", ph->p_vaddr - elf->e_phoff);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!