5

elfファイルの各セクションのオフセットとデータを取得しようとしています。私はすでにこのコードでセクション名を持っています:

#include <elf.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

int filesize(int fd)
{
  return (lseek(fd, 0, SEEK_END));
}

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

  for(i = 0; i < shNum; i++)
     printf("%02d: %s\n", i, &strTab[shdr[i].sh_name]);
}

int main(int ac, char **av)
{
  void  *data;
  Elf64_Ehdr    *elf;
  Elf64_Shdr    *shdr;
  int       fd;
  char      *strtab;

  fd = open(av[1], O_RDONLY);
  data = mmap(NULL, filesize(fd), PROT_READ, MAP_SHARED, fd, 0);
  elf = (Elf64_Ehdr *) data;
  shdr = (Elf64_Shdr *) (data + elf->e_shoff);
  strtab = (char *)(data + shdr[elf->e_shstrndx].sh_offset);
  print_section(shdr, strtab, elf->e_shnum);
  close(fd);
  return 0;
}

しかし、各セクションのデータも開始オフセットも取得する方法が見つかりません。ご協力いただきありがとうございます

4

1 に答える 1

7

私はあなたが使うことができると思いsh_offsetます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");
  }
}

そしてそれをこのように呼んでください:

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

仮想アドレス(オフセット)を取得する1つの方法:

Elf64_Phdr *ph = (Elf64_Phdr *) ((uint8_t *) data + elf->e_phoff);
printf("Virtual address offset: %lx\n", ph->p_vaddr - elf->e_phoff);
于 2013-03-12T03:30:17.563 に答える