xref: /onnv-gate/usr/src/cmd/sgs/nm/common/nm.c (revision 12792:1f56a791e275)
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
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * 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  */
211618Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  * Copyright (c) 1989 AT&T
250Sstevel@tonic-gate  * All Rights Reserved
260Sstevel@tonic-gate  *
2712049SAli.Bahrami@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate #include <libelf.h>
360Sstevel@tonic-gate #include <sys/elf_SPARC.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /* exit return codes */
400Sstevel@tonic-gate #define	NOARGS	1
410Sstevel@tonic-gate #define	BADELF	2
420Sstevel@tonic-gate #define	NOALLOC 3
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <fcntl.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <errno.h>
470Sstevel@tonic-gate #include <string.h>
480Sstevel@tonic-gate #include <dlfcn.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "sgs.h"
510Sstevel@tonic-gate #include "conv.h"
520Sstevel@tonic-gate #include "gelf.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct {		/* structure to translate symbol table data */
550Sstevel@tonic-gate 	int  indx;
560Sstevel@tonic-gate 	char *name;
570Sstevel@tonic-gate 	GElf_Addr value;
580Sstevel@tonic-gate 	GElf_Xword size;
590Sstevel@tonic-gate 	int type;
600Sstevel@tonic-gate 	int bind;
610Sstevel@tonic-gate 	unsigned char other;
620Sstevel@tonic-gate 	unsigned int shndx;
630Sstevel@tonic-gate 	unsigned int flags;	/* flags relevant to entry */
640Sstevel@tonic-gate } SYM;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	FLG_SYM_SPECSEC	0x00000001	/* reserved scn index */
670Sstevel@tonic-gate 					/*	(SHN_ABS, SHN_COMMON, ...) */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	UNDEFINED "U"
700Sstevel@tonic-gate #define	BSS_GLOB  "B"
710Sstevel@tonic-gate #define	BSS_WEAK  "B*"
720Sstevel@tonic-gate #define	BSS_LOCL  "b"
730Sstevel@tonic-gate #define	BSS_SECN  ".bss"
740Sstevel@tonic-gate #define	REG_GLOB  "R"
750Sstevel@tonic-gate #define	REG_WEAK  "R*"
760Sstevel@tonic-gate #define	REG_LOCL  "r"
770Sstevel@tonic-gate 
783621Sab196087 #define	OPTSTR	":APDoxhvnursplLCVefgRTt:" /* option string for getopt() */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #define	DATESIZE 60
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	TYPE 7
830Sstevel@tonic-gate #define	BIND 3
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	DEF_MAX_SYM_SIZE 256
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static char *key[TYPE][BIND];
880Sstevel@tonic-gate 
892564Sab196087 /*
902564Sab196087  * Format type used for printing value and size items.
912564Sab196087  * The non-negative values here are used as array indices into
922564Sab196087  * several arrays found below. Renumbering, or adding items,
932564Sab196087  * will require changes to those arrays as well.
942564Sab196087  */
952564Sab196087 typedef enum {
962564Sab196087 	FMT_T_NONE = -1,	/* No format type yet assigned */
972564Sab196087 
982564Sab196087 	/* The following are used as array indices */
992564Sab196087 	FMT_T_DEC = 0,
1002564Sab196087 	FMT_T_HEX = 1,
1012564Sab196087 	FMT_T_OCT = 2
1022564Sab196087 } FMT_T;
1032564Sab196087 
1042564Sab196087 /*
1052564Sab196087  * Determine whether a proposed format type is compatible with the current
1062564Sab196087  * setting. We allow setting the format as long as it hasn't already
1072564Sab196087  * been done, or if the new setting is the same as the current one.
1082564Sab196087  */
1092564Sab196087 #define	COMPAT_FMT_FLAG(new_fmt_flag) \
1102564Sab196087 	(fmt_flag == FMT_T_NONE) || (fmt_flag == new_fmt_flag)
1112564Sab196087 
1122564Sab196087 static FMT_T fmt_flag = FMT_T_NONE;	/* format style to use for value/size */
1132564Sab196087 
1140Sstevel@tonic-gate static  int	/* flags: ?_flag corresponds to ? option */
1150Sstevel@tonic-gate 	h_flag = 0,	/* suppress printing of headings */
1160Sstevel@tonic-gate 	v_flag = 0,	/* sort external symbols by value */
1170Sstevel@tonic-gate 	n_flag = 0,	/* sort external symbols by name */
1180Sstevel@tonic-gate 	u_flag = 0,	/* print only undefined symbols */
1190Sstevel@tonic-gate 	r_flag = 0,	/* prepend object file or archive name */
1200Sstevel@tonic-gate 			/* to each symbol name */
1210Sstevel@tonic-gate 	R_flag = 0,	/* if "-R" issued then prepend archive name, */
1220Sstevel@tonic-gate 			/* object file name to each symbol */
1230Sstevel@tonic-gate 	s_flag = 0,	/* print section name instead of section index */
1240Sstevel@tonic-gate 	p_flag = 0,	/* produce terse output */
1250Sstevel@tonic-gate 	P_flag = 0,	/* Portable format output */
1260Sstevel@tonic-gate 	l_flag = 0,	/* produce long listing of output */
1273621Sab196087 	L_flag = 0,	/* print SUNW_LDYNSYM instead of SYMTAB */
1280Sstevel@tonic-gate 	D_flag = 0,	/* print DYNSYM instead of SYMTAB */
1290Sstevel@tonic-gate 	C_flag = 0,	/* print decoded C++ names */
1302564Sab196087 	A_flag = 0,	/* File name */
1310Sstevel@tonic-gate 	e_flag = 0,	/* -e flag */
1320Sstevel@tonic-gate 	g_flag = 0,	/* -g flag */
1330Sstevel@tonic-gate 	V_flag = 0;	/* print version information */
1340Sstevel@tonic-gate static char A_header[DEF_MAX_SYM_SIZE+1] = {0};
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static char *prog_name;
1370Sstevel@tonic-gate static char *archive_name = (char *)0;
1380Sstevel@tonic-gate static int errflag = 0;
1390Sstevel@tonic-gate static void usage();
1400Sstevel@tonic-gate static void each_file(char *);
1410Sstevel@tonic-gate static void process(Elf *, char *);
1420Sstevel@tonic-gate static Elf_Scn * get_scnfd(Elf *, int, int);
1432352Sab196087 static void get_symtab(Elf *, char *);
1440Sstevel@tonic-gate static SYM * readsyms(Elf_Data *, GElf_Sxword, Elf *, unsigned int,
1450Sstevel@tonic-gate 			unsigned int);
1460Sstevel@tonic-gate static int compare(SYM *, SYM *);
1470Sstevel@tonic-gate static char *lookup(int, int);
1480Sstevel@tonic-gate static int  is_bss_section(unsigned int, Elf *, unsigned int);
1490Sstevel@tonic-gate static void print_ar_files(int, Elf *, char *);
1502352Sab196087 static void print_symtab(Elf *, unsigned int, Elf_Scn *, GElf_Shdr *, char *);
1510Sstevel@tonic-gate static void parsename(char *);
1520Sstevel@tonic-gate static void parse_fn_and_print(const char *, char *);
1530Sstevel@tonic-gate static char d_buf[512];
1540Sstevel@tonic-gate static char p_buf[512];
1559155SAli.Bahrami@Sun.COM static int exotic(const char *s);
1560Sstevel@tonic-gate static void set_A_header(char *);
1577008Sab196087 static char *FormatName(char *, const char *);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * Parses the command line options and then
1630Sstevel@tonic-gate  * calls each_file() to process each file.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])1660Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	char	*optstr = OPTSTR; /* option string used by getopt() */
1692564Sab196087 	int	optchar;
1702564Sab196087 	FMT_T	new_fmt_flag;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate #ifndef	XPG4
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
1750Sstevel@tonic-gate 	 */
1762647Srie 	(void) conv_check_native(argv, envp);
1770Sstevel@tonic-gate #endif
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* table of keyletters for use with -p and -P options */
1800Sstevel@tonic-gate 	key[STT_NOTYPE][STB_LOCAL] = "n";
1810Sstevel@tonic-gate 	key[STT_NOTYPE][STB_GLOBAL] = "N";
1820Sstevel@tonic-gate 	key[STT_NOTYPE][STB_WEAK] = "N*";
1830Sstevel@tonic-gate 	key[STT_OBJECT][STB_LOCAL] = "d";
1840Sstevel@tonic-gate 	key[STT_OBJECT][STB_GLOBAL] = "D";
1850Sstevel@tonic-gate 	key[STT_OBJECT][STB_WEAK] = "D*";
1860Sstevel@tonic-gate 	key[STT_FUNC][STB_LOCAL] = "t";
1870Sstevel@tonic-gate 	key[STT_FUNC][STB_GLOBAL] = "T";
1880Sstevel@tonic-gate 	key[STT_FUNC][STB_WEAK] = "T*";
1890Sstevel@tonic-gate 	key[STT_SECTION][STB_LOCAL] = "s";
1900Sstevel@tonic-gate 	key[STT_SECTION][STB_GLOBAL] = "S";
1910Sstevel@tonic-gate 	key[STT_SECTION][STB_WEAK] = "S*";
1920Sstevel@tonic-gate 	key[STT_FILE][STB_LOCAL] = "f";
1930Sstevel@tonic-gate 	key[STT_FILE][STB_GLOBAL] = "F";
1940Sstevel@tonic-gate 	key[STT_FILE][STB_WEAK] = "F*";
1950Sstevel@tonic-gate 	key[STT_COMMON][STB_LOCAL] = "c";
1960Sstevel@tonic-gate 	key[STT_COMMON][STB_GLOBAL] = "C";
1970Sstevel@tonic-gate 	key[STT_COMMON][STB_WEAK] = "C*";
1980Sstevel@tonic-gate 	key[STT_TLS][STB_LOCAL] = "l";
1990Sstevel@tonic-gate 	key[STT_TLS][STB_GLOBAL] = "L";
2000Sstevel@tonic-gate 	key[STT_TLS][STB_WEAK] = "L*";
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	prog_name = argv[0];
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2050Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2060Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
2110Sstevel@tonic-gate 		switch (optchar) {
2122564Sab196087 		case 'o':	if (COMPAT_FMT_FLAG(FMT_T_OCT))
2132564Sab196087 					fmt_flag = FMT_T_OCT;
2140Sstevel@tonic-gate 				else
2150Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2167008Sab196087 					    "%s: -x or -t set, -o ignored\n"),
2177008Sab196087 					    prog_name);
2180Sstevel@tonic-gate 				break;
2192564Sab196087 		case 'x':	if (COMPAT_FMT_FLAG(FMT_T_HEX))
2202564Sab196087 					fmt_flag = FMT_T_HEX;
2210Sstevel@tonic-gate 				else
2220Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2237008Sab196087 					    "%s: -o or -t set, -x ignored\n"),
2247008Sab196087 					    prog_name);
2250Sstevel@tonic-gate 				break;
2260Sstevel@tonic-gate 		case 'h':	h_flag = 1;
2270Sstevel@tonic-gate 				break;
2280Sstevel@tonic-gate 		case 'v':	if (!n_flag)
2290Sstevel@tonic-gate 					v_flag = 1;
2300Sstevel@tonic-gate 				else
2310Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2327008Sab196087 					    "%s: -n set, -v ignored\n"),
2337008Sab196087 					    prog_name);
2340Sstevel@tonic-gate 				break;
2350Sstevel@tonic-gate 		case 'n':	if (!v_flag)
2360Sstevel@tonic-gate 					n_flag = 1;
2370Sstevel@tonic-gate 				else
2380Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2397008Sab196087 					    "%s: -v set, -n ignored\n"),
2407008Sab196087 					    prog_name);
2410Sstevel@tonic-gate 				break;
2420Sstevel@tonic-gate 		case 'u':	if (!e_flag && !g_flag)
2430Sstevel@tonic-gate 					u_flag = 1;
2440Sstevel@tonic-gate 				else
2450Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2467008Sab196087 					    "%s: -e or -g set, -u ignored\n"),
2477008Sab196087 					    prog_name);
2480Sstevel@tonic-gate 				break;
2490Sstevel@tonic-gate 		case 'e':	if (!u_flag && !g_flag)
2500Sstevel@tonic-gate 					e_flag = 1;
2510Sstevel@tonic-gate 				else
2520Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2537008Sab196087 					    "%s: -u or -g set, -e ignored\n"),
2547008Sab196087 					    prog_name);
2550Sstevel@tonic-gate 				break;
2560Sstevel@tonic-gate 		case 'g':	if (!u_flag && !e_flag)
2570Sstevel@tonic-gate 					g_flag = 1;
2580Sstevel@tonic-gate 				else
2590Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2607008Sab196087 					    "%s: -u or -e set, -g ignored\n"),
2617008Sab196087 					    prog_name);
2620Sstevel@tonic-gate 				break;
2630Sstevel@tonic-gate 		case 'r': 	if (R_flag) {
2640Sstevel@tonic-gate 					R_flag = 0;
2650Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2667008Sab196087 					    "%s: -r set, -R ignored\n"),
2677008Sab196087 					    prog_name);
2680Sstevel@tonic-gate 				}
2690Sstevel@tonic-gate 				r_flag = 1;
2700Sstevel@tonic-gate 				break;
2710Sstevel@tonic-gate 		case 's':	s_flag = 1;
2720Sstevel@tonic-gate 				break;
2730Sstevel@tonic-gate 		case 'p':	if (P_flag == 1) {
2740Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2757008Sab196087 					    "nm: -P set. -p ignored\n"));
2760Sstevel@tonic-gate 				} else
2770Sstevel@tonic-gate 					p_flag = 1;
2780Sstevel@tonic-gate 				break;
2790Sstevel@tonic-gate 		case 'P':	if (p_flag == 1) {
2800Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2817008Sab196087 					    "nm: -p set. -P ignored\n"));
2820Sstevel@tonic-gate 				} else
2830Sstevel@tonic-gate 					P_flag = 1;
2840Sstevel@tonic-gate 				break;
2850Sstevel@tonic-gate 		case 'l':	l_flag = 1;
2860Sstevel@tonic-gate 				break;
2873621Sab196087 		case 'L':	if (D_flag == 1) {
2883621Sab196087 					(void) fprintf(stderr, gettext(
2897008Sab196087 					    "nm: -D set. -L ignored\n"));
2903621Sab196087 				} else
2913621Sab196087 					L_flag = 1;
2923621Sab196087 				break;
2933621Sab196087 		case 'D':	if (L_flag == 1) {
2943621Sab196087 					(void) fprintf(stderr, gettext(
2957008Sab196087 					    "nm: -L set. -D ignored\n"));
2963621Sab196087 				} else
2973621Sab196087 					D_flag = 1;
2980Sstevel@tonic-gate 				break;
2990Sstevel@tonic-gate 		case 'C':
3000Sstevel@tonic-gate 				C_flag = 1;
3010Sstevel@tonic-gate 				break;
3020Sstevel@tonic-gate 		case 'A':	A_flag = 1;
3030Sstevel@tonic-gate 				break;
3040Sstevel@tonic-gate 		case 'V':	V_flag = 1;
3057008Sab196087 				(void) fprintf(stderr, "nm: %s %s\n",
3067008Sab196087 				    (const char *)SGU_PKG,
3077008Sab196087 				    (const char *)SGU_REL);
3080Sstevel@tonic-gate 				break;
3090Sstevel@tonic-gate 		case 'f':	/* -f is a noop, see man page */
3100Sstevel@tonic-gate 				break;
3110Sstevel@tonic-gate 		case 'R':	if (!r_flag)
3120Sstevel@tonic-gate 					R_flag = 1;
3130Sstevel@tonic-gate 				else
3140Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3157008Sab196087 					    "%s: -r set, -R ignored\n"),
3167008Sab196087 					    prog_name);
3170Sstevel@tonic-gate 				break;
3180Sstevel@tonic-gate 		case 'T':
3190Sstevel@tonic-gate 				break;
3202564Sab196087 		case 't':	if (strcmp(optarg, "o") == 0) {
3212564Sab196087 					new_fmt_flag = FMT_T_OCT;
3222564Sab196087 				} else if (strcmp(optarg, "d") == 0) {
3232564Sab196087 					new_fmt_flag = FMT_T_DEC;
3242564Sab196087 				} else if (strcmp(optarg, "x") == 0) {
3252564Sab196087 					new_fmt_flag = FMT_T_HEX;
3262564Sab196087 				} else {
3272564Sab196087 					new_fmt_flag = FMT_T_NONE;
3282564Sab196087 				}
3292564Sab196087 				if (new_fmt_flag == FMT_T_NONE) {
33012049SAli.Bahrami@Sun.COM 					errflag += 1;
3312564Sab196087 					(void) fprintf(stderr, gettext(
33212049SAli.Bahrami@Sun.COM "nm: -t requires radix value (d, o, x): %s\n"), optarg);
3332564Sab196087 				} else if (COMPAT_FMT_FLAG(new_fmt_flag)) {
3342564Sab196087 					fmt_flag = new_fmt_flag;
3352564Sab196087 				} else {
3360Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3370Sstevel@tonic-gate 				"nm: -t or -o or -x set. -t ignored.\n"));
3380Sstevel@tonic-gate 				}
3390Sstevel@tonic-gate 				break;
3400Sstevel@tonic-gate 		case ':':	errflag += 1;
3410Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3427008Sab196087 				    "nm: %c requires operand\n"), optopt);
3430Sstevel@tonic-gate 				break;
3440Sstevel@tonic-gate 		case '?':	errflag += 1;
3450Sstevel@tonic-gate 				break;
3460Sstevel@tonic-gate 		default:	break;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (errflag || (optind >= argc)) {
3510Sstevel@tonic-gate 		if (!(V_flag && (argc == 2))) {
3520Sstevel@tonic-gate 			usage();
3530Sstevel@tonic-gate 			exit(NOARGS);
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3572564Sab196087 	/*
3582564Sab196087 	 * If no explicit format style was specified, set the default
3592564Sab196087 	 * here. In general, the default is for value and size items
3602564Sab196087 	 * to be displayed in decimal format. The exception is that
3612564Sab196087 	 * the default for -P is hexidecimal.
3622564Sab196087 	 */
3632564Sab196087 	if (fmt_flag == FMT_T_NONE)
3642564Sab196087 		fmt_flag = P_flag ? FMT_T_HEX : FMT_T_DEC;
3652564Sab196087 
3662564Sab196087 
3670Sstevel@tonic-gate 	while (optind < argc) {
3680Sstevel@tonic-gate 		each_file(argv[optind]);
3690Sstevel@tonic-gate 		optind++;
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	return (errflag);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * Print out a usage message in short form when program is invoked
3760Sstevel@tonic-gate  * with insufficient or no arguments, and in long form when given
3770Sstevel@tonic-gate  * either a ? or an invalid option.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate static void
usage()3800Sstevel@tonic-gate usage()
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
38312382SAli.Bahrami@Oracle.COM "Usage: nm [-ACDhLlnPpRrsTVv] [-efox] [-g | -u] [-t d|o|x] file ...\n"));
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate  * Takes a filename as input.  Test first for a valid version
3880Sstevel@tonic-gate  * of libelf.a and exit on error.  Process each valid file
3890Sstevel@tonic-gate  * or archive given as input on the command line.  Check
3900Sstevel@tonic-gate  * for file type.  If it is an archive, call print_ar_files
3910Sstevel@tonic-gate  * to process each member of the archive in the same manner
3920Sstevel@tonic-gate  * as object files on the command line.  The same tests for
3930Sstevel@tonic-gate  * valid object file type apply to regular archive members.
3940Sstevel@tonic-gate  * If it is an ELF object file, process it; otherwise
3950Sstevel@tonic-gate  * warn that it is an invalid file type and return from
3960Sstevel@tonic-gate  * processing the file.
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate static void
each_file(char * filename)4000Sstevel@tonic-gate each_file(char *filename)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	Elf	*elf_file;
4030Sstevel@tonic-gate 	int	fd;
4040Sstevel@tonic-gate 	Elf_Kind   file_type;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	struct stat64 buf;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	Elf_Cmd cmd;
4090Sstevel@tonic-gate 	errno = 0;
4100Sstevel@tonic-gate 	if (stat64(filename, &buf) == -1)	{
4117008Sab196087 		(void) fprintf(stderr, "%s: ", prog_name);
4120Sstevel@tonic-gate 		perror(filename);
4130Sstevel@tonic-gate 		errflag++;
4140Sstevel@tonic-gate 		return;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE)	{
4170Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
41812049SAli.Bahrami@Sun.COM 		    "%s: %s: libelf is out of date\n"),
4197008Sab196087 		    prog_name, filename);
4200Sstevel@tonic-gate 		exit(BADELF);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if ((fd = open((filename), O_RDONLY)) == -1) {
4247008Sab196087 		(void) fprintf(stderr, gettext("%s: %s: cannot read file\n"),
4257008Sab196087 		    prog_name, filename);
4260Sstevel@tonic-gate 		errflag++;
4270Sstevel@tonic-gate 		return;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	cmd = ELF_C_READ;
4300Sstevel@tonic-gate 	if ((elf_file = elf_begin(fd, cmd, (Elf *) 0)) == NULL)	{
4310Sstevel@tonic-gate 		(void) fprintf(stderr,
4327008Sab196087 		    "%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
4330Sstevel@tonic-gate 		errflag++;
4340Sstevel@tonic-gate 		(void) close(fd);
4350Sstevel@tonic-gate 		return;
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	file_type = elf_kind(elf_file);
4380Sstevel@tonic-gate 	if (file_type == ELF_K_AR) {
4390Sstevel@tonic-gate 		print_ar_files(fd, elf_file, filename);
4400Sstevel@tonic-gate 	} else {
4410Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
4420Sstevel@tonic-gate #ifndef XPG4
4430Sstevel@tonic-gate 			if (u_flag && !h_flag) {
4440Sstevel@tonic-gate 				/*
4450Sstevel@tonic-gate 				 * u_flag is specified.
4460Sstevel@tonic-gate 				 */
4470Sstevel@tonic-gate 				if (p_flag)
4487008Sab196087 					(void) printf("\n\n%s:\n\n", filename);
4490Sstevel@tonic-gate 				else
4500Sstevel@tonic-gate 					(void) printf(gettext(
4510Sstevel@tonic-gate 				"\n\nUndefined symbols from %s:\n\n"),
4527008Sab196087 					    filename);
4530Sstevel@tonic-gate 			} else if (!h_flag & !P_flag)
4540Sstevel@tonic-gate #else
4550Sstevel@tonic-gate 			if (!h_flag & !P_flag)
4560Sstevel@tonic-gate #endif
4570Sstevel@tonic-gate 			{
4580Sstevel@tonic-gate 				if (p_flag)
4597008Sab196087 					(void) printf("\n\n%s:\n", filename);
4600Sstevel@tonic-gate 				else {
4610Sstevel@tonic-gate 					if (A_flag != 0)
4627008Sab196087 						(void) printf("\n\n%s%s:\n",
4637008Sab196087 						    A_header, filename);
4640Sstevel@tonic-gate 					else
4657008Sab196087 						(void) printf("\n\n%s:\n",
4667008Sab196087 						    filename);
4670Sstevel@tonic-gate 				}
4680Sstevel@tonic-gate 			}
4690Sstevel@tonic-gate 			archive_name = (char *)0;
4700Sstevel@tonic-gate 			process(elf_file, filename);
4710Sstevel@tonic-gate 		} else {
4720Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4737008Sab196087 			    "%s: %s: invalid file type\n"),
4747008Sab196087 			    prog_name, filename);
4750Sstevel@tonic-gate 			errflag++;
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	(void) elf_end(elf_file);
4790Sstevel@tonic-gate 	(void) close(fd);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * Get the ELF header and, if it exists, call get_symtab()
4840Sstevel@tonic-gate  * to begin processing of the file; otherwise, return from
4850Sstevel@tonic-gate  * processing the file with a warning.
4860Sstevel@tonic-gate  */
4870Sstevel@tonic-gate static void
process(Elf * elf_file,char * filename)4880Sstevel@tonic-gate process(Elf *elf_file, char *filename)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	GElf_Ehdr ehdr;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (gelf_getehdr(elf_file, &ehdr) == NULL) {
4930Sstevel@tonic-gate 		(void) fprintf(stderr,
4947008Sab196087 		    "%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
4950Sstevel@tonic-gate 		return;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	set_A_header(filename);
4992352Sab196087 	get_symtab(elf_file, filename);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * Get section descriptor for the associated string table
5040Sstevel@tonic-gate  * and verify that the type of the section pointed to is
5050Sstevel@tonic-gate  * indeed of type STRTAB.  Returns a valid section descriptor
5060Sstevel@tonic-gate  * or NULL on error.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate static Elf_Scn *
get_scnfd(Elf * e_file,int shstrtab,int SCN_TYPE)5090Sstevel@tonic-gate get_scnfd(Elf * e_file, int shstrtab, int SCN_TYPE)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	Elf_Scn	*fd_scn;
5120Sstevel@tonic-gate 	GElf_Shdr shdr;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if ((fd_scn = elf_getscn(e_file, shstrtab)) == NULL) {
5150Sstevel@tonic-gate 		return (NULL);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	(void) gelf_getshdr(fd_scn, &shdr);
5190Sstevel@tonic-gate 	if (shdr.sh_type != SCN_TYPE) {
5200Sstevel@tonic-gate 		return (NULL);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 	return (fd_scn);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Print the symbol table.  This function does not print the contents
5280Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
5290Sstevel@tonic-gate  * print_symtab to print the symbols.  This function does not assume
5300Sstevel@tonic-gate  * that there is only one section of type SYMTAB.  Input is an opened
5310Sstevel@tonic-gate  * ELF file, a pointer to the ELF header, and the filename.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate static void
get_symtab(Elf * elf_file,char * filename)5342352Sab196087 get_symtab(Elf *elf_file, char *filename)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	Elf_Scn	*scn, *scnfd;
5370Sstevel@tonic-gate 	Elf_Data *data;
5380Sstevel@tonic-gate 	GElf_Word symtabtype;
5390Sstevel@tonic-gate 	size_t shstrndx;
5400Sstevel@tonic-gate 
5419900SAli.Bahrami@Sun.COM 	if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) {
5420Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
54312049SAli.Bahrami@Sun.COM 		    "%s: %s: cannot get e_shstrndx\n"),
5440Sstevel@tonic-gate 		    prog_name, filename);
5450Sstevel@tonic-gate 		return;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/* get section header string table */
5490Sstevel@tonic-gate 	scnfd = get_scnfd(elf_file, shstrndx, SHT_STRTAB);
5500Sstevel@tonic-gate 	if (scnfd == NULL) {
5510Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
55212049SAli.Bahrami@Sun.COM 		    "%s: %s: cannot get string table\n"),
5537008Sab196087 		    prog_name, filename);
5540Sstevel@tonic-gate 		return;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	data = elf_getdata(scnfd, NULL);
5580Sstevel@tonic-gate 	if (data->d_size == 0) {
5590Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5607008Sab196087 		    "%s: %s: no data in string table\n"),
5617008Sab196087 		    prog_name, filename);
5620Sstevel@tonic-gate 		return;
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (D_flag)
5660Sstevel@tonic-gate 		symtabtype = SHT_DYNSYM;
5673621Sab196087 	else if (L_flag)
5683621Sab196087 		symtabtype = SHT_SUNW_LDYNSYM;
5690Sstevel@tonic-gate 	else
5700Sstevel@tonic-gate 		symtabtype = SHT_SYMTAB;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	scn = 0;
5730Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0)	{
5740Sstevel@tonic-gate 		GElf_Shdr shdr;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL) {
5777008Sab196087 			(void) fprintf(stderr, "%s: %s: %s:\n",
5787008Sab196087 			    prog_name, filename, elf_errmsg(-1));
5790Sstevel@tonic-gate 			return;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 		if (shdr.sh_type == symtabtype)	{
5832352Sab196087 			print_symtab(elf_file, shstrndx, scn,
5847008Sab196087 			    &shdr, filename);
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 	} /* end while */
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * Process member files of an archive.  This function provides
5910Sstevel@tonic-gate  * a loop through an archive equivalent the processing of
5920Sstevel@tonic-gate  * each_file for individual object files.
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate static void
print_ar_files(int fd,Elf * elf_file,char * filename)5950Sstevel@tonic-gate print_ar_files(int fd, Elf * elf_file, char *filename)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
5980Sstevel@tonic-gate 	Elf	*arf;
5990Sstevel@tonic-gate 	Elf_Cmd    cmd;
6000Sstevel@tonic-gate 	Elf_Kind   file_type;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	cmd = ELF_C_READ;
6040Sstevel@tonic-gate 	archive_name = filename;
6050Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
6060Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
6070Sstevel@tonic-gate 		if (p_ar == NULL) {
6087008Sab196087 			(void) fprintf(stderr, "%s: %s: %s\n",
6097008Sab196087 			    prog_name, filename, elf_errmsg(-1));
6100Sstevel@tonic-gate 			return;
6110Sstevel@tonic-gate 		}
612*12792SAli.Bahrami@Oracle.COM 		if (p_ar->ar_name[0] == '/') {
6130Sstevel@tonic-gate 			cmd = elf_next(arf);
6140Sstevel@tonic-gate 			(void) elf_end(arf);
6150Sstevel@tonic-gate 			continue;
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		if (!h_flag & !P_flag) {
6190Sstevel@tonic-gate 			if (p_flag)
6200Sstevel@tonic-gate 				(void) printf("\n\n%s[%s]:\n",
6217008Sab196087 				    filename, p_ar->ar_name);
6220Sstevel@tonic-gate 			else {
6230Sstevel@tonic-gate 				if (A_flag != 0)
6247008Sab196087 					(void) printf("\n\n%s%s[%s]:\n",
6257008Sab196087 					    A_header, filename, p_ar->ar_name);
6260Sstevel@tonic-gate 				else
6277008Sab196087 					(void) printf("\n\n%s[%s]:\n",
6287008Sab196087 					    filename, p_ar->ar_name);
6290Sstevel@tonic-gate 			}
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 		file_type = elf_kind(arf);
6320Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
6330Sstevel@tonic-gate 			process(arf, p_ar->ar_name);
6340Sstevel@tonic-gate 		} else {
6350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6367008Sab196087 			    "%s: %s: invalid file type\n"),
6377008Sab196087 			    prog_name, p_ar->ar_name);
6380Sstevel@tonic-gate 			cmd = elf_next(arf);
6390Sstevel@tonic-gate 			(void) elf_end(arf);
6400Sstevel@tonic-gate 			errflag++;
6410Sstevel@tonic-gate 			continue;
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		cmd = elf_next(arf);
6450Sstevel@tonic-gate 		(void) elf_end(arf);
6460Sstevel@tonic-gate 	} /* end while */
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6499694SScott.Rotondo@Sun.COM static void print_header(int);
6509694SScott.Rotondo@Sun.COM #ifndef XPG4
6519694SScott.Rotondo@Sun.COM static void print_with_uflag(SYM *, char *);
6529694SScott.Rotondo@Sun.COM #endif
6539694SScott.Rotondo@Sun.COM static void print_with_pflag(int, Elf *, unsigned int, SYM *, char *);
6549694SScott.Rotondo@Sun.COM static void print_with_Pflag(int, Elf *, unsigned int, SYM *);
6559694SScott.Rotondo@Sun.COM static void print_with_otherflags(int, Elf *, unsigned int,
6569694SScott.Rotondo@Sun.COM 		SYM *, char *);
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate  * Print the symbol table according to the flags that were
6590Sstevel@tonic-gate  * set, if any.  Input is an opened ELF file, the section name,
6600Sstevel@tonic-gate  * the section header, the section descriptor, and the filename.
6610Sstevel@tonic-gate  * First get the symbol table with a call to elf_getdata.
6620Sstevel@tonic-gate  * Then translate the symbol table data in memory by calling
6630Sstevel@tonic-gate  * readsyms().  This avoids duplication of function calls
6640Sstevel@tonic-gate  * and improves sorting efficiency.  qsort is used when sorting
6650Sstevel@tonic-gate  * is requested.
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate static void
print_symtab(Elf * elf_file,unsigned int shstrndx,Elf_Scn * p_sd,GElf_Shdr * shdr,char * filename)6682352Sab196087 print_symtab(Elf *elf_file, unsigned int shstrndx,
6690Sstevel@tonic-gate 	Elf_Scn *p_sd, GElf_Shdr *shdr, char *filename)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	Elf_Data * sd;
6730Sstevel@tonic-gate 	SYM	*sym_data;
6740Sstevel@tonic-gate 	SYM	*s;
6750Sstevel@tonic-gate 	GElf_Sxword	count = 0;
6762564Sab196087 	const int ndigits_arr[] = {
6772564Sab196087 		10,		/* FMT_T_DEC */
6782564Sab196087 		8,		/* FMT_T_HEX */
6792564Sab196087 		11,		/* FMT_T_OCT */
6802564Sab196087 	};
6812352Sab196087 	int ndigits;
6822352Sab196087 
6832352Sab196087 	/*
6842352Sab196087 	 * Determine # of digits to use for each numeric value.
6852352Sab196087 	 */
6862564Sab196087 	ndigits = ndigits_arr[fmt_flag];
6872352Sab196087 	if (gelf_getclass(elf_file) == ELFCLASS64)
6882352Sab196087 		ndigits *= 2;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * print header
6920Sstevel@tonic-gate 	 */
6932352Sab196087 	print_header(ndigits);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * get symbol table data
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	if (((sd = elf_getdata(p_sd, NULL)) == NULL) || (sd->d_size == 0)) {
69912049SAli.Bahrami@Sun.COM 		(void) fprintf(stderr,
70012049SAli.Bahrami@Sun.COM 		    gettext("%s: %s: no symbol table data\n"),
7017008Sab196087 		    prog_name, filename);
7020Sstevel@tonic-gate 		return;
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	count = shdr->sh_size / shdr->sh_entsize;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * translate symbol table data
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	sym_data = readsyms(sd, count, elf_file, shdr->sh_link,
7107008Sab196087 	    (unsigned int)elf_ndxscn(p_sd));
7110Sstevel@tonic-gate 	if (sym_data == NULL) {
7120Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7137008Sab196087 		    "%s: %s: problem reading symbol data\n"),
7147008Sab196087 		    prog_name, filename);
7150Sstevel@tonic-gate 		return;
7160Sstevel@tonic-gate 	}
7177008Sab196087 	qsort((char *)sym_data, count-1, sizeof (SYM),
7187008Sab196087 	    (int (*)(const void *, const void *))compare);
7190Sstevel@tonic-gate 	s = sym_data;
7200Sstevel@tonic-gate 	while (count > 1) {
7210Sstevel@tonic-gate #ifndef XPG4
7220Sstevel@tonic-gate 		if (u_flag) {
7230Sstevel@tonic-gate 			/*
7240Sstevel@tonic-gate 			 * U_flag specified
7250Sstevel@tonic-gate 			 */
7260Sstevel@tonic-gate 			print_with_uflag(sym_data, filename);
7270Sstevel@tonic-gate 		} else if (p_flag)
7280Sstevel@tonic-gate #else
7290Sstevel@tonic-gate 		if (p_flag)
7300Sstevel@tonic-gate #endif
7312352Sab196087 			print_with_pflag(ndigits, elf_file, shstrndx,
7327008Sab196087 			    sym_data, filename);
7330Sstevel@tonic-gate 		else if (P_flag)
7342352Sab196087 			print_with_Pflag(ndigits, elf_file, shstrndx,
7357008Sab196087 			    sym_data);
7360Sstevel@tonic-gate 		else
7372352Sab196087 			print_with_otherflags(ndigits, elf_file,
7387008Sab196087 			    shstrndx, sym_data, filename);
7390Sstevel@tonic-gate 		sym_data++;
7400Sstevel@tonic-gate 		count--;
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	free(s);		/* allocated in readsym() */
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate  * Return appropriate keyletter(s) for -p option.
7480Sstevel@tonic-gate  * Returns an index into the key[][] table or NULL if
7490Sstevel@tonic-gate  * the value of the keyletter is unknown.
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate static char *
lookup(int a,int b)7520Sstevel@tonic-gate lookup(int a, int b)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	return (((a < TYPE) && (b < BIND)) ? key[a][b] : NULL);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*
7580Sstevel@tonic-gate  * Return TRUE(1) if the given section is ".bss" for "-p" option.
7590Sstevel@tonic-gate  * Return FALSE(0) if not ".bss" section.
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate static int
is_bss_section(unsigned int shndx,Elf * elf_file,unsigned int shstrndx)7620Sstevel@tonic-gate is_bss_section(unsigned int shndx, Elf * elf_file, unsigned int shstrndx)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	Elf_Scn *scn		= elf_getscn(elf_file, shndx);
7650Sstevel@tonic-gate 	char	*sym_name;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (scn != NULL) {
7680Sstevel@tonic-gate 		GElf_Shdr shdr;
7690Sstevel@tonic-gate 		(void) gelf_getshdr(scn, &shdr);
7707008Sab196087 		sym_name = elf_strptr(elf_file, shstrndx, shdr.sh_name);
7710Sstevel@tonic-gate 		if (strcmp(BSS_SECN, sym_name) == 0)
7720Sstevel@tonic-gate 			return (1);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	return (0);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /*
7780Sstevel@tonic-gate  * Translate symbol table data particularly for sorting.
7790Sstevel@tonic-gate  * Input is the symbol table data structure, number of symbols,
7800Sstevel@tonic-gate  * opened ELF file, and the string table link offset.
7810Sstevel@tonic-gate  */
7820Sstevel@tonic-gate static SYM *
readsyms(Elf_Data * data,GElf_Sxword num,Elf * elf,unsigned int link,unsigned int symscnndx)7830Sstevel@tonic-gate readsyms(Elf_Data * data, GElf_Sxword num, Elf *elf,
7840Sstevel@tonic-gate 	unsigned int link, unsigned int symscnndx)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	SYM		*s, *buf;
7870Sstevel@tonic-gate 	GElf_Sym	sym;
7880Sstevel@tonic-gate 	Elf32_Word	*symshndx = 0;
7890Sstevel@tonic-gate 	unsigned int	nosymshndx = 0;
7900Sstevel@tonic-gate 	int		i;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if ((buf = calloc(num, sizeof (SYM))) == NULL) {
79312049SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, gettext("%s: cannot allocate memory\n"),
79412049SAli.Bahrami@Sun.COM 		    prog_name);
7950Sstevel@tonic-gate 		return (NULL);
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	s = buf;	/* save pointer to head of array */
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	for (i = 1; i < num; i++, buf++) {
8010Sstevel@tonic-gate 		(void) gelf_getsym(data, i, &sym);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		buf->indx = i;
8040Sstevel@tonic-gate 		/* allow to work on machines where NULL-derefs dump core */
8050Sstevel@tonic-gate 		if (sym.st_name == 0)
8060Sstevel@tonic-gate 			buf->name = "";
8070Sstevel@tonic-gate 		else if (C_flag) {
8087008Sab196087 			const char *dn;
8097008Sab196087 			char *name = (char *)elf_strptr(elf, link, sym.st_name);
8107008Sab196087 			dn = conv_demangle_name(name);
8110Sstevel@tonic-gate 			if (strcmp(dn, name) == 0) {	/* Not demangled */
8120Sstevel@tonic-gate 				if (exotic(name)) {
8130Sstevel@tonic-gate 					name = FormatName(name, d_buf);
8140Sstevel@tonic-gate 				}
8150Sstevel@tonic-gate 			} else {  /* name demangled */
8160Sstevel@tonic-gate 				name = FormatName(name, dn);
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 			buf->name = name;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 		else
8217008Sab196087 			buf->name = (char *)elf_strptr(elf, link, sym.st_name);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		buf->value	= sym.st_value;
8240Sstevel@tonic-gate 		buf->size	= sym.st_size;
8250Sstevel@tonic-gate 		buf->type	= GELF_ST_TYPE(sym.st_info);
8260Sstevel@tonic-gate 		buf->bind	= GELF_ST_BIND(sym.st_info);
8270Sstevel@tonic-gate 		buf->other	= sym.st_other;
8280Sstevel@tonic-gate 		if ((sym.st_shndx == SHN_XINDEX) &&
8290Sstevel@tonic-gate 		    (symshndx == 0) && (nosymshndx == 0)) {
8300Sstevel@tonic-gate 			Elf_Scn		*_scn;
8310Sstevel@tonic-gate 			GElf_Shdr	_shdr;
8320Sstevel@tonic-gate 			_scn = 0;
8330Sstevel@tonic-gate 			while ((_scn = elf_nextscn(elf, _scn)) != 0) {
8340Sstevel@tonic-gate 				if (gelf_getshdr(_scn, &_shdr) == 0)
8350Sstevel@tonic-gate 					break;
8360Sstevel@tonic-gate 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
8370Sstevel@tonic-gate 				    (_shdr.sh_link == symscnndx)) {
8380Sstevel@tonic-gate 					Elf_Data	*_data;
8390Sstevel@tonic-gate 					if ((_data = elf_getdata(_scn,
8400Sstevel@tonic-gate 					    0)) != 0) {
8410Sstevel@tonic-gate 						symshndx =
8420Sstevel@tonic-gate 						    (Elf32_Word *)_data->d_buf;
8430Sstevel@tonic-gate 						break;
8440Sstevel@tonic-gate 					}
8450Sstevel@tonic-gate 				}
8460Sstevel@tonic-gate 			}
8470Sstevel@tonic-gate 			nosymshndx = 1;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
8500Sstevel@tonic-gate 			buf->shndx = symshndx[i];
8510Sstevel@tonic-gate 		} else {
8520Sstevel@tonic-gate 			buf->shndx	= sym.st_shndx;
8530Sstevel@tonic-gate 			if (sym.st_shndx >= SHN_LORESERVE)
8540Sstevel@tonic-gate 				buf->flags |= FLG_SYM_SPECSEC;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 	}	/* end for loop */
8570Sstevel@tonic-gate 	return (s);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate  * compare either by name or by value for sorting.
8620Sstevel@tonic-gate  * This is the comparison function called by qsort to
8630Sstevel@tonic-gate  * sort the symbols either by name or value when requested.
8640Sstevel@tonic-gate  */
8650Sstevel@tonic-gate static int
compare(SYM * a,SYM * b)8660Sstevel@tonic-gate compare(SYM *a, SYM *b)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	if (v_flag) {
8690Sstevel@tonic-gate 		if (a->value > b->value)
8700Sstevel@tonic-gate 			return (1);
8710Sstevel@tonic-gate 		else
8720Sstevel@tonic-gate 			return ((a->value == b->value) -1);
8730Sstevel@tonic-gate 	} else
8740Sstevel@tonic-gate 		return ((int)strcoll(a->name, b->name));
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate /*
8780Sstevel@tonic-gate  * Set up a header line for -A option.
8790Sstevel@tonic-gate  */
8800Sstevel@tonic-gate static void
set_A_header(char * fname)8810Sstevel@tonic-gate set_A_header(char *fname)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	if (A_flag == 0)
8840Sstevel@tonic-gate 		return;
8850Sstevel@tonic-gate 
8862352Sab196087 	if (archive_name == (char *)0) {
8872352Sab196087 		(void) snprintf(A_header, sizeof (A_header), "%s: ", fname);
8882352Sab196087 	} else {
8892352Sab196087 		(void) snprintf(A_header, sizeof (A_header), "%s[%s]: ",
8907008Sab196087 		    archive_name, fname);
8912352Sab196087 	}
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate  * output functions
8960Sstevel@tonic-gate  *	The following functions are called from
8970Sstevel@tonic-gate  *	print_symtab().
8980Sstevel@tonic-gate  */
8990Sstevel@tonic-gate 
9002352Sab196087 /*
9012352Sab196087  * Print header line if needed.
9022352Sab196087  *
9032352Sab196087  * entry:
9042352Sab196087  *	ndigits - # of digits to be used to format an integer
9052352Sab196087  *		value, not counting any '0x' (hex) or '0' (octal) prefix.
9062352Sab196087  */
9072352Sab196087 static void
print_header(int ndigits)9082352Sab196087 print_header(int ndigits)
9092352Sab196087 {
9102352Sab196087 	const char *fmt;
9112352Sab196087 	const char *section_title;
9122564Sab196087 	const int pad[] = {	/* Extra prefix characters for format */
9132564Sab196087 		1,		/* FMT_T_DEC: '|' */
9142564Sab196087 		3,		/* FMT_T_HEX: '|0x' */
9152564Sab196087 		2,		/* FMT_T_OCT: '|0' */
9162564Sab196087 	};
9172352Sab196087 	if (
9182352Sab196087 #ifndef XPG4
9192352Sab196087 	    !u_flag &&
9202352Sab196087 #endif
9212352Sab196087 	    !h_flag && !p_flag && !P_flag) {
9220Sstevel@tonic-gate 		(void) printf("\n");
9232352Sab196087 		if (!s_flag) {
9242352Sab196087 			fmt = "%-9s%-*s%-*s%-6s%-6s%-6s%-8s%s\n\n";
9252352Sab196087 			section_title = "Shndx";
9262352Sab196087 		} else {
9272352Sab196087 			fmt = "%-9s%-*s%-*s%-6s%-6s%-6s%-15s%s\n\n";
9282352Sab196087 			section_title = "Shname";
9292352Sab196087 		}
9300Sstevel@tonic-gate 		if (A_flag != 0)
9310Sstevel@tonic-gate 			(void) printf("%s", A_header);
9322564Sab196087 		ndigits += pad[fmt_flag];
9332564Sab196087 		(void) printf(fmt, "[Index]", ndigits, " Value",
9347008Sab196087 		    ndigits, " Size", "Type", "Bind",
9357008Sab196087 		    "Other", section_title, "Name");
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * If the symbol can be printed, then return 1.
9410Sstevel@tonic-gate  * If the symbol can not be printed, then return 0.
9420Sstevel@tonic-gate  */
9430Sstevel@tonic-gate static int
is_sym_print(SYM * sym_data)9440Sstevel@tonic-gate is_sym_print(SYM *sym_data)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * If -u flag is specified,
9480Sstevel@tonic-gate 	 *	the symbol has to be undefined.
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	if (u_flag != 0) {
9510Sstevel@tonic-gate 		if ((sym_data->shndx == SHN_UNDEF) &&
9527008Sab196087 		    (strlen(sym_data->name) != 0))
9530Sstevel@tonic-gate 			return (1);
9540Sstevel@tonic-gate 		else
9550Sstevel@tonic-gate 			return (0);
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * If -e flag is specified,
9600Sstevel@tonic-gate 	 *	the symbol has to be global or static.
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 	if (e_flag != 0) {
9630Sstevel@tonic-gate 		switch (sym_data->type) {
9640Sstevel@tonic-gate 		case STT_NOTYPE:
9650Sstevel@tonic-gate 		case STT_OBJECT:
9660Sstevel@tonic-gate 		case STT_FUNC:
9670Sstevel@tonic-gate 		case STT_COMMON:
9680Sstevel@tonic-gate 		case STT_TLS:
9690Sstevel@tonic-gate 			switch (sym_data->bind) {
9700Sstevel@tonic-gate 			case STB_LOCAL:
9710Sstevel@tonic-gate 			case STB_GLOBAL:
9720Sstevel@tonic-gate 			case STB_WEAK:
9730Sstevel@tonic-gate 				return (1);
9740Sstevel@tonic-gate 			default:
9750Sstevel@tonic-gate 				return (0);
9760Sstevel@tonic-gate 			}
9770Sstevel@tonic-gate 		default:
9780Sstevel@tonic-gate 			return (0);
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * If -g is specified,
9840Sstevel@tonic-gate 	 *	the symbol has to be global.
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	if (g_flag != 0) {
9870Sstevel@tonic-gate 		switch (sym_data->type) {
9880Sstevel@tonic-gate 		case STT_NOTYPE:
9890Sstevel@tonic-gate 		case STT_OBJECT:
9900Sstevel@tonic-gate 		case STT_FUNC:
9910Sstevel@tonic-gate 		case STT_COMMON:
9920Sstevel@tonic-gate 		case STT_TLS:
9930Sstevel@tonic-gate 			switch (sym_data->bind) {
9940Sstevel@tonic-gate 			case STB_GLOBAL:
9950Sstevel@tonic-gate 			case STB_WEAK:
9960Sstevel@tonic-gate 				return (1);
9970Sstevel@tonic-gate 			default:
9980Sstevel@tonic-gate 				return (0);
9990Sstevel@tonic-gate 			}
10000Sstevel@tonic-gate 		default:
10010Sstevel@tonic-gate 			return (0);
10020Sstevel@tonic-gate 		}
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * If it comes here, any symbol can be printed.
10070Sstevel@tonic-gate 	 *	(So basically, -f is no-op.)
10080Sstevel@tonic-gate 	 */
10090Sstevel@tonic-gate 	return (1);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate #ifndef XPG4
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate  * -u flag specified
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate static void
print_with_uflag(SYM * sym_data,char * filename)10170Sstevel@tonic-gate print_with_uflag(
10180Sstevel@tonic-gate 	SYM *sym_data,
10190Sstevel@tonic-gate 	char *filename
10200Sstevel@tonic-gate )
10210Sstevel@tonic-gate {
10227008Sab196087 	if ((sym_data->shndx == SHN_UNDEF) && (strlen(sym_data->name))) {
10230Sstevel@tonic-gate 		if (!r_flag) {
10240Sstevel@tonic-gate 			if (R_flag) {
10250Sstevel@tonic-gate 				if (archive_name != (char *)0)
10267008Sab196087 					(void) printf("   %s:%s:%s\n",
10277008Sab196087 					    archive_name, filename,
10287008Sab196087 					    sym_data->name);
10290Sstevel@tonic-gate 				else
10307008Sab196087 					(void) printf("    %s:%s\n",
10317008Sab196087 					    filename, sym_data->name);
10320Sstevel@tonic-gate 			}
10330Sstevel@tonic-gate 			else
10347008Sab196087 				(void) printf("    %s\n", sym_data->name);
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate 		else
10377008Sab196087 			(void) printf("    %s:%s\n", filename, sym_data->name);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate #endif
104112049SAli.Bahrami@Sun.COM 
10420Sstevel@tonic-gate /*
104312049SAli.Bahrami@Sun.COM  * Print a symbol type representation suitable for the -p or -P formats.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate static void
print_brief_sym_type(Elf * elf_file,unsigned int shstrndx,SYM * sym_data)104612049SAli.Bahrami@Sun.COM print_brief_sym_type(Elf *elf_file, unsigned int shstrndx, SYM *sym_data)
10470Sstevel@tonic-gate {
104812049SAli.Bahrami@Sun.COM 	const char	*sym_key = NULL;
10490Sstevel@tonic-gate 
10507008Sab196087 	if ((sym_data->shndx == SHN_UNDEF) && (strlen(sym_data->name)))
10510Sstevel@tonic-gate 		sym_key = UNDEFINED;
10520Sstevel@tonic-gate 	else if (sym_data->type == STT_SPARC_REGISTER) {
10530Sstevel@tonic-gate 		switch (sym_data->bind) {
10540Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = REG_LOCL;
10550Sstevel@tonic-gate 					break;
10560Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = REG_GLOB;
10570Sstevel@tonic-gate 					break;
10580Sstevel@tonic-gate 			case STB_WEAK   : sym_key = REG_WEAK;
10590Sstevel@tonic-gate 					break;
10600Sstevel@tonic-gate 			default	: sym_key = REG_GLOB;
10610Sstevel@tonic-gate 					break;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 	} else if (((sym_data->flags & FLG_SYM_SPECSEC) == 0) &&
10640Sstevel@tonic-gate 	    is_bss_section((int)sym_data->shndx, elf_file, shstrndx)) {
10650Sstevel@tonic-gate 		switch (sym_data->bind) {
10660Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = BSS_LOCL;
10670Sstevel@tonic-gate 					break;
10680Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = BSS_GLOB;
10690Sstevel@tonic-gate 					break;
10700Sstevel@tonic-gate 			case STB_WEAK   : sym_key = BSS_WEAK;
10710Sstevel@tonic-gate 					break;
10720Sstevel@tonic-gate 			default	: sym_key = BSS_GLOB;
10730Sstevel@tonic-gate 					break;
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 
107612049SAli.Bahrami@Sun.COM 	} else {
107712049SAli.Bahrami@Sun.COM 		sym_key = lookup(sym_data->type, sym_data->bind);
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (sym_key != NULL) {
10810Sstevel@tonic-gate 		if (!l_flag)
10820Sstevel@tonic-gate 			(void) printf("%c ", sym_key[0]);
10830Sstevel@tonic-gate 		else
10840Sstevel@tonic-gate 			(void) printf("%-3s", sym_key);
10850Sstevel@tonic-gate 	} else {
10860Sstevel@tonic-gate 		if (!l_flag)
10870Sstevel@tonic-gate 			(void) printf("%-2d", sym_data->type);
10880Sstevel@tonic-gate 		else
10890Sstevel@tonic-gate 			(void) printf("%-3d", sym_data->type);
10900Sstevel@tonic-gate 	}
109112049SAli.Bahrami@Sun.COM }
109212049SAli.Bahrami@Sun.COM 
109312049SAli.Bahrami@Sun.COM /*
109412049SAli.Bahrami@Sun.COM  * -p flag specified
109512049SAli.Bahrami@Sun.COM  */
109612049SAli.Bahrami@Sun.COM static void
print_with_pflag(int ndigits,Elf * elf_file,unsigned int shstrndx,SYM * sym_data,char * filename)109712049SAli.Bahrami@Sun.COM print_with_pflag(
109812049SAli.Bahrami@Sun.COM 	int ndigits,
109912049SAli.Bahrami@Sun.COM 	Elf *elf_file,
110012049SAli.Bahrami@Sun.COM 	unsigned int shstrndx,
110112049SAli.Bahrami@Sun.COM 	SYM *sym_data,
110212049SAli.Bahrami@Sun.COM 	char *filename
110312049SAli.Bahrami@Sun.COM )
110412049SAli.Bahrami@Sun.COM {
110512049SAli.Bahrami@Sun.COM 	const char * const fmt[] = {
110612049SAli.Bahrami@Sun.COM 		"%.*llu ",	/* FMT_T_DEC */
110712049SAli.Bahrami@Sun.COM 		"0x%.*llx ",	/* FMT_T_HEX */
110812049SAli.Bahrami@Sun.COM 		"0%.*llo "	/* FMT_T_OCT */
110912049SAli.Bahrami@Sun.COM 	};
111012049SAli.Bahrami@Sun.COM 
111112049SAli.Bahrami@Sun.COM 	if (is_sym_print(sym_data) != 1)
111212049SAli.Bahrami@Sun.COM 		return;
111312049SAli.Bahrami@Sun.COM 	/*
111412049SAli.Bahrami@Sun.COM 	 * -A header
111512049SAli.Bahrami@Sun.COM 	 */
111612049SAli.Bahrami@Sun.COM 	if (A_flag != 0)
111712049SAli.Bahrami@Sun.COM 		(void) printf("%s", A_header);
111812049SAli.Bahrami@Sun.COM 
111912049SAli.Bahrami@Sun.COM 	/*
112012049SAli.Bahrami@Sun.COM 	 * Symbol Value.
112112049SAli.Bahrami@Sun.COM 	 *	(hex/octal/decimal)
112212049SAli.Bahrami@Sun.COM 	 */
112312049SAli.Bahrami@Sun.COM 	(void) printf(fmt[fmt_flag], ndigits, EC_ADDR(sym_data->value));
112412049SAli.Bahrami@Sun.COM 
112512049SAli.Bahrami@Sun.COM 
112612049SAli.Bahrami@Sun.COM 	/*
112712049SAli.Bahrami@Sun.COM 	 * Symbol Type.
112812049SAli.Bahrami@Sun.COM 	 */
112912049SAli.Bahrami@Sun.COM 	print_brief_sym_type(elf_file, shstrndx, sym_data);
113012049SAli.Bahrami@Sun.COM 
11310Sstevel@tonic-gate 	if (!r_flag) {
11320Sstevel@tonic-gate 		if (R_flag) {
11330Sstevel@tonic-gate 			if (archive_name != (char *)0)
11347008Sab196087 				(void) printf("%s:%s:%s\n", archive_name,
11357008Sab196087 				    filename, sym_data->name);
11360Sstevel@tonic-gate 			else
11377008Sab196087 				(void) printf("%s:%s\n", filename,
11387008Sab196087 				    sym_data->name);
11390Sstevel@tonic-gate 		}
11400Sstevel@tonic-gate 		else
11410Sstevel@tonic-gate 			(void) printf("%s\n", sym_data->name);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	else
11447008Sab196087 		(void) printf("%s:%s\n", filename, sym_data->name);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * -P flag specified
11490Sstevel@tonic-gate  */
11500Sstevel@tonic-gate static void
print_with_Pflag(int ndigits,Elf * elf_file,unsigned int shstrndx,SYM * sym_data)11510Sstevel@tonic-gate print_with_Pflag(
11522352Sab196087 	int ndigits,
11530Sstevel@tonic-gate 	Elf *elf_file,
11540Sstevel@tonic-gate 	unsigned int shstrndx,
11550Sstevel@tonic-gate 	SYM *sym_data
11560Sstevel@tonic-gate )
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate #define	SYM_LEN 10
11590Sstevel@tonic-gate 	char sym_name[SYM_LEN+1];
11600Sstevel@tonic-gate 	size_t len;
11612564Sab196087 	const char * const fmt[] = {
11622564Sab196087 		"%*llu %*llu \n",	/* FMT_T_DEC */
11632564Sab196087 		"%*llx %*llx \n",	/* FMT_T_HEX */
11642564Sab196087 		"%*llo %*llo \n"	/* FMT_T_OCT */
11652564Sab196087 	};
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	if (is_sym_print(sym_data) != 1)
11680Sstevel@tonic-gate 		return;
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * -A header
11710Sstevel@tonic-gate 	 */
11720Sstevel@tonic-gate 	if (A_flag != 0)
11730Sstevel@tonic-gate 		(void) printf("%s", A_header);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * Symbol name
11770Sstevel@tonic-gate 	 */
11780Sstevel@tonic-gate 	len = strlen(sym_data->name);
11790Sstevel@tonic-gate 	if (len >= SYM_LEN)
11800Sstevel@tonic-gate 		(void) printf("%s ", sym_data->name);
11810Sstevel@tonic-gate 	else {
11820Sstevel@tonic-gate 		(void) sprintf(sym_name, "%-10s", sym_data->name);
11830Sstevel@tonic-gate 		(void) printf("%s ", sym_name);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	/*
11870Sstevel@tonic-gate 	 * Symbol Type.
11880Sstevel@tonic-gate 	 */
118912049SAli.Bahrami@Sun.COM 	print_brief_sym_type(elf_file, shstrndx, sym_data);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * Symbol Value & size
11930Sstevel@tonic-gate 	 *	(hex/octal/decimal)
11940Sstevel@tonic-gate 	 */
11952564Sab196087 	(void) printf(fmt[fmt_flag], ndigits, EC_ADDR(sym_data->value),
11967008Sab196087 	    ndigits, EC_XWORD(sym_data->size));
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate /*
12000Sstevel@tonic-gate  * other flags specified
12010Sstevel@tonic-gate  */
12020Sstevel@tonic-gate static void
print_with_otherflags(int ndigits,Elf * elf_file,unsigned int shstrndx,SYM * sym_data,char * filename)12030Sstevel@tonic-gate print_with_otherflags(
12042352Sab196087 	int ndigits,
12050Sstevel@tonic-gate 	Elf *elf_file,
12060Sstevel@tonic-gate 	unsigned int shstrndx,
12070Sstevel@tonic-gate 	SYM *sym_data,
12080Sstevel@tonic-gate 	char *filename
12090Sstevel@tonic-gate )
12100Sstevel@tonic-gate {
12112564Sab196087 	const char * const fmt_value_size[] = {
12122564Sab196087 		"%*llu|%*lld|",		/* FMT_T_DEC */
12132564Sab196087 		"0x%.*llx|0x%.*llx|",	/* FMT_T_HEX */
12142564Sab196087 		"0%.*llo|0%.*llo|"	/* FMT_T_OCT */
12152564Sab196087 	};
12162564Sab196087 	const char * const fmt_int[] = {
12172564Sab196087 		"%-5d",			/* FMT_T_DEC */
12182564Sab196087 		"%#-5x",		/* FMT_T_HEX */
12192564Sab196087 		"%#-5o"			/* FMT_T_OCT */
12202564Sab196087 	};
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	if (is_sym_print(sym_data) != 1)
12230Sstevel@tonic-gate 		return;
12240Sstevel@tonic-gate 	(void) printf("%s", A_header);
12250Sstevel@tonic-gate 	(void) printf("[%d]\t|", sym_data->indx);
12262564Sab196087 	(void) printf(fmt_value_size[fmt_flag], ndigits,
12277008Sab196087 	    EC_ADDR(sym_data->value), ndigits, EC_XWORD(sym_data->size));
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	switch (sym_data->type) {
12300Sstevel@tonic-gate 	case STT_NOTYPE:(void) printf("%-5s", "NOTY"); break;
12310Sstevel@tonic-gate 	case STT_OBJECT:(void) printf("%-5s", "OBJT"); break;
12320Sstevel@tonic-gate 	case STT_FUNC:	(void) printf("%-5s", "FUNC"); break;
12330Sstevel@tonic-gate 	case STT_SECTION:(void) printf("%-5s", "SECT"); break;
12340Sstevel@tonic-gate 	case STT_FILE:	(void) printf("%-5s", "FILE"); break;
12350Sstevel@tonic-gate 	case STT_COMMON: (void) printf("%-5s", "COMM"); break;
12360Sstevel@tonic-gate 	case STT_TLS:	(void) printf("%-5s", "TLS "); break;
12370Sstevel@tonic-gate 	case STT_SPARC_REGISTER: (void) printf("%-5s", "REGI"); break;
12380Sstevel@tonic-gate 	default:
12392564Sab196087 		(void) printf(fmt_int[fmt_flag], sym_data->type);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	(void) printf("|");
12420Sstevel@tonic-gate 	switch (sym_data->bind) {
12430Sstevel@tonic-gate 	case STB_LOCAL:	(void) printf("%-5s", "LOCL"); break;
12440Sstevel@tonic-gate 	case STB_GLOBAL:(void) printf("%-5s", "GLOB"); break;
12450Sstevel@tonic-gate 	case STB_WEAK:	(void) printf("%-5s", "WEAK"); break;
12460Sstevel@tonic-gate 	default:
12470Sstevel@tonic-gate 		(void) printf("%-5d", sym_data->bind);
12482564Sab196087 		(void) printf(fmt_int[fmt_flag], sym_data->bind);
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 	(void) printf("|");
12512564Sab196087 	(void) printf(fmt_int[fmt_flag], sym_data->other);
12520Sstevel@tonic-gate 	(void)  printf("|");
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	if (sym_data->shndx == SHN_UNDEF) {
12550Sstevel@tonic-gate 		if (!s_flag)
12567008Sab196087 			(void) printf("%-7s", "UNDEF");
12570Sstevel@tonic-gate 		else
12587008Sab196087 			(void) printf("%-14s", "UNDEF");
12590Sstevel@tonic-gate 	} else if (sym_data->shndx == SHN_SUNW_IGNORE) {
12600Sstevel@tonic-gate 		if (!s_flag)
12617008Sab196087 			(void) printf("%-7s", "IGNORE");
12620Sstevel@tonic-gate 		else
12637008Sab196087 			(void) printf("%-14s", "IGNORE");
12640Sstevel@tonic-gate 	} else if ((sym_data->flags & FLG_SYM_SPECSEC) &&
12650Sstevel@tonic-gate 	    (sym_data->shndx == SHN_ABS)) {
12660Sstevel@tonic-gate 		if (!s_flag)
12677008Sab196087 			(void) printf("%-7s", "ABS");
12680Sstevel@tonic-gate 		else
12697008Sab196087 			(void) printf("%-14s", "ABS");
12700Sstevel@tonic-gate 	} else if ((sym_data->flags & FLG_SYM_SPECSEC) &&
12710Sstevel@tonic-gate 	    (sym_data->shndx == SHN_COMMON)) {
12720Sstevel@tonic-gate 		if (!s_flag)
12737008Sab196087 			(void) printf("%-7s", "COMMON");
12740Sstevel@tonic-gate 		else
12757008Sab196087 			(void) printf("%-14s", "COMMON");
12760Sstevel@tonic-gate 	} else {
12772564Sab196087 		if (s_flag) {
12782564Sab196087 			Elf_Scn *scn = elf_getscn(elf_file, sym_data->shndx);
12790Sstevel@tonic-gate 			GElf_Shdr shdr;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			if ((gelf_getshdr(scn, &shdr) != 0) &&
12820Sstevel@tonic-gate 			    (shdr.sh_name != 0)) {
12830Sstevel@tonic-gate 				(void) printf("%-14s",
12847008Sab196087 				    (char *)elf_strptr(elf_file,
12857008Sab196087 				    shstrndx, shdr.sh_name));
12862564Sab196087 			} else {
12872564Sab196087 				(void) printf("%-14d", sym_data->shndx);
12882564Sab196087 			}
12892564Sab196087 		} else {
12902564Sab196087 			(void) printf("%-7d", sym_data->shndx);
12910Sstevel@tonic-gate 		}
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 	(void) printf("|");
12940Sstevel@tonic-gate 	if (!r_flag) {
12950Sstevel@tonic-gate 		if (R_flag) {
12960Sstevel@tonic-gate 			if (archive_name != (char *)0)
12977008Sab196087 				(void) printf("%s:%s:%s\n", archive_name,
12987008Sab196087 				    filename, sym_data->name);
12990Sstevel@tonic-gate 			else
13007008Sab196087 				(void) printf("%s:%s\n", filename,
13017008Sab196087 				    sym_data->name);
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 		else
13040Sstevel@tonic-gate 			(void) printf("%s\n", sym_data->name);
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 	else
13077008Sab196087 		(void) printf("%s:%s\n", filename, sym_data->name);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate /*
13110Sstevel@tonic-gate  * C++ name demangling supporting routines
13120Sstevel@tonic-gate  */
13130Sstevel@tonic-gate static const char *ctor_str = "static constructor function for %s";
13140Sstevel@tonic-gate static const char *dtor_str = "static destructor function for %s";
13150Sstevel@tonic-gate static const char *ptbl_str = "pointer to the virtual table vector for %s";
13160Sstevel@tonic-gate static const char *vtbl_str = "virtual table for %s";
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate /*
13190Sstevel@tonic-gate  * alloc memory and create name in necessary format.
13200Sstevel@tonic-gate  * Return name string
13210Sstevel@tonic-gate  */
13220Sstevel@tonic-gate static char *
FormatName(char * OldName,const char * NewName)13237008Sab196087 FormatName(char *OldName, const char *NewName)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate 	char *s = p_flag ?
13267008Sab196087 	    "%s\n             [%s]" :
13277008Sab196087 	    "%s\n\t\t\t\t\t\t       [%s]";
13280Sstevel@tonic-gate 	size_t length = strlen(s)+strlen(NewName)+strlen(OldName)-3;
13290Sstevel@tonic-gate 	char *hold = OldName;
13300Sstevel@tonic-gate 	OldName = malloc(length);
13312352Sab196087 	/*LINTED*/
13322352Sab196087 	(void) snprintf(OldName, length, s, NewName, hold);
13330Sstevel@tonic-gate 	return (OldName);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate /*
13380Sstevel@tonic-gate  * Return 1 when s is an exotic name, 0 otherwise.  s remains unchanged,
13390Sstevel@tonic-gate  * the exotic name, if exists, is saved in d_buf.
13400Sstevel@tonic-gate  */
13410Sstevel@tonic-gate static int
exotic(const char * in_str)13429155SAli.Bahrami@Sun.COM exotic(const char *in_str)
13430Sstevel@tonic-gate {
13449155SAli.Bahrami@Sun.COM 	static char	*buff = 0;
13459155SAli.Bahrami@Sun.COM 	static size_t	buf_size;
13469155SAli.Bahrami@Sun.COM 
13479155SAli.Bahrami@Sun.COM 	size_t		sym_len = strlen(in_str) + 1;
13489155SAli.Bahrami@Sun.COM 	int		tag = 0;
13499155SAli.Bahrami@Sun.COM 	char		*s;
13509155SAli.Bahrami@Sun.COM 
13519155SAli.Bahrami@Sun.COM 	/*
13529155SAli.Bahrami@Sun.COM 	 * We will need to modify the symbol (in_str) as we are analyzing it,
13539155SAli.Bahrami@Sun.COM 	 * so copy it into a buffer so that we can play around with it.
13549155SAli.Bahrami@Sun.COM 	 */
13559155SAli.Bahrami@Sun.COM 	if (buff == NULL) {
13569155SAli.Bahrami@Sun.COM 		buff = malloc(DEF_MAX_SYM_SIZE);
13579155SAli.Bahrami@Sun.COM 		buf_size = DEF_MAX_SYM_SIZE;
13589155SAli.Bahrami@Sun.COM 	}
13599155SAli.Bahrami@Sun.COM 
13609155SAli.Bahrami@Sun.COM 	if (sym_len > buf_size) {
13619155SAli.Bahrami@Sun.COM 		if (buff)
13629155SAli.Bahrami@Sun.COM 			free(buff);
13639155SAli.Bahrami@Sun.COM 		buff = malloc(sym_len);
13649155SAli.Bahrami@Sun.COM 		buf_size = sym_len;
13659155SAli.Bahrami@Sun.COM 	}
13669155SAli.Bahrami@Sun.COM 
13679155SAli.Bahrami@Sun.COM 	if (buff == NULL) {
13689155SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, gettext(
136912049SAli.Bahrami@Sun.COM 		    "%s: cannot allocate memory\n"), prog_name);
13709155SAli.Bahrami@Sun.COM 		exit(NOALLOC);
13719155SAli.Bahrami@Sun.COM 	}
13729155SAli.Bahrami@Sun.COM 	s = strcpy(buff, in_str);
13739155SAli.Bahrami@Sun.COM 
13749155SAli.Bahrami@Sun.COM 
13750Sstevel@tonic-gate 	if (strncmp(s, "__sti__", 7) == 0) {
13760Sstevel@tonic-gate 		s += 7; tag = 1;
13770Sstevel@tonic-gate 		parse_fn_and_print(ctor_str, s);
13780Sstevel@tonic-gate 	} else if (strncmp(s, "__std__", 7) == 0) {
13790Sstevel@tonic-gate 		s += 7; tag = 1;
13800Sstevel@tonic-gate 		parse_fn_and_print(dtor_str, s);
13810Sstevel@tonic-gate 	} else if (strncmp(s, "__vtbl__", 8) == 0) {
13820Sstevel@tonic-gate 		s += 8; tag = 1;
13830Sstevel@tonic-gate 		parsename(s);
13840Sstevel@tonic-gate 		(void) sprintf(d_buf, vtbl_str, p_buf);
13850Sstevel@tonic-gate 	} else if (strncmp(s, "__ptbl_vec__", 12) == 0) {
13860Sstevel@tonic-gate 		s += 12; tag = 1;
13870Sstevel@tonic-gate 		parse_fn_and_print(ptbl_str, s);
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 	return (tag);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate void
parsename(char * s)13930Sstevel@tonic-gate parsename(char *s)
13940Sstevel@tonic-gate {
13950Sstevel@tonic-gate 	register int len;
13960Sstevel@tonic-gate 	char c, *orig = s;
13970Sstevel@tonic-gate 	*p_buf = '\0';
13980Sstevel@tonic-gate 	(void) strcat(p_buf, "class ");
13990Sstevel@tonic-gate 	while (isdigit(*s)) s++;
14000Sstevel@tonic-gate 	c = *s;
14010Sstevel@tonic-gate 	*s = '\0';
14020Sstevel@tonic-gate 	len = atoi(orig);
14030Sstevel@tonic-gate 	*s = c;
14040Sstevel@tonic-gate 	if (*(s+len) == '\0') { /* only one class name */
14050Sstevel@tonic-gate 		(void) strcat(p_buf, s);
14060Sstevel@tonic-gate 		return;
14070Sstevel@tonic-gate 	} else
14080Sstevel@tonic-gate 	{ /* two classname  %drootname__%dchildname */
14090Sstevel@tonic-gate 		char *root, *child, *child_len_p;
14100Sstevel@tonic-gate 		int child_len;
14110Sstevel@tonic-gate 		root = s;
14120Sstevel@tonic-gate 		child = s + len + 2;
14130Sstevel@tonic-gate 		child_len_p = child;
14140Sstevel@tonic-gate 		if (!isdigit(*child)) {
14150Sstevel@tonic-gate 			/* ptbl file name */
14160Sstevel@tonic-gate 			/*  %drootname__%filename */
14170Sstevel@tonic-gate 			/* kludge for getting rid of '_' in file name */
14180Sstevel@tonic-gate 			char *p;
14190Sstevel@tonic-gate 			c = *(root + len);
14200Sstevel@tonic-gate 			*(root + len) = '\0';
14210Sstevel@tonic-gate 			(void) strcat(p_buf, root);
14220Sstevel@tonic-gate 			*(root + len) = c;
14230Sstevel@tonic-gate 			(void) strcat(p_buf, " in ");
14247008Sab196087 			for (p = child; *p != '_'; ++p)
14257008Sab196087 				;
14260Sstevel@tonic-gate 			c = *p;
14270Sstevel@tonic-gate 			*p = '.';
14280Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14290Sstevel@tonic-gate 			*p = c;
14300Sstevel@tonic-gate 			return;
14310Sstevel@tonic-gate 		}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 		while (isdigit(*child))
14340Sstevel@tonic-gate 			child++;
14350Sstevel@tonic-gate 		c = *child;
14360Sstevel@tonic-gate 		*child = '\0';
14370Sstevel@tonic-gate 		child_len = atoi(child_len_p);
14380Sstevel@tonic-gate 		*child = c;
14390Sstevel@tonic-gate 		if (*(child + child_len) == '\0') {
14400Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14410Sstevel@tonic-gate 			(void) strcat(p_buf, " derived from ");
14420Sstevel@tonic-gate 			c = *(root + len);
14430Sstevel@tonic-gate 			*(root + len) = '\0';
14440Sstevel@tonic-gate 			(void) strcat(p_buf, root);
14450Sstevel@tonic-gate 			*(root + len) = c;
14460Sstevel@tonic-gate 			return;
14470Sstevel@tonic-gate 		} else {
14480Sstevel@tonic-gate 			/* %drootname__%dchildname__filename */
14490Sstevel@tonic-gate 			/* kludge for getting rid of '_' in file name */
14500Sstevel@tonic-gate 			char *p;
14510Sstevel@tonic-gate 			c = *(child + child_len);
14520Sstevel@tonic-gate 			*(child + child_len) = '\0';
14530Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14540Sstevel@tonic-gate 			*(child+child_len) = c;
14550Sstevel@tonic-gate 			(void) strcat(p_buf, " derived from ");
14560Sstevel@tonic-gate 			c = *(root + len);
14570Sstevel@tonic-gate 			*(root + len) = '\0';
14580Sstevel@tonic-gate 			(void) strcat(p_buf, root);
14590Sstevel@tonic-gate 			*(root + len) = c;
14600Sstevel@tonic-gate 			(void) strcat(p_buf, " in ");
14617008Sab196087 			for (p = child + child_len + 2; *p != '_'; ++p)
14627008Sab196087 				;
14630Sstevel@tonic-gate 			c = *p;
14640Sstevel@tonic-gate 			*p = '.';
14650Sstevel@tonic-gate 			(void) strcat(p_buf, child + child_len + 2);
14660Sstevel@tonic-gate 			*p = c;
14670Sstevel@tonic-gate 			return;
14680Sstevel@tonic-gate 		}
14690Sstevel@tonic-gate 	}
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate void
parse_fn_and_print(const char * str,char * s)14730Sstevel@tonic-gate parse_fn_and_print(const char *str, char *s)
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	char		c, *p1, *p2;
14760Sstevel@tonic-gate 	int		yes = 1;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	if ((p1 = p2 =  strstr(s, "_c_")) == NULL)
14790Sstevel@tonic-gate 		if ((p1 = p2 =  strstr(s, "_C_")) == NULL)
14800Sstevel@tonic-gate 			if ((p1 = p2 =  strstr(s, "_cc_")) == NULL)
14810Sstevel@tonic-gate 				if ((p1 = p2 =  strstr(s, "_cxx_")) == NULL)
14827008Sab196087 					if ((p1 = p2 = strstr(s, "_h_")) ==
14837008Sab196087 					    NULL)
14840Sstevel@tonic-gate 			yes = 0;
14850Sstevel@tonic-gate 			else
14860Sstevel@tonic-gate 						p2 += 2;
14870Sstevel@tonic-gate 				else
14880Sstevel@tonic-gate 					p2 += 4;
14890Sstevel@tonic-gate 			else
14900Sstevel@tonic-gate 				p2 += 3;
14910Sstevel@tonic-gate 		else
14920Sstevel@tonic-gate 			p2 += 2;
14930Sstevel@tonic-gate 	else
14940Sstevel@tonic-gate 		p2 += 2;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	if (yes) {
14970Sstevel@tonic-gate 	*p1 = '.';
14980Sstevel@tonic-gate 		c = *p2;
14990Sstevel@tonic-gate 		*p2 = '\0';
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 
15027008Sab196087 	for (s = p1;  *s != '_';  --s)
15037008Sab196087 		;
15040Sstevel@tonic-gate 	++s;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	(void) sprintf(d_buf, str, s);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	if (yes) {
15090Sstevel@tonic-gate 		*p1 = '_';
15100Sstevel@tonic-gate 		*p2 = c;
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate }
1513