Skip to content

clang ignores BPF attributes in C++ #1

Open

Description

@maxskokov

Five BPF attributes are restricted to C in clang (LangOpts = [COnly] in clang/include/clang/Basic/Attr.td): preserve_access_index, preserve_static_offset, btf_decl_tag, btf_type_tag and bpf_fastcall. In C++ they are dropped with a -Wignored-attributes warning.

Effects:

  • The #pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record) in vmlinux.h has no effect, so plain field accesses get fixed offsets instead of CO-RE relocations.
  • __kptr, __percpu_kptr, __contains, __arg_ctx, __arg_nonnull and similar tags are missing from BTF, so kptrs in maps, lists, rbtrees and global functions with tagged arguments are rejected by the verifier.
struct __attribute__((preserve_access_index)) task { int pid; int tgid; };
int f(struct task *t) { return t->tgid; }

In C this gives CO-RE <byte_off> struct task::tgid; in C++ it compiles to a plain load at offset 4.

The restriction was added in 2019 (dd16b3fe2559, https://reviews.llvm.org/D70257) after review of the original attribute (https://reviews.llvm.org/D69759), because the behavior with C++ inheritance and templates was not defined or tested.

Workaround: __builtin_preserve_access_index() and BPF_CORE_READ() still produce CO-RE relocations in C++. There is no workaround for the BTF tags.

Next step: discuss lifting the restriction with the clang BPF and attribute maintainers, starting with the declaration attributes.

Activity

  1. added
    area/bpf
    upstream
    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/bpfupstream

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions