Skip to content

Document extern "C" for BPF programs and kfuncs #3

Open

Description

@maxskokov

Without extern "C", C++ name mangling changes ELF symbol names: a program on_switch becomes _Z9on_switchPy and a kfunc bpf_task_acquire becomes _Z16bpf_task_acquireP11task_struct. libbpf then names the program after the mangled symbol and cannot resolve the kfunc in the kernel.

BPF programs, kfunc and ksym declarations must be declared extern "C":

extern "C" struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;

SEC("tp_btf/sched_switch")
extern "C" int on_switch(unsigned long long *ctx) { ... }

The documentation and project template should cover this, together with the build flags -std=gnu++20 -fno-exceptions -fno-asynchronous-unwind-tables.

Activity

  1. added
    kind/docs
    area/docs
    good-first-issue
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/docsgood-first-issuekind/docs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions