libbpf: Fix btf_dump's packed struct determination
[platform/kernel/linux-starfive.git] / tools / testing / selftests / bpf / progs / btf_dump_test_case_packing.c
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3 /*
4  * BTF-to-C dumper tests for struct packing determination.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 /* ----- START-EXPECTED-OUTPUT ----- */
9 struct packed_trailing_space {
10         int a;
11         short b;
12 } __attribute__((packed));
13
14 struct non_packed_trailing_space {
15         int a;
16         short b;
17 };
18
19 struct packed_fields {
20         short a;
21         int b;
22 } __attribute__((packed));
23
24 struct non_packed_fields {
25         short a;
26         int b;
27 };
28
29 struct nested_packed {
30         char: 4;
31         int a: 4;
32         long b;
33         struct {
34                 char c;
35                 int d;
36         } __attribute__((packed)) e;
37 } __attribute__((packed));
38
39 union union_is_never_packed {
40         int a: 4;
41         char b;
42         char c: 1;
43 };
44
45 union union_does_not_need_packing {
46         struct {
47                 long a;
48                 int b;
49         } __attribute__((packed));
50         int c;
51 };
52
53 union jump_code_union {
54         char code[5];
55         struct {
56                 char jump;
57                 int offset;
58         } __attribute__((packed));
59 };
60
61 /* ----- START-EXPECTED-OUTPUT ----- */
62 /*
63  *struct nested_packed_but_aligned_struct {
64  *      int x1;
65  *      int x2;
66  *};
67  *
68  *struct outer_implicitly_packed_struct {
69  *      char y1;
70  *      struct nested_packed_but_aligned_struct y2;
71  *} __attribute__((packed));
72  *
73  */
74 /* ------ END-EXPECTED-OUTPUT ------ */
75
76 struct nested_packed_but_aligned_struct {
77         int x1;
78         int x2;
79 } __attribute__((packed));
80
81 struct outer_implicitly_packed_struct {
82         char y1;
83         struct nested_packed_but_aligned_struct y2;
84 };
85 /* ----- START-EXPECTED-OUTPUT ----- */
86 /*
87  *struct usb_ss_ep_comp_descriptor {
88  *      char: 8;
89  *      char bDescriptorType;
90  *      char bMaxBurst;
91  *      short wBytesPerInterval;
92  *};
93  *
94  *struct usb_host_endpoint {
95  *      long: 64;
96  *      char: 8;
97  *      struct usb_ss_ep_comp_descriptor ss_ep_comp;
98  *      long: 0;
99  *} __attribute__((packed));
100  *
101  */
102 /* ------ END-EXPECTED-OUTPUT ------ */
103
104 struct usb_ss_ep_comp_descriptor {
105         char: 8;
106         char bDescriptorType;
107         char bMaxBurst;
108         int: 0;
109         short wBytesPerInterval;
110 } __attribute__((packed));
111
112 struct usb_host_endpoint {
113         long: 64;
114         char: 8;
115         struct usb_ss_ep_comp_descriptor ss_ep_comp;
116         long: 0;
117 };
118
119 /* ----- START-EXPECTED-OUTPUT ----- */
120 struct nested_packed_struct {
121         int a;
122         char b;
123 } __attribute__((packed));
124
125 struct outer_nonpacked_struct {
126         short a;
127         struct nested_packed_struct b;
128 };
129
130 struct outer_packed_struct {
131         short a;
132         struct nested_packed_struct b;
133 } __attribute__((packed));
134
135 /* ------ END-EXPECTED-OUTPUT ------ */
136
137 int f(struct {
138         struct packed_trailing_space _1;
139         struct non_packed_trailing_space _2;
140         struct packed_fields _3;
141         struct non_packed_fields _4;
142         struct nested_packed _5;
143         union union_is_never_packed _6;
144         union union_does_not_need_packing _7;
145         union jump_code_union _8;
146         struct outer_implicitly_packed_struct _9;
147         struct usb_host_endpoint _10;
148         struct outer_nonpacked_struct _11;
149         struct outer_packed_struct _12;
150 } *_)
151 {
152         return 0;
153 }