1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
2 This file is part of elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
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.
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.
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/>. */
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);
37 main (int argc, char *argv[])
46 /* Open the test file. This is given as the first parameter to the
48 fd = open (argv[1], O_RDONLY);
50 error (EXIT_FAILURE, errno, "cannot open input file `%s'", argv[1]);
52 /* Set the library versio we expect. */
53 elf_version (EV_CURRENT);
55 /* Create the ELF descriptor. */
56 elf = elf_begin (fd, ELF_C_READ, NULL);
58 error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
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,
68 /* Close the descriptor. */
69 if (elf_end (elf) != 0)
70 error (EXIT_FAILURE, 0, "failure while closing ELF descriptor: %s",
78 handle_section (Elf *elf, Elf_Scn *scn)
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)
92 /* Print the information from the ELF section header. */
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),
114 /* Get the section data now. */
115 data = elf_getdata (scn, NULL);
119 /* Now proces the different section types accordingly. */
120 switch (shdr->sh_type)
123 print_symtab (elf, data);
132 /* Separate form the next section. */
135 /* All done correctly. */
141 print_bytes (Elf_Data *data)
143 size_t size = data->d_size;
144 off_t offset = data->d_off;
145 unsigned char *buf = (unsigned char *) data->d_buf;
148 for (cnt = 0; cnt < size; cnt += 16)
152 printf ("%*Zx: ", sizeof (size_t) == 4 ? 8 : 16, (size_t) offset + cnt);
154 for (inner = 0; inner < 16 && cnt + inner < size; ++inner)
155 printf (" %02hhx", buf[cnt + inner]);
163 print_symtab (Elf *elf, Elf_Data *data)
165 int class = gelf_getclass (elf);
166 size_t nsym = data->d_size / (class == ELFCLASS32
167 ? sizeof (Elf32_Sym) : sizeof (Elf64_Sym));
170 for (cnt = 0; cnt < nsym; ++cnt)
173 GElf_Sym *sym = gelf_getsym (data, cnt, &sym_mem);
175 printf ("%5Zu: %*" PRIx64 " %6" PRIx64 " %4d\n",
177 class == ELFCLASS32 ? 8 : 16,
180 GELF_ST_TYPE (sym->st_info));