xref: /openbmc/linux/tools/lib/bpf/relo_core.h (revision 8e74a48d)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2019 Facebook */
3 
4 #ifndef __RELO_CORE_H
5 #define __RELO_CORE_H
6 
7 #include <linux/bpf.h>
8 
9 struct bpf_core_cand {
10 	const struct btf *btf;
11 	__u32 id;
12 };
13 
14 /* dynamically sized list of type IDs and its associated struct btf */
15 struct bpf_core_cand_list {
16 	struct bpf_core_cand *cands;
17 	int len;
18 };
19 
20 #define BPF_CORE_SPEC_MAX_LEN 64
21 
22 /* represents BPF CO-RE field or array element accessor */
23 struct bpf_core_accessor {
24 	__u32 type_id;		/* struct/union type or array element type */
25 	__u32 idx;		/* field index or array index */
26 	const char *name;	/* field name or NULL for array accessor */
27 };
28 
29 struct bpf_core_spec {
30 	const struct btf *btf;
31 	/* high-level spec: named fields and array indices only */
32 	struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
33 	/* original unresolved (no skip_mods_or_typedefs) root type ID */
34 	__u32 root_type_id;
35 	/* CO-RE relocation kind */
36 	enum bpf_core_relo_kind relo_kind;
37 	/* high-level spec length */
38 	int len;
39 	/* raw, low-level spec: 1-to-1 with accessor spec string */
40 	int raw_spec[BPF_CORE_SPEC_MAX_LEN];
41 	/* raw spec length */
42 	int raw_len;
43 	/* field bit offset represented by spec */
44 	__u32 bit_offset;
45 };
46 
47 int bpf_core_apply_relo_insn(const char *prog_name,
48 			     struct bpf_insn *insn, int insn_idx,
49 			     const struct bpf_core_relo *relo, int relo_idx,
50 			     const struct btf *local_btf,
51 			     struct bpf_core_cand_list *cands,
52 			     struct bpf_core_spec *specs_scratch);
53 int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
54 			      const struct btf *targ_btf, __u32 targ_id);
55 
56 size_t bpf_core_essential_name_len(const char *name);
57 #endif
58