70,023
社区成员




#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libelf.h>
#include <gelf.h>
char* src_01 = "123456";
int src_02 = 800;
void test(int i)
{
printf("[int test]i is %d\n", i);
}
int main(int argc, char* argv[])
{
Elf *elf = NULL;
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
Elf_Data *data = NULL;
int fd, i, count;
void (*func)(int);
elf_version(EV_CURRENT);
fd = open(argv[1], O_RDONLY);
elf = elf_begin(fd, ELF_C_READ, NULL);
while((scn=elf_nextscn(elf,scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_SYMTAB)
{
break;
}
}
data = elf_getdata(scn, NULL);
count = shdr.sh_size/shdr.sh_entsize;
for (i=0; i<count; i++)
{
GElf_Sym sym;
gelf_getsym(data, i, &sym);
if (!(strcmp("test", elf_strptr(elf, shdr.sh_link, sym.st_name))))
{
func = (void(*)(int))(unsigned long)(sym.st_value&0xffffffff);
func(7);
}
if (!(strcmp("src_01", elf_strptr(elf, shdr.sh_link, sym.st_name))))
{
char* ps = (char *)((unsigned long)(sym.st_value&0xffffffff));
printf("sym.st_size = %d, ps = %s\n", sym.st_size, ps);
}
if (!(strcmp("src_02", elf_strptr(elf, shdr.sh_link, sym.st_name))))
{
int pi = (int)((unsigned long)(sym.st_value&0xffffffff));
printf("sym.st_size = %d, pi = %d\n", sym.st_size, pi);
}
}
elf_end(elf);
close(fd);
return 0;
}