Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
2 | | /* Copyright (c) 2018 Facebook */ |
3 | | |
4 | | #include <byteswap.h> |
5 | | #include <endian.h> |
6 | | #include <stdio.h> |
7 | | #include <stdlib.h> |
8 | | #include <string.h> |
9 | | #include <fcntl.h> |
10 | | #include <unistd.h> |
11 | | #include <errno.h> |
12 | | #include <sys/utsname.h> |
13 | | #include <sys/param.h> |
14 | | #include <sys/stat.h> |
15 | | #include <linux/kernel.h> |
16 | | #include <linux/err.h> |
17 | | #include <linux/btf.h> |
18 | | #include <gelf.h> |
19 | | #include "btf.h" |
20 | | #include "bpf.h" |
21 | | #include "libbpf.h" |
22 | | #include "libbpf_internal.h" |
23 | | #include "hashmap.h" |
24 | | #include "strset.h" |
25 | | |
26 | 46.7k | #define BTF_MAX_NR_TYPES 0x7fffffffU |
27 | 10.1k | #define BTF_MAX_STR_OFFSET 0x7fffffffU |
28 | | |
29 | | static struct btf_type btf_void; |
30 | | |
31 | | struct btf { |
32 | | /* raw BTF data in native endianness */ |
33 | | void *raw_data; |
34 | | /* raw BTF data in non-native endianness */ |
35 | | void *raw_data_swapped; |
36 | | __u32 raw_size; |
37 | | /* whether target endianness differs from the native one */ |
38 | | bool swapped_endian; |
39 | | |
40 | | /* |
41 | | * When BTF is loaded from an ELF or raw memory it is stored |
42 | | * in a contiguous memory block. The hdr, type_data, and, strs_data |
43 | | * point inside that memory region to their respective parts of BTF |
44 | | * representation: |
45 | | * |
46 | | * +--------------------------------+ |
47 | | * | Header | Types | Strings | |
48 | | * +--------------------------------+ |
49 | | * ^ ^ ^ |
50 | | * | | | |
51 | | * hdr | | |
52 | | * types_data-+ | |
53 | | * strs_data------------+ |
54 | | * |
55 | | * If BTF data is later modified, e.g., due to types added or |
56 | | * removed, BTF deduplication performed, etc, this contiguous |
57 | | * representation is broken up into three independently allocated |
58 | | * memory regions to be able to modify them independently. |
59 | | * raw_data is nulled out at that point, but can be later allocated |
60 | | * and cached again if user calls btf__raw_data(), at which point |
61 | | * raw_data will contain a contiguous copy of header, types, and |
62 | | * strings: |
63 | | * |
64 | | * +----------+ +---------+ +-----------+ |
65 | | * | Header | | Types | | Strings | |
66 | | * +----------+ +---------+ +-----------+ |
67 | | * ^ ^ ^ |
68 | | * | | | |
69 | | * hdr | | |
70 | | * types_data----+ | |
71 | | * strset__data(strs_set)-----+ |
72 | | * |
73 | | * +----------+---------+-----------+ |
74 | | * | Header | Types | Strings | |
75 | | * raw_data----->+----------+---------+-----------+ |
76 | | */ |
77 | | struct btf_header *hdr; |
78 | | |
79 | | void *types_data; |
80 | | size_t types_data_cap; /* used size stored in hdr->type_len */ |
81 | | |
82 | | /* type ID to `struct btf_type *` lookup index |
83 | | * type_offs[0] corresponds to the first non-VOID type: |
84 | | * - for base BTF it's type [1]; |
85 | | * - for split BTF it's the first non-base BTF type. |
86 | | */ |
87 | | __u32 *type_offs; |
88 | | size_t type_offs_cap; |
89 | | /* number of types in this BTF instance: |
90 | | * - doesn't include special [0] void type; |
91 | | * - for split BTF counts number of types added on top of base BTF. |
92 | | */ |
93 | | __u32 nr_types; |
94 | | /* if not NULL, points to the base BTF on top of which the current |
95 | | * split BTF is based |
96 | | */ |
97 | | struct btf *base_btf; |
98 | | /* BTF type ID of the first type in this BTF instance: |
99 | | * - for base BTF it's equal to 1; |
100 | | * - for split BTF it's equal to biggest type ID of base BTF plus 1. |
101 | | */ |
102 | | int start_id; |
103 | | /* logical string offset of this BTF instance: |
104 | | * - for base BTF it's equal to 0; |
105 | | * - for split BTF it's equal to total size of base BTF's string section size. |
106 | | */ |
107 | | int start_str_off; |
108 | | |
109 | | /* only one of strs_data or strs_set can be non-NULL, depending on |
110 | | * whether BTF is in a modifiable state (strs_set is used) or not |
111 | | * (strs_data points inside raw_data) |
112 | | */ |
113 | | void *strs_data; |
114 | | /* a set of unique strings */ |
115 | | struct strset *strs_set; |
116 | | /* whether strings are already deduplicated */ |
117 | | bool strs_deduped; |
118 | | |
119 | | /* whether base_btf should be freed in btf_free for this instance */ |
120 | | bool owns_base; |
121 | | |
122 | | /* BTF object FD, if loaded into kernel */ |
123 | | int fd; |
124 | | |
125 | | /* Pointer size (in bytes) for a target architecture of this BTF */ |
126 | | int ptr_sz; |
127 | | }; |
128 | | |
129 | | static inline __u64 ptr_to_u64(const void *ptr) |
130 | 0 | { |
131 | 0 | return (__u64) (unsigned long) ptr; |
132 | 0 | } |
133 | | |
134 | | /* Ensure given dynamically allocated memory region pointed to by *data* with |
135 | | * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough |
136 | | * memory to accommodate *add_cnt* new elements, assuming *cur_cnt* elements |
137 | | * are already used. At most *max_cnt* elements can be ever allocated. |
138 | | * If necessary, memory is reallocated and all existing data is copied over, |
139 | | * new pointer to the memory region is stored at *data, new memory region |
140 | | * capacity (in number of elements) is stored in *cap. |
141 | | * On success, memory pointer to the beginning of unused memory is returned. |
142 | | * On error, NULL is returned. |
143 | | */ |
144 | | void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz, |
145 | | size_t cur_cnt, size_t max_cnt, size_t add_cnt) |
146 | 48.9k | { |
147 | 48.9k | size_t new_cnt; |
148 | 48.9k | void *new_data; |
149 | | |
150 | 48.9k | if (cur_cnt + add_cnt <= *cap_cnt) |
151 | 40.8k | return *data + cur_cnt * elem_sz; |
152 | | |
153 | | /* requested more than the set limit */ |
154 | 8.11k | if (cur_cnt + add_cnt > max_cnt) |
155 | 0 | return NULL; |
156 | | |
157 | 8.11k | new_cnt = *cap_cnt; |
158 | 8.11k | new_cnt += new_cnt / 4; /* expand by 25% */ |
159 | 8.11k | if (new_cnt < 16) /* but at least 16 elements */ |
160 | 6.11k | new_cnt = 16; |
161 | 8.11k | if (new_cnt > max_cnt) /* but not exceeding a set limit */ |
162 | 0 | new_cnt = max_cnt; |
163 | 8.11k | if (new_cnt < cur_cnt + add_cnt) /* also ensure we have enough memory */ |
164 | 104 | new_cnt = cur_cnt + add_cnt; |
165 | | |
166 | 8.11k | new_data = libbpf_reallocarray(*data, new_cnt, elem_sz); |
167 | 8.11k | if (!new_data) |
168 | 0 | return NULL; |
169 | | |
170 | | /* zero out newly allocated portion of memory */ |
171 | 8.11k | memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz); |
172 | | |
173 | 8.11k | *data = new_data; |
174 | 8.11k | *cap_cnt = new_cnt; |
175 | 8.11k | return new_data + cur_cnt * elem_sz; |
176 | 8.11k | } |
177 | | |
178 | | /* Ensure given dynamically allocated memory region has enough allocated space |
179 | | * to accommodate *need_cnt* elements of size *elem_sz* bytes each |
180 | | */ |
181 | | int libbpf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt) |
182 | 2.92k | { |
183 | 2.92k | void *p; |
184 | | |
185 | 2.92k | if (need_cnt <= *cap_cnt) |
186 | 1.21k | return 0; |
187 | | |
188 | 1.70k | p = libbpf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt); |
189 | 1.70k | if (!p) |
190 | 0 | return -ENOMEM; |
191 | | |
192 | 1.70k | return 0; |
193 | 1.70k | } |
194 | | |
195 | | static void *btf_add_type_offs_mem(struct btf *btf, size_t add_cnt) |
196 | 46.1k | { |
197 | 46.1k | return libbpf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32), |
198 | 46.1k | btf->nr_types, BTF_MAX_NR_TYPES, add_cnt); |
199 | 46.1k | } |
200 | | |
201 | | static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off) |
202 | 46.1k | { |
203 | 46.1k | __u32 *p; |
204 | | |
205 | 46.1k | p = btf_add_type_offs_mem(btf, 1); |
206 | 46.1k | if (!p) |
207 | 0 | return -ENOMEM; |
208 | | |
209 | 46.1k | *p = type_off; |
210 | 46.1k | return 0; |
211 | 46.1k | } |
212 | | |
213 | | static void btf_bswap_hdr(struct btf_header *h) |
214 | 480 | { |
215 | 480 | h->magic = bswap_16(h->magic); |
216 | 480 | h->hdr_len = bswap_32(h->hdr_len); |
217 | 480 | h->type_off = bswap_32(h->type_off); |
218 | 480 | h->type_len = bswap_32(h->type_len); |
219 | 480 | h->str_off = bswap_32(h->str_off); |
220 | 480 | h->str_len = bswap_32(h->str_len); |
221 | 480 | } |
222 | | |
223 | | static int btf_parse_hdr(struct btf *btf) |
224 | 5.06k | { |
225 | 5.06k | struct btf_header *hdr = btf->hdr; |
226 | 5.06k | __u32 meta_left; |
227 | | |
228 | 5.06k | if (btf->raw_size < sizeof(struct btf_header)) { |
229 | 6 | pr_debug("BTF header not found\n"); |
230 | 6 | return -EINVAL; |
231 | 6 | } |
232 | | |
233 | 5.05k | if (hdr->magic == bswap_16(BTF_MAGIC)) { |
234 | 517 | btf->swapped_endian = true; |
235 | 517 | if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) { |
236 | 37 | pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n", |
237 | 37 | bswap_32(hdr->hdr_len)); |
238 | 37 | return -ENOTSUP; |
239 | 37 | } |
240 | 480 | btf_bswap_hdr(hdr); |
241 | 4.54k | } else if (hdr->magic != BTF_MAGIC) { |
242 | 44 | pr_debug("Invalid BTF magic: %x\n", hdr->magic); |
243 | 44 | return -EINVAL; |
244 | 44 | } |
245 | | |
246 | 4.97k | if (btf->raw_size < hdr->hdr_len) { |
247 | 54 | pr_debug("BTF header len %u larger than data size %u\n", |
248 | 54 | hdr->hdr_len, btf->raw_size); |
249 | 54 | return -EINVAL; |
250 | 54 | } |
251 | | |
252 | 4.92k | meta_left = btf->raw_size - hdr->hdr_len; |
253 | 4.92k | if (meta_left < (long long)hdr->str_off + hdr->str_len) { |
254 | 54 | pr_debug("Invalid BTF total size: %u\n", btf->raw_size); |
255 | 54 | return -EINVAL; |
256 | 54 | } |
257 | | |
258 | 4.86k | if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) { |
259 | 40 | pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n", |
260 | 40 | hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len); |
261 | 40 | return -EINVAL; |
262 | 40 | } |
263 | | |
264 | 4.82k | if (hdr->type_off % 4) { |
265 | 2 | pr_debug("BTF type section is not aligned to 4 bytes\n"); |
266 | 2 | return -EINVAL; |
267 | 2 | } |
268 | | |
269 | 4.82k | return 0; |
270 | 4.82k | } |
271 | | |
272 | | static int btf_parse_str_sec(struct btf *btf) |
273 | 4.82k | { |
274 | 4.82k | const struct btf_header *hdr = btf->hdr; |
275 | 4.82k | const char *start = btf->strs_data; |
276 | 4.82k | const char *end = start + btf->hdr->str_len; |
277 | | |
278 | 4.82k | if (btf->base_btf && hdr->str_len == 0) |
279 | 0 | return 0; |
280 | 4.82k | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) { |
281 | 14 | pr_debug("Invalid BTF string section\n"); |
282 | 14 | return -EINVAL; |
283 | 14 | } |
284 | 4.81k | if (!btf->base_btf && start[0]) { |
285 | 12 | pr_debug("Invalid BTF string section\n"); |
286 | 12 | return -EINVAL; |
287 | 12 | } |
288 | 4.80k | return 0; |
289 | 4.81k | } |
290 | | |
291 | | static int btf_type_size(const struct btf_type *t) |
292 | 45.7k | { |
293 | 45.7k | const int base_size = sizeof(struct btf_type); |
294 | 45.7k | __u16 vlen = btf_vlen(t); |
295 | | |
296 | 45.7k | switch (btf_kind(t)) { |
297 | 1.16k | case BTF_KIND_FWD: |
298 | 2.06k | case BTF_KIND_CONST: |
299 | 2.86k | case BTF_KIND_VOLATILE: |
300 | 4.39k | case BTF_KIND_RESTRICT: |
301 | 6.32k | case BTF_KIND_PTR: |
302 | 7.24k | case BTF_KIND_TYPEDEF: |
303 | 9.23k | case BTF_KIND_FUNC: |
304 | 11.5k | case BTF_KIND_FLOAT: |
305 | 12.3k | case BTF_KIND_TYPE_TAG: |
306 | 12.3k | return base_size; |
307 | 7.97k | case BTF_KIND_INT: |
308 | 7.97k | return base_size + sizeof(__u32); |
309 | 1.19k | case BTF_KIND_ENUM: |
310 | 1.19k | return base_size + vlen * sizeof(struct btf_enum); |
311 | 1.85k | case BTF_KIND_ENUM64: |
312 | 1.85k | return base_size + vlen * sizeof(struct btf_enum64); |
313 | 3.62k | case BTF_KIND_ARRAY: |
314 | 3.62k | return base_size + sizeof(struct btf_array); |
315 | 2.02k | case BTF_KIND_STRUCT: |
316 | 2.90k | case BTF_KIND_UNION: |
317 | 2.90k | return base_size + vlen * sizeof(struct btf_member); |
318 | 3.20k | case BTF_KIND_FUNC_PROTO: |
319 | 3.20k | return base_size + vlen * sizeof(struct btf_param); |
320 | 4.78k | case BTF_KIND_VAR: |
321 | 4.78k | return base_size + sizeof(struct btf_var); |
322 | 6.73k | case BTF_KIND_DATASEC: |
323 | 6.73k | return base_size + vlen * sizeof(struct btf_var_secinfo); |
324 | 1.12k | case BTF_KIND_DECL_TAG: |
325 | 1.12k | return base_size + sizeof(struct btf_decl_tag); |
326 | 62 | default: |
327 | 62 | pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t)); |
328 | 62 | return -EINVAL; |
329 | 45.7k | } |
330 | 45.7k | } |
331 | | |
332 | | static void btf_bswap_type_base(struct btf_type *t) |
333 | 11.1k | { |
334 | 11.1k | t->name_off = bswap_32(t->name_off); |
335 | 11.1k | t->info = bswap_32(t->info); |
336 | 11.1k | t->type = bswap_32(t->type); |
337 | 11.1k | } |
338 | | |
339 | | static int btf_bswap_type_rest(struct btf_type *t) |
340 | 11.1k | { |
341 | 11.1k | struct btf_var_secinfo *v; |
342 | 11.1k | struct btf_enum64 *e64; |
343 | 11.1k | struct btf_member *m; |
344 | 11.1k | struct btf_array *a; |
345 | 11.1k | struct btf_param *p; |
346 | 11.1k | struct btf_enum *e; |
347 | 11.1k | __u16 vlen = btf_vlen(t); |
348 | 11.1k | int i; |
349 | | |
350 | 11.1k | switch (btf_kind(t)) { |
351 | 512 | case BTF_KIND_FWD: |
352 | 1.01k | case BTF_KIND_CONST: |
353 | 1.45k | case BTF_KIND_VOLATILE: |
354 | 2.55k | case BTF_KIND_RESTRICT: |
355 | 3.13k | case BTF_KIND_PTR: |
356 | 3.61k | case BTF_KIND_TYPEDEF: |
357 | 4.14k | case BTF_KIND_FUNC: |
358 | 5.13k | case BTF_KIND_FLOAT: |
359 | 5.54k | case BTF_KIND_TYPE_TAG: |
360 | 5.54k | return 0; |
361 | 598 | case BTF_KIND_INT: |
362 | 598 | *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1)); |
363 | 598 | return 0; |
364 | 415 | case BTF_KIND_ENUM: |
365 | 2.81k | for (i = 0, e = btf_enum(t); i < vlen; i++, e++) { |
366 | 2.39k | e->name_off = bswap_32(e->name_off); |
367 | 2.39k | e->val = bswap_32(e->val); |
368 | 2.39k | } |
369 | 415 | return 0; |
370 | 617 | case BTF_KIND_ENUM64: |
371 | 2.88k | for (i = 0, e64 = btf_enum64(t); i < vlen; i++, e64++) { |
372 | 2.26k | e64->name_off = bswap_32(e64->name_off); |
373 | 2.26k | e64->val_lo32 = bswap_32(e64->val_lo32); |
374 | 2.26k | e64->val_hi32 = bswap_32(e64->val_hi32); |
375 | 2.26k | } |
376 | 617 | return 0; |
377 | 587 | case BTF_KIND_ARRAY: |
378 | 587 | a = btf_array(t); |
379 | 587 | a->type = bswap_32(a->type); |
380 | 587 | a->index_type = bswap_32(a->index_type); |
381 | 587 | a->nelems = bswap_32(a->nelems); |
382 | 587 | return 0; |
383 | 437 | case BTF_KIND_STRUCT: |
384 | 846 | case BTF_KIND_UNION: |
385 | 3.21k | for (i = 0, m = btf_members(t); i < vlen; i++, m++) { |
386 | 2.36k | m->name_off = bswap_32(m->name_off); |
387 | 2.36k | m->type = bswap_32(m->type); |
388 | 2.36k | m->offset = bswap_32(m->offset); |
389 | 2.36k | } |
390 | 846 | return 0; |
391 | 606 | case BTF_KIND_FUNC_PROTO: |
392 | 2.95k | for (i = 0, p = btf_params(t); i < vlen; i++, p++) { |
393 | 2.34k | p->name_off = bswap_32(p->name_off); |
394 | 2.34k | p->type = bswap_32(p->type); |
395 | 2.34k | } |
396 | 606 | return 0; |
397 | 835 | case BTF_KIND_VAR: |
398 | 835 | btf_var(t)->linkage = bswap_32(btf_var(t)->linkage); |
399 | 835 | return 0; |
400 | 571 | case BTF_KIND_DATASEC: |
401 | 2.53k | for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) { |
402 | 1.96k | v->type = bswap_32(v->type); |
403 | 1.96k | v->offset = bswap_32(v->offset); |
404 | 1.96k | v->size = bswap_32(v->size); |
405 | 1.96k | } |
406 | 571 | return 0; |
407 | 513 | case BTF_KIND_DECL_TAG: |
408 | 513 | btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx); |
409 | 513 | return 0; |
410 | 0 | default: |
411 | 0 | pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t)); |
412 | 0 | return -EINVAL; |
413 | 11.1k | } |
414 | 11.1k | } |
415 | | |
416 | | static int btf_parse_type_sec(struct btf *btf) |
417 | 4.80k | { |
418 | 4.80k | struct btf_header *hdr = btf->hdr; |
419 | 4.80k | void *next_type = btf->types_data; |
420 | 4.80k | void *end_type = next_type + hdr->type_len; |
421 | 4.80k | int err, type_size; |
422 | | |
423 | 50.4k | while (next_type + sizeof(struct btf_type) <= end_type) { |
424 | 45.7k | if (btf->swapped_endian) |
425 | 11.1k | btf_bswap_type_base(next_type); |
426 | | |
427 | 45.7k | type_size = btf_type_size(next_type); |
428 | 45.7k | if (type_size < 0) |
429 | 62 | return type_size; |
430 | 45.7k | if (next_type + type_size > end_type) { |
431 | 33 | pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types); |
432 | 33 | return -EINVAL; |
433 | 33 | } |
434 | | |
435 | 45.6k | if (btf->swapped_endian && btf_bswap_type_rest(next_type)) |
436 | 0 | return -EINVAL; |
437 | | |
438 | 45.6k | err = btf_add_type_idx_entry(btf, next_type - btf->types_data); |
439 | 45.6k | if (err) |
440 | 0 | return err; |
441 | | |
442 | 45.6k | next_type += type_size; |
443 | 45.6k | btf->nr_types++; |
444 | 45.6k | } |
445 | | |
446 | 4.70k | if (next_type != end_type) { |
447 | 83 | pr_warn("BTF types data is malformed\n"); |
448 | 83 | return -EINVAL; |
449 | 83 | } |
450 | | |
451 | 4.62k | return 0; |
452 | 4.70k | } |
453 | | |
454 | | static int btf_validate_str(const struct btf *btf, __u32 str_off, const char *what, __u32 type_id) |
455 | 45.4k | { |
456 | 45.4k | const char *s; |
457 | | |
458 | 45.4k | s = btf__str_by_offset(btf, str_off); |
459 | 45.4k | if (!s) { |
460 | 210 | pr_warn("btf: type [%u]: invalid %s (string offset %u)\n", type_id, what, str_off); |
461 | 210 | return -EINVAL; |
462 | 210 | } |
463 | | |
464 | 45.2k | return 0; |
465 | 45.4k | } |
466 | | |
467 | | static int btf_validate_id(const struct btf *btf, __u32 id, __u32 ctx_id) |
468 | 32.7k | { |
469 | 32.7k | const struct btf_type *t; |
470 | | |
471 | 32.7k | t = btf__type_by_id(btf, id); |
472 | 32.7k | if (!t) { |
473 | 257 | pr_warn("btf: type [%u]: invalid referenced type ID %u\n", ctx_id, id); |
474 | 257 | return -EINVAL; |
475 | 257 | } |
476 | | |
477 | 32.5k | return 0; |
478 | 32.7k | } |
479 | | |
480 | | static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __u32 id) |
481 | 35.1k | { |
482 | 35.1k | __u32 kind = btf_kind(t); |
483 | 35.1k | int err, i, n; |
484 | | |
485 | 35.1k | err = btf_validate_str(btf, t->name_off, "type name", id); |
486 | 35.1k | if (err) |
487 | 139 | return err; |
488 | | |
489 | 34.9k | switch (kind) { |
490 | 0 | case BTF_KIND_UNKN: |
491 | 7.37k | case BTF_KIND_INT: |
492 | 8.05k | case BTF_KIND_FWD: |
493 | 9.43k | case BTF_KIND_FLOAT: |
494 | 9.43k | break; |
495 | 1.36k | case BTF_KIND_PTR: |
496 | 1.82k | case BTF_KIND_TYPEDEF: |
497 | 2.22k | case BTF_KIND_VOLATILE: |
498 | 2.67k | case BTF_KIND_CONST: |
499 | 3.11k | case BTF_KIND_RESTRICT: |
500 | 7.07k | case BTF_KIND_VAR: |
501 | 7.63k | case BTF_KIND_DECL_TAG: |
502 | 7.91k | case BTF_KIND_TYPE_TAG: |
503 | 7.91k | err = btf_validate_id(btf, t->type, id); |
504 | 7.91k | if (err) |
505 | 65 | return err; |
506 | 7.85k | break; |
507 | 7.85k | case BTF_KIND_ARRAY: { |
508 | 2.97k | const struct btf_array *a = btf_array(t); |
509 | | |
510 | 2.97k | err = btf_validate_id(btf, a->type, id); |
511 | 2.97k | err = err ?: btf_validate_id(btf, a->index_type, id); |
512 | 2.97k | if (err) |
513 | 90 | return err; |
514 | 2.83k | break; |
515 | 2.92k | } |
516 | 2.83k | case BTF_KIND_STRUCT: |
517 | 2.12k | case BTF_KIND_UNION: { |
518 | 2.12k | const struct btf_member *m = btf_members(t); |
519 | | |
520 | 2.12k | n = btf_vlen(t); |
521 | 6.23k | for (i = 0; i < n; i++, m++) { |
522 | 4.17k | err = btf_validate_str(btf, m->name_off, "field name", id); |
523 | 4.17k | err = err ?: btf_validate_id(btf, m->type, id); |
524 | 4.17k | if (err) |
525 | 55 | return err; |
526 | 4.16k | } |
527 | 2.05k | break; |
528 | 2.12k | } |
529 | 2.05k | case BTF_KIND_ENUM: { |
530 | 869 | const struct btf_enum *m = btf_enum(t); |
531 | | |
532 | 869 | n = btf_vlen(t); |
533 | 1.80k | for (i = 0; i < n; i++, m++) { |
534 | 968 | err = btf_validate_str(btf, m->name_off, "enum name", id); |
535 | 968 | if (err) |
536 | 35 | return err; |
537 | 968 | } |
538 | 834 | break; |
539 | 869 | } |
540 | 1.26k | case BTF_KIND_ENUM64: { |
541 | 1.26k | const struct btf_enum64 *m = btf_enum64(t); |
542 | | |
543 | 1.26k | n = btf_vlen(t); |
544 | 4.60k | for (i = 0; i < n; i++, m++) { |
545 | 3.34k | err = btf_validate_str(btf, m->name_off, "enum name", id); |
546 | 3.34k | if (err) |
547 | 14 | return err; |
548 | 3.34k | } |
549 | 1.25k | break; |
550 | 1.26k | } |
551 | 1.44k | case BTF_KIND_FUNC: { |
552 | 1.44k | const struct btf_type *ft; |
553 | | |
554 | 1.44k | err = btf_validate_id(btf, t->type, id); |
555 | 1.44k | if (err) |
556 | 5 | return err; |
557 | 1.44k | ft = btf__type_by_id(btf, t->type); |
558 | 1.44k | if (btf_kind(ft) != BTF_KIND_FUNC_PROTO) { |
559 | 18 | pr_warn("btf: type [%u]: referenced type [%u] is not FUNC_PROTO\n", id, t->type); |
560 | 18 | return -EINVAL; |
561 | 18 | } |
562 | 1.42k | break; |
563 | 1.44k | } |
564 | 2.61k | case BTF_KIND_FUNC_PROTO: { |
565 | 2.61k | const struct btf_param *m = btf_params(t); |
566 | | |
567 | 2.61k | n = btf_vlen(t); |
568 | 4.45k | for (i = 0; i < n; i++, m++) { |
569 | 1.86k | err = btf_validate_str(btf, m->name_off, "param name", id); |
570 | 1.86k | err = err ?: btf_validate_id(btf, m->type, id); |
571 | 1.86k | if (err) |
572 | 19 | return err; |
573 | 1.85k | } |
574 | 2.58k | break; |
575 | 2.61k | } |
576 | 6.32k | case BTF_KIND_DATASEC: { |
577 | 6.32k | const struct btf_var_secinfo *m = btf_var_secinfos(t); |
578 | | |
579 | 6.32k | n = btf_vlen(t); |
580 | 17.7k | for (i = 0; i < n; i++, m++) { |
581 | 11.5k | err = btf_validate_id(btf, m->type, id); |
582 | 11.5k | if (err) |
583 | 45 | return err; |
584 | 11.5k | } |
585 | 6.28k | break; |
586 | 6.32k | } |
587 | 6.28k | default: |
588 | 0 | pr_warn("btf: type [%u]: unrecognized kind %u\n", id, kind); |
589 | 0 | return -EINVAL; |
590 | 34.9k | } |
591 | 34.6k | return 0; |
592 | 34.9k | } |
593 | | |
594 | | /* Validate basic sanity of BTF. It's intentionally less thorough than |
595 | | * kernel's validation and validates only properties of BTF that libbpf relies |
596 | | * on to be correct (e.g., valid type IDs, valid string offsets, etc) |
597 | | */ |
598 | | static int btf_sanity_check(const struct btf *btf) |
599 | 4.62k | { |
600 | 4.62k | const struct btf_type *t; |
601 | 4.62k | __u32 i, n = btf__type_cnt(btf); |
602 | 4.62k | int err; |
603 | | |
604 | 39.2k | for (i = btf->start_id; i < n; i++) { |
605 | 35.1k | t = btf_type_by_id(btf, i); |
606 | 35.1k | err = btf_validate_type(btf, t, i); |
607 | 35.1k | if (err) |
608 | 485 | return err; |
609 | 35.1k | } |
610 | 4.13k | return 0; |
611 | 4.62k | } |
612 | | |
613 | | __u32 btf__type_cnt(const struct btf *btf) |
614 | 22.7k | { |
615 | 22.7k | return btf->start_id + btf->nr_types; |
616 | 22.7k | } |
617 | | |
618 | | const struct btf *btf__base_btf(const struct btf *btf) |
619 | 0 | { |
620 | 0 | return btf->base_btf; |
621 | 0 | } |
622 | | |
623 | | /* internal helper returning non-const pointer to a type */ |
624 | | struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id) |
625 | 246k | { |
626 | 246k | if (type_id == 0) |
627 | 9.70k | return &btf_void; |
628 | 237k | if (type_id < btf->start_id) |
629 | 0 | return btf_type_by_id(btf->base_btf, type_id); |
630 | 237k | return btf->types_data + btf->type_offs[type_id - btf->start_id]; |
631 | 237k | } |
632 | | |
633 | | const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id) |
634 | 187k | { |
635 | 187k | if (type_id >= btf->start_id + btf->nr_types) |
636 | 257 | return errno = EINVAL, NULL; |
637 | 186k | return btf_type_by_id((struct btf *)btf, type_id); |
638 | 187k | } |
639 | | |
640 | | static int determine_ptr_size(const struct btf *btf) |
641 | 0 | { |
642 | 0 | static const char * const long_aliases[] = { |
643 | 0 | "long", |
644 | 0 | "long int", |
645 | 0 | "int long", |
646 | 0 | "unsigned long", |
647 | 0 | "long unsigned", |
648 | 0 | "unsigned long int", |
649 | 0 | "unsigned int long", |
650 | 0 | "long unsigned int", |
651 | 0 | "long int unsigned", |
652 | 0 | "int unsigned long", |
653 | 0 | "int long unsigned", |
654 | 0 | }; |
655 | 0 | const struct btf_type *t; |
656 | 0 | const char *name; |
657 | 0 | int i, j, n; |
658 | |
|
659 | 0 | if (btf->base_btf && btf->base_btf->ptr_sz > 0) |
660 | 0 | return btf->base_btf->ptr_sz; |
661 | | |
662 | 0 | n = btf__type_cnt(btf); |
663 | 0 | for (i = 1; i < n; i++) { |
664 | 0 | t = btf__type_by_id(btf, i); |
665 | 0 | if (!btf_is_int(t)) |
666 | 0 | continue; |
667 | | |
668 | 0 | if (t->size != 4 && t->size != 8) |
669 | 0 | continue; |
670 | | |
671 | 0 | name = btf__name_by_offset(btf, t->name_off); |
672 | 0 | if (!name) |
673 | 0 | continue; |
674 | | |
675 | 0 | for (j = 0; j < ARRAY_SIZE(long_aliases); j++) { |
676 | 0 | if (strcmp(name, long_aliases[j]) == 0) |
677 | 0 | return t->size; |
678 | 0 | } |
679 | 0 | } |
680 | | |
681 | 0 | return -1; |
682 | 0 | } |
683 | | |
684 | | static size_t btf_ptr_sz(const struct btf *btf) |
685 | 1.27k | { |
686 | 1.27k | if (!btf->ptr_sz) |
687 | 0 | ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); |
688 | 1.27k | return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz; |
689 | 1.27k | } |
690 | | |
691 | | /* Return pointer size this BTF instance assumes. The size is heuristically |
692 | | * determined by looking for 'long' or 'unsigned long' integer type and |
693 | | * recording its size in bytes. If BTF type information doesn't have any such |
694 | | * type, this function returns 0. In the latter case, native architecture's |
695 | | * pointer size is assumed, so will be either 4 or 8, depending on |
696 | | * architecture that libbpf was compiled for. It's possible to override |
697 | | * guessed value by using btf__set_pointer_size() API. |
698 | | */ |
699 | | size_t btf__pointer_size(const struct btf *btf) |
700 | 0 | { |
701 | 0 | if (!btf->ptr_sz) |
702 | 0 | ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); |
703 | |
|
704 | 0 | if (btf->ptr_sz < 0) |
705 | | /* not enough BTF type info to guess */ |
706 | 0 | return 0; |
707 | | |
708 | 0 | return btf->ptr_sz; |
709 | 0 | } |
710 | | |
711 | | /* Override or set pointer size in bytes. Only values of 4 and 8 are |
712 | | * supported. |
713 | | */ |
714 | | int btf__set_pointer_size(struct btf *btf, size_t ptr_sz) |
715 | 4.13k | { |
716 | 4.13k | if (ptr_sz != 4 && ptr_sz != 8) |
717 | 0 | return libbpf_err(-EINVAL); |
718 | 4.13k | btf->ptr_sz = ptr_sz; |
719 | 4.13k | return 0; |
720 | 4.13k | } |
721 | | |
722 | | static bool is_host_big_endian(void) |
723 | 0 | { |
724 | 0 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
725 | 0 | return false; |
726 | | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
727 | | return true; |
728 | | #else |
729 | | # error "Unrecognized __BYTE_ORDER__" |
730 | | #endif |
731 | 0 | } |
732 | | |
733 | | enum btf_endianness btf__endianness(const struct btf *btf) |
734 | 0 | { |
735 | 0 | if (is_host_big_endian()) |
736 | 0 | return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; |
737 | 0 | else |
738 | 0 | return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; |
739 | 0 | } |
740 | | |
741 | | int btf__set_endianness(struct btf *btf, enum btf_endianness endian) |
742 | 0 | { |
743 | 0 | if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN) |
744 | 0 | return libbpf_err(-EINVAL); |
745 | | |
746 | 0 | btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); |
747 | 0 | if (!btf->swapped_endian) { |
748 | 0 | free(btf->raw_data_swapped); |
749 | 0 | btf->raw_data_swapped = NULL; |
750 | 0 | } |
751 | 0 | return 0; |
752 | 0 | } |
753 | | |
754 | | static bool btf_type_is_void(const struct btf_type *t) |
755 | 5.31k | { |
756 | 5.31k | return t == &btf_void || btf_is_fwd(t); |
757 | 5.31k | } |
758 | | |
759 | | static bool btf_type_is_void_or_null(const struct btf_type *t) |
760 | 5.31k | { |
761 | 5.31k | return !t || btf_type_is_void(t); |
762 | 5.31k | } |
763 | | |
764 | 10.6k | #define MAX_RESOLVE_DEPTH 32 |
765 | | |
766 | | __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) |
767 | 2.18k | { |
768 | 2.18k | const struct btf_array *array; |
769 | 2.18k | const struct btf_type *t; |
770 | 2.18k | __u32 nelems = 1; |
771 | 2.18k | __s64 size = -1; |
772 | 2.18k | int i; |
773 | | |
774 | 2.18k | t = btf__type_by_id(btf, type_id); |
775 | 4.79k | for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); i++) { |
776 | 4.76k | switch (btf_kind(t)) { |
777 | 889 | case BTF_KIND_INT: |
778 | 1.17k | case BTF_KIND_STRUCT: |
779 | 1.25k | case BTF_KIND_UNION: |
780 | 1.40k | case BTF_KIND_ENUM: |
781 | 1.56k | case BTF_KIND_ENUM64: |
782 | 1.74k | case BTF_KIND_DATASEC: |
783 | 1.97k | case BTF_KIND_FLOAT: |
784 | 1.97k | size = t->size; |
785 | 1.97k | goto done; |
786 | 142 | case BTF_KIND_PTR: |
787 | 142 | size = btf_ptr_sz(btf); |
788 | 142 | goto done; |
789 | 299 | case BTF_KIND_TYPEDEF: |
790 | 542 | case BTF_KIND_VOLATILE: |
791 | 709 | case BTF_KIND_CONST: |
792 | 907 | case BTF_KIND_RESTRICT: |
793 | 1.24k | case BTF_KIND_VAR: |
794 | 1.35k | case BTF_KIND_DECL_TAG: |
795 | 1.57k | case BTF_KIND_TYPE_TAG: |
796 | 1.57k | type_id = t->type; |
797 | 1.57k | break; |
798 | 1.06k | case BTF_KIND_ARRAY: |
799 | 1.06k | array = btf_array(t); |
800 | 1.06k | if (nelems && array->nelems > UINT32_MAX / nelems) |
801 | 41 | return libbpf_err(-E2BIG); |
802 | 1.02k | nelems *= array->nelems; |
803 | 1.02k | type_id = array->type; |
804 | 1.02k | break; |
805 | 2 | default: |
806 | 2 | return libbpf_err(-EINVAL); |
807 | 4.76k | } |
808 | | |
809 | 2.60k | t = btf__type_by_id(btf, type_id); |
810 | 2.60k | } |
811 | | |
812 | 2.14k | done: |
813 | 2.14k | if (size < 0) |
814 | 32 | return libbpf_err(-EINVAL); |
815 | 2.11k | if (nelems && size > UINT32_MAX / nelems) |
816 | 46 | return libbpf_err(-E2BIG); |
817 | | |
818 | 2.06k | return nelems * size; |
819 | 2.11k | } |
820 | | |
821 | | int btf__align_of(const struct btf *btf, __u32 id) |
822 | 2.01k | { |
823 | 2.01k | const struct btf_type *t = btf__type_by_id(btf, id); |
824 | 2.01k | __u16 kind = btf_kind(t); |
825 | | |
826 | 2.01k | switch (kind) { |
827 | 778 | case BTF_KIND_INT: |
828 | 921 | case BTF_KIND_ENUM: |
829 | 1.07k | case BTF_KIND_ENUM64: |
830 | 1.12k | case BTF_KIND_FLOAT: |
831 | 1.12k | return min(btf_ptr_sz(btf), (size_t)t->size); |
832 | 10 | case BTF_KIND_PTR: |
833 | 10 | return btf_ptr_sz(btf); |
834 | 107 | case BTF_KIND_TYPEDEF: |
835 | 264 | case BTF_KIND_VOLATILE: |
836 | 358 | case BTF_KIND_CONST: |
837 | 490 | case BTF_KIND_RESTRICT: |
838 | 628 | case BTF_KIND_TYPE_TAG: |
839 | 628 | return btf__align_of(btf, t->type); |
840 | 113 | case BTF_KIND_ARRAY: |
841 | 113 | return btf__align_of(btf, btf_array(t)->type); |
842 | 36 | case BTF_KIND_STRUCT: |
843 | 92 | case BTF_KIND_UNION: { |
844 | 92 | const struct btf_member *m = btf_members(t); |
845 | 92 | __u16 vlen = btf_vlen(t); |
846 | 92 | int i, max_align = 1, align; |
847 | | |
848 | 193 | for (i = 0; i < vlen; i++, m++) { |
849 | 135 | align = btf__align_of(btf, m->type); |
850 | 135 | if (align <= 0) |
851 | 9 | return libbpf_err(align); |
852 | 126 | max_align = max(max_align, align); |
853 | | |
854 | | /* if field offset isn't aligned according to field |
855 | | * type's alignment, then struct must be packed |
856 | | */ |
857 | 126 | if (btf_member_bitfield_size(t, i) == 0 && |
858 | 126 | (m->offset % (8 * align)) != 0) |
859 | 25 | return 1; |
860 | 126 | } |
861 | | |
862 | | /* if struct/union size isn't a multiple of its alignment, |
863 | | * then struct must be packed |
864 | | */ |
865 | 58 | if ((t->size % max_align) != 0) |
866 | 12 | return 1; |
867 | | |
868 | 46 | return max_align; |
869 | 58 | } |
870 | 49 | default: |
871 | 49 | pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t)); |
872 | 49 | return errno = EINVAL, 0; |
873 | 2.01k | } |
874 | 2.01k | } |
875 | | |
876 | | int btf__resolve_type(const struct btf *btf, __u32 type_id) |
877 | 87 | { |
878 | 87 | const struct btf_type *t; |
879 | 87 | int depth = 0; |
880 | | |
881 | 87 | t = btf__type_by_id(btf, type_id); |
882 | 470 | while (depth < MAX_RESOLVE_DEPTH && |
883 | 470 | !btf_type_is_void_or_null(t) && |
884 | 470 | (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) { |
885 | 383 | type_id = t->type; |
886 | 383 | t = btf__type_by_id(btf, type_id); |
887 | 383 | depth++; |
888 | 383 | } |
889 | | |
890 | 87 | if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t)) |
891 | 8 | return libbpf_err(-EINVAL); |
892 | | |
893 | 79 | return type_id; |
894 | 87 | } |
895 | | |
896 | | __s32 btf__find_by_name(const struct btf *btf, const char *type_name) |
897 | 326 | { |
898 | 326 | __u32 i, nr_types = btf__type_cnt(btf); |
899 | | |
900 | 326 | if (!strcmp(type_name, "void")) |
901 | 0 | return 0; |
902 | | |
903 | 4.30k | for (i = 1; i < nr_types; i++) { |
904 | 4.14k | const struct btf_type *t = btf__type_by_id(btf, i); |
905 | 4.14k | const char *name = btf__name_by_offset(btf, t->name_off); |
906 | | |
907 | 4.14k | if (name && !strcmp(type_name, name)) |
908 | 169 | return i; |
909 | 4.14k | } |
910 | | |
911 | 157 | return libbpf_err(-ENOENT); |
912 | 326 | } |
913 | | |
914 | | static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id, |
915 | | const char *type_name, __u32 kind) |
916 | 4.01k | { |
917 | 4.01k | __u32 i, nr_types = btf__type_cnt(btf); |
918 | | |
919 | 4.01k | if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void")) |
920 | 0 | return 0; |
921 | | |
922 | 38.6k | for (i = start_id; i < nr_types; i++) { |
923 | 35.5k | const struct btf_type *t = btf__type_by_id(btf, i); |
924 | 35.5k | const char *name; |
925 | | |
926 | 35.5k | if (btf_kind(t) != kind) |
927 | 29.1k | continue; |
928 | 6.38k | name = btf__name_by_offset(btf, t->name_off); |
929 | 6.38k | if (name && !strcmp(type_name, name)) |
930 | 838 | return i; |
931 | 6.38k | } |
932 | | |
933 | 3.18k | return libbpf_err(-ENOENT); |
934 | 4.01k | } |
935 | | |
936 | | __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name, |
937 | | __u32 kind) |
938 | 0 | { |
939 | 0 | return btf_find_by_name_kind(btf, btf->start_id, type_name, kind); |
940 | 0 | } |
941 | | |
942 | | __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name, |
943 | | __u32 kind) |
944 | 4.01k | { |
945 | 4.01k | return btf_find_by_name_kind(btf, 1, type_name, kind); |
946 | 4.01k | } |
947 | | |
948 | | static bool btf_is_modifiable(const struct btf *btf) |
949 | 6.11k | { |
950 | 6.11k | return (void *)btf->hdr != btf->raw_data; |
951 | 6.11k | } |
952 | | |
953 | | void btf__free(struct btf *btf) |
954 | 24.3k | { |
955 | 24.3k | if (IS_ERR_OR_NULL(btf)) |
956 | 19.3k | return; |
957 | | |
958 | 5.06k | if (btf->fd >= 0) |
959 | 0 | close(btf->fd); |
960 | | |
961 | 5.06k | if (btf_is_modifiable(btf)) { |
962 | | /* if BTF was modified after loading, it will have a split |
963 | | * in-memory representation for header, types, and strings |
964 | | * sections, so we need to free all of them individually. It |
965 | | * might still have a cached contiguous raw data present, |
966 | | * which will be unconditionally freed below. |
967 | | */ |
968 | 526 | free(btf->hdr); |
969 | 526 | free(btf->types_data); |
970 | 526 | strset__free(btf->strs_set); |
971 | 526 | } |
972 | 5.06k | free(btf->raw_data); |
973 | 5.06k | free(btf->raw_data_swapped); |
974 | 5.06k | free(btf->type_offs); |
975 | 5.06k | if (btf->owns_base) |
976 | 0 | btf__free(btf->base_btf); |
977 | 5.06k | free(btf); |
978 | 5.06k | } |
979 | | |
980 | | static struct btf *btf_new_empty(struct btf *base_btf) |
981 | 0 | { |
982 | 0 | struct btf *btf; |
983 | |
|
984 | 0 | btf = calloc(1, sizeof(*btf)); |
985 | 0 | if (!btf) |
986 | 0 | return ERR_PTR(-ENOMEM); |
987 | | |
988 | 0 | btf->nr_types = 0; |
989 | 0 | btf->start_id = 1; |
990 | 0 | btf->start_str_off = 0; |
991 | 0 | btf->fd = -1; |
992 | 0 | btf->ptr_sz = sizeof(void *); |
993 | 0 | btf->swapped_endian = false; |
994 | |
|
995 | 0 | if (base_btf) { |
996 | 0 | btf->base_btf = base_btf; |
997 | 0 | btf->start_id = btf__type_cnt(base_btf); |
998 | 0 | btf->start_str_off = base_btf->hdr->str_len; |
999 | 0 | btf->swapped_endian = base_btf->swapped_endian; |
1000 | 0 | } |
1001 | | |
1002 | | /* +1 for empty string at offset 0 */ |
1003 | 0 | btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1); |
1004 | 0 | btf->raw_data = calloc(1, btf->raw_size); |
1005 | 0 | if (!btf->raw_data) { |
1006 | 0 | free(btf); |
1007 | 0 | return ERR_PTR(-ENOMEM); |
1008 | 0 | } |
1009 | | |
1010 | 0 | btf->hdr = btf->raw_data; |
1011 | 0 | btf->hdr->hdr_len = sizeof(struct btf_header); |
1012 | 0 | btf->hdr->magic = BTF_MAGIC; |
1013 | 0 | btf->hdr->version = BTF_VERSION; |
1014 | |
|
1015 | 0 | btf->types_data = btf->raw_data + btf->hdr->hdr_len; |
1016 | 0 | btf->strs_data = btf->raw_data + btf->hdr->hdr_len; |
1017 | 0 | btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */ |
1018 | |
|
1019 | 0 | return btf; |
1020 | 0 | } |
1021 | | |
1022 | | struct btf *btf__new_empty(void) |
1023 | 0 | { |
1024 | 0 | return libbpf_ptr(btf_new_empty(NULL)); |
1025 | 0 | } |
1026 | | |
1027 | | struct btf *btf__new_empty_split(struct btf *base_btf) |
1028 | 0 | { |
1029 | 0 | return libbpf_ptr(btf_new_empty(base_btf)); |
1030 | 0 | } |
1031 | | |
1032 | | static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf) |
1033 | 5.06k | { |
1034 | 5.06k | struct btf *btf; |
1035 | 5.06k | int err; |
1036 | | |
1037 | 5.06k | btf = calloc(1, sizeof(struct btf)); |
1038 | 5.06k | if (!btf) |
1039 | 0 | return ERR_PTR(-ENOMEM); |
1040 | | |
1041 | 5.06k | btf->nr_types = 0; |
1042 | 5.06k | btf->start_id = 1; |
1043 | 5.06k | btf->start_str_off = 0; |
1044 | 5.06k | btf->fd = -1; |
1045 | | |
1046 | 5.06k | if (base_btf) { |
1047 | 0 | btf->base_btf = base_btf; |
1048 | 0 | btf->start_id = btf__type_cnt(base_btf); |
1049 | 0 | btf->start_str_off = base_btf->hdr->str_len; |
1050 | 0 | } |
1051 | | |
1052 | 5.06k | btf->raw_data = malloc(size); |
1053 | 5.06k | if (!btf->raw_data) { |
1054 | 0 | err = -ENOMEM; |
1055 | 0 | goto done; |
1056 | 0 | } |
1057 | 5.06k | memcpy(btf->raw_data, data, size); |
1058 | 5.06k | btf->raw_size = size; |
1059 | | |
1060 | 5.06k | btf->hdr = btf->raw_data; |
1061 | 5.06k | err = btf_parse_hdr(btf); |
1062 | 5.06k | if (err) |
1063 | 237 | goto done; |
1064 | | |
1065 | 4.82k | btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off; |
1066 | 4.82k | btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off; |
1067 | | |
1068 | 4.82k | err = btf_parse_str_sec(btf); |
1069 | 4.82k | err = err ?: btf_parse_type_sec(btf); |
1070 | 4.82k | err = err ?: btf_sanity_check(btf); |
1071 | 4.80k | if (err) |
1072 | 689 | goto done; |
1073 | | |
1074 | 5.06k | done: |
1075 | 5.06k | if (err) { |
1076 | 926 | btf__free(btf); |
1077 | 926 | return ERR_PTR(err); |
1078 | 926 | } |
1079 | | |
1080 | 4.13k | return btf; |
1081 | 5.06k | } |
1082 | | |
1083 | | struct btf *btf__new(const void *data, __u32 size) |
1084 | 5.06k | { |
1085 | 5.06k | return libbpf_ptr(btf_new(data, size, NULL)); |
1086 | 5.06k | } |
1087 | | |
1088 | | struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf) |
1089 | 0 | { |
1090 | 0 | return libbpf_ptr(btf_new(data, size, base_btf)); |
1091 | 0 | } |
1092 | | |
1093 | | struct btf_elf_secs { |
1094 | | Elf_Data *btf_data; |
1095 | | Elf_Data *btf_ext_data; |
1096 | | Elf_Data *btf_base_data; |
1097 | | }; |
1098 | | |
1099 | | static int btf_find_elf_sections(Elf *elf, const char *path, struct btf_elf_secs *secs) |
1100 | 0 | { |
1101 | 0 | Elf_Scn *scn = NULL; |
1102 | 0 | Elf_Data *data; |
1103 | 0 | GElf_Ehdr ehdr; |
1104 | 0 | size_t shstrndx; |
1105 | 0 | int idx = 0; |
1106 | |
|
1107 | 0 | if (!gelf_getehdr(elf, &ehdr)) { |
1108 | 0 | pr_warn("failed to get EHDR from %s\n", path); |
1109 | 0 | goto err; |
1110 | 0 | } |
1111 | | |
1112 | 0 | if (elf_getshdrstrndx(elf, &shstrndx)) { |
1113 | 0 | pr_warn("failed to get section names section index for %s\n", |
1114 | 0 | path); |
1115 | 0 | goto err; |
1116 | 0 | } |
1117 | | |
1118 | 0 | if (!elf_rawdata(elf_getscn(elf, shstrndx), NULL)) { |
1119 | 0 | pr_warn("failed to get e_shstrndx from %s\n", path); |
1120 | 0 | goto err; |
1121 | 0 | } |
1122 | | |
1123 | 0 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
1124 | 0 | Elf_Data **field; |
1125 | 0 | GElf_Shdr sh; |
1126 | 0 | char *name; |
1127 | |
|
1128 | 0 | idx++; |
1129 | 0 | if (gelf_getshdr(scn, &sh) != &sh) { |
1130 | 0 | pr_warn("failed to get section(%d) header from %s\n", |
1131 | 0 | idx, path); |
1132 | 0 | goto err; |
1133 | 0 | } |
1134 | 0 | name = elf_strptr(elf, shstrndx, sh.sh_name); |
1135 | 0 | if (!name) { |
1136 | 0 | pr_warn("failed to get section(%d) name from %s\n", |
1137 | 0 | idx, path); |
1138 | 0 | goto err; |
1139 | 0 | } |
1140 | | |
1141 | 0 | if (strcmp(name, BTF_ELF_SEC) == 0) |
1142 | 0 | field = &secs->btf_data; |
1143 | 0 | else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) |
1144 | 0 | field = &secs->btf_ext_data; |
1145 | 0 | else if (strcmp(name, BTF_BASE_ELF_SEC) == 0) |
1146 | 0 | field = &secs->btf_base_data; |
1147 | 0 | else |
1148 | 0 | continue; |
1149 | | |
1150 | 0 | data = elf_getdata(scn, 0); |
1151 | 0 | if (!data) { |
1152 | 0 | pr_warn("failed to get section(%d, %s) data from %s\n", |
1153 | 0 | idx, name, path); |
1154 | 0 | goto err; |
1155 | 0 | } |
1156 | 0 | *field = data; |
1157 | 0 | } |
1158 | | |
1159 | 0 | return 0; |
1160 | | |
1161 | 0 | err: |
1162 | 0 | return -LIBBPF_ERRNO__FORMAT; |
1163 | 0 | } |
1164 | | |
1165 | | static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, |
1166 | | struct btf_ext **btf_ext) |
1167 | 0 | { |
1168 | 0 | struct btf_elf_secs secs = {}; |
1169 | 0 | struct btf *dist_base_btf = NULL; |
1170 | 0 | struct btf *btf = NULL; |
1171 | 0 | int err = 0, fd = -1; |
1172 | 0 | Elf *elf = NULL; |
1173 | |
|
1174 | 0 | if (elf_version(EV_CURRENT) == EV_NONE) { |
1175 | 0 | pr_warn("failed to init libelf for %s\n", path); |
1176 | 0 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
1177 | 0 | } |
1178 | | |
1179 | 0 | fd = open(path, O_RDONLY | O_CLOEXEC); |
1180 | 0 | if (fd < 0) { |
1181 | 0 | err = -errno; |
1182 | 0 | pr_warn("failed to open %s: %s\n", path, strerror(errno)); |
1183 | 0 | return ERR_PTR(err); |
1184 | 0 | } |
1185 | | |
1186 | 0 | elf = elf_begin(fd, ELF_C_READ, NULL); |
1187 | 0 | if (!elf) { |
1188 | 0 | pr_warn("failed to open %s as ELF file\n", path); |
1189 | 0 | goto done; |
1190 | 0 | } |
1191 | | |
1192 | 0 | err = btf_find_elf_sections(elf, path, &secs); |
1193 | 0 | if (err) |
1194 | 0 | goto done; |
1195 | | |
1196 | 0 | if (!secs.btf_data) { |
1197 | 0 | pr_warn("failed to find '%s' ELF section in %s\n", BTF_ELF_SEC, path); |
1198 | 0 | err = -ENODATA; |
1199 | 0 | goto done; |
1200 | 0 | } |
1201 | | |
1202 | 0 | if (secs.btf_base_data) { |
1203 | 0 | dist_base_btf = btf_new(secs.btf_base_data->d_buf, secs.btf_base_data->d_size, |
1204 | 0 | NULL); |
1205 | 0 | if (IS_ERR(dist_base_btf)) { |
1206 | 0 | err = PTR_ERR(dist_base_btf); |
1207 | 0 | dist_base_btf = NULL; |
1208 | 0 | goto done; |
1209 | 0 | } |
1210 | 0 | } |
1211 | | |
1212 | 0 | btf = btf_new(secs.btf_data->d_buf, secs.btf_data->d_size, |
1213 | 0 | dist_base_btf ?: base_btf); |
1214 | 0 | if (IS_ERR(btf)) { |
1215 | 0 | err = PTR_ERR(btf); |
1216 | 0 | goto done; |
1217 | 0 | } |
1218 | 0 | if (dist_base_btf && base_btf) { |
1219 | 0 | err = btf__relocate(btf, base_btf); |
1220 | 0 | if (err) |
1221 | 0 | goto done; |
1222 | 0 | btf__free(dist_base_btf); |
1223 | 0 | dist_base_btf = NULL; |
1224 | 0 | } |
1225 | | |
1226 | 0 | if (dist_base_btf) |
1227 | 0 | btf->owns_base = true; |
1228 | |
|
1229 | 0 | switch (gelf_getclass(elf)) { |
1230 | 0 | case ELFCLASS32: |
1231 | 0 | btf__set_pointer_size(btf, 4); |
1232 | 0 | break; |
1233 | 0 | case ELFCLASS64: |
1234 | 0 | btf__set_pointer_size(btf, 8); |
1235 | 0 | break; |
1236 | 0 | default: |
1237 | 0 | pr_warn("failed to get ELF class (bitness) for %s\n", path); |
1238 | 0 | break; |
1239 | 0 | } |
1240 | | |
1241 | 0 | if (btf_ext && secs.btf_ext_data) { |
1242 | 0 | *btf_ext = btf_ext__new(secs.btf_ext_data->d_buf, secs.btf_ext_data->d_size); |
1243 | 0 | if (IS_ERR(*btf_ext)) { |
1244 | 0 | err = PTR_ERR(*btf_ext); |
1245 | 0 | goto done; |
1246 | 0 | } |
1247 | 0 | } else if (btf_ext) { |
1248 | 0 | *btf_ext = NULL; |
1249 | 0 | } |
1250 | 0 | done: |
1251 | 0 | if (elf) |
1252 | 0 | elf_end(elf); |
1253 | 0 | close(fd); |
1254 | |
|
1255 | 0 | if (!err) |
1256 | 0 | return btf; |
1257 | | |
1258 | 0 | if (btf_ext) |
1259 | 0 | btf_ext__free(*btf_ext); |
1260 | 0 | btf__free(dist_base_btf); |
1261 | 0 | btf__free(btf); |
1262 | |
|
1263 | 0 | return ERR_PTR(err); |
1264 | 0 | } |
1265 | | |
1266 | | struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext) |
1267 | 0 | { |
1268 | 0 | return libbpf_ptr(btf_parse_elf(path, NULL, btf_ext)); |
1269 | 0 | } |
1270 | | |
1271 | | struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf) |
1272 | 0 | { |
1273 | 0 | return libbpf_ptr(btf_parse_elf(path, base_btf, NULL)); |
1274 | 0 | } |
1275 | | |
1276 | | static struct btf *btf_parse_raw(const char *path, struct btf *base_btf) |
1277 | 0 | { |
1278 | 0 | struct btf *btf = NULL; |
1279 | 0 | void *data = NULL; |
1280 | 0 | FILE *f = NULL; |
1281 | 0 | __u16 magic; |
1282 | 0 | int err = 0; |
1283 | 0 | long sz; |
1284 | |
|
1285 | 0 | f = fopen(path, "rbe"); |
1286 | 0 | if (!f) { |
1287 | 0 | err = -errno; |
1288 | 0 | goto err_out; |
1289 | 0 | } |
1290 | | |
1291 | | /* check BTF magic */ |
1292 | 0 | if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) { |
1293 | 0 | err = -EIO; |
1294 | 0 | goto err_out; |
1295 | 0 | } |
1296 | 0 | if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) { |
1297 | | /* definitely not a raw BTF */ |
1298 | 0 | err = -EPROTO; |
1299 | 0 | goto err_out; |
1300 | 0 | } |
1301 | | |
1302 | | /* get file size */ |
1303 | 0 | if (fseek(f, 0, SEEK_END)) { |
1304 | 0 | err = -errno; |
1305 | 0 | goto err_out; |
1306 | 0 | } |
1307 | 0 | sz = ftell(f); |
1308 | 0 | if (sz < 0) { |
1309 | 0 | err = -errno; |
1310 | 0 | goto err_out; |
1311 | 0 | } |
1312 | | /* rewind to the start */ |
1313 | 0 | if (fseek(f, 0, SEEK_SET)) { |
1314 | 0 | err = -errno; |
1315 | 0 | goto err_out; |
1316 | 0 | } |
1317 | | |
1318 | | /* pre-alloc memory and read all of BTF data */ |
1319 | 0 | data = malloc(sz); |
1320 | 0 | if (!data) { |
1321 | 0 | err = -ENOMEM; |
1322 | 0 | goto err_out; |
1323 | 0 | } |
1324 | 0 | if (fread(data, 1, sz, f) < sz) { |
1325 | 0 | err = -EIO; |
1326 | 0 | goto err_out; |
1327 | 0 | } |
1328 | | |
1329 | | /* finally parse BTF data */ |
1330 | 0 | btf = btf_new(data, sz, base_btf); |
1331 | |
|
1332 | 0 | err_out: |
1333 | 0 | free(data); |
1334 | 0 | if (f) |
1335 | 0 | fclose(f); |
1336 | 0 | return err ? ERR_PTR(err) : btf; |
1337 | 0 | } |
1338 | | |
1339 | | struct btf *btf__parse_raw(const char *path) |
1340 | 0 | { |
1341 | 0 | return libbpf_ptr(btf_parse_raw(path, NULL)); |
1342 | 0 | } |
1343 | | |
1344 | | struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf) |
1345 | 0 | { |
1346 | 0 | return libbpf_ptr(btf_parse_raw(path, base_btf)); |
1347 | 0 | } |
1348 | | |
1349 | | static struct btf *btf_parse(const char *path, struct btf *base_btf, struct btf_ext **btf_ext) |
1350 | 0 | { |
1351 | 0 | struct btf *btf; |
1352 | 0 | int err; |
1353 | |
|
1354 | 0 | if (btf_ext) |
1355 | 0 | *btf_ext = NULL; |
1356 | |
|
1357 | 0 | btf = btf_parse_raw(path, base_btf); |
1358 | 0 | err = libbpf_get_error(btf); |
1359 | 0 | if (!err) |
1360 | 0 | return btf; |
1361 | 0 | if (err != -EPROTO) |
1362 | 0 | return ERR_PTR(err); |
1363 | 0 | return btf_parse_elf(path, base_btf, btf_ext); |
1364 | 0 | } |
1365 | | |
1366 | | struct btf *btf__parse(const char *path, struct btf_ext **btf_ext) |
1367 | 0 | { |
1368 | 0 | return libbpf_ptr(btf_parse(path, NULL, btf_ext)); |
1369 | 0 | } |
1370 | | |
1371 | | struct btf *btf__parse_split(const char *path, struct btf *base_btf) |
1372 | 0 | { |
1373 | 0 | return libbpf_ptr(btf_parse(path, base_btf, NULL)); |
1374 | 0 | } |
1375 | | |
1376 | | static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian); |
1377 | | |
1378 | | int btf_load_into_kernel(struct btf *btf, |
1379 | | char *log_buf, size_t log_sz, __u32 log_level, |
1380 | | int token_fd) |
1381 | 0 | { |
1382 | 0 | LIBBPF_OPTS(bpf_btf_load_opts, opts); |
1383 | 0 | __u32 buf_sz = 0, raw_size; |
1384 | 0 | char *buf = NULL, *tmp; |
1385 | 0 | void *raw_data; |
1386 | 0 | int err = 0; |
1387 | |
|
1388 | 0 | if (btf->fd >= 0) |
1389 | 0 | return libbpf_err(-EEXIST); |
1390 | 0 | if (log_sz && !log_buf) |
1391 | 0 | return libbpf_err(-EINVAL); |
1392 | | |
1393 | | /* cache native raw data representation */ |
1394 | 0 | raw_data = btf_get_raw_data(btf, &raw_size, false); |
1395 | 0 | if (!raw_data) { |
1396 | 0 | err = -ENOMEM; |
1397 | 0 | goto done; |
1398 | 0 | } |
1399 | 0 | btf->raw_size = raw_size; |
1400 | 0 | btf->raw_data = raw_data; |
1401 | |
|
1402 | 0 | retry_load: |
1403 | | /* if log_level is 0, we won't provide log_buf/log_size to the kernel, |
1404 | | * initially. Only if BTF loading fails, we bump log_level to 1 and |
1405 | | * retry, using either auto-allocated or custom log_buf. This way |
1406 | | * non-NULL custom log_buf provides a buffer just in case, but hopes |
1407 | | * for successful load and no need for log_buf. |
1408 | | */ |
1409 | 0 | if (log_level) { |
1410 | | /* if caller didn't provide custom log_buf, we'll keep |
1411 | | * allocating our own progressively bigger buffers for BTF |
1412 | | * verification log |
1413 | | */ |
1414 | 0 | if (!log_buf) { |
1415 | 0 | buf_sz = max((__u32)BPF_LOG_BUF_SIZE, buf_sz * 2); |
1416 | 0 | tmp = realloc(buf, buf_sz); |
1417 | 0 | if (!tmp) { |
1418 | 0 | err = -ENOMEM; |
1419 | 0 | goto done; |
1420 | 0 | } |
1421 | 0 | buf = tmp; |
1422 | 0 | buf[0] = '\0'; |
1423 | 0 | } |
1424 | | |
1425 | 0 | opts.log_buf = log_buf ? log_buf : buf; |
1426 | 0 | opts.log_size = log_buf ? log_sz : buf_sz; |
1427 | 0 | opts.log_level = log_level; |
1428 | 0 | } |
1429 | | |
1430 | 0 | opts.token_fd = token_fd; |
1431 | 0 | if (token_fd) |
1432 | 0 | opts.btf_flags |= BPF_F_TOKEN_FD; |
1433 | |
|
1434 | 0 | btf->fd = bpf_btf_load(raw_data, raw_size, &opts); |
1435 | 0 | if (btf->fd < 0) { |
1436 | | /* time to turn on verbose mode and try again */ |
1437 | 0 | if (log_level == 0) { |
1438 | 0 | log_level = 1; |
1439 | 0 | goto retry_load; |
1440 | 0 | } |
1441 | | /* only retry if caller didn't provide custom log_buf, but |
1442 | | * make sure we can never overflow buf_sz |
1443 | | */ |
1444 | 0 | if (!log_buf && errno == ENOSPC && buf_sz <= UINT_MAX / 2) |
1445 | 0 | goto retry_load; |
1446 | | |
1447 | 0 | err = -errno; |
1448 | 0 | pr_warn("BTF loading error: %d\n", err); |
1449 | | /* don't print out contents of custom log_buf */ |
1450 | 0 | if (!log_buf && buf[0]) |
1451 | 0 | pr_warn("-- BEGIN BTF LOAD LOG ---\n%s\n-- END BTF LOAD LOG --\n", buf); |
1452 | 0 | } |
1453 | | |
1454 | 0 | done: |
1455 | 0 | free(buf); |
1456 | 0 | return libbpf_err(err); |
1457 | 0 | } |
1458 | | |
1459 | | int btf__load_into_kernel(struct btf *btf) |
1460 | 0 | { |
1461 | 0 | return btf_load_into_kernel(btf, NULL, 0, 0, 0); |
1462 | 0 | } |
1463 | | |
1464 | | int btf__fd(const struct btf *btf) |
1465 | 0 | { |
1466 | 0 | return btf->fd; |
1467 | 0 | } |
1468 | | |
1469 | | void btf__set_fd(struct btf *btf, int fd) |
1470 | 0 | { |
1471 | 0 | btf->fd = fd; |
1472 | 0 | } |
1473 | | |
1474 | | static const void *btf_strs_data(const struct btf *btf) |
1475 | 87.2k | { |
1476 | 87.2k | return btf->strs_data ? btf->strs_data : strset__data(btf->strs_set); |
1477 | 87.2k | } |
1478 | | |
1479 | | static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian) |
1480 | 0 | { |
1481 | 0 | struct btf_header *hdr = btf->hdr; |
1482 | 0 | struct btf_type *t; |
1483 | 0 | void *data, *p; |
1484 | 0 | __u32 data_sz; |
1485 | 0 | int i; |
1486 | |
|
1487 | 0 | data = swap_endian ? btf->raw_data_swapped : btf->raw_data; |
1488 | 0 | if (data) { |
1489 | 0 | *size = btf->raw_size; |
1490 | 0 | return data; |
1491 | 0 | } |
1492 | | |
1493 | 0 | data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len; |
1494 | 0 | data = calloc(1, data_sz); |
1495 | 0 | if (!data) |
1496 | 0 | return NULL; |
1497 | 0 | p = data; |
1498 | |
|
1499 | 0 | memcpy(p, hdr, hdr->hdr_len); |
1500 | 0 | if (swap_endian) |
1501 | 0 | btf_bswap_hdr(p); |
1502 | 0 | p += hdr->hdr_len; |
1503 | |
|
1504 | 0 | memcpy(p, btf->types_data, hdr->type_len); |
1505 | 0 | if (swap_endian) { |
1506 | 0 | for (i = 0; i < btf->nr_types; i++) { |
1507 | 0 | t = p + btf->type_offs[i]; |
1508 | | /* btf_bswap_type_rest() relies on native t->info, so |
1509 | | * we swap base type info after we swapped all the |
1510 | | * additional information |
1511 | | */ |
1512 | 0 | if (btf_bswap_type_rest(t)) |
1513 | 0 | goto err_out; |
1514 | 0 | btf_bswap_type_base(t); |
1515 | 0 | } |
1516 | 0 | } |
1517 | 0 | p += hdr->type_len; |
1518 | |
|
1519 | 0 | memcpy(p, btf_strs_data(btf), hdr->str_len); |
1520 | 0 | p += hdr->str_len; |
1521 | |
|
1522 | 0 | *size = data_sz; |
1523 | 0 | return data; |
1524 | 0 | err_out: |
1525 | 0 | free(data); |
1526 | 0 | return NULL; |
1527 | 0 | } |
1528 | | |
1529 | | const void *btf__raw_data(const struct btf *btf_ro, __u32 *size) |
1530 | 0 | { |
1531 | 0 | struct btf *btf = (struct btf *)btf_ro; |
1532 | 0 | __u32 data_sz; |
1533 | 0 | void *data; |
1534 | |
|
1535 | 0 | data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian); |
1536 | 0 | if (!data) |
1537 | 0 | return errno = ENOMEM, NULL; |
1538 | | |
1539 | 0 | btf->raw_size = data_sz; |
1540 | 0 | if (btf->swapped_endian) |
1541 | 0 | btf->raw_data_swapped = data; |
1542 | 0 | else |
1543 | 0 | btf->raw_data = data; |
1544 | 0 | *size = data_sz; |
1545 | 0 | return data; |
1546 | 0 | } |
1547 | | |
1548 | | __attribute__((alias("btf__raw_data"))) |
1549 | | const void *btf__get_raw_data(const struct btf *btf, __u32 *size); |
1550 | | |
1551 | | const char *btf__str_by_offset(const struct btf *btf, __u32 offset) |
1552 | 87.5k | { |
1553 | 87.5k | if (offset < btf->start_str_off) |
1554 | 0 | return btf__str_by_offset(btf->base_btf, offset); |
1555 | 87.5k | else if (offset - btf->start_str_off < btf->hdr->str_len) |
1556 | 87.2k | return btf_strs_data(btf) + (offset - btf->start_str_off); |
1557 | 308 | else |
1558 | 308 | return errno = EINVAL, NULL; |
1559 | 87.5k | } |
1560 | | |
1561 | | const char *btf__name_by_offset(const struct btf *btf, __u32 offset) |
1562 | 42.0k | { |
1563 | 42.0k | return btf__str_by_offset(btf, offset); |
1564 | 42.0k | } |
1565 | | |
1566 | | struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf) |
1567 | 0 | { |
1568 | 0 | struct bpf_btf_info btf_info; |
1569 | 0 | __u32 len = sizeof(btf_info); |
1570 | 0 | __u32 last_size; |
1571 | 0 | struct btf *btf; |
1572 | 0 | void *ptr; |
1573 | 0 | int err; |
1574 | | |
1575 | | /* we won't know btf_size until we call bpf_btf_get_info_by_fd(). so |
1576 | | * let's start with a sane default - 4KiB here - and resize it only if |
1577 | | * bpf_btf_get_info_by_fd() needs a bigger buffer. |
1578 | | */ |
1579 | 0 | last_size = 4096; |
1580 | 0 | ptr = malloc(last_size); |
1581 | 0 | if (!ptr) |
1582 | 0 | return ERR_PTR(-ENOMEM); |
1583 | | |
1584 | 0 | memset(&btf_info, 0, sizeof(btf_info)); |
1585 | 0 | btf_info.btf = ptr_to_u64(ptr); |
1586 | 0 | btf_info.btf_size = last_size; |
1587 | 0 | err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len); |
1588 | |
|
1589 | 0 | if (!err && btf_info.btf_size > last_size) { |
1590 | 0 | void *temp_ptr; |
1591 | |
|
1592 | 0 | last_size = btf_info.btf_size; |
1593 | 0 | temp_ptr = realloc(ptr, last_size); |
1594 | 0 | if (!temp_ptr) { |
1595 | 0 | btf = ERR_PTR(-ENOMEM); |
1596 | 0 | goto exit_free; |
1597 | 0 | } |
1598 | 0 | ptr = temp_ptr; |
1599 | |
|
1600 | 0 | len = sizeof(btf_info); |
1601 | 0 | memset(&btf_info, 0, sizeof(btf_info)); |
1602 | 0 | btf_info.btf = ptr_to_u64(ptr); |
1603 | 0 | btf_info.btf_size = last_size; |
1604 | |
|
1605 | 0 | err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len); |
1606 | 0 | } |
1607 | | |
1608 | 0 | if (err || btf_info.btf_size > last_size) { |
1609 | 0 | btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG); |
1610 | 0 | goto exit_free; |
1611 | 0 | } |
1612 | | |
1613 | 0 | btf = btf_new(ptr, btf_info.btf_size, base_btf); |
1614 | |
|
1615 | 0 | exit_free: |
1616 | 0 | free(ptr); |
1617 | 0 | return btf; |
1618 | 0 | } |
1619 | | |
1620 | | struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf) |
1621 | 0 | { |
1622 | 0 | struct btf *btf; |
1623 | 0 | int btf_fd; |
1624 | |
|
1625 | 0 | btf_fd = bpf_btf_get_fd_by_id(id); |
1626 | 0 | if (btf_fd < 0) |
1627 | 0 | return libbpf_err_ptr(-errno); |
1628 | | |
1629 | 0 | btf = btf_get_from_fd(btf_fd, base_btf); |
1630 | 0 | close(btf_fd); |
1631 | |
|
1632 | 0 | return libbpf_ptr(btf); |
1633 | 0 | } |
1634 | | |
1635 | | struct btf *btf__load_from_kernel_by_id(__u32 id) |
1636 | 0 | { |
1637 | 0 | return btf__load_from_kernel_by_id_split(id, NULL); |
1638 | 0 | } |
1639 | | |
1640 | | static void btf_invalidate_raw_data(struct btf *btf) |
1641 | 1.05k | { |
1642 | 1.05k | if (btf->raw_data) { |
1643 | 526 | free(btf->raw_data); |
1644 | 526 | btf->raw_data = NULL; |
1645 | 526 | } |
1646 | 1.05k | if (btf->raw_data_swapped) { |
1647 | 0 | free(btf->raw_data_swapped); |
1648 | 0 | btf->raw_data_swapped = NULL; |
1649 | 0 | } |
1650 | 1.05k | } |
1651 | | |
1652 | | /* Ensure BTF is ready to be modified (by splitting into a three memory |
1653 | | * regions for header, types, and strings). Also invalidate cached |
1654 | | * raw_data, if any. |
1655 | | */ |
1656 | | static int btf_ensure_modifiable(struct btf *btf) |
1657 | 1.05k | { |
1658 | 1.05k | void *hdr, *types; |
1659 | 1.05k | struct strset *set = NULL; |
1660 | 1.05k | int err = -ENOMEM; |
1661 | | |
1662 | 1.05k | if (btf_is_modifiable(btf)) { |
1663 | | /* any BTF modification invalidates raw_data */ |
1664 | 526 | btf_invalidate_raw_data(btf); |
1665 | 526 | return 0; |
1666 | 526 | } |
1667 | | |
1668 | | /* split raw data into three memory regions */ |
1669 | 526 | hdr = malloc(btf->hdr->hdr_len); |
1670 | 526 | types = malloc(btf->hdr->type_len); |
1671 | 526 | if (!hdr || !types) |
1672 | 0 | goto err_out; |
1673 | | |
1674 | 526 | memcpy(hdr, btf->hdr, btf->hdr->hdr_len); |
1675 | 526 | memcpy(types, btf->types_data, btf->hdr->type_len); |
1676 | | |
1677 | | /* build lookup index for all strings */ |
1678 | 526 | set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr->str_len); |
1679 | 526 | if (IS_ERR(set)) { |
1680 | 0 | err = PTR_ERR(set); |
1681 | 0 | goto err_out; |
1682 | 0 | } |
1683 | | |
1684 | | /* only when everything was successful, update internal state */ |
1685 | 526 | btf->hdr = hdr; |
1686 | 526 | btf->types_data = types; |
1687 | 526 | btf->types_data_cap = btf->hdr->type_len; |
1688 | 526 | btf->strs_data = NULL; |
1689 | 526 | btf->strs_set = set; |
1690 | | /* if BTF was created from scratch, all strings are guaranteed to be |
1691 | | * unique and deduplicated |
1692 | | */ |
1693 | 526 | if (btf->hdr->str_len == 0) |
1694 | 0 | btf->strs_deduped = true; |
1695 | 526 | if (!btf->base_btf && btf->hdr->str_len == 1) |
1696 | 0 | btf->strs_deduped = true; |
1697 | | |
1698 | | /* invalidate raw_data representation */ |
1699 | 526 | btf_invalidate_raw_data(btf); |
1700 | | |
1701 | 526 | return 0; |
1702 | | |
1703 | 0 | err_out: |
1704 | 0 | strset__free(set); |
1705 | 0 | free(hdr); |
1706 | 0 | free(types); |
1707 | 0 | return err; |
1708 | 526 | } |
1709 | | |
1710 | | /* Find an offset in BTF string section that corresponds to a given string *s*. |
1711 | | * Returns: |
1712 | | * - >0 offset into string section, if string is found; |
1713 | | * - -ENOENT, if string is not in the string section; |
1714 | | * - <0, on any other error. |
1715 | | */ |
1716 | | int btf__find_str(struct btf *btf, const char *s) |
1717 | 0 | { |
1718 | 0 | int off; |
1719 | |
|
1720 | 0 | if (btf->base_btf) { |
1721 | 0 | off = btf__find_str(btf->base_btf, s); |
1722 | 0 | if (off != -ENOENT) |
1723 | 0 | return off; |
1724 | 0 | } |
1725 | | |
1726 | | /* BTF needs to be in a modifiable state to build string lookup index */ |
1727 | 0 | if (btf_ensure_modifiable(btf)) |
1728 | 0 | return libbpf_err(-ENOMEM); |
1729 | | |
1730 | 0 | off = strset__find_str(btf->strs_set, s); |
1731 | 0 | if (off < 0) |
1732 | 0 | return libbpf_err(off); |
1733 | | |
1734 | 0 | return btf->start_str_off + off; |
1735 | 0 | } |
1736 | | |
1737 | | /* Add a string s to the BTF string section. |
1738 | | * Returns: |
1739 | | * - > 0 offset into string section, on success; |
1740 | | * - < 0, on error. |
1741 | | */ |
1742 | | int btf__add_str(struct btf *btf, const char *s) |
1743 | 526 | { |
1744 | 526 | int off; |
1745 | | |
1746 | 526 | if (btf->base_btf) { |
1747 | 0 | off = btf__find_str(btf->base_btf, s); |
1748 | 0 | if (off != -ENOENT) |
1749 | 0 | return off; |
1750 | 0 | } |
1751 | | |
1752 | 526 | if (btf_ensure_modifiable(btf)) |
1753 | 0 | return libbpf_err(-ENOMEM); |
1754 | | |
1755 | 526 | off = strset__add_str(btf->strs_set, s); |
1756 | 526 | if (off < 0) |
1757 | 0 | return libbpf_err(off); |
1758 | | |
1759 | 526 | btf->hdr->str_len = strset__data_size(btf->strs_set); |
1760 | | |
1761 | 526 | return btf->start_str_off + off; |
1762 | 526 | } |
1763 | | |
1764 | | static void *btf_add_type_mem(struct btf *btf, size_t add_sz) |
1765 | 526 | { |
1766 | 526 | return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1, |
1767 | 526 | btf->hdr->type_len, UINT_MAX, add_sz); |
1768 | 526 | } |
1769 | | |
1770 | | static void btf_type_inc_vlen(struct btf_type *t) |
1771 | 0 | { |
1772 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t)); |
1773 | 0 | } |
1774 | | |
1775 | | static int btf_commit_type(struct btf *btf, int data_sz) |
1776 | 526 | { |
1777 | 526 | int err; |
1778 | | |
1779 | 526 | err = btf_add_type_idx_entry(btf, btf->hdr->type_len); |
1780 | 526 | if (err) |
1781 | 0 | return libbpf_err(err); |
1782 | | |
1783 | 526 | btf->hdr->type_len += data_sz; |
1784 | 526 | btf->hdr->str_off += data_sz; |
1785 | 526 | btf->nr_types++; |
1786 | 526 | return btf->start_id + btf->nr_types - 1; |
1787 | 526 | } |
1788 | | |
1789 | | struct btf_pipe { |
1790 | | const struct btf *src; |
1791 | | struct btf *dst; |
1792 | | struct hashmap *str_off_map; /* map string offsets from src to dst */ |
1793 | | }; |
1794 | | |
1795 | | static int btf_rewrite_str(struct btf_pipe *p, __u32 *str_off) |
1796 | 0 | { |
1797 | 0 | long mapped_off; |
1798 | 0 | int off, err; |
1799 | |
|
1800 | 0 | if (!*str_off) /* nothing to do for empty strings */ |
1801 | 0 | return 0; |
1802 | | |
1803 | 0 | if (p->str_off_map && |
1804 | 0 | hashmap__find(p->str_off_map, *str_off, &mapped_off)) { |
1805 | 0 | *str_off = mapped_off; |
1806 | 0 | return 0; |
1807 | 0 | } |
1808 | | |
1809 | 0 | off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off)); |
1810 | 0 | if (off < 0) |
1811 | 0 | return off; |
1812 | | |
1813 | | /* Remember string mapping from src to dst. It avoids |
1814 | | * performing expensive string comparisons. |
1815 | | */ |
1816 | 0 | if (p->str_off_map) { |
1817 | 0 | err = hashmap__append(p->str_off_map, *str_off, off); |
1818 | 0 | if (err) |
1819 | 0 | return err; |
1820 | 0 | } |
1821 | | |
1822 | 0 | *str_off = off; |
1823 | 0 | return 0; |
1824 | 0 | } |
1825 | | |
1826 | | static int btf_add_type(struct btf_pipe *p, const struct btf_type *src_type) |
1827 | 0 | { |
1828 | 0 | struct btf_field_iter it; |
1829 | 0 | struct btf_type *t; |
1830 | 0 | __u32 *str_off; |
1831 | 0 | int sz, err; |
1832 | |
|
1833 | 0 | sz = btf_type_size(src_type); |
1834 | 0 | if (sz < 0) |
1835 | 0 | return libbpf_err(sz); |
1836 | | |
1837 | | /* deconstruct BTF, if necessary, and invalidate raw_data */ |
1838 | 0 | if (btf_ensure_modifiable(p->dst)) |
1839 | 0 | return libbpf_err(-ENOMEM); |
1840 | | |
1841 | 0 | t = btf_add_type_mem(p->dst, sz); |
1842 | 0 | if (!t) |
1843 | 0 | return libbpf_err(-ENOMEM); |
1844 | | |
1845 | 0 | memcpy(t, src_type, sz); |
1846 | |
|
1847 | 0 | err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS); |
1848 | 0 | if (err) |
1849 | 0 | return libbpf_err(err); |
1850 | | |
1851 | 0 | while ((str_off = btf_field_iter_next(&it))) { |
1852 | 0 | err = btf_rewrite_str(p, str_off); |
1853 | 0 | if (err) |
1854 | 0 | return libbpf_err(err); |
1855 | 0 | } |
1856 | | |
1857 | 0 | return btf_commit_type(p->dst, sz); |
1858 | 0 | } |
1859 | | |
1860 | | int btf__add_type(struct btf *btf, const struct btf *src_btf, const struct btf_type *src_type) |
1861 | 0 | { |
1862 | 0 | struct btf_pipe p = { .src = src_btf, .dst = btf }; |
1863 | |
|
1864 | 0 | return btf_add_type(&p, src_type); |
1865 | 0 | } |
1866 | | |
1867 | | static size_t btf_dedup_identity_hash_fn(long key, void *ctx); |
1868 | | static bool btf_dedup_equal_fn(long k1, long k2, void *ctx); |
1869 | | |
1870 | | int btf__add_btf(struct btf *btf, const struct btf *src_btf) |
1871 | 0 | { |
1872 | 0 | struct btf_pipe p = { .src = src_btf, .dst = btf }; |
1873 | 0 | int data_sz, sz, cnt, i, err, old_strs_len; |
1874 | 0 | __u32 *off; |
1875 | 0 | void *t; |
1876 | | |
1877 | | /* appending split BTF isn't supported yet */ |
1878 | 0 | if (src_btf->base_btf) |
1879 | 0 | return libbpf_err(-ENOTSUP); |
1880 | | |
1881 | | /* deconstruct BTF, if necessary, and invalidate raw_data */ |
1882 | 0 | if (btf_ensure_modifiable(btf)) |
1883 | 0 | return libbpf_err(-ENOMEM); |
1884 | | |
1885 | | /* remember original strings section size if we have to roll back |
1886 | | * partial strings section changes |
1887 | | */ |
1888 | 0 | old_strs_len = btf->hdr->str_len; |
1889 | |
|
1890 | 0 | data_sz = src_btf->hdr->type_len; |
1891 | 0 | cnt = btf__type_cnt(src_btf) - 1; |
1892 | | |
1893 | | /* pre-allocate enough memory for new types */ |
1894 | 0 | t = btf_add_type_mem(btf, data_sz); |
1895 | 0 | if (!t) |
1896 | 0 | return libbpf_err(-ENOMEM); |
1897 | | |
1898 | | /* pre-allocate enough memory for type offset index for new types */ |
1899 | 0 | off = btf_add_type_offs_mem(btf, cnt); |
1900 | 0 | if (!off) |
1901 | 0 | return libbpf_err(-ENOMEM); |
1902 | | |
1903 | | /* Map the string offsets from src_btf to the offsets from btf to improve performance */ |
1904 | 0 | p.str_off_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL); |
1905 | 0 | if (IS_ERR(p.str_off_map)) |
1906 | 0 | return libbpf_err(-ENOMEM); |
1907 | | |
1908 | | /* bulk copy types data for all types from src_btf */ |
1909 | 0 | memcpy(t, src_btf->types_data, data_sz); |
1910 | |
|
1911 | 0 | for (i = 0; i < cnt; i++) { |
1912 | 0 | struct btf_field_iter it; |
1913 | 0 | __u32 *type_id, *str_off; |
1914 | |
|
1915 | 0 | sz = btf_type_size(t); |
1916 | 0 | if (sz < 0) { |
1917 | | /* unlikely, has to be corrupted src_btf */ |
1918 | 0 | err = sz; |
1919 | 0 | goto err_out; |
1920 | 0 | } |
1921 | | |
1922 | | /* fill out type ID to type offset mapping for lookups by type ID */ |
1923 | 0 | *off = t - btf->types_data; |
1924 | | |
1925 | | /* add, dedup, and remap strings referenced by this BTF type */ |
1926 | 0 | err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS); |
1927 | 0 | if (err) |
1928 | 0 | goto err_out; |
1929 | 0 | while ((str_off = btf_field_iter_next(&it))) { |
1930 | 0 | err = btf_rewrite_str(&p, str_off); |
1931 | 0 | if (err) |
1932 | 0 | goto err_out; |
1933 | 0 | } |
1934 | | |
1935 | | /* remap all type IDs referenced from this BTF type */ |
1936 | 0 | err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS); |
1937 | 0 | if (err) |
1938 | 0 | goto err_out; |
1939 | | |
1940 | 0 | while ((type_id = btf_field_iter_next(&it))) { |
1941 | 0 | if (!*type_id) /* nothing to do for VOID references */ |
1942 | 0 | continue; |
1943 | | |
1944 | | /* we haven't updated btf's type count yet, so |
1945 | | * btf->start_id + btf->nr_types - 1 is the type ID offset we should |
1946 | | * add to all newly added BTF types |
1947 | | */ |
1948 | 0 | *type_id += btf->start_id + btf->nr_types - 1; |
1949 | 0 | } |
1950 | | |
1951 | | /* go to next type data and type offset index entry */ |
1952 | 0 | t += sz; |
1953 | 0 | off++; |
1954 | 0 | } |
1955 | | |
1956 | | /* Up until now any of the copied type data was effectively invisible, |
1957 | | * so if we exited early before this point due to error, BTF would be |
1958 | | * effectively unmodified. There would be extra internal memory |
1959 | | * pre-allocated, but it would not be available for querying. But now |
1960 | | * that we've copied and rewritten all the data successfully, we can |
1961 | | * update type count and various internal offsets and sizes to |
1962 | | * "commit" the changes and made them visible to the outside world. |
1963 | | */ |
1964 | 0 | btf->hdr->type_len += data_sz; |
1965 | 0 | btf->hdr->str_off += data_sz; |
1966 | 0 | btf->nr_types += cnt; |
1967 | |
|
1968 | 0 | hashmap__free(p.str_off_map); |
1969 | | |
1970 | | /* return type ID of the first added BTF type */ |
1971 | 0 | return btf->start_id + btf->nr_types - cnt; |
1972 | 0 | err_out: |
1973 | | /* zero out preallocated memory as if it was just allocated with |
1974 | | * libbpf_add_mem() |
1975 | | */ |
1976 | 0 | memset(btf->types_data + btf->hdr->type_len, 0, data_sz); |
1977 | 0 | memset(btf->strs_data + old_strs_len, 0, btf->hdr->str_len - old_strs_len); |
1978 | | |
1979 | | /* and now restore original strings section size; types data size |
1980 | | * wasn't modified, so doesn't need restoring, see big comment above |
1981 | | */ |
1982 | 0 | btf->hdr->str_len = old_strs_len; |
1983 | |
|
1984 | 0 | hashmap__free(p.str_off_map); |
1985 | |
|
1986 | 0 | return libbpf_err(err); |
1987 | 0 | } |
1988 | | |
1989 | | /* |
1990 | | * Append new BTF_KIND_INT type with: |
1991 | | * - *name* - non-empty, non-NULL type name; |
1992 | | * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes; |
1993 | | * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL. |
1994 | | * Returns: |
1995 | | * - >0, type ID of newly added BTF type; |
1996 | | * - <0, on error. |
1997 | | */ |
1998 | | int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding) |
1999 | 0 | { |
2000 | 0 | struct btf_type *t; |
2001 | 0 | int sz, name_off; |
2002 | | |
2003 | | /* non-empty name */ |
2004 | 0 | if (!name || !name[0]) |
2005 | 0 | return libbpf_err(-EINVAL); |
2006 | | /* byte_sz must be power of 2 */ |
2007 | 0 | if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16) |
2008 | 0 | return libbpf_err(-EINVAL); |
2009 | 0 | if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL)) |
2010 | 0 | return libbpf_err(-EINVAL); |
2011 | | |
2012 | | /* deconstruct BTF, if necessary, and invalidate raw_data */ |
2013 | 0 | if (btf_ensure_modifiable(btf)) |
2014 | 0 | return libbpf_err(-ENOMEM); |
2015 | | |
2016 | 0 | sz = sizeof(struct btf_type) + sizeof(int); |
2017 | 0 | t = btf_add_type_mem(btf, sz); |
2018 | 0 | if (!t) |
2019 | 0 | return libbpf_err(-ENOMEM); |
2020 | | |
2021 | | /* if something goes wrong later, we might end up with an extra string, |
2022 | | * but that shouldn't be a problem, because BTF can't be constructed |
2023 | | * completely anyway and will most probably be just discarded |
2024 | | */ |
2025 | 0 | name_off = btf__add_str(btf, name); |
2026 | 0 | if (name_off < 0) |
2027 | 0 | return name_off; |
2028 | | |
2029 | 0 | t->name_off = name_off; |
2030 | 0 | t->info = btf_type_info(BTF_KIND_INT, 0, 0); |
2031 | 0 | t->size = byte_sz; |
2032 | | /* set INT info, we don't allow setting legacy bit offset/size */ |
2033 | 0 | *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8); |
2034 | |
|
2035 | 0 | return btf_commit_type(btf, sz); |
2036 | 0 | } |
2037 | | |
2038 | | /* |
2039 | | * Append new BTF_KIND_FLOAT type with: |
2040 | | * - *name* - non-empty, non-NULL type name; |
2041 | | * - *sz* - size of the type, in bytes; |
2042 | | * Returns: |
2043 | | * - >0, type ID of newly added BTF type; |
2044 | | * - <0, on error. |
2045 | | */ |
2046 | | int btf__add_float(struct btf *btf, const char *name, size_t byte_sz) |
2047 | 0 | { |
2048 | 0 | struct btf_type *t; |
2049 | 0 | int sz, name_off; |
2050 | | |
2051 | | /* non-empty name */ |
2052 | 0 | if (!name || !name[0]) |
2053 | 0 | return libbpf_err(-EINVAL); |
2054 | | |
2055 | | /* byte_sz must be one of the explicitly allowed values */ |
2056 | 0 | if (byte_sz != 2 && byte_sz != 4 && byte_sz != 8 && byte_sz != 12 && |
2057 | 0 | byte_sz != 16) |
2058 | 0 | return libbpf_err(-EINVAL); |
2059 | | |
2060 | 0 | if (btf_ensure_modifiable(btf)) |
2061 | 0 | return libbpf_err(-ENOMEM); |
2062 | | |
2063 | 0 | sz = sizeof(struct btf_type); |
2064 | 0 | t = btf_add_type_mem(btf, sz); |
2065 | 0 | if (!t) |
2066 | 0 | return libbpf_err(-ENOMEM); |
2067 | | |
2068 | 0 | name_off = btf__add_str(btf, name); |
2069 | 0 | if (name_off < 0) |
2070 | 0 | return name_off; |
2071 | | |
2072 | 0 | t->name_off = name_off; |
2073 | 0 | t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0); |
2074 | 0 | t->size = byte_sz; |
2075 | |
|
2076 | 0 | return btf_commit_type(btf, sz); |
2077 | 0 | } |
2078 | | |
2079 | | /* it's completely legal to append BTF types with type IDs pointing forward to |
2080 | | * types that haven't been appended yet, so we only make sure that id looks |
2081 | | * sane, we can't guarantee that ID will always be valid |
2082 | | */ |
2083 | | static int validate_type_id(int id) |
2084 | 526 | { |
2085 | 526 | if (id < 0 || id > BTF_MAX_NR_TYPES) |
2086 | 0 | return -EINVAL; |
2087 | 526 | return 0; |
2088 | 526 | } |
2089 | | |
2090 | | /* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */ |
2091 | | static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id) |
2092 | 0 | { |
2093 | 0 | struct btf_type *t; |
2094 | 0 | int sz, name_off = 0; |
2095 | |
|
2096 | 0 | if (validate_type_id(ref_type_id)) |
2097 | 0 | return libbpf_err(-EINVAL); |
2098 | | |
2099 | 0 | if (btf_ensure_modifiable(btf)) |
2100 | 0 | return libbpf_err(-ENOMEM); |
2101 | | |
2102 | 0 | sz = sizeof(struct btf_type); |
2103 | 0 | t = btf_add_type_mem(btf, sz); |
2104 | 0 | if (!t) |
2105 | 0 | return libbpf_err(-ENOMEM); |
2106 | | |
2107 | 0 | if (name && name[0]) { |
2108 | 0 | name_off = btf__add_str(btf, name); |
2109 | 0 | if (name_off < 0) |
2110 | 0 | return name_off; |
2111 | 0 | } |
2112 | | |
2113 | 0 | t->name_off = name_off; |
2114 | 0 | t->info = btf_type_info(kind, 0, 0); |
2115 | 0 | t->type = ref_type_id; |
2116 | |
|
2117 | 0 | return btf_commit_type(btf, sz); |
2118 | 0 | } |
2119 | | |
2120 | | /* |
2121 | | * Append new BTF_KIND_PTR type with: |
2122 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2123 | | * Returns: |
2124 | | * - >0, type ID of newly added BTF type; |
2125 | | * - <0, on error. |
2126 | | */ |
2127 | | int btf__add_ptr(struct btf *btf, int ref_type_id) |
2128 | 0 | { |
2129 | 0 | return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id); |
2130 | 0 | } |
2131 | | |
2132 | | /* |
2133 | | * Append new BTF_KIND_ARRAY type with: |
2134 | | * - *index_type_id* - type ID of the type describing array index; |
2135 | | * - *elem_type_id* - type ID of the type describing array element; |
2136 | | * - *nr_elems* - the size of the array; |
2137 | | * Returns: |
2138 | | * - >0, type ID of newly added BTF type; |
2139 | | * - <0, on error. |
2140 | | */ |
2141 | | int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems) |
2142 | 0 | { |
2143 | 0 | struct btf_type *t; |
2144 | 0 | struct btf_array *a; |
2145 | 0 | int sz; |
2146 | |
|
2147 | 0 | if (validate_type_id(index_type_id) || validate_type_id(elem_type_id)) |
2148 | 0 | return libbpf_err(-EINVAL); |
2149 | | |
2150 | 0 | if (btf_ensure_modifiable(btf)) |
2151 | 0 | return libbpf_err(-ENOMEM); |
2152 | | |
2153 | 0 | sz = sizeof(struct btf_type) + sizeof(struct btf_array); |
2154 | 0 | t = btf_add_type_mem(btf, sz); |
2155 | 0 | if (!t) |
2156 | 0 | return libbpf_err(-ENOMEM); |
2157 | | |
2158 | 0 | t->name_off = 0; |
2159 | 0 | t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0); |
2160 | 0 | t->size = 0; |
2161 | |
|
2162 | 0 | a = btf_array(t); |
2163 | 0 | a->type = elem_type_id; |
2164 | 0 | a->index_type = index_type_id; |
2165 | 0 | a->nelems = nr_elems; |
2166 | |
|
2167 | 0 | return btf_commit_type(btf, sz); |
2168 | 0 | } |
2169 | | |
2170 | | /* generic STRUCT/UNION append function */ |
2171 | | static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz) |
2172 | 0 | { |
2173 | 0 | struct btf_type *t; |
2174 | 0 | int sz, name_off = 0; |
2175 | |
|
2176 | 0 | if (btf_ensure_modifiable(btf)) |
2177 | 0 | return libbpf_err(-ENOMEM); |
2178 | | |
2179 | 0 | sz = sizeof(struct btf_type); |
2180 | 0 | t = btf_add_type_mem(btf, sz); |
2181 | 0 | if (!t) |
2182 | 0 | return libbpf_err(-ENOMEM); |
2183 | | |
2184 | 0 | if (name && name[0]) { |
2185 | 0 | name_off = btf__add_str(btf, name); |
2186 | 0 | if (name_off < 0) |
2187 | 0 | return name_off; |
2188 | 0 | } |
2189 | | |
2190 | | /* start out with vlen=0 and no kflag; this will be adjusted when |
2191 | | * adding each member |
2192 | | */ |
2193 | 0 | t->name_off = name_off; |
2194 | 0 | t->info = btf_type_info(kind, 0, 0); |
2195 | 0 | t->size = bytes_sz; |
2196 | |
|
2197 | 0 | return btf_commit_type(btf, sz); |
2198 | 0 | } |
2199 | | |
2200 | | /* |
2201 | | * Append new BTF_KIND_STRUCT type with: |
2202 | | * - *name* - name of the struct, can be NULL or empty for anonymous structs; |
2203 | | * - *byte_sz* - size of the struct, in bytes; |
2204 | | * |
2205 | | * Struct initially has no fields in it. Fields can be added by |
2206 | | * btf__add_field() right after btf__add_struct() succeeds. |
2207 | | * |
2208 | | * Returns: |
2209 | | * - >0, type ID of newly added BTF type; |
2210 | | * - <0, on error. |
2211 | | */ |
2212 | | int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz) |
2213 | 0 | { |
2214 | 0 | return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz); |
2215 | 0 | } |
2216 | | |
2217 | | /* |
2218 | | * Append new BTF_KIND_UNION type with: |
2219 | | * - *name* - name of the union, can be NULL or empty for anonymous union; |
2220 | | * - *byte_sz* - size of the union, in bytes; |
2221 | | * |
2222 | | * Union initially has no fields in it. Fields can be added by |
2223 | | * btf__add_field() right after btf__add_union() succeeds. All fields |
2224 | | * should have *bit_offset* of 0. |
2225 | | * |
2226 | | * Returns: |
2227 | | * - >0, type ID of newly added BTF type; |
2228 | | * - <0, on error. |
2229 | | */ |
2230 | | int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz) |
2231 | 0 | { |
2232 | 0 | return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz); |
2233 | 0 | } |
2234 | | |
2235 | | static struct btf_type *btf_last_type(struct btf *btf) |
2236 | 0 | { |
2237 | 0 | return btf_type_by_id(btf, btf__type_cnt(btf) - 1); |
2238 | 0 | } |
2239 | | |
2240 | | /* |
2241 | | * Append new field for the current STRUCT/UNION type with: |
2242 | | * - *name* - name of the field, can be NULL or empty for anonymous field; |
2243 | | * - *type_id* - type ID for the type describing field type; |
2244 | | * - *bit_offset* - bit offset of the start of the field within struct/union; |
2245 | | * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields; |
2246 | | * Returns: |
2247 | | * - 0, on success; |
2248 | | * - <0, on error. |
2249 | | */ |
2250 | | int btf__add_field(struct btf *btf, const char *name, int type_id, |
2251 | | __u32 bit_offset, __u32 bit_size) |
2252 | 0 | { |
2253 | 0 | struct btf_type *t; |
2254 | 0 | struct btf_member *m; |
2255 | 0 | bool is_bitfield; |
2256 | 0 | int sz, name_off = 0; |
2257 | | |
2258 | | /* last type should be union/struct */ |
2259 | 0 | if (btf->nr_types == 0) |
2260 | 0 | return libbpf_err(-EINVAL); |
2261 | 0 | t = btf_last_type(btf); |
2262 | 0 | if (!btf_is_composite(t)) |
2263 | 0 | return libbpf_err(-EINVAL); |
2264 | | |
2265 | 0 | if (validate_type_id(type_id)) |
2266 | 0 | return libbpf_err(-EINVAL); |
2267 | | /* best-effort bit field offset/size enforcement */ |
2268 | 0 | is_bitfield = bit_size || (bit_offset % 8 != 0); |
2269 | 0 | if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff)) |
2270 | 0 | return libbpf_err(-EINVAL); |
2271 | | |
2272 | | /* only offset 0 is allowed for unions */ |
2273 | 0 | if (btf_is_union(t) && bit_offset) |
2274 | 0 | return libbpf_err(-EINVAL); |
2275 | | |
2276 | | /* decompose and invalidate raw data */ |
2277 | 0 | if (btf_ensure_modifiable(btf)) |
2278 | 0 | return libbpf_err(-ENOMEM); |
2279 | | |
2280 | 0 | sz = sizeof(struct btf_member); |
2281 | 0 | m = btf_add_type_mem(btf, sz); |
2282 | 0 | if (!m) |
2283 | 0 | return libbpf_err(-ENOMEM); |
2284 | | |
2285 | 0 | if (name && name[0]) { |
2286 | 0 | name_off = btf__add_str(btf, name); |
2287 | 0 | if (name_off < 0) |
2288 | 0 | return name_off; |
2289 | 0 | } |
2290 | | |
2291 | 0 | m->name_off = name_off; |
2292 | 0 | m->type = type_id; |
2293 | 0 | m->offset = bit_offset | (bit_size << 24); |
2294 | | |
2295 | | /* btf_add_type_mem can invalidate t pointer */ |
2296 | 0 | t = btf_last_type(btf); |
2297 | | /* update parent type's vlen and kflag */ |
2298 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t)); |
2299 | |
|
2300 | 0 | btf->hdr->type_len += sz; |
2301 | 0 | btf->hdr->str_off += sz; |
2302 | 0 | return 0; |
2303 | 0 | } |
2304 | | |
2305 | | static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz, |
2306 | | bool is_signed, __u8 kind) |
2307 | 0 | { |
2308 | 0 | struct btf_type *t; |
2309 | 0 | int sz, name_off = 0; |
2310 | | |
2311 | | /* byte_sz must be power of 2 */ |
2312 | 0 | if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8) |
2313 | 0 | return libbpf_err(-EINVAL); |
2314 | | |
2315 | 0 | if (btf_ensure_modifiable(btf)) |
2316 | 0 | return libbpf_err(-ENOMEM); |
2317 | | |
2318 | 0 | sz = sizeof(struct btf_type); |
2319 | 0 | t = btf_add_type_mem(btf, sz); |
2320 | 0 | if (!t) |
2321 | 0 | return libbpf_err(-ENOMEM); |
2322 | | |
2323 | 0 | if (name && name[0]) { |
2324 | 0 | name_off = btf__add_str(btf, name); |
2325 | 0 | if (name_off < 0) |
2326 | 0 | return name_off; |
2327 | 0 | } |
2328 | | |
2329 | | /* start out with vlen=0; it will be adjusted when adding enum values */ |
2330 | 0 | t->name_off = name_off; |
2331 | 0 | t->info = btf_type_info(kind, 0, is_signed); |
2332 | 0 | t->size = byte_sz; |
2333 | |
|
2334 | 0 | return btf_commit_type(btf, sz); |
2335 | 0 | } |
2336 | | |
2337 | | /* |
2338 | | * Append new BTF_KIND_ENUM type with: |
2339 | | * - *name* - name of the enum, can be NULL or empty for anonymous enums; |
2340 | | * - *byte_sz* - size of the enum, in bytes. |
2341 | | * |
2342 | | * Enum initially has no enum values in it (and corresponds to enum forward |
2343 | | * declaration). Enumerator values can be added by btf__add_enum_value() |
2344 | | * immediately after btf__add_enum() succeeds. |
2345 | | * |
2346 | | * Returns: |
2347 | | * - >0, type ID of newly added BTF type; |
2348 | | * - <0, on error. |
2349 | | */ |
2350 | | int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz) |
2351 | 0 | { |
2352 | | /* |
2353 | | * set the signedness to be unsigned, it will change to signed |
2354 | | * if any later enumerator is negative. |
2355 | | */ |
2356 | 0 | return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM); |
2357 | 0 | } |
2358 | | |
2359 | | /* |
2360 | | * Append new enum value for the current ENUM type with: |
2361 | | * - *name* - name of the enumerator value, can't be NULL or empty; |
2362 | | * - *value* - integer value corresponding to enum value *name*; |
2363 | | * Returns: |
2364 | | * - 0, on success; |
2365 | | * - <0, on error. |
2366 | | */ |
2367 | | int btf__add_enum_value(struct btf *btf, const char *name, __s64 value) |
2368 | 0 | { |
2369 | 0 | struct btf_type *t; |
2370 | 0 | struct btf_enum *v; |
2371 | 0 | int sz, name_off; |
2372 | | |
2373 | | /* last type should be BTF_KIND_ENUM */ |
2374 | 0 | if (btf->nr_types == 0) |
2375 | 0 | return libbpf_err(-EINVAL); |
2376 | 0 | t = btf_last_type(btf); |
2377 | 0 | if (!btf_is_enum(t)) |
2378 | 0 | return libbpf_err(-EINVAL); |
2379 | | |
2380 | | /* non-empty name */ |
2381 | 0 | if (!name || !name[0]) |
2382 | 0 | return libbpf_err(-EINVAL); |
2383 | 0 | if (value < INT_MIN || value > UINT_MAX) |
2384 | 0 | return libbpf_err(-E2BIG); |
2385 | | |
2386 | | /* decompose and invalidate raw data */ |
2387 | 0 | if (btf_ensure_modifiable(btf)) |
2388 | 0 | return libbpf_err(-ENOMEM); |
2389 | | |
2390 | 0 | sz = sizeof(struct btf_enum); |
2391 | 0 | v = btf_add_type_mem(btf, sz); |
2392 | 0 | if (!v) |
2393 | 0 | return libbpf_err(-ENOMEM); |
2394 | | |
2395 | 0 | name_off = btf__add_str(btf, name); |
2396 | 0 | if (name_off < 0) |
2397 | 0 | return name_off; |
2398 | | |
2399 | 0 | v->name_off = name_off; |
2400 | 0 | v->val = value; |
2401 | | |
2402 | | /* update parent type's vlen */ |
2403 | 0 | t = btf_last_type(btf); |
2404 | 0 | btf_type_inc_vlen(t); |
2405 | | |
2406 | | /* if negative value, set signedness to signed */ |
2407 | 0 | if (value < 0) |
2408 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t), true); |
2409 | |
|
2410 | 0 | btf->hdr->type_len += sz; |
2411 | 0 | btf->hdr->str_off += sz; |
2412 | 0 | return 0; |
2413 | 0 | } |
2414 | | |
2415 | | /* |
2416 | | * Append new BTF_KIND_ENUM64 type with: |
2417 | | * - *name* - name of the enum, can be NULL or empty for anonymous enums; |
2418 | | * - *byte_sz* - size of the enum, in bytes. |
2419 | | * - *is_signed* - whether the enum values are signed or not; |
2420 | | * |
2421 | | * Enum initially has no enum values in it (and corresponds to enum forward |
2422 | | * declaration). Enumerator values can be added by btf__add_enum64_value() |
2423 | | * immediately after btf__add_enum64() succeeds. |
2424 | | * |
2425 | | * Returns: |
2426 | | * - >0, type ID of newly added BTF type; |
2427 | | * - <0, on error. |
2428 | | */ |
2429 | | int btf__add_enum64(struct btf *btf, const char *name, __u32 byte_sz, |
2430 | | bool is_signed) |
2431 | 0 | { |
2432 | 0 | return btf_add_enum_common(btf, name, byte_sz, is_signed, |
2433 | 0 | BTF_KIND_ENUM64); |
2434 | 0 | } |
2435 | | |
2436 | | /* |
2437 | | * Append new enum value for the current ENUM64 type with: |
2438 | | * - *name* - name of the enumerator value, can't be NULL or empty; |
2439 | | * - *value* - integer value corresponding to enum value *name*; |
2440 | | * Returns: |
2441 | | * - 0, on success; |
2442 | | * - <0, on error. |
2443 | | */ |
2444 | | int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value) |
2445 | 0 | { |
2446 | 0 | struct btf_enum64 *v; |
2447 | 0 | struct btf_type *t; |
2448 | 0 | int sz, name_off; |
2449 | | |
2450 | | /* last type should be BTF_KIND_ENUM64 */ |
2451 | 0 | if (btf->nr_types == 0) |
2452 | 0 | return libbpf_err(-EINVAL); |
2453 | 0 | t = btf_last_type(btf); |
2454 | 0 | if (!btf_is_enum64(t)) |
2455 | 0 | return libbpf_err(-EINVAL); |
2456 | | |
2457 | | /* non-empty name */ |
2458 | 0 | if (!name || !name[0]) |
2459 | 0 | return libbpf_err(-EINVAL); |
2460 | | |
2461 | | /* decompose and invalidate raw data */ |
2462 | 0 | if (btf_ensure_modifiable(btf)) |
2463 | 0 | return libbpf_err(-ENOMEM); |
2464 | | |
2465 | 0 | sz = sizeof(struct btf_enum64); |
2466 | 0 | v = btf_add_type_mem(btf, sz); |
2467 | 0 | if (!v) |
2468 | 0 | return libbpf_err(-ENOMEM); |
2469 | | |
2470 | 0 | name_off = btf__add_str(btf, name); |
2471 | 0 | if (name_off < 0) |
2472 | 0 | return name_off; |
2473 | | |
2474 | 0 | v->name_off = name_off; |
2475 | 0 | v->val_lo32 = (__u32)value; |
2476 | 0 | v->val_hi32 = value >> 32; |
2477 | | |
2478 | | /* update parent type's vlen */ |
2479 | 0 | t = btf_last_type(btf); |
2480 | 0 | btf_type_inc_vlen(t); |
2481 | |
|
2482 | 0 | btf->hdr->type_len += sz; |
2483 | 0 | btf->hdr->str_off += sz; |
2484 | 0 | return 0; |
2485 | 0 | } |
2486 | | |
2487 | | /* |
2488 | | * Append new BTF_KIND_FWD type with: |
2489 | | * - *name*, non-empty/non-NULL name; |
2490 | | * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT, |
2491 | | * BTF_FWD_UNION, or BTF_FWD_ENUM; |
2492 | | * Returns: |
2493 | | * - >0, type ID of newly added BTF type; |
2494 | | * - <0, on error. |
2495 | | */ |
2496 | | int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind) |
2497 | 0 | { |
2498 | 0 | if (!name || !name[0]) |
2499 | 0 | return libbpf_err(-EINVAL); |
2500 | | |
2501 | 0 | switch (fwd_kind) { |
2502 | 0 | case BTF_FWD_STRUCT: |
2503 | 0 | case BTF_FWD_UNION: { |
2504 | 0 | struct btf_type *t; |
2505 | 0 | int id; |
2506 | |
|
2507 | 0 | id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0); |
2508 | 0 | if (id <= 0) |
2509 | 0 | return id; |
2510 | 0 | t = btf_type_by_id(btf, id); |
2511 | 0 | t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION); |
2512 | 0 | return id; |
2513 | 0 | } |
2514 | 0 | case BTF_FWD_ENUM: |
2515 | | /* enum forward in BTF currently is just an enum with no enum |
2516 | | * values; we also assume a standard 4-byte size for it |
2517 | | */ |
2518 | 0 | return btf__add_enum(btf, name, sizeof(int)); |
2519 | 0 | default: |
2520 | 0 | return libbpf_err(-EINVAL); |
2521 | 0 | } |
2522 | 0 | } |
2523 | | |
2524 | | /* |
2525 | | * Append new BTF_KING_TYPEDEF type with: |
2526 | | * - *name*, non-empty/non-NULL name; |
2527 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2528 | | * Returns: |
2529 | | * - >0, type ID of newly added BTF type; |
2530 | | * - <0, on error. |
2531 | | */ |
2532 | | int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id) |
2533 | 0 | { |
2534 | 0 | if (!name || !name[0]) |
2535 | 0 | return libbpf_err(-EINVAL); |
2536 | | |
2537 | 0 | return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id); |
2538 | 0 | } |
2539 | | |
2540 | | /* |
2541 | | * Append new BTF_KIND_VOLATILE type with: |
2542 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2543 | | * Returns: |
2544 | | * - >0, type ID of newly added BTF type; |
2545 | | * - <0, on error. |
2546 | | */ |
2547 | | int btf__add_volatile(struct btf *btf, int ref_type_id) |
2548 | 0 | { |
2549 | 0 | return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id); |
2550 | 0 | } |
2551 | | |
2552 | | /* |
2553 | | * Append new BTF_KIND_CONST type with: |
2554 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2555 | | * Returns: |
2556 | | * - >0, type ID of newly added BTF type; |
2557 | | * - <0, on error. |
2558 | | */ |
2559 | | int btf__add_const(struct btf *btf, int ref_type_id) |
2560 | 0 | { |
2561 | 0 | return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id); |
2562 | 0 | } |
2563 | | |
2564 | | /* |
2565 | | * Append new BTF_KIND_RESTRICT type with: |
2566 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2567 | | * Returns: |
2568 | | * - >0, type ID of newly added BTF type; |
2569 | | * - <0, on error. |
2570 | | */ |
2571 | | int btf__add_restrict(struct btf *btf, int ref_type_id) |
2572 | 0 | { |
2573 | 0 | return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id); |
2574 | 0 | } |
2575 | | |
2576 | | /* |
2577 | | * Append new BTF_KIND_TYPE_TAG type with: |
2578 | | * - *value*, non-empty/non-NULL tag value; |
2579 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2580 | | * Returns: |
2581 | | * - >0, type ID of newly added BTF type; |
2582 | | * - <0, on error. |
2583 | | */ |
2584 | | int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id) |
2585 | 0 | { |
2586 | 0 | if (!value || !value[0]) |
2587 | 0 | return libbpf_err(-EINVAL); |
2588 | | |
2589 | 0 | return btf_add_ref_kind(btf, BTF_KIND_TYPE_TAG, value, ref_type_id); |
2590 | 0 | } |
2591 | | |
2592 | | /* |
2593 | | * Append new BTF_KIND_FUNC type with: |
2594 | | * - *name*, non-empty/non-NULL name; |
2595 | | * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet; |
2596 | | * Returns: |
2597 | | * - >0, type ID of newly added BTF type; |
2598 | | * - <0, on error. |
2599 | | */ |
2600 | | int btf__add_func(struct btf *btf, const char *name, |
2601 | | enum btf_func_linkage linkage, int proto_type_id) |
2602 | 0 | { |
2603 | 0 | int id; |
2604 | |
|
2605 | 0 | if (!name || !name[0]) |
2606 | 0 | return libbpf_err(-EINVAL); |
2607 | 0 | if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL && |
2608 | 0 | linkage != BTF_FUNC_EXTERN) |
2609 | 0 | return libbpf_err(-EINVAL); |
2610 | | |
2611 | 0 | id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id); |
2612 | 0 | if (id > 0) { |
2613 | 0 | struct btf_type *t = btf_type_by_id(btf, id); |
2614 | |
|
2615 | 0 | t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0); |
2616 | 0 | } |
2617 | 0 | return libbpf_err(id); |
2618 | 0 | } |
2619 | | |
2620 | | /* |
2621 | | * Append new BTF_KIND_FUNC_PROTO with: |
2622 | | * - *ret_type_id* - type ID for return result of a function. |
2623 | | * |
2624 | | * Function prototype initially has no arguments, but they can be added by |
2625 | | * btf__add_func_param() one by one, immediately after |
2626 | | * btf__add_func_proto() succeeded. |
2627 | | * |
2628 | | * Returns: |
2629 | | * - >0, type ID of newly added BTF type; |
2630 | | * - <0, on error. |
2631 | | */ |
2632 | | int btf__add_func_proto(struct btf *btf, int ret_type_id) |
2633 | 0 | { |
2634 | 0 | struct btf_type *t; |
2635 | 0 | int sz; |
2636 | |
|
2637 | 0 | if (validate_type_id(ret_type_id)) |
2638 | 0 | return libbpf_err(-EINVAL); |
2639 | | |
2640 | 0 | if (btf_ensure_modifiable(btf)) |
2641 | 0 | return libbpf_err(-ENOMEM); |
2642 | | |
2643 | 0 | sz = sizeof(struct btf_type); |
2644 | 0 | t = btf_add_type_mem(btf, sz); |
2645 | 0 | if (!t) |
2646 | 0 | return libbpf_err(-ENOMEM); |
2647 | | |
2648 | | /* start out with vlen=0; this will be adjusted when adding enum |
2649 | | * values, if necessary |
2650 | | */ |
2651 | 0 | t->name_off = 0; |
2652 | 0 | t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0); |
2653 | 0 | t->type = ret_type_id; |
2654 | |
|
2655 | 0 | return btf_commit_type(btf, sz); |
2656 | 0 | } |
2657 | | |
2658 | | /* |
2659 | | * Append new function parameter for current FUNC_PROTO type with: |
2660 | | * - *name* - parameter name, can be NULL or empty; |
2661 | | * - *type_id* - type ID describing the type of the parameter. |
2662 | | * Returns: |
2663 | | * - 0, on success; |
2664 | | * - <0, on error. |
2665 | | */ |
2666 | | int btf__add_func_param(struct btf *btf, const char *name, int type_id) |
2667 | 0 | { |
2668 | 0 | struct btf_type *t; |
2669 | 0 | struct btf_param *p; |
2670 | 0 | int sz, name_off = 0; |
2671 | |
|
2672 | 0 | if (validate_type_id(type_id)) |
2673 | 0 | return libbpf_err(-EINVAL); |
2674 | | |
2675 | | /* last type should be BTF_KIND_FUNC_PROTO */ |
2676 | 0 | if (btf->nr_types == 0) |
2677 | 0 | return libbpf_err(-EINVAL); |
2678 | 0 | t = btf_last_type(btf); |
2679 | 0 | if (!btf_is_func_proto(t)) |
2680 | 0 | return libbpf_err(-EINVAL); |
2681 | | |
2682 | | /* decompose and invalidate raw data */ |
2683 | 0 | if (btf_ensure_modifiable(btf)) |
2684 | 0 | return libbpf_err(-ENOMEM); |
2685 | | |
2686 | 0 | sz = sizeof(struct btf_param); |
2687 | 0 | p = btf_add_type_mem(btf, sz); |
2688 | 0 | if (!p) |
2689 | 0 | return libbpf_err(-ENOMEM); |
2690 | | |
2691 | 0 | if (name && name[0]) { |
2692 | 0 | name_off = btf__add_str(btf, name); |
2693 | 0 | if (name_off < 0) |
2694 | 0 | return name_off; |
2695 | 0 | } |
2696 | | |
2697 | 0 | p->name_off = name_off; |
2698 | 0 | p->type = type_id; |
2699 | | |
2700 | | /* update parent type's vlen */ |
2701 | 0 | t = btf_last_type(btf); |
2702 | 0 | btf_type_inc_vlen(t); |
2703 | |
|
2704 | 0 | btf->hdr->type_len += sz; |
2705 | 0 | btf->hdr->str_off += sz; |
2706 | 0 | return 0; |
2707 | 0 | } |
2708 | | |
2709 | | /* |
2710 | | * Append new BTF_KIND_VAR type with: |
2711 | | * - *name* - non-empty/non-NULL name; |
2712 | | * - *linkage* - variable linkage, one of BTF_VAR_STATIC, |
2713 | | * BTF_VAR_GLOBAL_ALLOCATED, or BTF_VAR_GLOBAL_EXTERN; |
2714 | | * - *type_id* - type ID of the type describing the type of the variable. |
2715 | | * Returns: |
2716 | | * - >0, type ID of newly added BTF type; |
2717 | | * - <0, on error. |
2718 | | */ |
2719 | | int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id) |
2720 | 526 | { |
2721 | 526 | struct btf_type *t; |
2722 | 526 | struct btf_var *v; |
2723 | 526 | int sz, name_off; |
2724 | | |
2725 | | /* non-empty name */ |
2726 | 526 | if (!name || !name[0]) |
2727 | 0 | return libbpf_err(-EINVAL); |
2728 | 526 | if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED && |
2729 | 526 | linkage != BTF_VAR_GLOBAL_EXTERN) |
2730 | 0 | return libbpf_err(-EINVAL); |
2731 | 526 | if (validate_type_id(type_id)) |
2732 | 0 | return libbpf_err(-EINVAL); |
2733 | | |
2734 | | /* deconstruct BTF, if necessary, and invalidate raw_data */ |
2735 | 526 | if (btf_ensure_modifiable(btf)) |
2736 | 0 | return libbpf_err(-ENOMEM); |
2737 | | |
2738 | 526 | sz = sizeof(struct btf_type) + sizeof(struct btf_var); |
2739 | 526 | t = btf_add_type_mem(btf, sz); |
2740 | 526 | if (!t) |
2741 | 0 | return libbpf_err(-ENOMEM); |
2742 | | |
2743 | 526 | name_off = btf__add_str(btf, name); |
2744 | 526 | if (name_off < 0) |
2745 | 0 | return name_off; |
2746 | | |
2747 | 526 | t->name_off = name_off; |
2748 | 526 | t->info = btf_type_info(BTF_KIND_VAR, 0, 0); |
2749 | 526 | t->type = type_id; |
2750 | | |
2751 | 526 | v = btf_var(t); |
2752 | 526 | v->linkage = linkage; |
2753 | | |
2754 | 526 | return btf_commit_type(btf, sz); |
2755 | 526 | } |
2756 | | |
2757 | | /* |
2758 | | * Append new BTF_KIND_DATASEC type with: |
2759 | | * - *name* - non-empty/non-NULL name; |
2760 | | * - *byte_sz* - data section size, in bytes. |
2761 | | * |
2762 | | * Data section is initially empty. Variables info can be added with |
2763 | | * btf__add_datasec_var_info() calls, after btf__add_datasec() succeeds. |
2764 | | * |
2765 | | * Returns: |
2766 | | * - >0, type ID of newly added BTF type; |
2767 | | * - <0, on error. |
2768 | | */ |
2769 | | int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz) |
2770 | 0 | { |
2771 | 0 | struct btf_type *t; |
2772 | 0 | int sz, name_off; |
2773 | | |
2774 | | /* non-empty name */ |
2775 | 0 | if (!name || !name[0]) |
2776 | 0 | return libbpf_err(-EINVAL); |
2777 | | |
2778 | 0 | if (btf_ensure_modifiable(btf)) |
2779 | 0 | return libbpf_err(-ENOMEM); |
2780 | | |
2781 | 0 | sz = sizeof(struct btf_type); |
2782 | 0 | t = btf_add_type_mem(btf, sz); |
2783 | 0 | if (!t) |
2784 | 0 | return libbpf_err(-ENOMEM); |
2785 | | |
2786 | 0 | name_off = btf__add_str(btf, name); |
2787 | 0 | if (name_off < 0) |
2788 | 0 | return name_off; |
2789 | | |
2790 | | /* start with vlen=0, which will be update as var_secinfos are added */ |
2791 | 0 | t->name_off = name_off; |
2792 | 0 | t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0); |
2793 | 0 | t->size = byte_sz; |
2794 | |
|
2795 | 0 | return btf_commit_type(btf, sz); |
2796 | 0 | } |
2797 | | |
2798 | | /* |
2799 | | * Append new data section variable information entry for current DATASEC type: |
2800 | | * - *var_type_id* - type ID, describing type of the variable; |
2801 | | * - *offset* - variable offset within data section, in bytes; |
2802 | | * - *byte_sz* - variable size, in bytes. |
2803 | | * |
2804 | | * Returns: |
2805 | | * - 0, on success; |
2806 | | * - <0, on error. |
2807 | | */ |
2808 | | int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz) |
2809 | 0 | { |
2810 | 0 | struct btf_type *t; |
2811 | 0 | struct btf_var_secinfo *v; |
2812 | 0 | int sz; |
2813 | | |
2814 | | /* last type should be BTF_KIND_DATASEC */ |
2815 | 0 | if (btf->nr_types == 0) |
2816 | 0 | return libbpf_err(-EINVAL); |
2817 | 0 | t = btf_last_type(btf); |
2818 | 0 | if (!btf_is_datasec(t)) |
2819 | 0 | return libbpf_err(-EINVAL); |
2820 | | |
2821 | 0 | if (validate_type_id(var_type_id)) |
2822 | 0 | return libbpf_err(-EINVAL); |
2823 | | |
2824 | | /* decompose and invalidate raw data */ |
2825 | 0 | if (btf_ensure_modifiable(btf)) |
2826 | 0 | return libbpf_err(-ENOMEM); |
2827 | | |
2828 | 0 | sz = sizeof(struct btf_var_secinfo); |
2829 | 0 | v = btf_add_type_mem(btf, sz); |
2830 | 0 | if (!v) |
2831 | 0 | return libbpf_err(-ENOMEM); |
2832 | | |
2833 | 0 | v->type = var_type_id; |
2834 | 0 | v->offset = offset; |
2835 | 0 | v->size = byte_sz; |
2836 | | |
2837 | | /* update parent type's vlen */ |
2838 | 0 | t = btf_last_type(btf); |
2839 | 0 | btf_type_inc_vlen(t); |
2840 | |
|
2841 | 0 | btf->hdr->type_len += sz; |
2842 | 0 | btf->hdr->str_off += sz; |
2843 | 0 | return 0; |
2844 | 0 | } |
2845 | | |
2846 | | /* |
2847 | | * Append new BTF_KIND_DECL_TAG type with: |
2848 | | * - *value* - non-empty/non-NULL string; |
2849 | | * - *ref_type_id* - referenced type ID, it might not exist yet; |
2850 | | * - *component_idx* - -1 for tagging reference type, otherwise struct/union |
2851 | | * member or function argument index; |
2852 | | * Returns: |
2853 | | * - >0, type ID of newly added BTF type; |
2854 | | * - <0, on error. |
2855 | | */ |
2856 | | int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id, |
2857 | | int component_idx) |
2858 | 0 | { |
2859 | 0 | struct btf_type *t; |
2860 | 0 | int sz, value_off; |
2861 | |
|
2862 | 0 | if (!value || !value[0] || component_idx < -1) |
2863 | 0 | return libbpf_err(-EINVAL); |
2864 | | |
2865 | 0 | if (validate_type_id(ref_type_id)) |
2866 | 0 | return libbpf_err(-EINVAL); |
2867 | | |
2868 | 0 | if (btf_ensure_modifiable(btf)) |
2869 | 0 | return libbpf_err(-ENOMEM); |
2870 | | |
2871 | 0 | sz = sizeof(struct btf_type) + sizeof(struct btf_decl_tag); |
2872 | 0 | t = btf_add_type_mem(btf, sz); |
2873 | 0 | if (!t) |
2874 | 0 | return libbpf_err(-ENOMEM); |
2875 | | |
2876 | 0 | value_off = btf__add_str(btf, value); |
2877 | 0 | if (value_off < 0) |
2878 | 0 | return value_off; |
2879 | | |
2880 | 0 | t->name_off = value_off; |
2881 | 0 | t->info = btf_type_info(BTF_KIND_DECL_TAG, 0, false); |
2882 | 0 | t->type = ref_type_id; |
2883 | 0 | btf_decl_tag(t)->component_idx = component_idx; |
2884 | |
|
2885 | 0 | return btf_commit_type(btf, sz); |
2886 | 0 | } |
2887 | | |
2888 | | struct btf_ext_sec_info_param { |
2889 | | __u32 off; |
2890 | | __u32 len; |
2891 | | __u32 min_rec_size; |
2892 | | struct btf_ext_info *ext_info; |
2893 | | const char *desc; |
2894 | | }; |
2895 | | |
2896 | | /* |
2897 | | * Parse a single info subsection of the BTF.ext info data: |
2898 | | * - validate subsection structure and elements |
2899 | | * - save info subsection start and sizing details in struct btf_ext |
2900 | | * - endian-independent operation, for calling before byte-swapping |
2901 | | */ |
2902 | | static int btf_ext_parse_sec_info(struct btf_ext *btf_ext, |
2903 | | struct btf_ext_sec_info_param *ext_sec, |
2904 | | bool is_native) |
2905 | 386 | { |
2906 | 386 | const struct btf_ext_info_sec *sinfo; |
2907 | 386 | struct btf_ext_info *ext_info; |
2908 | 386 | __u32 info_left, record_size; |
2909 | 386 | size_t sec_cnt = 0; |
2910 | 386 | void *info; |
2911 | | |
2912 | 386 | if (ext_sec->len == 0) |
2913 | 146 | return 0; |
2914 | | |
2915 | 240 | if (ext_sec->off & 0x03) { |
2916 | 8 | pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n", |
2917 | 8 | ext_sec->desc); |
2918 | 8 | return -EINVAL; |
2919 | 8 | } |
2920 | | |
2921 | | /* The start of the info sec (including the __u32 record_size). */ |
2922 | 232 | info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off; |
2923 | 232 | info_left = ext_sec->len; |
2924 | | |
2925 | 232 | if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) { |
2926 | 31 | pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n", |
2927 | 31 | ext_sec->desc, ext_sec->off, ext_sec->len); |
2928 | 31 | return -EINVAL; |
2929 | 31 | } |
2930 | | |
2931 | | /* At least a record size */ |
2932 | 201 | if (info_left < sizeof(__u32)) { |
2933 | 1 | pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc); |
2934 | 1 | return -EINVAL; |
2935 | 1 | } |
2936 | | |
2937 | | /* The record size needs to meet either the minimum standard or, when |
2938 | | * handling non-native endianness data, the exact standard so as |
2939 | | * to allow safe byte-swapping. |
2940 | | */ |
2941 | 200 | record_size = is_native ? *(__u32 *)info : bswap_32(*(__u32 *)info); |
2942 | 200 | if (record_size < ext_sec->min_rec_size || |
2943 | 200 | (!is_native && record_size != ext_sec->min_rec_size) || |
2944 | 200 | record_size & 0x03) { |
2945 | 19 | pr_debug("%s section in .BTF.ext has invalid record size %u\n", |
2946 | 19 | ext_sec->desc, record_size); |
2947 | 19 | return -EINVAL; |
2948 | 19 | } |
2949 | | |
2950 | 181 | sinfo = info + sizeof(__u32); |
2951 | 181 | info_left -= sizeof(__u32); |
2952 | | |
2953 | | /* If no records, return failure now so .BTF.ext won't be used. */ |
2954 | 181 | if (!info_left) { |
2955 | 2 | pr_debug("%s section in .BTF.ext has no records\n", ext_sec->desc); |
2956 | 2 | return -EINVAL; |
2957 | 2 | } |
2958 | | |
2959 | 399 | while (info_left) { |
2960 | 347 | unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec); |
2961 | 347 | __u64 total_record_size; |
2962 | 347 | __u32 num_records; |
2963 | | |
2964 | 347 | if (info_left < sec_hdrlen) { |
2965 | 2 | pr_debug("%s section header is not found in .BTF.ext\n", |
2966 | 2 | ext_sec->desc); |
2967 | 2 | return -EINVAL; |
2968 | 2 | } |
2969 | | |
2970 | 345 | num_records = is_native ? sinfo->num_info : bswap_32(sinfo->num_info); |
2971 | 345 | if (num_records == 0) { |
2972 | 1 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
2973 | 1 | ext_sec->desc); |
2974 | 1 | return -EINVAL; |
2975 | 1 | } |
2976 | | |
2977 | 344 | total_record_size = sec_hdrlen + (__u64)num_records * record_size; |
2978 | 344 | if (info_left < total_record_size) { |
2979 | 124 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
2980 | 124 | ext_sec->desc); |
2981 | 124 | return -EINVAL; |
2982 | 124 | } |
2983 | | |
2984 | 220 | info_left -= total_record_size; |
2985 | 220 | sinfo = (void *)sinfo + total_record_size; |
2986 | 220 | sec_cnt++; |
2987 | 220 | } |
2988 | | |
2989 | 52 | ext_info = ext_sec->ext_info; |
2990 | 52 | ext_info->len = ext_sec->len - sizeof(__u32); |
2991 | 52 | ext_info->rec_size = record_size; |
2992 | 52 | ext_info->info = info + sizeof(__u32); |
2993 | 52 | ext_info->sec_cnt = sec_cnt; |
2994 | | |
2995 | 52 | return 0; |
2996 | 179 | } |
2997 | | |
2998 | | /* Parse all info secs in the BTF.ext info data */ |
2999 | | static int btf_ext_parse_info(struct btf_ext *btf_ext, bool is_native) |
3000 | 229 | { |
3001 | 229 | struct btf_ext_sec_info_param func_info = { |
3002 | 229 | .off = btf_ext->hdr->func_info_off, |
3003 | 229 | .len = btf_ext->hdr->func_info_len, |
3004 | 229 | .min_rec_size = sizeof(struct bpf_func_info_min), |
3005 | 229 | .ext_info = &btf_ext->func_info, |
3006 | 229 | .desc = "func_info" |
3007 | 229 | }; |
3008 | 229 | struct btf_ext_sec_info_param line_info = { |
3009 | 229 | .off = btf_ext->hdr->line_info_off, |
3010 | 229 | .len = btf_ext->hdr->line_info_len, |
3011 | 229 | .min_rec_size = sizeof(struct bpf_line_info_min), |
3012 | 229 | .ext_info = &btf_ext->line_info, |
3013 | 229 | .desc = "line_info", |
3014 | 229 | }; |
3015 | 229 | struct btf_ext_sec_info_param core_relo = { |
3016 | 229 | .off = btf_ext->hdr->core_relo_off, |
3017 | 229 | .len = btf_ext->hdr->core_relo_len, |
3018 | 229 | .min_rec_size = sizeof(struct bpf_core_relo), |
3019 | 229 | .ext_info = &btf_ext->core_relo_info, |
3020 | 229 | .desc = "core_relo", |
3021 | 229 | }; |
3022 | 229 | int err; |
3023 | | |
3024 | 229 | err = btf_ext_parse_sec_info(btf_ext, &func_info, is_native); |
3025 | 229 | if (err) |
3026 | 144 | return err; |
3027 | | |
3028 | 85 | err = btf_ext_parse_sec_info(btf_ext, &line_info, is_native); |
3029 | 85 | if (err) |
3030 | 12 | return err; |
3031 | | |
3032 | 73 | if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) |
3033 | 1 | return 0; /* skip core relos parsing */ |
3034 | | |
3035 | 72 | err = btf_ext_parse_sec_info(btf_ext, &core_relo, is_native); |
3036 | 72 | if (err) |
3037 | 32 | return err; |
3038 | | |
3039 | 40 | return 0; |
3040 | 72 | } |
3041 | | |
3042 | | /* Swap byte-order of BTF.ext header with any endianness */ |
3043 | | static void btf_ext_bswap_hdr(struct btf_ext_header *h) |
3044 | 0 | { |
3045 | 0 | bool is_native = h->magic == BTF_MAGIC; |
3046 | 0 | __u32 hdr_len; |
3047 | |
|
3048 | 0 | hdr_len = is_native ? h->hdr_len : bswap_32(h->hdr_len); |
3049 | |
|
3050 | 0 | h->magic = bswap_16(h->magic); |
3051 | 0 | h->hdr_len = bswap_32(h->hdr_len); |
3052 | 0 | h->func_info_off = bswap_32(h->func_info_off); |
3053 | 0 | h->func_info_len = bswap_32(h->func_info_len); |
3054 | 0 | h->line_info_off = bswap_32(h->line_info_off); |
3055 | 0 | h->line_info_len = bswap_32(h->line_info_len); |
3056 | |
|
3057 | 0 | if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) |
3058 | 0 | return; |
3059 | | |
3060 | 0 | h->core_relo_off = bswap_32(h->core_relo_off); |
3061 | 0 | h->core_relo_len = bswap_32(h->core_relo_len); |
3062 | 0 | } |
3063 | | |
3064 | | /* Swap byte-order of generic info subsection */ |
3065 | | static void btf_ext_bswap_info_sec(void *info, __u32 len, bool is_native, |
3066 | | info_rec_bswap_fn bswap_fn) |
3067 | 0 | { |
3068 | 0 | struct btf_ext_info_sec *sec; |
3069 | 0 | __u32 info_left, rec_size, *rs; |
3070 | |
|
3071 | 0 | if (len == 0) |
3072 | 0 | return; |
3073 | | |
3074 | 0 | rs = info; /* info record size */ |
3075 | 0 | rec_size = is_native ? *rs : bswap_32(*rs); |
3076 | 0 | *rs = bswap_32(*rs); |
3077 | |
|
3078 | 0 | sec = info + sizeof(__u32); /* info sec #1 */ |
3079 | 0 | info_left = len - sizeof(__u32); |
3080 | 0 | while (info_left) { |
3081 | 0 | unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec); |
3082 | 0 | __u32 i, num_recs; |
3083 | 0 | void *p; |
3084 | |
|
3085 | 0 | num_recs = is_native ? sec->num_info : bswap_32(sec->num_info); |
3086 | 0 | sec->sec_name_off = bswap_32(sec->sec_name_off); |
3087 | 0 | sec->num_info = bswap_32(sec->num_info); |
3088 | 0 | p = sec->data; /* info rec #1 */ |
3089 | 0 | for (i = 0; i < num_recs; i++, p += rec_size) |
3090 | 0 | bswap_fn(p); |
3091 | 0 | sec = p; |
3092 | 0 | info_left -= sec_hdrlen + (__u64)rec_size * num_recs; |
3093 | 0 | } |
3094 | 0 | } |
3095 | | |
3096 | | /* |
3097 | | * Swap byte-order of all info data in a BTF.ext section |
3098 | | * - requires BTF.ext hdr in native endianness |
3099 | | */ |
3100 | | static void btf_ext_bswap_info(struct btf_ext *btf_ext, void *data) |
3101 | 0 | { |
3102 | 0 | const bool is_native = btf_ext->swapped_endian; |
3103 | 0 | const struct btf_ext_header *h = data; |
3104 | 0 | void *info; |
3105 | | |
3106 | | /* Swap func_info subsection byte-order */ |
3107 | 0 | info = data + h->hdr_len + h->func_info_off; |
3108 | 0 | btf_ext_bswap_info_sec(info, h->func_info_len, is_native, |
3109 | 0 | (info_rec_bswap_fn)bpf_func_info_bswap); |
3110 | | |
3111 | | /* Swap line_info subsection byte-order */ |
3112 | 0 | info = data + h->hdr_len + h->line_info_off; |
3113 | 0 | btf_ext_bswap_info_sec(info, h->line_info_len, is_native, |
3114 | 0 | (info_rec_bswap_fn)bpf_line_info_bswap); |
3115 | | |
3116 | | /* Swap core_relo subsection byte-order (if present) */ |
3117 | 0 | if (h->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) |
3118 | 0 | return; |
3119 | | |
3120 | 0 | info = data + h->hdr_len + h->core_relo_off; |
3121 | 0 | btf_ext_bswap_info_sec(info, h->core_relo_len, is_native, |
3122 | 0 | (info_rec_bswap_fn)bpf_core_relo_bswap); |
3123 | 0 | } |
3124 | | |
3125 | | /* Parse hdr data and info sections: check and convert to native endianness */ |
3126 | | static int btf_ext_parse(struct btf_ext *btf_ext) |
3127 | 295 | { |
3128 | 295 | __u32 hdr_len, data_size = btf_ext->data_size; |
3129 | 295 | struct btf_ext_header *hdr = btf_ext->hdr; |
3130 | 295 | bool swapped_endian = false; |
3131 | 295 | int err; |
3132 | | |
3133 | 295 | if (data_size < offsetofend(struct btf_ext_header, hdr_len)) { |
3134 | 3 | pr_debug("BTF.ext header too short\n"); |
3135 | 3 | return -EINVAL; |
3136 | 3 | } |
3137 | | |
3138 | 292 | hdr_len = hdr->hdr_len; |
3139 | 292 | if (hdr->magic == bswap_16(BTF_MAGIC)) { |
3140 | 1 | swapped_endian = true; |
3141 | 1 | hdr_len = bswap_32(hdr_len); |
3142 | 291 | } else if (hdr->magic != BTF_MAGIC) { |
3143 | 39 | pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); |
3144 | 39 | return -EINVAL; |
3145 | 39 | } |
3146 | | |
3147 | | /* Ensure known version of structs, current BTF_VERSION == 1 */ |
3148 | 253 | if (hdr->version != 1) { |
3149 | 9 | pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); |
3150 | 9 | return -ENOTSUP; |
3151 | 9 | } |
3152 | | |
3153 | 244 | if (hdr->flags) { |
3154 | 8 | pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags); |
3155 | 8 | return -ENOTSUP; |
3156 | 8 | } |
3157 | | |
3158 | 236 | if (data_size < hdr_len) { |
3159 | 1 | pr_debug("BTF.ext header not found\n"); |
3160 | 1 | return -EINVAL; |
3161 | 235 | } else if (data_size == hdr_len) { |
3162 | 1 | pr_debug("BTF.ext has no data\n"); |
3163 | 1 | return -EINVAL; |
3164 | 1 | } |
3165 | | |
3166 | | /* Verify mandatory hdr info details present */ |
3167 | 234 | if (hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { |
3168 | 5 | pr_warn("BTF.ext header missing func_info, line_info\n"); |
3169 | 5 | return -EINVAL; |
3170 | 5 | } |
3171 | | |
3172 | | /* Keep hdr native byte-order in memory for introspection */ |
3173 | 229 | if (swapped_endian) |
3174 | 0 | btf_ext_bswap_hdr(btf_ext->hdr); |
3175 | | |
3176 | | /* Validate info subsections and cache key metadata */ |
3177 | 229 | err = btf_ext_parse_info(btf_ext, !swapped_endian); |
3178 | 229 | if (err) |
3179 | 188 | return err; |
3180 | | |
3181 | | /* Keep infos native byte-order in memory for introspection */ |
3182 | 41 | if (swapped_endian) |
3183 | 0 | btf_ext_bswap_info(btf_ext, btf_ext->data); |
3184 | | |
3185 | | /* |
3186 | | * Set btf_ext->swapped_endian only after all header and info data has |
3187 | | * been swapped, helping bswap functions determine if their data are |
3188 | | * in native byte-order when called. |
3189 | | */ |
3190 | 41 | btf_ext->swapped_endian = swapped_endian; |
3191 | 41 | return 0; |
3192 | 229 | } |
3193 | | |
3194 | | void btf_ext__free(struct btf_ext *btf_ext) |
3195 | 11.9k | { |
3196 | 11.9k | if (IS_ERR_OR_NULL(btf_ext)) |
3197 | 11.6k | return; |
3198 | 295 | free(btf_ext->func_info.sec_idxs); |
3199 | 295 | free(btf_ext->line_info.sec_idxs); |
3200 | 295 | free(btf_ext->core_relo_info.sec_idxs); |
3201 | 295 | free(btf_ext->data); |
3202 | 295 | free(btf_ext->data_swapped); |
3203 | 295 | free(btf_ext); |
3204 | 295 | } |
3205 | | |
3206 | | struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) |
3207 | 295 | { |
3208 | 295 | struct btf_ext *btf_ext; |
3209 | 295 | int err; |
3210 | | |
3211 | 295 | btf_ext = calloc(1, sizeof(struct btf_ext)); |
3212 | 295 | if (!btf_ext) |
3213 | 0 | return libbpf_err_ptr(-ENOMEM); |
3214 | | |
3215 | 295 | btf_ext->data_size = size; |
3216 | 295 | btf_ext->data = malloc(size); |
3217 | 295 | if (!btf_ext->data) { |
3218 | 0 | err = -ENOMEM; |
3219 | 0 | goto done; |
3220 | 0 | } |
3221 | 295 | memcpy(btf_ext->data, data, size); |
3222 | | |
3223 | 295 | err = btf_ext_parse(btf_ext); |
3224 | | |
3225 | 295 | done: |
3226 | 295 | if (err) { |
3227 | 254 | btf_ext__free(btf_ext); |
3228 | 254 | return libbpf_err_ptr(err); |
3229 | 254 | } |
3230 | | |
3231 | 41 | return btf_ext; |
3232 | 295 | } |
3233 | | |
3234 | | static void *btf_ext_raw_data(const struct btf_ext *btf_ext_ro, bool swap_endian) |
3235 | 0 | { |
3236 | 0 | struct btf_ext *btf_ext = (struct btf_ext *)btf_ext_ro; |
3237 | 0 | const __u32 data_sz = btf_ext->data_size; |
3238 | 0 | void *data; |
3239 | | |
3240 | | /* Return native data (always present) or swapped data if present */ |
3241 | 0 | if (!swap_endian) |
3242 | 0 | return btf_ext->data; |
3243 | 0 | else if (btf_ext->data_swapped) |
3244 | 0 | return btf_ext->data_swapped; |
3245 | | |
3246 | | /* Recreate missing swapped data, then cache and return */ |
3247 | 0 | data = calloc(1, data_sz); |
3248 | 0 | if (!data) |
3249 | 0 | return NULL; |
3250 | 0 | memcpy(data, btf_ext->data, data_sz); |
3251 | |
|
3252 | 0 | btf_ext_bswap_info(btf_ext, data); |
3253 | 0 | btf_ext_bswap_hdr(data); |
3254 | 0 | btf_ext->data_swapped = data; |
3255 | 0 | return data; |
3256 | 0 | } |
3257 | | |
3258 | | const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size) |
3259 | 0 | { |
3260 | 0 | void *data; |
3261 | |
|
3262 | 0 | data = btf_ext_raw_data(btf_ext, btf_ext->swapped_endian); |
3263 | 0 | if (!data) |
3264 | 0 | return errno = ENOMEM, NULL; |
3265 | | |
3266 | 0 | *size = btf_ext->data_size; |
3267 | 0 | return data; |
3268 | 0 | } |
3269 | | |
3270 | | __attribute__((alias("btf_ext__raw_data"))) |
3271 | | const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size); |
3272 | | |
3273 | | enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext) |
3274 | 0 | { |
3275 | 0 | if (is_host_big_endian()) |
3276 | 0 | return btf_ext->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; |
3277 | 0 | else |
3278 | 0 | return btf_ext->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; |
3279 | 0 | } |
3280 | | |
3281 | | int btf_ext__set_endianness(struct btf_ext *btf_ext, enum btf_endianness endian) |
3282 | 0 | { |
3283 | 0 | if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN) |
3284 | 0 | return libbpf_err(-EINVAL); |
3285 | | |
3286 | 0 | btf_ext->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); |
3287 | |
|
3288 | 0 | if (!btf_ext->swapped_endian) { |
3289 | 0 | free(btf_ext->data_swapped); |
3290 | 0 | btf_ext->data_swapped = NULL; |
3291 | 0 | } |
3292 | 0 | return 0; |
3293 | 0 | } |
3294 | | |
3295 | | struct btf_dedup; |
3296 | | |
3297 | | static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts); |
3298 | | static void btf_dedup_free(struct btf_dedup *d); |
3299 | | static int btf_dedup_prep(struct btf_dedup *d); |
3300 | | static int btf_dedup_strings(struct btf_dedup *d); |
3301 | | static int btf_dedup_prim_types(struct btf_dedup *d); |
3302 | | static int btf_dedup_struct_types(struct btf_dedup *d); |
3303 | | static int btf_dedup_ref_types(struct btf_dedup *d); |
3304 | | static int btf_dedup_resolve_fwds(struct btf_dedup *d); |
3305 | | static int btf_dedup_compact_types(struct btf_dedup *d); |
3306 | | static int btf_dedup_remap_types(struct btf_dedup *d); |
3307 | | |
3308 | | /* |
3309 | | * Deduplicate BTF types and strings. |
3310 | | * |
3311 | | * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF |
3312 | | * section with all BTF type descriptors and string data. It overwrites that |
3313 | | * memory in-place with deduplicated types and strings without any loss of |
3314 | | * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section |
3315 | | * is provided, all the strings referenced from .BTF.ext section are honored |
3316 | | * and updated to point to the right offsets after deduplication. |
3317 | | * |
3318 | | * If function returns with error, type/string data might be garbled and should |
3319 | | * be discarded. |
3320 | | * |
3321 | | * More verbose and detailed description of both problem btf_dedup is solving, |
3322 | | * as well as solution could be found at: |
3323 | | * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html |
3324 | | * |
3325 | | * Problem description and justification |
3326 | | * ===================================== |
3327 | | * |
3328 | | * BTF type information is typically emitted either as a result of conversion |
3329 | | * from DWARF to BTF or directly by compiler. In both cases, each compilation |
3330 | | * unit contains information about a subset of all the types that are used |
3331 | | * in an application. These subsets are frequently overlapping and contain a lot |
3332 | | * of duplicated information when later concatenated together into a single |
3333 | | * binary. This algorithm ensures that each unique type is represented by single |
3334 | | * BTF type descriptor, greatly reducing resulting size of BTF data. |
3335 | | * |
3336 | | * Compilation unit isolation and subsequent duplication of data is not the only |
3337 | | * problem. The same type hierarchy (e.g., struct and all the type that struct |
3338 | | * references) in different compilation units can be represented in BTF to |
3339 | | * various degrees of completeness (or, rather, incompleteness) due to |
3340 | | * struct/union forward declarations. |
3341 | | * |
3342 | | * Let's take a look at an example, that we'll use to better understand the |
3343 | | * problem (and solution). Suppose we have two compilation units, each using |
3344 | | * same `struct S`, but each of them having incomplete type information about |
3345 | | * struct's fields: |
3346 | | * |
3347 | | * // CU #1: |
3348 | | * struct S; |
3349 | | * struct A { |
3350 | | * int a; |
3351 | | * struct A* self; |
3352 | | * struct S* parent; |
3353 | | * }; |
3354 | | * struct B; |
3355 | | * struct S { |
3356 | | * struct A* a_ptr; |
3357 | | * struct B* b_ptr; |
3358 | | * }; |
3359 | | * |
3360 | | * // CU #2: |
3361 | | * struct S; |
3362 | | * struct A; |
3363 | | * struct B { |
3364 | | * int b; |
3365 | | * struct B* self; |
3366 | | * struct S* parent; |
3367 | | * }; |
3368 | | * struct S { |
3369 | | * struct A* a_ptr; |
3370 | | * struct B* b_ptr; |
3371 | | * }; |
3372 | | * |
3373 | | * In case of CU #1, BTF data will know only that `struct B` exist (but no |
3374 | | * more), but will know the complete type information about `struct A`. While |
3375 | | * for CU #2, it will know full type information about `struct B`, but will |
3376 | | * only know about forward declaration of `struct A` (in BTF terms, it will |
3377 | | * have `BTF_KIND_FWD` type descriptor with name `B`). |
3378 | | * |
3379 | | * This compilation unit isolation means that it's possible that there is no |
3380 | | * single CU with complete type information describing structs `S`, `A`, and |
3381 | | * `B`. Also, we might get tons of duplicated and redundant type information. |
3382 | | * |
3383 | | * Additional complication we need to keep in mind comes from the fact that |
3384 | | * types, in general, can form graphs containing cycles, not just DAGs. |
3385 | | * |
3386 | | * While algorithm does deduplication, it also merges and resolves type |
3387 | | * information (unless disabled throught `struct btf_opts`), whenever possible. |
3388 | | * E.g., in the example above with two compilation units having partial type |
3389 | | * information for structs `A` and `B`, the output of algorithm will emit |
3390 | | * a single copy of each BTF type that describes structs `A`, `B`, and `S` |
3391 | | * (as well as type information for `int` and pointers), as if they were defined |
3392 | | * in a single compilation unit as: |
3393 | | * |
3394 | | * struct A { |
3395 | | * int a; |
3396 | | * struct A* self; |
3397 | | * struct S* parent; |
3398 | | * }; |
3399 | | * struct B { |
3400 | | * int b; |
3401 | | * struct B* self; |
3402 | | * struct S* parent; |
3403 | | * }; |
3404 | | * struct S { |
3405 | | * struct A* a_ptr; |
3406 | | * struct B* b_ptr; |
3407 | | * }; |
3408 | | * |
3409 | | * Algorithm summary |
3410 | | * ================= |
3411 | | * |
3412 | | * Algorithm completes its work in 7 separate passes: |
3413 | | * |
3414 | | * 1. Strings deduplication. |
3415 | | * 2. Primitive types deduplication (int, enum, fwd). |
3416 | | * 3. Struct/union types deduplication. |
3417 | | * 4. Resolve unambiguous forward declarations. |
3418 | | * 5. Reference types deduplication (pointers, typedefs, arrays, funcs, func |
3419 | | * protos, and const/volatile/restrict modifiers). |
3420 | | * 6. Types compaction. |
3421 | | * 7. Types remapping. |
3422 | | * |
3423 | | * Algorithm determines canonical type descriptor, which is a single |
3424 | | * representative type for each truly unique type. This canonical type is the |
3425 | | * one that will go into final deduplicated BTF type information. For |
3426 | | * struct/unions, it is also the type that algorithm will merge additional type |
3427 | | * information into (while resolving FWDs), as it discovers it from data in |
3428 | | * other CUs. Each input BTF type eventually gets either mapped to itself, if |
3429 | | * that type is canonical, or to some other type, if that type is equivalent |
3430 | | * and was chosen as canonical representative. This mapping is stored in |
3431 | | * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that |
3432 | | * FWD type got resolved to. |
3433 | | * |
3434 | | * To facilitate fast discovery of canonical types, we also maintain canonical |
3435 | | * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash |
3436 | | * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types |
3437 | | * that match that signature. With sufficiently good choice of type signature |
3438 | | * hashing function, we can limit number of canonical types for each unique type |
3439 | | * signature to a very small number, allowing to find canonical type for any |
3440 | | * duplicated type very quickly. |
3441 | | * |
3442 | | * Struct/union deduplication is the most critical part and algorithm for |
3443 | | * deduplicating structs/unions is described in greater details in comments for |
3444 | | * `btf_dedup_is_equiv` function. |
3445 | | */ |
3446 | | int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts) |
3447 | 0 | { |
3448 | 0 | struct btf_dedup *d; |
3449 | 0 | int err; |
3450 | |
|
3451 | 0 | if (!OPTS_VALID(opts, btf_dedup_opts)) |
3452 | 0 | return libbpf_err(-EINVAL); |
3453 | | |
3454 | 0 | d = btf_dedup_new(btf, opts); |
3455 | 0 | if (IS_ERR(d)) { |
3456 | 0 | pr_debug("btf_dedup_new failed: %ld\n", PTR_ERR(d)); |
3457 | 0 | return libbpf_err(-EINVAL); |
3458 | 0 | } |
3459 | | |
3460 | 0 | if (btf_ensure_modifiable(btf)) { |
3461 | 0 | err = -ENOMEM; |
3462 | 0 | goto done; |
3463 | 0 | } |
3464 | | |
3465 | 0 | err = btf_dedup_prep(d); |
3466 | 0 | if (err) { |
3467 | 0 | pr_debug("btf_dedup_prep failed:%d\n", err); |
3468 | 0 | goto done; |
3469 | 0 | } |
3470 | 0 | err = btf_dedup_strings(d); |
3471 | 0 | if (err < 0) { |
3472 | 0 | pr_debug("btf_dedup_strings failed:%d\n", err); |
3473 | 0 | goto done; |
3474 | 0 | } |
3475 | 0 | err = btf_dedup_prim_types(d); |
3476 | 0 | if (err < 0) { |
3477 | 0 | pr_debug("btf_dedup_prim_types failed:%d\n", err); |
3478 | 0 | goto done; |
3479 | 0 | } |
3480 | 0 | err = btf_dedup_struct_types(d); |
3481 | 0 | if (err < 0) { |
3482 | 0 | pr_debug("btf_dedup_struct_types failed:%d\n", err); |
3483 | 0 | goto done; |
3484 | 0 | } |
3485 | 0 | err = btf_dedup_resolve_fwds(d); |
3486 | 0 | if (err < 0) { |
3487 | 0 | pr_debug("btf_dedup_resolve_fwds failed:%d\n", err); |
3488 | 0 | goto done; |
3489 | 0 | } |
3490 | 0 | err = btf_dedup_ref_types(d); |
3491 | 0 | if (err < 0) { |
3492 | 0 | pr_debug("btf_dedup_ref_types failed:%d\n", err); |
3493 | 0 | goto done; |
3494 | 0 | } |
3495 | 0 | err = btf_dedup_compact_types(d); |
3496 | 0 | if (err < 0) { |
3497 | 0 | pr_debug("btf_dedup_compact_types failed:%d\n", err); |
3498 | 0 | goto done; |
3499 | 0 | } |
3500 | 0 | err = btf_dedup_remap_types(d); |
3501 | 0 | if (err < 0) { |
3502 | 0 | pr_debug("btf_dedup_remap_types failed:%d\n", err); |
3503 | 0 | goto done; |
3504 | 0 | } |
3505 | | |
3506 | 0 | done: |
3507 | 0 | btf_dedup_free(d); |
3508 | 0 | return libbpf_err(err); |
3509 | 0 | } |
3510 | | |
3511 | 0 | #define BTF_UNPROCESSED_ID ((__u32)-1) |
3512 | 0 | #define BTF_IN_PROGRESS_ID ((__u32)-2) |
3513 | | |
3514 | | struct btf_dedup { |
3515 | | /* .BTF section to be deduped in-place */ |
3516 | | struct btf *btf; |
3517 | | /* |
3518 | | * Optional .BTF.ext section. When provided, any strings referenced |
3519 | | * from it will be taken into account when deduping strings |
3520 | | */ |
3521 | | struct btf_ext *btf_ext; |
3522 | | /* |
3523 | | * This is a map from any type's signature hash to a list of possible |
3524 | | * canonical representative type candidates. Hash collisions are |
3525 | | * ignored, so even types of various kinds can share same list of |
3526 | | * candidates, which is fine because we rely on subsequent |
3527 | | * btf_xxx_equal() checks to authoritatively verify type equality. |
3528 | | */ |
3529 | | struct hashmap *dedup_table; |
3530 | | /* Canonical types map */ |
3531 | | __u32 *map; |
3532 | | /* Hypothetical mapping, used during type graph equivalence checks */ |
3533 | | __u32 *hypot_map; |
3534 | | __u32 *hypot_list; |
3535 | | size_t hypot_cnt; |
3536 | | size_t hypot_cap; |
3537 | | /* Whether hypothetical mapping, if successful, would need to adjust |
3538 | | * already canonicalized types (due to a new forward declaration to |
3539 | | * concrete type resolution). In such case, during split BTF dedup |
3540 | | * candidate type would still be considered as different, because base |
3541 | | * BTF is considered to be immutable. |
3542 | | */ |
3543 | | bool hypot_adjust_canon; |
3544 | | /* Various option modifying behavior of algorithm */ |
3545 | | struct btf_dedup_opts opts; |
3546 | | /* temporary strings deduplication state */ |
3547 | | struct strset *strs_set; |
3548 | | }; |
3549 | | |
3550 | | static long hash_combine(long h, long value) |
3551 | 0 | { |
3552 | 0 | return h * 31 + value; |
3553 | 0 | } |
3554 | | |
3555 | | #define for_each_dedup_cand(d, node, hash) \ |
3556 | 0 | hashmap__for_each_key_entry(d->dedup_table, node, hash) |
3557 | | |
3558 | | static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id) |
3559 | 0 | { |
3560 | 0 | return hashmap__append(d->dedup_table, hash, type_id); |
3561 | 0 | } |
3562 | | |
3563 | | static int btf_dedup_hypot_map_add(struct btf_dedup *d, |
3564 | | __u32 from_id, __u32 to_id) |
3565 | 0 | { |
3566 | 0 | if (d->hypot_cnt == d->hypot_cap) { |
3567 | 0 | __u32 *new_list; |
3568 | |
|
3569 | 0 | d->hypot_cap += max((size_t)16, d->hypot_cap / 2); |
3570 | 0 | new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32)); |
3571 | 0 | if (!new_list) |
3572 | 0 | return -ENOMEM; |
3573 | 0 | d->hypot_list = new_list; |
3574 | 0 | } |
3575 | 0 | d->hypot_list[d->hypot_cnt++] = from_id; |
3576 | 0 | d->hypot_map[from_id] = to_id; |
3577 | 0 | return 0; |
3578 | 0 | } |
3579 | | |
3580 | | static void btf_dedup_clear_hypot_map(struct btf_dedup *d) |
3581 | 0 | { |
3582 | 0 | int i; |
3583 | |
|
3584 | 0 | for (i = 0; i < d->hypot_cnt; i++) |
3585 | 0 | d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; |
3586 | 0 | d->hypot_cnt = 0; |
3587 | 0 | d->hypot_adjust_canon = false; |
3588 | 0 | } |
3589 | | |
3590 | | static void btf_dedup_free(struct btf_dedup *d) |
3591 | 0 | { |
3592 | 0 | hashmap__free(d->dedup_table); |
3593 | 0 | d->dedup_table = NULL; |
3594 | |
|
3595 | 0 | free(d->map); |
3596 | 0 | d->map = NULL; |
3597 | |
|
3598 | 0 | free(d->hypot_map); |
3599 | 0 | d->hypot_map = NULL; |
3600 | |
|
3601 | 0 | free(d->hypot_list); |
3602 | 0 | d->hypot_list = NULL; |
3603 | |
|
3604 | 0 | free(d); |
3605 | 0 | } |
3606 | | |
3607 | | static size_t btf_dedup_identity_hash_fn(long key, void *ctx) |
3608 | 0 | { |
3609 | 0 | return key; |
3610 | 0 | } |
3611 | | |
3612 | | static size_t btf_dedup_collision_hash_fn(long key, void *ctx) |
3613 | 0 | { |
3614 | 0 | return 0; |
3615 | 0 | } |
3616 | | |
3617 | | static bool btf_dedup_equal_fn(long k1, long k2, void *ctx) |
3618 | 0 | { |
3619 | 0 | return k1 == k2; |
3620 | 0 | } |
3621 | | |
3622 | | static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts) |
3623 | 0 | { |
3624 | 0 | struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup)); |
3625 | 0 | hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn; |
3626 | 0 | int i, err = 0, type_cnt; |
3627 | |
|
3628 | 0 | if (!d) |
3629 | 0 | return ERR_PTR(-ENOMEM); |
3630 | | |
3631 | 0 | if (OPTS_GET(opts, force_collisions, false)) |
3632 | 0 | hash_fn = btf_dedup_collision_hash_fn; |
3633 | |
|
3634 | 0 | d->btf = btf; |
3635 | 0 | d->btf_ext = OPTS_GET(opts, btf_ext, NULL); |
3636 | |
|
3637 | 0 | d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL); |
3638 | 0 | if (IS_ERR(d->dedup_table)) { |
3639 | 0 | err = PTR_ERR(d->dedup_table); |
3640 | 0 | d->dedup_table = NULL; |
3641 | 0 | goto done; |
3642 | 0 | } |
3643 | | |
3644 | 0 | type_cnt = btf__type_cnt(btf); |
3645 | 0 | d->map = malloc(sizeof(__u32) * type_cnt); |
3646 | 0 | if (!d->map) { |
3647 | 0 | err = -ENOMEM; |
3648 | 0 | goto done; |
3649 | 0 | } |
3650 | | /* special BTF "void" type is made canonical immediately */ |
3651 | 0 | d->map[0] = 0; |
3652 | 0 | for (i = 1; i < type_cnt; i++) { |
3653 | 0 | struct btf_type *t = btf_type_by_id(d->btf, i); |
3654 | | |
3655 | | /* VAR and DATASEC are never deduped and are self-canonical */ |
3656 | 0 | if (btf_is_var(t) || btf_is_datasec(t)) |
3657 | 0 | d->map[i] = i; |
3658 | 0 | else |
3659 | 0 | d->map[i] = BTF_UNPROCESSED_ID; |
3660 | 0 | } |
3661 | |
|
3662 | 0 | d->hypot_map = malloc(sizeof(__u32) * type_cnt); |
3663 | 0 | if (!d->hypot_map) { |
3664 | 0 | err = -ENOMEM; |
3665 | 0 | goto done; |
3666 | 0 | } |
3667 | 0 | for (i = 0; i < type_cnt; i++) |
3668 | 0 | d->hypot_map[i] = BTF_UNPROCESSED_ID; |
3669 | |
|
3670 | 0 | done: |
3671 | 0 | if (err) { |
3672 | 0 | btf_dedup_free(d); |
3673 | 0 | return ERR_PTR(err); |
3674 | 0 | } |
3675 | | |
3676 | 0 | return d; |
3677 | 0 | } |
3678 | | |
3679 | | /* |
3680 | | * Iterate over all possible places in .BTF and .BTF.ext that can reference |
3681 | | * string and pass pointer to it to a provided callback `fn`. |
3682 | | */ |
3683 | | static int btf_for_each_str_off(struct btf_dedup *d, str_off_visit_fn fn, void *ctx) |
3684 | 0 | { |
3685 | 0 | int i, r; |
3686 | |
|
3687 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
3688 | 0 | struct btf_field_iter it; |
3689 | 0 | struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); |
3690 | 0 | __u32 *str_off; |
3691 | |
|
3692 | 0 | r = btf_field_iter_init(&it, t, BTF_FIELD_ITER_STRS); |
3693 | 0 | if (r) |
3694 | 0 | return r; |
3695 | | |
3696 | 0 | while ((str_off = btf_field_iter_next(&it))) { |
3697 | 0 | r = fn(str_off, ctx); |
3698 | 0 | if (r) |
3699 | 0 | return r; |
3700 | 0 | } |
3701 | 0 | } |
3702 | | |
3703 | 0 | if (!d->btf_ext) |
3704 | 0 | return 0; |
3705 | | |
3706 | 0 | r = btf_ext_visit_str_offs(d->btf_ext, fn, ctx); |
3707 | 0 | if (r) |
3708 | 0 | return r; |
3709 | | |
3710 | 0 | return 0; |
3711 | 0 | } |
3712 | | |
3713 | | static int strs_dedup_remap_str_off(__u32 *str_off_ptr, void *ctx) |
3714 | 0 | { |
3715 | 0 | struct btf_dedup *d = ctx; |
3716 | 0 | __u32 str_off = *str_off_ptr; |
3717 | 0 | const char *s; |
3718 | 0 | int off, err; |
3719 | | |
3720 | | /* don't touch empty string or string in main BTF */ |
3721 | 0 | if (str_off == 0 || str_off < d->btf->start_str_off) |
3722 | 0 | return 0; |
3723 | | |
3724 | 0 | s = btf__str_by_offset(d->btf, str_off); |
3725 | 0 | if (d->btf->base_btf) { |
3726 | 0 | err = btf__find_str(d->btf->base_btf, s); |
3727 | 0 | if (err >= 0) { |
3728 | 0 | *str_off_ptr = err; |
3729 | 0 | return 0; |
3730 | 0 | } |
3731 | 0 | if (err != -ENOENT) |
3732 | 0 | return err; |
3733 | 0 | } |
3734 | | |
3735 | 0 | off = strset__add_str(d->strs_set, s); |
3736 | 0 | if (off < 0) |
3737 | 0 | return off; |
3738 | | |
3739 | 0 | *str_off_ptr = d->btf->start_str_off + off; |
3740 | 0 | return 0; |
3741 | 0 | } |
3742 | | |
3743 | | /* |
3744 | | * Dedup string and filter out those that are not referenced from either .BTF |
3745 | | * or .BTF.ext (if provided) sections. |
3746 | | * |
3747 | | * This is done by building index of all strings in BTF's string section, |
3748 | | * then iterating over all entities that can reference strings (e.g., type |
3749 | | * names, struct field names, .BTF.ext line info, etc) and marking corresponding |
3750 | | * strings as used. After that all used strings are deduped and compacted into |
3751 | | * sequential blob of memory and new offsets are calculated. Then all the string |
3752 | | * references are iterated again and rewritten using new offsets. |
3753 | | */ |
3754 | | static int btf_dedup_strings(struct btf_dedup *d) |
3755 | 0 | { |
3756 | 0 | int err; |
3757 | |
|
3758 | 0 | if (d->btf->strs_deduped) |
3759 | 0 | return 0; |
3760 | | |
3761 | 0 | d->strs_set = strset__new(BTF_MAX_STR_OFFSET, NULL, 0); |
3762 | 0 | if (IS_ERR(d->strs_set)) { |
3763 | 0 | err = PTR_ERR(d->strs_set); |
3764 | 0 | goto err_out; |
3765 | 0 | } |
3766 | | |
3767 | 0 | if (!d->btf->base_btf) { |
3768 | | /* insert empty string; we won't be looking it up during strings |
3769 | | * dedup, but it's good to have it for generic BTF string lookups |
3770 | | */ |
3771 | 0 | err = strset__add_str(d->strs_set, ""); |
3772 | 0 | if (err < 0) |
3773 | 0 | goto err_out; |
3774 | 0 | } |
3775 | | |
3776 | | /* remap string offsets */ |
3777 | 0 | err = btf_for_each_str_off(d, strs_dedup_remap_str_off, d); |
3778 | 0 | if (err) |
3779 | 0 | goto err_out; |
3780 | | |
3781 | | /* replace BTF string data and hash with deduped ones */ |
3782 | 0 | strset__free(d->btf->strs_set); |
3783 | 0 | d->btf->hdr->str_len = strset__data_size(d->strs_set); |
3784 | 0 | d->btf->strs_set = d->strs_set; |
3785 | 0 | d->strs_set = NULL; |
3786 | 0 | d->btf->strs_deduped = true; |
3787 | 0 | return 0; |
3788 | | |
3789 | 0 | err_out: |
3790 | 0 | strset__free(d->strs_set); |
3791 | 0 | d->strs_set = NULL; |
3792 | |
|
3793 | 0 | return err; |
3794 | 0 | } |
3795 | | |
3796 | | static long btf_hash_common(struct btf_type *t) |
3797 | 0 | { |
3798 | 0 | long h; |
3799 | |
|
3800 | 0 | h = hash_combine(0, t->name_off); |
3801 | 0 | h = hash_combine(h, t->info); |
3802 | 0 | h = hash_combine(h, t->size); |
3803 | 0 | return h; |
3804 | 0 | } |
3805 | | |
3806 | | static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2) |
3807 | 0 | { |
3808 | 0 | return t1->name_off == t2->name_off && |
3809 | 0 | t1->info == t2->info && |
3810 | 0 | t1->size == t2->size; |
3811 | 0 | } |
3812 | | |
3813 | | /* Calculate type signature hash of INT or TAG. */ |
3814 | | static long btf_hash_int_decl_tag(struct btf_type *t) |
3815 | 0 | { |
3816 | 0 | __u32 info = *(__u32 *)(t + 1); |
3817 | 0 | long h; |
3818 | |
|
3819 | 0 | h = btf_hash_common(t); |
3820 | 0 | h = hash_combine(h, info); |
3821 | 0 | return h; |
3822 | 0 | } |
3823 | | |
3824 | | /* Check structural equality of two INTs or TAGs. */ |
3825 | | static bool btf_equal_int_tag(struct btf_type *t1, struct btf_type *t2) |
3826 | 0 | { |
3827 | 0 | __u32 info1, info2; |
3828 | |
|
3829 | 0 | if (!btf_equal_common(t1, t2)) |
3830 | 0 | return false; |
3831 | 0 | info1 = *(__u32 *)(t1 + 1); |
3832 | 0 | info2 = *(__u32 *)(t2 + 1); |
3833 | 0 | return info1 == info2; |
3834 | 0 | } |
3835 | | |
3836 | | /* Calculate type signature hash of ENUM/ENUM64. */ |
3837 | | static long btf_hash_enum(struct btf_type *t) |
3838 | 0 | { |
3839 | 0 | long h; |
3840 | | |
3841 | | /* don't hash vlen, enum members and size to support enum fwd resolving */ |
3842 | 0 | h = hash_combine(0, t->name_off); |
3843 | 0 | return h; |
3844 | 0 | } |
3845 | | |
3846 | | static bool btf_equal_enum_members(struct btf_type *t1, struct btf_type *t2) |
3847 | 0 | { |
3848 | 0 | const struct btf_enum *m1, *m2; |
3849 | 0 | __u16 vlen; |
3850 | 0 | int i; |
3851 | |
|
3852 | 0 | vlen = btf_vlen(t1); |
3853 | 0 | m1 = btf_enum(t1); |
3854 | 0 | m2 = btf_enum(t2); |
3855 | 0 | for (i = 0; i < vlen; i++) { |
3856 | 0 | if (m1->name_off != m2->name_off || m1->val != m2->val) |
3857 | 0 | return false; |
3858 | 0 | m1++; |
3859 | 0 | m2++; |
3860 | 0 | } |
3861 | 0 | return true; |
3862 | 0 | } |
3863 | | |
3864 | | static bool btf_equal_enum64_members(struct btf_type *t1, struct btf_type *t2) |
3865 | 0 | { |
3866 | 0 | const struct btf_enum64 *m1, *m2; |
3867 | 0 | __u16 vlen; |
3868 | 0 | int i; |
3869 | |
|
3870 | 0 | vlen = btf_vlen(t1); |
3871 | 0 | m1 = btf_enum64(t1); |
3872 | 0 | m2 = btf_enum64(t2); |
3873 | 0 | for (i = 0; i < vlen; i++) { |
3874 | 0 | if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 || |
3875 | 0 | m1->val_hi32 != m2->val_hi32) |
3876 | 0 | return false; |
3877 | 0 | m1++; |
3878 | 0 | m2++; |
3879 | 0 | } |
3880 | 0 | return true; |
3881 | 0 | } |
3882 | | |
3883 | | /* Check structural equality of two ENUMs or ENUM64s. */ |
3884 | | static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) |
3885 | 0 | { |
3886 | 0 | if (!btf_equal_common(t1, t2)) |
3887 | 0 | return false; |
3888 | | |
3889 | | /* t1 & t2 kinds are identical because of btf_equal_common */ |
3890 | 0 | if (btf_kind(t1) == BTF_KIND_ENUM) |
3891 | 0 | return btf_equal_enum_members(t1, t2); |
3892 | 0 | else |
3893 | 0 | return btf_equal_enum64_members(t1, t2); |
3894 | 0 | } |
3895 | | |
3896 | | static inline bool btf_is_enum_fwd(struct btf_type *t) |
3897 | 0 | { |
3898 | 0 | return btf_is_any_enum(t) && btf_vlen(t) == 0; |
3899 | 0 | } |
3900 | | |
3901 | | static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2) |
3902 | 0 | { |
3903 | 0 | if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2)) |
3904 | 0 | return btf_equal_enum(t1, t2); |
3905 | | /* At this point either t1 or t2 or both are forward declarations, thus: |
3906 | | * - skip comparing vlen because it is zero for forward declarations; |
3907 | | * - skip comparing size to allow enum forward declarations |
3908 | | * to be compatible with enum64 full declarations; |
3909 | | * - skip comparing kind for the same reason. |
3910 | | */ |
3911 | 0 | return t1->name_off == t2->name_off && |
3912 | 0 | btf_is_any_enum(t1) && btf_is_any_enum(t2); |
3913 | 0 | } |
3914 | | |
3915 | | /* |
3916 | | * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs, |
3917 | | * as referenced type IDs equivalence is established separately during type |
3918 | | * graph equivalence check algorithm. |
3919 | | */ |
3920 | | static long btf_hash_struct(struct btf_type *t) |
3921 | 0 | { |
3922 | 0 | const struct btf_member *member = btf_members(t); |
3923 | 0 | __u32 vlen = btf_vlen(t); |
3924 | 0 | long h = btf_hash_common(t); |
3925 | 0 | int i; |
3926 | |
|
3927 | 0 | for (i = 0; i < vlen; i++) { |
3928 | 0 | h = hash_combine(h, member->name_off); |
3929 | 0 | h = hash_combine(h, member->offset); |
3930 | | /* no hashing of referenced type ID, it can be unresolved yet */ |
3931 | 0 | member++; |
3932 | 0 | } |
3933 | 0 | return h; |
3934 | 0 | } |
3935 | | |
3936 | | /* |
3937 | | * Check structural compatibility of two STRUCTs/UNIONs, ignoring referenced |
3938 | | * type IDs. This check is performed during type graph equivalence check and |
3939 | | * referenced types equivalence is checked separately. |
3940 | | */ |
3941 | | static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2) |
3942 | 0 | { |
3943 | 0 | const struct btf_member *m1, *m2; |
3944 | 0 | __u16 vlen; |
3945 | 0 | int i; |
3946 | |
|
3947 | 0 | if (!btf_equal_common(t1, t2)) |
3948 | 0 | return false; |
3949 | | |
3950 | 0 | vlen = btf_vlen(t1); |
3951 | 0 | m1 = btf_members(t1); |
3952 | 0 | m2 = btf_members(t2); |
3953 | 0 | for (i = 0; i < vlen; i++) { |
3954 | 0 | if (m1->name_off != m2->name_off || m1->offset != m2->offset) |
3955 | 0 | return false; |
3956 | 0 | m1++; |
3957 | 0 | m2++; |
3958 | 0 | } |
3959 | 0 | return true; |
3960 | 0 | } |
3961 | | |
3962 | | /* |
3963 | | * Calculate type signature hash of ARRAY, including referenced type IDs, |
3964 | | * under assumption that they were already resolved to canonical type IDs and |
3965 | | * are not going to change. |
3966 | | */ |
3967 | | static long btf_hash_array(struct btf_type *t) |
3968 | 0 | { |
3969 | 0 | const struct btf_array *info = btf_array(t); |
3970 | 0 | long h = btf_hash_common(t); |
3971 | |
|
3972 | 0 | h = hash_combine(h, info->type); |
3973 | 0 | h = hash_combine(h, info->index_type); |
3974 | 0 | h = hash_combine(h, info->nelems); |
3975 | 0 | return h; |
3976 | 0 | } |
3977 | | |
3978 | | /* |
3979 | | * Check exact equality of two ARRAYs, taking into account referenced |
3980 | | * type IDs, under assumption that they were already resolved to canonical |
3981 | | * type IDs and are not going to change. |
3982 | | * This function is called during reference types deduplication to compare |
3983 | | * ARRAY to potential canonical representative. |
3984 | | */ |
3985 | | static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2) |
3986 | 0 | { |
3987 | 0 | const struct btf_array *info1, *info2; |
3988 | |
|
3989 | 0 | if (!btf_equal_common(t1, t2)) |
3990 | 0 | return false; |
3991 | | |
3992 | 0 | info1 = btf_array(t1); |
3993 | 0 | info2 = btf_array(t2); |
3994 | 0 | return info1->type == info2->type && |
3995 | 0 | info1->index_type == info2->index_type && |
3996 | 0 | info1->nelems == info2->nelems; |
3997 | 0 | } |
3998 | | |
3999 | | /* |
4000 | | * Check structural compatibility of two ARRAYs, ignoring referenced type |
4001 | | * IDs. This check is performed during type graph equivalence check and |
4002 | | * referenced types equivalence is checked separately. |
4003 | | */ |
4004 | | static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2) |
4005 | 0 | { |
4006 | 0 | if (!btf_equal_common(t1, t2)) |
4007 | 0 | return false; |
4008 | | |
4009 | 0 | return btf_array(t1)->nelems == btf_array(t2)->nelems; |
4010 | 0 | } |
4011 | | |
4012 | | /* |
4013 | | * Calculate type signature hash of FUNC_PROTO, including referenced type IDs, |
4014 | | * under assumption that they were already resolved to canonical type IDs and |
4015 | | * are not going to change. |
4016 | | */ |
4017 | | static long btf_hash_fnproto(struct btf_type *t) |
4018 | 0 | { |
4019 | 0 | const struct btf_param *member = btf_params(t); |
4020 | 0 | __u16 vlen = btf_vlen(t); |
4021 | 0 | long h = btf_hash_common(t); |
4022 | 0 | int i; |
4023 | |
|
4024 | 0 | for (i = 0; i < vlen; i++) { |
4025 | 0 | h = hash_combine(h, member->name_off); |
4026 | 0 | h = hash_combine(h, member->type); |
4027 | 0 | member++; |
4028 | 0 | } |
4029 | 0 | return h; |
4030 | 0 | } |
4031 | | |
4032 | | /* |
4033 | | * Check exact equality of two FUNC_PROTOs, taking into account referenced |
4034 | | * type IDs, under assumption that they were already resolved to canonical |
4035 | | * type IDs and are not going to change. |
4036 | | * This function is called during reference types deduplication to compare |
4037 | | * FUNC_PROTO to potential canonical representative. |
4038 | | */ |
4039 | | static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2) |
4040 | 0 | { |
4041 | 0 | const struct btf_param *m1, *m2; |
4042 | 0 | __u16 vlen; |
4043 | 0 | int i; |
4044 | |
|
4045 | 0 | if (!btf_equal_common(t1, t2)) |
4046 | 0 | return false; |
4047 | | |
4048 | 0 | vlen = btf_vlen(t1); |
4049 | 0 | m1 = btf_params(t1); |
4050 | 0 | m2 = btf_params(t2); |
4051 | 0 | for (i = 0; i < vlen; i++) { |
4052 | 0 | if (m1->name_off != m2->name_off || m1->type != m2->type) |
4053 | 0 | return false; |
4054 | 0 | m1++; |
4055 | 0 | m2++; |
4056 | 0 | } |
4057 | 0 | return true; |
4058 | 0 | } |
4059 | | |
4060 | | /* |
4061 | | * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type |
4062 | | * IDs. This check is performed during type graph equivalence check and |
4063 | | * referenced types equivalence is checked separately. |
4064 | | */ |
4065 | | static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2) |
4066 | 0 | { |
4067 | 0 | const struct btf_param *m1, *m2; |
4068 | 0 | __u16 vlen; |
4069 | 0 | int i; |
4070 | | |
4071 | | /* skip return type ID */ |
4072 | 0 | if (t1->name_off != t2->name_off || t1->info != t2->info) |
4073 | 0 | return false; |
4074 | | |
4075 | 0 | vlen = btf_vlen(t1); |
4076 | 0 | m1 = btf_params(t1); |
4077 | 0 | m2 = btf_params(t2); |
4078 | 0 | for (i = 0; i < vlen; i++) { |
4079 | 0 | if (m1->name_off != m2->name_off) |
4080 | 0 | return false; |
4081 | 0 | m1++; |
4082 | 0 | m2++; |
4083 | 0 | } |
4084 | 0 | return true; |
4085 | 0 | } |
4086 | | |
4087 | | /* Prepare split BTF for deduplication by calculating hashes of base BTF's |
4088 | | * types and initializing the rest of the state (canonical type mapping) for |
4089 | | * the fixed base BTF part. |
4090 | | */ |
4091 | | static int btf_dedup_prep(struct btf_dedup *d) |
4092 | 0 | { |
4093 | 0 | struct btf_type *t; |
4094 | 0 | int type_id; |
4095 | 0 | long h; |
4096 | |
|
4097 | 0 | if (!d->btf->base_btf) |
4098 | 0 | return 0; |
4099 | | |
4100 | 0 | for (type_id = 1; type_id < d->btf->start_id; type_id++) { |
4101 | 0 | t = btf_type_by_id(d->btf, type_id); |
4102 | | |
4103 | | /* all base BTF types are self-canonical by definition */ |
4104 | 0 | d->map[type_id] = type_id; |
4105 | |
|
4106 | 0 | switch (btf_kind(t)) { |
4107 | 0 | case BTF_KIND_VAR: |
4108 | 0 | case BTF_KIND_DATASEC: |
4109 | | /* VAR and DATASEC are never hash/deduplicated */ |
4110 | 0 | continue; |
4111 | 0 | case BTF_KIND_CONST: |
4112 | 0 | case BTF_KIND_VOLATILE: |
4113 | 0 | case BTF_KIND_RESTRICT: |
4114 | 0 | case BTF_KIND_PTR: |
4115 | 0 | case BTF_KIND_FWD: |
4116 | 0 | case BTF_KIND_TYPEDEF: |
4117 | 0 | case BTF_KIND_FUNC: |
4118 | 0 | case BTF_KIND_FLOAT: |
4119 | 0 | case BTF_KIND_TYPE_TAG: |
4120 | 0 | h = btf_hash_common(t); |
4121 | 0 | break; |
4122 | 0 | case BTF_KIND_INT: |
4123 | 0 | case BTF_KIND_DECL_TAG: |
4124 | 0 | h = btf_hash_int_decl_tag(t); |
4125 | 0 | break; |
4126 | 0 | case BTF_KIND_ENUM: |
4127 | 0 | case BTF_KIND_ENUM64: |
4128 | 0 | h = btf_hash_enum(t); |
4129 | 0 | break; |
4130 | 0 | case BTF_KIND_STRUCT: |
4131 | 0 | case BTF_KIND_UNION: |
4132 | 0 | h = btf_hash_struct(t); |
4133 | 0 | break; |
4134 | 0 | case BTF_KIND_ARRAY: |
4135 | 0 | h = btf_hash_array(t); |
4136 | 0 | break; |
4137 | 0 | case BTF_KIND_FUNC_PROTO: |
4138 | 0 | h = btf_hash_fnproto(t); |
4139 | 0 | break; |
4140 | 0 | default: |
4141 | 0 | pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id); |
4142 | 0 | return -EINVAL; |
4143 | 0 | } |
4144 | 0 | if (btf_dedup_table_add(d, h, type_id)) |
4145 | 0 | return -ENOMEM; |
4146 | 0 | } |
4147 | | |
4148 | 0 | return 0; |
4149 | 0 | } |
4150 | | |
4151 | | /* |
4152 | | * Deduplicate primitive types, that can't reference other types, by calculating |
4153 | | * their type signature hash and comparing them with any possible canonical |
4154 | | * candidate. If no canonical candidate matches, type itself is marked as |
4155 | | * canonical and is added into `btf_dedup->dedup_table` as another candidate. |
4156 | | */ |
4157 | | static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) |
4158 | 0 | { |
4159 | 0 | struct btf_type *t = btf_type_by_id(d->btf, type_id); |
4160 | 0 | struct hashmap_entry *hash_entry; |
4161 | 0 | struct btf_type *cand; |
4162 | | /* if we don't find equivalent type, then we are canonical */ |
4163 | 0 | __u32 new_id = type_id; |
4164 | 0 | __u32 cand_id; |
4165 | 0 | long h; |
4166 | |
|
4167 | 0 | switch (btf_kind(t)) { |
4168 | 0 | case BTF_KIND_CONST: |
4169 | 0 | case BTF_KIND_VOLATILE: |
4170 | 0 | case BTF_KIND_RESTRICT: |
4171 | 0 | case BTF_KIND_PTR: |
4172 | 0 | case BTF_KIND_TYPEDEF: |
4173 | 0 | case BTF_KIND_ARRAY: |
4174 | 0 | case BTF_KIND_STRUCT: |
4175 | 0 | case BTF_KIND_UNION: |
4176 | 0 | case BTF_KIND_FUNC: |
4177 | 0 | case BTF_KIND_FUNC_PROTO: |
4178 | 0 | case BTF_KIND_VAR: |
4179 | 0 | case BTF_KIND_DATASEC: |
4180 | 0 | case BTF_KIND_DECL_TAG: |
4181 | 0 | case BTF_KIND_TYPE_TAG: |
4182 | 0 | return 0; |
4183 | | |
4184 | 0 | case BTF_KIND_INT: |
4185 | 0 | h = btf_hash_int_decl_tag(t); |
4186 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4187 | 0 | cand_id = hash_entry->value; |
4188 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4189 | 0 | if (btf_equal_int_tag(t, cand)) { |
4190 | 0 | new_id = cand_id; |
4191 | 0 | break; |
4192 | 0 | } |
4193 | 0 | } |
4194 | 0 | break; |
4195 | | |
4196 | 0 | case BTF_KIND_ENUM: |
4197 | 0 | case BTF_KIND_ENUM64: |
4198 | 0 | h = btf_hash_enum(t); |
4199 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4200 | 0 | cand_id = hash_entry->value; |
4201 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4202 | 0 | if (btf_equal_enum(t, cand)) { |
4203 | 0 | new_id = cand_id; |
4204 | 0 | break; |
4205 | 0 | } |
4206 | 0 | if (btf_compat_enum(t, cand)) { |
4207 | 0 | if (btf_is_enum_fwd(t)) { |
4208 | | /* resolve fwd to full enum */ |
4209 | 0 | new_id = cand_id; |
4210 | 0 | break; |
4211 | 0 | } |
4212 | | /* resolve canonical enum fwd to full enum */ |
4213 | 0 | d->map[cand_id] = type_id; |
4214 | 0 | } |
4215 | 0 | } |
4216 | 0 | break; |
4217 | | |
4218 | 0 | case BTF_KIND_FWD: |
4219 | 0 | case BTF_KIND_FLOAT: |
4220 | 0 | h = btf_hash_common(t); |
4221 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4222 | 0 | cand_id = hash_entry->value; |
4223 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4224 | 0 | if (btf_equal_common(t, cand)) { |
4225 | 0 | new_id = cand_id; |
4226 | 0 | break; |
4227 | 0 | } |
4228 | 0 | } |
4229 | 0 | break; |
4230 | | |
4231 | 0 | default: |
4232 | 0 | return -EINVAL; |
4233 | 0 | } |
4234 | | |
4235 | 0 | d->map[type_id] = new_id; |
4236 | 0 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
4237 | 0 | return -ENOMEM; |
4238 | | |
4239 | 0 | return 0; |
4240 | 0 | } |
4241 | | |
4242 | | static int btf_dedup_prim_types(struct btf_dedup *d) |
4243 | 0 | { |
4244 | 0 | int i, err; |
4245 | |
|
4246 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
4247 | 0 | err = btf_dedup_prim_type(d, d->btf->start_id + i); |
4248 | 0 | if (err) |
4249 | 0 | return err; |
4250 | 0 | } |
4251 | 0 | return 0; |
4252 | 0 | } |
4253 | | |
4254 | | /* |
4255 | | * Check whether type is already mapped into canonical one (could be to itself). |
4256 | | */ |
4257 | | static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id) |
4258 | 0 | { |
4259 | 0 | return d->map[type_id] <= BTF_MAX_NR_TYPES; |
4260 | 0 | } |
4261 | | |
4262 | | /* |
4263 | | * Resolve type ID into its canonical type ID, if any; otherwise return original |
4264 | | * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow |
4265 | | * STRUCT/UNION link and resolve it into canonical type ID as well. |
4266 | | */ |
4267 | | static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id) |
4268 | 0 | { |
4269 | 0 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
4270 | 0 | type_id = d->map[type_id]; |
4271 | 0 | return type_id; |
4272 | 0 | } |
4273 | | |
4274 | | /* |
4275 | | * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original |
4276 | | * type ID. |
4277 | | */ |
4278 | | static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id) |
4279 | 0 | { |
4280 | 0 | __u32 orig_type_id = type_id; |
4281 | |
|
4282 | 0 | if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) |
4283 | 0 | return type_id; |
4284 | | |
4285 | 0 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
4286 | 0 | type_id = d->map[type_id]; |
4287 | |
|
4288 | 0 | if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) |
4289 | 0 | return type_id; |
4290 | | |
4291 | 0 | return orig_type_id; |
4292 | 0 | } |
4293 | | |
4294 | | |
4295 | | static inline __u16 btf_fwd_kind(struct btf_type *t) |
4296 | 0 | { |
4297 | 0 | return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT; |
4298 | 0 | } |
4299 | | |
4300 | | /* Check if given two types are identical ARRAY definitions */ |
4301 | | static bool btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2) |
4302 | 0 | { |
4303 | 0 | struct btf_type *t1, *t2; |
4304 | |
|
4305 | 0 | t1 = btf_type_by_id(d->btf, id1); |
4306 | 0 | t2 = btf_type_by_id(d->btf, id2); |
4307 | 0 | if (!btf_is_array(t1) || !btf_is_array(t2)) |
4308 | 0 | return false; |
4309 | | |
4310 | 0 | return btf_equal_array(t1, t2); |
4311 | 0 | } |
4312 | | |
4313 | | /* Check if given two types are identical STRUCT/UNION definitions */ |
4314 | | static bool btf_dedup_identical_structs(struct btf_dedup *d, __u32 id1, __u32 id2) |
4315 | 0 | { |
4316 | 0 | const struct btf_member *m1, *m2; |
4317 | 0 | struct btf_type *t1, *t2; |
4318 | 0 | int n, i; |
4319 | |
|
4320 | 0 | t1 = btf_type_by_id(d->btf, id1); |
4321 | 0 | t2 = btf_type_by_id(d->btf, id2); |
4322 | |
|
4323 | 0 | if (!btf_is_composite(t1) || btf_kind(t1) != btf_kind(t2)) |
4324 | 0 | return false; |
4325 | | |
4326 | 0 | if (!btf_shallow_equal_struct(t1, t2)) |
4327 | 0 | return false; |
4328 | | |
4329 | 0 | m1 = btf_members(t1); |
4330 | 0 | m2 = btf_members(t2); |
4331 | 0 | for (i = 0, n = btf_vlen(t1); i < n; i++, m1++, m2++) { |
4332 | 0 | if (m1->type != m2->type && |
4333 | 0 | !btf_dedup_identical_arrays(d, m1->type, m2->type) && |
4334 | 0 | !btf_dedup_identical_structs(d, m1->type, m2->type)) |
4335 | 0 | return false; |
4336 | 0 | } |
4337 | 0 | return true; |
4338 | 0 | } |
4339 | | |
4340 | | /* |
4341 | | * Check equivalence of BTF type graph formed by candidate struct/union (we'll |
4342 | | * call it "candidate graph" in this description for brevity) to a type graph |
4343 | | * formed by (potential) canonical struct/union ("canonical graph" for brevity |
4344 | | * here, though keep in mind that not all types in canonical graph are |
4345 | | * necessarily canonical representatives themselves, some of them might be |
4346 | | * duplicates or its uniqueness might not have been established yet). |
4347 | | * Returns: |
4348 | | * - >0, if type graphs are equivalent; |
4349 | | * - 0, if not equivalent; |
4350 | | * - <0, on error. |
4351 | | * |
4352 | | * Algorithm performs side-by-side DFS traversal of both type graphs and checks |
4353 | | * equivalence of BTF types at each step. If at any point BTF types in candidate |
4354 | | * and canonical graphs are not compatible structurally, whole graphs are |
4355 | | * incompatible. If types are structurally equivalent (i.e., all information |
4356 | | * except referenced type IDs is exactly the same), a mapping from `canon_id` to |
4357 | | * a `cand_id` is recoded in hypothetical mapping (`btf_dedup->hypot_map`). |
4358 | | * If a type references other types, then those referenced types are checked |
4359 | | * for equivalence recursively. |
4360 | | * |
4361 | | * During DFS traversal, if we find that for current `canon_id` type we |
4362 | | * already have some mapping in hypothetical map, we check for two possible |
4363 | | * situations: |
4364 | | * - `canon_id` is mapped to exactly the same type as `cand_id`. This will |
4365 | | * happen when type graphs have cycles. In this case we assume those two |
4366 | | * types are equivalent. |
4367 | | * - `canon_id` is mapped to different type. This is contradiction in our |
4368 | | * hypothetical mapping, because same graph in canonical graph corresponds |
4369 | | * to two different types in candidate graph, which for equivalent type |
4370 | | * graphs shouldn't happen. This condition terminates equivalence check |
4371 | | * with negative result. |
4372 | | * |
4373 | | * If type graphs traversal exhausts types to check and find no contradiction, |
4374 | | * then type graphs are equivalent. |
4375 | | * |
4376 | | * When checking types for equivalence, there is one special case: FWD types. |
4377 | | * If FWD type resolution is allowed and one of the types (either from canonical |
4378 | | * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind |
4379 | | * flag) and their names match, hypothetical mapping is updated to point from |
4380 | | * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully, |
4381 | | * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently. |
4382 | | * |
4383 | | * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution, |
4384 | | * if there are two exactly named (or anonymous) structs/unions that are |
4385 | | * compatible structurally, one of which has FWD field, while other is concrete |
4386 | | * STRUCT/UNION, but according to C sources they are different structs/unions |
4387 | | * that are referencing different types with the same name. This is extremely |
4388 | | * unlikely to happen, but btf_dedup API allows to disable FWD resolution if |
4389 | | * this logic is causing problems. |
4390 | | * |
4391 | | * Doing FWD resolution means that both candidate and/or canonical graphs can |
4392 | | * consists of portions of the graph that come from multiple compilation units. |
4393 | | * This is due to the fact that types within single compilation unit are always |
4394 | | * deduplicated and FWDs are already resolved, if referenced struct/union |
4395 | | * definition is available. So, if we had unresolved FWD and found corresponding |
4396 | | * STRUCT/UNION, they will be from different compilation units. This |
4397 | | * consequently means that when we "link" FWD to corresponding STRUCT/UNION, |
4398 | | * type graph will likely have at least two different BTF types that describe |
4399 | | * same type (e.g., most probably there will be two different BTF types for the |
4400 | | * same 'int' primitive type) and could even have "overlapping" parts of type |
4401 | | * graph that describe same subset of types. |
4402 | | * |
4403 | | * This in turn means that our assumption that each type in canonical graph |
4404 | | * must correspond to exactly one type in candidate graph might not hold |
4405 | | * anymore and will make it harder to detect contradictions using hypothetical |
4406 | | * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION |
4407 | | * resolution only in canonical graph. FWDs in candidate graphs are never |
4408 | | * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs |
4409 | | * that can occur: |
4410 | | * - Both types in canonical and candidate graphs are FWDs. If they are |
4411 | | * structurally equivalent, then they can either be both resolved to the |
4412 | | * same STRUCT/UNION or not resolved at all. In both cases they are |
4413 | | * equivalent and there is no need to resolve FWD on candidate side. |
4414 | | * - Both types in canonical and candidate graphs are concrete STRUCT/UNION, |
4415 | | * so nothing to resolve as well, algorithm will check equivalence anyway. |
4416 | | * - Type in canonical graph is FWD, while type in candidate is concrete |
4417 | | * STRUCT/UNION. In this case candidate graph comes from single compilation |
4418 | | * unit, so there is exactly one BTF type for each unique C type. After |
4419 | | * resolving FWD into STRUCT/UNION, there might be more than one BTF type |
4420 | | * in canonical graph mapping to single BTF type in candidate graph, but |
4421 | | * because hypothetical mapping maps from canonical to candidate types, it's |
4422 | | * alright, and we still maintain the property of having single `canon_id` |
4423 | | * mapping to single `cand_id` (there could be two different `canon_id` |
4424 | | * mapped to the same `cand_id`, but it's not contradictory). |
4425 | | * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate |
4426 | | * graph is FWD. In this case we are just going to check compatibility of |
4427 | | * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll |
4428 | | * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to |
4429 | | * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs |
4430 | | * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from |
4431 | | * canonical graph. |
4432 | | */ |
4433 | | static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, |
4434 | | __u32 canon_id) |
4435 | 0 | { |
4436 | 0 | struct btf_type *cand_type; |
4437 | 0 | struct btf_type *canon_type; |
4438 | 0 | __u32 hypot_type_id; |
4439 | 0 | __u16 cand_kind; |
4440 | 0 | __u16 canon_kind; |
4441 | 0 | int i, eq; |
4442 | | |
4443 | | /* if both resolve to the same canonical, they must be equivalent */ |
4444 | 0 | if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id)) |
4445 | 0 | return 1; |
4446 | | |
4447 | 0 | canon_id = resolve_fwd_id(d, canon_id); |
4448 | |
|
4449 | 0 | hypot_type_id = d->hypot_map[canon_id]; |
4450 | 0 | if (hypot_type_id <= BTF_MAX_NR_TYPES) { |
4451 | 0 | if (hypot_type_id == cand_id) |
4452 | 0 | return 1; |
4453 | | /* In some cases compiler will generate different DWARF types |
4454 | | * for *identical* array type definitions and use them for |
4455 | | * different fields within the *same* struct. This breaks type |
4456 | | * equivalence check, which makes an assumption that candidate |
4457 | | * types sub-graph has a consistent and deduped-by-compiler |
4458 | | * types within a single CU. So work around that by explicitly |
4459 | | * allowing identical array types here. |
4460 | | */ |
4461 | 0 | if (btf_dedup_identical_arrays(d, hypot_type_id, cand_id)) |
4462 | 0 | return 1; |
4463 | | /* It turns out that similar situation can happen with |
4464 | | * struct/union sometimes, sigh... Handle the case where |
4465 | | * structs/unions are exactly the same, down to the referenced |
4466 | | * type IDs. Anything more complicated (e.g., if referenced |
4467 | | * types are different, but equivalent) is *way more* |
4468 | | * complicated and requires a many-to-many equivalence mapping. |
4469 | | */ |
4470 | 0 | if (btf_dedup_identical_structs(d, hypot_type_id, cand_id)) |
4471 | 0 | return 1; |
4472 | 0 | return 0; |
4473 | 0 | } |
4474 | | |
4475 | 0 | if (btf_dedup_hypot_map_add(d, canon_id, cand_id)) |
4476 | 0 | return -ENOMEM; |
4477 | | |
4478 | 0 | cand_type = btf_type_by_id(d->btf, cand_id); |
4479 | 0 | canon_type = btf_type_by_id(d->btf, canon_id); |
4480 | 0 | cand_kind = btf_kind(cand_type); |
4481 | 0 | canon_kind = btf_kind(canon_type); |
4482 | |
|
4483 | 0 | if (cand_type->name_off != canon_type->name_off) |
4484 | 0 | return 0; |
4485 | | |
4486 | | /* FWD <--> STRUCT/UNION equivalence check, if enabled */ |
4487 | 0 | if ((cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD) |
4488 | 0 | && cand_kind != canon_kind) { |
4489 | 0 | __u16 real_kind; |
4490 | 0 | __u16 fwd_kind; |
4491 | |
|
4492 | 0 | if (cand_kind == BTF_KIND_FWD) { |
4493 | 0 | real_kind = canon_kind; |
4494 | 0 | fwd_kind = btf_fwd_kind(cand_type); |
4495 | 0 | } else { |
4496 | 0 | real_kind = cand_kind; |
4497 | 0 | fwd_kind = btf_fwd_kind(canon_type); |
4498 | | /* we'd need to resolve base FWD to STRUCT/UNION */ |
4499 | 0 | if (fwd_kind == real_kind && canon_id < d->btf->start_id) |
4500 | 0 | d->hypot_adjust_canon = true; |
4501 | 0 | } |
4502 | 0 | return fwd_kind == real_kind; |
4503 | 0 | } |
4504 | | |
4505 | 0 | if (cand_kind != canon_kind) |
4506 | 0 | return 0; |
4507 | | |
4508 | 0 | switch (cand_kind) { |
4509 | 0 | case BTF_KIND_INT: |
4510 | 0 | return btf_equal_int_tag(cand_type, canon_type); |
4511 | | |
4512 | 0 | case BTF_KIND_ENUM: |
4513 | 0 | case BTF_KIND_ENUM64: |
4514 | 0 | return btf_compat_enum(cand_type, canon_type); |
4515 | | |
4516 | 0 | case BTF_KIND_FWD: |
4517 | 0 | case BTF_KIND_FLOAT: |
4518 | 0 | return btf_equal_common(cand_type, canon_type); |
4519 | | |
4520 | 0 | case BTF_KIND_CONST: |
4521 | 0 | case BTF_KIND_VOLATILE: |
4522 | 0 | case BTF_KIND_RESTRICT: |
4523 | 0 | case BTF_KIND_PTR: |
4524 | 0 | case BTF_KIND_TYPEDEF: |
4525 | 0 | case BTF_KIND_FUNC: |
4526 | 0 | case BTF_KIND_TYPE_TAG: |
4527 | 0 | if (cand_type->info != canon_type->info) |
4528 | 0 | return 0; |
4529 | 0 | return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
4530 | | |
4531 | 0 | case BTF_KIND_ARRAY: { |
4532 | 0 | const struct btf_array *cand_arr, *canon_arr; |
4533 | |
|
4534 | 0 | if (!btf_compat_array(cand_type, canon_type)) |
4535 | 0 | return 0; |
4536 | 0 | cand_arr = btf_array(cand_type); |
4537 | 0 | canon_arr = btf_array(canon_type); |
4538 | 0 | eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type); |
4539 | 0 | if (eq <= 0) |
4540 | 0 | return eq; |
4541 | 0 | return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); |
4542 | 0 | } |
4543 | | |
4544 | 0 | case BTF_KIND_STRUCT: |
4545 | 0 | case BTF_KIND_UNION: { |
4546 | 0 | const struct btf_member *cand_m, *canon_m; |
4547 | 0 | __u16 vlen; |
4548 | |
|
4549 | 0 | if (!btf_shallow_equal_struct(cand_type, canon_type)) |
4550 | 0 | return 0; |
4551 | 0 | vlen = btf_vlen(cand_type); |
4552 | 0 | cand_m = btf_members(cand_type); |
4553 | 0 | canon_m = btf_members(canon_type); |
4554 | 0 | for (i = 0; i < vlen; i++) { |
4555 | 0 | eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); |
4556 | 0 | if (eq <= 0) |
4557 | 0 | return eq; |
4558 | 0 | cand_m++; |
4559 | 0 | canon_m++; |
4560 | 0 | } |
4561 | | |
4562 | 0 | return 1; |
4563 | 0 | } |
4564 | | |
4565 | 0 | case BTF_KIND_FUNC_PROTO: { |
4566 | 0 | const struct btf_param *cand_p, *canon_p; |
4567 | 0 | __u16 vlen; |
4568 | |
|
4569 | 0 | if (!btf_compat_fnproto(cand_type, canon_type)) |
4570 | 0 | return 0; |
4571 | 0 | eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
4572 | 0 | if (eq <= 0) |
4573 | 0 | return eq; |
4574 | 0 | vlen = btf_vlen(cand_type); |
4575 | 0 | cand_p = btf_params(cand_type); |
4576 | 0 | canon_p = btf_params(canon_type); |
4577 | 0 | for (i = 0; i < vlen; i++) { |
4578 | 0 | eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); |
4579 | 0 | if (eq <= 0) |
4580 | 0 | return eq; |
4581 | 0 | cand_p++; |
4582 | 0 | canon_p++; |
4583 | 0 | } |
4584 | 0 | return 1; |
4585 | 0 | } |
4586 | | |
4587 | 0 | default: |
4588 | 0 | return -EINVAL; |
4589 | 0 | } |
4590 | 0 | return 0; |
4591 | 0 | } |
4592 | | |
4593 | | /* |
4594 | | * Use hypothetical mapping, produced by successful type graph equivalence |
4595 | | * check, to augment existing struct/union canonical mapping, where possible. |
4596 | | * |
4597 | | * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record |
4598 | | * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional: |
4599 | | * it doesn't matter if FWD type was part of canonical graph or candidate one, |
4600 | | * we are recording the mapping anyway. As opposed to carefulness required |
4601 | | * for struct/union correspondence mapping (described below), for FWD resolution |
4602 | | * it's not important, as by the time that FWD type (reference type) will be |
4603 | | * deduplicated all structs/unions will be deduped already anyway. |
4604 | | * |
4605 | | * Recording STRUCT/UNION mapping is purely a performance optimization and is |
4606 | | * not required for correctness. It needs to be done carefully to ensure that |
4607 | | * struct/union from candidate's type graph is not mapped into corresponding |
4608 | | * struct/union from canonical type graph that itself hasn't been resolved into |
4609 | | * canonical representative. The only guarantee we have is that canonical |
4610 | | * struct/union was determined as canonical and that won't change. But any |
4611 | | * types referenced through that struct/union fields could have been not yet |
4612 | | * resolved, so in case like that it's too early to establish any kind of |
4613 | | * correspondence between structs/unions. |
4614 | | * |
4615 | | * No canonical correspondence is derived for primitive types (they are already |
4616 | | * deduplicated completely already anyway) or reference types (they rely on |
4617 | | * stability of struct/union canonical relationship for equivalence checks). |
4618 | | */ |
4619 | | static void btf_dedup_merge_hypot_map(struct btf_dedup *d) |
4620 | 0 | { |
4621 | 0 | __u32 canon_type_id, targ_type_id; |
4622 | 0 | __u16 t_kind, c_kind; |
4623 | 0 | __u32 t_id, c_id; |
4624 | 0 | int i; |
4625 | |
|
4626 | 0 | for (i = 0; i < d->hypot_cnt; i++) { |
4627 | 0 | canon_type_id = d->hypot_list[i]; |
4628 | 0 | targ_type_id = d->hypot_map[canon_type_id]; |
4629 | 0 | t_id = resolve_type_id(d, targ_type_id); |
4630 | 0 | c_id = resolve_type_id(d, canon_type_id); |
4631 | 0 | t_kind = btf_kind(btf__type_by_id(d->btf, t_id)); |
4632 | 0 | c_kind = btf_kind(btf__type_by_id(d->btf, c_id)); |
4633 | | /* |
4634 | | * Resolve FWD into STRUCT/UNION. |
4635 | | * It's ok to resolve FWD into STRUCT/UNION that's not yet |
4636 | | * mapped to canonical representative (as opposed to |
4637 | | * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because |
4638 | | * eventually that struct is going to be mapped and all resolved |
4639 | | * FWDs will automatically resolve to correct canonical |
4640 | | * representative. This will happen before ref type deduping, |
4641 | | * which critically depends on stability of these mapping. This |
4642 | | * stability is not a requirement for STRUCT/UNION equivalence |
4643 | | * checks, though. |
4644 | | */ |
4645 | | |
4646 | | /* if it's the split BTF case, we still need to point base FWD |
4647 | | * to STRUCT/UNION in a split BTF, because FWDs from split BTF |
4648 | | * will be resolved against base FWD. If we don't point base |
4649 | | * canonical FWD to the resolved STRUCT/UNION, then all the |
4650 | | * FWDs in split BTF won't be correctly resolved to a proper |
4651 | | * STRUCT/UNION. |
4652 | | */ |
4653 | 0 | if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD) |
4654 | 0 | d->map[c_id] = t_id; |
4655 | | |
4656 | | /* if graph equivalence determined that we'd need to adjust |
4657 | | * base canonical types, then we need to only point base FWDs |
4658 | | * to STRUCTs/UNIONs and do no more modifications. For all |
4659 | | * other purposes the type graphs were not equivalent. |
4660 | | */ |
4661 | 0 | if (d->hypot_adjust_canon) |
4662 | 0 | continue; |
4663 | | |
4664 | 0 | if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD) |
4665 | 0 | d->map[t_id] = c_id; |
4666 | |
|
4667 | 0 | if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) && |
4668 | 0 | c_kind != BTF_KIND_FWD && |
4669 | 0 | is_type_mapped(d, c_id) && |
4670 | 0 | !is_type_mapped(d, t_id)) { |
4671 | | /* |
4672 | | * as a perf optimization, we can map struct/union |
4673 | | * that's part of type graph we just verified for |
4674 | | * equivalence. We can do that for struct/union that has |
4675 | | * canonical representative only, though. |
4676 | | */ |
4677 | 0 | d->map[t_id] = c_id; |
4678 | 0 | } |
4679 | 0 | } |
4680 | 0 | } |
4681 | | |
4682 | | /* |
4683 | | * Deduplicate struct/union types. |
4684 | | * |
4685 | | * For each struct/union type its type signature hash is calculated, taking |
4686 | | * into account type's name, size, number, order and names of fields, but |
4687 | | * ignoring type ID's referenced from fields, because they might not be deduped |
4688 | | * completely until after reference types deduplication phase. This type hash |
4689 | | * is used to iterate over all potential canonical types, sharing same hash. |
4690 | | * For each canonical candidate we check whether type graphs that they form |
4691 | | * (through referenced types in fields and so on) are equivalent using algorithm |
4692 | | * implemented in `btf_dedup_is_equiv`. If such equivalence is found and |
4693 | | * BTF_KIND_FWD resolution is allowed, then hypothetical mapping |
4694 | | * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence |
4695 | | * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to |
4696 | | * potentially map other structs/unions to their canonical representatives, |
4697 | | * if such relationship hasn't yet been established. This speeds up algorithm |
4698 | | * by eliminating some of the duplicate work. |
4699 | | * |
4700 | | * If no matching canonical representative was found, struct/union is marked |
4701 | | * as canonical for itself and is added into btf_dedup->dedup_table hash map |
4702 | | * for further look ups. |
4703 | | */ |
4704 | | static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id) |
4705 | 0 | { |
4706 | 0 | struct btf_type *cand_type, *t; |
4707 | 0 | struct hashmap_entry *hash_entry; |
4708 | | /* if we don't find equivalent type, then we are canonical */ |
4709 | 0 | __u32 new_id = type_id; |
4710 | 0 | __u16 kind; |
4711 | 0 | long h; |
4712 | | |
4713 | | /* already deduped or is in process of deduping (loop detected) */ |
4714 | 0 | if (d->map[type_id] <= BTF_MAX_NR_TYPES) |
4715 | 0 | return 0; |
4716 | | |
4717 | 0 | t = btf_type_by_id(d->btf, type_id); |
4718 | 0 | kind = btf_kind(t); |
4719 | |
|
4720 | 0 | if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION) |
4721 | 0 | return 0; |
4722 | | |
4723 | 0 | h = btf_hash_struct(t); |
4724 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4725 | 0 | __u32 cand_id = hash_entry->value; |
4726 | 0 | int eq; |
4727 | | |
4728 | | /* |
4729 | | * Even though btf_dedup_is_equiv() checks for |
4730 | | * btf_shallow_equal_struct() internally when checking two |
4731 | | * structs (unions) for equivalence, we need to guard here |
4732 | | * from picking matching FWD type as a dedup candidate. |
4733 | | * This can happen due to hash collision. In such case just |
4734 | | * relying on btf_dedup_is_equiv() would lead to potentially |
4735 | | * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because |
4736 | | * FWD and compatible STRUCT/UNION are considered equivalent. |
4737 | | */ |
4738 | 0 | cand_type = btf_type_by_id(d->btf, cand_id); |
4739 | 0 | if (!btf_shallow_equal_struct(t, cand_type)) |
4740 | 0 | continue; |
4741 | | |
4742 | 0 | btf_dedup_clear_hypot_map(d); |
4743 | 0 | eq = btf_dedup_is_equiv(d, type_id, cand_id); |
4744 | 0 | if (eq < 0) |
4745 | 0 | return eq; |
4746 | 0 | if (!eq) |
4747 | 0 | continue; |
4748 | 0 | btf_dedup_merge_hypot_map(d); |
4749 | 0 | if (d->hypot_adjust_canon) /* not really equivalent */ |
4750 | 0 | continue; |
4751 | 0 | new_id = cand_id; |
4752 | 0 | break; |
4753 | 0 | } |
4754 | | |
4755 | 0 | d->map[type_id] = new_id; |
4756 | 0 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
4757 | 0 | return -ENOMEM; |
4758 | | |
4759 | 0 | return 0; |
4760 | 0 | } |
4761 | | |
4762 | | static int btf_dedup_struct_types(struct btf_dedup *d) |
4763 | 0 | { |
4764 | 0 | int i, err; |
4765 | |
|
4766 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
4767 | 0 | err = btf_dedup_struct_type(d, d->btf->start_id + i); |
4768 | 0 | if (err) |
4769 | 0 | return err; |
4770 | 0 | } |
4771 | 0 | return 0; |
4772 | 0 | } |
4773 | | |
4774 | | /* |
4775 | | * Deduplicate reference type. |
4776 | | * |
4777 | | * Once all primitive and struct/union types got deduplicated, we can easily |
4778 | | * deduplicate all other (reference) BTF types. This is done in two steps: |
4779 | | * |
4780 | | * 1. Resolve all referenced type IDs into their canonical type IDs. This |
4781 | | * resolution can be done either immediately for primitive or struct/union types |
4782 | | * (because they were deduped in previous two phases) or recursively for |
4783 | | * reference types. Recursion will always terminate at either primitive or |
4784 | | * struct/union type, at which point we can "unwind" chain of reference types |
4785 | | * one by one. There is no danger of encountering cycles because in C type |
4786 | | * system the only way to form type cycle is through struct/union, so any chain |
4787 | | * of reference types, even those taking part in a type cycle, will inevitably |
4788 | | * reach struct/union at some point. |
4789 | | * |
4790 | | * 2. Once all referenced type IDs are resolved into canonical ones, BTF type |
4791 | | * becomes "stable", in the sense that no further deduplication will cause |
4792 | | * any changes to it. With that, it's now possible to calculate type's signature |
4793 | | * hash (this time taking into account referenced type IDs) and loop over all |
4794 | | * potential canonical representatives. If no match was found, current type |
4795 | | * will become canonical representative of itself and will be added into |
4796 | | * btf_dedup->dedup_table as another possible canonical representative. |
4797 | | */ |
4798 | | static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id) |
4799 | 0 | { |
4800 | 0 | struct hashmap_entry *hash_entry; |
4801 | 0 | __u32 new_id = type_id, cand_id; |
4802 | 0 | struct btf_type *t, *cand; |
4803 | | /* if we don't find equivalent type, then we are representative type */ |
4804 | 0 | int ref_type_id; |
4805 | 0 | long h; |
4806 | |
|
4807 | 0 | if (d->map[type_id] == BTF_IN_PROGRESS_ID) |
4808 | 0 | return -ELOOP; |
4809 | 0 | if (d->map[type_id] <= BTF_MAX_NR_TYPES) |
4810 | 0 | return resolve_type_id(d, type_id); |
4811 | | |
4812 | 0 | t = btf_type_by_id(d->btf, type_id); |
4813 | 0 | d->map[type_id] = BTF_IN_PROGRESS_ID; |
4814 | |
|
4815 | 0 | switch (btf_kind(t)) { |
4816 | 0 | case BTF_KIND_CONST: |
4817 | 0 | case BTF_KIND_VOLATILE: |
4818 | 0 | case BTF_KIND_RESTRICT: |
4819 | 0 | case BTF_KIND_PTR: |
4820 | 0 | case BTF_KIND_TYPEDEF: |
4821 | 0 | case BTF_KIND_FUNC: |
4822 | 0 | case BTF_KIND_TYPE_TAG: |
4823 | 0 | ref_type_id = btf_dedup_ref_type(d, t->type); |
4824 | 0 | if (ref_type_id < 0) |
4825 | 0 | return ref_type_id; |
4826 | 0 | t->type = ref_type_id; |
4827 | |
|
4828 | 0 | h = btf_hash_common(t); |
4829 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4830 | 0 | cand_id = hash_entry->value; |
4831 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4832 | 0 | if (btf_equal_common(t, cand)) { |
4833 | 0 | new_id = cand_id; |
4834 | 0 | break; |
4835 | 0 | } |
4836 | 0 | } |
4837 | 0 | break; |
4838 | | |
4839 | 0 | case BTF_KIND_DECL_TAG: |
4840 | 0 | ref_type_id = btf_dedup_ref_type(d, t->type); |
4841 | 0 | if (ref_type_id < 0) |
4842 | 0 | return ref_type_id; |
4843 | 0 | t->type = ref_type_id; |
4844 | |
|
4845 | 0 | h = btf_hash_int_decl_tag(t); |
4846 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4847 | 0 | cand_id = hash_entry->value; |
4848 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4849 | 0 | if (btf_equal_int_tag(t, cand)) { |
4850 | 0 | new_id = cand_id; |
4851 | 0 | break; |
4852 | 0 | } |
4853 | 0 | } |
4854 | 0 | break; |
4855 | | |
4856 | 0 | case BTF_KIND_ARRAY: { |
4857 | 0 | struct btf_array *info = btf_array(t); |
4858 | |
|
4859 | 0 | ref_type_id = btf_dedup_ref_type(d, info->type); |
4860 | 0 | if (ref_type_id < 0) |
4861 | 0 | return ref_type_id; |
4862 | 0 | info->type = ref_type_id; |
4863 | |
|
4864 | 0 | ref_type_id = btf_dedup_ref_type(d, info->index_type); |
4865 | 0 | if (ref_type_id < 0) |
4866 | 0 | return ref_type_id; |
4867 | 0 | info->index_type = ref_type_id; |
4868 | |
|
4869 | 0 | h = btf_hash_array(t); |
4870 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4871 | 0 | cand_id = hash_entry->value; |
4872 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4873 | 0 | if (btf_equal_array(t, cand)) { |
4874 | 0 | new_id = cand_id; |
4875 | 0 | break; |
4876 | 0 | } |
4877 | 0 | } |
4878 | 0 | break; |
4879 | 0 | } |
4880 | | |
4881 | 0 | case BTF_KIND_FUNC_PROTO: { |
4882 | 0 | struct btf_param *param; |
4883 | 0 | __u16 vlen; |
4884 | 0 | int i; |
4885 | |
|
4886 | 0 | ref_type_id = btf_dedup_ref_type(d, t->type); |
4887 | 0 | if (ref_type_id < 0) |
4888 | 0 | return ref_type_id; |
4889 | 0 | t->type = ref_type_id; |
4890 | |
|
4891 | 0 | vlen = btf_vlen(t); |
4892 | 0 | param = btf_params(t); |
4893 | 0 | for (i = 0; i < vlen; i++) { |
4894 | 0 | ref_type_id = btf_dedup_ref_type(d, param->type); |
4895 | 0 | if (ref_type_id < 0) |
4896 | 0 | return ref_type_id; |
4897 | 0 | param->type = ref_type_id; |
4898 | 0 | param++; |
4899 | 0 | } |
4900 | | |
4901 | 0 | h = btf_hash_fnproto(t); |
4902 | 0 | for_each_dedup_cand(d, hash_entry, h) { |
4903 | 0 | cand_id = hash_entry->value; |
4904 | 0 | cand = btf_type_by_id(d->btf, cand_id); |
4905 | 0 | if (btf_equal_fnproto(t, cand)) { |
4906 | 0 | new_id = cand_id; |
4907 | 0 | break; |
4908 | 0 | } |
4909 | 0 | } |
4910 | 0 | break; |
4911 | 0 | } |
4912 | | |
4913 | 0 | default: |
4914 | 0 | return -EINVAL; |
4915 | 0 | } |
4916 | | |
4917 | 0 | d->map[type_id] = new_id; |
4918 | 0 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
4919 | 0 | return -ENOMEM; |
4920 | | |
4921 | 0 | return new_id; |
4922 | 0 | } |
4923 | | |
4924 | | static int btf_dedup_ref_types(struct btf_dedup *d) |
4925 | 0 | { |
4926 | 0 | int i, err; |
4927 | |
|
4928 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
4929 | 0 | err = btf_dedup_ref_type(d, d->btf->start_id + i); |
4930 | 0 | if (err < 0) |
4931 | 0 | return err; |
4932 | 0 | } |
4933 | | /* we won't need d->dedup_table anymore */ |
4934 | 0 | hashmap__free(d->dedup_table); |
4935 | 0 | d->dedup_table = NULL; |
4936 | 0 | return 0; |
4937 | 0 | } |
4938 | | |
4939 | | /* |
4940 | | * Collect a map from type names to type ids for all canonical structs |
4941 | | * and unions. If the same name is shared by several canonical types |
4942 | | * use a special value 0 to indicate this fact. |
4943 | | */ |
4944 | | static int btf_dedup_fill_unique_names_map(struct btf_dedup *d, struct hashmap *names_map) |
4945 | 0 | { |
4946 | 0 | __u32 nr_types = btf__type_cnt(d->btf); |
4947 | 0 | struct btf_type *t; |
4948 | 0 | __u32 type_id; |
4949 | 0 | __u16 kind; |
4950 | 0 | int err; |
4951 | | |
4952 | | /* |
4953 | | * Iterate over base and split module ids in order to get all |
4954 | | * available structs in the map. |
4955 | | */ |
4956 | 0 | for (type_id = 1; type_id < nr_types; ++type_id) { |
4957 | 0 | t = btf_type_by_id(d->btf, type_id); |
4958 | 0 | kind = btf_kind(t); |
4959 | |
|
4960 | 0 | if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION) |
4961 | 0 | continue; |
4962 | | |
4963 | | /* Skip non-canonical types */ |
4964 | 0 | if (type_id != d->map[type_id]) |
4965 | 0 | continue; |
4966 | | |
4967 | 0 | err = hashmap__add(names_map, t->name_off, type_id); |
4968 | 0 | if (err == -EEXIST) |
4969 | 0 | err = hashmap__set(names_map, t->name_off, 0, NULL, NULL); |
4970 | |
|
4971 | 0 | if (err) |
4972 | 0 | return err; |
4973 | 0 | } |
4974 | | |
4975 | 0 | return 0; |
4976 | 0 | } |
4977 | | |
4978 | | static int btf_dedup_resolve_fwd(struct btf_dedup *d, struct hashmap *names_map, __u32 type_id) |
4979 | 0 | { |
4980 | 0 | struct btf_type *t = btf_type_by_id(d->btf, type_id); |
4981 | 0 | enum btf_fwd_kind fwd_kind = btf_kflag(t); |
4982 | 0 | __u16 cand_kind, kind = btf_kind(t); |
4983 | 0 | struct btf_type *cand_t; |
4984 | 0 | uintptr_t cand_id; |
4985 | |
|
4986 | 0 | if (kind != BTF_KIND_FWD) |
4987 | 0 | return 0; |
4988 | | |
4989 | | /* Skip if this FWD already has a mapping */ |
4990 | 0 | if (type_id != d->map[type_id]) |
4991 | 0 | return 0; |
4992 | | |
4993 | 0 | if (!hashmap__find(names_map, t->name_off, &cand_id)) |
4994 | 0 | return 0; |
4995 | | |
4996 | | /* Zero is a special value indicating that name is not unique */ |
4997 | 0 | if (!cand_id) |
4998 | 0 | return 0; |
4999 | | |
5000 | 0 | cand_t = btf_type_by_id(d->btf, cand_id); |
5001 | 0 | cand_kind = btf_kind(cand_t); |
5002 | 0 | if ((cand_kind == BTF_KIND_STRUCT && fwd_kind != BTF_FWD_STRUCT) || |
5003 | 0 | (cand_kind == BTF_KIND_UNION && fwd_kind != BTF_FWD_UNION)) |
5004 | 0 | return 0; |
5005 | | |
5006 | 0 | d->map[type_id] = cand_id; |
5007 | |
|
5008 | 0 | return 0; |
5009 | 0 | } |
5010 | | |
5011 | | /* |
5012 | | * Resolve unambiguous forward declarations. |
5013 | | * |
5014 | | * The lion's share of all FWD declarations is resolved during |
5015 | | * `btf_dedup_struct_types` phase when different type graphs are |
5016 | | * compared against each other. However, if in some compilation unit a |
5017 | | * FWD declaration is not a part of a type graph compared against |
5018 | | * another type graph that declaration's canonical type would not be |
5019 | | * changed. Example: |
5020 | | * |
5021 | | * CU #1: |
5022 | | * |
5023 | | * struct foo; |
5024 | | * struct foo *some_global; |
5025 | | * |
5026 | | * CU #2: |
5027 | | * |
5028 | | * struct foo { int u; }; |
5029 | | * struct foo *another_global; |
5030 | | * |
5031 | | * After `btf_dedup_struct_types` the BTF looks as follows: |
5032 | | * |
5033 | | * [1] STRUCT 'foo' size=4 vlen=1 ... |
5034 | | * [2] INT 'int' size=4 ... |
5035 | | * [3] PTR '(anon)' type_id=1 |
5036 | | * [4] FWD 'foo' fwd_kind=struct |
5037 | | * [5] PTR '(anon)' type_id=4 |
5038 | | * |
5039 | | * This pass assumes that such FWD declarations should be mapped to |
5040 | | * structs or unions with identical name in case if the name is not |
5041 | | * ambiguous. |
5042 | | */ |
5043 | | static int btf_dedup_resolve_fwds(struct btf_dedup *d) |
5044 | 0 | { |
5045 | 0 | int i, err; |
5046 | 0 | struct hashmap *names_map; |
5047 | |
|
5048 | 0 | names_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL); |
5049 | 0 | if (IS_ERR(names_map)) |
5050 | 0 | return PTR_ERR(names_map); |
5051 | | |
5052 | 0 | err = btf_dedup_fill_unique_names_map(d, names_map); |
5053 | 0 | if (err < 0) |
5054 | 0 | goto exit; |
5055 | | |
5056 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
5057 | 0 | err = btf_dedup_resolve_fwd(d, names_map, d->btf->start_id + i); |
5058 | 0 | if (err < 0) |
5059 | 0 | break; |
5060 | 0 | } |
5061 | |
|
5062 | 0 | exit: |
5063 | 0 | hashmap__free(names_map); |
5064 | 0 | return err; |
5065 | 0 | } |
5066 | | |
5067 | | /* |
5068 | | * Compact types. |
5069 | | * |
5070 | | * After we established for each type its corresponding canonical representative |
5071 | | * type, we now can eliminate types that are not canonical and leave only |
5072 | | * canonical ones layed out sequentially in memory by copying them over |
5073 | | * duplicates. During compaction btf_dedup->hypot_map array is reused to store |
5074 | | * a map from original type ID to a new compacted type ID, which will be used |
5075 | | * during next phase to "fix up" type IDs, referenced from struct/union and |
5076 | | * reference types. |
5077 | | */ |
5078 | | static int btf_dedup_compact_types(struct btf_dedup *d) |
5079 | 0 | { |
5080 | 0 | __u32 *new_offs; |
5081 | 0 | __u32 next_type_id = d->btf->start_id; |
5082 | 0 | const struct btf_type *t; |
5083 | 0 | void *p; |
5084 | 0 | int i, id, len; |
5085 | | |
5086 | | /* we are going to reuse hypot_map to store compaction remapping */ |
5087 | 0 | d->hypot_map[0] = 0; |
5088 | | /* base BTF types are not renumbered */ |
5089 | 0 | for (id = 1; id < d->btf->start_id; id++) |
5090 | 0 | d->hypot_map[id] = id; |
5091 | 0 | for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) |
5092 | 0 | d->hypot_map[id] = BTF_UNPROCESSED_ID; |
5093 | |
|
5094 | 0 | p = d->btf->types_data; |
5095 | |
|
5096 | 0 | for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) { |
5097 | 0 | if (d->map[id] != id) |
5098 | 0 | continue; |
5099 | | |
5100 | 0 | t = btf__type_by_id(d->btf, id); |
5101 | 0 | len = btf_type_size(t); |
5102 | 0 | if (len < 0) |
5103 | 0 | return len; |
5104 | | |
5105 | 0 | memmove(p, t, len); |
5106 | 0 | d->hypot_map[id] = next_type_id; |
5107 | 0 | d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data; |
5108 | 0 | p += len; |
5109 | 0 | next_type_id++; |
5110 | 0 | } |
5111 | | |
5112 | | /* shrink struct btf's internal types index and update btf_header */ |
5113 | 0 | d->btf->nr_types = next_type_id - d->btf->start_id; |
5114 | 0 | d->btf->type_offs_cap = d->btf->nr_types; |
5115 | 0 | d->btf->hdr->type_len = p - d->btf->types_data; |
5116 | 0 | new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap, |
5117 | 0 | sizeof(*new_offs)); |
5118 | 0 | if (d->btf->type_offs_cap && !new_offs) |
5119 | 0 | return -ENOMEM; |
5120 | 0 | d->btf->type_offs = new_offs; |
5121 | 0 | d->btf->hdr->str_off = d->btf->hdr->type_len; |
5122 | 0 | d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len; |
5123 | 0 | return 0; |
5124 | 0 | } |
5125 | | |
5126 | | /* |
5127 | | * Figure out final (deduplicated and compacted) type ID for provided original |
5128 | | * `type_id` by first resolving it into corresponding canonical type ID and |
5129 | | * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map, |
5130 | | * which is populated during compaction phase. |
5131 | | */ |
5132 | | static int btf_dedup_remap_type_id(__u32 *type_id, void *ctx) |
5133 | 0 | { |
5134 | 0 | struct btf_dedup *d = ctx; |
5135 | 0 | __u32 resolved_type_id, new_type_id; |
5136 | |
|
5137 | 0 | resolved_type_id = resolve_type_id(d, *type_id); |
5138 | 0 | new_type_id = d->hypot_map[resolved_type_id]; |
5139 | 0 | if (new_type_id > BTF_MAX_NR_TYPES) |
5140 | 0 | return -EINVAL; |
5141 | | |
5142 | 0 | *type_id = new_type_id; |
5143 | 0 | return 0; |
5144 | 0 | } |
5145 | | |
5146 | | /* |
5147 | | * Remap referenced type IDs into deduped type IDs. |
5148 | | * |
5149 | | * After BTF types are deduplicated and compacted, their final type IDs may |
5150 | | * differ from original ones. The map from original to a corresponding |
5151 | | * deduped type ID is stored in btf_dedup->hypot_map and is populated during |
5152 | | * compaction phase. During remapping phase we are rewriting all type IDs |
5153 | | * referenced from any BTF type (e.g., struct fields, func proto args, etc) to |
5154 | | * their final deduped type IDs. |
5155 | | */ |
5156 | | static int btf_dedup_remap_types(struct btf_dedup *d) |
5157 | 0 | { |
5158 | 0 | int i, r; |
5159 | |
|
5160 | 0 | for (i = 0; i < d->btf->nr_types; i++) { |
5161 | 0 | struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); |
5162 | 0 | struct btf_field_iter it; |
5163 | 0 | __u32 *type_id; |
5164 | |
|
5165 | 0 | r = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS); |
5166 | 0 | if (r) |
5167 | 0 | return r; |
5168 | | |
5169 | 0 | while ((type_id = btf_field_iter_next(&it))) { |
5170 | 0 | __u32 resolved_id, new_id; |
5171 | |
|
5172 | 0 | resolved_id = resolve_type_id(d, *type_id); |
5173 | 0 | new_id = d->hypot_map[resolved_id]; |
5174 | 0 | if (new_id > BTF_MAX_NR_TYPES) |
5175 | 0 | return -EINVAL; |
5176 | | |
5177 | 0 | *type_id = new_id; |
5178 | 0 | } |
5179 | 0 | } |
5180 | | |
5181 | 0 | if (!d->btf_ext) |
5182 | 0 | return 0; |
5183 | | |
5184 | 0 | r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d); |
5185 | 0 | if (r) |
5186 | 0 | return r; |
5187 | | |
5188 | 0 | return 0; |
5189 | 0 | } |
5190 | | |
5191 | | /* |
5192 | | * Probe few well-known locations for vmlinux kernel image and try to load BTF |
5193 | | * data out of it to use for target BTF. |
5194 | | */ |
5195 | | struct btf *btf__load_vmlinux_btf(void) |
5196 | 0 | { |
5197 | 0 | const char *sysfs_btf_path = "/sys/kernel/btf/vmlinux"; |
5198 | | /* fall back locations, trying to find vmlinux on disk */ |
5199 | 0 | const char *locations[] = { |
5200 | 0 | "/boot/vmlinux-%1$s", |
5201 | 0 | "/lib/modules/%1$s/vmlinux-%1$s", |
5202 | 0 | "/lib/modules/%1$s/build/vmlinux", |
5203 | 0 | "/usr/lib/modules/%1$s/kernel/vmlinux", |
5204 | 0 | "/usr/lib/debug/boot/vmlinux-%1$s", |
5205 | 0 | "/usr/lib/debug/boot/vmlinux-%1$s.debug", |
5206 | 0 | "/usr/lib/debug/lib/modules/%1$s/vmlinux", |
5207 | 0 | }; |
5208 | 0 | char path[PATH_MAX + 1]; |
5209 | 0 | struct utsname buf; |
5210 | 0 | struct btf *btf; |
5211 | 0 | int i, err; |
5212 | | |
5213 | | /* is canonical sysfs location accessible? */ |
5214 | 0 | if (faccessat(AT_FDCWD, sysfs_btf_path, F_OK, AT_EACCESS) < 0) { |
5215 | 0 | pr_warn("kernel BTF is missing at '%s', was CONFIG_DEBUG_INFO_BTF enabled?\n", |
5216 | 0 | sysfs_btf_path); |
5217 | 0 | } else { |
5218 | 0 | btf = btf__parse(sysfs_btf_path, NULL); |
5219 | 0 | if (!btf) { |
5220 | 0 | err = -errno; |
5221 | 0 | pr_warn("failed to read kernel BTF from '%s': %d\n", sysfs_btf_path, err); |
5222 | 0 | return libbpf_err_ptr(err); |
5223 | 0 | } |
5224 | 0 | pr_debug("loaded kernel BTF from '%s'\n", sysfs_btf_path); |
5225 | 0 | return btf; |
5226 | 0 | } |
5227 | | |
5228 | | /* try fallback locations */ |
5229 | 0 | uname(&buf); |
5230 | 0 | for (i = 0; i < ARRAY_SIZE(locations); i++) { |
5231 | 0 | snprintf(path, PATH_MAX, locations[i], buf.release); |
5232 | |
|
5233 | 0 | if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS)) |
5234 | 0 | continue; |
5235 | | |
5236 | 0 | btf = btf__parse(path, NULL); |
5237 | 0 | err = libbpf_get_error(btf); |
5238 | 0 | pr_debug("loading kernel BTF '%s': %d\n", path, err); |
5239 | 0 | if (err) |
5240 | 0 | continue; |
5241 | | |
5242 | 0 | return btf; |
5243 | 0 | } |
5244 | | |
5245 | 0 | pr_warn("failed to find valid kernel BTF\n"); |
5246 | 0 | return libbpf_err_ptr(-ESRCH); |
5247 | 0 | } |
5248 | | |
5249 | | struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf"))); |
5250 | | |
5251 | | struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf) |
5252 | 0 | { |
5253 | 0 | char path[80]; |
5254 | |
|
5255 | 0 | snprintf(path, sizeof(path), "/sys/kernel/btf/%s", module_name); |
5256 | 0 | return btf__parse_split(path, vmlinux_btf); |
5257 | 0 | } |
5258 | | |
5259 | | int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx) |
5260 | 0 | { |
5261 | 0 | const struct btf_ext_info *seg; |
5262 | 0 | struct btf_ext_info_sec *sec; |
5263 | 0 | int i, err; |
5264 | |
|
5265 | 0 | seg = &btf_ext->func_info; |
5266 | 0 | for_each_btf_ext_sec(seg, sec) { |
5267 | 0 | struct bpf_func_info_min *rec; |
5268 | |
|
5269 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5270 | 0 | err = visit(&rec->type_id, ctx); |
5271 | 0 | if (err < 0) |
5272 | 0 | return err; |
5273 | 0 | } |
5274 | 0 | } |
5275 | | |
5276 | 0 | seg = &btf_ext->core_relo_info; |
5277 | 0 | for_each_btf_ext_sec(seg, sec) { |
5278 | 0 | struct bpf_core_relo *rec; |
5279 | |
|
5280 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5281 | 0 | err = visit(&rec->type_id, ctx); |
5282 | 0 | if (err < 0) |
5283 | 0 | return err; |
5284 | 0 | } |
5285 | 0 | } |
5286 | | |
5287 | 0 | return 0; |
5288 | 0 | } |
5289 | | |
5290 | | int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx) |
5291 | 0 | { |
5292 | 0 | const struct btf_ext_info *seg; |
5293 | 0 | struct btf_ext_info_sec *sec; |
5294 | 0 | int i, err; |
5295 | |
|
5296 | 0 | seg = &btf_ext->func_info; |
5297 | 0 | for_each_btf_ext_sec(seg, sec) { |
5298 | 0 | err = visit(&sec->sec_name_off, ctx); |
5299 | 0 | if (err) |
5300 | 0 | return err; |
5301 | 0 | } |
5302 | | |
5303 | 0 | seg = &btf_ext->line_info; |
5304 | 0 | for_each_btf_ext_sec(seg, sec) { |
5305 | 0 | struct bpf_line_info_min *rec; |
5306 | |
|
5307 | 0 | err = visit(&sec->sec_name_off, ctx); |
5308 | 0 | if (err) |
5309 | 0 | return err; |
5310 | | |
5311 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5312 | 0 | err = visit(&rec->file_name_off, ctx); |
5313 | 0 | if (err) |
5314 | 0 | return err; |
5315 | 0 | err = visit(&rec->line_off, ctx); |
5316 | 0 | if (err) |
5317 | 0 | return err; |
5318 | 0 | } |
5319 | 0 | } |
5320 | | |
5321 | 0 | seg = &btf_ext->core_relo_info; |
5322 | 0 | for_each_btf_ext_sec(seg, sec) { |
5323 | 0 | struct bpf_core_relo *rec; |
5324 | |
|
5325 | 0 | err = visit(&sec->sec_name_off, ctx); |
5326 | 0 | if (err) |
5327 | 0 | return err; |
5328 | | |
5329 | 0 | for_each_btf_ext_rec(seg, sec, i, rec) { |
5330 | 0 | err = visit(&rec->access_str_off, ctx); |
5331 | 0 | if (err) |
5332 | 0 | return err; |
5333 | 0 | } |
5334 | 0 | } |
5335 | | |
5336 | 0 | return 0; |
5337 | 0 | } |
5338 | | |
5339 | | struct btf_distill { |
5340 | | struct btf_pipe pipe; |
5341 | | int *id_map; |
5342 | | unsigned int split_start_id; |
5343 | | unsigned int split_start_str; |
5344 | | int diff_id; |
5345 | | }; |
5346 | | |
5347 | | static int btf_add_distilled_type_ids(struct btf_distill *dist, __u32 i) |
5348 | 0 | { |
5349 | 0 | struct btf_type *split_t = btf_type_by_id(dist->pipe.src, i); |
5350 | 0 | struct btf_field_iter it; |
5351 | 0 | __u32 *id; |
5352 | 0 | int err; |
5353 | |
|
5354 | 0 | err = btf_field_iter_init(&it, split_t, BTF_FIELD_ITER_IDS); |
5355 | 0 | if (err) |
5356 | 0 | return err; |
5357 | 0 | while ((id = btf_field_iter_next(&it))) { |
5358 | 0 | struct btf_type *base_t; |
5359 | |
|
5360 | 0 | if (!*id) |
5361 | 0 | continue; |
5362 | | /* split BTF id, not needed */ |
5363 | 0 | if (*id >= dist->split_start_id) |
5364 | 0 | continue; |
5365 | | /* already added ? */ |
5366 | 0 | if (dist->id_map[*id] > 0) |
5367 | 0 | continue; |
5368 | | |
5369 | | /* only a subset of base BTF types should be referenced from |
5370 | | * split BTF; ensure nothing unexpected is referenced. |
5371 | | */ |
5372 | 0 | base_t = btf_type_by_id(dist->pipe.src, *id); |
5373 | 0 | switch (btf_kind(base_t)) { |
5374 | 0 | case BTF_KIND_INT: |
5375 | 0 | case BTF_KIND_FLOAT: |
5376 | 0 | case BTF_KIND_FWD: |
5377 | 0 | case BTF_KIND_ARRAY: |
5378 | 0 | case BTF_KIND_STRUCT: |
5379 | 0 | case BTF_KIND_UNION: |
5380 | 0 | case BTF_KIND_TYPEDEF: |
5381 | 0 | case BTF_KIND_ENUM: |
5382 | 0 | case BTF_KIND_ENUM64: |
5383 | 0 | case BTF_KIND_PTR: |
5384 | 0 | case BTF_KIND_CONST: |
5385 | 0 | case BTF_KIND_RESTRICT: |
5386 | 0 | case BTF_KIND_VOLATILE: |
5387 | 0 | case BTF_KIND_FUNC_PROTO: |
5388 | 0 | case BTF_KIND_TYPE_TAG: |
5389 | 0 | dist->id_map[*id] = *id; |
5390 | 0 | break; |
5391 | 0 | default: |
5392 | 0 | pr_warn("unexpected reference to base type[%u] of kind [%u] when creating distilled base BTF.\n", |
5393 | 0 | *id, btf_kind(base_t)); |
5394 | 0 | return -EINVAL; |
5395 | 0 | } |
5396 | | /* If a base type is used, ensure types it refers to are |
5397 | | * marked as used also; so for example if we find a PTR to INT |
5398 | | * we need both the PTR and INT. |
5399 | | * |
5400 | | * The only exception is named struct/unions, since distilled |
5401 | | * base BTF composite types have no members. |
5402 | | */ |
5403 | 0 | if (btf_is_composite(base_t) && base_t->name_off) |
5404 | 0 | continue; |
5405 | 0 | err = btf_add_distilled_type_ids(dist, *id); |
5406 | 0 | if (err) |
5407 | 0 | return err; |
5408 | 0 | } |
5409 | 0 | return 0; |
5410 | 0 | } |
5411 | | |
5412 | | static int btf_add_distilled_types(struct btf_distill *dist) |
5413 | 0 | { |
5414 | 0 | bool adding_to_base = dist->pipe.dst->start_id == 1; |
5415 | 0 | int id = btf__type_cnt(dist->pipe.dst); |
5416 | 0 | struct btf_type *t; |
5417 | 0 | int i, err = 0; |
5418 | | |
5419 | | |
5420 | | /* Add types for each of the required references to either distilled |
5421 | | * base or split BTF, depending on type characteristics. |
5422 | | */ |
5423 | 0 | for (i = 1; i < dist->split_start_id; i++) { |
5424 | 0 | const char *name; |
5425 | 0 | int kind; |
5426 | |
|
5427 | 0 | if (!dist->id_map[i]) |
5428 | 0 | continue; |
5429 | 0 | t = btf_type_by_id(dist->pipe.src, i); |
5430 | 0 | kind = btf_kind(t); |
5431 | 0 | name = btf__name_by_offset(dist->pipe.src, t->name_off); |
5432 | |
|
5433 | 0 | switch (kind) { |
5434 | 0 | case BTF_KIND_INT: |
5435 | 0 | case BTF_KIND_FLOAT: |
5436 | 0 | case BTF_KIND_FWD: |
5437 | | /* Named int, float, fwd are added to base. */ |
5438 | 0 | if (!adding_to_base) |
5439 | 0 | continue; |
5440 | 0 | err = btf_add_type(&dist->pipe, t); |
5441 | 0 | break; |
5442 | 0 | case BTF_KIND_STRUCT: |
5443 | 0 | case BTF_KIND_UNION: |
5444 | | /* Named struct/union are added to base as 0-vlen |
5445 | | * struct/union of same size. Anonymous struct/unions |
5446 | | * are added to split BTF as-is. |
5447 | | */ |
5448 | 0 | if (adding_to_base) { |
5449 | 0 | if (!t->name_off) |
5450 | 0 | continue; |
5451 | 0 | err = btf_add_composite(dist->pipe.dst, kind, name, t->size); |
5452 | 0 | } else { |
5453 | 0 | if (t->name_off) |
5454 | 0 | continue; |
5455 | 0 | err = btf_add_type(&dist->pipe, t); |
5456 | 0 | } |
5457 | 0 | break; |
5458 | 0 | case BTF_KIND_ENUM: |
5459 | 0 | case BTF_KIND_ENUM64: |
5460 | | /* Named enum[64]s are added to base as a sized |
5461 | | * enum; relocation will match with appropriately-named |
5462 | | * and sized enum or enum64. |
5463 | | * |
5464 | | * Anonymous enums are added to split BTF as-is. |
5465 | | */ |
5466 | 0 | if (adding_to_base) { |
5467 | 0 | if (!t->name_off) |
5468 | 0 | continue; |
5469 | 0 | err = btf__add_enum(dist->pipe.dst, name, t->size); |
5470 | 0 | } else { |
5471 | 0 | if (t->name_off) |
5472 | 0 | continue; |
5473 | 0 | err = btf_add_type(&dist->pipe, t); |
5474 | 0 | } |
5475 | 0 | break; |
5476 | 0 | case BTF_KIND_ARRAY: |
5477 | 0 | case BTF_KIND_TYPEDEF: |
5478 | 0 | case BTF_KIND_PTR: |
5479 | 0 | case BTF_KIND_CONST: |
5480 | 0 | case BTF_KIND_RESTRICT: |
5481 | 0 | case BTF_KIND_VOLATILE: |
5482 | 0 | case BTF_KIND_FUNC_PROTO: |
5483 | 0 | case BTF_KIND_TYPE_TAG: |
5484 | | /* All other types are added to split BTF. */ |
5485 | 0 | if (adding_to_base) |
5486 | 0 | continue; |
5487 | 0 | err = btf_add_type(&dist->pipe, t); |
5488 | 0 | break; |
5489 | 0 | default: |
5490 | 0 | pr_warn("unexpected kind when adding base type '%s'[%u] of kind [%u] to distilled base BTF.\n", |
5491 | 0 | name, i, kind); |
5492 | 0 | return -EINVAL; |
5493 | |
|
5494 | 0 | } |
5495 | 0 | if (err < 0) |
5496 | 0 | break; |
5497 | 0 | dist->id_map[i] = id++; |
5498 | 0 | } |
5499 | 0 | return err; |
5500 | 0 | } |
5501 | | |
5502 | | /* Split BTF ids without a mapping will be shifted downwards since distilled |
5503 | | * base BTF is smaller than the original base BTF. For those that have a |
5504 | | * mapping (either to base or updated split BTF), update the id based on |
5505 | | * that mapping. |
5506 | | */ |
5507 | | static int btf_update_distilled_type_ids(struct btf_distill *dist, __u32 i) |
5508 | 0 | { |
5509 | 0 | struct btf_type *t = btf_type_by_id(dist->pipe.dst, i); |
5510 | 0 | struct btf_field_iter it; |
5511 | 0 | __u32 *id; |
5512 | 0 | int err; |
5513 | |
|
5514 | 0 | err = btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS); |
5515 | 0 | if (err) |
5516 | 0 | return err; |
5517 | 0 | while ((id = btf_field_iter_next(&it))) { |
5518 | 0 | if (dist->id_map[*id]) |
5519 | 0 | *id = dist->id_map[*id]; |
5520 | 0 | else if (*id >= dist->split_start_id) |
5521 | 0 | *id -= dist->diff_id; |
5522 | 0 | } |
5523 | 0 | return 0; |
5524 | 0 | } |
5525 | | |
5526 | | /* Create updated split BTF with distilled base BTF; distilled base BTF |
5527 | | * consists of BTF information required to clarify the types that split |
5528 | | * BTF refers to, omitting unneeded details. Specifically it will contain |
5529 | | * base types and memberless definitions of named structs, unions and enumerated |
5530 | | * types. Associated reference types like pointers, arrays and anonymous |
5531 | | * structs, unions and enumerated types will be added to split BTF. |
5532 | | * Size is recorded for named struct/unions to help guide matching to the |
5533 | | * target base BTF during later relocation. |
5534 | | * |
5535 | | * The only case where structs, unions or enumerated types are fully represented |
5536 | | * is when they are anonymous; in such cases, the anonymous type is added to |
5537 | | * split BTF in full. |
5538 | | * |
5539 | | * We return newly-created split BTF where the split BTF refers to a newly-created |
5540 | | * distilled base BTF. Both must be freed separately by the caller. |
5541 | | */ |
5542 | | int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf, |
5543 | | struct btf **new_split_btf) |
5544 | 0 | { |
5545 | 0 | struct btf *new_base = NULL, *new_split = NULL; |
5546 | 0 | const struct btf *old_base; |
5547 | 0 | unsigned int n = btf__type_cnt(src_btf); |
5548 | 0 | struct btf_distill dist = {}; |
5549 | 0 | struct btf_type *t; |
5550 | 0 | int i, err = 0; |
5551 | | |
5552 | | /* src BTF must be split BTF. */ |
5553 | 0 | old_base = btf__base_btf(src_btf); |
5554 | 0 | if (!new_base_btf || !new_split_btf || !old_base) |
5555 | 0 | return libbpf_err(-EINVAL); |
5556 | | |
5557 | 0 | new_base = btf__new_empty(); |
5558 | 0 | if (!new_base) |
5559 | 0 | return libbpf_err(-ENOMEM); |
5560 | | |
5561 | 0 | btf__set_endianness(new_base, btf__endianness(src_btf)); |
5562 | |
|
5563 | 0 | dist.id_map = calloc(n, sizeof(*dist.id_map)); |
5564 | 0 | if (!dist.id_map) { |
5565 | 0 | err = -ENOMEM; |
5566 | 0 | goto done; |
5567 | 0 | } |
5568 | 0 | dist.pipe.src = src_btf; |
5569 | 0 | dist.pipe.dst = new_base; |
5570 | 0 | dist.pipe.str_off_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL); |
5571 | 0 | if (IS_ERR(dist.pipe.str_off_map)) { |
5572 | 0 | err = -ENOMEM; |
5573 | 0 | goto done; |
5574 | 0 | } |
5575 | 0 | dist.split_start_id = btf__type_cnt(old_base); |
5576 | 0 | dist.split_start_str = old_base->hdr->str_len; |
5577 | | |
5578 | | /* Pass over src split BTF; generate the list of base BTF type ids it |
5579 | | * references; these will constitute our distilled BTF set to be |
5580 | | * distributed over base and split BTF as appropriate. |
5581 | | */ |
5582 | 0 | for (i = src_btf->start_id; i < n; i++) { |
5583 | 0 | err = btf_add_distilled_type_ids(&dist, i); |
5584 | 0 | if (err < 0) |
5585 | 0 | goto done; |
5586 | 0 | } |
5587 | | /* Next add types for each of the required references to base BTF and split BTF |
5588 | | * in turn. |
5589 | | */ |
5590 | 0 | err = btf_add_distilled_types(&dist); |
5591 | 0 | if (err < 0) |
5592 | 0 | goto done; |
5593 | | |
5594 | | /* Create new split BTF with distilled base BTF as its base; the final |
5595 | | * state is split BTF with distilled base BTF that represents enough |
5596 | | * about its base references to allow it to be relocated with the base |
5597 | | * BTF available. |
5598 | | */ |
5599 | 0 | new_split = btf__new_empty_split(new_base); |
5600 | 0 | if (!new_split) { |
5601 | 0 | err = -errno; |
5602 | 0 | goto done; |
5603 | 0 | } |
5604 | 0 | dist.pipe.dst = new_split; |
5605 | | /* First add all split types */ |
5606 | 0 | for (i = src_btf->start_id; i < n; i++) { |
5607 | 0 | t = btf_type_by_id(src_btf, i); |
5608 | 0 | err = btf_add_type(&dist.pipe, t); |
5609 | 0 | if (err < 0) |
5610 | 0 | goto done; |
5611 | 0 | } |
5612 | | /* Now add distilled types to split BTF that are not added to base. */ |
5613 | 0 | err = btf_add_distilled_types(&dist); |
5614 | 0 | if (err < 0) |
5615 | 0 | goto done; |
5616 | | |
5617 | | /* All split BTF ids will be shifted downwards since there are less base |
5618 | | * BTF ids in distilled base BTF. |
5619 | | */ |
5620 | 0 | dist.diff_id = dist.split_start_id - btf__type_cnt(new_base); |
5621 | |
|
5622 | 0 | n = btf__type_cnt(new_split); |
5623 | | /* Now update base/split BTF ids. */ |
5624 | 0 | for (i = 1; i < n; i++) { |
5625 | 0 | err = btf_update_distilled_type_ids(&dist, i); |
5626 | 0 | if (err < 0) |
5627 | 0 | break; |
5628 | 0 | } |
5629 | 0 | done: |
5630 | 0 | free(dist.id_map); |
5631 | 0 | hashmap__free(dist.pipe.str_off_map); |
5632 | 0 | if (err) { |
5633 | 0 | btf__free(new_split); |
5634 | 0 | btf__free(new_base); |
5635 | 0 | return libbpf_err(err); |
5636 | 0 | } |
5637 | 0 | *new_base_btf = new_base; |
5638 | 0 | *new_split_btf = new_split; |
5639 | |
|
5640 | 0 | return 0; |
5641 | 0 | } |
5642 | | |
5643 | | const struct btf_header *btf_header(const struct btf *btf) |
5644 | 0 | { |
5645 | 0 | return btf->hdr; |
5646 | 0 | } |
5647 | | |
5648 | | void btf_set_base_btf(struct btf *btf, const struct btf *base_btf) |
5649 | 0 | { |
5650 | 0 | btf->base_btf = (struct btf *)base_btf; |
5651 | 0 | btf->start_id = btf__type_cnt(base_btf); |
5652 | 0 | btf->start_str_off = base_btf->hdr->str_len; |
5653 | 0 | } |
5654 | | |
5655 | | int btf__relocate(struct btf *btf, const struct btf *base_btf) |
5656 | 0 | { |
5657 | 0 | int err = btf_relocate(btf, base_btf, NULL); |
5658 | |
|
5659 | 0 | if (!err) |
5660 | 0 | btf->owns_base = false; |
5661 | 0 | return libbpf_err(err); |
5662 | 0 | } |