Debian Codegraph @

fuzz/fuzz_findfuncbypc.c

raw
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 #define TRUE 1
32 #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 #define NOT_THIS_CU 10
73 #define IN_THIS_CU 11
74 #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 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
95 char filepath[256];
96 sprintf(filepath, "/tmp/libfuzzer.%d", getpid());
97
98 FILE *fp = fopen(filepath, "wb");
99 if (!fp) {
100 return 0;
101 }
102 fwrite(data, size, 1, fp);
103 fclose(fp);
104
105 Dwarf_Debug dbg = 0;
106 Dwarf_Error error = 0;
107 Dwarf_Handler errhand = 0;
108 Dwarf_Ptr errarg = 0;
109 int i = 0;
110 Dwarf_Unsigned target_pc = 0x1000;
111 #define PATH_LEN 2000
112 char real_path[PATH_LEN];
113 struct target_data_s target_data;
114
115 /* Added 19 May 2023 so valgrind will not complain
116 about testing uninitialized values in
117 check_coup_dir (for example). */
118 memset(&target_data,0,sizeof(target_data));
119 int res =
120 dwarf_init_path(filepath, 0, 0, DW_GROUPNUMBER_ANY, 0, 0, &dbg, &error);
121 if (res == DW_DLV_ERROR) {
122 dwarf_dealloc_error(dbg, error);
123 dwarf_finish(dbg);
124 } else {
125 res = look_for_our_target(dbg, &target_data, &error);
126 res = dwarf_finish(dbg);
127 }
128
129 unlink(filepath);
130 return 0;
131 }
132
133 static int read_line_data(Dwarf_Debug dbg, struct target_data_s *td,
134 Dwarf_Error *errp) {
135 int res = 0;
136 Dwarf_Unsigned line_version = 0;
137 Dwarf_Small table_type = 0;
138 Dwarf_Line_Context line_context = 0;
139 Dwarf_Signed i = 0;
140 Dwarf_Signed baseindex = 0;
141 Dwarf_Signed endindex = 0;
142 Dwarf_Signed file_count = 0;
143 Dwarf_Unsigned dirindex = 0;
144
145 (void)dbg;
146 res = dwarf_srclines_b(td->td_cu_die, &line_version, &table_type,
147 &line_context, errp);
148 if (res != DW_DLV_OK) {
149 return res;
150 }
151 if (table_type == 0) {
152 int sres = 0;
153
154 sres = dwarf_srclines_files_indexes(line_context, &baseindex, &file_count,
155 &endindex, errp);
156 if (sres != DW_DLV_OK) {
157 dwarf_srclines_dealloc_b(line_context);
158 line_context = 0;
159 return sres;
160 }
161 for (i = baseindex; i < endindex; i++) {
162 Dwarf_Unsigned modtime = 0;
163 Dwarf_Unsigned flength = 0;
164 Dwarf_Form_Data16 *md5data = 0;
165 int vres = 0;
166 const char *name = 0;
167
168 vres = dwarf_srclines_files_data_b(line_context, i, &name, &dirindex,
169 &modtime, &flength, &md5data, errp);
170 if (vres != DW_DLV_OK) {
171 dwarf_srclines_dealloc_b(line_context);
172 line_context = 0;
173 return vres;
174 }
175 }
176 dwarf_srclines_dealloc_b(line_context);
177 return DW_DLV_OK;
178 } else if (table_type == 1) {
179 const char *dir_name = 0;
180 int sres = 0;
181 Dwarf_Line *linebuf = 0;
182 Dwarf_Signed linecount = 0;
183 Dwarf_Signed dir_count = 0;
184 Dwarf_Addr prev_lineaddr = 0;
185 Dwarf_Unsigned prev_lineno = 0;
186 char *prev_linesrcfile = 0;
187
188 sres = dwarf_srclines_files_indexes(line_context, &baseindex, &file_count,
189 &endindex, errp);
190 if (sres != DW_DLV_OK) {
191 dwarf_srclines_dealloc_b(line_context);
192 line_context = 0;
193 return sres;
194 }
195 for (i = baseindex; i < endindex; i++) {
196 Dwarf_Unsigned dirindexb = 0;
197 Dwarf_Unsigned modtime = 0;
198 Dwarf_Unsigned flength = 0;
199 Dwarf_Form_Data16 *md5data = 0;
200 int vres = 0;
201 const char *name = 0;
202
203 vres = dwarf_srclines_files_data_b(line_context, i, &name, &dirindexb,
204 &modtime, &flength, &md5data, errp);
205 if (vres != DW_DLV_OK) {
206 dwarf_srclines_dealloc_b(line_context);
207 line_context = 0;
208 return vres;
209 }
210 }
211 sres = dwarf_srclines_include_dir_count(line_context, &dir_count, errp);
212 if (sres != DW_DLV_OK) {
213 dwarf_srclines_dealloc_b(line_context);
214 line_context = 0;
215 return sres;
216 }
217
218 for (i = 1; i <= dir_count; ++i) {
219 dir_name = 0;
220 sres = dwarf_srclines_include_dir_data(line_context, i, &dir_name, errp);
221 if (sres == DW_DLV_ERROR) {
222 dwarf_srclines_dealloc_b(line_context);
223 line_context = 0;
224 return sres;
225 }
226 }
227
228 sres = dwarf_srclines_from_linecontext(line_context, &linebuf, &linecount,
229 errp);
230 if (sres != DW_DLV_OK) {
231 dwarf_srclines_dealloc_b(line_context);
232 line_context = 0;
233 return sres;
234 }
235 for (i = 0; i < linecount; ++i) {
236 Dwarf_Addr lineaddr = 0;
237 Dwarf_Unsigned filenum = 0;
238 Dwarf_Unsigned lineno = 0;
239 char *linesrcfile = 0;
240
241 sres = dwarf_lineno(linebuf[i], &lineno, errp);
242 if (sres == DW_DLV_ERROR) {
243 if (prev_linesrcfile) {
244 dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
245 }
246 return sres;
247 }
248 sres = dwarf_line_srcfileno(linebuf[i], &filenum, errp);
249 if (sres == DW_DLV_ERROR) {
250 if (prev_linesrcfile) {
251 dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
252 }
253 return sres;
254 }
255 if (filenum) {
256 filenum -= 1;
257 }
258 sres = dwarf_lineaddr(linebuf[i], &lineaddr, errp);
259 if (sres == DW_DLV_ERROR) {
260 if (prev_linesrcfile) {
261 dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
262 }
263 return sres;
264 }
265 sres = dwarf_linesrc(linebuf[i], &linesrcfile, errp);
266 if (sres == DW_DLV_ERROR) {
267 if (prev_linesrcfile) {
268 dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
269 }
270 return sres;
271 }
272 if (lineaddr > td->td_target_pc) {
273 td->td_subprog_lineaddr = prev_lineaddr;
274 td->td_subprog_lineno = prev_lineno;
275 td->td_subprog_srcfile = prev_linesrcfile;
276 dwarf_dealloc(dbg, linesrcfile, DW_DLA_STRING);
277 return DW_DLV_OK;
278 }
279 prev_lineaddr = lineaddr;
280 prev_lineno = lineno;
281 if (prev_linesrcfile) {
282 dwarf_dealloc(dbg, prev_linesrcfile, DW_DLA_STRING);
283 }
284 prev_linesrcfile = linesrcfile;
285 }
286 td->td_subprog_lineaddr = prev_lineaddr;
287 td->td_subprog_lineno = prev_lineno;
288 td->td_subprog_srcfile = prev_linesrcfile;
289 dwarf_srclines_dealloc_b(line_context);
290 return DW_DLV_OK;
291 }
292 return DW_DLV_ERROR;
293 }
294
295 static int look_for_our_target(Dwarf_Debug dbg, struct target_data_s *td,
296 Dwarf_Error *errp) {
297 Dwarf_Unsigned cu_header_length = 0;
298 Dwarf_Unsigned abbrev_offset = 0;
299 Dwarf_Half address_size = 0;
300 Dwarf_Half version_stamp = 0;
301 Dwarf_Half offset_size = 0;
302 Dwarf_Half extension_size = 0;
303 Dwarf_Unsigned typeoffset = 0;
304 Dwarf_Half header_cu_type = unittype;
305 Dwarf_Bool is_info = g_is_info;
306 int cu_number = 0;
307
308 for (;; ++cu_number) {
309 Dwarf_Die no_die = 0;
310 Dwarf_Die cu_die = 0;
311 int res = DW_DLV_ERROR;
312 Dwarf_Sig8 signature;
313
314 memset(&signature, 0, sizeof(signature));
315 res = dwarf_next_cu_header_d(dbg, is_info, &cu_header_length,
316 &version_stamp, &abbrev_offset, &address_size,
317 &offset_size, &extension_size, &signature,
318 &typeoffset, 0, &header_cu_type, errp);
319 if (res == DW_DLV_ERROR) {
320 if (errp) {
321 char *em = dwarf_errmsg(*errp);
322 }
323 return DW_DLV_NO_ENTRY;
324 }
325 if (res == DW_DLV_NO_ENTRY) {
326 return DW_DLV_NO_ENTRY;
327 }
328 cu_version_stamp = version_stamp;
329 cu_offset_size = offset_size;
330 res = dwarf_siblingof_b(dbg, no_die, is_info, &cu_die, errp);
331 if (res == DW_DLV_ERROR) {
332 if (errp) {
333 char *em = dwarf_errmsg(*errp);
334 }
335 return res;
336 }
337 if (res == DW_DLV_NO_ENTRY) {
338 return res;
339 }
340
341 td->td_cu_die = cu_die;
342 res = get_die_and_siblings(dbg, cu_die, is_info, 0, cu_number, td, errp);
343 if (res == FOUND_SUBPROG) {
344 read_line_data(dbg, td, errp);
345 if (td->td_reportallfound) {
346 return res;
347 }
348 return res;
349 } else if (res == IN_THIS_CU) {
350 if (errp) {
351 char *em = dwarf_errmsg(*errp);
352 }
353 return res;
354 } else if (res == DW_DLV_ERROR) {
355 if (errp) {
356 char *em = dwarf_errmsg(*errp);
357 }
358 return DW_DLV_ERROR;
359 }
360 return DW_DLV_NO_ENTRY;
361 }
362 }
363
364 static int get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die, int is_info,
365 int in_level, int cu_number,
366 struct target_data_s *td, Dwarf_Error *errp) {
367 int res = DW_DLV_ERROR;
368 Dwarf_Die cur_die = in_die;
369 Dwarf_Die child = 0;
370
371 td->td_cu_number = cu_number;
372 res = examine_die_data(dbg, is_info, in_die, in_level, td, errp);
373 if (res == DW_DLV_ERROR) {
374 return res;
375 }
376 if (res == DW_DLV_NO_ENTRY) {
377 return res;
378 }
379 if (res == NOT_THIS_CU) {
380 return res;
381 } else if (res == FOUND_SUBPROG) {
382 return res;
383 } else {
384 }
385
386 for (;;) {
387 Dwarf_Die sib_die = 0;
388 res = dwarf_child(cur_die, &child, errp);
389 if (res == DW_DLV_ERROR) {
390 return res;
391 }
392 if (res == DW_DLV_OK) {
393 int res2 = 0;
394
395 res2 = get_die_and_siblings(dbg, child, is_info, in_level + 1, cu_number,
396 td, errp);
397 if (child != td->td_cu_die && child != td->td_subprog_die) {
398 dwarf_dealloc(dbg, child, DW_DLA_DIE);
399 }
400 if (res2 == FOUND_SUBPROG) {
401 return res2;
402 } else if (res2 == IN_THIS_CU) {
403 } else if (res2 == NOT_THIS_CU) {
404 return res2;
405 } else if (res2 == DW_DLV_ERROR) {
406 return res2;
407 } else if (res2 == DW_DLV_NO_ENTRY) {
408 } else { /* DW_DLV_OK */
409 }
410 child = 0;
411 }
412 res = dwarf_siblingof_b(dbg, cur_die, is_info, &sib_die, errp);
413 if (res == DW_DLV_ERROR) {
414 if (errp) {
415 char *em = dwarf_errmsg(*errp);
416 }
417 return res;
418 }
419 if (res == DW_DLV_NO_ENTRY) {
420 break;
421 }
422 if (cur_die != in_die) {
423 if (child != td->td_cu_die && child != td->td_subprog_die) {
424 dwarf_dealloc(dbg, cur_die, DW_DLA_DIE);
425 }
426 cur_die = 0;
427 }
428 cur_die = sib_die;
429 res = examine_die_data(dbg, is_info, cur_die, in_level, td, errp);
430 if (res == DW_DLV_ERROR) {
431 return res;
432 } else if (res == DW_DLV_OK) {
433 } else if (res == FOUND_SUBPROG) {
434 return res;
435 } else if (res == NOT_THIS_CU) {
436 } else if (res == IN_THIS_CU) {
437 } else {
438 }
439 }
440 return DW_DLV_OK;
441 }
442
443 static void dealloc_rest_of_list(Dwarf_Debug dbg, Dwarf_Attribute *attrbuf,
444 Dwarf_Signed attrcount, Dwarf_Signed i) {
445 for (; i < attrcount; ++i) {
446 dwarf_dealloc_attribute(attrbuf[i]);
447 }
448 dwarf_dealloc(dbg, attrbuf, DW_DLA_LIST);
449 }
450
451 static int getlowhighpc(Dwarf_Die die, int *have_pc_range,
452 Dwarf_Addr *lowpc_out, Dwarf_Addr *highpc_out,
453 Dwarf_Error *error) {
454 Dwarf_Addr hipc = 0;
455 int res = 0;
456 Dwarf_Half form = 0;
457 enum Dwarf_Form_Class formclass = 0;
458
459 *have_pc_range = FALSE;
460 res = dwarf_lowpc(die, lowpc_out, error);
461 if (res == DW_DLV_OK) {
462 res = dwarf_highpc_b(die, &hipc, &form, &formclass, error);
463 if (res == DW_DLV_OK) {
464 if (formclass == DW_FORM_CLASS_CONSTANT) {
465 hipc += *lowpc_out;
466 }
467 *highpc_out = hipc;
468 *have_pc_range = TRUE;
469 return DW_DLV_OK;
470 }
471 }
472 return DW_DLV_NO_ENTRY;
473 }
474
475 static int check_subprog_ranges_for_match(Dwarf_Debug dbg, Dwarf_Die die,
476 struct target_data_s *td,
477 int *have_pc_range,
478 Dwarf_Addr *lowpc_out,
479 Dwarf_Addr *highpc_out,
480 Dwarf_Error *errp) {
481 int res = 0;
482 Dwarf_Ranges *ranges;
483 Dwarf_Signed ranges_count;
484 Dwarf_Unsigned byte_count;
485 Dwarf_Signed i = 0;
486 Dwarf_Addr baseaddr = 0;
487 Dwarf_Off actualoffset = 0;
488 int done = FALSE;
489
490 res = dwarf_get_ranges_b(dbg, td->td_ranges_offset, die, &actualoffset,
491 &ranges, &ranges_count, &byte_count, errp);
492 if (res != DW_DLV_OK) {
493 return res;
494 }
495 for (i = 0; i < ranges_count && !done; ++i) {
496 Dwarf_Ranges *cur = ranges + i;
497 Dwarf_Addr lowpc = 0;
498 Dwarf_Addr highpc = 0;
499 switch (cur->dwr_type) {
500 case DW_RANGES_ENTRY:
501 lowpc = cur->dwr_addr1 + baseaddr;
502 highpc = cur->dwr_addr2 + baseaddr;
503 if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
504 break;
505 }
506 *lowpc_out = lowpc;
507 *highpc_out = highpc;
508 *have_pc_range = TRUE;
509 done = TRUE;
510 res = FOUND_SUBPROG;
511 break;
512 case DW_RANGES_ADDRESS_SELECTION:
513 baseaddr = cur->dwr_addr2;
514 break;
515 case DW_RANGES_END:
516 break;
517 default:
518 return DW_DLV_ERROR;
519 }
520 }
521 dwarf_dealloc_ranges(dbg, ranges, ranges_count);
522 return res;
523 }
524
525 static int get_name_from_abstract_origin(Dwarf_Debug dbg, int is_info,
526 Dwarf_Die die, char **name,
527 Dwarf_Error *errp) {
528 int res = 0;
529 Dwarf_Die abrootdie = 0;
530 Dwarf_Attribute ab_attr = 0;
531 Dwarf_Off ab_offset = 0;
532
533 res = dwarf_attr(die, DW_AT_abstract_origin, &ab_attr, errp);
534 if (res != DW_DLV_OK) {
535 return res;
536 }
537
538 res = dwarf_global_formref(ab_attr, &ab_offset, errp);
539 if (res != DW_DLV_OK) {
540 dwarf_dealloc(dbg, ab_attr, DW_DLA_ATTR);
541 return res;
542 }
543
544 dwarf_dealloc(dbg, ab_attr, DW_DLA_ATTR);
545 res = dwarf_offdie_b(dbg, ab_offset, is_info, &abrootdie, errp);
546 if (res != DW_DLV_OK) {
547 return res;
548 }
549 res = dwarf_diename(abrootdie, name, errp);
550 dwarf_dealloc_die(abrootdie);
551 return res;
552 }
553
554 static int check_subprog_details(Dwarf_Debug dbg, int is_info, Dwarf_Die die,
555 struct target_data_s *td,
556 int *have_pc_range_out, Dwarf_Addr *lowpc_out,
557 Dwarf_Addr *highpc_out, Dwarf_Error *errp) {
558 int res = 0;
559 Dwarf_Addr lowpc = 0;
560 Dwarf_Addr highpc = 0;
561 int finalres = 0;
562 int have_pc_range = FALSE;
563
564 res = getlowhighpc(die, &have_pc_range, &lowpc, &highpc, errp);
565 if (res == DW_DLV_OK) {
566 if (have_pc_range) {
567 int res2 = DW_DLV_OK;
568 char *name = 0;
569
570 if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
571 finalres = DW_DLV_OK;
572 } else {
573 td->td_subprog_die = die;
574 td->td_subprog_lowpc = lowpc;
575 *lowpc_out = lowpc;
576 *highpc_out = highpc;
577 *have_pc_range_out = have_pc_range;
578 td->td_subprog_highpc = highpc;
579 td->td_subprog_haslowhighpc = have_pc_range;
580 res2 = dwarf_diename(die, &name, errp);
581 if (res2 == DW_DLV_OK) {
582 td->td_subprog_name = name;
583 } else {
584 get_name_from_abstract_origin(dbg, is_info, die, &name, errp);
585 }
586 td->td_subprog_name = name;
587 name = 0;
588 finalres = FOUND_SUBPROG;
589 }
590 }
591 }
592 {
593 Dwarf_Signed i = 0;
594 Dwarf_Signed atcount = 0;
595 Dwarf_Attribute *atlist = 0;
596
597 res = dwarf_attrlist(die, &atlist, &atcount, errp);
598 if (res != DW_DLV_OK) {
599 return res;
600 }
601 for (i = 0; i < atcount; ++i) {
602 Dwarf_Half atr = 0;
603 Dwarf_Attribute attrib = atlist[i];
604
605 res = dwarf_whatattr(attrib, &atr, errp);
606 if (res != DW_DLV_OK) {
607 dealloc_rest_of_list(dbg, atlist, atcount, i);
608 return res;
609 }
610 if (atr == DW_AT_ranges) {
611 int res2 = 0;
612 int res4 = 0;
613 Dwarf_Off ret_offset = 0;
614 int has_low_hi = FALSE;
615 Dwarf_Addr low = 0;
616 Dwarf_Addr high = 0;
617
618 res2 = dwarf_global_formref(attrib, &ret_offset, errp);
619 if (res2 != DW_DLV_OK) {
620 dealloc_rest_of_list(dbg, atlist, atcount, i);
621 return res2;
622 }
623 td->td_ranges_offset = ret_offset + td->td_cu_ranges_base;
624 res4 = check_subprog_ranges_for_match(dbg, die, td, &has_low_hi, &low,
625 &high, errp);
626 if (res4 == DW_DLV_OK) {
627 continue;
628 }
629 if (res4 == DW_DLV_NO_ENTRY) {
630 continue;
631 }
632 if (res4 == FOUND_SUBPROG) {
633 td->td_subprog_lowpc = lowpc;
634 td->td_subprog_highpc = highpc;
635 td->td_subprog_haslowhighpc = has_low_hi;
636 finalres = FOUND_SUBPROG;
637 continue;
638 }
639 dealloc_rest_of_list(dbg, atlist, atcount, i);
640 return res4;
641 } else if (atr == DW_AT_decl_file) {
642 int res5 = 0;
643 Dwarf_Unsigned file_index = 0;
644
645 res5 = dwarf_formudata(attrib, &file_index, errp);
646 if (res5 != DW_DLV_OK) {
647 dealloc_rest_of_list(dbg, atlist, atcount, i);
648 return res5;
649 }
650 td->td_subprog_fileindex = file_index;
651 }
652 dwarf_dealloc(dbg, attrib, DW_DLA_ATTR);
653 }
654 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
655 }
656 return finalres;
657 }
658
659 static int check_comp_dir(Dwarf_Debug dbg, Dwarf_Die die,
660 struct target_data_s *td, Dwarf_Error *errp) {
661 int res = 0;
662 int finalres = DW_DLV_NO_ENTRY;
663 int have_pc_range = FALSE;
664 Dwarf_Addr lowpc = 0;
665 Dwarf_Addr highpc = 0;
666 Dwarf_Off real_ranges_offset = 0;
667 int rdone = FALSE;
668
669 res = getlowhighpc(die, &have_pc_range, &lowpc, &highpc, errp);
670 if (res == DW_DLV_OK) {
671 if (have_pc_range) {
672 if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
673 res = NOT_THIS_CU;
674 } else {
675 td->td_cu_lowpc = lowpc;
676 td->td_cu_highpc = highpc;
677 res = IN_THIS_CU;
678 }
679 }
680 }
681 finalres = res;
682 {
683 Dwarf_Signed atcount = 0;
684 Dwarf_Attribute *atlist = 0;
685 Dwarf_Signed j = 0;
686 int alres = 0;
687
688 alres = dwarf_attrlist(die, &atlist, &atcount, errp);
689 if (alres != DW_DLV_OK) {
690 return alres;
691 }
692 for (j = 0; j < atcount; ++j) {
693 Dwarf_Half atr = 0;
694 Dwarf_Attribute attrib = atlist[j];
695 int resb = 0;
696
697 resb = dwarf_whatattr(attrib, &atr, errp);
698 if (resb != DW_DLV_OK) {
699 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
700 return resb;
701 }
702 if (atr == DW_AT_name) {
703 char *name = 0;
704 resb = dwarf_formstring(attrib, &name, errp);
705 if (resb == DW_DLV_OK) {
706 td->td_cu_name = name;
707 }
708 } else if (atr == DW_AT_comp_dir) {
709 char *name = 0;
710 resb = dwarf_formstring(attrib, &name, errp);
711 if (resb == DW_DLV_OK) {
712 td->td_cu_comp_dir = name;
713 }
714 } else if (atr == DW_AT_rnglists_base || atr == DW_AT_GNU_ranges_base) {
715 Dwarf_Off rbase = 0;
716
717 resb = dwarf_global_formref(attrib, &rbase, errp);
718 if (resb != DW_DLV_OK) {
719 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
720 return resb;
721 }
722 td->td_cu_ranges_base = rbase;
723 } else if (atr == DW_AT_ranges) {
724 /* we have actual ranges. */
725 Dwarf_Off rbase = 0;
726
727 resb = dwarf_global_formref(attrib, &rbase, errp);
728 if (resb != DW_DLV_OK) {
729 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
730 return resb;
731 }
732 real_ranges_offset = rbase;
733 rdone = TRUE;
734 }
735 dwarf_dealloc(dbg, attrib, DW_DLA_ATTR);
736 }
737 dwarf_dealloc(dbg, atlist, DW_DLA_LIST);
738 }
739 if (rdone) {
740 int resr = 0;
741 Dwarf_Ranges *ranges = 0;
742 Dwarf_Signed ranges_count = 0;
743 Dwarf_Unsigned byte_count = 0;
744 Dwarf_Off actualoffset = 0;
745 Dwarf_Signed k = 0;
746 int done = FALSE;
747
748 resr = dwarf_get_ranges_b(dbg, real_ranges_offset, die, &actualoffset,
749 &ranges, &ranges_count, &byte_count, errp);
750 if (resr != DW_DLV_OK) {
751 return res;
752 }
753 for (k = 0; k < ranges_count && !done; ++k) {
754 Dwarf_Ranges *cur = ranges + k;
755 Dwarf_Addr lowpcr = 0;
756 Dwarf_Addr highpcr = 0;
757 Dwarf_Addr baseaddr = td->td_cu_ranges_base;
758
759 switch (cur->dwr_type) {
760 case DW_RANGES_ENTRY:
761 lowpc = cur->dwr_addr1 + baseaddr;
762 highpc = cur->dwr_addr2 + baseaddr;
763 if (td->td_target_pc < lowpc || td->td_target_pc >= highpc) {
764 break;
765 }
766 td->td_cu_lowpc = lowpcr;
767 td->td_cu_highpc = highpcr;
768 td->td_cu_haslowhighpc = TRUE;
769 done = TRUE;
770 finalres = IN_THIS_CU;
771 break;
772 case DW_RANGES_ADDRESS_SELECTION:
773 baseaddr = cur->dwr_addr2;
774 break;
775 case DW_RANGES_END:
776 break;
777 default:
778 return DW_DLV_ERROR;
779 }
780 }
781 dwarf_dealloc_ranges(dbg, ranges, ranges_count);
782 }
783 return finalres;
784 }
785
786 static int examine_die_data(Dwarf_Debug dbg, int is_info, Dwarf_Die die,
787 int level, struct target_data_s *td,
788 Dwarf_Error *errp) {
789 Dwarf_Half tag = 0;
790 int res = 0;
791
792 res = dwarf_tag(die, &tag, errp);
793 if (res != DW_DLV_OK) {
794 return res;
795 }
796 if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
797 int have_pc_range = 0;
798 Dwarf_Addr lowpc = 0;
799 Dwarf_Addr highpc = 0;
800
801 res = check_subprog_details(dbg, is_info, die, td, &have_pc_range, &lowpc,
802 &highpc, errp);
803 if (res == FOUND_SUBPROG) {
804 td->td_subprog_die = die;
805 return res;
806 } else if (res == DW_DLV_ERROR) {
807 return res;
808 } else if (res == DW_DLV_NO_ENTRY) {
809 /* impossible? */
810 return res;
811 } else if (res == NOT_THIS_CU) {
812 /* impossible */
813 return res;
814 } else if (res == IN_THIS_CU) {
815 /* impossible */
816 return res;
817 } else {
818 /* DW_DLV_OK */
819 }
820 return DW_DLV_OK;
821 } else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit ||
822 tag == DW_TAG_type_unit) {
823
824 if (level) {
825 return NOT_THIS_CU;
826 }
827 res = check_comp_dir(dbg, die, td, errp);
828 return res;
829 }
830 return DW_DLV_OK;
831 }