Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_findfuncbypc.c
Line
Count
Source
1
/* Copyright 2021 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
// #include <config.h>
13
#include <stdint.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
#include <sys/types.h>
18
#include <unistd.h>
19
20
/*
21
 * Libdwarf library callers can only use these headers.
22
 */
23
#include "dwarf.h"
24
#include "libdwarf.h"
25
26
#define DW_PR_DUx "llx"
27
#define DW_PR_DSx "llx"
28
#define DW_PR_DUu "llu"
29
#define DW_PR_DSd "lld"
30
31
65.2k
#define TRUE 1
32
359k
#define FALSE 0
33
static int unittype = DW_UT_compile;
34
static Dwarf_Bool g_is_info = FALSE;
35
36
int cu_version_stamp = 0;
37
int cu_offset_size = 0;
38
39
struct srcfilesdata {
40
  char **srcfiles;
41
  Dwarf_Signed srcfilescount;
42
  int srcfilesres;
43
};
44
struct target_data_s {
45
  Dwarf_Debug td_dbg;
46
  Dwarf_Unsigned td_target_pc; /* from argv */
47
  int td_print_details;        /* from argv */
48
  int td_reportallfound;       /* from argv */
49
50
  /*  cu die data. */
51
  Dwarf_Unsigned td_cu_lowpc;
52
  Dwarf_Unsigned td_cu_highpc;
53
  int td_cu_haslowhighpc;
54
  Dwarf_Die td_cu_die;
55
  char *td_cu_name;
56
  char *td_cu_comp_dir;
57
  Dwarf_Unsigned td_cu_number;
58
  struct srcfilesdata td_cu_srcfiles;
59
  Dwarf_Unsigned td_cu_ranges_base;
60
61
  Dwarf_Off td_ranges_offset;
62
  char *td_subprog_name;
63
  Dwarf_Unsigned td_subprog_fileindex;
64
  Dwarf_Die td_subprog_die;
65
  Dwarf_Unsigned td_subprog_lowpc;
66
  Dwarf_Unsigned td_subprog_highpc;
67
  int td_subprog_haslowhighpc;
68
  Dwarf_Unsigned td_subprog_lineaddr;
69
  Dwarf_Unsigned td_subprog_lineno;
70
  char *td_subprog_srcfile; /* dealloc */
71
};
72
710k
#define NOT_THIS_CU 10
73
418k
#define IN_THIS_CU 11
74
861k
#define FOUND_SUBPROG 12
75
76
static int look_for_our_target(Dwarf_Debug dbg,
77
                               struct target_data_s *target_data,
78
                               Dwarf_Error *errp);
79
static int examine_die_data(Dwarf_Debug dbg, int is_info, Dwarf_Die die,
80
                            int level, struct target_data_s *td,
81
                            Dwarf_Error *errp);
82
static int check_comp_dir(Dwarf_Debug dbg, Dwarf_Die die,
83
                          struct target_data_s *td, Dwarf_Error *errp);
84
static int get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die, int is_info,
85
                                int in_level, int cu_number,
86
                                struct target_data_s *td, Dwarf_Error *errp);
87
88
#if 0
89
DW_UT_compile                   0x01  /* DWARF5 */
90
DW_UT_type                      0x02  /* DWARF5 */
91
DW_UT_partial                   0x03  /* DWARF5 */
92
#endif
93
94
15.2k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
95
15.2k
  char filename[256];
96
97
#ifdef DWREGRESSIONTEMP 
98
  /*  under msys2, the /tmp/ results in an open fail,
99
      so we discard the /tmp/. No need for
100
      /tmp/ in Linux anyway. libdwarf regressiontests
101
      use it this way. */
102
  sprintf(filename, "junklibfuzzer.%d", getpid());
103
#else
104
15.2k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
105
15.2k
#endif
106
15.2k
  FILE *fp = fopen(filename, "wb");
107
15.2k
  if (!fp) {
108
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
109
0
        filename);
110
0
    return 0;
111
0
  }
112
15.2k
  fwrite(data, size, 1, fp);
113
15.2k
  fclose(fp);
114
115
15.2k
  Dwarf_Debug dbg = 0;
116
15.2k
  Dwarf_Error error = 0;
117
15.2k
  Dwarf_Handler errhand = 0;
118
15.2k
  Dwarf_Ptr errarg = 0;
119
15.2k
  int i = 0;
120
15.2k
  Dwarf_Unsigned target_pc = 0x1000;
121
15.2k
#define PATH_LEN 2000
122
15.2k
  char real_path[PATH_LEN];
123
15.2k
  struct target_data_s target_data;
124
125
  /*  Added 19 May 2023 so valgrind will not complain
126
      about testing uninitialized values in
127
      check_coup_dir (for example). */
128
15.2k
  memset(&target_data,0,sizeof(target_data));
129
15.2k
  int res =
130
15.2k
      dwarf_init_path(filename, 0, 0, DW_GROUPNUMBER_ANY, 0, 0, &dbg, &error);
