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