I got this error message on ubuntu 19.04 when I try to execute sudo ./mineonlyret
which is the user space program which loads an ebpf program and it is described after. I tried the same configuration on ubuntu 18.04 and it worked without errors. Which can be the cause of this error?
If you need more details let me know.
libbpf: Error loading ELF section .BTF: 0.
mineonlyret_user.c
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include "libbpf.h"
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>
int main(int ac, char **argv)
{
int sock, prog_fd;
struct bpf_object *obj;
char filename[256];
if (bpf_prog_load("mineonlyret_kern.o", BPF_PROG_TYPE_SOCKET_FILTER,
&obj, &prog_fd))
return 1;
/* open up a packet socket */
sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sock < 0){
printf("socket");
return -1;
}
/* attach the filter */
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
sizeof(prog_fd)) == 0);
int i;
char buf[65535];
for (i = 0; i < 5; i++) {
printf("ci\n");
int res = recvfrom(sock, buf, sizeof(buf), 0, NULL, 0);
printf("res=%d\n", res);
sleep(5);
}
return 0;
}
mineonlyret_kern.c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/in.h>
#include "bpf_helpers.h"
#include <stddef.h>
SEC("socket")
int bpf_sk_prog(struct __sk_buff *skb)
{
return 0;
}
char _license[] SEC("license") = "GPL";
I compile the _kern.c with
clang -S -I. -O2 -emit-llvm -c mineonlyret_kern.c -o - | llc -march=bpf -filetype=obj -o mineonlyret_kern.o
and the _user.c with
gcc -o mineonlyret mineonlyret_user.c -I../libbpf/src/ ../libbpf/src/libbpf.a -lelf
Also here I have another doubt: why If I use the shared version of the library it doesn't work? I mean If I execute
gcc -o mineonlyret mineonlyret_user.c -I../libbpf/src/ -L../libbpf/src/ -lbpf
Directory tree
.
├── libbpf
│ ├── include
│ ├── src
│ │ ├── libbpf.so
│ │ └── libbpf.a
└── libbpfebpf
|── mineonlyret_user.c
|── mineonlyret_kern.c
You need to specify -g
to request Clang to generate .BTF (it also will generate DWARF debug info, but you can ignore or strip it out, it's not used by libbpf). While .BTF initially used to be an optional extra information for better debugging and BPF assembly dumps, it grew into a pretty much required part of modern BPF application (at least those that use libbpf), so make sure you always have '-g' specified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With