dl.c revision 5088:26c540f30cd3
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 */
61const char *
62conv_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, 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#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 */
141const char *
142conv_dl_flag(int flags, Conv_fmt_flags_t fmt_flags,
143    Conv_dl_flag_buf_t *dl_flag_buf)
144{
145	static Val_desc vda[] = {
146		{ RTLD_REL_RELATIVE,	MSG_ORIG(MSG_RTLD_REL_RELATIVE) },
147		{ RTLD_REL_EXEC,	MSG_ORIG(MSG_RTLD_REL_EXEC) },
148		{ RTLD_REL_DEPENDS,	MSG_ORIG(MSG_RTLD_REL_DEPENDS) },
149		{ RTLD_REL_PRELOAD,	MSG_ORIG(MSG_RTLD_REL_PRELOAD) },
150		{ RTLD_REL_SELF,	MSG_ORIG(MSG_RTLD_REL_SELF) },
151		{ RTLD_REL_WEAK,	MSG_ORIG(MSG_RTLD_REL_WEAK) },
152		{ RTLD_MEMORY,		MSG_ORIG(MSG_RTLD_MEMORY) },
153		{ RTLD_STRIP,		MSG_ORIG(MSG_RTLD_STRIP) },
154		{ RTLD_NOHEAP,		MSG_ORIG(MSG_RTLD_NOHEAP) },
155		{ RTLD_CONFSET,		MSG_ORIG(MSG_RTLD_CONFSET) },
156		{ 0,			0 }
157	};
158	static const char *leading_str_arr[2];
159	static CONV_EXPN_FIELD_ARG conv_arg = {
160	    NULL, sizeof (dl_flag_buf->buf), vda, leading_str_arr };
161
162	const char **lstr = leading_str_arr;
163
164	if (flags == 0)
165		return (MSG_ORIG(MSG_GBL_ZERO));
166
167	conv_arg.buf = dl_flag_buf->buf;
168	if (CONV_TYPE_FMT_ALT(fmt_flags) == CONV_FMT_ALT_CRLE) {
169		conv_arg.prefix = conv_arg.suffix = MSG_ORIG(MSG_GBL_QUOTE);
170		conv_arg.sep = MSG_ORIG(MSG_GBL_SEP);
171	} else {		/* Use default delimiters */
172		conv_arg.prefix = conv_arg.suffix = conv_arg.sep = NULL;
173	}
174
175	if ((flags & RTLD_REL_ALL) == RTLD_REL_ALL) {
176		*lstr++ = MSG_ORIG(MSG_RTLD_REL_ALL);
177		flags &= ~RTLD_REL_ALL;
178	}
179	*lstr = NULL;
180	conv_arg.oflags = conv_arg.rflags = flags;
181
182	(void) conv_expn_field(&conv_arg, fmt_flags);
183
184	return ((const char *)dl_flag_buf->buf);
185}
186