1 #include "config.h"
2
3 #include <fcntl.h>
4 #include <libelf.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
main(int argc,const char ** argv)11 int main (int argc, const char **argv)
12 {
13 int fd;
14 Elf *elf;
15 size_t phnum;
16
17 if (argc != 2)
18 {
19 fprintf (stderr, "usage: %s FILE\n", argv[0]);
20 return EXIT_FAILURE;
21 }
22
23 fd = open (argv[1], O_RDONLY);
24 if (fd == -1)
25 {
26 perror ("open");
27 return EXIT_FAILURE;
28 }
29 elf_version (EV_CURRENT);
30 elf = elf_begin (fd, ELF_C_READ, NULL);
31 if (!elf)
32 {
33 fprintf (stderr, "elf_begin: %s\n", elf_errmsg (-1));
34 return EXIT_FAILURE;
35 }
36 if (elf_getphdrnum (elf, &phnum))
37 {
38 fprintf(stderr, "elf_getphdrnum: %s\n", elf_errmsg (-1));
39 return EXIT_FAILURE;
40 }
41
42 printf("%zu\n", phnum);
43
44 elf_end (elf);
45 close (fd);
46
47 return EXIT_SUCCESS;
48 }
49