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