Coverage Report

Created: 2023-12-08 06:43

/src/libdwarf/src/lib/libdwarf/dwarf_die_deliv.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
  Copyright (C) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
3
  Portions Copyright (C) 2007-2022 David Anderson. All Rights Reserved.
4
  Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5
6
  This program is free software; you can redistribute it
7
  and/or modify it under the terms of version 2.1 of the
8
  GNU Lesser General Public License as published by the Free
9
  Software Foundation.
10
11
  This program is distributed in the hope that it would be
12
  useful, but WITHOUT ANY WARRANTY; without even the implied
13
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
  PURPOSE.
15
16
  Further, this software is distributed without any warranty
17
  that it is free of the rightful claim of any third person
18
  regarding infringement or the like.  Any license provided
19
  herein, whether implied or otherwise, applies only to this
20
  software file.  Patent licenses, if any, provided herein
21
  do not apply to combinations of this program with other
22
  software, or any other product whatsoever.
23
24
  You should have received a copy of the GNU Lesser General
25
  Public License along with this program; if not, write the
26
  Free Software Foundation, Inc., 51 Franklin Street - Fifth
27
  Floor, Boston MA 02110-1301, USA.
28
29
*/
30
31
#include <config.h>
32
#include <stdio.h> /* debugging */
33
34
#include <string.h> /* memcmp() memcpy() memset() strcmp() strlen() */
35
#include <stdlib.h> /* calloc() free() */
36
37
#if defined(_WIN32) && defined(HAVE_STDAFX_H)
38
#include "stdafx.h"
39
#endif /* HAVE_STDAFX_H */
40
41
#ifdef HAVE_STDINT_H
42
#include <stdint.h> /* uintptr_t */
43
#endif /* HAVE_STDINT_H */
44
45
#include "dwarf.h"
46
#include "libdwarf.h"
47
#include "libdwarf_private.h"
48
#include "dwarf_base_types.h"
49
#include "dwarf_safe_strcpy.h"
50
#include "dwarf_opaque.h"
51
#include "dwarf_alloc.h"
52
#include "dwarf_error.h"
53
#include "dwarf_util.h"
54
#include "dwarf_str_offsets.h"
55
#include "dwarf_string.h"
56
#include "dwarf_die_deliv.h"
57
58
/* These are sanity checks, not 'rules'. */
59
79.8k
#define MINIMUM_ADDRESS_SIZE 2
60
39.9k
#define MAXIMUM_ADDRESS_SIZE 8
61
62
static void assign_correct_unit_type(Dwarf_CU_Context cu_context);
63
static int find_cu_die_base_fields(Dwarf_Debug dbg,
64
    Dwarf_CU_Context cucon,
65
    Dwarf_Die        cudie,
66
    Dwarf_Error     *error);
67
68
static int _dwarf_siblingof_internal(Dwarf_Debug dbg,
69
    Dwarf_Die die,
70
    Dwarf_CU_Context context,
71
    Dwarf_Bool is_info,
72
    Dwarf_Die * caller_ret_die, Dwarf_Error * error);
73
74
/*  see cuandunit.txt for an overview of the
75
    DWARF5 split dwarf sections and values
76
    and the DWARF4 GNU cc version of a draft
77
    version of DWARF5 (quite different from
78
    the final DWARF5).
79
*/
80
81
static struct Dwarf_Sig8_s dwarfsig8zero;
82
83
#if 0
84
static void
85
dump_bytes(char * msg,Dwarf_Small * start, long len)
86
{
87
    Dwarf_Small *end = start + len;
88
    Dwarf_Small *cur = start;
89
90
    printf("%s ",msg);
91
    for (; cur < end; cur++) {
92
        printf("%02x ", *cur);
93
    }
94
    printf("\n");
95
}
96
#endif /*0*/
97
98
/*  New October 2011.  Enables client code to know if
99
    it is a debug_info or debug_types context. */
100
Dwarf_Bool
101
dwarf_get_die_infotypes_flag(Dwarf_Die die)
102
3.72k
{
103
3.72k
    return die->di_is_info;
104
3.72k
}
105
106
/*
107
    For a given Dwarf_Debug dbg, this function checks
108
    if a CU that includes the given offset has been read
109
    or not.  If yes, it returns the Dwarf_CU_Context
110
    for the CU.  Otherwise it returns NULL.  Being an
111
    internal routine, it is assumed that a valid dbg
112
    is passed.
113
114
    **This is a sequential search.  May be too slow.
115
116
    If debug_info and debug_abbrev not loaded, this will
117
    wind up returning NULL. So no need to load before calling
118
    this.
119
*/
120
static Dwarf_CU_Context
121
_dwarf_find_CU_Context(Dwarf_Debug dbg,
122
    Dwarf_Off offset,
123
    Dwarf_Bool is_info)
124
45.7k
{
125
45.7k
    Dwarf_CU_Context cu_context = 0;
126
45.7k
    Dwarf_Debug_InfoTypes dis = is_info? &dbg->de_info_reading:
127
45.7k
        &dbg->de_types_reading;
128
129
45.7k
    if (offset >= dis->de_last_offset){
130
41.1k
        return NULL;
131
41.1k
    }
132
4.55k
    if (dis->de_cu_context != NULL &&
133
4.55k
        dis->de_cu_context->cc_next != NULL &&
134
4.55k
        dis->de_cu_context->cc_next->cc_debug_offset == offset) {
135
41
        return dis->de_cu_context->cc_next;
136
41
    }
137
4.50k
    if (dis->de_cu_context != NULL &&
138
4.50k
        dis->de_cu_context->cc_debug_offset <= offset) {
139
4.50k
        for (cu_context = dis->de_cu_context;
140
5.06k
            cu_context != NULL;
141
5.06k
            cu_context = cu_context->cc_next) {
142
5.06k
            if (offset >= cu_context->cc_debug_offset &&
143
5.06k
                offset < cu_context->cc_debug_offset +
144
5.06k
                cu_context->cc_length + cu_context->cc_length_size
145
5.06k
                + cu_context->cc_extension_size) {
146
4.50k
                return cu_context;
147
4.50k
            }
148
5.06k
        }
149
4.50k
    }
150
3
    for (cu_context = dis->de_cu_context_list;
151
4
        cu_context != NULL;
152
4
        cu_context = cu_context->cc_next) {
153
4
        if (offset >= cu_context->cc_debug_offset &&
154
4
            offset < cu_context->cc_debug_offset +
155
4
            cu_context->cc_length + cu_context->cc_length_size
156
4
            + cu_context->cc_extension_size) {
157
3
            return cu_context;
158
3
        }
159
4
    }
160
0
    return NULL;
161
3
}
162
163
int
164
dwarf_get_debugfission_for_die(Dwarf_Die die,
165
    struct Dwarf_Debug_Fission_Per_CU_s *fission_out,
166
    Dwarf_Error *error)
167
3.72k
{
168
3.72k
    Dwarf_CU_Context context = 0;
169
3.72k
    Dwarf_Debug dbg = 0;
170
3.72k
    struct Dwarf_Debug_Fission_Per_CU_s * percu = 0;
171
172
3.72k
    CHECK_DIE(die, DW_DLV_ERROR);
173
3.72k
    context = die->di_cu_context;
174
3.72k
    dbg = context->cc_dbg;
175
3.72k
    if (!_dwarf_file_has_debug_fission_index(dbg)) {
176
3.72k
        return DW_DLV_NO_ENTRY;
177
3.72k
    }
178
179
    /*  Logic should work for DW4 and DW5. */
180
0
    if (context->cc_unit_type == DW_UT_type||
181
0
        context->cc_unit_type == DW_UT_split_type ) {
182
0
        if (!_dwarf_file_has_debug_fission_tu_index(dbg)) {
183
0
            return DW_DLV_NO_ENTRY;
184
0
        }
185
0
    } else if (context->cc_unit_type == DW_UT_split_compile) {
186
0
        if (!_dwarf_file_has_debug_fission_cu_index(dbg)) {
187
0
            return DW_DLV_NO_ENTRY;
188
0
        }
189
0
    } else { /* Fall through*/ }
190
0
    percu = &context->cc_dwp_offsets;
191
0
    if (!percu->pcu_type) {
192
0
        return DW_DLV_NO_ENTRY;
193
0
    }
194
0
    *fission_out = *percu;
195
0
    return DW_DLV_OK;
196
0
}
197
198
static Dwarf_Bool
199
is_unknown_UT_value(int ut)
200
17.5k
{
201
17.5k
    switch(ut) {
202
4.71k
    case DW_UT_compile:
203
5.22k
    case DW_UT_type:
204
9.67k
    case DW_UT_partial:
205
9.67k
        return FALSE;
206
1.90k
    case DW_UT_skeleton:
207
6.62k
    case DW_UT_split_compile:
208
7.82k
    case DW_UT_split_type:
209
7.82k
        return FALSE;
210
70
    default:
211
70
        break;
212
17.5k
    }
213
70
    return TRUE;
214
17.5k
}
215
216
/*  ASSERT: whichone is a DW_SECT* macro value. */
217
Dwarf_Unsigned
218
_dwarf_get_dwp_extra_offset(struct Dwarf_Debug_Fission_Per_CU_s* dwp,
219
    unsigned whichone, Dwarf_Unsigned * size)
220
10.3k
{
221
10.3k
    Dwarf_Unsigned sectoff = 0;
222
10.3k
    if (!dwp->pcu_type) {
223
10.2k
        return 0;
224
10.2k
    }
225
110
    sectoff = dwp->pcu_offset[whichone];
226
110
    *size = dwp->pcu_size[whichone];
227
110
    return sectoff;
228
10.3k
}
229
230
/*  _dwarf_get_fission_addition_die returns DW_DLV_OK etc.
231
*/
232
int
233
_dwarf_get_fission_addition_die(Dwarf_Die die, int dw_sect_index,
234
    Dwarf_Unsigned *offset,
235
    Dwarf_Unsigned *size,
236
    Dwarf_Error *error)
237
10.2k
{
238
    /* We do not yet know the DIE hash, so we cannot use it
239
        to identify the offset. */
240
10.2k
    Dwarf_CU_Context context = 0;
241
10.2k
    Dwarf_Unsigned dwpadd = 0;
242
10.2k
    Dwarf_Unsigned dwpsize = 0;
243
244
10.2k
    CHECK_DIE(die, DW_DLV_ERROR);
245
10.2k
    context = die->di_cu_context;
246
10.2k
    dwpadd =  _dwarf_get_dwp_extra_offset(
247
10.2k
        &context->cc_dwp_offsets,
248
10.2k
        dw_sect_index,&dwpsize);
249
10.2k
    *offset = dwpadd;
250
10.2k
    *size = dwpsize;
251
10.2k
    return DW_DLV_OK;
252
10.2k
}
253
254
/*  Not sure if this is the only way to be sure early on in
255
    reading a compile unit.  */
256
static int
257
section_name_ends_with_dwo(const char *name)
258
42.8k
{
259
42.8k
    size_t lenstr = 0;
260
42.8k
    size_t dotpos = 0;
261
42.8k
    if (!name) {
262
0
        return FALSE;
263
0
    }
264
42.8k
    lenstr = strlen(name);
265
42.8k
    if (lenstr < 5) {
266
0
        return FALSE;
267
0
    }
268
42.8k
    dotpos = lenstr - 4;
269
42.8k
    if (strcmp(name+dotpos,".dwo")) {
270
31.1k
        return FALSE;
271
31.1k
    }
272
11.6k
    return TRUE;
273
42.8k
}
274
275
void
276
_dwarf_create_address_size_dwarf_error(Dwarf_Debug dbg,
277
    Dwarf_Error *error,
278
    Dwarf_Unsigned addrsize,
279
    int errcode,const char *errname)
280
125
{
281
125
    dwarfstring m;
282
125
    const char *bites = "bytes";
283
125
    if (addrsize == 1) {
284
13
        bites = "byte";
285
13
    }
286
287
125
    dwarfstring_constructor(&m);
288
125
    dwarfstring_append(&m,(char *)errname);
289
125
    dwarfstring_append_printf_u(&m,
290
125
        ": Address size of %u ",
291
125
        addrsize);
292
125
    dwarfstring_append_printf_s(&m,
293
125
        "%s is not supported. Corrupt DWARF.",
294
125
        (char *)bites);
295
125
    _dwarf_error_string(dbg,error,errcode,
296
125
        dwarfstring_string(&m));
297
125
    dwarfstring_destructor(&m);
298
125
}
299
300
/*  New January 2017 */
301
static int
302
_dwarf_read_cu_version_and_abbrev_offset(Dwarf_Debug dbg,
303
    Dwarf_Small *data,
304
    Dwarf_Bool is_info,
305
    unsigned offset_size, /* 4 or 8 */
306
    Dwarf_CU_Context cu_context,
307
    /* end_data used for sanity checking */
308
    Dwarf_Small *    end_data,
309
    Dwarf_Unsigned * bytes_read_out,
310
    Dwarf_Error *    error)
311
40.3k
{
312
40.3k
    Dwarf_Small *  data_start = data;
313
40.3k
    Dwarf_Small *  dataptr = data;
314
40.3k
    Dwarf_Ubyte    unit_type = 0;
315
40.3k
    Dwarf_Ubyte    addrsize =  0;
316
40.3k
    Dwarf_Unsigned abbrev_offset = 0;
317
40.3k
    Dwarf_Half version = 0;
318
319
40.3k
    READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
320
40.3k
        dataptr,DWARF_HALF_SIZE,error,end_data);
321
40.3k
    dataptr += DWARF_HALF_SIZE;
322
40.3k
    if (version == DW_CU_VERSION5) {
323
17.5k
        Dwarf_Ubyte unit_typeb = 0;
324
17.5k
        Dwarf_Unsigned herelen = sizeof(unit_typeb) +
325
17.5k
            sizeof(addrsize) + offset_size;
326
327
17.5k
        if ((dataptr+herelen) > end_data) {
328
0
            _dwarf_error_string(dbg, error,
329
0
            DW_DLE_CU_UT_TYPE_ERROR,
330
0
            "DW_DLE_UT_TYPE_ERROR: "
331
0
            " Reading the unit type, address size, "
332
0
            "and abbrev_offset of the DWARF5 header"
333
0
            " will run off the end of the section. "
334
0
            "Corrupt DWARF");
335
0
        }
336
17.5k
        READ_UNALIGNED_CK(dbg, unit_typeb, Dwarf_Ubyte,
337
17.5k
            dataptr, sizeof(unit_typeb),error,end_data);
338
17.5k
        dataptr += sizeof(unit_typeb);
339
340
17.5k
        unit_type = unit_typeb;
341
        /* We do not need is_info flag in DWARF5 */
342
17.5k
        if (is_unknown_UT_value(unit_type)) {
343
            /*  DWARF5 object file is corrupt. Invalid value */
344
70
            dwarfstring m;
345
70
            dwarfstring_constructor(&m);
346
70
            dwarfstring_append_printf_u(&m,
347
70
                "DW_DLE_CU_UT_TYPE_ERROR: we do not know "
348
70
                " the CU header unit_type 0x%x",unit_type);
349
70
            dwarfstring_append_printf_u(&m," (%u) so cannot"
350
70
                "process this compilation_unit. A valid type ",
351
70
                unit_type);
352
70
            dwarfstring_append(&m,"would be DW_UT_compile"
353
70
                ", for example");
354
70
            _dwarf_error_string(dbg, error,
355
70
                DW_DLE_CU_UT_TYPE_ERROR,
356
70
                dwarfstring_string(&m));
357
70
            dwarfstring_destructor(&m);
358
70
            return DW_DLV_ERROR;
359
70
        }
360
17.5k
        READ_UNALIGNED_CK(dbg, addrsize, unsigned char,
361
17.5k
            dataptr, sizeof(addrsize),error,end_data);
362
17.5k
        dataptr += sizeof(char);
363
364
17.5k
        READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
365
17.5k
            dataptr, offset_size,error,end_data);
366
17.5k
        dataptr += offset_size;
367
368
22.8k
    } else if (version == DW_CU_VERSION2 ||
369
22.8k
        version == DW_CU_VERSION3 ||
370
22.8k
        version == DW_CU_VERSION4) {
371
22.4k
        Dwarf_Unsigned herelen = sizeof(addrsize) + offset_size;
372
373
22.4k
        if ((dataptr+herelen) > end_data) {
374
0
            _dwarf_error_string(dbg, error,
375
0
            DW_DLE_CU_UT_TYPE_ERROR,
376
0
            "DW_DLE_UT_TYPE_ERROR: "
377
0
            " Reading the address size, "
378
0
            "and abbrev_offset of the DWARF header"
379
0
            " will run off the end of the section. "
380
0
            "Corrupt DWARF");
381
0
        }
382
        /*  DWARF2,3,4  */
383
22.4k
        READ_UNALIGNED_CK(dbg, abbrev_offset, Dwarf_Unsigned,
384
22.4k
            dataptr, offset_size,error,end_data);
385
22.4k
        dataptr += offset_size;
386
387
22.4k
        READ_UNALIGNED_CK(dbg, addrsize, Dwarf_Ubyte,
388
22.4k
            dataptr, sizeof(addrsize),error,end_data);
389
22.4k
        dataptr += sizeof(addrsize);
390
391
        /*  This is an initial approximation of unit_type.
392
            For DW4 we will refine this after we
393
            have built the CU header (by reading
394
            CU_die)
395
        */
396
22.4k
        unit_type = is_info?DW_UT_compile:DW_UT_type;
397
22.4k
    } else {
398
359
        _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
399
359
        return DW_DLV_ERROR;
400
359
    }
401
39.9k
    cu_context->cc_version_stamp = version;
402
39.9k
    cu_context->cc_unit_type     = unit_type;
403
39.9k
    cu_context->cc_address_size  = addrsize;
404
39.9k
    cu_context->cc_abbrev_offset = abbrev_offset;
405
39.9k
    if (!addrsize) {
406
21
        _dwarf_error(dbg,error,DW_DLE_ADDRESS_SIZE_ZERO);
407
21
        return DW_DLV_ERROR;
408
21
    }
409
39.9k
    if (addrsize < MINIMUM_ADDRESS_SIZE ||
410
39.9k
        addrsize > MAXIMUM_ADDRESS_SIZE ) {
411
106
        _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
412
106
            DW_DLE_ADDRESS_SIZE_ERROR,
413
106
            "DW_DLE_ADDRESS_SIZE_ERROR::");
414
106
        return DW_DLV_ERROR;
415
106
    }
416
39.8k
    if (addrsize  > sizeof(Dwarf_Addr)) {
417
0
        _dwarf_create_address_size_dwarf_error(dbg,error,addrsize,
418
0
            DW_DLE_ADDRESS_SIZE_ERROR,
419
0
            "DW_DLE_ADDRESS_SIZE_ERROR: not representable"
420
0
            " in Dwarf_Addr field.");
421
0
        return DW_DLV_ERROR;
422
0
    }
423
424
    /* We are ignoring this. Can get it from DWARF5. */
425
39.8k
    cu_context->cc_segment_selector_size = 0;
426
39.8k
    *bytes_read_out = (dataptr - data_start);
427
39.8k
    return DW_DLV_OK;
428
39.8k
}
429
430
/*  .debug_info[.dwo]   .debug_types[.dwo]
431
    the latter only DWARF4. */
432
static int
433
read_info_area_length_and_check(Dwarf_Debug dbg,
434
    Dwarf_CU_Context cu_context,
435
    Dwarf_Unsigned offset,
436
    Dwarf_Byte_Ptr *cu_ptr_io,
437
    Dwarf_Unsigned section_size,
438
    Dwarf_Byte_Ptr section_end_ptr,
439
    Dwarf_Unsigned *max_cu_global_offset_out,
440
    Dwarf_Error *error)
