summaryrefslogtreecommitdiff
path: root/tests/getphdrnum.c
blob: 07f75db0bb654212612f51b64f418171e1eece8d (plain)
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
#include "config.h"

#include <fcntl.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int main (int argc, const char **argv)
{
  int fd;
  Elf *elf;
  size_t phnum;

  if (argc != 2)
    {
      fprintf (stderr, "usage: %s FILE\n", argv[0]);
      return EXIT_FAILURE;
    }

  fd = open (argv[1], O_RDONLY);
  if (fd == -1)
    {
      perror ("open");
      return EXIT_FAILURE;
    }
  elf_version (EV_CURRENT);
  elf = elf_begin (fd, ELF_C_READ, NULL);
  if (!elf)
    {
      fprintf (stderr, "elf_begin: %s\n", elf_errmsg (-1));
      return EXIT_FAILURE;
    }
  if (elf_getphdrnum (elf, &phnum))
    {
      fprintf(stderr, "elf_getphdrnum: %s\n", elf_errmsg (-1));
      return EXIT_FAILURE;
    }

  printf("%zu\n", phnum);

  elf_end (elf);
  close (fd);

  return EXIT_SUCCESS;
}