xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/phdr.c (revision 7a088f03b431bdffa96c3b2175964d4d38420caa)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * String conversion routines for program header attributes.
29  */
30 #include	<string.h>
31 #include	<_conv.h>
32 #include	<phdr_msg.h>
33 
34 /* Instantiate a local copy of conv_map2str() from _conv.h */
35 DEFINE_conv_map2str
36 
37 /*ARGSUSED*/
38 const char *
39 conv_phdr_type(Half mach, Word type, Conv_fmt_flags_t fmt_flags,
40     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 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
71 		case CONV_FMT_ALT_DUMP:
72 		case CONV_FMT_ALT_FILE:
73 			return (conv_map2str(inv_buf, type, fmt_flags,
74 			    ARRAY_NELTS(phdrs_alt), phdrs_alt));
75 		default:
76 			return (conv_map2str(inv_buf, type, fmt_flags,
77 			    ARRAY_NELTS(phdrs), phdrs));
78 		}
79 	} else if ((type >= PT_SUNWBSS) && (type <= PT_HISUNW)) {
80 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
81 		case CONV_FMT_ALT_DUMP:
82 		case CONV_FMT_ALT_FILE:
83 			return (conv_map2str(inv_buf, (type - PT_SUNWBSS),
84 			    fmt_flags, ARRAY_NELTS(uphdrs_alt), uphdrs_alt));
85 		default:
86 			return (conv_map2str(inv_buf, (type - PT_SUNWBSS),
87 			    fmt_flags, ARRAY_NELTS(uphdrs), uphdrs));
88 		}
89 	} else if (type == PT_SUNW_UNWIND) {
90 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
91 		case CONV_FMT_ALT_DUMP:
92 		case CONV_FMT_ALT_FILE:
93 			return (MSG_ORIG(MSG_PT_SUNW_UNWIND_ALT));
94 		default:
95 			return (MSG_ORIG(MSG_PT_SUNW_UNWIND));
96 		}
97 	} else if (type == PT_SUNW_EH_FRAME) {
98 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
99 		case CONV_FMT_ALT_DUMP:
100 		case CONV_FMT_ALT_FILE:
101 			return (MSG_ORIG(MSG_PT_SUNW_EH_FRAME_ALT));
102 		default:
103 			return (MSG_ORIG(MSG_PT_SUNW_EH_FRAME));
104 		}
105 	} else
106 		return (conv_invalid_val(inv_buf, type, 0));
107 }
108 
109 #define	PHDRSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
110 		MSG_PF_X_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
111 		MSG_PF_W_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
112 		MSG_PF_R_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
113 		MSG_PF_SUNW_FAILURE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
114 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
115 
116 /*
117  * Ensure that Conv_phdr_flags_buf_t is large enough:
118  *
119  * PHDRSZ is the real minimum size of the buffer required by conv_phdr_flags().
120  * However, Conv_phdr_flags_buf_t uses CONV_PHDR_FLAGS_BUFSIZE to set the
121  * buffer size. We do things this way because the definition of PHDRSZ uses
122  * information that is not available in the environment of other programs
123  * that include the conv.h header file.
124  */
125 #if (CONV_PHDR_FLAGS_BUFSIZE != PHDRSZ) && !defined(__lint)
126 #define	REPORT_BUFSIZE PHDRSZ
127 #include "report_bufsize.h"
128 #error "CONV_PHDR_FLAGS_BUFSIZE does not match PHDRSZ"
129 #endif
130 
131 const char *
132 conv_phdr_flags(Word flags, Conv_fmt_flags_t fmt_flags,
133     Conv_phdr_flags_buf_t *phdr_flags_buf)
134 {
135 	static Val_desc vda[] = {
136 		{ PF_X,			MSG_ORIG(MSG_PF_X) },
137 		{ PF_W,			MSG_ORIG(MSG_PF_W) },
138 		{ PF_R,			MSG_ORIG(MSG_PF_R) },
139 		{ PF_SUNW_FAILURE,	MSG_ORIG(MSG_PF_SUNW_FAILURE) },
140 		{ 0,			0 }
141 	};
142 	static CONV_EXPN_FIELD_ARG conv_arg = {
143 	    NULL, sizeof (phdr_flags_buf->buf), vda };
144 
145 	if (flags == 0)
146 		return (MSG_ORIG(MSG_GBL_ZERO));
147 
148 	conv_arg.buf = phdr_flags_buf->buf;
149 	conv_arg.oflags = conv_arg.rflags = flags;
150 	(void) conv_expn_field(&conv_arg, fmt_flags);
151 
152 	return ((const char *)phdr_flags_buf->buf);
153 }
154