1/*
2
3  Copyright (C) 2000,2002,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., 1500 Crittenden Lane,
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#include <stddef.h>
38
39/*
40    Sgidefs included to define __uint32_t,
41    a guaranteed 4-byte quantity.
42*/
43#include "libdwarfdefs.h"
44
45#define true                    1
46#define false                   0
47
48/* to identify a cie */
49#define DW_CIE_ID 		~(0x0)
50#define DW_CIE_VERSION		1
51
52/*Dwarf_Word  is unsigned word usable for index, count in memory */
53/*Dwarf_Sword is   signed word usable for index, count in memory */
54/* The are 32 or 64 bits depending if 64 bit longs or not, which
55** fits the  ILP32 and LP64 models
56** These work equally well with ILP64.
57*/
58
59typedef unsigned long Dwarf_Word;
60typedef long Dwarf_Sword;
61
62
63typedef signed char Dwarf_Sbyte;
64typedef unsigned char Dwarf_Ubyte;
65typedef signed short Dwarf_Shalf;
66
67/*
68	On any change that makes libdwarf producer
69	incompatible, increment this number.
70	1->2->3 ...
71
72*/
73#define  PRO_VERSION_MAGIC 0xdead1
74
75
76/* these 2 are fixed sizes which must not vary with the
77** ILP32/LP64 model. These two stay at 32 bit.
78*/
79typedef __uint32_t Dwarf_ufixed;
80typedef __int32_t Dwarf_sfixed;
81
82/*
83	producer:
84	This struct is used to hold information about all
85	debug* sections. On creating a new section, section
86	names and indices are added to this struct
87	definition in pro_section.h
88*/
89typedef struct Dwarf_P_Section_Data_s *Dwarf_P_Section_Data;
90
91/*
92	producer:
93	This struct is used to hold entries in the include directories
94	part of statement prologue. Definition in pro_line.h
95*/
96typedef struct Dwarf_P_Inc_Dir_s *Dwarf_P_Inc_Dir;
97
98/*
99	producer:
100	This struct holds file entries for the statement prologue.
101	Defined in pro_line.h
102*/
103typedef struct Dwarf_P_F_Entry_s *Dwarf_P_F_Entry;
104
105/*
106	producer:
107	This struct holds information for each cie. Defn in pro_frame.h
108*/
109typedef struct Dwarf_P_Cie_s *Dwarf_P_Cie;
110
111/*
112	producer:
113	Struct to hold line number information, different from
114	Dwarf_Line opaque type.
115*/
116typedef struct Dwarf_P_Line_s *Dwarf_P_Line;
117
118/*
119	producer:
120	Struct to hold information about address ranges.
121*/
122typedef struct Dwarf_P_Simple_nameentry_s *Dwarf_P_Simple_nameentry;
123typedef struct Dwarf_P_Simple_name_header_s *Dwarf_P_Simple_name_header;
124typedef struct Dwarf_P_Arange_s *Dwarf_P_Arange;
125typedef struct Dwarf_P_Per_Reloc_Sect_s *Dwarf_P_Per_Reloc_Sect;
126
127/* Defined to get at the elf section numbers and section name
128   indices in symtab for the dwarf sections
129   Must match .rel.* names in _dwarf_rel_section_names
130   exactly.
131*/
132#define         DEBUG_INFO      0
133#define         DEBUG_LINE      1
134#define         DEBUG_ABBREV    2
135#define         DEBUG_FRAME     3
136#define         DEBUG_ARANGES   4
137#define         DEBUG_PUBNAMES  5
138#define         DEBUG_STR       6
139#define         DEBUG_FUNCNAMES 7
140#define         DEBUG_TYPENAMES 8
141#define         DEBUG_VARNAMES  9
142#define         DEBUG_WEAKNAMES 10
143#define         DEBUG_MACINFO   11
144#define         DEBUG_LOC   12
145
146    /* number of debug_* sections not including the relocations */
147#define         NUM_DEBUG_SECTIONS      DEBUG_LOC + 1
148
149
150struct Dwarf_P_Die_s {
151    Dwarf_Unsigned di_offset;	/* offset in debug info */
152    char *di_abbrev;		/* abbreviation */
153    Dwarf_Word di_abbrev_nbytes;	/* # of bytes in abbrev */
154    Dwarf_Tag di_tag;
155    Dwarf_P_Die di_parent;	/* parent of current die */
156    Dwarf_P_Die di_child;	/* first child */
157    Dwarf_P_Die di_left;	/* left sibling */
158    Dwarf_P_Die di_right;	/* right sibling */
159    Dwarf_P_Attribute di_attrs;	/* list of attributes */
160    Dwarf_P_Attribute di_last_attr;	/* last attribute */
161    int di_n_attr;		/* number of attributes */
162};
163
164
165/* producer fields */
166struct Dwarf_P_Attribute_s {
167    Dwarf_Half ar_attribute;	/* Attribute Value. */
168    Dwarf_Half ar_attribute_form;	/* Attribute Form. */
169    Dwarf_P_Die ar_ref_die;	/* die pointer if form ref */
170    char *ar_data;		/* data, format given by form */
171    Dwarf_Unsigned ar_nbytes;	/* no. of bytes of data */
172    Dwarf_Unsigned ar_rel_symidx;	/* when attribute has a
173					   relocatable value, holds
174					   index of symbol in SYMTAB */
175    Dwarf_Ubyte ar_rel_type;	/* relocation type */
176    Dwarf_Word ar_rel_offset;	/* Offset of relocation within block */
177    char ar_reloc_len;		/* Number of bytes that relocation
178				   applies to. 4 or 8. Unused and may
179				   be 0 if if ar_rel_type is
180				   R_MIPS_NONE */
181    Dwarf_P_Attribute ar_next;
182};
183
184/* A block of .debug_macinfo data: this forms a series of blocks.
185** Each macinfo input is compressed immediately and put into
186** the current block if room, else a newblock allocated.
187** The space allocation is such that the block and the macinfo
188** data are one malloc block: free with a pointer to this and the
189** mb_data is freed automatically.
190** Like the struct hack, but legal ANSI C.
191*/
192struct dw_macinfo_block_s {
193    struct dw_macinfo_block_s *mb_next;
194    unsigned long mb_avail_len;
195    unsigned long mb_used_len;
196    unsigned long mb_macinfo_data_space_len;
197    char *mb_data;		/* original malloc ptr. */
198};
199
200/* dwarf_sn_kind is for the array of similarly-treated
201   name -> cu ties
202*/
203enum dwarf_sn_kind { dwarf_snk_pubname, dwarf_snk_funcname,
204    dwarf_snk_weakname, dwarf_snk_typename,
205    dwarf_snk_varname,
206    dwarf_snk_entrycount	/* this one must be last */
207};
208
209
210
211/* The calls to add a varname etc use a list of
212   these as the list.
213*/
214struct Dwarf_P_Simple_nameentry_s {
215    Dwarf_P_Die sne_die;
216    char *sne_name;
217    int sne_name_len;
218    Dwarf_P_Simple_nameentry sne_next;
219};
220
221/* An array of these, each of which heads a list
222   of Dwarf_P_Simple_nameentry
223*/
224struct Dwarf_P_Simple_name_header_s {
225    Dwarf_P_Simple_nameentry sn_head;
226    Dwarf_P_Simple_nameentry sn_tail;
227    Dwarf_Signed sn_count;
228
229    /* length that will be generated, not counting fixed header or
230       trailer */
231    Dwarf_Signed sn_net_len;
232};
233typedef int (*_dwarf_pro_reloc_name_func_ptr) (Dwarf_P_Debug dbg, int sec_index, Dwarf_Unsigned offset,	/* r_offset
234													 */
235					       Dwarf_Unsigned symidx,
236					       enum Dwarf_Rel_Type type,
237					       int reltarget_length);
238
239typedef int (*_dwarf_pro_reloc_length_func_ptr) (Dwarf_P_Debug dbg, int sec_index, Dwarf_Unsigned offset,	/* r_offset
240														 */
241						 Dwarf_Unsigned
242						 start_symidx,
243						 Dwarf_Unsigned
244						 end_symidx,
245						 enum Dwarf_Rel_Type
246						 type,
247						 int reltarget_length);
248typedef int (*_dwarf_pro_transform_relocs_func_ptr) (Dwarf_P_Debug dbg,
249						     Dwarf_Signed *
250						     new_sec_count);
251
252/*
253	Each slot in a block of slots could be:
254	a binary stream relocation entry (32 or 64bit relocation data)
255        a SYMBOLIC relocation entry.
256	During creation sometimes we create multiple chained blocks,
257	but sometimes we create a single long block.
258        Before returning reloc data to caller,
259        we switch to a single, long-enough,
260	block.
261
262	We make counters here Dwarf_Unsigned so that we
263	get sufficient alignment. Since we use space after
264	the struct (at malloc time) for user data which
265        must have Dwarf_Unsigned alignment, this
266	struct must have that alignment too.
267
268*/
269struct Dwarf_P_Relocation_Block_s {
270    Dwarf_Unsigned rb_slots_in_block;	/* slots in block, as created */
271    Dwarf_Unsigned rb_next_slot_to_use;	/* counter, start at 0. */
272    struct Dwarf_P_Relocation_Block_s *rb_next;
273    char *rb_where_to_add_next;	/* pointer to next slot (might be past
274				   end, depending on
275				   rb_next_slot_to_use) */
276    char *rb_data;		/* data area */
277};
278
279/* One of these per potential relocation section
280   So one per actual dwarf section.
281   Left zeroed when not used (some sections have
282   no relocations).
283*/
284struct Dwarf_P_Per_Reloc_Sect_s {
285
286
287    unsigned long pr_reloc_total_count;	/* total number of entries
288					   across all blocks */
289
290    unsigned long pr_slots_per_block_to_alloc;	/* at Block alloc, this
291						   is the default
292						   number of slots to
293						   use */
294
295    int pr_sect_num_of_reloc_sect;	/* sect number returned by
296					   de_func() or de_func_b()
297					   call, this is the sect
298					   number of the relocation
299					   section. */
300
301
302    /* singly-linked list. add at and ('last') with count of blocks */
303    struct Dwarf_P_Relocation_Block_s *pr_first_block;
304    struct Dwarf_P_Relocation_Block_s *pr_last_block;
305    unsigned long pr_block_count;
306};
307
308#define DEFAULT_SLOTS_PER_BLOCK 3
309
310
311/* Fields used by producer */
312struct Dwarf_P_Debug_s {
313    /* used to catch dso passing dbg to another DSO with incompatible
314       version of libdwarf See PRO_VERSION_MAGIC */
315    int de_version_magic_number;
316
317    Dwarf_Unsigned de_access;
318    Dwarf_Handler de_errhand;
319    Dwarf_Ptr de_errarg;
320
321    /*
322       Call back function, used to create .debug* sections. Provided
323       by user. Only of these used per dbg. */
324    Dwarf_Callback_Func de_func;
325    Dwarf_Callback_Func_b de_func_b;
326
327    /* Flags from producer_init call */
328    Dwarf_Unsigned de_flags;
329
330    /* This holds information on debug section stream output, including
331       the stream data */
332    Dwarf_P_Section_Data de_debug_sects;
333
334    /* Pointer to the 'current active' section */
335    Dwarf_P_Section_Data de_current_active_section;
336
337    /* Number of debug data streams globs. */
338    Dwarf_Word de_n_debug_sect;
339
340    /* File entry information, null terminated singly-linked list */
341    Dwarf_P_F_Entry de_file_entries;
342    Dwarf_P_F_Entry de_last_file_entry;
343    Dwarf_Unsigned de_n_file_entries;
344
345    /* Has the directories used to search for source files */
346    Dwarf_P_Inc_Dir de_inc_dirs;
347    Dwarf_P_Inc_Dir de_last_inc_dir;
348    Dwarf_Unsigned de_n_inc_dirs;
349
350    /* Has all the line number info for the stmt program */
351    Dwarf_P_Line de_lines;
352    Dwarf_P_Line de_last_line;
353
354    /* List of cie's for the debug unit */
355    Dwarf_P_Cie de_frame_cies;
356    Dwarf_P_Cie de_last_cie;
357    Dwarf_Unsigned de_n_cie;
358
359
360    /* Singly-linked list of fde's for the debug unit */
361    Dwarf_P_Fde de_frame_fdes;
362    Dwarf_P_Fde de_last_fde;
363    Dwarf_Unsigned de_n_fde;
364
365    /* First die, leads to all others */
366    Dwarf_P_Die de_dies;
367
368    /* Pointer to list of strings */
369    char *de_strings;
370
371    /* Pointer to chain of aranges */
372    Dwarf_P_Arange de_arange;
373    Dwarf_P_Arange de_last_arange;
374    Dwarf_Sword de_arange_count;
375
376    /* macinfo controls. */
377    /* first points to beginning of the list during creation */
378    struct dw_macinfo_block_s *de_first_macinfo;
379
380    /* current points to the current, unfilled, block */
381    struct dw_macinfo_block_s *de_current_macinfo;
382
383
384    /* Pointer to the first section, to support reset_section_bytes */
385    Dwarf_P_Section_Data de_first_debug_sect;
386
387    /* handles pubnames, weaknames, etc. See dwarf_sn_kind in
388       pro_opaque.h */
389    struct Dwarf_P_Simple_name_header_s
390      de_simple_name_headers[dwarf_snk_entrycount];
391
392    /* relocation data. not all sections will actally have relocation
393       info, of course */
394    struct Dwarf_P_Per_Reloc_Sect_s de_reloc_sect[NUM_DEBUG_SECTIONS];
395    int de_reloc_next_to_return;	/* iterator on reloc sections
396					   (SYMBOLIC output) */
397
398    /* used in remembering sections */
399    int de_elf_sects[NUM_DEBUG_SECTIONS];	/*
400						   elf sect number of
401						   the section itself,
402						   DEBUG_LINE for
403						   example */
404
405    Dwarf_Unsigned de_sect_name_idx[NUM_DEBUG_SECTIONS];	/* section
406								   name
407								   index
408								   or
409								   handle
410								   for
411								   the
412								   name
413								   of
414								   the
415								   symbol
416								   for
417								   DEBUG_LINE
418								   for
419								   example
420								 */
421
422
423
424    int de_offset_reloc;	/* offset reloc type, R_MIPS_32 for
425				   example. Specific to the ABI being
426				   produced. Relocates offset size
427				   field */
428    int de_exc_reloc;		/* reloc type specific to exception
429				   table relocs. */
430    int de_ptr_reloc;		/* standard reloc type, R_MIPS_32 for
431				   example. Specific to the ABI being
432				   produced. relocates pointer size
433				   field */
434
435    unsigned char de_offset_size;	/* section offset. Here to
436					   avoid test of abi in macro
437					   at run time MIPS -n32 4,
438					   -64 8.  */
439
440    unsigned char de_pointer_size;	/* size of pointer in target.
441					   Here to avoid test of abi in
442					   macro at run time MIPS -n32
443					   4, -64 is 8.  */
444
445    unsigned char de_is_64bit;	/* non-zero if is 64bit. Else 32 bit:
446				   used for passing this info as a flag
447				 */
448    unsigned char de_relocation_record_size;	/* reloc record size
449						   varies by ABI and
450						   relocation-output
451						   method (stream or
452						   symbolic) */
453
454    unsigned char de_64bit_extension;	/* non-zero if creating 64 bit
455					   offsets using dwarf2-99
456					   extension proposal */
457
458    int de_ar_data_attribute_form;	/* data8, data4 abi dependent */
459    int de_ar_ref_attr_form;	/* ref8 ref4 , abi dependent */
460
461    /* simple name relocations */
462    _dwarf_pro_reloc_name_func_ptr de_reloc_name;
463
464    /* relocations for a length, requiring a pair of symbols */
465    _dwarf_pro_reloc_length_func_ptr de_reloc_pair;
466
467    _dwarf_pro_transform_relocs_func_ptr de_transform_relocs_to_disk;
468
469    /* following used for macro buffers */
470    unsigned long de_compose_avail;
471    unsigned long de_compose_used_len;
472
473    unsigned char de_same_endian;
474    void *(*de_copy_word) (void *, const void *, size_t);
475
476    /* Add new fields at the END of this struct to preserve some hope
477       of sensible behavior on dbg passing between DSOs linked with
478       mismatched libdwarf producer versions. */
479
480};
481
482
483#define CURRENT_VERSION_STAMP		2
484
485Dwarf_Unsigned _dwarf_add_simple_name_entry(Dwarf_P_Debug dbg,
486					    Dwarf_P_Die die,
487					    char *entry_name,
488					    enum dwarf_sn_kind
489					    entrykind,
490					    Dwarf_Error * error);
491
492
493#define DISTINGUISHED_VALUE 0xffffffff	/* 64bit extension flag */
494