blob: a794b0e8821f3eefd0307e6c64fb7532dfb0a78c [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00003 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
4
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000017
18#include <config.h>
19
20#include <errno.h>
21#include <fcntl.h>
22#include <gelf.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27int
28main (int argc, char *argv[])
29{
30 Elf *elf;
31 int fd;
32 GElf_Ehdr ehdr;
33 int cnt;
34
35 if (argc < 2)
36 {
37 puts ("missing parameter");
38 exit (1);
39 }
40
41 fd = open (argv[1], O_RDONLY);
42 if (fd == -1)
43 {
44 printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
45 exit (1);
46 }
47
48 elf_version (EV_CURRENT);
49
50 elf = elf_begin (fd, ELF_C_READ, NULL);
51 if (elf == NULL)
52 {
53 printf ("cannot open ELF file: %s\n", elf_errmsg (-1));
54 exit (1);
55 }
56
57 if (elf_kind (elf) != ELF_K_ELF)
58 {
59 printf ("\"%s\" is not an ELF file\n", argv[1]);
60 exit (1);
61 }
62
63 if (gelf_getehdr (elf, &ehdr) == NULL)
64 {
65 printf ("cannot get the ELF header: %s\n", elf_errmsg (-1));
66 exit (1);
67 }
68
69 printf ("idx type %*s %*s %*s %*s %*s align flags\n",
70 gelf_getclass (elf) == ELFCLASS32 ? 9 : 17, "offset",
71 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "vaddr",
72 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18, "paddr",
73 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "filesz",
74 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12, "memsz");
75
76 for (cnt = 0; cnt < ehdr.e_phnum; ++cnt)
77 {
78 static const char *typenames[] =
79 {
80 [PT_NULL] = "NULL",
81 [PT_LOAD] = "LOAD",
82 [PT_DYNAMIC] = "DYNAMIC",
83 [PT_INTERP] = "INTERP",
84 [PT_NOTE] = "NOTE",
85 [PT_SHLIB] = "SHLIB",
86 [PT_PHDR] = "PHDR"
87 };
88 GElf_Phdr mem;
89 GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &mem);
90 char buf[19];
91 const char *p_type = typenames[phdr->p_type];
92
93 /* If we don't know the name of the type we use the number value. */
94 if (phdr->p_type >= PT_NUM)
95 {
96 snprintf (buf, sizeof (buf), "%x", phdr->p_type);
97 p_type = buf;
98 }
99
100 printf ("%3d %-7s %#0*llx %#0*llx %#0*llx %#0*llx %#0*llx %#6llx ",
101 cnt, p_type,
102 gelf_getclass (elf) == ELFCLASS32 ? 9 : 17,
103 (unsigned long long int) phdr->p_offset,
104 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
105 (unsigned long long int) phdr->p_vaddr,
106 gelf_getclass (elf) == ELFCLASS32 ? 10 : 18,
107 (unsigned long long int) phdr->p_paddr,
108 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
109 (unsigned long long int) phdr->p_filesz,
110 gelf_getclass (elf) == ELFCLASS32 ? 9 : 12,
111 (unsigned long long int) phdr->p_memsz,
112 (unsigned long long int) phdr->p_align);
113
114 putc_unlocked ((phdr->p_flags & PF_X) ? 'X' : ' ', stdout);
115 putc_unlocked ((phdr->p_flags & PF_W) ? 'W' : ' ', stdout);
116 putc_unlocked ((phdr->p_flags & PF_R) ? 'R' : ' ', stdout);
117
118 putc_unlocked ('\n', stdout);
119
120 if (phdr->p_type == PT_INTERP)
121 {
122 /* We can show the user the name of the interpreter. */
123 size_t maxsize;
124 char *filedata = elf_rawfile (elf, &maxsize);
125
126 if (filedata != NULL && phdr->p_offset < maxsize)
127 printf ("\t[Requesting program interpreter: %s]\n",
128 filedata + phdr->p_offset);
129 }
130 }
131
132 if (elf_end (elf) != 0)
133 {
134 printf ("error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
135 exit (1);
136 }
137
138 return 0;
139}