131
15.2k
  if (res == DW_DLV_ERROR) {
132
4.33k
    dwarf_dealloc_error(dbg, error);
133
4.33k
    dwarf_finish(dbg);
134
10.9k
  } else {
135
10.9k
    res = look_for_our_target(dbg, &target_data, &error);
136
10.9k
    res = dwarf_finish(dbg);
137
10.9k
  }
138
139
15.2k
  unlink(filename);
140
15.2k
  return 0;
141
15.2k
}
142
143
static int read_line_data(Dwarf_Debug dbg, struct target_data_s *td,
144
1.08k
                          Dwarf_Error *errp) {
145
1.08k
  int res = 0;
146
1.08k
  Dwarf_Unsigned line_version = 0;
147
1.08k
  Dwarf_Small table_type = 0;
148
1.08k
  Dwarf_Line_Context line_context = 0;
149
1.08k
  Dwarf_Signed i = 0;
150
1.08k
  Dwarf_Signed baseindex = 0;
151
1.08k
  Dwarf_Signed endindex = 0;
152
1.08k
  Dwarf_Signed file_count = 0;
153
1.08k
  Dwarf_Unsigned dirindex = 0;
154
155
1.08k
  (void)dbg;
156
1.08k
  res = dwarf_srclines_b(td->td_cu_die, &line_version, &table_type,
157
1.08k
                         &line_context, errp);
158
1.08k
  if (res != DW_DLV_OK) {
159
792
    return res;
160
792
  }
161
294
  if (table_type == 0) {
162
11
    int sres = 0;
163
164
11
    sres = dwarf_srclines_files_indexes(line_context, &baseindex, &file_count,
165
11
                                        &endindex, errp);
166
11
    if (sres != DW_DLV_OK) {
167
0
      dwarf_srclines_dealloc_b(line_context);
168
0
      line_context = 0;
169
0
      return sres;
170
0
    }
171
784
    for (i = baseindex; i < endindex; i++) {
172
773
      Dwarf_Unsigned modtime = 0;
173
773
      Dwarf_Unsigned flength = 0;
174
773
      Dwarf_Form_Data16 *md5data = 0;
175
773
      int vres = 0;
176
773
      const char *name = 0;
177
178
773
      vres = dwarf_srclines_files_data_b(line_context, i, &name, &dirindex,
179
773
                                         &modtime, &flength, &md5data, errp);
180
773
      if (vres != DW_DLV_OK) {
181
0
        dwarf_srclines_dealloc_b(line_context);
182
0
        line_context = 0;
183
0
        return vres;
184
0
      }
185
773
    }
186
11
    dwarf_srclines_dealloc_b(line_context);
187
11
    return DW_DLV_OK;
188
283
  } else if (table_type == 1) {
189
280
    const char *dir_name = 0;
190
280
    int sres = 0;
191
280
    Dwarf_Line *linebuf = 0;
192
280
    Dwarf_Signed linecount = 0;
193
280
    Dwarf_Signed dir_count = 0;
194
280
    Dwarf_Addr prev_lineaddr = 0;
195
280
    Dwarf_Unsigned prev_lineno = 0;
196
280
    char *prev_linesrcfile = 0;
197
198
280
    sres = dwarf_srclines_files_indexes(line_context, &baseindex, &file_count,
199
280
                                        &endindex, errp);
200
280
    if (sres != DW_DLV_OK) {
201
0
      dwarf_srclines_dealloc_b(line_context);
202
0
      line_context = 0;
203
0
      return sres;
204
0
    }
205
23.4k
    for (i = baseindex; i < endindex; i++) {
206
23.1k
      Dwarf_Unsigned dirindexb = 0;
207
23.1k
      Dwarf_Unsigned modtime = 0;
208
23.1k
      Dwarf_Unsigned flength = 0;
209
23.1k
      Dwarf_Form_Data16 *md5data = 0;
210
23.1k
      int vres = 0;
211
23.1k
      const char *name = 0;
212
213
23.1k
      vres = dwarf_srclines_files_data_b(line_context, i, &name, &dirindexb,
214
23.1k
                                         &modtime, &flength, &md5data, errp);
215
23.1k
      if (vres != DW_DLV_OK) {
216
0
        dwarf_srclines_dealloc_b(line_context);
217
0
        line_context = 0;
218
0
        return vres;
219
0
      }
220
23.1k
    }
221
280
    sres = dwarf_srclines_include_dir_count(line_context, &dir_count, errp);
222
280
    if (sres != DW_DLV_OK) {
223
0
      dwarf_srclines_dealloc_b(line_context);
224
0
      line_context = 0;
225
0
      return sres;
226
0
    }
227
228
2.67k
    for (i = 1; i <= dir_count; ++i) {
229
2.39k
      dir_name = 0;
230
2.39k
      sres = dwarf_srclines_include_dir_data(line_context, i, &dir_name, errp);
231
2.39k
      if (sres == DW_DLV_ERROR) {
232
0
        dwarf_srclines_dealloc_b(line_context);
233
0
        line_context = 0;
234
0
        return sres;
235
0
      }
236
2.39k
    }
237
238
280
    sres = dwarf_srclines_from_linecontext(line_context, &linebuf, &linecount,
239
280
                                           errp);
240
280
    if (sres != DW_DLV_OK) {
241
0
      dwarf_srclines_dealloc_b(line_context);
242
0
      line_context = 0;
243
0
      return sres;
244
0
    }
245
306k
    for (i = 0; i < linecount; ++i) {
246
306k
      Dwarf_Addr lineaddr = 0;
247
306k
      Dwarf_Unsigned filenum = 0;
248
306k
      Dwarf_Unsigned lineno = 0;
249
306k
      char *linesrcfile = 0;
250
251
306k
      sres = dwarf_lineno(linebuf[i], &lineno, errp);
252
306k
      if (sres == DW_DLV_ERROR) {
253
0
        if (prev_linesrcfile) {
254
0
          dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
255
0
        }
256
0
        return sres;
257
0
      }
258
306k
      sres = dwarf_line_srcfileno(linebuf[i], &filenum, errp);
259
306k
      if (sres == DW_DLV_ERROR) {
260
0
        if (prev_linesrcfile) {
261
0
          dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
262
0
        }
263
0
        return sres;
264
0
      }
265
306k
      if (filenum) {
266
294k
        filenum -= 1;
267
294k
      }
268
306k
      sres = dwarf_lineaddr(linebuf[i], &lineaddr, errp);
269
306k
      if (sres == DW_DLV_ERROR) {
270
0
        if (prev_linesrcfile) {
271
0
          dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
272
0
        }
273
0
        return sres;
274
0
      }
275
306k
      sres = dwarf_linesrc(linebuf[i], &linesrcfile, errp);
276
306k
      if (sres == DW_DLV_ERROR) {
277
132
        if (prev_linesrcfile) {
278
115
          dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
279
115
        }
280
132
        return sres;
281
132
      }
282
306k
      if (lineaddr > td->td_target_pc) {
283
82
        td->td_subprog_lineaddr = prev_lineaddr;
284
82
        td->td_subprog_lineno = prev_lineno;
285
82
        td->td_subprog_srcfile = prev_linesrcfile;
286
82
        dwarf_dealloc(dbg, linesrcfile, DW_DLA_STRING);
287
82
        return DW_DLV_OK;
288
82
      }
289
306k
      prev_lineaddr = lineaddr;
290
306k
      prev_lineno = lineno;
291
306k
      if (prev_linesrcfile) {
292
293k
        dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
293
293k
      }
294
306k
      prev_linesrcfile = linesrcfile;
295
306k
    }
296
66
    td->td_subprog_lineaddr = prev_lineaddr;
297
66
    td->td_subprog_lineno = prev_lineno;
298
66
    td->td_subprog_srcfile = prev_linesrcfile;
299
66
    dwarf_srclines_dealloc_b(line_context);
300
66
    return DW_DLV_OK;
301
280
  }
302
3
  return DW_DLV_ERROR;
303
294
}
304
305
static int look_for_our_target(Dwarf_Debug dbg, struct target_data_s *td,
306
10.9k
                               Dwarf_Error *errp) {
307
10.9k
  Dwarf_Unsigned cu_header_length = 0;
308
10.9k
  Dwarf_Unsigned abbrev_offset = 0;
309
10.9k
  Dwarf_Half address_size = 0;
310
10.9k
  Dwarf_Half version_stamp = 0;
311
10.9k
  Dwarf_Half offset_size = 0;
312
10.9k
  Dwarf_Half extension_size = 0;
313
10.9k
  Dwarf_Unsigned typeoffset = 0;
314
10.9k
  Dwarf_Half header_cu_type = unittype;
315
10.9k
  Dwarf_Bool is_info = g_is_info;
316
10.9k
  int cu_number = 0;
317
318
10.9k
  for (;; ++cu_number) {
319
10.9k
    Dwarf_Die no_die = 0;
320
10.9k
    Dwarf_Die cu_die = 0;
321
10.9k
    int res = DW_DLV_ERROR;
322
10.9k
    Dwarf_Sig8 signature;
323
324
10.9k
    memset(&signature, 0, sizeof(signature));
325
10.9k
    res = dwarf_next_cu_header_d(dbg, is_info, &cu_header_length,
326
10.9k
                                 &version_stamp, &abbrev_offset, &address_size,
327
10.9k
                                 &offset_size, &extension_size, &signature,
328
10.9k
                                 &typeoffset, 0, &header_cu_type, errp);
329
10.9k
    if (res == DW_DLV_ERROR) {
330
3.99k
      if (errp) {
331
3.99k
        char *em = dwarf_errmsg(*errp);
332
3.99k
      }
333
3.99k
      return DW_DLV_NO_ENTRY;
334
3.99k
    }
335
6.91k
    if (res == DW_DLV_NO_ENTRY) {
336
651
      return DW_DLV_NO_ENTRY;
337
651
    }
338
6.26k
    cu_version_stamp = version_stamp;
339
6.26k
    cu_offset_size = offset_size;
340
6.26k
    res = dwarf_siblingof_b(dbg, no_die, is_info, &cu_die, errp);
341
6.26k
    if (res == DW_DLV_ERROR) {
342
0
      if (errp) {
343
0
        char *em = dwarf_errmsg(*errp);
344
0
      }
345
0
      return res;
346
0
    }
347
6.26k
    if (res == DW_DLV_NO_ENTRY) {
348
511
      return res;
349
511
    }
350
351
5.74k
    td->td_cu_die = cu_die;
352
5.74k
    res = get_die_and_siblings(dbg, cu_die, is_info, 0, cu_number, td, errp);
353
5.74k
    if (res == FOUND_SUBPROG) {
354
1.08k
      read_line_data(dbg, td, errp);
355
1.08k
      if (td->td_reportallfound) {
356
0
        return res;
357
0
      }
358
1.08k
      return res;
359
4.66k
    } else if (res == IN_THIS_CU) {
360
0
      if (errp) {
361
0
          char *em = dwarf_errmsg(*errp);
362
0
      }
363
0
      return res;
364
4.66k
    } else if (res == DW_DLV_ERROR) {
365
2.26k
      if (errp) {
366
2.26k
        char *em = dwarf_errmsg(*errp);
367
2.26k
      }
368
2.26k
      return DW_DLV_ERROR;
369
2.26k
    }
370
2.40k
    return DW_DLV_NO_ENTRY;
371
5.74k
  }
372
10.9k
}
373
374
static int get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die, int is_info,
375
                                int in_level, int cu_number,
