1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <stdio.h> // printf
#include <stdlib.h> // EXIT_FAILURE
#include <error.h> // error
#include <gelf.h> // elf_* and gelf_*
#include <sys/types.h> // open
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // close
int main( int argc, char *argv[])
{
int fd, cmd;
size_t i, n;
char *p;
Elf *arf, *elf;
GElf_Ehdr ehdr;
Elf_Scn *scn;
GElf_Shdr shdr;
Elf_Data *data;
GElf_Dyn dyn;
if(elf_version(EV_CURRENT) == EV_NONE)
error(EXIT_FAILURE, 0, "Library out of date.");
if(argc != 2)
error(EXIT_FAILURE, 0, "Usage: %s <filename>", argv[0]);
if((fd = open(argv[1], O_RDONLY)) == -1)
error(EXIT_FAILURE, 0, "Failed open file.");
cmd = ELF_C_READ;
if((arf = elf_begin(fd, cmd, (Elf *)0)) == NULL)
error(EXIT_FAILURE, 0, "Failed open elf: %s", elf_errmsg ( -1));
while((elf = elf_begin(fd, cmd, arf)) != NULL)
{
if(gelf_getehdr(elf,&ehdr) != NULL)
{
scn = NULL;
while((scn = elf_nextscn(elf, scn)) != NULL)
{
gelf_getshdr(scn, &shdr);
data = elf_getdata(scn, NULL);
if(shdr.sh_type != SHT_DYNAMIC)
continue;
printf("Section name: %s\n", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
if(data != NULL)
for( i=0; gelf_getdyn(data, i, &dyn) != NULL; i++)
{
if(dyn.d_tag == DT_RPATH)
printf("DT_RPATH found\n");
if(dyn.d_tag == DT_RUNPATH)
printf("DT_RUNPATH found\n");
}
}
}
cmd = elf_next(elf);
elf_end(elf);
}
elf_end(arf);
close(fd);
}
|