xref: /onnv-gate/usr/src/cmd/sgs/nm/common/nm.c (revision 2564:233b9418a61c)
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  *
271618Srie  * Copyright 2006 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 #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <locale.h>
380Sstevel@tonic-gate #include <libelf.h>
390Sstevel@tonic-gate #include <sys/elf_SPARC.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* exit return codes */
430Sstevel@tonic-gate #define	NOARGS	1
440Sstevel@tonic-gate #define	BADELF	2
450Sstevel@tonic-gate #define	NOALLOC 3
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <dlfcn.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "sgs.h"
540Sstevel@tonic-gate #include "conv.h"
550Sstevel@tonic-gate #include "gelf.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate typedef struct {		/* structure to translate symbol table data */
580Sstevel@tonic-gate 	int  indx;
590Sstevel@tonic-gate 	char *name;
600Sstevel@tonic-gate 	GElf_Addr value;
610Sstevel@tonic-gate 	GElf_Xword size;
620Sstevel@tonic-gate 	int type;
630Sstevel@tonic-gate 	int bind;
640Sstevel@tonic-gate 	unsigned char other;
650Sstevel@tonic-gate 	unsigned int shndx;
660Sstevel@tonic-gate 	unsigned int flags;	/* flags relevant to entry */
670Sstevel@tonic-gate } SYM;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	FLG_SYM_SPECSEC	0x00000001	/* reserved scn index */
700Sstevel@tonic-gate 					/*	(SHN_ABS, SHN_COMMON, ...) */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	UNDEFINED "U"
730Sstevel@tonic-gate #define	BSS_GLOB  "B"
740Sstevel@tonic-gate #define	BSS_WEAK  "B*"
750Sstevel@tonic-gate #define	BSS_LOCL  "b"
760Sstevel@tonic-gate #define	BSS_SECN  ".bss"
770Sstevel@tonic-gate #define	REG_GLOB  "R"
780Sstevel@tonic-gate #define	REG_WEAK  "R*"
790Sstevel@tonic-gate #define	REG_LOCL  "r"
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	OPTSTR	":APDoxhvnursplCVefgRTt:" /* option string for getopt() */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	DATESIZE 60
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	TYPE 7
860Sstevel@tonic-gate #define	BIND 3
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	DEF_MAX_SYM_SIZE 256
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static char *key[TYPE][BIND];
910Sstevel@tonic-gate 
92*2564Sab196087 /*
93*2564Sab196087  * Format type used for printing value and size items.
94*2564Sab196087  * The non-negative values here are used as array indices into
95*2564Sab196087  * several arrays found below. Renumbering, or adding items,
96*2564Sab196087  * will require changes to those arrays as well.
97*2564Sab196087  */
98*2564Sab196087 typedef enum {
99*2564Sab196087 	FMT_T_NONE = -1,	/* No format type yet assigned */
100*2564Sab196087 
101*2564Sab196087 	/* The following are used as array indices */
102*2564Sab196087 	FMT_T_DEC = 0,
103*2564Sab196087 	FMT_T_HEX = 1,
104*2564Sab196087 	FMT_T_OCT = 2
105*2564Sab196087 } FMT_T;
106*2564Sab196087 
107*2564Sab196087 /*
108*2564Sab196087  * Determine whether a proposed format type is compatible with the current
109*2564Sab196087  * setting. We allow setting the format as long as it hasn't already
110*2564Sab196087  * been done, or if the new setting is the same as the current one.
111*2564Sab196087  */
112*2564Sab196087 #define	COMPAT_FMT_FLAG(new_fmt_flag) \
113*2564Sab196087 	(fmt_flag == FMT_T_NONE) || (fmt_flag == new_fmt_flag)
114*2564Sab196087 
115*2564Sab196087 static FMT_T fmt_flag = FMT_T_NONE;	/* format style to use for value/size */
116*2564Sab196087 
1170Sstevel@tonic-gate static  int	/* flags: ?_flag corresponds to ? option */
1180Sstevel@tonic-gate 	h_flag = 0,	/* suppress printing of headings */
1190Sstevel@tonic-gate 	v_flag = 0,	/* sort external symbols by value */
1200Sstevel@tonic-gate 	n_flag = 0,	/* sort external symbols by name */
1210Sstevel@tonic-gate 	u_flag = 0,	/* print only undefined symbols */
1220Sstevel@tonic-gate 	r_flag = 0,	/* prepend object file or archive name */
1230Sstevel@tonic-gate 			/* to each symbol name */
1240Sstevel@tonic-gate 	R_flag = 0,	/* if "-R" issued then prepend archive name, */
1250Sstevel@tonic-gate 			/* object file name to each symbol */
1260Sstevel@tonic-gate 	s_flag = 0,	/* print section name instead of section index */
1270Sstevel@tonic-gate 	p_flag = 0,	/* produce terse output */
1280Sstevel@tonic-gate 	P_flag = 0,	/* Portable format output */
1290Sstevel@tonic-gate 	l_flag = 0,	/* produce long listing of output */
1300Sstevel@tonic-gate 	D_flag = 0,	/* print DYNSYM instead of SYMTAB */
1310Sstevel@tonic-gate 	C_flag = 0,	/* print decoded C++ names */
132*2564Sab196087 	A_flag = 0,	/* File name */
1330Sstevel@tonic-gate 	e_flag = 0,	/* -e flag */
1340Sstevel@tonic-gate 	g_flag = 0,	/* -g flag */
1350Sstevel@tonic-gate 	V_flag = 0;	/* print version information */
1360Sstevel@tonic-gate static char A_header[DEF_MAX_SYM_SIZE+1] = {0};
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static char *prog_name;
1390Sstevel@tonic-gate static char *archive_name = (char *)0;
1400Sstevel@tonic-gate static int errflag = 0;
1410Sstevel@tonic-gate static void usage();
1420Sstevel@tonic-gate static void each_file(char *);
1430Sstevel@tonic-gate static void process(Elf *, char *);
1440Sstevel@tonic-gate static Elf_Scn * get_scnfd(Elf *, int, int);
1452352Sab196087 static void get_symtab(Elf *, char *);
1460Sstevel@tonic-gate static SYM * readsyms(Elf_Data *, GElf_Sxword, Elf *, unsigned int,
1470Sstevel@tonic-gate 			unsigned int);
1480Sstevel@tonic-gate static int compare(SYM *, SYM *);
1490Sstevel@tonic-gate static char *lookup(int, int);
1500Sstevel@tonic-gate static int  is_bss_section(unsigned int, Elf *, unsigned int);
1510Sstevel@tonic-gate static void print_ar_files(int, Elf *, char *);
1522352Sab196087 static void print_symtab(Elf *, unsigned int, Elf_Scn *, GElf_Shdr *, char *);
1530Sstevel@tonic-gate static void parsename(char *);
1540Sstevel@tonic-gate static void parse_fn_and_print(const char *, char *);
1550Sstevel@tonic-gate static char d_buf[512];
1560Sstevel@tonic-gate static char p_buf[512];
1570Sstevel@tonic-gate static int exotic(char *s);
1580Sstevel@tonic-gate static void set_A_header(char *);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Parses the command line options and then
1640Sstevel@tonic-gate  * calls each_file() to process each file.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate int
1670Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	char	*optstr = OPTSTR; /* option string used by getopt() */
170*2564Sab196087 	int	optchar;
171*2564Sab196087 	FMT_T	new_fmt_flag;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate #ifndef	XPG4
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	conv_check_native(argv, envp);
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* table of keyletters for use with -p and -P options */
1810Sstevel@tonic-gate 	key[STT_NOTYPE][STB_LOCAL] = "n";
1820Sstevel@tonic-gate 	key[STT_NOTYPE][STB_GLOBAL] = "N";
1830Sstevel@tonic-gate 	key[STT_NOTYPE][STB_WEAK] = "N*";
1840Sstevel@tonic-gate 	key[STT_OBJECT][STB_LOCAL] = "d";
1850Sstevel@tonic-gate 	key[STT_OBJECT][STB_GLOBAL] = "D";
1860Sstevel@tonic-gate 	key[STT_OBJECT][STB_WEAK] = "D*";
1870Sstevel@tonic-gate 	key[STT_FUNC][STB_LOCAL] = "t";
1880Sstevel@tonic-gate 	key[STT_FUNC][STB_GLOBAL] = "T";
1890Sstevel@tonic-gate 	key[STT_FUNC][STB_WEAK] = "T*";
1900Sstevel@tonic-gate 	key[STT_SECTION][STB_LOCAL] = "s";
1910Sstevel@tonic-gate 	key[STT_SECTION][STB_GLOBAL] = "S";
1920Sstevel@tonic-gate 	key[STT_SECTION][STB_WEAK] = "S*";
1930Sstevel@tonic-gate 	key[STT_FILE][STB_LOCAL] = "f";
1940Sstevel@tonic-gate 	key[STT_FILE][STB_GLOBAL] = "F";
1950Sstevel@tonic-gate 	key[STT_FILE][STB_WEAK] = "F*";
1960Sstevel@tonic-gate 	key[STT_COMMON][STB_LOCAL] = "c";
1970Sstevel@tonic-gate 	key[STT_COMMON][STB_GLOBAL] = "C";
1980Sstevel@tonic-gate 	key[STT_COMMON][STB_WEAK] = "C*";
1990Sstevel@tonic-gate 	key[STT_TLS][STB_LOCAL] = "l";
2000Sstevel@tonic-gate 	key[STT_TLS][STB_GLOBAL] = "L";
2010Sstevel@tonic-gate 	key[STT_TLS][STB_WEAK] = "L*";
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	prog_name = argv[0];
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2060Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2070Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	while ((optchar = getopt(argc, argv, optstr)) != -1) {
2120Sstevel@tonic-gate 		switch (optchar) {
213*2564Sab196087 		case 'o':	if (COMPAT_FMT_FLAG(FMT_T_OCT))
214*2564Sab196087 					fmt_flag = FMT_T_OCT;
2150Sstevel@tonic-gate 				else
2160Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
217*2564Sab196087 					"%s: -x or -t set, -o ignored\n"),
2180Sstevel@tonic-gate 					prog_name);
2190Sstevel@tonic-gate 				break;
220*2564Sab196087 		case 'x':	if (COMPAT_FMT_FLAG(FMT_T_HEX))
221*2564Sab196087 					fmt_flag = FMT_T_HEX;
2220Sstevel@tonic-gate 				else
2230Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
224*2564Sab196087 					"%s: -o or -t set, -x ignored\n"),
2250Sstevel@tonic-gate 					prog_name);
2260Sstevel@tonic-gate 				break;
2270Sstevel@tonic-gate 		case 'h':	h_flag = 1;
2280Sstevel@tonic-gate 				break;
2290Sstevel@tonic-gate 		case 'v':	if (!n_flag)
2300Sstevel@tonic-gate 					v_flag = 1;
2310Sstevel@tonic-gate 				else
2320Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2330Sstevel@tonic-gate 					"%s: -n set, -v ignored\n"),
2340Sstevel@tonic-gate 					prog_name);
2350Sstevel@tonic-gate 				break;
2360Sstevel@tonic-gate 		case 'n':	if (!v_flag)
2370Sstevel@tonic-gate 					n_flag = 1;
2380Sstevel@tonic-gate 				else
2390Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2400Sstevel@tonic-gate 					"%s: -v set, -n ignored\n"),
2410Sstevel@tonic-gate 					prog_name);
2420Sstevel@tonic-gate 				break;
2430Sstevel@tonic-gate 		case 'u':	if (!e_flag && !g_flag)
2440Sstevel@tonic-gate 					u_flag = 1;
2450Sstevel@tonic-gate 				else
2460Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2470Sstevel@tonic-gate 					"%s: -e or -g set, -u ignored\n"),
2480Sstevel@tonic-gate 					prog_name);
2490Sstevel@tonic-gate 				break;
2500Sstevel@tonic-gate 		case 'e':	if (!u_flag && !g_flag)
2510Sstevel@tonic-gate 					e_flag = 1;
2520Sstevel@tonic-gate 				else
2530Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2540Sstevel@tonic-gate 					"%s: -u or -g set, -e ignored\n"),
2550Sstevel@tonic-gate 					prog_name);
2560Sstevel@tonic-gate 				break;
2570Sstevel@tonic-gate 		case 'g':	if (!u_flag && !e_flag)
2580Sstevel@tonic-gate 					g_flag = 1;
2590Sstevel@tonic-gate 				else
2600Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2610Sstevel@tonic-gate 					"%s: -u or -e set, -g ignored\n"),
2620Sstevel@tonic-gate 					prog_name);
2630Sstevel@tonic-gate 				break;
2640Sstevel@tonic-gate 		case 'r': 	if (R_flag) {
2650Sstevel@tonic-gate 					R_flag = 0;
2660Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2670Sstevel@tonic-gate 						"%s: -r set, -R ignored\n"),
2680Sstevel@tonic-gate 						prog_name);
2690Sstevel@tonic-gate 				}
2700Sstevel@tonic-gate 				r_flag = 1;
2710Sstevel@tonic-gate 				break;
2720Sstevel@tonic-gate 		case 's':	s_flag = 1;
2730Sstevel@tonic-gate 				break;
2740Sstevel@tonic-gate 		case 'p':	if (P_flag == 1) {
2750Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2760Sstevel@tonic-gate 					"nm: -P set. -p ignored\n"));
2770Sstevel@tonic-gate 				} else
2780Sstevel@tonic-gate 					p_flag = 1;
2790Sstevel@tonic-gate 				break;
2800Sstevel@tonic-gate 		case 'P':	if (p_flag == 1) {
2810Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2820Sstevel@tonic-gate 					"nm: -p set. -P ignored\n"));
2830Sstevel@tonic-gate 				} else
2840Sstevel@tonic-gate 					P_flag = 1;
2850Sstevel@tonic-gate 				break;
2860Sstevel@tonic-gate 		case 'l':	l_flag = 1;
2870Sstevel@tonic-gate 				break;
2880Sstevel@tonic-gate 		case 'D':	D_flag = 1;
2890Sstevel@tonic-gate 				break;
2900Sstevel@tonic-gate 		case 'C':
2910Sstevel@tonic-gate 				C_flag = 1;
2920Sstevel@tonic-gate 				break;
2930Sstevel@tonic-gate 		case 'A':	A_flag = 1;
2940Sstevel@tonic-gate 				break;
2950Sstevel@tonic-gate 		case 'V':	V_flag = 1;
2960Sstevel@tonic-gate 				(void) fprintf(stderr,
2970Sstevel@tonic-gate 					"nm: %s %s\n",
2980Sstevel@tonic-gate 					(const char *)SGU_PKG,
2990Sstevel@tonic-gate 					(const char *)SGU_REL);
3000Sstevel@tonic-gate 				break;
3010Sstevel@tonic-gate 		case 'f':	/* -f is a noop, see man page */
3020Sstevel@tonic-gate 				break;
3030Sstevel@tonic-gate 		case 'R':	if (!r_flag)
3040Sstevel@tonic-gate 					R_flag = 1;
3050Sstevel@tonic-gate 				else
3060Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3070Sstevel@tonic-gate 						"%s: -r set, -R ignored\n"),
3080Sstevel@tonic-gate 						prog_name);
3090Sstevel@tonic-gate 				break;
3100Sstevel@tonic-gate 		case 'T':
3110Sstevel@tonic-gate 				break;
312*2564Sab196087 		case 't':	if (strcmp(optarg, "o") == 0) {
313*2564Sab196087 					new_fmt_flag = FMT_T_OCT;
314*2564Sab196087 				} else if (strcmp(optarg, "d") == 0) {
315*2564Sab196087 					new_fmt_flag = FMT_T_DEC;
316*2564Sab196087 				} else if (strcmp(optarg, "x") == 0) {
317*2564Sab196087 					new_fmt_flag = FMT_T_HEX;
318*2564Sab196087 				} else {
319*2564Sab196087 					new_fmt_flag = FMT_T_NONE;
320*2564Sab196087 				}
321*2564Sab196087 				if (new_fmt_flag == FMT_T_NONE) {
322*2564Sab196087 					(void) fprintf(stderr, gettext(
323*2564Sab196087 "nm: illegal format '%s' for -t is specified. -t ignored.\n"), optarg);
324*2564Sab196087 				} else if (COMPAT_FMT_FLAG(new_fmt_flag)) {
325*2564Sab196087 					fmt_flag = new_fmt_flag;
326*2564Sab196087 				} else {
3270Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3280Sstevel@tonic-gate 				"nm: -t or -o or -x set. -t ignored.\n"));
3290Sstevel@tonic-gate 				}
3300Sstevel@tonic-gate 				break;
3310Sstevel@tonic-gate 		case ':':	errflag += 1;
3320Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3330Sstevel@tonic-gate 					"nm: %c requires operand\n"),
3340Sstevel@tonic-gate 					optopt);
3350Sstevel@tonic-gate 				break;
3360Sstevel@tonic-gate 		case '?':	errflag += 1;
3370Sstevel@tonic-gate 				break;
3380Sstevel@tonic-gate 		default:	break;
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (errflag || (optind >= argc)) {
3430Sstevel@tonic-gate 		if (!(V_flag && (argc == 2))) {
3440Sstevel@tonic-gate 			usage();
3450Sstevel@tonic-gate 			exit(NOARGS);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
349*2564Sab196087 	/*
350*2564Sab196087 	 * If no explicit format style was specified, set the default
351*2564Sab196087 	 * here. In general, the default is for value and size items
352*2564Sab196087 	 * to be displayed in decimal format. The exception is that
353*2564Sab196087 	 * the default for -P is hexidecimal.
354*2564Sab196087 	 */
355*2564Sab196087 	if (fmt_flag == FMT_T_NONE)
356*2564Sab196087 		fmt_flag = P_flag ? FMT_T_HEX : FMT_T_DEC;
357*2564Sab196087 
358*2564Sab196087 
3590Sstevel@tonic-gate 	while (optind < argc) {
3600Sstevel@tonic-gate 		each_file(argv[optind]);
3610Sstevel@tonic-gate 		optind++;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	return (errflag);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Print out a usage message in short form when program is invoked
3680Sstevel@tonic-gate  * with insufficient or no arguments, and in long form when given
3690Sstevel@tonic-gate  * either a ? or an invalid option.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate static void
3720Sstevel@tonic-gate usage()
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3750Sstevel@tonic-gate "Usage: nm [-APvChlnV] [-efox] [-r | -R]  [-g | -u] [-t format] file ...\n"));
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * Takes a filename as input.  Test first for a valid version
3800Sstevel@tonic-gate  * of libelf.a and exit on error.  Process each valid file
3810Sstevel@tonic-gate  * or archive given as input on the command line.  Check
3820Sstevel@tonic-gate  * for file type.  If it is an archive, call print_ar_files
3830Sstevel@tonic-gate  * to process each member of the archive in the same manner
3840Sstevel@tonic-gate  * as object files on the command line.  The same tests for
3850Sstevel@tonic-gate  * valid object file type apply to regular archive members.
3860Sstevel@tonic-gate  * If it is an ELF object file, process it; otherwise
3870Sstevel@tonic-gate  * warn that it is an invalid file type and return from
3880Sstevel@tonic-gate  * processing the file.
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static void
3920Sstevel@tonic-gate each_file(char *filename)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	Elf	*elf_file;
3950Sstevel@tonic-gate 	int	fd;
3960Sstevel@tonic-gate 	Elf_Kind   file_type;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	struct stat64 buf;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	Elf_Cmd cmd;
4010Sstevel@tonic-gate 	errno = 0;
4020Sstevel@tonic-gate 	if (stat64(filename, &buf) == -1)	{
4030Sstevel@tonic-gate 		(void) fprintf(stderr,
4040Sstevel@tonic-gate 			"%s: ", prog_name);
4050Sstevel@tonic-gate 		perror(filename);
4060Sstevel@tonic-gate 		errflag++;
4070Sstevel@tonic-gate 		return;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE)	{
4100Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4110Sstevel@tonic-gate 			"%s: %s: Libelf is out of date\n"),
4120Sstevel@tonic-gate 			prog_name, filename);
4130Sstevel@tonic-gate 		exit(BADELF);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if ((fd = open((filename), O_RDONLY)) == -1) {
4170Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4180Sstevel@tonic-gate 		"%s: %s: cannot read file\n"),
4190Sstevel@tonic-gate 		prog_name, filename);
4200Sstevel@tonic-gate 		errflag++;
4210Sstevel@tonic-gate 		return;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 	cmd = ELF_C_READ;
4240Sstevel@tonic-gate 	if ((elf_file = elf_begin(fd, cmd, (Elf *) 0)) == NULL)	{
4250Sstevel@tonic-gate 		(void) fprintf(stderr,
4260Sstevel@tonic-gate 		"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
4270Sstevel@tonic-gate 		errflag++;
4280Sstevel@tonic-gate 		(void) close(fd);
4290Sstevel@tonic-gate 		return;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	file_type = elf_kind(elf_file);
4320Sstevel@tonic-gate 	if (file_type == ELF_K_AR) {
4330Sstevel@tonic-gate 		print_ar_files(fd, elf_file, filename);
4340Sstevel@tonic-gate 	} else {
4350Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
4360Sstevel@tonic-gate #ifndef XPG4
4370Sstevel@tonic-gate 			if (u_flag && !h_flag) {
4380Sstevel@tonic-gate 				/*
4390Sstevel@tonic-gate 				 * u_flag is specified.
4400Sstevel@tonic-gate 				 */
4410Sstevel@tonic-gate 				if (p_flag)
4420Sstevel@tonic-gate 					(void) printf(
4430Sstevel@tonic-gate 						"\n\n%s:\n\n",
4440Sstevel@tonic-gate 						filename);
4450Sstevel@tonic-gate 				else
4460Sstevel@tonic-gate 					(void) printf(gettext(
4470Sstevel@tonic-gate 				"\n\nUndefined symbols from %s:\n\n"),
4480Sstevel@tonic-gate 				filename);
4490Sstevel@tonic-gate 			} else if (!h_flag & !P_flag)
4500Sstevel@tonic-gate #else
4510Sstevel@tonic-gate 			if (!h_flag & !P_flag)
4520Sstevel@tonic-gate #endif
4530Sstevel@tonic-gate 			{
4540Sstevel@tonic-gate 				if (p_flag)
4550Sstevel@tonic-gate 					(void) printf(
4560Sstevel@tonic-gate 						"\n\n%s:\n",
4570Sstevel@tonic-gate 						filename);
4580Sstevel@tonic-gate 				else {
4590Sstevel@tonic-gate 					if (A_flag != 0)
4600Sstevel@tonic-gate 						(void) printf(
4610Sstevel@tonic-gate 						"\n\n%s%s:\n",
4620Sstevel@tonic-gate 						A_header,
4630Sstevel@tonic-gate 						filename);
4640Sstevel@tonic-gate 					else
4650Sstevel@tonic-gate 						(void) printf(
4660Sstevel@tonic-gate 						"\n\n%s:\n",
4670Sstevel@tonic-gate 						filename);
4680Sstevel@tonic-gate 				}
4690Sstevel@tonic-gate 			}
4700Sstevel@tonic-gate 			archive_name = (char *)0;
4710Sstevel@tonic-gate 			process(elf_file, filename);
4720Sstevel@tonic-gate 		} else {
4730Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4740Sstevel@tonic-gate 				"%s: %s: invalid file type\n"),
4750Sstevel@tonic-gate 				prog_name, filename);
4760Sstevel@tonic-gate 			errflag++;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 	(void) elf_end(elf_file);
4800Sstevel@tonic-gate 	(void) close(fd);
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate  * Get the ELF header and, if it exists, call get_symtab()
4850Sstevel@tonic-gate  * to begin processing of the file; otherwise, return from
4860Sstevel@tonic-gate  * processing the file with a warning.
4870Sstevel@tonic-gate  */
4880Sstevel@tonic-gate static void
4890Sstevel@tonic-gate process(Elf *elf_file, char *filename)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	GElf_Ehdr ehdr;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (gelf_getehdr(elf_file, &ehdr) == NULL) {
4940Sstevel@tonic-gate 		(void) fprintf(stderr,
4950Sstevel@tonic-gate 			"%s: %s: %s\n", prog_name, filename, elf_errmsg(-1));
4960Sstevel@tonic-gate 		return;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	set_A_header(filename);
5002352Sab196087 	get_symtab(elf_file, filename);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * Get section descriptor for the associated string table
5050Sstevel@tonic-gate  * and verify that the type of the section pointed to is
5060Sstevel@tonic-gate  * indeed of type STRTAB.  Returns a valid section descriptor
5070Sstevel@tonic-gate  * or NULL on error.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static Elf_Scn *
5100Sstevel@tonic-gate get_scnfd(Elf * e_file, int shstrtab, int SCN_TYPE)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	Elf_Scn	*fd_scn;
5130Sstevel@tonic-gate 	GElf_Shdr shdr;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((fd_scn = elf_getscn(e_file, shstrtab)) == NULL) {
5160Sstevel@tonic-gate 		return (NULL);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	(void) gelf_getshdr(fd_scn, &shdr);
5200Sstevel@tonic-gate 	if (shdr.sh_type != SCN_TYPE) {
5210Sstevel@tonic-gate 		return (NULL);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	return (fd_scn);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * Print the symbol table.  This function does not print the contents
5290Sstevel@tonic-gate  * of the symbol table but sets up the parameters and then calls
5300Sstevel@tonic-gate  * print_symtab to print the symbols.  This function does not assume
5310Sstevel@tonic-gate  * that there is only one section of type SYMTAB.  Input is an opened
5320Sstevel@tonic-gate  * ELF file, a pointer to the ELF header, and the filename.
5330Sstevel@tonic-gate  */
5340Sstevel@tonic-gate static void
5352352Sab196087 get_symtab(Elf *elf_file, char *filename)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	Elf_Scn	*scn, *scnfd;
5380Sstevel@tonic-gate 	Elf_Data *data;
5390Sstevel@tonic-gate 	GElf_Word symtabtype;
5400Sstevel@tonic-gate 	size_t shstrndx;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (elf_getshstrndx(elf_file, &shstrndx) == 0) {
5430Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5440Sstevel@tonic-gate 		    "%s: %s: could not get e_shstrndx\n"),
5450Sstevel@tonic-gate 		    prog_name, filename);
5460Sstevel@tonic-gate 		return;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/* get section header string table */
5500Sstevel@tonic-gate 	scnfd = get_scnfd(elf_file, shstrndx, SHT_STRTAB);
5510Sstevel@tonic-gate 	if (scnfd == NULL) {
5520Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5530Sstevel@tonic-gate 			"%s: %s: could not get string table\n"),
5540Sstevel@tonic-gate 			prog_name, filename);
5550Sstevel@tonic-gate 		return;
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	data = elf_getdata(scnfd, NULL);
5590Sstevel@tonic-gate 	if (data->d_size == 0) {
5600Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5610Sstevel@tonic-gate 			"%s: %s: no data in string table\n"),
5620Sstevel@tonic-gate 			prog_name, filename);
5630Sstevel@tonic-gate 		return;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (D_flag)
5670Sstevel@tonic-gate 		symtabtype = SHT_DYNSYM;
5680Sstevel@tonic-gate 	else
5690Sstevel@tonic-gate 		symtabtype = SHT_SYMTAB;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	scn = 0;
5720Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf_file, scn)) != 0)	{
5730Sstevel@tonic-gate 		GElf_Shdr shdr;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL) {
5760Sstevel@tonic-gate 			(void) fprintf(stderr,
5770Sstevel@tonic-gate 				"%s: %s: %s:\n",
5780Sstevel@tonic-gate 				prog_name,
5790Sstevel@tonic-gate 				filename, elf_errmsg(-1));
5800Sstevel@tonic-gate 			return;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		if (shdr.sh_type == symtabtype)	{
5842352Sab196087 			print_symtab(elf_file, shstrndx, scn,
5850Sstevel@tonic-gate 				&shdr, filename);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	} /* end while */
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * Process member files of an archive.  This function provides
5920Sstevel@tonic-gate  * a loop through an archive equivalent the processing of
5930Sstevel@tonic-gate  * each_file for individual object files.
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate static void
5960Sstevel@tonic-gate print_ar_files(int fd, Elf * elf_file, char *filename)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	Elf_Arhdr  *p_ar;
5990Sstevel@tonic-gate 	Elf	*arf;
6000Sstevel@tonic-gate 	Elf_Cmd    cmd;
6010Sstevel@tonic-gate 	Elf_Kind   file_type;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	cmd = ELF_C_READ;
6050Sstevel@tonic-gate 	archive_name = filename;
6060Sstevel@tonic-gate 	while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
6070Sstevel@tonic-gate 		p_ar = elf_getarhdr(arf);
6080Sstevel@tonic-gate 		if (p_ar == NULL) {
6090Sstevel@tonic-gate 			(void) fprintf(stderr,
6100Sstevel@tonic-gate 				"%s: %s: %s\n",
6110Sstevel@tonic-gate 				prog_name,
6120Sstevel@tonic-gate 				filename,
6130Sstevel@tonic-gate 				elf_errmsg(-1));
6140Sstevel@tonic-gate 			return;
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 		if ((int)strncmp(p_ar->ar_name, "/", 1) == 0) {
6170Sstevel@tonic-gate 			cmd = elf_next(arf);
6180Sstevel@tonic-gate 			(void) elf_end(arf);
6190Sstevel@tonic-gate 			continue;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		if (!h_flag & !P_flag) {
6230Sstevel@tonic-gate 			if (p_flag)
6240Sstevel@tonic-gate 				(void) printf("\n\n%s[%s]:\n",
6250Sstevel@tonic-gate 				filename, p_ar->ar_name);
6260Sstevel@tonic-gate 			else {
6270Sstevel@tonic-gate 				if (A_flag != 0)
6280Sstevel@tonic-gate 					(void) printf(
6290Sstevel@tonic-gate 					"\n\n%s%s[%s]:\n",
6300Sstevel@tonic-gate 					A_header,
6310Sstevel@tonic-gate 					filename, p_ar->ar_name);
6320Sstevel@tonic-gate 				else
6330Sstevel@tonic-gate 					(void) printf(
6340Sstevel@tonic-gate 					"\n\n%s[%s]:\n",
6350Sstevel@tonic-gate 					filename, p_ar->ar_name);
6360Sstevel@tonic-gate 			}
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 		file_type = elf_kind(arf);
6390Sstevel@tonic-gate 		if (file_type == ELF_K_ELF) {
6400Sstevel@tonic-gate 			process(arf, p_ar->ar_name);
6410Sstevel@tonic-gate 		} else {
6420Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6430Sstevel@tonic-gate 				"%s: %s: invalid file type\n"),
6440Sstevel@tonic-gate 				prog_name, p_ar->ar_name);
6450Sstevel@tonic-gate 			cmd = elf_next(arf);
6460Sstevel@tonic-gate 			(void) elf_end(arf);
6470Sstevel@tonic-gate 			errflag++;
6480Sstevel@tonic-gate 			continue;
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		cmd = elf_next(arf);
6520Sstevel@tonic-gate 		(void) elf_end(arf);
6530Sstevel@tonic-gate 	} /* end while */
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate  * Print the symbol table according to the flags that were
6580Sstevel@tonic-gate  * set, if any.  Input is an opened ELF file, the section name,
6590Sstevel@tonic-gate  * the section header, the section descriptor, and the filename.
6600Sstevel@tonic-gate  * First get the symbol table with a call to elf_getdata.
6610Sstevel@tonic-gate  * Then translate the symbol table data in memory by calling
6620Sstevel@tonic-gate  * readsyms().  This avoids duplication of function calls
6630Sstevel@tonic-gate  * and improves sorting efficiency.  qsort is used when sorting
6640Sstevel@tonic-gate  * is requested.
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate static void
6672352Sab196087 print_symtab(Elf *elf_file, unsigned int shstrndx,
6680Sstevel@tonic-gate 	Elf_Scn *p_sd, GElf_Shdr *shdr, char *filename)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	Elf_Data * sd;
6720Sstevel@tonic-gate 	SYM	*sym_data;
6730Sstevel@tonic-gate 	SYM	*s;
6740Sstevel@tonic-gate 	GElf_Sxword	count = 0;
675*2564Sab196087 	const int ndigits_arr[] = {
676*2564Sab196087 		10,		/* FMT_T_DEC */
677*2564Sab196087 		8,		/* FMT_T_HEX */
678*2564Sab196087 		11,		/* FMT_T_OCT */
679*2564Sab196087 	};
6800Sstevel@tonic-gate 	static void print_header(int);
6812352Sab196087 	int ndigits;
6820Sstevel@tonic-gate #ifndef XPG4
6830Sstevel@tonic-gate 	static void print_with_uflag(SYM *, char *);
6840Sstevel@tonic-gate #endif
6852352Sab196087 	static void print_with_pflag(int, Elf *, unsigned int, SYM *, char *);
6862352Sab196087 	static void print_with_Pflag(int, Elf *, unsigned int, SYM *);
6872352Sab196087 	static void print_with_otherflags(int, Elf *, unsigned int,
6882352Sab196087 		SYM *, char *);
6892352Sab196087 
6902352Sab196087 	/*
6912352Sab196087 	 * Determine # of digits to use for each numeric value.
6922352Sab196087 	 */
693*2564Sab196087 	ndigits = ndigits_arr[fmt_flag];
6942352Sab196087 	if (gelf_getclass(elf_file) == ELFCLASS64)
6952352Sab196087 		ndigits *= 2;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	/*
6980Sstevel@tonic-gate 	 * print header
6990Sstevel@tonic-gate 	 */
7002352Sab196087 	print_header(ndigits);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * get symbol table data
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	if (((sd = elf_getdata(p_sd, NULL)) == NULL) || (sd->d_size == 0)) {
7060Sstevel@tonic-gate 		(void) printf(gettext(
7070Sstevel@tonic-gate 			"%s: %s - No symbol table data\n"),
7080Sstevel@tonic-gate 			prog_name, filename);
7090Sstevel@tonic-gate 		return;
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	count = shdr->sh_size / shdr->sh_entsize;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/*
7140Sstevel@tonic-gate 	 * translate symbol table data
7150Sstevel@tonic-gate 	 */
7160Sstevel@tonic-gate 	sym_data = readsyms(sd, count, elf_file, shdr->sh_link,
7170Sstevel@tonic-gate 		(unsigned int)elf_ndxscn(p_sd));
7180Sstevel@tonic-gate 	if (sym_data == NULL) {
7190Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7200Sstevel@tonic-gate 			"%s: %s: problem reading symbol data\n"),
7210Sstevel@tonic-gate 			prog_name, filename);
7220Sstevel@tonic-gate 		return;
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	qsort((char *)sym_data,
7250Sstevel@tonic-gate 		count-1, sizeof (SYM),
7260Sstevel@tonic-gate 		(int (*)(const void *, const void *))compare);
7270Sstevel@tonic-gate 	s = sym_data;
7280Sstevel@tonic-gate 	while (count > 1) {
7290Sstevel@tonic-gate #ifndef XPG4
7300Sstevel@tonic-gate 		if (u_flag) {
7310Sstevel@tonic-gate 			/*
7320Sstevel@tonic-gate 			 * U_flag specified
7330Sstevel@tonic-gate 			 */
7340Sstevel@tonic-gate 			print_with_uflag(sym_data, filename);
7350Sstevel@tonic-gate 		} else if (p_flag)
7360Sstevel@tonic-gate #else
7370Sstevel@tonic-gate 		if (p_flag)
7380Sstevel@tonic-gate #endif
7392352Sab196087 			print_with_pflag(ndigits, elf_file, shstrndx,
7402352Sab196087 				sym_data, filename);
7410Sstevel@tonic-gate 		else if (P_flag)
7422352Sab196087 			print_with_Pflag(ndigits, elf_file, shstrndx,
7432352Sab196087 				sym_data);
7440Sstevel@tonic-gate 		else
7452352Sab196087 			print_with_otherflags(ndigits, elf_file,
7462352Sab196087 				shstrndx, sym_data, filename);
7470Sstevel@tonic-gate 		sym_data++;
7480Sstevel@tonic-gate 		count--;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	free(s);		/* allocated in readsym() */
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * Return appropriate keyletter(s) for -p option.
7560Sstevel@tonic-gate  * Returns an index into the key[][] table or NULL if
7570Sstevel@tonic-gate  * the value of the keyletter is unknown.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate static char *
7600Sstevel@tonic-gate lookup(int a, int b)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	return (((a < TYPE) && (b < BIND)) ? key[a][b] : NULL);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Return TRUE(1) if the given section is ".bss" for "-p" option.
7670Sstevel@tonic-gate  * Return FALSE(0) if not ".bss" section.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate static int
7700Sstevel@tonic-gate is_bss_section(unsigned int shndx, Elf * elf_file, unsigned int shstrndx)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	Elf_Scn *scn		= elf_getscn(elf_file, shndx);
7730Sstevel@tonic-gate 	char	*sym_name;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if (scn != NULL) {
7760Sstevel@tonic-gate 		GElf_Shdr shdr;
7770Sstevel@tonic-gate 		(void) gelf_getshdr(scn, &shdr);
7780Sstevel@tonic-gate 		sym_name = elf_strptr(elf_file, shstrndx,
7790Sstevel@tonic-gate 			shdr.sh_name);
7800Sstevel@tonic-gate 		if (strcmp(BSS_SECN, sym_name) == 0)
7810Sstevel@tonic-gate 			return (1);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 	return (0);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate /*
7870Sstevel@tonic-gate  * Translate symbol table data particularly for sorting.
7880Sstevel@tonic-gate  * Input is the symbol table data structure, number of symbols,
7890Sstevel@tonic-gate  * opened ELF file, and the string table link offset.
7900Sstevel@tonic-gate  */
7910Sstevel@tonic-gate static SYM *
7920Sstevel@tonic-gate readsyms(Elf_Data * data, GElf_Sxword num, Elf *elf,
7930Sstevel@tonic-gate 	unsigned int link, unsigned int symscnndx)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	static char *FormatName(char *, char *);
7960Sstevel@tonic-gate 	SYM		*s, *buf;
7970Sstevel@tonic-gate 	GElf_Sym	sym;
7980Sstevel@tonic-gate 	Elf32_Word	*symshndx = 0;
7990Sstevel@tonic-gate 	unsigned int	nosymshndx = 0;
8000Sstevel@tonic-gate 	int		i;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if ((buf = calloc(num, sizeof (SYM))) == NULL) {
8030Sstevel@tonic-gate 		(void) fprintf(stderr,
8040Sstevel@tonic-gate 			"%s: cannot calloc space\n", prog_name);
8050Sstevel@tonic-gate 		return (NULL);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	s = buf;	/* save pointer to head of array */
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	for (i = 1; i < num; i++, buf++) {
8110Sstevel@tonic-gate 		(void) gelf_getsym(data, i, &sym);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		buf->indx = i;
8140Sstevel@tonic-gate 		/* allow to work on machines where NULL-derefs dump core */
8150Sstevel@tonic-gate 		if (sym.st_name == 0)
8160Sstevel@tonic-gate 			buf->name = "";
8170Sstevel@tonic-gate 		else if (C_flag) {
8180Sstevel@tonic-gate 			char *dn;
8190Sstevel@tonic-gate 			char *name = (char *)elf_strptr(elf, link,
8200Sstevel@tonic-gate 					sym.st_name);
8210Sstevel@tonic-gate 			dn = sgs_demangle(name);
8220Sstevel@tonic-gate 			if (strcmp(dn, name) == 0) {	/* Not demangled */
8230Sstevel@tonic-gate 				if (exotic(name)) {
8240Sstevel@tonic-gate 					name = FormatName(name, d_buf);
8250Sstevel@tonic-gate 				}
8260Sstevel@tonic-gate 			} else {  /* name demangled */
8270Sstevel@tonic-gate 				name = FormatName(name, dn);
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 			buf->name = name;
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 		else
8320Sstevel@tonic-gate 			buf->name = (char *)elf_strptr(elf,
8330Sstevel@tonic-gate 					link,
8340Sstevel@tonic-gate 					sym.st_name);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		buf->value	= sym.st_value;
8370Sstevel@tonic-gate 		buf->size	= sym.st_size;
8380Sstevel@tonic-gate 		buf->type	= GELF_ST_TYPE(sym.st_info);
8390Sstevel@tonic-gate 		buf->bind	= GELF_ST_BIND(sym.st_info);
8400Sstevel@tonic-gate 		buf->other	= sym.st_other;
8410Sstevel@tonic-gate 		if ((sym.st_shndx == SHN_XINDEX) &&
8420Sstevel@tonic-gate 		    (symshndx == 0) && (nosymshndx == 0)) {
8430Sstevel@tonic-gate 			Elf_Scn		*_scn;
8440Sstevel@tonic-gate 			GElf_Shdr	_shdr;
8450Sstevel@tonic-gate 			_scn = 0;
8460Sstevel@tonic-gate 			while ((_scn = elf_nextscn(elf, _scn)) != 0) {
8470Sstevel@tonic-gate 				if (gelf_getshdr(_scn, &_shdr) == 0)
8480Sstevel@tonic-gate 					break;
8490Sstevel@tonic-gate 				if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
8500Sstevel@tonic-gate 				    (_shdr.sh_link == symscnndx)) {
8510Sstevel@tonic-gate 					Elf_Data	*_data;
8520Sstevel@tonic-gate 					if ((_data = elf_getdata(_scn,
8530Sstevel@tonic-gate 					    0)) != 0) {
8540Sstevel@tonic-gate 						symshndx =
8550Sstevel@tonic-gate 						    (Elf32_Word *)_data->d_buf;
8560Sstevel@tonic-gate 						break;
8570Sstevel@tonic-gate 					}
8580Sstevel@tonic-gate 				}
8590Sstevel@tonic-gate 			}
8600Sstevel@tonic-gate 			nosymshndx = 1;
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 		if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
8630Sstevel@tonic-gate 			buf->shndx = symshndx[i];
8640Sstevel@tonic-gate 		} else {
8650Sstevel@tonic-gate 			buf->shndx	= sym.st_shndx;
8660Sstevel@tonic-gate 			if (sym.st_shndx >= SHN_LORESERVE)
8670Sstevel@tonic-gate 				buf->flags |= FLG_SYM_SPECSEC;
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 	}	/* end for loop */
8700Sstevel@tonic-gate 	return (s);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * compare either by name or by value for sorting.
8750Sstevel@tonic-gate  * This is the comparison function called by qsort to
8760Sstevel@tonic-gate  * sort the symbols either by name or value when requested.
8770Sstevel@tonic-gate  */
8780Sstevel@tonic-gate static int
8790Sstevel@tonic-gate compare(SYM *a, SYM *b)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 	if (v_flag) {
8820Sstevel@tonic-gate 		if (a->value > b->value)
8830Sstevel@tonic-gate 			return (1);
8840Sstevel@tonic-gate 		else
8850Sstevel@tonic-gate 			return ((a->value == b->value) -1);
8860Sstevel@tonic-gate 	} else
8870Sstevel@tonic-gate 		return ((int)strcoll(a->name, b->name));
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * Set up a header line for -A option.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate static void
8940Sstevel@tonic-gate set_A_header(char *fname)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	if (A_flag == 0)
8970Sstevel@tonic-gate 		return;
8980Sstevel@tonic-gate 
8992352Sab196087 	if (archive_name == (char *)0) {
9002352Sab196087 		(void) snprintf(A_header, sizeof (A_header), "%s: ", fname);
9012352Sab196087 	} else {
9022352Sab196087 		(void) snprintf(A_header, sizeof (A_header), "%s[%s]: ",
9032352Sab196087 			archive_name, fname);
9042352Sab196087 	}
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * output functions
9090Sstevel@tonic-gate  *	The following functions are called from
9100Sstevel@tonic-gate  *	print_symtab().
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate 
9132352Sab196087 /*
9142352Sab196087  * Print header line if needed.
9152352Sab196087  *
9162352Sab196087  * entry:
9172352Sab196087  *	ndigits - # of digits to be used to format an integer
9182352Sab196087  *		value, not counting any '0x' (hex) or '0' (octal) prefix.
9192352Sab196087  */
9202352Sab196087 static void
9212352Sab196087 print_header(int ndigits)
9222352Sab196087 {
9232352Sab196087 	const char *fmt;
9242352Sab196087 	const char *section_title;
925*2564Sab196087 	const int pad[] = {	/* Extra prefix characters for format */
926*2564Sab196087 		1,		/* FMT_T_DEC: '|' */
927*2564Sab196087 		3,		/* FMT_T_HEX: '|0x' */
928*2564Sab196087 		2,		/* FMT_T_OCT: '|0' */
929*2564Sab196087 	};
9302352Sab196087 	if (
9312352Sab196087 #ifndef XPG4
9322352Sab196087 	    !u_flag &&
9332352Sab196087 #endif
9342352Sab196087 	    !h_flag && !p_flag && !P_flag) {
9350Sstevel@tonic-gate 		(void) printf("\n");
9362352Sab196087 		if (!s_flag) {
9372352Sab196087 			fmt = "%-9s%-*s%-*s%-6s%-6s%-6s%-8s%s\n\n";
9382352Sab196087 			section_title = "Shndx";
9392352Sab196087 		} else {
9402352Sab196087 			fmt = "%-9s%-*s%-*s%-6s%-6s%-6s%-15s%s\n\n";
9412352Sab196087 			section_title = "Shname";
9422352Sab196087 		}
9430Sstevel@tonic-gate 		if (A_flag != 0)
9440Sstevel@tonic-gate 			(void) printf("%s", A_header);
945*2564Sab196087 		ndigits += pad[fmt_flag];
946*2564Sab196087 		(void) printf(fmt, "[Index]", ndigits, " Value",
947*2564Sab196087 			ndigits, " Size", "Type", "Bind",
9482352Sab196087 			"Other", section_title, "Name");
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * If the symbol can be printed, then return 1.
9540Sstevel@tonic-gate  * If the symbol can not be printed, then return 0.
9550Sstevel@tonic-gate  */
9560Sstevel@tonic-gate static int
9570Sstevel@tonic-gate is_sym_print(SYM *sym_data)
9580Sstevel@tonic-gate {
9590Sstevel@tonic-gate 	/*
9600Sstevel@tonic-gate 	 * If -u flag is specified,
9610Sstevel@tonic-gate 	 *	the symbol has to be undefined.
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	if (u_flag != 0) {
9640Sstevel@tonic-gate 		if ((sym_data->shndx == SHN_UNDEF) &&
9650Sstevel@tonic-gate 			(strlen(sym_data->name) != 0))
9660Sstevel@tonic-gate 			return (1);
9670Sstevel@tonic-gate 		else
9680Sstevel@tonic-gate 			return (0);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * If -e flag is specified,
9730Sstevel@tonic-gate 	 *	the symbol has to be global or static.
9740Sstevel@tonic-gate 	 */
9750Sstevel@tonic-gate 	if (e_flag != 0) {
9760Sstevel@tonic-gate 		switch (sym_data->type) {
9770Sstevel@tonic-gate 		case STT_NOTYPE:
9780Sstevel@tonic-gate 		case STT_OBJECT:
9790Sstevel@tonic-gate 		case STT_FUNC:
9800Sstevel@tonic-gate 		case STT_COMMON:
9810Sstevel@tonic-gate 		case STT_TLS:
9820Sstevel@tonic-gate 			switch (sym_data->bind) {
9830Sstevel@tonic-gate 			case STB_LOCAL:
9840Sstevel@tonic-gate 			case STB_GLOBAL:
9850Sstevel@tonic-gate 			case STB_WEAK:
9860Sstevel@tonic-gate 				return (1);
9870Sstevel@tonic-gate 			default:
9880Sstevel@tonic-gate 				return (0);
9890Sstevel@tonic-gate 			}
9900Sstevel@tonic-gate 		default:
9910Sstevel@tonic-gate 			return (0);
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * If -g is specified,
9970Sstevel@tonic-gate 	 *	the symbol has to be global.
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	if (g_flag != 0) {
10000Sstevel@tonic-gate 		switch (sym_data->type) {
10010Sstevel@tonic-gate 		case STT_NOTYPE:
10020Sstevel@tonic-gate 		case STT_OBJECT:
10030Sstevel@tonic-gate 		case STT_FUNC:
10040Sstevel@tonic-gate 		case STT_COMMON:
10050Sstevel@tonic-gate 		case STT_TLS:
10060Sstevel@tonic-gate 			switch (sym_data->bind) {
10070Sstevel@tonic-gate 			case STB_GLOBAL:
10080Sstevel@tonic-gate 			case STB_WEAK:
10090Sstevel@tonic-gate 				return (1);
10100Sstevel@tonic-gate 			default:
10110Sstevel@tonic-gate 				return (0);
10120Sstevel@tonic-gate 			}
10130Sstevel@tonic-gate 		default:
10140Sstevel@tonic-gate 			return (0);
10150Sstevel@tonic-gate 		}
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * If it comes here, any symbol can be printed.
10200Sstevel@tonic-gate 	 *	(So basically, -f is no-op.)
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 	return (1);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate #ifndef XPG4
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate  * -u flag specified
10280Sstevel@tonic-gate  */
10290Sstevel@tonic-gate static void
10300Sstevel@tonic-gate print_with_uflag(
10310Sstevel@tonic-gate 	SYM *sym_data,
10320Sstevel@tonic-gate 	char *filename
10330Sstevel@tonic-gate )
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	if ((sym_data->shndx == SHN_UNDEF) &&
10360Sstevel@tonic-gate 		(strlen(sym_data->name))) {
10370Sstevel@tonic-gate 		if (!r_flag) {
10380Sstevel@tonic-gate 			if (R_flag) {
10390Sstevel@tonic-gate 				if (archive_name != (char *)0)
10400Sstevel@tonic-gate 					(void) printf(
10410Sstevel@tonic-gate 					"   %s:%s:%s\n",
10420Sstevel@tonic-gate 					archive_name,
10430Sstevel@tonic-gate 					filename,
10440Sstevel@tonic-gate 					sym_data->name);
10450Sstevel@tonic-gate 				else
10460Sstevel@tonic-gate 					(void) printf(
10470Sstevel@tonic-gate 					"    %s:%s\n",
10480Sstevel@tonic-gate 					filename,
10490Sstevel@tonic-gate 					sym_data->name);
10500Sstevel@tonic-gate 			}
10510Sstevel@tonic-gate 			else
10520Sstevel@tonic-gate 				(void) printf(
10530Sstevel@tonic-gate 					"    %s\n",
10540Sstevel@tonic-gate 					sym_data->name);
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 		else
10570Sstevel@tonic-gate 			(void) printf("    %s:%s\n",
10580Sstevel@tonic-gate 				filename,
10590Sstevel@tonic-gate 				sym_data->name);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate #endif
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * -p flag specified
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate static void
10670Sstevel@tonic-gate print_with_pflag(
10682352Sab196087 	int ndigits,
10690Sstevel@tonic-gate 	Elf *elf_file,
10700Sstevel@tonic-gate 	unsigned int shstrndx,
10710Sstevel@tonic-gate 	SYM *sym_data,
10720Sstevel@tonic-gate 	char *filename
10730Sstevel@tonic-gate )
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	char *sym_key	= NULL;
1076*2564Sab196087 	const char * const fmt[] = {
1077*2564Sab196087 		"%.*llu ",	/* FMT_T_DEC */
1078*2564Sab196087 		"0x%.*llx ",	/* FMT_T_HEX */
1079*2564Sab196087 		"0%.*llo "	/* FMT_T_OCT */
1080*2564Sab196087 	};
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (is_sym_print(sym_data) != 1)
10830Sstevel@tonic-gate 		return;
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * -A header
10860Sstevel@tonic-gate 	 */
10870Sstevel@tonic-gate 	if (A_flag != 0)
10880Sstevel@tonic-gate 		(void) printf("%s", A_header);
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	/*
10910Sstevel@tonic-gate 	 * Symbol Value.
10920Sstevel@tonic-gate 	 *	(hex/octal/decimal)
10930Sstevel@tonic-gate 	 */
1094*2564Sab196087 	(void) printf(fmt[fmt_flag], ndigits, EC_ADDR(sym_data->value));
10952352Sab196087 
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * Symbol Type.
10990Sstevel@tonic-gate 	 */
11000Sstevel@tonic-gate 	if ((sym_data->shndx == SHN_UNDEF) &&
11010Sstevel@tonic-gate 		(strlen(sym_data->name)))
11020Sstevel@tonic-gate 		sym_key = UNDEFINED;
11030Sstevel@tonic-gate 	else if (sym_data->type == STT_SPARC_REGISTER) {
11040Sstevel@tonic-gate 		switch (sym_data->bind) {
11050Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = REG_LOCL;
11060Sstevel@tonic-gate 					break;
11070Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = REG_GLOB;
11080Sstevel@tonic-gate 					break;
11090Sstevel@tonic-gate 			case STB_WEAK   : sym_key = REG_WEAK;
11100Sstevel@tonic-gate 					break;
11110Sstevel@tonic-gate 			default	: sym_key = REG_GLOB;
11120Sstevel@tonic-gate 					break;
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 	} else if (((sym_data->flags & FLG_SYM_SPECSEC) == 0) &&
11150Sstevel@tonic-gate 	    is_bss_section((int)sym_data->shndx, elf_file, shstrndx)) {
11160Sstevel@tonic-gate 		switch (sym_data->bind) {
11170Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = BSS_LOCL;
11180Sstevel@tonic-gate 					break;
11190Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = BSS_GLOB;
11200Sstevel@tonic-gate 					break;
11210Sstevel@tonic-gate 			case STB_WEAK   : sym_key = BSS_WEAK;
11220Sstevel@tonic-gate 					break;
11230Sstevel@tonic-gate 			default	: sym_key = BSS_GLOB;
11240Sstevel@tonic-gate 					break;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	else
11290Sstevel@tonic-gate 		sym_key = lookup(sym_data->type,
11300Sstevel@tonic-gate 			sym_data->bind);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	if (sym_key != NULL) {
11330Sstevel@tonic-gate 		if (!l_flag)
11340Sstevel@tonic-gate 			(void) printf("%c ", sym_key[0]);
11350Sstevel@tonic-gate 		else
11360Sstevel@tonic-gate 			(void) printf("%-3s", sym_key);
11370Sstevel@tonic-gate 	} else {
11380Sstevel@tonic-gate 		if (!l_flag)
11390Sstevel@tonic-gate 			(void) printf("%-2d", sym_data->type);
11400Sstevel@tonic-gate 		else
11410Sstevel@tonic-gate 			(void) printf("%-3d", sym_data->type);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	if (!r_flag) {
11440Sstevel@tonic-gate 		if (R_flag) {
11450Sstevel@tonic-gate 			if (archive_name != (char *)0)
11460Sstevel@tonic-gate 				(void) printf(
11470Sstevel@tonic-gate 				"%s:%s:%s\n",
11480Sstevel@tonic-gate 				archive_name,
11490Sstevel@tonic-gate 				filename,
11500Sstevel@tonic-gate 				sym_data->name);
11510Sstevel@tonic-gate 			else
11520Sstevel@tonic-gate 				(void) printf(
11530Sstevel@tonic-gate 				"%s:%s\n",
11540Sstevel@tonic-gate 				filename,
11550Sstevel@tonic-gate 				sym_data->name);
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		else
11580Sstevel@tonic-gate 			(void) printf("%s\n", sym_data->name);
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 	else
11610Sstevel@tonic-gate 		(void) printf("%s:%s\n",
11620Sstevel@tonic-gate 			filename,
11630Sstevel@tonic-gate 			sym_data->name);
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate /*
11670Sstevel@tonic-gate  * -P flag specified
11680Sstevel@tonic-gate  */
11690Sstevel@tonic-gate static void
11700Sstevel@tonic-gate print_with_Pflag(
11712352Sab196087 	int ndigits,
11720Sstevel@tonic-gate 	Elf *elf_file,
11730Sstevel@tonic-gate 	unsigned int shstrndx,
11740Sstevel@tonic-gate 	SYM *sym_data
11750Sstevel@tonic-gate )
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	char *sym_key = NULL;
11780Sstevel@tonic-gate #define	SYM_LEN 10
11790Sstevel@tonic-gate 	char sym_name[SYM_LEN+1];
11800Sstevel@tonic-gate 	size_t len;
1181*2564Sab196087 	const char * const fmt[] = {
1182*2564Sab196087 		"%*llu %*llu \n",	/* FMT_T_DEC */
1183*2564Sab196087 		"%*llx %*llx \n",	/* FMT_T_HEX */
1184*2564Sab196087 		"%*llo %*llo \n"	/* FMT_T_OCT */
1185*2564Sab196087 	};
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (is_sym_print(sym_data) != 1)
11880Sstevel@tonic-gate 		return;
11890Sstevel@tonic-gate 	/*
11900Sstevel@tonic-gate 	 * -A header
11910Sstevel@tonic-gate 	 */
11920Sstevel@tonic-gate 	if (A_flag != 0)
11930Sstevel@tonic-gate 		(void) printf("%s", A_header);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11960Sstevel@tonic-gate 	 * Symbol name
11970Sstevel@tonic-gate 	 */
11980Sstevel@tonic-gate 	len = strlen(sym_data->name);
11990Sstevel@tonic-gate 	if (len >= SYM_LEN)
12000Sstevel@tonic-gate 		(void) printf("%s ", sym_data->name);
12010Sstevel@tonic-gate 	else {
12020Sstevel@tonic-gate 		(void) sprintf(sym_name, "%-10s", sym_data->name);
12030Sstevel@tonic-gate 		(void) printf("%s ", sym_name);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	/*
12070Sstevel@tonic-gate 	 * Symbol Type.
12080Sstevel@tonic-gate 	 */
12090Sstevel@tonic-gate 	if ((sym_data->shndx == SHN_UNDEF) &&
12100Sstevel@tonic-gate 		(strlen(sym_data->name)))
12110Sstevel@tonic-gate 		sym_key = UNDEFINED;
12120Sstevel@tonic-gate 	else if (sym_data->type == STT_SPARC_REGISTER) {
12130Sstevel@tonic-gate 		switch (sym_data->bind) {
12140Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = REG_LOCL;
12150Sstevel@tonic-gate 					break;
12160Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = REG_GLOB;
12170Sstevel@tonic-gate 					break;
12180Sstevel@tonic-gate 			case STB_WEAK   : sym_key = REG_WEAK;
12190Sstevel@tonic-gate 					break;
12200Sstevel@tonic-gate 			default	: sym_key = REG_GLOB;
12210Sstevel@tonic-gate 					break;
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 	} else if (((sym_data->flags & FLG_SYM_SPECSEC) == 0) &&
12240Sstevel@tonic-gate 	    is_bss_section((int)sym_data->shndx,
12250Sstevel@tonic-gate 		elf_file, shstrndx)) {
12260Sstevel@tonic-gate 		switch (sym_data->bind) {
12270Sstevel@tonic-gate 			case STB_LOCAL  : sym_key = BSS_LOCL;
12280Sstevel@tonic-gate 					break;
12290Sstevel@tonic-gate 			case STB_GLOBAL : sym_key = BSS_GLOB;
12300Sstevel@tonic-gate 					break;
12310Sstevel@tonic-gate 			case STB_WEAK   : sym_key = BSS_WEAK;
12320Sstevel@tonic-gate 					break;
12330Sstevel@tonic-gate 			default	: sym_key = BSS_GLOB;
12340Sstevel@tonic-gate 					break;
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	} else
12380Sstevel@tonic-gate 		sym_key = lookup(sym_data->type,
12390Sstevel@tonic-gate 			sym_data->bind);
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	if (sym_key != NULL) {
12420Sstevel@tonic-gate 		if (!l_flag)
12430Sstevel@tonic-gate 			(void) printf("%c ", sym_key[0]);
12440Sstevel@tonic-gate 		else
12450Sstevel@tonic-gate 			(void) printf("%-3s", sym_key);
12460Sstevel@tonic-gate 	} else {
12470Sstevel@tonic-gate 		if (!l_flag)
12480Sstevel@tonic-gate 			(void) printf("%-2d", sym_data->type);
12490Sstevel@tonic-gate 		else
12500Sstevel@tonic-gate 			(void) printf("%-3d", sym_data->type);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * Symbol Value & size
12550Sstevel@tonic-gate 	 *	(hex/octal/decimal)
12560Sstevel@tonic-gate 	 */
1257*2564Sab196087 	(void) printf(fmt[fmt_flag], ndigits, EC_ADDR(sym_data->value),
12582352Sab196087 		ndigits, EC_XWORD(sym_data->size));
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate /*
12620Sstevel@tonic-gate  * other flags specified
12630Sstevel@tonic-gate  */
12640Sstevel@tonic-gate static void
12650Sstevel@tonic-gate print_with_otherflags(
12662352Sab196087 	int ndigits,
12670Sstevel@tonic-gate 	Elf *elf_file,
12680Sstevel@tonic-gate 	unsigned int shstrndx,
12690Sstevel@tonic-gate 	SYM *sym_data,
12700Sstevel@tonic-gate 	char *filename
12710Sstevel@tonic-gate )
12720Sstevel@tonic-gate {
1273*2564Sab196087 	const char * const fmt_value_size[] = {
1274*2564Sab196087 		"%*llu|%*lld|",		/* FMT_T_DEC */
1275*2564Sab196087 		"0x%.*llx|0x%.*llx|",	/* FMT_T_HEX */
1276*2564Sab196087 		"0%.*llo|0%.*llo|"	/* FMT_T_OCT */
1277*2564Sab196087 	};
1278*2564Sab196087 	const char * const fmt_int[] = {
1279*2564Sab196087 		"%-5d",			/* FMT_T_DEC */
1280*2564Sab196087 		"%#-5x",		/* FMT_T_HEX */
1281*2564Sab196087 		"%#-5o"			/* FMT_T_OCT */
1282*2564Sab196087 	};
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (is_sym_print(sym_data) != 1)
12850Sstevel@tonic-gate 		return;
12860Sstevel@tonic-gate 	(void) printf("%s", A_header);
12870Sstevel@tonic-gate 	(void) printf("[%d]\t|", sym_data->indx);
1288*2564Sab196087 	(void) printf(fmt_value_size[fmt_flag], ndigits,
1289*2564Sab196087 		EC_ADDR(sym_data->value), ndigits, EC_XWORD(sym_data->size));
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	switch (sym_data->type) {
12920Sstevel@tonic-gate 	case STT_NOTYPE:(void) printf("%-5s", "NOTY"); break;
12930Sstevel@tonic-gate 	case STT_OBJECT:(void) printf("%-5s", "OBJT"); break;
12940Sstevel@tonic-gate 	case STT_FUNC:	(void) printf("%-5s", "FUNC"); break;
12950Sstevel@tonic-gate 	case STT_SECTION:(void) printf("%-5s", "SECT"); break;
12960Sstevel@tonic-gate 	case STT_FILE:	(void) printf("%-5s", "FILE"); break;
12970Sstevel@tonic-gate 	case STT_COMMON: (void) printf("%-5s", "COMM"); break;
12980Sstevel@tonic-gate 	case STT_TLS:	(void) printf("%-5s", "TLS "); break;
12990Sstevel@tonic-gate 	case STT_SPARC_REGISTER: (void) printf("%-5s", "REGI"); break;
13000Sstevel@tonic-gate 	default:
1301*2564Sab196087 		(void) printf(fmt_int[fmt_flag], sym_data->type);
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 	(void) printf("|");
13040Sstevel@tonic-gate 	switch (sym_data->bind) {
13050Sstevel@tonic-gate 	case STB_LOCAL:	(void) printf("%-5s", "LOCL"); break;
13060Sstevel@tonic-gate 	case STB_GLOBAL:(void) printf("%-5s", "GLOB"); break;
13070Sstevel@tonic-gate 	case STB_WEAK:	(void) printf("%-5s", "WEAK"); break;
13080Sstevel@tonic-gate 	default:
13090Sstevel@tonic-gate 		(void) printf("%-5d", sym_data->bind);
1310*2564Sab196087 		(void) printf(fmt_int[fmt_flag], sym_data->bind);
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 	(void) printf("|");
1313*2564Sab196087 	(void) printf(fmt_int[fmt_flag], sym_data->other);
13140Sstevel@tonic-gate 	(void)  printf("|");
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	if (sym_data->shndx == SHN_UNDEF) {
13170Sstevel@tonic-gate 		if (!s_flag)
13180Sstevel@tonic-gate 			(void) printf("%-7s",
13190Sstevel@tonic-gate 				"UNDEF");
13200Sstevel@tonic-gate 		else
13210Sstevel@tonic-gate 			(void) printf("%-14s",
13220Sstevel@tonic-gate 				"UNDEF");
13230Sstevel@tonic-gate 	} else if (sym_data->shndx == SHN_SUNW_IGNORE) {
13240Sstevel@tonic-gate 		if (!s_flag)
13250Sstevel@tonic-gate 			(void) printf("%-7s",
13260Sstevel@tonic-gate 				"IGNORE");
13270Sstevel@tonic-gate 		else
13280Sstevel@tonic-gate 			(void) printf("%-14s",
13290Sstevel@tonic-gate 				"IGNORE");
13300Sstevel@tonic-gate 	} else if ((sym_data->flags & FLG_SYM_SPECSEC) &&
13310Sstevel@tonic-gate 	    (sym_data->shndx == SHN_ABS)) {
13320Sstevel@tonic-gate 		if (!s_flag)
13330Sstevel@tonic-gate 			(void) printf("%-7s",
13340Sstevel@tonic-gate 				"ABS");
13350Sstevel@tonic-gate 		else
13360Sstevel@tonic-gate 			(void) printf("%-14s",
13370Sstevel@tonic-gate 				"ABS");
13380Sstevel@tonic-gate 	} else if ((sym_data->flags & FLG_SYM_SPECSEC) &&
13390Sstevel@tonic-gate 	    (sym_data->shndx == SHN_COMMON)) {
13400Sstevel@tonic-gate 		if (!s_flag)
13410Sstevel@tonic-gate 			(void) printf("%-7s",
13420Sstevel@tonic-gate 				"COMMON");
13430Sstevel@tonic-gate 		else
13440Sstevel@tonic-gate 			(void) printf("%-14s",
13450Sstevel@tonic-gate 				"COMMON");
13460Sstevel@tonic-gate 	} else {
1347*2564Sab196087 		if (s_flag) {
1348*2564Sab196087 			Elf_Scn *scn = elf_getscn(elf_file, sym_data->shndx);
13490Sstevel@tonic-gate 			GElf_Shdr shdr;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 			if ((gelf_getshdr(scn, &shdr) != 0) &&
13520Sstevel@tonic-gate 			    (shdr.sh_name != 0)) {
13530Sstevel@tonic-gate 				(void) printf("%-14s",
13540Sstevel@tonic-gate 				(char *)elf_strptr(elf_file,
13550Sstevel@tonic-gate 				shstrndx, shdr.sh_name));
1356*2564Sab196087 			} else {
1357*2564Sab196087 				(void) printf("%-14d", sym_data->shndx);
1358*2564Sab196087 			}
1359*2564Sab196087 		} else {
1360*2564Sab196087 			(void) printf("%-7d", sym_data->shndx);
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 	(void) printf("|");
13640Sstevel@tonic-gate 	if (!r_flag) {
13650Sstevel@tonic-gate 		if (R_flag) {
13660Sstevel@tonic-gate 			if (archive_name != (char *)0)
13670Sstevel@tonic-gate 				(void) printf("%s:%s:%s\n",
13680Sstevel@tonic-gate 					archive_name,
13690Sstevel@tonic-gate 					filename,
13700Sstevel@tonic-gate 					sym_data->name);
13710Sstevel@tonic-gate 			else
13720Sstevel@tonic-gate 				(void) printf("%s:%s\n",
13730Sstevel@tonic-gate 					filename,
13740Sstevel@tonic-gate 					sym_data->name);
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 		else
13770Sstevel@tonic-gate 			(void) printf("%s\n", sym_data->name);
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 	else
13800Sstevel@tonic-gate 		(void) printf("%s:%s\n",
13810Sstevel@tonic-gate 			filename, sym_data->name);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate /*
13850Sstevel@tonic-gate  * C++ name demangling supporting routines
13860Sstevel@tonic-gate  */
13870Sstevel@tonic-gate static const char *ctor_str = "static constructor function for %s";
13880Sstevel@tonic-gate static const char *dtor_str = "static destructor function for %s";
13890Sstevel@tonic-gate static const char *ptbl_str = "pointer to the virtual table vector for %s";
13900Sstevel@tonic-gate static const char *vtbl_str = "virtual table for %s";
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate  * alloc memory and create name in necessary format.
13940Sstevel@tonic-gate  * Return name string
13950Sstevel@tonic-gate  */
13960Sstevel@tonic-gate static char *
13970Sstevel@tonic-gate FormatName(char *OldName, char *NewName)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	char *s = p_flag ?
14000Sstevel@tonic-gate 		"%s\n             [%s]" :
14010Sstevel@tonic-gate 		"%s\n\t\t\t\t\t\t       [%s]";
14020Sstevel@tonic-gate 	size_t length = strlen(s)+strlen(NewName)+strlen(OldName)-3;
14030Sstevel@tonic-gate 	char *hold = OldName;
14040Sstevel@tonic-gate 	OldName = malloc(length);
14052352Sab196087 	/*LINTED*/
14062352Sab196087 	(void) snprintf(OldName, length, s, NewName, hold);
14070Sstevel@tonic-gate 	return (OldName);
14080Sstevel@tonic-gate }
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Return 1 when s is an exotic name, 0 otherwise.  s remains unchanged,
14130Sstevel@tonic-gate  * the exotic name, if exists, is saved in d_buf.
14140Sstevel@tonic-gate  */
14150Sstevel@tonic-gate static int
14160Sstevel@tonic-gate exotic(char *s)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	int tag = 0;
14190Sstevel@tonic-gate 	if (strncmp(s, "__sti__", 7) == 0) {
14200Sstevel@tonic-gate 		s += 7; tag = 1;
14210Sstevel@tonic-gate 		parse_fn_and_print(ctor_str, s);
14220Sstevel@tonic-gate 	} else if (strncmp(s, "__std__", 7) == 0) {
14230Sstevel@tonic-gate 		s += 7; tag = 1;
14240Sstevel@tonic-gate 		parse_fn_and_print(dtor_str, s);
14250Sstevel@tonic-gate 	} else if (strncmp(s, "__vtbl__", 8) == 0) {
14260Sstevel@tonic-gate 		s += 8; tag = 1;
14270Sstevel@tonic-gate 		parsename(s);
14280Sstevel@tonic-gate 		(void) sprintf(d_buf, vtbl_str, p_buf);
14290Sstevel@tonic-gate 	} else if (strncmp(s, "__ptbl_vec__", 12) == 0) {
14300Sstevel@tonic-gate 		s += 12; tag = 1;
14310Sstevel@tonic-gate 		parse_fn_and_print(ptbl_str, s);
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 	return (tag);
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate void
14370Sstevel@tonic-gate parsename(char *s)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	register int len;
14400Sstevel@tonic-gate 	char c, *orig = s;
14410Sstevel@tonic-gate 	*p_buf = '\0';
14420Sstevel@tonic-gate 	(void) strcat(p_buf, "class ");
14430Sstevel@tonic-gate 	while (isdigit(*s)) s++;
14440Sstevel@tonic-gate 	c = *s;
14450Sstevel@tonic-gate 	*s = '\0';
14460Sstevel@tonic-gate 	len = atoi(orig);
14470Sstevel@tonic-gate 	*s = c;
14480Sstevel@tonic-gate 	if (*(s+len) == '\0') { /* only one class name */
14490Sstevel@tonic-gate 		(void) strcat(p_buf, s);
14500Sstevel@tonic-gate 		return;
14510Sstevel@tonic-gate 	} else
14520Sstevel@tonic-gate 	{ /* two classname  %drootname__%dchildname */
14530Sstevel@tonic-gate 		char *root, *child, *child_len_p;
14540Sstevel@tonic-gate 		int child_len;
14550Sstevel@tonic-gate 		root = s;
14560Sstevel@tonic-gate 		child = s + len + 2;
14570Sstevel@tonic-gate 		child_len_p = child;
14580Sstevel@tonic-gate 		if (!isdigit(*child)) {
14590Sstevel@tonic-gate 			/* ptbl file name */
14600Sstevel@tonic-gate 			/*  %drootname__%filename */
14610Sstevel@tonic-gate 			/* kludge for getting rid of '_' in file name */
14620Sstevel@tonic-gate 			char *p;
14630Sstevel@tonic-gate 			c = *(root + len);
14640Sstevel@tonic-gate 			*(root + len) = '\0';
14650Sstevel@tonic-gate 			(void) strcat(p_buf, root);
14660Sstevel@tonic-gate 			*(root + len) = c;
14670Sstevel@tonic-gate 			(void) strcat(p_buf, " in ");
14680Sstevel@tonic-gate 			for (p = child; *p != '_'; ++p);
14690Sstevel@tonic-gate 			c = *p;
14700Sstevel@tonic-gate 			*p = '.';
14710Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14720Sstevel@tonic-gate 			*p = c;
14730Sstevel@tonic-gate 			return;
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		while (isdigit(*child))
14770Sstevel@tonic-gate 			child++;
14780Sstevel@tonic-gate 		c = *child;
14790Sstevel@tonic-gate 		*child = '\0';
14800Sstevel@tonic-gate 		child_len = atoi(child_len_p);
14810Sstevel@tonic-gate 		*child = c;
14820Sstevel@tonic-gate 		if (*(child + child_len) == '\0') {
14830Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14840Sstevel@tonic-gate 			(void) strcat(p_buf, " derived from ");
14850Sstevel@tonic-gate 			c = *(root + len);
14860Sstevel@tonic-gate 			*(root + len) = '\0';
14870Sstevel@tonic-gate 			(void) strcat(p_buf, root);
14880Sstevel@tonic-gate 			*(root + len) = c;
14890Sstevel@tonic-gate 			return;
14900Sstevel@tonic-gate 		} else {
14910Sstevel@tonic-gate 			/* %drootname__%dchildname__filename */
14920Sstevel@tonic-gate 			/* kludge for getting rid of '_' in file name */
14930Sstevel@tonic-gate 			char *p;
14940Sstevel@tonic-gate 			c = *(child + child_len);
14950Sstevel@tonic-gate 			*(child + child_len) = '\0';
14960Sstevel@tonic-gate 			(void) strcat(p_buf, child);
14970Sstevel@tonic-gate 			*(child+child_len) = c;
14980Sstevel@tonic-gate 			(void) strcat(p_buf, " derived from ");
14990Sstevel@tonic-gate 			c = *(root + len);
15000Sstevel@tonic-gate 			*(root + len) = '\0';
15010Sstevel@tonic-gate 			(void) strcat(p_buf, root);
15020Sstevel@tonic-gate 			*(root + len) = c;
15030Sstevel@tonic-gate 			(void) strcat(p_buf, " in ");
15040Sstevel@tonic-gate 			for (p = child + child_len + 2; *p != '_'; ++p);
15050Sstevel@tonic-gate 			c = *p;
15060Sstevel@tonic-gate 			*p = '.';
15070Sstevel@tonic-gate 			(void) strcat(p_buf, child + child_len + 2);
15080Sstevel@tonic-gate 			*p = c;
15090Sstevel@tonic-gate 			return;
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate void
15150Sstevel@tonic-gate parse_fn_and_print(const char *str, char *s)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate 	char		c, *p1, *p2;
15180Sstevel@tonic-gate 	size_t		sym_len = strlen(s);
15190Sstevel@tonic-gate 	int		yes = 1;
15200Sstevel@tonic-gate 	static char	*buff = 0;
15210Sstevel@tonic-gate 	static size_t	buf_size;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * We will need to modify the symbol (s) as we are analyzing it,
15250Sstevel@tonic-gate 	 * so copy it into a buffer so that we can play around with it.
15260Sstevel@tonic-gate 	 */
15270Sstevel@tonic-gate 	if (buff == NULL) {
15280Sstevel@tonic-gate 	buff = malloc(DEF_MAX_SYM_SIZE);
15290Sstevel@tonic-gate 	buf_size = DEF_MAX_SYM_SIZE;
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if (++sym_len > buf_size) {
15330Sstevel@tonic-gate 	if (buff)
15340Sstevel@tonic-gate 		free(buff);
15350Sstevel@tonic-gate 	buff = malloc(sym_len);
15360Sstevel@tonic-gate 	buf_size = sym_len;
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (buff == NULL) {
15400Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15410Sstevel@tonic-gate 			"%s: cannot malloc space\n"), prog_name);
15420Sstevel@tonic-gate 		exit(NOALLOC);
15430Sstevel@tonic-gate 	}
15440Sstevel@tonic-gate 	s = strcpy(buff, s);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if ((p1 = p2 =  strstr(s, "_c_")) == NULL)
15470Sstevel@tonic-gate 		if ((p1 = p2 =  strstr(s, "_C_")) == NULL)
15480Sstevel@tonic-gate 			if ((p1 = p2 =  strstr(s, "_cc_")) == NULL)
15490Sstevel@tonic-gate 				if ((p1 = p2 =  strstr(s, "_cxx_")) == NULL)
15500Sstevel@tonic-gate 					if (
15510Sstevel@tonic-gate 					(p1 = p2 = strstr(s, "_h_")) == NULL)
15520Sstevel@tonic-gate 			yes = 0;
15530Sstevel@tonic-gate 			else
15540Sstevel@tonic-gate 						p2 += 2;
15550Sstevel@tonic-gate 				else
15560Sstevel@tonic-gate 					p2 += 4;
15570Sstevel@tonic-gate 			else
15580Sstevel@tonic-gate 				p2 += 3;
15590Sstevel@tonic-gate 		else
15600Sstevel@tonic-gate 			p2 += 2;
15610Sstevel@tonic-gate 	else
15620Sstevel@tonic-gate 		p2 += 2;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	if (yes) {
15650Sstevel@tonic-gate 	*p1 = '.';
15660Sstevel@tonic-gate 		c = *p2;
15670Sstevel@tonic-gate 		*p2 = '\0';
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	for (s = p1;  *s != '_';  --s);
15710Sstevel@tonic-gate 	++s;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	(void) sprintf(d_buf, str, s);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	if (yes) {
15760Sstevel@tonic-gate 		*p1 = '_';
15770Sstevel@tonic-gate 		*p2 = c;
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate }
1580