forked from acmel/dwarves
-
Notifications
You must be signed in to change notification settings - Fork 1
/
btf_encoder.c
1896 lines (1625 loc) · 52.4 KB
/
btf_encoder.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) 2019 Facebook
Derived from ctf_encoder.c, which is:
Copyright (C) Arnaldo Carvalho de Melo <acme@redhat.com>
Copyright (C) Red Hat Inc
*/
#include <linux/btf.h>
#include "dwarves.h"
#include "elf_symtab.h"
#include "btf_encoder.h"
#include "gobuffer.h"
#include <bpf/btf.h>
#include <bpf/libbpf.h>
#include <ctype.h> /* for isalpha() and isalnum() */
#include <stdlib.h> /* for qsort() and bsearch() */
#include <inttypes.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <search.h> /* for tsearch(), tfind() and tdestroy() */
#include <pthread.h>
#define BTF_ENCODER_MAX_PARAMETERS 12
/* state used to do later encoding of saved functions */
struct btf_encoder_state {
uint32_t type_id_off;
bool got_parameter_names;
const char *parameter_names[BTF_ENCODER_MAX_PARAMETERS];
};
struct elf_function {
const char *name;
bool generated;
size_t prefixlen;
struct function *function;
struct btf_encoder_state state;
};
#define MAX_PERCPU_VAR_CNT 4096
struct var_info {
uint64_t addr;
const char *name;
uint32_t sz;
};
/*
* cu: cu being processed.
*/
struct btf_encoder {
struct list_head node;
struct btf *btf;
struct cu *cu;
struct gobuffer percpu_secinfo;
const char *filename;
struct elf_symtab *symtab;
uint32_t type_id_off;
int saved_func_cnt;
bool has_index_type,
need_index_type,
skip_encoding_vars,
raw_output,
verbose,
force,
gen_floats,
is_rel;
uint32_t array_index_id;
struct {
struct var_info vars[MAX_PERCPU_VAR_CNT];
int var_cnt;
uint32_t shndx;
uint64_t base_addr;
uint64_t sec_sz;
} percpu;
struct {
struct elf_function *entries;
int allocated;
int cnt;
int suffix_cnt; /* number of .isra, .part etc */
} functions;
};
static LIST_HEAD(encoders);
static pthread_mutex_t encoders__lock = PTHREAD_MUTEX_INITIALIZER;
static void btf_encoder__add_saved_funcs(struct btf_encoder *encoder);
/* mutex only needed for add/delete, as this can happen in multiple encoding
* threads. Traversal of the list is currently confined to thread collection.
*/
#define btf_encoders__for_each_encoder(encoder) \
list_for_each_entry(encoder, &encoders, node)
static void btf_encoders__add(struct btf_encoder *encoder)
{
pthread_mutex_lock(&encoders__lock);
list_add_tail(&encoder->node, &encoders);
pthread_mutex_unlock(&encoders__lock);
}
static void btf_encoders__delete(struct btf_encoder *encoder)
{
struct btf_encoder *existing = NULL;
pthread_mutex_lock(&encoders__lock);
/* encoder may not have been added to list yet; check. */
btf_encoders__for_each_encoder(existing) {
if (encoder == existing)
break;
}
if (encoder == existing)
list_del(&encoder->node);
pthread_mutex_unlock(&encoders__lock);
}
#define PERCPU_SECTION ".data..percpu"
/*
* This depends on the GNU extension to eliminate the stray comma in the zero
* arguments case.
*
* The difference between elf_errmsg(-1) and elf_errmsg(elf_errno()) is that the
* latter clears the current error.
*/
#define elf_error(fmt, ...) \
fprintf(stderr, "%s: " fmt ": %s.\n", __func__, ##__VA_ARGS__, elf_errmsg(-1))
/*
* This depends on the GNU extension to eliminate the stray comma in the zero
* arguments case.
*
* The difference between elf_errmsg(-1) and elf_errmsg(elf_errno()) is that the
* latter clears the current error.
*/
#define elf_error(fmt, ...) \
fprintf(stderr, "%s: " fmt ": %s.\n", __func__, ##__VA_ARGS__, elf_errmsg(-1))
static int btf_var_secinfo_cmp(const void *a, const void *b)
{
const struct btf_var_secinfo *av = a;
const struct btf_var_secinfo *bv = b;
return av->offset - bv->offset;
}
#define BITS_PER_BYTE 8
#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
#define BITS_ROUNDUP_BYTES(bits) (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
static const char * const btf_kind_str[] = {
[BTF_KIND_UNKN] = "UNKNOWN",
[BTF_KIND_INT] = "INT",
[BTF_KIND_PTR] = "PTR",
[BTF_KIND_ARRAY] = "ARRAY",
[BTF_KIND_STRUCT] = "STRUCT",
[BTF_KIND_UNION] = "UNION",
[BTF_KIND_ENUM] = "ENUM",
[BTF_KIND_FWD] = "FWD",
[BTF_KIND_TYPEDEF] = "TYPEDEF",
[BTF_KIND_VOLATILE] = "VOLATILE",
[BTF_KIND_CONST] = "CONST",
[BTF_KIND_RESTRICT] = "RESTRICT",
[BTF_KIND_FUNC] = "FUNC",
[BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
[BTF_KIND_VAR] = "VAR",
[BTF_KIND_DATASEC] = "DATASEC",
[BTF_KIND_FLOAT] = "FLOAT",
[BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
[BTF_KIND_ENUM64] = "ENUM64",
};
static const char *btf__printable_name(const struct btf *btf, uint32_t offset)
{
if (!offset)
return "(anon)";
else
return btf__str_by_offset(btf, offset);
}
static const char * btf__int_encoding_str(uint8_t encoding)
{
if (encoding == 0)
return "(none)";
else if (encoding == BTF_INT_SIGNED)
return "SIGNED";
else if (encoding == BTF_INT_CHAR)
return "CHAR";
else if (encoding == BTF_INT_BOOL)
return "BOOL";
else
return "UNKN";
}
__attribute ((format (printf, 5, 6)))
static void btf__log_err(const struct btf *btf, int kind, const char *name,
bool output_cr, const char *fmt, ...)
{
fprintf(stderr, "[%u] %s %s", btf__type_cnt(btf),
btf_kind_str[kind], name ?: "(anon)");
if (fmt && *fmt) {
va_list ap;
fprintf(stderr, " ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
if (output_cr)
fprintf(stderr, "\n");
}
__attribute ((format (printf, 5, 6)))
static void btf_encoder__log_type(const struct btf_encoder *encoder, const struct btf_type *t,
bool err, bool output_cr, const char *fmt, ...)
{
const struct btf *btf = encoder->btf;
uint8_t kind;
FILE *out;
if (!encoder->verbose && !err)
return;
kind = BTF_INFO_KIND(t->info);
out = err ? stderr : stdout;
fprintf(out, "[%u] %s %s",
btf__type_cnt(btf) - 1, btf_kind_str[kind],
btf__printable_name(btf, t->name_off));
if (fmt && *fmt) {
va_list ap;
fprintf(out, " ");
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
}
if (output_cr)
fprintf(out, "\n");
}
__attribute ((format (printf, 5, 6)))
static void btf_encoder__log_member(const struct btf_encoder *encoder, const struct btf_type *t,
const struct btf_member *member, bool err, const char *fmt, ...)
{
const struct btf *btf = encoder->btf;
FILE *out;
if (!encoder->verbose && !err)
return;
out = err ? stderr : stdout;
if (btf_kflag(t))
fprintf(out, "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
btf__printable_name(btf, member->name_off),
member->type,
BTF_MEMBER_BITFIELD_SIZE(member->offset),
BTF_MEMBER_BIT_OFFSET(member->offset));
else
fprintf(out, "\t%s type_id=%u bits_offset=%u",
btf__printable_name(btf, member->name_off),
member->type,
member->offset);
if (fmt && *fmt) {
va_list ap;
fprintf(out, " ");
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
}
fprintf(out, "\n");
}
__attribute ((format (printf, 6, 7)))
static void btf_encoder__log_func_param(struct btf_encoder *encoder, const char *name, uint32_t type,
bool err, bool is_last_param, const char *fmt, ...)
{
FILE *out;
if (!encoder->verbose && !err)
return;
out = err ? stderr : stdout;
if (is_last_param && !type)
fprintf(out, "vararg)\n");
else
fprintf(out, "%u %s%s", type, name, is_last_param ? ")\n" : ", ");
if (fmt && *fmt) {
va_list ap;
fprintf(out, " ");
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
}
}
static int32_t btf_encoder__add_float(struct btf_encoder *encoder, const struct base_type *bt, const char *name)
{
int32_t id = btf__add_float(encoder->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
if (id < 0) {
btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
} else {
const struct btf_type *t;
t = btf__type_by_id(encoder->btf, id);
btf_encoder__log_type(encoder, t, false, true, "size=%u nr_bits=%u", t->size, bt->bit_size);
}
return id;
}
static int32_t btf_encoder__add_base_type(struct btf_encoder *encoder, const struct base_type *bt, const char *name)
{
const struct btf_type *t;
uint8_t encoding = 0;
uint16_t byte_sz;
int32_t id;
if (bt->is_signed) {
encoding = BTF_INT_SIGNED;
} else if (bt->is_bool) {
encoding = BTF_INT_BOOL;
} else if (bt->float_type && encoder->gen_floats) {
/*
* Encode floats as BTF_KIND_FLOAT if allowed, otherwise (in
* compatibility mode) encode them as BTF_KIND_INT - that's not
* fully correct, but that's what it used to be.
*/
if (bt->float_type == BT_FP_SINGLE ||
bt->float_type == BT_FP_DOUBLE ||
bt->float_type == BT_FP_LDBL)
return btf_encoder__add_float(encoder, bt, name);
fprintf(stderr, "Complex, interval and imaginary float types are not supported\n");
return -1;
}
/* dwarf5 may emit DW_ATE_[un]signed_{num} base types where
* {num} is not power of 2 and may exceed 128. Such attributes
* are mostly used to record operation for an actual parameter
* or variable.
* For example,
* DW_AT_location (indexed (0x3c) loclist = 0x00008fb0:
* [0xffffffff82808812, 0xffffffff82808817):
* DW_OP_breg0 RAX+0,
* DW_OP_convert (0x000e97d5) "DW_ATE_unsigned_64",
* DW_OP_convert (0x000e97df) "DW_ATE_unsigned_8",
* DW_OP_stack_value,
* DW_OP_piece 0x1,
* DW_OP_breg0 RAX+0,
* DW_OP_convert (0x000e97d5) "DW_ATE_unsigned_64",
* DW_OP_convert (0x000e97da) "DW_ATE_unsigned_32",
* DW_OP_lit8,
* DW_OP_shr,
* DW_OP_convert (0x000e97da) "DW_ATE_unsigned_32",
* DW_OP_convert (0x000e97e4) "DW_ATE_unsigned_24",
* DW_OP_stack_value, DW_OP_piece 0x3
* DW_AT_name ("ebx")
* DW_AT_decl_file ("/linux/arch/x86/events/intel/core.c")
*
* In the above example, at some point, one unsigned_32 value
* is right shifted by 8 and the result is converted to unsigned_32
* and then unsigned_24.
*
* BTF does not need such DW_OP_* information so let us sanitize
* these non-regular int types to avoid libbpf/kernel complaints.
*/
byte_sz = BITS_ROUNDUP_BYTES(bt->bit_size);
if (!byte_sz || (byte_sz & (byte_sz - 1))) {
name = "__SANITIZED_FAKE_INT__";
byte_sz = 4;
}
id = btf__add_int(encoder->btf, name, byte_sz, encoding);
if (id < 0) {
btf__log_err(encoder->btf, BTF_KIND_INT, name, true, "Error emitting BTF type");
} else {
t = btf__type_by_id(encoder->btf, id);
btf_encoder__log_type(encoder, t, false, true, "size=%u nr_bits=%u encoding=%s%s",
t->size, bt->bit_size, btf__int_encoding_str(encoding),
id < 0 ? " Error in emitting BTF" : "" );
}
return id;
}
static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t kind, uint32_t type,
const char *name, bool kind_flag)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
int32_t id;
switch (kind) {
case BTF_KIND_PTR:
id = btf__add_ptr(btf, type);
break;
case BTF_KIND_VOLATILE:
id = btf__add_volatile(btf, type);
break;
case BTF_KIND_CONST:
id = btf__add_const(btf, type);
break;
case BTF_KIND_RESTRICT:
id = btf__add_restrict(btf, type);
break;
case BTF_KIND_TYPEDEF:
id = btf__add_typedef(btf, name, type);
break;
case BTF_KIND_TYPE_TAG:
id = btf__add_type_tag(btf, name, type);
break;
case BTF_KIND_FWD:
id = btf__add_fwd(btf, name, kind_flag);
break;
case BTF_KIND_FUNC:
id = btf__add_func(btf, name, BTF_FUNC_STATIC, type);
break;
default:
btf__log_err(btf, kind, name, true, "Unexpected kind for reference");
return -1;
}
if (id > 0) {
t = btf__type_by_id(btf, id);
if (kind == BTF_KIND_FWD)
btf_encoder__log_type(encoder, t, false, true, "%s", kind_flag ? "union" : "struct");
else
btf_encoder__log_type(encoder, t, false, true, "type_id=%u", t->type);
} else {
btf__log_err(btf, kind, name, true, "Error emitting BTF type");
}
return id;
}
static int32_t btf_encoder__add_array(struct btf_encoder *encoder, uint32_t type, uint32_t index_type, uint32_t nelems)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
const struct btf_array *array;
int32_t id;
id = btf__add_array(btf, index_type, type, nelems);
if (id > 0) {
t = btf__type_by_id(btf, id);
array = btf_array(t);
btf_encoder__log_type(encoder, t, false, true, "type_id=%u index_type_id=%u nr_elems=%u",
array->type, array->index_type, array->nelems);
} else {
btf__log_err(btf, BTF_KIND_ARRAY, NULL, true,
"type_id=%u index_type_id=%u nr_elems=%u Error emitting BTF type",
type, index_type, nelems);
}
return id;
}
static int btf_encoder__add_field(struct btf_encoder *encoder, const char *name, uint32_t type, uint32_t bitfield_size, uint32_t offset)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
const struct btf_member *m;
int err;
err = btf__add_field(btf, name, type, offset, bitfield_size);
t = btf__type_by_id(btf, btf__type_cnt(btf) - 1);
if (err) {
fprintf(stderr, "[%u] %s %s's field '%s' offset=%u bit_size=%u type=%u Error emitting field\n",
btf__type_cnt(btf) - 1, btf_kind_str[btf_kind(t)],
btf__printable_name(btf, t->name_off),
name, offset, bitfield_size, type);
} else {
m = &btf_members(t)[btf_vlen(t) - 1];
btf_encoder__log_member(encoder, t, m, false, NULL);
}
return err;
}
static int32_t btf_encoder__add_struct(struct btf_encoder *encoder, uint8_t kind, const char *name, uint32_t size)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
int32_t id;
switch (kind) {
case BTF_KIND_STRUCT:
id = btf__add_struct(btf, name, size);
break;
case BTF_KIND_UNION:
id = btf__add_union(btf, name, size);
break;
default:
btf__log_err(btf, kind, name, true, "Unexpected kind of struct");
return -1;
}
if (id < 0) {
btf__log_err(btf, kind, name, true, "Error emitting BTF type");
} else {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
}
return id;
}
#if LIBBPF_MAJOR_VERSION < 1
static inline int libbpf_err(int ret)
{
if (ret < 0)
errno = -ret;
return ret;
}
static
int btf__add_enum64(struct btf *btf __maybe_unused, const char *name __maybe_unused,
__u32 byte_sz __maybe_unused, bool is_signed __maybe_unused)
{
return libbpf_err(-ENOTSUP);
}
static
int btf__add_enum64_value(struct btf *btf __maybe_unused, const char *name __maybe_unused,
__u64 value __maybe_unused)
{
return libbpf_err(-ENOTSUP);
}
#endif
static int32_t btf_encoder__add_enum(struct btf_encoder *encoder, const char *name, struct type *etype,
struct conf_load *conf_load)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
int32_t id, size;
bool is_enum32;
size = BITS_ROUNDUP_BYTES(etype->size);
is_enum32 = size <= 4 || conf_load->skip_encoding_btf_enum64;
if (is_enum32)
id = btf__add_enum(btf, name, size);
else
id = btf__add_enum64(btf, name, size, etype->is_signed_enum);
if (id > 0) {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
} else {
btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true,
"size=%u Error emitting BTF type", size);
}
return id;
}
static int btf_encoder__add_enum_val(struct btf_encoder *encoder, const char *name, int64_t value,
struct type *etype, struct conf_load *conf_load)
{
const char *fmt_str;
int err;
/* If enum64 is not allowed, generate enum32 with unsigned int value. In enum64-supported
* libbpf library, btf__add_enum_value() will set the kflag (sign bit) in common_type
* if the value is negative.
*/
if (conf_load->skip_encoding_btf_enum64)
err = btf__add_enum_value(encoder->btf, name, (uint32_t)value);
else if (etype->size > 32)
err = btf__add_enum64_value(encoder->btf, name, value);
else
err = btf__add_enum_value(encoder->btf, name, value);
if (!err) {
if (encoder->verbose) {
if (conf_load->skip_encoding_btf_enum64) {
printf("\t%s val=%u\n", name, (uint32_t)value);
} else {
fmt_str = etype->is_signed_enum ? "\t%s val=%lld\n" : "\t%s val=%llu\n";
printf(fmt_str, name, (unsigned long long)value);
}
}
} else {
if (conf_load->skip_encoding_btf_enum64) {
fprintf(stderr, "\t%s val=%u Error emitting BTF enum value\n", name, (uint32_t)value);
} else {
fmt_str = etype->is_signed_enum ? "\t%s val=%lld Error emitting BTF enum value\n"
: "\t%s val=%llu Error emitting BTF enum value\n";
fprintf(stderr, fmt_str, name, (unsigned long long)value);
}
}
return err;
}
static int32_t btf_encoder__add_func_param(struct btf_encoder *encoder, const char *name, uint32_t type, bool is_last_param)
{
int err = btf__add_func_param(encoder->btf, name, type);
if (!err) {
btf_encoder__log_func_param(encoder, name, type, false, is_last_param, NULL);
return 0;
} else {
btf_encoder__log_func_param(encoder, name, type, true, is_last_param, "Error adding func param");
return -1;
}
}
static int32_t btf_encoder__tag_type(struct btf_encoder *encoder, uint32_t tag_type)
{
if (tag_type == 0)
return 0;
return encoder->type_id_off + tag_type;
}
static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct ftype *ftype)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
struct parameter *param;
uint16_t nr_params, param_idx;
int32_t id, type_id;
/* add btf_type for func_proto */
nr_params = ftype->nr_parms + (ftype->unspec_parms ? 1 : 0);
type_id = btf_encoder__tag_type(encoder, ftype->tag.type);
id = btf__add_func_proto(btf, type_id);
if (id > 0) {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, false, "return=%u args=(%s", t->type, !nr_params ? "void)\n" : "");
} else {
btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true,
"return=%u vlen=%u Error emitting BTF type",
type_id, nr_params);
return id;
}
/* add parameters */
param_idx = 0;
ftype__for_each_parameter(ftype, param) {
const char *name = parameter__name(param);
type_id = param->tag.type == 0 ? 0 : encoder->type_id_off + param->tag.type;
++param_idx;
if (btf_encoder__add_func_param(encoder, name, type_id, param_idx == nr_params))
return -1;
}
++param_idx;
if (ftype->unspec_parms)
if (btf_encoder__add_func_param(encoder, NULL, 0, param_idx == nr_params))
return -1;
return id;
}
static int32_t btf_encoder__add_var(struct btf_encoder *encoder, uint32_t type, const char *name, uint32_t linkage)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
int32_t id;
id = btf__add_var(btf, name, linkage, type);
if (id > 0) {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, true, "type=%u linkage=%u", t->type, btf_var(t)->linkage);
} else {
btf__log_err(btf, BTF_KIND_VAR, name, true,
"type=%u linkage=%u Error emitting BTF type",
type, linkage);
}
return id;
}
static int32_t btf_encoder__add_var_secinfo(struct btf_encoder *encoder, uint32_t type,
uint32_t offset, uint32_t size)
{
struct btf_var_secinfo si = {
.type = type,
.offset = offset,
.size = size,
};
return gobuffer__add(&encoder->percpu_secinfo, &si, sizeof(si));
}
int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder *other)
{
struct gobuffer *var_secinfo_buf = &other->percpu_secinfo;
size_t sz = gobuffer__size(var_secinfo_buf);
uint16_t nr_var_secinfo = sz / sizeof(struct btf_var_secinfo);
uint32_t type_id;
uint32_t next_type_id = btf__type_cnt(encoder->btf);
int32_t i, id;
struct btf_var_secinfo *vsi;
if (encoder == other)
return 0;
btf_encoder__add_saved_funcs(other);
for (i = 0; i < nr_var_secinfo; i++) {
vsi = (struct btf_var_secinfo *)var_secinfo_buf->entries + i;
type_id = next_type_id + vsi->type - 1; /* Type ID starts from 1 */
id = btf_encoder__add_var_secinfo(encoder, type_id, vsi->offset, vsi->size);
if (id < 0)
return id;
}
return btf__add_btf(encoder->btf, other->btf);
}
static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, const char *section_name)
{
struct gobuffer *var_secinfo_buf = &encoder->percpu_secinfo;
struct btf *btf = encoder->btf;
size_t sz = gobuffer__size(var_secinfo_buf);
uint16_t nr_var_secinfo = sz / sizeof(struct btf_var_secinfo);
struct btf_var_secinfo *last_vsi, *vsi;
const struct btf_type *t;
uint32_t datasec_sz;
int32_t err, id, i;
qsort(var_secinfo_buf->entries, nr_var_secinfo,
sizeof(struct btf_var_secinfo), btf_var_secinfo_cmp);
last_vsi = (struct btf_var_secinfo *)var_secinfo_buf->entries + nr_var_secinfo - 1;
datasec_sz = last_vsi->offset + last_vsi->size;
id = btf__add_datasec(btf, section_name, datasec_sz);
if (id < 0) {
btf__log_err(btf, BTF_KIND_DATASEC, section_name, true,
"size=%u vlen=%u Error emitting BTF type",
datasec_sz, nr_var_secinfo);
} else {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, true, "size=%u vlen=%u", t->size, nr_var_secinfo);
}
for (i = 0; i < nr_var_secinfo; i++) {
vsi = (struct btf_var_secinfo *)var_secinfo_buf->entries + i;
err = btf__add_datasec_var_info(btf, vsi->type, vsi->offset, vsi->size);
if (!err) {
if (encoder->verbose)
printf("\ttype=%u offset=%u size=%u\n",
vsi->type, vsi->offset, vsi->size);
} else {
fprintf(stderr, "\ttype=%u offset=%u size=%u Error emitting BTF datasec var info\n",
vsi->type, vsi->offset, vsi->size);
return -1;
}
}
return id;
}
static int32_t btf_encoder__add_decl_tag(struct btf_encoder *encoder, const char *value, uint32_t type,
int component_idx)
{
struct btf *btf = encoder->btf;
const struct btf_type *t;
int32_t id;
id = btf__add_decl_tag(btf, value, type, component_idx);
if (id > 0) {
t = btf__type_by_id(btf, id);
btf_encoder__log_type(encoder, t, false, true, "type_id=%u component_idx=%d",
t->type, component_idx);
} else {
btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, "component_idx=%d Error emitting BTF type",
component_idx);
}
return id;
}
static void parameter_names__get(struct ftype *ftype, size_t nr_parameters,
const char **parameter_names)
{
struct parameter *parameter;
int i = 0;
ftype__for_each_parameter(ftype, parameter) {
if (i >= nr_parameters)
return;
parameter_names[i++] = parameter__name(parameter);
}
}
static bool funcs__match(struct btf_encoder *encoder, struct elf_function *func, struct function *f2)
{
const char *parameter_names[BTF_ENCODER_MAX_PARAMETERS];
struct function *f1 = func->function;
const char *name;
int i;
if (!f1)
return false;
name = function__name(f1);
if (f1->proto.nr_parms != f2->proto.nr_parms) {
if (encoder->verbose)
printf("function mismatch for '%s'(%s): %d params != %d params\n",
name, f1->alias ?: name,
f1->proto.nr_parms, f2->proto.nr_parms);
return false;
}
if (f1->proto.nr_parms == 0)
return true;
if (!func->state.got_parameter_names) {
parameter_names__get(&f1->proto, BTF_ENCODER_MAX_PARAMETERS,
func->state.parameter_names);
func->state.got_parameter_names = true;
}
parameter_names__get(&f2->proto, BTF_ENCODER_MAX_PARAMETERS, parameter_names);
for (i = 0; i < f1->proto.nr_parms && i < BTF_ENCODER_MAX_PARAMETERS; i++) {
if (!func->state.parameter_names[i]) {
if (!parameter_names[i])
continue;
} else if (parameter_names[i]) {
if (strcmp(func->state.parameter_names[i], parameter_names[i]) == 0)
continue;
}
if (encoder->verbose) {
printf("function mismatch for '%s'(%s): parameter #%d '%s' != '%s'\n",
name, f1->alias ?: name, i,
func->state.parameter_names[i] ?: "<null>",
parameter_names[i] ?: "<null>");
}
return false;
}
return true;
}
static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct function *fn, struct elf_function *func)
{
if (func->function) {
struct function *existing = func->function;
/* If saving and we find an existing entry, we want to merge
* observations across both functions, checking that the
* "seen optimized parameters", "inconsistent prototype"
* and "unexpected register" status is reflected in the
* the func entry.
* If the entry is new, record encoder state required
* to add the local function later (encoder + type_id_off)
* such that we can add the function later.
*/
existing->proto.optimized_parms |= fn->proto.optimized_parms;
existing->proto.unexpected_reg |= fn->proto.unexpected_reg;
if (!existing->proto.unexpected_reg && !existing->proto.inconsistent_proto &&
!funcs__match(encoder, func, fn))
existing->proto.inconsistent_proto = 1;
} else {
func->state.type_id_off = encoder->type_id_off;
func->function = fn;
encoder->saved_func_cnt++;
}
return 0;
}
static int32_t btf_encoder__add_func(struct btf_encoder *encoder, struct function *fn)
{
int btf_fnproto_id, btf_fn_id, tag_type_id;
struct llvm_annotation *annot;
const char *name;
btf_fnproto_id = btf_encoder__add_func_proto(encoder, &fn->proto);
name = function__name(fn);
btf_fn_id = btf_encoder__add_ref_type(encoder, BTF_KIND_FUNC, btf_fnproto_id, name, false);
if (btf_fnproto_id < 0 || btf_fn_id < 0) {
printf("error: failed to encode function '%s'\n", function__name(fn));
return -1;
}
list_for_each_entry(annot, &fn->annots, node) {
tag_type_id = btf_encoder__add_decl_tag(encoder, annot->value, btf_fn_id,
annot->component_idx);
if (tag_type_id < 0) {
fprintf(stderr, "error: failed to encode tag '%s' to func %s with component_idx %d\n",
annot->value, name, annot->component_idx);
return -1;
}
}
return 0;
}
static void btf_encoder__add_saved_funcs(struct btf_encoder *encoder)
{
int i;
for (i = 0; i < encoder->functions.cnt; i++) {
struct elf_function *func = &encoder->functions.entries[i];
struct function *fn = func->function;
struct btf_encoder *other_encoder;
if (!fn || fn->proto.processed)
continue;
/* merge optimized-out status across encoders; since each
* encoder has the same elf symbol table we can use the
* same index to access the same elf symbol.
*/
btf_encoders__for_each_encoder(other_encoder) {
struct function *other_fn;
if (other_encoder == encoder)
continue;
other_fn = other_encoder->functions.entries[i].function;
if (!other_fn)
continue;
fn->proto.optimized_parms |= other_fn->proto.optimized_parms;
fn->proto.unexpected_reg |= other_fn->proto.unexpected_reg;
if (other_fn->proto.inconsistent_proto)
fn->proto.inconsistent_proto = 1;
if (!fn->proto.unexpected_reg && !fn->proto.inconsistent_proto &&
!funcs__match(encoder, func, other_fn))
fn->proto.inconsistent_proto = 1;
other_fn->proto.processed = 1;
}
/* do not exclude functions with optimized-out parameters; they
* may still be _called_ with the right parameter values, they
* just do not _use_ them. Only exclude functions with
* unexpected register use or multiple inconsistent prototypes.
*/
if (fn->proto.unexpected_reg || fn->proto.inconsistent_proto) {
if (encoder->verbose) {
const char *name = function__name(fn);
printf("skipping addition of '%s'(%s) due to %s\n",
name, fn->alias ?: name,
fn->proto.unexpected_reg ? "unexpected register used for parameter" :
"multiple inconsistent function prototypes");
}
} else {
encoder->type_id_off = func->state.type_id_off;
btf_encoder__add_func(encoder, fn);
}
fn->proto.processed = 1;
}
}
/*
* This corresponds to the same macro defined in
* include/linux/kallsyms.h
*/
#define KSYM_NAME_LEN 128
static int functions_cmp(const void *_a, const void *_b)
{
const struct elf_function *a = _a;
const struct elf_function *b = _b;
/* if search key allows prefix match, verify target has matching
* prefix len and prefix matches.
*/
if (a->prefixlen && a->prefixlen == b->prefixlen)
return strncmp(a->name, b->name, b->prefixlen);
return strcmp(a->name, b->name);
}
#ifndef max
#define max(x, y) ((x) < (y) ? (y) : (x))
#endif
static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym *sym)
{
struct elf_function *new;
const char *name;
if (elf_sym__type(sym) != STT_FUNC)
return 0;
name = elf_sym__name(sym, encoder->symtab);
if (!name)