376
307k
                                struct target_data_s *td, Dwarf_Error *errp) {
377
307k
  int res = DW_DLV_ERROR;
378
307k
  Dwarf_Die cur_die = in_die;
379
307k
  Dwarf_Die child = 0;
380
381
307k
  td->td_cu_number = cu_number;
382
307k
  res = examine_die_data(dbg, is_info, in_die, in_level, td, errp);
383
307k
  if (res == DW_DLV_ERROR) {
384
365
    return res;
385
365
  }
386
307k
  if (res == DW_DLV_NO_ENTRY) {
387
14.9k
    return res;
388
14.9k
  }
389
292k
  if (res == NOT_THIS_CU) {
390
28
    return res;
391
292k
  } else if (res == FOUND_SUBPROG) {
392
258
    return res;
393
291k
  } else {
394
291k
  }
395
396
10.9M
  for (;;) {
397
10.9M
    Dwarf_Die sib_die = 0;
398
10.9M
    res = dwarf_child(cur_die, &child, errp);
399
10.9M
    if (res == DW_DLV_ERROR) {
400
663
      return res;
401
663
    }
402
10.9M
    if (res == DW_DLV_OK) {
403
301k
      int res2 = 0;
404
405
301k
      res2 = get_die_and_siblings(dbg, child, is_info, in_level + 1, cu_number,
406
301k
                                  td, errp);
407
301k
      if (child != td->td_cu_die && child != td->td_subprog_die) {
408
290k
        dwarf_dealloc(dbg, child, DW_DLA_DIE);
409
290k
      }
410
301k
      if (res2 == FOUND_SUBPROG) {
411
3.46k
        return res2;
412
298k
      } else if (res2 == IN_THIS_CU) {
413
298k
      } else if (res2 == NOT_THIS_CU) {
414
256
        return res2;
415
298k
      } else if (res2 == DW_DLV_ERROR) {
416
53.1k
        return res2;
417
244k
      } else if (res2 == DW_DLV_NO_ENTRY) {
418
230k
      } else { /* DW_DLV_OK */
419
230k
      }
420
244k
      child = 0;
421
244k
    }
422
10.8M
    res = dwarf_siblingof_b(dbg, cur_die, is_info, &sib_die, errp);
423
10.8M
    if (res == DW_DLV_ERROR) {
424
1.08k
      if (errp) {
425
1.08k
          char *em = dwarf_errmsg(*errp);
426
1.08k
      }
427
1.08k
      return res;
428
1.08k
    }
429
10.8M
    if (res == DW_DLV_NO_ENTRY) {
430
232k
      break;
431
232k
    }
432
10.6M
    if (cur_die != in_die) {
433
10.4M
      if (child != td->td_cu_die && child != td->td_subprog_die) {
434
3.03M
        dwarf_dealloc(dbg, cur_die, DW_DLA_DIE);
435
3.03M
      }
436
10.4M
      cur_die = 0;
437
10.4M
    }
438
10.6M
    cur_die = sib_die;
439
10.6M
    res = examine_die_data(dbg, is_info, cur_die, in_level, td, errp);
440
10.6M
    if (res == DW_DLV_ERROR) {
441
146
      return res;
442
10.6M
    } else if (res == DW_DLV_OK) {
443
10.5M
    } else if (res == FOUND_SUBPROG) {
444
828
      return res;
445
86.6k
    } else if (res == NOT_THIS_CU) {
446
83.7k
    } else if (res == IN_THIS_CU) {
447
83.3k
    } else {
448
83.3k
    }
449
10.6M
  }
450
232k
  return DW_DLV_OK;
451
291k
}
452
453
static void dealloc_rest_of_list(Dwarf_Debug dbg, Dwarf_Attribute *attrbuf,
454
67.9k
                                 Dwarf_Signed attrcount, Dwarf_Signed i) {
455
405k
  for (; i < attrcount; ++i) {
456
337k
    dwarf_dealloc_attribute(attrbuf[i]);
457
337k
  }
458
67.9k
  dwarf_dealloc(dbg, attrbuf, DW_DLA_LIST);
459
67.9k
}
460
461
static int getlowhighpc(Dwarf_Die die, int *have_pc_range,
462
                        Dwarf_Addr *lowpc_out, Dwarf_Addr *highpc_out,
463
132k
                        Dwarf_Error *error) {
464
132k
  Dwarf_Addr hipc = 0;
465
132k
  int res = 0;
466
132k
  Dwarf_Half form = 0;
467
132k
  enum Dwarf_Form_Class formclass = 0;
468
469
132k
  *have_pc_range = FALSE;
470
132k
  res = dwarf_lowpc(die, lowpc_out, error);
471
132k
  if (res == DW_DLV_OK) {
472
71.8k
    res = dwarf_highpc_b(die, &hipc, &form, &formclass, error);
473
71.8k
    if (res == DW_DLV_OK) {
474
63.0k
      if (formclass == DW_FORM_CLASS_CONSTANT) {
475
31.6k
        hipc += *lowpc_out;
476
31.6k
      }
477
63.0k
      *highpc_out = hipc;
478
63.0k
      *have_pc_range = TRUE;
479
63.0k
      return DW_DLV_OK;
480
63.0k
    }
481
71.8k
  }
482
69.2k
  return DW_DLV_NO_ENTRY;
483
132k
}
484
485
static int check_subprog_ranges_for_match(Dwarf_Debug dbg, Dwarf_Die die,
486
                                          struct target_data_s *td,
487
                                          int *have_pc_range,
488
                                          Dwarf_Addr *lowpc_out,
489
                                          Dwarf_Addr *highpc_out,
490
7.27k
                                          Dwarf_Error *errp) {
491
7.27k
  int res = 0;
492
7.27k
  Dwarf_Ranges *ranges;
493
7.27k
  Dwarf_Signed ranges_count;
494
7.27k
  Dwarf_Unsigned byte_count;
495
7.27k
  Dwarf_Signed i = 0;
496
7.27k
  Dwarf_Addr baseaddr = 0;
497
7.27k
  Dwarf_Off actualoffset = 0;
498
7.27k
  int done = FALSE;
499
500
7.27k
  res = dwarf_get_ranges_b(dbg, td->td_ranges_offset, die, &actualoffset,
501
7.27k
                           &ranges, &ranges_count, &byte_count, errp);
502
7.27k
  if (res != DW_DLV_OK) {
503
6.55k
    return res;
504
6.55k
  }
505
24.8k
  for (i = 0; i < ranges_count && !done; ++i) {
506
24.1k
    Dwarf_Ranges *cur = ranges + i;
507
24.1k
    Dwarf_Addr lowpc = 0;
508
24.1k
    Dwarf_Addr highpc = 0;
509
24.1k
    switch (cur->dwr_type) {
510
22.9k
    case DW_RANGES_ENTRY:
511
22.9k
      lowpc = cur->dwr_addr1 + baseaddr;
512
22.9k
      highpc = cur->dwr_addr2 + baseaddr;
513
22.9k
      if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
514
22.9k
        break;
515
22.9k
      }
516
64
      *lowpc_out = lowpc;
517
64
      *highpc_out = highpc;
518
64
      *have_pc_range = TRUE;
519
64
      done = TRUE;
520
64
      res = FOUND_SUBPROG;
521
64
      break;
522
804
    case DW_RANGES_ADDRESS_SELECTION:
523
804
      baseaddr = cur->dwr_addr2;
524
804
      break;
525
382
    case DW_RANGES_END:
526
382
      break;
527
0
    default:
528
0
      return DW_DLV_ERROR;
529
24.1k
    }
530
24.1k
  }
