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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * String conversion routines for section attributes.
30  */
31 #include	<string.h>
32 #include	<sys/param.h>
33 #include	<sys/elf_SPARC.h>
34 #include	<sys/elf_amd64.h>
35 #include	<_conv.h>
36 #include	<sections_msg.h>
37 
38 static const Msg secs[SHT_NUM] = {
39 	MSG_SHT_NULL,		MSG_SHT_PROGBITS,	MSG_SHT_SYMTAB,
40 	MSG_SHT_STRTAB,		MSG_SHT_RELA,		MSG_SHT_HASH,
41 	MSG_SHT_DYNAMIC,	MSG_SHT_NOTE,		MSG_SHT_NOBITS,
42 	MSG_SHT_REL,		MSG_SHT_SHLIB,		MSG_SHT_DYNSYM,
43 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY,
44 	MSG_SHT_FINI_ARRAY,	MSG_SHT_PREINIT_ARRAY,	MSG_SHT_GROUP,
45 	MSG_SHT_SYMTAB_SHNDX
46 };
47 #if	(SHT_NUM != (SHT_SYMTAB_SHNDX + 1))
48 #error	"SHT_NUM has grown"
49 #endif
50 
51 static const Msg usecs[SHT_HISUNW - SHT_LOSUNW + 1] = {
52 	MSG_SHT_SUNW_dof,	MSG_SHT_SUNW_cap,	MSG_SHT_SUNW_SIGNATURE,
53 	MSG_SHT_SUNW_ANNOTATE,	MSG_SHT_SUNW_DEBUGSTR,	MSG_SHT_SUNW_DEBUG,
54 	MSG_SHT_SUNW_move,	MSG_SHT_SUNW_COMDAT,	MSG_SHT_SUNW_syminfo,
55 	MSG_SHT_SUNW_verdef,	MSG_SHT_SUNW_verneed,	MSG_SHT_SUNW_versym
56 };
57 #if	(SHT_LOSUNW != SHT_SUNW_dof)
58 #error	"SHT_LOSUNW has moved"
59 #endif
60 
61 const char *
62 /* ARGSUSED 1 */
63 conv_sectyp_str(ushort_t mach, uint_t sec)
64 {
65 	static char	string[STRSIZE] = { '\0' };
66 
67 	if (sec < SHT_NUM)
68 		return (MSG_ORIG(secs[sec]));
69 	else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW))
70 		return (MSG_ORIG(usecs[sec - SHT_LOSUNW]));
71 	else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
72 		if ((sec == (uint_t)SHT_SPARC_GOTDATA) &&
73 		    ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
74 		    (mach == EM_SPARCV9)))
75 			return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA));
76 		else if ((sec == (uint_t)SHT_AMD64_UNWIND) &&
77 		    (mach == EM_AMD64))
78 			return (MSG_ORIG(MSG_SHT_AMD64_UNWIND));
79 		else
80 			return (conv_invalid_str(string, STRSIZE, sec, 0));
81 	} else
82 		return (conv_invalid_str(string, STRSIZE, sec, 0));
83 }
84 
85 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
86 		MSG_SHF_WRITE_SIZE + \
87 		MSG_SHF_ALLOC_SIZE + \
88 		MSG_SHF_EXECINSTR_SIZE + \
89 		MSG_SHF_MERGE_SIZE + \
90 		MSG_SHF_STRINGS_SIZE + \
91 		MSG_SHF_INFO_LINK_SIZE + \
92 		MSG_SHF_LINK_ORDER_SIZE + \
93 		MSG_SHF_OS_NONCONFORMING_SIZE + \
94 		MSG_SHF_GROUP_SIZE + \
95 		MSG_SHF_TLS_SIZE + \
96 		MSG_SHF_EXCLUDE_SIZE + \
97 		MSG_SHF_ORDERED_SIZE + \
98 		MSG_GBL_CSQBRKT_SIZE
99 
100 const char *
101 /* ARGSUSED 1 */
102 conv_secflg_str(ushort_t mach, uint_t flags)
103 {
104 	static	char	string[FLAGSZ] = { '\0' };
105 
106 	if (flags == 0)
107 		return (MSG_ORIG(MSG_GBL_ZERO));
108 	else {
109 		uint_t	flags_handled = 0;
110 
111 		(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
112 		if (flags & SHF_WRITE) {
113 			(void) strcat(string, MSG_ORIG(MSG_SHF_WRITE));
114 			flags_handled |= SHF_WRITE;
115 		}
116 		if (flags & SHF_ALLOC) {
117 			(void) strcat(string, MSG_ORIG(MSG_SHF_ALLOC));
118 			flags_handled |= SHF_ALLOC;
119 		}
120 		if (flags & SHF_EXECINSTR) {
121 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXECINSTR));
122 			flags_handled |= SHF_EXECINSTR;
123 		}
124 		if (flags & SHF_MERGE) {
125 			(void) strcat(string, MSG_ORIG(MSG_SHF_MERGE));
126 			flags_handled |= SHF_MERGE;
127 		}
128 		if (flags & SHF_STRINGS) {
129 			(void) strcat(string, MSG_ORIG(MSG_SHF_STRINGS));
130 			flags_handled |= SHF_STRINGS;
131 		}
132 		if (flags & SHF_INFO_LINK) {
133 			(void) strcat(string, MSG_ORIG(MSG_SHF_INFO_LINK));
134 			flags_handled |= SHF_INFO_LINK;
135 		}
136 		if (flags & SHF_LINK_ORDER) {
137 			(void) strcat(string, MSG_ORIG(MSG_SHF_LINK_ORDER));
138 			flags_handled |= SHF_LINK_ORDER;
139 		}
140 		if (flags & SHF_OS_NONCONFORMING) {
141 			(void) strcat(string,
142 				MSG_ORIG(MSG_SHF_OS_NONCONFORMING));
143 			flags_handled |= SHF_OS_NONCONFORMING;
144 		}
145 		if (flags & SHF_GROUP) {
146 			(void) strcat(string, MSG_ORIG(MSG_SHF_GROUP));
147 			flags_handled |= SHF_GROUP;
148 		}
149 		if (flags & SHF_TLS) {
150 			(void) strcat(string, MSG_ORIG(MSG_SHF_TLS));
151 			flags_handled |= SHF_TLS;
152 		}
153 		if (flags & SHF_EXCLUDE) {
154 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXCLUDE));
155 			flags_handled |= SHF_EXCLUDE;
156 		}
157 		if (flags & SHF_ORDERED) {
158 			(void) strcat(string, MSG_ORIG(MSG_SHF_ORDERED));
159 			flags_handled |= SHF_ORDERED;
160 		}
161 
162 		/*
163 		 * Are there any flags that haven't been handled.
164 		 */
165 		if ((flags & flags_handled) != flags) {
166 			char	*str;
167 			size_t	len;
168 
169 			len = strlen(string);
170 			str = string + len;
171 			(void) conv_invalid_str(str, FLAGSZ - len,
172 			    (flags & (~flags_handled)), 0);
173 		}
174 		(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
175 
176 		return ((const char *)string);
177 	}
178 }
179 
180 /*
181  * Need to be able to hold a 32bit signed integer.
182  */
183 #define	INFOSZ	128
184 
185 const char *
186 conv_secinfo_str(uint_t info, uint_t flags)
187 {
188 	static	char	string[INFOSZ] = { '\0' };
189 
190 	if (flags & SHF_ORDERED) {
191 		if (info == SHN_BEFORE)
192 			return (MSG_ORIG(MSG_SHN_BEFORE));
193 		else if (info == SHN_AFTER)
194 			return (MSG_ORIG(MSG_SHN_AFTER));
195 	}
196 	(void) conv_invalid_str(string, INFOSZ, info, 1);
197 	return ((const char *)string);
198 }
199