1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 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>. */
36 main (int argc, char *argv[])
45 puts ("missing parameter");
49 fd = open (argv[1], O_RDONLY);
52 printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
56 elf_version (EV_CURRENT);
58 elf = elf_begin (fd, ELF_C_READ, NULL);
61 printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
65 if (elf_kind (elf) != ELF_K_ELF)
67 printf ("\"%s\" is not an ELF file\n", argv[1]);
71 if (gelf_getehdr (elf, &ehdr) == NULL)
73 printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
77 printf ("idx type %*s %*s %*s %*s %*s align flags\n",
78 gelf_getclass (elf) == ELFCLASS32 ? 9 : 17, "offset",
79 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "vaddr",
80 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "paddr",
81 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "filesz",
82 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "memsz");
84 for (cnt = 0; cnt < ehdr.e_phnum; ++cnt)
86 static const char *typenames[] =
90 [PT_DYNAMIC] = "DYNAMIC",
91 [PT_INTERP] = "INTERP",
97 GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &mem);
99 const char *p_type = typenames[phdr->p_type];
101 /* If we don't know the name of the type we use the number value. */
102 if (phdr->p_type >= PT_NUM)
104 snprintf (buf, sizeof (buf), "%x", phdr->p_type);
108 printf ("%3d %-7s %#0*llx %#0*llx %#0*llx %#0*llx %#0*llx %#6llx ",
110 gelf_getclass (elf) == ELFCLASS32 ? 9 : 17,
111 (unsigned long long int) phdr->p_offset,
112 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
113 (unsigned long long int) phdr->p_vaddr,
114 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
115 (unsigned long long int) phdr->p_paddr,
116 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
117 (unsigned long long int) phdr->p_filesz,
118 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
119 (unsigned long long int) phdr->p_memsz,
120 (unsigned long long int) phdr->p_align);
122 putc_unlocked ((phdr->p_flags & PF_X) ? 'X' : ' ', stdout);
123 putc_unlocked ((phdr->p_flags & PF_W) ? 'W' : ' ', stdout);
124 putc_unlocked ((phdr->p_flags & PF_R) ? 'R' : ' ', stdout);
126 putc_unlocked ('\n', stdout);
128 if (phdr->p_type == PT_INTERP)
130 /* We can show the user the name of the interpreter. */
132 char *filedata = elf_rawfile (elf, &maxsize);
134 if (filedata != NULL && phdr->p_offset < maxsize)
135 printf ("\t[Requesting program interpreter: %s]\n",
136 filedata + phdr->p_offset);
140 if (elf_end (elf) != 0)
142 printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));