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