1 /* Copyright (C) 2012 Red Hat, Inc.
2 This file is part of elfutils.
4 This file is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <sys/types.h>
35 main (int argc, char *argv[])
39 fprintf (stderr, "Needs two arguments.\n");
40 fprintf (stderr, "First needs to be 'READ', 'MMAP' or 'FDREAD'\n");
41 fprintf (stderr, "Second is the ELF file to read.\n");
42 exit (2); /* user error */
46 bool close_fd = false;
47 if (strcmp (argv[1], "READ") == 0)
52 else if (strcmp (argv[1], "MMAP") == 0)
57 else if (strcmp (argv[1], "FDREAD") == 0)
64 fprintf (stderr, "First arg needs to be 'READ', 'MMAP' or 'FDREAD'\n");
65 exit (2); /* user error */
68 elf_version (EV_CURRENT);
70 int fd = open (argv[2], O_RDONLY);
73 fprintf (stderr, "Cannot open input file %s: %s\n", argv[2],
78 Elf *elf = elf_begin (fd, do_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
81 fprintf (stderr, "elf_begin failed for %s: %s\n", argv[2],
86 if (! do_mmap && close_fd)
88 if (elf_cntl (elf, ELF_C_FDREAD) < 0)
90 fprintf (stderr, "elf_cntl failed for %s: %s\n", argv[2],
98 while ((scn = elf_nextscn (elf, scn)) != NULL)
101 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
102 printf ("Section at offset %#0" PRIx64 "\n", shdr->sh_offset);