xref: /onnv-gate/usr/src/cmd/sgs/size/common/main.c (revision 6951:59445bec7ef4)
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*6951Sab196087  * Common Development and Distribution License (the "License").
6*6951Sab196087  * 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  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1988 AT&T
230Sstevel@tonic-gate  * Copyright (c) 1989 AT&T
240Sstevel@tonic-gate  * All Rights Reserved
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  *
27*6951Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* UNIX HEADERS */
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <libelf.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /* SIZE HEADER */
420Sstevel@tonic-gate #include "defs.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* RELEASE STRING */
450Sstevel@tonic-gate #include "conv.h"
460Sstevel@tonic-gate #include "sgs.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* EXTERNAL VARIABLES DEFINED */
500Sstevel@tonic-gate int		fflag = 0,	/* print full output if -f option is supplied */
510Sstevel@tonic-gate 		Fflag = 0,	/* print full output if -F option is supplied */
520Sstevel@tonic-gate 		nflag = 0; 	/* include NOLOAD sections in size if -n */
530Sstevel@tonic-gate 				/* option  is supplied */
540Sstevel@tonic-gate int		numbase = DECIMAL;
550Sstevel@tonic-gate static int	errflag = 0;	/* Global error flag */
560Sstevel@tonic-gate int		oneflag = 0;
570Sstevel@tonic-gate int		exitcode = 0;   /* Global exit code */
580Sstevel@tonic-gate char		*fname;
590Sstevel@tonic-gate char		*archive;
600Sstevel@tonic-gate int		is_archive = 0;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static char	*tool_name;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void	usagerr();
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	OPTSTR "VoxnfF"		/* option string for usage error message */
670Sstevel@tonic-gate #define	GETOPTSTR "VoxnfF?"	/* option string for getopt */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static Elf	*elf;
700Sstevel@tonic-gate static Elf_Arhdr	*arhdr;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  *  main(argc, argv)
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *  parses the command line
760Sstevel@tonic-gate  *  opens, processes and closes each object file command line argument
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  *  defines:
790Sstevel@tonic-gate  *      - int	numbase = HEX if the -x flag is in the command line
800Sstevel@tonic-gate  *			= OCTAL if the -o flag is in the command line
810Sstevel@tonic-gate  *			= DECIMAL if the -d flag is in the command line
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  *  calls:
840Sstevel@tonic-gate  *      - process(filename) to print the size information in the object file
850Sstevel@tonic-gate  *        filename
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *  prints:
880Sstevel@tonic-gate  *      - an error message if any unknown options appear on the command line
890Sstevel@tonic-gate  *      - a usage message if no object file args appear on the command line
900Sstevel@tonic-gate  *      - an error message if it can't open an object file
910Sstevel@tonic-gate  *	      or if the object file has the wrong magic number
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *  exits 1 - errors found, 0 - no errors
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate int
main(int argc,char ** argv,char ** envp)960Sstevel@tonic-gate main(int argc, char ** argv, char ** envp)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	/* UNIX FUNCTIONS CALLED */
990Sstevel@tonic-gate 	extern	void	error();
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* SIZE FUNCTIONS CALLED */
1020Sstevel@tonic-gate 	extern void process();
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/* EXTERNAL VARIABLES USED */
1050Sstevel@tonic-gate 	extern int	numbase;
1060Sstevel@tonic-gate 	extern int	errflag;
1070Sstevel@tonic-gate 	extern int	oneflag;
1080Sstevel@tonic-gate 	extern int	optind;
1090Sstevel@tonic-gate 	extern char	*fname;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	int c;
1120Sstevel@tonic-gate 	static int	fd;
1130Sstevel@tonic-gate 	extern char	*archive;
1140Sstevel@tonic-gate 	Elf_Cmd		cmd;
1150Sstevel@tonic-gate 	Elf		*arf;
1160Sstevel@tonic-gate 	unsigned	Vflag = 0;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
1200Sstevel@tonic-gate 	 */
121*6951Sab196087 	(void) conv_check_native(argv, envp);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	tool_name = argv[0];
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	while ((c = getopt(argc, argv, GETOPTSTR)) != EOF) {
1260Sstevel@tonic-gate 		switch (c) {
1270Sstevel@tonic-gate 		case 'o':
1280Sstevel@tonic-gate 			if (numbase != HEX)
1290Sstevel@tonic-gate 				numbase = OCTAL;
1300Sstevel@tonic-gate 			else
1310Sstevel@tonic-gate 				(void) fprintf(stderr,
1320Sstevel@tonic-gate 				"size: -x set, -o ignored\n");
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		case 'd':
1360Sstevel@tonic-gate 			numbase = DECIMAL;
1370Sstevel@tonic-gate 			break;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		case 'x':
1400Sstevel@tonic-gate 			if (numbase != OCTAL)
1410Sstevel@tonic-gate 				numbase = HEX;
1420Sstevel@tonic-gate 			else
1430Sstevel@tonic-gate 				(void) fprintf(stderr,
1440Sstevel@tonic-gate 				"size: -o set, -x ignored\n");
1450Sstevel@tonic-gate 			break;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		case 'f':
1480Sstevel@tonic-gate 			fflag++;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		case 'F':
1520Sstevel@tonic-gate 			Fflag++;
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		case 'n':
1560Sstevel@tonic-gate 			nflag++;
1570Sstevel@tonic-gate 			break;
1580Sstevel@tonic-gate 		case 'V':
1590Sstevel@tonic-gate 			(void) fprintf(stderr, "size: %s %s\n",
1600Sstevel@tonic-gate 			    (const char *)SGU_PKG,
1610Sstevel@tonic-gate 			    (const char *)SGU_REL);
1620Sstevel@tonic-gate 			Vflag++;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 		case '?':
1650Sstevel@tonic-gate 			errflag++;
1660Sstevel@tonic-gate 			break;
1670Sstevel@tonic-gate 		default:
1680Sstevel@tonic-gate 			break;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	if (errflag || (optind >= argc)) {
1720Sstevel@tonic-gate 		if (!(Vflag && (argc == 2) && !errflag)) {
1730Sstevel@tonic-gate 			usagerr();
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	if ((argc - optind) == 1) {
1770Sstevel@tonic-gate 		oneflag++;	/* only one file to process */
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
1810Sstevel@tonic-gate 		(void) fprintf(stderr, "size: Libelf is out of date");
1820Sstevel@tonic-gate 		exit(FATAL);	/* library out of date */
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	for (; optind < argc; optind++) {
1860Sstevel@tonic-gate 		fname = argv[optind];
1870Sstevel@tonic-gate 		if ((fd = open(argv[optind], O_RDONLY)) == -1) {
1880Sstevel@tonic-gate 			error(fname, "cannot open");
1890Sstevel@tonic-gate 		} else {
1900Sstevel@tonic-gate 			cmd = ELF_C_READ;
1910Sstevel@tonic-gate 			arf = 0;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 			if ((arf = elf_begin(fd, cmd, arf)) == 0) {
1940Sstevel@tonic-gate 				/* error(fname, "cannot open"); */
1950Sstevel@tonic-gate 				(void) fprintf(stderr,
1960Sstevel@tonic-gate 				"size: %s: %s\n", fname, elf_errmsg(-1));
1970Sstevel@tonic-gate 				return (FATAL);
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 			if (elf_kind(arf) == ELF_K_AR) {
2010Sstevel@tonic-gate 				archive = argv[optind];
2020Sstevel@tonic-gate 			} else {
203*6951Sab196087 				archive = "";
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 			while ((elf = elf_begin(fd, cmd, arf)) != 0) {
207*6951Sab196087 				if ((arhdr = elf_getarhdr(elf)) == 0) {
208*6951Sab196087 					if (elf_kind(arf) == ELF_K_NONE) {
209*6951Sab196087 						/* BEGIN CSTYLED */
210*6951Sab196087 						(void) fprintf(stderr,
211*6951Sab196087 						  "%s: %s: invalid file type\n",
212*6951Sab196087 						    tool_name, fname);
213*6951Sab196087 						/* END CSTYLED */
214*6951Sab196087 						exitcode++;
215*6951Sab196087 						break;
216*6951Sab196087 					} else {
217*6951Sab196087 						process(elf);
218*6951Sab196087 					}
219*6951Sab196087 				} else if (arhdr->ar_name[0] != '/') {
220*6951Sab196087 					fname = arhdr->ar_name;
221*6951Sab196087 					if (elf_kind(arf) == ELF_K_NONE) {
222*6951Sab196087 						/* BEGIN CSTYLED */
223*6951Sab196087 						(void) fprintf(stderr,
224*6951Sab196087 					    "%s: %s[%s]: invalid file type\n",
225*6951Sab196087 						    tool_name, archive, fname);
226*6951Sab196087 						/* END CSTYLED */
227*6951Sab196087 						exitcode++;
228*6951Sab196087 						break;
229*6951Sab196087 					} else {
230*6951Sab196087 						is_archive++;
231*6951Sab196087 						process(elf);
232*6951Sab196087 					}
2330Sstevel@tonic-gate 				}
234*6951Sab196087 				cmd = elf_next(elf);
235*6951Sab196087 				(void) elf_end(elf);
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 			(void) elf_end(arf);
2380Sstevel@tonic-gate 			(void) close(fd);
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 	if (exitcode)
2420Sstevel@tonic-gate 		exit(FATAL);
2430Sstevel@tonic-gate 	else
2440Sstevel@tonic-gate 		exit(0);
2450Sstevel@tonic-gate 	return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate static void
usagerr()2490Sstevel@tonic-gate usagerr()
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	(void) fprintf(stderr,
2520Sstevel@tonic-gate 	"usage: %s [-%s] file(s)...\n", tool_name, OPTSTR);
2530Sstevel@tonic-gate 	exitcode++;
2540Sstevel@tonic-gate }
255