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/>. */
17 #include <sys/types.h>
31 main (int argc, char *argv[])
35 fprintf (stderr, "Needs two arguments.\n");
36 fprintf (stderr, "First needs to be 'READ', 'MMAP' or 'FDREAD'\n");
37 fprintf (stderr, "Second is the ELF file to read.\n");
38 exit (2); /* user error */
42 bool close_fd = false;
43 if (strcmp (argv[1], "READ") == 0)
48 else if (strcmp (argv[1], "MMAP") == 0)
53 else if (strcmp (argv[1], "FDREAD") == 0)
60 fprintf (stderr, "First arg needs to be 'READ', 'MMAP' or 'FDREAD'\n");
61 exit (2); /* user error */
64 elf_version (EV_CURRENT);
66 int fd = open (argv[2], O_RDONLY);
69 fprintf (stderr, "Cannot open input file %s: %s\n", argv[2],
74 Elf *elf = elf_begin (fd, do_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
77 fprintf (stderr, "elf_begin failed for %s: %s\n", argv[2],
82 if (! do_mmap && close_fd)
84 if (elf_cntl (elf, ELF_C_FDREAD) < 0)
86 fprintf (stderr, "elf_cntl failed for %s: %s\n", argv[2],
94 while ((scn = elf_nextscn (elf, scn)) != NULL)
97 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
98 printf ("Section at offset %#0" PRIx64 "\n", shdr->sh_offset);