1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <test_progs.h>
4 #include <bpf/btf.h>
5 #include "btf_helpers.h"
6 
7 static void gen_btf(struct btf *btf)
8 {
9 	const struct btf_var_secinfo *vi;
10 	const struct btf_type *t;
11 	const struct btf_member *m;
12 	const struct btf_enum *v;
13 	const struct btf_param *p;
14 	int id, err, str_off;
15 
16 	str_off = btf__find_str(btf, "int");
17 	ASSERT_EQ(str_off, -ENOENT, "int_str_missing_off");
18 
19 	str_off = btf__add_str(btf, "int");
20 	ASSERT_EQ(str_off, 1, "int_str_off");
21 
22 	str_off = btf__find_str(btf, "int");
23 	ASSERT_EQ(str_off, 1, "int_str_found_off");
24 
25 	/* BTF_KIND_INT */
26 	id = btf__add_int(btf, "int", 4,  BTF_INT_SIGNED);
27 	ASSERT_EQ(id, 1, "int_id");
28 
29 	t = btf__type_by_id(btf, 1);
30 	/* should re-use previously added "int" string */
31 	ASSERT_EQ(t->name_off, str_off, "int_name_off");
32 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "int", "int_name");
33 	ASSERT_EQ(btf_kind(t), BTF_KIND_INT, "int_kind");
34 	ASSERT_EQ(t->size, 4, "int_sz");
35 	ASSERT_EQ(btf_int_encoding(t), BTF_INT_SIGNED, "int_enc");
36 	ASSERT_EQ(btf_int_bits(t), 32, "int_bits");
37 	ASSERT_STREQ(btf_type_raw_dump(btf, 1),
38 		     "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", "raw_dump");
39 
40 	/* invalid int size */
41 	id = btf__add_int(btf, "bad sz int", 7, 0);
42 	ASSERT_ERR(id, "int_bad_sz");
43 	/* invalid encoding */
44 	id = btf__add_int(btf, "bad enc int", 4, 123);
45 	ASSERT_ERR(id, "int_bad_enc");
46 	/* NULL name */
47 	id = btf__add_int(btf, NULL, 4, 0);
48 	ASSERT_ERR(id, "int_bad_null_name");
49 	/* empty name */
50 	id = btf__add_int(btf, "", 4, 0);
51 	ASSERT_ERR(id, "int_bad_empty_name");
52 
53 	/* PTR/CONST/VOLATILE/RESTRICT */
54 	id = btf__add_ptr(btf, 1);
55 	ASSERT_EQ(id, 2, "ptr_id");
56 	t = btf__type_by_id(btf, 2);
57 	ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_kind");
58 	ASSERT_EQ(t->type, 1, "ptr_type");
59 	ASSERT_STREQ(btf_type_raw_dump(btf, 2),
60 		     "[2] PTR '(anon)' type_id=1", "raw_dump");
61 
62 	id = btf__add_const(btf, 5); /* points forward to restrict */
63 	ASSERT_EQ(id, 3, "const_id");
64 	t = btf__type_by_id(btf, 3);
65 	ASSERT_EQ(btf_kind(t), BTF_KIND_CONST, "const_kind");
66 	ASSERT_EQ(t->type, 5, "const_type");
67 	ASSERT_STREQ(btf_type_raw_dump(btf, 3),
68 		     "[3] CONST '(anon)' type_id=5", "raw_dump");
69 
70 	id = btf__add_volatile(btf, 3);
71 	ASSERT_EQ(id, 4, "volatile_id");
72 	t = btf__type_by_id(btf, 4);
73 	ASSERT_EQ(btf_kind(t), BTF_KIND_VOLATILE, "volatile_kind");
74 	ASSERT_EQ(t->type, 3, "volatile_type");
75 	ASSERT_STREQ(btf_type_raw_dump(btf, 4),
76 		     "[4] VOLATILE '(anon)' type_id=3", "raw_dump");
77 
78 	id = btf__add_restrict(btf, 4);
79 	ASSERT_EQ(id, 5, "restrict_id");
80 	t = btf__type_by_id(btf, 5);
81 	ASSERT_EQ(btf_kind(t), BTF_KIND_RESTRICT, "restrict_kind");
82 	ASSERT_EQ(t->type, 4, "restrict_type");
83 	ASSERT_STREQ(btf_type_raw_dump(btf, 5),
84 		     "[5] RESTRICT '(anon)' type_id=4", "raw_dump");
85 
86 	/* ARRAY */
87 	id = btf__add_array(btf, 1, 2, 10); /* int *[10] */
88 	ASSERT_EQ(id, 6, "array_id");
89 	t = btf__type_by_id(btf, 6);
90 	ASSERT_EQ(btf_kind(t), BTF_KIND_ARRAY, "array_kind");
91 	ASSERT_EQ(btf_array(t)->index_type, 1, "array_index_type");
92 	ASSERT_EQ(btf_array(t)->type, 2, "array_elem_type");
93 	ASSERT_EQ(btf_array(t)->nelems, 10, "array_nelems");
94 	ASSERT_STREQ(btf_type_raw_dump(btf, 6),
95 		     "[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10", "raw_dump");
96 
97 	/* STRUCT */
98 	err = btf__add_field(btf, "field", 1, 0, 0);
99 	ASSERT_ERR(err, "no_struct_field");
100 	id = btf__add_struct(btf, "s1", 8);
101 	ASSERT_EQ(id, 7, "struct_id");
102 	err = btf__add_field(btf, "f1", 1, 0, 0);
103 	ASSERT_OK(err, "f1_res");
104 	err = btf__add_field(btf, "f2", 1, 32, 16);
105 	ASSERT_OK(err, "f2_res");
106 
107 	t = btf__type_by_id(btf, 7);
108 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "s1", "struct_name");
109 	ASSERT_EQ(btf_kind(t), BTF_KIND_STRUCT, "struct_kind");
110 	ASSERT_EQ(btf_vlen(t), 2, "struct_vlen");
111 	ASSERT_EQ(btf_kflag(t), true, "struct_kflag");
112 	ASSERT_EQ(t->size, 8, "struct_sz");
113 	m = btf_members(t) + 0;
114 	ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
115 	ASSERT_EQ(m->type, 1, "f1_type");
116 	ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
117 	ASSERT_EQ(btf_member_bitfield_size(t, 0), 0, "f1_bit_sz");
118 	m = btf_members(t) + 1;
119 	ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f2", "f2_name");
120 	ASSERT_EQ(m->type, 1, "f2_type");
121 	ASSERT_EQ(btf_member_bit_offset(t, 1), 32, "f2_bit_off");
122 	ASSERT_EQ(btf_member_bitfield_size(t, 1), 16, "f2_bit_sz");
123 	ASSERT_STREQ(btf_type_raw_dump(btf, 7),
124 		     "[7] STRUCT 's1' size=8 vlen=2\n"
125 		     "\t'f1' type_id=1 bits_offset=0\n"
126 		     "\t'f2' type_id=1 bits_offset=32 bitfield_size=16", "raw_dump");
127 
128 	/* UNION */
129 	id = btf__add_union(btf, "u1", 8);
130 	ASSERT_EQ(id, 8, "union_id");
131 
132 	/* invalid, non-zero offset */
133 	err = btf__add_field(btf, "field", 1, 1, 0);
134 	ASSERT_ERR(err, "no_struct_field");
135 
136 	err = btf__add_field(btf, "f1", 1, 0, 16);
137 	ASSERT_OK(err, "f1_res");
138 
139 	t = btf__type_by_id(btf, 8);
140 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "u1", "union_name");
141 	ASSERT_EQ(btf_kind(t), BTF_KIND_UNION, "union_kind");
142 	ASSERT_EQ(btf_vlen(t), 1, "union_vlen");
143 	ASSERT_EQ(btf_kflag(t), true, "union_kflag");
144 	ASSERT_EQ(t->size, 8, "union_sz");
145 	m = btf_members(t) + 0;
146 	ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
147 	ASSERT_EQ(m->type, 1, "f1_type");
148 	ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
149 	ASSERT_EQ(btf_member_bitfield_size(t, 0), 16, "f1_bit_sz");
150 	ASSERT_STREQ(btf_type_raw_dump(btf, 8),
151 		     "[8] UNION 'u1' size=8 vlen=1\n"
152 		     "\t'f1' type_id=1 bits_offset=0 bitfield_size=16", "raw_dump");
153 
154 	/* ENUM */
155 	id = btf__add_enum(btf, "e1", 4);
156 	ASSERT_EQ(id, 9, "enum_id");
157 	err = btf__add_enum_value(btf, "v1", 1);
158 	ASSERT_OK(err, "v1_res");
159 	err = btf__add_enum_value(btf, "v2", 2);
160 	ASSERT_OK(err, "v2_res");
161 
162 	t = btf__type_by_id(btf, 9);
163 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum_name");
164 	ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_kind");
165 	ASSERT_EQ(btf_vlen(t), 2, "enum_vlen");
166 	ASSERT_EQ(t->size, 4, "enum_sz");
167 	v = btf_enum(t) + 0;
168 	ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v1", "v1_name");
169 	ASSERT_EQ(v->val, 1, "v1_val");
170 	v = btf_enum(t) + 1;
171 	ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v2", "v2_name");
172 	ASSERT_EQ(v->val, 2, "v2_val");
173 	ASSERT_STREQ(btf_type_raw_dump(btf, 9),
174 		     "[9] ENUM 'e1' size=4 vlen=2\n"
175 		     "\t'v1' val=1\n"
176 		     "\t'v2' val=2", "raw_dump");
177 
178 	/* FWDs */
179 	id = btf__add_fwd(btf, "struct_fwd", BTF_FWD_STRUCT);
180 	ASSERT_EQ(id, 10, "struct_fwd_id");
181 	t = btf__type_by_id(btf, 10);
182 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "struct_fwd", "fwd_name");
183 	ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
184 	ASSERT_EQ(btf_kflag(t), 0, "fwd_kflag");
185 	ASSERT_STREQ(btf_type_raw_dump(btf, 10),
186 		     "[10] FWD 'struct_fwd' fwd_kind=struct", "raw_dump");
187 
188 	id = btf__add_fwd(btf, "union_fwd", BTF_FWD_UNION);
189 	ASSERT_EQ(id, 11, "union_fwd_id");
190 	t = btf__type_by_id(btf, 11);
191 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "union_fwd", "fwd_name");
192 	ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
193 	ASSERT_EQ(btf_kflag(t), 1, "fwd_kflag");
194 	ASSERT_STREQ(btf_type_raw_dump(btf, 11),
195 		     "[11] FWD 'union_fwd' fwd_kind=union", "raw_dump");
196 
197 	id = btf__add_fwd(btf, "enum_fwd", BTF_FWD_ENUM);
198 	ASSERT_EQ(id, 12, "enum_fwd_id");
199 	t = btf__type_by_id(btf, 12);
200 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "enum_fwd", "fwd_name");
201 	ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_fwd_kind");
202 	ASSERT_EQ(btf_vlen(t), 0, "enum_fwd_kind");
203 	ASSERT_EQ(t->size, 4, "enum_fwd_sz");
204 	ASSERT_STREQ(btf_type_raw_dump(btf, 12),
205 		     "[12] ENUM 'enum_fwd' size=4 vlen=0", "raw_dump");
206 
207 	/* TYPEDEF */
208 	id = btf__add_typedef(btf, "typedef1", 1);
209 	ASSERT_EQ(id, 13, "typedef_fwd_id");
210 	t = btf__type_by_id(btf, 13);
211 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "typedef1", "typedef_name");
212 	ASSERT_EQ(btf_kind(t), BTF_KIND_TYPEDEF, "typedef_kind");
213 	ASSERT_EQ(t->type, 1, "typedef_type");
214 	ASSERT_STREQ(btf_type_raw_dump(btf, 13),
215 		     "[13] TYPEDEF 'typedef1' type_id=1", "raw_dump");
216 
217 	/* FUNC & FUNC_PROTO */
218 	id = btf__add_func(btf, "func1", BTF_FUNC_GLOBAL, 15);
219 	ASSERT_EQ(id, 14, "func_id");
220 	t = btf__type_by_id(btf, 14);
221 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "func1", "func_name");
222 	ASSERT_EQ(t->type, 15, "func_type");
223 	ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC, "func_kind");
224 	ASSERT_EQ(btf_vlen(t), BTF_FUNC_GLOBAL, "func_vlen");
225 	ASSERT_STREQ(btf_type_raw_dump(btf, 14),
226 		     "[14] FUNC 'func1' type_id=15 linkage=global", "raw_dump");
227 
228 	id = btf__add_func_proto(btf, 1);
229 	ASSERT_EQ(id, 15, "func_proto_id");
230 	err = btf__add_func_param(btf, "p1", 1);
231 	ASSERT_OK(err, "p1_res");
232 	err = btf__add_func_param(btf, "p2", 2);
233 	ASSERT_OK(err, "p2_res");
234 
235 	t = btf__type_by_id(btf, 15);
236 	ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC_PROTO, "func_proto_kind");
237 	ASSERT_EQ(btf_vlen(t), 2, "func_proto_vlen");
238 	ASSERT_EQ(t->type, 1, "func_proto_ret_type");
239 	p = btf_params(t) + 0;
240 	ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p1", "p1_name");
241 	ASSERT_EQ(p->type, 1, "p1_type");
242 	p = btf_params(t) + 1;
243 	ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p2", "p2_name");
244 	ASSERT_EQ(p->type, 2, "p2_type");
245 	ASSERT_STREQ(btf_type_raw_dump(btf, 15),
246 		     "[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
247 		     "\t'p1' type_id=1\n"
248 		     "\t'p2' type_id=2", "raw_dump");
249 
250 	/* VAR */
251 	id = btf__add_var(btf, "var1", BTF_VAR_GLOBAL_ALLOCATED, 1);
252 	ASSERT_EQ(id, 16, "var_id");
253 	t = btf__type_by_id(btf, 16);
254 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "var1", "var_name");
255 	ASSERT_EQ(btf_kind(t), BTF_KIND_VAR, "var_kind");
256 	ASSERT_EQ(t->type, 1, "var_type");
257 	ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_type");
258 	ASSERT_STREQ(btf_type_raw_dump(btf, 16),
259 		     "[16] VAR 'var1' type_id=1, linkage=global-alloc", "raw_dump");
260 
261 	/* DATASECT */
262 	id = btf__add_datasec(btf, "datasec1", 12);
263 	ASSERT_EQ(id, 17, "datasec_id");
264 	err = btf__add_datasec_var_info(btf, 1, 4, 8);
265 	ASSERT_OK(err, "v1_res");
266 
267 	t = btf__type_by_id(btf, 17);
268 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "datasec1", "datasec_name");
269 	ASSERT_EQ(t->size, 12, "datasec_sz");
270 	ASSERT_EQ(btf_kind(t), BTF_KIND_DATASEC, "datasec_kind");
271 	ASSERT_EQ(btf_vlen(t), 1, "datasec_vlen");
272 	vi = btf_var_secinfos(t) + 0;
273 	ASSERT_EQ(vi->type, 1, "v1_type");
274 	ASSERT_EQ(vi->offset, 4, "v1_off");
275 	ASSERT_EQ(vi->size, 8, "v1_sz");
276 	ASSERT_STREQ(btf_type_raw_dump(btf, 17),
277 		     "[17] DATASEC 'datasec1' size=12 vlen=1\n"
278 		     "\ttype_id=1 offset=4 size=8", "raw_dump");
279 
280 	/* DECL_TAG */
281 	id = btf__add_decl_tag(btf, "tag1", 16, -1);
282 	ASSERT_EQ(id, 18, "tag_id");
283 	t = btf__type_by_id(btf, 18);
284 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag1", "tag_value");
285 	ASSERT_EQ(btf_kind(t), BTF_KIND_DECL_TAG, "tag_kind");
286 	ASSERT_EQ(t->type, 16, "tag_type");
287 	ASSERT_EQ(btf_decl_tag(t)->component_idx, -1, "tag_component_idx");
288 	ASSERT_STREQ(btf_type_raw_dump(btf, 18),
289 		     "[18] DECL_TAG 'tag1' type_id=16 component_idx=-1", "raw_dump");
290 
291 	id = btf__add_decl_tag(btf, "tag2", 14, 1);
292 	ASSERT_EQ(id, 19, "tag_id");
293 	t = btf__type_by_id(btf, 19);
294 	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag2", "tag_value");
295 	ASSERT_EQ(btf_kind(t), BTF_KIND_DECL_TAG, "tag_kind");
296 	ASSERT_EQ(t->type, 14, "tag_type");
297 	ASSERT_EQ(btf_decl_tag(t)->component_idx, 1, "tag_component_idx");
298 	ASSERT_STREQ(btf_type_raw_dump(btf, 19),
299 		     "[19] DECL_TAG 'tag2' type_id=14 component_idx=1", "raw_dump");
300 }
301 
302 static void test_btf_add()
303 {
304 	struct btf *btf;
305 
306 	btf = btf__new_empty();
307 	if (!ASSERT_OK_PTR(btf, "new_empty"))
308 		return;
309 
310 	gen_btf(btf);
311 
312 	VALIDATE_RAW_BTF(
313 		btf,
314 		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
315 		"[2] PTR '(anon)' type_id=1",
316 		"[3] CONST '(anon)' type_id=5",
317 		"[4] VOLATILE '(anon)' type_id=3",
318 		"[5] RESTRICT '(anon)' type_id=4",
319 		"[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10",
320 		"[7] STRUCT 's1' size=8 vlen=2\n"
321 		"\t'f1' type_id=1 bits_offset=0\n"
322 		"\t'f2' type_id=1 bits_offset=32 bitfield_size=16",
323 		"[8] UNION 'u1' size=8 vlen=1\n"
324 		"\t'f1' type_id=1 bits_offset=0 bitfield_size=16",
325 		"[9] ENUM 'e1' size=4 vlen=2\n"
326 		"\t'v1' val=1\n"
327 		"\t'v2' val=2",
328 		"[10] FWD 'struct_fwd' fwd_kind=struct",
329 		"[11] FWD 'union_fwd' fwd_kind=union",
330 		"[12] ENUM 'enum_fwd' size=4 vlen=0",
331 		"[13] TYPEDEF 'typedef1' type_id=1",
332 		"[14] FUNC 'func1' type_id=15 linkage=global",
333 		"[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
334 		"\t'p1' type_id=1\n"
335 		"\t'p2' type_id=2",
336 		"[16] VAR 'var1' type_id=1, linkage=global-alloc",
337 		"[17] DATASEC 'datasec1' size=12 vlen=1\n"
338 		"\ttype_id=1 offset=4 size=8",
339 		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
340 		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1");
341 
342 	btf__free(btf);
343 }
344 
345 static void test_btf_add_btf()
346 {
347 	struct btf *btf1 = NULL, *btf2 = NULL;
348 	int id;
349 
350 	btf1 = btf__new_empty();
351 	if (!ASSERT_OK_PTR(btf1, "btf1"))
352 		return;
353 
354 	btf2 = btf__new_empty();
355 	if (!ASSERT_OK_PTR(btf2, "btf2"))
356 		goto cleanup;
357 
358 	gen_btf(btf1);
359 	gen_btf(btf2);
360 
361 	id = btf__add_btf(btf1, btf2);
362 	if (!ASSERT_EQ(id, 20, "id"))
363 		goto cleanup;
364 
365 	VALIDATE_RAW_BTF(
366 		btf1,
367 		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
368 		"[2] PTR '(anon)' type_id=1",
369 		"[3] CONST '(anon)' type_id=5",
370 		"[4] VOLATILE '(anon)' type_id=3",
371 		"[5] RESTRICT '(anon)' type_id=4",
372 		"[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10",
373 		"[7] STRUCT 's1' size=8 vlen=2\n"
374 		"\t'f1' type_id=1 bits_offset=0\n"
375 		"\t'f2' type_id=1 bits_offset=32 bitfield_size=16",
376 		"[8] UNION 'u1' size=8 vlen=1\n"
377 		"\t'f1' type_id=1 bits_offset=0 bitfield_size=16",
378 		"[9] ENUM 'e1' size=4 vlen=2\n"
379 		"\t'v1' val=1\n"
380 		"\t'v2' val=2",
381 		"[10] FWD 'struct_fwd' fwd_kind=struct",
382 		"[11] FWD 'union_fwd' fwd_kind=union",
383 		"[12] ENUM 'enum_fwd' size=4 vlen=0",
384 		"[13] TYPEDEF 'typedef1' type_id=1",
385 		"[14] FUNC 'func1' type_id=15 linkage=global",
386 		"[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
387 		"\t'p1' type_id=1\n"
388 		"\t'p2' type_id=2",
389 		"[16] VAR 'var1' type_id=1, linkage=global-alloc",
390 		"[17] DATASEC 'datasec1' size=12 vlen=1\n"
391 		"\ttype_id=1 offset=4 size=8",
392 		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
393 		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1",
394 
395 		/* types appended from the second BTF */
396 		"[20] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
397 		"[21] PTR '(anon)' type_id=20",
398 		"[22] CONST '(anon)' type_id=24",
399 		"[23] VOLATILE '(anon)' type_id=22",
400 		"[24] RESTRICT '(anon)' type_id=23",
401 		"[25] ARRAY '(anon)' type_id=21 index_type_id=20 nr_elems=10",
402 		"[26] STRUCT 's1' size=8 vlen=2\n"
403 		"\t'f1' type_id=20 bits_offset=0\n"
404 		"\t'f2' type_id=20 bits_offset=32 bitfield_size=16",
405 		"[27] UNION 'u1' size=8 vlen=1\n"
406 		"\t'f1' type_id=20 bits_offset=0 bitfield_size=16",
407 		"[28] ENUM 'e1' size=4 vlen=2\n"
408 		"\t'v1' val=1\n"
409 		"\t'v2' val=2",
410 		"[29] FWD 'struct_fwd' fwd_kind=struct",
411 		"[30] FWD 'union_fwd' fwd_kind=union",
412 		"[31] ENUM 'enum_fwd' size=4 vlen=0",
413 		"[32] TYPEDEF 'typedef1' type_id=20",
414 		"[33] FUNC 'func1' type_id=34 linkage=global",
415 		"[34] FUNC_PROTO '(anon)' ret_type_id=20 vlen=2\n"
416 		"\t'p1' type_id=20\n"
417 		"\t'p2' type_id=21",
418 		"[35] VAR 'var1' type_id=20, linkage=global-alloc",
419 		"[36] DATASEC 'datasec1' size=12 vlen=1\n"
420 		"\ttype_id=20 offset=4 size=8",
421 		"[37] DECL_TAG 'tag1' type_id=35 component_idx=-1",
422 		"[38] DECL_TAG 'tag2' type_id=33 component_idx=1");
423 
424 cleanup:
425 	btf__free(btf1);
426 	btf__free(btf2);
427 }
428 
429 void test_btf_write()
430 {
431 	if (test__start_subtest("btf_add"))
432 		test_btf_add();
433 	if (test__start_subtest("btf_add_btf"))
434 		test_btf_add_btf();
435 }
436