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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <libelf.h>
/*
* ELF -- Test program to access an ELF format file (executable).
*/
main (argc,argv)
int argc;
char **argv;
{
register Elf32_Phdr *phdr;
register Elf32_Ehdr *ehdr;
register Elf32_Shdr *shdr;
Elf32_Phdr *phdr_array;
int phnum, fd, i;
char strbuf[512];
Elf_Scn *scn;
Elf *elf;
elf_version (EV_CURRENT);
fd = open (argv[1], 2);
if (fd < 0) {
printf ("cannot open file\n");
exit (1);
}
elf = elf_begin (fd, ELF_C_READ, NULL);
if (!elf) {
printf ("not an ELF format file\n");
exit (2);
}
/* Read and print file header.
*/
ehdr = elf32_getehdr (elf);
if (!ehdr) {
printf ("cannot read file header\n");
exit (3);
}
printf ("File type=%d machine=%d version=%d shnum=%d phnum=%d\n",
ehdr->e_type,
ehdr->e_machine,
ehdr->e_version,
ehdr->e_shnum,
phnum = ehdr->e_phnum);
printf ("--------------------------------------------------------\n");
/* Read and print program header.
*/
phdr_array = elf32_getphdr (elf);
if (phnum <= 0 || !phdr_array) {
printf ("cannot read program header\n");
} else {
for (i=0; i < phnum; i++) {
phdr = (Elf32_Phdr *)
((char *)phdr_array + i*ehdr->e_phentsize);
printf ("type=%d offset=%d",
phdr->p_type,
phdr->p_offset);
printf (" vaddr=0x%x fsize=0x%x msize=0x%x align=0x%x\n",
phdr->p_vaddr,
phdr->p_filesz,
phdr->p_memsz,
phdr->p_align);
}
}
printf ("--------------------------------------------------------\n");
/* Summarize files sections.
*/
/* Get section header string buffer. */
scn = elf_getscn (elf, ehdr->e_shstrndx);
shdr = elf32_getshdr (scn);
if (!scn || !shdr)
goto nosec;
lseek (fd, (long)shdr->sh_offset, 0);
if (read (fd, strbuf, sizeof(strbuf)) < sizeof(strbuf)) {
nosec: printf ("cannot read section header\n");
exit (4);
}
/* Print section headers. */
scn = NULL;
while (scn = elf_nextscn(elf,scn)) {
shdr = elf32_getshdr (scn);
printf ("type=%d addr=0x%x offset=0x%x size=0x%x %s\n",
shdr->sh_type,
shdr->sh_addr,
shdr->sh_offset,
shdr->sh_size,
strbuf + shdr->sh_name);
}
elf_end (elf);
close (fd);
}
|