File: pro_types.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (272 lines) | stat: -rw-r--r-- 8,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*

  Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright 2011  David Anderson. All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement
  or the like.  Any license provided herein, whether implied or
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with
  other software, or any other product whatsoever.

  You should have received a copy of the GNU Lesser General Public
  License along with this program; if not, write the Free Software
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

*/

#include "config.h"
#include "libdwarfdefs.h"
#include <stdio.h>
#include <string.h>
#ifdef HAVE_ELFACCESS_H
#include <elfaccess.h>
#endif
#include "pro_incl.h"
#include "pro_section.h"
#include "pro_types.h"


/*
    This function adds another type name to the
    list of type names for the given Dwarf_P_Debug.
    It returns 0 on error, and 1 otherwise.
*/
Dwarf_Unsigned
dwarf_add_typename(Dwarf_P_Debug dbg,
    Dwarf_P_Die die,
    char *type_name, Dwarf_Error * error)
{
    return
        _dwarf_add_simple_name_entry(dbg, die, type_name,
            dwarf_snk_typename, error);
}

/*
  The following is the generic 'add a simple name entry'
  for any of the simple name sections.

  See enum dwarf_sn_kind in pro_opaque.h

*/
Dwarf_Unsigned
_dwarf_add_simple_name_entry(Dwarf_P_Debug dbg,
    Dwarf_P_Die die,
    char *entry_name,
    enum dwarf_sn_kind entrykind,
    Dwarf_Error * error)
{
    Dwarf_P_Simple_nameentry nameentry;
    Dwarf_P_Simple_name_header hdr;
    char *name;
    int uword_size;

    if (dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (0);
    }

    if (die == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DIE_NULL);
        return (0);
    }


    nameentry = (Dwarf_P_Simple_nameentry)
        _dwarf_p_get_alloc(dbg,
            sizeof(struct Dwarf_P_Simple_nameentry_s));
    if (nameentry == NULL) {
        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (0);
    }

    name = _dwarf_p_get_alloc(dbg, strlen(entry_name) + 1);
    if (name == NULL) {
        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (0);
    }
    strcpy(name, entry_name);

    nameentry->sne_die = die;
    nameentry->sne_name = name;
    nameentry->sne_name_len = strlen(name);
    uword_size = dbg->de_offset_size;

    hdr = &dbg->de_simple_name_headers[entrykind];
    if (hdr->sn_head == NULL)
        hdr->sn_head = hdr->sn_tail = nameentry;
    else {
        hdr->sn_tail->sne_next = nameentry;
        hdr->sn_tail = nameentry;
    }
    hdr->sn_count++;
    hdr->sn_net_len += uword_size + nameentry->sne_name_len + 1;

    return (1);
}



/*
    _dwarf_transform_simplename_to_disk writes
    ".rel.debug_pubnames",
    ".rel.debug_funcnames",       sgi extension
    ".rel.debug_typenames",       sgi extension
    ".rel.debug_varnames",        sgi extension
    ".rel.debug_weaknames",       sgi extension
    to disk.
    section_index indexes one of those sections.
    entrykind is one of those 'kind's.  */
