xref: /freebsd/contrib/elftoolchain/libdwarf/_libdwarf.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3  * Copyright (c) 2009-2014 Kai Wang
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $Id: _libdwarf.h 3298 2016-01-09 15:43:31Z jkoshy $
28  */
29 
30 #ifndef	__LIBDWARF_H_
31 #define	__LIBDWARF_H_
32 
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <assert.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <gelf.h>
41 #include "dwarf.h"
42 #include "libdwarf.h"
43 #include "uthash.h"
44 
45 #include "_elftc.h"
46 
47 #define DWARF_DIE_HASH_SIZE		8191
48 
49 struct _libdwarf_globals {
50 	Dwarf_Handler	errhand;
51 	Dwarf_Ptr	errarg;
52 	int		applyreloc;
53 };
54 
55 extern struct _libdwarf_globals _libdwarf;
56 
57 #define	_DWARF_SET_ERROR(_d, _e, _err, _elf_err)			\
58 	_dwarf_set_error(_d, _e, _err, _elf_err, __func__, __LINE__)
59 #define	DWARF_SET_ERROR(_d, _e, _err)					\
60 	_DWARF_SET_ERROR(_d, _e, _err, 0)
61 #define	DWARF_SET_ELF_ERROR(_d, _e)					\
62 	_DWARF_SET_ERROR(_d, _e, DW_DLE_ELF, elf_errno())
63 
64 /*
65  * Convenient macros for producer bytes stream generation.
66  */
67 #define	WRITE_VALUE(value, bytes)					\
68 	dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &ds->ds_size,	\
69 	    (value), (bytes), error)
70 #define	WRITE_ULEB128(value)						\
71 	_dwarf_write_uleb128_alloc(&ds->ds_data, &ds->ds_cap,		\
72 	    &ds->ds_size, (value), error)
73 #define	WRITE_SLEB128(value)						\
74 	_dwarf_write_sleb128_alloc(&ds->ds_data, &ds->ds_cap,		\
75 	    &ds->ds_size, (value), error)
76 #define	WRITE_STRING(string)						\
77 	_dwarf_write_string_alloc(&ds->ds_data, &ds->ds_cap,		\
78 	    &ds->ds_size, (string), error)
79 #define	WRITE_BLOCK(blk, size)						\
80 	_dwarf_write_block_alloc(&ds->ds_data, &ds->ds_cap,		\
81 	    &ds->ds_size, (blk), (size), error)
82 #define	WRITE_PADDING(byte, cnt)					\
83 	_dwarf_write_padding_alloc(&ds->ds_data, &ds->ds_cap,		\
84 	    &ds->ds_size, (byte), (cnt), error)
85 #define	RCHECK(expr)							\
86 	do {								\
87 		ret = expr;						\
88 		if (ret != DW_DLE_NONE)					\
89 			goto gen_fail;					\
90 	} while(0)
91 
92 typedef struct _Dwarf_CU *Dwarf_CU;
93 
94 struct _Dwarf_AttrDef {
95 	Dwarf_Half	ad_attrib;		/* DW_AT_XXX */
96 	Dwarf_Half	ad_form;		/* DW_FORM_XXX */
97 	uint64_t	ad_offset;		/* Offset in abbrev section. */
98 	STAILQ_ENTRY(_Dwarf_AttrDef) ad_next;	/* Next attribute define. */
99 };
100 
101 struct _Dwarf_Attribute {
102 	Dwarf_Die		at_die;		/* Ptr to containing DIE. */
103 	Dwarf_Die		at_refdie;	/* Ptr to reference DIE. */
104 	uint64_t		at_offset;	/* Offset in info section. */
105 	Dwarf_Half		at_attrib;	/* DW_AT_XXX */
106 	Dwarf_Half		at_form;	/* DW_FORM_XXX */
107 	int			at_indirect;	/* Has indirect form. */
108 	union {
109 		uint64_t	u64;		/* Unsigned value. */
110 		int64_t		s64;		/* Signed value. */
111 		char		*s;   		/* String. */
112 		uint8_t		*u8p;		/* Block data. */
113 	} u[2];					/* Value. */
114 	Dwarf_Block		at_block;	/* Block. */
115 	Dwarf_Locdesc		*at_ld;		/* at value is locdesc. */
116 	Dwarf_P_Expr		at_expr;	/* at value is expr. */
117 	uint64_t		at_relsym;	/* Relocation symbol index. */
118 	const char		*at_relsec;	/* Rel. to dwarf section. */
119 	STAILQ_ENTRY(_Dwarf_Attribute) at_next;	/* Next attribute. */
120 };
121 
122 struct _Dwarf_Abbrev {
123 	uint64_t	ab_entry;	/* Abbrev entry. */
124 	uint64_t	ab_tag;		/* Tag: DW_TAG_ */
125 	uint8_t		ab_children;	/* DW_CHILDREN_no or DW_CHILDREN_yes */
126 	uint64_t	ab_offset;	/* Offset in abbrev section. */
127 	uint64_t	ab_length;	/* Length of this abbrev entry. */
128 	uint64_t	ab_atnum;	/* Number of attribute defines. */
129 	UT_hash_handle	ab_hh;		/* Uthash handle. */
130 	STAILQ_HEAD(, _Dwarf_AttrDef) ab_attrdef; /* List of attribute defs. */
131 };
132 
133 struct _Dwarf_Die {
134 	Dwarf_Die	die_parent;	/* Parent DIE. */
135 	Dwarf_Die	die_child;	/* First child DIE. */
136 	Dwarf_Die	die_left;	/* Left sibling DIE. */
137 	Dwarf_Die	die_right;	/* Right sibling DIE. */
138 	uint64_t	die_offset;	/* DIE offset in section. */
139 	uint64_t	die_next_off;	/* Next DIE offset in section. */
140 	uint64_t	die_abnum;	/* Abbrev number. */
141 	Dwarf_Abbrev	die_ab;		/* Abbrev pointer. */
142 	Dwarf_Tag	die_tag;	/* DW_TAG_ */
143 	Dwarf_Debug	die_dbg;	/* Dwarf_Debug pointer. */
144 	Dwarf_CU	die_cu;		/* Compilation unit pointer. */
145 	char		*die_name;	/* Ptr to the name string. */
146 	Dwarf_Attribute	*die_attrarray;	/* Array of attributes. */
147 	STAILQ_HEAD(, _Dwarf_Attribute)	die_attr; /* List of attributes. */
148 	STAILQ_ENTRY(_Dwarf_Die) die_pro_next; /* Next die in pro-die list. */
149 };
150 
151 struct _Dwarf_P_Expr_Entry {
152 	Dwarf_Loc	ee_loc;		/* Location expression. */
153 	Dwarf_Unsigned	ee_sym;		/* Optional related reloc sym index. */
154 	STAILQ_ENTRY(_Dwarf_P_Expr_Entry) ee_next; /* Next entry in list. */
155 };
156 
157 struct _Dwarf_P_Expr {
158 	Dwarf_Debug	pe_dbg;		/* Dwarf_Debug pointer. */
159 	uint8_t		*pe_block;	/* Expression block data. */
160 	int		pe_invalid;	/* Block data is up-to-date or not. */
161 	Dwarf_Unsigned	pe_length;	/* Length of the block. */
162 	STAILQ_HEAD(, _Dwarf_P_Expr_Entry) pe_eelist; /* List of entries. */
163 	STAILQ_ENTRY(_Dwarf_P_Expr) pe_next; /* Next expr in list. */
164 };
165 
166 struct _Dwarf_Line {
167 	Dwarf_LineInfo	ln_li;		/* Ptr to line info. */
168 	Dwarf_Addr	ln_addr;	/* Line address. */
169 	Dwarf_Unsigned	ln_symndx;	/* Symbol index for relocation. */
170 	Dwarf_Unsigned	ln_fileno;	/* File number. */
171 	Dwarf_Unsigned	ln_lineno;	/* Line number. */
172 	Dwarf_Signed	ln_column;	/* Column number. */
173 	Dwarf_Bool	ln_bblock;	/* Basic block flag. */
174 	Dwarf_Bool	ln_stmt;	/* Begin statement flag. */
175 	Dwarf_Bool	ln_endseq;	/* End sequence flag. */
176 	STAILQ_ENTRY(_Dwarf_Line) ln_next; /* Next line in list. */
177 };
178 
179 struct _Dwarf_LineFile {
180 	char		*lf_fname;	/* Filename. */
181 	char		*lf_fullpath;	/* Full pathname of the file. */
182 	Dwarf_Unsigned	lf_dirndx;	/* Dir index. */
183 	Dwarf_Unsigned	lf_mtime;	/* Modification time. */
184 	Dwarf_Unsigned	lf_size;	/* File size. */
185 	STAILQ_ENTRY(_Dwarf_LineFile) lf_next; /* Next file in list. */
186 };
187 
188 struct _Dwarf_LineInfo {
189 	Dwarf_Unsigned	li_length;	/* Length of line info data. */
190 	Dwarf_Half	li_version;	/* Version of line info. */
191 	Dwarf_Unsigned	li_hdrlen;	/* Length of line info header. */
192 	Dwarf_Small	li_minlen;	/* Minimum instrutction length. */
193 	Dwarf_Small	li_maxop;	/* Maximum operations per inst. */
194 	Dwarf_Small	li_defstmt;	/* Default value of is_stmt. */
195 	int8_t		li_lbase;    	/* Line base for special opcode. */
196 	Dwarf_Small	li_lrange;    	/* Line range for special opcode. */
197 	Dwarf_Small	li_opbase;	/* Fisrt std opcode number. */
198 	Dwarf_Small	*li_oplen;	/* Array of std opcode len. */
199 	char		**li_incdirs;	/* Array of include dirs. */
200 	Dwarf_Unsigned	li_inclen;	/* Length of inc dir array. */
201 	char		**li_lfnarray;	/* Array of file names. */
202 	Dwarf_Unsigned	li_lflen;	/* Length of filename array. */
203 	STAILQ_HEAD(, _Dwarf_LineFile) li_lflist; /* List of files. */
204 	Dwarf_Line	*li_lnarray;	/* Array of lines. */
205 	Dwarf_Unsigned	li_lnlen;	/* Length of the line array. */
206 	STAILQ_HEAD(, _Dwarf_Line) li_lnlist; /* List of lines. */
207 };
208 
209 struct _Dwarf_NamePair {
210 	Dwarf_NameTbl	np_nt;		/* Ptr to containing name table. */
211 	Dwarf_Die	np_die;		/* Ptr to Ref. Die. */
212 	Dwarf_Unsigned	np_offset;	/* Offset in CU. */
213 	char		*np_name;	/* Object/Type name. */
214 	STAILQ_ENTRY(_Dwarf_NamePair) np_next; /* Next pair in the list. */
215 };
216 
217 struct _Dwarf_NameTbl {
218 	Dwarf_Unsigned	nt_length;	/* Name lookup table length. */
219 	Dwarf_Half	nt_version;	/* Name lookup table version. */
220 	Dwarf_CU	nt_cu;		/* Ptr to Ref. CU. */
221 	Dwarf_Off	nt_cu_offset;	/* Ref. CU offset in .debug_info */
222 	Dwarf_Unsigned	nt_cu_length;	/* Ref. CU length. */
223 	STAILQ_HEAD(, _Dwarf_NamePair) nt_nplist; /* List of offset+name pairs. */
224 	STAILQ_ENTRY(_Dwarf_NameTbl) nt_next; /* Next name table in the list. */
225 };
226 
227 struct _Dwarf_NameSec {
228 	STAILQ_HEAD(, _Dwarf_NameTbl) ns_ntlist; /* List of name tables. */
229 	Dwarf_NamePair	*ns_array;	/* Array of pairs of all tables. */
230 	Dwarf_Unsigned	ns_len;		/* Length of the pair array. */
231 };
232 
233 struct _Dwarf_Fde {
234 	Dwarf_Debug	fde_dbg;	/* Ptr to containing dbg. */
235 	Dwarf_Cie	fde_cie;	/* Ptr to associated CIE. */
236 	Dwarf_FrameSec	fde_fs;		/* Ptr to containing .debug_frame. */
237 	Dwarf_Ptr	fde_addr;	/* Ptr to start of the FDE. */
238 	Dwarf_Unsigned	fde_offset;	/* Offset of the FDE. */
239 	Dwarf_Unsigned	fde_length;	/* Length of the FDE. */
240 	Dwarf_Unsigned	fde_cieoff;	/* Offset of associated CIE. */
241 	Dwarf_Unsigned	fde_initloc;	/* Initial location. */
242 	Dwarf_Unsigned	fde_adrange;	/* Address range. */
243 	Dwarf_Unsigned	fde_auglen;	/* Augmentation length. */
244 	uint8_t		*fde_augdata;	/* Augmentation data. */
245 	uint8_t		*fde_inst;	/* Instructions. */
246 	Dwarf_Unsigned	fde_instlen;	/* Length of instructions. */
247 	Dwarf_Unsigned	fde_instcap;	/* Capacity of inst buffer. */
248 	Dwarf_Unsigned	fde_symndx;	/* Symbol index for relocation. */
249 	Dwarf_Unsigned	fde_esymndx;	/* End symbol index for relocation. */
250 	Dwarf_Addr	fde_eoff;	/* Offset from the end symbol. */
251 	STAILQ_ENTRY(_Dwarf_Fde) fde_next; /* Next FDE in list. */
252 };
253 
254 struct _Dwarf_Cie {
255 	Dwarf_Debug	cie_dbg;	/* Ptr to containing dbg. */
256 	Dwarf_Unsigned	cie_index;	/* Index of the CIE. */
257 	Dwarf_Unsigned	cie_offset;	/* Offset of the CIE. */
258 	Dwarf_Unsigned	cie_length;	/* Length of the CIE. */
259 	Dwarf_Half	cie_version;	/* CIE version. */
260 	uint8_t		*cie_augment;	/* CIE augmentation (UTF-8). */
261 	Dwarf_Unsigned	cie_ehdata;	/* Optional EH Data. */
262 	uint8_t		cie_addrsize;	/* Address size. (DWARF4) */
263 	uint8_t		cie_segmentsize; /* Segment size. (DWARF4) */
264 	Dwarf_Unsigned	cie_caf;	/* Code alignment factor. */
265 	Dwarf_Signed	cie_daf;	/* Data alignment factor. */
266 	Dwarf_Unsigned	cie_ra;		/* Return address register. */
267 	Dwarf_Unsigned	cie_auglen;	/* Augmentation length. */
268 	uint8_t		*cie_augdata;	/* Augmentation data; */
269 	uint8_t		cie_fde_encode; /* FDE PC start/range encode. */
270 	Dwarf_Ptr	cie_initinst;	/* Initial instructions. */
271 	Dwarf_Unsigned	cie_instlen;	/* Length of init instructions. */
272 	STAILQ_ENTRY(_Dwarf_Cie) cie_next;  /* Next CIE in list. */
273 };
274 
275 struct _Dwarf_FrameSec {
276 	STAILQ_HEAD(, _Dwarf_Cie) fs_cielist; /* List of CIE. */
277 	STAILQ_HEAD(, _Dwarf_Fde) fs_fdelist; /* List of FDE. */
278 	Dwarf_Cie	*fs_ciearray;	/* Array of CIE. */
279 	Dwarf_Unsigned	fs_cielen;	/* Length of CIE array. */
280 	Dwarf_Fde	*fs_fdearray;	/* Array of FDE.*/
281 	Dwarf_Unsigned	fs_fdelen;	/* Length of FDE array. */
282 };
283 
284 struct _Dwarf_Arange {
285 	Dwarf_ArangeSet	ar_as;		/* Ptr to the set it belongs to. */
286 	Dwarf_Unsigned	ar_address;	/* Start PC. */
287 	Dwarf_Unsigned	ar_range;	/* PC range. */
288 	Dwarf_Unsigned	ar_symndx;	/* First symbol index for reloc. */
289 	Dwarf_Unsigned	ar_esymndx;	/* Second symbol index for reloc. */
290 	Dwarf_Addr	ar_eoff;	/* Offset from second symbol. */
291 	STAILQ_ENTRY(_Dwarf_Arange) ar_next; /* Next arange in list. */
292 };
293 
294 struct _Dwarf_ArangeSet {
295 	Dwarf_Unsigned	as_length;	/* Length of the arange set. */
296 	Dwarf_Half	as_version;	/* Version of the arange set. */
297 	Dwarf_Off	as_cu_offset;	/* Offset of associated CU. */
298 	Dwarf_CU	as_cu;		/* Ptr to associated CU. */
299 	Dwarf_Small	as_addrsz;	/* Target address size. */
300 	Dwarf_Small	as_segsz;	/* Target segment size. */
301 	STAILQ_HEAD (, _Dwarf_Arange) as_arlist; /* List of ae entries. */
302 	STAILQ_ENTRY(_Dwarf_ArangeSet) as_next; /* Next set in list. */
303 };
304 
305 struct _Dwarf_MacroSet {
306 	Dwarf_Macro_Details *ms_mdlist; /* Array of macinfo entries. */
307 	Dwarf_Unsigned	ms_cnt;		/* Length of the array. */
308 	STAILQ_ENTRY(_Dwarf_MacroSet) ms_next; /* Next set in list. */
309 };
310 
311 struct _Dwarf_Rangelist {
312 	Dwarf_CU	rl_cu;		/* Ptr to associated CU. */
313 	Dwarf_Unsigned	rl_offset;	/* Offset of the rangelist. */
314 	Dwarf_Ranges	*rl_rgarray;	/* Array of ranges. */
315 	Dwarf_Unsigned	rl_rglen;	/* Length of the ranges array. */
316 	STAILQ_ENTRY(_Dwarf_Rangelist) rl_next; /* Next rangelist in list. */
317 };
318 
319 struct _Dwarf_CU {
320 	Dwarf_Debug	cu_dbg;		/* Ptr to containing dbg. */
321 	Dwarf_Off	cu_offset;	/* Offset to the this CU. */
322 	uint32_t	cu_length;	/* Length of CU data. */
323 	uint16_t	cu_length_size; /* Size in bytes of the length field. */
324 	uint16_t	cu_version;	/* DWARF version. */
325 	uint64_t	cu_abbrev_offset; /* Offset into .debug_abbrev. */
326 	uint64_t	cu_abbrev_offset_cur; /* Current abbrev offset. */
327 	int		cu_abbrev_loaded; /* Abbrev table parsed. */
328 	uint64_t	cu_abbrev_cnt;	/* Abbrev entry count. */
329 	uint64_t	cu_lineno_offset; /* Offset into .debug_lineno. */
330 	uint8_t		cu_pointer_size;/* Number of bytes in pointer. */
331 	uint8_t		cu_dwarf_size;	/* CU section dwarf size. */
332 	Dwarf_Sig8	cu_type_sig;	/* Type unit's signature. */
333 	uint64_t	cu_type_offset; /* Type unit's type offset. */
334 	Dwarf_Off	cu_next_offset; /* Offset to the next CU. */
335 	uint64_t	cu_1st_offset;	/* First DIE offset. */
336 	int		cu_pass2;	/* Two pass DIE traverse. */
337 	Dwarf_LineInfo	cu_lineinfo;	/* Ptr to Dwarf_LineInfo. */
338 	Dwarf_Abbrev	cu_abbrev_hash; /* Abbrev hash table. */
339 	Dwarf_Bool	cu_is_info;	/* Compilation/type unit flag. */
340 	STAILQ_ENTRY(_Dwarf_CU) cu_next; /* Next compilation unit. */
341 };
342 
343 typedef struct _Dwarf_Section {
344 	const char	*ds_name;	/* Section name. */
345 	Dwarf_Small	*ds_data;	/* Section data. */
346 	Dwarf_Unsigned	ds_addr;	/* Section virtual addr. */
347 	Dwarf_Unsigned	ds_size;	/* Section size. */
348 } Dwarf_Section;
349 
350 typedef struct _Dwarf_P_Section {
351 	char		*ds_name;	/* Section name. */
352 	Dwarf_Small	*ds_data;	/* Section data. */
353 	Dwarf_Unsigned	ds_size;	/* Section size. */
354 	Dwarf_Unsigned	ds_cap;		/* Section capacity. */
355 	Dwarf_Unsigned	ds_ndx;		/* ELF section index. */
356 	Dwarf_Unsigned	ds_symndx;	/* Section symbol index. (for reloc) */
357 	STAILQ_ENTRY(_Dwarf_P_Section) ds_next; /* Next section in the list. */
358 } *Dwarf_P_Section;
359 
360 typedef struct _Dwarf_Rel_Entry {
361 	unsigned char	dre_type;	/* Reloc type. */
362 	unsigned char	dre_length;	/* Reloc storage unit length. */
363 	Dwarf_Unsigned	dre_offset;	/* Reloc storage unit offset. */
364 	Dwarf_Unsigned	dre_addend;	/* Reloc addend. */
365 	Dwarf_Unsigned	dre_symndx;	/* Reloc symbol index. */
366 	const char	*dre_secname;	/* Refer to some debug section. */
367 	STAILQ_ENTRY(_Dwarf_Rel_Entry) dre_next; /* Next reloc entry. */
368 } *Dwarf_Rel_Entry;
369 
370 typedef struct _Dwarf_Rel_Section {
371 	struct _Dwarf_P_Section *drs_ds; /* Ptr to actual reloc ELF section. */
372 	struct _Dwarf_P_Section *drs_ref; /* Which debug section it refers. */
373 	struct Dwarf_Relocation_Data_s *drs_drd; /* Reloc data array. */
374 	STAILQ_HEAD(, _Dwarf_Rel_Entry) drs_dre; /* Reloc entry list. */
375 	Dwarf_Unsigned	drs_drecnt;	/* Count of entries. */
376 	Dwarf_Unsigned	drs_size;	/* Size of ELF section in bytes. */
377 	int		drs_addend;	/* Elf_Rel or Elf_Rela */
378 	STAILQ_ENTRY(_Dwarf_Rel_Section) drs_next; /* Next reloc section. */
379 } *Dwarf_Rel_Section;
380 
381 typedef struct {
382 	Elf_Data *ed_data;
383 	void *ed_alloc;
384 } Dwarf_Elf_Data;
385 
386 typedef struct {
387 	Elf		*eo_elf;
388 	GElf_Ehdr	eo_ehdr;
389 	GElf_Shdr	*eo_shdr;
390 	Dwarf_Elf_Data	*eo_data;
391 	Dwarf_Unsigned	eo_seccnt;
392 	size_t		eo_strndx;
393 	Dwarf_Obj_Access_Methods eo_methods;
394 } Dwarf_Elf_Object;
395 
396 struct _Dwarf_Debug {
397 	Dwarf_Obj_Access_Interface *dbg_iface;
398 	Dwarf_Section	*dbg_section;	/* Dwarf section list. */
399 	Dwarf_Section	*dbg_info_sec;	/* Pointer to info section. */
400 	Dwarf_Off	dbg_info_off;	/* Current info section offset. */
401 	Dwarf_Section	*dbg_types_sec; /* Pointer to type section. */
402 	Dwarf_Off	dbg_types_off;	/* Current types section offset. */
403 	Dwarf_Unsigned	dbg_seccnt;	/* Total number of dwarf sections. */
404 	int		dbg_mode;	/* Access mode. */
405 	int		dbg_pointer_size; /* Object address size. */
406 	int		dbg_offset_size;  /* DWARF offset size. */
407 	int		dbg_info_loaded; /* Flag indicating all CU loaded. */
408 	int		dbg_types_loaded; /* Flag indicating all TU loaded. */
409 	Dwarf_Half	dbg_machine;	/* ELF machine architecture. */
410 	Dwarf_Handler	dbg_errhand;	/* Error handler. */
411 	Dwarf_Ptr	dbg_errarg;	/* Argument to the error handler. */
412 	STAILQ_HEAD(, _Dwarf_CU) dbg_cu;/* List of compilation units. */
413 	STAILQ_HEAD(, _Dwarf_CU) dbg_tu;/* List of type units. */
414 	Dwarf_CU	dbg_cu_current; /* Ptr to the current CU. */
415 	Dwarf_CU	dbg_tu_current; /* Ptr to the current TU. */
416 	Dwarf_NameSec	dbg_globals;	/* Ptr to pubnames lookup section. */
417 	Dwarf_NameSec	dbg_pubtypes;	/* Ptr to pubtypes lookup section. */
418 	Dwarf_NameSec	dbg_weaks;	/* Ptr to weaknames lookup section. */
419 	Dwarf_NameSec	dbg_funcs;	/* Ptr to static funcs lookup sect. */
420 	Dwarf_NameSec	dbg_vars;	/* Ptr to static vars lookup sect. */
421 	Dwarf_NameSec	dbg_types;	/* Ptr to types lookup section. */
422 	Dwarf_FrameSec	dbg_frame;	/* Ptr to .debug_frame section. */
423 	Dwarf_FrameSec	dbg_eh_frame;	/* Ptr to .eh_frame section. */
424 	STAILQ_HEAD(, _Dwarf_ArangeSet) dbg_aslist; /* List of arange set. */
425 	Dwarf_Arange	*dbg_arange_array; /* Array of arange. */
426 	Dwarf_Unsigned	dbg_arange_cnt;	/* Length of the arange array. */
427 	char		*dbg_strtab;	/* Dwarf string table. */
428 	Dwarf_Unsigned	dbg_strtab_cap; /* Dwarf string table capacity. */
429 	Dwarf_Unsigned	dbg_strtab_size; /* Dwarf string table size. */
430 	STAILQ_HEAD(, _Dwarf_MacroSet) dbg_mslist; /* List of macro set. */
431 	STAILQ_HEAD(, _Dwarf_Rangelist) dbg_rllist; /* List of rangelist. */
432 	uint64_t	(*read)(uint8_t *, uint64_t *, int);
433 	void		(*write)(uint8_t *, uint64_t *, uint64_t, int);
434 	int		(*write_alloc)(uint8_t **, uint64_t *, uint64_t *,
435 			    uint64_t, int, Dwarf_Error *);
436 	uint64_t	(*decode)(uint8_t **, int);
437 
438 	Dwarf_Half	dbg_frame_rule_table_size;
439 	Dwarf_Half	dbg_frame_rule_initial_value;
440 	Dwarf_Half	dbg_frame_cfa_value;
441 	Dwarf_Half	dbg_frame_same_value;
442 	Dwarf_Half	dbg_frame_undefined_value;
443 
444 	Dwarf_Regtable3	*dbg_internal_reg_table;
445 
446 	/*
447 	 * Fields used by libdwarf producer.
448 	 */
449 
450 	Dwarf_Unsigned	dbgp_flags;
451 	Dwarf_Unsigned	dbgp_isa;
452 	Dwarf_Callback_Func dbgp_func;
453 	Dwarf_Callback_Func_b dbgp_func_b;
454 	Dwarf_Die	dbgp_root_die;
455 	STAILQ_HEAD(, _Dwarf_Die) dbgp_dielist;
456 	STAILQ_HEAD(, _Dwarf_P_Expr) dbgp_pelist;
457 	Dwarf_LineInfo	dbgp_lineinfo;
458 	Dwarf_ArangeSet dbgp_as;
459 	Dwarf_Macro_Details *dbgp_mdlist;
460 	Dwarf_Unsigned	dbgp_mdcnt;
461 	STAILQ_HEAD(, _Dwarf_Cie) dbgp_cielist;
462 	STAILQ_HEAD(, _Dwarf_Fde) dbgp_fdelist;
463 	Dwarf_Unsigned	dbgp_cielen;
464 	Dwarf_Unsigned	dbgp_fdelen;
465 	Dwarf_NameTbl	dbgp_pubs;
466 	Dwarf_NameTbl	dbgp_weaks;
467 	Dwarf_NameTbl	dbgp_funcs;
468 	Dwarf_NameTbl	dbgp_types;
469 	Dwarf_NameTbl	dbgp_vars;
470 	STAILQ_HEAD(, _Dwarf_P_Section) dbgp_seclist;
471 	Dwarf_Unsigned	dbgp_seccnt;
472 	Dwarf_P_Section	dbgp_secpos;
473 	Dwarf_P_Section	dbgp_info;
474 	STAILQ_HEAD(, _Dwarf_Rel_Section) dbgp_drslist;
475 	Dwarf_Unsigned	dbgp_drscnt;
476 	Dwarf_Rel_Section dbgp_drspos;
477 };
478 
479 /*
480  * Internal function prototypes.
481  */
482 
483 int		_dwarf_abbrev_add(Dwarf_CU, uint64_t, uint64_t, uint8_t,
484 		    uint64_t, Dwarf_Abbrev *, Dwarf_Error *);
485 void		_dwarf_abbrev_cleanup(Dwarf_CU);
486 int		_dwarf_abbrev_find(Dwarf_CU, uint64_t, Dwarf_Abbrev *,
487 		    Dwarf_Error *);
488 int		_dwarf_abbrev_gen(Dwarf_P_Debug, Dwarf_Error *);
489 int		_dwarf_abbrev_parse(Dwarf_Debug, Dwarf_CU, Dwarf_Unsigned *,
490 		    Dwarf_Abbrev *, Dwarf_Error *);
491 int		_dwarf_add_AT_dataref(Dwarf_P_Debug, Dwarf_P_Die, Dwarf_Half,
492 		    Dwarf_Unsigned, Dwarf_Unsigned, const char *,
493 		    Dwarf_P_Attribute *, Dwarf_Error *);
494 int		_dwarf_add_string_attr(Dwarf_P_Die, Dwarf_P_Attribute *,
495 		    Dwarf_Half, char *, Dwarf_Error *);
496 int		_dwarf_alloc(Dwarf_Debug *, int, Dwarf_Error *);
497 void		_dwarf_arange_cleanup(Dwarf_Debug);
498 int		_dwarf_arange_gen(Dwarf_P_Debug, Dwarf_Error *);
499 int		_dwarf_arange_init(Dwarf_Debug, Dwarf_Error *);
500 void		_dwarf_arange_pro_cleanup(Dwarf_P_Debug);
501 int		_dwarf_attr_alloc(Dwarf_Die, Dwarf_Attribute *, Dwarf_Error *);
502 Dwarf_Attribute	_dwarf_attr_find(Dwarf_Die, Dwarf_Half);
503 int		_dwarf_attr_gen(Dwarf_P_Debug, Dwarf_P_Section, Dwarf_Rel_Section,
504 		    Dwarf_CU, Dwarf_Die, int, Dwarf_Error *);
505 int		_dwarf_attr_init(Dwarf_Debug, Dwarf_Section *, uint64_t *, int,
506 		    Dwarf_CU, Dwarf_Die, Dwarf_AttrDef, uint64_t, int,
507 		    Dwarf_Error *);
508 int		_dwarf_attrdef_add(Dwarf_Debug, Dwarf_Abbrev, uint64_t,
509 		    uint64_t, uint64_t, Dwarf_AttrDef *, Dwarf_Error *);
510 uint64_t	_dwarf_decode_lsb(uint8_t **, int);
511 uint64_t	_dwarf_decode_msb(uint8_t **, int);
512 int64_t		_dwarf_decode_sleb128(uint8_t **);
513 uint64_t	_dwarf_decode_uleb128(uint8_t **);
514 void		_dwarf_deinit(Dwarf_Debug);
515 int		_dwarf_die_alloc(Dwarf_Debug, Dwarf_Die *, Dwarf_Error *);
516 int		_dwarf_die_count_links(Dwarf_P_Die, Dwarf_P_Die,
517 		    Dwarf_P_Die, Dwarf_P_Die);
518 Dwarf_Die	_dwarf_die_find(Dwarf_Die, Dwarf_Unsigned);
519 int		_dwarf_die_gen(Dwarf_P_Debug, Dwarf_CU, Dwarf_Rel_Section,
520 		    Dwarf_Error *);
521 void		_dwarf_die_link(Dwarf_P_Die, Dwarf_P_Die, Dwarf_P_Die,
522 		    Dwarf_P_Die, Dwarf_P_Die);
523 int		_dwarf_die_parse(Dwarf_Debug, Dwarf_Section *, Dwarf_CU, int,
524 		    uint64_t, uint64_t, Dwarf_Die *, int, Dwarf_Error *);
525 void		_dwarf_die_pro_cleanup(Dwarf_P_Debug);
526 void		_dwarf_elf_deinit(Dwarf_Debug);
527 int		_dwarf_elf_init(Dwarf_Debug, Elf *, Dwarf_Error *);
528 int		_dwarf_elf_load_section(void *, Dwarf_Half, Dwarf_Small **,
529 		    int *);
530 Dwarf_Endianness _dwarf_elf_get_byte_order(void *);
531 Dwarf_Small	_dwarf_elf_get_length_size(void *);
532 Dwarf_Small	_dwarf_elf_get_pointer_size(void *);
533 Dwarf_Unsigned	_dwarf_elf_get_section_count(void *);
534 int		_dwarf_elf_get_section_info(void *, Dwarf_Half,
535 		    Dwarf_Obj_Access_Section *, int *);
536 void		_dwarf_expr_cleanup(Dwarf_P_Debug);
537 int		_dwarf_expr_into_block(Dwarf_P_Expr, Dwarf_Error *);
538 Dwarf_Section	*_dwarf_find_next_types_section(Dwarf_Debug, Dwarf_Section *);
539 Dwarf_Section	*_dwarf_find_section(Dwarf_Debug, const char *);
540 void		_dwarf_frame_cleanup(Dwarf_Debug);
541 int		_dwarf_frame_fde_add_inst(Dwarf_P_Fde, Dwarf_Small,
542 		    Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Error *);
543 int		_dwarf_frame_gen(Dwarf_P_Debug, Dwarf_Error *);
544 int		_dwarf_frame_get_fop(Dwarf_Debug, uint8_t, uint8_t *,
545 		    Dwarf_Unsigned, Dwarf_Frame_Op **, Dwarf_Signed *,
546 		    Dwarf_Error *);
547 int		_dwarf_frame_get_internal_table(Dwarf_Fde, Dwarf_Addr,
548 		    Dwarf_Regtable3 **, Dwarf_Addr *, Dwarf_Error *);
549 int		_dwarf_frame_interal_table_init(Dwarf_Debug, Dwarf_Error *);
550 void		_dwarf_frame_params_init(Dwarf_Debug);
551 void		_dwarf_frame_pro_cleanup(Dwarf_P_Debug);
552 int		_dwarf_frame_regtable_copy(Dwarf_Debug, Dwarf_Regtable3 **,
553 		    Dwarf_Regtable3 *, Dwarf_Error *);
554 int		_dwarf_frame_section_load(Dwarf_Debug, Dwarf_Error *);
555 int		_dwarf_frame_section_load_eh(Dwarf_Debug, Dwarf_Error *);
556 int		_dwarf_generate_sections(Dwarf_P_Debug, Dwarf_Error *);
557 Dwarf_Unsigned	_dwarf_get_reloc_type(Dwarf_P_Debug, int);
558 int		_dwarf_get_reloc_size(Dwarf_Debug, Dwarf_Unsigned);
559 void		_dwarf_info_cleanup(Dwarf_Debug);
560 int		_dwarf_info_first_cu(Dwarf_Debug, Dwarf_Error *);
561 int		_dwarf_info_first_tu(Dwarf_Debug, Dwarf_Error *);
562 int		_dwarf_info_gen(Dwarf_P_Debug, Dwarf_Error *);
563 int		_dwarf_info_load(Dwarf_Debug, Dwarf_Bool, Dwarf_Bool,
564 		    Dwarf_Error *);
565 int		_dwarf_info_next_cu(Dwarf_Debug, Dwarf_Error *);
566 int		_dwarf_info_next_tu(Dwarf_Debug, Dwarf_Error *);
567 void		_dwarf_info_pro_cleanup(Dwarf_P_Debug);
568 int		_dwarf_init(Dwarf_Debug, Dwarf_Unsigned, Dwarf_Handler,
569 		    Dwarf_Ptr, Dwarf_Error *);
570 int		_dwarf_lineno_gen(Dwarf_P_Debug, Dwarf_Error *);
571 int		_dwarf_lineno_init(Dwarf_Die, uint64_t, Dwarf_Error *);
572 void		_dwarf_lineno_cleanup(Dwarf_LineInfo);
573 void		_dwarf_lineno_pro_cleanup(Dwarf_P_Debug);
574 int		_dwarf_loc_fill_locdesc(Dwarf_Debug, Dwarf_Locdesc *,
575 		    uint8_t *, uint64_t, uint8_t, uint8_t, uint8_t,
576 		    Dwarf_Error *);
577 int		_dwarf_loc_fill_locexpr(Dwarf_Debug, Dwarf_Locdesc **,
578 		    uint8_t *, uint64_t, uint8_t, uint8_t, uint8_t,
579 		    Dwarf_Error *);
580 int		_dwarf_loc_add(Dwarf_Die, Dwarf_Attribute, Dwarf_Error *);
581 int		_dwarf_loc_expr_add_atom(Dwarf_Debug, uint8_t *, uint8_t *,
582 		    Dwarf_Small, Dwarf_Unsigned, Dwarf_Unsigned, int *,
583 		    Dwarf_Error *);
584 int		_dwarf_loclist_find(Dwarf_Debug, Dwarf_CU, uint64_t,
585 		    Dwarf_Locdesc ***, Dwarf_Signed *, Dwarf_Unsigned *,
586 		    Dwarf_Error *);
587 void		_dwarf_macinfo_cleanup(Dwarf_Debug);
588 int		_dwarf_macinfo_gen(Dwarf_P_Debug, Dwarf_Error *);
589 int		_dwarf_macinfo_init(Dwarf_Debug, Dwarf_Error *);
590 void		_dwarf_macinfo_pro_cleanup(Dwarf_P_Debug);
591 int		_dwarf_nametbl_init(Dwarf_Debug, Dwarf_NameSec *,
592 		    Dwarf_Section *, Dwarf_Error *);
593 void		_dwarf_nametbl_cleanup(Dwarf_NameSec *);
594 int		_dwarf_nametbl_gen(Dwarf_P_Debug, const char *, Dwarf_NameTbl,
595 		    Dwarf_Error *);
596 void		_dwarf_nametbl_pro_cleanup(Dwarf_NameTbl *);
597 int		_dwarf_pro_callback(Dwarf_P_Debug, char *, int, Dwarf_Unsigned,
598 		    Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Unsigned,
599 		    Dwarf_Unsigned *, int *);
600 Dwarf_P_Section	_dwarf_pro_find_section(Dwarf_P_Debug, const char *);
601 int		_dwarf_ranges_add(Dwarf_Debug, Dwarf_CU, uint64_t,
602 		    Dwarf_Rangelist *, Dwarf_Error *);
603 void		_dwarf_ranges_cleanup(Dwarf_Debug);
604 int		_dwarf_ranges_find(Dwarf_Debug, uint64_t, Dwarf_Rangelist *);
605 uint64_t	_dwarf_read_lsb(uint8_t *, uint64_t *, int);
606 uint64_t	_dwarf_read_msb(uint8_t *, uint64_t *, int);
607 int64_t		_dwarf_read_sleb128(uint8_t *, uint64_t *);
608 uint64_t	_dwarf_read_uleb128(uint8_t *, uint64_t *);
609 char		*_dwarf_read_string(void *, Dwarf_Unsigned, uint64_t *);
610 uint8_t		*_dwarf_read_block(void *, uint64_t *, uint64_t);
611 int		_dwarf_reloc_section_finalize(Dwarf_P_Debug, Dwarf_Rel_Section,
612 		    Dwarf_Error *);
613 int		_dwarf_reloc_entry_add(Dwarf_P_Debug, Dwarf_Rel_Section,
614 		    Dwarf_P_Section, unsigned char, unsigned char,
615 		    Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Unsigned,
616 		    const char *, Dwarf_Error *);
617 int		_dwarf_reloc_entry_add_pair(Dwarf_P_Debug, Dwarf_Rel_Section,
618 		    Dwarf_P_Section, unsigned char, Dwarf_Unsigned,
619 		    Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Unsigned,
620 		    Dwarf_Unsigned, Dwarf_Error *);
621 void		_dwarf_reloc_cleanup(Dwarf_P_Debug);
622 int		_dwarf_reloc_gen(Dwarf_P_Debug, Dwarf_Error *);
623 int		_dwarf_reloc_section_gen(Dwarf_P_Debug, Dwarf_Rel_Section,
624 		    Dwarf_Error *);
625 int		_dwarf_reloc_section_init(Dwarf_P_Debug, Dwarf_Rel_Section *,
626 		    Dwarf_P_Section, Dwarf_Error *);
627 void		_dwarf_reloc_section_free(Dwarf_P_Debug, Dwarf_Rel_Section *);
628 void		_dwarf_section_cleanup(Dwarf_P_Debug);
629 int		_dwarf_section_callback(Dwarf_P_Debug, Dwarf_P_Section,
630 		    Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Unsigned,
631 		    Dwarf_Unsigned, Dwarf_Error *);
632 void		_dwarf_section_free(Dwarf_P_Debug, Dwarf_P_Section *);
633 int		_dwarf_section_init(Dwarf_P_Debug, Dwarf_P_Section *,
634 		    const char *, int, Dwarf_Error *);
635 void		_dwarf_set_error(Dwarf_Debug, Dwarf_Error *, int, int,
636 		    const char *, int);
637 int		_dwarf_strtab_add(Dwarf_Debug, char *, uint64_t *,
638 		    Dwarf_Error *);
639 void		_dwarf_strtab_cleanup(Dwarf_Debug);
640 int		_dwarf_strtab_gen(Dwarf_P_Debug, Dwarf_Error *);
641 char		*_dwarf_strtab_get_table(Dwarf_Debug);
642 int		_dwarf_strtab_init(Dwarf_Debug, Dwarf_Error *);
643 void		_dwarf_type_unit_cleanup(Dwarf_Debug);
644 void		_dwarf_write_block(void *, uint64_t *, uint8_t *, uint64_t);
645 int		_dwarf_write_block_alloc(uint8_t **, uint64_t *, uint64_t *,
646 		    uint8_t *, uint64_t, Dwarf_Error *);
647 void		_dwarf_write_lsb(uint8_t *, uint64_t *, uint64_t, int);
648 int		_dwarf_write_lsb_alloc(uint8_t **, uint64_t *, uint64_t *,
649 		    uint64_t, int, Dwarf_Error *);
650 void		_dwarf_write_msb(uint8_t *, uint64_t *, uint64_t, int);
651 int		_dwarf_write_msb_alloc(uint8_t **, uint64_t *, uint64_t *,
652 		    uint64_t, int, Dwarf_Error *);
653 void		_dwarf_write_padding(void *, uint64_t *, uint8_t, uint64_t);
654 int		_dwarf_write_padding_alloc(uint8_t **, uint64_t *, uint64_t *,
655 		    uint8_t, uint64_t, Dwarf_Error *);
656 void		_dwarf_write_string(void *, uint64_t *, char *);
657 int		_dwarf_write_string_alloc(uint8_t **, uint64_t *, uint64_t *,
658 		    char *, Dwarf_Error *);
659 int		_dwarf_write_sleb128(uint8_t *, uint8_t *, int64_t);
660 int		_dwarf_write_sleb128_alloc(uint8_t **, uint64_t *, uint64_t *,
661 		    int64_t, Dwarf_Error *);
662 int		_dwarf_write_uleb128(uint8_t *, uint8_t *, uint64_t);
663 int		_dwarf_write_uleb128_alloc(uint8_t **, uint64_t *, uint64_t *,
664 		    uint64_t, Dwarf_Error *);
665 
666 #endif /* !__LIBDWARF_H_ */
667