linux/tools/testing/selftests/bpf/prog_tests/btf_write.c
<<
>>
Prefs
   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
   7static 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        /* TYPE_TAG */
 302        id = btf__add_type_tag(btf, "tag1", 1);
 303        ASSERT_EQ(id, 20, "tag_id");
 304        t = btf__type_by_id(btf, 20);
 305        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag1", "tag_value");
 306        ASSERT_EQ(btf_kind(t), BTF_KIND_TYPE_TAG, "tag_kind");
 307        ASSERT_EQ(t->type, 1, "tag_type");
 308        ASSERT_STREQ(btf_type_raw_dump(btf, 20),
 309                     "[20] TYPE_TAG 'tag1' type_id=1", "raw_dump");
 310}
 311
 312static void test_btf_add()
 313{
 314        struct btf *btf;
 315
 316        btf = btf__new_empty();
 317        if (!ASSERT_OK_PTR(btf, "new_empty"))
 318                return;
 319
 320        gen_btf(btf);
 321
 322        VALIDATE_RAW_BTF(
 323                btf,
 324                "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
 325                "[2] PTR '(anon)' type_id=1",
 326                "[3] CONST '(anon)' type_id=5",
 327                "[4] VOLATILE '(anon)' type_id=3",
 328                "[5] RESTRICT '(anon)' type_id=4",
 329                "[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10",
 330                "[7] STRUCT 's1' size=8 vlen=2\n"
 331                "\t'f1' type_id=1 bits_offset=0\n"
 332                "\t'f2' type_id=1 bits_offset=32 bitfield_size=16",
 333                "[8] UNION 'u1' size=8 vlen=1\n"
 334                "\t'f1' type_id=1 bits_offset=0 bitfield_size=16",
 335                "[9] ENUM 'e1' size=4 vlen=2\n"
 336                "\t'v1' val=1\n"
 337                "\t'v2' val=2",
 338                "[10] FWD 'struct_fwd' fwd_kind=struct",
 339                "[11] FWD 'union_fwd' fwd_kind=union",
 340                "[12] ENUM 'enum_fwd' size=4 vlen=0",
 341                "[13] TYPEDEF 'typedef1' type_id=1",
 342                "[14] FUNC 'func1' type_id=15 linkage=global",
 343                "[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
 344                "\t'p1' type_id=1\n"
 345                "\t'p2' type_id=2",
 346                "[16] VAR 'var1' type_id=1, linkage=global-alloc",
 347                "[17] DATASEC 'datasec1' size=12 vlen=1\n"
 348                "\ttype_id=1 offset=4 size=8",
 349                "[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
 350                "[19] DECL_TAG 'tag2' type_id=14 component_idx=1",
 351                "[20] TYPE_TAG 'tag1' type_id=1");
 352
 353        btf__free(btf);
 354}
 355
 356static void test_btf_add_btf()
 357{
 358        struct btf *btf1 = NULL, *btf2 = NULL;
 359        int id;
 360
 361        btf1 = btf__new_empty();
 362        if (!ASSERT_OK_PTR(btf1, "btf1"))
 363                return;
 364
 365        btf2 = btf__new_empty();
 366        if (!ASSERT_OK_PTR(btf2, "btf2"))
 367                goto cleanup;
 368
 369        gen_btf(btf1);
 370        gen_btf(btf2);
 371
 372        id = btf__add_btf(btf1, btf2);
 373        if (!ASSERT_EQ(id, 21, "id"))
 374                goto cleanup;
 375
 376        VALIDATE_RAW_BTF(
 377                btf1,
 378                "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
 379                "[2] PTR '(anon)' type_id=1",
 380                "[3] CONST '(anon)' type_id=5",
 381                "[4] VOLATILE '(anon)' type_id=3",
 382                "[5] RESTRICT '(anon)' type_id=4",
 383                "[6] ARRAY '(anon)' type_id=2 index_type_id=1 nr_elems=10",
 384                "[7] STRUCT 's1' size=8 vlen=2\n"
 385                "\t'f1' type_id=1 bits_offset=0\n"
 386                "\t'f2' type_id=1 bits_offset=32 bitfield_size=16",
 387                "[8] UNION 'u1' size=8 vlen=1\n"
 388                "\t'f1' type_id=1 bits_offset=0 bitfield_size=16",
 389                "[9] ENUM 'e1' size=4 vlen=2\n"
 390                "\t'v1' val=1\n"
 391                "\t'v2' val=2",
 392                "[10] FWD 'struct_fwd' fwd_kind=struct",
 393                "[11] FWD 'union_fwd' fwd_kind=union",
 394                "[12] ENUM 'enum_fwd' size=4 vlen=0",
 395                "[13] TYPEDEF 'typedef1' type_id=1",
 396                "[14] FUNC 'func1' type_id=15 linkage=global",
 397                "[15] FUNC_PROTO '(anon)' ret_type_id=1 vlen=2\n"
 398                "\t'p1' type_id=1\n"
 399                "\t'p2' type_id=2",
 400                "[16] VAR 'var1' type_id=1, linkage=global-alloc",
 401                "[17] DATASEC 'datasec1' size=12 vlen=1\n"
 402                "\ttype_id=1 offset=4 size=8",
 403                "[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
 404                "[19] DECL_TAG 'tag2' type_id=14 component_idx=1",
 405                "[20] TYPE_TAG 'tag1' type_id=1",
 406
 407                /* types appended from the second BTF */
 408                "[21] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
 409                "[22] PTR '(anon)' type_id=21",
 410                "[23] CONST '(anon)' type_id=25",
 411                "[24] VOLATILE '(anon)' type_id=23",
 412                "[25] RESTRICT '(anon)' type_id=24",
 413                "[26] ARRAY '(anon)' type_id=22 index_type_id=21 nr_elems=10",
 414                "[27] STRUCT 's1' size=8 vlen=2\n"
 415                "\t'f1' type_id=21 bits_offset=0\n"
 416                "\t'f2' type_id=21 bits_offset=32 bitfield_size=16",
 417                "[28] UNION 'u1' size=8 vlen=1\n"
 418                "\t'f1' type_id=21 bits_offset=0 bitfield_size=16",
 419                "[29] ENUM 'e1' size=4 vlen=2\n"
 420                "\t'v1' val=1\n"
 421                "\t'v2' val=2",
 422                "[30] FWD 'struct_fwd' fwd_kind=struct",
 423                "[31] FWD 'union_fwd' fwd_kind=union",
 424                "[32] ENUM 'enum_fwd' size=4 vlen=0",
 425                "[33] TYPEDEF 'typedef1' type_id=21",
 426                "[34] FUNC 'func1' type_id=35 linkage=global",
 427                "[35] FUNC_PROTO '(anon)' ret_type_id=21 vlen=2\n"
 428                "\t'p1' type_id=21\n"
 429                "\t'p2' type_id=22",
 430                "[36] VAR 'var1' type_id=21, linkage=global-alloc",
 431                "[37] DATASEC 'datasec1' size=12 vlen=1\n"
 432                "\ttype_id=21 offset=4 size=8",
 433                "[38] DECL_TAG 'tag1' type_id=36 component_idx=-1",
 434                "[39] DECL_TAG 'tag2' type_id=34 component_idx=1",
 435                "[40] TYPE_TAG 'tag1' type_id=21");
 436
 437cleanup:
 438        btf__free(btf1);
 439        btf__free(btf2);
 440}
 441
 442void test_btf_write()
 443{
 444        if (test__start_subtest("btf_add"))
 445                test_btf_add();
 446        if (test__start_subtest("btf_add_btf"))
 447                test_btf_add_btf();
 448}
 449