531
717
  dwarf_dealloc_ranges(dbg, ranges, ranges_count);
532
717
  return res;
533
717
}
534
535
static int get_name_from_abstract_origin(Dwarf_Debug dbg, int is_info,
536
                                         Dwarf_Die die, char **name,
537
52.3k
                                         Dwarf_Error *errp) {
538
52.3k
  int res = 0;
539
52.3k
  Dwarf_Die abrootdie = 0;
540
52.3k
  Dwarf_Attribute ab_attr = 0;
541
52.3k
  Dwarf_Off ab_offset = 0;
542
543
52.3k
  res = dwarf_attr(die, DW_AT_abstract_origin, &ab_attr, errp);
544
52.3k
  if (res != DW_DLV_OK) {
545
1.81k
    return res;
546
1.81k
  }
547
548
50.5k
  res = dwarf_global_formref(ab_attr, &ab_offset, errp);
549
50.5k
  if (res != DW_DLV_OK) {
550
13.5k
    dwarf_dealloc(dbg, ab_attr, DW_DLA_ATTR);
551
13.5k
    return res;
552
13.5k
  }
553
554
37.0k
  dwarf_dealloc(dbg, ab_attr, DW_DLA_ATTR);
555
37.0k
  res = dwarf_offdie_b(dbg, ab_offset, is_info, &abrootdie, errp);
556
37.0k
  if (res != DW_DLV_OK) {
557
29.2k
    return res;
558
29.2k
  }
559
7.77k
  res = dwarf_diename(abrootdie, name, errp);
560
7.77k
  dwarf_dealloc_die(abrootdie);
561
7.77k
  return res;
562
37.0k
}
563
564
static int check_subprog_details(Dwarf_Debug dbg, int is_info, Dwarf_Die die,
565
                                 struct target_data_s *td,
566
                                 int *have_pc_range_out, Dwarf_Addr *lowpc_out,
567
121k
                                 Dwarf_Addr *highpc_out, Dwarf_Error *errp) {
568
121k
  int res = 0;
569
121k
  Dwarf_Addr lowpc = 0;
570
121k
  Dwarf_Addr highpc = 0;
571
121k
  int finalres = 0;
572
121k
  int have_pc_range = FALSE;
573
574
121k
  res = getlowhighpc(die, &have_pc_range, &lowpc, &highpc, errp);
575
121k
  if (res == DW_DLV_OK) {
576
61.4k
    if (have_pc_range) {
577
61.4k
      int res2 = DW_DLV_OK;
578
61.4k
      char *name = 0;
579
580
61.4k
      if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
581
8.56k
        finalres = DW_DLV_OK;
582
52.8k
      } else {
583
52.8k
        td->td_subprog_die = die;
584
52.8k
        td->td_subprog_lowpc = lowpc;
585
52.8k
        *lowpc_out = lowpc;
586
52.8k
        *highpc_out = highpc;
587
52.8k
        *have_pc_range_out = have_pc_range;
588
52.8k
        td->td_subprog_highpc = highpc;
589
52.8k
        td->td_subprog_haslowhighpc = have_pc_range;
590
52.8k
        res2 = dwarf_diename(die, &name, errp);
591
52.8k
        if (res2 == DW_DLV_OK) {
592
493
          td->td_subprog_name = name;
593
52.3k
        } else {
594
52.3k
          get_name_from_abstract_origin(dbg, is_info, die, &name, errp);
595
52.3k
        }
596
52.8k
        td->td_subprog_name = name;
597
52.8k
        name = 0;
598
52.8k
        finalres = FOUND_SUBPROG;
599
52.8k
      }
600
61.4k
    }
601
61.4k
  }
