xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/sections.c (revision 574:31f933284640)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*574Sseizo  * Copyright 2005 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 
380Sstevel@tonic-gate static const Msg secs[SHT_NUM] = {
390Sstevel@tonic-gate 	MSG_SHT_NULL,		MSG_SHT_PROGBITS,	MSG_SHT_SYMTAB,
400Sstevel@tonic-gate 	MSG_SHT_STRTAB,		MSG_SHT_RELA,		MSG_SHT_HASH,
410Sstevel@tonic-gate 	MSG_SHT_DYNAMIC,	MSG_SHT_NOTE,		MSG_SHT_NOBITS,
420Sstevel@tonic-gate 	MSG_SHT_REL,		MSG_SHT_SHLIB,		MSG_SHT_DYNSYM,
430Sstevel@tonic-gate 	MSG_SHT_UNKNOWN12,	MSG_SHT_UNKNOWN13,	MSG_SHT_INIT_ARRAY,
440Sstevel@tonic-gate 	MSG_SHT_FINI_ARRAY,	MSG_SHT_PREINIT_ARRAY,	MSG_SHT_GROUP,
450Sstevel@tonic-gate 	MSG_SHT_SYMTAB_SHNDX
460Sstevel@tonic-gate };
470Sstevel@tonic-gate #if	(SHT_NUM != (SHT_SYMTAB_SHNDX + 1))
480Sstevel@tonic-gate #error	"SHT_NUM has grown"
490Sstevel@tonic-gate #endif
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static const Msg usecs[SHT_HISUNW - SHT_LOSUNW + 1] = {
520Sstevel@tonic-gate 	MSG_SHT_SUNW_dof,	MSG_SHT_SUNW_cap,	MSG_SHT_SUNW_SIGNATURE,
530Sstevel@tonic-gate 	MSG_SHT_SUNW_ANNOTATE,	MSG_SHT_SUNW_DEBUGSTR,	MSG_SHT_SUNW_DEBUG,
540Sstevel@tonic-gate 	MSG_SHT_SUNW_move,	MSG_SHT_SUNW_COMDAT,	MSG_SHT_SUNW_syminfo,
550Sstevel@tonic-gate 	MSG_SHT_SUNW_verdef,	MSG_SHT_SUNW_verneed,	MSG_SHT_SUNW_versym
560Sstevel@tonic-gate };
570Sstevel@tonic-gate #if	(SHT_LOSUNW != SHT_SUNW_dof)
580Sstevel@tonic-gate #error	"SHT_LOSUNW has moved"
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate 
610Sstevel@tonic-gate const char *
620Sstevel@tonic-gate /* ARGSUSED 1 */
630Sstevel@tonic-gate conv_sectyp_str(ushort_t mach, uint_t sec)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	static char	string[STRSIZE] = { '\0' };
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (sec < SHT_NUM)
680Sstevel@tonic-gate 		return (MSG_ORIG(secs[sec]));
690Sstevel@tonic-gate 	else if ((sec >= SHT_LOSUNW) && (sec <= SHT_HISUNW))
700Sstevel@tonic-gate 		return (MSG_ORIG(usecs[sec - SHT_LOSUNW]));
710Sstevel@tonic-gate 	else if ((sec >= SHT_LOPROC) && (sec <= SHT_HIPROC)) {
720Sstevel@tonic-gate 		if ((sec == (uint_t)SHT_SPARC_GOTDATA) &&
730Sstevel@tonic-gate 		    ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
740Sstevel@tonic-gate 		    (mach == EM_SPARCV9)))
750Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHT_SPARC_GOTDATA));
760Sstevel@tonic-gate 		else if ((sec == (uint_t)SHT_AMD64_UNWIND) &&
770Sstevel@tonic-gate 		    (mach == EM_AMD64))
780Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHT_AMD64_UNWIND));
790Sstevel@tonic-gate 		else
800Sstevel@tonic-gate 			return (conv_invalid_str(string, STRSIZE, sec, 0));
810Sstevel@tonic-gate 	} else
820Sstevel@tonic-gate 		return (conv_invalid_str(string, STRSIZE, sec, 0));
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
860Sstevel@tonic-gate 		MSG_SHF_WRITE_SIZE + \
870Sstevel@tonic-gate 		MSG_SHF_ALLOC_SIZE + \
880Sstevel@tonic-gate 		MSG_SHF_EXECINSTR_SIZE + \
890Sstevel@tonic-gate 		MSG_SHF_MERGE_SIZE + \
900Sstevel@tonic-gate 		MSG_SHF_STRINGS_SIZE + \
910Sstevel@tonic-gate 		MSG_SHF_INFO_LINK_SIZE + \
920Sstevel@tonic-gate 		MSG_SHF_LINK_ORDER_SIZE + \
930Sstevel@tonic-gate 		MSG_SHF_OS_NONCONFORMING_SIZE + \
940Sstevel@tonic-gate 		MSG_SHF_GROUP_SIZE + \
950Sstevel@tonic-gate 		MSG_SHF_TLS_SIZE + \
960Sstevel@tonic-gate 		MSG_SHF_EXCLUDE_SIZE + \
970Sstevel@tonic-gate 		MSG_SHF_ORDERED_SIZE + \
98*574Sseizo 		MSG_SHF_AMD64_LARGE + \
990Sstevel@tonic-gate 		MSG_GBL_CSQBRKT_SIZE
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate const char *
1020Sstevel@tonic-gate /* ARGSUSED 1 */
1030Sstevel@tonic-gate conv_secflg_str(ushort_t mach, uint_t flags)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	static	char	string[FLAGSZ] = { '\0' };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (flags == 0)
1080Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
1090Sstevel@tonic-gate 	else {
1100Sstevel@tonic-gate 		uint_t	flags_handled = 0;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
1130Sstevel@tonic-gate 		if (flags & SHF_WRITE) {
1140Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_WRITE));
1150Sstevel@tonic-gate 			flags_handled |= SHF_WRITE;
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 		if (flags & SHF_ALLOC) {
1180Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_ALLOC));
1190Sstevel@tonic-gate 			flags_handled |= SHF_ALLOC;
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 		if (flags & SHF_EXECINSTR) {
1220Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXECINSTR));
1230Sstevel@tonic-gate 			flags_handled |= SHF_EXECINSTR;
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		if (flags & SHF_MERGE) {
1260Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_MERGE));
1270Sstevel@tonic-gate 			flags_handled |= SHF_MERGE;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 		if (flags & SHF_STRINGS) {
1300Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_STRINGS));
1310Sstevel@tonic-gate 			flags_handled |= SHF_STRINGS;
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 		if (flags & SHF_INFO_LINK) {
1340Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_INFO_LINK));
1350Sstevel@tonic-gate 			flags_handled |= SHF_INFO_LINK;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 		if (flags & SHF_LINK_ORDER) {
1380Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_LINK_ORDER));
1390Sstevel@tonic-gate 			flags_handled |= SHF_LINK_ORDER;
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 		if (flags & SHF_OS_NONCONFORMING) {
1420Sstevel@tonic-gate 			(void) strcat(string,
1430Sstevel@tonic-gate 				MSG_ORIG(MSG_SHF_OS_NONCONFORMING));
1440Sstevel@tonic-gate 			flags_handled |= SHF_OS_NONCONFORMING;
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 		if (flags & SHF_GROUP) {
1470Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_GROUP));
1480Sstevel@tonic-gate 			flags_handled |= SHF_GROUP;
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 		if (flags & SHF_TLS) {
1510Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_TLS));
1520Sstevel@tonic-gate 			flags_handled |= SHF_TLS;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		if (flags & SHF_EXCLUDE) {
1550Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_EXCLUDE));
1560Sstevel@tonic-gate 			flags_handled |= SHF_EXCLUDE;
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 		if (flags & SHF_ORDERED) {
1590Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_SHF_ORDERED));
1600Sstevel@tonic-gate 			flags_handled |= SHF_ORDERED;
1610Sstevel@tonic-gate 		}
162*574Sseizo 		if (flags & SHF_AMD64_LARGE) {
163*574Sseizo 			(void) strcat(string, MSG_ORIG(MSG_SHF_AMD64_LARGE));
164*574Sseizo 			flags_handled |= SHF_AMD64_LARGE;
165*574Sseizo 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * Are there any flags that haven't been handled.
1690Sstevel@tonic-gate 		 */
1700Sstevel@tonic-gate 		if ((flags & flags_handled) != flags) {
1710Sstevel@tonic-gate 			char	*str;
1720Sstevel@tonic-gate 			size_t	len;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 			len = strlen(string);
1750Sstevel@tonic-gate 			str = string + len;
1760Sstevel@tonic-gate 			(void) conv_invalid_str(str, FLAGSZ - len,
1770Sstevel@tonic-gate 			    (flags & (~flags_handled)), 0);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		return ((const char *)string);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Need to be able to hold a 32bit signed integer.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate #define	INFOSZ	128
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate const char *
1910Sstevel@tonic-gate conv_secinfo_str(uint_t info, uint_t flags)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	static	char	string[INFOSZ] = { '\0' };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (flags & SHF_ORDERED) {
1960Sstevel@tonic-gate 		if (info == SHN_BEFORE)
1970Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_BEFORE));
1980Sstevel@tonic-gate 		else if (info == SHN_AFTER)
1990Sstevel@tonic-gate 			return (MSG_ORIG(MSG_SHN_AFTER));
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	(void) conv_invalid_str(string, INFOSZ, info, 1);
2020Sstevel@tonic-gate 	return ((const char *)string);
2030Sstevel@tonic-gate }
204