1 /* Copyright (C) 2012 Red Hat, Inc. 2 This file is part of elfutils. 3 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. 8 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. 13 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/>. */ 16 17 #ifdef HAVE_CONFIG_H 18 # include <config.h> 19 #endif 20 21 #include <sys/types.h> 22 #include <sys/stat.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <unistd.h> 26 #include <errno.h> 27 #include <string.h> 28 #include <fcntl.h> 29 #include <gelf.h> 30 #include <libelf.h> 31 #include <stdbool.h> 32 #include <inttypes.h> 33 34 int 35 main (int argc, char *argv[]) 36 { 37 if (argc != 3) 38 { 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 */ 43 } 44 45 bool do_mmap = false; 46 bool close_fd = false; 47 if (strcmp (argv[1], "READ") == 0) 48 { 49 do_mmap = false; 50 close_fd = false; 51 } 52 else if (strcmp (argv[1], "MMAP") == 0) 53 { 54 do_mmap = true; 55 close_fd = false; 56 } 57 else if (strcmp (argv[1], "FDREAD") == 0) 58 { 59 do_mmap = false; 60 close_fd = true; 61 } 62 else 63 { 64 fprintf (stderr, "First arg needs to be 'READ', 'MMAP' or 'FDREAD'\n"); 65 exit (2); /* user error */ 66 } 67 68 elf_version (EV_CURRENT); 69 70 int fd = open (argv[2], O_RDONLY); 71 if (fd < 0) 72 { 73 fprintf (stderr, "Cannot open input file %s: %s\n", argv[2], 74 strerror (errno)); 75 exit (2); 76 } 77 78 Elf *elf = elf_begin (fd, do_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL); 79 if (elf == NULL) 80 { 81 fprintf (stderr, "elf_begin failed for %s: %s\n", argv[2], 82 elf_errmsg (-1)); 83 exit (2); 84 } 85 86 if (! do_mmap && close_fd) 87 { 88 if (elf_cntl (elf, ELF_C_FDREAD) < 0) 89 { 90 fprintf (stderr, "elf_cntl failed for %s: %s\n", argv[2], 91 elf_errmsg (-1)); 92 exit (1); 93 } 94 close (fd); 95 } 96 97 Elf_Scn *scn = NULL; 98 while ((scn = elf_nextscn (elf, scn)) != NULL) 99 { 100 GElf_Shdr shdr_mem; 101 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 102 printf ("Section at offset %#0" PRIx64 "\n", shdr->sh_offset); 103 } 104 105 elf_end (elf); 106 exit (0); 107 } 108