xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 54d82594cac34899a52710db0b8235a171e83e31)
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 2005 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_SHF_AMD64_LARGE + \
99 		MSG_GBL_CSQBRKT_SIZE
100 
101 const char *
102 /* ARGSUSED 1 */
103 conv_secflg_str(ushort_t mach, uint_t flags)
104 {
105 	static	char	string[FLAGSZ] = { '\0' };
106 
107 	if (flags == 0)
108 		return (MSG_ORIG(MSG_GBL_ZERO));
109 	else {
110 		uint_t	flags_handled = 0;
111 
112 		(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
113 		if (flags & SHF_WRITE) {
114 			(void) strcat(string, MSG_ORIG(MSG_SHF_WRITE));
115 			flags_handled |= SHF_WRITE;
116 		}
117 		if (flags & SHF_ALLOC) {
118 			(void) strcat(string, MSG_ORIG(MSG_SHF_ALLOC));
119 			flags_handled |= SHF_ALLOC;
120 		}
121 		if (flags & SHF_EXECINSTR) {
122 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXECINSTR));
123 			flags_handled |= SHF_EXECINSTR;
124 		}
125 		if (flags & SHF_MERGE) {
126 			(void) strcat(string, MSG_ORIG(MSG_SHF_MERGE));
127 			flags_handled |= SHF_MERGE;
128 		}
129 		if (flags & SHF_STRINGS) {
130 			(void) strcat(string, MSG_ORIG(MSG_SHF_STRINGS));
131 			flags_handled |= SHF_STRINGS;
132 		}
133 		if (flags & SHF_INFO_LINK) {
134 			(void) strcat(string, MSG_ORIG(MSG_SHF_INFO_LINK));
135 			flags_handled |= SHF_INFO_LINK;
136 		}
137 		if (flags & SHF_LINK_ORDER) {
138 			(void) strcat(string, MSG_ORIG(MSG_SHF_LINK_ORDER));
139 			flags_handled |= SHF_LINK_ORDER;
140 		}
141 		if (flags & SHF_OS_NONCONFORMING) {
142 			(void) strcat(string,
143 				MSG_ORIG(MSG_SHF_OS_NONCONFORMING));
144 			flags_handled |= SHF_OS_NONCONFORMING;
145 		}
146 		if (flags & SHF_GROUP) {
147 			(void) strcat(string, MSG_ORIG(MSG_SHF_GROUP));
148 			flags_handled |= SHF_GROUP;
149 		}
150 		if (flags & SHF_TLS) {
151 			(void) strcat(string, MSG_ORIG(MSG_SHF_TLS));
152 			flags_handled |= SHF_TLS;
153 		}
154 		if (flags & SHF_EXCLUDE) {
155 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXCLUDE));
156 			flags_handled |= SHF_EXCLUDE;
157 		}
158 		if (flags & SHF_ORDERED) {
159 			(void) strcat(string, MSG_ORIG(MSG_SHF_ORDERED));
160 			flags_handled |= SHF_ORDERED;
161 		}
162 		if (flags & SHF_AMD64_LARGE) {
163 			(void) strcat(string, MSG_ORIG(MSG_SHF_AMD64_LARGE));
164 			flags_handled |= SHF_AMD64_LARGE;
165 		}
166 
167 		/*
168 		 * Are there any flags that haven't been handled.
169 		 */
170 		if ((flags & flags_handled) != flags) {
171 			char	*str;
172 			size_t	len;
173 
174 			len = strlen(string);
175 			str = string + len;
176 			(void) conv_invalid_str(str, FLAGSZ - len,
177 			    (flags & (~flags_handled)), 0);
178 		}
179 		(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
180 
181 		return ((const char *)string);
182 	}
183 }
184 
185 /*
186  * Need to be able to hold a 32bit signed integer.
187  */
188 #define	INFOSZ	128
189 
190 const char *
191 conv_secinfo_str(uint_t info, uint_t flags)
192 {
193 	static	char	string[INFOSZ] = { '\0' };
194 
195 	if (flags & SHF_ORDERED) {
196 		if (info == SHN_BEFORE)
197 			return (MSG_ORIG(MSG_SHN_BEFORE));
198 		else if (info == SHN_AFTER)
199 			return (MSG_ORIG(MSG_SHN_AFTER));
200 	}
201 	(void) conv_invalid_str(string, INFOSZ, info, 1);
202 	return ((const char *)string);
203 }
204