/
dwarf.c
2616 lines (2388 loc) · 73.3 KB
/
dwarf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* radare - LGPL - Copyright 2012-2021 - pancake, Fedor Sakharov */
#include <errno.h>
#include <r_bin.h>
#include <r_bin_dwarf.h>
#include <r_core.h>
#define STANDARD_OPERAND_COUNT_DWARF2 9
#define STANDARD_OPERAND_COUNT_DWARF3 12
#define R_BIN_DWARF_INFO 1
// TODO: kill this global
static R_TH_LOCAL bool big_end = false;
/* This macro seems bad regarding to endianess XXX, use only for single byte */
#define READ(buf, type) \
(((buf) + sizeof (type) < buf_end) ? *((type *)(buf)) : 0); \
(buf) += sizeof (type)
#define READ8(buf) \
(((buf) + sizeof (ut8) < buf_end) ? ((ut8 *)buf)[0] : 0); \
(buf) += sizeof (ut8)
#define READ16(buf) \
(((buf) + sizeof (ut16) < buf_end) ? r_read_ble16 (buf, big_end) : 0); \
(buf) += sizeof (ut16)
#define READ32(buf) \
(((buf) + sizeof (ut32) < buf_end) ? r_read_ble32 (buf, big_end) : 0); \
(buf) += sizeof (ut32)
#define READ64(buf) \
(((buf) + sizeof (ut64) < buf_end) ? r_read_ble64 (buf, big_end) : 0); \
(buf) += sizeof (ut64)
static const char *dwarf_tag_name_encodings[] = {
[DW_TAG_null_entry] = "DW_TAG_null_entry",
[DW_TAG_array_type] = "DW_TAG_array_type",
[DW_TAG_class_type] = "DW_TAG_class_type",
[DW_TAG_entry_point] = "DW_TAG_entry_point",
[DW_TAG_enumeration_type] = "DW_TAG_enumeration_type",
[DW_TAG_formal_parameter] = "DW_TAG_formal_parameter",
[DW_TAG_imported_declaration] = "DW_TAG_imported_declaration",
[DW_TAG_label] = "DW_TAG_label",
[DW_TAG_lexical_block] = "DW_TAG_lexical_block",
[DW_TAG_member] = "DW_TAG_member",
[DW_TAG_pointer_type] = "DW_TAG_pointer_type",
[DW_TAG_reference_type] = "DW_TAG_reference_type",
[DW_TAG_compile_unit] = "DW_TAG_compile_unit",
[DW_TAG_string_type] = "DW_TAG_string_type",
[DW_TAG_structure_type] = "DW_TAG_structure_type",
[DW_TAG_subroutine_type] = "DW_TAG_subroutine_type",
[DW_TAG_typedef] = "DW_TAG_typedef",
[DW_TAG_union_type] = "DW_TAG_union_type",
[DW_TAG_unspecified_parameters] = "DW_TAG_unspecified_parameters",
[DW_TAG_variant] = "DW_TAG_variant",
[DW_TAG_common_block] = "DW_TAG_common_block",
[DW_TAG_common_inclusion] = "DW_TAG_common_inclusion",
[DW_TAG_inheritance] = "DW_TAG_inheritance",
[DW_TAG_inlined_subroutine] = "DW_TAG_inlined_subroutine",
[DW_TAG_module] = "DW_TAG_module",
[DW_TAG_ptr_to_member_type] = "DW_TAG_ptr_to_member_type",
[DW_TAG_set_type] = "DW_TAG_set_type",
[DW_TAG_subrange_type] = "DW_TAG_subrange_type",
[DW_TAG_with_stmt] = "DW_TAG_with_stmt",
[DW_TAG_access_declaration] = "DW_TAG_access_declaration",
[DW_TAG_base_type] = "DW_TAG_base_type",
[DW_TAG_catch_block] = "DW_TAG_catch_block",
[DW_TAG_const_type] = "DW_TAG_const_type",
[DW_TAG_constant] = "DW_TAG_constant",
[DW_TAG_enumerator] = "DW_TAG_enumerator",
[DW_TAG_file_type] = "DW_TAG_file_type",
[DW_TAG_friend] = "DW_TAG_friend",
[DW_TAG_namelist] = "DW_TAG_namelist",
[DW_TAG_namelist_item] = "DW_TAG_namelist_item",
[DW_TAG_packed_type] = "DW_TAG_packed_type",
[DW_TAG_subprogram] = "DW_TAG_subprogram",
[DW_TAG_template_type_param] = "DW_TAG_template_type_param",
[DW_TAG_template_value_param] = "DW_TAG_template_value_param",
[DW_TAG_thrown_type] = "DW_TAG_thrown_type",
[DW_TAG_try_block] = "DW_TAG_try_block",
[DW_TAG_variant_part] = "DW_TAG_variant_part",
[DW_TAG_variable] = "DW_TAG_variable",
[DW_TAG_volatile_type] = "DW_TAG_volatile_type",
[DW_TAG_dwarf_procedure] = "DW_TAG_dwarf_procedure",
[DW_TAG_restrict_type] = "DW_TAG_restrict_type",
[DW_TAG_interface_type] = "DW_TAG_interface_type",
[DW_TAG_namespace] = "DW_TAG_namespace",
[DW_TAG_imported_module] = "DW_TAG_imported_module",
[DW_TAG_unspecified_type] = "DW_TAG_unspecified_type",
[DW_TAG_partial_unit] = "DW_TAG_partial_unit",
[DW_TAG_imported_unit] = "DW_TAG_imported_unit",
[DW_TAG_mutable_type] = "DW_TAG_mutable_type",
[DW_TAG_condition] = "DW_TAG_condition",
[DW_TAG_shared_type] = "DW_TAG_shared_type",
[DW_TAG_type_unit] = "DW_TAG_type_unit",
[DW_TAG_rvalue_reference_type] = "DW_TAG_rvalue_reference_type",
[DW_TAG_template_alias] = "DW_TAG_template_alias",
[DW_TAG_LAST] = "DW_TAG_LAST",
};
static const char *dwarf_attr_encodings[] = {
[DW_AT_sibling] = "DW_AT_siblings",
[DW_AT_location] = "DW_AT_location",
[DW_AT_name] = "DW_AT_name",
[DW_AT_ordering] = "DW_AT_ordering",
[DW_AT_byte_size] = "DW_AT_byte_size",
[DW_AT_bit_size] = "DW_AT_bit_size",
[DW_AT_stmt_list] = "DW_AT_stmt_list",
[DW_AT_low_pc] = "DW_AT_low_pc",
[DW_AT_high_pc] = "DW_AT_high_pc",
[DW_AT_language] = "DW_AT_language",
[DW_AT_discr] = "DW_AT_discr",
[DW_AT_discr_value] = "DW_AT_discr_value",
[DW_AT_visibility] = "DW_AT_visibility",
[DW_AT_import] = "DW_AT_import",
[DW_AT_string_length] = "DW_AT_string_length",
[DW_AT_common_reference] = "DW_AT_common_reference",
[DW_AT_comp_dir] = "DW_AT_comp_dir",
[DW_AT_const_value] = "DW_AT_const_value",
[DW_AT_containing_type] = "DW_AT_containing_type",
[DW_AT_default_value] = "DW_AT_default_value",
[DW_AT_inline] = "DW_AT_inline",
[DW_AT_is_optional] = "DW_AT_is_optional",
[DW_AT_lower_bound] = "DW_AT_lower_bound",
[DW_AT_producer] = "DW_AT_producer",
[DW_AT_prototyped] = "DW_AT_prototyped",
[DW_AT_return_addr] = "DW_AT_return_addr",
[DW_AT_start_scope] = "DW_AT_start_scope",
[DW_AT_stride_size] = "DW_AT_stride_size",
[DW_AT_upper_bound] = "DW_AT_upper_bound",
[DW_AT_abstract_origin] = "DW_AT_abstract_origin",
[DW_AT_accessibility] = "DW_AT_accessibility",
[DW_AT_address_class] = "DW_AT_address_class",
[DW_AT_artificial] = "DW_AT_artificial",
[DW_AT_base_types] = "DW_AT_base_types",
[DW_AT_calling_convention] = "DW_AT_calling_convention",
[DW_AT_count] = "DW_AT_count",
[DW_AT_data_member_location] = "DW_AT_data_member_location",
[DW_AT_decl_column] = "DW_AT_decl_column",
[DW_AT_decl_file] = "DW_AT_decl_file",
[DW_AT_decl_line] = "DW_AT_decl_line",
[DW_AT_declaration] = "DW_AT_declaration",
[DW_AT_discr_list] = "DW_AT_discr_list",
[DW_AT_encoding] = "DW_AT_encoding",
[DW_AT_external] = "DW_AT_external",
[DW_AT_frame_base] = "DW_AT_frame_base",
[DW_AT_friend] = "DW_AT_friend",
[DW_AT_identifier_case] = "DW_AT_identifier_case",
[DW_AT_macro_info] = "DW_AT_macro_info",
[DW_AT_namelist_item] = "DW_AT_namelist_item",
[DW_AT_priority] = "DW_AT_priority",
[DW_AT_segment] = "DW_AT_segment",
[DW_AT_specification] = "DW_AT_specification",
[DW_AT_static_link] = "DW_AT_static_link",
[DW_AT_type] = "DW_AT_type",
[DW_AT_use_location] = "DW_AT_use_location",
[DW_AT_variable_parameter] = "DW_AT_variable_parameter",
[DW_AT_virtuality] = "DW_AT_virtuality",
[DW_AT_vtable_elem_location] = "DW_AT_vtable_elem_location",
[DW_AT_allocated] = "DW_AT_allocated",
[DW_AT_associated] = "DW_AT_associated",
[DW_AT_data_location] = "DW_AT_data_location",
[DW_AT_byte_stride] = "DW_AT_byte_stride",
[DW_AT_entry_pc] = "DW_AT_entry_pc",
[DW_AT_use_UTF8] = "DW_AT_use_UTF8",
[DW_AT_extension] = "DW_AT_extension",
[DW_AT_ranges] = "DW_AT_ranges",
[DW_AT_trampoline] = "DW_AT_trampoline",
[DW_AT_call_column] = "DW_AT_call_column",
[DW_AT_call_file] = "DW_AT_call_file",
[DW_AT_call_line] = "DW_AT_call_line",
[DW_AT_description] = "DW_AT_description",
[DW_AT_binary_scale] = "DW_AT_binary_scale",
[DW_AT_decimal_scale] = "DW_AT_decimal_scale",
[DW_AT_small] = "DW_AT_small",
[DW_AT_decimal_sign] = "DW_AT_decimal_sign",
[DW_AT_digit_count] = "DW_AT_digit_count",
[DW_AT_picture_string] = "DW_AT_picture_string",
[DW_AT_mutable] = "DW_AT_mutable",
[DW_AT_threads_scaled] = "DW_AT_threads_scaled",
[DW_AT_explicit] = "DW_AT_explicit",
[DW_AT_object_pointer] = "DW_AT_object_pointer",
[DW_AT_endianity] = "DW_AT_endianity",
[DW_AT_elemental] = "DW_AT_elemental",
[DW_AT_pure] = "DW_AT_pure",
[DW_AT_recursive] = "DW_AT_recursive",
[DW_AT_signature] = "DW_AT_signature",
[DW_AT_main_subprogram] = "DW_AT_main_subprogram",
[DW_AT_data_bit_offset] = "DW_AT_data_big_offset",
[DW_AT_const_expr] = "DW_AT_const_expr",
[DW_AT_enum_class] = "DW_AT_enum_class",
[DW_AT_linkage_name] = "DW_AT_linkage_name",
[DW_AT_string_length_bit_size] = "DW_AT_string_length_bit_size",
[DW_AT_string_length_byte_size] = "DW_AT_string_length_byte_size",
[DW_AT_rank] = "DW_AT_rank",
[DW_AT_str_offsets_base] = "DW_AT_str_offsets_base",
[DW_AT_addr_base] = "DW_AT_addr_base",
[DW_AT_rnglists_base] = "DW_AT_rnglists_base",
[DW_AT_dwo_name] = "DW_AT_dwo_name",
[DW_AT_reference] = "DW_AT_reference",
[DW_AT_rvalue_reference] = "DW_AT_rvalue_reference",
[DW_AT_macros] = "DW_AT_macros",
[DW_AT_call_all_calls] = "DW_AT_call_all_calls",
[DW_AT_call_all_source_calls] = "DW_AT_call_all_source_calls",
[DW_AT_call_all_tail_calls] = "DW_AT_call_all_tail_calls",
[DW_AT_call_return_pc] = "DW_AT_call_return_pc",
[DW_AT_call_value] = "DW_AT_call_value",
[DW_AT_call_origin] = "DW_AT_call_origin",
[DW_AT_call_parameter] = "DW_AT_call_parameter",
[DW_AT_call_pc] = "DW_AT_call_pc",
[DW_AT_call_tail_call] = "DW_AT_call_tail_call",
[DW_AT_call_target] = "DW_AT_call_target",
[DW_AT_call_target_clobbered] = "DW_AT_call_target_clobbered",
[DW_AT_call_data_location] = "DW_AT_call_data_location",
[DW_AT_call_data_value] = "DW_AT_call_data_value",
[DW_AT_noreturn] = "DW_AT_noreturn",
[DW_AT_alignment] = "DW_AT_alignment",
[DW_AT_export_symbols] = "DW_AT_export_symbols",
[DW_AT_deleted] = "DW_AT_deleted",
[DW_AT_defaulted] = "DW_AT_defaulted",
[DW_AT_loclists_base] = "DW_AT_loclists_base",
[DW_AT_lo_user] = "DW_AT_lo_user",
[DW_AT_MIPS_linkage_name] = "DW_AT_MIPS_linkage_name",
[DW_AT_GNU_call_site_value] = "DW_AT_GNU_call_site_value",
[DW_AT_GNU_call_site_data_value] = "DW_AT_GNU_call_site_data_value",
[DW_AT_GNU_call_site_target] = "DW_AT_GNU_call_site_target",
[DW_AT_GNU_call_site_target_clobbered] = "DW_AT_GNU_call_site_target_clobbered",
[DW_AT_GNU_tail_call] = "DW_AT_GNU_tail_call",
[DW_AT_GNU_all_tail_call_sites] = "DW_AT_GNU_all_tail_call_sites",
[DW_AT_GNU_all_call_sites] = "DW_AT_GNU_all_call_sites",
[DW_AT_GNU_all_source_call_sites] = "DW_AT_GNU_all_source_call_sites",
[DW_AT_GNU_macros] = "DW_AT_GNU_macros",
[DW_AT_GNU_deleted] = "DW_AT_GNU_deleted",
[DW_AT_GNU_dwo_name] = "DW_AT_GNU_dwo_name",
[DW_AT_GNU_dwo_id] = "DW_AT_GNU_dwo_id",
[DW_AT_GNU_ranges_base] = "DW_AT_GNU_ranges_base",
[DW_AT_GNU_addr_base] = "DW_AT_GNU_addr_base",
[DW_AT_GNU_pubnames] = "DW_AT_GNU_pubnames",
[DW_AT_GNU_pubtypes] = "DW_AT_GNU_pubtypes",
[DW_AT_hi_user] = "DW_AT_hi_user",
};
static const char *dwarf_attr_form_encodings[] = {
[DW_FORM_addr] = "DW_FORM_addr",
[DW_FORM_block2] = "DW_FORM_block2",
[DW_FORM_block4] = "DW_FORM_block4",
[DW_FORM_data2] = "DW_FORM_data2",
[DW_FORM_data4] = "DW_FORM_data4",
[DW_FORM_data8] = "DW_FORM_data8",
[DW_FORM_string] = "DW_FORM_string",
[DW_FORM_block] = "DW_FORM_block",
[DW_FORM_block1] = "DW_FORM_block1",
[DW_FORM_data1] = "DW_FORM_data1",
[DW_FORM_flag] = "DW_FORM_flag",
[DW_FORM_sdata] = "DW_FORM_sdata",
[DW_FORM_strp] = "DW_FORM_strp",
[DW_FORM_udata] = "DW_FORM_udata",
[DW_FORM_ref_addr] = "DW_FORM_ref_addr",
[DW_FORM_ref1] = "DW_FORM_ref1",
[DW_FORM_ref2] = "DW_FORM_ref2",
[DW_FORM_ref4] = "DW_FORM_ref4",
[DW_FORM_ref8] = "DW_FORM_ref8",
[DW_FORM_ref_udata] = "DW_FORM_ref_udata",
[DW_FORM_indirect] = "DW_FORM_indirect",
[DW_FORM_sec_offset] = "DW_FORM_sec_offset",
[DW_FORM_exprloc] = "DW_FORM_exprloc",
[DW_FORM_flag_present] = "DW_FORM_flag_present",
[DW_FORM_strx] = "DW_FORM_strx",
[DW_FORM_addrx] = "DW_FORM_addrx",
[DW_FORM_ref_sup4] = "DW_FORM_ref_sup4",
[DW_FORM_strp_sup] = "DW_FORM_strp_sup",
[DW_FORM_data16] = "DW_FORM_data16",
[DW_FORM_line_ptr] = "DW_FORM_line_ptr",
[DW_FORM_ref_sig8] = "DW_FORM_ref_sig8",
[DW_FORM_implicit_const] = "DW_FORM_implicit_const",
[DW_FORM_loclistx] = "DW_FORM_loclistx",
[DW_FORM_rnglistx] = "DW_FORM_rnglistx",
[DW_FORM_ref_sup8] = "DW_FORM_ref_sup8",
[DW_FORM_strx1] = "DW_FORM_strx1",
[DW_FORM_strx2] = "DW_FORM_strx2",
[DW_FORM_strx3] = "DW_FORM_strx3",
[DW_FORM_strx4] = "DW_FORM_strx4",
[DW_FORM_addrx1] = "DW_FORM_addrx1",
[DW_FORM_addrx2] = "DW_FORM_addrx2",
[DW_FORM_addrx3] = "DW_FORM_addrx3",
[DW_FORM_addrx4] = "DW_FORM_addrx4",
};
static const char *dwarf_langs[] = {
[DW_LANG_C89] = "C89",
[DW_LANG_C] = "C",
[DW_LANG_Ada83] = "Ada83",
[DW_LANG_C_plus_plus] = "C++",
[DW_LANG_Cobol74] = "Cobol74",
[DW_LANG_Cobol85] = "Cobol85",
[DW_LANG_Fortran77] = "Fortran77",
[DW_LANG_Fortran90] = "Fortran90",
[DW_LANG_Pascal83] = "Pascal83",
[DW_LANG_Modula2] = "Modula2",
[DW_LANG_Java] = "Java",
[DW_LANG_C99] = "C99",
[DW_LANG_Ada95] = "Ada95",
[DW_LANG_Fortran95] = "Fortran95",
[DW_LANG_PLI] = "PLI",
[DW_LANG_ObjC] = "ObjC",
[DW_LANG_ObjC_plus_plus] = "ObjC_plus_plus",
[DW_LANG_UPC] = "UPC",
[DW_LANG_D] = "D",
[DW_LANG_Python] = "Python",
[DW_LANG_Rust] = "Rust",
[DW_LANG_C11] = "C11",
[DW_LANG_Swift] = "Swift",
[DW_LANG_Julia] = "Julia",
[DW_LANG_Dylan] = "Dylan",
[DW_LANG_C_plus_plus_14] = "C++14",
[DW_LANG_Fortran03] = "Fortran03",
[DW_LANG_Fortran08] = "Fortran08"
};
static const char *dwarf_unit_types[] = {
[DW_UT_compile] = "DW_UT_compile",
[DW_UT_type] = "DW_UT_type",
[DW_UT_partial] = "DW_UT_partial",
[DW_UT_skeleton] = "DW_UT_skeleton",
[DW_UT_split_compile] = "DW_UT_split_compile",
[DW_UT_split_type] = "DW_UT_split_type",
[DW_UT_lo_user] = "DW_UT_lo_user",
[DW_UT_hi_user] = "DW_UT_hi_user",
};
static int abbrev_cmp(const void *a, const void *b) {
const RBinDwarfAbbrevDecl *first = a;
const RBinDwarfAbbrevDecl *second = b;
if (first->offset > second->offset) {
return 1;
} else if (first->offset < second->offset) {
return -1;
} else {
return 0;
}
}
static bool is_printable_lang(ut64 attr_code) {
if (attr_code >= sizeof (dwarf_langs) / sizeof (dwarf_langs[0])) {
return false;
}
return dwarf_langs[attr_code];
}
static inline bool is_printable_attr(ut64 attr_code) {
return (attr_code >= DW_AT_sibling && attr_code <= DW_AT_loclists_base) ||
attr_code == DW_AT_MIPS_linkage_name ||
(attr_code >= DW_AT_GNU_call_site_value && attr_code <= DW_AT_GNU_deleted) ||
(attr_code >= DW_AT_GNU_dwo_name && attr_code <= DW_AT_GNU_pubtypes);
}
static inline bool is_printable_form(ut64 form_code) {
return form_code >= DW_FORM_addr && form_code <= DW_FORM_addrx4;
}
static inline bool is_printable_tag(ut64 attr_code) {
return attr_code <= DW_TAG_LAST;
}
static inline bool is_printable_unit_type(ut64 unit_type) {
return unit_type > 0 && unit_type <= DW_UT_split_type;
}
/**
* @brief Reads 64/32 bit unsigned based on format
*
* @param is_64bit Format of the comp unit
* @param buf Pointer to the buffer to read from, to update after read
* @param buf_end To check the boundary /for READ macro/
* @return ut64 Read value
*/
static inline ut64 dwarf_read_offset(bool is_64bit, const ut8 **buf, const ut8 *buf_end) {
ut64 result;
if (!buf || !*buf || !buf_end) {
return 0;
}
if (is_64bit) {
if (*buf + 8 >= buf_end) {
return 0;
}
result = READ64 (*buf);
} else {
if (*buf + 4 >= buf_end) {
return 0;
}
result = (ut64)READ32 (*buf);
}
return result;
}
static inline ut64 dwarf_read_address(size_t size, const ut8 **buf, const ut8 *buf_end) {
ut64 result;
switch (size) {
case 2: result = READ16 (*buf); break;
case 4: result = READ32 (*buf); break;
case 8: result = READ64 (*buf); break;
default:
result = 0;
*buf += size;
R_LOG_WARN ("Unsupported dwarf address size: %u", (int)size);
}
return result;
}
static int add_sdb_include_dir(Sdb *s, const char *incl, int idx) {
if (!s || !incl) {
return false;
}
return sdb_array_set (s, "includedirs", idx, incl, 0);
}
static void line_header_fini(RBinDwarfLineHeader *hdr) {
if (hdr) {
size_t i;
for (i = 0; i < hdr->file_names_count; i ++) {
free (hdr->file_names[i].name);
}
free (hdr->std_opcode_lengths);
free (hdr->file_names);
}
}
// Parses source file header of DWARF version <= 4
static const ut8 *parse_line_header_source(RBinFile *bf, const ut8 *buf, const ut8 *buf_end,
RBinDwarfLineHeader *hdr, Sdb *sdb, int mode, PrintfCallback print) {
int i = 0;
size_t count;
const ut8 *tmp_buf = NULL;
char *fn = NULL;
if (mode == R_MODE_PRINT) {
print (" The Directory Table:\n");
}
while (buf + 1 < buf_end) {
size_t maxlen = R_MIN ((size_t) (buf_end - buf) - 1, 0xfff);
size_t len = r_str_nlen ((const char *)buf, maxlen);
char *str = r_str_ndup ((const char *)buf, len);
if (len < 1 || len >= 0xfff || !str) {
buf += 1;
free (str);
break;
}
if (mode == R_MODE_PRINT) {
print (" %d %s\n", i + 1, str);
}
add_sdb_include_dir (sdb, str, i);
free (str);
i++;
buf += len + 1;
}
tmp_buf = buf;
count = 0;
if (mode == R_MODE_PRINT) {
print ("\n");
print (" The File Name Table:\n");
print (" Entry Dir Time Size Name\n");
}
int entry_index = 1; // used for printing information
for (i = 0; i < 2; i++) {
while (buf + 1 < buf_end) {
size_t maxlen = R_MIN ((size_t) (buf_end - buf - 1), 0xfff);
ut64 id_idx, mod_time, file_len;
free (fn);
fn = r_str_ndup ((const char *)buf, maxlen);
r_str_ansi_strip (fn);
size_t len = strlen (fn);
if (!len) {
buf++;
break;
}
buf += len + 1;
if (buf >= buf_end) {
buf = NULL;
goto beach;
}
buf = r_uleb128 (buf, buf_end - buf, &id_idx, NULL);
if (buf >= buf_end) {
buf = NULL;
goto beach;
}
buf = r_uleb128 (buf, buf_end - buf, &mod_time, NULL);
if (buf >= buf_end) {
buf = NULL;
goto beach;
}
buf = r_uleb128 (buf, buf_end - buf, &file_len, NULL);
if (buf >= buf_end) {
buf = NULL;
goto beach;
}
if (i) {
char *include_dir = NULL, *comp_dir = NULL, *pinclude_dir = NULL;
if (id_idx > 0) {
include_dir = pinclude_dir = sdb_array_get (sdb, "includedirs", id_idx - 1, 0);
if (include_dir && include_dir[0] != '/') {
comp_dir = sdb_get (bf->sdb_addrinfo, "DW_AT_comp_dir", 0);
if (comp_dir) {
include_dir = r_str_newf ("%s/%s/", comp_dir, include_dir);
}
}
} else {
include_dir = pinclude_dir = sdb_get (bf->sdb_addrinfo, "DW_AT_comp_dir", 0);
if (!include_dir) {
include_dir = "./";
}
}
if (hdr->file_names) {
hdr->file_names[count].name = r_str_newf("%s/%s", r_str_get (include_dir), fn);
hdr->file_names[count].id_idx = id_idx;
hdr->file_names[count].mod_time = mod_time;
hdr->file_names[count].file_len = file_len;
}
if (comp_dir) {
R_FREE (include_dir);
R_FREE (comp_dir);
}
R_FREE (pinclude_dir);
}
count++;
if (mode == R_MODE_PRINT && i) {
print (" %d %" PFMT64d " %" PFMT64d " %" PFMT64d " %s\n",
entry_index++, id_idx, mod_time, file_len, fn);
}
}
if (i == 0) {
if (count > 0) {
hdr->file_names = calloc (sizeof (file_entry), count);
} else {
hdr->file_names = NULL;
}
hdr->file_names_count = count;
buf = tmp_buf;
count = 0;
}
}
if (mode == R_MODE_PRINT) {
print ("\n");
}
beach:
free (fn);
sdb_free (sdb);
return buf;
}
#if 0
// TODO DWARF 5 line header parsing, very different from ver. 4
// Because this function needs ability to parse a lot of FORMS just like debug info
// I'll complete this function after completing debug_info parsing and merging
// for the meanwhile I am skipping the space.
static const ut8 *parse_line_header_source_dwarf5(RBinFile *bf, const ut8 *buf, const ut8 *buf_end,
RBinDwarfLineHeader *hdr, Sdb *sdb, int mode) {
// int i = 0;
// size_t count;
// const ut8 *tmp_buf = NULL;
// ut8 dir_entry_count = READ8 (buf);
// // uleb128 pairs
// ut8 dir_count = READ8 (buf);
// // dirs
// ut8 file_entry_count = READ8 (buf);
// // uleb128 pairs
// ut8 file_count = READ8 (buf);
// // file names
// beach:
// sdb_free (sdb);
return NULL;
}
#endif
static const ut8 *parse_line_header(
RBinFile *bf, const ut8 *buf, const ut8 *buf_end,
RBinDwarfLineHeader *hdr, int mode, PrintfCallback print) {
r_return_val_if_fail (hdr && bf && buf, NULL);
hdr->is_64bit = false;
hdr->unit_length = READ32 (buf);
if (hdr->unit_length == DWARF_INIT_LEN_64) {
hdr->unit_length = READ64 (buf);
hdr->is_64bit = true;
}
hdr->version = READ16 (buf);
if (hdr->version == 5) {
hdr->address_size = READ8 (buf);
hdr->segment_selector_size = READ8 (buf);
}
hdr->header_length = dwarf_read_offset(hdr->is_64bit, &buf, buf_end);
const ut8 *tmp_buf = buf; // So I can skip parsing DWARF 5 headers for now
if (buf_end - buf < 8) {
return NULL;
}
hdr->min_inst_len = READ8 (buf);
if (hdr->version >= 4) {
hdr->max_ops_per_inst = READ8 (buf);
}
hdr->default_is_stmt = READ8 (buf);
hdr->line_base = READ (buf, int8_t); // signed
hdr->line_range = READ8 (buf);
hdr->opcode_base = READ8 (buf);
hdr->file_names_count = 0;
hdr->file_names = NULL;
if (mode == R_MODE_PRINT) {
print (" Header information:\n");
print (" Length: %" PFMT64u "\n", hdr->unit_length);
print (" DWARF Version: %d\n", hdr->version);
print (" Header Length: %" PFMT64d "\n", hdr->header_length);
print (" Minimum Instruction Length: %d\n", hdr->min_inst_len);
print (" Maximum Operations per Instruction: %d\n", hdr->max_ops_per_inst);
print (" Initial value of 'is_stmt': %d\n", hdr->default_is_stmt);
print (" Line Base: %d\n", hdr->line_base);
print (" Line Range: %d\n", hdr->line_range);
print (" Opcode Base: %d\n\n", hdr->opcode_base);
}
if (hdr->opcode_base > 0) {
hdr->std_opcode_lengths = calloc (sizeof (ut8), hdr->opcode_base);
if (mode == R_MODE_PRINT) {
print (" Opcodes:\n");
}
size_t i;
for (i = 1; i < hdr->opcode_base; i++) {
if (buf + 2 > buf_end) {
break;
}
hdr->std_opcode_lengths[i] = READ (buf, ut8);
if (mode == R_MODE_PRINT) {
print (" Opcode %u has %d arg\n", (int)i, hdr->std_opcode_lengths[i]);
}
}
if (mode == R_MODE_PRINT) {
print ("\n");
}
} else {
hdr->std_opcode_lengths = NULL;
}
// TODO finish parsing of source files out of DWARF 5 header
// for now we skip
if (hdr->version == 5) {
tmp_buf += hdr->header_length;
R_LOG_WARN ("DWARF5 format is not yet supported by radare2, please contribute");
return tmp_buf;
}
Sdb *sdb = sdb_new (NULL, NULL, 0);
if (!sdb) {
return NULL;
}
if (hdr->version <= 4) {
buf = parse_line_header_source (bf, buf, buf_end, hdr, sdb, mode, print);
} else { // because Version 5 source files are very different
// dwarf5 parsing is not supported
}
return buf;
}
static inline void add_sdb_addrline(Sdb *s, ut64 addr, const char *file, ut64 line, int mode, PrintfCallback print) {
const char *p;
char offset[SDB_NUM_BUFSZ];
char *offset_ptr;
if (!s || !file) {
return;
}
p = r_str_rchr (file, NULL, '/');
if (p) {
p++;
} else {
p = file;
}
// includedirs and properly check full paths
switch (mode) {
case 1:
case 'r':
case '*':
print ("CL %s:%d 0x%08"PFMT64x"\n", p, (int)line, addr);
break;
}
#if 0
/* THIS IS TOO SLOW */
if (r_file_exists (file)) {
p = file;
}
#else
p = file;
#endif
char *fileline = r_str_newf ("%s|%"PFMT64d, p, line);
r_str_ansi_strip (fileline);
r_str_replace_ch (fileline, '\n', 0, true);
r_str_replace_ch (fileline, '\t', 0, true);
offset_ptr = sdb_itoa (addr, 16, offset, sizeof (offset));
sdb_add (s, offset_ptr, fileline, 0);
sdb_add (s, fileline, offset_ptr, 0);
free (fileline);
}
static const ut8 *parse_ext_opcode(const RBin *bin, const ut8 *obuf,
size_t len, const RBinDwarfLineHeader *hdr,
RBinDwarfSMRegisters *regs, int mode) {
r_return_val_if_fail (bin && bin->cur && obuf && hdr && regs, NULL);
PrintfCallback print = bin->cb_printf;
const ut8 *buf;
const ut8 *buf_end;
ut8 opcode;
ut64 addr;
buf = obuf;
st64 op_len;
RBinFile *binfile = bin->cur;
RBinObject *o = binfile->o;
ut32 addr_size = o && o->info && o->info->bits ? o->info->bits / 8 : 4;
const char *filename;
buf_end = buf + len;
buf = r_leb128 (buf, len, &op_len);
if (buf >= buf_end) {
return NULL;
}
opcode = *buf++;
if (mode == R_MODE_PRINT) {
print (" Extended opcode %d: ", opcode);
}
switch (opcode) {
case DW_LNE_end_sequence:
regs->end_sequence = DWARF_TRUE;
if (binfile && binfile->sdb_addrinfo && hdr->file_names) {
int fnidx = regs->file - 1;
if (fnidx >= 0 && fnidx < hdr->file_names_count) {
add_sdb_addrline (binfile->sdb_addrinfo, regs->address,
hdr->file_names[fnidx].name, regs->line, mode, print);
}
}
if (mode == R_MODE_PRINT) {
print ("End of Sequence\n");
}
break;
case DW_LNE_set_address:
if (addr_size == 8) {
addr = READ64 (buf);
} else {
addr = READ32 (buf);
}
regs->address = addr;
if (mode == R_MODE_PRINT) {
print ("set Address to 0x%"PFMT64x"\n", addr);
}
break;
case DW_LNE_define_file:
filename = (const char*)buf;
if (mode == R_MODE_PRINT) {
print ("define_file\n");
print ("filename %s\n", filename);
}
buf += (strlen (filename) + 1);
ut64 dir_idx;
ut64 ignore;
if (buf + 1 < buf_end) {
buf = r_uleb128 (buf, buf_end - buf, &dir_idx, NULL);
}
if (buf + 1 < buf_end) {
buf = r_uleb128 (buf, buf_end - buf, &ignore, NULL);
}
if (buf + 1 < buf_end) {
buf = r_uleb128 (buf, buf_end - buf, &ignore, NULL);
}
break;
case DW_LNE_set_discriminator:
buf = r_uleb128 (buf, buf_end - buf, &addr, NULL);
if (mode == R_MODE_PRINT) {
print ("set Discriminator to %"PFMT64d"\n", addr);
}
regs->discriminator = addr;
break;
default:
if (mode == R_MODE_PRINT) {
print ("Unexpected ext opcode %d\n", opcode);
}
buf = NULL;
break;
}
return buf;
}
static const ut8 *parse_spec_opcode(
const RBin *bin, const ut8 *obuf, size_t len,
const RBinDwarfLineHeader *hdr,
RBinDwarfSMRegisters *regs,
ut8 opcode, int mode) {
r_return_val_if_fail (bin && obuf && hdr && regs, NULL);
PrintfCallback print = bin->cb_printf;
RBinFile *binfile = bin->cur;
const ut8 *buf = obuf;
ut8 adj_opcode = 0;
ut64 advance_adr;
adj_opcode = opcode - hdr->opcode_base;
if (!hdr->line_range) {
// line line-range information. move away
return NULL;
}
advance_adr = (adj_opcode / hdr->line_range) * hdr->min_inst_len;
regs->address += advance_adr;
int line_increment = hdr->line_base + (adj_opcode % hdr->line_range);
regs->line += line_increment;
if (mode == R_MODE_PRINT) {
print (" Special opcode %d: ", adj_opcode);
print ("advance Address by %"PFMT64d " to 0x%"PFMT64x" and Line by %d to %"PFMT64d"\n",
advance_adr, regs->address, line_increment, regs->line);
}
if (binfile && binfile->sdb_addrinfo && hdr->file_names) {
int idx = regs->file -1;
if (idx >= 0 && idx < hdr->file_names_count) {
add_sdb_addrline (binfile->sdb_addrinfo, regs->address,
hdr->file_names[idx].name,
regs->line, mode, print);
}
}
regs->basic_block = DWARF_FALSE;
regs->prologue_end = DWARF_FALSE;
regs->epilogue_begin = DWARF_FALSE;
regs->discriminator = 0;
return buf;
}
static const ut8 *parse_std_opcode(
const RBin *bin, const ut8 *obuf, size_t len,
const RBinDwarfLineHeader *hdr, RBinDwarfSMRegisters *regs,
ut8 opcode, int mode) {
r_return_val_if_fail (bin && bin->cur && obuf && hdr && regs, NULL);
PrintfCallback print = bin->cb_printf;
RBinFile *binfile = bin->cur;
const ut8* buf = obuf;
const ut8* buf_end = obuf + len;
ut64 addr = 0LL;
st64 sbuf;
ut8 adj_opcode;
ut64 op_advance;
ut16 operand;
if (mode == R_MODE_PRINT) {
print (" "); // formatting
}
switch (opcode) {
case DW_LNS_copy:
if (mode == R_MODE_PRINT) {
print ("Copy\n");
}
if (binfile && binfile->sdb_addrinfo && hdr->file_names) {
int fnidx = regs->file - 1;
if (fnidx >= 0 && fnidx < hdr->file_names_count) {
add_sdb_addrline (binfile->sdb_addrinfo,
regs->address,
hdr->file_names[fnidx].name,
regs->line, mode, print);
}
}
regs->basic_block = DWARF_FALSE;
break;
case DW_LNS_advance_pc:
buf = r_uleb128 (buf, buf_end - buf, &addr, NULL);
regs->address += addr * hdr->min_inst_len;
if (mode == R_MODE_PRINT) {
print ("Advance PC by %"PFMT64d" to 0x%"PFMT64x"\n",
addr * hdr->min_inst_len, regs->address);
}
break;
case DW_LNS_advance_line:
buf = r_leb128(buf, buf_end - buf, &sbuf);
regs->line += sbuf;
if (mode == R_MODE_PRINT) {
print ("Advance line by %"PFMT64d", to %"PFMT64d"\n", sbuf, regs->line);
}
break;
case DW_LNS_set_file:
buf = r_uleb128 (buf, buf_end - buf, &addr, NULL);
if (mode == R_MODE_PRINT) {
print ("Set file to %"PFMT64d"\n", addr);
}
regs->file = addr;
break;
case DW_LNS_set_column:
buf = r_uleb128 (buf, buf_end - buf, &addr, NULL);
if (mode == R_MODE_PRINT) {
print ("Set column to %"PFMT64d"\n", addr);
}
regs->column = addr;
break;
case DW_LNS_negate_stmt:
regs->is_stmt = regs->is_stmt ? DWARF_FALSE : DWARF_TRUE;
if (mode == R_MODE_PRINT) {
print ("Set is_stmt to %d\n", regs->is_stmt);
}
break;
case DW_LNS_set_basic_block:
if (mode == R_MODE_PRINT) {
print ("set_basic_block\n");
}
regs->basic_block = DWARF_TRUE;
break;
case DW_LNS_const_add_pc:
adj_opcode = 255 - hdr->opcode_base;
if (hdr->line_range > 0) { // to dodge division by zero
op_advance = (adj_opcode / hdr->line_range) * hdr->min_inst_len;
} else {
op_advance = 0;
}
regs->address += op_advance;
if (mode == R_MODE_PRINT) {
print ("Advance PC by constant %"PFMT64d" to 0x%"PFMT64x"\n",
op_advance, regs->address);
}
break;
case DW_LNS_fixed_advance_pc:
operand = READ16 (buf);
regs->address += operand;
if (mode == R_MODE_PRINT) {
print ("Fixed advance pc to %"PFMT64d"\n", regs->address);
}
break;
case DW_LNS_set_prologue_end:
regs->prologue_end = ~0;
if (mode == R_MODE_PRINT) {
print ("set_prologue_end\n");
}
break;
case DW_LNS_set_epilogue_begin:
regs->epilogue_begin = ~0;
if (mode == R_MODE_PRINT) {
print ("set_epilogue_begin\n");
}
break;
case DW_LNS_set_isa:
buf = r_uleb128 (buf, buf_end - buf, &addr, NULL);
regs->isa = addr;
if (mode == R_MODE_PRINT) {
print ("set_isa\n");
}
break;
default:
if (mode == R_MODE_PRINT) {
print ("Unexpected std opcode %d\n", opcode);
}
break;
}
return buf;
}
static void set_regs_default(const RBinDwarfLineHeader *hdr, RBinDwarfSMRegisters *regs) {
regs->address = 0;
regs->file = 1;
regs->line = 1;
regs->column = 0;
regs->is_stmt = hdr->default_is_stmt;
regs->basic_block = DWARF_FALSE;
regs->end_sequence = DWARF_FALSE;
regs->prologue_end = DWARF_FALSE;
regs->epilogue_begin = DWARF_FALSE;
regs->isa = 0;
}