xref: /titanic_41/usr/src/tools/ctf/dwarf/common/dwarf_util.h (revision 26f3cdf03f1adcc98f6d3d99843ee71e9229a8c0)
1 /*
2 
3   Copyright (C) 2000,2003,2004 Silicon Graphics, Inc.  All Rights Reserved.
4 
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2.1 of the GNU Lesser General Public License
7   as published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it would be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13   Further, this software is distributed without any warranty that it is
14   free of the rightful claim of any third person regarding infringement
15   or the like.  Any license provided herein, whether implied or
16   otherwise, applies only to this software file.  Patent licenses, if
17   any, provided herein do not apply to combinations of this program with
18   other software, or any other product whatsoever.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program; if not, write the Free Software
22   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307,
23   USA.
24 
25   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
26   Mountain View, CA 94043, or:
27 
28   http://www.sgi.com
29 
30   For further information regarding this notice, see:
31 
32   http://oss.sgi.com/projects/GenInfo/NoticeExplan
33 
34 */
35 
36 
37 
38 /*
39     Decodes unsigned leb128 encoded numbers that
40     are assumed to be less than 4 bytes long.
41     Make sure ptr is a pointer to a 1-byte type.
42     Returns UINT_MAX on error.
43 
44 */
45 #define DECODE_LEB128_UWORD(ptr, value) \
46     { \
47        Dwarf_Word uleblen; \
48 	value = _dwarf_decode_u_leb128(ptr,&uleblen); \
49         ptr += uleblen; \
50     }
51 
52 /*
53     Decodes signed leb128 encoded numbers.
54     Make sure ptr is a pointer to a 1-byte type.
55 
56 */
57 #define DECODE_LEB128_SWORD(ptr, value) \
58     { \
59        Dwarf_Word sleblen; \
60 	value = _dwarf_decode_s_leb128(ptr,&sleblen); \
61         ptr += sleblen; \
62     }
63 
64 
65 /*
66     Skips leb128_encoded numbers that are guaranteed
67     to be no more than 4 bytes long.  Same for both
68     signed and unsigned numbers.
69 */
70 #define SKIP_LEB128_WORD(ptr) \
71     if ((*(ptr++) & 0x80) != 0) { \
72         if ((*(ptr++) & 0x80) != 0) { \
73             if ((*(ptr++) & 0x80) != 0) { \
74 	        if ((*(ptr++) & 0x80) != 0) { \
75 	        } \
76 	    } \
77         } \
78     }
79 
80 
81 #define CHECK_DIE(die, error_ret_value) \
82     if (die == NULL) { \
83 	_dwarf_error(NULL, error, DW_DLE_DIE_NULL); \
84 	return(error_ret_value); \
85     } \
86     if (die->di_cu_context == NULL) { \
87 	_dwarf_error(NULL, error, DW_DLE_DIE_NO_CU_CONTEXT); \
88 	return(error_ret_value); \
89     } \
90     if (die->di_cu_context->cc_dbg == NULL) { \
91 	_dwarf_error(NULL, error, DW_DLE_DBG_NULL); \
92 	return(error_ret_value); \
93     }
94 
95 
96 /*
97    Reads 'source' for 'length' bytes from unaligned addr.
98 
99    Avoids any constant-in-conditional warnings and
100    avoids a test in the generated code (for non-const cases,
101 	which are in the majority.)
102    Uses a temp to avoid the test.
103    The decl here should avoid any problem of size in the temp.
104    This code is ENDIAN DEPENDENT
105    The memcpy args are the endian issue.
106 */
107 typedef Dwarf_Unsigned BIGGEST_UINT;
108 
109 #ifdef WORDS_BIGENDIAN
110 #define READ_UNALIGNED(dbg,dest,desttype, source, length) \
111     { \
112       BIGGEST_UINT _ltmp = 0;  \
113       dbg->de_copy_word( (((char *)(&_ltmp)) + sizeof(_ltmp) - length), \
114 			source, length) ; \
115       dest = (desttype)_ltmp;  \
116     }
117 
118 
119 /*
120     This macro sign-extends a variable depending on the length.
121     It fills the bytes between the size of the destination and
122     the length with appropriate padding.
123     This code is ENDIAN DEPENDENT but dependent only
124     on host endianness, not object file endianness.
125     The memcpy args are the issue.
126 */
127 #define SIGN_EXTEND(dest, length) \
128     if (*(Dwarf_Sbyte *)((char *)&dest + sizeof(dest) - length) < 0) \
129 	memcpy((char *)&dest, "\xff\xff\xff\xff\xff\xff\xff\xff", \
130 	    sizeof(dest) - length)
131 #else /* LITTLE ENDIAN */
132 
133 #define READ_UNALIGNED(dbg,dest,desttype, source, length) \
134     { \
135       BIGGEST_UINT _ltmp = 0;  \
136       dbg->de_copy_word( (char *)(&_ltmp) , \
137                         source, length) ; \
138       dest = (desttype)_ltmp;  \
139     }
140 
141 
142 /*
143     This macro sign-extends a variable depending on the length.
144     It fills the bytes between the size of the destination and
145     the length with appropriate padding.
146     This code is ENDIAN DEPENDENT but dependent only
147     on host endianness, not object file endianness.
148     The memcpy args are the issue.
149 */
150 #define SIGN_EXTEND(dest, length) \
151     if (*(Dwarf_Sbyte *)((char *)&dest + (length-1)) < 0) \
152         memcpy((char *)&dest+length,    \
153                 "\xff\xff\xff\xff\xff\xff\xff\xff", \
154             sizeof(dest) - length)
155 
156 #endif /* ! LITTLE_ENDIAN */
157 
158 
159 
160 /*
161    READ_AREA LENGTH reads the length (the older way
162    of pure 32 or 64 bit
163    or the new proposed dwarfv2.1 64bit-extension way)
164 
165    It reads the bits from where rw_src_data_p  points to
166    and updates the rw_src_data_p to point past what was just read.
167 
168    It updates w_length_size and w_exten_size (which
169 	are really issues only for the dwarfv2.1  64bit extension).
170 
171    r_dbg is just the current dbg pointer.
172    w_target is the output length field.
173    r_targtype is the output type. Always Dwarf_Unsigned so far.
174 
175 */
176 /* This one handles the v2.1 64bit extension
177    and 32bit (and   MIPS fixed 64  bit via the
178 	dwarf_init-set r_dbg->de_length_size)..
179    It does not recognize any but the one distingushed value
180    (the only one with defined meaning).
181    It assumes that no CU will have a length
182 	0xffffffxx  (32bit length)
183 	or
184 	0xffffffxx xxxxxxxx (64bit length)
185    which makes possible auto-detection of the extension.
186 
187    This depends on knowing that only a non-zero length
188    is legitimate (AFAICT), and for IRIX non-standard -64
189    dwarf that the first 32 bits of the 64bit offset will be
190    zero (because the compiler could not handle a truly large
191    value as of Jan 2003 and because no app has that much debug
192    info anyway (yet)).
193 
194    At present not testing for '64bit elf' here as that
195    does not seem necessary (none of the 64bit length seems
196    appropriate unless it's  ident[EI_CLASS] == ELFCLASS64).
197    Might be a good idea though.
198 
199 */
200 #   define    READ_AREA_LENGTH(r_dbg,w_target,r_targtype,         \
201 	rw_src_data_p,w_length_size,w_exten_size)                 \
202     READ_UNALIGNED(r_dbg,w_target,r_targtype,                     \
203                 rw_src_data_p, ORIGINAL_DWARF_OFFSET_SIZE);       \
204     if(w_target == DISTINGUISHED_VALUE) {                         \
205 	     /* dwarf3 64bit extension */                         \
206              w_length_size  = DISTINGUISHED_VALUE_OFFSET_SIZE;    \
207              rw_src_data_p += ORIGINAL_DWARF_OFFSET_SIZE;         \
208              w_exten_size   = ORIGINAL_DWARF_OFFSET_SIZE;         \
209              READ_UNALIGNED(r_dbg,w_target,r_targtype,            \
210                   rw_src_data_p, DISTINGUISHED_VALUE_OFFSET_SIZE);\
211              rw_src_data_p += DISTINGUISHED_VALUE_OFFSET_SIZE;    \
212     } else {                                                      \
213 	if(w_target == 0 && r_dbg->de_big_endian_object) {        \
214 	     /* IRIX 64 bit, big endian */                        \
215              READ_UNALIGNED(r_dbg,w_target,r_targtype,            \
216                 rw_src_data_p, DISTINGUISHED_VALUE_OFFSET_SIZE);  \
217 	     w_length_size  = DISTINGUISHED_VALUE_OFFSET_SIZE;    \
218 	     rw_src_data_p += DISTINGUISHED_VALUE_OFFSET_SIZE;    \
219 	     w_exten_size = 0;                                    \
220 	} else {                                                  \
221 	     /* standard 32 bit dwarf2/dwarf3 */                  \
222 	     w_exten_size   = 0;                                  \
223              w_length_size  = ORIGINAL_DWARF_OFFSET_SIZE;         \
224              rw_src_data_p += w_length_size;                      \
225 	}                                                         \
226     }
227 
228 
229 
230 Dwarf_Unsigned
231 _dwarf_decode_u_leb128(Dwarf_Small * leb128,
232 		       Dwarf_Word * leb128_length);
233 
234 Dwarf_Signed
235 _dwarf_decode_s_leb128(Dwarf_Small * leb128,
236 		       Dwarf_Word * leb128_length);
237 
238 Dwarf_Unsigned
239 _dwarf_get_size_of_val(Dwarf_Debug dbg,
240 		       Dwarf_Unsigned form,
241 		       Dwarf_Small * val_ptr, int v_length_size);
242 
243 /*
244     This struct is used to build a hash table for the
245     abbreviation codes for a compile-unit.
246 */
247 struct Dwarf_Hash_Table_s {
248     Dwarf_Abbrev_List at_head;
249     Dwarf_Abbrev_List at_tail;
250 };
251 
252 Dwarf_Abbrev_List
253 _dwarf_get_abbrev_for_code(Dwarf_CU_Context cu_context,
254 			   Dwarf_Word code);
255 
256 
257 /* return 1 if string ends before 'endptr' else
258 ** return 0 meaning string is not properly terminated.
259 ** Presumption is the 'endptr' pts to end of some dwarf section data.
260 */
261 int _dwarf_string_valid(void *startptr, void *endptr);
262 
263 Dwarf_Unsigned _dwarf_length_of_cu_header(Dwarf_Debug,
264 					  Dwarf_Unsigned offset);
265 Dwarf_Unsigned _dwarf_length_of_cu_header_simple(Dwarf_Debug);
266 
267 int _dwarf_load_debug_info(Dwarf_Debug dbg, Dwarf_Error *error);
268