cilium / ebpf

ebpf-go is a pure-Go library to read, modify and load eBPF programs and attach them to various hooks in the Linux kernel.

Home Page:https://ebpf-go.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loader: `__ksym` support for variables

dylandreimerink opened this issue · comments

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.