1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019 Facebook
3
4#include <linux/bpf.h>
5#include <stdint.h>
6#include <bpf/bpf_helpers.h>
7#include <bpf/bpf_core_read.h>
8
9char _license[] SEC("license") = "GPL";
10
11struct {
12 char in[256];
13 char out[256];
14} data = {};
15
16struct core_reloc_size_output {
17 int int_sz;
18 int int_off;
19 int struct_sz;
20 int struct_off;
21 int union_sz;
22 int union_off;
23 int arr_sz;
24 int arr_off;
25 int arr_elem_sz;
26 int arr_elem_off;
27 int ptr_sz;
28 int ptr_off;
29 int enum_sz;
30 int enum_off;
31 int float_sz;
32 int float_off;
33};
34
35struct core_reloc_size {
36 int int_field;
37 struct { int x; } struct_field;
38 union { int x; } union_field;
39 int arr_field[4];
40 void *ptr_field;
41 enum { VALUE = 123 } enum_field;
42 float float_field;
43};
44
45SEC("raw_tracepoint/sys_enter")
46int test_core_size(void *ctx)
47{
48 struct core_reloc_size *in = (void *)&data.in;
49 struct core_reloc_size_output *out = (void *)&data.out;
50
51 out->int_sz = bpf_core_field_size(in->int_field);
52 out->int_off = bpf_core_field_offset(in->int_field);
53
54 out->struct_sz = bpf_core_field_size(in->struct_field);
55 out->struct_off = bpf_core_field_offset(in->struct_field);
56
57 out->union_sz = bpf_core_field_size(in->union_field);
58 out->union_off = bpf_core_field_offset(in->union_field);
59
60 out->arr_sz = bpf_core_field_size(in->arr_field);
61 out->arr_off = bpf_core_field_offset(in->arr_field);
62
63 out->arr_elem_sz = bpf_core_field_size(struct core_reloc_size, arr_field[1]);
64 out->arr_elem_off = bpf_core_field_offset(struct core_reloc_size, arr_field[1]);
65
66 out->ptr_sz = bpf_core_field_size(struct core_reloc_size, ptr_field);
67 out->ptr_off = bpf_core_field_offset(struct core_reloc_size, ptr_field);
68
69 out->enum_sz = bpf_core_field_size(struct core_reloc_size, enum_field);
70 out->enum_off = bpf_core_field_offset(struct core_reloc_size, enum_field);
71
72 out->float_sz = bpf_core_field_size(struct core_reloc_size, float_field);
73 out->float_off = bpf_core_field_offset(struct core_reloc_size, float_field);
74
75 return 0;
76}
77
78

source code of linux/tools/testing/selftests/bpf/progs/test_core_reloc_size.c