Skip to content

Commit da92746

Browse files
author
Alexei Starovoitov
committed
Merge branch 'libbpf-field-existence'
Andrii Nakryiko says: ==================== This patch set generalizes libbpf's CO-RE relocation support. In addition to existing field's byte offset relocation, libbpf now supports field existence relocations, which are emitted by Clang when using __builtin_preserve_field_info(<field>, BPF_FIELD_EXISTS). A convenience bpf_core_field_exists() macro is added to bpf_core_read.h BPF-side header, along the bpf_field_info_kind enum containing currently supported types of field information libbpf supports. This list will grow as libbpf gains support for other relo kinds. This patch set upgrades the format of .BTF.ext's relocation record to match latest Clang's format (12 -> 16 bytes). This is not a breaking change, as the previous format hasn't been released yet as part of official Clang version release. v1->v2: - unify bpf_field_info_kind enum and naming changes (Alexei); - added bpf_core_field_exists() to bpf_core_read.h. ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 14f2cf6 + c7566a6 commit da92746

17 files changed

+392
-85
lines changed

tools/lib/bpf/bpf_core_read.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
#ifndef __BPF_CORE_READ_H__
33
#define __BPF_CORE_READ_H__
44

5+
/*
6+
* enum bpf_field_info_kind is passed as a second argument into
7+
* __builtin_preserve_field_info() built-in to get a specific aspect of
8+
* a field, captured as a first argument. __builtin_preserve_field_info(field,
9+
* info_kind) returns __u32 integer and produces BTF field relocation, which
10+
* is understood and processed by libbpf during BPF object loading. See
11+
* selftests/bpf for examples.
12+
*/
13+
enum bpf_field_info_kind {
14+
BPF_FIELD_BYTE_OFFSET = 0, /* field byte offset */
15+
BPF_FIELD_EXISTS = 2, /* field existence in target kernel */
16+
};
17+
18+
/*
19+
* Convenience macro to check that field actually exists in target kernel's.
20+
* Returns:
21+
* 1, if matching field is present in target kernel;
22+
* 0, if no matching field found.
23+
*/
24+
#define bpf_core_field_exists(field) \
25+
__builtin_preserve_field_info(field, BPF_FIELD_EXISTS)
26+
527
/*
628
* bpf_core_read() abstracts away bpf_probe_read() call and captures offset
729
* relocation for source address using __builtin_preserve_access_index()
@@ -12,7 +34,7 @@
1234
* a relocation, which records BTF type ID describing root struct/union and an
1335
* accessor string which describes exact embedded field that was used to take
1436
* an address. See detailed description of this relocation format and
15-
* semantics in comments to struct bpf_offset_reloc in libbpf_internal.h.
37+
* semantics in comments to struct bpf_field_reloc in libbpf_internal.h.
1638
*
1739
* This relocation allows libbpf to adjust BPF instruction to use correct
1840
* actual field offset, based on target kernel BTF type that matches original

tools/lib/bpf/btf.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -888,14 +888,14 @@ static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
888888
return btf_ext_setup_info(btf_ext, &param);
889889
}
890890

891-
static int btf_ext_setup_offset_reloc(struct btf_ext *btf_ext)
891+
static int btf_ext_setup_field_reloc(struct btf_ext *btf_ext)
892892
{
893893
struct btf_ext_sec_setup_param param = {
894-
.off = btf_ext->hdr->offset_reloc_off,
895-
.len = btf_ext->hdr->offset_reloc_len,
896-
.min_rec_size = sizeof(struct bpf_offset_reloc),
897-
.ext_info = &btf_ext->offset_reloc_info,
898-
.desc = "offset_reloc",
894+
.off = btf_ext->hdr->field_reloc_off,
895+
.len = btf_ext->hdr->field_reloc_len,
896+
.min_rec_size = sizeof(struct bpf_field_reloc),
897+
.ext_info = &btf_ext->field_reloc_info,
898+
.desc = "field_reloc",
899899
};
900900

901901
return btf_ext_setup_info(btf_ext, &param);
@@ -975,9 +975,9 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
975975
goto done;
976976

977977
if (btf_ext->hdr->hdr_len <
978-
offsetofend(struct btf_ext_header, offset_reloc_len))
978+
offsetofend(struct btf_ext_header, field_reloc_len))
979979
goto done;
980-
err = btf_ext_setup_offset_reloc(btf_ext);
980+
err = btf_ext_setup_field_reloc(btf_ext);
981981
if (err)
982982
goto done;
983983

tools/lib/bpf/btf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ struct btf_ext_header {
6060
__u32 line_info_len;
6161

6262
/* optional part of .BTF.ext header */
63-
__u32 offset_reloc_off;
64-
__u32 offset_reloc_len;
63+
__u32 field_reloc_off;
64+
__u32 field_reloc_len;
6565
};
6666

6767
LIBBPF_API void btf__free(struct btf *btf);

0 commit comments

Comments
 (0)