xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 3492:cd4326c9ab0e)
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 /*
23*3492Sab196087  * Copyright 2007 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 
381976Sab196087 
391976Sab196087 
401976Sab196087 /* Instantiate a local copy of conv_map2str() from _conv.h */
411976Sab196087 DEFINE_conv_map2str
421976Sab196087 
431976Sab196087 
441976Sab196087 
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 };
541976Sab196087 static const Msg secs_alt[SHT_NUM] = {
551976Sab196087 	MSG_SHT_NULL_ALT,	MSG_SHT_PROGBITS_ALT,	MSG_SHT_SYMTAB_ALT,
561976Sab196087 	MSG_SHT_STRTAB_ALT,	MSG_SHT_RELA_ALT,	MSG_SHT_HASH_ALT,
571976Sab196087 	MSG_SHT_DYNAMIC_ALT,	MSG_SHT_NOTE_ALT,	MSG_SHT_NOBITS_ALT,
581976Sab196087 	MSG_SHT_REL_ALT,	MSG_SHT_SHLIB_ALT,	MSG_SHT_DYNSYM_ALT,
591976Sab196087 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY_ALT,
601976Sab196087 	MSG_SHT_FINI_ARRAY_ALT,	MSG_SHT_PREINIT_ARRAY_ALT, MSG_SHT_GROUP_ALT,
611976Sab196087 	MSG_SHT_SYMTAB_SHNDX_ALT
621976Sab196087 };
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*3492Sab196087 	MSG_SHT_SUNW_symsort,		MSG_SHT_SUNW_tlssort,
692766Sab196087 	MSG_SHT_SUNW_LDYNSYM,		MSG_SHT_SUNW_dof,
702766Sab196087 	MSG_SHT_SUNW_cap,		MSG_SHT_SUNW_SIGNATURE,
712766Sab196087 	MSG_SHT_SUNW_ANNOTATE,		MSG_SHT_SUNW_DEBUGSTR,
722766Sab196087 	MSG_SHT_SUNW_DEBUG,		MSG_SHT_SUNW_move,
732766Sab196087 	MSG_SHT_SUNW_COMDAT,		MSG_SHT_SUNW_syminfo,
742766Sab196087 	MSG_SHT_SUNW_verdef,		MSG_SHT_SUNW_verneed,
752766Sab196087 	MSG_SHT_SUNW_versym
761976Sab196087 };
771976Sab196087 static const Msg usecs_alt[SHT_HISUNW - SHT_LOSUNW + 1] = {
78*3492Sab196087 	MSG_SHT_SUNW_symsort_ALT,	MSG_SHT_SUNW_tlssort_ALT,
792766Sab196087 	MSG_SHT_SUNW_LDYNSYM_ALT,	MSG_SHT_SUNW_dof_ALT,
802766Sab196087 	MSG_SHT_SUNW_cap_ALT,		MSG_SHT_SUNW_SIGNATURE_ALT,
812766Sab196087 	MSG_SHT_SUNW_ANNOTATE_ALT,	MSG_SHT_SUNW_DEBUGSTR_ALT,
822766Sab196087 	MSG_SHT_SUNW_DEBUG_ALT,		MSG_SHT_SUNW_move_ALT,
832766Sab196087 	MSG_SHT_SUNW_COMDAT_ALT,	MSG_SHT_SUNW_syminfo_ALT,
842766Sab196087 	MSG_SHT_SUNW_verdef_ALT,	MSG_SHT_SUNW_verneed_ALT,
852766Sab196087 	MSG_SHT_SUNW_versym_ALT
860Sstevel@tonic-gate };
87*3492Sab196087 #if	(SHT_LOSUNW != SHT_SUNW_symsort)
880Sstevel@tonic-gate #error	"SHT_LOSUNW has moved"
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 
911976Sab196087 
920Sstevel@tonic-gate const char *
931976Sab196087 conv_sec_type(Half mach, Word sec, int fmt_flags)
940Sstevel@tonic-gate {
952850Srie 	static Conv_inv_buf_t	string;
960Sstevel@tonic-gate 
971976Sab196087 	if (sec < SHT_NUM) {
981976Sab196087 		return (conv_map2str(string, sizeof (string), sec, fmt_flags,
991976Sab196087 			ARRAY_NELTS(secs), secs, secs_alt, NULL));
1001976Sab196087 	} else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW)) {
1011976Sab196087 		return (conv_map2str(string, sizeof (string), sec - SHT_LOSUNW,
1021976Sab196087 			fmt_flags, ARRAY_NELTS(usecs), usecs, usecs_alt, NULL));
1031976Sab196087 	} else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
1041976Sab196087 		switch (mach) {
1051976Sab196087 		case EM_SPARC:
1061976Sab196087 		case EM_SPARC32PLUS:
1071976Sab196087 		case EM_SPARCV9:
1081976Sab196087 			if (sec == SHT_SPARC_GOTDATA) {
1091976Sab196087 				return (fmt_flags & CONV_FMT_ALTDUMP)
1101976Sab196087 					? MSG_ORIG(MSG_SHT_SPARC_GOTDATA_ALT)
1111976Sab196087 					: MSG_ORIG(MSG_SHT_SPARC_GOTDATA);
1121976Sab196087 			}
1131976Sab196087 			break;
1141976Sab196087 		case EM_AMD64:
1151976Sab196087 			if (sec == SHT_AMD64_UNWIND) {
1161976Sab196087 				return (fmt_flags & CONV_FMT_ALTDUMP)
1171976Sab196087 					? MSG_ORIG(MSG_SHT_AMD64_UNWIND_ALT)
1181976Sab196087 					: MSG_ORIG(MSG_SHT_AMD64_UNWIND);
1191976Sab196087 			}
1201976Sab196087 		}
1211976Sab196087 	}
1221976Sab196087 
1231976Sab196087 	/* If we get here, it's an unknown type */
1241976Sab196087 	return (conv_invalid_val(string, CONV_INV_STRSIZE, sec, fmt_flags));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1272352Sab196087 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
1282352Sab196087 		MSG_SHF_WRITE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1292352Sab196087 		MSG_SHF_ALLOC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1302352Sab196087 		MSG_SHF_EXECINSTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1312352Sab196087 		MSG_SHF_MERGE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1322352Sab196087 		MSG_SHF_STRINGS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1332352Sab196087 		MSG_SHF_INFO_LINK_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1342352Sab196087 		MSG_SHF_LINK_ORDER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1352352Sab196087 		MSG_SHF_OS_NONCONFORMING_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1362352Sab196087 		MSG_SHF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1372352Sab196087 		MSG_SHF_TLS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1382352Sab196087 		MSG_SHF_EXCLUDE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1392352Sab196087 		MSG_SHF_ORDERED_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1402352Sab196087 		MSG_SHF_AMD64_LARGE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1412352Sab196087 		CONV_INV_STRSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate const char *
1441618Srie conv_sec_flags(Xword flags)
1450Sstevel@tonic-gate {
1461618Srie 	static	char	string[FLAGSZ];
1471618Srie 	static Val_desc vda[] = {
1481618Srie 		{ SHF_WRITE,		MSG_ORIG(MSG_SHF_WRITE) },
1491618Srie 		{ SHF_ALLOC,		MSG_ORIG(MSG_SHF_ALLOC) },
1501618Srie 		{ SHF_EXECINSTR,	MSG_ORIG(MSG_SHF_EXECINSTR) },
1511618Srie 		{ SHF_MERGE,		MSG_ORIG(MSG_SHF_MERGE) },
1521618Srie 		{ SHF_STRINGS,		MSG_ORIG(MSG_SHF_STRINGS) },
1531618Srie 		{ SHF_INFO_LINK,	MSG_ORIG(MSG_SHF_INFO_LINK) },
1541618Srie 		{ SHF_LINK_ORDER,	MSG_ORIG(MSG_SHF_LINK_ORDER) },
1551618Srie 		{ SHF_OS_NONCONFORMING,	MSG_ORIG(MSG_SHF_OS_NONCONFORMING) },
1561618Srie 		{ SHF_GROUP,		MSG_ORIG(MSG_SHF_GROUP) },
1571618Srie 		{ SHF_TLS,		MSG_ORIG(MSG_SHF_TLS) },
1581618Srie 		{ SHF_EXCLUDE,		MSG_ORIG(MSG_SHF_EXCLUDE) },
1591618Srie 		{ SHF_ORDERED,		MSG_ORIG(MSG_SHF_ORDERED) },
1601618Srie 		{ SHF_AMD64_LARGE,	MSG_ORIG(MSG_SHF_AMD64_LARGE) },
1611618Srie 		{ 0,			0 }
1621618Srie 	};
1632352Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = { string, sizeof (string), vda };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (flags == 0)
1660Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
1670Sstevel@tonic-gate 
1682352Sab196087 	conv_arg.oflags = conv_arg.rflags = flags;
1692352Sab196087 	(void) conv_expn_field(&conv_arg);
1700Sstevel@tonic-gate 
1711618Srie 	return ((const char *)string);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1742850Srie /*
1752850Srie  * conv_sec_linkinfo() is one of the few conversion routines that can be called
1762850Srie  * twice for the same diagnostic (see liblddbg:Elf_Shdr()), hence the caller
1772850Srie  * has to supply different string buffers.
1782850Srie  */
1790Sstevel@tonic-gate const char *
1802850Srie conv_sec_linkinfo(Word info, Xword flags, Conv_inv_buf_t buffer)
1810Sstevel@tonic-gate {
1822647Srie 	if (flags & ALL_SHF_ORDER) {
1830Sstevel@tonic-gate 		if (info == SHN_BEFORE)
1840Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_BEFORE));
1850Sstevel@tonic-gate 		else if (info == SHN_AFTER)
1860Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_AFTER));
1870Sstevel@tonic-gate 	}
1882850Srie 	(void) conv_invalid_val(buffer, CONV_INV_STRSIZE, info, 1);
1892850Srie 	return ((const char *)buffer);
1900Sstevel@tonic-gate }
191