int
_dwarf_transform_simplename_to_disk(Dwarf_P_Debug dbg,
    enum dwarf_sn_kind entrykind,
    int section_index, /* in de_elf_sects etc */
    Dwarf_Signed *nbufs,
    Dwarf_Error * error)
{


    /* Used to fill in 0. */
    const Dwarf_Signed big_zero = 0;

    /* Used to scan the section data buffers. */
    Dwarf_P_Section_Data debug_sect;

    Dwarf_Signed debug_info_size;

    Dwarf_P_Simple_nameentry nameentry_original;
    Dwarf_P_Simple_nameentry nameentry;
    Dwarf_Small *stream_bytes;
    Dwarf_Small *cur_stream_bytes_ptr;
    Dwarf_Unsigned stream_bytes_count;
    Dwarf_Unsigned adjusted_length; /* count excluding length field */


    int uword_size = dbg->de_offset_size;
    int extension_size = dbg->de_64bit_extension ? 4 : 0;

    Dwarf_P_Simple_name_header hdr;


    /* ***** BEGIN CODE ***** */

    debug_info_size = 0;
    for (debug_sect = dbg->de_debug_sects; debug_sect != NULL;
        debug_sect = debug_sect->ds_next) {
        /*  We want the size of the .debug_info section for this CU
            because the dwarf spec requires us to output it below so we
            look for it specifically. */
        if (debug_sect->ds_elf_sect_no == dbg->de_elf_sects[DEBUG_INFO]) {
            debug_info_size += debug_sect->ds_nbytes;
        }
    }

    hdr = &dbg->de_simple_name_headers[entrykind];
    /* Size of the .debug_typenames (or similar) section header. */
    stream_bytes_count = extension_size + uword_size +  /* Size of
        length field. */
        DWARF_HALF_SIZE +    /* Size of version field. */
        uword_size +            /* Size of .debug_info offset. */
        uword_size;             /* Size of .debug_names. */



    nameentry_original = hdr->sn_head;
    nameentry = nameentry_original;
    /* add in the content size */
    stream_bytes_count += hdr->sn_net_len;

    /* Size of the last 0 offset. */
    stream_bytes_count += uword_size;

    /* Now we know how long the entire section is */
    GET_CHUNK(dbg, dbg->de_elf_sects[section_index],
        stream_bytes, (unsigned long) stream_bytes_count, error);
    cur_stream_bytes_ptr = stream_bytes;

    if (extension_size) {
        Dwarf_Unsigned x = DISTINGUISHED_VALUE;

        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
            (const void *) &x, sizeof(x), extension_size);
        cur_stream_bytes_ptr += extension_size;

    }
    /* Write the adjusted length of .debug_*names section. */
    adjusted_length = stream_bytes_count - uword_size - extension_size;
    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
        (const void *) &adjusted_length,
        sizeof(adjusted_length), uword_size);
    cur_stream_bytes_ptr += uword_size;

    /* Write the version as 2 bytes. */
    {
        Dwarf_Half verstamp = CURRENT_VERSION_STAMP;

        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
            (const void *) &verstamp,
            sizeof(verstamp), DWARF_HALF_SIZE);
        cur_stream_bytes_ptr += DWARF_HALF_SIZE;
    }

    /* Write the offset of the compile-unit. */
    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
        (const void *) &big_zero,
        sizeof(big_zero), uword_size);
    cur_stream_bytes_ptr += uword_size;

    /* now create the relocation for the compile_unit offset */
    {
        int res = dbg->de_relocate_by_name_symbol(dbg,
            section_index,
            extension_size + uword_size +
            DWARF_HALF_SIZE /* r_offset */ ,
            /* debug_info section name symbol */
            dbg->de_sect_name_idx[DEBUG_INFO],
            dwarf_drt_data_reloc,
            uword_size);

        if (res != DW_DLV_OK) {
            _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
            return DW_DLV_ERROR;
        }
    }

    /* Write the size of .debug_info section. */
    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
        (const void *) &debug_info_size,
        sizeof(debug_info_size), uword_size);
    cur_stream_bytes_ptr += uword_size;


    for (nameentry = nameentry_original;
        nameentry != NULL; nameentry = nameentry->sne_next) {

        /* Copy offset of die from start of compile-unit. */
        WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
            (const void *) &nameentry->sne_die->di_offset,
            sizeof(nameentry->sne_die->di_offset),
            uword_size);
        cur_stream_bytes_ptr += uword_size;

        /* Copy the type name. */
        strcpy((char *) cur_stream_bytes_ptr, nameentry->sne_name);
        cur_stream_bytes_ptr += nameentry->sne_name_len + 1;
    }

    WRITE_UNALIGNED(dbg, cur_stream_bytes_ptr,
        (const void *) &big_zero,
        sizeof(big_zero), uword_size);
    *nbufs =  dbg->de_n_debug_sect;
    return DW_DLV_OK;
}