10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9694SScott.Rotondo@Sun.COM * Common Development and Distribution License (the "License").
6*9694SScott.Rotondo@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*9694SScott.Rotondo@Sun.COM /*
22*9694SScott.Rotondo@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23*9694SScott.Rotondo@Sun.COM * Use is subject to license terms.
24*9694SScott.Rotondo@Sun.COM */
25*9694SScott.Rotondo@Sun.COM
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* Copyright (c) 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
310Sstevel@tonic-gate /* UNIX HEADER */
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate /* SIZE HEADERS */
350Sstevel@tonic-gate #include "defs.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /* ELF HEADERS */
380Sstevel@tonic-gate #include "gelf.h"
390Sstevel@tonic-gate
400Sstevel@tonic-gate
410Sstevel@tonic-gate /* SIZE FUNCTIONS CALLED */
420Sstevel@tonic-gate extern void error();
430Sstevel@tonic-gate
440Sstevel@tonic-gate
450Sstevel@tonic-gate /* FORMAT STRINGS */
460Sstevel@tonic-gate
470Sstevel@tonic-gate static const char *prusect[3] = {
480Sstevel@tonic-gate "%llx",
490Sstevel@tonic-gate "%llo",
500Sstevel@tonic-gate "%lld"
510Sstevel@tonic-gate };
520Sstevel@tonic-gate
530Sstevel@tonic-gate static const char *prusum[3] = {
540Sstevel@tonic-gate " = 0x%llx\n",
550Sstevel@tonic-gate " = 0%llo\n",
560Sstevel@tonic-gate " = %lld\n"
570Sstevel@tonic-gate };
580Sstevel@tonic-gate
590Sstevel@tonic-gate static const char *format[3] = {
600Sstevel@tonic-gate "%llx + %llx + %llx = 0x%llx\n",
610Sstevel@tonic-gate "%llo + %llo + %llo = 0%llo\n",
620Sstevel@tonic-gate "%lld + %lld + %lld = %lld\n"
630Sstevel@tonic-gate };
640Sstevel@tonic-gate
65*9694SScott.Rotondo@Sun.COM static void process_phdr(Elf *elf, GElf_Half num);
660Sstevel@tonic-gate
670Sstevel@tonic-gate void
process(Elf * elf)680Sstevel@tonic-gate process(Elf * elf)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate /* EXTERNAL VARIABLES USED */
710Sstevel@tonic-gate extern int fflag, /* full format for sections */
720Sstevel@tonic-gate Fflag, /* full format for segments */
730Sstevel@tonic-gate nflag; /* include non-loadable segments or sections */
740Sstevel@tonic-gate extern int numbase; /* hex, octal, or decimal */
750Sstevel@tonic-gate extern char *fname;
760Sstevel@tonic-gate extern char *archive;
770Sstevel@tonic-gate extern int is_archive;
780Sstevel@tonic-gate extern int oneflag;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* LOCAL VARIABLES */
810Sstevel@tonic-gate GElf_Xword size, /* total size in non-default case for sections */
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * size of first, second, third number and total size
840Sstevel@tonic-gate * in default case for sections.
850Sstevel@tonic-gate */
860Sstevel@tonic-gate first,
870Sstevel@tonic-gate second,
880Sstevel@tonic-gate third,
890Sstevel@tonic-gate totsize;
900Sstevel@tonic-gate GElf_Ehdr ehdr;
910Sstevel@tonic-gate GElf_Shdr shdr;
920Sstevel@tonic-gate Elf_Scn *scn;
930Sstevel@tonic-gate unsigned ndx = 0;
940Sstevel@tonic-gate int numsect = 0;
950Sstevel@tonic-gate int notfirst = 0;
960Sstevel@tonic-gate int i;
970Sstevel@tonic-gate char *name = 0;
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * If there is a program header and the -f flag requesting section infor-
1010Sstevel@tonic-gate * mation is not set, then process segments with the process_phdr function.
1020Sstevel@tonic-gate * Otherwise, process sections. For the default case, the first number
1030Sstevel@tonic-gate * shall be the size of all sections that are allocatable, nonwritable and
1040Sstevel@tonic-gate * not of type NOBITS; the second number shall be the size of all sections
1050Sstevel@tonic-gate * that are allocatable, writable, and not of type NOBITS; the third number
1060Sstevel@tonic-gate * is the size of all sections that are writable and not of type NOBITS.
1070Sstevel@tonic-gate * If -f is set, print the size of each allocatable section, followed by
1080Sstevel@tonic-gate * the section name in parentheses.
1090Sstevel@tonic-gate * If -n is set, print the size of all sections, followed by the section
1100Sstevel@tonic-gate * name in parentheses.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == 0) {
1140Sstevel@tonic-gate error(fname, "invalid file type");
1150Sstevel@tonic-gate return;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate if ((ehdr.e_phnum != 0) && !(fflag)) {
1180Sstevel@tonic-gate process_phdr(elf, ehdr.e_phnum);
1190Sstevel@tonic-gate return;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if (is_archive) {
1230Sstevel@tonic-gate (void) printf("%s[%s]: ", archive, fname);
1240Sstevel@tonic-gate } else if (!oneflag && !is_archive) {
1250Sstevel@tonic-gate (void) printf("%s: ", fname);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate ndx = ehdr.e_shstrndx;
1280Sstevel@tonic-gate scn = 0;
1290Sstevel@tonic-gate size = 0;
1300Sstevel@tonic-gate first = second = third = totsize = 0;
1310Sstevel@tonic-gate if (ehdr.e_shnum == 0) {
1320Sstevel@tonic-gate error(fname, "no section data");
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate numsect = ehdr.e_shnum;
1350Sstevel@tonic-gate for (i = 0; i < numsect; i++) {
1360Sstevel@tonic-gate if ((scn = elf_nextscn(elf, scn)) == 0) {
1370Sstevel@tonic-gate break;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == 0) {
1400Sstevel@tonic-gate error(fname, "could not get section header");
1410Sstevel@tonic-gate break;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate if ((Fflag) && !(fflag)) {
1440Sstevel@tonic-gate error(fname, "no segment data");
1450Sstevel@tonic-gate return;
1460Sstevel@tonic-gate } else if ((!(shdr.sh_flags & SHF_ALLOC)) &&
1470Sstevel@tonic-gate fflag && !(nflag)) {
1480Sstevel@tonic-gate continue;
1490Sstevel@tonic-gate } else if ((!(shdr.sh_flags & SHF_ALLOC)) && !(nflag)) {
1500Sstevel@tonic-gate continue;
1510Sstevel@tonic-gate } else if ((shdr.sh_flags & SHF_ALLOC) &&
1520Sstevel@tonic-gate (!(shdr.sh_flags & SHF_WRITE)) &&
1530Sstevel@tonic-gate (!(shdr.sh_type == SHT_NOBITS)) &&
1540Sstevel@tonic-gate !(fflag) && !(nflag)) {
1550Sstevel@tonic-gate first += shdr.sh_size;
1560Sstevel@tonic-gate } else if ((shdr.sh_flags & SHF_ALLOC) &&
1570Sstevel@tonic-gate (shdr.sh_flags & SHF_WRITE) &&
1580Sstevel@tonic-gate (!(shdr.sh_type == SHT_NOBITS)) &&
1590Sstevel@tonic-gate !(fflag) && !(nflag)) {
1600Sstevel@tonic-gate second += shdr.sh_size;
1610Sstevel@tonic-gate } else if ((shdr.sh_flags & SHF_WRITE) &&
1620Sstevel@tonic-gate (shdr.sh_type == SHT_NOBITS) &&
1630Sstevel@tonic-gate !(fflag) && !(nflag)) {
1640Sstevel@tonic-gate third += shdr.sh_size;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate name = elf_strptr(elf, ndx, (size_t)shdr.sh_name);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (fflag || nflag) {
1690Sstevel@tonic-gate size += shdr.sh_size;
1700Sstevel@tonic-gate if (notfirst) {
1710Sstevel@tonic-gate (void) printf(" + ");
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate (void) printf(prusect[numbase], shdr.sh_size);
1740Sstevel@tonic-gate (void) printf("(%s)", name);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate notfirst++;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate if ((fflag || nflag) && (numsect > 0)) {
1790Sstevel@tonic-gate (void) printf(prusum[numbase], size);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (!fflag && !nflag) {
1830Sstevel@tonic-gate totsize = first + second + third;
1840Sstevel@tonic-gate (void) printf(format[numbase],
1850Sstevel@tonic-gate first, second, third, totsize);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate if (Fflag) {
1890Sstevel@tonic-gate if (ehdr.e_phnum != 0) {
1900Sstevel@tonic-gate process_phdr(elf, ehdr.e_phnum);
1910Sstevel@tonic-gate return;
1920Sstevel@tonic-gate } else {
1930Sstevel@tonic-gate error(fname, "no segment data");
1940Sstevel@tonic-gate return;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * If there is a program exection header, process segments. In the default
2010Sstevel@tonic-gate * case, the first number is the file size of all nonwritable segments
2020Sstevel@tonic-gate * of type PT_LOAD; the second number is the file size of all writable
2030Sstevel@tonic-gate * segments whose type is PT_LOAD; the third number is the memory size
2040Sstevel@tonic-gate * minus the file size of all writable segments of type PT_LOAD.
2050Sstevel@tonic-gate * If the -F flag is set, size will print the memory size of each loadable
2060Sstevel@tonic-gate * segment, followed by its permission flags.
2070Sstevel@tonic-gate * If -n is set, size will print the memory size of all loadable segments
2080Sstevel@tonic-gate * and the file size of all non-loadable segments, followed by their
2090Sstevel@tonic-gate * permission flags.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate static void
process_phdr(Elf * elf,GElf_Half num)2130Sstevel@tonic-gate process_phdr(Elf * elf, GElf_Half num)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate int i;
2160Sstevel@tonic-gate int notfirst = 0;
2170Sstevel@tonic-gate GElf_Phdr p;
2180Sstevel@tonic-gate GElf_Xword memsize,
2190Sstevel@tonic-gate total,
2200Sstevel@tonic-gate First,
2210Sstevel@tonic-gate Second,
2220Sstevel@tonic-gate Third,
2230Sstevel@tonic-gate Totsize;
2240Sstevel@tonic-gate extern int Fflag;
2250Sstevel@tonic-gate extern int nflag;
2260Sstevel@tonic-gate extern int numbase;
2270Sstevel@tonic-gate extern char *fname;
2280Sstevel@tonic-gate extern char *archive;
2290Sstevel@tonic-gate extern int is_archive;
2300Sstevel@tonic-gate extern int oneflag;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate memsize = total = 0;
2330Sstevel@tonic-gate First = Second = Third = Totsize = 0;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate if (is_archive) {
2360Sstevel@tonic-gate (void) printf("%s[%s]: ", archive, fname);
2370Sstevel@tonic-gate } else if (!oneflag && !is_archive) {
2380Sstevel@tonic-gate (void) printf("%s: ", fname);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate for (i = 0; i < (int)num; i++) {
2420Sstevel@tonic-gate if (gelf_getphdr(elf, i, &p) == NULL) {
2430Sstevel@tonic-gate error(fname, "no segment data");
2440Sstevel@tonic-gate return;
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate if ((!(p.p_flags & PF_W)) &&
2470Sstevel@tonic-gate (p.p_type == PT_LOAD) && !(Fflag)) {
2480Sstevel@tonic-gate First += p.p_filesz;
2490Sstevel@tonic-gate } else if ((p.p_flags & PF_W) &&
2500Sstevel@tonic-gate (p.p_type == PT_LOAD) && !(Fflag)) {
2510Sstevel@tonic-gate Second += p.p_filesz;
2520Sstevel@tonic-gate Third += p.p_memsz;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate memsize += p.p_memsz;
2550Sstevel@tonic-gate if ((p.p_type == PT_LOAD) && nflag) {
2560Sstevel@tonic-gate if (notfirst) {
2570Sstevel@tonic-gate (void) printf(" + ");
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate (void) printf(prusect[numbase], p.p_memsz);
2600Sstevel@tonic-gate total += p.p_memsz;
2610Sstevel@tonic-gate notfirst++;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate if (!(p.p_type == PT_LOAD) && nflag) {
2640Sstevel@tonic-gate if (notfirst) {
2650Sstevel@tonic-gate (void) printf(" + ");
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate (void) printf(prusect[numbase], p.p_filesz);
2680Sstevel@tonic-gate total += p.p_filesz;
2690Sstevel@tonic-gate notfirst++;
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate if ((p.p_type == PT_LOAD) && Fflag && !nflag) {
2720Sstevel@tonic-gate if (notfirst) {
2730Sstevel@tonic-gate (void) printf(" + ");
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate (void) printf(prusect[numbase], p.p_memsz);
2760Sstevel@tonic-gate notfirst++;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate if ((Fflag) && !(nflag) && (!(p.p_type == PT_LOAD))) {
2790Sstevel@tonic-gate continue;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate if (Fflag || nflag) {
2820Sstevel@tonic-gate switch (p.p_flags) {
2830Sstevel@tonic-gate case 0: (void) printf("(---)"); break;
2840Sstevel@tonic-gate case PF_X: (void) printf("(--x)"); break;
2850Sstevel@tonic-gate case PF_W: (void) printf("(-w-)"); break;
2860Sstevel@tonic-gate case PF_W+PF_X: (void) printf("(-wx)"); break;
2870Sstevel@tonic-gate case PF_R: (void) printf("(r--)"); break;
2880Sstevel@tonic-gate case PF_R+PF_X: (void) printf("(r-x)"); break;
2890Sstevel@tonic-gate case PF_R+PF_W: (void) printf("(rw-)"); break;
2900Sstevel@tonic-gate case PF_R+PF_W+PF_X: (void) printf("(rwx)"); break;
2910Sstevel@tonic-gate default: (void) printf("flags(%#x)", p.p_flags);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate if (nflag) {
2960Sstevel@tonic-gate (void) printf(prusum[numbase], total);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate if (Fflag && !nflag) {
2990Sstevel@tonic-gate (void) printf(prusum[numbase], memsize);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate if (!Fflag && !nflag) {
3020Sstevel@tonic-gate Totsize = First + Second + (Third - Second);
3030Sstevel@tonic-gate (void) printf(format[numbase],
3040Sstevel@tonic-gate First, Second, Third - Second, Totsize);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
307