#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <elf.h>
Elf64_Rela *retab;
Elf64_Rela *retab_end;
Elf64_Ehdr *ehdr;
Elf64_Shdr *shdr;
char *strtab;
void elf_open(char *filename)
{
int fd = open(filename, O_RDONLY);
struct stat sbuf;
fstat(fd, &sbuf);
void *maddr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
ehdr = maddr;
shdr = (Elf64_Shdr *)(maddr + ehdr->e_shoff);
for (int i = 0; i < ehdr->e_shnum; i++)
{
if (shdr[i].sh_type == SHT_RELA)
{
retab = (Elf64_Rela *)(maddr + shdr[i].sh_offset);
retab_end = (Elf64_Rela *)((char *)retab + shdr[i].sh_size);
strtab = (char *)(maddr + shdr[shdr[i].sh_link].sh_offset);
break;
}
}
}
int main()
{
elf_open("lib1.so");
Elf64_Rela *p = retab;
while(p<retab_end)
{
printf("%x %d\n",p->r_offset,p->r_info);
p++;
}
}
这是我获取.rela.dyn
section 的代码。但我不知道如何获得符号的名称。我知道Elf64_Rela
结构没有name
字段。在“SYMTAB”部分,我可以使用&strtab[p->st_name]
. 我能怎么做?
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
Elf64_Sxword r_addend;
} Elf64_Rela;