#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++;
}
}
This is my code to get .rela.dyn
section . But I don't know hot to get the symbol's name. I know that Elf64_Rela
structure doesn't have a name
field. In the 'SYMTAB' section, I can get the symbol name using &strtab[p->st_name]
. How can I do?
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
Elf64_Sxword r_addend;
} Elf64_Rela;