xref: /titanic_44/usr/src/tools/ctf/dwarf/common/dwarf_opaque.h (revision db8b037b5616a366b7dfdc01ef9552f02f9adfdd)
1 /*
2 
3   Copyright (C) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
4   Portions Copyright (C) 2007-2010 David Anderson. All Rights Reserved.
5   Portions Copyright (C) 2008-2010 Arxan Technologies, Inc. All Rights Reserved.
6 
7   This program is free software; you can redistribute it and/or modify it
8   under the terms of version 2.1 of the GNU Lesser General Public License
9   as published by the Free Software Foundation.
10 
11   This program is distributed in the hope that it would be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15   Further, this software is distributed without any warranty that it is
16   free of the rightful claim of any third person regarding infringement
17   or the like.  Any license provided herein, whether implied or
18   otherwise, applies only to this software file.  Patent licenses, if
19   any, provided herein do not apply to combinations of this program with
20   other software, or any other product whatsoever.
21 
22   You should have received a copy of the GNU Lesser General Public
23   License along with this program; if not, write the Free Software
24   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
25   USA.
26 
27   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
28   Mountain View, CA 94043, or:
29 
30   http://www.sgi.com
31 
32   For further information regarding this notice, see:
33 
34   http://oss.sgi.com/projects/GenInfo/NoticeExplan
35 
36 */
37 /* The versions applicable by section are:
38                        DWARF2 DWARF3 DWARF4
39  .debug_abbrev           -      -      -
40  .debug_aranges          2      2      2
41  .debug_frame            1      3      4
42  .debug_info             2      3      4
43  .debug_line             2      3      4
44  .debug_loc              -      -      -
45  .debug_macinfo          -      -      -
46  .debug_pubtypes         x      2      2
47  .debug_pubnames         2      2      2
48  .debug_ranges           x      -      -
49  .debug_str              -      -      -
50  .debug_types            x      x      4
51 */
52 
53 #include <stddef.h>
54 
55 
56 struct Dwarf_Die_s {
57     Dwarf_Byte_Ptr di_debug_info_ptr;
58     Dwarf_Abbrev_List di_abbrev_list;
59     Dwarf_CU_Context di_cu_context;
60     int  di_abbrev_code;
61 };
62 
63 struct Dwarf_Attribute_s {
64     Dwarf_Half ar_attribute;	/* Attribute Value. */
65     Dwarf_Half ar_attribute_form;	/* Attribute Form. */
66     Dwarf_Half ar_attribute_form_direct;
67 	        /* Identical to ar_attribute_form except that if
68 		the original form uleb was DW_FORM_indirect,
69 		ar_attribute_form_direct contains DW_FORM_indirect
70 		but ar_attribute_form contains the true form. */
71 
72     Dwarf_CU_Context ar_cu_context;
73     Dwarf_Small *ar_debug_info_ptr;
74     Dwarf_Attribute ar_next;
75 };
76 
77 /*
78     This structure provides the context for a compilation unit.
79     Thus, it contains the Dwarf_Debug, cc_dbg, that this cu
80     belongs to.  It contains the information in the compilation
81     unit header, cc_length, cc_version_stamp, cc_abbrev_offset,
82     and cc_address_size, in the .debug_info section for that cu.
83     In addition, it contains the count, cc_count_cu, of the cu
84     number of that cu in the list of cu's in the .debug_info.
85     The count starts at 1, ie cc_count_cu is 1 for the first cu,
86     2 for the second and so on.  This struct also contains a
87     pointer, cc_abbrev_table, to a list of pairs of abbrev code
88     and a pointer to the start of that abbrev
89     in the .debug_abbrev section.
90 
91     Each die will also contain a pointer to such a struct to
92     record the context for that die.
93 
94     Notice that a pointer to the CU DIE itself is
95     Dwarf_Off off2 = cu_context->cc_debug_info_offset;
96     cu_die_info_ptr = dbg->de_debug_info.dss_data +
97             off2 + _dwarf_length_of_cu_header(dbg, off2);
98 
99     **Updated by dwarf_next_cu_header in dwarf_die_deliv.c
100 */
101 struct Dwarf_CU_Context_s {
102     Dwarf_Debug cc_dbg;
103     /* The sum of cc_length, cc_length_size, and cc_extension_size
104        is the total length of the CU including its header. */
105     Dwarf_Word cc_length;
106     /* cc_length_size is the size in bytes of an offset.
107        4 for 32bit dwarf, 8 for 64bit dwarf (whether MIPS/IRIX
108        64bit dwarf or standard 64bit dwarf using the extension
109        mechanism). */
110     Dwarf_Small cc_length_size;
111     /* cc_extension_size is zero unless this is standard
112        DWARF3 and later 64bit dwarf using the extension mechanism.
113        If it is the DWARF3 and later 64bit dwarf cc_extension
114        size is 4. So for 32bit dwarf and MIPS/IRIX 64bit dwarf
115        cc_extension_size is zero.  */
116     Dwarf_Small cc_extension_size;
117     Dwarf_Half cc_version_stamp;
118     Dwarf_Sword cc_abbrev_offset;
119     Dwarf_Small cc_address_size;
120     /* cc_debug_info_offset is the offset in the section
121        of the CU header of this CU.  Dwarf_Word
122        should be large enough. */
123     Dwarf_Word cc_debug_info_offset;
124     Dwarf_Byte_Ptr cc_last_abbrev_ptr;
125     Dwarf_Hash_Table cc_abbrev_hash_table;
126     Dwarf_CU_Context cc_next;
127     /*unsigned char cc_offset_length; */
128 };
129 
130 /* Consolidates section-specific data in one place.
131    Section is an Elf specific term, intended as a general
132    term (for non-Elf objects some code must synthesize the
133    values somehow).
134    Makes adding more section-data much simpler. */
135 struct Dwarf_Section_s {
136     Dwarf_Small *  dss_data;
137     Dwarf_Unsigned dss_size;
138     Dwarf_Word     dss_index;
139     /* dss_addr is the 'section address' which is only
140        non-zero for a GNU eh section.
141        Purpose: to handle DW_EH_PE_pcrel encoding. Leaving
142        it zero is fine for non-elf.  */
143     Dwarf_Addr     dss_addr;
144     Dwarf_Small    dss_data_was_malloc;
145 
146     /* For non-elf, leaving the following fields zero
147        will mean they are ignored. */
148     /* dss_link should be zero unless a section has a link
149        to another (sh_link).  Used to access relocation data for
150        a section (and for symtab section, access its strtab). */
151     Dwarf_Word     dss_link;
152     /* The following is used when reading .rela sections
153        (such sections appear in some .o files). */
154     Dwarf_Half     dss_reloc_index; /* Zero means ignore the reloc fields. */
155     Dwarf_Small *  dss_reloc_data;
156     Dwarf_Unsigned dss_reloc_size;
157     Dwarf_Addr     dss_reloc_addr;
158     /* dss_reloc_symtab is the sh_link of a .rela to its .symtab, leave
159        it 0 if non-meaningful. */
160     Dwarf_Addr     dss_reloc_symtab;
161     /* dss_reloc_link should be zero unless a reloc section has a link
162        to another (sh_link).  Used to access the symtab for relocations
163        a section. */
164     Dwarf_Word     dss_reloc_link;
165     /* Pointer to the elf symtab, used for elf .rela. Leave it 0
166        if not relevant. */
167     struct Dwarf_Section_s *dss_symtab;
168 };
169 
170 /* Overview: if next_to_use== first, no error slots are used.
171    If next_to_use+1 (mod maxcount) == first the slots are all used
172 */
173 struct Dwarf_Harmless_s {
174   unsigned dh_maxcount;
175   unsigned dh_next_to_use;
176   unsigned dh_first;
177   unsigned dh_errs_count;
178   char **  dh_errors;
179 };
180 
181 struct Dwarf_Debug_s {
182     /* All file access methods and support data
183        are hidden in this structure.
184        We get a pointer, callers control the lifetime of the
185        structure and contents. */
186     struct Dwarf_Obj_Access_Interface_s *de_obj_file;
187 
188     Dwarf_Handler de_errhand;
189     Dwarf_Ptr de_errarg;
190 
191     /*
192        Context for the compilation_unit just read by a call to
193        dwarf_next_cu_header. **Updated by dwarf_next_cu_header in
194        dwarf_die_deliv.c */
195     Dwarf_CU_Context de_cu_context;
196 
197     /*
198        Points to linked list of CU Contexts for the CU's already read.
199        These are only CU's read by dwarf_next_cu_header(). */
200     Dwarf_CU_Context de_cu_context_list;
201 
202     /*
203        Points to the last CU Context added to the list by
204        dwarf_next_cu_header(). */
205     Dwarf_CU_Context de_cu_context_list_end;
206 
207     /*
208        This is the list of CU contexts read for dwarf_offdie().  These
209        may read ahead of dwarf_next_cu_header(). */
210     Dwarf_CU_Context de_offdie_cu_context;
211     Dwarf_CU_Context de_offdie_cu_context_end;
212 
213     /* Offset of last byte of last CU read. */
214     Dwarf_Word de_info_last_offset;
215 
216     /*
217        Number of bytes in the length, and offset field in various
218        .debug_* sections.  It's not very meaningful, and is
219        only used in one 'approximate' calculation.  */
220     Dwarf_Small de_length_size;
221 
222     /* number of bytes in a pointer of the target in various .debug_
223        sections. 4 in 32bit, 8 in MIPS 64, ia64. */
224     Dwarf_Small de_pointer_size;
225 
226     /* set at creation of a Dwarf_Debug to say if form_string should be
227        checked for valid length at every call. 0 means do the check.
228        non-zero means do not do the check. */
229     Dwarf_Small de_assume_string_in_bounds;
230 
231     /*
232        Dwarf_Alloc_Hdr_s structs used to manage chunks that are
233        malloc'ed for each allocation type for structs. */
234     struct Dwarf_Alloc_Hdr_s de_alloc_hdr[ALLOC_AREA_REAL_TABLE_MAX];
235 #ifdef DWARF_SIMPLE_MALLOC
236     struct simple_malloc_record_s *  de_simple_malloc_base;
237 #endif
238 
239 
240     /*
241        These fields are used to process debug_frame section.  **Updated
242        by dwarf_get_fde_list in dwarf_frame.h */
243     /*
244        Points to contiguous block of pointers to Dwarf_Cie_s structs. */
245     Dwarf_Cie *de_cie_data;
246     /* Count of number of Dwarf_Cie_s structs. */
247     Dwarf_Signed de_cie_count;
248     /* Keep eh (GNU) separate!. */
249     Dwarf_Cie *de_cie_data_eh;
250     Dwarf_Signed de_cie_count_eh;
251     /*
252        Points to contiguous block of pointers to Dwarf_Fde_s structs. */
253     Dwarf_Fde *de_fde_data;
254     /* Count of number of Dwarf_Fde_s structs. */
255     Dwarf_Signed de_fde_count;
256     /* Keep eh (GNU) separate!. */
257     Dwarf_Fde *de_fde_data_eh;
258     Dwarf_Signed de_fde_count_eh;
259 
260     struct Dwarf_Section_s de_debug_info;
261     struct Dwarf_Section_s de_debug_abbrev;
262     struct Dwarf_Section_s de_debug_line;
263     struct Dwarf_Section_s de_debug_loc;
264     struct Dwarf_Section_s de_debug_aranges;
265     struct Dwarf_Section_s de_debug_macinfo;
266     struct Dwarf_Section_s de_debug_pubnames;
267     struct Dwarf_Section_s de_debug_str;
268     struct Dwarf_Section_s de_debug_frame;
269 
270     /* gnu: the g++ eh_frame section */
271     struct Dwarf_Section_s de_debug_frame_eh_gnu;
272 
273     struct Dwarf_Section_s de_debug_pubtypes; /* DWARF3 .debug_pubtypes */
274 
275     struct Dwarf_Section_s de_debug_funcnames;
276     struct Dwarf_Section_s de_debug_typenames; /* SGI IRIX extension essentially
277 			identical to DWARF3 .debug_pubtypes. */
278     struct Dwarf_Section_s de_debug_varnames;
279     struct Dwarf_Section_s de_debug_weaknames;
280     struct Dwarf_Section_s de_debug_ranges;
281 
282     /* For non-elf, simply leave the following two structs zeroed and
283        they will be ignored. */
284     struct Dwarf_Section_s de_elf_symtab;
285     struct Dwarf_Section_s de_elf_strtab;
286 
287 
288     void *(*de_copy_word) (void *, const void *, size_t);
289     unsigned char de_same_endian;
290     unsigned char de_elf_must_close; /* if non-zero, then
291 	it was dwarf_init (not dwarf_elf_init)
292 	so must elf_end() */
293 
294     /* Default is DW_FRAME_INITIAL_VALUE from header. */
295     Dwarf_Half de_frame_rule_initial_value;
296 
297     /* Default is   DW_FRAME_LAST_REG_NUM. */
298     Dwarf_Half de_frame_reg_rules_entry_count;
299 
300     Dwarf_Half de_frame_cfa_col_number;
301     Dwarf_Half de_frame_same_value_number;
302     Dwarf_Half de_frame_undefined_value_number;
303 
304     unsigned char de_big_endian_object; /* non-zero if big-endian
305 		object opened. */
306 
307     struct Dwarf_Harmless_s de_harmless_errors;
308 };
309 
310 typedef struct Dwarf_Chain_s *Dwarf_Chain;
311 struct Dwarf_Chain_s {
312     void *ch_item;
313     Dwarf_Chain ch_next;
314 };
315 
316 
317 #define CURRENT_VERSION_STAMP		2 /* DWARF2 */
318 #define CURRENT_VERSION_STAMP3		3 /* DWARF3 */
319 #define CURRENT_VERSION_STAMP4		4 /* DWARF4 */
320 
321     /* Size of cu header version stamp field. */
322 #define CU_VERSION_STAMP_SIZE   sizeof(Dwarf_Half)
323 
324     /* Size of cu header address size field. */
325 #define CU_ADDRESS_SIZE_SIZE	sizeof(Dwarf_Small)
326 
327 void *_dwarf_memcpy_swap_bytes(void *s1, const void *s2, size_t len);
328 
329 #define ORIGINAL_DWARF_OFFSET_SIZE  4
330 #define DISTINGUISHED_VALUE  0xffffffff
331 #define DISTINGUISHED_VALUE_OFFSET_SIZE 8
332 
333 /*
334     We don't load the sections until they are needed. This function is
335     used to load the section.
336  */
337 int _dwarf_load_section(Dwarf_Debug,
338     struct Dwarf_Section_s *,
339     Dwarf_Error *);
340