xref: /titanic_50/usr/src/cmd/sgs/libconv/common/phdr.c (revision 3eae19d9cf3390cf5b75e10c9c1945fd36ad856a)
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 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * String conversion routines for program header attributes.
31  */
32 #include	<string.h>
33 #include	<_conv.h>
34 #include	<phdr_msg.h>
35 
36 /* Instantiate a local copy of conv_map2str() from _conv.h */
37 DEFINE_conv_map2str
38 
39 const char *
40 conv_phdr_type(Half mach, Word type, int fmt_flags, Conv_inv_buf_t *inv_buf)
41 {
42 	static const Msg	phdrs[] = {
43 		MSG_PT_NULL,		MSG_PT_LOAD,
44 		MSG_PT_DYNAMIC,		MSG_PT_INTERP,
45 		MSG_PT_NOTE,		MSG_PT_SHLIB,
46 		MSG_PT_PHDR,		MSG_PT_TLS
47 	};
48 	static const Msg	phdrs_alt[] = {
49 		MSG_PT_NULL_ALT,	MSG_PT_LOAD_ALT,
50 		MSG_PT_DYNAMIC_ALT,	MSG_PT_INTERP_ALT,
51 		MSG_PT_NOTE_ALT,	MSG_PT_SHLIB_ALT,
52 		MSG_PT_PHDR_ALT,	MSG_PT_TLS_ALT
53 	};
54 #if PT_NUM != (PT_TLS + 1)
55 error "PT_NUM has grown. Update phdrs[]"
56 #endif
57 	static const Msg uphdrs[] = {
58 		MSG_PT_SUNWBSS,		MSG_PT_SUNWSTACK,
59 		MSG_PT_SUNWDTRACE,	MSG_PT_SUNWCAP
60 	};
61 	static const Msg uphdrs_alt[] = {
62 		MSG_PT_SUNWBSS_ALT,	MSG_PT_SUNWSTACK_ALT,
63 		MSG_PT_SUNWDTRACE_ALT,	MSG_PT_SUNWCAP_ALT
64 	};
65 #if PT_LOSUNW != PT_SUNWBSS
66 #error "PT_LOSUNW has grown. Update uphdrs[]"
67 #endif
68 
69 	if (type < PT_NUM) {
70 		return (conv_map2str(inv_buf, type, fmt_flags,
71 		    ARRAY_NELTS(phdrs), phdrs, phdrs_alt, phdrs_alt));
72 	} else if ((type >= PT_SUNWBSS) && (type <= PT_HISUNW)) {
73 		return (conv_map2str(inv_buf, (type - PT_SUNWBSS), fmt_flags,
74 		    ARRAY_NELTS(uphdrs), uphdrs, uphdrs_alt, uphdrs_alt));
75 	} else if ((type == PT_SUNW_UNWIND) && (mach == EM_AMD64)) {
76 		return ((fmt_flags & CONV_FMTALTMASK) ?
77 		    MSG_ORIG(MSG_PT_SUNW_UNWIND_ALT) :
78 		    MSG_ORIG(MSG_PT_SUNW_UNWIND));
79 	} else
80 		return (conv_invalid_val(inv_buf, type, 0));
81 }
82 
83 #define	PHDRSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
84 		MSG_PF_X_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
85 		MSG_PF_W_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
86 		MSG_PF_R_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
87 		MSG_PF_SUNW_FAILURE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
88 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
89 
90 /*
91  * Ensure that Conv_phdr_flags_buf_t is large enough:
92  *
93  * PHDRSZ is the real minimum size of the buffer required by conv_phdr_flags().
94  * However, Conv_phdr_flags_buf_t uses CONV_PHDR_FLAGS_BUFSIZE to set the
95  * buffer size. We do things this way because the definition of PHDRSZ uses
96  * information that is not available in the environment of other programs
97  * that include the conv.h header file.
98  */
99 #if CONV_PHDR_FLAGS_BUFSIZE < PHDRSZ
100 #error "CONV_PHDR_FLAGS_BUFSIZE is not large enough"
101 #endif
102 
103 const char *
104 conv_phdr_flags(Word flags, Conv_phdr_flags_buf_t *phdr_flags_buf)
105 {
106 	static Val_desc vda[] = {
107 		{ PF_X,			MSG_ORIG(MSG_PF_X) },
108 		{ PF_W,			MSG_ORIG(MSG_PF_W) },
109 		{ PF_R,			MSG_ORIG(MSG_PF_R) },
110 		{ PF_SUNW_FAILURE,	MSG_ORIG(MSG_PF_SUNW_FAILURE) },
111 		{ 0,			0 }
112 	};
113 	static CONV_EXPN_FIELD_ARG conv_arg = {
114 	    NULL, sizeof (phdr_flags_buf->buf), vda };
115 
116 	if (flags == 0)
117 		return (MSG_ORIG(MSG_GBL_ZERO));
118 
119 	conv_arg.buf = phdr_flags_buf->buf;
120 	conv_arg.oflags = conv_arg.rflags = flags;
121 	(void) conv_expn_field(&conv_arg);
122 
123 	return ((const char *)phdr_flags_buf->buf);
124 }
125