naccyde / rpms / iproute

Forked from rpms/iproute 8 months ago
Clone

Blame SOURCES/0059-bpf-implement-btf-handling-and-map-annotation.patch

7e752c
From e8386c4e1fa3b5486487fa4d6c350a0d5e300aaf Mon Sep 17 00:00:00 2001
7e752c
From: Andrea Claudi <aclaudi@redhat.com>
7e752c
Date: Thu, 13 Jun 2019 14:37:56 +0200
7e752c
Subject: [PATCH] bpf: implement btf handling and map annotation
7e752c
7e752c
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1716361
7e752c
Upstream Status: iproute2.git commit f823f36012fb5
7e752c
7e752c
commit f823f36012fb5ab4ddfca6ed4ff56188730f281e
7e752c
Author: Daniel Borkmann <daniel@iogearbox.net>
7e752c
Date:   Wed Jul 18 01:31:22 2018 +0200
7e752c
7e752c
    bpf: implement btf handling and map annotation
7e752c
7e752c
    Implement loading of .BTF section from object file and build up
7e752c
    internal table for retrieving key/value id related to maps in
7e752c
    the BPF program. Latter is done by setting up struct btf_type
7e752c
    table.
7e752c
7e752c
    One of the issues is that there's a disconnect between the data
7e752c
    types used in the map and struct bpf_elf_map, meaning the underlying
7e752c
    types are unknown from the map description. One way to overcome
7e752c
    this is to add a annotation such that the loader will recognize
7e752c
    the relation to both. BPF_ANNOTATE_KV_PAIR(map_foo, struct key,
7e752c
    struct val); has been added to the API that programs can use.
7e752c
7e752c
    The loader will then pick the corresponding key/value type ids and
7e752c
    attach it to the maps for creation. This can later on be dumped via
7e752c
    bpftool for introspection.
7e752c
7e752c
    Example with test_xdp_noinline.o from kernel selftests:
7e752c
7e752c
      [...]
7e752c
7e752c
      struct ctl_value {
7e752c
            union {
7e752c
                    __u64 value;
7e752c
                    __u32 ifindex;
7e752c
                    __u8 mac[6];
7e752c
            };
7e752c
      };
7e752c
7e752c
      struct bpf_map_def __attribute__ ((section("maps"), used)) ctl_array = {
7e752c
            .type           = BPF_MAP_TYPE_ARRAY,
7e752c
            .key_size       = sizeof(__u32),
7e752c
            .value_size     = sizeof(struct ctl_value),
7e752c
            .max_entries    = 16,
7e752c
            .map_flags      = 0,
7e752c
      };
7e752c
      BPF_ANNOTATE_KV_PAIR(ctl_array, __u32, struct ctl_value);
7e752c
7e752c
      [...]
7e752c
7e752c
    Above could also further be wrapped in a macro. Compiling through LLVM and
7e752c
    converting to BTF:
7e752c
7e752c
      # llc --version