602
121k
  {
603
121k
    Dwarf_Signed i = 0;
604
121k
    Dwarf_Signed atcount = 0;
605
121k
    Dwarf_Attribute *atlist = 0;
606
607
121k
    res = dwarf_attrlist(die, &atlist, &atcount, errp);
608
121k
    if (res != DW_DLV_OK) {
609
22.0k
      return res;
610
22.0k
    }
611
278k
    for (i = 0; i < atcount; ++i) {
612
246k
      Dwarf_Half atr = 0;
613
246k
      Dwarf_Attribute attrib = atlist[i];
614
615
246k
      res = dwarf_whatattr(attrib, &atr, errp);
616
246k
      if (res != DW_DLV_OK) {
617
0
        dealloc_rest_of_list(dbg, atlist, atcount, i);
618
0
        return res;
619
0
      }
620
246k
      if (atr == DW_AT_ranges) {
621
75.1k
        int res2 = 0;
622
75.1k
        int res4 = 0;
623
75.1k
        Dwarf_Off ret_offset = 0;
624
75.1k
        int has_low_hi = FALSE;
625
75.1k
        Dwarf_Addr low = 0;
626
75.1k
        Dwarf_Addr high = 0;
627
628
75.1k
        res2 = dwarf_global_formref(attrib, &ret_offset, errp);
629
75.1k
        if (res2 != DW_DLV_OK) {
630
67.8k
          dealloc_rest_of_list(dbg, atlist, atcount, i);
631
67.8k
          return res2;
632
67.8k
        }
633
7.27k
        td->td_ranges_offset = ret_offset + td->td_cu_ranges_base;
634
7.27k
        res4 = check_subprog_ranges_for_match(dbg, die, td, &has_low_hi, &low,
635
7.27k
                                              &high, errp);
636
7.27k
        if (res4 == DW_DLV_OK) {
637
653
          continue;
638
653
        }
639
6.62k
        if (res4 == DW_DLV_NO_ENTRY) {
640
6.52k
          continue;
641
6.52k
        }
642
96
        if (res4 == FOUND_SUBPROG) {
643
64
          td->td_subprog_lowpc = lowpc;
644
64
          td->td_subprog_highpc = highpc;
645
64
          td->td_subprog_haslowhighpc = has_low_hi;
646
64
          finalres = FOUND_SUBPROG;
647
64
          continue;
648
64
        }
649
32
        dealloc_rest_of_list(dbg, atlist, atcount, i);
650
32
        return res4;
651
171k
      } else if (atr == DW_AT_decl_file) {
652
4.89k
        int res5 = 0;
653
4.89k
        Dwarf_Unsigned file_index = 0;
654
655
4.89k
        res5 = dwarf_formudata(attrib, &file_index, errp);
656
4.89k
        if (res5 != DW_DLV_OK) {
657
21
          dealloc_rest_of_list(dbg, atlist, atcount, i);
658
21
          return res5;
659
21
        }
660
4.87k
        td->td_subprog_fileindex = file_index;
661
4.87k
      }
662
171k
      dwarf_dealloc(dbg, attrib, DW_DLA_ATTR);
663
171k
    }
664
31.5k
    dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
665
31.5k
  }
