xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 1976:f0691a145b7e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21685Srie 
220Sstevel@tonic-gate /*
231618Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * String conversion routines for section attributes.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include	<string.h>
320Sstevel@tonic-gate #include	<sys/param.h>
330Sstevel@tonic-gate #include	<sys/elf_SPARC.h>
340Sstevel@tonic-gate #include	<sys/elf_amd64.h>
350Sstevel@tonic-gate #include	<_conv.h>
360Sstevel@tonic-gate #include	<sections_msg.h>
370Sstevel@tonic-gate 
38*1976Sab196087 
39*1976Sab196087 
40*1976Sab196087 /* Instantiate a local copy of conv_map2str() from _conv.h */
41*1976Sab196087 DEFINE_conv_map2str
42*1976Sab196087 
43*1976Sab196087 
44*1976Sab196087 
450Sstevel@tonic-gate static const Msg secs[SHT_NUM] = {
460Sstevel@tonic-gate 	MSG_SHT_NULL,		MSG_SHT_PROGBITS,	MSG_SHT_SYMTAB,
470Sstevel@tonic-gate 	MSG_SHT_STRTAB,		MSG_SHT_RELA,		MSG_SHT_HASH,
480Sstevel@tonic-gate 	MSG_SHT_DYNAMIC,	MSG_SHT_NOTE,		MSG_SHT_NOBITS,
490Sstevel@tonic-gate 	MSG_SHT_REL,		MSG_SHT_SHLIB,		MSG_SHT_DYNSYM,
500Sstevel@tonic-gate 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY,
510Sstevel@tonic-gate 	MSG_SHT_FINI_ARRAY,	MSG_SHT_PREINIT_ARRAY,	MSG_SHT_GROUP,
520Sstevel@tonic-gate 	MSG_SHT_SYMTAB_SHNDX
530Sstevel@tonic-gate };
54*1976Sab196087 static const Msg secs_alt[SHT_NUM] = {
55*1976Sab196087 	MSG_SHT_NULL_ALT,	MSG_SHT_PROGBITS_ALT,	MSG_SHT_SYMTAB_ALT,
56*1976Sab196087 	MSG_SHT_STRTAB_ALT,	MSG_SHT_RELA_ALT,	MSG_SHT_HASH_ALT,
57*1976Sab196087 	MSG_SHT_DYNAMIC_ALT,	MSG_SHT_NOTE_ALT,	MSG_SHT_NOBITS_ALT,
58*1976Sab196087 	MSG_SHT_REL_ALT,	MSG_SHT_SHLIB_ALT,	MSG_SHT_DYNSYM_ALT,
59*1976Sab196087 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY_ALT,
60*1976Sab196087 	MSG_SHT_FINI_ARRAY_ALT,	MSG_SHT_PREINIT_ARRAY_ALT, MSG_SHT_GROUP_ALT,
61*1976Sab196087 	MSG_SHT_SYMTAB_SHNDX_ALT
62*1976Sab196087 };
630Sstevel@tonic-gate #if	(SHT_NUM != (SHT_SYMTAB_SHNDX + 1))
640Sstevel@tonic-gate #error	"SHT_NUM has grown"
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static const Msg usecs[SHT_HISUNW - SHT_LOSUNW + 1] = {
68*1976Sab196087 	MSG_SHT_SUNW_dof,		MSG_SHT_SUNW_cap,
69*1976Sab196087 	MSG_SHT_SUNW_SIGNATURE,		MSG_SHT_SUNW_ANNOTATE,
70*1976Sab196087 	MSG_SHT_SUNW_DEBUGSTR,		MSG_SHT_SUNW_DEBUG,
71*1976Sab196087 	MSG_SHT_SUNW_move,		MSG_SHT_SUNW_COMDAT,
72*1976Sab196087 	MSG_SHT_SUNW_syminfo,		MSG_SHT_SUNW_verdef,
73*1976Sab196087 	MSG_SHT_SUNW_verneed,		MSG_SHT_SUNW_versym
74*1976Sab196087 };
75*1976Sab196087 static const Msg usecs_alt[SHT_HISUNW - SHT_LOSUNW + 1] = {
76*1976Sab196087 	MSG_SHT_SUNW_dof_ALT,		MSG_SHT_SUNW_cap_ALT,
77*1976Sab196087 	MSG_SHT_SUNW_SIGNATURE_ALT,	MSG_SHT_SUNW_ANNOTATE_ALT,
78*1976Sab196087 	MSG_SHT_SUNW_DEBUGSTR_ALT,	MSG_SHT_SUNW_DEBUG_ALT,
79*1976Sab196087 	MSG_SHT_SUNW_move_ALT,		MSG_SHT_SUNW_COMDAT_ALT,
80*1976Sab196087 	MSG_SHT_SUNW_syminfo_ALT,	MSG_SHT_SUNW_verdef_ALT,
81*1976Sab196087 	MSG_SHT_SUNW_verneed_ALT,	MSG_SHT_SUNW_versym_ALT
820Sstevel@tonic-gate };
830Sstevel@tonic-gate #if	(SHT_LOSUNW != SHT_SUNW_dof)
840Sstevel@tonic-gate #error	"SHT_LOSUNW has moved"
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 
87*1976Sab196087 
880Sstevel@tonic-gate const char *
89*1976Sab196087 conv_sec_type(Half mach, Word sec, int fmt_flags)
900Sstevel@tonic-gate {
911618Srie 	static char	string[CONV_INV_STRSIZE];
920Sstevel@tonic-gate 
93*1976Sab196087 	if (sec < SHT_NUM) {
94*1976Sab196087 		return (conv_map2str(string, sizeof (string), sec, fmt_flags,
95*1976Sab196087 			ARRAY_NELTS(secs), secs, secs_alt, NULL));
96*1976Sab196087 	} else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW)) {
97*1976Sab196087 		return (conv_map2str(string, sizeof (string), sec - SHT_LOSUNW,
98*1976Sab196087 			fmt_flags, ARRAY_NELTS(usecs), usecs, usecs_alt, NULL));
99*1976Sab196087 	} else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
100*1976Sab196087 		switch (mach) {
101*1976Sab196087 		case EM_SPARC:
102*1976Sab196087 		case EM_SPARC32PLUS:
103*1976Sab196087 		case EM_SPARCV9:
104*1976Sab196087 			if (sec == SHT_SPARC_GOTDATA) {
105*1976Sab196087 				return (fmt_flags & CONV_FMT_ALTDUMP)
106*1976Sab196087 					? MSG_ORIG(MSG_SHT_SPARC_GOTDATA_ALT)
107*1976Sab196087 					: MSG_ORIG(MSG_SHT_SPARC_GOTDATA);
108*1976Sab196087 			}
109*1976Sab196087 			break;
110*1976Sab196087 		case EM_AMD64:
111*1976Sab196087 			if (sec == SHT_AMD64_UNWIND) {
112*1976Sab196087 				return (fmt_flags & CONV_FMT_ALTDUMP)
113*1976Sab196087 					? MSG_ORIG(MSG_SHT_AMD64_UNWIND_ALT)
114*1976Sab196087 					: MSG_ORIG(MSG_SHT_AMD64_UNWIND);
115*1976Sab196087 			}
116*1976Sab196087 		}
117*1976Sab196087 	}
118*1976Sab196087 
119*1976Sab196087 	/* If we get here, it's an unknown type */
120*1976Sab196087 	return (conv_invalid_val(string, CONV_INV_STRSIZE, sec, fmt_flags));
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
1240Sstevel@tonic-gate 		MSG_SHF_WRITE_SIZE + \
1250Sstevel@tonic-gate 		MSG_SHF_ALLOC_SIZE + \
1260Sstevel@tonic-gate 		MSG_SHF_EXECINSTR_SIZE + \
1270Sstevel@tonic-gate 		MSG_SHF_MERGE_SIZE + \
1280Sstevel@tonic-gate 		MSG_SHF_STRINGS_SIZE + \
1290Sstevel@tonic-gate 		MSG_SHF_INFO_LINK_SIZE + \
1300Sstevel@tonic-gate 		MSG_SHF_LINK_ORDER_SIZE + \
1310Sstevel@tonic-gate 		MSG_SHF_OS_NONCONFORMING_SIZE + \
1320Sstevel@tonic-gate 		MSG_SHF_GROUP_SIZE + \
1330Sstevel@tonic-gate 		MSG_SHF_TLS_SIZE + \
1340Sstevel@tonic-gate 		MSG_SHF_EXCLUDE_SIZE + \
1350Sstevel@tonic-gate 		MSG_SHF_ORDERED_SIZE + \
136685Srie 		MSG_SHF_AMD64_LARGE_SIZE + \
1371618Srie 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate const char *
1401618Srie conv_sec_flags(Xword flags)
1410Sstevel@tonic-gate {
1421618Srie 	static	char	string[FLAGSZ];
1431618Srie 	static Val_desc vda[] = {
1441618Srie 		{ SHF_WRITE,		MSG_ORIG(MSG_SHF_WRITE) },
1451618Srie 		{ SHF_ALLOC,		MSG_ORIG(MSG_SHF_ALLOC) },
1461618Srie 		{ SHF_EXECINSTR,	MSG_ORIG(MSG_SHF_EXECINSTR) },
1471618Srie 		{ SHF_MERGE,		MSG_ORIG(MSG_SHF_MERGE) },
1481618Srie 		{ SHF_STRINGS,		MSG_ORIG(MSG_SHF_STRINGS) },
1491618Srie 		{ SHF_INFO_LINK,	MSG_ORIG(MSG_SHF_INFO_LINK) },
1501618Srie 		{ SHF_LINK_ORDER,	MSG_ORIG(MSG_SHF_LINK_ORDER) },
1511618Srie 		{ SHF_OS_NONCONFORMING,	MSG_ORIG(MSG_SHF_OS_NONCONFORMING) },
1521618Srie 		{ SHF_GROUP,		MSG_ORIG(MSG_SHF_GROUP) },
1531618Srie 		{ SHF_TLS,		MSG_ORIG(MSG_SHF_TLS) },
1541618Srie 		{ SHF_EXCLUDE,		MSG_ORIG(MSG_SHF_EXCLUDE) },
1551618Srie 		{ SHF_ORDERED,		MSG_ORIG(MSG_SHF_ORDERED) },
1561618Srie 		{ SHF_AMD64_LARGE,	MSG_ORIG(MSG_SHF_AMD64_LARGE) },
1571618Srie 		{ 0,			0 }
1581618Srie 	};
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (flags == 0)
1610Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
1620Sstevel@tonic-gate 
1631618Srie 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
1641618Srie 	if (conv_expn_field(string, FLAGSZ, vda, flags, flags, 0, 0))
1651618Srie 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
1660Sstevel@tonic-gate 
1671618Srie 	return ((const char *)string);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate const char *
1711618Srie conv_sec_info(Word info, Xword flags)
1720Sstevel@tonic-gate {
1731618Srie 	static	char	string[CONV_INV_STRSIZE];
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (flags & SHF_ORDERED) {
1760Sstevel@tonic-gate 		if (info == SHN_BEFORE)
1770Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_BEFORE));
1780Sstevel@tonic-gate 		else if (info == SHN_AFTER)
1790Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_AFTER));
1800Sstevel@tonic-gate 	}
1811618Srie 	(void) conv_invalid_val(string, CONV_INV_STRSIZE, info, 1);
1820Sstevel@tonic-gate 	return ((const char *)string);
1830Sstevel@tonic-gate }
184