[PATCH dwarves v1 2/3] btf_encoder: factor out btf_encoder__add_bpf_kfunc()

Ihor Solodrai
2025-10-29T19:03:11Z

Emitting BTF for BPF kernel functions requires special
handling. Consolidate this behavior into btf_encoder__add_bpf_kfunc()
function, which uses simplified btf_encoder_add_func()

No functional changes.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
btf_encoder.c | 42 +++++++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index 0416824..b730280 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -774,6 +774,7 @@ static int btf__tag_bpf_arena_arg(struct btf *btf, struct btf_encoder_func_state
return id;
}

+/* Modifies state->ret_type_id and state->parms[i].type_id for flagged kfuncs */
static int btf__add_bpf_arena_type_tags(struct btf *btf, struct btf_encoder_func_state *state)
{
uint32_t flags = state->elf->kfunc_flags;
@@ -883,10 +884,6 @@ static int32_t btf_encoder__add_func_proto_for_state(struct btf_encoder *encoder

assert(state != NULL);

- if (is_kfunc_state(state) && encoder->tag_kfuncs && encoder->encode_attributes)
- if (btf__add_bpf_arena_type_tags(encoder->btf, state) < 0)
- return -1;
-
encoder = state->encoder;
btf = state->encoder->btf;
nr_params = state->nr_parms;
@@ -1370,7 +1367,6 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
const char *value;
char tmp_value[KSYM_NAME_LEN];
uint16_t idx;
- int err;

btf_fnproto_id = btf_encoder__add_func_proto_for_state(encoder, state);
name = func->name;
@@ -1383,15 +1379,6 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
return -1;
}

- if (func->kfunc && encoder->tag_kfuncs && !encoder->skip_encoding_decl_tag) {
- err = btf__tag_kfunc(encoder->btf, func, btf_fn_id);
- if (err < 0)
- return err;
- }
-
- if (state->nr_annots == 0)
- return 0;
-
for (idx = 0; idx < state->nr_annots; idx++) {
struct btf_encoder_func_annot *a = &state->annots[idx];

@@ -1414,6 +1401,28 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
return -1;
}

+ return btf_fn_id;
+}
+
+static int btf_encoder__add_bpf_kfunc(struct btf_encoder *encoder,
+ struct btf_encoder_func_state *state)
+{
+ int btf_fn_id, err;
+
+ if (encoder->tag_kfuncs && encoder->encode_attributes)
+ if (btf__add_bpf_arena_type_tags(encoder->btf, state) < 0)
+ return -1;
+
+ btf_fn_id = btf_encoder__add_func(encoder, state);
+ if (btf_fn_id < 0)
+ return -1;
+
+ if (encoder->tag_kfuncs && !encoder->skip_encoding_decl_tag) {
+ err = btf__tag_kfunc(encoder->btf, state->elf, btf_fn_id);
+ if (err < 0)
+ return -1;
+ }
+
return 0;
}

@@ -1511,7 +1520,10 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e
0, 0);

if (add_to_btf) {
- err = btf_encoder__add_func(state->encoder, state);
+ if (is_kfunc_state(state))
+ err = btf_encoder__add_bpf_kfunc(state->encoder, state);
+ else
+ err = btf_encoder__add_func(state->encoder, state);
if (err < 0)
goto out;
}
--
2.51.1