666
0
  return finalres;
667
99.4k
}
668
669
static int check_comp_dir(Dwarf_Debug dbg, Dwarf_Die die,
670
10.7k
                          struct target_data_s *td, Dwarf_Error *errp) {
671
10.7k
  int res = 0;
672
10.7k
  int finalres = DW_DLV_NO_ENTRY;
673
10.7k
  int have_pc_range = FALSE;
674
10.7k
  Dwarf_Addr lowpc = 0;
675
10.7k
  Dwarf_Addr highpc = 0;
676
10.7k
  Dwarf_Off real_ranges_offset = 0;
677
10.7k
  int rdone = FALSE;
678
679
10.7k
  res = getlowhighpc(die, &have_pc_range, &lowpc, &highpc, errp);
680
10.7k
  if (res == DW_DLV_OK) {
681
1.64k
    if (have_pc_range) {
682
1.64k
      if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
683
621
        res = NOT_THIS_CU;
684
1.02k
      } else {
685
1.02k
        td->td_cu_lowpc = lowpc;
686
1.02k
        td->td_cu_highpc = highpc;
687
1.02k
        res = IN_THIS_CU;
688
1.02k
      }
689
1.64k
    }
690
1.64k
  }
691
10.7k
  finalres = res;
692
10.7k
  {
693
10.7k
    Dwarf_Signed atcount = 0;
694
10.7k
    Dwarf_Attribute *atlist = 0;
695
10.7k
    Dwarf_Signed j = 0;
696
10.7k
    int alres = 0;
697
698
10.7k
    alres = dwarf_attrlist(die, &atlist, &atcount, errp);
699
10.7k
    if (alres != DW_DLV_OK) {
700
333
      return alres;
701
333
    }
702
59.5k
    for (j = 0; j < atcount; ++j) {
703
49.2k
      Dwarf_Half atr = 0;
704
49.2k
      Dwarf_Attribute attrib = atlist[j];
705
49.2k
      int resb = 0;
706
707
49.2k
      resb = dwarf_whatattr(attrib, &atr, errp);
708
49.2k
      if (resb != DW_DLV_OK) {
709
0
        dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
710
0
        return resb;
711
0
      }
712
49.2k
      if (atr == DW_AT_name) {
713
9.53k
        char *name = 0;
714
9.53k
        resb = dwarf_formstring(attrib, &name, errp);
715
9.53k
        if (resb == DW_DLV_OK) {
716
415
          td->td_cu_name = name;
717
415
        }
718
39.7k
      } else if (atr == DW_AT_comp_dir) {
719
5.22k
        char *name = 0;
720
5.22k
        resb = dwarf_formstring(attrib, &name, errp);
721
5.22k
        if (resb == DW_DLV_OK) {
722
352
          td->td_cu_comp_dir = name;
723
352
        }
724
34.5k
      } else if (atr == DW_AT_rnglists_base || atr == DW_AT_GNU_ranges_base) {
725
813
        Dwarf_Off rbase = 0;
726
727
813
        resb = dwarf_global_formref(attrib, &rbase, errp);
728
813
        if (resb != DW_DLV_OK) {
729
83
          dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
730
83
          return resb;
731
83
        }
732
730
        td->td_cu_ranges_base = rbase;
733
33.7k
      } else if (atr == DW_AT_ranges) {
734
        /* we have actual ranges. */
735
1.57k
        Dwarf_Off rbase = 0;
736
737
1.57k
        resb = dwarf_global_formref(attrib, &rbase, errp);
738
1.57k
        if (resb != DW_DLV_OK) {
739
77
          dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
740
77
          return resb;
741
77
        }
742
1.49k
        real_ranges_offset = rbase;
743
1.49k
        rdone = TRUE;
744
1.49k
      }
745
49.1k
      dwarf_dealloc(dbg, attrib, DW_DLA_ATTR);
746
49.1k
    }
747
10.2k
    dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
748
10.2k
  }
