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