1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. 2 This file is part of elfutils. 3 Written by Ulrich Drepper <drepper (at) redhat.com>, 1998. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include <config.h> 19 20 #include <errno.h> 21 #include <error.h> 22 #include <fcntl.h> 23 #include <gelf.h> 24 #include <inttypes.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <unistd.h> 28 29 30 /* Prototypes for local functions. */ 31 static int handle_section (Elf *elf, Elf_Scn *scn); 32 static void print_bytes (Elf_Data *data); 33 static void print_symtab (Elf *elf, Elf_Data *data); 34 35 36 int 37 main (int argc, char *argv[]) 38 { 39 Elf *elf; 40 int fd; 41 int cnt; 42 43 if (argc <= 1) 44 exit (1); 45 46 /* Open the test file. This is given as the first parameter to the 47 program. */ 48 fd = open (argv[1], O_RDONLY); 49 if (fd == -1) 50 error (EXIT_FAILURE, errno, "cannot open input file `%s'", argv[1]); 51 52 /* Set the library versio we expect. */ 53 elf_version (EV_CURRENT); 54 55 /* Create the ELF descriptor. */ 56 elf = elf_begin (fd, ELF_C_READ, NULL); 57 if (elf == NULL) 58 error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s", 59 elf_errmsg (0)); 60 61 /* Now proces all the sections mentioned in the rest of the command line. */ 62 for (cnt = 2; cnt < argc; ++cnt) 63 if (handle_section (elf, elf_getscn (elf, atoi (argv[cnt]))) != 0) 64 /* When we encounter an error stop immediately. */ 65 error (EXIT_FAILURE, 0, "while processing section %d: %s", cnt, 66 elf_errmsg (0)); 67 68 /* Close the descriptor. */ 69 if (elf_end (elf) != 0) 70 error (EXIT_FAILURE, 0, "failure while closing ELF descriptor: %s", 71 elf_errmsg (0)); 72 73 return 0; 74 } 75 76 77 static int 78 handle_section (Elf *elf, Elf_Scn *scn) 79 { 80 GElf_Ehdr *ehdr; 81 GElf_Ehdr ehdr_mem; 82 GElf_Shdr *shdr; 83 GElf_Shdr shdr_mem; 84 Elf_Data *data; 85 86 /* First get the ELF and section header. */ 87 ehdr = gelf_getehdr (elf, &ehdr_mem); 88 shdr = gelf_getshdr (scn, &shdr_mem); 89 if (ehdr == NULL || shdr == NULL) 90 return 1; 91 92 /* Print the information from the ELF section header. */ 93 printf ("name = %s\n" 94 "type = %" PRId32 "\n" 95 "flags = %" PRIx64 "\n" 96 "addr = %" PRIx64 "\n" 97 "offset = %" PRIx64 "\n" 98 "size = %" PRId64 "\n" 99 "link = %" PRId32 "\n" 100 "info = %" PRIx32 "\n" 101 "addralign = %" PRIx64 "\n" 102 "entsize = %" PRId64 "\n", 103 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name), 104 shdr->sh_type, 105 shdr->sh_flags, 106 shdr->sh_addr, 107 shdr->sh_offset, 108 shdr->sh_size, 109 shdr->sh_link, 110 shdr->sh_info, 111 shdr->sh_addralign, 112 shdr->sh_entsize); 113 114 /* Get the section data now. */ 115 data = elf_getdata (scn, NULL); 116 if (data == NULL) 117 return 1; 118 119 /* Now proces the different section types accordingly. */ 120 switch (shdr->sh_type) 121 { 122 case SHT_SYMTAB: 123 print_symtab (elf, data); 124 break; 125 126 case SHT_PROGBITS: 127 default: 128 print_bytes (data); 129 break; 130 } 131 132 /* Separate form the next section. */ 133 puts (""); 134 135 /* All done correctly. */ 136 return 0; 137 } 138 139 140 static void 141 print_bytes (Elf_Data *data) 142 { 143 size_t size = data->d_size; 144 off_t offset = data->d_off; 145 unsigned char *buf = (unsigned char *) data->d_buf; 146 size_t cnt; 147 148 for (cnt = 0; cnt < size; cnt += 16) 149 { 150 size_t inner; 151 152 printf ("%*zx: ", sizeof (size_t) == 4 ? 8 : 16, (size_t) offset + cnt); 153 154 for (inner = 0; inner < 16 && cnt + inner < size; ++inner) 155 printf (" %02hhx", buf[cnt + inner]); 156 157 puts (""); 158 } 159 } 160 161 162 static void 163 print_symtab (Elf *elf, Elf_Data *data) 164 { 165 int class = gelf_getclass (elf); 166 size_t nsym = data->d_size / (class == ELFCLASS32 167 ? sizeof (Elf32_Sym) : sizeof (Elf64_Sym)); 168 size_t cnt; 169 170 for (cnt = 0; cnt < nsym; ++cnt) 171 { 172 GElf_Sym sym_mem; 173 GElf_Sym *sym = gelf_getsym (data, cnt, &sym_mem); 174 175 printf ("%5zu: %*" PRIx64 " %6" PRIx64 " %4d\n", 176 cnt, 177 class == ELFCLASS32 ? 8 : 16, 178 sym->st_value, 179 sym->st_size, 180 GELF_ST_TYPE (sym->st_info)); 181 } 182 } 183