749
10.2k
  if (rdone) {
750
1.48k
    int resr = 0;
751
1.48k
    Dwarf_Ranges *ranges = 0;
752
1.48k
    Dwarf_Signed ranges_count = 0;
753
1.48k
    Dwarf_Unsigned byte_count = 0;
754
1.48k
    Dwarf_Off actualoffset = 0;
755
1.48k
    Dwarf_Signed k = 0;
756
1.48k
    int done = FALSE;
757
758
1.48k
    resr = dwarf_get_ranges_b(dbg, real_ranges_offset, die, &actualoffset,
759
1.48k
                              &ranges, &ranges_count, &byte_count, errp);
760
1.48k
    if (resr != DW_DLV_OK) {
761
855
      return res;
762
855
    }
763
22.8k
    for (k = 0; k < ranges_count && !done; ++k) {
764
22.1k
      Dwarf_Ranges *cur = ranges + k;
765
22.1k
      Dwarf_Addr lowpcr = 0;
766
22.1k
      Dwarf_Addr highpcr = 0;
767
22.1k
      Dwarf_Addr baseaddr = td->td_cu_ranges_base;
768
769
22.1k
      switch (cur->dwr_type) {
770
21.2k
      case DW_RANGES_ENTRY:
771
21.2k
        lowpc = cur->dwr_addr1 + baseaddr;
772
21.2k
        highpc = cur->dwr_addr2 + baseaddr;
773
21.2k
        if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
774
20.9k
          break;
775
20.9k
        }
776
301
        td->td_cu_lowpc = lowpcr;
777
301
        td->td_cu_highpc = highpcr;
778
301
        td->td_cu_haslowhighpc = TRUE;
779
301
        done = TRUE;
780
301
        finalres = IN_THIS_CU;
781
301
        break;
782
621
      case DW_RANGES_ADDRESS_SELECTION:
783
621
        baseaddr = cur->dwr_addr2;
784
621
        break;
785
280
      case DW_RANGES_END:
786
280
        break;
787
0
      default:
788
0
        return DW_DLV_ERROR;
789
22.1k
      }
790
22.1k
    }
