/
dwarf_process.c
1722 lines (1633 loc) · 47.3 KB
/
dwarf_process.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
// SPDX-FileCopyrightText: 2012-2020 houndthe <cgkajm@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_util.h>
#include <rz_type.h>
#include <sdb.h>
#include <rz_analysis.h>
#include <rz_bin_dwarf.h>
#include <string.h>
typedef struct dwarf_parse_context_t {
const RzAnalysis *analysis;
const RzBinDwarfDie *all_dies;
const ut64 count;
Sdb *sdb;
HtUP /*<ut64 offset, DwarfDie *die>*/ *die_map;
HtUP /*<offset, RzBinDwarfLocList*>*/ *locations;
char *lang; // for demangling
} Context;
typedef struct dwarf_function_t {
ut64 addr;
const char *name;
const char *signature;
bool is_external;
bool is_method;
bool is_virtual;
bool is_trampoline; // intermediary in making call to another func
ut8 access; // public = 1, protected = 2, private = 3, if not set assume private
ut64 vtable_addr; // location description
ut64 call_conv; // normal || program || nocall
} Function;
typedef enum dwarf_location_kind {
LOCATION_UNKNOWN = 0,
LOCATION_GLOBAL = 1,
LOCATION_BP = 2,
LOCATION_SP = 3,
LOCATION_REGISTER = 4,
} VariableLocationKind;
typedef struct dwarf_var_location_t {
VariableLocationKind kind;
ut64 address;
ut64 reg_num;
st64 offset;
const char *reg_name; /* string literal */
} VariableLocation;
typedef struct dwarf_variable_t {
VariableLocation *location;
char *name;
char *type;
} Variable;
static void variable_free(Variable *var) {
free(var->name);
free(var->location);
free(var->type);
free(var);
}
/* return -1 if attr isn't found */
static inline st32 find_attr_idx(const RzBinDwarfDie *die, st32 attr_name) {
st32 i;
rz_return_val_if_fail(die, -1);
for (i = 0; i < die->count; i++) {
if (die->attr_values[i].attr_name == attr_name) {
return i;
}
}
return -1;
}
/* return NULL if attr isn't found */
static RzBinDwarfAttrValue *find_attr(const RzBinDwarfDie *die, st32 attr_name) {
st32 i;
rz_return_val_if_fail(die, NULL);
for (i = 0; i < die->count; i++) {
if (die->attr_values[i].attr_name == attr_name) {
return &die->attr_values[i];
}
}
return NULL;
}
/**
* \brief Pasted from rz_strbuf_*
* Prepends string before a last occurence of character c
* Used to replicate proper C declaration for function pointers
* \param sb
* \param s
* \param c
*/
static bool strbuf_rev_prepend_char(RzStrBuf *sb, const char *s, int c) {
rz_return_val_if_fail(sb && s, false);
size_t l = strlen(s);
// fast path if no chars to append
if (l == 0) {
return true;
}
size_t newlen = l + sb->len;
char *ns = malloc(newlen + 1);
bool ret = false;
char *sb_str = sb->ptr ? sb->ptr : sb->buf;
char *pivot = strrchr(sb_str, c);
if (!pivot) {
free(ns);
return false;
}
size_t idx = pivot - sb_str;
if (ns) {
memcpy(ns, sb_str, idx);
memcpy(ns + idx, s, l);
memcpy(ns + idx + l, sb_str + idx, sb->len - idx);
ns[newlen] = 0;
ret = rz_strbuf_set(sb, ns) != NULL;
free(ns);
}
return ret;
}
/**
* \brief Pasted from rz_strbuf_*
* Appends string after a first occurence of character c
* Used to replicate proper C declaration for function pointers
* \param sb
* \param s
* \param needle
*/
static bool strbuf_rev_append_char(RzStrBuf *sb, const char *s, const char *needle) {
rz_return_val_if_fail(sb && s, false);
size_t l = strlen(s);
// fast path if no chars to append
if (l == 0) {
return true;
}
bool ret = false;
char *sb_str = sb->ptr ? sb->ptr : sb->buf;
char *pivot = strstr(sb_str, needle);
if (!pivot) {
return false;
}
pivot += strlen(needle);
size_t idx = pivot - sb_str;
size_t newlen = l + sb->len;
char *ns = malloc(newlen + 1);
if (ns) {
memcpy(ns, sb_str, idx);
memcpy(ns + idx, s, l);
memcpy(ns + idx + l, sb_str + idx, sb->len - idx);
ns[newlen] = 0;
ret = rz_strbuf_set(sb, ns) != NULL;
free(ns);
}
return ret;
}
static inline char *create_type_name_from_offset(ut64 offset) {
return rz_str_newf("type_0x%" PFMT64x, offset);
}
/**
* \brief Get the DIE name or create unique one from its offset
*
* \param die
* \return char* DIEs name or NULL if error
*/
static char *get_die_name(const RzBinDwarfDie *die) {
char *name = NULL;
st32 name_attr_idx = find_attr_idx(die, DW_AT_name);
if (name_attr_idx != -1) {
const char *s = rz_bin_dwarf_attr_value_get_string_content(&die->attr_values[name_attr_idx]);
name = RZ_STR_DUP(s);
}
return name ? name : create_type_name_from_offset(die->offset);
}
/**
* \brief Get the DIE size in bits
*
* \param die
* \return ut64 size in bits or 0 if not found
*/
static ut64 get_die_size(const RzBinDwarfDie *die) {
ut64 size = 0;
st32 byte_size_idx = find_attr_idx(die, DW_AT_byte_size);
if (byte_size_idx != -1) {
size = die->attr_values[byte_size_idx].uconstant * CHAR_BIT;
} else {
st32 bit_size_idx = find_attr_idx(die, DW_AT_bit_size);
if (bit_size_idx != -1) {
size = die->attr_values[bit_size_idx].uconstant;
}
}
return size;
}
/**
* \brief Parses array type entry signature into strbuf
*
* \param ctx
* \param idx index of the current entry
* \param strbuf strbuf to store the type into
* \return st32 -1 if error else 0
*/
static st32 parse_array_type(Context *ctx, ut64 idx, RzStrBuf *strbuf) {
const RzBinDwarfDie *die = &ctx->all_dies[idx++];
if (die->has_children) {
int child_depth = 1;
size_t j;
for (j = idx; child_depth > 0 && j < ctx->count; j++) {
const RzBinDwarfDie *child_die = &ctx->all_dies[j];
// right now we skip non direct descendats of the structure
// can be also DW_TAG_suprogram for class methods or tag for templates
if (child_depth == 1 && child_die->tag == DW_TAG_subrange_type) {
size_t i;
for (i = 0; i < child_die->count; i++) {
const RzBinDwarfAttrValue *value = &child_die->attr_values[i];
switch (value->attr_name) {
case DW_AT_upper_bound:
case DW_AT_count:
rz_strbuf_appendf(strbuf, "[%" PFMT64d "]", value->uconstant + 1);
break;
default:
break;
}
}
}
if (child_die->has_children) {
child_depth++;
}
// sibling list is terminated by null entry
if (child_die->abbrev_code == 0) {
child_depth--;
}
}
}
return 0;
}
/**
* \brief Recursively parses type entry of a certain offset into strbuf
* saves type size into *size
*
* \param ctx
* \param offset offset of the type entry
* \param strbuf string to store the type into
* \param size ptr to size of a type to fill up (can be NULL if unwanted)
* \param set of visited die offsets, to prevent infinite recursion
* \return st32 -1 if error else DW_TAG of the entry
*
* TODO make cache for type entries, one type is usually referenced
* multiple times which means it's parsed multiple times instead of once
*/
static st32 parse_type(Context *ctx, const ut64 offset, RzStrBuf *strbuf, ut64 *size, RZ_NONNULL SetU *visited) {
rz_return_val_if_fail(strbuf && visited, -1);
if (set_u_contains(visited, offset)) {
return -1;
}
set_u_add(visited, offset);
RzBinDwarfDie *die = ht_up_find(ctx->die_map, offset, NULL);
if (!die) {
return -1;
}
st32 type_idx;
st32 tag;
char *name = NULL;
// get size of first type DIE that has size
if (size && *size == 0) {
*size = get_die_size(die);
}
switch (die->tag) {
// this should be recursive search for the type until you find base/user defined type
case DW_TAG_pointer_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx == -1) {
rz_strbuf_append(strbuf, "void");
rz_strbuf_append(strbuf, " *");
} else {
tag = parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
if (tag == DW_TAG_subroutine_type) {
strbuf_rev_prepend_char(strbuf, "(*)", '(');
} else if (tag == DW_TAG_pointer_type) {
if (!strbuf_rev_append_char(strbuf, "*", "(*")) {
strbuf_rev_prepend_char(strbuf, "*", '*');
}
} else {
rz_strbuf_append(strbuf, " *");
}
}
break;
// We won't parse them as a complete type, because that will already be done
// so just a name now
case DW_TAG_typedef:
case DW_TAG_base_type:
case DW_TAG_structure_type:
case DW_TAG_enumeration_type:
case DW_TAG_union_type:
case DW_TAG_class_type:
name = get_die_name(die);
rz_strbuf_append(strbuf, name);
free(name);
break;
case DW_TAG_subroutine_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx == -1) {
rz_strbuf_append(strbuf, "void");
} else {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " (");
if (die->has_children) { // has parameters
}
rz_strbuf_append(strbuf, ")");
break;
case DW_TAG_array_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
parse_array_type(ctx, die - ctx->all_dies, strbuf);
break;
case DW_TAG_const_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " const");
break;
case DW_TAG_volatile_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " volatile");
break;
case DW_TAG_restrict_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " restrict");
break;
case DW_TAG_rvalue_reference_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " &&");
break;
case DW_TAG_reference_type:
type_idx = find_attr_idx(die, DW_AT_type);
if (type_idx != -1) {
parse_type(ctx, die->attr_values[type_idx].reference, strbuf, size, visited);
}
rz_strbuf_append(strbuf, " &");
break;
default:
break;
}
set_u_delete(visited, offset);
return (st32)die->tag;
}
/**
* \brief Convenience function for calling parse_type with an empty visited set
* See documentation of parse_type
*/
static st32 parse_type_outer(Context *ctx, const ut64 offset, RzStrBuf *strbuf, ut64 *size) {
SetU *visited = set_u_new();
if (!visited) {
return -1;
}
st32 r = parse_type(ctx, offset, strbuf, size, visited);
set_u_free(visited);
return r;
}
/**
* \brief Parses structured entry into *result RzTypeStructMember
* http://www.dwarfstd.org/doc/DWARF4.pdf#page=102&zoom=100,0,0
*
* \param ctx
* \param idx index of the current entry
* \param result ptr to result member to fill up
* \return RzTypeStructMember* ptr to parsed Member
*/
static RzTypeStructMember *parse_struct_member(Context *ctx, ut64 idx, RzTypeStructMember *result) {
rz_return_val_if_fail(result, NULL);
const RzBinDwarfDie *die = &ctx->all_dies[idx];
char *name = NULL;
char *type = NULL;
ut64 offset = 0;
ut64 size = 0;
RzStrBuf strbuf;
rz_strbuf_init(&strbuf);
size_t i;
for (i = 0; i < die->count; i++) {
RzBinDwarfAttrValue *value = &die->attr_values[i];
switch (die->attr_values[i].attr_name) {
case DW_AT_name:
free(name);
name = get_die_name(die);
if (!name) {
goto cleanup;
}
break;
case DW_AT_type:
parse_type_outer(ctx, value->reference, &strbuf, &size);
free(type);
type = rz_strbuf_drain_nofree(&strbuf);
if (!type || !*type) {
goto cleanup;
}
break;
case DW_AT_data_member_location:
/*
2 cases, 1.: If val is integer, it offset in bytes from
the beginning of containing entity. If containing entity has
a bit offset, member has that bit offset aswell
2.: value is a location description
http://www.dwarfstd.org/doc/DWARF4.pdf#page=39&zoom=100,0,0
*/
offset = value->uconstant;
break;
case DW_AT_accessibility: // private, public etc.
case DW_AT_mutable: // flag is it is mutable
case DW_AT_data_bit_offset:
/*
int that specifies the number of bits from beginning
of containing entity to the beginning of the data member
*/
break;
// If the size of a data member is not the same as the
// size of the type given for the data member
case DW_AT_byte_size:
size = value->uconstant * CHAR_BIT;
break;
case DW_AT_bit_size:
size = value->uconstant;
break;
case DW_AT_containing_type:
default:
break;
}
}
result->name = name;
result->type = rz_type_parse_string_single(ctx->analysis->typedb->parser, type, NULL);
if (!result->type) {
goto cleanup;
}
result->offset = offset;
result->size = size;
free(type);
return result;
cleanup:
free(name);
free(type);
return NULL;
}
/**
* \brief Parses enum entry into *result RzTypeEnumCase
* http://www.dwarfstd.org/doc/DWARF4.pdf#page=110&zoom=100,0,0
*
* \param ctx
* \param idx index of the current entry
* \param result ptr to result case to fill up
* \return RzTypeEnumCase* Ptr to parsed enum case
*/
static RzTypeEnumCase *parse_enumerator(Context *ctx, ut64 idx, RzTypeEnumCase *result) {
const RzBinDwarfDie *die = &ctx->all_dies[idx];
char *name = NULL;
int val = 0;
size_t i;
// Enumerator has DW_AT_name and DW_AT_const_value
for (i = 0; i < die->count; i++) {
RzBinDwarfAttrValue *value = &die->attr_values[i];
switch (die->attr_values[i].attr_name) {
case DW_AT_name:
free(name);
name = get_die_name(die);
if (!name) {
goto cleanup;
}
break;
case DW_AT_const_value:
// ?? can be block, sdata, data, string w/e
val = value->uconstant; // TODO solve the encoding, I don't know in which union member is it store
break;
default:
break;
}
}
result->name = name;
result->val = (int)val;
return result;
cleanup:
free(name);
return NULL;
}
/**
* \brief Parses a structured entry (structs, classes, unions) into
* RzBaseType and saves it using rz_analysis_save_base_type ()
*
* \param ctx
* \param idx index of the current entry
*/
// http://www.dwarfstd.org/doc/DWARF4.pdf#page=102&zoom=100,0,0
static void parse_structure_type(Context *ctx, ut64 idx) {
const RzBinDwarfDie *die = &ctx->all_dies[idx];
RzBaseTypeKind kind;
if (die->tag == DW_TAG_union_type) {
kind = RZ_BASE_TYPE_KIND_UNION;
} else {
kind = RZ_BASE_TYPE_KIND_STRUCT;
}
RzBaseType *base_type = rz_type_base_type_new(kind);
if (!base_type) {
return;
}
base_type->name = get_die_name(die);
if (!base_type->name) {
rz_type_base_type_free(base_type);
return;
}
// if it is definition of previous declaration (TODO Fix, big ugly hotfix addition)
st32 spec_attr_idx = find_attr_idx(die, DW_AT_specification);
if (spec_attr_idx != -1) {
RzBinDwarfDie *decl_die = ht_up_find(ctx->die_map, die->attr_values[spec_attr_idx].reference, NULL);
if (!decl_die) {
rz_type_base_type_free(base_type);
return;
}
st32 name_attr_idx = find_attr_idx(decl_die, DW_AT_name);
if (name_attr_idx != -1) {
free(base_type->name);
base_type->name = get_die_name(decl_die);
}
}
base_type->size = get_die_size(die);
RzTypeStructMember member = { 0 };
// Parse out all members, can this in someway be extracted to a function?
if (die->has_children) {
int child_depth = 1; // Direct children of the node
size_t j;
idx++; // Move to the first children node
for (j = idx; child_depth > 0 && j < ctx->count; j++) {
const RzBinDwarfDie *child_die = &ctx->all_dies[j];
// we take only direct descendats of the structure
// can be also DW_TAG_suprogram for class methods or tag for templates
if (child_depth == 1 && child_die->tag == DW_TAG_member) {
RzTypeStructMember *result = parse_struct_member(ctx, j, &member);
if (!result) {
rz_type_base_type_free(base_type);
return;
} else {
void *element = rz_vector_push(&base_type->struct_data.members, &member);
if (!element) {
rz_type_base_type_free(base_type);
return;
}
}
}
if (child_die->has_children) {
child_depth++;
}
if (child_die->abbrev_code == 0) { // siblings terminator
child_depth--;
}
}
}
rz_type_db_save_base_type(ctx->analysis->typedb, base_type);
}
/**
* \brief Parses a enum entry into RzBaseType and saves it
* int Sdb using rz_analysis_save_base_type ()
*
* \param ctx
* \param idx index of the current entry
*/
static void parse_enum_type(Context *ctx, ut64 idx) {
const RzBinDwarfDie *die = &ctx->all_dies[idx];
RzBaseType *base_type = rz_type_base_type_new(RZ_BASE_TYPE_KIND_ENUM);
if (!base_type) {
return;
}
base_type->name = get_die_name(die);
if (!base_type->name) {
rz_type_base_type_free(base_type);
return;
}
base_type->size = get_die_size(die);
st32 type_attr_idx = find_attr_idx(die, DW_AT_type);
if (type_attr_idx != -1) {
RzStrBuf strbuf;
rz_strbuf_init(&strbuf);
parse_type_outer(ctx, die->attr_values[type_attr_idx].reference, &strbuf, &base_type->size);
char *type = rz_strbuf_drain_nofree(&strbuf);
base_type->type = rz_type_parse_string_single(ctx->analysis->typedb->parser, type, NULL);
free(type);
if (!base_type->type) {
rz_type_base_type_free(base_type);
return;
}
}
RzTypeEnumCase cas;
if (die->has_children) {
int child_depth = 1; // Direct children of the node
size_t j;
idx++; // Move to the first children node
for (j = idx; child_depth > 0 && j < ctx->count; j++) {
const RzBinDwarfDie *child_die = &ctx->all_dies[j];
// we take only direct descendats of the structure
if (child_depth == 1 && child_die->tag == DW_TAG_enumerator) {
RzTypeEnumCase *result = parse_enumerator(ctx, j, &cas);
if (!result) {
rz_type_base_type_free(base_type);
return;
} else {
void *element = rz_vector_push(&base_type->enum_data.cases, &cas);
if (!element) {
rz_type_base_enum_case_free(result, NULL);
rz_type_base_type_free(base_type);
return;
}
}
}
if (child_die->has_children) {
child_depth++;
}
// sibling list is terminated by null entry
if (child_die->abbrev_code == 0) {
child_depth--;
}
}
}
rz_type_db_save_base_type(ctx->analysis->typedb, base_type);
}
/**
* \brief Parses a typedef entry into RzBaseType and saves it
* using rz_analysis_save_base_type ()
*
* http://www.dwarfstd.org/doc/DWARF4.pdf#page=96&zoom=100,0,0
*
* \param ctx
* \param idx index of the current entry
*/
static void parse_typedef(Context *ctx, ut64 idx) {
const RzBinDwarfDie *die = &ctx->all_dies[idx];
char *name = NULL;
char *type = NULL;
ut64 size = 0;
RzStrBuf strbuf;
rz_strbuf_init(&strbuf);
size_t i;
for (i = 0; i < die->count; i++) {
RzBinDwarfAttrValue *value = &die->attr_values[i];
switch (die->attr_values[i].attr_name) {
case DW_AT_name:
name = get_die_name(die);
if (!name) {
goto cleanup;
}
break;
case DW_AT_type:
parse_type_outer(ctx, value->reference, &strbuf, &size);
type = rz_strbuf_drain_nofree(&strbuf);
if (!type) {
goto cleanup;
}
break;
default:
break;
}
}
if (!name || !type) { // type has to have a name for now
goto cleanup;
}
RzBaseType *base_type = rz_type_base_type_new(RZ_BASE_TYPE_KIND_TYPEDEF);
if (!base_type) {
goto cleanup;
}
base_type->name = name;
base_type->type = rz_type_parse_string_single(ctx->analysis->typedb->parser, type, NULL);
if (!base_type->type) {
rz_type_base_type_free(base_type);
goto cleanup;
}
rz_type_db_save_base_type(ctx->analysis->typedb, base_type);
cleanup:
free(type);
rz_strbuf_fini(&strbuf);
return;
}
static void parse_atomic_type(Context *ctx, ut64 idx) {
const RzBinDwarfDie *die = &ctx->all_dies[idx];
char *name = NULL;
ut64 size = 0;
size_t i;
// TODO support endiannity and encoding in future?
for (i = 0; i < die->count; i++) {
RzBinDwarfAttrValue *value = &die->attr_values[i];
switch (die->attr_values[i].attr_name) {
case DW_AT_name: {
free(name);
const char *s = rz_bin_dwarf_attr_value_get_string_content(&die->attr_values[i]);
if (s) {
name = strdup(s);
} else {
name = create_type_name_from_offset(die->offset);
}
if (!name) {
return;
}
break;
}
case DW_AT_byte_size:
size = value->uconstant * CHAR_BIT;
break;
case DW_AT_bit_size:
size = value->uconstant;
break;
case DW_AT_encoding:
default:
break;
}
}
if (!name) { // type has to have a name for now
return;
}
RzBaseType *base_type = rz_type_base_type_new(RZ_BASE_TYPE_KIND_ATOMIC);
if (!base_type) {
free(name);
return;
}
base_type->name = name;
base_type->size = size;
rz_type_db_save_base_type(ctx->analysis->typedb, base_type);
}
static const char *get_specification_die_name(const RzBinDwarfDie *die) {
st32 linkage_name_attr_idx = find_attr_idx(die, DW_AT_linkage_name);
if (linkage_name_attr_idx != -1) {
const char *s = rz_bin_dwarf_attr_value_get_string_content(&die->attr_values[linkage_name_attr_idx]);
if (s) {
return s;
}
}
st32 name_attr_idx = find_attr_idx(die, DW_AT_name);
if (name_attr_idx != -1) {
const char *s = rz_bin_dwarf_attr_value_get_string_content(&die->attr_values[name_attr_idx]);
if (s) {
return s;
}
}
return NULL;
}
static void get_spec_die_type(Context *ctx, RzBinDwarfDie *die, RzStrBuf *ret_type) {
st32 attr_idx = find_attr_idx(die, DW_AT_type);
if (attr_idx != -1) {
ut64 size = 0;
parse_type_outer(ctx, die->attr_values[attr_idx].reference, ret_type, &size);
}
}
/* For some languages linkage name is more informative like C++,
but for Rust it's rubbish and the normal name is fine */
static bool prefer_linkage_name(char *lang) {
if (!lang) {
return false;
}
if (!strcmp(lang, "rust")) {
return false;
} else if (!strcmp(lang, "ada")) {
return false;
}
return true;
}
static void parse_abstract_origin(Context *ctx, ut64 offset, RzStrBuf *type, const char **name) {
RzBinDwarfDie *die = ht_up_find(ctx->die_map, offset, NULL);
if (die) {
size_t i;
ut64 size = 0;
bool has_linkage_name = false;
bool get_linkage_name = prefer_linkage_name(ctx->lang);
for (i = 0; i < die->count; i++) {
const RzBinDwarfAttrValue *val = &die->attr_values[i];
switch (val->attr_name) {
case DW_AT_name:
if (!get_linkage_name || !has_linkage_name) {
*name = val->string.content;
}
break;
case DW_AT_linkage_name:
case DW_AT_MIPS_linkage_name:
*name = val->string.content;
has_linkage_name = true;
break;
case DW_AT_type:
parse_type_outer(ctx, val->reference, type, &size);
break;
default:
break;
}
}
}
}
/* x86_64 https://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf */
static const char *map_dwarf_reg_to_x86_64_reg(ut64 reg_num, VariableLocationKind *kind) {
*kind = LOCATION_REGISTER;
switch (reg_num) {
case 0: return "rax";
case 1: return "rdx";
case 2: return "rcx";
case 3: return "rbx";
case 4: return "rsi";
case 5: return "rdi";
case 6:
*kind = LOCATION_BP;
return "rbp";
case 7:
*kind = LOCATION_SP;
return "rsp";
case 8: return "r8";
case 9: return "r9";
case 10: return "r10";
case 11: return "r11";
case 12: return "r12";
case 13: return "r13";
case 14: return "r14";
case 15: return "r15";
case 17: return "xmm0";
case 18: return "xmm1";
case 19: return "xmm2";
case 20: return "xmm3";
case 21: return "xmm4";
case 22: return "xmm5";
case 23: return "xmm6";
case 24: return "xmm7";
default:
*kind = LOCATION_UNKNOWN;
return "unsupported_reg";
}
}
/* x86 https://01.org/sites/default/files/file_attach/intel386-psabi-1.0.pdf */
static const char *map_dwarf_reg_to_x86_reg(ut64 reg_num, VariableLocationKind *kind) {
*kind = LOCATION_REGISTER;
switch (reg_num) {
case 0:
case 8:
return "eax";
case 1: return "edx";
case 2: return "ecx";
case 3: return "ebx";
case 4:
*kind = LOCATION_SP;
return "esp";
case 5:
*kind = LOCATION_BP;
return "ebp";
case 6: return "esi";
case 7: return "edi";
case 9: return "EFLAGS";
case 11: return "st0";
case 12: return "st1";
case 13: return "st2";
case 14: return "st3";
case 15: return "st4";
case 16: return "st5";
case 17: return "st6";
case 18: return "st7";
case 21: return "xmm0";
case 22: return "xmm1";
case 23: return "xmm2";
case 24: return "xmm3";
case 25: return "xmm4";
case 26: return "xmm5";
case 27: return "xmm6";
case 28: return "xmm7";
case 29: return "mm0";
case 30: return "mm1";
case 31: return "mm2";
case 32: return "mm3";
case 33: return "mm4";
case 34: return "mm5";
case 35: return "mm6";
case 36: return "mm7";
case 40: return "es";
case 41: return "cs";
case 42: return "ss";
case 43: return "ds";
case 44: return "fs";
case 45: return "gs";
default:
rz_warn_if_reached();
*kind = LOCATION_UNKNOWN;
return "unsupported_reg";
}
}
/* https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html#DW-REG */
static const char *map_dwarf_reg_to_ppc64_reg(ut64 reg_num, VariableLocationKind *kind) {
*kind = LOCATION_REGISTER;
switch (reg_num) {
case 0: return "r0";
case 1:
*kind = LOCATION_SP;
return "r1";
case 2: return "r2";
case 3: return "r3";
case 4: return "r4";
case 5: return "r5";
case 6: return "r6";
case 7: return "r7";
case 8: return "r8";
case 9: return "r9";
case 10: return "r10";
case 11: return "r11";
case 12: return "r12";
case 13: return "r13";
case 14: return "r14";
case 15: return "r15";
case 16: return "r16";
case 17: return "r17";
case 18: return "r18";
case 19: return "r19";
case 20: return "r20";
case 21: return "r21";
case 22: return "r22";
case 23: return "r23";
case 24: return "r24";
case 25: return "r25";
case 26: return "r26";
case 27: return "r27";
case 28: return "r28";
case 29: return "r29";
case 30: return "r30";
case 31: return "r31";
default:
rz_warn_if_reached();
*kind = LOCATION_UNKNOWN;
return "unsupported_reg";
}
}
/* returns string literal register name!
TODO add more arches */
static const char *get_dwarf_reg_name(RZ_NONNULL char *arch, int reg_num, VariableLocationKind *kind, int bits) {
if (!strcmp(arch, "x86")) {
if (bits == 64) {
return map_dwarf_reg_to_x86_64_reg(reg_num, kind);
} else {
return map_dwarf_reg_to_x86_reg(reg_num, kind);
}
} else if (!strcmp(arch, "ppc")) {
if (bits == 64) {
return map_dwarf_reg_to_ppc64_reg(reg_num, kind);
}
}
*kind = LOCATION_UNKNOWN;
return "unsupported_reg";
}
static RzBinDwarfLocRange *find_largest_loc_range(RzList /*<RzBinDwarfLocRange *>*/ *loc_list) {
RzBinDwarfLocRange *largest = NULL;
ut64 max_range_size = 0;
RzListIter *iter;