149d3bc9Richard Lowe/*
249d3bc9Richard Lowe
307dc194Richard Lowe  Copyright (C) 2000,2002,2004 Silicon Graphics, Inc.  All Rights Reserved.
407dc194Richard Lowe  Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
54d9fdb4Robert Mustacchi  Portions Copyright 2011-2017 David Anderson. All Rights Reserved.
649d3bc9Richard Lowe
749d3bc9Richard Lowe  This program is free software; you can redistribute it and/or modify it
84d9fdb4Robert Mustacchi  under the terms of version 2.1 of the GNU Lesser General Public License
949d3bc9Richard Lowe  as published by the Free Software Foundation.
1049d3bc9Richard Lowe
1149d3bc9Richard Lowe  This program is distributed in the hope that it would be useful, but
1249d3bc9Richard Lowe  WITHOUT ANY WARRANTY; without even the implied warranty of
134d9fdb4Robert Mustacchi  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1449d3bc9Richard Lowe
1549d3bc9Richard Lowe  Further, this software is distributed without any warranty that it is
164d9fdb4Robert Mustacchi  free of the rightful claim of any third person regarding infringement
174d9fdb4Robert Mustacchi  or the like.  Any license provided herein, whether implied or
1849d3bc9Richard Lowe  otherwise, applies only to this software file.  Patent licenses, if
194d9fdb4Robert Mustacchi  any, provided herein do not apply to combinations of this program with
204d9fdb4Robert Mustacchi  other software, or any other product whatsoever.
2149d3bc9Richard Lowe
2207dc194Richard Lowe  You should have received a copy of the GNU Lesser General Public
2307dc194Richard Lowe  License along with this program; if not, write the Free Software
2407dc194Richard Lowe  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
2549d3bc9Richard Lowe  USA.
2649d3bc9Richard Lowe
2749d3bc9Richard Lowe*/
2849d3bc9Richard Lowe
2949d3bc9Richard Lowe#include "libdwarfdefs.h"
3049d3bc9Richard Lowe
3149d3bc9Richard Lowe#define true                    1
3249d3bc9Richard Lowe#define false                   0
3349d3bc9Richard Lowe
344d9fdb4Robert Mustacchi/*  The DISTINGUISHED VALUE is 4 byte value defined by DWARF
354d9fdb4Robert Mustacchi    since DWARF3. */
364d9fdb4Robert Mustacchi#define DISTINGUISHED_VALUE_ARRAY(x)  unsigned char x[4] = { 0xff,0xff,0xff,0xff }
374d9fdb4Robert Mustacchi#define DISTINGUISHED_VALUE 0xffffffff /* 64bit extension flag */
3849d3bc9Richard Lowe
394d9fdb4Robert Mustacchi/* to identify a cie */
404d9fdb4Robert Mustacchi#define DW_CIE_ID          ~(0x0)
414d9fdb4Robert Mustacchi#define DW_CIE_VERSION     1
4249d3bc9Richard Lowe
4349d3bc9Richard Lowetypedef signed char Dwarf_Sbyte;
4449d3bc9Richard Lowetypedef unsigned char Dwarf_Ubyte;
4549d3bc9Richard Lowetypedef signed short Dwarf_Shalf;
4649d3bc9Richard Lowe
4749d3bc9Richard Lowe/*
484d9fdb4Robert Mustacchi    On any change that makes libdwarf producer
494d9fdb4Robert Mustacchi    incompatible, increment this number.
504d9fdb4Robert Mustacchi    1->2->3 ...  */
5149d3bc9Richard Lowe#define  PRO_VERSION_MAGIC 0xdead1
5249d3bc9Richard Lowe
5349d3bc9Richard Lowe
544d9fdb4Robert Mustacchi#define DWARF_HALF_SIZE 2
554d9fdb4Robert Mustacchi#define DWARF_32BIT_SIZE 4
564d9fdb4Robert Mustacchi#define DWARF_64BIT_SIZE 8
5749d3bc9Richard Lowe
5849d3bc9Richard Lowe/*
594d9fdb4Robert Mustacchi    producer:
604d9fdb4Robert Mustacchi    This struct is used to hold information about all
614d9fdb4Robert Mustacchi    debug* sections. On creating a new section, section
624d9fdb4Robert Mustacchi    names and indices are added to this struct
634d9fdb4Robert Mustacchi    definition in pro_section.h */
644d9fdb4Robert Mustacchitypedef struct Dwarf_P_Section_Data_s *Dwarf_P_Section_Data;
6549d3bc9Richard Lowe
6649d3bc9Richard Lowe/*
674d9fdb4Robert Mustacchi    producer:
684d9fdb4Robert Mustacchi    This struct holds file entries or
694d9fdb4Robert Mustacchi    include file entries for the statement prologue.
704d9fdb4Robert Mustacchi    Defined in pro_line.h
7149d3bc9Richard Lowe*/
7249d3bc9Richard Lowetypedef struct Dwarf_P_F_Entry_s *Dwarf_P_F_Entry;
7349d3bc9Richard Lowe
7449d3bc9Richard Lowe/*
754d9fdb4Robert Mustacchi    producer:
764d9fdb4Robert Mustacchi    This struct holds information for each cie. Defn in pro_frame.h
7749d3bc9Richard Lowe*/
7849d3bc9Richard Lowetypedef struct Dwarf_P_Cie_s *Dwarf_P_Cie;
7949d3bc9Richard Lowe
8049d3bc9Richard Lowe/*
814d9fdb4Robert Mustacchi    producer:
824d9fdb4Robert Mustacchi    Struct to hold line number information, different from
834d9fdb4Robert Mustacchi    Dwarf_Line opaque type.
8449d3bc9Richard Lowe*/
8549d3bc9Richard Lowetypedef struct Dwarf_P_Line_s *Dwarf_P_Line;
8649d3bc9Richard Lowe
8749d3bc9Richard Lowe/*
884d9fdb4Robert Mustacchi    producer:
894d9fdb4Robert Mustacchi    Struct to hold information about address ranges.
9049d3bc9Richard Lowe*/
9149d3bc9Richard Lowetypedef struct Dwarf_P_Simple_nameentry_s *Dwarf_P_Simple_nameentry;
9249d3bc9Richard Lowetypedef struct Dwarf_P_Simple_name_header_s *Dwarf_P_Simple_name_header;
9349d3bc9Richard Lowetypedef struct Dwarf_P_Arange_s *Dwarf_P_Arange;
9449d3bc9Richard Lowetypedef struct Dwarf_P_Per_Reloc_Sect_s *Dwarf_P_Per_Reloc_Sect;
9507dc194Richard Lowetypedef struct Dwarf_P_Per_Sect_String_Attrs_s *Dwarf_P_Per_Sect_String_Attrs;
964d9fdb4Robert Mustacchitypedef struct Dwarf_P_Dnames_s *Dwarf_P_Dnames;
9749d3bc9Richard Lowe
9849d3bc9Richard Lowe/* Defined to get at the elf section numbers and section name
9949d3bc9Richard Lowe   indices in symtab for the dwarf sections
10049d3bc9Richard Lowe   Must match .rel.* names in _dwarf_rel_section_names
10149d3bc9Richard Lowe   exactly.
10249d3bc9Richard Lowe*/
10349d3bc9Richard Lowe#define         DEBUG_INFO      0
10449d3bc9Richard Lowe#define         DEBUG_LINE      1
10549d3bc9Richard Lowe#define         DEBUG_ABBREV    2
10649d3bc9Richard Lowe#define         DEBUG_FRAME     3
10749d3bc9Richard Lowe#define         DEBUG_ARANGES   4
10849d3bc9Richard Lowe#define         DEBUG_PUBNAMES  5
1094d9fdb4Robert Mustacchi#define         DEBUG_FUNCNAMES 6
1104d9fdb4Robert Mustacchi#define         DEBUG_TYPENAMES 7
1114d9fdb4Robert Mustacchi#define         DEBUG_VARNAMES  8
1124d9fdb4Robert Mustacchi#define         DEBUG_WEAKNAMES 9
1134d9fdb4Robert Mustacchi#define         DEBUG_MACINFO   10 /* DWARF 2,3,4 only */
1144d9fdb4Robert Mustacchi#define         DEBUG_LOC       11
1154d9fdb4Robert Mustacchi#define         DEBUG_RANGES    12
1164d9fdb4Robert Mustacchi#define         DEBUG_TYPES     13
1174d9fdb4Robert Mustacchi#define         DEBUG_PUBTYPES  14
1184d9fdb4Robert Mustacchi#define         DEBUG_NAMES     15 /* DWARF5. aka dnames */
1194d9fdb4Robert Mustacchi#define         DEBUG_STR       16
1204d9fdb4Robert Mustacchi#define         DEBUG_LINE_STR  17
1214d9fdb4Robert Mustacchi#define         DEBUG_MACRO     18 /* DWARF 5. */
1224d9fdb4Robert Mustacchi#define         DEBUG_LOCLISTS  19 /* DWARF 5. */
1234d9fdb4Robert Mustacchi#define         DEBUG_RNGLISTS  20 /* DWARF 5. */
1244d9fdb4Robert Mustacchi
1254d9fdb4Robert Mustacchi/* Maximum number of debug_* sections not including the relocations */
1264d9fdb4Robert Mustacchi#define         NUM_DEBUG_SECTIONS      21
1274d9fdb4Robert Mustacchi
1284d9fdb4Robert Mustacchi/*  The FORM codes available are defined in DWARF5
1294d9fdb4Robert Mustacchi    on page 158, DW_LNCT_path  */
1304d9fdb4Robert Mustacchistruct Dwarf_P_Line_format_s {
1314d9fdb4Robert Mustacchi    /* DW_LNCT_path etc. */
1324d9fdb4Robert Mustacchi    unsigned def_content_type;
1334d9fdb4Robert Mustacchi
1344d9fdb4Robert Mustacchi    /* DW_FORM_string or DW_FORM_strp or DW_FORM_strp
1354d9fdb4Robert Mustacchi        or DW_FORM_strp_sup or for dwo, some others. */
1364d9fdb4Robert Mustacchi    unsigned def_form_code;
1374d9fdb4Robert Mustacchi};
13849d3bc9Richard Lowe
1394d9fdb4Robert Mustacchi#define DW_LINE_FORMATS_MAX 6
1404d9fdb4Robert Mustacchi/*  Describes the data needed to generate line table header info
1414d9fdb4Robert Mustacchi    so we can vary the init at runtime. */
1424d9fdb4Robert Mustacchistruct Dwarf_P_Line_Inits_s {
1434d9fdb4Robert Mustacchi    unsigned pi_linetable_version; /* line table version number */
1444d9fdb4Robert Mustacchi    unsigned pi_default_is_stmt; /* default value for is_stmt */
1454d9fdb4Robert Mustacchi
1464d9fdb4Robert Mustacchi    /* Size of the smallest instruction, in bytes. */
1474d9fdb4Robert Mustacchi    unsigned pi_minimum_instruction_length;
1484d9fdb4Robert Mustacchi
1494d9fdb4Robert Mustacchi    /*  Normally opcode_base is determined by pi_version, but we
1504d9fdb4Robert Mustacchi        allow manual setting here so we can generate data like
1514d9fdb4Robert Mustacchi        GNU with a DWARF3 opcode base in a DWARF2 section.
1524d9fdb4Robert Mustacchi        This determines how much of the header_opcode_lengths
1534d9fdb4Robert Mustacchi        table is emitted in the line table header */
1544d9fdb4Robert Mustacchi    unsigned pi_opcode_base;
1554d9fdb4Robert Mustacchi
1564d9fdb4Robert Mustacchi    int      pi_line_base;   /* For line table header. */
1574d9fdb4Robert Mustacchi    int      pi_line_range;  /* For line table header. */
1584d9fdb4Robert Mustacchi
1594d9fdb4Robert Mustacchi    /* Make this >1 for VLIW machines.  DWARF4,DWARF5 */
1604d9fdb4Robert Mustacchi    unsigned pi_maximum_operations_per_instruction;
1614d9fdb4Robert Mustacchi
1624d9fdb4Robert Mustacchi    /* DWARF 5  */
1634d9fdb4Robert Mustacchi    unsigned pi_segment_selector_size;
1644d9fdb4Robert Mustacchi    unsigned pi_address_size;
1654d9fdb4Robert Mustacchi    unsigned pi_segment_size;
1664d9fdb4Robert Mustacchi    unsigned pi_directory_entry_format_count;
1674d9fdb4Robert Mustacchi    struct Dwarf_P_Line_format_s pi_incformats[DW_LINE_FORMATS_MAX];
1684d9fdb4Robert Mustacchi
1694d9fdb4Robert Mustacchi    unsigned pi_file_entry_format_count;
1704d9fdb4Robert Mustacchi    struct Dwarf_P_Line_format_s pi_fileformats[DW_LINE_FORMATS_MAX];
1714d9fdb4Robert Mustacchi};
17249d3bc9Richard Lowe
17349d3bc9Richard Lowe
17449d3bc9Richard Lowestruct Dwarf_P_Die_s {
1754d9fdb4Robert Mustacchi    Dwarf_Unsigned di_offset; /* offset in debug info */
1764d9fdb4Robert Mustacchi    char *di_abbrev;  /* abbreviation */
1774d9fdb4Robert Mustacchi    Dwarf_Unsigned di_abbrev_nbytes; /* # of bytes in abbrev */
17849d3bc9Richard Lowe    Dwarf_Tag di_tag;
1794d9fdb4Robert Mustacchi    Dwarf_P_Die di_parent; /* parent of current die */
1804d9fdb4Robert Mustacchi    Dwarf_P_Die di_child; /* first child */
1814d9fdb4Robert Mustacchi    /*  The last child field makes linking up children an O(1) operation,
1824d9fdb4Robert Mustacchi        See pro_die.c. */
1834d9fdb4Robert Mustacchi    Dwarf_P_Die di_last_child;
1844d9fdb4Robert Mustacchi    Dwarf_P_Die di_left; /* left sibling */
1854d9fdb4Robert Mustacchi    Dwarf_P_Die di_right; /* right sibling */
1864d9fdb4Robert Mustacchi    Dwarf_P_Attribute di_attrs; /* list of attributes */
1874d9fdb4Robert Mustacchi    Dwarf_P_Attribute di_last_attr; /* last attribute */
1884d9fdb4Robert Mustacchi    int di_n_attr;  /* number of attributes */
1894d9fdb4Robert Mustacchi    Dwarf_P_Debug di_dbg; /* For memory management */
19007dc194Richard Lowe    Dwarf_Unsigned di_marker;   /* used to attach symbols to dies */
19149d3bc9Richard Lowe};
19249d3bc9Richard Lowe
19349d3bc9Richard Lowe
19449d3bc9Richard Lowe/* producer fields */
19549d3bc9Richard Lowestruct Dwarf_P_Attribute_s {
1964d9fdb4Robert Mustacchi    Dwarf_Half ar_attribute; /* Attribute Value. */
1974d9fdb4Robert Mustacchi    Dwarf_Half ar_attribute_form; /* Attribute Form. */
1984d9fdb4Robert Mustacchi    Dwarf_P_Die ar_ref_die; /* die pointer if form ref */
1994d9fdb4Robert Mustacchi    char *ar_data; /* data, format given by form */
2004d9fdb4Robert Mustacchi    Dwarf_Unsigned ar_nbytes; /* no. of bytes of data */
2014d9fdb4Robert Mustacchi    Dwarf_Unsigned ar_rel_symidx; /* when attribute has a
2024d9fdb4Robert Mustacchi        relocatable value, holds
2034d9fdb4Robert Mustacchi        index of symbol in SYMTAB */
2044d9fdb4Robert Mustacchi    Dwarf_Unsigned ar_debug_str_offset; /* Offset in .debug_str
2054d9fdb4Robert Mustacchi        if non-zero. Zero offset never assigned a string. */
2064d9fdb4Robert Mustacchi    Dwarf_Ubyte ar_rel_type;  /* relocation type */
2074d9fdb4Robert Mustacchi    Dwarf_Unsigned ar_rel_offset; /* Offset of relocation within block */
2084d9fdb4Robert Mustacchi    char ar_reloc_len; /* Number of bytes that relocation
2094d9fdb4Robert Mustacchi        applies to. 4 or 8. Unused and may
2104d9fdb4Robert Mustacchi        be 0 if if ar_rel_type is
2114d9fdb4Robert Mustacchi        R_MIPS_NONE */
21249d3bc9Richard Lowe    Dwarf_P_Attribute ar_next;
2134d9fdb4Robert Mustacchi    /*  set if form = DW_FORM_implicit_const; */
2144d9fdb4Robert Mustacchi    Dwarf_Signed  ar_implicit_const;
21549d3bc9Richard Lowe};
21649d3bc9Richard Lowe
21749d3bc9Richard Lowe/* A block of .debug_macinfo data: this forms a series of blocks.
21849d3bc9Richard Lowe** Each macinfo input is compressed immediately and put into
21949d3bc9Richard Lowe** the current block if room, else a newblock allocated.
22049d3bc9Richard Lowe** The space allocation is such that the block and the macinfo
22149d3bc9Richard Lowe** data are one malloc block: free with a pointer to this and the
22249d3bc9Richard Lowe** mb_data is freed automatically.
22349d3bc9Richard Lowe** Like the struct hack, but legal ANSI C.
22449d3bc9Richard Lowe*/
22549d3bc9Richard Lowestruct dw_macinfo_block_s {
22649d3bc9Richard Lowe    struct dw_macinfo_block_s *mb_next;
22749d3bc9Richard Lowe    unsigned long mb_avail_len;
22849d3bc9Richard Lowe    unsigned long mb_used_len;
22949d3bc9Richard Lowe    unsigned long mb_macinfo_data_space_len;
2304d9fdb4Robert Mustacchi    char *mb_data;/* original malloc ptr. */
23149d3bc9Richard Lowe};
23249d3bc9Richard Lowe
2334d9fdb4Robert Mustacchi/*  dwarf_sn_kind is for the array of similarly-treated
2344d9fdb4Robert Mustacchi    name -> cu ties
23549d3bc9Richard Lowe*/
2364d9fdb4Robert Mustacchienum dwarf_sn_kind { dwarf_snk_pubname,  /* .debug_pubnames */
2374d9fdb4Robert Mustacchi    dwarf_snk_funcname,  /* SGI extension. */
2384d9fdb4Robert Mustacchi    dwarf_snk_weakname,  /* SGI extension. */
2394d9fdb4Robert Mustacchi    dwarf_snk_typename,  /* SGI extension. */
2404d9fdb4Robert Mustacchi    dwarf_snk_varname,   /* SGI extension. */
2414d9fdb4Robert Mustacchi    dwarf_snk_pubtype,   /* .debug_pubtypes */
2424d9fdb4Robert Mustacchi    dwarf_snk_entrycount /* this one must be last */
24349d3bc9Richard Lowe};
24449d3bc9Richard Lowe
24549d3bc9Richard Lowe
24649d3bc9Richard Lowe
24749d3bc9Richard Lowe/* The calls to add a varname etc use a list of
24849d3bc9Richard Lowe   these as the list.
24949d3bc9Richard Lowe*/
25049d3bc9Richard Lowestruct Dwarf_P_Simple_nameentry_s {
25149d3bc9Richard Lowe    Dwarf_P_Die sne_die;
25249d3bc9Richard Lowe    char *sne_name;
25349d3bc9Richard Lowe    int sne_name_len;
25449d3bc9Richard Lowe    Dwarf_P_Simple_nameentry sne_next;
25549d3bc9Richard Lowe};
25649d3bc9Richard Lowe
2574d9fdb4Robert Mustacchi/*  An array of these, each of which heads a list
2584d9fdb4Robert Mustacchi    of Dwarf_P_Simple_nameentry
25949d3bc9Richard Lowe*/
26049d3bc9Richard Lowestruct Dwarf_P_Simple_name_header_s {
26149d3bc9Richard Lowe    Dwarf_P_Simple_nameentry sn_head;
26249d3bc9Richard Lowe    Dwarf_P_Simple_nameentry sn_tail;
26349d3bc9Richard Lowe    Dwarf_Signed sn_count;
26449d3bc9Richard Lowe
2654d9fdb4Robert Mustacchi    /*  Length that will be generated, not counting fixed header or
2664d9fdb4Robert Mustacchi        trailer */
26749d3bc9Richard Lowe    Dwarf_Signed sn_net_len;
26849d3bc9Richard Lowe};
2694d9fdb4Robert Mustacchitypedef int (*_dwarf_pro_reloc_name_func_ptr) (Dwarf_P_Debug dbg,
2704d9fdb4Robert Mustacchi    int sec_index,
27107dc194Richard Lowe    Dwarf_Unsigned offset,/* r_offset */
27207dc194Richard Lowe    Dwarf_Unsigned symidx,
27307dc194Richard Lowe    enum Dwarf_Rel_Type type,
27407dc194Richard Lowe    int reltarget_length);
27507dc194Richard Lowe
2764d9fdb4Robert Mustacchitypedef int (*_dwarf_pro_reloc_length_func_ptr) (Dwarf_P_Debug dbg,
27707dc194Richard Lowe    int sec_index, Dwarf_Unsigned offset,/* r_offset */
27807dc194Richard Lowe    Dwarf_Unsigned start_symidx,
27907dc194Richard Lowe    Dwarf_Unsigned end_symidx,
28007dc194Richard Lowe    enum Dwarf_Rel_Type type,
28107dc194Richard Lowe    int reltarget_length);
28249d3bc9Richard Lowetypedef int (*_dwarf_pro_transform_relocs_func_ptr) (Dwarf_P_Debug dbg,
2834d9fdb4Robert Mustacchi    Dwarf_Signed *
2844d9fdb4Robert Mustacchi    new_sec_count);
28549d3bc9Richard Lowe
28649d3bc9Richard Lowe/*
28707dc194Richard Lowe    Each slot in a block of slots could be:
28807dc194Richard Lowe    a binary stream relocation entry (32 or 64bit relocation data)
28907dc194Richard Lowe    a SYMBOLIC relocation entry.
29007dc194Richard Lowe    During creation sometimes we create multiple chained blocks,
29107dc194Richard Lowe    but sometimes we create a single long block.
2924d9fdb4Robert Mustacchi    Before returning reloc data to caller,
29307dc194Richard Lowe    we switch to a single, long-enough,
29407dc194Richard Lowe    block.
29507dc194Richard Lowe
29607dc194Richard Lowe    We make counters here Dwarf_Unsigned so that we
29707dc194Richard Lowe    get sufficient alignment. Since we use space after
29807dc194Richard Lowe    the struct (at malloc time) for user data which
29907dc194Richard Lowe    must have Dwarf_Unsigned alignment, this
30007dc194Richard Lowe    struct must have that alignment too.
30149d3bc9Richard Lowe*/
30249d3bc9Richard Lowestruct Dwarf_P_Relocation_Block_s {
3034d9fdb4Robert Mustacchi    Dwarf_Unsigned rb_slots_in_block; /* slots in block, as created */
3044d9fdb4Robert Mustacchi    Dwarf_Unsigned rb_next_slot_to_use; /* counter, start at 0. */
30549d3bc9Richard Lowe    struct Dwarf_P_Relocation_Block_s *rb_next;
3064d9fdb4Robert Mustacchi    char *rb_where_to_add_next; /* pointer to next slot (might be past
3074d9fdb4Robert Mustacchi        end, depending on
3084d9fdb4Robert Mustacchi        rb_next_slot_to_use) */
3094d9fdb4Robert Mustacchi    char *rb_data; /* data area */
31049d3bc9Richard Lowe};
31149d3bc9Richard Lowe
3124d9fdb4Robert Mustacchi/* One of these per potential relocation section
31349d3bc9Richard Lowe   So one per actual dwarf section.
31449d3bc9Richard Lowe   Left zeroed when not used (some sections have
31549d3bc9Richard Lowe   no relocations).
31649d3bc9Richard Lowe*/
31749d3bc9Richard Lowestruct Dwarf_P_Per_Reloc_Sect_s {
3184d9fdb4Robert Mustacchi    unsigned long pr_reloc_total_count; /* total number of entries
31907dc194Richard Lowe        across all blocks */
32049d3bc9Richard Lowe
3214d9fdb4Robert Mustacchi    unsigned long pr_slots_per_block_to_alloc; /* at Block alloc, this
32207dc194Richard Lowe        is the default number of slots to use */
32349d3bc9Richard Lowe
3244d9fdb4Robert Mustacchi    int pr_sect_num_of_reloc_sect; /* sect number returned by
3254d9fdb4Robert Mustacchi        de_callback_func() or de_callback_func_b() or_c()
3264d9fdb4Robert Mustacchi        call, this is the sect
32707dc194Richard Lowe        number of the relocation section. */
32849d3bc9Richard Lowe
32949d3bc9Richard Lowe    /* singly-linked list. add at and ('last') with count of blocks */
33049d3bc9Richard Lowe    struct Dwarf_P_Relocation_Block_s *pr_first_block;
33149d3bc9Richard Lowe    struct Dwarf_P_Relocation_Block_s *pr_last_block;
33249d3bc9Richard Lowe    unsigned long pr_block_count;
33349d3bc9Richard Lowe};
33449d3bc9Richard Lowe
33549d3bc9Richard Lowe#define DEFAULT_SLOTS_PER_BLOCK 3
33649d3bc9Richard Lowe
33707dc194Richard Lowetypedef struct memory_list_s {
33807dc194Richard Lowe  struct memory_list_s *prev;
33907dc194Richard Lowe  struct memory_list_s *next;
34007dc194Richard Lowe} memory_list_t;
34107dc194Richard Lowe
34207dc194Richard Lowestruct Dwarf_P_Per_Sect_String_Attrs_s {
34307dc194Richard Lowe    int sect_sa_section_number;
34407dc194Richard Lowe    unsigned sect_sa_n_alloc;
34507dc194Richard Lowe    unsigned sect_sa_n_used;
34607dc194Richard Lowe    Dwarf_P_String_Attr sect_sa_list;
34707dc194Richard Lowe};
34849d3bc9Richard Lowe
3494d9fdb4Robert Mustacchistruct Dwarf_P_debug_str_entry_s {
3504d9fdb4Robert Mustacchi    Dwarf_P_Debug  dse_dbg;
3514d9fdb4Robert Mustacchi    /*  Name used initially with tfind. */
3524d9fdb4Robert Mustacchi    char *dse_name;
3534d9fdb4Robert Mustacchi
3544d9fdb4Robert Mustacchi    Dwarf_Unsigned dse_slen; /* includes space for NUL terminator */
3554d9fdb4Robert Mustacchi
3564d9fdb4Robert Mustacchi    /*  See dse_has_table_offset below. */
3574d9fdb4Robert Mustacchi    Dwarf_Unsigned dse_table_offset;
3584d9fdb4Robert Mustacchi
3594d9fdb4Robert Mustacchi    /*  For tsearch a hash table exists and we have a table offset.
3604d9fdb4Robert Mustacchi        dse_dbg->de_debug_str->ds_data + dse_table_offset
3614d9fdb4Robert Mustacchi        points to the string iff dse_has_table_offset != 0. */
3624d9fdb4Robert Mustacchi    unsigned char  dse_has_table_offset;
3634d9fdb4Robert Mustacchi};
3644d9fdb4Robert Mustacchi
3654d9fdb4Robert Mustacchistruct Dwarf_P_Str_stats_s {
3664d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_strp_count_debug_str;
3674d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_strp_len_debug_str;
3684d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_strp_len_debug_line_str;
3694d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_strp_reused_count;
3704d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_strp_reused_len;
3714d9fdb4Robert Mustacchi};
3724d9fdb4Robert Mustacchi
3734d9fdb4Robert Mustacchistruct Dwarf_P_Stats_s {
3744d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_str_count;
3754d9fdb4Robert Mustacchi    Dwarf_Unsigned ps_str_total_length;
3764d9fdb4Robert Mustacchi    struct Dwarf_P_Str_stats_s ps_strp;
3774d9fdb4Robert Mustacchi    struct Dwarf_P_Str_stats_s ps_line_strp;
3784d9fdb4Robert Mustacchi};
3794d9fdb4Robert Mustacchi
38049d3bc9Richard Lowe/* Fields used by producer */
38149d3bc9Richard Lowestruct Dwarf_P_Debug_s {
3824d9fdb4Robert Mustacchi    /*  Used to catch dso passing dbg to another DSO with incompatible
3834d9fdb4Robert Mustacchi        version of libdwarf See PRO_VERSION_MAGIC */
38449d3bc9Richard Lowe    int de_version_magic_number;
38549d3bc9Richard Lowe
38649d3bc9Richard Lowe    Dwarf_Handler de_errhand;
3874d9fdb4Robert Mustacchi    /*  de_user_data is provided so users can use it to readily tie
3884d9fdb4Robert Mustacchi        a callback to anything they desire.  The contents are not
3894d9fdb4Robert Mustacchi        used by libdwarf except to pass the data as a callback
3904d9fdb4Robert Mustacchi        argument.  New in June 2011. Available in
3914d9fdb4Robert Mustacchi        dwarf_pro_init_c() and its callback function.  */
3924d9fdb4Robert Mustacchi    void *    de_user_data;
39349d3bc9Richard Lowe    Dwarf_Ptr de_errarg;
39449d3bc9Richard Lowe
3954d9fdb4Robert Mustacchi    /*  Call back function, used to create .debug* sections. Provided
3964d9fdb4Robert Mustacchi        By user.  */
39707dc194Richard Lowe    Dwarf_Callback_Func de_callback_func;
39849d3bc9Richard Lowe
3994d9fdb4Robert Mustacchi    /*  Flags from producer_init call */
40049d3bc9Richard Lowe    Dwarf_Unsigned de_flags;
40149d3bc9Richard Lowe
4024d9fdb4Robert Mustacchi    /*  This holds information on debug info section
4034d9fdb4Robert Mustacchi        stream output, including the stream data */
40449d3bc9Richard Lowe    Dwarf_P_Section_Data de_debug_sects;
40549d3bc9Richard Lowe
4064d9fdb4Robert Mustacchi    /*  Defaults set as DW_FORM_string,
4074d9fdb4Robert Mustacchi        meaning not using .debug_str by default.
4084d9fdb4Robert Mustacchi        This intended for the .debug_info section. */
4094d9fdb4Robert Mustacchi    int de_debug_default_str_form;
4104d9fdb4Robert Mustacchi
4114d9fdb4Robert Mustacchi    /* If form DW_FORM_strp */
4124d9fdb4Robert Mustacchi    Dwarf_P_Section_Data de_debug_str;
4134d9fdb4Robert Mustacchi    void *de_debug_str_hashtab; /* for tsearch */
4144d9fdb4Robert Mustacchi
4154d9fdb4Robert Mustacchi    /* .debug_line_str section data if form DW_FORM_line_strp */
4164d9fdb4Robert Mustacchi    Dwarf_P_Section_Data de_debug_line_str;
4174d9fdb4Robert Mustacchi    void *de_debug_line_str_hashtab; /* for tsearch */
4184d9fdb4Robert Mustacchi
4194d9fdb4Robert Mustacchi    /*  Pointer to the 'current active' section */
42049d3bc9Richard Lowe    Dwarf_P_Section_Data de_current_active_section;
42149d3bc9Richard Lowe
4224d9fdb4Robert Mustacchi    /*  Number of debug data streams globs. */
4234d9fdb4Robert Mustacchi    Dwarf_Unsigned de_n_debug_sect;
42449d3bc9Richard Lowe
4254d9fdb4Robert Mustacchi    /*  File entry information, null terminated singly-linked list */
42649d3bc9Richard Lowe    Dwarf_P_F_Entry de_file_entries;
42749d3bc9Richard Lowe    Dwarf_P_F_Entry de_last_file_entry;
42849d3bc9Richard Lowe    Dwarf_Unsigned de_n_file_entries;
42949d3bc9Richard Lowe
4304d9fdb4Robert Mustacchi    /*  Has the directories used to search for source files */
4314d9fdb4Robert Mustacchi    Dwarf_P_F_Entry de_inc_dirs;
4324d9fdb4Robert Mustacchi    Dwarf_P_F_Entry de_last_inc_dir;
43349d3bc9Richard Lowe    Dwarf_Unsigned de_n_inc_dirs;
43449d3bc9Richard Lowe
4354d9fdb4Robert Mustacchi    /*  Has all the line number info for the stmt program */
43649d3bc9Richard Lowe    Dwarf_P_Line de_lines;
43749d3bc9Richard Lowe    Dwarf_P_Line de_last_line;
43849d3bc9Richard Lowe
4394d9fdb4Robert Mustacchi    /*  List of cie's for the debug unit */
44049d3bc9Richard Lowe    Dwarf_P_Cie de_frame_cies;
44149d3bc9Richard Lowe    Dwarf_P_Cie de_last_cie;
44249d3bc9Richard Lowe    Dwarf_Unsigned de_n_cie;
44349d3bc9Richard Lowe
44449d3bc9Richard Lowe    /* Singly-linked list of fde's for the debug unit */
44549d3bc9Richard Lowe    Dwarf_P_Fde de_frame_fdes;
44649d3bc9Richard Lowe    Dwarf_P_Fde de_last_fde;
44749d3bc9Richard Lowe    Dwarf_Unsigned de_n_fde;
44849d3bc9Richard Lowe
44949d3bc9Richard Lowe    /* First die, leads to all others */
45049d3bc9Richard Lowe    Dwarf_P_Die de_dies;
45149d3bc9Richard Lowe
45249d3bc9Richard Lowe    /* Pointer to chain of aranges */
45349d3bc9Richard Lowe    Dwarf_P_Arange de_arange;
45449d3bc9Richard Lowe    Dwarf_P_Arange de_last_arange;
4554d9fdb4Robert Mustacchi    Dwarf_Signed de_arange_count;
4564d9fdb4Robert Mustacchi
4574d9fdb4Robert Mustacchi    /*  debug_names  de_dnames is base of dnames info
4584d9fdb4Robert Mustacchi        before disk form */
4594d9fdb4Robert Mustacchi    Dwarf_P_Dnames de_dnames;
4604d9fdb4Robert Mustacchi    Dwarf_P_Section_Data de_dnames_sect;
46149d3bc9Richard Lowe
46249d3bc9Richard Lowe    /* macinfo controls. */
46349d3bc9Richard Lowe    /* first points to beginning of the list during creation */
46449d3bc9Richard Lowe    struct dw_macinfo_block_s *de_first_macinfo;
46549d3bc9Richard Lowe
46649d3bc9Richard Lowe    /* current points to the current, unfilled, block */
46749d3bc9Richard Lowe    struct dw_macinfo_block_s *de_current_macinfo;
46849d3bc9Richard Lowe
46949d3bc9Richard Lowe    /* Pointer to the first section, to support reset_section_bytes */
47049d3bc9Richard Lowe    Dwarf_P_Section_Data de_first_debug_sect;
47149d3bc9Richard Lowe
4724d9fdb4Robert Mustacchi    /*  Handles pubnames, weaknames, etc. See dwarf_sn_kind in
4734d9fdb4Robert Mustacchi        pro_opaque.h */
47449d3bc9Richard Lowe    struct Dwarf_P_Simple_name_header_s
4754d9fdb4Robert Mustacchi        de_simple_name_headers[dwarf_snk_entrycount];
47649d3bc9Richard Lowe
4774d9fdb4Robert Mustacchi    /*  Relocation data. not all sections will actally have relocation
4784d9fdb4Robert Mustacchi        info, of course.  de_reloc_sect, de_elf_sects, and de_sect_name_idx
4794d9fdb4Robert Mustacchi        arrays are exactly in parallel. Not every de_elf_sect has
4804d9fdb4Robert Mustacchi        any relocations for it, of course. */
48149d3bc9Richard Lowe    struct Dwarf_P_Per_Reloc_Sect_s de_reloc_sect[NUM_DEBUG_SECTIONS];
4824d9fdb4Robert Mustacchi    int de_reloc_next_to_return; /* iterator on reloc sections
4834d9fdb4Robert Mustacchi        (SYMBOLIC output) */
48449d3bc9Richard Lowe
4854d9fdb4Robert Mustacchi    /*  Used in remembering sections. See de_reloc_sect above.  */
48607dc194Richard Lowe    int de_elf_sects[NUM_DEBUG_SECTIONS];  /* elf sect number of
48707dc194Richard Lowe        the section itself, DEBUG_LINE for example */
48849d3bc9Richard Lowe
4894d9fdb4Robert Mustacchi    /*  Section name index or handle for the name of the symbol for
49007dc194Richard Lowe        DEBUG_LINE for example */
4914d9fdb4Robert Mustacchi    Dwarf_Unsigned de_sect_name_idx[NUM_DEBUG_SECTIONS];
4924d9fdb4Robert Mustacchi
4934d9fdb4Robert Mustacchi    int de_offset_reloc; /* offset reloc type, R_MIPS_32 for
4944d9fdb4Robert Mustacchi        example. Specific to the ABI being
4954d9fdb4Robert Mustacchi        produced. Relocates offset size
4964d9fdb4Robert Mustacchi        field */
4974d9fdb4Robert Mustacchi    int de_exc_reloc; /* reloc type specific to exception
4984d9fdb4Robert Mustacchi        table relocs. */
4994d9fdb4Robert Mustacchi    int de_ptr_reloc;  /* standard reloc type, R_MIPS_32 for
5004d9fdb4Robert Mustacchi        example. Specific to the ABI being
5014d9fdb4Robert Mustacchi        produced. relocates pointer size
5024d9fdb4Robert Mustacchi        field */
5034d9fdb4Robert Mustacchi    unsigned char de_irix_exc_augmentation; /* If non-zero means
5044d9fdb4Robert Mustacchi        that producing an IRIX exception-table offset in a CIE header
5054d9fdb4Robert Mustacchi        is allowed (depending on the augmentation string). */
5064d9fdb4Robert Mustacchi
5074d9fdb4Robert Mustacchi    unsigned char de_dwarf_offset_size; /* dwarf  offset size. */
5084d9fdb4Robert Mustacchi    unsigned char de_elf_offset_size;  /* object section offset size. */
5094d9fdb4Robert Mustacchi    unsigned char de_pointer_size; /* size of address in target. */
5104d9fdb4Robert Mustacchi
5114d9fdb4Robert Mustacchi    /*  Added April 19, 2017.  For DWARF5 */
5124d9fdb4Robert Mustacchi    unsigned char de_segment_selector_size;
5134d9fdb4Robert Mustacchi
5144d9fdb4Robert Mustacchi    unsigned char de_relocation_record_size; /* reloc record size
5154d9fdb4Robert Mustacchi        varies by ABI and
5164d9fdb4Robert Mustacchi        relocation-output
5174d9fdb4Robert Mustacchi        method (stream or
5184d9fdb4Robert Mustacchi        symbolic) */
5194d9fdb4Robert Mustacchi
5204d9fdb4Robert Mustacchi    unsigned char de_64bit_extension;/* non-zero if creating 64 bit
5214d9fdb4Robert Mustacchi        offsets using dwarf2-99
5224d9fdb4Robert Mustacchi        extension proposal */
5234d9fdb4Robert Mustacchi
5244d9fdb4Robert Mustacchi    unsigned char de_output_version; /* 2,3,4, or 5. The version number
5254d9fdb4Robert Mustacchi        of the output. (not necessarily that of each section,
5264d9fdb4Robert Mustacchi        which depends on the base version). */
5274d9fdb4Robert Mustacchi
5284d9fdb4Robert Mustacchi    /*  Defaults will be mostly useless, but such do exist */
5294d9fdb4Robert Mustacchi    unsigned       de_big_endian; /* if 0 target is little-endian */
5304d9fdb4Robert Mustacchi
5314d9fdb4Robert Mustacchi    int de_ar_data_attribute_form; /* data8, data4 abi &version dependent */
5324d9fdb4Robert Mustacchi    int de_ar_ref_attr_form; /* ref8 ref4 , abi dependent */
53349d3bc9Richard Lowe
53449d3bc9Richard Lowe    /* simple name relocations */
5354d9fdb4Robert Mustacchi    _dwarf_pro_reloc_name_func_ptr de_relocate_by_name_symbol;
53649d3bc9Richard Lowe
53749d3bc9Richard Lowe    /* relocations for a length, requiring a pair of symbols */
5384d9fdb4Robert Mustacchi    _dwarf_pro_reloc_length_func_ptr de_relocate_pair_by_symbol;
53949d3bc9Richard Lowe
54049d3bc9Richard Lowe    _dwarf_pro_transform_relocs_func_ptr de_transform_relocs_to_disk;
54149d3bc9Richard Lowe
54249d3bc9Richard Lowe    /* following used for macro buffers */
54349d3bc9Richard Lowe    unsigned long de_compose_avail;
54449d3bc9Richard Lowe    unsigned long de_compose_used_len;
54549d3bc9Richard Lowe
54649d3bc9Richard Lowe    unsigned char de_same_endian;
5474d9fdb4Robert Mustacchi    void (*de_copy_word) (void *, const void *, unsigned long);
54849d3bc9Richard Lowe
5494d9fdb4Robert Mustacchi    /*  Add new fields at the END of this struct to preserve some hope
5504d9fdb4Robert Mustacchi        of sensible behavior on dbg passing between DSOs linked with
5514d9fdb4Robert Mustacchi        mismatched libdwarf producer versions. */
55249d3bc9Richard Lowe
55307dc194Richard Lowe    Dwarf_P_Marker de_markers;  /* pointer to array of markers */
55407dc194Richard Lowe    unsigned de_marker_n_alloc;
55507dc194Richard Lowe    unsigned de_marker_n_used;
55607dc194Richard Lowe    int de_sect_sa_next_to_return;  /* Iterator on sring attrib sects */
55707dc194Richard Lowe    /* String attributes data of each section. */
55807dc194Richard Lowe    struct Dwarf_P_Per_Sect_String_Attrs_s de_sect_string_attr[NUM_DEBUG_SECTIONS];
5594d9fdb4Robert Mustacchi
5604d9fdb4Robert Mustacchi    /* Hold data needed to init line output flexibly. */
5614d9fdb4Robert Mustacchi    struct Dwarf_P_Line_Inits_s de_line_inits;
5624d9fdb4Robert Mustacchi
5634d9fdb4Robert Mustacchi    struct Dwarf_P_Stats_s de_stats;
56449d3bc9Richard Lowe};
56549d3bc9Richard Lowe
5664d9fdb4Robert Mustacchi#define CURRENT_VERSION_STAMP   2
56749d3bc9Richard Lowe
5684d9fdb4Robert Mustacchiint _dwarf_add_simple_name_entry(Dwarf_P_Debug dbg,
5694d9fdb4Robert Mustacchi    Dwarf_P_Die die,
5704d9fdb4Robert Mustacchi    char *entry_name,
5714d9fdb4Robert Mustacchi    enum dwarf_sn_kind
5724d9fdb4Robert Mustacchi    entrykind,
5734d9fdb4Robert Mustacchi    Dwarf_Error * error);
57449d3bc9Richard Lowe
5754d9fdb4Robert Mustacchienum dwarf_which_hash {
5764d9fdb4Robert Mustacchi    _dwarf_hash_debug_str,
5774d9fdb4Robert Mustacchi    _dwarf_hash_debug_line_str,
5784d9fdb4Robert Mustacchi    _dwarf_hash_debug_str_sup
5794d9fdb4Robert Mustacchi};
58049d3bc9Richard Lowe
5814d9fdb4Robert Mustacchiint
5824d9fdb4Robert Mustacchi_dwarf_insert_or_find_in_debug_str(Dwarf_P_Debug dbg,
5834d9fdb4Robert Mustacchi    char *name,
5844d9fdb4Robert Mustacchi    enum  dwarf_which_hash,
5854d9fdb4Robert Mustacchi    unsigned slen, /* includes space for trailing NUL */
5864d9fdb4Robert Mustacchi    Dwarf_Unsigned *offset_in_debug_str,
5874d9fdb4Robert Mustacchi    Dwarf_Error *error);
5884d9fdb4Robert Mustacchi
5894d9fdb4Robert Mustacchiint _dwarf_log_extra_flagstrings(Dwarf_P_Debug dbg,
5904d9fdb4Robert Mustacchi  const char *extra,
5914d9fdb4Robert Mustacchi  int *err);
592