791
628
    dwarf_dealloc_ranges(dbg, ranges, ranges_count);
792
628
  }
793
9.41k
  return finalres;
794
10.2k
}
795
796
static int examine_die_data(Dwarf_Debug dbg, int is_info, Dwarf_Die die,
797
                            int level, struct target_data_s *td,
798
10.9M
                            Dwarf_Error *errp) {
799
10.9M
  Dwarf_Half tag = 0;
800
10.9M
  int res = 0;
801
802
10.9M
  res = dwarf_tag(die, &tag, errp);
803
10.9M
  if (res != DW_DLV_OK) {
804
0
    return res;
805
0
  }
806
10.9M
  if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
807
121k
    int have_pc_range = 0;
808
121k
    Dwarf_Addr lowpc = 0;
809
121k
    Dwarf_Addr highpc = 0;
810
811
121k
    res = check_subprog_details(dbg, is_info, die, td, &have_pc_range, &lowpc,
812
121k
                                &highpc, errp);
813
121k
    if (res == FOUND_SUBPROG) {
814
1.08k
      td->td_subprog_die = die;
815
1.08k
      return res;
816
120k
    } else if (res == DW_DLV_ERROR) {
817
419
      return res;
818
119k
    } else if (res == DW_DLV_NO_ENTRY) {
819
      /* impossible? */
820
89.5k
      return res;
821
89.5k
    } else if (res == NOT_THIS_CU) {
822
      /* impossible */
823
0
      return res;
824
30.4k
    } else if (res == IN_THIS_CU) {
825
      /* impossible */
826
0
      return res;
827
30.4k
    } else {
828
      /* DW_DLV_OK */
829
30.4k
    }
830
30.4k
    return DW_DLV_OK;
831
10.8M
  } else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit ||
832
10.7M
             tag == DW_TAG_type_unit) {
833
834
13.1k
    if (level) {
835
2.37k
      return NOT_THIS_CU;
836
2.37k
    }
837
10.7k
    res = check_comp_dir(dbg, die, td, errp);
838
10.7k
    return res;
839
13.1k
  }
840
10.7M
  return DW_DLV_OK;
841
10.9M
}