1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2019 Facebook */
7 /* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
8 * has to be adjusted by relocations.
10 enum bpf_core_relo_kind {
11 BPF_FIELD_BYTE_OFFSET = 0, /* field byte offset */
12 BPF_FIELD_BYTE_SIZE = 1, /* field size in bytes */
13 BPF_FIELD_EXISTS = 2, /* field existence in target kernel */
14 BPF_FIELD_SIGNED = 3, /* field signedness (0 - unsigned, 1 - signed) */
15 BPF_FIELD_LSHIFT_U64 = 4, /* bitfield-specific left bitshift */
16 BPF_FIELD_RSHIFT_U64 = 5, /* bitfield-specific right bitshift */
17 BPF_TYPE_ID_LOCAL = 6, /* type ID in local BPF object */
18 BPF_TYPE_ID_TARGET = 7, /* type ID in target kernel */
19 BPF_TYPE_EXISTS = 8, /* type existence in target kernel */
20 BPF_TYPE_SIZE = 9, /* type size in bytes */
21 BPF_ENUMVAL_EXISTS = 10, /* enum value existence in target kernel */
22 BPF_ENUMVAL_VALUE = 11, /* enum value integer value */
25 /* The minimum bpf_core_relo checked by the loader
27 * CO-RE relocation captures the following data:
28 * - insn_off - instruction offset (in bytes) within a BPF program that needs
29 * its insn->imm field to be relocated with actual field info;
30 * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
32 * - access_str_off - offset into corresponding .BTF string section. String
33 * interpretation depends on specific relocation kind:
34 * - for field-based relocations, string encodes an accessed field using
35 * a sequence of field and array indices, separated by colon (:). It's
36 * conceptually very close to LLVM's getelementptr ([0]) instruction's
37 * arguments for identifying offset to a field.
38 * - for type-based relocations, strings is expected to be just "0";
39 * - for enum value-based relocations, string contains an index of enum
40 * value within its enum type;
42 * Example to provide a better feel.
51 * struct sample *s = ...;
52 * int x = &s->a; // encoded as "0:0" (a is field #0)
53 * int y = &s->b[5]; // encoded as "0:1:0:5" (anon struct is field #1,
54 * // b is field #0 inside anon struct, accessing elem #5)
55 * int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
57 * type_id for all relocs in this example will capture BTF type id of
60 * Such relocation is emitted when using __builtin_preserve_access_index()
61 * Clang built-in, passing expression that captures field address, e.g.:
63 * bpf_probe_read(&dst, sizeof(dst),
64 * __builtin_preserve_access_index(&src->a.b.c));
66 * In this case Clang will emit field relocation recording necessary data to
67 * be able to find offset of embedded `a.b.c` field within `src` struct.
69 * [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
71 struct bpf_core_relo {
75 enum bpf_core_relo_kind kind;
78 struct bpf_core_cand {
79 const struct btf *btf;
80 const struct btf_type *t;
85 /* dynamically sized list of type IDs and its associated struct btf */
86 struct bpf_core_cand_list {
87 struct bpf_core_cand *cands;
91 int bpf_core_apply_relo_insn(const char *prog_name,
92 struct bpf_insn *insn, int insn_idx,
93 const struct bpf_core_relo *relo, int relo_idx,
94 const struct btf *local_btf,
95 struct bpf_core_cand_list *cands);
96 int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
97 const struct btf *targ_btf, __u32 targ_id);
99 size_t bpf_core_essential_name_len(const char *name);