xref: /titanic_44/usr/src/cmd/sgs/libconv/common/dl.c (revision 28cdc3d776761766afeb198769d1b70ed7e0f2e1)
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 2006 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	MSG_GBL_OSQBRKT_SIZE + \
33 		MSG_RTLD_LAZY_SIZE + \
34 		MSG_RTLD_GLOBAL_SIZE + \
35 		MSG_RTLD_NOLOAD_SIZE + \
36 		MSG_RTLD_PARENT_SIZE + \
37 		MSG_RTLD_GROUP_SIZE + \
38 		MSG_RTLD_WORLD_SIZE + \
39 		MSG_RTLD_NODELETE_SIZE + \
40 		MSG_RTLD_FIRST_SIZE + \
41 		MSG_RTLD_CONFGEN_SIZE + \
42 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
43 
44 
45 /*
46  * String conversion routine for dlopen() attributes.
47  */
48 const char *
49 conv_dl_mode(int mode, int fabricate)
50 {
51 	static	char	string[MODESZ];
52 	static Val_desc vda[] = {
53 		{ RTLD_NOLOAD,		MSG_ORIG(MSG_RTLD_NOLOAD) },
54 		{ RTLD_PARENT,		MSG_ORIG(MSG_RTLD_PARENT) },
55 		{ RTLD_GROUP,		MSG_ORIG(MSG_RTLD_GROUP) },
56 		{ RTLD_WORLD,		MSG_ORIG(MSG_RTLD_WORLD) },
57 		{ RTLD_NODELETE,	MSG_ORIG(MSG_RTLD_NODELETE) },
58 		{ RTLD_FIRST,		MSG_ORIG(MSG_RTLD_FIRST) },
59 		{ RTLD_CONFGEN,		MSG_ORIG(MSG_RTLD_CONFGEN) },
60 		{ 0,			0 }
61 	};
62 	int		_mode = mode;
63 
64 	(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
65 
66 	if (mode & RTLD_NOW) {
67 	    if (strlcat(string, MSG_ORIG(MSG_RTLD_NOW), MODESZ) >= MODESZ)
68 		return (conv_invalid_val(string, MODESZ, mode, 0));
69 	} else if (fabricate) {
70 	    if (strlcat(string, MSG_ORIG(MSG_RTLD_LAZY), MODESZ) >= MODESZ)
71 		return (conv_invalid_val(string, MODESZ, mode, 0));
72 	}
73 	if (mode & RTLD_GLOBAL) {
74 	    if (strlcat(string, MSG_ORIG(MSG_RTLD_GLOBAL), MODESZ) >= MODESZ)
75 		return (conv_invalid_val(string, MODESZ, mode, 0));
76 	} else if (fabricate) {
77 	    if (strlcat(string, MSG_ORIG(MSG_RTLD_LOCAL), MODESZ) >= MODESZ)
78 		return (conv_invalid_val(string, MODESZ, mode, 0));
79 	}
80 	_mode &= ~(RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL);
81 
82 	if (conv_expn_field(string, MODESZ, vda, mode, _mode, 0, 0))
83 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), MODESZ);
84 
85 	return ((const char *)string);
86 }
87 
88 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
89 		MSG_RTLD_REL_RELATIVE_SIZE +	MSG_GBL_SEP_SIZE + \
90 		MSG_RTLD_REL_EXEC_SIZE +	MSG_GBL_SEP_SIZE + \
91 		MSG_RTLD_REL_DEPENDS_SIZE +	MSG_GBL_SEP_SIZE + \
92 		MSG_RTLD_REL_PRELOAD_SIZE +	MSG_GBL_SEP_SIZE + \
93 		MSG_RTLD_REL_SELF_SIZE +	MSG_GBL_SEP_SIZE + \
94 		MSG_RTLD_REL_WEAK_SIZE +	MSG_GBL_SEP_SIZE + \
95 		MSG_RTLD_MEMORY_SIZE +		MSG_GBL_SEP_SIZE + \
96 		MSG_RTLD_STRIP_SIZE +		MSG_GBL_SEP_SIZE + \
97 		MSG_RTLD_NOHEAP_SIZE +		MSG_GBL_SEP_SIZE + \
98 		MSG_RTLD_CONFSET_SIZE + \
99 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
100 
101 /*
102  * String conversion routine for dldump() flags.
103  * crle(1) uses this routine to generate update information, and in this case
104  * we build a "|" separated string.
105  */
106 const char *
107 conv_dl_flag(int flags, int separator)
108 {
109 	static	char	string[FLAGSZ];
110 	static Val_desc vda[] = {
111 		{ RTLD_REL_RELATIVE,	MSG_ORIG(MSG_RTLD_REL_RELATIVE) },
112 		{ RTLD_REL_EXEC,	MSG_ORIG(MSG_RTLD_REL_EXEC) },
113 		{ RTLD_REL_DEPENDS,	MSG_ORIG(MSG_RTLD_REL_DEPENDS) },
114 		{ RTLD_REL_PRELOAD,	MSG_ORIG(MSG_RTLD_REL_PRELOAD) },
115 		{ RTLD_REL_SELF,	MSG_ORIG(MSG_RTLD_REL_SELF) },
116 		{ RTLD_REL_WEAK,	MSG_ORIG(MSG_RTLD_REL_WEAK) },
117 		{ RTLD_MEMORY,		MSG_ORIG(MSG_RTLD_MEMORY) },
118 		{ RTLD_STRIP,		MSG_ORIG(MSG_RTLD_STRIP) },
119 		{ RTLD_NOHEAP,		MSG_ORIG(MSG_RTLD_NOHEAP) },
120 		{ RTLD_CONFSET,		MSG_ORIG(MSG_RTLD_CONFSET) },
121 		{ 0,			0 }
122 	};
123 	int		_flags = flags, element = 0;
124 
125 	if (flags == 0)
126 		return (MSG_ORIG(MSG_GBL_ZERO));
127 
128 	if (separator)
129 		(void) strlcpy(string, MSG_ORIG(MSG_GBL_QUOTE), FLAGSZ);
130 	else
131 		(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
132 
133 	if ((flags & RTLD_REL_ALL) == RTLD_REL_ALL) {
134 	    if (strlcat(string, MSG_ORIG(MSG_RTLD_REL_ALL), FLAGSZ) >= FLAGSZ)
135 		return (conv_invalid_val(string, FLAGSZ, flags, 0));
136 	    element++;
137 	    flags = _flags &= ~RTLD_REL_ALL;
138 	}
139 
140 	if (conv_expn_field(string, FLAGSZ, vda, flags, _flags,
141 	    (separator ? MSG_ORIG(MSG_GBL_SEP) : 0), element)) {
142 		if (separator)
143 		    (void) strlcat(string, MSG_ORIG(MSG_GBL_QUOTE), FLAGSZ);
144 		else
145 		    (void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
146 	}
147 
148 	return ((const char *)string);
149 }
150