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.
4
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.
8
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.
13
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.
17
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>. */
25
26 #include <config.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <gelf.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 int
main(int argc,char * argv[])36 main (int argc, char *argv[])
37 {
38 Elf *elf;
39 int fd;
40 GElf_Ehdr ehdr;
41 int cnt;
42
43 if (argc < 2)
44 {
45 puts ("missing parameter");
46 exit (1);
47 }
48
49 fd = open (argv[1], O_RDONLY);
50 if (fd == -1)
51 {
52 printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
53 exit (1);
54 }
55
56 elf_version (EV_CURRENT);
57
58 elf = elf_begin (fd, ELF_C_READ, NULL);
59 if (elf == NULL)
60 {
61 printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
62 exit (1);
63 }
64
65 if (elf_kind (elf) != ELF_K_ELF)
66 {
67 printf ("\"%s\" is not an ELF file\n", argv[1]);
68 exit (1);
69 }
70
71 if (gelf_getehdr (elf, &ehdr) == NULL)
72 {
73 printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
74 exit (1);
75 }
76
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");
83
84 for (cnt = 0; cnt < ehdr.e_phnum; ++cnt)
85 {
86 static const char *typenames[] =
87 {
88 [PT_NULL] = "NULL",
89 [PT_LOAD] = "LOAD",
90 [PT_DYNAMIC] = "DYNAMIC",
91 [PT_INTERP] = "INTERP",
92 [PT_NOTE] = "NOTE",
93 [PT_SHLIB] = "SHLIB",
94 [PT_PHDR] = "PHDR"
95 };
96 GElf_Phdr mem;
97 GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &mem);
98 char buf[19];
99 const char *p_type = typenames[phdr->p_type];
100
101 /* If we don't know the name of the type we use the number value. */
102 if (phdr->p_type >= PT_NUM)
103 {
104 snprintf (buf, sizeof (buf), "%x", phdr->p_type);
105 p_type = buf;
106 }
107
108 printf ("%3d %-7s %#0*llx %#0*llx %#0*llx %#0*llx %#0*llx %#6llx ",
109 cnt, p_type,
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);
121
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);
125
126 putc_unlocked ('\n', stdout);
127
128 if (phdr->p_type == PT_INTERP)
129 {
130 /* We can show the user the name of the interpreter. */
131 size_t maxsize;
132 char *filedata = elf_rawfile (elf, &maxsize);
133
134 if (filedata != NULL && phdr->p_offset < maxsize)
135 printf ("\t[Requesting program interpreter: %s]\n",
136 filedata + phdr->p_offset);
137 }
138 }
139
140 if (elf_end (elf) != 0)
141 {
142 printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
143 exit (1);
144 }
145
146 return 0;
147 }
148