forked from acmel/dwarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarf_loader.c
3636 lines (3051 loc) · 92.8 KB
/
dwarf_loader.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) 2008 Arnaldo Carvalho de Melo <[email protected]>
*/
#include <assert.h>
#include <dirent.h>
#include <dwarf.h>
#include <elfutils/libdwfl.h>
#include <elfutils/version.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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "list.h"
#include "dwarves.h"
#include "dutil.h"
#include "hash.h"
#ifndef DW_AT_alignment
#define DW_AT_alignment 0x88
#endif
#ifndef DW_AT_GNU_vector
#define DW_AT_GNU_vector 0x2107
#endif
#ifndef DW_TAG_GNU_call_site
#define DW_TAG_GNU_call_site 0x4109
#define DW_TAG_GNU_call_site_parameter 0x410a
#endif
#ifndef DW_TAG_call_site
#define DW_TAG_call_site 0x48
#define DW_TAG_call_site_parameter 0x49
#endif
#ifndef DW_FORM_implicit_const
#define DW_FORM_implicit_const 0x21
#endif
#ifndef DW_OP_addrx
#define DW_OP_addrx 0xa1
#endif
#ifndef EM_RISCV
#define EM_RISCV 243
#endif
static pthread_mutex_t libdw__lock = PTHREAD_MUTEX_INITIALIZER;
static uint32_t hashtags__bits = 12;
static uint32_t max_hashtags__bits = 21;
static uint32_t hashtags__fn(Dwarf_Off key)
{
return hash_64(key, hashtags__bits);
}
bool no_bitfield_type_recode = true;
static void __tag__print_not_supported(uint32_t tag, const char *func)
{
static bool dwarf_tags_warned[DW_TAG_GNU_call_site_parameter + 64];
if (tag < sizeof(dwarf_tags_warned)) {
if (dwarf_tags_warned[tag])
return;
dwarf_tags_warned[tag] = true;
}
fprintf(stderr, "%s: tag not supported %#x (%s)!\n", func,
tag, dwarf_tag_name(tag));
}
#define tag__print_not_supported(tag) \
__tag__print_not_supported(tag, __func__)
struct dwarf_off_ref {
unsigned int from_types : 1;
Dwarf_Off off;
};
typedef struct dwarf_off_ref dwarf_off_ref;
struct dwarf_tag {
struct hlist_node hash_node;
dwarf_off_ref type;
Dwarf_Off id;
union {
dwarf_off_ref abstract_origin;
dwarf_off_ref containing_type;
};
struct tag *tag;
uint32_t small_id;
uint16_t decl_line;
const char *decl_file;
};
static dwarf_off_ref dwarf_tag__spec(struct dwarf_tag *dtag)
{
return *(dwarf_off_ref *)(dtag + 1);
}
static void dwarf_tag__set_spec(struct dwarf_tag *dtag, dwarf_off_ref spec)
{
*(dwarf_off_ref *)(dtag + 1) = spec;
}
struct dwarf_cu {
struct hlist_head *hash_tags;
struct hlist_head *hash_types;
struct dwarf_tag *last_type_lookup;
struct cu *cu;
struct dwarf_cu *type_unit;
};
static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
{
static struct dwarf_tag sentinel_dtag = { .id = ULLONG_MAX, };
uint64_t hashtags_size = 1UL << hashtags__bits;
dcu->cu = cu;
dcu->hash_tags = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
if (!dcu->hash_tags)
return -ENOMEM;
dcu->hash_types = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
if (!dcu->hash_types) {
cu__free(cu, dcu->hash_tags);
return -ENOMEM;
}
unsigned int i;
for (i = 0; i < hashtags_size; ++i) {
INIT_HLIST_HEAD(&dcu->hash_tags[i]);
INIT_HLIST_HEAD(&dcu->hash_types[i]);
}
dcu->type_unit = NULL;
// To avoid a per-lookup check against NULL in dwarf_cu__find_type_by_ref()
dcu->last_type_lookup = &sentinel_dtag;
return 0;
}
static struct dwarf_cu *dwarf_cu__new(struct cu *cu)
{
struct dwarf_cu *dwarf_cu = cu__zalloc(cu, sizeof(*dwarf_cu));
if (dwarf_cu != NULL && dwarf_cu__init(dwarf_cu, cu) != 0) {
cu__free(cu, dwarf_cu);
dwarf_cu = NULL;
}
return dwarf_cu;
}
static void dwarf_cu__delete(struct cu *cu)
{
if (cu == NULL || cu->priv == NULL)
return;
struct dwarf_cu *dcu = cu->priv;
// dcu->hash_tags & dcu->hash_types are on cu->obstack
cu__free(cu, dcu);
cu->priv = NULL;
}
static void __tag__print_type_not_found(struct tag *tag, const char *func)
{
struct dwarf_tag *dtag = tag->priv;
fprintf(stderr, "%s: couldn't find %#llx type for %#llx (%s)!\n", func,
(unsigned long long)dtag->type.off, (unsigned long long)dtag->id,
dwarf_tag_name(tag->tag));
}
#define tag__print_type_not_found(tag) \
__tag__print_type_not_found(tag, __func__)
static void hashtags__hash(struct hlist_head *hashtable,
struct dwarf_tag *dtag)
{
struct hlist_head *head = hashtable + hashtags__fn(dtag->id);
hlist_add_head(&dtag->hash_node, head);
}
static struct dwarf_tag *hashtags__find(const struct hlist_head *hashtable,
const Dwarf_Off id)
{
if (id == 0)
return NULL;
struct dwarf_tag *tpos;
struct hlist_node *pos;
uint32_t bucket = hashtags__fn(id);
const struct hlist_head *head = hashtable + bucket;
hlist_for_each_entry(tpos, pos, head, hash_node) {
if (tpos->id == id)
return tpos;
}
return NULL;
}
static void cu__hash(struct cu *cu, struct tag *tag)
{
struct dwarf_cu *dcu = cu->priv;
struct hlist_head *hashtable = tag__is_tag_type(tag) ?
dcu->hash_types :
dcu->hash_tags;
hashtags__hash(hashtable, tag->priv);
}
static struct dwarf_tag *dwarf_cu__find_tag_by_ref(const struct dwarf_cu *cu,
const struct dwarf_off_ref *ref)
{
if (cu == NULL)
return NULL;
if (ref->from_types) {
return NULL;
}
return hashtags__find(cu->hash_tags, ref->off);
}
static struct dwarf_tag *dwarf_cu__find_type_by_ref(struct dwarf_cu *dcu,
const struct dwarf_off_ref *ref)
{
if (dcu == NULL)
return NULL;
if (ref->from_types) {
dcu = dcu->type_unit;
if (dcu == NULL) {
return NULL;
}
}
if (dcu->last_type_lookup->id == ref->off)
return dcu->last_type_lookup;
struct dwarf_tag *dtag = hashtags__find(dcu->hash_types, ref->off);
if (dtag)
dcu->last_type_lookup = dtag;
return dtag;
}
static void *memdup(const void *src, size_t len, struct cu *cu)
{
void *s = cu__malloc(cu, len);
if (s != NULL)
memcpy(s, src, len);
return s;
}
/* Number decoding macros. See 7.6 Variable Length Data. */
#define get_uleb128_step(var, addr, nth, break) \
__b = *(addr)++; \
var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
if ((__b & 0x80) == 0) \
break
#define get_uleb128_rest_return(var, i, addrp) \
do { \
for (; i < 10; ++i) { \
get_uleb128_step(var, *addrp, i, \
return var); \
} \
/* Other implementations set VALUE to UINT_MAX in this \
case. So we better do this as well. */ \
return UINT64_MAX; \
} while (0)
static uint64_t __libdw_get_uleb128(uint64_t acc, uint32_t i,
const uint8_t **addrp)
{
uint8_t __b;
get_uleb128_rest_return (acc, i, addrp);
}
#define get_uleb128(var, addr) \
do { \
uint8_t __b; \
var = 0; \
get_uleb128_step(var, addr, 0, break); \
var = __libdw_get_uleb128 (var, 1, &(addr)); \
} while (0)
static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
{
Dwarf_Attribute attr;
uint32_t form;
if (dwarf_attr(die, name, &attr) == NULL)
return 0;
form = dwarf_whatform(&attr);
switch (form) {
case DW_FORM_addr: {
Dwarf_Addr addr;
if (dwarf_formaddr(&attr, &addr) == 0)
return addr;
}
break;
case DW_FORM_implicit_const:
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_sdata:
case DW_FORM_udata: {
Dwarf_Word value;
if (dwarf_formudata(&attr, &value) == 0)
return value;
}
break;
case DW_FORM_flag:
case DW_FORM_flag_present: {
bool value;
if (dwarf_formflag(&attr, &value) == 0)
return value;
}
break;
default:
fprintf(stderr, "DW_AT_<0x%x>=0x%x\n", name, form);
break;
}
return 0;
}
static uint64_t attr_alignment(Dwarf_Die *die, struct conf_load *conf)
{
return conf->ignore_alignment_attr ? 0 : attr_numeric(die, DW_AT_alignment);
}
static uint64_t dwarf_expr(const uint8_t *expr, uint32_t len __maybe_unused)
{
/* Common case: offset from start of the class */
if (expr[0] == DW_OP_plus_uconst ||
expr[0] == DW_OP_constu) {
uint64_t result;
++expr;
get_uleb128(result, expr);
return result;
}
fprintf(stderr, "%s: unhandled %#x DW_OP_ operation\n",
__func__, *expr);
return UINT64_MAX;
}
static Dwarf_Off __attr_offset(Dwarf_Attribute *attr)
{
Dwarf_Block block;
switch (dwarf_whatform(attr)) {
case DW_FORM_implicit_const:
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_sdata:
case DW_FORM_udata: {
Dwarf_Word value;
if (dwarf_formudata(attr, &value) == 0)
return value;
break;
}
default:
if (dwarf_formblock(attr, &block) == 0)
return dwarf_expr(block.data, block.length);
}
return 0;
}
static Dwarf_Off attr_offset(Dwarf_Die *die, const uint32_t name)
{
Dwarf_Attribute attr;
if (dwarf_attr(die, name, &attr) == NULL)
return 0;
return __attr_offset(&attr);
}
static const char *attr_string(Dwarf_Die *die, uint32_t name, struct conf_load *conf __maybe_unused)
{
const char *str = NULL;
Dwarf_Attribute attr;
if (dwarf_attr(die, name, &attr) != NULL) {
str = dwarf_formstring(&attr);
if (conf && conf->kabi_prefix && str && strncmp(str, conf->kabi_prefix, conf->kabi_prefix_len) == 0)
return conf->kabi_prefix;
}
return str;
}
static struct dwarf_off_ref attr_type(Dwarf_Die *die, uint32_t attr_name)
{
Dwarf_Attribute attr;
struct dwarf_off_ref ref;
if (dwarf_attr(die, attr_name, &attr) != NULL) {
Dwarf_Die type_die;
if (dwarf_formref_die(&attr, &type_die) != NULL) {
ref.from_types = attr.form == DW_FORM_ref_sig8;
ref.off = dwarf_dieoffset(&type_die);
return ref;
}
}
memset(&ref, 0, sizeof(ref));
return ref;
}
static int attr_location(Dwarf_Die *die, Dwarf_Op **expr, size_t *exprlen)
{
Dwarf_Attribute attr;
if (dwarf_attr(die, DW_AT_location, &attr) != NULL) {
if (dwarf_getlocation(&attr, expr, exprlen) == 0) {
/* DW_OP_addrx needs additional lookup for real addr. */
if (*exprlen != 0 && expr[0]->atom == DW_OP_addrx) {
Dwarf_Attribute addr_attr;
dwarf_getlocation_attr(&attr, expr[0], &addr_attr);
Dwarf_Addr address;
dwarf_formaddr (&addr_attr, &address);
expr[0]->number = address;
}
return 0;
}
}
return 1;
}
static void *__tag__alloc(struct dwarf_cu *dcu, size_t size, bool spec)
{
struct dwarf_tag *dtag = cu__zalloc(dcu->cu, (sizeof(*dtag) + (spec ? sizeof(dwarf_off_ref) : 0)));
if (dtag == NULL)
return NULL;
struct tag *tag = cu__zalloc(dcu->cu, size);
if (tag == NULL)
return NULL;
dtag->tag = tag;
tag->priv = dtag;
tag->type = 0;
tag->top_level = 0;
return tag;
}
static void *tag__alloc(struct cu *cu, size_t size)
{
return __tag__alloc(cu->priv, size, false);
}
static void *tag__alloc_with_spec(struct cu *cu, size_t size)
{
return __tag__alloc(cu->priv, size, true);
}
static void tag__init(struct tag *tag, struct cu *cu, Dwarf_Die *die)
{
struct dwarf_tag *dtag = tag->priv;
tag->tag = dwarf_tag(die);
dtag->id = dwarf_dieoffset(die);
if (tag->tag == DW_TAG_imported_module ||
tag->tag == DW_TAG_imported_declaration)
dtag->type = attr_type(die, DW_AT_import);
else
dtag->type = attr_type(die, DW_AT_type);
dtag->abstract_origin = attr_type(die, DW_AT_abstract_origin);
tag->recursivity_level = 0;
if (cu->extra_dbg_info) {
pthread_mutex_lock(&libdw__lock);
int32_t decl_line;
const char *decl_file = dwarf_decl_file(die);
static const char *last_decl_file, *last_decl_file_ptr;
if (decl_file != last_decl_file_ptr) {
last_decl_file = decl_file ? strdup(decl_file) : NULL;
last_decl_file_ptr = decl_file;
}
dtag->decl_file = last_decl_file;
dwarf_decl_line(die, &decl_line);
dtag->decl_line = decl_line;
pthread_mutex_unlock(&libdw__lock);
}
INIT_LIST_HEAD(&tag->node);
}
static struct tag *tag__new(Dwarf_Die *die, struct cu *cu)
{
struct tag *tag = tag__alloc(cu, sizeof(*tag));
if (tag != NULL)
tag__init(tag, cu, die);
return tag;
}
static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
struct cu *cu)
{
struct ptr_to_member_type *ptr = tag__alloc(cu, sizeof(*ptr));
if (ptr != NULL) {
tag__init(&ptr->tag, cu, die);
struct dwarf_tag *dtag = ptr->tag.priv;
dtag->containing_type = attr_type(die, DW_AT_containing_type);
}
return ptr;
}
static uint8_t encoding_to_float_type(uint64_t encoding)
{
switch (encoding) {
case DW_ATE_complex_float: return BT_FP_CMPLX;
case DW_ATE_float: return BT_FP_SINGLE;
case DW_ATE_imaginary_float: return BT_FP_IMGRY;
default: return 0;
}
}
static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
struct base_type *bt = tag__alloc(cu, sizeof(*bt));
if (bt != NULL) {
tag__init(&bt->tag, cu, die);
bt->name = attr_string(die, DW_AT_name, conf);
bt->bit_size = attr_numeric(die, DW_AT_byte_size) * 8;
uint64_t encoding = attr_numeric(die, DW_AT_encoding);
bt->is_bool = encoding == DW_ATE_boolean;
bt->is_signed = (encoding == DW_ATE_signed) || (encoding == DW_ATE_signed_char);
bt->is_varargs = false;
bt->name_has_encoding = true;
bt->float_type = encoding_to_float_type(encoding);
INIT_LIST_HEAD(&bt->node);
}
return bt;
}
static struct array_type *array_type__new(Dwarf_Die *die, struct cu *cu)
{
struct array_type *at = tag__alloc(cu, sizeof(*at));
if (at != NULL) {
tag__init(&at->tag, cu, die);
at->dimensions = 0;
at->nr_entries = NULL;
at->is_vector = dwarf_hasattr(die, DW_AT_GNU_vector);
}
return at;
}
static struct string_type *string_type__new(Dwarf_Die *die, struct cu *cu)
{
struct string_type *st = tag__alloc(cu, sizeof(*st));
if (st != NULL) {
tag__init(&st->tag, cu, die);
st->nr_entries = attr_numeric(die, DW_AT_byte_size);
if (st->nr_entries == 0)
st->nr_entries = 1;
}
return st;
}
static void namespace__init(struct namespace *namespace, Dwarf_Die *die,
struct cu *cu, struct conf_load *conf)
{
tag__init(&namespace->tag, cu, die);
INIT_LIST_HEAD(&namespace->tags);
INIT_LIST_HEAD(&namespace->annots);
namespace->name = attr_string(die, DW_AT_name, conf);
namespace->nr_tags = 0;
namespace->shared_tags = 0;
}
static struct namespace *namespace__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
struct namespace *namespace = tag__alloc(cu, sizeof(*namespace));
if (namespace != NULL)
namespace__init(namespace, die, cu, conf);
return namespace;
}
static void type__init(struct type *type, Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
namespace__init(&type->namespace, die, cu, conf);
__type__init(type);
type->size = attr_numeric(die, DW_AT_byte_size);
type->alignment = attr_alignment(die, conf);
type->declaration = attr_numeric(die, DW_AT_declaration);
dwarf_tag__set_spec(type->namespace.tag.priv,
attr_type(die, DW_AT_specification));
type->definition_emitted = 0;
type->fwd_decl_emitted = 0;
type->resized = 0;
type->nr_members = 0;
type->nr_static_members = 0;
type->is_signed_enum = 0;
Dwarf_Attribute attr;
if (dwarf_attr(die, DW_AT_type, &attr) != NULL) {
Dwarf_Die type_die;
if (dwarf_formref_die(&attr, &type_die) != NULL) {
uint64_t encoding = attr_numeric(&type_die, DW_AT_encoding);
if (encoding == DW_ATE_signed || encoding == DW_ATE_signed_char)
type->is_signed_enum = 1;
}
}
}
static struct type *type__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
struct type *type = tag__alloc_with_spec(cu, sizeof(*type));
if (type != NULL)
type__init(type, die, cu, conf);
return type;
}
static struct enumerator *enumerator__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
struct enumerator *enumerator = tag__alloc(cu, sizeof(*enumerator));
if (enumerator != NULL) {
tag__init(&enumerator->tag, cu, die);
enumerator->name = attr_string(die, DW_AT_name, conf);
enumerator->value = attr_numeric(die, DW_AT_const_value);
}
return enumerator;
}
static enum vscope dwarf__location(Dwarf_Die *die, uint64_t *addr, struct location *location)
{
enum vscope scope = VSCOPE_UNKNOWN;
if (attr_location(die, &location->expr, &location->exprlen) != 0)
scope = VSCOPE_OPTIMIZED;
else if (location->exprlen != 0) {
Dwarf_Op *expr = location->expr;
switch (expr->atom) {
case DW_OP_addr:
case DW_OP_addrx:
scope = VSCOPE_GLOBAL;
*addr = expr[0].number;
break;
case DW_OP_reg1 ... DW_OP_reg31:
case DW_OP_breg0 ... DW_OP_breg31:
scope = VSCOPE_REGISTER; break;
case DW_OP_fbreg:
scope = VSCOPE_LOCAL; break;
}
}
return scope;
}
enum vscope variable__scope(const struct variable *var)
{
return var->scope;
}
const char *variable__scope_str(const struct variable *var)
{
switch (var->scope) {
case VSCOPE_LOCAL: return "local";
case VSCOPE_GLOBAL: return "global";
case VSCOPE_REGISTER: return "register";
case VSCOPE_OPTIMIZED: return "optimized";
default: break;
};
return "unknown";
}
static struct variable *variable__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
{
struct variable *var;
bool has_specification;
has_specification = dwarf_hasattr(die, DW_AT_specification);
if (has_specification) {
var = tag__alloc_with_spec(cu, sizeof(*var));
} else {
var = tag__alloc(cu, sizeof(*var));
}
if (var != NULL) {
tag__init(&var->ip.tag, cu, die);
var->name = attr_string(die, DW_AT_name, conf);
/* variable is visible outside of its enclosing cu */
var->external = dwarf_hasattr(die, DW_AT_external);
/* non-defining declaration of an object */
var->declaration = dwarf_hasattr(die, DW_AT_declaration);
var->has_specification = has_specification;
var->scope = VSCOPE_UNKNOWN;
INIT_LIST_HEAD(&var->annots);
var->ip.addr = 0;
if (!var->declaration && cu->has_addr_info)
var->scope = dwarf__location(die, &var->ip.addr, &var->location);
if (has_specification) {
dwarf_tag__set_spec(var->ip.tag.priv,
attr_type(die, DW_AT_specification));
}
}
return var;
}
static int tag__recode_dwarf_bitfield(struct tag *tag, struct cu *cu, uint16_t bit_size)
{
int id;
type_id_t short_id;
struct tag *recoded;
/* in all the cases the name is at the same offset */
const char *name = namespace__name(tag__namespace(tag));
switch (tag->tag) {
case DW_TAG_typedef: {
const struct dwarf_tag *dtag = tag->priv;
struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
if (dtype == NULL) {
tag__print_type_not_found(tag);
return -ENOENT;
}
struct tag *type = dtype->tag;
id = tag__recode_dwarf_bitfield(type, cu, bit_size);
if (id < 0)
return id;
struct type *new_typedef = cu__zalloc(cu, sizeof(*new_typedef));
if (new_typedef == NULL)
return -ENOMEM;
recoded = (struct tag *)new_typedef;
recoded->tag = DW_TAG_typedef;
recoded->type = id;
new_typedef->namespace.name = tag__namespace(tag)->name;
}
break;
case DW_TAG_const_type:
case DW_TAG_volatile_type:
case DW_TAG_atomic_type: {
const struct dwarf_tag *dtag = tag->priv;
struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
if (dtype == NULL) {
tag__print_type_not_found(tag);
return -ENOENT;
}
struct tag *type = dtype->tag;
id = tag__recode_dwarf_bitfield(type, cu, bit_size);
if (id >= 0 && (uint32_t)id == tag->type)
return id;
recoded = cu__zalloc(cu, sizeof(*recoded));
if (recoded == NULL)
return -ENOMEM;
recoded->tag = DW_TAG_volatile_type;
recoded->type = id;
}
break;
case DW_TAG_base_type:
/*
* Here we must search on the final, core cu, not on
* the dwarf_cu as in dwarf there are no such things
* as base_types of less than 8 bits, etc.
*/
recoded = cu__find_base_type_by_name_and_size(cu, name, bit_size, &short_id);
if (recoded != NULL)
return short_id;
struct base_type *new_bt = cu__zalloc(cu, sizeof(*new_bt));
if (new_bt == NULL)
return -ENOMEM;
recoded = (struct tag *)new_bt;
recoded->tag = DW_TAG_base_type;
recoded->top_level = 1;
new_bt->name = strdup(name);
new_bt->bit_size = bit_size;
break;
case DW_TAG_enumeration_type:
/*
* Here we must search on the final, core cu, not on
* the dwarf_cu as in dwarf there are no such things
* as enumeration_types of less than 8 bits, etc.
*/
recoded = cu__find_enumeration_by_name_and_size(cu, name, bit_size, &short_id);
if (recoded != NULL)
return short_id;
struct type *alias = tag__type(tag);
struct type *new_enum = cu__zalloc(cu, sizeof(*new_enum));
if (new_enum == NULL)
return -ENOMEM;
recoded = (struct tag *)new_enum;
recoded->tag = DW_TAG_enumeration_type;
recoded->top_level = 1;
new_enum->nr_members = alias->nr_members;
/*
* Share the tags
*/
new_enum->namespace.tags.next = &alias->namespace.tags;
new_enum->namespace.shared_tags = 1;
new_enum->namespace.name = strdup(name);
new_enum->size = bit_size;
break;
default:
fprintf(stderr, "%s: tag=%s, name=%s, bit_size=%d\n",
__func__, dwarf_tag_name(tag->tag),
name, bit_size);
return -EINVAL;
}
uint32_t new_id;
if (cu__add_tag(cu, recoded, &new_id) == 0)
return new_id;
free(recoded);
return -ENOMEM;
}
static int add_llvm_annotation(Dwarf_Die *die, int component_idx, struct conf_load *conf,
struct list_head *head)
{
struct llvm_annotation *annot;
const char *name;
if (conf->skip_encoding_btf_decl_tag)
return 0;
/* Only handle btf_decl_tag annotation for now. */
name = attr_string(die, DW_AT_name, conf);
if (strcmp(name, "btf_decl_tag") != 0)
return 0;
annot = zalloc(sizeof(*annot));
if (!annot)
return -ENOMEM;
annot->value = attr_string(die, DW_AT_const_value, conf);
annot->component_idx = component_idx;
list_add_tail(&annot->node, head);
return 0;
}
static int add_child_llvm_annotations(Dwarf_Die *die, int component_idx,
struct conf_load *conf, struct list_head *head)
{
Dwarf_Die child;
int ret;
if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
return 0;
die = &child;
do {
if (dwarf_tag(die) == DW_TAG_LLVM_annotation) {
ret = add_llvm_annotation(die, component_idx, conf, head);
if (ret)
return ret;
}
} while (dwarf_siblingof(die, die) == 0);
return 0;
}
int class_member__dwarf_recode_bitfield(struct class_member *member,
struct cu *cu)
{
struct dwarf_tag *dtag = member->tag.priv;
struct dwarf_tag *type = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
int recoded_type_id;
if (type == NULL)
return -ENOENT;
recoded_type_id = tag__recode_dwarf_bitfield(type->tag, cu, member->bitfield_size);
if (recoded_type_id < 0)
return recoded_type_id;
member->tag.type = recoded_type_id;
return 0;
}
static struct class_member *class_member__new(Dwarf_Die *die, struct cu *cu,
bool in_union, struct conf_load *conf)
{
struct class_member *member = tag__alloc(cu, sizeof(*member));
if (member != NULL) {
tag__init(&member->tag, cu, die);
member->name = attr_string(die, DW_AT_name, conf);
member->alignment = attr_alignment(die, conf);
Dwarf_Attribute attr;
member->has_bit_offset = dwarf_attr(die, DW_AT_data_bit_offset, &attr) != NULL;
if (member->has_bit_offset) {
member->bit_offset = __attr_offset(&attr);
// byte_offset and bitfield_offset will be recalculated later, when
// we discover the size of this bitfield base type.
} else {
if (dwarf_attr(die, DW_AT_data_member_location, &attr) != NULL) {
member->byte_offset = __attr_offset(&attr);
} else {
member->is_static = !in_union;
}
/*
* Bit offset calculated here is valid only for byte-aligned
* fields. For bitfields on little-endian archs we need to
* adjust them taking into account byte size of the field,
* which might not be yet known. So we'll re-calculate bit
* offset later, in class_member__cache_byte_size.
*/
member->bit_offset = member->byte_offset * 8;
member->bitfield_offset = attr_numeric(die, DW_AT_bit_offset);
}
/*
* If DW_AT_byte_size is not present, byte size will be
* determined later in class_member__cache_byte_size using
* base integer/enum type
*/
member->byte_size = attr_numeric(die, DW_AT_byte_size);
member->bitfield_size = attr_numeric(die, DW_AT_bit_size);
member->bit_hole = 0;
member->bitfield_end = 0;
member->visited = 0;
if (!cu__is_c(cu)) {
member->accessibility = attr_numeric(die, DW_AT_accessibility);
member->const_value = attr_numeric(die, DW_AT_const_value);
member->virtuality = attr_numeric(die, DW_AT_virtuality);
}
member->hole = 0;
}
return member;
}
/* How many function parameters are passed via registers? Used below in