xref: /titanic_50/usr/src/cmd/sgs/libconv/common/version.c (revision 385cc6b4ad1792caef3f84eb61eed3f27085801f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * String conversion routine for version flag entries.
29  */
30 #include	<stdio.h>
31 #include	"_conv.h"
32 #include	"version_msg.h"
33 
34 #define	VERFLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
35 	MSG_VER_FLG_WEAK_SIZE		+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
36 	MSG_VER_FLG_BASE_SIZE		+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37 	MSG_VER_FLG_INFO_SIZE		+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38 	CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
39 
40 /*
41  * Ensure that Conv_ver_flags_buf_t is large enough:
42  *
43  * VERFLAGSZ is the real minimum size of the buffer required by
44  * conv_ver_flags(). However, Conv_ver_flags_buf_t uses CONV_VER_FLAGS_BUFSIZE
45  * to set the buffer size. We do things this way because the definition of
46  * VERFLAGSZ uses information that is not available in the environment of
47  * other programs that include the conv.h header file.
48  */
49 #if (CONV_VER_FLAGS_BUFSIZE != VERFLAGSZ) && !defined(__lint)
50 #define	REPORT_BUFSIZE VERFLAGSZ
51 #include "report_bufsize.h"
52 #error "CONV_VER_FLAGS_BUFSIZE does not match VERFLAGSZ"
53 #endif
54 
55 const char *
56 conv_ver_flags(Half flags, Conv_fmt_flags_t fmt_flags,
57     Conv_ver_flags_buf_t *ver_flags_buf)
58 {
59 	static const Val_desc vda[] = {
60 		{ VER_FLG_WEAK,		MSG_VER_FLG_WEAK },
61 		{ VER_FLG_BASE,		MSG_VER_FLG_BASE },
62 		{ VER_FLG_INFO,		MSG_VER_FLG_INFO },
63 		{ 0,			0 }
64 	};
65 	static CONV_EXPN_FIELD_ARG conv_arg = {
66 	    NULL, sizeof (ver_flags_buf->buf) };
67 
68 	if (flags == 0)
69 		return (MSG_ORIG(MSG_GBL_NULL));
70 
71 	conv_arg.buf = ver_flags_buf->buf;
72 	conv_arg.oflags = conv_arg.rflags = flags;
73 	(void) conv_expn_field(&conv_arg, vda, fmt_flags);
74 
75 	return ((const char *)ver_flags_buf->buf);
76 }
77 
78 
79 /*
80  * Format a version index as contained in a VERSYM section
81  *
82  * entry:
83  *	verndx - Version index to format
84  *	gnuver - If True (non-zero), the version rules used by the
85  *		GNU ld are assumed. If False (0), Solaris ld rules apply.
86  */
87 const char *
88 conv_ver_index(Versym verndx, int gnuver, Conv_inv_buf_t *inv_buf)
89 {
90 	const char		*fmt;
91 
92 	/* Special case versions starting at VER_NDX_LORESERVE */
93 	if (verndx == VER_NDX_ELIMINATE)
94 		return (MSG_ORIG(MSG_VERSYM_ELIMINATE));
95 
96 	/*
97 	 * The GNU style of versioning uses the top bit of the
98 	 * 16-bit version index (0x8000) as a "hidden bit". A
99 	 * hidden symbol is supposed to be ignored by the linker.
100 	 */
101 	if (gnuver && (verndx & 0x8000)) {
102 		verndx &= ~0x8000;
103 		fmt = MSG_ORIG(MSG_VERSYM_GNUH_FMT);
104 	} else {
105 		fmt = MSG_ORIG(MSG_VERSYM_FMT);
106 	}
107 
108 	/* format as numeric */
109 	(void) snprintf(inv_buf->buf, sizeof (inv_buf->buf),
110 	    fmt, EC_HALF(verndx));
111 	return (inv_buf->buf);
112 }
113