xref: /titanic_50/usr/src/cmd/sgs/libconv/common/dl.c (revision cd997836b08639dc4d44a032cadd0c7d526f960c)
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 #include	<string.h>
28 #include	"_conv.h"
29 #include	"dl_msg.h"
30 
31 #define	MODESZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
32 		MSG_RTLD_LAZY_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
33 		MSG_RTLD_GLOBAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
34 		MSG_RTLD_NOLOAD_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
35 		MSG_RTLD_PARENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
36 		MSG_RTLD_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37 		MSG_RTLD_WORLD_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38 		MSG_RTLD_NODELETE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39 		MSG_RTLD_FIRST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40 		MSG_RTLD_CONFGEN_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
41 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
42 
43 
44 /*
45  * Ensure that Conv_dl_mode_buf_t is large enough:
46  *
47  * MODESZ is the real minimum size of the buffer required by conv_dl_mode().
48  * However, Conv_dl_mode_buf_t uses CONV_DL_MODE_BUFSIZE to set the
49  * buffer size. We do things this way because the definition of MODESZ uses
50  * information that is not available in the environment of other programs
51  * that include the conv.h header file.
52  */
53 #if (CONV_DL_MODE_BUFSIZE != MODESZ) && !defined(__lint)
54 #define	REPORT_BUFSIZE MODESZ
55 #include "report_bufsize.h"
56 #error "CONV_DL_MODE_BUFSIZE does not match MODESZ"
57 #endif
58 
59 /*
60  * String conversion routine for dlopen() attributes.
61  */
62 const char *
63 conv_dl_mode(int mode, int fabricate, Conv_dl_mode_buf_t *dl_mode_buf)
64 {
65 	static const Val_desc vda[] = {
66 		{ RTLD_NOLOAD,		MSG_RTLD_NOLOAD },
67 		{ RTLD_PARENT,		MSG_RTLD_PARENT },
68 		{ RTLD_GROUP,		MSG_RTLD_GROUP },
69 		{ RTLD_WORLD,		MSG_RTLD_WORLD },
70 		{ RTLD_NODELETE,	MSG_RTLD_NODELETE },
71 		{ RTLD_FIRST,		MSG_RTLD_FIRST },
72 		{ RTLD_CONFGEN,		MSG_RTLD_CONFGEN },
73 		{ 0,			0 }
74 	};
75 	static const char *leading_str_arr[3];
76 	static CONV_EXPN_FIELD_ARG conv_arg = {
77 	    NULL, sizeof (dl_mode_buf->buf), leading_str_arr };
78 
79 	const char **lstr = leading_str_arr;
80 
81 	conv_arg.buf = dl_mode_buf->buf;
82 	conv_arg.oflags = conv_arg.rflags = mode;
83 
84 
85 	if (mode & RTLD_NOW) {
86 		*lstr++ = MSG_ORIG(MSG_RTLD_NOW);
87 	} else if ((mode & RTLD_LAZY) || fabricate) {
88 		*lstr++ = MSG_ORIG(MSG_RTLD_LAZY);
89 	}
90 	if (mode & RTLD_GLOBAL) {
91 		*lstr++ = MSG_ORIG(MSG_RTLD_GLOBAL);
92 	} else if (fabricate) {
93 		*lstr++ = MSG_ORIG(MSG_RTLD_LOCAL);
94 	}
95 	*lstr = NULL;
96 	conv_arg.oflags = mode;
97 	conv_arg.rflags = mode & ~(RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL);
98 
99 	(void) conv_expn_field(&conv_arg, vda, 0);
100 
101 	return ((const char *)dl_mode_buf->buf);
102 }
103 
104 /*
105  * Note: We can use two different sets of prefix/separator/suffix
106  * strings in conv_dl_flag(), depending on the value of the separator
107  * argument. To size the buffer, I use the default prefix and suffix
108  * sizes, and the alternate separator size, because they are larger.
109  */
110 
111 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
112 		MSG_RTLD_REL_RELATIVE_SIZE +	MSG_GBL_SEP_SIZE + \
113 		MSG_RTLD_REL_EXEC_SIZE +	MSG_GBL_SEP_SIZE + \
114 		MSG_RTLD_REL_DEPENDS_SIZE +	MSG_GBL_SEP_SIZE + \
115 		MSG_RTLD_REL_PRELOAD_SIZE +	MSG_GBL_SEP_SIZE + \
116 		MSG_RTLD_REL_SELF_SIZE +	MSG_GBL_SEP_SIZE + \
117 		MSG_RTLD_REL_WEAK_SIZE +	MSG_GBL_SEP_SIZE + \
118 		MSG_RTLD_MEMORY_SIZE +		MSG_GBL_SEP_SIZE + \
119 		MSG_RTLD_STRIP_SIZE +		MSG_GBL_SEP_SIZE + \
120 		MSG_RTLD_NOHEAP_SIZE +		MSG_GBL_SEP_SIZE + \
121 		MSG_RTLD_CONFSET_SIZE + \
122 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
123 
124 /*
125  * Ensure that Conv_dl_flag_buf_t is large enough:
126  *
127  * FLAGSZ is the real minimum size of the buffer required by conv_dl_flag().
128  * However, Conv_dl_flag_buf_t uses CONV_DL_FLAG_BUFSIZE to set the
129  * buffer size. We do things this way because the definition of FLAGSZ uses
130  * information that is not available in the environment of other programs
131  * that include the conv.h header file.
132  */
133 #if (CONV_DL_FLAG_BUFSIZE != FLAGSZ) && !defined(__lint)
134 #define	REPORT_BUFSIZE FLAGSZ
135 #include "report_bufsize.h"
136 #error "CONV_DL_FLAG_BUFSIZE does not match FLAGSZ"
137 #endif
138 
139 /*
140  * String conversion routine for dldump() flags.
141  * crle(1) uses this routine to generate update information, and in this case
142  * we build a "|" separated string.
143  */
144 const char *
145 conv_dl_flag(int flags, Conv_fmt_flags_t fmt_flags,
146     Conv_dl_flag_buf_t *dl_flag_buf)
147 {
148 	static const Val_desc vda[] = {
149 		{ RTLD_REL_RELATIVE,	MSG_RTLD_REL_RELATIVE },
150 		{ RTLD_REL_EXEC,	MSG_RTLD_REL_EXEC },
151 		{ RTLD_REL_DEPENDS,	MSG_RTLD_REL_DEPENDS },
152 		{ RTLD_REL_PRELOAD,	MSG_RTLD_REL_PRELOAD },
153 		{ RTLD_REL_SELF,	MSG_RTLD_REL_SELF },
154 		{ RTLD_REL_WEAK,	MSG_RTLD_REL_WEAK },
155 		{ RTLD_MEMORY,		MSG_RTLD_MEMORY },
156 		{ RTLD_STRIP,		MSG_RTLD_STRIP },
157 		{ RTLD_NOHEAP,		MSG_RTLD_NOHEAP },
158 		{ RTLD_CONFSET,		MSG_RTLD_CONFSET },
159 		{ 0,			0 }
160 	};
161 	static const char *leading_str_arr[2];
162 	static CONV_EXPN_FIELD_ARG conv_arg = {
163 	    NULL, sizeof (dl_flag_buf->buf), leading_str_arr };
164 
165 	const char **lstr = leading_str_arr;
166 
167 	if (flags == 0)
168 		return (MSG_ORIG(MSG_GBL_ZERO));
169 
170 	conv_arg.buf = dl_flag_buf->buf;
171 	if (CONV_TYPE_FMT_ALT(fmt_flags) == CONV_FMT_ALT_CRLE) {
172 		conv_arg.prefix = conv_arg.suffix = MSG_ORIG(MSG_GBL_QUOTE);
173 		conv_arg.sep = MSG_ORIG(MSG_GBL_SEP);
174 	} else {		/* Use default delimiters */
175 		conv_arg.prefix = conv_arg.suffix = conv_arg.sep = NULL;
176 	}
177 
178 	if ((flags & RTLD_REL_ALL) == RTLD_REL_ALL) {
179 		*lstr++ = MSG_ORIG(MSG_RTLD_REL_ALL);
180 		flags &= ~RTLD_REL_ALL;
181 	}
182 	*lstr = NULL;
183 	conv_arg.oflags = conv_arg.rflags = flags;
184 
185 	(void) conv_expn_field(&conv_arg, vda, fmt_flags);
186 
187 	return ((const char *)dl_flag_buf->buf);
188 }
189