-
Notifications
You must be signed in to change notification settings - Fork 53
/
dwarves.c
3105 lines (2518 loc) · 70 KB
/
dwarves.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-License-Identifier: GPL-2.0-only
Copyright (C) 2006 Mandriva Conectiva S.A.
Copyright (C) 2006 Arnaldo Carvalho de Melo <[email protected]>
Copyright (C) 2007 Red Hat Inc.
Copyright (C) 2007 Arnaldo Carvalho de Melo <[email protected]>
*/
#include <assert.h>
#include <dirent.h>
#include <dwarf.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <libelf.h>
#include <limits.h>
#include <pthread.h>
#include <search.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include "config.h"
#include "list.h"
#include "dwarves.h"
#include "dutil.h"
#define min(x, y) ((x) < (y) ? (x) : (y))
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
static void *obstack_zalloc(struct obstack *obstack, size_t size)
{
void *o = obstack_alloc(obstack, size);
if (o)
memset(o, 0, size);
return o;
}
void *cu__zalloc(struct cu *cu, size_t size)
{
if (cu->use_obstack)
return obstack_zalloc(&cu->obstack, size);
return zalloc(size);
}
void *cu__malloc(struct cu *cu, size_t size)
{
if (cu->use_obstack)
return obstack_alloc(&cu->obstack, size);
return malloc(size);
}
void cu__free(struct cu *cu, void *ptr)
{
if (!cu->use_obstack)
free(ptr);
// When using an obstack we'll free everything in cu__delete()
}
void cu__tag_free(struct cu *cu, struct tag *tag)
{
if (cu->dfops && cu->dfops->tag__free)
cu->dfops->tag__free(tag, cu);
else
cu__free(cu, tag);
}
void *cu__tag_alloc(struct cu *cu, size_t size)
{
if (cu->dfops && cu->dfops->tag__alloc)
return cu->dfops->tag__alloc(cu, size);
return cu__zalloc(cu, size);
}
int tag__is_base_type(const struct tag *tag, const struct cu *cu)
{
switch (tag->tag) {
case DW_TAG_base_type:
return 1;
case DW_TAG_typedef: {
const struct tag *type = cu__type(cu, tag->type);
if (type == NULL)
return 0;
return tag__is_base_type(type, cu);
}
}
return 0;
}
bool tag__is_array(const struct tag *tag, const struct cu *cu)
{
switch (tag->tag) {
case DW_TAG_array_type:
return true;
case DW_TAG_const_type:
case DW_TAG_typedef: {
const struct tag *type = cu__type(cu, tag->type);
if (type == NULL)
return 0;
return tag__is_array(type, cu);
}
}
return 0;
}
int __tag__has_type_loop(const struct tag *tag, const struct tag *type,
char *bf, size_t len, FILE *fp,
const char *fn, int line)
{
char bbf[2048], *abf = bbf;
if (type == NULL)
return 0;
if (tag->type == type->type) {
int printed;
if (bf != NULL)
abf = bf;
else
len = sizeof(bbf);
printed = snprintf(abf, len, "<ERROR(%s:%d): detected type loop: type=%d, tag=%s>",
fn, line, tag->type, dwarf_tag_name(tag->tag));
if (bf == NULL)
printed = fprintf(fp ?: stderr, "%s\n", abf);
return printed;
}
return 0;
}
static void lexblock__delete_tags(struct tag *tag, struct cu *cu)
{
struct lexblock *block = tag__lexblock(tag);
struct tag *pos, *n;
list_for_each_entry_safe_reverse(pos, n, &block->tags, node) {
list_del_init(&pos->node);
tag__delete(pos, cu);
}
}
void lexblock__delete(struct lexblock *block, struct cu *cu)
{
if (block == NULL)
return;
lexblock__delete_tags(&block->ip.tag, cu);
cu__tag_free(cu, &block->ip.tag);
}
static void template_parameter_pack__delete_tags(struct template_parameter_pack *pack, struct cu *cu)
{
struct tag *pos, *n;
list_for_each_entry_safe_reverse(pos, n, &pack->params, node) {
list_del_init(&pos->node);
tag__delete(pos, cu);
}
}
void template_parameter_pack__delete(struct template_parameter_pack *pack, struct cu *cu)
{
if (pack == NULL)
return;
template_parameter_pack__delete_tags(pack, cu);
cu__tag_free(cu, &pack->tag);
}
static void formal_parameter_pack__delete_tags(struct formal_parameter_pack *pack, struct cu *cu)
{
struct tag *pos, *n;
list_for_each_entry_safe_reverse(pos, n, &pack->params, node) {
list_del_init(&pos->node);
tag__delete(pos, cu);
}
}
void formal_parameter_pack__delete(struct formal_parameter_pack *pack, struct cu *cu)
{
if (pack == NULL)
return;
formal_parameter_pack__delete_tags(pack, cu);
cu__tag_free(cu, &pack->tag);
}
void tag__delete(struct tag *tag, struct cu *cu)
{
if (tag == NULL)
return;
assert(list_empty(&tag->node));
switch (tag->tag) {
case DW_TAG_union_type:
type__delete(tag__type(tag), cu); break;
case DW_TAG_class_type:
case DW_TAG_structure_type:
class__delete(tag__class(tag), cu); break;
case DW_TAG_enumeration_type:
enumeration__delete(tag__type(tag), cu); break;
case DW_TAG_subroutine_type:
ftype__delete(tag__ftype(tag), cu); break;
case DW_TAG_subprogram:
function__delete(tag__function(tag), cu); break;
case DW_TAG_lexical_block:
lexblock__delete(tag__lexblock(tag), cu); break;
case DW_TAG_GNU_template_parameter_pack:
template_parameter_pack__delete(tag__template_parameter_pack(tag), cu); break;
case DW_TAG_GNU_formal_parameter_pack:
formal_parameter_pack__delete(tag__formal_parameter_pack(tag), cu); break;
default:
cu__tag_free(cu, tag);
}
}
void tag__not_found_die(const char *file, int line, const char *func, int tag, const char *name)
{
fprintf(stderr, "%s::%s(%d, related to the type of tag DW_TAG_%s \"%s\"): tag not found, please report to "
"[email protected]\n", file, func, line, dwarf_tag_name(tag), name);
exit(1);
}
struct tag *tag__follow_typedef(const struct tag *tag, const struct cu *cu)
{
struct tag *type = cu__type(cu, tag->type);
if (type != NULL && tag__is_typedef(type))
return tag__follow_typedef(type, cu);
return type;
}
struct tag *tag__strip_typedefs_and_modifiers(const struct tag *tag, const struct cu *cu)
{
struct tag *type = cu__type(cu, tag->type);
while (type != NULL && (tag__is_typedef(type) || tag__is_modifier(type)))
type = cu__type(cu, type->type);
return type;
}
size_t __tag__id_not_found_fprintf(FILE *fp, type_id_t id,
const char *fn, int line)
{
return fprintf(fp, "<ERROR(%s:%d): %d not found!>\n", fn, line, id);
}
static struct ase_type_name_to_size {
const char *name;
size_t size;
} base_type_name_to_size_table[] = {
{ .name = "unsigned", .size = 32, },
{ .name = "signed int", .size = 32, },
{ .name = "unsigned int", .size = 32, },
{ .name = "int", .size = 32, },
{ .name = "short unsigned int", .size = 16, },
{ .name = "signed short", .size = 16, },
{ .name = "unsigned short", .size = 16, },
{ .name = "short int", .size = 16, },
{ .name = "short", .size = 16, },
{ .name = "char", .size = 8, },
{ .name = "signed char", .size = 8, },
{ .name = "unsigned char", .size = 8, },
{ .name = "signed long", .size = 0, },
{ .name = "long int", .size = 0, },
{ .name = "long", .size = 0, },
{ .name = "signed long", .size = 0, },
{ .name = "unsigned long", .size = 0, },
{ .name = "long unsigned int", .size = 0, },
{ .name = "bool", .size = 8, },
{ .name = "_Bool", .size = 8, },
{ .name = "long long unsigned int", .size = 64, },
{ .name = "long long int", .size = 64, },
{ .name = "long long", .size = 64, },
{ .name = "signed long long", .size = 64, },
{ .name = "unsigned long long", .size = 64, },
{ .name = "double", .size = 64, },
{ .name = "double double", .size = 64, },
{ .name = "single float", .size = 32, },
{ .name = "float", .size = 32, },
{ .name = "long double", .size = sizeof(long double) * 8, },
{ .name = "long double long double", .size = sizeof(long double) * 8, },
{ .name = "__int128", .size = 128, },
{ .name = "unsigned __int128", .size = 128, },
{ .name = "__int128 unsigned", .size = 128, },
{ .name = "_Float128", .size = 128, },
{ .name = NULL },
};
bool base_type__language_defined(struct base_type *bt)
{
int i = 0;
char bf[64];
const char *name;
if (bt->name_has_encoding)
name = bt->name;
else
name = base_type__name(bt, bf, sizeof(bf));
while (base_type_name_to_size_table[i].name != NULL) {
if (bt->name_has_encoding) {
if (strcmp(base_type_name_to_size_table[i].name, bt->name) == 0)
return true;
} else if (strcmp(base_type_name_to_size_table[i].name, name) == 0)
return true;
++i;
}
return false;
}
size_t base_type__name_to_size(struct base_type *bt, struct cu *cu)
{
int i = 0;
char bf[64];
const char *name, *orig_name;
if (bt->name_has_encoding)
name = bt->name;
else
name = base_type__name(bt, bf, sizeof(bf));
orig_name = name;
try_again:
while (base_type_name_to_size_table[i].name != NULL) {
if (bt->name_has_encoding) {
if (strcmp(base_type_name_to_size_table[i].name, bt->name) == 0) {
size_t size;
found:
size = base_type_name_to_size_table[i].size;
return size ?: ((size_t)cu->addr_size * 8);
}
} else if (strcmp(base_type_name_to_size_table[i].name, name) == 0)
goto found;
++i;
}
if (strstarts(name, "signed ")) {
i = 0;
name += sizeof("signed");
goto try_again;
}
fprintf(stderr, "%s: %s %s\n",
__func__, dwarf_tag_name(bt->tag.tag), orig_name);
return 0;
}
static const char *base_type_fp_type_str[] = {
[BT_FP_SINGLE] = "single",
[BT_FP_DOUBLE] = "double",
[BT_FP_CMPLX] = "complex",
[BT_FP_CMPLX_DBL] = "complex double",
[BT_FP_CMPLX_LDBL] = "complex long double",
[BT_FP_LDBL] = "long double",
[BT_FP_INTVL] = "interval",
[BT_FP_INTVL_DBL] = "interval double",
[BT_FP_INTVL_LDBL] = "interval long double",
[BT_FP_IMGRY] = "imaginary",
[BT_FP_IMGRY_DBL] = "imaginary double",
[BT_FP_IMGRY_LDBL] = "imaginary long double",
};
const char *__base_type__name(const struct base_type *bt)
{
return bt->name;
}
const char *base_type__name(const struct base_type *bt, char *bf, size_t len)
{
if (bt->name_has_encoding)
return __base_type__name(bt);
if (bt->float_type)
snprintf(bf, len, "%s %s", base_type_fp_type_str[bt->float_type], bt->name);
else
snprintf(bf, len, "%s%s%s", bt->is_bool ? "bool " : "", bt->is_varargs ? "... " : "", bt->name);
return bf;
}
void namespace__delete(struct namespace *space, struct cu *cu)
{
struct tag *pos, *n;
if (space == NULL)
return;
namespace__for_each_tag_safe_reverse(space, pos, n) {
list_del_init(&pos->node);
/* Look for nested namespaces */
if (tag__has_namespace(pos))
namespace__delete(tag__namespace(pos), cu);
tag__delete(pos, cu);
}
tag__delete(&space->tag, cu);
}
void __type__init(struct type *type)
{
INIT_LIST_HEAD(&type->node);
INIT_LIST_HEAD(&type->type_enum);
INIT_LIST_HEAD(&type->template_type_params);
INIT_LIST_HEAD(&type->template_value_params);
type->template_parameter_pack = NULL;
type->sizeof_member = NULL;
type->member_prefix = NULL;
type->member_prefix_len = 0;
type->suffix_disambiguation = 0;
}
struct class_member *
type__find_first_biggest_size_base_type_member(struct type *type,
const struct cu *cu)
{
struct class_member *pos, *result = NULL;
size_t result_size = 0;
type__for_each_data_member(type, pos) {
if (pos->is_static)
continue;
struct tag *type = cu__type(cu, pos->tag.type);
size_t member_size = 0, power2;
struct class_member *inner = NULL;
if (type == NULL) {
tag__id_not_found_fprintf(stderr, pos->tag.type);
continue;
}
reevaluate:
switch (type->tag) {
case DW_TAG_base_type:
member_size = base_type__size(type);
break;
case DW_TAG_pointer_type:
case DW_TAG_reference_type:
member_size = cu->addr_size;
break;
case DW_TAG_class_type:
case DW_TAG_union_type:
case DW_TAG_structure_type:
if (tag__type(type)->nr_members == 0)
continue;
inner = type__find_first_biggest_size_base_type_member(tag__type(type), cu);
member_size = inner->byte_size;
break;
case DW_TAG_array_type:
case DW_TAG_const_type:
case DW_TAG_typedef:
case DW_TAG_rvalue_reference_type:
case DW_TAG_volatile_type:
case DW_TAG_atomic_type: {
struct tag *tag = cu__type(cu, type->type);
if (tag == NULL) {
tag__id_not_found_fprintf(stderr, type->type);
continue;
}
type = tag;
}
goto reevaluate;
case DW_TAG_enumeration_type:
member_size = tag__type(type)->size / 8;
break;
}
/* long long */
if (member_size > cu->addr_size)
return pos;
for (power2 = cu->addr_size; power2 > result_size; power2 /= 2)
if (member_size >= power2) {
if (power2 == cu->addr_size)
return inner ?: pos;
result_size = power2;
result = inner ?: pos;
}
}
return result;
}
static void cu__find_class_holes(struct cu *cu)
{
uint32_t id;
struct class *pos;
cu__for_each_struct(cu, id, pos)
class__find_holes(pos);
}
struct cus {
uint32_t nr_entries;
struct list_head cus;
pthread_mutex_t mutex;
void (*loader_exit)(struct cus *cus);
void *priv; // Used in dwarf_loader__exit()
};
void cus__lock(struct cus *cus)
{
pthread_mutex_lock(&cus->mutex);
}
void cus__unlock(struct cus *cus)
{
pthread_mutex_unlock(&cus->mutex);
}
void cus__set_cu_state(struct cus *cus, struct cu *cu, enum cu_state state)
{
cus__lock(cus);
cu->state = state;
cus__unlock(cus);
}
// Used only when reproducible builds are desired
struct cu *cus__get_next_processable_cu(struct cus *cus)
{
struct cu *cu;
cus__lock(cus);
list_for_each_entry(cu, &cus->cus, node) {
switch (cu->state) {
case CU__LOADED:
cu->state = CU__PROCESSING;
goto found;
case CU__PROCESSING:
// This will happen when we get to parallel
// reproducible BTF encoding, libbpf dedup work needed
// here. The other possibility is when we're flushing
// the DWARF processed CUs when the parallel DWARF
// loading stoped and we still have CUs to encode to
// BTF because of ordering requirements.
continue;
case CU__UNPROCESSED:
// The first entry isn't loaded, signal the
// caller to return and try another day, as we
// need to respect the original DWARF CU ordering.
goto out;
}
}
out:
cu = NULL;
found:
cus__unlock(cus);
return cu;
}
bool cus__empty(const struct cus *cus)
{
return list_empty(&cus->cus);
}
uint32_t cus__nr_entries(const struct cus *cus)
{
return cus->nr_entries;
}
void __cus__remove(struct cus *cus, struct cu *cu)
{
cus->nr_entries--;
list_del_init(&cu->node);
}
void cus__remove(struct cus *cus, struct cu *cu)
{
cus__lock(cus);
__cus__remove(cus, cu);
cus__unlock(cus);
}
void __cus__add(struct cus *cus, struct cu *cu)
{
cus->nr_entries++;
list_add_tail(&cu->node, &cus->cus);
}
void cus__add(struct cus *cus, struct cu *cu)
{
cus__lock(cus);
__cus__add(cus, cu);
cus__unlock(cus);
cu__find_class_holes(cu);
}
static void ptr_table__init(struct ptr_table *pt)
{
pt->entries = NULL;
pt->nr_entries = pt->allocated_entries = 0;
}
static void ptr_table__exit(struct ptr_table *pt)
{
zfree(&pt->entries);
}
static int ptr_table__add(struct ptr_table *pt, void *ptr, uint32_t *idxp)
{
const uint32_t nr_entries = pt->nr_entries + 1;
const uint32_t rc = pt->nr_entries;
if (nr_entries > pt->allocated_entries) {
uint32_t allocated_entries = pt->allocated_entries + 2048;
void *entries = realloc(pt->entries,
sizeof(void *) * allocated_entries);
if (entries == NULL)
return -ENOMEM;
/* Zero out the new range */
memset(entries + pt->allocated_entries * sizeof(void *), 0,
(allocated_entries - pt->allocated_entries) * sizeof(void *));
pt->allocated_entries = allocated_entries;
pt->entries = entries;
}
pt->entries[rc] = ptr;
pt->nr_entries = nr_entries;
*idxp = rc;
return 0;
}
static int ptr_table__add_with_id(struct ptr_table *pt, void *ptr,
uint32_t id)
{
/* Assume we won't be fed with the same id more than once */
if (id >= pt->allocated_entries) {
uint32_t allocated_entries = roundup(id + 1, 2048);
void *entries = realloc(pt->entries,
sizeof(void *) * allocated_entries);
if (entries == NULL)
return -ENOMEM;
/* Zero out the new range */
memset(entries + pt->allocated_entries * sizeof(void *), 0,
(allocated_entries - pt->allocated_entries) * sizeof(void *));
pt->allocated_entries = allocated_entries;
pt->entries = entries;
}
pt->entries[id] = ptr;
if (id >= pt->nr_entries)
pt->nr_entries = id + 1;
return 0;
}
static void *ptr_table__entry(const struct ptr_table *pt, uint32_t id)
{
return id >= pt->nr_entries ? NULL : pt->entries[id];
}
static void cu__insert_function(struct cu *cu, struct tag *tag)
{
struct function *function = tag__function(tag);
struct rb_node **p = &cu->functions.rb_node;
struct rb_node *parent = NULL;
struct function *f;
while (*p != NULL) {
parent = *p;
f = rb_entry(parent, struct function, rb_node);
if (function->lexblock.ip.addr < f->lexblock.ip.addr)
p = &(*p)->rb_left;
else
p = &(*p)->rb_right;
}
rb_link_node(&function->rb_node, parent, p);
rb_insert_color(&function->rb_node, &cu->functions);
}
int cu__table_add_tag(struct cu *cu, struct tag *tag, uint32_t *type_id)
{
struct ptr_table *pt = &cu->tags_table;
if (tag__is_tag_type(tag))
pt = &cu->types_table;
else if (tag__is_function(tag)) {
pt = &cu->functions_table;
cu__insert_function(cu, tag);
}
return ptr_table__add(pt, tag, type_id) ? -ENOMEM : 0;
}
int cu__table_nullify_type_entry(struct cu *cu, uint32_t id)
{
return ptr_table__add_with_id(&cu->types_table, NULL, id);
}
int cu__add_tag(struct cu *cu, struct tag *tag, uint32_t *id)
{
int err = cu__table_add_tag(cu, tag, id);
if (err == 0)
list_add_tail(&tag->node, &cu->tags);
return err;
}
int cu__table_add_tag_with_id(struct cu *cu, struct tag *tag, uint32_t id)
{
struct ptr_table *pt = &cu->tags_table;
if (tag__is_tag_type(tag)) {
pt = &cu->types_table;
} else if (tag__is_function(tag)) {
pt = &cu->functions_table;
cu__insert_function(cu, tag);
}
return ptr_table__add_with_id(pt, tag, id);
}
int cu__add_tag_with_id(struct cu *cu, struct tag *tag, uint32_t id)
{
int err = cu__table_add_tag_with_id(cu, tag, id);
if (err == 0)
list_add_tail(&tag->node, &cu->tags);
return err;
}
int cus__fprintf_ptr_table_stats_csv_header(FILE *fp)
{
return fprintf(fp, "# cu,tags,allocated_tags,types,allocated_types,functions,allocated_functions\n");
}
int cu__fprintf_ptr_table_stats_csv(struct cu *cu, FILE *fp)
{
int printed = fprintf(fp, "%s,%u,%u,%u,%u,%u,%u\n", cu->name,
cu->tags_table.nr_entries, cu->tags_table.allocated_entries,
cu->types_table.nr_entries, cu->types_table.allocated_entries,
cu->functions_table.nr_entries, cu->functions_table.allocated_entries);
return printed;
}
struct cu *cu__new(const char *name, uint8_t addr_size,
const unsigned char *build_id, int build_id_len,
const char *filename, bool use_obstack)
{
struct cu *cu = zalloc(sizeof(*cu) + build_id_len);
if (cu != NULL) {
uint32_t void_id;
cu->use_obstack = use_obstack;
if (cu->use_obstack)
obstack_init(&cu->obstack);
if (name == NULL || filename == NULL)
goto out_free;
cu->name = strdup(name);
if (cu->name == NULL)
goto out_free;
cu->filename = strdup(filename);
if (cu->filename == NULL)
goto out_free_name;
ptr_table__init(&cu->tags_table);
ptr_table__init(&cu->types_table);
ptr_table__init(&cu->functions_table);
/*
* the first entry is historically associated with void,
* so make sure we don't use it
*/
if (ptr_table__add(&cu->types_table, NULL, &void_id) < 0)
goto out_free_filename;
cu->functions = RB_ROOT;
cu->dfops = NULL;
INIT_LIST_HEAD(&cu->tags);
INIT_LIST_HEAD(&cu->tool_list);
INIT_LIST_HEAD(&cu->node);
cu->addr_size = addr_size;
cu->extra_dbg_info = 0;
cu->state = CU__UNPROCESSED;
cu->nr_inline_expansions = 0;
cu->size_inline_expansions = 0;
cu->nr_structures_changed = 0;
cu->nr_functions_changed = 0;
cu->max_len_changed_item = 0;
cu->function_bytes_added = 0;
cu->function_bytes_removed = 0;
cu->build_id_len = build_id_len;
if (build_id_len > 0)
memcpy(cu->build_id, build_id, build_id_len);
cu->priv = NULL;
}
return cu;
out_free_filename:
zfree(&cu->filename);
out_free_name:
zfree(&cu->name);
out_free:
free(cu);
return NULL;
}
void cu__delete(struct cu *cu)
{
if (cu == NULL)
return;
ptr_table__exit(&cu->tags_table);
ptr_table__exit(&cu->types_table);
ptr_table__exit(&cu->functions_table);
if (cu->dfops && cu->dfops->cu__delete)
cu->dfops->cu__delete(cu);
if (cu->use_obstack)
obstack_free(&cu->obstack, NULL);
zfree(&cu->filename);
zfree(&cu->name);
free(cu);
}
bool cu__same_build_id(const struct cu *cu, const struct cu *other)
{
return cu->build_id_len != 0 &&
cu->build_id_len == other->build_id_len &&
memcmp(cu->build_id, other->build_id, cu->build_id_len) == 0;
}
struct tag *cu__function(const struct cu *cu, const uint32_t id)
{
return cu ? ptr_table__entry(&cu->functions_table, id) : NULL;
}
struct tag *cu__tag(const struct cu *cu, const uint32_t id)
{
return cu ? ptr_table__entry(&cu->tags_table, id) : NULL;
}
struct tag *cu__type(const struct cu *cu, const type_id_t id)
{
return cu ? ptr_table__entry(&cu->types_table, id) : NULL;
}
struct tag *cu__find_first_typedef_of_type(const struct cu *cu,
const type_id_t type)
{
uint32_t id;
struct tag *pos;
if (cu == NULL || type == 0)
return NULL;
cu__for_each_type(cu, id, pos)
if (tag__is_typedef(pos) && pos->type == type)
return pos;
return NULL;
}
struct tag *cu__find_base_type_by_name(const struct cu *cu,
const char *name, type_id_t *idp)
{
uint32_t id;
struct tag *pos;
if (cu == NULL || name == NULL)
return NULL;
cu__for_each_type(cu, id, pos) {
if (pos->tag != DW_TAG_base_type)
continue;
const struct base_type *bt = tag__base_type(pos);
char bf[64];
const char *bname = base_type__name(bt, bf, sizeof(bf));
if (!bname || strcmp(bname, name) != 0)
continue;
if (idp != NULL)
*idp = id;
return pos;
}
return NULL;
}
struct tag *cu__find_base_type_by_name_and_size(const struct cu *cu, const char *name,
uint16_t bit_size, type_id_t *idp)
{
uint32_t id;
struct tag *pos;
if (name == NULL)
return NULL;
cu__for_each_type(cu, id, pos) {
if (pos->tag == DW_TAG_base_type) {
const struct base_type *bt = tag__base_type(pos);
char bf[64];
if (bt->bit_size == bit_size &&
strcmp(base_type__name(bt, bf, sizeof(bf)), name) == 0) {
if (idp != NULL)
*idp = id;
return pos;
}
}
}
return NULL;
}
struct tag *cu__find_enumeration_by_name_and_size(const struct cu *cu, const char *name,
uint16_t bit_size, type_id_t *idp)
{
uint32_t id;
struct tag *pos;
if (name == NULL)
return NULL;
cu__for_each_type(cu, id, pos) {
if (pos->tag == DW_TAG_enumeration_type) {
const struct type *t = tag__type(pos);
if (t->size == bit_size &&
strcmp(type__name(t), name) == 0) {
if (idp != NULL)
*idp = id;
return pos;
}
}
}
return NULL;
}
struct tag *cu__find_enumeration_by_name(const struct cu *cu, const char *name, type_id_t *idp)
{
uint32_t id;
struct tag *pos;
if (name == NULL)
return NULL;
cu__for_each_type(cu, id, pos) {
if (pos->tag == DW_TAG_enumeration_type) {
const struct type *type = tag__type(pos);
const char *tname = type__name(type);
if (tname && strcmp(tname, name) == 0) {
if (idp != NULL)
*idp = id;
return pos;
}
}
}
return NULL;
}
struct tag *cu__find_type_by_name(const struct cu *cu, const char *name, const int include_decls, type_id_t *idp)
{
if (cu == NULL || name == NULL)
return NULL;