7e752c
      LLVM (http://llvm.org/):
7e752c
        LLVM version 7.0.0svn
7e752c
        Optimized build.
7e752c
        Default target: x86_64-unknown-linux-gnu
7e752c
        Host CPU: skylake
7e752c
7e752c
        Registered Targets:
7e752c
          bpf    - BPF (host endian)
7e752c
          bpfeb  - BPF (big endian)
7e752c
          bpfel  - BPF (little endian)
7e752c
      [...]
7e752c
7e752c
      # clang [...] -O2 -target bpf -g -emit-llvm -c test_xdp_noinline.c -o - |
7e752c
        llc -march=bpf -mcpu=probe -mattr=dwarfris -filetype=obj -o test_xdp_noinline.o
7e752c
      # pahole -J test_xdp_noinline.o
7e752c
7e752c
    Checking pahole dump of BPF object file:
7e752c
7e752c
      # file test_xdp_noinline.o
7e752c
      test_xdp_noinline.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped
7e752c
      # pahole test_xdp_noinline.o
7e752c
      [...]
7e752c
      struct ctl_value {
7e752c
            union {
7e752c
                    __u64              value;                /*     0     8 */
7e752c
                    __u32              ifindex;              /*     0     4 */
7e752c
                    __u8               mac[0];               /*     0     0 */
7e752c
            };                                               /*     0     8 */
7e752c
7e752c
            /* size: 8, cachelines: 1, members: 1 */
7e752c
            /* last cacheline: 8 bytes */
7e752c
      };
7e752c
7e752c
    Now loading into kernel and dumping the map via bpftool:
7e752c
7e752c
      # ip -force link set dev lo xdp obj test_xdp_noinline.o sec xdp-test
7e752c
      # ip a
7e752c
      1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 xdpgeneric/id:227 qdisc noqueue state UNKNOWN group default qlen 1000
7e752c
          link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
7e752c
          inet 127.0.0.1/8 scope host lo
7e752c
             valid_lft forever preferred_lft forever
7e752c
          inet6 ::1/128 scope host
7e752c
             valid_lft forever preferred_lft forever
7e752c
      [...]
7e752c
      # bpftool prog show id 227
7e752c
      227: xdp  tag a85e060c275c5616  gpl
7e752c
          loaded_at 2018-07-17T14:41:29+0000  uid 0
7e752c
          xlated 8152B  not jited  memlock 12288B  map_ids 381,385,386,382,384,383
7e752c
      # bpftool map dump id 386
7e752c
       [{
7e752c
            "key": 0,
7e752c
            "value": {
7e752c
                "": {
7e752c
                    "value": 0,
7e752c
                    "ifindex": 0,
7e752c
                    "mac": []
7e752c
                }
7e752c
            }
7e752c
        },{
7e752c
            "key": 1,
7e752c
            "value": {
7e752c
                "": {
7e752c
                    "value": 0,
7e752c
                    "ifindex": 0,
7e752c
                    "mac": []
7e752c
                }
7e752c
            }
7e752c
        },{
7e752c
      [...]
7e752c
7e752c
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
7e752c
    Signed-off-by: David Ahern <dsahern@gmail.com>
7e752c
---
7e752c
 include/bpf_elf.h  |   9 ++
7e752c
 include/bpf_util.h |   1 +
7e752c
 lib/bpf.c          | 332 ++++++++++++++++++++++++++++++++++++++++++++-
7e752c
 3 files changed, 338 insertions(+), 4 deletions(-)
7e752c
7e752c
diff --git a/include/bpf_elf.h b/include/bpf_elf.h
7e752c
index a8e360f3bbb28..84e8ae00834c8 100644
7e752c
--- a/include/bpf_elf.h
7e752c
+++ b/include/bpf_elf.h
7e752c
@@ -41,4 +41,13 @@ struct bpf_elf_map {
7e752c
 	__u32 inner_idx;
7e752c
 };
7e752c
 
7e752c
+#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val)		\
7e752c
+	struct ____btf_map_##name {				\
7e752c
+		type_key key;					\
7e752c
+		type_val value;					\
7e752c
+	};							\
7e752c
+	struct ____btf_map_##name				\
7e752c
+	    __attribute__ ((section(".maps." #name), used))	\
7e752c
+	    ____btf_map_##name = { }
7e752c
+
7e752c
 #endif /* __BPF_ELF__ */
7e752c
diff --git a/include/bpf_util.h b/include/bpf_util.h
7e752c
index 219beb40cd253..63837a04e56fe 100644
7e752c
--- a/include/bpf_util.h
7e752c
+++ b/include/bpf_util.h
7e752c
@@ -14,6 +14,7 @@
7e752c
 #define __BPF_UTIL__
7e752c
 
7e752c
 #include <linux/bpf.h>
7e752c
+#include <linux/btf.h>
7e752c
 #include <linux/filter.h>
7e752c
 #include <linux/magic.h>
7e752c
 #include <linux/elf-em.h>
7e752c
diff --git a/lib/bpf.c b/lib/bpf.c
7e752c
index 1b87490555050..d093d0bd86eae 100644
7e752c
--- a/lib/bpf.c
7e752c
+++ b/lib/bpf.c
7e752c
@@ -393,6 +393,8 @@ struct bpf_prog_data {
7e752c
 
7e752c
 struct bpf_map_ext {
7e752c
 	struct bpf_prog_data owner;
7e752c
+	unsigned int btf_id_key;
7e752c
+	unsigned int btf_id_val;
7e752c
 };
7e752c
 
7e752c
 static int bpf_derive_elf_map_from_fdinfo(int fd, struct bpf_elf_map *map,
7e752c
@@ -1125,24 +1127,36 @@ struct bpf_config {
7e752c
 	unsigned int		jit_enabled;
7e752c
 };
7e752c
 
7e752c
+struct bpf_btf {
7e752c
+	const struct btf_header	*hdr;
7e752c
+	const void		*raw;
7e752c
+	const char		*strings;
7e752c
+	const struct btf_type	**types;
7e752c
+	int			types_num;
7e752c
+};
7e752c
+
7e752c
 struct bpf_elf_ctx {
7e752c
 	struct bpf_config	cfg;
7e752c
 	Elf			*elf_fd;
7e752c
 	GElf_Ehdr		elf_hdr;
7e752c
 	Elf_Data		*sym_tab;
7e752c
 	Elf_Data		*str_tab;
7e752c
+	Elf_Data		*btf_data;
7e752c
 	char			obj_uid[64];
7e752c
 	int			obj_fd;
7e752c
+	int			btf_fd;
7e752c
 	int			map_fds[ELF_MAX_MAPS];
7e752c
 	struct bpf_elf_map	maps[ELF_MAX_MAPS];
7e752c
 	struct bpf_map_ext	maps_ext[ELF_MAX_MAPS];
7e752c
 	struct bpf_elf_prog	prog_text;
7e752c
+	struct bpf_btf		btf;
7e752c
 	int			sym_num;
7e752c
 	int			map_num;
7e752c
 	int			map_len;
7e752c
 	bool			*sec_done;
7e752c
 	int			sec_maps;
7e752c
 	int			sec_text;
7e752c
+	int			sec_btf;
7e752c
 	char			license[ELF_MAX_LICENSE_LEN];
7e752c
 	enum bpf_prog_type	type;
7e752c
 	__u32			ifindex;
7e752c
@@ -1167,6 +1181,11 @@ struct bpf_map_data {
7e752c
 	struct bpf_elf_map	*ent;
7e752c
 };
7e752c
 
7e752c
+static bool bpf_log_has_data(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	return ctx->log && ctx->log[0];
7e752c
+}
7e752c
+
7e752c
 static __check_format_string(2, 3) void
7e752c
 bpf_dump_error(struct bpf_elf_ctx *ctx, const char *format, ...)
7e752c
 {
7e752c
@@ -1176,7 +1195,7 @@ bpf_dump_error(struct bpf_elf_ctx *ctx, const char *format, ...)
7e752c
 	vfprintf(stderr, format, vl);
7e752c
 	va_end(vl);
7e752c
 
7e752c
-	if (ctx->log && ctx->log[0]) {
7e752c
+	if (bpf_log_has_data(ctx)) {
7e752c
 		if (ctx->verbose) {
7e752c
 			fprintf(stderr, "%s\n", ctx->log);
7e752c
 		} else {
7e752c
@@ -1223,7 +1242,9 @@ static int bpf_log_realloc(struct bpf_elf_ctx *ctx)
7e752c
 
7e752c
 static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
7e752c
 			  uint32_t size_value, uint32_t max_elem,
7e752c
-			  uint32_t flags, int inner_fd, uint32_t ifindex)
7e752c
+			  uint32_t flags, int inner_fd, int btf_fd,
7e752c
+			  uint32_t ifindex, uint32_t btf_id_key,
7e752c
+			  uint32_t btf_id_val)
7e752c
 {
7e752c
 	union bpf_attr attr = {};
7e752c
 
7e752c
@@ -1234,10 +1255,30 @@ static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
7e752c
 	attr.map_flags = flags;
7e752c
 	attr.inner_map_fd = inner_fd;
7e752c
 	attr.map_ifindex = ifindex;
7e752c
+	attr.btf_fd = btf_fd;
7e752c
+	attr.btf_key_type_id = btf_id_key;
7e752c
+	attr.btf_value_type_id = btf_id_val;
7e752c
 
7e752c
 	return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
7e752c
 }
7e752c
 
7e752c
+static int bpf_btf_load(void *btf, size_t size_btf,
7e752c
+			char *log, size_t size_log)
7e752c
+{
7e752c
+	union bpf_attr attr = {};
7e752c
+
7e752c
+	attr.btf = bpf_ptr_to_u64(btf);
7e752c
+	attr.btf_size = size_btf;
7e752c
+
7e752c
+	if (size_log > 0) {
7e752c
+		attr.btf_log_buf = bpf_ptr_to_u64(log);
7e752c
+		attr.btf_log_size = size_log;
7e752c
+		attr.btf_log_level = 1;
7e752c
+	}
7e752c
+
7e752c
+	return bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
7e752c
+}
7e752c
+
7e752c
 static int bpf_obj_pin(int fd, const char *pathname)
7e752c
 {
7e752c
 	union bpf_attr attr = {};
7e752c
@@ -1613,7 +1654,8 @@ static int bpf_map_attach(const char *name, struct bpf_elf_ctx *ctx,
7e752c
 	ifindex = bpf_map_offload_neutral(map->type) ? 0 : ctx->ifindex;
7e752c
 	errno = 0;
7e752c
 	fd = bpf_map_create(map->type, map->size_key, map->size_value,
7e752c
-			    map->max_elem, map->flags, map_inner_fd, ifindex);
7e752c
+			    map->max_elem, map->flags, map_inner_fd, ctx->btf_fd,
7e752c
+			    ifindex, ext->btf_id_key, ext->btf_id_val);
7e752c
 
7e752c
 	if (fd < 0 || ctx->verbose) {
7e752c
 		bpf_map_report(fd, name, map, ctx, map_inner_fd);
7e752c
@@ -1638,8 +1680,80 @@ static const char *bpf_str_tab_name(const struct bpf_elf_ctx *ctx,
7e752c
 	return ctx->str_tab->d_buf + sym->st_name;
7e752c
 }
7e752c
 
7e752c
+static int bpf_btf_find(struct bpf_elf_ctx *ctx, const char *name)
7e752c
+{
7e752c
+	const struct btf_type *type;
7e752c
+	const char *res;
7e752c
+	int id;
7e752c
+
7e752c
+	for (id = 1; id < ctx->btf.types_num; id++) {
7e752c
+		type = ctx->btf.types[id];
7e752c
+		if (type->name_off >= ctx->btf.hdr->str_len)
7e752c
+			continue;
7e752c
+		res = &ctx->btf.strings[type->name_off];
7e752c
+		if (!strcmp(res, name))
7e752c
+			return id;
7e752c
+	}
7e752c
+
7e752c
+	return -ENOENT;
7e752c
+}
7e752c
+
7e752c
+static int bpf_btf_find_kv(struct bpf_elf_ctx *ctx, const struct bpf_elf_map *map,
7e752c
+			   const char *name, uint32_t *id_key, uint32_t *id_val)
7e752c
+{
7e752c
+	const struct btf_member *key, *val;
7e752c
+	const struct btf_type *type;
7e752c
+	char btf_name[512];
7e752c
+	const char *res;
7e752c
+	int id;
7e752c
+
7e752c
+	snprintf(btf_name, sizeof(btf_name), "____btf_map_%s", name);
7e752c
+	id = bpf_btf_find(ctx, btf_name);
7e752c
+	if (id < 0)
7e752c
+		return id;
7e752c
+
7e752c
+	type = ctx->btf.types[id];
7e752c
+	if (BTF_INFO_KIND(type->info) != BTF_KIND_STRUCT)
7e752c
+		return -EINVAL;
7e752c
+	if (BTF_INFO_VLEN(type->info) != 2)
7e752c
+		return -EINVAL;
7e752c
+
7e752c
+	key = ((void *) type) + sizeof(*type);
7e752c
+	val = key + 1;
7e752c
+	if (!key->type || key->type >= ctx->btf.types_num ||
7e752c
+	    !val->type || val->type >= ctx->btf.types_num)
7e752c
+		return -EINVAL;
7e752c
+
7e752c
+	if (key->name_off >= ctx->btf.hdr->str_len ||
7e752c
+	    val->name_off >= ctx->btf.hdr->str_len)
7e752c
+		return -EINVAL;
7e752c
+
7e752c
+	res = &ctx->btf.strings[key->name_off];
7e752c
+	if (strcmp(res, "key"))
7e752c
+		return -EINVAL;
7e752c
+
7e752c
+	res = &ctx->btf.strings[val->name_off];
7e752c
+	if (strcmp(res, "value"))
7e752c
+		return -EINVAL;
7e752c
+
7e752c
+	*id_key = key->type;
7e752c
+	*id_val = val->type;
7e752c
+	return 0;
7e752c
+}
7e752c
+
7e752c
+static void bpf_btf_annotate(struct bpf_elf_ctx *ctx, int which, const char *name)
7e752c
+{
7e752c
+	uint32_t id_key = 0, id_val = 0;
7e752c
+
7e752c
+	if (!bpf_btf_find_kv(ctx, &ctx->maps[which], name, &id_key, &id_val)) {
7e752c
+		ctx->maps_ext[which].btf_id_key = id_key;
7e752c
+		ctx->maps_ext[which].btf_id_val = id_val;
7e752c
+	}
7e752c
+}
7e752c
+
7e752c
 static const char *bpf_map_fetch_name(struct bpf_elf_ctx *ctx, int which)
7e752c
 {
7e752c
+	const char *name;
7e752c
 	GElf_Sym sym;
7e752c
 	int i;
7e752c
 
7e752c
@@ -1653,7 +1767,9 @@ static const char *bpf_map_fetch_name(struct bpf_elf_ctx *ctx, int which)
7e752c
 		    sym.st_value / ctx->map_len != which)
7e752c
 			continue;
7e752c
 
7e752c
-		return bpf_str_tab_name(ctx, &sym);
7e752c
+		name = bpf_str_tab_name(ctx, &sym);
7e752c
+		bpf_btf_annotate(ctx, which, name);
7e752c
+		return name;
7e752c
 	}
7e752c
 
7e752c
 	return NULL;
7e752c
@@ -1915,11 +2031,210 @@ static int bpf_fetch_text(struct bpf_elf_ctx *ctx, int section,
7e752c
 	return 0;
7e752c
 }
7e752c
 
7e752c
+static void bpf_btf_report(int fd, struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	fprintf(stderr, "\nBTF debug data section \'.BTF\' %s%s (%d)!\n",
7e752c
+		fd < 0 ? "rejected: " : "loaded",
7e752c
+		fd < 0 ? strerror(errno) : "",
7e752c
+		fd < 0 ? errno : fd);
7e752c
+
7e752c
+	fprintf(stderr, " - Length:       %zu\n", ctx->btf_data->d_size);
7e752c
+
7e752c
+	bpf_dump_error(ctx, "Verifier analysis:\n\n");
7e752c
+}
7e752c
+
7e752c
+static int bpf_btf_attach(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	int tries = 0, fd;
7e752c
+retry:
7e752c
+	errno = 0;
7e752c
+	fd = bpf_btf_load(ctx->btf_data->d_buf, ctx->btf_data->d_size,
7e752c
+			  ctx->log, ctx->log_size);
7e752c
+	if (fd < 0 || ctx->verbose) {
7e752c
+		if (fd < 0 && (errno == ENOSPC || !ctx->log_size)) {
7e752c
+			if (tries++ < 10 && !bpf_log_realloc(ctx))
7e752c
+				goto retry;
7e752c
+
7e752c
+			fprintf(stderr, "Log buffer too small to dump verifier log %zu bytes (%d tries)!\n",
7e752c
+				ctx->log_size, tries);
7e752c
+			return fd;
7e752c
+		}
7e752c
+
7e752c
+		if (bpf_log_has_data(ctx))
7e752c
+			bpf_btf_report(fd, ctx);
7e752c
+	}
7e752c
+
7e752c
+	return fd;
7e752c
+}
7e752c
+
7e752c
+static int bpf_fetch_btf_begin(struct bpf_elf_ctx *ctx, int section,
7e752c
+			       struct bpf_elf_sec_data *data)
7e752c
+{
7e752c
+	ctx->btf_data = data->sec_data;
7e752c
+	ctx->sec_btf = section;
7e752c
+	ctx->sec_done[section] = true;
7e752c
+	return 0;
7e752c
+}
7e752c
+
7e752c
+static int bpf_btf_check_header(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	const struct btf_header *hdr = ctx->btf_data->d_buf;
7e752c
+	const char *str_start, *str_end;
7e752c
+	unsigned int data_len;
7e752c
+
7e752c
+	if (hdr->magic != BTF_MAGIC) {
7e752c
+		fprintf(stderr, "Object has wrong BTF magic: %x, expected: %x!\n",
7e752c
+			hdr->magic, BTF_MAGIC);
7e752c
+		return -EINVAL;
7e752c
+	}
7e752c
+
7e752c
+	if (hdr->version != BTF_VERSION) {
7e752c
+		fprintf(stderr, "Object has wrong BTF version: %u, expected: %u!\n",
7e752c
+			hdr->version, BTF_VERSION);
7e752c
+		return -EINVAL;
7e752c
+	}
7e752c
+
7e752c
+	if (hdr->flags) {
7e752c
+		fprintf(stderr, "Object has unsupported BTF flags %x!\n",
7e752c
+			hdr->flags);
7e752c
+		return -EINVAL;
7e752c
+	}
7e752c
+
7e752c
+	data_len = ctx->btf_data->d_size - sizeof(*hdr);
7e752c
+	if (data_len < hdr->type_off ||
7e752c
+	    data_len < hdr->str_off ||
7e752c
+	    data_len < hdr->type_len + hdr->str_len ||
7e752c
+	    hdr->type_off >= hdr->str_off ||
7e752c
+	    hdr->type_off + hdr->type_len != hdr->str_off ||
7e752c
+	    hdr->str_off + hdr->str_len != data_len ||
7e752c
+	    (hdr->type_off & (sizeof(uint32_t) - 1))) {
7e752c
+		fprintf(stderr, "Object has malformed BTF data!\n");
7e752c
+		return -EINVAL;
7e752c
+	}
7e752c
+
7e752c
+	ctx->btf.hdr = hdr;
7e752c
+	ctx->btf.raw = hdr + 1;
7e752c
+
7e752c
+	str_start = ctx->btf.raw + hdr->str_off;
7e752c
+	str_end = str_start + hdr->str_len;
7e752c
+	if (!hdr->str_len ||
7e752c
+	    hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
7e752c
+	    str_start[0] || str_end[-1]) {
7e752c
+		fprintf(stderr, "Object has malformed BTF string data!\n");
7e752c
+		return -EINVAL;
7e752c
+	}
7e752c
+
7e752c
+	ctx->btf.strings = str_start;
7e752c
+	return 0;
7e752c
+}
7e752c
+
7e752c
+static int bpf_btf_register_type(struct bpf_elf_ctx *ctx,
7e752c
+				 const struct btf_type *type)
7e752c
+{
7e752c
+	int cur = ctx->btf.types_num, num = cur + 1;
7e752c
+	const struct btf_type **types;
7e752c
+
7e752c
+	types = realloc(ctx->btf.types, num * sizeof(type));
7e752c
+	if (!types) {
7e752c
+		free(ctx->btf.types);
7e752c
+		ctx->btf.types = NULL;
7e752c
+		ctx->btf.types_num = 0;
7e752c
+		return -ENOMEM;
7e752c
+	}
7e752c
+
7e752c
+	ctx->btf.types = types;
7e752c
+	ctx->btf.types[cur] = type;
7e752c
+	ctx->btf.types_num = num;
7e752c
+	return 0;
7e752c
+}
7e752c
+
7e752c
+static struct btf_type btf_type_void;
7e752c
+
7e752c
+static int bpf_btf_prep_type_data(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	const void *type_cur = ctx->btf.raw + ctx->btf.hdr->type_off;
7e752c
+	const void *type_end = ctx->btf.raw + ctx->btf.hdr->str_off;
7e752c
+	const struct btf_type *type;
7e752c
+	uint16_t var_len;
7e752c
+	int ret, kind;
7e752c
+
7e752c
+	ret = bpf_btf_register_type(ctx, &btf_type_void);
7e752c
+	if (ret < 0)
7e752c
+		return ret;
7e752c
+
7e752c
+	while (type_cur < type_end) {
7e752c
+		type = type_cur;
7e752c
+		type_cur += sizeof(*type);
7e752c
+
7e752c
+		var_len = BTF_INFO_VLEN(type->info);
7e752c
+		kind = BTF_INFO_KIND(type->info);
7e752c
+
7e752c
+		switch (kind) {
7e752c
+		case BTF_KIND_INT:
7e752c
+			type_cur += sizeof(int);
7e752c
+			break;
7e752c
+		case BTF_KIND_ARRAY:
7e752c
+			type_cur += sizeof(struct btf_array);
7e752c
+			break;
7e752c
+		case BTF_KIND_STRUCT:
7e752c
+		case BTF_KIND_UNION:
7e752c
+			type_cur += var_len * sizeof(struct btf_member);
7e752c
+			break;
7e752c
+		case BTF_KIND_ENUM:
7e752c
+			type_cur += var_len * sizeof(struct btf_enum);
7e752c
+			break;
7e752c
+		case BTF_KIND_TYPEDEF:
7e752c
+		case BTF_KIND_PTR:
7e752c
+		case BTF_KIND_FWD:
7e752c
+		case BTF_KIND_VOLATILE:
7e752c
+		case BTF_KIND_CONST:
7e752c
+		case BTF_KIND_RESTRICT:
7e752c
+			break;
7e752c
+		default:
7e752c
+			fprintf(stderr, "Object has unknown BTF type: %u!\n", kind);
7e752c
+			return -EINVAL;
7e752c
+		}
7e752c
+
7e752c
+		ret = bpf_btf_register_type(ctx, type);
7e752c
+		if (ret < 0)
7e752c
+			return ret;
7e752c
+	}
7e752c
+
7e752c
+	return 0;
7e752c
+}
7e752c
+
7e752c
+static int bpf_btf_prep_data(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	int ret = bpf_btf_check_header(ctx);
7e752c
+
7e752c
+	if (!ret)
7e752c
+		return bpf_btf_prep_type_data(ctx);
7e752c
+	return ret;
7e752c
+}
7e752c
+
7e752c
+static void bpf_fetch_btf_end(struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	int fd = bpf_btf_attach(ctx);
7e752c
+
7e752c
+	if (fd < 0)
7e752c
+		return;
7e752c
+	ctx->btf_fd = fd;
7e752c
+	if (bpf_btf_prep_data(ctx) < 0) {
7e752c
+		close(ctx->btf_fd);
7e752c
+		ctx->btf_fd = 0;
7e752c
+	}
7e752c
+}
7e752c
+
7e752c
 static bool bpf_has_map_data(const struct bpf_elf_ctx *ctx)
7e752c
 {
7e752c
 	return ctx->sym_tab && ctx->str_tab && ctx->sec_maps;
7e752c
 }
7e752c
 
7e752c
+static bool bpf_has_btf_data(const struct bpf_elf_ctx *ctx)
7e752c
+{
7e752c
+	return ctx->sec_btf;
7e752c
+}
7e752c
+
7e752c
 static bool bpf_has_call_data(const struct bpf_elf_ctx *ctx)
7e752c
 {
7e752c
 	return ctx->sec_text;
7e752c
@@ -1952,6 +2267,9 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx, bool check_text_sec)
7e752c
 		else if (data.sec_hdr.sh_type == SHT_STRTAB &&
7e752c
 			 !strcmp(data.sec_name, ".strtab"))
7e752c
 			ret = bpf_fetch_strtab(ctx, i, &data);
7e752c
+		else if (data.sec_hdr.sh_type == SHT_PROGBITS &&
7e752c
+			 !strcmp(data.sec_name, ".BTF"))
7e752c
+			ret = bpf_fetch_btf_begin(ctx, i, &data);
7e752c
 		if (ret < 0) {
7e752c
 			fprintf(stderr, "Error parsing section %d! Perhaps check with readelf -a?\n",
7e752c
 				i);
7e752c
@@ -1959,6 +2277,8 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx, bool check_text_sec)
7e752c
 		}
7e752c
 	}
7e752c
 
7e752c
+	if (bpf_has_btf_data(ctx))
7e752c
+		bpf_fetch_btf_end(ctx);
7e752c
 	if (bpf_has_map_data(ctx)) {
7e752c
 		ret = bpf_fetch_maps_end(ctx);
7e752c
 		if (ret < 0) {
7e752c
@@ -2596,6 +2916,10 @@ static void bpf_maps_teardown(struct bpf_elf_ctx *ctx)
7e752c
 		if (ctx->map_fds[i])
7e752c
 			close(ctx->map_fds[i]);
7e752c
 	}
7e752c
+
7e752c
+	if (ctx->btf_fd)
7e752c
+		close(ctx->btf_fd);
7e752c
+	free(ctx->btf.types);
7e752c
 }
7e752c
 
7e752c
 static void bpf_elf_ctx_destroy(struct bpf_elf_ctx *ctx, bool failure)
7e752c
-- 
7e752c
2.20.1
7e752c