xref: /titanic_41/usr/src/cmd/sgs/libconv/common/sections.c (revision 5aefb6555731130ca4fd295960123d71f2d21fe8)
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 2006 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 conv_sec_type(Half mach, Word sec)
63 {
64 	static char	string[CONV_INV_STRSIZE];
65 
66 	if (sec < SHT_NUM)
67 		return (MSG_ORIG(secs[sec]));
68 	else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW))
69 		return (MSG_ORIG(usecs[sec - SHT_LOSUNW]));
70 	else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
71 		if ((sec == SHT_SPARC_GOTDATA) && ((mach == EM_SPARC) ||
72 		    (mach == EM_SPARC32PLUS) || (mach == EM_SPARCV9)))
73 			return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA));
74 		else if ((sec == SHT_AMD64_UNWIND) && (mach == EM_AMD64))
75 			return (MSG_ORIG(MSG_SHT_AMD64_UNWIND));
76 		else
77 			return (conv_invalid_val(string, CONV_INV_STRSIZE,
78 			    sec, 0));
79 	} else
80 		return (conv_invalid_val(string, CONV_INV_STRSIZE, sec, 0));
81 }
82 
83 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
84 		MSG_SHF_WRITE_SIZE + \
85 		MSG_SHF_ALLOC_SIZE + \
86 		MSG_SHF_EXECINSTR_SIZE + \
87 		MSG_SHF_MERGE_SIZE + \
88 		MSG_SHF_STRINGS_SIZE + \
89 		MSG_SHF_INFO_LINK_SIZE + \
90 		MSG_SHF_LINK_ORDER_SIZE + \
91 		MSG_SHF_OS_NONCONFORMING_SIZE + \
92 		MSG_SHF_GROUP_SIZE + \
93 		MSG_SHF_TLS_SIZE + \
94 		MSG_SHF_EXCLUDE_SIZE + \
95 		MSG_SHF_ORDERED_SIZE + \
96 		MSG_SHF_AMD64_LARGE_SIZE + \
97 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
98 
99 const char *
100 conv_sec_flags(Xword flags)
101 {
102 	static	char	string[FLAGSZ];
103 	static Val_desc vda[] = {
104 		{ SHF_WRITE,		MSG_ORIG(MSG_SHF_WRITE) },
105 		{ SHF_ALLOC,		MSG_ORIG(MSG_SHF_ALLOC) },
106 		{ SHF_EXECINSTR,	MSG_ORIG(MSG_SHF_EXECINSTR) },
107 		{ SHF_MERGE,		MSG_ORIG(MSG_SHF_MERGE) },
108 		{ SHF_STRINGS,		MSG_ORIG(MSG_SHF_STRINGS) },
109 		{ SHF_INFO_LINK,	MSG_ORIG(MSG_SHF_INFO_LINK) },
110 		{ SHF_LINK_ORDER,	MSG_ORIG(MSG_SHF_LINK_ORDER) },
111 		{ SHF_OS_NONCONFORMING,	MSG_ORIG(MSG_SHF_OS_NONCONFORMING) },
112 		{ SHF_GROUP,		MSG_ORIG(MSG_SHF_GROUP) },
113 		{ SHF_TLS,		MSG_ORIG(MSG_SHF_TLS) },
114 		{ SHF_EXCLUDE,		MSG_ORIG(MSG_SHF_EXCLUDE) },
115 		{ SHF_ORDERED,		MSG_ORIG(MSG_SHF_ORDERED) },
116 		{ SHF_AMD64_LARGE,	MSG_ORIG(MSG_SHF_AMD64_LARGE) },
117 		{ 0,			0 }
118 	};
119 
120 	if (flags == 0)
121 		return (MSG_ORIG(MSG_GBL_ZERO));
122 
123 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
124 	if (conv_expn_field(string, FLAGSZ, vda, flags, flags, 0, 0))
125 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
126 
127 	return ((const char *)string);
128 }
129 
130 const char *
131 conv_sec_info(Word info, Xword flags)
132 {
133 	static	char	string[CONV_INV_STRSIZE];
134 
135 	if (flags & SHF_ORDERED) {
136 		if (info == SHN_BEFORE)
137 			return (MSG_ORIG(MSG_SHN_BEFORE));
138 		else if (info == SHN_AFTER)
139 			return (MSG_ORIG(MSG_SHN_AFTER));
140 	}
141 	(void) conv_invalid_val(string, CONV_INV_STRSIZE, info, 1);
142 	return ((const char *)string);
143 }
144