xref: /openbmc/linux/tools/lib/bpf/libbpf_internal.h (revision 50df3be7)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Internal libbpf helpers.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 
9 #ifndef __LIBBPF_LIBBPF_INTERNAL_H
10 #define __LIBBPF_LIBBPF_INTERNAL_H
11 
12 #include "libbpf.h"
13 
14 #define BTF_INFO_ENC(kind, kind_flag, vlen) \
15 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
16 #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
17 #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
18 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
19 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
20 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
21 	BTF_INT_ENC(encoding, bits_offset, bits)
22 #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
23 #define BTF_PARAM_ENC(name, type) (name), (type)
24 #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
25 
26 #ifndef min
27 # define min(x, y) ((x) < (y) ? (x) : (y))
28 #endif
29 #ifndef max
30 # define max(x, y) ((x) < (y) ? (y) : (x))
31 #endif
32 #ifndef offsetofend
33 # define offsetofend(TYPE, FIELD) \
34 	(offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
35 #endif
36 
37 extern void libbpf_print(enum libbpf_print_level level,
38 			 const char *format, ...)
39 	__attribute__((format(printf, 2, 3)));
40 
41 #define __pr(level, fmt, ...)	\
42 do {				\
43 	libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__);	\
44 } while (0)
45 
46 #define pr_warning(fmt, ...)	__pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
47 #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
48 #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
49 
50 int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
51 			 const char *str_sec, size_t str_len);
52 
53 struct btf_ext_info {
54 	/*
55 	 * info points to the individual info section (e.g. func_info and
56 	 * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
57 	 */
58 	void *info;
59 	__u32 rec_size;
60 	__u32 len;
61 };
62 
63 #define for_each_btf_ext_sec(seg, sec)					\
64 	for (sec = (seg)->info;						\
65 	     (void *)sec < (seg)->info + (seg)->len;			\
66 	     sec = (void *)sec + sizeof(struct btf_ext_info_sec) +	\
67 		   (seg)->rec_size * sec->num_info)
68 
69 #define for_each_btf_ext_rec(seg, sec, i, rec)				\
70 	for (i = 0, rec = (void *)&(sec)->data;				\
71 	     i < (sec)->num_info;					\
72 	     i++, rec = (void *)rec + (seg)->rec_size)
73 
74 struct btf_ext {
75 	union {
76 		struct btf_ext_header *hdr;
77 		void *data;
78 	};
79 	struct btf_ext_info func_info;
80 	struct btf_ext_info line_info;
81 	struct btf_ext_info offset_reloc_info;
82 	__u32 data_size;
83 };
84 
85 struct btf_ext_info_sec {
86 	__u32	sec_name_off;
87 	__u32	num_info;
88 	/* Followed by num_info * record_size number of bytes */
89 	__u8	data[0];
90 };
91 
92 /* The minimum bpf_func_info checked by the loader */
93 struct bpf_func_info_min {
94 	__u32   insn_off;
95 	__u32   type_id;
96 };
97 
98 /* The minimum bpf_line_info checked by the loader */
99 struct bpf_line_info_min {
100 	__u32	insn_off;
101 	__u32	file_name_off;
102 	__u32	line_off;
103 	__u32	line_col;
104 };
105 
106 /* The minimum bpf_offset_reloc checked by the loader
107  *
108  * Offset relocation captures the following data:
109  * - insn_off - instruction offset (in bytes) within a BPF program that needs
110  *   its insn->imm field to be relocated with actual offset;
111  * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
112  *   offset;
113  * - access_str_off - offset into corresponding .BTF string section. String
114  *   itself encodes an accessed field using a sequence of field and array
115  *   indicies, separated by colon (:). It's conceptually very close to LLVM's
116  *   getelementptr ([0]) instruction's arguments for identifying offset to
117  *   a field.
118  *
119  * Example to provide a better feel.
120  *
121  *   struct sample {
122  *       int a;
123  *       struct {
124  *           int b[10];
125  *       };
126  *   };
127  *
128  *   struct sample *s = ...;
129  *   int x = &s->a;     // encoded as "0:0" (a is field #0)
130  *   int y = &s->b[5];  // encoded as "0:1:0:5" (anon struct is field #1,
131  *                      // b is field #0 inside anon struct, accessing elem #5)
132  *   int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
133  *
134  * type_id for all relocs in this example  will capture BTF type id of
135  * `struct sample`.
136  *
137  * Such relocation is emitted when using __builtin_preserve_access_index()
138  * Clang built-in, passing expression that captures field address, e.g.:
139  *
140  * bpf_probe_read(&dst, sizeof(dst),
141  *		  __builtin_preserve_access_index(&src->a.b.c));
142  *
143  * In this case Clang will emit offset relocation recording necessary data to
144  * be able to find offset of embedded `a.b.c` field within `src` struct.
145  *
146  *   [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
147  */
148 struct bpf_offset_reloc {
149 	__u32   insn_off;
150 	__u32   type_id;
151 	__u32   access_str_off;
152 };
153 
154 #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
155