Skip to content

Commit

Permalink
libbpf: Add btf__new_empty() to create an empty BTF object
Browse files Browse the repository at this point in the history
Add an ability to create an empty BTF object from scratch. This is going to be
used by pahole for BTF encoding. And also by selftest for convenient creation
of BTF objects.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200926011357.2366158-7-andriin@fb.com
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Sep 29, 2020
1 parent 919d2b1 commit a871b04
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tools/lib/bpf/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,36 @@ void btf__free(struct btf *btf)
free(btf);
}

struct btf *btf__new_empty(void)
{
struct btf *btf;

btf = calloc(1, sizeof(*btf));
if (!btf)
return ERR_PTR(-ENOMEM);
btf->fd = -1;
btf->ptr_sz = sizeof(void *);

/* +1 for empty string at offset 0 */
btf->raw_size = sizeof(struct btf_header) + 1;
btf->raw_data = calloc(1, btf->raw_size);
if (!btf->raw_data) {
free(btf);
return ERR_PTR(-ENOMEM);
}

btf->hdr = btf->raw_data;
btf->hdr->hdr_len = sizeof(struct btf_header);
btf->hdr->magic = BTF_MAGIC;
btf->hdr->version = BTF_VERSION;

btf->types_data = btf->raw_data + btf->hdr->hdr_len;
btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
btf->hdr->str_len = 1; /* empty string at offset 0 */

return btf;
}

struct btf *btf__new(const void *data, __u32 size)
{
struct btf *btf;
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/btf.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct bpf_object;

LIBBPF_API void btf__free(struct btf *btf);
LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
LIBBPF_API struct btf *btf__new_empty(void);
LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_raw(const char *path);
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ LIBBPF_0.2.0 {
bpf_program__section_name;
btf__add_str;
btf__find_str;
btf__new_empty;
perf_buffer__buffer_cnt;
perf_buffer__buffer_fd;
perf_buffer__epoll_fd;
Expand Down

0 comments on commit a871b04

Please sign in to comment.