441
42.8k
{
442
42.8k
    Dwarf_Byte_Ptr cu_ptr = 0;
443
    /*  The following two will be either 0,4, or 8. */
444
42.8k
    Dwarf_Unsigned local_length_size = 0;
445
42.8k
    Dwarf_Unsigned local_extension_size = 0;
446
447
42.8k
    Dwarf_Unsigned max_cu_global_offset = 0;
448
42.8k
    Dwarf_Unsigned length = 0;
449
450
42.8k
    cu_ptr = *cu_ptr_io;
451
    /* READ_AREA_LENGTH updates cu_ptr for consumed bytes */
452
42.8k
    READ_AREA_LENGTH_CK(dbg, length, Dwarf_Unsigned,
453
42.8k
        cu_ptr, local_length_size, local_extension_size,
454
42.8k
        error,section_size,section_end_ptr);
455
40.7k
    if (!length) {
456
292
        return DW_DLV_NO_ENTRY;
457
292
    }
458
459
    /* ASSERT: The following is either  4 or 8. */
460
40.4k
    cu_context->cc_length_size =    (Dwarf_Small)local_length_size;
461
    /* ASSERT: The following is either  0 or 4. */
462
40.4k
    cu_context->cc_extension_size = (Dwarf_Small)local_extension_size;
463
40.4k
    cu_context->cc_length = length;
464
465
    /*  This is a bare minimum, not the real max offset.
466
        A preliminary sanity check. */
467
40.4k
    max_cu_global_offset =  offset + length +
468
40.4k
        local_extension_size + local_length_size;
469
40.4k
    if (length > section_size ||
470
40.4k
        (length+local_length_size + local_extension_size)>
471
40.4k
        section_size) {
472
44
        _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
473
44
        return DW_DLV_ERROR;
474
44
    }
475
40.4k
    if (max_cu_global_offset > section_size) {
476
17
        _dwarf_error(dbg, error, DW_DLE_CU_LENGTH_ERROR);
477
17
        return DW_DLV_ERROR;
478
17
    }
479
40.3k
    *cu_ptr_io = cu_ptr;
480
40.3k
    *max_cu_global_offset_out = max_cu_global_offset;
481
40.3k
    return DW_DLV_OK;
482
40.4k
}
483
484
/*  In DWARF4  GNU dwp there is a problem.
485
    We cannot read the CU die  and it's
486
    DW_AT_GNU_dwo_id until we know the
487
    section offsets from the index files.
488
    Hence we do not know how to search the
489
    index files by key. So search by offset.
490
491
    There is no such problem in DWARF5.
492
493
    We have not yet corrected the unit_type so, for DWARF4,
494
    we check for simpler unit types.
495
*/
496
497
static int
498
fill_in_dwp_offsets_if_present(Dwarf_Debug dbg,
499
    Dwarf_CU_Context cu_context,
500
    Dwarf_Sig8 * signaturedata,
501
    Dwarf_Off    offset,
502
    Dwarf_Error *error)
503
38.9k
{
504
38.9k
    Dwarf_Half unit_type = cu_context->cc_unit_type;
505
38.9k
    const char * typename = 0;
506
38.9k
    Dwarf_Half ver = cu_context->cc_version_stamp;
507
508
38.9k
    if (unit_type == DW_UT_split_type ||
509
38.9k
        (ver == DW_CU_VERSION4 && unit_type == DW_UT_type)){
510
13.7k
        typename = "tu";
511
13.7k
        if (!_dwarf_file_has_debug_fission_tu_index(dbg) ){
512
            /* nothing to do. */
513
13.4k
            return DW_DLV_OK;
514
13.4k
        }
515
25.2k
    } else if (unit_type == DW_UT_split_compile ||
516
25.2k
        (ver == DW_CU_VERSION4 &&
517
20.5k
        unit_type == DW_UT_compile)){
518
5.14k
        typename = "cu";
519
5.14k
        if (!_dwarf_file_has_debug_fission_cu_index(dbg) ){
520
            /* nothing to do. */
521
4.95k
            return DW_DLV_OK;
522
4.95k
        }
523
20.0k
    } else {
524
        /* nothing to do. */
525
20.0k
        return DW_DLV_OK;
526
20.0k
    }
527
528
426
    if (cu_context->cc_signature_present) {
529
389
        int resdf = 0;
530
531
389
        resdf = dwarf_get_debugfission_for_key(dbg,
532
389
            signaturedata,
533
389
            typename,
534
389
            &cu_context->cc_dwp_offsets,
535
389
            error);
536
389
        if (resdf == DW_DLV_ERROR) {
537
182
            return resdf;
538
182
        }
539
207
        if (resdf == DW_DLV_NO_ENTRY) {
540
128
            _dwarf_error_string(dbg, error,
541
128
                DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
542
128
                "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
543
128
                " dwarf_get_debugfission_for_key returned"
544
128
                " DW_DLV_NO_ENTRY, something is wrong");
545
128
            return DW_DLV_ERROR;
546
128
        }
547
207
    } else {
548
37
        int resdf = 0;
549
550
37
        resdf = _dwarf_get_debugfission_for_offset(dbg,
551
37
            offset,
552
37
            typename,
553
37
            &cu_context->cc_dwp_offsets,
554
37
            error);
555
37
        if (resdf == DW_DLV_ERROR) {
556
17
            return resdf;
557
17
        }
558
20
        if (resdf == DW_DLV_NO_ENTRY) {
559
10
            _dwarf_error_string(dbg, error,
560
10
                DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH,
561
10
                "DW_DLE_MISSING_REQUIRED_CU_OFFSET_HASH: "
562
10
                " dwarf_get_debugfission_for_offset returned"
563
10
                " DW_DLV_NO_ENTRY, something is wrong");
564
10
            return DW_DLV_ERROR;
565
10
        }
566
10
        cu_context->cc_signature =
567
10
            cu_context->cc_dwp_offsets.pcu_hash;
568
10
        cu_context->cc_signature_present = TRUE;
569
10
    }
570
89
    return DW_DLV_OK;
571
426
}
572
573
/*  If returning DW_DLV_OK this will
574
    push the cudie pointer back up through
575
    local_cudie_return if local_cudie_return
576
    is non-null. */
577
static int
578
finish_cu_context_via_cudie_inner(
579
    Dwarf_Debug dbg,
580
    Dwarf_CU_Context cu_context,
581
    Dwarf_Die *local_cudie_return,
582
    Dwarf_Error *error)
583
37.9k
{
584
    /*  DW4: Look for DW_AT_dwo_id and
585
        DW_AT_low_pc and more.
586
        if there is one pick up the hash
587
        DW5: hash in skeleton CU die
588
        Also pick up cc_str_offset_base and
589
        any other base values. */
590
37.9k
    Dwarf_Die cudie = 0;
591
37.9k
    int resdwo = 0;
592
593
    /*  Must call the internal siblingof so
594
        we do not depend on the dbg...de_cu_context
595
        used by and for dwarf_cu_header_* calls.
596
        Safe because we know the correct cu_context.  */
597
37.9k
    resdwo = _dwarf_siblingof_internal(dbg,NULL,
598
37.9k
        cu_context,
599
37.9k
        cu_context->cc_is_info,
600
37.9k
        &cudie, error);
601
37.9k
    if (resdwo == DW_DLV_OK) {
602
30.1k
        Dwarf_Half cutag = 0;
603
30.1k
        int resdwob = 0;
604
30.1k
        resdwob = find_cu_die_base_fields(dbg,
605
30.1k
            cu_context,
606
30.1k
            cudie,
607
30.1k
            error);
608
30.1k
        if (resdwob == DW_DLV_NO_ENTRY) {
609
            /* The CU die has no children */
610
2.04k
            if (local_cudie_return) {
611
1.81k
                *local_cudie_return = cudie;
612
1.81k
            } else {
613
235
                dwarf_dealloc_die(cudie);
614
235
            }
615
2.04k
            cu_context->cc_cu_die_has_children = FALSE;
616
2.04k
            return DW_DLV_OK;
617
2.04k
        }
618
28.1k
        if (resdwob == DW_DLV_ERROR) {
619
            /*  Not applicable or an error */
620
9.67k
            dwarf_dealloc_die(cudie);
621
9.67k
            cudie = 0;
622
9.67k
            return resdwob;
623
9.67k
        }
624
18.4k
        resdwob = dwarf_tag(cudie,&cutag,error);
625
18.4k
        if (resdwob == DW_DLV_OK) {
626
18.4k
            cu_context->cc_cu_die_tag = cutag;
627
18.4k
        }
628
18.4k
        if (local_cudie_return) {
629
17.5k
            *local_cudie_return = cudie;
630
17.5k
        } else {
631
930
            dwarf_dealloc_die(cudie);
632
930
        }
633
18.4k
        return resdwob;
634
28.1k
    }
635
7.76k
    if (resdwo == DW_DLV_NO_ENTRY) {
636
        /* no cudie. Empty CU. */
637
1.19k
        return DW_DLV_OK;
638
1.19k
    }
639
    /* no cudie. DW_DLV_ERROR.*/
640
6.57k
    return resdwo;
641
7.76k
}
642
643
static void
644
local_dealloc_cu_context(Dwarf_Debug dbg,
645
    Dwarf_CU_Context context)
646
21.1k
{
647
21.1k
    Dwarf_Hash_Table hash_table = 0;
648
649
21.1k
    if (!context) {
650
0
        return;
651
0
    }
652
21.1k
    hash_table = context->cc_abbrev_hash_table;
653
21.1k
    if (hash_table) {
654
17.2k
        _dwarf_free_abbrev_hash_table_contents(hash_table,
655
17.2k
            FALSE);
656
17.2k
        hash_table->tb_entries = 0;
657
17.2k
        free(hash_table);
658
17.2k
        context->cc_abbrev_hash_table = 0;
659
17.2k
    }
660
21.1k
    dwarf_dealloc(dbg, context, DW_DLA_CU_CONTEXT);
661
21.1k
}
662
663
static void
664
report_local_unit_type_error(Dwarf_Debug dbg,
665
    int unit_type,
666
    const char *msg,
667
    Dwarf_Error *error)
668
0
{
669
0
    dwarfstring m;
670
671
0
    if (!error) {
672
0
        return;
673
0
    }
674
0
    dwarfstring_constructor(&m);
675
0
    dwarfstring_append_printf_s(&m,
676
0
        "DW_DLE_CU_UT_TYPE_VALUE: %s ",(char *)msg);
677
0
    dwarfstring_append_printf_u(&m,
678
0
        "the compilation unit unit_type is 0x%x,"
679
0
        " which is unknown to libdwarf. Corrupt DWARF.",
680
0
        unit_type);
681
0
    _dwarf_error_string(dbg,error,DW_DLE_CU_UT_TYPE_VALUE,
682
0
        dwarfstring_string(&m));
683
0
    dwarfstring_destructor(&m);
684
0
}
685
686
/*  This function is used to create a CU Context for
687
    a compilation-unit that begins at offset in
688
    .debug_info.  The CU Context is attached to the
689
    list of CU Contexts for this dbg.  It is assumed
690
    that the CU at offset has not been read before,
691
    and so do not call this routine before making
692
    sure of this with _dwarf_find_CU_Context().
693
    Returns NULL on error.  As always, being an
694
    internal routine, assumes a good dbg.
695
696
    The offset argument is global offset, the offset
697
    in the section, irrespective of CUs.
698
    The offset has the DWP Package File offset built in
699
    as it comes from the actual section.
700
701
    max_cu_local_offset is a local offset in this CU.
702
    So zero of this field is immediately following the length
703
    field of the CU header. so max_cu_local_offset is
704
    identical to the CU length field.
705
    max_cu_global_offset is the offset one-past the end
706
    of this entire CU.  */
707
static int
708
_dwarf_make_CU_Context(Dwarf_Debug dbg,
709
    Dwarf_Off offset,Dwarf_Bool is_info,
710
    Dwarf_CU_Context * context_out,Dwarf_Error * error)
711
42.8k
{
712
42.8k
    Dwarf_CU_Context cu_context = 0;
713
42.8k
    Dwarf_Unsigned   length = 0;
714
42.8k
    Dwarf_Unsigned   typeoffset = 0;
715
42.8k
    Dwarf_Sig8       signaturedata;
716
42.8k
    Dwarf_Unsigned   types_extra_len = 0;
717
42.8k
    Dwarf_Unsigned   max_cu_local_offset =  0;
718
42.8k
    Dwarf_Unsigned   max_cu_global_offset =  0;
719
42.8k
    Dwarf_Byte_Ptr   cu_ptr = 0;
720
42.8k
    Dwarf_Byte_Ptr   section_end_ptr = 0;
721
42.8k
    int              local_length_size = 0;
722
42.8k
    Dwarf_Unsigned   bytes_read = 0;
723
42.8k
    const char *     secname = 0;
724
42.8k
    Dwarf_Debug_InfoTypes dis = 0;
725
42.8k
    struct Dwarf_Section_s * secdp = 0;
726
42.8k
    Dwarf_Unsigned   section_size = 0;
727
42.8k
    Dwarf_Half       unit_type = 0;
728
42.8k
    Dwarf_Unsigned   version = 0;
729
42.8k
    Dwarf_Small *    dataptr = 0;
730
42.8k
    int              res = 0;
731
42.8k
    if (is_info) {
732
2.74k
        secname = dbg->de_debug_info.dss_name;
733
2.74k
        dis     = &dbg->de_info_reading;
734
2.74k
        secdp   = &dbg->de_debug_info;
735
40.1k
    } else {
736
40.1k
        secname = dbg->de_debug_types.dss_name;
737
40.1k
        dis =     &dbg->de_types_reading;
738
40.1k
        secdp   = &dbg->de_debug_types;
739
40.1k
    }
740
42.8k
    section_size = secdp->dss_size;
741
742
42.8k
    signaturedata = dwarfsig8zero;
743
42.8k
    cu_context =
744
42.8k
        (Dwarf_CU_Context)_dwarf_get_alloc(dbg, DW_DLA_CU_CONTEXT, 1);
745
42.8k
    if (!cu_context) {
746
0
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
747
0
        return DW_DLV_ERROR;
748
0
    }
749
42.8k
    cu_context->cc_dbg = dbg;
750
42.8k
    cu_context->cc_is_info = is_info;
751
752
42.8k
    dataptr = is_info? dbg->de_debug_info.dss_data:
753
42.8k
        dbg->de_debug_types.dss_data;
754
    /*  Preliminary sanity checking. */
755
42.8k
    if (!dataptr) {
756
17
        local_dealloc_cu_context(dbg,cu_context);
757
17
        _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
758
17
        return DW_DLV_ERROR;
759
17
    }
760
42.8k
    if (offset >= section_size) {
761
0
        local_dealloc_cu_context(dbg,cu_context);
762
0
        _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
763
0
        return DW_DLV_ERROR;
764
0
    }
765
42.8k
    if ((offset+4) > section_size) {
766
0
        local_dealloc_cu_context(dbg,cu_context);
767
0
        _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
768
0
        return DW_DLV_ERROR;
769
0
    }
770
42.8k
    section_end_ptr = dataptr+section_size;
771
42.8k
    cu_ptr = (Dwarf_Byte_Ptr) (dataptr+offset);
772
773
42.8k
    if (section_name_ends_with_dwo(secname)) {
774
11.6k
        cu_context->cc_is_dwo = TRUE;
775
11.6k
    }
776
42.8k
    res = read_info_area_length_and_check(dbg,
777
42.8k
        cu_context,
778
42.8k
        offset,
779
42.8k
        &cu_ptr,
780
42.8k
        section_size,
781
42.8k
        section_end_ptr,
782
42.8k
        &max_cu_global_offset,
783
42.8k
        error);
784
42.8k
    if (res != DW_DLV_OK) {
785
2.46k
        local_dealloc_cu_context(dbg,cu_context);
786
2.46k
        return res;
787
2.46k
    }
788
40.3k
    local_length_size = cu_context->cc_length_size;
789
40.3k
    length = cu_context->cc_length;
790
40.3k
    max_cu_local_offset =  length;
791
40.3k
    res  = _dwarf_read_cu_version_and_abbrev_offset(dbg,
792
40.3k
        cu_ptr,
793
40.3k
        is_info,
794
40.3k
        local_length_size,
795
40.3k
        cu_context,
796
40.3k
        section_end_ptr,
797
40.3k
        &bytes_read,error);
798
40.3k
    if (res != DW_DLV_OK) {
799
556
        local_dealloc_cu_context(dbg,cu_context);
800
556
        return res;
801
556
    }
802
39.8k
    version = cu_context->cc_version_stamp;
803
39.8k
    cu_ptr += bytes_read;
804
39.8k
    unit_type = cu_context->cc_unit_type;
805
39.8k
    if (cu_ptr > section_end_ptr) {
806
0
        local_dealloc_cu_context(dbg,cu_context);
807
0
        _dwarf_error(dbg, error, DW_DLE_INFO_HEADER_ERROR);
808
0
        return DW_DLV_ERROR;
809
0
    }
810
811
    /*  In a dwp context, the abbrev_offset is
812
        still  incomplete.
813
        We need to add in the base from the .debug_cu_index
814
        or .debug_tu_index . Done below */
815
816
    /*  At this point, for DW4, the unit_type is not fully
817
        correct as we don't know if it is a skeleton or
818
        a split_compile or split_type */
819
39.8k
    if (version ==  DW_CU_VERSION5 ||
820
39.8k
        version == DW_CU_VERSION4) {
821
        /*  DW4/DW5  header fields, depending on UT type.
822
            See DW5  section 7.5.1.x, DW4
823
            data is a GNU extension of DW4. */
824
30.7k
        switch(unit_type) {
825
1.19k
        case DW_UT_split_type:
826
14.5k
        case DW_UT_type: {
827
14.5k
            types_extra_len = sizeof(Dwarf_Sig8) /* 8 */ +
828
14.5k
                local_length_size /*type_offset size*/;
829
14.5k
            break;
830
1.19k
        }
831
1.90k
        case DW_UT_skeleton:
832
6.61k
        case DW_UT_split_compile: {
833
6.61k
            types_extra_len = sizeof(Dwarf_Sig8) /* 8 */;
834
6.61k
            break;
835
1.90k
        }
836
5.12k
        case DW_UT_compile: /*  No additional fields */
837
9.57k
        case DW_UT_partial: /*  No additional fields */
838
9.57k
            break;
839
0
        default:
840
            /*  Data corruption in libdwarf? */
841
0
            report_local_unit_type_error(dbg, unit_type,
842
0
                "(DW4 or DW5)",error);
843
0
            local_dealloc_cu_context(dbg,cu_context);
844
0
            return DW_DLV_ERROR;
845
30.7k
        }
846
30.7k
    }
847
848
    /*  Compare the space following the length field
849
        to the bytes in the CU header. */
850
39.8k
    if (length <
851
39.8k
        (CU_VERSION_STAMP_SIZE /* is 2 */ +
852
39.8k
        local_length_size /*for debug_abbrev offset */ +
853
39.8k
        CU_ADDRESS_SIZE_SIZE /* is 1 */ +
854
        /* and finally size of the rest of the header: */
855
39.8k
        types_extra_len)) {
856
857
15
        local_dealloc_cu_context(dbg,cu_context);
858
15
        _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
859
15
            "DW_DLE_CU_LENGTH_ERROR: reading version "
860
15
            "stamp and address size fields");
861
15
        return DW_DLV_ERROR;
862
15
    }
863
    /*  Now we can read the fields with some confidence,
864
        we know the fields of the header are inside
865
        the section. */
866
867
39.8k
    cu_context->cc_unit_type = unit_type;
868
39.8k
    switch(unit_type) {
869
1.19k
    case DW_UT_split_type:
870
23.3k
    case DW_UT_type: {
871
23.3k
        int tres = 0;
872
        /*  ASSERT: DW_CU_VERSION4 or DW_CU_VERSION5,
873
            determined by logic above.
874
            Now read the debug_types extra header fields of
875
            the signature (8 bytes) and the typeoffset.
876
            This can be in executable, ordinary object
877
            (as in Type Unit),
878
            there was no dwo in DWARF4
879
        */
880
23.3k
        if ((cu_ptr + sizeof(signaturedata)) > section_end_ptr) {
881
3
            _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
882
3
                "DW_DLE_CU_LENGTH_ERROR: reading "
883
3
                "Dwarf_Sig8 signature field");
884
3
            local_dealloc_cu_context(dbg,cu_context);
885
3
            return DW_DLV_ERROR;
886
3
        }
887
23.3k
        memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
888
23.3k
        cu_ptr += sizeof(signaturedata);
889
23.3k
        tres = _dwarf_read_unaligned_ck_wrapper(dbg,
890
23.3k
            &typeoffset,cu_ptr,local_length_size,
891
23.3k
            section_end_ptr,error);
892
23.3k
        if (tres != DW_DLV_OK ) {
893
8
            local_dealloc_cu_context(dbg,cu_context);
894
8
            return tres;
895
8
        }
896
23.3k
        cu_context->cc_signature = signaturedata;
897
23.3k
        cu_context->cc_signature_present = TRUE;
898
23.3k
        cu_context->cc_signature_offset = typeoffset;
899
23.3k
        if (typeoffset >= max_cu_local_offset) {
900
831
            local_dealloc_cu_context(dbg,cu_context);
901
831
            _dwarf_error(dbg, error,
902
831
                DW_DLE_DEBUG_TYPEOFFSET_BAD);
903
831
            return DW_DLV_ERROR;
904
831
        }
905
23.3k
        }
906
22.5k
        break;
907
22.5k
    case DW_UT_skeleton:
908
6.61k
    case DW_UT_split_compile: {
909
6.61k
        if ((cu_ptr + sizeof(signaturedata)) > section_end_ptr) {
910
2
            _dwarf_error_string(dbg, error, DW_DLE_CU_LENGTH_ERROR,
911
2
                "DW_DLE_CU_LENGTH_ERROR: reading "
912
2
                "Dwarf_Sig8 signature field");
913
2
            local_dealloc_cu_context(dbg,cu_context);
914
2
            return DW_DLV_ERROR;
915
2
        }
916
        /*  These unit types make a pair and
917
            paired units have identical signature.*/
918
6.61k
        memcpy(&signaturedata,cu_ptr,sizeof(signaturedata));
919
6.61k
        cu_context->cc_signature = signaturedata;
920
6.61k
        cu_context->cc_signature_present = TRUE;
921
922
6.61k
        break;
923
6.61k
        }
924
    /* The following with no additional fields */
925
5.39k
    case DW_UT_compile:
926
9.83k
    case DW_UT_partial:
927
9.83k
        break;
928
0
    default: {
929
        /*  Data corruption in libdwarf? */
930
0
        report_local_unit_type_error(dbg, unit_type,
931
0
            "",error);
932
0
        local_dealloc_cu_context(dbg,cu_context);
933
0
        return DW_DLV_ERROR;
934
5.39k
        }
935
39.8k
    }
936
38.9k
    cu_context->cc_abbrev_hash_table =
937
38.9k
        (Dwarf_Hash_Table) calloc(1,
938
38.9k
        sizeof(struct Dwarf_Hash_Table_s));
939
38.9k
    if (!cu_context->cc_abbrev_hash_table) {
940
0
        local_dealloc_cu_context(dbg,cu_context);
941
0
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
942
0
        return DW_DLV_ERROR;
943
0
    }
944
38.9k
    cu_context->cc_debug_offset = offset;
945
946
    /*  This is recording an overall section value for later
947
        sanity checking. */
948
38.9k
    dis->de_last_offset = max_cu_global_offset;
949
38.9k
    *context_out  = cu_context;
950
38.9k
    return DW_DLV_OK;
951
38.9k
}
952
953
static int
954
reloc_incomplete(int res,Dwarf_Error err)
955
3.24k
{
956
3.24k
    Dwarf_Unsigned e = 0;
957
958
3.24k
    if (res == DW_DLV_OK) {
959
0
        return FALSE;
960
0
    }
961
3.24k
    if (res == DW_DLV_NO_ENTRY) {
962
0
        return FALSE;
963
0
    }
964
3.24k
    e = dwarf_errno(err);
965
3.24k
    switch(e) {
966
0
    case DW_DLE_RELOC_MISMATCH_INDEX:
967
31
    case DW_DLE_RELOC_MISMATCH_RELOC_INDEX:
968
31
    case DW_DLE_RELOC_MISMATCH_STRTAB_INDEX:
969
31
    case DW_DLE_RELOC_SECTION_MISMATCH:
970
31
    case DW_DLE_RELOC_SECTION_MISSING_INDEX:
971
31
    case DW_DLE_RELOC_SECTION_LENGTH_ODD:
972
31
    case DW_DLE_RELOC_SECTION_PTR_NULL:
973
31
    case DW_DLE_RELOC_SECTION_MALLOC_FAIL:
974
42
    case DW_DLE_SEEK_OFF_END:
975
274
    case DW_DLE_RELOC_INVALID:
976
588
    case DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD:
977
588
    case DW_DLE_ELF_RELOC_SECTION_ERROR:
978
588
    case DW_DLE_RELOCATION_SECTION_SIZE_ERROR:
979
588
        return TRUE;
980
2.65k
    default: break;
981
3.24k
    }
982
2.65k
    return FALSE;
983
3.24k
}
984
985
/*  Returns offset of next compilation-unit thru next_cu_offset
986
    pointer.
987
    It sequentially moves from one
988
    cu to the next.  The current cu is recorded
989
    internally by libdwarf. */
990
int
991
dwarf_next_cu_header_d(Dwarf_Debug dbg,
992
    Dwarf_Bool is_info,
993
    Dwarf_Unsigned * cu_header_length,
994
    Dwarf_Half * version_stamp,
995
    Dwarf_Unsigned * abbrev_offset,
996
    Dwarf_Half * address_size,
997
    Dwarf_Half * offset_size,
998
    Dwarf_Half * extension_size,
999
    Dwarf_Sig8 * signature,
1000
    Dwarf_Unsigned * typeoffset,
1001
    Dwarf_Unsigned * next_cu_offset,
1002
    Dwarf_Half * header_cu_type,
1003
    Dwarf_Error * error)
1004
43.4k
{
1005
43.4k
    Dwarf_Bool has_signature = FALSE;
1006
43.4k
    int res = 0;
1007
1008
43.4k
    res = _dwarf_next_cu_header_internal(dbg,
1009
43.4k
        is_info,
1010
43.4k
        NULL,
1011
43.4k
        cu_header_length,
1012
43.4k
        version_stamp,
1013
43.4k
        abbrev_offset,
1014
43.4k
        address_size,
1015
43.4k
        offset_size,
1016
43.4k
        extension_size,
1017
43.4k
        signature,
1018
43.4k
        &has_signature,
1019
43.4k
        typeoffset,
1020
43.4k
        next_cu_offset,
1021
43.4k
        header_cu_type,
1022
43.4k
        error);
1023
43.4k
    return res;
1024
43.4k
}
1025
int
1026
dwarf_next_cu_header_e(Dwarf_Debug dbg,
1027
    Dwarf_Bool is_info,
1028
    Dwarf_Die  * cu_die_out,
1029
    Dwarf_Unsigned * cu_header_length,
1030
    Dwarf_Half * version_stamp,
1031
    Dwarf_Unsigned * abbrev_offset,
1032
    Dwarf_Half * address_size,
1033
    Dwarf_Half * offset_size,
1034
    Dwarf_Half * extension_size,
1035
    Dwarf_Sig8 * signature,
1036
    Dwarf_Unsigned * typeoffset,
1037
    Dwarf_Unsigned * next_cu_offset,
1038
    Dwarf_Half * header_cu_type,
1039
    Dwarf_Error * error)
1040
5.11k
{
1041
5.11k
    Dwarf_Bool has_signature = FALSE;
1042
5.11k
    int res = 0;
1043
1044
5.11k
    res = _dwarf_next_cu_header_internal(dbg,
1045
5.11k
        is_info,
1046
5.11k
        cu_die_out,
1047
5.11k
        cu_header_length,
1048
5.11k
        version_stamp,
1049
5.11k
        abbrev_offset,
1050
5.11k
        address_size,
1051
5.11k
        offset_size,
1052
5.11k
        extension_size,
1053
5.11k
        signature,
1054
5.11k
        &has_signature,
1055
5.11k
        typeoffset,
1056
5.11k
        next_cu_offset,
1057
5.11k
        header_cu_type,
1058
5.11k
        error);
1059
5.11k
    return res;
1060
5.11k
}
1061
1062
static void
1063
local_attrlist_dealloc(Dwarf_Debug dbg,
1064
    Dwarf_Signed atcount,
1065
    Dwarf_Attribute *alist)
1066
25.9k
{
1067
25.9k
    Dwarf_Signed i = 0;
1068
1069
5.09M
    for ( ; i < atcount; ++i) {
1070
5.06M
        dwarf_dealloc(dbg,alist[i],DW_DLA_ATTR);
1071
5.06M
    }
1072
25.9k
    dwarf_dealloc(dbg,alist,DW_DLA_LIST);
1073
25.9k
}
1074
1075
static int
1076
_dwarf_setup_base_address(Dwarf_Debug dbg,
1077
    const char      *attrname,
1078
    Dwarf_Attribute  attr,
1079
    Dwarf_Signed     at_addr_base_attrnum,
1080
    Dwarf_CU_Context cucon,
1081
    Dwarf_Error     *error)
1082
6.20k
{
1083
6.20k
    int lres = 0;
1084
6.20k
    Dwarf_Half form = 0;
1085
1086
    /*  If the form is indexed, we better have
1087
        seen DW_AT_addr_base.! */
1088
6.20k
    lres = dwarf_whatform(attr,&form,error);
1089
6.20k
    if (lres != DW_DLV_OK) {
1090
0
        return lres;
1091
0
    }
1092
6.20k
    if (dwarf_addr_form_is_indexed(form)) {
1093
1.04k
        if (at_addr_base_attrnum < 0) {
1094
117
            dwarfstring m;
1095
1096
117
            dwarfstring_constructor(&m);
1097
117
            dwarfstring_append(&m,
1098
117
                "DW_DLE_ATTR_NO_CU_CONTEXT: The ");
1099
117
            dwarfstring_append(&m,(char *)attrname);
1100
117
            dwarfstring_append(&m," CU_DIE uses "
1101
117
                "an indexed attribute yet "
1102
117
                "DW_AT_addr_base is not in the CU DIE.");
1103
117
            _dwarf_error_string(dbg,error,
1104
117
                DW_DLE_ATTR_NO_CU_CONTEXT,
1105
117
                dwarfstring_string(&m));
1106
117
            dwarfstring_destructor(&m);
1107
117
            return DW_DLV_ERROR;
1108
117
        }
1109
1.04k
    }
1110
6.09k
    lres = dwarf_formaddr(attr,
1111
6.09k
        &cucon->cc_low_pc,error);
1112
6.09k
    if (lres == DW_DLV_OK) {
1113
        /*  Pretending low_pc (ie cu base address for loclists)
1114
            if it was DW_AT_entry_pc with no DW_AT_low_pc
1115
            Allowing DW_AT_entry_pc */
1116
3.73k
        cucon->cc_low_pc_present = TRUE;
1117
3.73k
    } else {
1118
        /* Something is badly wrong. */
1119
2.35k
        return lres;
1120
2.35k
    }
1121
3.73k
    return lres;
1122
6.09k
}
1123
1124
/*
1125
    For a DWP/DWO the base fields
1126
    of a CU are inherited from the skeleton.
1127
    DWARF5 section 3.1.3
1128
    "Split Full Compilation Unit Entries".
1129
*/
1130
static int
1131
find_cu_die_base_fields(Dwarf_Debug dbg,
1132
    Dwarf_CU_Context cucon,
1133
    Dwarf_Die cudie,
1134
    Dwarf_Error*    error)
1135
30.1k
{
1136
30.1k
    Dwarf_CU_Context  cu_context = 0;
1137
30.1k
    Dwarf_Attribute * alist = 0;
1138
30.1k
    Dwarf_Signed      atcount = 0;
1139
30.1k
    unsigned          version_stamp = 2;
1140
30.1k
    int               alres = 0;
1141
30.1k
    Dwarf_Signed      i = 0;
1142
30.1k
    Dwarf_Signed low_pc_attrnum = -1;
1143
30.1k
    Dwarf_Signed entry_pc_attrnum = -1;
1144
30.1k
    Dwarf_Signed at_addr_base_attrnum = -1;
1145
1146
30.1k
    cu_context = cudie->di_cu_context;
1147
30.1k
    version_stamp = cu_context->cc_version_stamp;
1148
1149
30.1k
    alres = dwarf_attrlist(cudie, &alist,
1150
30.1k
        &atcount,error);
1151
30.1k
    if (alres != DW_DLV_OK) {
1152
        /* Something is badly wrong. No attrlist! */
1153
4.20k
        return alres;
1154
4.20k
    }
1155
    /*  DW_AT_dwo_id and/or DW_AT_GNU_dwo_id
1156
        are only found  in some
1157
        experimental DWARF4.
1158
        Even DWARF3,4 use DW_AT_low_pc as base address
1159
        DWARF5 changed CU header contents
1160
        to make this attribute unnecessary.
1161
        DW_AT_GNU_odr_signature is the same format,
1162
        but is in a different namespace so not
1163
        appropriate here..
1164
    */
1165
3.69M
    for (i = 0;  i < atcount; ++i) {
1166
3.67M
        Dwarf_Half attrnum = 0;
1167
3.67M
        Dwarf_Half form = 0;
1168
3.67M
        int ares = 0;
1169
3.67M
        int ares2 = 0;
1170
3.67M
        Dwarf_Attribute attr = alist[i];
1171
1172
3.67M
        ares = dwarf_whatattr(attr,&attrnum,error);
1173
3.67M
        if (ares == DW_DLV_ERROR && error) {
1174
0
            dwarf_dealloc_error(dbg,*error);
1175
0
            *error = 0;
1176
0
        }
1177
3.67M
        ares2 = dwarf_whatform(attr,&form,error);
1178
3.67M
        if (ares2 == DW_DLV_ERROR && error) {
1179
0
            dwarf_dealloc_error(dbg,*error);
1180
0
            *error = 0;
1181
0
        }
1182
        /*  We are not returning on DW_DLV_NO_ENTRY
1183
            or DW_DLV_ERROR here. Such will be
1184
            caught later. Lets finish a CU die
1185
            scan and finish the cu_context  */
1186
3.67M
        if (ares == DW_DLV_OK && ares2 == DW_DLV_OK) {
1187
3.67M
            switch(form) {
1188
50.6k
            case DW_FORM_strx:
1189
132k
            case DW_FORM_strx1:
1190
193k
            case DW_FORM_strx2:
1191
211k
            case DW_FORM_strx3:
1192
233k
            case DW_FORM_strx4:
1193
233k
                cucon->cc_at_strx_present = TRUE;
1194
3.67M
            default:
1195
3.67M
                break;
1196
3.67M
            }
1197
3.67M
            switch(attrnum) {
1198
2.67k
            case DW_AT_dwo_id:
1199
2.82k
            case DW_AT_GNU_dwo_id: {
1200
2.82k
                Dwarf_Sig8 signature;
1201
                /*  This is for DWARF4 with an early
1202
                    non-standard version
1203
                    of split dwarf. Not DWARF5. */
1204
2.82k
                int sres = 0;
1205
2.82k
                if (version_stamp != DW_CU_VERSION4 ) {
1206
                    /* Not supposed to happen. */
1207
228
                    local_attrlist_dealloc(dbg,atcount,alist);
1208
228
                    _dwarf_error(dbg,error,
1209
228
                        DW_DLE_IMPROPER_DWO_ID);
1210
228
                    return DW_DLV_ERROR;
1211
228
                }
1212
2.59k
                signature = dwarfsig8zero;
1213
2.59k
                sres = dwarf_formsig8_const(attr,
1214
2.59k
                    &signature,error);
1215
2.59k
                if (sres == DW_DLV_OK) {
1216
1.75k
                    if (!cucon->cc_signature_present) {
1217
107
                        cucon->cc_signature = signature;
1218
107
                        cucon->cc_signature_present = TRUE;
1219
1.64k
                    } else {
1220
                        /*  Something wrong. Two styles of sig?
1221
                            Can happen with DWARF4
1222
                            debug-fission extension DWO_id.
1223
                        */
1224
1.64k
                        if (memcmp(&signature,&cucon->cc_signature,
1225
1.64k
                            sizeof(signature))) {
1226
                            /*  The two sigs do not match! */
1227
1.34k
                            const char *m="DW_DLE_SIGNATURE_MISMATCH"
1228
1.34k
                                "DWARF4 extension fission signature"
1229
1.34k
                                " and DW_AT_GNU_dwo_id do not match"
1230
1.34k
                                " ignoring DW_AT[_GNU]_dwo_id";
1231
1.34k
                            dwarf_insert_harmless_error(dbg,
1232
1.34k
                                (char*)m);
1233
1.34k
                        }
1234
1.64k
                    }
1235
1.75k
                } else {
1236
                    /* Something is badly wrong. */
1237
848
                    local_attrlist_dealloc(dbg,atcount,alist);
1238
848
                    return sres;
1239
848
                }
1240
                    /* Something is badly wrong. */
1241
1.75k
                break;
1242
2.59k
            }
1243
            /*  If, in .debug_rnglists for a CU the
1244
                applicable range has no base address
1245
                this attribute provides a base address.
1246
                If this is indexed doing this now would
1247
                lead to an infinite recursion.
1248
                So wait till all the other fields seen.
1249
            */
1250
284k
            case DW_AT_low_pc: {
1251
284k
                low_pc_attrnum = i;
1252
284k
                break;
1253
2.59k
            }
1254
            /*  DW_AT_producer 4.2.1 (Based on Apple Inc. build 5658)
1255
                (LLVM build 2336.1.00) uses DW_AT_entry_pc as the
1256
                base address (DW_AT_entry_pc
1257
                first appears in DWARF3).
1258
                So we allow that as an extension,
1259
                as a 'low_pc' if there is DW_AT_entry_pc with
1260
                no DW_AT_low_pc. 19 May 2022.
1261
            */
1262
5.88k
            case DW_AT_entry_pc: {
1263
5.88k
                entry_pc_attrnum = i;
1264
5.88k
                break;
1265
2.59k
            }
1266
1267
            /*  The offset is of the first offset in
1268
                .debug_str_offsets that is the string table
1269
                offset array for this CU. */
1270
13.6k
            case DW_AT_str_offsets_base:{
1271
13.6k
                int udres = 0;
1272
13.6k
                Dwarf_Bool is_info = cucon->cc_is_info;
1273
1274
13.6k
                udres = _dwarf_internal_global_formref_b(attr,
1275
13.6k
                    /* avoid recurse creating context */ 1,
1276
13.6k
                    &cucon->cc_str_offsets_array_offset,
1277
13.6k
                    &is_info,
1278
13.6k
                    error);
1279
13.6k
                if (udres == DW_DLV_OK) {
1280
12.8k
                    cucon->cc_str_offsets_array_offset_present = TRUE;
1281
12.8k
                } else {
1282
766
                    local_attrlist_dealloc(dbg,atcount,alist);
1283
                    /* Something is badly wrong. */
1284
766
                    return udres;
1285
766
                }
1286
12.8k
                break;
1287
13.6k
            }
1288
            /*  offset in .debug_loclists  of the offsets table
1289
                applicable to this CU. */
1290
12.8k
            case DW_AT_loclists_base: {
1291
5.77k
                int udres = 0;
1292
5.77k
                Dwarf_Bool is_info = cucon->cc_is_info;
1293
1294
5.77k
                udres = _dwarf_internal_global_formref_b(attr,
1295
5.77k
                    /* avoid recurse creating context */ 1,
1296
5.77k
                    &cucon->cc_loclists_base,
1297
5.77k
                    &is_info,
1298
5.77k
                    error);
1299
5.77k
                if (udres == DW_DLV_OK) {
1300
5.47k
                    cucon->cc_loclists_base_present = TRUE;
1301
5.47k
                } else {
1302
298
                    local_attrlist_dealloc(dbg,atcount,alist);
1303
                    /* Something is badly wrong. */
1304
298
                    return udres;
1305
298
                }
1306
5.47k
                break;
1307
5.77k
                }
1308
            /*  Base offset  in .debug_addr of the addr table
1309
                for this CU. DWARF5 (and possibly GNU DWARF4)
1310
                So we really want to look in only
1311
                this section, not an offset referring
1312
                to another (DWARF5 debug_info vs debug_types) */
1313
27.0k
            case DW_AT_addr_base:
1314
31.5k
            case DW_AT_GNU_addr_base: {
1315
31.5k
                int udres = 0;
1316
31.5k
                Dwarf_Bool is_info = cucon->cc_is_info;
1317
1318
31.5k
                at_addr_base_attrnum = i;
1319
1320
31.5k
                udres = _dwarf_internal_global_formref_b(attr,
1321
31.5k
                    /* avoid recurse creating context */ 1,
1322
31.5k
                    &cucon->cc_addr_base,
1323
31.5k
                    &is_info,
1324
31.5k
                    error);
1325
31.5k
                if (udres == DW_DLV_OK) {
1326
30.5k
                    if (is_info == cucon->cc_is_info) {
1327
                        /*  Only accept if same .debug section,
1328
                            which is relevant for DWARF4 */
1329
30.5k
                        cucon->cc_addr_base_present = TRUE;
1330
30.5k
                    }
1331
30.5k
                } else {
1332
923
                    local_attrlist_dealloc(dbg,atcount,alist);
1333
                    /* Something is badly wrong. */
1334
923
                    return udres;
1335
923
                }
1336
30.5k
                break;
1337
31.5k
            }
1338
30.5k
            case DW_AT_GNU_ranges_base: {
1339
            /*  The DW4 ranges base was never used in GNU
1340
                but did get emitted in skeletons.
1341
                http://llvm.1065342.n5.nabble.com/
1342
                DebugInfo-DW-AT-GNU-ranges-base-in-
1343
                non-fission-td64194.html
1344
                But we accept it anyway. */
1345
            /*  offset in .debug_rnglists  of the offsets table
1346
                applicable to this CU.
1347
                Note that this base applies when
1348
                referencing from the dwp, but NOT
1349
                when referencing from the a.out */
1350
12.0k
                int udres = 0;
1351
12.0k
                Dwarf_Bool is_info = cucon->cc_is_info;
1352
1353
12.0k
                udres = _dwarf_internal_global_formref_b(attr,
1354
12.0k
                    /* avoid recurse creating context */ 1,
1355
12.0k
                    &cucon->cc_ranges_base,
1356
12.0k
                    &is_info,
1357
12.0k
                    error);
1358
12.0k
                if (udres == DW_DLV_OK) {
1359
11.7k
                    cucon->cc_ranges_base_present = TRUE;
1360
11.7k
                } else {
1361
226
                    local_attrlist_dealloc(dbg,atcount,alist);
1362
                    /* Something is badly wrong. */
1363
226
                    return udres;
1364
226
                }
1365
11.7k
                break;
1366
12.0k
                }
1367
11.9k
            case  DW_AT_rnglists_base: {
1368
11.9k
                int udres = 0;
1369
11.9k
                Dwarf_Bool is_info = cucon->cc_is_info;
1370
1371
11.9k
                udres = _dwarf_internal_global_formref_b(attr,
1372
11.9k
                    /* avoid recurse creating context */ 1,
1373
11.9k
                    &cucon->cc_rnglists_base,
1374
11.9k
                    &is_info,
1375
11.9k
                    error);
1376
11.9k
                if (udres == DW_DLV_OK) {
1377
11.2k
                    cucon->cc_rnglists_base_present = TRUE;
1378
11.2k
                } else {
1379
631
                    local_attrlist_dealloc(dbg,atcount,alist);
1380
                    /* Something is badly wrong. */
1381
631
                    return udres;
1382
631
                }
1383
11.2k
                break;
1384
11.9k
                }
1385
            /*  A signature, found in a DWARF5 skeleton
1386
                compilation unit. */
1387
11.2k
            case DW_AT_GNU_dwo_name:
1388
14.9k
            case DW_AT_dwo_name: {
1389
14.9k
                int dnres = 0;
1390
1391
14.9k
                dnres = dwarf_formstring(attr,
1392
14.9k
                    &cucon->cc_dwo_name,error);
1393
14.9k
                if (dnres != DW_DLV_OK) {
1394
1.11k
                    local_attrlist_dealloc(dbg,atcount,alist);
1395
1.11k
                    return dnres;
1396
1.11k
                }
1397
13.8k
                cucon->cc_dwo_name_present = TRUE;
1398
13.8k
                break;
1399
14.9k
                }
1400
3.29M
            default: /* do nothing, not an attribute
1401
                we need to deal with here. */
1402
3.29M
                break;
1403
3.67M
            }
1404
3.67M
        }
1405
3.67M
    }
1406
20.9k
    if (low_pc_attrnum >= 0 ){
1407
5.68k
        int battr = 0;
1408
1409
        /* Prefer DW_AT_low_pc */
1410
5.68k
        Dwarf_Attribute attr = alist[low_pc_attrnum];
1411
5.68k
        battr = _dwarf_setup_base_address(dbg,"DW_AT_low_pc",
1412
5.68k
            attr,at_addr_base_attrnum, cucon,error);
1413
5.68k
        if (battr != DW_DLV_OK) {
1414
2.01k
            local_attrlist_dealloc(dbg,atcount,alist);
1415
            /* Something is badly wrong */
1416
2.01k
            return battr;
1417
2.01k
        }
1418
15.2k
    } else if (entry_pc_attrnum >= 0) {
1419
525
        int battr = 0;
1420
1421
        /*  Pretending that DW_AT_entry_pc with no
1422
            DW_AT_low_pc is a valid base address for
1423
            loccation lists.
1424
            DW_AT_producer 4.2.1 (Based on Apple Inc. build 5658)
1425
            (LLVM build 2336.1.00) uses DW_AT_entry_pc as the
1426
            base address (DW_AT_entry_pc first appears in DWARF3).
1427
            So we allow that as an extension,
1428
            as a 'low_pc' if there is DW_AT_entry_pc with
1429
            no DW_AT_low_pc. 19 May 2022. */
1430
525
        Dwarf_Attribute attr = alist[entry_pc_attrnum];
1431
525
        battr = _dwarf_setup_base_address(dbg,"DW_AT_entry_pc",
1432
525
            attr,at_addr_base_attrnum, cucon,error);
1433
525
        if (battr != DW_DLV_OK) {
1434
457
            local_attrlist_dealloc(dbg,atcount,alist);
1435
            /* Something is badly wrong */
1436
457
            return battr;
1437
457
        }
1438
525
    }
1439
18.4k
    local_attrlist_dealloc(dbg,atcount,alist);
1440
18.4k
    alist = 0;
1441
18.4k
    atcount = 0;
1442
18.4k
    {
1443
18.4k
        int chres = 0;
1444
18.4k
        Dwarf_Half flag = 0;
1445
1446
        /*  always winds up with cc_cu_die_has_children
1447
            set intentionally...to something. */
1448
18.4k
        cucon->cc_cu_die_has_children = TRUE;
1449
18.4k
        chres = dwarf_die_abbrev_children_flag(cudie,&flag);
1450
        /*  If chres is not DW_DLV_OK the assumption
1451
            of children remains true. */
1452
18.4k
        if (chres == DW_DLV_OK) {
1453
18.4k
            cucon->cc_cu_die_has_children = flag;
1454
18.4k
        }
1455
18.4k
    }
1456
18.4k
    return DW_DLV_OK;
1457
20.9k
}
1458
1459
/*  Called only for DWARF4 */
1460
static void
1461
assign_correct_unit_type(Dwarf_CU_Context cu_context)
1462
11.1k
{
1463
11.1k
    Dwarf_Half tag = cu_context->cc_cu_die_tag;
1464
11.1k
    if (!cu_context->cc_cu_die_has_children) {
1465
2.16k
        if (cu_context->cc_signature_present) {
1466
2.12k
            if (tag == DW_TAG_compile_unit ||
1467
2.12k
                tag == DW_TAG_type_unit ) {
1468
219
                cu_context->cc_unit_type = DW_UT_skeleton;
1469
219
            }
1470
2.12k
        }
1471
8.95k
    } else {
1472
8.95k
        if (cu_context->cc_signature_present) {
1473
8.88k
            if (tag == DW_TAG_compile_unit) {
1474
3.32k
                cu_context->cc_unit_type = DW_UT_split_compile;
1475
5.56k
            } else if (tag == DW_TAG_type_unit) {
1476
5.35k
                cu_context->cc_unit_type = DW_UT_split_type;
1477
5.35k
            }
1478
8.88k
        }
1479
8.95k
    }
1480
11.1k
}
1481
1482
/*  If local_cudie_return non-null, and returning DW_DLV_OK,
1483
    then we return a valid CU_DIE through
1484
    local_cudie_return. */
1485
static int
1486
finish_up_cu_context_from_cudie(Dwarf_Debug dbg,
1487
    Dwarf_Unsigned offset,
1488
    Dwarf_CU_Context cu_context,
1489
    Dwarf_Die *cudie_return,
1490
    Dwarf_Error *error)
1491
38.9k
{
1492
38.9k
    int version = cu_context->cc_version_stamp;
1493
38.9k
    Dwarf_Sig8 signaturedata = cu_context->cc_signature;
1494
38.9k
    int res = 0;
1495
1496
    /*  Loads and initializes the dwarf .debug_cu_index
1497
        and .debug_tu_index split dwarf package
1498
        file sections */
1499
38.9k
    res = fill_in_dwp_offsets_if_present(dbg,
1500
38.9k
        cu_context,
1501
38.9k
        &signaturedata,
1502
38.9k
        offset,
1503
38.9k
        error);
1504
38.9k
    if (res != DW_DLV_OK) {
1505
337
        return res;
1506
337
    }
1507
38.6k
    if (cu_context->cc_dwp_offsets.pcu_type) {
1508
89
        Dwarf_Unsigned absize = 0;
1509
89
        Dwarf_Unsigned aboff = 0;
1510
1511
89
        aboff = _dwarf_get_dwp_extra_offset(
1512
89
            &cu_context->cc_dwp_offsets,
1513
89
            DW_SECT_ABBREV, &absize);
1514
89
        cu_context->cc_abbrev_offset +=  aboff;
1515
89
    }
1516
1517
38.6k
    if (cu_context->cc_abbrev_offset >=
1518
38.6k
        dbg->de_debug_abbrev.dss_size) {
1519
702
        _dwarf_error(dbg, error, DW_DLE_ABBREV_OFFSET_ERROR);
1520
702
        return DW_DLV_ERROR;
1521
702
    }
1522
    /*  Now we can read the CU die and determine
1523
        the correct DW_UT_ type for DWARF4 and some
1524
        offset base fields for DW4-fission and DW5,
1525
        and even DW3 and DW4 and some non-std DW2 */
1526
37.9k
    {
1527
37.9k
        res = finish_cu_context_via_cudie_inner(dbg,
1528
37.9k
            cu_context,cudie_return, error);
1529
37.9k
        if (res == DW_DLV_ERROR) {
1530
16.2k
            return res;
1531
16.2k
        }
1532
21.6k
        if (res != DW_DLV_OK) {
1533
0
            return res;
1534
0
        }
1535
21.6k
        if (version == DW_CU_VERSION4) {
1536
11.1k
            assign_correct_unit_type(cu_context);
1537
11.1k
        }
1538
21.6k
        if (cu_context->cc_signature_present) {
1539
            /*  Initially just for DW_SECT_STR_OFFSETS,
1540
                finds the section offset of the
1541
                contribution which is not the same
1542
                as the table offset. */
1543
18.6k
            res = _dwarf_find_all_offsets_via_fission(dbg,
1544
18.6k
                cu_context,error);
1545
18.6k
            if (res == DW_DLV_ERROR) {
1546
                /* something seriously wrong. */
1547
2
                return res;
1548
2
            }
1549
18.6k
        }
1550
21.6k
    }
1551
21.6k
    return DW_DLV_OK;
1552
21.6k
}
1553
/*
1554
    CU_Contexts do not overlap.
1555
    cu_context we see here is not in the list we
1556
    are updating. See _dwarf_find_CU_Context()
1557
1558
    Invariant: cc_debug_offset in strictly
1559
        ascending order in the list.
1560
*/
1561
static int
1562
insert_into_cu_context_list(Dwarf_Debug_InfoTypes dis,
1563
    Dwarf_CU_Context icu_context)
1564
21.6k
{
1565
21.6k
    Dwarf_Unsigned ioffset = icu_context->cc_debug_offset;
1566
21.6k
    Dwarf_Unsigned eoffset = 0;
1567
21.6k
    Dwarf_Unsigned hoffset = 0;
1568
21.6k
    Dwarf_Unsigned coffset = 0;
1569
21.6k
    Dwarf_CU_Context next = 0;
1570
21.6k
    Dwarf_CU_Context past = 0;
1571
21.6k
    Dwarf_CU_Context cur = 0;
1572
1573
    /*  Add the context into the section context list.
1574
        This is the one and only place where it is
1575
        saved for re-use and eventual dealloc. */
1576
21.6k
    if (!dis->de_cu_context_list) {
1577
        /*  First cu encountered. */
1578
20.4k
        dis->de_cu_context_list = icu_context;
1579
20.4k
        dis->de_cu_context_list_end = icu_context;
1580
20.4k
        return DW_DLV_OK;
1581
20.4k
    }
1582
1.21k
    if (!dis->de_cu_context_list_end) {
1583
0
        return DW_DLV_ERROR;
1584
0
    }
1585
1.21k
    eoffset = dis->de_cu_context_list_end->cc_debug_offset;
1586
1.21k
    if (eoffset < ioffset) {
1587
        /* Normal case, add at end. */
1588
1.21k
        dis->de_cu_context_list_end->cc_next = icu_context;
1589
1.21k
        dis->de_cu_context_list_end = icu_context;
1590
1.21k
        return DW_DLV_OK;
1591
1.21k
    }
1592
0
    hoffset = dis->de_cu_context_list->cc_debug_offset;
1593
0
    if (hoffset > ioffset) {
1594
        /* insert as new head. Unusual. */
1595
0
        next =  dis->de_cu_context_list;
1596
0
        dis->de_cu_context_list = icu_context;
1597
0
        dis->de_cu_context_list->cc_next = next;
1598
        /*  No need to touch de_cu_context_list_end */
1599
0
        return DW_DLV_OK;
1600
0
    }
1601
0
    cur = dis->de_cu_context_list;
1602
0
    past = 0;
1603
    /*  Insert in middle somewhere. Neither at
1604
        start nor end.
1605
        ASSERT: cur non-null
1606
        ASSERT: past non-null */
1607
0
    past = cur;
1608
0
    cur = cur->cc_next;
1609
0
    for ( ; cur ; cur = next) {
1610
0
        next = cur->cc_next;
1611
0
        coffset = cur->cc_debug_offset;
1612
0
        if (coffset  >  ioffset) {
1613
            /*  Insert before cur, using past.
1614
                ASSERT: past non-null  */
1615
0
            past->cc_next = icu_context;
1616
0
            icu_context->cc_next = cur;
1617
0
            return DW_DLV_OK;
1618
0
        }
1619
0
        past = cur;
1620
0
    }
1621
    /*  Impossible, for end, coffset (ie, eoffset) > ioffset  */
1622
    /* NOTREACHED */
1623
0
    return DW_DLV_ERROR;
1624
0
}
1625
1626
Dwarf_Unsigned
1627
_dwarf_calculate_next_cu_context_offset(Dwarf_CU_Context cu_context)
1628
1.86k
{
1629
1.86k
    Dwarf_Unsigned next_cu_offset = 0;
1630
1631
1.86k
    next_cu_offset = cu_context->cc_debug_offset +
1632
1.86k
        cu_context->cc_length +
1633
1.86k
        cu_context->cc_length_size +
1634
1.86k
        cu_context->cc_extension_size;
1635
1.86k
    return next_cu_offset;
1636
1.86k
}
1637
1638
/*  If local_cudie_return non-null, and returning DW_DLV_OK,
1639
    then we return a valid CU_DIE through
1640
    local_cudie_return. */
1641
int
1642
_dwarf_create_a_new_cu_context_record_on_list(
1643
    Dwarf_Debug dbg,
1644
    Dwarf_Debug_InfoTypes dis,
1645
    Dwarf_Bool is_info,
1646
    Dwarf_Unsigned section_size,
1647
    Dwarf_Unsigned new_cu_offset,
1648
    Dwarf_CU_Context *context_out,
1649
    Dwarf_Die *cudie_return,
1650
    Dwarf_Error *error)
1651
42.9k
{
1652
42.9k
    int res = 0;
1653
42.9k
    Dwarf_CU_Context cu_context = 0;
1654
42.9k
    int icres = 0;
1655
1656
42.9k
    if ((new_cu_offset +
1657
42.9k
        _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
1658
42.9k
        section_size) {
1659
47
        _dwarf_error(dbg, error, DW_DLE_OFFSET_BAD);
1660
47
        return DW_DLV_ERROR;
1661
47
    }
1662
42.8k
    res = _dwarf_make_CU_Context(dbg, new_cu_offset,is_info,
1663
42.8k
        &cu_context,error);
1664
42.8k
    if (res != DW_DLV_OK) {
1665
3.90k
        return res;
1666
3.90k
    }
1667
    /*  The called func does not dealloc cu_context
1668
        in case of error, so we do it here. */
1669
38.9k
    res = finish_up_cu_context_from_cudie(dbg,new_cu_offset,
1670
38.9k
        cu_context,cudie_return,error);
1671
38.9k
    if (res == DW_DLV_ERROR) {
1672
17.2k
        local_dealloc_cu_context(dbg,cu_context);
1673
17.2k
        return res;
1674
17.2k
    }
1675
21.6k
    if (res == DW_DLV_NO_ENTRY) {
1676
0
        local_dealloc_cu_context(dbg,cu_context);
1677
0
        return res;
1678
0
    }
1679
    /*  Add the new cu_context to a list of contexts */
1680
21.6k
    icres = insert_into_cu_context_list(dis,cu_context);
1681
21.6k
    if (icres == DW_DLV_ERROR) {
1682
0
        local_dealloc_cu_context(dbg,cu_context);
1683
0
        _dwarf_error_string(dbg,error,DW_DLE_DIE_NO_CU_CONTEXT,
1684
0
            "DW_DLE_DIE_NO_CU_CONTEXT"
1685
0
            "Impossible error inserting into internal context list");
1686
0
        return icres;
1687
0
    }
1688
21.6k
    *context_out = cu_context;
1689
21.6k
    return DW_DLV_OK;
1690
21.6k
}
1691
1692
int
1693
_dwarf_load_die_containing_section(Dwarf_Debug dbg,
1694
    Dwarf_Bool is_info,
1695
    Dwarf_Error *error)
1696
49.0k
{
1697
49.0k
    Dwarf_Error err2 = 0;
1698
1699
49.0k
    int resd = is_info?
1700
4.53k
        _dwarf_load_debug_info(dbg, &err2):
1701
49.0k
        _dwarf_load_debug_types(dbg,&err2);
1702
49.0k
    if (resd == DW_DLV_ERROR) {
1703
3.24k
        if (reloc_incomplete(resd,err2)) {
1704
            /*  We will assume all is ok, though it is not.
1705
                Relocation errors need not be fatal. */
1706
588
            char msg_buf[300];
1707
588
            char *dwerrmsg = 0;
1708
588
            char *msgprefix =
1709
588
                "Relocations did not complete successfully, "
1710
588
                "but we are " " ignoring error: ";
1711
588
            size_t totallen = 0;
1712
588
            size_t prefixlen = 0;
1713
1714
588
            dwerrmsg = dwarf_errmsg(err2);
1715
588
            prefixlen = strlen(msgprefix);
1716
588
            totallen = prefixlen + strlen(dwerrmsg);
1717
588
            if ( totallen >= sizeof(msg_buf)) {
1718
0
                const char *m= "Error:corrupted dwarf message table!";
1719
                /*  Impossible unless something corrupted.
1720
                    Provide a shorter dwerrmsg*/
1721
0
                _dwarf_safe_strcpy(msg_buf,sizeof(msg_buf),
1722
0
                    m,strlen(m));
1723
588
            } else {
1724
588
                _dwarf_safe_strcpy(msg_buf,sizeof(msg_buf),
1725
588
                    msgprefix,prefixlen);
1726
588
                _dwarf_safe_strcpy(msg_buf +prefixlen,
1727
588
                    sizeof(msg_buf)-prefixlen,
1728
588
                    dwerrmsg,strlen(dwerrmsg));
1729
588
            }
1730
588
            dwarf_insert_harmless_error(dbg,msg_buf);
1731
            /*  Fall thru to use the newly loaded section.
1732
                even though it might not be adequately
1733
                relocated. */
1734
588
            dwarf_dealloc_error(dbg,err2);
1735
588
            if (error) {
1736
292
                *error = 0;
1737
292
            }
1738
588
            return DW_DLV_OK;
1739
588
        }
1740
2.65k
        if (error) {
1741
337
            *error = err2;
1742
2.31k
        } else {
1743
2.31k
            dwarf_dealloc_error(dbg,err2);
1744
2.31k
        }
1745
2.65k
        return DW_DLV_ERROR;
1746
3.24k
    }
1747
45.7k
    return resd;
1748
49.0k
}
1749
1750
int
1751
_dwarf_next_cu_header_internal(Dwarf_Debug dbg,
1752
    Dwarf_Bool is_info,
1753
    Dwarf_Die *cu_die_out,
1754
    Dwarf_Unsigned * cu_header_length,
1755
    Dwarf_Half * version_stamp,
1756
    Dwarf_Unsigned * abbrev_offset,
1757
    Dwarf_Half * address_size,
1758
    Dwarf_Half * offset_size,
1759
    Dwarf_Half * extension_size,
1760
    Dwarf_Sig8 * signature_out,
1761
    Dwarf_Bool * has_signature,
1762
    Dwarf_Unsigned *typeoffset,
1763
    Dwarf_Unsigned * next_cu_offset,
1764
1765
    /*  header_type: DW_UT_compile, DW_UT_partial,
1766
        DW_UT_type, returned through the pointer.
1767
        A new item in DWARF5, synthesized for earlier DWARF
1768
        CUs (& TUs). */
1769
    Dwarf_Half * header_type,
1770
    Dwarf_Error * error)
1771
48.5k
{
1772
    /* Offset for current and new CU. */
1773
48.5k
    Dwarf_Unsigned new_offset = 0;
1774
48.5k
    Dwarf_Die local_cudie = 0;
1775
1776
    /* CU Context for current CU. */
1777
48.5k
    Dwarf_CU_Context cu_context = 0;
1778
48.5k
    Dwarf_Debug_InfoTypes dis = 0;
1779
48.5k
    Dwarf_Unsigned section_size =  0;
1780
48.5k
    Dwarf_Small *dataptr = 0;
1781
48.5k
    struct Dwarf_Section_s *secdp = 0;
1782
48.5k
    int res = 0;
1783
1784
    /* ***** BEGIN CODE ***** */
1785
1786
48.5k
    CHECK_DBG(dbg,error,"dwarf_next_cuheader_[d,e]()");
1787
46.4k
    if (is_info) {
1788
3.19k
        dis =&dbg->de_info_reading;
1789
3.19k
        dataptr = dbg->de_debug_info.dss_data;
1790
3.19k
        secdp = &dbg->de_debug_info;
1791
43.2k
    } else {
1792
43.2k
        dis =&dbg->de_types_reading;
1793
43.2k
        dataptr = dbg->de_debug_types.dss_data;
1794
43.2k
        secdp = &dbg->de_debug_types;
1795
43.2k
    }
1796
1797
46.4k
    if (!dataptr) {
1798
46.4k
        res = _dwarf_load_die_containing_section(dbg,
1799
46.4k
            is_info, error);
1800
46.4k
        if (res != DW_DLV_OK) {
1801
4.78k
            return res;
1802
4.78k
        }
1803
46.4k
    }
1804
41.6k
    if (!dis->de_cu_context) {
1805
        /*  We are leaving new_offset zero. We are at the
1806
            start of a section. */
1807
41.6k
        new_offset = 0;
1808
41.6k
    } else {
1809
0
        new_offset = _dwarf_calculate_next_cu_context_offset(
1810
0
            dis->de_cu_context);
1811
0
    }
1812
1813
    /*  Check that there is room in .debug_info beyond
1814
        the new offset for at least a new cu header.
1815
        If not, return DW_DLV_NO_ENTRY to indicate end
1816
        of debug_info section, and reset
1817
        de_cu_debug_info_offset to
1818
        enable looping back through the cu's. */
1819
41.6k
    section_size = secdp->dss_size;
1820
41.6k
    if ((new_offset +
1821
41.6k
        _dwarf_length_of_cu_header_simple(dbg,is_info)) >=
1822
41.6k
        section_size) {
1823
        /*  We must reset as we will not create a proper
1824
            de_cu_context here, see comment just above. */
1825
585
        dis->de_cu_context = NULL;
1826
585
        return DW_DLV_NO_ENTRY;
1827
585
    }
1828
1829
    /* Check if this CU has been read before. */
1830
41.0k
    cu_context = _dwarf_find_CU_Context(dbg, new_offset,is_info);
1831
1832
    /* If not, make CU Context for it. */
1833
41.0k
    if (!cu_context) {
1834
41.0k
        res = _dwarf_create_a_new_cu_context_record_on_list(
1835
41.0k
            dbg,dis,is_info,section_size,new_offset,
1836
41.0k
            &cu_context,&local_cudie,error);
1837
41.0k
        if (res != DW_DLV_OK) {
1838
20.7k
            if (local_cudie) {
1839
0
                dwarf_dealloc_die(local_cudie);
1840
0
            }
1841
20.7k
            return res;
1842
20.7k
        }
1843
41.0k
    }
1844
    /*  Next assignment is what makes
1845
        _dwarf_next_cu_header_d()
1846
        with no offset presented work to march
1847
        through all the CUs in order. Other places
1848
        creating a cu_context do not set de_cu_context.
1849
        if callers use dwarf_next_cu_header_e() this
1850
        is unimportant but not harmful.  */
1851
20.3k
    dis->de_cu_context = cu_context;
1852
20.3k
    if (cu_header_length) {
1853
20.3k
        *cu_header_length = cu_context->cc_length;
1854
20.3k
    }
1855
20.3k
    if (version_stamp) {
1856
20.3k
        *version_stamp = cu_context->cc_version_stamp;
1857
20.3k
    }
1858
20.3k
    if (abbrev_offset) {
1859
20.3k
        *abbrev_offset = cu_context->cc_abbrev_offset;
1860
20.3k
    }
1861
20.3k
    if (address_size) {
1862
20.3k
        *address_size = cu_context->cc_address_size;
1863
20.3k
    }
1864
20.3k
    if (offset_size) {
1865
20.3k
        *offset_size = cu_context->cc_length_size;
1866
20.3k
    }
1867
20.3k
    if (extension_size) {
1868
20.3k
        *extension_size = cu_context->cc_extension_size;
1869
20.3k
    }
1870
20.3k
    if (header_type) {
1871
20.3k
        *header_type = cu_context->cc_unit_type;
1872
20.3k
    }
1873
20.3k
    if (typeoffset) {
1874
20.3k
        *typeoffset = cu_context->cc_signature_offset;
1875
20.3k
    }
1876
20.3k
    if (signature_out) {
1877
20.3k
        *signature_out = cu_context->cc_signature;
1878
20.3k
    }
1879
20.3k
    if (has_signature) {
1880
20.3k
        *has_signature = cu_context->cc_signature_present;
1881
20.3k
    }
1882
    /*  Determine the offset of the next CU. */
1883
20.3k
    new_offset = new_offset + cu_context->cc_length +
1884
20.3k
        cu_context->cc_length_size + cu_context->cc_extension_size;
1885
    /*  Allowing null argument starting 22 April 2019. */
1886
20.3k
    if (next_cu_offset) {
1887
16.4k
        *next_cu_offset = new_offset;
1888
16.4k
    }
1889
20.3k
    {
1890
20.3k
        Dwarf_Debug tieddbg = 0;
1891
20.3k
        int tres = 0;
1892
20.3k
        tieddbg = dbg->de_tied_data.td_tied_object;
1893
20.3k
        if (tieddbg) {
1894
0
            tres = _dwarf_merge_all_base_attrs_of_cu_die(
1895
0
                dbg, cu_context,
1896
0
                tieddbg, 0,
1897
0
                error);
1898
0
        }
1899
20.3k
        if (tres == DW_DLV_ERROR && error) {
1900
            /*  We'll assume any errors will be
1901
                discovered later. Lets get our CU_context
1902
                finished.
1903
                if error NULL it's a caller issue
1904
                and there is nothing we can do here */
1905
0
            dwarf_dealloc_error(dbg,*error);
1906
0
            *error = 0;
1907
0
        }
1908
20.3k
    }
1909
20.3k
    if (cu_die_out) {
1910
1.20k
        if (!local_cudie) {
1911
            /*  This is safe since we know the
1912
                correct cu_context */
1913
44
            res = _dwarf_siblingof_internal(dbg,NULL,
1914
44
                cu_context, is_info,&local_cudie,error);
1915
44
            if (res != DW_DLV_OK) {
1916
44
                return res;
1917
44
            }
1918
0
            *cu_die_out = local_cudie;
1919
1.16k
        } else {
1920
1.16k
            *cu_die_out = local_cudie;
1921
1.16k
        }
1922
19.1k
    } else {
1923
19.1k
        if (local_cudie) {
1924
18.1k
            dwarf_dealloc_die(local_cudie);
1925
18.1k
        }
1926
19.1k
    }
1927
20.3k
    return DW_DLV_OK;
1928
20.3k
}
1929
1930
/*  This involves data in a split dwarf or package file.
1931
1932
    Given hash signature, return the CU_die of the applicable CU.
1933
    The hash is assumed to be from 'somewhere'.
1934
    For DWARF 4:
1935
        From a skeleton DIE DW_AT_GNU_dwo_id  ("cu" case) or
1936
        From a DW_FORM_ref_sig8 ("tu" case).
1937
    For DWARF5:
1938
        From  dwo_id in a skeleton CU header (DW_UT_skeleton).
1939
        From a DW_FORM_ref_sig8 ("tu" case).
1940
1941
    If "tu" request,  the CU_die
1942
    of of the type unit.
1943
    Works on either a dwp package file or a dwo object.
1944
1945
    If "cu" request,  the CU_die
1946
    of the compilation unit.
1947
    Works on either a dwp package file or a dwo object.
1948
1949
    If the hash passed is not present, returns DW_DLV_NO_ENTRY
1950
    (but read the next two paragraphs for more detail).
1951
1952
    If a dwp package file with the hash signature
1953
    is present in the applicable index but no matching
1954
    compilation unit can be found, it returns DW_DLV_ERROR.
1955
1956
    If a .dwo object there is no index and we look at the
1957
    compilation units (possibly all of them). If not present
1958
    then we return DW_DLV_NO_ENTRY.
1959
1960
    The returned_die is a CU DIE if the sig_type is "cu".
1961
    The returned_die is a type DIE if the sig_type is "tu".
1962
    Perhaps both should return CU die.
1963
1964
    New 27 April, 2015
1965
*/
1966
int
1967
dwarf_die_from_hash_signature(Dwarf_Debug dbg,
1968
    Dwarf_Sig8 *     hash_sig,
1969
    const char *     sig_type  /* "tu" or "cu"*/,
1970
    Dwarf_Die  *     returned_die,
1971
    Dwarf_Error*     error)
1972
1
{
1973
1
    Dwarf_Bool is_type_unit = FALSE;
1974
1
    int sres = 0;
1975
1976
1
    CHECK_DBG(dbg,error,"dwarf_die_from_hash_signature()");
1977
0
    sres = _dwarf_load_debug_info(dbg,error);
1978
0
    if (sres == DW_DLV_ERROR) {
1979
0
        return sres;
1980
0
    }
1981
0
    sres = _dwarf_load_debug_types(dbg,error);
1982
0
    if (sres == DW_DLV_ERROR) {
1983
0
        return sres;
1984
0
    }
1985
1986
0
    if (!strcmp(sig_type,"tu")) {
1987
0
        is_type_unit = TRUE;
1988
0
    } else if (!strcmp(sig_type,"cu")) {
1989
0
        is_type_unit = FALSE;
1990
0
    } else {
1991
0
        _dwarf_error(dbg,error,DW_DLE_SIG_TYPE_WRONG_STRING);
1992
0
        return DW_DLV_ERROR;
1993
0
    }
1994
1995
0
    if (_dwarf_file_has_debug_fission_index(dbg)) {
1996
        /* This is a dwp package file. */
1997
0
        int fisres = 0;
1998
0
        Dwarf_Bool is_info2 = TRUE;
1999
0
        Dwarf_Off cu_header_off = 0;
2000
0
        Dwarf_Off cu_size = 0;
2001
0
        Dwarf_Off cu_die_off = 0;
2002
0
        Dwarf_Off typeoffset = 0;
2003
0
        Dwarf_Die cudie = 0;
2004
0
        Dwarf_Die typedie = 0;
2005
0
        Dwarf_CU_Context context = 0;
2006
0
        Dwarf_Debug_Fission_Per_CU fiss;
2007
2008
0
        memset(&fiss,0,sizeof(fiss));
2009
0
        fisres = dwarf_get_debugfission_for_key(dbg,hash_sig,
2010
0
            sig_type,&fiss,error);
2011
0
        if (fisres != DW_DLV_OK) {
2012
0
            return fisres;
2013
0
        }
2014
        /* Found it */
2015
0
        if (is_type_unit) {
2016
            /*  DW4 has debug_types, so look in .debug_types
2017
                Else look in .debug_info.  */
2018
0
            is_info2 = dbg->de_debug_types.dss_size?FALSE:TRUE;
2019
0
        } else {
2020
0
            is_info2 = TRUE;
2021
0
        }
2022
2023
0
        cu_header_off = _dwarf_get_dwp_extra_offset(&fiss,
2024
0
            is_info2?DW_SECT_INFO:DW_SECT_TYPES,
2025
0
            &cu_size);
2026
2027
0
        fisres = dwarf_get_cu_die_offset_given_cu_header_offset_b(
2028
0
            dbg,cu_header_off,
2029
0
            is_info2,
2030
0
            &cu_die_off,error);
2031
0
        if (fisres != DW_DLV_OK) {
2032
0
            return fisres;
2033
0
        }
2034
0
        fisres = dwarf_offdie_b(dbg,cu_die_off,is_info2,
2035
0
            &cudie,error);
2036
0
        if (fisres != DW_DLV_OK) {
2037
0
            return fisres;
2038
0
        }
2039
0
        if (!is_type_unit) {
2040
0
            *returned_die = cudie;
2041
0
            return DW_DLV_OK;
2042
0
        }
2043
0
        context = cudie->di_cu_context;
2044
0
        typeoffset = context->cc_signature_offset;
2045
0
        typeoffset += cu_header_off;
2046
0
        fisres = dwarf_offdie_b(dbg,typeoffset,is_info2,
2047
0
            &typedie,error);
2048
0
        if (fisres != DW_DLV_OK) {
2049
0
            dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
2050
0
            return fisres;
2051
0
        }
2052
0
        *returned_die = typedie;
2053
0
        dwarf_dealloc(dbg,cudie,DW_DLA_DIE);
2054
0
        return DW_DLV_OK;
2055
0
    }
2056
    /*  Look thru all the CUs, there is no DWP tu/cu index.
2057
        There will be COMDAT sections for the type TUs
2058
            (DW_UT_type).
2059
        A single non-comdat for the DW_UT_compile. */
2060
    /*  FIXME: DW_DLE_DEBUG_FISSION_INCOMPLETE  */
2061
0
    _dwarf_error(dbg,error,DW_DLE_DEBUG_FISSION_INCOMPLETE);
2062
0
    return DW_DLV_ERROR;
2063
0
}
2064
2065
static int
2066
_dwarf_ptr_CU_offset(Dwarf_CU_Context cu_context,
2067
    Dwarf_Byte_Ptr di_ptr,
2068
    Dwarf_Bool is_info,
2069
    Dwarf_Off * cu_off)
2070
0
{
2071
0
    Dwarf_Debug dbg = cu_context->cc_dbg;
2072
0
    Dwarf_Small *dataptr = is_info? dbg->de_debug_info.dss_data:
2073
0
        dbg->de_debug_types.dss_data;
2074
0
    *cu_off = (di_ptr - dataptr);
2075
0
    return DW_DLV_OK;
2076
0
}
2077
#if 0 /* FOR DEBUGGING */
2078
/* Just for debug purposes */
2079
void print_sib_offset(Dwarf_Die sibling)
2080
{
2081
    Dwarf_Off sib_off;
2082
    Dwarf_Error error;
2083
    dwarf_dieoffset(sibling,&sib_off,&error);
2084
    fprintf(stderr," SIB OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,sib_off);
2085
}
2086
void print_ptr_offset(Dwarf_CU_Context cu_context,
2087
    Dwarf_Byte_Ptr di_ptr)
2088
{
2089
    Dwarf_Off ptr_off;
2090
    _dwarf_ptr_CU_offset(cu_context,di_ptr,
2091
        di_ptr->di_is_info,&ptr_off);
2092
    fprintf(stderr," PTR OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,ptr_off);
2093
}
2094
#endif /*0*/
2095
2096
/*  Validate the sibling DIE. This only makes sense to call
2097
    if the sibling's DIEs have been travsersed and
2098
    dwarf_child() called on each,
2099
    so that the last DIE dwarf_child saw was the last.
2100
    Essentially ensuring that (after such traversal) that we
2101
    are in the same place a sibling attribute would identify.
2102
    In case we return DW_DLV_ERROR, the global offset of the last
2103
    DIE traversed by dwarf_child is returned through *offset
2104
2105
    It is essentially guaranteed that  dbg->de_last_die
2106
    is a stale DIE pointer of a deallocated DIE when we get here.
2107
    It must not be used as a DIE pointer here,
2108
    just as a sort of anonymous pointer that we just check against
2109
    NULL.
2110
2111
    There is a (subtle?) dependence on the fact that when we
2112
    call this the last dwarf_child() call would have been for
2113
    this sibling.
2114
    Meaning that this works in a depth-first
2115
    traversal even though there
2116
    is no stack of 'de_last_die' values.
2117
2118
    The check for dbg->de_last_die just ensures sanity.
2119
2120
    If one is switching between normal debug_frame and eh_frame
2121
    (traversing them in tandem, let us say) in a single
2122
    Dwarf_Debug this validator makes no sense.
2123
    It works if one processes a .debug_frame (entirely) and
2124
    then an eh_frame (or vice versa) though.
2125
    Use caution.
2126
*/
2127
int
2128
dwarf_validate_die_sibling(Dwarf_Die sibling,Dwarf_Off *offset)
2129
0
{
2130
0
    Dwarf_Debug dbg = 0;
2131
0
    Dwarf_Error *error = 0;
2132
0
    Dwarf_Debug_InfoTypes dis = 0;
2133
2134
0
    CHECK_DIE(sibling, DW_DLV_ERROR);
2135
0
    dbg = sibling->di_cu_context->cc_dbg;
2136
0
    dis = sibling->di_is_info?
2137
0
        &dbg->de_info_reading: &dbg->de_types_reading;
2138
0
    *offset = 0;
2139
0
    if (dis->de_last_die && dis->de_last_di_ptr) {
2140
0
        if (sibling->di_debug_ptr == dis->de_last_di_ptr) {
2141
0
            return DW_DLV_OK;
2142
0
        }
2143
0
    }
2144
    /* Calculate global offset used for error reporting */
2145
0
    _dwarf_ptr_CU_offset(sibling->di_cu_context,
2146
0
        dis->de_last_di_ptr,sibling->di_is_info,offset);
2147
0
    return DW_DLV_ERROR;
2148
0
}
2149
2150
/*  This function does two slightly different things
2151
    depending on the input flag want_AT_sibling.  If
2152
    this flag is true, it checks if the input die has
2153
    a DW_AT_sibling attribute.  If it does it returns
2154
    a pointer to the start of the sibling die in the
2155
    .debug_info section.  Otherwise it behaves the
2156
    same as the want_AT_sibling false case.
2157
2158
    If the want_AT_sibling flag is false, it returns
2159
    a pointer to the immediately adjacent die in the
2160
    .debug_info section.
2161
2162
    Die_info_end points to the end of the .debug_info
2163
    portion for the cu the die belongs to.  It is used
2164
    to check that the search for the next die does not
2165
    cross the end of the current cu.  Cu_info_start points
2166
    to the start of the .debug_info portion for the
2167
    current cu, and is used to add to the offset for
2168
    DW_AT_sibling attributes.  Finally, has_die_child
2169
    is a pointer to a Dwarf_Bool that is set true if
2170
    the present die has children, false otherwise.
2171
    However, in case want_AT_child is true and the die
2172
    has a DW_AT_sibling attribute *has_die_child is set
2173
    false to indicate that the children are being skipped.
2174
2175
    die_info_end  points to the last byte+1 of the cu.  */
2176
static int
2177
_dwarf_next_die_info_ptr(Dwarf_Byte_Ptr die_info_ptr,
2178
    Dwarf_CU_Context cu_context,
2179
    Dwarf_Byte_Ptr die_info_end,
2180
    Dwarf_Byte_Ptr cu_info_start,
2181
    Dwarf_Bool want_AT_sibling,
2182
    Dwarf_Bool * has_die_child,
2183
    Dwarf_Byte_Ptr *next_die_ptr_out,
2184
    Dwarf_Error *error)
2185
611k
{
2186
611k
    Dwarf_Byte_Ptr info_ptr = 0;
2187
611k
    Dwarf_Byte_Ptr abbrev_ptr = 0;
2188
611k
    Dwarf_Unsigned abbrev_code = 0;
2189
611k
    Dwarf_Abbrev_List abbrev_list = 0;
2190
611k
    Dwarf_Unsigned offset = 0;
2191
611k
    Dwarf_Unsigned utmp = 0;
2192
611k
    Dwarf_Debug    dbg = 0;
2193
611k
    Dwarf_Byte_Ptr abbrev_end = 0;
2194
611k
    int            lres = 0;
2195
611k
    Dwarf_Unsigned i = 0;
2196
611k
    Dwarf_Unsigned highest_code = 0;
2197
2198
611k
    dbg = cu_context->cc_dbg;
2199
611k
    info_ptr = die_info_ptr;
2200
611k
    DECODE_LEB128_UWORD_CK(info_ptr, utmp,dbg,error,die_info_end);
2201
611k
    abbrev_code = (Dwarf_Unsigned) utmp;
2202
611k
    if (abbrev_code == 0) {
2203
        /*  Should never happen. Tested before we got here. */
2204
3
        _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_PTR_NULL);
2205
3
        return DW_DLV_ERROR;
2206
3
    }
2207
611k
    lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
2208
611k
        &abbrev_list,&highest_code,error);
2209
611k
    if (lres == DW_DLV_ERROR) {
2210
37
        return lres;
2211
37
    }
2212
611k
    if (lres == DW_DLV_NO_ENTRY) {
2213
165
        dwarfstring m;
2214
2215
165
        dwarfstring_constructor(&m);
2216
165
        dwarfstring_append_printf_u(&m,
2217
165
            " DW_DLE_NEXT_DIE_NO_ABBREV_LIST "
2218
165
            "There is no abbrev present for code %u"
2219
165
            " in this compilation unit. ",
2220
165
            abbrev_code);
2221
165
        dwarfstring_append_printf_u(&m,
2222
165
            "The highest known code in any "
2223
165
            "compilation unit is %u.",
2224
165
            highest_code);
2225
165
        _dwarf_error_string(dbg, error,
2226
165
            DW_DLE_NEXT_DIE_NO_ABBREV_LIST,
2227
165
            dwarfstring_string(&m));
2228
165
        dwarfstring_destructor(&m);
2229
165
        return DW_DLV_ERROR;
2230
165
    }
2231
2232
610k
    *has_die_child = abbrev_list->abl_has_child;
2233
610k
    abbrev_ptr = abbrev_list->abl_abbrev_ptr;
2234
610k
    abbrev_end = _dwarf_calculate_abbrev_section_end_ptr(cu_context);
2235
2236
610k
    if (!abbrev_list->abl_attr) {
2237
998
        int bres = 0;
2238
2239
998
        bres = _dwarf_fill_in_attr_form_abtable(cu_context,
2240
998
            abbrev_ptr, abbrev_end, abbrev_list,
2241
998
            error);
2242
998
        if (bres != DW_DLV_OK) {
2243
3
            return bres;
2244
3
        }
2245
998
    }
2246
    /*  ASSERT  list->abl_addr and list->abl_form
2247
        are non-null and if  list->abl_implicit_const_count > 0
2248
        list->abl_implicit_const is non-null. */
2249
2250
1.45M
    for ( i = 0; i <abbrev_list->abl_abbrev_count; ++i) {
2251
        /* Dwarf_Signed implicit_const = 0; */
2252
850k
        Dwarf_Half   attr = 0;
2253
850k
        Dwarf_Half   attr_form = 0;
2254
850k
        int          res = 0;
2255
850k
        Dwarf_Byte_Ptr next_die_ptr = 0;
2256
2257
850k
        attr =  abbrev_list->abl_attr[i];
2258
850k
        attr_form =  abbrev_list->abl_form[i];
2259
850k
        if (attr_form == DW_FORM_implicit_const) {
2260
            /* implicit_const = abbrev_list->abl_implicit_const[i];*/
2261
776k
        } else if (attr_form == DW_FORM_indirect) {
2262
4.56k
            Dwarf_Unsigned utmp6;
2263
            /* DECODE_LEB128_UWORD updates info_ptr */
2264
4.56k
            DECODE_LEB128_UWORD_CK(info_ptr, utmp6,dbg,error,
2265
4.56k
                die_info_end);
2266
4.55k
            attr_form = (Dwarf_Half) utmp6;
2267
4.55k
            if (attr_form == DW_FORM_implicit_const ||
2268
4.55k
                attr_form == DW_FORM_indirect) {
2269
112
                _dwarf_error_string(dbg, error,
2270
112
                    DW_DLE_NEXT_DIE_WRONG_FORM,
2271
112
                    "DW_DLE_NEXT_DIE_WRONG_FORM: "
2272
112
                    " Reading Attriutes: an indirect "
2273
112
                    " or implicit_const form "
2274
112
                    "leads to one of the same. "
2275
112
                    "Which is not handled. Corrupt Dwarf");
2276
112
                return DW_DLV_ERROR;
2277
112
            }
2278
4.55k
        }
2279
850k
        if (attr_form == DW_FORM_implicit_const) {
2280
74.4k
            SKIP_LEB128_CK(abbrev_ptr,dbg,error, abbrev_end);
2281
74.4k
        }
2282
2283
850k
        if (want_AT_sibling && attr == DW_AT_sibling) {
2284
1.19k
            switch (attr_form) {
2285
57
            case DW_FORM_ref1:
2286
57
                READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2287
57
                    info_ptr, sizeof(Dwarf_Small),
2288
57
                    error,die_info_end);
2289
55
                break;
2290
114
            case DW_FORM_ref2:
2291
                /* READ_UNALIGNED does not update info_ptr */
2292
114
                READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2293
114
                    info_ptr,DWARF_HALF_SIZE,
2294
114
                    error,die_info_end);
2295
112
                break;
2296
112
            case DW_FORM_ref4:
2297
78
                READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2298
78
                    info_ptr, DWARF_32BIT_SIZE,
2299
78
                    error,die_info_end);
2300
75
                break;
2301
151
            case DW_FORM_ref8:
2302
151
                READ_UNALIGNED_CK(dbg, offset, Dwarf_Unsigned,
2303
151
                    info_ptr, DWARF_64BIT_SIZE,
2304
151
                    error,die_info_end);
2305
149
                break;
2306
149
            case DW_FORM_ref_udata:
2307
87
                DECODE_LEB128_UWORD_CK(info_ptr, offset,
2308
87
                    dbg,error,die_info_end);
2309
85
                break;
2310
351
            case DW_FORM_ref_addr:
2311
                /*  Very unusual.  The FORM is intended to refer to
2312
                    a different CU, but a different CU cannot
2313
                    be a sibling, can it?
2314
                    We could ignore this and treat as if no
2315
                    DW_AT_sibling
2316
                    present.   Or derive the offset from it and if
2317
                    it is in the same CU use it directly.
2318
                    The offset here is *supposed* to be a
2319
                    global offset,
2320
                    so adding cu_info_start is wrong  to any offset
2321
                    we find here unless cu_info_start
2322
                    is zero! Lets pretend there is no DW_AT_sibling
2323
                    attribute.  */
2324
351
                goto no_sibling_attr;
2325
352
            default:
2326
352
                _dwarf_error(dbg, error, DW_DLE_NEXT_DIE_WRONG_FORM);
2327
352
                return DW_DLV_ERROR;
2328
1.19k
            }
2329
2330
            /*  Reset *has_die_child to indicate children skipped.  */
2331
476
            *has_die_child = false;
2332
2333
            /*  A value beyond die_info_end indicates an error.
2334
                Exactly at die_info_end means 1-past-cu-end
2335
                and simply means we
2336
                are at the end, do not return error. Higher level
2337
                will detect that we are at the end. */
2338
476
            {   /*  Care required here. Offset can be garbage. */
2339
476
                Dwarf_Unsigned plen = 0;
2340
2341
                /*  ptrdiff_t is generated but not named */
2342
476
                plen = (die_info_end >= cu_info_start)?
2343
476
                    (die_info_end - cu_info_start):0;
2344
476
                if (offset > plen) {
2345
                    /* Error case, bad DWARF. */
2346
200
                    _dwarf_error_string(dbg, error,
2347
200
                        DW_DLE_SIBLING_OFFSET_WRONG,
2348
200
                        "DW_DLE_SIBLING_OFFSET_WRONG "
2349
200
                        "the offset makes the new die ptr "
2350
200
                        "off the end of the section. Corrupt dwarf");
2351
200
                    return DW_DLV_ERROR;
2352
200
                }
2353
476
            }
2354
            /* At or before end-of-cu */
2355
276
            next_die_ptr = cu_info_start + offset;
2356
276
            if (next_die_ptr <= die_info_ptr) {
2357
                /*  This is a fix for ossfuzz 57562 */
2358
17
                dwarfstring m;
2359
17
                dwarfstring_constructor(&m);
2360
17
                dwarfstring_append_printf_u(&m,
2361
17
                    "DW_DLE_SIBLING_OFFSET_WRONG "
2362
17
                    "the DW_AT_sibling offset (%u) puts "
2363
17
                    "the sibling DIE ptr "
2364
17
                    "equal to or less then the current DIE ptr. "
2365
17
                    "Corrupt dwarf",offset);
2366
17
                _dwarf_error_string(dbg, error,
2367
17
                    DW_DLE_SIBLING_OFFSET_WRONG,
2368
17
                    dwarfstring_string(&m));
2369
17
                dwarfstring_destructor(&m);
2370
17
                return DW_DLV_ERROR;
2371
17
            }
2372
259
            *next_die_ptr_out = next_die_ptr;
2373
259
            return DW_DLV_OK;
2374
276
        }
2375
2376
849k
        no_sibling_attr:
2377
849k
        if (attr_form != 0 && attr_form != DW_FORM_implicit_const) {
2378
614k
            Dwarf_Unsigned sizeofval = 0;
2379
614k
            Dwarf_Unsigned ssize     = 0;
2380
2381
614k
            res = _dwarf_get_size_of_val(cu_context->cc_dbg,
2382
614k
                attr_form,
2383
614k
                cu_context->cc_version_stamp,
2384
614k
                cu_context->cc_address_size,
2385
614k
                info_ptr,
2386
614k
                cu_context->cc_length_size,
2387
614k
                &sizeofval,
2388
614k
                die_info_end,
2389
614k
                error);
2390
614k
            if (res != DW_DLV_OK) {
2391
50
                return res;
2392
50
            }
2393
            /*  It is ok for info_ptr == die_info_end, as we
2394
                will test later before using a too-large info_ptr */
2395
            /*  ptrdiff_t is generated but not named */
2396
614k
            ssize = (die_info_end >= info_ptr)?
2397
614k
                (die_info_end - info_ptr): 0;
2398
614k
            if (sizeofval > ssize) {
2399
1.33k
                dwarfstring m;
2400
2401
1.33k
                dwarfstring_constructor(&m);
2402
1.33k
                dwarfstring_append_printf_u(&m,
2403
1.33k
                    "DW_DLE_NEXT_DIE_PAST_END:"
2404
1.33k
                    " the DIE value just checked is %u"
2405
1.33k
                    " bytes long, and that would extend"
2406
1.33k
                    " past the end of the section.",
2407
1.33k
                    sizeofval);
2408
1.33k
                _dwarf_error_string(dbg, error,
2409
1.33k
                    DW_DLE_NEXT_DIE_PAST_END,
2410
1.33k
                    dwarfstring_string(&m));
2411
1.33k
                dwarfstring_destructor(&m);
2412
1.33k
                return DW_DLV_ERROR;
2413
1.33k
            }
2414
612k
            info_ptr += sizeofval;
2415
612k
            if (info_ptr > die_info_end) {
2416
0
                dwarfstring m;
2417
2418
0
                dwarfstring_constructor(&m);
2419
0
                dwarfstring_append_printf_u(&m,
2420
0
                    "DW_DLE_NEXT_DIE_PAST_END:"
2421
0
                    " the DIE value just checked is %u"
2422
0
                    " bytes long, and puts us past"
2423
0
                    " the end of the section",
2424
0
                    sizeofval);
2425
0
                dwarfstring_append_printf_u(&m,
2426
0
                    " which is 0x%x",
2427
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_end);
2428
0
                _dwarf_error_string(dbg, error,
2429
0
                    DW_DLE_NEXT_DIE_PAST_END,
2430
0
                    dwarfstring_string(&m));
2431
0
                dwarfstring_destructor(&m);
2432
                /*  More than one-past-end indicates a bug somewhere,
2433
                    likely bad dwarf generation. */
2434
0
                return DW_DLV_ERROR;
2435
0
            }
2436
612k
        }
2437
849k
    }
2438
608k
    *next_die_ptr_out = info_ptr;
2439
608k
    return DW_DLV_OK;
2440
610k
}
2441
2442
/*  Multiple TAGs are in fact compile units.
2443
    Allow them all.
2444
    Return non-zero if a CU tag.
2445
    Else return 0.
2446
*/
2447
static int
2448
is_cu_tag(int t)
2449
48.6k
{
2450
48.6k
    if (t == DW_TAG_compile_unit  ||
2451
48.6k
        t == DW_TAG_partial_unit  ||
2452
48.6k
        t == DW_TAG_skeleton_unit ||
2453
48.6k
        t == DW_TAG_type_unit) {
2454
48.3k
        return 1;
2455
48.3k
    }
2456
306
    return 0;
2457
48.6k
}
2458
2459
/*  Given a Dwarf_Debug dbg, and a Dwarf_Die die, it returns
2460
    a Dwarf_Die for the sibling of die.  In case die is NULL,
2461
    it returns (thru ptr) a Dwarf_Die for the first die in the current
2462
    cu in dbg.  Returns DW_DLV_ERROR on error.
2463
2464
    It is assumed that every sibling chain including those with
2465
    only one element is terminated with a NULL die, except a
2466
    chain with only a NULL die.
2467
2468
    The algorithm moves from one die to the adjacent one.  It
2469
    returns when the depth of children it sees equals the number
2470
    of sibling chain terminations.  A single count, child_depth
2471
    is used to track the depth of children and sibling terminations
2472
    encountered.  Child_depth is incremented when a die has the
2473
    Has-Child flag set unless the child happens to be a NULL die.
2474
    Child_depth is decremented when a die has Has-Child false,
2475
    and the adjacent die is NULL.  Algorithm returns when
2476
    child_depth is 0.
2477
2478
    **NOTE: Do not modify input die, since it is used at the end.
2479
2480
 *  This is the correct form.  On calling with 'die' NULL,
2481
    we cannot tell if this is debug_info or debug_types, so
2482
    we must be informed!. */
2483
int
2484
dwarf_siblingof_b(Dwarf_Debug dbg,
2485
    Dwarf_Die    die,
2486
    Dwarf_Bool   is_info,
2487
    Dwarf_Die   *caller_ret_die,
2488
    Dwarf_Error *error)
