xref: /titanic_50/usr/src/cmd/sgs/libconv/common/phdr.c (revision 582271e8d649568c83e9a016cc0d54265389c5d9)
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, Conv_fmt_flags_t fmt_flags,
41     Conv_inv_buf_t *inv_buf)
42 {
43 	static const Msg	phdrs[] = {
44 		MSG_PT_NULL,		MSG_PT_LOAD,
45 		MSG_PT_DYNAMIC,		MSG_PT_INTERP,
46 		MSG_PT_NOTE,		MSG_PT_SHLIB,
47 		MSG_PT_PHDR,		MSG_PT_TLS
48 	};
49 	static const Msg	phdrs_alt[] = {
50 		MSG_PT_NULL_ALT,	MSG_PT_LOAD_ALT,
51 		MSG_PT_DYNAMIC_ALT,	MSG_PT_INTERP_ALT,
52 		MSG_PT_NOTE_ALT,	MSG_PT_SHLIB_ALT,
53 		MSG_PT_PHDR_ALT,	MSG_PT_TLS_ALT
54 	};
55 #if PT_NUM != (PT_TLS + 1)
56 error "PT_NUM has grown. Update phdrs[]"
57 #endif
58 	static const Msg uphdrs[] = {
59 		MSG_PT_SUNWBSS,		MSG_PT_SUNWSTACK,
60 		MSG_PT_SUNWDTRACE,	MSG_PT_SUNWCAP
61 	};
62 	static const Msg uphdrs_alt[] = {
63 		MSG_PT_SUNWBSS_ALT,	MSG_PT_SUNWSTACK_ALT,
64 		MSG_PT_SUNWDTRACE_ALT,	MSG_PT_SUNWCAP_ALT
65 	};
66 #if PT_LOSUNW != PT_SUNWBSS
67 #error "PT_LOSUNW has grown. Update uphdrs[]"
68 #endif
69 
70 	if (type < PT_NUM) {
71 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
72 		case CONV_FMT_ALT_DUMP:
73 		case CONV_FMT_ALT_FILE:
74 			return (conv_map2str(inv_buf, type, fmt_flags,
75 			    ARRAY_NELTS(phdrs_alt), phdrs_alt));
76 		default:
77 			return (conv_map2str(inv_buf, type, fmt_flags,
78 			    ARRAY_NELTS(phdrs), phdrs));
79 		}
80 	} else if ((type >= PT_SUNWBSS) && (type <= PT_HISUNW)) {
81 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
82 		case CONV_FMT_ALT_DUMP:
83 		case CONV_FMT_ALT_FILE:
84 			return (conv_map2str(inv_buf, (type - PT_SUNWBSS),
85 			    fmt_flags, ARRAY_NELTS(uphdrs_alt), uphdrs_alt));
86 		default:
87 			return (conv_map2str(inv_buf, (type - PT_SUNWBSS),
88 			    fmt_flags, ARRAY_NELTS(uphdrs), uphdrs));
89 		}
90 	} else if ((type == PT_SUNW_UNWIND) && (mach == EM_AMD64)) {
91 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
92 		case CONV_FMT_ALT_DUMP:
93 		case CONV_FMT_ALT_FILE:
94 			return (MSG_ORIG(MSG_PT_SUNW_UNWIND_ALT));
95 		default:
96 			return (MSG_ORIG(MSG_PT_SUNW_UNWIND));
97 		}
98 	} else
99 		return (conv_invalid_val(inv_buf, type, 0));
100 }
101 
102 #define	PHDRSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
103 		MSG_PF_X_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
104 		MSG_PF_W_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
105 		MSG_PF_R_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
106 		MSG_PF_SUNW_FAILURE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
107 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
108 
109 /*
110  * Ensure that Conv_phdr_flags_buf_t is large enough:
111  *
112  * PHDRSZ is the real minimum size of the buffer required by conv_phdr_flags().
113  * However, Conv_phdr_flags_buf_t uses CONV_PHDR_FLAGS_BUFSIZE to set the
114  * buffer size. We do things this way because the definition of PHDRSZ uses
115  * information that is not available in the environment of other programs
116  * that include the conv.h header file.
117  */
118 #if (CONV_PHDR_FLAGS_BUFSIZE != PHDRSZ) && !defined(__lint)
119 #define	REPORT_BUFSIZE PHDRSZ
120 #include "report_bufsize.h"
121 #error "CONV_PHDR_FLAGS_BUFSIZE does not match PHDRSZ"
122 #endif
123 
124 const char *
125 conv_phdr_flags(Word flags, Conv_fmt_flags_t fmt_flags,
126     Conv_phdr_flags_buf_t *phdr_flags_buf)
127 {
128 	static Val_desc vda[] = {
129 		{ PF_X,			MSG_ORIG(MSG_PF_X) },
130 		{ PF_W,			MSG_ORIG(MSG_PF_W) },
131 		{ PF_R,			MSG_ORIG(MSG_PF_R) },
132 		{ PF_SUNW_FAILURE,	MSG_ORIG(MSG_PF_SUNW_FAILURE) },
133 		{ 0,			0 }
134 	};
135 	static CONV_EXPN_FIELD_ARG conv_arg = {
136 	    NULL, sizeof (phdr_flags_buf->buf), vda };
137 
138 	if (flags == 0)
139 		return (MSG_ORIG(MSG_GBL_ZERO));
140 
141 	conv_arg.buf = phdr_flags_buf->buf;
142 	conv_arg.oflags = conv_arg.rflags = flags;
143 	(void) conv_expn_field(&conv_arg, fmt_flags);
144 
145 	return ((const char *)phdr_flags_buf->buf);
146 }
147