xref: /onnv-gate/usr/src/cmd/sgs/pvs/common/pvs.c (revision 11993:a39d3ec14d8f)
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 /*
23*11993SAli.Bahrami@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Analyze the versioning information within a file.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *   -C		demangle C++ symbol names.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *   -d		dump version definitions.
330Sstevel@tonic-gate  *
347682SAli.Bahrami@Sun.COM  *   -l		print reduced (local) symbols. Implies -s.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *   -n		normalize any version definitions.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *   -o		dump output in one-line fashion	(more suitable for grep'ing
390Sstevel@tonic-gate  *		and diff'ing).
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  *   -r		dump the version requirements on library dependencies
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *   -s		display the symbols associated with each version definition.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  *   -v		verbose output.  With the -r and -d options any WEAK attribute
460Sstevel@tonic-gate  *		is displayed.  With the -d option, any version inheritance,
477682SAli.Bahrami@Sun.COM  *		and the base version are displayed.  With the -r option,
487682SAli.Bahrami@Sun.COM  *		WEAK and INFO attributes are displayed. With the -s option
497682SAli.Bahrami@Sun.COM  *		the version symbol is displayed.
507682SAli.Bahrami@Sun.COM  *
517682SAli.Bahrami@Sun.COM  *   -I index	only print the specifed version index, or index range.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  *   -N name	only print the specifed `name'.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #include	<fcntl.h>
560Sstevel@tonic-gate #include	<stdio.h>
570Sstevel@tonic-gate #include	<libelf.h>
580Sstevel@tonic-gate #include	<link.h>
590Sstevel@tonic-gate #include	<stdlib.h>
600Sstevel@tonic-gate #include	<string.h>
610Sstevel@tonic-gate #include	<unistd.h>
620Sstevel@tonic-gate #include	<locale.h>
630Sstevel@tonic-gate #include	<errno.h>
641618Srie #include	<sgs.h>
651618Srie #include	<conv.h>
661618Srie #include	<gelf.h>
671618Srie #include	<debug.h>
687682SAli.Bahrami@Sun.COM #include	<ctype.h>
697682SAli.Bahrami@Sun.COM #include	<alist.h>
700Sstevel@tonic-gate #include	"msg.h"
710Sstevel@tonic-gate 
727682SAli.Bahrami@Sun.COM /*
737682SAli.Bahrami@Sun.COM  * Define Alist initialization sizes.
747682SAli.Bahrami@Sun.COM  */
757682SAli.Bahrami@Sun.COM #define	AL_CNT_MATCH_LIST	5	/* match_list initial alist count */
767682SAli.Bahrami@Sun.COM #define	AL_CNT_GVER_DESC	25	/* version tracking descriptors */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate typedef struct cache {
790Sstevel@tonic-gate 	Elf_Scn		*c_scn;
800Sstevel@tonic-gate 	Elf_Data	*c_data;
810Sstevel@tonic-gate 	char		*c_name;
820Sstevel@tonic-gate } Cache;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate typedef struct gver_desc {
850Sstevel@tonic-gate 	const char	*vd_name;
860Sstevel@tonic-gate 	unsigned long	vd_hash;
870Sstevel@tonic-gate 	GElf_Half	vd_ndx;
880Sstevel@tonic-gate 	GElf_Half	vd_flags;
897682SAli.Bahrami@Sun.COM 	APlist		*vd_deps;
900Sstevel@tonic-gate } GVer_desc;
910Sstevel@tonic-gate 
927682SAli.Bahrami@Sun.COM /* Versym related data used by gvers_syms() */
937682SAli.Bahrami@Sun.COM typedef struct {
947682SAli.Bahrami@Sun.COM 	GElf_Versym	*vsd_vsp;   	/* ptr to versym data */
957682SAli.Bahrami@Sun.COM 	Elf_Data	*vsd_sym_data;	/* ptr to symtab data */
967682SAli.Bahrami@Sun.COM 	Word		vsd_symn;	/* # of symbols in symtab */
977682SAli.Bahrami@Sun.COM 	const char	*vsd_strs;	/* string table data */
987682SAli.Bahrami@Sun.COM } Gver_sym_data;
997682SAli.Bahrami@Sun.COM 
1007682SAli.Bahrami@Sun.COM /*
1017682SAli.Bahrami@Sun.COM  * Type used to manage -I and -N options:
1027682SAli.Bahrami@Sun.COM  *
1037682SAli.Bahrami@Sun.COM  * The -I option specifies a VERSYM index, or index range. The
1047682SAli.Bahrami@Sun.COM  * result is to select the VERDEF or VERNEED records with
1057682SAli.Bahrami@Sun.COM  * indexes that match those given.
1067682SAli.Bahrami@Sun.COM  *
1077682SAli.Bahrami@Sun.COM  * -N options come in two forms:
1087682SAli.Bahrami@Sun.COM  *
1097682SAli.Bahrami@Sun.COM  *	1) name
1107682SAli.Bahrami@Sun.COM  *	2) needobj (version)
1117682SAli.Bahrami@Sun.COM  *
1127682SAli.Bahrami@Sun.COM  * The meaning of the first case depends on the type of
1137682SAli.Bahrami@Sun.COM  * version record being matched:
1147682SAli.Bahrami@Sun.COM  *
1157682SAli.Bahrami@Sun.COM  *	VERDEF - name is the name of a version defined
1167682SAli.Bahrami@Sun.COM  *		by the object being processed (i.e. SUNW_1.1).
1177682SAli.Bahrami@Sun.COM  *
1187682SAli.Bahrami@Sun.COM  *	VERNEED - name is the name of the object file
1197682SAli.Bahrami@Sun.COM  *		on which the dependency exists (i.e. libc.so.1).
1207682SAli.Bahrami@Sun.COM  *
1217682SAli.Bahrami@Sun.COM  * -N options of the second form only apply to VERNEED records.
1227682SAli.Bahrami@Sun.COM  * They are used to specify a version from a needed object.
1237682SAli.Bahrami@Sun.COM  */
1247682SAli.Bahrami@Sun.COM /* match_opt_t is  used to note which match option was used */
1257682SAli.Bahrami@Sun.COM typedef enum {
1267682SAli.Bahrami@Sun.COM 	MATCH_OPT_NAME,		/* Record contains a name */
1277682SAli.Bahrami@Sun.COM 	MATCH_OPT_NEED_VER,	/* Record contains needed object and version */
1287682SAli.Bahrami@Sun.COM 	MATCH_OPT_NDX,		/* Record contains a single index */
1297682SAli.Bahrami@Sun.COM 	MATCH_OPT_RANGE,	/* Record contains an index range */
1307682SAli.Bahrami@Sun.COM } match_opt_t;
1317682SAli.Bahrami@Sun.COM 
1327682SAli.Bahrami@Sun.COM typedef struct {
1337682SAli.Bahrami@Sun.COM 	match_opt_t	opt_type;
1347682SAli.Bahrami@Sun.COM 	union {
1357682SAli.Bahrami@Sun.COM 		struct {
1367682SAli.Bahrami@Sun.COM 			const char *version;	/* MATCH_OPT_{NAME|NEED_VER} */
1377682SAli.Bahrami@Sun.COM 			const char *needobj;	/* MATCH_OPT_NEED_VER only */
1387682SAli.Bahrami@Sun.COM 		} name;
1397682SAli.Bahrami@Sun.COM 		struct {
1407682SAli.Bahrami@Sun.COM 			int start;		/* MATCH_OPT_{NDX|RANGE} */
1417682SAli.Bahrami@Sun.COM 			int end;		/* MATCH_OPT_RANGE only) */
1427682SAli.Bahrami@Sun.COM 		} ndx;
1437682SAli.Bahrami@Sun.COM 	} value;
1447682SAli.Bahrami@Sun.COM } match_rec_t;
1457682SAli.Bahrami@Sun.COM 
1467682SAli.Bahrami@Sun.COM 
1477682SAli.Bahrami@Sun.COM 
1480Sstevel@tonic-gate static const char	*cname;
1490Sstevel@tonic-gate static int		Cflag, dflag, lflag, nflag, oflag, rflag, sflag, vflag;
1507682SAli.Bahrami@Sun.COM static Alist		*match_list;
1510Sstevel@tonic-gate 
1527682SAli.Bahrami@Sun.COM /* Used to track whether an option defaulted to on, or was explicitly set */
1530Sstevel@tonic-gate #define	DEF_DEFINED	1
1540Sstevel@tonic-gate #define	USR_DEFINED	2
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate static const char *
demangle(const char * name)1600Sstevel@tonic-gate demangle(const char *name)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	if (Cflag)
1631618Srie 		return (Elf_demangle_name(name));
1640Sstevel@tonic-gate 	else
1650Sstevel@tonic-gate 		return (name);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1697682SAli.Bahrami@Sun.COM  * Append an item to the specified list, and return a pointer to the list
1707682SAli.Bahrami@Sun.COM  * node created.
1717682SAli.Bahrami@Sun.COM  *
1727682SAli.Bahrami@Sun.COM  * exit:
1737682SAli.Bahrami@Sun.COM  *	On success, a new list node is created and the item is
1747682SAli.Bahrami@Sun.COM  *	added to the list. On failure, a fatal error is issued
1757682SAli.Bahrami@Sun.COM  *	and the process exits.
1767682SAli.Bahrami@Sun.COM  */
1777682SAli.Bahrami@Sun.COM static void
pvs_aplist_append(APlist ** lst,const void * item,const char * file)1787682SAli.Bahrami@Sun.COM pvs_aplist_append(APlist **lst, const void *item, const char *file)
1797682SAli.Bahrami@Sun.COM {
1807682SAli.Bahrami@Sun.COM 	if (aplist_append(lst, item, AL_CNT_GVER_DESC) == NULL) {
1817682SAli.Bahrami@Sun.COM 		int err = errno;
1827682SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), cname, file,
1837682SAli.Bahrami@Sun.COM 		    strerror(err));
1847682SAli.Bahrami@Sun.COM 		exit(1);
1857682SAli.Bahrami@Sun.COM 	}
1867682SAli.Bahrami@Sun.COM }
1877682SAli.Bahrami@Sun.COM 
1887682SAli.Bahrami@Sun.COM /*
1897682SAli.Bahrami@Sun.COM  * Add an entry to match_list for use by match(). This routine is for
1907682SAli.Bahrami@Sun.COM  * use during getopt() processing.
1917682SAli.Bahrami@Sun.COM  *
1927682SAli.Bahrami@Sun.COM  * entry:
1937682SAli.Bahrami@Sun.COM  *	opt - One of 'N' or 'I', indicating the option
1947682SAli.Bahrami@Sun.COM  *	str - Value string corresponding to opt
1957682SAli.Bahrami@Sun.COM  *
1967682SAli.Bahrami@Sun.COM  * exit:
1977682SAli.Bahrami@Sun.COM  *	The new match record has been added. On error, a fatal
1987682SAli.Bahrami@Sun.COM  *	error is issued and and the process exits.
1997682SAli.Bahrami@Sun.COM  */
2007682SAli.Bahrami@Sun.COM static void
add_match_record(int opt,const char * str)2017682SAli.Bahrami@Sun.COM add_match_record(int opt, const char *str)
2027682SAli.Bahrami@Sun.COM {
2037682SAli.Bahrami@Sun.COM 	/*
2047682SAli.Bahrami@Sun.COM 	 * Macros for removing leading and trailing whitespace:
2057682SAli.Bahrami@Sun.COM 	 *	WS_SKIP - Advance _str without passing the NULL termination,
2067682SAli.Bahrami@Sun.COM 	 *		until the first character is not whitespace.
2077682SAli.Bahrami@Sun.COM 	 *	WS_SKIP_LIMIT - Advance _str without passing _limit,
2087682SAli.Bahrami@Sun.COM 	 *		until the first character is not whitespace.
2097682SAli.Bahrami@Sun.COM 	 *	WS_RSKIP_LIMIT - Move _tail back without passing _str,
2107682SAli.Bahrami@Sun.COM 	 *		until the character before it is not whitespace.
2117682SAli.Bahrami@Sun.COM 	 *		Write a NULL termination at that point.
2127682SAli.Bahrami@Sun.COM 	 */
2137682SAli.Bahrami@Sun.COM #define	WS_SKIP(_str) for (; *(_str) && isspace(*(_str)); (_str)++)
2147682SAli.Bahrami@Sun.COM #define	WS_SKIP_LIMIT(_str, _limit) \
2157682SAli.Bahrami@Sun.COM 	while (((_str) < s2) && isspace(*(_str))) \
2167682SAli.Bahrami@Sun.COM 		(_str)++
2177682SAli.Bahrami@Sun.COM #define	WS_RSKIP_LIMIT(_str, _tail) \
2187682SAli.Bahrami@Sun.COM 	while (((_tail) > (_str)) && isspace(*((_tail) - 1)))	\
2197682SAli.Bahrami@Sun.COM 		(_tail)--;					\
2207682SAli.Bahrami@Sun.COM 	*(_tail) = '\0'
2217682SAli.Bahrami@Sun.COM 
2227682SAli.Bahrami@Sun.COM 
2237682SAli.Bahrami@Sun.COM 	match_rec_t	*rec;
2247682SAli.Bahrami@Sun.COM 	char		*lstr, *s1, *s2;
2257682SAli.Bahrami@Sun.COM 
2267682SAli.Bahrami@Sun.COM 	rec = alist_append(&match_list, NULL, sizeof (match_rec_t),
2277682SAli.Bahrami@Sun.COM 	    AL_CNT_MATCH_LIST);
2287682SAli.Bahrami@Sun.COM 	if (rec == NULL) {
2297682SAli.Bahrami@Sun.COM 		int err = errno;
2307682SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), cname,
2317682SAli.Bahrami@Sun.COM 		    MSG_INTL(MSG_STR_MATCH_RECORD), strerror(err));
2327682SAli.Bahrami@Sun.COM 		exit(1);
2337682SAli.Bahrami@Sun.COM 	}
2347682SAli.Bahrami@Sun.COM 
2357682SAli.Bahrami@Sun.COM 	if (opt == 'N') {
2367682SAli.Bahrami@Sun.COM 		if ((lstr = strdup(str)) == NULL) {
2377682SAli.Bahrami@Sun.COM 			int err = errno;
2387682SAli.Bahrami@Sun.COM 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
2397682SAli.Bahrami@Sun.COM 			    cname, MSG_INTL(MSG_STR_MATCH_RECORD),
2407682SAli.Bahrami@Sun.COM 			    strerror(err));
2417682SAli.Bahrami@Sun.COM 			exit(1);
2427682SAli.Bahrami@Sun.COM 		}
2437682SAli.Bahrami@Sun.COM 
2447682SAli.Bahrami@Sun.COM 		/* Strip leading/trailing whitespace */
2457682SAli.Bahrami@Sun.COM 		s2 = lstr + strlen(lstr);
2467682SAli.Bahrami@Sun.COM 		WS_SKIP_LIMIT(lstr, s2);
2477682SAli.Bahrami@Sun.COM 		WS_RSKIP_LIMIT(lstr, s2);
2487682SAli.Bahrami@Sun.COM 
2497682SAli.Bahrami@Sun.COM 		/* Assume this is a plain string */
2507682SAli.Bahrami@Sun.COM 		rec->opt_type = MATCH_OPT_NAME;
2517682SAli.Bahrami@Sun.COM 		rec->value.name.version = lstr;
2527682SAli.Bahrami@Sun.COM 
2537682SAli.Bahrami@Sun.COM 		/*
2547682SAli.Bahrami@Sun.COM 		 * If s2 points at a closing paren, then this might
2557682SAli.Bahrami@Sun.COM 		 * be a MATCH_OPT_NEED_VER case. Otherwise we're done.
2567682SAli.Bahrami@Sun.COM 		 */
2577682SAli.Bahrami@Sun.COM 		if ((s2 == lstr) || (*(s2 - 1) != ')'))
2587682SAli.Bahrami@Sun.COM 			return;
2597682SAli.Bahrami@Sun.COM 
2607682SAli.Bahrami@Sun.COM 		/* We have a closing paren. Locate the opening one. */
2617682SAli.Bahrami@Sun.COM 		for (s1 = lstr; *s1 && (*s1 != '('); s1++)
2627682SAli.Bahrami@Sun.COM 			;
2637682SAli.Bahrami@Sun.COM 		if (*s1 != '(')
2647682SAli.Bahrami@Sun.COM 			return;
2657682SAli.Bahrami@Sun.COM 
2667682SAli.Bahrami@Sun.COM 		rec->opt_type = MATCH_OPT_NEED_VER;
2677682SAli.Bahrami@Sun.COM 		rec->value.name.needobj = lstr;
2687682SAli.Bahrami@Sun.COM 		rec->value.name.version = s1 + 1;
2697682SAli.Bahrami@Sun.COM 		s2--;		/* Points at closing paren */
2707682SAli.Bahrami@Sun.COM 
2717682SAli.Bahrami@Sun.COM 		/* Remove whitespace from head/tail of version */
2727682SAli.Bahrami@Sun.COM 		WS_SKIP_LIMIT(rec->value.name.version, s2);
2737682SAli.Bahrami@Sun.COM 		WS_RSKIP_LIMIT(rec->value.name.version, s2);
2747682SAli.Bahrami@Sun.COM 
2757682SAli.Bahrami@Sun.COM 		/* Terminate needobj, skipping trailing whitespace */
2767682SAli.Bahrami@Sun.COM 		WS_RSKIP_LIMIT(rec->value.name.needobj, s1);
2777682SAli.Bahrami@Sun.COM 
2787682SAli.Bahrami@Sun.COM 		return;
2797682SAli.Bahrami@Sun.COM 	}
2807682SAli.Bahrami@Sun.COM 
2817682SAli.Bahrami@Sun.COM 
2827682SAli.Bahrami@Sun.COM 	/* If we get here, we are looking at a -I index option */
2837682SAli.Bahrami@Sun.COM 	rec->value.ndx.start = strtol(str, &s2, 10);
2847682SAli.Bahrami@Sun.COM 	/* Value must use some of the input, and be positive */
2857682SAli.Bahrami@Sun.COM 	if ((str == s2) || (rec->value.ndx.start < 1))
2867682SAli.Bahrami@Sun.COM 		goto syntax_error;
2877682SAli.Bahrami@Sun.COM 	str = s2;
2887682SAli.Bahrami@Sun.COM 
2897682SAli.Bahrami@Sun.COM 	WS_SKIP(str);
2907682SAli.Bahrami@Sun.COM 	if (*str != ':') {
2917682SAli.Bahrami@Sun.COM 		rec->opt_type = MATCH_OPT_NDX;
2927682SAli.Bahrami@Sun.COM 	} else {
2937682SAli.Bahrami@Sun.COM 		str++;					/* Skip the ':' */
2947682SAli.Bahrami@Sun.COM 		rec->opt_type = MATCH_OPT_RANGE;
2957682SAli.Bahrami@Sun.COM 		WS_SKIP(str);
2967682SAli.Bahrami@Sun.COM 		if (*str == '\0') {
2977682SAli.Bahrami@Sun.COM 			rec->value.ndx.end = -1;	/* Indicates "to end" */
2987682SAli.Bahrami@Sun.COM 		} else {
2997682SAli.Bahrami@Sun.COM 			rec->value.ndx.end = strtol(str, &s2, 10);
3007682SAli.Bahrami@Sun.COM 			if ((str == s2) || (rec->value.ndx.end < 0))
3017682SAli.Bahrami@Sun.COM 				goto syntax_error;
3027682SAli.Bahrami@Sun.COM 			str = s2;
3037682SAli.Bahrami@Sun.COM 			WS_SKIP(str);
3047682SAli.Bahrami@Sun.COM 		}
3057682SAli.Bahrami@Sun.COM 	}
3067682SAli.Bahrami@Sun.COM 
3077682SAli.Bahrami@Sun.COM 	/* If we are successful, there is nothing left to parse */
3087682SAli.Bahrami@Sun.COM 	if (*str == '\0')
3097682SAli.Bahrami@Sun.COM 		return;
3107682SAli.Bahrami@Sun.COM 
3117682SAli.Bahrami@Sun.COM 	/*
3127682SAli.Bahrami@Sun.COM 	 * If we get here, there is leftover input. Fall through
3137682SAli.Bahrami@Sun.COM 	 * to issue a syntax error.
3147682SAli.Bahrami@Sun.COM 	 */
3157682SAli.Bahrami@Sun.COM syntax_error:
3167682SAli.Bahrami@Sun.COM 	(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF), cname);
3177682SAli.Bahrami@Sun.COM 	exit(1);
3187682SAli.Bahrami@Sun.COM 
3197682SAli.Bahrami@Sun.COM #undef	WS_SKIP
3207682SAli.Bahrami@Sun.COM #undef	WS_SKIP_LIMIT
3217682SAli.Bahrami@Sun.COM #undef	WS_RSKIP_LIMIT
3227682SAli.Bahrami@Sun.COM }
3237682SAli.Bahrami@Sun.COM 
3247682SAli.Bahrami@Sun.COM /*
3257682SAli.Bahrami@Sun.COM  * Returns True (1) if the version with the given name or index should
3267682SAli.Bahrami@Sun.COM  * be displayed, and False (0) if it should not be.
3277682SAli.Bahrami@Sun.COM  *
3287682SAli.Bahrami@Sun.COM  * entry:
3297682SAli.Bahrami@Sun.COM  *	needobj - NULL for VERDEF records, the name of the
3307682SAli.Bahrami@Sun.COM  *		needed object for VERNEED.
3317682SAli.Bahrami@Sun.COM  *	version - NULL, or needed version
3327682SAli.Bahrami@Sun.COM  *	ndx - Versym index of version under consideration, or a value less
3337682SAli.Bahrami@Sun.COM  *		than 1 to indicate that no valid index is given.
3347682SAli.Bahrami@Sun.COM  *
3357682SAli.Bahrami@Sun.COM  * exit:
3367682SAli.Bahrami@Sun.COM  *	True will be returned if the given name/index matches those given
3377682SAli.Bahrami@Sun.COM  *	by one of the -I or -N command line options, or if no such option
3387682SAli.Bahrami@Sun.COM  *	was used in the command invocation.
3397682SAli.Bahrami@Sun.COM  */
3407682SAli.Bahrami@Sun.COM int
match(const char * needobj,const char * version,int ndx)3417682SAli.Bahrami@Sun.COM match(const char *needobj, const char *version, int ndx)
3427682SAli.Bahrami@Sun.COM {
3437682SAli.Bahrami@Sun.COM 	Aliste		_idx;
3447682SAli.Bahrami@Sun.COM 	match_rec_t	*rec;
3457682SAli.Bahrami@Sun.COM 	const char	*str;
3467682SAli.Bahrami@Sun.COM 
3477682SAli.Bahrami@Sun.COM 	/* If there is no match list, then we approve everything */
3487682SAli.Bahrami@Sun.COM 	if (alist_nitems(match_list) == 0)
3497682SAli.Bahrami@Sun.COM 		return (1);
3507682SAli.Bahrami@Sun.COM 
3517682SAli.Bahrami@Sun.COM 	/* Run through the match records and check for a hit */
3527682SAli.Bahrami@Sun.COM 	for (ALIST_TRAVERSE(match_list, _idx, rec)) {
3537682SAli.Bahrami@Sun.COM 		switch (rec->opt_type) {
3547682SAli.Bahrami@Sun.COM 		case MATCH_OPT_NAME:
3557682SAli.Bahrami@Sun.COM 			if (needobj)
3567682SAli.Bahrami@Sun.COM 				str = needobj;
3577682SAli.Bahrami@Sun.COM 			else if (version)
3587682SAli.Bahrami@Sun.COM 				str = version;
3597682SAli.Bahrami@Sun.COM 			else
3607682SAli.Bahrami@Sun.COM 				break;
3617682SAli.Bahrami@Sun.COM 			if (strcmp(rec->value.name.version, str) == 0)
3627682SAli.Bahrami@Sun.COM 				return (1);
3637682SAli.Bahrami@Sun.COM 			break;
3647682SAli.Bahrami@Sun.COM 		case MATCH_OPT_NEED_VER:
3657682SAli.Bahrami@Sun.COM 			if (needobj && version &&
3667682SAli.Bahrami@Sun.COM 			    (strcmp(rec->value.name.needobj, needobj) == 0) &&
3677682SAli.Bahrami@Sun.COM 			    (strcmp(rec->value.name.version, version) == 0))
3687682SAli.Bahrami@Sun.COM 				return (1);
3697682SAli.Bahrami@Sun.COM 			break;
3707682SAli.Bahrami@Sun.COM 		case MATCH_OPT_NDX:
3717682SAli.Bahrami@Sun.COM 			if ((ndx > 0) && (ndx == rec->value.ndx.start))
3727682SAli.Bahrami@Sun.COM 				return (1);
3737682SAli.Bahrami@Sun.COM 			break;
3747682SAli.Bahrami@Sun.COM 		case MATCH_OPT_RANGE:
3757682SAli.Bahrami@Sun.COM 			/*
3767682SAli.Bahrami@Sun.COM 			 * A range end value less than 0 means that any value
3777682SAli.Bahrami@Sun.COM 			 * above the start is acceptible.
3787682SAli.Bahrami@Sun.COM 			 */
3797682SAli.Bahrami@Sun.COM 			if ((ndx > 0) &&
3807682SAli.Bahrami@Sun.COM 			    (ndx >= rec->value.ndx.start) &&
3817682SAli.Bahrami@Sun.COM 			    ((rec->value.ndx.end < 0) ||
3827682SAli.Bahrami@Sun.COM 			    (ndx <= rec->value.ndx.end)))
3837682SAli.Bahrami@Sun.COM 				return (1);
3847682SAli.Bahrami@Sun.COM 			break;
3857682SAli.Bahrami@Sun.COM 		}
3867682SAli.Bahrami@Sun.COM 	}
3877682SAli.Bahrami@Sun.COM 
3887682SAli.Bahrami@Sun.COM 	/* Nothing matched */
3897682SAli.Bahrami@Sun.COM 	return (0);
3907682SAli.Bahrami@Sun.COM }
3917682SAli.Bahrami@Sun.COM 
3927682SAli.Bahrami@Sun.COM /*
3937682SAli.Bahrami@Sun.COM  * List the symbols that belong to a specified version
3947682SAli.Bahrami@Sun.COM  *
3957682SAli.Bahrami@Sun.COM  * entry:
3967682SAli.Bahrami@Sun.COM  *	vsdata - VERSYM related data from the object
3977682SAli.Bahrami@Sun.COM  *	vd_ndx - The VERSYM index for symbols to display
3987682SAli.Bahrami@Sun.COM  *	vd_name - Version name
3997682SAli.Bahrami@Sun.COM  *	needobj - NULL for symbols corresponding to a VERDEF
4007682SAli.Bahrami@Sun.COM  *		record. Name of the needed object in the case
4017682SAli.Bahrami@Sun.COM  *		of a VERNEED record.
4027682SAli.Bahrami@Sun.COM  *	file - Object file
4037682SAli.Bahrami@Sun.COM  */
4047682SAli.Bahrami@Sun.COM static void
gvers_syms(const Gver_sym_data * vsdata,GElf_Half vd_ndx,const char * vd_name,const char * needobj,const char * file)4057682SAli.Bahrami@Sun.COM gvers_syms(const Gver_sym_data *vsdata, GElf_Half vd_ndx,
4067682SAli.Bahrami@Sun.COM     const char *vd_name, const char *needobj, const char *file)
4077682SAli.Bahrami@Sun.COM {
4087682SAli.Bahrami@Sun.COM 	GElf_Sym	sym;
4097682SAli.Bahrami@Sun.COM 	int		_symn;
4107682SAli.Bahrami@Sun.COM 
4117682SAli.Bahrami@Sun.COM 	for (_symn = 0; _symn < vsdata->vsd_symn; _symn++) {
4127682SAli.Bahrami@Sun.COM 		size_t		size =	0;
4137682SAli.Bahrami@Sun.COM 		const char	*name;
4147682SAli.Bahrami@Sun.COM 
4157682SAli.Bahrami@Sun.COM 		if (vsdata->vsd_vsp[_symn] != vd_ndx)
4167682SAli.Bahrami@Sun.COM 			continue;
4177682SAli.Bahrami@Sun.COM 
4187682SAli.Bahrami@Sun.COM 		(void) gelf_getsym(vsdata->vsd_sym_data, _symn, &sym);
4197682SAli.Bahrami@Sun.COM 		name = demangle(vsdata->vsd_strs + sym.st_name);
4207682SAli.Bahrami@Sun.COM 
4217682SAli.Bahrami@Sun.COM 		/*
4227682SAli.Bahrami@Sun.COM 		 * Symbols that reference a VERDEF record
4237682SAli.Bahrami@Sun.COM 		 * have some extra details to handle.
4247682SAli.Bahrami@Sun.COM 		 */
4257682SAli.Bahrami@Sun.COM 		if (needobj == NULL) {
4267682SAli.Bahrami@Sun.COM 			/*
4277682SAli.Bahrami@Sun.COM 			 * For data symbols defined by this object,
4287682SAli.Bahrami@Sun.COM 			 * determine the size.
4297682SAli.Bahrami@Sun.COM 			 */
4307682SAli.Bahrami@Sun.COM 			if ((GELF_ST_TYPE(sym.st_info) == STT_OBJECT) ||
4317682SAli.Bahrami@Sun.COM 			    (GELF_ST_TYPE(sym.st_info) == STT_COMMON) ||
4327682SAli.Bahrami@Sun.COM 			    (GELF_ST_TYPE(sym.st_info) == STT_TLS))
4337682SAli.Bahrami@Sun.COM 				size = (size_t)sym.st_size;
4347682SAli.Bahrami@Sun.COM 
4357682SAli.Bahrami@Sun.COM 			/*
4367682SAli.Bahrami@Sun.COM 			 * Only output the version symbol when the verbose
4377682SAli.Bahrami@Sun.COM 			 * flag is used.
4387682SAli.Bahrami@Sun.COM 			 */
4397682SAli.Bahrami@Sun.COM 			if (!vflag && (sym.st_shndx == SHN_ABS) &&
4407682SAli.Bahrami@Sun.COM 			    (strcmp(name, vd_name) == 0))
4417682SAli.Bahrami@Sun.COM 				continue;
4427682SAli.Bahrami@Sun.COM 		}
4437682SAli.Bahrami@Sun.COM 
4447682SAli.Bahrami@Sun.COM 		if (oflag) {
4457682SAli.Bahrami@Sun.COM 			if (needobj == NULL)
4467682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM_OFIL),
4477682SAli.Bahrami@Sun.COM 				    file, vd_name);
4487682SAli.Bahrami@Sun.COM 			else
4497682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM_NEED_OFIL),
4507682SAli.Bahrami@Sun.COM 				    file, needobj, vd_name);
4517682SAli.Bahrami@Sun.COM 
4527682SAli.Bahrami@Sun.COM 			if (size)
4537682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM_SZ_OFLG),
4547682SAli.Bahrami@Sun.COM 				    name, (ulong_t)size);
4557682SAli.Bahrami@Sun.COM 			else
4567682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM_OFLG), name);
4577682SAli.Bahrami@Sun.COM 		} else {
4587682SAli.Bahrami@Sun.COM 			if (size)
4597682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM_SZ), name,
4607682SAli.Bahrami@Sun.COM 				    (ulong_t)size);
4617682SAli.Bahrami@Sun.COM 			else
4627682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SYM), name);
4637682SAli.Bahrami@Sun.COM 		}
4647682SAli.Bahrami@Sun.COM 	}
4657682SAli.Bahrami@Sun.COM }
4667682SAli.Bahrami@Sun.COM 
4677682SAli.Bahrami@Sun.COM /*
4680Sstevel@tonic-gate  * Print any reduced symbols.  The convention is that reduced symbols exist as
4690Sstevel@tonic-gate  * LOCL entries in the .symtab, between the FILE symbol for the output file and
4700Sstevel@tonic-gate  * the first FILE symbol for any input file used to build the output file.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate static void
sym_local(Cache * cache,Cache * csym,const char * file)4730Sstevel@tonic-gate sym_local(Cache *cache, Cache *csym, const char *file)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	int		symn, _symn, found = 0;
4760Sstevel@tonic-gate 	GElf_Shdr	shdr;
4770Sstevel@tonic-gate 	GElf_Sym	sym;
4787682SAli.Bahrami@Sun.COM 	char		*strs;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	(void) gelf_getshdr(csym->c_scn, &shdr);
4810Sstevel@tonic-gate 	strs = (char *)cache[shdr.sh_link].c_data->d_buf;
4820Sstevel@tonic-gate 	/* LINTED */
4830Sstevel@tonic-gate 	symn = shdr.sh_info;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * Verify symtab[1] is the output file symbol.
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 	(void) gelf_getsym(csym->c_data, 1, &sym);
4890Sstevel@tonic-gate 	if (GELF_ST_TYPE(sym.st_info) != STT_FILE) {
4900Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_VER_UNREDSYMS), cname,
4910Sstevel@tonic-gate 		    file);
4920Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_VER_NOTSTTFILE),
4930Sstevel@tonic-gate 		    csym->c_name);
4940Sstevel@tonic-gate 		return;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Scan the remaining symbols until the next file symbol is found.
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 	for (_symn = 2; _symn < symn; _symn++) {
5010Sstevel@tonic-gate 		const char	*name;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		(void) gelf_getsym(csym->c_data, _symn, &sym);
5040Sstevel@tonic-gate 		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION)
5050Sstevel@tonic-gate 			continue;
5060Sstevel@tonic-gate 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE)
5070Sstevel@tonic-gate 			break;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		/*
5100Sstevel@tonic-gate 		 * Its possible that section symbols are followed immediately
5110Sstevel@tonic-gate 		 * by globals.  This is the case if an object (filter) is
5120Sstevel@tonic-gate 		 * generated exclusively from mapfile symbol definitions.
5130Sstevel@tonic-gate 		 */
5140Sstevel@tonic-gate 		if (GELF_ST_BIND(sym.st_info) != STB_LOCAL)
5150Sstevel@tonic-gate 			break;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		name = demangle(strs + sym.st_name);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		if (oflag) {
5207682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_LOCSYM_OFLG),
5217682SAli.Bahrami@Sun.COM 			    file, name);
5220Sstevel@tonic-gate 		} else {
5230Sstevel@tonic-gate 			if (found == 0) {
5240Sstevel@tonic-gate 				found = 1;
5257682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_LOCSYM_HDR));
5260Sstevel@tonic-gate 			}
5277682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_LOCSYM), name);
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5337682SAli.Bahrami@Sun.COM  * Print data from the files VERNEED section.
5347682SAli.Bahrami@Sun.COM  *
5357682SAli.Bahrami@Sun.COM  * If we have been asked to display symbols, then the
5367682SAli.Bahrami@Sun.COM  * output format follows that used for verdef sections,
5377682SAli.Bahrami@Sun.COM  * with each version displayed separately. For instance:
5387682SAli.Bahrami@Sun.COM  *
5397682SAli.Bahrami@Sun.COM  *	libc.so.1 (SUNW_1.7):
5407682SAli.Bahrami@Sun.COM  *		sym1;
5417682SAli.Bahrami@Sun.COM  *		sym2;
5427682SAli.Bahrami@Sun.COM  *	libc.so.1 (SUNW_1.9):
5437682SAli.Bahrami@Sun.COM  *		sym3;
5447682SAli.Bahrami@Sun.COM  *
5457682SAli.Bahrami@Sun.COM  * If we are not displaying symbols, then a terse format
5467682SAli.Bahrami@Sun.COM  * is used, which combines all the needed versions from
5477682SAli.Bahrami@Sun.COM  * a given object into a single line. In this case, the
5487682SAli.Bahrami@Sun.COM  * versions are shown whether or not they contribute symbols.
5497682SAli.Bahrami@Sun.COM  *
5507682SAli.Bahrami@Sun.COM  *	libc.so.1 (SUNW_1.7, SUNW_1.9);
5510Sstevel@tonic-gate  */
5520Sstevel@tonic-gate static int
gvers_need(Cache * cache,Cache * need,const Gver_sym_data * vsdata,const char * file)5537682SAli.Bahrami@Sun.COM gvers_need(Cache *cache, Cache *need, const Gver_sym_data *vsdata,
5547682SAli.Bahrami@Sun.COM     const char *file)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	unsigned int	num, _num;
5570Sstevel@tonic-gate 	char		*strs;
5580Sstevel@tonic-gate 	GElf_Verneed	*vnd = need->c_data->d_buf;
5590Sstevel@tonic-gate 	GElf_Shdr	shdr;
5600Sstevel@tonic-gate 	int		error = 0;
5617682SAli.Bahrami@Sun.COM 	int		show = vflag || (vsdata == NULL) || !oflag;
5627682SAli.Bahrami@Sun.COM 
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	(void) gelf_getshdr(need->c_scn, &shdr);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * Verify the version revision.  We only check the first version
5680Sstevel@tonic-gate 	 * structure as it is assumed all other version structures in this
5690Sstevel@tonic-gate 	 * data section will be of the same revision.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	if (vnd->vn_version > VER_DEF_CURRENT)
5720Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_VER_HIGHREV), cname, file,
5730Sstevel@tonic-gate 		    vnd->vn_version, VER_DEF_CURRENT);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * Get the data buffer for the associated string table.
5770Sstevel@tonic-gate 	 */
5780Sstevel@tonic-gate 	strs = (char *)cache[shdr.sh_link].c_data->d_buf;
5790Sstevel@tonic-gate 	num = shdr.sh_info;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
5820Sstevel@tonic-gate 	    vnd = (GElf_Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
5837682SAli.Bahrami@Sun.COM 		GElf_Vernaux	*vnap;
5847682SAli.Bahrami@Sun.COM 		Word		ndx;
585*11993SAli.Bahrami@Sun.COM 		const char	*needobj, *dep;
586*11993SAli.Bahrami@Sun.COM 		int		started = 0, listcnt = 0;
5870Sstevel@tonic-gate 
5887682SAli.Bahrami@Sun.COM 		vnap = (GElf_Vernaux *) ((uintptr_t)vnd + vnd->vn_aux);
5897682SAli.Bahrami@Sun.COM 
5907682SAli.Bahrami@Sun.COM 		/* Obtain the needed object file name */
5917682SAli.Bahrami@Sun.COM 		needobj = (char *)(strs + vnd->vn_file);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		error = 1;
5940Sstevel@tonic-gate 
5957682SAli.Bahrami@Sun.COM 		/* Process the versions needed from this object */
5967682SAli.Bahrami@Sun.COM 		for (ndx = 0; ndx < vnd->vn_cnt; ndx++,
5977682SAli.Bahrami@Sun.COM 		    vnap = (GElf_Vernaux *)((uintptr_t)vnap + vnap->vna_next)) {
5987682SAli.Bahrami@Sun.COM 			Conv_ver_flags_buf_t	ver_flags_buf;
5997682SAli.Bahrami@Sun.COM 
6007682SAli.Bahrami@Sun.COM 			dep = (char *)(strs + vnap->vna_name);
6017682SAli.Bahrami@Sun.COM 
6027682SAli.Bahrami@Sun.COM 			if (!match(needobj, dep, vnap->vna_other))
6037682SAli.Bahrami@Sun.COM 				continue;
6040Sstevel@tonic-gate 
6057682SAli.Bahrami@Sun.COM 			if (show) {
6067682SAli.Bahrami@Sun.COM 				if ((started == 0) || (vsdata != NULL))  {
6077682SAli.Bahrami@Sun.COM 					/*
6087682SAli.Bahrami@Sun.COM 					 * If one-line ouput is called for
6097682SAli.Bahrami@Sun.COM 					 * display the filename being processed.
6107682SAli.Bahrami@Sun.COM 					 */
6117682SAli.Bahrami@Sun.COM 					if (oflag && show)
6127682SAli.Bahrami@Sun.COM 						(void) printf(
6137682SAli.Bahrami@Sun.COM 						    MSG_ORIG(MSG_FMT_OFIL),
6147682SAli.Bahrami@Sun.COM 						    file);
6157682SAli.Bahrami@Sun.COM 
6167682SAli.Bahrami@Sun.COM 					(void) printf(
6177682SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_BEGIN),
6187682SAli.Bahrami@Sun.COM 					    needobj);
6197682SAli.Bahrami@Sun.COM 					started = 1;
6207682SAli.Bahrami@Sun.COM 				}
6210Sstevel@tonic-gate 
6227682SAli.Bahrami@Sun.COM 				/*
6237682SAli.Bahrami@Sun.COM 				 * If not showing symbols, only show INFO
6247682SAli.Bahrami@Sun.COM 				 * versions in verbose mode. They don't
6257682SAli.Bahrami@Sun.COM 				 * actually contribute to the version
6267682SAli.Bahrami@Sun.COM 				 * interface as seen by rtld, so listing them
6277682SAli.Bahrami@Sun.COM 				 * without qualification can be misleading.
6287682SAli.Bahrami@Sun.COM 				 */
6297682SAli.Bahrami@Sun.COM 				if (vflag || (vsdata != NULL) ||
6307682SAli.Bahrami@Sun.COM 				    (alist_nitems(match_list) != 0) ||
6317682SAli.Bahrami@Sun.COM 				    !(vnap->vna_flags & VER_FLG_INFO)) {
632*11993SAli.Bahrami@Sun.COM 					const char *fmt = (listcnt == 0) ?
633*11993SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_FIRST) :
634*11993SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_NEXT);
635*11993SAli.Bahrami@Sun.COM 
636*11993SAli.Bahrami@Sun.COM 					if (vsdata == NULL)
637*11993SAli.Bahrami@Sun.COM 						listcnt++;
6387682SAli.Bahrami@Sun.COM 					(void) printf(fmt, dep);
6390Sstevel@tonic-gate 
6407682SAli.Bahrami@Sun.COM 					/* Show non-zero flags */
6417682SAli.Bahrami@Sun.COM 					if (vflag && (vnap->vna_flags != 0))
6427682SAli.Bahrami@Sun.COM 						(void) printf(
6437682SAli.Bahrami@Sun.COM 						    MSG_ORIG(MSG_FMT_VER_FLG),
6447682SAli.Bahrami@Sun.COM 						    conv_ver_flags(
6457682SAli.Bahrami@Sun.COM 						    vnap->vna_flags,
6467682SAli.Bahrami@Sun.COM 						    CONV_FMT_NOBKT,
6477682SAli.Bahrami@Sun.COM 						    &ver_flags_buf));
6487682SAli.Bahrami@Sun.COM 				}
6497682SAli.Bahrami@Sun.COM 				if (vsdata != NULL)
6507682SAli.Bahrami@Sun.COM 					(void) printf(oflag ?
6517682SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_END_SEM) :
6527682SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_END_COL));
6537682SAli.Bahrami@Sun.COM 			}
6547682SAli.Bahrami@Sun.COM 
6557682SAli.Bahrami@Sun.COM 			/*
6567682SAli.Bahrami@Sun.COM 			 * If we are showing symbols, and vna_other is
6577682SAli.Bahrami@Sun.COM 			 * non-zero, list them here.
6587682SAli.Bahrami@Sun.COM 			 *
6597682SAli.Bahrami@Sun.COM 			 * A value of 0 means that this object uses
6607682SAli.Bahrami@Sun.COM 			 * traditional Solaris versioning rules, under
6617682SAli.Bahrami@Sun.COM 			 * which VERSYM does not contain indexes to VERNEED
6627682SAli.Bahrami@Sun.COM 			 * records. In this case, there is nothing to show.
6637682SAli.Bahrami@Sun.COM 			 */
6647682SAli.Bahrami@Sun.COM 			if (vsdata && (vnap->vna_other > 0))
6657682SAli.Bahrami@Sun.COM 				gvers_syms(vsdata, vnap->vna_other,
6667682SAli.Bahrami@Sun.COM 				    dep, needobj, file);
6670Sstevel@tonic-gate 		}
6687682SAli.Bahrami@Sun.COM 		if (show && started && (vsdata == NULL))
6697682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_LIST_END_SEM));
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	return (error);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6757682SAli.Bahrami@Sun.COM  * Return a GVer_desc descriptor for the given version if one
6767682SAli.Bahrami@Sun.COM  * exists.
6777682SAli.Bahrami@Sun.COM  *
6787682SAli.Bahrami@Sun.COM  * entry:
6797682SAli.Bahrami@Sun.COM  *	name - Version name
6807682SAli.Bahrami@Sun.COM  *	hash - ELF hash of name
6817682SAli.Bahrami@Sun.COM  *	lst - APlist of existing descriptors.
6827682SAli.Bahrami@Sun.COM  *	file - Object file containing the version
6837682SAli.Bahrami@Sun.COM  *
6847682SAli.Bahrami@Sun.COM  * exit:
6857682SAli.Bahrami@Sun.COM  *	Return the corresponding GVer_desc struct if it
6867682SAli.Bahrami@Sun.COM  *	exists, and NULL otherwise.
6870Sstevel@tonic-gate  */
6887682SAli.Bahrami@Sun.COM static GVer_desc *
gvers_find(const char * name,unsigned long hash,APlist * lst)6897682SAli.Bahrami@Sun.COM gvers_find(const char *name, unsigned long hash, APlist *lst)
6900Sstevel@tonic-gate {
6917682SAli.Bahrami@Sun.COM 	Aliste		idx;
6927682SAli.Bahrami@Sun.COM 	GVer_desc	*vdp;
6930Sstevel@tonic-gate 
6947682SAli.Bahrami@Sun.COM 	for (APLIST_TRAVERSE(lst, idx, vdp))
6957682SAli.Bahrami@Sun.COM 		if ((vdp->vd_hash == hash) &&
6967682SAli.Bahrami@Sun.COM 		    (strcmp(vdp->vd_name, name) == 0))
6977682SAli.Bahrami@Sun.COM 			return (vdp);
6980Sstevel@tonic-gate 
6997682SAli.Bahrami@Sun.COM 	return (NULL);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7027682SAli.Bahrami@Sun.COM /*
7037682SAli.Bahrami@Sun.COM  * Return a GVer_desc descriptor for the given version.
7047682SAli.Bahrami@Sun.COM  *
7057682SAli.Bahrami@Sun.COM  * entry:
7067682SAli.Bahrami@Sun.COM  *	name - Version name
7077682SAli.Bahrami@Sun.COM  *	hash - ELF hash of name
7087682SAli.Bahrami@Sun.COM  *	lst - List of existing descriptors.
7097682SAli.Bahrami@Sun.COM  *	file - Object file containing the version
7107682SAli.Bahrami@Sun.COM  *
7117682SAli.Bahrami@Sun.COM  * exit:
7127682SAli.Bahrami@Sun.COM  *	Return the corresponding GVer_desc struct. If the
7137682SAli.Bahrami@Sun.COM  * 	descriptor does not already exist, it is created.
7147682SAli.Bahrami@Sun.COM  *	On error, a fatal error is issued and the process exits.
7157682SAli.Bahrami@Sun.COM  */
7160Sstevel@tonic-gate static GVer_desc *
gvers_desc(const char * name,unsigned long hash,APlist ** lst,const char * file)7177682SAli.Bahrami@Sun.COM gvers_desc(const char *name, unsigned long hash, APlist **lst, const char *file)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	GVer_desc	*vdp;
7200Sstevel@tonic-gate 
7217682SAli.Bahrami@Sun.COM 	if ((vdp = gvers_find(name, hash, *lst)) == NULL) {
7227682SAli.Bahrami@Sun.COM 		if ((vdp = calloc(sizeof (GVer_desc), 1)) == NULL) {
7230Sstevel@tonic-gate 			int err = errno;
7240Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), cname,
7250Sstevel@tonic-gate 			    file, strerror(err));
7260Sstevel@tonic-gate 			exit(1);
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		vdp->vd_name = name;
7300Sstevel@tonic-gate 		vdp->vd_hash = hash;
7310Sstevel@tonic-gate 
7327682SAli.Bahrami@Sun.COM 		pvs_aplist_append(lst, vdp, file);
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 	return (vdp);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7377682SAli.Bahrami@Sun.COM /*
7387682SAli.Bahrami@Sun.COM  * Insert a version dependency for the given GVer_desc descriptor.
7397682SAli.Bahrami@Sun.COM  *
7407682SAli.Bahrami@Sun.COM  * entry:
7417682SAli.Bahrami@Sun.COM  *	name - Dependency version name
7427682SAli.Bahrami@Sun.COM  *	hash - ELF hash of name
7437682SAli.Bahrami@Sun.COM  *	lst - List of existing descriptors.
7447682SAli.Bahrami@Sun.COM  *	vdp - Existing version descriptor to which the dependency
7457682SAli.Bahrami@Sun.COM  *		is to be added.
7467682SAli.Bahrami@Sun.COM  *	file - Object file containing the version
7477682SAli.Bahrami@Sun.COM  *
7487682SAli.Bahrami@Sun.COM  * exit:
7497682SAli.Bahrami@Sun.COM  *	A descriptor for the dependency version is looked up
7507682SAli.Bahrami@Sun.COM  *	(created if necessary), and then added to the dependency
7517682SAli.Bahrami@Sun.COM  *	list for vdp. Returns the dependency descriptor. On error,
7527682SAli.Bahrami@Sun.COM  *	a fatal error is issued and the process exits.
7537682SAli.Bahrami@Sun.COM  */
7540Sstevel@tonic-gate static GVer_desc *
gvers_depend(const char * name,unsigned long hash,GVer_desc * vdp,APlist ** lst,const char * file)7557682SAli.Bahrami@Sun.COM gvers_depend(const char *name, unsigned long hash, GVer_desc *vdp, APlist **lst,
7560Sstevel@tonic-gate     const char *file)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	GVer_desc	*_vdp;
7590Sstevel@tonic-gate 
7607682SAli.Bahrami@Sun.COM 	_vdp = gvers_desc(name, hash, lst, file);
7617682SAli.Bahrami@Sun.COM 	pvs_aplist_append(&vdp->vd_deps, _vdp, file);
7620Sstevel@tonic-gate 	return (vdp);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate static void
gvers_derefer(GVer_desc * vdp,int weak)7667682SAli.Bahrami@Sun.COM gvers_derefer(GVer_desc *vdp, int weak)
7670Sstevel@tonic-gate {
7687682SAli.Bahrami@Sun.COM 	Aliste		idx;
7697682SAli.Bahrami@Sun.COM 	GVer_desc 	*_vdp;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * If the head of the list was a weak then we only clear out
7730Sstevel@tonic-gate 	 * weak dependencies, but if the head of the list was 'strong'
7740Sstevel@tonic-gate 	 * we clear the REFER bit on all dependencies.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 	if ((weak && (vdp->vd_flags & VER_FLG_WEAK)) || (!weak))
7770Sstevel@tonic-gate 		vdp->vd_flags &= ~FLG_VER_AVAIL;
7780Sstevel@tonic-gate 
7797682SAli.Bahrami@Sun.COM 	for (APLIST_TRAVERSE(vdp->vd_deps, idx, _vdp))
7800Sstevel@tonic-gate 		gvers_derefer(_vdp, weak);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate static void
recurse_syms(const Gver_sym_data * vsdata,GVer_desc * vdp,const char * file)7857682SAli.Bahrami@Sun.COM recurse_syms(const Gver_sym_data *vsdata, GVer_desc *vdp, const char *file)
7860Sstevel@tonic-gate {
7877682SAli.Bahrami@Sun.COM 	Aliste		idx;
7880Sstevel@tonic-gate 	GVer_desc	*_vdp;
7890Sstevel@tonic-gate 
7907682SAli.Bahrami@Sun.COM 	for (APLIST_TRAVERSE(vdp->vd_deps, idx, _vdp)) {
7910Sstevel@tonic-gate 		if (!oflag)
7927682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_TNCO), _vdp->vd_name);
7937682SAli.Bahrami@Sun.COM 		gvers_syms(vsdata, _vdp->vd_ndx, _vdp->vd_name, NULL, file);
7947682SAli.Bahrami@Sun.COM 		if (aplist_nitems(_vdp->vd_deps) != 0)
7957682SAli.Bahrami@Sun.COM 			recurse_syms(vsdata, _vdp, file);
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate  * Print the files version definition sections.
8020Sstevel@tonic-gate  */
8030Sstevel@tonic-gate static int
gvers_def(Cache * cache,Cache * def,const Gver_sym_data * vsdata,const char * file)8047682SAli.Bahrami@Sun.COM gvers_def(Cache *cache, Cache *def, const Gver_sym_data *vsdata,
8057682SAli.Bahrami@Sun.COM     const char *file)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate 	unsigned int	num, _num;
8080Sstevel@tonic-gate 	char		*strs;
8090Sstevel@tonic-gate 	GElf_Verdef	*vdf = def->c_data->d_buf;
8100Sstevel@tonic-gate 	GElf_Shdr	shdr;
8117682SAli.Bahrami@Sun.COM 	GVer_desc	*vdp, *bvdp = NULL;
8127682SAli.Bahrami@Sun.COM 	Aliste		idx1;
8137682SAli.Bahrami@Sun.COM 	APlist		*verdefs = NULL;
8140Sstevel@tonic-gate 	int		error = 0;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * Verify the version revision.  We only check the first version
8180Sstevel@tonic-gate 	 * structure as it is assumed all other version structures in this
8190Sstevel@tonic-gate 	 * data section will be of the same revision.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (vdf->vd_version > VER_DEF_CURRENT) {
8220Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_VER_HIGHREV), cname, file,
8230Sstevel@tonic-gate 		    vdf->vd_version, VER_DEF_CURRENT);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	/*
8270Sstevel@tonic-gate 	 * Get the data buffer for the associated string table.
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	(void) gelf_getshdr(def->c_scn, &shdr);
8300Sstevel@tonic-gate 	strs = (char *)cache[shdr.sh_link].c_data->d_buf;
8310Sstevel@tonic-gate 	num = shdr.sh_info;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * Process the version definitions placing each on a version dependency
8350Sstevel@tonic-gate 	 * list.
8360Sstevel@tonic-gate 	 */
8370Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
8380Sstevel@tonic-gate 	    vdf = (GElf_Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
8390Sstevel@tonic-gate 		GElf_Half	cnt = vdf->vd_cnt;
8400Sstevel@tonic-gate 		GElf_Half	ndx = vdf->vd_ndx;
8417682SAli.Bahrami@Sun.COM 		GElf_Verdaux	*vdap;
8420Sstevel@tonic-gate 		const char	*_name;
8430Sstevel@tonic-gate 
8447682SAli.Bahrami@Sun.COM 		vdap = (GElf_Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
8457682SAli.Bahrami@Sun.COM 
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * Determine the version name and any dependencies.
8480Sstevel@tonic-gate 		 */
8490Sstevel@tonic-gate 		_name = (char *)(strs + vdap->vda_name);
8500Sstevel@tonic-gate 
8517682SAli.Bahrami@Sun.COM 		vdp = gvers_desc(_name, elf_hash(_name), &verdefs, file);
8520Sstevel@tonic-gate 		vdp->vd_ndx = ndx;
8530Sstevel@tonic-gate 		vdp->vd_flags = vdf->vd_flags | FLG_VER_AVAIL;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		vdap = (GElf_Verdaux *)((uintptr_t)vdap + vdap->vda_next);
8560Sstevel@tonic-gate 		for (cnt--; cnt; cnt--,
8570Sstevel@tonic-gate 		    vdap = (GElf_Verdaux *)((uintptr_t)vdap + vdap->vda_next)) {
8580Sstevel@tonic-gate 			_name = (char *)(strs + vdap->vda_name);
8590Sstevel@tonic-gate 			if (gvers_depend(_name, elf_hash(_name), vdp,
8607682SAli.Bahrami@Sun.COM 			    &verdefs, file) == NULL)
8610Sstevel@tonic-gate 				return (0);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * Remember the base version for possible later use.
8660Sstevel@tonic-gate 		 */
8670Sstevel@tonic-gate 		if (ndx == VER_NDX_GLOBAL)
8680Sstevel@tonic-gate 			bvdp = vdp;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Normalize the dependency list if required.
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	if (nflag) {
8757682SAli.Bahrami@Sun.COM 		for (APLIST_TRAVERSE(verdefs, idx1, vdp)) {
8767682SAli.Bahrami@Sun.COM 			Aliste		idx2;
8777682SAli.Bahrami@Sun.COM 			GVer_desc 	*_vdp;
8780Sstevel@tonic-gate 			int		type = vdp->vd_flags & VER_FLG_WEAK;
8790Sstevel@tonic-gate 
8807682SAli.Bahrami@Sun.COM 			for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp))
8810Sstevel@tonic-gate 				gvers_derefer(_vdp, type);
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		/*
8850Sstevel@tonic-gate 		 * Always dereference the base version.
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		if (bvdp)
8880Sstevel@tonic-gate 			bvdp->vd_flags &= ~FLG_VER_AVAIL;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Traverse the dependency list and print out the appropriate
8940Sstevel@tonic-gate 	 * information.
8950Sstevel@tonic-gate 	 */
8967682SAli.Bahrami@Sun.COM 	for (APLIST_TRAVERSE(verdefs, idx1, vdp)) {
8977682SAli.Bahrami@Sun.COM 		Aliste		idx2;
8987682SAli.Bahrami@Sun.COM 		GVer_desc 	*_vdp;
8990Sstevel@tonic-gate 		int		count;
9000Sstevel@tonic-gate 
9017682SAli.Bahrami@Sun.COM 		if (!match(NULL, vdp->vd_name, vdp->vd_ndx))
9020Sstevel@tonic-gate 			continue;
9037682SAli.Bahrami@Sun.COM 		if ((alist_nitems(match_list) == 0) &&
9047682SAli.Bahrami@Sun.COM 		    !(vdp->vd_flags & FLG_VER_AVAIL))
9050Sstevel@tonic-gate 			continue;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 		error = 1;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		if (vflag) {
9100Sstevel@tonic-gate 			/*
9110Sstevel@tonic-gate 			 * If the verbose flag is set determine if this version
9120Sstevel@tonic-gate 			 * has a `weak' attribute, and print any version
9130Sstevel@tonic-gate 			 * dependencies this version inherits.
9140Sstevel@tonic-gate 			 */
9150Sstevel@tonic-gate 			if (oflag)
9167682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_OFIL), file);
9177682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_VER_NAME), vdp->vd_name);
9187682SAli.Bahrami@Sun.COM 			if ((vdp->vd_flags & MSK_VER_USER) != 0) {
9197682SAli.Bahrami@Sun.COM 				Conv_ver_flags_buf_t	ver_flags_buf;
9207682SAli.Bahrami@Sun.COM 
9217682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_VER_FLG),
9227682SAli.Bahrami@Sun.COM 				    conv_ver_flags(
9237682SAli.Bahrami@Sun.COM 				    vdp->vd_flags & MSK_VER_USER,
9247682SAli.Bahrami@Sun.COM 				    CONV_FMT_NOBKT, &ver_flags_buf));
9257682SAli.Bahrami@Sun.COM 			}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 			count = 1;
9287682SAli.Bahrami@Sun.COM 			for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
9290Sstevel@tonic-gate 				const char	*_name = _vdp->vd_name;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 				if (count++ == 1) {
9327682SAli.Bahrami@Sun.COM 
9330Sstevel@tonic-gate 					if (oflag)
9347682SAli.Bahrami@Sun.COM 						(void) printf(
9357682SAli.Bahrami@Sun.COM 						    MSG_ORIG(MSG_FMT_IN_OFLG),
9367682SAli.Bahrami@Sun.COM 						    _name);
9370Sstevel@tonic-gate 					else if (vdp->vd_flags & VER_FLG_WEAK)
9387682SAli.Bahrami@Sun.COM 						(void) printf(
9397682SAli.Bahrami@Sun.COM 						    MSG_ORIG(MSG_FMT_IN_WEAK),
9407682SAli.Bahrami@Sun.COM 						    _name);
9410Sstevel@tonic-gate 					else
9427682SAli.Bahrami@Sun.COM 						(void) printf(
9437682SAli.Bahrami@Sun.COM 						    MSG_ORIG(MSG_FMT_IN),
9440Sstevel@tonic-gate 						    _name);
9450Sstevel@tonic-gate 				} else
9467682SAli.Bahrami@Sun.COM 					(void) printf(
9477682SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_FMT_LIST_NEXT), _name);
9480Sstevel@tonic-gate 			}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 			if (count != 1)
9517682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_IN_END));
9520Sstevel@tonic-gate 
9537682SAli.Bahrami@Sun.COM 			if (vsdata && !oflag)
9547682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_COL_NL));
9550Sstevel@tonic-gate 			else
9567682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_SEM_NL));
9570Sstevel@tonic-gate 		} else {
9587682SAli.Bahrami@Sun.COM 			if (vsdata && !oflag)
9597682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_TNCO),
9607682SAli.Bahrami@Sun.COM 				    vdp->vd_name);
9617682SAli.Bahrami@Sun.COM 			else if (!vsdata) {
9620Sstevel@tonic-gate 				if (oflag)
9637682SAli.Bahrami@Sun.COM 					(void) printf(MSG_ORIG(MSG_FMT_OFIL),
9647682SAli.Bahrami@Sun.COM 					    file);
9657682SAli.Bahrami@Sun.COM 				(void) printf(MSG_ORIG(MSG_FMT_TNSE),
9667682SAli.Bahrami@Sun.COM 				    vdp->vd_name);
9670Sstevel@tonic-gate 			}
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 
9707682SAli.Bahrami@Sun.COM 		/* If we are not printing symbols, we're done */
9717682SAli.Bahrami@Sun.COM 		if (vsdata == NULL)
9720Sstevel@tonic-gate 			continue;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		/*
9757682SAli.Bahrami@Sun.COM 		 * If a specific version to match has been specified then
9767682SAli.Bahrami@Sun.COM 		 * display any of its own symbols plus any inherited from
9777682SAli.Bahrami@Sun.COM 		 * other versions. Otherwise simply print out the symbols
9787682SAli.Bahrami@Sun.COM 		 * for this version.
9790Sstevel@tonic-gate 		 */
9807682SAli.Bahrami@Sun.COM 		gvers_syms(vsdata, vdp->vd_ndx, vdp->vd_name, NULL, file);
9817682SAli.Bahrami@Sun.COM 		if (alist_nitems(match_list) != 0) {
9827682SAli.Bahrami@Sun.COM 			recurse_syms(vsdata, vdp, file);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 			/*
9857682SAli.Bahrami@Sun.COM 			 * If the verbose flag is set, and this is not
9867682SAli.Bahrami@Sun.COM 			 * the base version, then add the base version as a
9877682SAli.Bahrami@Sun.COM 			 * dependency.
9880Sstevel@tonic-gate 			 */
9897682SAli.Bahrami@Sun.COM 			if (vflag && bvdp &&
9907682SAli.Bahrami@Sun.COM 			    !match(NULL, bvdp->vd_name, bvdp->vd_ndx)) {
9910Sstevel@tonic-gate 				if (!oflag)
9927682SAli.Bahrami@Sun.COM 					(void) printf(MSG_ORIG(MSG_FMT_TNCO),
9937682SAli.Bahrami@Sun.COM 					    bvdp->vd_name);
9947682SAli.Bahrami@Sun.COM 				gvers_syms(vsdata, bvdp->vd_ndx,
9957682SAli.Bahrami@Sun.COM 				    bvdp->vd_name, NULL, file);
9960Sstevel@tonic-gate 			}
9970Sstevel@tonic-gate 		}
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	return (error);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate int
main(int argc,char ** argv,char ** envp)10030Sstevel@tonic-gate main(int argc, char **argv, char **envp)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	GElf_Shdr	shdr;
10060Sstevel@tonic-gate 	Elf		*elf;
10070Sstevel@tonic-gate 	Elf_Scn		*scn;
10080Sstevel@tonic-gate 	Elf_Data	*data;
10090Sstevel@tonic-gate 	GElf_Ehdr 	ehdr;
10100Sstevel@tonic-gate 	int		nfile, var;
10110Sstevel@tonic-gate 	char		*names;
10120Sstevel@tonic-gate 	Cache		*cache, *_cache;
10130Sstevel@tonic-gate 	Cache		*_cache_def, *_cache_need, *_cache_sym, *_cache_loc;
10140Sstevel@tonic-gate 	int		error = 0;
10157682SAli.Bahrami@Sun.COM 	Gver_sym_data 	vsdata_s;
10167682SAli.Bahrami@Sun.COM 	const Gver_sym_data	*vsdata = NULL;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
10200Sstevel@tonic-gate 	 */
10212647Srie 	(void) conv_check_native(argv, envp);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	/*
10240Sstevel@tonic-gate 	 * Establish locale.
10250Sstevel@tonic-gate 	 */
10260Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
10270Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	cname = argv[0];
10300Sstevel@tonic-gate 	Cflag = dflag = lflag = nflag = oflag = rflag = sflag = vflag = 0;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	opterr = 0;
10337682SAli.Bahrami@Sun.COM 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
10340Sstevel@tonic-gate 		switch (var) {
10350Sstevel@tonic-gate 		case 'C':
10360Sstevel@tonic-gate 			Cflag = USR_DEFINED;
10370Sstevel@tonic-gate 			break;
10380Sstevel@tonic-gate 		case 'd':
10390Sstevel@tonic-gate 			dflag = USR_DEFINED;
10400Sstevel@tonic-gate 			break;
10410Sstevel@tonic-gate 		case 'l':
10427682SAli.Bahrami@Sun.COM 			lflag = sflag = USR_DEFINED;
10430Sstevel@tonic-gate 			break;
10440Sstevel@tonic-gate 		case 'n':
10450Sstevel@tonic-gate 			nflag = USR_DEFINED;
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 		case 'o':
10480Sstevel@tonic-gate 			oflag = USR_DEFINED;
10490Sstevel@tonic-gate 			break;
10500Sstevel@tonic-gate 		case 'r':
10510Sstevel@tonic-gate 			rflag = USR_DEFINED;
10520Sstevel@tonic-gate 			break;
10530Sstevel@tonic-gate 		case 's':
10540Sstevel@tonic-gate 			sflag = USR_DEFINED;
10550Sstevel@tonic-gate 			break;
10560Sstevel@tonic-gate 		case 'v':
10570Sstevel@tonic-gate 			vflag = USR_DEFINED;
10580Sstevel@tonic-gate 			break;
10597682SAli.Bahrami@Sun.COM 		case 'I':
10600Sstevel@tonic-gate 		case 'N':
10617682SAli.Bahrami@Sun.COM 			add_match_record(var, optarg);
10620Sstevel@tonic-gate 			break;
10630Sstevel@tonic-gate 		case '?':
10640Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF),
10650Sstevel@tonic-gate 			    cname);
10660Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USAGE_DETAIL));
10670Sstevel@tonic-gate 			exit(1);
10680Sstevel@tonic-gate 		default:
10690Sstevel@tonic-gate 			break;
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	/*
10740Sstevel@tonic-gate 	 * No files specified on the command line?
10750Sstevel@tonic-gate 	 */
10760Sstevel@tonic-gate 	if ((nfile = argc - optind) == 0) {
10770Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF), cname);
10780Sstevel@tonic-gate 		exit(1);
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * By default print both version definitions and needed dependencies.
10830Sstevel@tonic-gate 	 */
10847682SAli.Bahrami@Sun.COM 	if ((dflag == 0) && (rflag == 0) && (lflag == 0))
10850Sstevel@tonic-gate 		dflag = rflag = DEF_DEFINED;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	/*
10880Sstevel@tonic-gate 	 * Open the input file and initialize the elf interface.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 	for (; optind < argc; optind++) {
10910Sstevel@tonic-gate 		int		derror = 0, nerror = 0,	err;
10920Sstevel@tonic-gate 		const char	*file = argv[optind];
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		if ((var = open(file, O_RDONLY)) == -1) {
10950Sstevel@tonic-gate 			err = errno;
10960Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN),
10970Sstevel@tonic-gate 			    cname, file, strerror(err));
10980Sstevel@tonic-gate 			error = 1;
10990Sstevel@tonic-gate 			continue;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 		(void) elf_version(EV_CURRENT);
11020Sstevel@tonic-gate 		if ((elf = elf_begin(var, ELF_C_READ, NULL)) == NULL) {
11030Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_ELF_BEGIN), cname,
11040Sstevel@tonic-gate 			    file, elf_errmsg(elf_errno()));
11050Sstevel@tonic-gate 			error = 1;
11060Sstevel@tonic-gate 			(void) close(var);
11070Sstevel@tonic-gate 			continue;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		if (elf_kind(elf) != ELF_K_ELF) {
11100Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_NOTELF), cname,
11110Sstevel@tonic-gate 			    file);
11120Sstevel@tonic-gate 			error = 1;
11130Sstevel@tonic-gate 			(void) close(var);
11140Sstevel@tonic-gate 			(void) elf_end(elf);
11150Sstevel@tonic-gate 			continue;
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 		if (gelf_getehdr(elf, &ehdr) == NULL) {
11180Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_ELF_GETEHDR), cname,
11190Sstevel@tonic-gate 			    file, elf_errmsg(elf_errno()));
11200Sstevel@tonic-gate 			error = 1;
11210Sstevel@tonic-gate 			(void) close(var);
11220Sstevel@tonic-gate 			(void) elf_end(elf);
11230Sstevel@tonic-gate 			continue;
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		/*
11270Sstevel@tonic-gate 		 *  Obtain the .shstrtab data buffer to provide the required
11280Sstevel@tonic-gate 		 * section name strings.
11290Sstevel@tonic-gate 		 */
11300Sstevel@tonic-gate 		if ((scn = elf_getscn(elf, ehdr.e_shstrndx)) == NULL) {
11310Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_ELF_GETSCN), cname,
11320Sstevel@tonic-gate 			    file, elf_errmsg(elf_errno()));
11330Sstevel@tonic-gate 			error = 1;
11340Sstevel@tonic-gate 			(void) close(var);
11350Sstevel@tonic-gate 			(void) elf_end(elf);
11360Sstevel@tonic-gate 			continue;
11370Sstevel@tonic-gate 		}
11380Sstevel@tonic-gate 		if ((data = elf_getdata(scn, NULL)) == NULL) {
11390Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_ELF_GETDATA), cname,
11400Sstevel@tonic-gate 			    file, elf_errmsg(elf_errno()));
11410Sstevel@tonic-gate 			error = 1;
11420Sstevel@tonic-gate 			(void) close(var);
11430Sstevel@tonic-gate 			(void) elf_end(elf);
11440Sstevel@tonic-gate 			continue;
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		names = data->d_buf;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		/*
11490Sstevel@tonic-gate 		 * Fill in the cache descriptor with information for each
11500Sstevel@tonic-gate 		 * section we might need.   We probably only need to save
11510Sstevel@tonic-gate 		 * read-only allocable sections as this is where the version
11520Sstevel@tonic-gate 		 * structures and their associated symbols and strings live.
11530Sstevel@tonic-gate 		 * However, God knows what someone can do with a mapfile, and
11540Sstevel@tonic-gate 		 * as elf_begin has already gone through all the overhead we
11550Sstevel@tonic-gate 		 * might as well set up the cache for every section.
11560Sstevel@tonic-gate 		 */
11577682SAli.Bahrami@Sun.COM 		if ((cache = calloc(ehdr.e_shnum, sizeof (Cache))) == NULL) {
11580Sstevel@tonic-gate 			int err = errno;
11590Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), cname,
11600Sstevel@tonic-gate 			    file, strerror(err));
11610Sstevel@tonic-gate 			exit(1);
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 
11647682SAli.Bahrami@Sun.COM 		_cache_def = _cache_need = _cache_sym = _cache_loc = NULL;
11650Sstevel@tonic-gate 		_cache = cache;
11660Sstevel@tonic-gate 		_cache++;
11670Sstevel@tonic-gate 		for (scn = NULL; scn = elf_nextscn(elf, scn); _cache++) {
11680Sstevel@tonic-gate 			if (gelf_getshdr(scn, &shdr) == NULL) {
11690Sstevel@tonic-gate 				(void) fprintf(stderr,
11700Sstevel@tonic-gate 				    MSG_ORIG(MSG_ELF_GETSHDR), cname, file,
11710Sstevel@tonic-gate 				    elf_errmsg(elf_errno()));
11720Sstevel@tonic-gate 				error = 1;
11730Sstevel@tonic-gate 				continue;
11740Sstevel@tonic-gate 			}
11750Sstevel@tonic-gate 			if ((_cache->c_data = elf_getdata(scn, NULL)) ==
11760Sstevel@tonic-gate 			    NULL) {
11770Sstevel@tonic-gate 				(void) fprintf(stderr,
11780Sstevel@tonic-gate 				    MSG_ORIG(MSG_ELF_GETDATA), cname, file,
11790Sstevel@tonic-gate 				    elf_errmsg(elf_errno()));
11800Sstevel@tonic-gate 				error = 1;
11810Sstevel@tonic-gate 				continue;
11820Sstevel@tonic-gate 			}
11830Sstevel@tonic-gate 			_cache->c_scn = scn;
11840Sstevel@tonic-gate 			_cache->c_name = names + shdr.sh_name;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 			/*
11870Sstevel@tonic-gate 			 * Remember the version sections and symbol table.
11880Sstevel@tonic-gate 			 */
11892766Sab196087 			switch (shdr.sh_type) {
11902766Sab196087 			case SHT_SUNW_verdef:
11912766Sab196087 				if (dflag)
11922766Sab196087 					_cache_def = _cache;
11932766Sab196087 				break;
11942766Sab196087 			case SHT_SUNW_verneed:
11952766Sab196087 				if (rflag)
11962766Sab196087 					_cache_need = _cache;
11972766Sab196087 				break;
11982766Sab196087 			case SHT_SUNW_versym:
11992766Sab196087 				if (sflag)
12002766Sab196087 					_cache_sym = _cache;
12012766Sab196087 				break;
12022766Sab196087 			case SHT_SYMTAB:
12032766Sab196087 				if (lflag)
12042766Sab196087 					_cache_loc = _cache;
12052766Sab196087 				break;
12062766Sab196087 			}
12070Sstevel@tonic-gate 		}
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		/*
12100Sstevel@tonic-gate 		 * Before printing anything out determine if any warnings are
12110Sstevel@tonic-gate 		 * necessary.
12120Sstevel@tonic-gate 		 */
12137682SAli.Bahrami@Sun.COM 		if (lflag && (_cache_loc == NULL)) {
12140Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_VER_UNREDSYMS),
12150Sstevel@tonic-gate 			    cname, file);
12160Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_VER_NOSYMTAB));
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		/*
12200Sstevel@tonic-gate 		 * If there is more than one input file, and we're not printing
12210Sstevel@tonic-gate 		 * one-line output, display the filename being processed.
12220Sstevel@tonic-gate 		 */
12230Sstevel@tonic-gate 		if ((nfile > 1) && !oflag)
12247682SAli.Bahrami@Sun.COM 			(void) printf(MSG_ORIG(MSG_FMT_FILE), file);
12257682SAli.Bahrami@Sun.COM 
12267682SAli.Bahrami@Sun.COM 		/*
12277682SAli.Bahrami@Sun.COM 		 * If we're printing symbols, then collect the data
12287682SAli.Bahrami@Sun.COM 		 * necessary to do that.
12297682SAli.Bahrami@Sun.COM 		 */
12307682SAli.Bahrami@Sun.COM 		if (_cache_sym != NULL) {
12317682SAli.Bahrami@Sun.COM 			vsdata = &vsdata_s;
12327682SAli.Bahrami@Sun.COM 			(void) gelf_getshdr(_cache_sym->c_scn, &shdr);
12337682SAli.Bahrami@Sun.COM 			vsdata_s.vsd_vsp =
12347682SAli.Bahrami@Sun.COM 			    (GElf_Versym *)_cache_sym->c_data->d_buf;
12357682SAli.Bahrami@Sun.COM 			vsdata_s.vsd_sym_data = cache[shdr.sh_link].c_data;
12367682SAli.Bahrami@Sun.COM 			(void) gelf_getshdr(cache[shdr.sh_link].c_scn, &shdr);
12377682SAli.Bahrami@Sun.COM 			vsdata_s.vsd_symn = shdr.sh_size / shdr.sh_entsize;
12387682SAli.Bahrami@Sun.COM 			vsdata_s.vsd_strs =
12397682SAli.Bahrami@Sun.COM 			    (const char *)cache[shdr.sh_link].c_data->d_buf;
12407682SAli.Bahrami@Sun.COM 		}
12417682SAli.Bahrami@Sun.COM 
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 		/*
12440Sstevel@tonic-gate 		 * Print the files version needed sections.
12450Sstevel@tonic-gate 		 */
12460Sstevel@tonic-gate 		if (_cache_need)
12477682SAli.Bahrami@Sun.COM 			nerror = gvers_need(cache, _cache_need, vsdata, file);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		/*
12500Sstevel@tonic-gate 		 * Print the files version definition sections.
12510Sstevel@tonic-gate 		 */
12520Sstevel@tonic-gate 		if (_cache_def)
12537682SAli.Bahrami@Sun.COM 			derror = gvers_def(cache, _cache_def, vsdata, file);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 		/*
12560Sstevel@tonic-gate 		 * Print any local symbol reductions.
12570Sstevel@tonic-gate 		 */
12580Sstevel@tonic-gate 		if (_cache_loc)
12590Sstevel@tonic-gate 			sym_local(cache, _cache_loc, file);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 		/*
12620Sstevel@tonic-gate 		 * Determine the error return.  There are three conditions that
12630Sstevel@tonic-gate 		 * may produce an error (a non-zero return):
12640Sstevel@tonic-gate 		 *
12650Sstevel@tonic-gate 		 *  o	if the user specified -d and no version definitions
12660Sstevel@tonic-gate 		 *	were found.
12670Sstevel@tonic-gate 		 *
12680Sstevel@tonic-gate 		 *  o	if the user specified -r and no version requirements
12690Sstevel@tonic-gate 		 *	were found.
12700Sstevel@tonic-gate 		 *
12710Sstevel@tonic-gate 		 *  o	if the user specified neither -d or -r, (thus both are
12720Sstevel@tonic-gate 		 *	enabled by default), and no version definitions or
12730Sstevel@tonic-gate 		 *	version dependencies were found.
12740Sstevel@tonic-gate 		 */
12750Sstevel@tonic-gate 		if (((dflag == USR_DEFINED) && (derror == 0)) ||
12760Sstevel@tonic-gate 		    ((rflag == USR_DEFINED) && (nerror == 0)) ||
12770Sstevel@tonic-gate 		    (rflag && dflag && (derror == 0) && (nerror == 0)))
12780Sstevel@tonic-gate 			error = 1;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		(void) close(var);
12810Sstevel@tonic-gate 		(void) elf_end(elf);
12820Sstevel@tonic-gate 		free(cache);
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 	return (error);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate const char *
_pvs_msg(Msg mid)12880Sstevel@tonic-gate _pvs_msg(Msg mid)
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
12910Sstevel@tonic-gate }
1292