2489
279k
{
2490
279k
    int res = 0;
2491
279k
    Dwarf_CU_Context context = 0;
2492
2493
279k
    CHECK_DBG(dbg,error,"dwarf_siblingof_b()");
2494
279k
    if (die) {
2495
260k
        CHECK_DIE(die,DW_DLV_ERROR);
2496
260k
        context = die->di_cu_context;
2497
        /*  Ignore is_info passed-in, we have the correct
2498
            value in cu_context.  */
2499
260k
        is_info =  die->di_cu_context->cc_is_info;
2500
260k
    } else {
2501
        /*  This is the pre-0.9.0 way, and is assuming
2502
            that the 'dis' has the correct cu context.
2503
            Which might not be true if a caller
2504
            used dwarf_next_cu_header_d() twice in a
2505
            row before calling dwarf_siblingof_b().
2506
            Use dwarf_next_cu_header_e() instead of
2507
            dwarf_next_cu_header_d() */
2508
19.1k
        context = is_info? dbg->de_info_reading.de_cu_context:
2509
19.1k
            dbg->de_types_reading.de_cu_context;
2510
19.1k
    }
2511
279k
    res = _dwarf_siblingof_internal(dbg,die,
2512
279k
        context, is_info,caller_ret_die,error);
2513
279k
    return res;
2514
279k
}
2515
2516
int
2517
dwarf_siblingof_c(Dwarf_Die die,
2518
    Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2519
1.85k
{
2520
1.85k
    int res = 0;
2521
1.85k
    Dwarf_Debug dbg = 0;
2522
1.85k
    Dwarf_Bool is_info = FALSE;
2523
2524
1.85k
    CHECK_DIE(die,DW_DLV_ERROR);
2525
1.85k
    dbg =  die->di_cu_context->cc_dbg;
2526
1.85k
    is_info =  die->di_cu_context->cc_is_info;
2527
1.85k
    res = _dwarf_siblingof_internal(dbg,die,
2528
1.85k
        die->di_cu_context, is_info,
2529
1.85k
        caller_ret_die,error);
2530
1.85k
    return res;
2531
1.85k
}
2532
2533
static int
2534
dw_start_load_root_die(Dwarf_Debug dbg,
2535
    Dwarf_CU_Context context,
2536
    Dwarf_Bool is_info,
2537
    Dwarf_Small *dataptr,
2538
    Dwarf_Byte_Ptr *die_info_ptr,
2539
    Dwarf_Byte_Ptr *die_info_end,
2540
    Dwarf_Error *error)
2541
57.1k
{
2542
    /*  Find root die of cu */
2543
    /*  die_info_end is untouched here, need not be set in this
2544
        branch. */
2545
57.1k
    Dwarf_Off off2 = 0;
2546
57.1k
    Dwarf_Unsigned headerlen = 0;
2547
57.1k
    Dwarf_Byte_Ptr cu_info_start = 0;
2548
57.1k
    int cres = 0;
2549
2550
    /*  If we've not loaded debug_info
2551
        context will be NULL. */
2552
57.1k
    if (!context) {
2553
0
        local_dealloc_cu_context(dbg,context);
2554
0
        _dwarf_error_string(dbg,error,
2555
0
            DW_DLE_DBG_NO_CU_CONTEXT,
2556
0
            "DW_DLE_DBG_NO_CU_CONTEXT:"
2557
0
            " Setting up a new CU failed loading root die");
2558
0
        return DW_DLV_ERROR;
2559
0
    }
2560
57.1k
    off2 = context->cc_debug_offset;
2561
57.1k
    cu_info_start = dataptr + off2;
2562
57.1k
    cres = _dwarf_length_of_cu_header(dbg, off2,is_info,
2563
57.1k
        &headerlen,error);
2564
57.1k
    if (cres != DW_DLV_OK) {
2565
0
        return cres;
2566
0
    }
2567
57.1k
    *die_info_ptr = cu_info_start + headerlen;
2568
57.1k
    *die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2569
2570
    /*  Recording the CU die pointer so we can later access
2571
        for special FORMs relating to .debug_str_offsets
2572
        and .debug_addr  */
2573
57.1k
    context->cc_cu_die_offset_present = TRUE;
2574
57.1k
    context->cc_cu_die_global_sec_offset = off2 + headerlen;
2575
2576
57.1k
    return DW_DLV_OK;
2577
57.1k
}
2578
2579
static int
2580
_dwarf_siblingof_internal(Dwarf_Debug dbg,
2581
    Dwarf_Die die,
2582
    Dwarf_CU_Context context,
2583
    Dwarf_Bool is_info,
2584
    Dwarf_Die * caller_ret_die, Dwarf_Error * error)
2585
319k
{
2586
319k
    Dwarf_Die ret_die = 0;
2587
319k
    Dwarf_Byte_Ptr die_info_ptr = 0;
2588
319k
    Dwarf_Byte_Ptr cu_info_start = 0;
2589
2590
    /* die_info_end points 1-past end of die (once set) */
2591
319k
    Dwarf_Byte_Ptr die_info_end = 0;
2592
319k
    Dwarf_Unsigned abbrev_code = 0;
2593
319k
    Dwarf_Unsigned utmp = 0;
2594
319k
    Dwarf_Unsigned highest_code = 0;
2595
319k
    int lres = 0;
2596
319k
    int dieres = 0;
2597
    /* Since die may be NULL, we rely on the input argument. */
2598
319k
    Dwarf_Small *dataptr =  0;
2599
2600
319k
    if (IS_INVALID_DBG(dbg)) {
2601
0
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2602
0
        return DW_DLV_ERROR;
2603
0
    }
2604
319k
    dataptr = is_info? dbg->de_debug_info.dss_data:
2605
319k
        dbg->de_debug_types.dss_data;
2606
319k
    if (!dataptr) {
2607
0
        return DW_DLV_NO_ENTRY;
2608
0
    }
2609
319k
    if (!die) {
2610
57.1k
        dieres = dw_start_load_root_die(dbg,context,is_info,
2611
57.1k
            dataptr,&die_info_ptr,&die_info_end,error);
2612
57.1k
        if (dieres != DW_DLV_OK) {
2613
0
            return dieres;
2614
0
        }
2615
261k
    } else {
2616
        /* Find sibling die. */
2617
261k
        Dwarf_Bool has_child = false;
2618
261k
        Dwarf_Signed child_depth = 0;
2619
2620
        /*  We cannot have a legal die unless debug_info
2621
            was loaded, so
2622
            no need to load debug_info here. */
2623
261k
        CHECK_DIE(die, DW_DLV_ERROR);
2624
2625
261k
        die_info_ptr = die->di_debug_ptr;
2626
261k
        if (*die_info_ptr == 0) {
2627
0
            return DW_DLV_NO_ENTRY;
2628
0
        }
2629
261k
        context = die->di_cu_context;
2630
261k
        cu_info_start = dataptr+ context->cc_debug_offset;
2631
261k
        die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2632
2633
261k
        if ((*die_info_ptr) == 0) {
2634
0
            return DW_DLV_NO_ENTRY;
2635
0
        }
2636
261k
        child_depth = 0;
2637
334k
        do {
2638
334k
            int res2 = 0;
2639
334k
            Dwarf_Byte_Ptr die_info_ptr2 = 0;
2640
2641
334k
            res2 = _dwarf_next_die_info_ptr(die_info_ptr,
2642
334k
                context, die_info_end,
2643
334k
                cu_info_start, true, &has_child,
2644
334k
                &die_info_ptr2,
2645
334k
                error);
2646
334k
            if (res2 != DW_DLV_OK) {
2647
859
                return res2;
2648
859
            }
2649
334k
            if (die_info_ptr2 == die_info_ptr) {
2650
                /*  There is something very wrong, our die value
2651
                    unchanged.  Bad DWARF. */
2652
0
                dwarfstring m;
2653
2654
0
                dwarfstring_constructor(&m);
2655
0
                dwarfstring_append_printf_u(&m,
2656
0
                    "DW_DLE_NEXT_DIE_LOW_ERROR: "
2657
0
                    "Somehow the next die pointer 0x%x",
2658
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2659
0
                dwarfstring_append_printf_u(&m,
2660
0
                    " points before the current die "
2661
0
                    "pointer 0x%x so an "
2662
0
                    "overflow of some sort happened",
2663
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_ptr);
2664
0
                _dwarf_error_string(dbg, error,
2665
0
                    DW_DLE_NEXT_DIE_LOW_ERROR,
2666
0
                    dwarfstring_string(&m));
2667
0
                dwarfstring_destructor(&m);
2668
0
                return DW_DLV_ERROR;
2669
0
            }
2670
334k
            if (die_info_ptr2 < die_info_ptr) {
2671
                /*  There is something very wrong, our die value
2672
                    decreased.  Bad DWARF. */
2673
0
                dwarfstring m;
2674
2675
0
                dwarfstring_constructor(&m);
2676
0
                dwarfstring_append_printf_u(&m,
2677
0
                    "DW_DLE_NEXT_DIE_LOW_ERROR: "
2678
0
                    "Somehow the next die pointer 0x%x",
2679
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2680
0
                dwarfstring_append_printf_u(&m,
2681
0
                    " points before the current die "
2682
0
                    "pointer 0x%x so an "
2683
0
                    "overflow of some sort happened",
2684
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_ptr);
2685
0
                _dwarf_error_string(dbg, error,
2686
0
                    DW_DLE_NEXT_DIE_LOW_ERROR,
2687
0
                    dwarfstring_string(&m));
2688
0
                dwarfstring_destructor(&m);
2689
0
                return DW_DLV_ERROR;
2690
0
            }
2691
334k
            if (die_info_ptr2 > die_info_end) {
2692
0
                dwarfstring m;
2693
2694
0
                dwarfstring_constructor(&m);
2695
0
                dwarfstring_append_printf_u(&m,
2696
0
                    "DW_DLE_NEXT_DIE_PAST_END: "
2697
0
                    "the next DIE at 0x%x",
2698
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_ptr2);
2699
0
                dwarfstring_append_printf_u(&m,
2700
0
                    " would be past "
2701
0
                    " the end of the section (0x%x),"
2702
0
                    " which is an error.",
2703
0
                    (Dwarf_Unsigned)(uintptr_t)die_info_end);
2704
0
                _dwarf_error_string(dbg, error,
2705
0
                    DW_DLE_NEXT_DIE_PAST_END,
2706
0
                    dwarfstring_string(&m));
2707
0
                dwarfstring_destructor(&m);
2708
0
                return DW_DLV_ERROR;
2709
0
            }
2710
334k
            die_info_ptr = die_info_ptr2;
2711
2712
            /*  die_info_end is one past end. Do not read it!
2713
                A test for '!= die_info_end'  would work as well,
2714
                but perhaps < reads more like the meaning. */
2715
334k
            if (die_info_ptr < die_info_end) {
2716
331k
                if ((*die_info_ptr) == 0 && has_child) {
2717
10.2k
                    die_info_ptr++;
2718
10.2k
                    has_child = false;
2719
10.2k
                }
2720
331k
            }
2721
2722
            /*  die_info_ptr can be one-past-end.  */
2723
334k
            if ((die_info_ptr == die_info_end) ||
2724
334k
                ((*die_info_ptr) == 0)) {
2725
                /* We are at the end of a sibling list.
2726
                    get back to the next containing
2727
                    sibling list (looking for a libling
2728
                    list with more on it).
2729
                    */
2730
34.5k
                for (;;) {
2731
34.5k
                    if (child_depth == 0) {
2732
                        /*  Meaning there is no outer list,
2733
                            so stop. */
2734
12.3k
                        break;
2735
12.3k
                    }
2736
22.1k
                    if (die_info_ptr == die_info_end) {
2737
                        /*  September 2016: do not deref
2738
                            if we are past end.
2739
                            If we are at end at this point
2740
                            it means the sibling list
2741
                            inside this CU is not properly
2742
                            terminated.
2743
                            August 2019:
2744
                            We used to declare an error,
2745
                            DW_DLE_SIBLING_LIST_IMPROPER but
2746
                            now we just silently
2747
                            declare this is the end of the list.
2748
                            Each level of a sibling nest should
2749
                            have a single NUL byte, but here
2750
                            things are wrong, the DWARF
2751
                            is corrupt.  */
2752
4.81k
                        return DW_DLV_NO_ENTRY;
2753
4.81k
                    }
2754
17.3k
                    if (*die_info_ptr) {
2755
                        /* We have a real sibling. */
2756
1.07k
                        break;
2757
1.07k
                    }
2758
                    /*  Move out one DIE level.
2759
                        Move past NUL byte marking end of
2760
                        this sibling list. */
2761
16.2k
                    child_depth--;
2762
16.2k
                    die_info_ptr++;
2763
16.2k
                }
2764
315k
            } else {
2765
315k
                child_depth = has_child ?
2766
274k
                    child_depth + 1 : child_depth;
2767
315k
            }
2768
334k
        } while (child_depth != 0);
2769
261k
    }
2770
    /*  die_info_ptr > die_info_end is really a bug (possibly in dwarf
2771
        generation)(but we are past end, no more DIEs here), whereas
2772
        die_info_ptr == die_info_end means 'one past end, no more DIEs
2773
        here'. */
2774
313k
    if (die_info_ptr >= die_info_end) {
2775
3.24k
        return DW_DLV_NO_ENTRY;
2776
3.24k
    }
2777
310k
    if ((*die_info_ptr) == 0) {
2778
        /*  We are not at the end of the section, but a
2779
            valid DIE will not start with a zero byte.
2780
            We will just assume it is a padding byte and is
2781
            not an error.   An error report will appear
2782
            later if actually reading DIEs*/
2783
10.5k
        return DW_DLV_NO_ENTRY;
2784
10.5k
    }
2785
299k
    ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2786
299k
    if (!ret_die) {
2787
0
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2788
0
        return DW_DLV_ERROR;
2789
0
    }
2790
2791
299k
    ret_die->di_is_info = is_info;
2792
299k
    ret_die->di_debug_ptr = die_info_ptr;
2793
299k
    ret_die->di_cu_context =
2794
299k
        die == NULL ? context : die->di_cu_context;
2795
299k
    dieres = _dwarf_leb128_uword_wrapper(dbg,
2796
299k
        &die_info_ptr,die_info_end,&utmp,error);
2797
299k
    if (dieres == DW_DLV_ERROR) {
2798
27
        dwarf_dealloc_die(ret_die);
2799
27
        return dieres;
2800
27
    }
2801
299k
    if (die_info_ptr > die_info_end) {
2802
        /*  We managed to go past the end of the CU!.
2803
            Something is badly wrong. */
2804
0
        dwarf_dealloc_die(ret_die);
2805
0
        _dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
2806
0
        return DW_DLV_ERROR;
2807
0
    }
2808
299k
    abbrev_code = utmp;
2809
299k
    if (abbrev_code == 0) {
2810
        /* Zero means a null DIE */
2811
406
        dwarf_dealloc_die(ret_die);
2812
406
        return DW_DLV_NO_ENTRY;
2813
406
    }
2814
299k
    ret_die->di_abbrev_code = abbrev_code;
2815
299k
    lres = _dwarf_get_abbrev_for_code(ret_die->di_cu_context,
2816
299k
        abbrev_code,
2817
299k
        &ret_die->di_abbrev_list,
2818
299k
        &highest_code,error);
2819
299k
    if (lres == DW_DLV_ERROR) {
2820
4.27k
        dwarf_dealloc_die(ret_die);
2821
4.27k
        return lres;
2822
4.27k
    }
2823
294k
    if (lres == DW_DLV_NO_ENTRY) {
2824
2.16k
        dwarfstring m;
2825
2.16k
        char buf[130];
2826
2827
2.16k
        buf[0] = 0;
2828
2.16k
        dwarfstring_constructor_static(&m,buf,sizeof(buf));
2829
2.16k
        dwarf_dealloc_die(ret_die);
2830
2.16k
        dwarfstring_append_printf_u(&m,
2831
2.16k
            "DW_DLE_DIE_ABBREV_LIST_NULL: "
2832
2.16k
            "There is no abbrev present for code %u"
2833
2.16k
            " in this compilation unit. ",
2834
2.16k
            abbrev_code);
2835
2.16k
        dwarfstring_append_printf_u(&m,
2836
2.16k
            "The highest known code"
2837
2.16k
            " in any compilation unit is %u .",
2838
2.16k
            highest_code);
2839
2.16k
        _dwarf_error_string(dbg, error,
2840
2.16k
            DW_DLE_DIE_ABBREV_LIST_NULL,dwarfstring_string(&m));
2841
2.16k
        dwarfstring_destructor(&m);
2842
2.16k
        return DW_DLV_ERROR;
2843
2.16k
    }
2844
292k
    if (!ret_die->di_abbrev_list->abl_attr) {
2845
32.2k
        int bres = 0;
2846
32.2k
        Dwarf_Byte_Ptr abbrev_ptr =
2847
32.2k
            ret_die->di_abbrev_list->abl_abbrev_ptr;
2848
32.2k
        Dwarf_Byte_Ptr abbrev_end =
2849
32.2k
            _dwarf_calculate_abbrev_section_end_ptr(
2850
32.2k
            ret_die->di_cu_context);
2851
32.2k
        bres = _dwarf_fill_in_attr_form_abtable(
2852
32.2k
            ret_die->di_cu_context,
2853
32.2k
            abbrev_ptr,
2854
32.2k
            abbrev_end,
2855
32.2k
            ret_die->di_abbrev_list,
2856
32.2k
            error);
2857
32.2k
        if (bres != DW_DLV_OK) {
2858
107
            dwarf_dealloc_die(ret_die);
2859
107
            return bres;
2860
107
        }
2861
32.2k
    }
2862
2863
292k
    if (die == NULL && !is_cu_tag(ret_die->di_abbrev_list->abl_tag)) {
2864
306
        dwarf_dealloc_die(ret_die);
2865
306
        _dwarf_error(dbg, error, DW_DLE_FIRST_DIE_NOT_CU);
2866
306
        return DW_DLV_ERROR;
2867
306
    }
2868
292k
    *caller_ret_die = ret_die;
2869
292k
    return DW_DLV_OK;
2870
292k
}
2871
2872
int
2873
dwarf_child(Dwarf_Die die,
2874
    Dwarf_Die * caller_ret_die,
2875
    Dwarf_Error * error)
2876
276k
{
2877
276k
    Dwarf_Byte_Ptr die_info_ptr = 0;
2878
276k
    Dwarf_Byte_Ptr die_info_ptr2 = 0;
2879
2880
    /* die_info_end points one-past-end of die area. */
2881
276k
    Dwarf_Byte_Ptr die_info_end = 0;
2882
276k
    Dwarf_Die ret_die = 0;
2883
276k
    Dwarf_Bool has_die_child = 0;
2884
276k
    Dwarf_Debug dbg;
2885
276k
    Dwarf_Unsigned abbrev_code = 0;
2886
276k
    Dwarf_Unsigned utmp = 0;
2887
276k
    Dwarf_Debug_InfoTypes dis = 0;
2888
276k
    int res = 0;
2889
276k
    Dwarf_CU_Context context = 0;
2890
276k
    int lres = 0;
2891
276k
    Dwarf_Unsigned highest_code = 0;
2892
2893
276k
    CHECK_DIE(die, DW_DLV_ERROR);
2894
276k
    dbg = die->di_cu_context->cc_dbg;
2895
276k
    dis = die->di_is_info? &dbg->de_info_reading:
2896
276k
        &dbg->de_types_reading;
2897
276k
    die_info_ptr = die->di_debug_ptr;
2898
2899
    /*  We are saving a DIE pointer here, but the pointer
2900
        will not be presumed live later, when it is tested. */
2901
276k
    dis->de_last_die = die;
2902
276k
    dis->de_last_di_ptr = die_info_ptr;
2903
2904
    /* NULL die has no child. */
2905
276k
    if ((*die_info_ptr) == 0) {
2906
0
        return DW_DLV_NO_ENTRY;
2907
0
    }
2908
276k
    context = die->di_cu_context;
2909
276k
    die_info_end = _dwarf_calculate_info_section_end_ptr(context);
2910
2911
276k
    res = _dwarf_next_die_info_ptr(die_info_ptr,
2912
276k
        die->di_cu_context,
2913
276k
        die_info_end,
2914
276k
        NULL, false,
2915
276k
        &has_die_child,
2916
276k
        &die_info_ptr2,
2917
276k
        error);
2918
276k
    if (res != DW_DLV_OK) {
2919
1.43k
        return res;
2920
1.43k
    }
2921
274k
    if (die_info_ptr == die_info_end) {
2922
0
        return DW_DLV_NO_ENTRY;
2923
0
    }
2924
274k
    die_info_ptr = die_info_ptr2;
2925
2926
274k
    dis->de_last_di_ptr = die_info_ptr;
2927
2928
274k
    if (!has_die_child) {
2929
        /* Look for end of sibling chain. */
2930
256k
        while (dis->de_last_di_ptr < die_info_end) {
2931
253k
            if (*dis->de_last_di_ptr) {
2932
235k
                break;
2933
235k
            }
2934
17.9k
            ++dis->de_last_di_ptr;
2935
17.9k
        }
2936
238k
        return DW_DLV_NO_ENTRY;
2937
238k
    }
2938
2939
36.3k
    ret_die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
2940
36.3k
    if (!ret_die) {
2941
0
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2942
0
        return DW_DLV_ERROR;
2943
0
    }
2944
36.3k
    ret_die->di_debug_ptr = die_info_ptr;
2945
36.3k
    ret_die->di_cu_context = die->di_cu_context;
2946
36.3k
    ret_die->di_is_info = die->di_is_info;
2947
2948
36.3k
    res =  _dwarf_leb128_uword_wrapper(dbg,&die_info_ptr,
2949
36.3k
        die_info_end, &utmp,error);
2950
36.3k
    if (res != DW_DLV_OK) {
2951
144
        dwarf_dealloc_die(ret_die);
2952
144
        return res;
2953
144
    }
2954
36.2k
    abbrev_code = (Dwarf_Unsigned) utmp;
2955
2956
36.2k
    dis->de_last_di_ptr = die_info_ptr;
2957
2958
36.2k
    if (abbrev_code == 0) {
2959
        /* Look for end of sibling chain */
2960
22.1k
        while (dis->de_last_di_ptr < die_info_end) {
2961
19.9k
            if (*dis->de_last_di_ptr) {
2962
3.60k
                break;
2963
3.60k
            }
2964
16.3k
            ++dis->de_last_di_ptr;
2965
16.3k
        }
2966
2967
        /*  We have arrived at a null DIE,
2968
            at the end of a CU or the end
2969
            of a list of siblings. */
2970
5.78k
        *caller_ret_die = 0;
2971
5.78k
        dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2972
5.78k
        ret_die = 0;
2973
5.78k
        return DW_DLV_NO_ENTRY;
2974
5.78k
    }
2975
30.4k
    ret_die->di_abbrev_code = abbrev_code;
2976
30.4k
    lres = _dwarf_get_abbrev_for_code(ret_die->di_cu_context,
2977
30.4k
        abbrev_code,
2978
30.4k
        &ret_die->di_abbrev_list,
2979
30.4k
        &highest_code,error);
2980
30.4k
    if (lres == DW_DLV_ERROR) {
2981
723
        dwarf_dealloc(dbg, ret_die, DW_DLA_DIE);
2982
723
        ret_die = 0;
2983
723
        return lres;
2984
723
    }
2985
29.7k
    if (lres == DW_DLV_NO_ENTRY) {
2986
2.14k
        dwarfstring m;
2987
2988
2.14k
        dwarfstring_constructor(&m);
2989
2.14k
        dwarf_dealloc_die(ret_die);
2990
2.14k
        ret_die = 0;
2991
2.14k
        dwarfstring_append_printf_u(&m,
2992
2.14k
            "DW_DLE_ABBREV_MISSING: the abbrev code not found "
2993
2.14k
            " in dwarf_child() is %u. ",abbrev_code);
2994
2.14k
        dwarfstring_append_printf_u(&m,
2995
2.14k
            "The highest known code"
2996
2.14k
            " in any compilation unit is %u.",
2997
2.14k
            highest_code);
2998
2.14k
        _dwarf_error_string(dbg, error, DW_DLE_ABBREV_MISSING,
2999
2.14k
            dwarfstring_string(&m));
3000
2.14k
        dwarfstring_destructor(&m);
3001
2.14k
        return DW_DLV_ERROR;
3002
2.14k
    }
3003
27.5k
    if (!ret_die->di_abbrev_list->abl_attr) {
3004
3.28k
        int bres = 0;
3005
3.28k
        Dwarf_Byte_Ptr abbrev_ptr =
3006
3.28k
            ret_die->di_abbrev_list->abl_abbrev_ptr;
3007
3.28k
        Dwarf_Byte_Ptr abbrev_end =
3008
3.28k
            _dwarf_calculate_abbrev_section_end_ptr(
3009
3.28k
            ret_die->di_cu_context);
3010
3.28k
        bres = _dwarf_fill_in_attr_form_abtable(
3011
3.28k
            ret_die->di_cu_context,
3012
3.28k
            abbrev_ptr,
3013
3.28k
            abbrev_end,
3014
3.28k
            ret_die->di_abbrev_list,
3015
3.28k
            error);
3016
3.28k
        if (bres != DW_DLV_OK) {
3017
4
            dwarf_dealloc_die(ret_die);
3018
4
            return bres;
3019
4
        }
3020
3.28k
    }
3021
27.5k
    *caller_ret_die = ret_die;
3022
27.5k
    return DW_DLV_OK;
3023
27.5k
}
3024
3025
/*  Given a (global, not cu_relative) die offset, this returns
3026
    a pointer to a DIE thru *new_die.
3027
    It is up to the caller to do a
3028
    dwarf_dealloc(dbg,*new_die,DW_DLE_DIE);
3029
    The old form only works with debug_info.
3030
    The new _b form works with debug_info or debug_types.
3031
3032
    */
3033
int
3034
dwarf_offdie_b(Dwarf_Debug dbg,
3035
    Dwarf_Off offset, Dwarf_Bool is_info,
3036
    Dwarf_Die * new_die, Dwarf_Error * error)
3037
4.62k
{
3038
4.62k
    Dwarf_CU_Context cu_context = 0;
3039
4.62k
    Dwarf_Small     *dataptr = 0;
3040
4.62k
    Dwarf_Off        new_cu_offset = 0;
3041
4.62k
    Dwarf_Die        die = 0;
3042
4.62k
    Dwarf_Byte_Ptr   info_ptr = 0;
3043
4.62k
    Dwarf_Unsigned   abbrev_code = 0;
3044
4.62k
    Dwarf_Unsigned   utmp = 0;
3045
4.62k
    int              lres = 0;
3046
4.62k
    Dwarf_Debug_InfoTypes dis = 0;
3047
4.62k
    Dwarf_Byte_Ptr   die_info_end = 0;
3048
4.62k
    Dwarf_Unsigned   highest_code = 0;
3049
4.62k
    struct Dwarf_Section_s * secdp = 0;
3050
3051
4.62k
    CHECK_DBG(dbg,error,"dwarf_offdie_b()");
3052
4.62k
    if (is_info) {
3053
3
        dis =&dbg->de_info_reading;
3054
3
        secdp = &dbg->de_debug_info;
3055
3
        dataptr = dbg->de_debug_info.dss_data;
3056
4.62k
    } else {
3057
4.62k
        dis =&dbg->de_types_reading;
3058
4.62k
        secdp = &dbg->de_debug_types;
3059
4.62k
        dataptr = dbg->de_debug_types.dss_data;
3060
4.62k
    }
3061
3062
4.62k
    if (!dataptr) {
3063
0
        lres = _dwarf_load_die_containing_section(dbg,
3064
0
            is_info, error);
3065
0
        if (lres != DW_DLV_OK) {
3066
0
            return lres;
3067
0
        }
3068
0
    }
3069
4.62k
    cu_context = _dwarf_find_CU_Context(dbg, offset,is_info);
3070
4.62k
    if (cu_context == NULL) {
3071
73
        Dwarf_Unsigned section_size = 0;
3072
3073
73
        if (dis->de_cu_context_list_end != NULL) {
3074
73
            new_cu_offset = _dwarf_calculate_next_cu_context_offset(
3075
73
                dis->de_cu_context_list_end);
3076
73
        }/* Else new_cu_offset remains 0, no CUs on list,
3077
            a fresh section setup. */
3078
73
        section_size = secdp->dss_size;
3079
95
        do {
3080
            /*  We do not want this to return cu_die as
3081
                we only want the last one to create DIE,
3082
                and that will be done just below. */
3083
95
            lres = _dwarf_create_a_new_cu_context_record_on_list(
3084
95
                dbg, dis,is_info,section_size,new_cu_offset,
3085
95
                &cu_context,NULL,error);
3086
95
            if (lres != DW_DLV_OK) {
3087
72
                return lres;
3088
72
            }
3089
23
            new_cu_offset =  _dwarf_calculate_next_cu_context_offset(
3090
23
                cu_context);
3091
            /*  Not setting dis->de_cu_context, leave
3092
                that unchanged. */
3093
23
        } while (offset >= new_cu_offset);
3094
73
    }
3095
    /*  We have a cu_context for this offset. */
3096
4.55k
    die_info_end = _dwarf_calculate_info_section_end_ptr(cu_context);
3097
4.55k
    die = (Dwarf_Die) _dwarf_get_alloc(dbg, DW_DLA_DIE, 1);
3098
4.55k
    if (!die) {
3099
0
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
3100
0
        return DW_DLV_ERROR;
3101
0
    }
3102
4.55k
    die->di_cu_context = cu_context;
3103
4.55k
    die->di_is_info = is_info;
3104
    /*  dataptr above might be stale if we loaded a section
3105
        above.  So access dss_data here. */
3106
4.55k
    if (is_info) {
3107
3
        info_ptr = offset + dbg->de_debug_info.dss_data;
3108
4.54k
    } else {
3109
4.54k
        info_ptr = offset + dbg->de_debug_types.dss_data;
3110
4.54k
    }
3111
4.55k
    die->di_debug_ptr = info_ptr;
3112
4.55k
    lres = _dwarf_leb128_uword_wrapper(dbg,&info_ptr,die_info_end,
3113
4.55k
        &utmp,error);
3114
4.55k
    if (lres != DW_DLV_OK) {
3115
4
        dwarf_dealloc_die(die);
3116
4
        return lres;
3117
4
    }
3118
4.54k
    abbrev_code = utmp;
3119
4.54k
    if (abbrev_code == 0) {
3120
        /* we are at a null DIE (or there is a bug). */
3121
146
        dwarf_dealloc_die(die);
3122
146
        die = 0;
3123
146
        return DW_DLV_NO_ENTRY;
3124
146
    }
3125
4.40k
    die->di_abbrev_code = abbrev_code;
3126
4.40k
    lres = _dwarf_get_abbrev_for_code(cu_context, abbrev_code,
3127
4.40k
        &die->di_abbrev_list,
3128
4.40k
        &highest_code,error);
3129
4.40k
    if (lres == DW_DLV_ERROR) {
3130
33
        dwarf_dealloc_die(die);
3131
33
        die = 0;
3132
33
        return lres;
3133
33
    }
3134
4.36k
    if (lres == DW_DLV_NO_ENTRY) {
3135
41
        dwarfstring m;
3136
3137
41
        dwarf_dealloc_die(die);
3138
41
        die = 0;
3139
41
        dwarfstring_constructor(&m);
3140
41
        dwarfstring_append_printf_u(&m,
3141
41
            "DW_DLE_DIE_ABBREV_LIST_NULL: "
3142
41
            "There is no abbrev present for code %u"
3143
41
            " in this compilation unit"
3144
41
            " when calling dwarf_offdie_b(). ",
3145
41
            abbrev_code);
3146
41
        dwarfstring_append_printf_u(&m,
3147
41
            "The highest known code "
3148
41
            "in any compilation unit is %u .",
3149
41
            highest_code);
3150
41
        _dwarf_error_string(dbg, error,
3151
41
            DW_DLE_DIE_ABBREV_LIST_NULL,
3152
41
            dwarfstring_string(&m));
3153
41
        dwarfstring_destructor(&m);
3154
41
        return DW_DLV_ERROR;
3155
41
    }
3156
4.32k
    if (!die->di_abbrev_list->abl_attr) {
3157
62
        int bres = 0;
3158
62
        Dwarf_Byte_Ptr abbrev_ptr =
3159
62
            die->di_abbrev_list->abl_abbrev_ptr;
3160
62
        Dwarf_Byte_Ptr abbrev_end =
3161
62
            _dwarf_calculate_abbrev_section_end_ptr(
3162
62
            die->di_cu_context);
3163
62
        bres = _dwarf_fill_in_attr_form_abtable(
3164
62
            die->di_cu_context,
3165
62
            abbrev_ptr,
3166
62
            abbrev_end,
3167
62
            die->di_abbrev_list,
3168
62
            error);
3169
62
        if (bres != DW_DLV_OK) {
3170
1
            dwarf_dealloc_die(die);
3171
1
            return bres;
3172
1
        }
3173
62
    }
3174
4.32k
    *new_die = die;
3175
4.32k
    return DW_DLV_OK;
3176
4.32k
}
3177
3178
/*  New March 2016.
3179
    Lets one cross check the abbreviations section and
3180
    the DIE information presented  by dwarfdump -i -G -v. */
3181
int
3182
dwarf_die_abbrev_global_offset(Dwarf_Die die,
3183
    Dwarf_Off       * abbrev_goffset,
3184
    Dwarf_Unsigned  * abbrev_count,
3185
    Dwarf_Error*      error)
3186
3.72k
{
3187
3.72k
    Dwarf_Abbrev_List dal = 0;
3188
3.72k
    Dwarf_Debug dbg = 0;
3189
3190
3.72k
    CHECK_DIE(die, DW_DLV_ERROR);
3191
3.72k
    dbg = die->di_cu_context->cc_dbg;
3192
3.72k
    dal = die->di_abbrev_list;
3193
3.72k
    if (!dal) {
3194
0
        _dwarf_error(dbg,error,DW_DLE_DWARF_ABBREV_NULL);
3195
0
        return DW_DLV_ERROR;
3196
0
    }
3197
3.72k
    *abbrev_goffset = dal->abl_goffset;
3198
3.72k
    *abbrev_count = dal->abl_abbrev_count;
3199
3.72k
    return DW_DLV_OK;
3200
3.72k
}
3201
3202
/*  New August 2018.
3203
    Because some real compressed sections
3204
    have .zdebug instead
3205
    of .debug as the leading characters.
3206
    actual_sec_name_out points to a static
3207
    string so so not free it. */
3208
int
3209
dwarf_get_real_section_name(Dwarf_Debug dbg,
3210
    const char  *std_section_name,
3211
    const char **actual_sec_name_out,
3212
    Dwarf_Small *marked_zcompressed, /* zdebug */
3213
    Dwarf_Small *marked_zlib_compressed, /* ZLIB string */
3214
    Dwarf_Small *marked_shf_compressed, /* SHF_COMPRESSED */
3215
    Dwarf_Unsigned *compressed_length,
3216
    Dwarf_Unsigned *uncompressed_length,
3217
    Dwarf_Error *error)
3218
0
{
3219
0
    unsigned i = 0;
3220
0
    char tbuf[100] = {0};
3221
0
    size_t std_sec_name_len = 0;
3222
3223
0
    CHECK_DBG(dbg,error,"dwarf_get_real_section_name()");
3224
0
    if (!std_section_name || 0 == std_section_name[0]) {
3225
0
        _dwarf_error_string(dbg,error,DW_DLE_SECTION_NAME_BIG,
3226
0
            "DW_DLE_SECTION_NAME_BIG: Actually the "
3227
0
            "section name is empty, not big.");
3228
0
        return DW_DLV_ERROR;
3229
0
    }
3230
0
    std_sec_name_len = strlen(std_section_name);
3231
    /*  std_section_name never has the .dwo on the end,
3232
        so allow for that and allow one (arbitrarily) more. */
3233
0
    if ((std_sec_name_len + 5) < sizeof(tbuf)) {
3234
0
        _dwarf_safe_strcpy(tbuf,sizeof(tbuf),
3235
0
            std_section_name,std_sec_name_len);
3236
0
        _dwarf_safe_strcpy(tbuf+std_sec_name_len,
3237
0
            sizeof(tbuf)-std_sec_name_len,
3238
0
            ".dwo",4);
3239
0
    }
3240
0
    for (i=0; i < dbg->de_debug_sections_total_entries; i++) {
3241
0
        struct Dwarf_dbg_sect_s *sdata = &dbg->de_debug_sections[i];
3242
0
        struct Dwarf_Section_s *section = sdata->ds_secdata;
3243
0
        const char *std = section->dss_standard_name;
3244
3245
0
        if (!strcmp(std,std_section_name) ||
3246
0
            !strcmp(std,tbuf)) {
3247
0
            const char *used = section->dss_name;
3248
0
            *actual_sec_name_out = used;
3249
0
            if (sdata->ds_have_zdebug) {
3250
0
                *marked_zcompressed = TRUE;
3251
0
            }
3252
0
            if (section->dss_ZLIB_compressed) {
3253
0
                *marked_zlib_compressed = TRUE;
3254
0
                if (uncompressed_length) {
3255
0
                    *uncompressed_length =
3256
0
                        section->dss_uncompressed_length;
3257
0
                }
3258
0
                if (compressed_length) {
3259
0
                    *compressed_length =
3260
0
                        section->dss_compressed_length;
3261
0
                }
3262
0
            }
3263
0
            if (section->dss_shf_compressed) {
3264
0
                *marked_shf_compressed = TRUE;
3265
0
                if (uncompressed_length) {
3266
0
                    *uncompressed_length =
3267
0
                        section->dss_uncompressed_length;
3268
0
                }
3269
0
                if (compressed_length) {
3270
0
                    *compressed_length =
3271
0
                        section->dss_compressed_length;
3272
0
                }
3273
0
            }
3274
0
            return DW_DLV_OK;
3275
0
        }
3276
0
    }
3277
0
    return DW_DLV_NO_ENTRY;
3278
0
}
3279
/*  This is useful when printing DIE data.
3280
    The string pointer returned must not be freed.
3281
    With non-elf objects it is possible the
3282
    string returned might be empty or NULL,
3283
    so callers should be prepared for that kind
3284
    of return. */
3285
int
3286
dwarf_get_die_section_name(Dwarf_Debug dbg,
3287
    Dwarf_Bool    is_info,
3288
    const char ** sec_name,
3289
    Dwarf_Error * error)
3290
0
{
3291
0
    struct Dwarf_Section_s *sec = 0;
3292
3293
0
    CHECK_DBG(dbg,error,"dwarf_get_die_section_name()");
3294
0
    if (is_info) {
3295
0
        sec = &dbg->de_debug_info;
3296
0
    } else {
3297
0
        sec = &dbg->de_debug_types;
3298
0
    }
3299
0
    if (sec->dss_size == 0) {
3300
        /* We don't have such a  section at all. */
3301
0
        return DW_DLV_NO_ENTRY;
3302
0
    }
3303
0
    *sec_name = sec->dss_name;
3304
0
    return DW_DLV_OK;
3305
0
}
3306
3307
/* This one assumes is_info not known to caller but a DIE is known. */
3308
int
3309
dwarf_get_die_section_name_b(Dwarf_Die die,
3310
    const char ** sec_name,
3311
    Dwarf_Error * error)
3312
0
{
3313
0
    Dwarf_CU_Context context = 0;
3314
0
    Dwarf_Bool is_info = 0;
3315
0
    Dwarf_Debug dbg = 0;
3316
3317
0
    CHECK_DIE(die, DW_DLV_ERROR);
3318
0
    context = die->di_cu_context;
3319
0
    dbg = context->cc_dbg;
3320
0
    is_info = context->cc_is_info;
3321
0
    return dwarf_get_die_section_name(dbg,is_info,sec_name,error);
3322
0
}