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