Skip to content

loader: __ksym support for variables #1354

@dylandreimerink

Description

@dylandreimerink

While trying to replicate some tests for __weak symbols I discovered that we at pressent don't support __ksym variables, only functions.

For example, if you were to attempt to load:

extern const int bpf_prog_active __ksym;

SEC("xdp") int xdp_prog(struct xdp_md *ctx) {
	int *active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, bpf_get_smp_processor_id());
	if (active)
		return 1;
	return 0;
}

It results in the following error: load BTF: data section .ksyms: expected *btf.Func, not *btf.Var: not supported, while loading with bpftool (libbpf) works fine.

What is supposed to happen is that the loader finds the address of the given kernel symbol and relocates it in the assembly.

Initial logic was added in https://lore.kernel.org/bpf/20200619231703.738941-1-andriin@fb.com/, at that point only "typeless" variables were supported so just a memory address essentially.

In a followup patch set BTF support was added https://lore.kernel.org/bpf/20200929235049.2533242-1-haoluo@google.com/, after which structured global variables can be read from the kernel with helpers such as bpf_this_cpu_ptr.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions