1
/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
2
This file is part of Red Hat elfutils.
3
Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Red Hat elfutils is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by the
7
Free Software Foundation; version 2 of the License.
9
Red Hat 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 GNU
12
General Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with Red Hat elfutils; if not, write to the Free Software Foundation,
16
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
Red Hat elfutils is an included package of the Open Invention Network.
19
An included package of the Open Invention Network is a package for which
20
Open Invention Network licensees cross-license their patents. No patent
21
license is granted, either expressly or impliedly, by designation as an
22
included package. Should you wish to participate in the Open Invention
23
Network licensing program, please visit www.openinventionnetwork.com
24
<http://www.openinventionnetwork.com>. */
38
/* Prototypes for local functions. */
39
static int handle_section (Elf *elf, Elf_Scn *scn);
40
static void print_bytes (Elf_Data *data);
41
static void print_symtab (Elf *elf, Elf_Data *data);
45
main (int argc, char *argv[])
54
/* Open the test file. This is given as the first parameter to the
56
fd = open (argv[1], O_RDONLY);
58
error (EXIT_FAILURE, errno, "cannot open input file `%s'", argv[1]);
60
/* Set the library versio we expect. */
61
elf_version (EV_CURRENT);
63
/* Create the ELF descriptor. */
64
elf = elf_begin (fd, ELF_C_READ, NULL);
66
error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
69
/* Now proces all the sections mentioned in the rest of the command line. */
70
for (cnt = 2; cnt < argc; ++cnt)
71
if (handle_section (elf, elf_getscn (elf, atoi (argv[cnt]))) != 0)
72
/* When we encounter an error stop immediately. */
73
error (EXIT_FAILURE, 0, "while processing section %d: %s", cnt,
76
/* Close the descriptor. */
77
if (elf_end (elf) != 0)
78
error (EXIT_FAILURE, 0, "failure while closing ELF descriptor: %s",
86
handle_section (Elf *elf, Elf_Scn *scn)
94
/* First get the ELF and section header. */
95
ehdr = gelf_getehdr (elf, &ehdr_mem);
96
shdr = gelf_getshdr (scn, &shdr_mem);
97
if (ehdr == NULL || shdr == NULL)
100
/* Print the information from the ELF section header. */
101
printf ("name = %s\n"
102
"type = %" PRId32 "\n"
103
"flags = %" PRIx64 "\n"
104
"addr = %" PRIx64 "\n"
105
"offset = %" PRIx64 "\n"
106
"size = %" PRId64 "\n"
107
"link = %" PRId32 "\n"
108
"info = %" PRIx32 "\n"
109
"addralign = %" PRIx64 "\n"
110
"entsize = %" PRId64 "\n",
111
elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
122
/* Get the section data now. */
123
data = elf_getdata (scn, NULL);
127
/* Now proces the different section types accordingly. */
128
switch (shdr->sh_type)
131
print_symtab (elf, data);
140
/* Separate form the next section. */
143
/* All done correctly. */
149
print_bytes (Elf_Data *data)
151
size_t size = data->d_size;
152
off_t offset = data->d_off;
153
unsigned char *buf = (unsigned char *) data->d_buf;
156
for (cnt = 0; cnt < size; cnt += 16)
160
printf ("%*Zx: ", sizeof (size_t) == 4 ? 8 : 16, (size_t) offset + cnt);
162
for (inner = 0; inner < 16 && cnt + inner < size; ++inner)
163
printf (" %02hhx", buf[cnt + inner]);
171
print_symtab (Elf *elf, Elf_Data *data)
173
int class = gelf_getclass (elf);
174
size_t nsym = data->d_size / (class == ELFCLASS32
175
? sizeof (Elf32_Sym) : sizeof (Elf64_Sym));
178
for (cnt = 0; cnt < nsym; ++cnt)
181
GElf_Sym *sym = gelf_getsym (data, cnt, &sym_mem);
183
printf ("%5Zu: %*" PRIx64 " %6" PRIx64 " %4d\n",
185
class == ELFCLASS32 ? 8 : 16,
188
GELF_ST_TYPE (sym->st_info));