xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 5152:fa5ab16f3606)
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 /*
233492Sab196087  * 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] = {
683492Sab196087 	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] = {
783492Sab196087 	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 };
873492Sab196087 #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 *
935088Sab196087 conv_sec_type(Half mach, Word sec, Conv_fmt_flags_t fmt_flags,
945088Sab196087     Conv_inv_buf_t *inv_buf)
950Sstevel@tonic-gate {
961976Sab196087 	if (sec < SHT_NUM) {
975088Sab196087 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
985088Sab196087 		case CONV_FMT_ALT_DUMP:
995088Sab196087 			return (conv_map2str(inv_buf, sec, fmt_flags,
1005088Sab196087 			    ARRAY_NELTS(secs_alt), secs_alt));
1015088Sab196087 		default:
1025088Sab196087 			return (conv_map2str(inv_buf, sec, fmt_flags,
1035088Sab196087 			    ARRAY_NELTS(secs), secs));
1045088Sab196087 		}
1051976Sab196087 	} else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW)) {
1065088Sab196087 		switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
1075088Sab196087 		case CONV_FMT_ALT_DUMP:
1085088Sab196087 			return (conv_map2str(inv_buf, sec - SHT_LOSUNW,
1095088Sab196087 			    fmt_flags, ARRAY_NELTS(usecs_alt), usecs_alt));
1105088Sab196087 		default:
1115088Sab196087 			return (conv_map2str(inv_buf, sec - SHT_LOSUNW,
1125088Sab196087 			    fmt_flags, ARRAY_NELTS(usecs), usecs));
1135088Sab196087 		}
1141976Sab196087 	} else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
1151976Sab196087 		switch (mach) {
1161976Sab196087 		case EM_SPARC:
1171976Sab196087 		case EM_SPARC32PLUS:
1181976Sab196087 		case EM_SPARCV9:
1195088Sab196087 			if (sec != SHT_SPARC_GOTDATA)
1205088Sab196087 				break;
1215088Sab196087 			switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
1225088Sab196087 			case CONV_FMT_ALT_DUMP:
1235088Sab196087 			case CONV_FMT_ALT_FILE:
1245088Sab196087 				return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA_ALT));
1251976Sab196087 			}
1265088Sab196087 			return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA));
1271976Sab196087 		case EM_AMD64:
1285088Sab196087 			if (sec != SHT_AMD64_UNWIND)
1295088Sab196087 				break;
1305088Sab196087 			switch (CONV_TYPE_FMT_ALT(fmt_flags)) {
1315088Sab196087 			case CONV_FMT_ALT_DUMP:
1325088Sab196087 			case CONV_FMT_ALT_FILE:
1335088Sab196087 				return (MSG_ORIG(MSG_SHT_AMD64_UNWIND_ALT));
1341976Sab196087 			}
1355088Sab196087 			return (MSG_ORIG(MSG_SHT_AMD64_UNWIND));
1361976Sab196087 		}
1371976Sab196087 	}
1381976Sab196087 
1391976Sab196087 	/* If we get here, it's an unknown type */
1404734Sab196087 	return (conv_invalid_val(inv_buf, sec, fmt_flags));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1432352Sab196087 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
1442352Sab196087 		MSG_SHF_WRITE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1452352Sab196087 		MSG_SHF_ALLOC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1462352Sab196087 		MSG_SHF_EXECINSTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1472352Sab196087 		MSG_SHF_MERGE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1482352Sab196087 		MSG_SHF_STRINGS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1492352Sab196087 		MSG_SHF_INFO_LINK_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1502352Sab196087 		MSG_SHF_LINK_ORDER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1512352Sab196087 		MSG_SHF_OS_NONCONFORMING_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1522352Sab196087 		MSG_SHF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1532352Sab196087 		MSG_SHF_TLS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1542352Sab196087 		MSG_SHF_EXCLUDE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1552352Sab196087 		MSG_SHF_ORDERED_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1562352Sab196087 		MSG_SHF_AMD64_LARGE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1574734Sab196087 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
1584734Sab196087 
1594734Sab196087 /*
1604734Sab196087  * Ensure that Conv_sec_flags_buf_t is large enough:
1614734Sab196087  *
1624734Sab196087  * FLAGSZ is the real minimum size of the buffer required by conv_sec_flags().
1634734Sab196087  * However, Conv_sec_flags_buf_t uses CONV_SEC_FLAGS_BUFSIZE to set the
1644734Sab196087  * buffer size. We do things this way because the definition of FLAGSZ uses
1654734Sab196087  * information that is not available in the environment of other programs
1664734Sab196087  * that include the conv.h header file.
1674734Sab196087  */
168*5152Sab196087 #if (CONV_SEC_FLAGS_BUFSIZE != FLAGSZ) && !defined(__lint)
169*5152Sab196087 #define	REPORT_BUFSIZE FLAGSZ
170*5152Sab196087 #include "report_bufsize.h"
171*5152Sab196087 #error "CONV_SEC_FLAGS_BUFSIZE does not match FLAGSZ"
1724734Sab196087 #endif
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate const char *
1755088Sab196087 conv_sec_flags(Xword flags, Conv_fmt_flags_t fmt_flags,
1765088Sab196087     Conv_sec_flags_buf_t *sec_flags_buf)
1770Sstevel@tonic-gate {
1781618Srie 	static Val_desc vda[] = {
1791618Srie 		{ SHF_WRITE,		MSG_ORIG(MSG_SHF_WRITE) },
1801618Srie 		{ SHF_ALLOC,		MSG_ORIG(MSG_SHF_ALLOC) },
1811618Srie 		{ SHF_EXECINSTR,	MSG_ORIG(MSG_SHF_EXECINSTR) },
1821618Srie 		{ SHF_MERGE,		MSG_ORIG(MSG_SHF_MERGE) },
1831618Srie 		{ SHF_STRINGS,		MSG_ORIG(MSG_SHF_STRINGS) },
1841618Srie 		{ SHF_INFO_LINK,	MSG_ORIG(MSG_SHF_INFO_LINK) },
1851618Srie 		{ SHF_LINK_ORDER,	MSG_ORIG(MSG_SHF_LINK_ORDER) },
1861618Srie 		{ SHF_OS_NONCONFORMING,	MSG_ORIG(MSG_SHF_OS_NONCONFORMING) },
1871618Srie 		{ SHF_GROUP,		MSG_ORIG(MSG_SHF_GROUP) },
1881618Srie 		{ SHF_TLS,		MSG_ORIG(MSG_SHF_TLS) },
1891618Srie 		{ SHF_EXCLUDE,		MSG_ORIG(MSG_SHF_EXCLUDE) },
1901618Srie 		{ SHF_ORDERED,		MSG_ORIG(MSG_SHF_ORDERED) },
1911618Srie 		{ SHF_AMD64_LARGE,	MSG_ORIG(MSG_SHF_AMD64_LARGE) },
1921618Srie 		{ 0,			0 }
1931618Srie 	};
1944734Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = {
1954734Sab196087 	    NULL, sizeof (sec_flags_buf->buf), vda };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (flags == 0)
1980Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
1990Sstevel@tonic-gate 
2004734Sab196087 	conv_arg.buf = sec_flags_buf->buf;
2012352Sab196087 	conv_arg.oflags = conv_arg.rflags = flags;
2025088Sab196087 	(void) conv_expn_field(&conv_arg, fmt_flags);
2030Sstevel@tonic-gate 
2044734Sab196087 	return ((const char *)sec_flags_buf->buf);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate const char *
2084734Sab196087 conv_sec_linkinfo(Word info, Xword flags, Conv_inv_buf_t *inv_buf)
2090Sstevel@tonic-gate {
2102647Srie 	if (flags & ALL_SHF_ORDER) {
2110Sstevel@tonic-gate 		if (info == SHN_BEFORE)
2120Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_BEFORE));
2130Sstevel@tonic-gate 		else if (info == SHN_AFTER)
2140Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_AFTER));
2150Sstevel@tonic-gate 	}
2164734Sab196087 
2175088Sab196087 	(void) conv_invalid_val(inv_buf, info, CONV_FMT_DECIMAL);
2184734Sab196087 	return ((const char *)inv_buf->buf);
2190Sstevel@tonic-gate }
220