xref: /titanic_50/usr/src/cmd/sgs/libconv/common/dwarf_ehe.c (revision f38cb554a534c6df738be3f4d23327e69888e634)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<strings.h>
29 #include	<dwarf.h>
30 #include	"_conv.h"
31 #include	<dwarf_ehe_msg.h>
32 
33 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
34 		MSG_DWEHE_SLEB128_SIZE + \
35 		MSG_DWEHE_INDIRECT_SIZE + \
36 		CONV_INV_BUFSIZE + MSG_GBL_CSQBRKT_SIZE
37 
38 /*
39  * Ensure that Conv_dwarf_ehe_buf_t is large enough:
40  *
41  * FLAGSZ is the real minimum size of the buffer required by conv_dwarf_ehe().
42  * However, Conv_dwarf_ehe_buf_t uses CONV_EHDR_FLAG_BUFSIZE to set the
43  * buffer size. We do things this way because the definition of FLAGSZ uses
44  * information that is not available in the environment of other programs
45  * that include the conv.h header file.
46  */
47 #if (CONV_DWARF_EHE_BUFSIZE != FLAGSZ) && !defined(__lint)
48 #define	REPORT_BUFSIZE FLAGSZ
49 #include "report_bufsize.h"
50 #error "CONV_DWARF_EHE_BUFSIZE does not match FLAGSZ"
51 #endif
52 
53 const char *
54 conv_dwarf_ehe(uint_t flags, Conv_dwarf_ehe_buf_t *dwarf_ehe_buf)
55 {
56 	char	*buf = dwarf_ehe_buf->buf;
57 	size_t	ret = 0;
58 
59 	(void) strncpy(buf, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
60 
61 	if (flags == DW_EH_PE_omit)
62 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_OMIT), FLAGSZ);
63 	else if (flags == DW_EH_PE_absptr)
64 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_ABSPTR), FLAGSZ);
65 
66 	if (ret >= FLAGSZ)
67 		return (conv_invalid_val(&dwarf_ehe_buf->inv_buf, flags, 0));
68 
69 	if ((flags == DW_EH_PE_omit) || (flags == DW_EH_PE_absptr)) {
70 		(void) strlcat(buf, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
71 		return (buf);
72 	}
73 
74 	switch (flags & 0x0f) {
75 	case DW_EH_PE_uleb128:
76 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_ULEB128), FLAGSZ);
77 		break;
78 	case DW_EH_PE_udata2:
79 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_UDATA2), FLAGSZ);
80 		break;
81 	case DW_EH_PE_udata4:
82 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_UDATA4), FLAGSZ);
83 		break;
84 	case DW_EH_PE_udata8:
85 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_UDATA8), FLAGSZ);
86 		break;
87 	case DW_EH_PE_sleb128:
88 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_SLEB128), FLAGSZ);
89 		break;
90 	case DW_EH_PE_sdata2:
91 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_SDATA2), FLAGSZ);
92 		break;
93 	case DW_EH_PE_sdata4:
94 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_SDATA4), FLAGSZ);
95 		break;
96 	case DW_EH_PE_sdata8:
97 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_SDATA8), FLAGSZ);
98 		break;
99 	}
100 	if (ret >= FLAGSZ)
101 		return (conv_invalid_val(&dwarf_ehe_buf->inv_buf, flags, 0));
102 
103 	switch (flags & 0xf0) {
104 	case DW_EH_PE_pcrel:
105 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_PCREL), FLAGSZ);
106 		break;
107 	case DW_EH_PE_textrel:
108 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_TEXTREL), FLAGSZ);
109 		break;
110 	case DW_EH_PE_datarel:
111 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_DATAREL), FLAGSZ);
112 		break;
113 	case DW_EH_PE_funcrel:
114 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_FUNCREL), FLAGSZ);
115 		break;
116 	case DW_EH_PE_aligned:
117 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_ALIGNED), FLAGSZ);
118 		break;
119 	case DW_EH_PE_indirect:
120 		ret = strlcat(buf, MSG_ORIG(MSG_DWEHE_INDIRECT), FLAGSZ);
121 		break;
122 	}
123 	if (ret >= FLAGSZ)
124 		return (conv_invalid_val(&dwarf_ehe_buf->inv_buf, flags, 0));
125 
126 	(void) strlcat(buf, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
127 	return (buf);
128 }
129