xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 11a2bb386c90df26ed2d2d6086a56cb503465e33)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * String conversion routines for section attributes.
31  */
32 #include	<string.h>
33 #include	<sys/param.h>
34 #include	<sys/elf_SPARC.h>
35 #include	<sys/elf_amd64.h>
36 #include	<_conv.h>
37 #include	<sections_msg.h>
38 
39 static const Msg secs[SHT_NUM] = {
40 	MSG_SHT_NULL,		MSG_SHT_PROGBITS,	MSG_SHT_SYMTAB,
41 	MSG_SHT_STRTAB,		MSG_SHT_RELA,		MSG_SHT_HASH,
42 	MSG_SHT_DYNAMIC,	MSG_SHT_NOTE,		MSG_SHT_NOBITS,
43 	MSG_SHT_REL,		MSG_SHT_SHLIB,		MSG_SHT_DYNSYM,
44 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY,
45 	MSG_SHT_FINI_ARRAY,	MSG_SHT_PREINIT_ARRAY,	MSG_SHT_GROUP,
46 	MSG_SHT_SYMTAB_SHNDX
47 };
48 #if	(SHT_NUM != (SHT_SYMTAB_SHNDX + 1))
49 #error	"SHT_NUM has grown"
50 #endif
51 
52 static const Msg usecs[SHT_HISUNW - SHT_LOSUNW + 1] = {
53 	MSG_SHT_SUNW_dof,	MSG_SHT_SUNW_cap,	MSG_SHT_SUNW_SIGNATURE,
54 	MSG_SHT_SUNW_ANNOTATE,	MSG_SHT_SUNW_DEBUGSTR,	MSG_SHT_SUNW_DEBUG,
55 	MSG_SHT_SUNW_move,	MSG_SHT_SUNW_COMDAT,	MSG_SHT_SUNW_syminfo,
56 	MSG_SHT_SUNW_verdef,	MSG_SHT_SUNW_verneed,	MSG_SHT_SUNW_versym
57 };
58 #if	(SHT_LOSUNW != SHT_SUNW_dof)
59 #error	"SHT_LOSUNW has moved"
60 #endif
61 
62 const char *
63 /* ARGSUSED 1 */
64 conv_sectyp_str(ushort_t mach, uint_t sec)
65 {
66 	static char	string[STRSIZE] = { '\0' };
67 
68 	if (sec < SHT_NUM)
69 		return (MSG_ORIG(secs[sec]));
70 	else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW))
71 		return (MSG_ORIG(usecs[sec - SHT_LOSUNW]));
72 	else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
73 		if ((sec == (uint_t)SHT_SPARC_GOTDATA) &&
74 		    ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
75 		    (mach == EM_SPARCV9)))
76 			return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA));
77 		else if ((sec == (uint_t)SHT_AMD64_UNWIND) &&
78 		    (mach == EM_AMD64))
79 			return (MSG_ORIG(MSG_SHT_AMD64_UNWIND));
80 		else
81 			return (conv_invalid_str(string, STRSIZE, sec, 0));
82 	} else
83 		return (conv_invalid_str(string, STRSIZE, sec, 0));
84 }
85 
86 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
87 		MSG_SHF_WRITE_SIZE + \
88 		MSG_SHF_ALLOC_SIZE + \
89 		MSG_SHF_EXECINSTR_SIZE + \
90 		MSG_SHF_MERGE_SIZE + \
91 		MSG_SHF_STRINGS_SIZE + \
92 		MSG_SHF_INFO_LINK_SIZE + \
93 		MSG_SHF_LINK_ORDER_SIZE + \
94 		MSG_SHF_OS_NONCONFORMING_SIZE + \
95 		MSG_SHF_GROUP_SIZE + \
96 		MSG_SHF_TLS_SIZE + \
97 		MSG_SHF_EXCLUDE_SIZE + \
98 		MSG_SHF_ORDERED_SIZE + \
99 		MSG_SHF_AMD64_LARGE_SIZE + \
100 		MSG_GBL_CSQBRKT_SIZE
101 
102 const char *
103 /* ARGSUSED 1 */
104 conv_secflg_str(ushort_t mach, uint_t flags)
105 {
106 	static	char	string[FLAGSZ] = { '\0' };
107 
108 	if (flags == 0)
109 		return (MSG_ORIG(MSG_GBL_ZERO));
110 	else {
111 		uint_t	flags_handled = 0;
112 
113 		(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
114 		if (flags & SHF_WRITE) {
115 			(void) strcat(string, MSG_ORIG(MSG_SHF_WRITE));
116 			flags_handled |= SHF_WRITE;
117 		}
118 		if (flags & SHF_ALLOC) {
119 			(void) strcat(string, MSG_ORIG(MSG_SHF_ALLOC));
120 			flags_handled |= SHF_ALLOC;
121 		}
122 		if (flags & SHF_EXECINSTR) {
123 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXECINSTR));
124 			flags_handled |= SHF_EXECINSTR;
125 		}
126 		if (flags & SHF_MERGE) {
127 			(void) strcat(string, MSG_ORIG(MSG_SHF_MERGE));
128 			flags_handled |= SHF_MERGE;
129 		}
130 		if (flags & SHF_STRINGS) {
131 			(void) strcat(string, MSG_ORIG(MSG_SHF_STRINGS));
132 			flags_handled |= SHF_STRINGS;
133 		}
134 		if (flags & SHF_INFO_LINK) {
135 			(void) strcat(string, MSG_ORIG(MSG_SHF_INFO_LINK));
136 			flags_handled |= SHF_INFO_LINK;
137 		}
138 		if (flags & SHF_LINK_ORDER) {
139 			(void) strcat(string, MSG_ORIG(MSG_SHF_LINK_ORDER));
140 			flags_handled |= SHF_LINK_ORDER;
141 		}
142 		if (flags & SHF_OS_NONCONFORMING) {
143 			(void) strcat(string,
144 				MSG_ORIG(MSG_SHF_OS_NONCONFORMING));
145 			flags_handled |= SHF_OS_NONCONFORMING;
146 		}
147 		if (flags & SHF_GROUP) {
148 			(void) strcat(string, MSG_ORIG(MSG_SHF_GROUP));
149 			flags_handled |= SHF_GROUP;
150 		}
151 		if (flags & SHF_TLS) {
152 			(void) strcat(string, MSG_ORIG(MSG_SHF_TLS));
153 			flags_handled |= SHF_TLS;
154 		}
155 		if (flags & SHF_EXCLUDE) {
156 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXCLUDE));
157 			flags_handled |= SHF_EXCLUDE;
158 		}
159 		if (flags & SHF_ORDERED) {
160 			(void) strcat(string, MSG_ORIG(MSG_SHF_ORDERED));
161 			flags_handled |= SHF_ORDERED;
162 		}
163 		if (flags & SHF_AMD64_LARGE) {
164 			(void) strcat(string, MSG_ORIG(MSG_SHF_AMD64_LARGE));
165 			flags_handled |= SHF_AMD64_LARGE;
166 		}
167 
168 		/*
169 		 * Are there any flags that haven't been handled.
170 		 */
171 		if ((flags & flags_handled) != flags) {
172 			char	*str;
173 			size_t	len;
174 
175 			len = strlen(string);
176 			str = string + len;
177 			(void) conv_invalid_str(str, FLAGSZ - len,
178 			    (flags & (~flags_handled)), 0);
179 		}
180 		(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
181 
182 		return ((const char *)string);
183 	}
184 }
185 
186 /*
187  * Need to be able to hold a 32bit signed integer.
188  */
189 #define	INFOSZ	128
190 
191 const char *
192 conv_secinfo_str(uint_t info, uint_t flags)
193 {
194 	static	char	string[INFOSZ] = { '\0' };
195 
196 	if (flags & SHF_ORDERED) {
197 		if (info == SHN_BEFORE)
198 			return (MSG_ORIG(MSG_SHN_BEFORE));
199 		else if (info == SHN_AFTER)
200 			return (MSG_ORIG(MSG_SHN_AFTER));
201 	}
202 	(void) conv_invalid_str(string, INFOSZ, info, 1);
203 	return ((const char *)string);
204 }
205