1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2021 Facebook */
3 /* Copyright (c) 2024, Oracle and/or its affiliates. */
9 #define btf_var_secinfos(t) (struct btf_var_secinfo *)btf_type_var_secinfo(t)
13 #include "libbpf_internal.h"
16 int btf_field_iter_init(struct btf_field_iter
*it
, struct btf_type
*t
,
17 enum btf_field_iter_kind iter_kind
)
25 case BTF_FIELD_ITER_IDS
:
26 switch (btf_kind(t
)) {
32 it
->desc
= (struct btf_field_desc
) {};
36 case BTF_KIND_VOLATILE
:
37 case BTF_KIND_RESTRICT
:
39 case BTF_KIND_TYPEDEF
:
42 case BTF_KIND_DECL_TAG
:
43 case BTF_KIND_TYPE_TAG
:
44 it
->desc
= (struct btf_field_desc
) { 1, {offsetof(struct btf_type
, type
)} };
47 it
->desc
= (struct btf_field_desc
) {
48 2, {sizeof(struct btf_type
) + offsetof(struct btf_array
, type
),
49 sizeof(struct btf_type
) + offsetof(struct btf_array
, index_type
)}
54 it
->desc
= (struct btf_field_desc
) {
56 sizeof(struct btf_member
),
57 1, {offsetof(struct btf_member
, type
)}
60 case BTF_KIND_FUNC_PROTO
:
61 it
->desc
= (struct btf_field_desc
) {
62 1, {offsetof(struct btf_type
, type
)},
63 sizeof(struct btf_param
),
64 1, {offsetof(struct btf_param
, type
)}
67 case BTF_KIND_DATASEC
:
68 it
->desc
= (struct btf_field_desc
) {
70 sizeof(struct btf_var_secinfo
),
71 1, {offsetof(struct btf_var_secinfo
, type
)}
78 case BTF_FIELD_ITER_STRS
:
79 switch (btf_kind(t
)) {
81 it
->desc
= (struct btf_field_desc
) {};
88 case BTF_KIND_VOLATILE
:
89 case BTF_KIND_RESTRICT
:
91 case BTF_KIND_TYPEDEF
:
94 case BTF_KIND_DECL_TAG
:
95 case BTF_KIND_TYPE_TAG
:
96 case BTF_KIND_DATASEC
:
97 it
->desc
= (struct btf_field_desc
) {
98 1, {offsetof(struct btf_type
, name_off
)}
102 it
->desc
= (struct btf_field_desc
) {
103 1, {offsetof(struct btf_type
, name_off
)},
104 sizeof(struct btf_enum
),
105 1, {offsetof(struct btf_enum
, name_off
)}
108 case BTF_KIND_ENUM64
:
109 it
->desc
= (struct btf_field_desc
) {
110 1, {offsetof(struct btf_type
, name_off
)},
111 sizeof(struct btf_enum64
),
112 1, {offsetof(struct btf_enum64
, name_off
)}
115 case BTF_KIND_STRUCT
:
117 it
->desc
= (struct btf_field_desc
) {
118 1, {offsetof(struct btf_type
, name_off
)},
119 sizeof(struct btf_member
),
120 1, {offsetof(struct btf_member
, name_off
)}
123 case BTF_KIND_FUNC_PROTO
:
124 it
->desc
= (struct btf_field_desc
) {
125 1, {offsetof(struct btf_type
, name_off
)},
126 sizeof(struct btf_param
),
127 1, {offsetof(struct btf_param
, name_off
)}
139 it
->vlen
= btf_vlen(t
);
145 __u32
*btf_field_iter_next(struct btf_field_iter
*it
)
151 if (it
->off_idx
< it
->desc
.t_off_cnt
)
152 return it
->p
+ it
->desc
.t_offs
[it
->off_idx
++];
153 /* move to per-member iteration */
155 it
->p
+= sizeof(struct btf_type
);
159 /* if type doesn't have members, stop */
160 if (it
->desc
.m_sz
== 0) {
165 if (it
->off_idx
>= it
->desc
.m_off_cnt
) {
166 /* exhausted this member's fields, go to the next member */
168 it
->p
+= it
->desc
.m_sz
;
172 if (it
->m_idx
< it
->vlen
)
173 return it
->p
+ it
->desc
.m_offs
[it
->off_idx
++];