xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/dl.c (revision d8e10381a0083d7717710b0db7e64707bc0f3ff8)
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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include	<string.h>
27 #include	"_conv.h"
28 #include	"dl_msg.h"
29 
30 #define	MODESZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
31 		MSG_RTLD_LAZY_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
32 		MSG_RTLD_GLOBAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
33 		MSG_RTLD_NOLOAD_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
34 		MSG_RTLD_PARENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
35 		MSG_RTLD_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
36 		MSG_RTLD_WORLD_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37 		MSG_RTLD_NODELETE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38 		MSG_RTLD_FIRST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39 		MSG_RTLD_CONFGEN_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
41 
42 
43 /*
44  * Ensure that Conv_dl_mode_buf_t is large enough:
45  *
46  * MODESZ is the real minimum size of the buffer required by conv_dl_mode().
47  * However, Conv_dl_mode_buf_t uses CONV_DL_MODE_BUFSIZE to set the
48  * buffer size. We do things this way because the definition of MODESZ uses
49  * information that is not available in the environment of other programs
50  * that include the conv.h header file.
51  */
52 #if (CONV_DL_MODE_BUFSIZE != MODESZ) && !defined(__lint)
53 #define	REPORT_BUFSIZE MODESZ
54 #include "report_bufsize.h"
55 #error "CONV_DL_MODE_BUFSIZE does not match MODESZ"
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 const Val_desc vda[] = {
65 		{ RTLD_NOLOAD,		MSG_RTLD_NOLOAD },
66 		{ RTLD_PARENT,		MSG_RTLD_PARENT },
67 		{ RTLD_GROUP,		MSG_RTLD_GROUP },
68 		{ RTLD_WORLD,		MSG_RTLD_WORLD },
69 		{ RTLD_NODELETE,	MSG_RTLD_NODELETE },
70 		{ RTLD_FIRST,		MSG_RTLD_FIRST },
71 		{ RTLD_CONFGEN,		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), 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 ((mode & RTLD_LAZY) || 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, vda, 0);
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 #define	REPORT_BUFSIZE FLAGSZ
134 #include "report_bufsize.h"
135 #error "CONV_DL_FLAG_BUFSIZE does not match FLAGSZ"
136 #endif
137 
138 /*
139  * String conversion routine for dldump() flags.
140  * crle(1) uses this routine to generate update information, and in this case
141  * we build a "|" separated string.
142  */
143 const char *
144 conv_dl_flag(int flags, Conv_fmt_flags_t fmt_flags,
145     Conv_dl_flag_buf_t *dl_flag_buf)
146 {
147 	static const Val_desc vda[] = {
148 		{ RTLD_REL_RELATIVE,	MSG_RTLD_REL_RELATIVE },
149 		{ RTLD_REL_EXEC,	MSG_RTLD_REL_EXEC },
150 		{ RTLD_REL_DEPENDS,	MSG_RTLD_REL_DEPENDS },
151 		{ RTLD_REL_PRELOAD,	MSG_RTLD_REL_PRELOAD },
152 		{ RTLD_REL_SELF,	MSG_RTLD_REL_SELF },
153 		{ RTLD_REL_WEAK,	MSG_RTLD_REL_WEAK },
154 		{ RTLD_MEMORY,		MSG_RTLD_MEMORY },
155 		{ RTLD_STRIP,		MSG_RTLD_STRIP },
156 		{ RTLD_NOHEAP,		MSG_RTLD_NOHEAP },
157 		{ RTLD_CONFSET,		MSG_RTLD_CONFSET },
158 		{ 0,			0 }
159 	};
160 	static const char *leading_str_arr[2];
161 	static CONV_EXPN_FIELD_ARG conv_arg = {
162 	    NULL, sizeof (dl_flag_buf->buf), leading_str_arr };
163 
164 	const char **lstr = leading_str_arr;
165 
166 	if (flags == 0)
167 		return (MSG_ORIG(MSG_GBL_ZERO));
168 
169 	conv_arg.buf = dl_flag_buf->buf;
170 	if (CONV_TYPE_FMT_ALT(fmt_flags) == CONV_FMT_ALT_CRLE) {
171 		conv_arg.prefix = conv_arg.suffix = MSG_ORIG(MSG_GBL_QUOTE);
172 		conv_arg.sep = MSG_ORIG(MSG_GBL_SEP);
173 	} else {		/* Use default delimiters */
174 		conv_arg.prefix = conv_arg.suffix = conv_arg.sep = NULL;
175 	}
176 
177 	if ((flags & RTLD_REL_ALL) == RTLD_REL_ALL) {
178 		*lstr++ = MSG_ORIG(MSG_RTLD_REL_ALL);
179 		flags &= ~RTLD_REL_ALL;
180 	}
181 	*lstr = NULL;
182 	conv_arg.oflags = conv_arg.rflags = flags;
183 
184 	(void) conv_expn_field(&conv_arg, vda, fmt_flags);
185 
186 	return ((const char *)dl_flag_buf->buf);
187 }
188 
189 const char *
190 conv_dl_info(int request)
191 {
192 	static const Msg	requests[RTLD_DI_MAX] = {
193 		MSG_RTLD_DI_LMID,	/* MSG_ORIG(MSG_RTLD_DI_LMID) */
194 		MSG_RTLD_DI_LINKMAP,	/* MSG_ORIG(MSG_RTLD_DI_LINKMAP) */
195 		MSG_RTLD_DI_CONFIGADDR,	/* MSG_ORIG(MSG_RTLD_DI_CONFIGADDR) */
196 		MSG_RTLD_DI_SERINFO,	/* MSG_ORIG(MSG_RTLD_DI_SERINFO) */
197 		MSG_RTLD_DI_SERINFOSIZE, /* MSG_ORIG(MSG_RTLD_DI_SERINFOSIZE) */
198 		MSG_RTLD_DI_ORIGIN,	/* MSG_ORIG(MSG_RTLD_DI_ORIGIN) */
199 		MSG_RTLD_DI_PROFILENAME, /* MSG_ORIG(MSG_RTLD_DI_PROFILENAME) */
200 		MSG_RTLD_DI_PROFILEOUT,	/* MSG_ORIG(MSG_RTLD_DI_PROFILEOUT) */
201 		MSG_RTLD_DI_GETSIGNAL,	/* MSG_ORIG(MSG_RTLD_DI_GETSIGNAL) */
202 		MSG_RTLD_DI_SETSIGNAL,	/* MSG_ORIG(MSG_RTLD_DI_SETSIGNAL) */
203 		MSG_RTLD_DI_ARGSINFO,	/* MSG_ORIG(MSG_RTLD_DI_ARGSINFO) */
204 		MSG_RTLD_DI_MMAPS,	/* MSG_ORIG(MSG_RTLD_DI_MMAPS) */
205 		MSG_RTLD_DI_MMAPCNT,	/* MSG_ORIG(MSG_RTLD_DI_MMAPCNT) */
206 		MSG_RTLD_DI_DEFERRED,	/* MSG_ORIG(MSG_RTLD_DI_DEFERRED) */
207 		MSG_RTLD_DI_DEFERRED_SYM
208 					/* MSG_ORIG(MSG_RTLD_DI_DEFERRED_SYM) */
209 	};
210 	static Conv_inv_buf_t	inv_buf;
211 
212 #if	(RTLD_DI_MAX != RTLD_DI_DEFERRED_SYM)
213 #error	"RTLD_DI_MAX has grown"
214 #endif
215 	if (request && (request <= RTLD_DI_MAX))
216 		return (MSG_ORIG(requests[request - 1]));
217 
218 	(void) conv_invalid_val(&inv_buf, EC_NATPTR(request), 0);
219 	return (inv_buf.buf);
220 }
221