blob: 4843e44916f7c454939ba0c32b174ac3ab077efa [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002/* Copyright (c) 2018 Facebook */
3
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -03004#include <endian.h>
Yonghong Song96408c42019-02-04 11:00:58 -08005#include <stdio.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006#include <stdlib.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07007#include <string.h>
Andrii Nakryikoe6c64852019-05-24 11:58:57 -07008#include <fcntl.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07009#include <unistd.h>
10#include <errno.h>
Martin KaFai Laufb2426a2020-01-15 15:00:31 -080011#include <sys/utsname.h>
12#include <sys/param.h>
13#include <sys/stat.h>
14#include <linux/kernel.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070015#include <linux/err.h>
16#include <linux/btf.h>
Andrii Nakryikoe6c64852019-05-24 11:58:57 -070017#include <gelf.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070018#include "btf.h"
19#include "bpf.h"
Yonghong Song8461ef82019-02-01 16:14:14 -080020#include "libbpf.h"
Andrii Nakryikod72386f2019-05-15 20:39:27 -070021#include "libbpf_internal.h"
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -070022#include "hashmap.h"
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070023
Andrii Nakryiko1d1a3bc2020-01-10 10:19:16 -080024/* make sure libbpf doesn't use kernel-only integer typedefs */
25#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
26
Martin KaFai Laufb2426a2020-01-15 15:00:31 -080027#define BTF_MAX_NR_TYPES 0x7fffffffU
28#define BTF_MAX_STR_OFFSET 0x7fffffffU
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070029
30static struct btf_type btf_void;
31
32struct btf {
33 union {
34 struct btf_header *hdr;
35 void *data;
36 };
37 struct btf_type **types;
38 const char *strings;
39 void *nohdr_data;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -070040 __u32 nr_types;
41 __u32 types_size;
42 __u32 data_size;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070043 int fd;
44};
45
Yonghong Songd7f5b5e2018-11-19 15:29:18 -080046static inline __u64 ptr_to_u64(const void *ptr)
47{
48 return (__u64) (unsigned long) ptr;
49}
50
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070051static int btf_add_type(struct btf *btf, struct btf_type *t)
52{
53 if (btf->types_size - btf->nr_types < 2) {
54 struct btf_type **new_types;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -070055 __u32 expand_by, new_size;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070056
57 if (btf->types_size == BTF_MAX_NR_TYPES)
58 return -E2BIG;
59
Martin KaFai Laufb2426a2020-01-15 15:00:31 -080060 expand_by = max(btf->types_size >> 2, 16U);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070061 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
62
63 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
64 if (!new_types)
65 return -ENOMEM;
66
67 if (btf->nr_types == 0)
68 new_types[0] = &btf_void;
69
70 btf->types = new_types;
71 btf->types_size = new_size;
72 }
73
74 btf->types[++(btf->nr_types)] = t;
75
76 return 0;
77}
78
Yonghong Song8461ef82019-02-01 16:14:14 -080079static int btf_parse_hdr(struct btf *btf)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070080{
81 const struct btf_header *hdr = btf->hdr;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -070082 __u32 meta_left;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070083
84 if (btf->data_size < sizeof(struct btf_header)) {
Yonghong Song8461ef82019-02-01 16:14:14 -080085 pr_debug("BTF header not found\n");
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070086 return -EINVAL;
87 }
88
89 if (hdr->magic != BTF_MAGIC) {
Yonghong Song8461ef82019-02-01 16:14:14 -080090 pr_debug("Invalid BTF magic:%x\n", hdr->magic);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070091 return -EINVAL;
92 }
93
94 if (hdr->version != BTF_VERSION) {
Yonghong Song8461ef82019-02-01 16:14:14 -080095 pr_debug("Unsupported BTF version:%u\n", hdr->version);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070096 return -ENOTSUP;
97 }
98
99 if (hdr->flags) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800100 pr_debug("Unsupported BTF flags:%x\n", hdr->flags);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700101 return -ENOTSUP;
102 }
103
104 meta_left = btf->data_size - sizeof(*hdr);
105 if (!meta_left) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800106 pr_debug("BTF has no data\n");
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700107 return -EINVAL;
108 }
109
110 if (meta_left < hdr->type_off) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800111 pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700112 return -EINVAL;
113 }
114
115 if (meta_left < hdr->str_off) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800116 pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700117 return -EINVAL;
118 }
119
120 if (hdr->type_off >= hdr->str_off) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800121 pr_debug("BTF type section offset >= string section offset. No type?\n");
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700122 return -EINVAL;
123 }
124
125 if (hdr->type_off & 0x02) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800126 pr_debug("BTF type section is not aligned to 4 bytes\n");
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700127 return -EINVAL;
128 }
129
130 btf->nohdr_data = btf->hdr + 1;
131
132 return 0;
133}
134
Yonghong Song8461ef82019-02-01 16:14:14 -0800135static int btf_parse_str_sec(struct btf *btf)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700136{
137 const struct btf_header *hdr = btf->hdr;
138 const char *start = btf->nohdr_data + hdr->str_off;
139 const char *end = start + btf->hdr->str_len;
140
Andrii Nakryiko5aab3922019-02-15 19:52:18 -0800141 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700142 start[0] || end[-1]) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800143 pr_debug("Invalid BTF string section\n");
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700144 return -EINVAL;
145 }
146
147 btf->strings = start;
148
149 return 0;
150}
151
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800152static int btf_type_size(struct btf_type *t)
153{
154 int base_size = sizeof(struct btf_type);
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700155 __u16 vlen = btf_vlen(t);
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800156
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700157 switch (btf_kind(t)) {
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800158 case BTF_KIND_FWD:
159 case BTF_KIND_CONST:
160 case BTF_KIND_VOLATILE:
161 case BTF_KIND_RESTRICT:
162 case BTF_KIND_PTR:
163 case BTF_KIND_TYPEDEF:
164 case BTF_KIND_FUNC:
165 return base_size;
166 case BTF_KIND_INT:
167 return base_size + sizeof(__u32);
168 case BTF_KIND_ENUM:
169 return base_size + vlen * sizeof(struct btf_enum);
170 case BTF_KIND_ARRAY:
171 return base_size + sizeof(struct btf_array);
172 case BTF_KIND_STRUCT:
173 case BTF_KIND_UNION:
174 return base_size + vlen * sizeof(struct btf_member);
175 case BTF_KIND_FUNC_PROTO:
176 return base_size + vlen * sizeof(struct btf_param);
Daniel Borkmann1713d682019-04-09 23:20:14 +0200177 case BTF_KIND_VAR:
178 return base_size + sizeof(struct btf_var);
179 case BTF_KIND_DATASEC:
180 return base_size + vlen * sizeof(struct btf_var_secinfo);
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800181 default:
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700182 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800183 return -EINVAL;
184 }
185}
186
Yonghong Song8461ef82019-02-01 16:14:14 -0800187static int btf_parse_type_sec(struct btf *btf)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700188{
189 struct btf_header *hdr = btf->hdr;
190 void *nohdr_data = btf->nohdr_data;
191 void *next_type = nohdr_data + hdr->type_off;
192 void *end_type = nohdr_data + hdr->str_off;
193
194 while (next_type < end_type) {
195 struct btf_type *t = next_type;
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800196 int type_size;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700197 int err;
198
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800199 type_size = btf_type_size(t);
200 if (type_size < 0)
201 return type_size;
202 next_type += type_size;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700203 err = btf_add_type(btf, t);
204 if (err)
205 return err;
206 }
207
208 return 0;
209}
210
Andrii Nakryiko9c651122019-02-04 17:29:46 -0800211__u32 btf__get_nr_types(const struct btf *btf)
212{
213 return btf->nr_types;
214}
215
Martin KaFai Lau38d5d3b2018-07-24 08:40:22 -0700216const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700217{
218 if (type_id > btf->nr_types)
219 return NULL;
220
221 return btf->types[type_id];
222}
223
224static bool btf_type_is_void(const struct btf_type *t)
225{
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700226 return t == &btf_void || btf_is_fwd(t);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700227}
228
229static bool btf_type_is_void_or_null(const struct btf_type *t)
230{
231 return !t || btf_type_is_void(t);
232}
233
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700234#define MAX_RESOLVE_DEPTH 32
235
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700236__s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700237{
238 const struct btf_array *array;
239 const struct btf_type *t;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700240 __u32 nelems = 1;
241 __s64 size = -1;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700242 int i;
243
Okash Khawaja92b57122018-07-13 21:57:02 -0700244 t = btf__type_by_id(btf, type_id);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700245 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
246 i++) {
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700247 switch (btf_kind(t)) {
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800248 case BTF_KIND_INT:
249 case BTF_KIND_STRUCT:
250 case BTF_KIND_UNION:
251 case BTF_KIND_ENUM:
Daniel Borkmann1713d682019-04-09 23:20:14 +0200252 case BTF_KIND_DATASEC:
Andrii Nakryiko69eaab042019-02-04 17:29:44 -0800253 size = t->size;
254 goto done;
255 case BTF_KIND_PTR:
256 size = sizeof(void *);
257 goto done;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700258 case BTF_KIND_TYPEDEF:
259 case BTF_KIND_VOLATILE:
260 case BTF_KIND_CONST:
261 case BTF_KIND_RESTRICT:
Daniel Borkmann1713d682019-04-09 23:20:14 +0200262 case BTF_KIND_VAR:
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700263 type_id = t->type;
264 break;
265 case BTF_KIND_ARRAY:
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700266 array = btf_array(t);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700267 if (nelems && array->nelems > UINT32_MAX / nelems)
268 return -E2BIG;
269 nelems *= array->nelems;
270 type_id = array->type;
271 break;
272 default:
273 return -EINVAL;
274 }
275
Okash Khawaja92b57122018-07-13 21:57:02 -0700276 t = btf__type_by_id(btf, type_id);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700277 }
278
Andrii Nakryiko994021a2019-11-06 18:08:54 -0800279done:
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700280 if (size < 0)
281 return -EINVAL;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700282 if (nelems && size > UINT32_MAX / nelems)
283 return -E2BIG;
284
285 return nelems * size;
286}
287
Andrii Nakryiko3d208f42019-12-13 17:43:30 -0800288int btf__align_of(const struct btf *btf, __u32 id)
289{
290 const struct btf_type *t = btf__type_by_id(btf, id);
291 __u16 kind = btf_kind(t);
292
293 switch (kind) {
294 case BTF_KIND_INT:
295 case BTF_KIND_ENUM:
Martin KaFai Laufb2426a2020-01-15 15:00:31 -0800296 return min(sizeof(void *), (size_t)t->size);
Andrii Nakryiko3d208f42019-12-13 17:43:30 -0800297 case BTF_KIND_PTR:
298 return sizeof(void *);
299 case BTF_KIND_TYPEDEF:
300 case BTF_KIND_VOLATILE:
301 case BTF_KIND_CONST:
302 case BTF_KIND_RESTRICT:
303 return btf__align_of(btf, t->type);
304 case BTF_KIND_ARRAY:
305 return btf__align_of(btf, btf_array(t)->type);
306 case BTF_KIND_STRUCT:
307 case BTF_KIND_UNION: {
308 const struct btf_member *m = btf_members(t);
309 __u16 vlen = btf_vlen(t);
Prashant Bholea79ac2d2019-12-16 17:27:38 +0900310 int i, max_align = 1, align;
Andrii Nakryiko3d208f42019-12-13 17:43:30 -0800311
312 for (i = 0; i < vlen; i++, m++) {
Prashant Bholea79ac2d2019-12-16 17:27:38 +0900313 align = btf__align_of(btf, m->type);
314 if (align <= 0)
315 return align;
316 max_align = max(max_align, align);
Andrii Nakryiko3d208f42019-12-13 17:43:30 -0800317 }
318
Prashant Bholea79ac2d2019-12-16 17:27:38 +0900319 return max_align;
Andrii Nakryiko3d208f42019-12-13 17:43:30 -0800320 }
321 default:
322 pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
323 return 0;
324 }
325}
326
Okash Khawaja92b57122018-07-13 21:57:02 -0700327int btf__resolve_type(const struct btf *btf, __u32 type_id)
328{
329 const struct btf_type *t;
330 int depth = 0;
331
332 t = btf__type_by_id(btf, type_id);
333 while (depth < MAX_RESOLVE_DEPTH &&
334 !btf_type_is_void_or_null(t) &&
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700335 (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
Okash Khawaja92b57122018-07-13 21:57:02 -0700336 type_id = t->type;
337 t = btf__type_by_id(btf, type_id);
338 depth++;
339 }
340
341 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
342 return -EINVAL;
343
344 return type_id;
345}
346
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700347__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700348{
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700349 __u32 i;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700350
351 if (!strcmp(type_name, "void"))
352 return 0;
353
354 for (i = 1; i <= btf->nr_types; i++) {
355 const struct btf_type *t = btf->types[i];
Okash Khawaja92b57122018-07-13 21:57:02 -0700356 const char *name = btf__name_by_offset(btf, t->name_off);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700357
358 if (name && !strcmp(type_name, name))
359 return i;
360 }
361
362 return -ENOENT;
363}
364
Alexei Starovoitov1442e282019-11-14 10:57:05 -0800365__s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
366 __u32 kind)
367{
368 __u32 i;
369
370 if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
371 return 0;
372
373 for (i = 1; i <= btf->nr_types; i++) {
374 const struct btf_type *t = btf->types[i];
375 const char *name;
376
377 if (btf_kind(t) != kind)
378 continue;
379 name = btf__name_by_offset(btf, t->name_off);
380 if (name && !strcmp(type_name, name))
381 return i;
382 }
383
384 return -ENOENT;
385}
386
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700387void btf__free(struct btf *btf)
388{
Andrii Nakryiko50450fc2020-07-29 16:21:48 -0700389 if (IS_ERR_OR_NULL(btf))
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700390 return;
391
Andrii Nakryiko81372e12020-07-07 18:53:14 -0700392 if (btf->fd >= 0)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700393 close(btf->fd);
394
395 free(btf->data);
396 free(btf->types);
397 free(btf);
398}
399
Andrii Nakryiko5c3320d2020-07-09 18:10:23 -0700400struct btf *btf__new(const void *data, __u32 size)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700401{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700402 struct btf *btf;
403 int err;
404
405 btf = calloc(1, sizeof(struct btf));
406 if (!btf)
407 return ERR_PTR(-ENOMEM);
408
409 btf->fd = -1;
410
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700411 btf->data = malloc(size);
412 if (!btf->data) {
413 err = -ENOMEM;
414 goto done;
415 }
416
417 memcpy(btf->data, data, size);
418 btf->data_size = size;
419
Yonghong Song8461ef82019-02-01 16:14:14 -0800420 err = btf_parse_hdr(btf);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700421 if (err)
422 goto done;
423
Yonghong Song8461ef82019-02-01 16:14:14 -0800424 err = btf_parse_str_sec(btf);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700425 if (err)
426 goto done;
427
Yonghong Song8461ef82019-02-01 16:14:14 -0800428 err = btf_parse_type_sec(btf);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700429
430done:
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700431 if (err) {
432 btf__free(btf);
433 return ERR_PTR(err);
434 }
435
436 return btf;
437}
438
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700439static bool btf_check_endianness(const GElf_Ehdr *ehdr)
440{
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -0300441#if __BYTE_ORDER == __LITTLE_ENDIAN
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700442 return ehdr->e_ident[EI_DATA] == ELFDATA2LSB;
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -0300443#elif __BYTE_ORDER == __BIG_ENDIAN
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700444 return ehdr->e_ident[EI_DATA] == ELFDATA2MSB;
445#else
446# error "Unrecognized __BYTE_ORDER__"
447#endif
448}
449
450struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
451{
452 Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
453 int err = 0, fd = -1, idx = 0;
454 struct btf *btf = NULL;
455 Elf_Scn *scn = NULL;
456 Elf *elf = NULL;
457 GElf_Ehdr ehdr;
458
459 if (elf_version(EV_CURRENT) == EV_NONE) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800460 pr_warn("failed to init libelf for %s\n", path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700461 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
462 }
463
464 fd = open(path, O_RDONLY);
465 if (fd < 0) {
466 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +0800467 pr_warn("failed to open %s: %s\n", path, strerror(errno));
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700468 return ERR_PTR(err);
469 }
470
471 err = -LIBBPF_ERRNO__FORMAT;
472
473 elf = elf_begin(fd, ELF_C_READ, NULL);
474 if (!elf) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800475 pr_warn("failed to open %s as ELF file\n", path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700476 goto done;
477 }
478 if (!gelf_getehdr(elf, &ehdr)) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800479 pr_warn("failed to get EHDR from %s\n", path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700480 goto done;
481 }
482 if (!btf_check_endianness(&ehdr)) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800483 pr_warn("non-native ELF endianness is not supported\n");
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700484 goto done;
485 }
486 if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800487 pr_warn("failed to get e_shstrndx from %s\n", path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700488 goto done;
489 }
490
491 while ((scn = elf_nextscn(elf, scn)) != NULL) {
492 GElf_Shdr sh;
493 char *name;
494
495 idx++;
496 if (gelf_getshdr(scn, &sh) != &sh) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800497 pr_warn("failed to get section(%d) header from %s\n",
498 idx, path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700499 goto done;
500 }
501 name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
502 if (!name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800503 pr_warn("failed to get section(%d) name from %s\n",
504 idx, path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700505 goto done;
506 }
507 if (strcmp(name, BTF_ELF_SEC) == 0) {
508 btf_data = elf_getdata(scn, 0);
509 if (!btf_data) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800510 pr_warn("failed to get section(%d, %s) data from %s\n",
511 idx, name, path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700512 goto done;
513 }
514 continue;
515 } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
516 btf_ext_data = elf_getdata(scn, 0);
517 if (!btf_ext_data) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800518 pr_warn("failed to get section(%d, %s) data from %s\n",
519 idx, name, path);
Andrii Nakryikoe6c64852019-05-24 11:58:57 -0700520 goto done;
521 }
522 continue;
523 }
524 }
525
526 err = 0;
527
528 if (!btf_data) {
529 err = -ENOENT;
530 goto done;
531 }
532 btf = btf__new(btf_data->d_buf, btf_data->d_size);
533 if (IS_ERR(btf))
534 goto done;
535
536 if (btf_ext && btf_ext_data) {
537 *btf_ext = btf_ext__new(btf_ext_data->d_buf,
538 btf_ext_data->d_size);
539 if (IS_ERR(*btf_ext))
540 goto done;
541 } else if (btf_ext) {
542 *btf_ext = NULL;
543 }
544done:
545 if (elf)
546 elf_end(elf);
547 close(fd);
548
549 if (err)
550 return ERR_PTR(err);
551 /*
552 * btf is always parsed before btf_ext, so no need to clean up
553 * btf_ext, if btf loading failed
554 */
555 if (IS_ERR(btf))
556 return btf;
557 if (btf_ext && IS_ERR(*btf_ext)) {
558 btf__free(btf);
559 err = PTR_ERR(*btf_ext);
560 return ERR_PTR(err);
561 }
562 return btf;
563}
564
Andrii Nakryiko94a1fedd62020-08-01 18:32:17 -0700565struct btf *btf__parse_raw(const char *path)
566{
Daniel T. Lee932ac542020-08-06 07:33:59 +0900567 struct btf *btf = NULL;
Andrii Nakryiko94a1fedd62020-08-01 18:32:17 -0700568 void *data = NULL;
Andrii Nakryiko94a1fedd62020-08-01 18:32:17 -0700569 FILE *f = NULL;
570 __u16 magic;
571 int err = 0;
572 long sz;
573
574 f = fopen(path, "rb");
575 if (!f) {
576 err = -errno;
577 goto err_out;
578 }
579
580 /* check BTF magic */
581 if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
582 err = -EIO;
583 goto err_out;
584 }
585 if (magic != BTF_MAGIC) {
586 /* definitely not a raw BTF */
587 err = -EPROTO;
588 goto err_out;
589 }
590
591 /* get file size */
592 if (fseek(f, 0, SEEK_END)) {
593 err = -errno;
594 goto err_out;
595 }
596 sz = ftell(f);
597 if (sz < 0) {
598 err = -errno;
599 goto err_out;
600 }
601 /* rewind to the start */
602 if (fseek(f, 0, SEEK_SET)) {
603 err = -errno;
604 goto err_out;
605 }
606
607 /* pre-alloc memory and read all of BTF data */
608 data = malloc(sz);
609 if (!data) {
610 err = -ENOMEM;
611 goto err_out;
612 }
613 if (fread(data, 1, sz, f) < sz) {
614 err = -EIO;
615 goto err_out;
616 }
617
618 /* finally parse BTF data */
619 btf = btf__new(data, sz);
620
621err_out:
622 free(data);
623 if (f)
624 fclose(f);
625 return err ? ERR_PTR(err) : btf;
626}
627
628struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
629{
630 struct btf *btf;
631
632 if (btf_ext)
633 *btf_ext = NULL;
634
635 btf = btf__parse_raw(path);
636 if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
637 return btf;
638
639 return btf__parse_elf(path, btf_ext);
640}
641
Daniel Borkmann1713d682019-04-09 23:20:14 +0200642static int compare_vsi_off(const void *_a, const void *_b)
643{
644 const struct btf_var_secinfo *a = _a;
645 const struct btf_var_secinfo *b = _b;
646
647 return a->offset - b->offset;
648}
649
650static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
651 struct btf_type *t)
652{
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700653 __u32 size = 0, off = 0, i, vars = btf_vlen(t);
Daniel Borkmann1713d682019-04-09 23:20:14 +0200654 const char *name = btf__name_by_offset(btf, t->name_off);
655 const struct btf_type *t_var;
656 struct btf_var_secinfo *vsi;
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700657 const struct btf_var *var;
Daniel Borkmann1713d682019-04-09 23:20:14 +0200658 int ret;
659
660 if (!name) {
661 pr_debug("No name found in string section for DATASEC kind.\n");
662 return -ENOENT;
663 }
664
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800665 /* .extern datasec size and var offsets were set correctly during
666 * extern collection step, so just skip straight to sorting variables
667 */
668 if (t->size)
669 goto sort_vars;
670
Daniel Borkmann1713d682019-04-09 23:20:14 +0200671 ret = bpf_object__section_size(obj, name, &size);
672 if (ret || !size || (t->size && t->size != size)) {
673 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
674 return -ENOENT;
675 }
676
677 t->size = size;
678
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700679 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
Daniel Borkmann1713d682019-04-09 23:20:14 +0200680 t_var = btf__type_by_id(btf, vsi->type);
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700681 var = btf_var(t_var);
Daniel Borkmann1713d682019-04-09 23:20:14 +0200682
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700683 if (!btf_is_var(t_var)) {
Daniel Borkmann1713d682019-04-09 23:20:14 +0200684 pr_debug("Non-VAR type seen in section %s\n", name);
685 return -EINVAL;
686 }
687
688 if (var->linkage == BTF_VAR_STATIC)
689 continue;
690
691 name = btf__name_by_offset(btf, t_var->name_off);
692 if (!name) {
693 pr_debug("No name found in string section for VAR kind\n");
694 return -ENOENT;
695 }
696
697 ret = bpf_object__variable_offset(obj, name, &off);
698 if (ret) {
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700699 pr_debug("No offset found in symbol table for VAR %s\n",
700 name);
Daniel Borkmann1713d682019-04-09 23:20:14 +0200701 return -ENOENT;
702 }
703
704 vsi->offset = off;
705 }
706
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800707sort_vars:
708 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
Daniel Borkmann1713d682019-04-09 23:20:14 +0200709 return 0;
710}
711
712int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
713{
714 int err = 0;
715 __u32 i;
716
717 for (i = 1; i <= btf->nr_types; i++) {
718 struct btf_type *t = btf->types[i];
719
720 /* Loader needs to fix up some of the things compiler
721 * couldn't get its hands on while emitting BTF. This
722 * is section size and global variable offset. We use
723 * the info from the ELF itself for this purpose.
724 */
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700725 if (btf_is_datasec(t)) {
Daniel Borkmann1713d682019-04-09 23:20:14 +0200726 err = btf_fixup_datasec(obj, btf, t);
727 if (err)
728 break;
729 }
730 }
731
732 return err;
733}
734
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800735int btf__load(struct btf *btf)
736{
Stanislav Fomichev8395f322020-03-25 12:55:21 -0700737 __u32 log_buf_size = 0;
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800738 char *log_buf = NULL;
739 int err = 0;
740
741 if (btf->fd >= 0)
742 return -EEXIST;
743
Stanislav Fomichev8395f322020-03-25 12:55:21 -0700744retry_load:
745 if (log_buf_size) {
746 log_buf = malloc(log_buf_size);
747 if (!log_buf)
748 return -ENOMEM;
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800749
Stanislav Fomichev8395f322020-03-25 12:55:21 -0700750 *log_buf = 0;
751 }
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800752
753 btf->fd = bpf_load_btf(btf->data, btf->data_size,
754 log_buf, log_buf_size, false);
755 if (btf->fd < 0) {
Stanislav Fomichev8395f322020-03-25 12:55:21 -0700756 if (!log_buf || errno == ENOSPC) {
757 log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
758 log_buf_size << 1);
759 free(log_buf);
760 goto retry_load;
761 }
762
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800763 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +0800764 pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800765 if (*log_buf)
Kefeng Wangbe180102019-10-21 13:55:32 +0800766 pr_warn("%s\n", log_buf);
Andrii Nakryikod29d87f2019-02-08 11:19:36 -0800767 goto done;
768 }
769
770done:
771 free(log_buf);
772 return err;
773}
774
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700775int btf__fd(const struct btf *btf)
776{
777 return btf->fd;
778}
Okash Khawaja92b57122018-07-13 21:57:02 -0700779
Andrii Nakryiko81372e12020-07-07 18:53:14 -0700780void btf__set_fd(struct btf *btf, int fd)
781{
782 btf->fd = fd;
783}
784
Andrii Nakryiko02c87442019-02-08 11:19:37 -0800785const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
786{
787 *size = btf->data_size;
788 return btf->data;
789}
790
Okash Khawaja92b57122018-07-13 21:57:02 -0700791const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
792{
793 if (offset < btf->hdr->str_len)
794 return &btf->strings[offset];
795 else
796 return NULL;
797}
Yonghong Song2993e052018-11-19 15:29:16 -0800798
Martin KaFai Lau1d2f44c2018-11-23 16:44:32 -0800799int btf__get_from_id(__u32 id, struct btf **btf)
Yonghong Songd7f5b5e2018-11-19 15:29:18 -0800800{
801 struct bpf_btf_info btf_info = { 0 };
802 __u32 len = sizeof(btf_info);
803 __u32 last_size;
804 int btf_fd;
805 void *ptr;
806 int err;
807
808 err = 0;
809 *btf = NULL;
810 btf_fd = bpf_btf_get_fd_by_id(id);
811 if (btf_fd < 0)
812 return 0;
813
814 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
815 * let's start with a sane default - 4KiB here - and resize it only if
816 * bpf_obj_get_info_by_fd() needs a bigger buffer.
817 */
818 btf_info.btf_size = 4096;
819 last_size = btf_info.btf_size;
820 ptr = malloc(last_size);
821 if (!ptr) {
822 err = -ENOMEM;
823 goto exit_free;
824 }
825
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800826 memset(ptr, 0, last_size);
Yonghong Songd7f5b5e2018-11-19 15:29:18 -0800827 btf_info.btf = ptr_to_u64(ptr);
828 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
829
830 if (!err && btf_info.btf_size > last_size) {
831 void *temp_ptr;
832
833 last_size = btf_info.btf_size;
834 temp_ptr = realloc(ptr, last_size);
835 if (!temp_ptr) {
836 err = -ENOMEM;
837 goto exit_free;
838 }
839 ptr = temp_ptr;
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800840 memset(ptr, 0, last_size);
Yonghong Songd7f5b5e2018-11-19 15:29:18 -0800841 btf_info.btf = ptr_to_u64(ptr);
842 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
843 }
844
845 if (err || btf_info.btf_size > last_size) {
846 err = errno;
847 goto exit_free;
848 }
849
Yonghong Song8461ef82019-02-01 16:14:14 -0800850 *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
Yonghong Songd7f5b5e2018-11-19 15:29:18 -0800851 if (IS_ERR(*btf)) {
852 err = PTR_ERR(*btf);
853 *btf = NULL;
854 }
855
856exit_free:
857 close(btf_fd);
858 free(ptr);
859
860 return err;
861}
862
Yonghong Songa6c109a2019-02-05 11:48:22 -0800863int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
Yonghong Song96408c42019-02-04 11:00:58 -0800864 __u32 expected_key_size, __u32 expected_value_size,
865 __u32 *key_type_id, __u32 *value_type_id)
866{
867 const struct btf_type *container_type;
868 const struct btf_member *key, *value;
869 const size_t max_name = 256;
870 char container_name[max_name];
871 __s64 key_size, value_size;
872 __s32 container_id;
873
874 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
875 max_name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800876 pr_warn("map:%s length of '____btf_map_%s' is too long\n",
877 map_name, map_name);
Yonghong Song96408c42019-02-04 11:00:58 -0800878 return -EINVAL;
879 }
880
881 container_id = btf__find_by_name(btf, container_name);
882 if (container_id < 0) {
Yonghong Songf7748e22019-02-05 21:38:30 -0800883 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
884 map_name, container_name);
Yonghong Song96408c42019-02-04 11:00:58 -0800885 return container_id;
886 }
887
888 container_type = btf__type_by_id(btf, container_id);
889 if (!container_type) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800890 pr_warn("map:%s cannot find BTF type for container_id:%u\n",
891 map_name, container_id);
Yonghong Song96408c42019-02-04 11:00:58 -0800892 return -EINVAL;
893 }
894
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700895 if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800896 pr_warn("map:%s container_name:%s is an invalid container struct\n",
897 map_name, container_name);
Yonghong Song96408c42019-02-04 11:00:58 -0800898 return -EINVAL;
899 }
900
Andrii Nakryikob03bc682019-08-07 14:39:49 -0700901 key = btf_members(container_type);
Yonghong Song96408c42019-02-04 11:00:58 -0800902 value = key + 1;
903
904 key_size = btf__resolve_size(btf, key->type);
905 if (key_size < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800906 pr_warn("map:%s invalid BTF key_type_size\n", map_name);
Yonghong Song96408c42019-02-04 11:00:58 -0800907 return key_size;
908 }
909
910 if (expected_key_size != key_size) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800911 pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
912 map_name, (__u32)key_size, expected_key_size);
Yonghong Song96408c42019-02-04 11:00:58 -0800913 return -EINVAL;
914 }
915
916 value_size = btf__resolve_size(btf, value->type);
917 if (value_size < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800918 pr_warn("map:%s invalid BTF value_type_size\n", map_name);
Yonghong Song96408c42019-02-04 11:00:58 -0800919 return value_size;
920 }
921
922 if (expected_value_size != value_size) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800923 pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
924 map_name, (__u32)value_size, expected_value_size);
Yonghong Song96408c42019-02-04 11:00:58 -0800925 return -EINVAL;
926 }
927
928 *key_type_id = key->type;
929 *value_type_id = value->type;
930
931 return 0;
932}
933
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800934struct btf_ext_sec_setup_param {
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800935 __u32 off;
936 __u32 len;
937 __u32 min_rec_size;
938 struct btf_ext_info *ext_info;
939 const char *desc;
940};
941
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800942static int btf_ext_setup_info(struct btf_ext *btf_ext,
943 struct btf_ext_sec_setup_param *ext_sec)
Yonghong Song2993e052018-11-19 15:29:16 -0800944{
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800945 const struct btf_ext_info_sec *sinfo;
946 struct btf_ext_info *ext_info;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800947 __u32 info_left, record_size;
948 /* The start of the info sec (including the __u32 record_size). */
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800949 void *info;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800950
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -0700951 if (ext_sec->len == 0)
952 return 0;
953
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800954 if (ext_sec->off & 0x03) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800955 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800956 ext_sec->desc);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800957 return -EINVAL;
958 }
959
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800960 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
961 info_left = ext_sec->len;
962
963 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800964 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800965 ext_sec->desc, ext_sec->off, ext_sec->len);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800966 return -EINVAL;
967 }
968
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800969 /* At least a record size */
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800970 if (info_left < sizeof(__u32)) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800971 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
Yonghong Song2993e052018-11-19 15:29:16 -0800972 return -EINVAL;
973 }
974
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800975 /* The record size needs to meet the minimum standard */
976 record_size = *(__u32 *)info;
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800977 if (record_size < ext_sec->min_rec_size ||
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800978 record_size & 0x03) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800979 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -0800980 ext_sec->desc, record_size);
Yonghong Song2993e052018-11-19 15:29:16 -0800981 return -EINVAL;
982 }
983
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800984 sinfo = info + sizeof(__u32);
985 info_left -= sizeof(__u32);
Yonghong Song2993e052018-11-19 15:29:16 -0800986
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800987 /* If no records, return failure now so .BTF.ext won't be used. */
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800988 if (!info_left) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800989 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
Yonghong Song2993e052018-11-19 15:29:16 -0800990 return -EINVAL;
991 }
992
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800993 while (info_left) {
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800994 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800995 __u64 total_record_size;
996 __u32 num_records;
997
998 if (info_left < sec_hdrlen) {
Yonghong Song8461ef82019-02-01 16:14:14 -0800999 pr_debug("%s section header is not found in .BTF.ext\n",
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001000 ext_sec->desc);
Yonghong Song2993e052018-11-19 15:29:16 -08001001 return -EINVAL;
1002 }
1003
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001004 num_records = sinfo->num_info;
Yonghong Song2993e052018-11-19 15:29:16 -08001005 if (num_records == 0) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001006 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001007 ext_sec->desc);
Yonghong Song2993e052018-11-19 15:29:16 -08001008 return -EINVAL;
1009 }
1010
1011 total_record_size = sec_hdrlen +
1012 (__u64)num_records * record_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001013 if (info_left < total_record_size) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001014 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001015 ext_sec->desc);
Yonghong Song2993e052018-11-19 15:29:16 -08001016 return -EINVAL;
1017 }
1018
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001019 info_left -= total_record_size;
Yonghong Song2993e052018-11-19 15:29:16 -08001020 sinfo = (void *)sinfo + total_record_size;
1021 }
1022
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001023 ext_info = ext_sec->ext_info;
1024 ext_info->len = ext_sec->len - sizeof(__u32);
1025 ext_info->rec_size = record_size;
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001026 ext_info->info = info + sizeof(__u32);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001027
Yonghong Song2993e052018-11-19 15:29:16 -08001028 return 0;
1029}
1030
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001031static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001032{
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001033 struct btf_ext_sec_setup_param param = {
1034 .off = btf_ext->hdr->func_info_off,
1035 .len = btf_ext->hdr->func_info_len,
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001036 .min_rec_size = sizeof(struct bpf_func_info_min),
1037 .ext_info = &btf_ext->func_info,
1038 .desc = "func_info"
1039 };
1040
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001041 return btf_ext_setup_info(btf_ext, &param);
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001042}
1043
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001044static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001045{
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001046 struct btf_ext_sec_setup_param param = {
1047 .off = btf_ext->hdr->line_info_off,
1048 .len = btf_ext->hdr->line_info_len,
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001049 .min_rec_size = sizeof(struct bpf_line_info_min),
1050 .ext_info = &btf_ext->line_info,
1051 .desc = "line_info",
1052 };
1053
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001054 return btf_ext_setup_info(btf_ext, &param);
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001055}
1056
Andrii Nakryiko511bb002019-10-15 11:28:45 -07001057static int btf_ext_setup_field_reloc(struct btf_ext *btf_ext)
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001058{
1059 struct btf_ext_sec_setup_param param = {
Andrii Nakryiko511bb002019-10-15 11:28:45 -07001060 .off = btf_ext->hdr->field_reloc_off,
1061 .len = btf_ext->hdr->field_reloc_len,
1062 .min_rec_size = sizeof(struct bpf_field_reloc),
1063 .ext_info = &btf_ext->field_reloc_info,
1064 .desc = "field_reloc",
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001065 };
1066
1067 return btf_ext_setup_info(btf_ext, &param);
1068}
1069
Yonghong Song8461ef82019-02-01 16:14:14 -08001070static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
Yonghong Song2993e052018-11-19 15:29:16 -08001071{
1072 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
Yonghong Song2993e052018-11-19 15:29:16 -08001073
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001074 if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
Yonghong Song2993e052018-11-19 15:29:16 -08001075 data_size < hdr->hdr_len) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001076 pr_debug("BTF.ext header not found");
Yonghong Song2993e052018-11-19 15:29:16 -08001077 return -EINVAL;
1078 }
1079
1080 if (hdr->magic != BTF_MAGIC) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001081 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
Yonghong Song2993e052018-11-19 15:29:16 -08001082 return -EINVAL;
1083 }
1084
1085 if (hdr->version != BTF_VERSION) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001086 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
Yonghong Song2993e052018-11-19 15:29:16 -08001087 return -ENOTSUP;
1088 }
1089
1090 if (hdr->flags) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001091 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
Yonghong Song2993e052018-11-19 15:29:16 -08001092 return -ENOTSUP;
1093 }
1094
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001095 if (data_size == hdr->hdr_len) {
Yonghong Song8461ef82019-02-01 16:14:14 -08001096 pr_debug("BTF.ext has no data\n");
Yonghong Song2993e052018-11-19 15:29:16 -08001097 return -EINVAL;
1098 }
1099
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001100 return 0;
Yonghong Song2993e052018-11-19 15:29:16 -08001101}
1102
1103void btf_ext__free(struct btf_ext *btf_ext)
1104{
Andrii Nakryiko50450fc2020-07-29 16:21:48 -07001105 if (IS_ERR_OR_NULL(btf_ext))
Yonghong Song2993e052018-11-19 15:29:16 -08001106 return;
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001107 free(btf_ext->data);
Yonghong Song2993e052018-11-19 15:29:16 -08001108 free(btf_ext);
1109}
1110
Yonghong Song8461ef82019-02-01 16:14:14 -08001111struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
Yonghong Song2993e052018-11-19 15:29:16 -08001112{
Yonghong Song2993e052018-11-19 15:29:16 -08001113 struct btf_ext *btf_ext;
Yonghong Song2993e052018-11-19 15:29:16 -08001114 int err;
1115
Yonghong Song8461ef82019-02-01 16:14:14 -08001116 err = btf_ext_parse_hdr(data, size);
Yonghong Song2993e052018-11-19 15:29:16 -08001117 if (err)
1118 return ERR_PTR(err);
1119
1120 btf_ext = calloc(1, sizeof(struct btf_ext));
1121 if (!btf_ext)
1122 return ERR_PTR(-ENOMEM);
1123
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001124 btf_ext->data_size = size;
1125 btf_ext->data = malloc(size);
1126 if (!btf_ext->data) {
1127 err = -ENOMEM;
1128 goto done;
Yonghong Song2993e052018-11-19 15:29:16 -08001129 }
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001130 memcpy(btf_ext->data, data, size);
Yonghong Song2993e052018-11-19 15:29:16 -08001131
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001132 if (btf_ext->hdr->hdr_len <
1133 offsetofend(struct btf_ext_header, line_info_len))
1134 goto done;
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001135 err = btf_ext_setup_func_info(btf_ext);
1136 if (err)
1137 goto done;
1138
1139 err = btf_ext_setup_line_info(btf_ext);
1140 if (err)
1141 goto done;
1142
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001143 if (btf_ext->hdr->hdr_len <
Andrii Nakryiko511bb002019-10-15 11:28:45 -07001144 offsetofend(struct btf_ext_header, field_reloc_len))
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001145 goto done;
Andrii Nakryiko511bb002019-10-15 11:28:45 -07001146 err = btf_ext_setup_field_reloc(btf_ext);
Andrii Nakryiko4cedc0d2019-08-07 14:39:50 -07001147 if (err)
1148 goto done;
1149
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001150done:
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001151 if (err) {
1152 btf_ext__free(btf_ext);
1153 return ERR_PTR(err);
1154 }
1155
Yonghong Song2993e052018-11-19 15:29:16 -08001156 return btf_ext;
1157}
1158
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001159const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
1160{
1161 *size = btf_ext->data_size;
1162 return btf_ext->data;
1163}
1164
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001165static int btf_ext_reloc_info(const struct btf *btf,
1166 const struct btf_ext_info *ext_info,
1167 const char *sec_name, __u32 insns_cnt,
1168 void **info, __u32 *cnt)
Yonghong Song2993e052018-11-19 15:29:16 -08001169{
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001170 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
1171 __u32 i, record_size, existing_len, records_len;
1172 struct btf_ext_info_sec *sinfo;
Yonghong Song2993e052018-11-19 15:29:16 -08001173 const char *info_sec_name;
1174 __u64 remain_len;
1175 void *data;
1176
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001177 record_size = ext_info->rec_size;
1178 sinfo = ext_info->info;
1179 remain_len = ext_info->len;
Yonghong Song2993e052018-11-19 15:29:16 -08001180 while (remain_len > 0) {
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001181 records_len = sinfo->num_info * record_size;
Yonghong Song2993e052018-11-19 15:29:16 -08001182 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
1183 if (strcmp(info_sec_name, sec_name)) {
1184 remain_len -= sec_hdrlen + records_len;
1185 sinfo = (void *)sinfo + sec_hdrlen + records_len;
1186 continue;
1187 }
1188
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001189 existing_len = (*cnt) * record_size;
1190 data = realloc(*info, existing_len + records_len);
Yonghong Song2993e052018-11-19 15:29:16 -08001191 if (!data)
1192 return -ENOMEM;
1193
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001194 memcpy(data + existing_len, sinfo->data, records_len);
Martin KaFai Lau84ecc1f2018-12-05 17:35:47 -08001195 /* adjust insn_off only, the rest data will be passed
Yonghong Song2993e052018-11-19 15:29:16 -08001196 * to the kernel.
1197 */
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001198 for (i = 0; i < sinfo->num_info; i++) {
1199 __u32 *insn_off;
Yonghong Song2993e052018-11-19 15:29:16 -08001200
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001201 insn_off = data + existing_len + (i * record_size);
1202 *insn_off = *insn_off / sizeof(struct bpf_insn) +
Yonghong Song2993e052018-11-19 15:29:16 -08001203 insns_cnt;
1204 }
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001205 *info = data;
1206 *cnt += sinfo->num_info;
Yonghong Song2993e052018-11-19 15:29:16 -08001207 return 0;
1208 }
1209
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001210 return -ENOENT;
1211}
1212
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001213int btf_ext__reloc_func_info(const struct btf *btf,
1214 const struct btf_ext *btf_ext,
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001215 const char *sec_name, __u32 insns_cnt,
1216 void **func_info, __u32 *cnt)
1217{
1218 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
1219 insns_cnt, func_info, cnt);
1220}
1221
Andrii Nakryikoae4ab4b2019-02-08 11:19:38 -08001222int btf_ext__reloc_line_info(const struct btf *btf,
1223 const struct btf_ext *btf_ext,
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001224 const char *sec_name, __u32 insns_cnt,
1225 void **line_info, __u32 *cnt)
1226{
1227 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
1228 insns_cnt, line_info, cnt);
1229}
1230
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001231__u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
1232{
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001233 return btf_ext->func_info.rec_size;
1234}
1235
1236__u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
1237{
1238 return btf_ext->line_info.rec_size;
Yonghong Song2993e052018-11-19 15:29:16 -08001239}
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001240
1241struct btf_dedup;
1242
1243static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1244 const struct btf_dedup_opts *opts);
1245static void btf_dedup_free(struct btf_dedup *d);
1246static int btf_dedup_strings(struct btf_dedup *d);
1247static int btf_dedup_prim_types(struct btf_dedup *d);
1248static int btf_dedup_struct_types(struct btf_dedup *d);
1249static int btf_dedup_ref_types(struct btf_dedup *d);
1250static int btf_dedup_compact_types(struct btf_dedup *d);
1251static int btf_dedup_remap_types(struct btf_dedup *d);
1252
1253/*
1254 * Deduplicate BTF types and strings.
1255 *
1256 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
1257 * section with all BTF type descriptors and string data. It overwrites that
1258 * memory in-place with deduplicated types and strings without any loss of
1259 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
1260 * is provided, all the strings referenced from .BTF.ext section are honored
1261 * and updated to point to the right offsets after deduplication.
1262 *
1263 * If function returns with error, type/string data might be garbled and should
1264 * be discarded.
1265 *
1266 * More verbose and detailed description of both problem btf_dedup is solving,
1267 * as well as solution could be found at:
1268 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
1269 *
1270 * Problem description and justification
1271 * =====================================
1272 *
1273 * BTF type information is typically emitted either as a result of conversion
1274 * from DWARF to BTF or directly by compiler. In both cases, each compilation
1275 * unit contains information about a subset of all the types that are used
1276 * in an application. These subsets are frequently overlapping and contain a lot
1277 * of duplicated information when later concatenated together into a single
1278 * binary. This algorithm ensures that each unique type is represented by single
1279 * BTF type descriptor, greatly reducing resulting size of BTF data.
1280 *
1281 * Compilation unit isolation and subsequent duplication of data is not the only
1282 * problem. The same type hierarchy (e.g., struct and all the type that struct
1283 * references) in different compilation units can be represented in BTF to
1284 * various degrees of completeness (or, rather, incompleteness) due to
1285 * struct/union forward declarations.
1286 *
1287 * Let's take a look at an example, that we'll use to better understand the
1288 * problem (and solution). Suppose we have two compilation units, each using
1289 * same `struct S`, but each of them having incomplete type information about
1290 * struct's fields:
1291 *
1292 * // CU #1:
1293 * struct S;
1294 * struct A {
1295 * int a;
1296 * struct A* self;
1297 * struct S* parent;
1298 * };
1299 * struct B;
1300 * struct S {
1301 * struct A* a_ptr;
1302 * struct B* b_ptr;
1303 * };
1304 *
1305 * // CU #2:
1306 * struct S;
1307 * struct A;
1308 * struct B {
1309 * int b;
1310 * struct B* self;
1311 * struct S* parent;
1312 * };
1313 * struct S {
1314 * struct A* a_ptr;
1315 * struct B* b_ptr;
1316 * };
1317 *
1318 * In case of CU #1, BTF data will know only that `struct B` exist (but no
1319 * more), but will know the complete type information about `struct A`. While
1320 * for CU #2, it will know full type information about `struct B`, but will
1321 * only know about forward declaration of `struct A` (in BTF terms, it will
1322 * have `BTF_KIND_FWD` type descriptor with name `B`).
1323 *
1324 * This compilation unit isolation means that it's possible that there is no
1325 * single CU with complete type information describing structs `S`, `A`, and
1326 * `B`. Also, we might get tons of duplicated and redundant type information.
1327 *
1328 * Additional complication we need to keep in mind comes from the fact that
1329 * types, in general, can form graphs containing cycles, not just DAGs.
1330 *
1331 * While algorithm does deduplication, it also merges and resolves type
1332 * information (unless disabled throught `struct btf_opts`), whenever possible.
1333 * E.g., in the example above with two compilation units having partial type
1334 * information for structs `A` and `B`, the output of algorithm will emit
1335 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
1336 * (as well as type information for `int` and pointers), as if they were defined
1337 * in a single compilation unit as:
1338 *
1339 * struct A {
1340 * int a;
1341 * struct A* self;
1342 * struct S* parent;
1343 * };
1344 * struct B {
1345 * int b;
1346 * struct B* self;
1347 * struct S* parent;
1348 * };
1349 * struct S {
1350 * struct A* a_ptr;
1351 * struct B* b_ptr;
1352 * };
1353 *
1354 * Algorithm summary
1355 * =================
1356 *
1357 * Algorithm completes its work in 6 separate passes:
1358 *
1359 * 1. Strings deduplication.
1360 * 2. Primitive types deduplication (int, enum, fwd).
1361 * 3. Struct/union types deduplication.
1362 * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
1363 * protos, and const/volatile/restrict modifiers).
1364 * 5. Types compaction.
1365 * 6. Types remapping.
1366 *
1367 * Algorithm determines canonical type descriptor, which is a single
1368 * representative type for each truly unique type. This canonical type is the
1369 * one that will go into final deduplicated BTF type information. For
1370 * struct/unions, it is also the type that algorithm will merge additional type
1371 * information into (while resolving FWDs), as it discovers it from data in
1372 * other CUs. Each input BTF type eventually gets either mapped to itself, if
1373 * that type is canonical, or to some other type, if that type is equivalent
1374 * and was chosen as canonical representative. This mapping is stored in
1375 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
1376 * FWD type got resolved to.
1377 *
1378 * To facilitate fast discovery of canonical types, we also maintain canonical
1379 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
1380 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
1381 * that match that signature. With sufficiently good choice of type signature
1382 * hashing function, we can limit number of canonical types for each unique type
1383 * signature to a very small number, allowing to find canonical type for any
1384 * duplicated type very quickly.
1385 *
1386 * Struct/union deduplication is the most critical part and algorithm for
1387 * deduplicating structs/unions is described in greater details in comments for
1388 * `btf_dedup_is_equiv` function.
1389 */
1390int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
1391 const struct btf_dedup_opts *opts)
1392{
1393 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
1394 int err;
1395
1396 if (IS_ERR(d)) {
1397 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
1398 return -EINVAL;
1399 }
1400
1401 err = btf_dedup_strings(d);
1402 if (err < 0) {
1403 pr_debug("btf_dedup_strings failed:%d\n", err);
1404 goto done;
1405 }
1406 err = btf_dedup_prim_types(d);
1407 if (err < 0) {
1408 pr_debug("btf_dedup_prim_types failed:%d\n", err);
1409 goto done;
1410 }
1411 err = btf_dedup_struct_types(d);
1412 if (err < 0) {
1413 pr_debug("btf_dedup_struct_types failed:%d\n", err);
1414 goto done;
1415 }
1416 err = btf_dedup_ref_types(d);
1417 if (err < 0) {
1418 pr_debug("btf_dedup_ref_types failed:%d\n", err);
1419 goto done;
1420 }
1421 err = btf_dedup_compact_types(d);
1422 if (err < 0) {
1423 pr_debug("btf_dedup_compact_types failed:%d\n", err);
1424 goto done;
1425 }
1426 err = btf_dedup_remap_types(d);
1427 if (err < 0) {
1428 pr_debug("btf_dedup_remap_types failed:%d\n", err);
1429 goto done;
1430 }
1431
1432done:
1433 btf_dedup_free(d);
1434 return err;
1435}
1436
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001437#define BTF_UNPROCESSED_ID ((__u32)-1)
1438#define BTF_IN_PROGRESS_ID ((__u32)-2)
1439
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001440struct btf_dedup {
1441 /* .BTF section to be deduped in-place */
1442 struct btf *btf;
1443 /*
1444 * Optional .BTF.ext section. When provided, any strings referenced
1445 * from it will be taken into account when deduping strings
1446 */
1447 struct btf_ext *btf_ext;
1448 /*
1449 * This is a map from any type's signature hash to a list of possible
1450 * canonical representative type candidates. Hash collisions are
1451 * ignored, so even types of various kinds can share same list of
1452 * candidates, which is fine because we rely on subsequent
1453 * btf_xxx_equal() checks to authoritatively verify type equality.
1454 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001455 struct hashmap *dedup_table;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001456 /* Canonical types map */
1457 __u32 *map;
1458 /* Hypothetical mapping, used during type graph equivalence checks */
1459 __u32 *hypot_map;
1460 __u32 *hypot_list;
1461 size_t hypot_cnt;
1462 size_t hypot_cap;
1463 /* Various option modifying behavior of algorithm */
1464 struct btf_dedup_opts opts;
1465};
1466
1467struct btf_str_ptr {
1468 const char *str;
1469 __u32 new_off;
1470 bool used;
1471};
1472
1473struct btf_str_ptrs {
1474 struct btf_str_ptr *ptrs;
1475 const char *data;
1476 __u32 cnt;
1477 __u32 cap;
1478};
1479
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001480static long hash_combine(long h, long value)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001481{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001482 return h * 31 + value;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001483}
1484
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001485#define for_each_dedup_cand(d, node, hash) \
1486 hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001487
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001488static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001489{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001490 return hashmap__append(d->dedup_table,
1491 (void *)hash, (void *)(long)type_id);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001492}
1493
1494static int btf_dedup_hypot_map_add(struct btf_dedup *d,
1495 __u32 from_id, __u32 to_id)
1496{
1497 if (d->hypot_cnt == d->hypot_cap) {
1498 __u32 *new_list;
1499
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08001500 d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001501 new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap);
1502 if (!new_list)
1503 return -ENOMEM;
1504 d->hypot_list = new_list;
1505 }
1506 d->hypot_list[d->hypot_cnt++] = from_id;
1507 d->hypot_map[from_id] = to_id;
1508 return 0;
1509}
1510
1511static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
1512{
1513 int i;
1514
1515 for (i = 0; i < d->hypot_cnt; i++)
1516 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
1517 d->hypot_cnt = 0;
1518}
1519
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001520static void btf_dedup_free(struct btf_dedup *d)
1521{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001522 hashmap__free(d->dedup_table);
1523 d->dedup_table = NULL;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001524
1525 free(d->map);
1526 d->map = NULL;
1527
1528 free(d->hypot_map);
1529 d->hypot_map = NULL;
1530
1531 free(d->hypot_list);
1532 d->hypot_list = NULL;
1533
1534 free(d);
1535}
1536
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001537static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
Andrii Nakryiko51edf5f2019-02-28 15:31:23 -08001538{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001539 return (size_t)key;
Andrii Nakryiko51edf5f2019-02-28 15:31:23 -08001540}
1541
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001542static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
1543{
1544 return 0;
1545}
1546
1547static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
1548{
1549 return k1 == k2;
1550}
Andrii Nakryiko51edf5f2019-02-28 15:31:23 -08001551
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001552static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1553 const struct btf_dedup_opts *opts)
1554{
1555 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001556 hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001557 int i, err = 0;
1558
1559 if (!d)
1560 return ERR_PTR(-ENOMEM);
1561
Andrii Nakryiko51edf5f2019-02-28 15:31:23 -08001562 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001563 /* dedup_table_size is now used only to force collisions in tests */
1564 if (opts && opts->dedup_table_size == 1)
1565 hash_fn = btf_dedup_collision_hash_fn;
Andrii Nakryiko51edf5f2019-02-28 15:31:23 -08001566
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001567 d->btf = btf;
1568 d->btf_ext = btf_ext;
1569
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001570 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
1571 if (IS_ERR(d->dedup_table)) {
1572 err = PTR_ERR(d->dedup_table);
1573 d->dedup_table = NULL;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001574 goto done;
1575 }
1576
1577 d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1578 if (!d->map) {
1579 err = -ENOMEM;
1580 goto done;
1581 }
1582 /* special BTF "void" type is made canonical immediately */
1583 d->map[0] = 0;
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07001584 for (i = 1; i <= btf->nr_types; i++) {
1585 struct btf_type *t = d->btf->types[i];
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07001586
1587 /* VAR and DATASEC are never deduped and are self-canonical */
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001588 if (btf_is_var(t) || btf_is_datasec(t))
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07001589 d->map[i] = i;
1590 else
1591 d->map[i] = BTF_UNPROCESSED_ID;
1592 }
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001593
1594 d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1595 if (!d->hypot_map) {
1596 err = -ENOMEM;
1597 goto done;
1598 }
1599 for (i = 0; i <= btf->nr_types; i++)
1600 d->hypot_map[i] = BTF_UNPROCESSED_ID;
1601
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001602done:
1603 if (err) {
1604 btf_dedup_free(d);
1605 return ERR_PTR(err);
1606 }
1607
1608 return d;
1609}
1610
1611typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
1612
1613/*
1614 * Iterate over all possible places in .BTF and .BTF.ext that can reference
1615 * string and pass pointer to it to a provided callback `fn`.
1616 */
1617static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
1618{
1619 void *line_data_cur, *line_data_end;
1620 int i, j, r, rec_size;
1621 struct btf_type *t;
1622
1623 for (i = 1; i <= d->btf->nr_types; i++) {
1624 t = d->btf->types[i];
1625 r = fn(&t->name_off, ctx);
1626 if (r)
1627 return r;
1628
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001629 switch (btf_kind(t)) {
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001630 case BTF_KIND_STRUCT:
1631 case BTF_KIND_UNION: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001632 struct btf_member *m = btf_members(t);
1633 __u16 vlen = btf_vlen(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001634
1635 for (j = 0; j < vlen; j++) {
1636 r = fn(&m->name_off, ctx);
1637 if (r)
1638 return r;
1639 m++;
1640 }
1641 break;
1642 }
1643 case BTF_KIND_ENUM: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001644 struct btf_enum *m = btf_enum(t);
1645 __u16 vlen = btf_vlen(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001646
1647 for (j = 0; j < vlen; j++) {
1648 r = fn(&m->name_off, ctx);
1649 if (r)
1650 return r;
1651 m++;
1652 }
1653 break;
1654 }
1655 case BTF_KIND_FUNC_PROTO: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001656 struct btf_param *m = btf_params(t);
1657 __u16 vlen = btf_vlen(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001658
1659 for (j = 0; j < vlen; j++) {
1660 r = fn(&m->name_off, ctx);
1661 if (r)
1662 return r;
1663 m++;
1664 }
1665 break;
1666 }
1667 default:
1668 break;
1669 }
1670 }
1671
1672 if (!d->btf_ext)
1673 return 0;
1674
1675 line_data_cur = d->btf_ext->line_info.info;
1676 line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
1677 rec_size = d->btf_ext->line_info.rec_size;
1678
1679 while (line_data_cur < line_data_end) {
1680 struct btf_ext_info_sec *sec = line_data_cur;
1681 struct bpf_line_info_min *line_info;
1682 __u32 num_info = sec->num_info;
1683
1684 r = fn(&sec->sec_name_off, ctx);
1685 if (r)
1686 return r;
1687
1688 line_data_cur += sizeof(struct btf_ext_info_sec);
1689 for (i = 0; i < num_info; i++) {
1690 line_info = line_data_cur;
1691 r = fn(&line_info->file_name_off, ctx);
1692 if (r)
1693 return r;
1694 r = fn(&line_info->line_off, ctx);
1695 if (r)
1696 return r;
1697 line_data_cur += rec_size;
1698 }
1699 }
1700
1701 return 0;
1702}
1703
1704static int str_sort_by_content(const void *a1, const void *a2)
1705{
1706 const struct btf_str_ptr *p1 = a1;
1707 const struct btf_str_ptr *p2 = a2;
1708
1709 return strcmp(p1->str, p2->str);
1710}
1711
1712static int str_sort_by_offset(const void *a1, const void *a2)
1713{
1714 const struct btf_str_ptr *p1 = a1;
1715 const struct btf_str_ptr *p2 = a2;
1716
1717 if (p1->str != p2->str)
1718 return p1->str < p2->str ? -1 : 1;
1719 return 0;
1720}
1721
1722static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
1723{
1724 const struct btf_str_ptr *p = pelem;
1725
1726 if (str_ptr != p->str)
1727 return (const char *)str_ptr < p->str ? -1 : 1;
1728 return 0;
1729}
1730
1731static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
1732{
1733 struct btf_str_ptrs *strs;
1734 struct btf_str_ptr *s;
1735
1736 if (*str_off_ptr == 0)
1737 return 0;
1738
1739 strs = ctx;
1740 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1741 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1742 if (!s)
1743 return -EINVAL;
1744 s->used = true;
1745 return 0;
1746}
1747
1748static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
1749{
1750 struct btf_str_ptrs *strs;
1751 struct btf_str_ptr *s;
1752
1753 if (*str_off_ptr == 0)
1754 return 0;
1755
1756 strs = ctx;
1757 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1758 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1759 if (!s)
1760 return -EINVAL;
1761 *str_off_ptr = s->new_off;
1762 return 0;
1763}
1764
1765/*
1766 * Dedup string and filter out those that are not referenced from either .BTF
1767 * or .BTF.ext (if provided) sections.
1768 *
1769 * This is done by building index of all strings in BTF's string section,
1770 * then iterating over all entities that can reference strings (e.g., type
1771 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
1772 * strings as used. After that all used strings are deduped and compacted into
1773 * sequential blob of memory and new offsets are calculated. Then all the string
1774 * references are iterated again and rewritten using new offsets.
1775 */
1776static int btf_dedup_strings(struct btf_dedup *d)
1777{
1778 const struct btf_header *hdr = d->btf->hdr;
1779 char *start = (char *)d->btf->nohdr_data + hdr->str_off;
1780 char *end = start + d->btf->hdr->str_len;
1781 char *p = start, *tmp_strs = NULL;
1782 struct btf_str_ptrs strs = {
1783 .cnt = 0,
1784 .cap = 0,
1785 .ptrs = NULL,
1786 .data = start,
1787 };
1788 int i, j, err = 0, grp_idx;
1789 bool grp_used;
1790
1791 /* build index of all strings */
1792 while (p < end) {
1793 if (strs.cnt + 1 > strs.cap) {
1794 struct btf_str_ptr *new_ptrs;
1795
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08001796 strs.cap += max(strs.cnt / 2, 16U);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001797 new_ptrs = realloc(strs.ptrs,
1798 sizeof(strs.ptrs[0]) * strs.cap);
1799 if (!new_ptrs) {
1800 err = -ENOMEM;
1801 goto done;
1802 }
1803 strs.ptrs = new_ptrs;
1804 }
1805
1806 strs.ptrs[strs.cnt].str = p;
1807 strs.ptrs[strs.cnt].used = false;
1808
1809 p += strlen(p) + 1;
1810 strs.cnt++;
1811 }
1812
1813 /* temporary storage for deduplicated strings */
1814 tmp_strs = malloc(d->btf->hdr->str_len);
1815 if (!tmp_strs) {
1816 err = -ENOMEM;
1817 goto done;
1818 }
1819
1820 /* mark all used strings */
1821 strs.ptrs[0].used = true;
1822 err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
1823 if (err)
1824 goto done;
1825
1826 /* sort strings by context, so that we can identify duplicates */
1827 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
1828
1829 /*
1830 * iterate groups of equal strings and if any instance in a group was
1831 * referenced, emit single instance and remember new offset
1832 */
1833 p = tmp_strs;
1834 grp_idx = 0;
1835 grp_used = strs.ptrs[0].used;
1836 /* iterate past end to avoid code duplication after loop */
1837 for (i = 1; i <= strs.cnt; i++) {
1838 /*
1839 * when i == strs.cnt, we want to skip string comparison and go
1840 * straight to handling last group of strings (otherwise we'd
1841 * need to handle last group after the loop w/ duplicated code)
1842 */
1843 if (i < strs.cnt &&
1844 !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
1845 grp_used = grp_used || strs.ptrs[i].used;
1846 continue;
1847 }
1848
1849 /*
1850 * this check would have been required after the loop to handle
1851 * last group of strings, but due to <= condition in a loop
1852 * we avoid that duplication
1853 */
1854 if (grp_used) {
1855 int new_off = p - tmp_strs;
1856 __u32 len = strlen(strs.ptrs[grp_idx].str);
1857
1858 memmove(p, strs.ptrs[grp_idx].str, len + 1);
1859 for (j = grp_idx; j < i; j++)
1860 strs.ptrs[j].new_off = new_off;
1861 p += len + 1;
1862 }
1863
1864 if (i < strs.cnt) {
1865 grp_idx = i;
1866 grp_used = strs.ptrs[i].used;
1867 }
1868 }
1869
1870 /* replace original strings with deduped ones */
1871 d->btf->hdr->str_len = p - tmp_strs;
1872 memmove(start, tmp_strs, d->btf->hdr->str_len);
1873 end = start + d->btf->hdr->str_len;
1874
1875 /* restore original order for further binary search lookups */
1876 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
1877
1878 /* remap string offsets */
1879 err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
1880 if (err)
1881 goto done;
1882
1883 d->btf->hdr->str_len = end - start;
1884
1885done:
1886 free(tmp_strs);
1887 free(strs.ptrs);
1888 return err;
1889}
1890
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001891static long btf_hash_common(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001892{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001893 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001894
1895 h = hash_combine(0, t->name_off);
1896 h = hash_combine(h, t->info);
1897 h = hash_combine(h, t->size);
1898 return h;
1899}
1900
1901static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
1902{
1903 return t1->name_off == t2->name_off &&
1904 t1->info == t2->info &&
1905 t1->size == t2->size;
1906}
1907
1908/* Calculate type signature hash of INT. */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001909static long btf_hash_int(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001910{
1911 __u32 info = *(__u32 *)(t + 1);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001912 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001913
1914 h = btf_hash_common(t);
1915 h = hash_combine(h, info);
1916 return h;
1917}
1918
1919/* Check structural equality of two INTs. */
1920static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
1921{
1922 __u32 info1, info2;
1923
1924 if (!btf_equal_common(t1, t2))
1925 return false;
1926 info1 = *(__u32 *)(t1 + 1);
1927 info2 = *(__u32 *)(t2 + 1);
1928 return info1 == info2;
1929}
1930
1931/* Calculate type signature hash of ENUM. */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001932static long btf_hash_enum(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001933{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001934 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001935
Andrii Nakryiko97680952019-03-10 17:44:09 -07001936 /* don't hash vlen and enum members to support enum fwd resolving */
1937 h = hash_combine(0, t->name_off);
1938 h = hash_combine(h, t->info & ~0xffff);
1939 h = hash_combine(h, t->size);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001940 return h;
1941}
1942
1943/* Check structural equality of two ENUMs. */
1944static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
1945{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001946 const struct btf_enum *m1, *m2;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001947 __u16 vlen;
1948 int i;
1949
1950 if (!btf_equal_common(t1, t2))
1951 return false;
1952
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001953 vlen = btf_vlen(t1);
1954 m1 = btf_enum(t1);
1955 m2 = btf_enum(t2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001956 for (i = 0; i < vlen; i++) {
1957 if (m1->name_off != m2->name_off || m1->val != m2->val)
1958 return false;
1959 m1++;
1960 m2++;
1961 }
1962 return true;
1963}
1964
Andrii Nakryiko97680952019-03-10 17:44:09 -07001965static inline bool btf_is_enum_fwd(struct btf_type *t)
1966{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001967 return btf_is_enum(t) && btf_vlen(t) == 0;
Andrii Nakryiko97680952019-03-10 17:44:09 -07001968}
1969
1970static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
1971{
1972 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
1973 return btf_equal_enum(t1, t2);
1974 /* ignore vlen when comparing */
1975 return t1->name_off == t2->name_off &&
1976 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
1977 t1->size == t2->size;
1978}
1979
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001980/*
1981 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
1982 * as referenced type IDs equivalence is established separately during type
1983 * graph equivalence check algorithm.
1984 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001985static long btf_hash_struct(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001986{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001987 const struct btf_member *member = btf_members(t);
1988 __u32 vlen = btf_vlen(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07001989 long h = btf_hash_common(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08001990 int i;
1991
1992 for (i = 0; i < vlen; i++) {
1993 h = hash_combine(h, member->name_off);
1994 h = hash_combine(h, member->offset);
1995 /* no hashing of referenced type ID, it can be unresolved yet */
1996 member++;
1997 }
1998 return h;
1999}
2000
2001/*
2002 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
2003 * IDs. This check is performed during type graph equivalence check and
2004 * referenced types equivalence is checked separately.
2005 */
Andrii Nakryiko91097fb2019-02-28 15:31:24 -08002006static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002007{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002008 const struct btf_member *m1, *m2;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002009 __u16 vlen;
2010 int i;
2011
2012 if (!btf_equal_common(t1, t2))
2013 return false;
2014
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002015 vlen = btf_vlen(t1);
2016 m1 = btf_members(t1);
2017 m2 = btf_members(t2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002018 for (i = 0; i < vlen; i++) {
2019 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
2020 return false;
2021 m1++;
2022 m2++;
2023 }
2024 return true;
2025}
2026
2027/*
2028 * Calculate type signature hash of ARRAY, including referenced type IDs,
2029 * under assumption that they were already resolved to canonical type IDs and
2030 * are not going to change.
2031 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002032static long btf_hash_array(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002033{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002034 const struct btf_array *info = btf_array(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002035 long h = btf_hash_common(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002036
2037 h = hash_combine(h, info->type);
2038 h = hash_combine(h, info->index_type);
2039 h = hash_combine(h, info->nelems);
2040 return h;
2041}
2042
2043/*
2044 * Check exact equality of two ARRAYs, taking into account referenced
2045 * type IDs, under assumption that they were already resolved to canonical
2046 * type IDs and are not going to change.
2047 * This function is called during reference types deduplication to compare
2048 * ARRAY to potential canonical representative.
2049 */
2050static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
2051{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002052 const struct btf_array *info1, *info2;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002053
2054 if (!btf_equal_common(t1, t2))
2055 return false;
2056
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002057 info1 = btf_array(t1);
2058 info2 = btf_array(t2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002059 return info1->type == info2->type &&
2060 info1->index_type == info2->index_type &&
2061 info1->nelems == info2->nelems;
2062}
2063
2064/*
2065 * Check structural compatibility of two ARRAYs, ignoring referenced type
2066 * IDs. This check is performed during type graph equivalence check and
2067 * referenced types equivalence is checked separately.
2068 */
2069static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
2070{
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002071 if (!btf_equal_common(t1, t2))
2072 return false;
2073
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002074 return btf_array(t1)->nelems == btf_array(t2)->nelems;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002075}
2076
2077/*
2078 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
2079 * under assumption that they were already resolved to canonical type IDs and
2080 * are not going to change.
2081 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002082static long btf_hash_fnproto(struct btf_type *t)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002083{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002084 const struct btf_param *member = btf_params(t);
2085 __u16 vlen = btf_vlen(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002086 long h = btf_hash_common(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002087 int i;
2088
2089 for (i = 0; i < vlen; i++) {
2090 h = hash_combine(h, member->name_off);
2091 h = hash_combine(h, member->type);
2092 member++;
2093 }
2094 return h;
2095}
2096
2097/*
2098 * Check exact equality of two FUNC_PROTOs, taking into account referenced
2099 * type IDs, under assumption that they were already resolved to canonical
2100 * type IDs and are not going to change.
2101 * This function is called during reference types deduplication to compare
2102 * FUNC_PROTO to potential canonical representative.
2103 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002104static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002105{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002106 const struct btf_param *m1, *m2;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002107 __u16 vlen;
2108 int i;
2109
2110 if (!btf_equal_common(t1, t2))
2111 return false;
2112
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002113 vlen = btf_vlen(t1);
2114 m1 = btf_params(t1);
2115 m2 = btf_params(t2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002116 for (i = 0; i < vlen; i++) {
2117 if (m1->name_off != m2->name_off || m1->type != m2->type)
2118 return false;
2119 m1++;
2120 m2++;
2121 }
2122 return true;
2123}
2124
2125/*
2126 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
2127 * IDs. This check is performed during type graph equivalence check and
2128 * referenced types equivalence is checked separately.
2129 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002130static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002131{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002132 const struct btf_param *m1, *m2;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002133 __u16 vlen;
2134 int i;
2135
2136 /* skip return type ID */
2137 if (t1->name_off != t2->name_off || t1->info != t2->info)
2138 return false;
2139
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002140 vlen = btf_vlen(t1);
2141 m1 = btf_params(t1);
2142 m2 = btf_params(t2);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002143 for (i = 0; i < vlen; i++) {
2144 if (m1->name_off != m2->name_off)
2145 return false;
2146 m1++;
2147 m2++;
2148 }
2149 return true;
2150}
2151
2152/*
2153 * Deduplicate primitive types, that can't reference other types, by calculating
2154 * their type signature hash and comparing them with any possible canonical
2155 * candidate. If no canonical candidate matches, type itself is marked as
2156 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
2157 */
2158static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
2159{
2160 struct btf_type *t = d->btf->types[type_id];
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002161 struct hashmap_entry *hash_entry;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002162 struct btf_type *cand;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002163 /* if we don't find equivalent type, then we are canonical */
2164 __u32 new_id = type_id;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002165 __u32 cand_id;
2166 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002167
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002168 switch (btf_kind(t)) {
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002169 case BTF_KIND_CONST:
2170 case BTF_KIND_VOLATILE:
2171 case BTF_KIND_RESTRICT:
2172 case BTF_KIND_PTR:
2173 case BTF_KIND_TYPEDEF:
2174 case BTF_KIND_ARRAY:
2175 case BTF_KIND_STRUCT:
2176 case BTF_KIND_UNION:
2177 case BTF_KIND_FUNC:
2178 case BTF_KIND_FUNC_PROTO:
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07002179 case BTF_KIND_VAR:
2180 case BTF_KIND_DATASEC:
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002181 return 0;
2182
2183 case BTF_KIND_INT:
2184 h = btf_hash_int(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002185 for_each_dedup_cand(d, hash_entry, h) {
2186 cand_id = (__u32)(long)hash_entry->value;
2187 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002188 if (btf_equal_int(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002189 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002190 break;
2191 }
2192 }
2193 break;
2194
2195 case BTF_KIND_ENUM:
2196 h = btf_hash_enum(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002197 for_each_dedup_cand(d, hash_entry, h) {
2198 cand_id = (__u32)(long)hash_entry->value;
2199 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002200 if (btf_equal_enum(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002201 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002202 break;
2203 }
Andrii Nakryiko97680952019-03-10 17:44:09 -07002204 if (d->opts.dont_resolve_fwds)
2205 continue;
2206 if (btf_compat_enum(t, cand)) {
2207 if (btf_is_enum_fwd(t)) {
2208 /* resolve fwd to full enum */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002209 new_id = cand_id;
Andrii Nakryiko97680952019-03-10 17:44:09 -07002210 break;
2211 }
2212 /* resolve canonical enum fwd to full enum */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002213 d->map[cand_id] = type_id;
Andrii Nakryiko97680952019-03-10 17:44:09 -07002214 }
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002215 }
2216 break;
2217
2218 case BTF_KIND_FWD:
2219 h = btf_hash_common(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002220 for_each_dedup_cand(d, hash_entry, h) {
2221 cand_id = (__u32)(long)hash_entry->value;
2222 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002223 if (btf_equal_common(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002224 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002225 break;
2226 }
2227 }
2228 break;
2229
2230 default:
2231 return -EINVAL;
2232 }
2233
2234 d->map[type_id] = new_id;
2235 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2236 return -ENOMEM;
2237
2238 return 0;
2239}
2240
2241static int btf_dedup_prim_types(struct btf_dedup *d)
2242{
2243 int i, err;
2244
2245 for (i = 1; i <= d->btf->nr_types; i++) {
2246 err = btf_dedup_prim_type(d, i);
2247 if (err)
2248 return err;
2249 }
2250 return 0;
2251}
2252
2253/*
2254 * Check whether type is already mapped into canonical one (could be to itself).
2255 */
2256static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
2257{
Andrii Nakryiko5aab3922019-02-15 19:52:18 -08002258 return d->map[type_id] <= BTF_MAX_NR_TYPES;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002259}
2260
2261/*
2262 * Resolve type ID into its canonical type ID, if any; otherwise return original
2263 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
2264 * STRUCT/UNION link and resolve it into canonical type ID as well.
2265 */
2266static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
2267{
2268 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2269 type_id = d->map[type_id];
2270 return type_id;
2271}
2272
2273/*
2274 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
2275 * type ID.
2276 */
2277static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
2278{
2279 __u32 orig_type_id = type_id;
2280
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002281 if (!btf_is_fwd(d->btf->types[type_id]))
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002282 return type_id;
2283
2284 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2285 type_id = d->map[type_id];
2286
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002287 if (!btf_is_fwd(d->btf->types[type_id]))
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002288 return type_id;
2289
2290 return orig_type_id;
2291}
2292
2293
2294static inline __u16 btf_fwd_kind(struct btf_type *t)
2295{
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002296 return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002297}
2298
2299/*
2300 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
2301 * call it "candidate graph" in this description for brevity) to a type graph
2302 * formed by (potential) canonical struct/union ("canonical graph" for brevity
2303 * here, though keep in mind that not all types in canonical graph are
2304 * necessarily canonical representatives themselves, some of them might be
2305 * duplicates or its uniqueness might not have been established yet).
2306 * Returns:
2307 * - >0, if type graphs are equivalent;
2308 * - 0, if not equivalent;
2309 * - <0, on error.
2310 *
2311 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
2312 * equivalence of BTF types at each step. If at any point BTF types in candidate
2313 * and canonical graphs are not compatible structurally, whole graphs are
2314 * incompatible. If types are structurally equivalent (i.e., all information
2315 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
2316 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
2317 * If a type references other types, then those referenced types are checked
2318 * for equivalence recursively.
2319 *
2320 * During DFS traversal, if we find that for current `canon_id` type we
2321 * already have some mapping in hypothetical map, we check for two possible
2322 * situations:
2323 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
2324 * happen when type graphs have cycles. In this case we assume those two
2325 * types are equivalent.
2326 * - `canon_id` is mapped to different type. This is contradiction in our
2327 * hypothetical mapping, because same graph in canonical graph corresponds
2328 * to two different types in candidate graph, which for equivalent type
2329 * graphs shouldn't happen. This condition terminates equivalence check
2330 * with negative result.
2331 *
2332 * If type graphs traversal exhausts types to check and find no contradiction,
2333 * then type graphs are equivalent.
2334 *
2335 * When checking types for equivalence, there is one special case: FWD types.
2336 * If FWD type resolution is allowed and one of the types (either from canonical
2337 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
2338 * flag) and their names match, hypothetical mapping is updated to point from
2339 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
2340 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
2341 *
2342 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
2343 * if there are two exactly named (or anonymous) structs/unions that are
2344 * compatible structurally, one of which has FWD field, while other is concrete
2345 * STRUCT/UNION, but according to C sources they are different structs/unions
2346 * that are referencing different types with the same name. This is extremely
2347 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
2348 * this logic is causing problems.
2349 *
2350 * Doing FWD resolution means that both candidate and/or canonical graphs can
2351 * consists of portions of the graph that come from multiple compilation units.
2352 * This is due to the fact that types within single compilation unit are always
2353 * deduplicated and FWDs are already resolved, if referenced struct/union
2354 * definiton is available. So, if we had unresolved FWD and found corresponding
2355 * STRUCT/UNION, they will be from different compilation units. This
2356 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
2357 * type graph will likely have at least two different BTF types that describe
2358 * same type (e.g., most probably there will be two different BTF types for the
2359 * same 'int' primitive type) and could even have "overlapping" parts of type
2360 * graph that describe same subset of types.
2361 *
2362 * This in turn means that our assumption that each type in canonical graph
2363 * must correspond to exactly one type in candidate graph might not hold
2364 * anymore and will make it harder to detect contradictions using hypothetical
2365 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
2366 * resolution only in canonical graph. FWDs in candidate graphs are never
2367 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
2368 * that can occur:
2369 * - Both types in canonical and candidate graphs are FWDs. If they are
2370 * structurally equivalent, then they can either be both resolved to the
2371 * same STRUCT/UNION or not resolved at all. In both cases they are
2372 * equivalent and there is no need to resolve FWD on candidate side.
2373 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
2374 * so nothing to resolve as well, algorithm will check equivalence anyway.
2375 * - Type in canonical graph is FWD, while type in candidate is concrete
2376 * STRUCT/UNION. In this case candidate graph comes from single compilation
2377 * unit, so there is exactly one BTF type for each unique C type. After
2378 * resolving FWD into STRUCT/UNION, there might be more than one BTF type
2379 * in canonical graph mapping to single BTF type in candidate graph, but
2380 * because hypothetical mapping maps from canonical to candidate types, it's
2381 * alright, and we still maintain the property of having single `canon_id`
2382 * mapping to single `cand_id` (there could be two different `canon_id`
2383 * mapped to the same `cand_id`, but it's not contradictory).
2384 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
2385 * graph is FWD. In this case we are just going to check compatibility of
2386 * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
2387 * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
2388 * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
2389 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
2390 * canonical graph.
2391 */
2392static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
2393 __u32 canon_id)
2394{
2395 struct btf_type *cand_type;
2396 struct btf_type *canon_type;
2397 __u32 hypot_type_id;
2398 __u16 cand_kind;
2399 __u16 canon_kind;
2400 int i, eq;
2401
2402 /* if both resolve to the same canonical, they must be equivalent */
2403 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
2404 return 1;
2405
2406 canon_id = resolve_fwd_id(d, canon_id);
2407
2408 hypot_type_id = d->hypot_map[canon_id];
Andrii Nakryiko5aab3922019-02-15 19:52:18 -08002409 if (hypot_type_id <= BTF_MAX_NR_TYPES)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002410 return hypot_type_id == cand_id;
2411
2412 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
2413 return -ENOMEM;
2414
2415 cand_type = d->btf->types[cand_id];
2416 canon_type = d->btf->types[canon_id];
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002417 cand_kind = btf_kind(cand_type);
2418 canon_kind = btf_kind(canon_type);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002419
2420 if (cand_type->name_off != canon_type->name_off)
2421 return 0;
2422
2423 /* FWD <--> STRUCT/UNION equivalence check, if enabled */
2424 if (!d->opts.dont_resolve_fwds
2425 && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
2426 && cand_kind != canon_kind) {
2427 __u16 real_kind;
2428 __u16 fwd_kind;
2429
2430 if (cand_kind == BTF_KIND_FWD) {
2431 real_kind = canon_kind;
2432 fwd_kind = btf_fwd_kind(cand_type);
2433 } else {
2434 real_kind = cand_kind;
2435 fwd_kind = btf_fwd_kind(canon_type);
2436 }
2437 return fwd_kind == real_kind;
2438 }
2439
Andrii Nakryiko9ec71c12019-03-26 22:00:06 -07002440 if (cand_kind != canon_kind)
2441 return 0;
2442
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002443 switch (cand_kind) {
2444 case BTF_KIND_INT:
2445 return btf_equal_int(cand_type, canon_type);
2446
2447 case BTF_KIND_ENUM:
Andrii Nakryiko97680952019-03-10 17:44:09 -07002448 if (d->opts.dont_resolve_fwds)
2449 return btf_equal_enum(cand_type, canon_type);
2450 else
2451 return btf_compat_enum(cand_type, canon_type);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002452
2453 case BTF_KIND_FWD:
2454 return btf_equal_common(cand_type, canon_type);
2455
2456 case BTF_KIND_CONST:
2457 case BTF_KIND_VOLATILE:
2458 case BTF_KIND_RESTRICT:
2459 case BTF_KIND_PTR:
2460 case BTF_KIND_TYPEDEF:
2461 case BTF_KIND_FUNC:
Andrii Nakryiko97680952019-03-10 17:44:09 -07002462 if (cand_type->info != canon_type->info)
2463 return 0;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002464 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2465
2466 case BTF_KIND_ARRAY: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002467 const struct btf_array *cand_arr, *canon_arr;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002468
2469 if (!btf_compat_array(cand_type, canon_type))
2470 return 0;
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002471 cand_arr = btf_array(cand_type);
2472 canon_arr = btf_array(canon_type);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002473 eq = btf_dedup_is_equiv(d,
2474 cand_arr->index_type, canon_arr->index_type);
2475 if (eq <= 0)
2476 return eq;
2477 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
2478 }
2479
2480 case BTF_KIND_STRUCT:
2481 case BTF_KIND_UNION: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002482 const struct btf_member *cand_m, *canon_m;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002483 __u16 vlen;
2484
Andrii Nakryiko91097fb2019-02-28 15:31:24 -08002485 if (!btf_shallow_equal_struct(cand_type, canon_type))
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002486 return 0;
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002487 vlen = btf_vlen(cand_type);
2488 cand_m = btf_members(cand_type);
2489 canon_m = btf_members(canon_type);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002490 for (i = 0; i < vlen; i++) {
2491 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
2492 if (eq <= 0)
2493 return eq;
2494 cand_m++;
2495 canon_m++;
2496 }
2497
2498 return 1;
2499 }
2500
2501 case BTF_KIND_FUNC_PROTO: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002502 const struct btf_param *cand_p, *canon_p;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002503 __u16 vlen;
2504
2505 if (!btf_compat_fnproto(cand_type, canon_type))
2506 return 0;
2507 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2508 if (eq <= 0)
2509 return eq;
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002510 vlen = btf_vlen(cand_type);
2511 cand_p = btf_params(cand_type);
2512 canon_p = btf_params(canon_type);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002513 for (i = 0; i < vlen; i++) {
2514 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
2515 if (eq <= 0)
2516 return eq;
2517 cand_p++;
2518 canon_p++;
2519 }
2520 return 1;
2521 }
2522
2523 default:
2524 return -EINVAL;
2525 }
2526 return 0;
2527}
2528
2529/*
2530 * Use hypothetical mapping, produced by successful type graph equivalence
2531 * check, to augment existing struct/union canonical mapping, where possible.
2532 *
2533 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
2534 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
2535 * it doesn't matter if FWD type was part of canonical graph or candidate one,
2536 * we are recording the mapping anyway. As opposed to carefulness required
2537 * for struct/union correspondence mapping (described below), for FWD resolution
2538 * it's not important, as by the time that FWD type (reference type) will be
2539 * deduplicated all structs/unions will be deduped already anyway.
2540 *
2541 * Recording STRUCT/UNION mapping is purely a performance optimization and is
2542 * not required for correctness. It needs to be done carefully to ensure that
2543 * struct/union from candidate's type graph is not mapped into corresponding
2544 * struct/union from canonical type graph that itself hasn't been resolved into
2545 * canonical representative. The only guarantee we have is that canonical
2546 * struct/union was determined as canonical and that won't change. But any
2547 * types referenced through that struct/union fields could have been not yet
2548 * resolved, so in case like that it's too early to establish any kind of
2549 * correspondence between structs/unions.
2550 *
2551 * No canonical correspondence is derived for primitive types (they are already
2552 * deduplicated completely already anyway) or reference types (they rely on
2553 * stability of struct/union canonical relationship for equivalence checks).
2554 */
2555static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
2556{
2557 __u32 cand_type_id, targ_type_id;
2558 __u16 t_kind, c_kind;
2559 __u32 t_id, c_id;
2560 int i;
2561
2562 for (i = 0; i < d->hypot_cnt; i++) {
2563 cand_type_id = d->hypot_list[i];
2564 targ_type_id = d->hypot_map[cand_type_id];
2565 t_id = resolve_type_id(d, targ_type_id);
2566 c_id = resolve_type_id(d, cand_type_id);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002567 t_kind = btf_kind(d->btf->types[t_id]);
2568 c_kind = btf_kind(d->btf->types[c_id]);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002569 /*
2570 * Resolve FWD into STRUCT/UNION.
2571 * It's ok to resolve FWD into STRUCT/UNION that's not yet
2572 * mapped to canonical representative (as opposed to
2573 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
2574 * eventually that struct is going to be mapped and all resolved
2575 * FWDs will automatically resolve to correct canonical
2576 * representative. This will happen before ref type deduping,
2577 * which critically depends on stability of these mapping. This
2578 * stability is not a requirement for STRUCT/UNION equivalence
2579 * checks, though.
2580 */
2581 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
2582 d->map[c_id] = t_id;
2583 else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
2584 d->map[t_id] = c_id;
2585
2586 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
2587 c_kind != BTF_KIND_FWD &&
2588 is_type_mapped(d, c_id) &&
2589 !is_type_mapped(d, t_id)) {
2590 /*
2591 * as a perf optimization, we can map struct/union
2592 * that's part of type graph we just verified for
2593 * equivalence. We can do that for struct/union that has
2594 * canonical representative only, though.
2595 */
2596 d->map[t_id] = c_id;
2597 }
2598 }
2599}
2600
2601/*
2602 * Deduplicate struct/union types.
2603 *
2604 * For each struct/union type its type signature hash is calculated, taking
2605 * into account type's name, size, number, order and names of fields, but
2606 * ignoring type ID's referenced from fields, because they might not be deduped
2607 * completely until after reference types deduplication phase. This type hash
2608 * is used to iterate over all potential canonical types, sharing same hash.
2609 * For each canonical candidate we check whether type graphs that they form
2610 * (through referenced types in fields and so on) are equivalent using algorithm
2611 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
2612 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
2613 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
2614 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
2615 * potentially map other structs/unions to their canonical representatives,
2616 * if such relationship hasn't yet been established. This speeds up algorithm
2617 * by eliminating some of the duplicate work.
2618 *
2619 * If no matching canonical representative was found, struct/union is marked
2620 * as canonical for itself and is added into btf_dedup->dedup_table hash map
2621 * for further look ups.
2622 */
2623static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
2624{
Andrii Nakryiko91097fb2019-02-28 15:31:24 -08002625 struct btf_type *cand_type, *t;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002626 struct hashmap_entry *hash_entry;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002627 /* if we don't find equivalent type, then we are canonical */
2628 __u32 new_id = type_id;
2629 __u16 kind;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002630 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002631
2632 /* already deduped or is in process of deduping (loop detected) */
Andrii Nakryiko5aab3922019-02-15 19:52:18 -08002633 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002634 return 0;
2635
2636 t = d->btf->types[type_id];
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002637 kind = btf_kind(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002638
2639 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
2640 return 0;
2641
2642 h = btf_hash_struct(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002643 for_each_dedup_cand(d, hash_entry, h) {
2644 __u32 cand_id = (__u32)(long)hash_entry->value;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002645 int eq;
2646
Andrii Nakryiko91097fb2019-02-28 15:31:24 -08002647 /*
2648 * Even though btf_dedup_is_equiv() checks for
2649 * btf_shallow_equal_struct() internally when checking two
2650 * structs (unions) for equivalence, we need to guard here
2651 * from picking matching FWD type as a dedup candidate.
2652 * This can happen due to hash collision. In such case just
2653 * relying on btf_dedup_is_equiv() would lead to potentially
2654 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
2655 * FWD and compatible STRUCT/UNION are considered equivalent.
2656 */
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002657 cand_type = d->btf->types[cand_id];
Andrii Nakryiko91097fb2019-02-28 15:31:24 -08002658 if (!btf_shallow_equal_struct(t, cand_type))
2659 continue;
2660
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002661 btf_dedup_clear_hypot_map(d);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002662 eq = btf_dedup_is_equiv(d, type_id, cand_id);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002663 if (eq < 0)
2664 return eq;
2665 if (!eq)
2666 continue;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002667 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002668 btf_dedup_merge_hypot_map(d);
2669 break;
2670 }
2671
2672 d->map[type_id] = new_id;
2673 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2674 return -ENOMEM;
2675
2676 return 0;
2677}
2678
2679static int btf_dedup_struct_types(struct btf_dedup *d)
2680{
2681 int i, err;
2682
2683 for (i = 1; i <= d->btf->nr_types; i++) {
2684 err = btf_dedup_struct_type(d, i);
2685 if (err)
2686 return err;
2687 }
2688 return 0;
2689}
2690
2691/*
2692 * Deduplicate reference type.
2693 *
2694 * Once all primitive and struct/union types got deduplicated, we can easily
2695 * deduplicate all other (reference) BTF types. This is done in two steps:
2696 *
2697 * 1. Resolve all referenced type IDs into their canonical type IDs. This
2698 * resolution can be done either immediately for primitive or struct/union types
2699 * (because they were deduped in previous two phases) or recursively for
2700 * reference types. Recursion will always terminate at either primitive or
2701 * struct/union type, at which point we can "unwind" chain of reference types
2702 * one by one. There is no danger of encountering cycles because in C type
2703 * system the only way to form type cycle is through struct/union, so any chain
2704 * of reference types, even those taking part in a type cycle, will inevitably
2705 * reach struct/union at some point.
2706 *
2707 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
2708 * becomes "stable", in the sense that no further deduplication will cause
2709 * any changes to it. With that, it's now possible to calculate type's signature
2710 * hash (this time taking into account referenced type IDs) and loop over all
2711 * potential canonical representatives. If no match was found, current type
2712 * will become canonical representative of itself and will be added into
2713 * btf_dedup->dedup_table as another possible canonical representative.
2714 */
2715static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
2716{
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002717 struct hashmap_entry *hash_entry;
2718 __u32 new_id = type_id, cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002719 struct btf_type *t, *cand;
2720 /* if we don't find equivalent type, then we are representative type */
Dan Carpenter3d8669e2019-02-28 21:06:47 +03002721 int ref_type_id;
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002722 long h;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002723
2724 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
2725 return -ELOOP;
Andrii Nakryiko5aab3922019-02-15 19:52:18 -08002726 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002727 return resolve_type_id(d, type_id);
2728
2729 t = d->btf->types[type_id];
2730 d->map[type_id] = BTF_IN_PROGRESS_ID;
2731
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002732 switch (btf_kind(t)) {
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002733 case BTF_KIND_CONST:
2734 case BTF_KIND_VOLATILE:
2735 case BTF_KIND_RESTRICT:
2736 case BTF_KIND_PTR:
2737 case BTF_KIND_TYPEDEF:
2738 case BTF_KIND_FUNC:
2739 ref_type_id = btf_dedup_ref_type(d, t->type);
2740 if (ref_type_id < 0)
2741 return ref_type_id;
2742 t->type = ref_type_id;
2743
2744 h = btf_hash_common(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002745 for_each_dedup_cand(d, hash_entry, h) {
2746 cand_id = (__u32)(long)hash_entry->value;
2747 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002748 if (btf_equal_common(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002749 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002750 break;
2751 }
2752 }
2753 break;
2754
2755 case BTF_KIND_ARRAY: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002756 struct btf_array *info = btf_array(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002757
2758 ref_type_id = btf_dedup_ref_type(d, info->type);
2759 if (ref_type_id < 0)
2760 return ref_type_id;
2761 info->type = ref_type_id;
2762
2763 ref_type_id = btf_dedup_ref_type(d, info->index_type);
2764 if (ref_type_id < 0)
2765 return ref_type_id;
2766 info->index_type = ref_type_id;
2767
2768 h = btf_hash_array(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002769 for_each_dedup_cand(d, hash_entry, h) {
2770 cand_id = (__u32)(long)hash_entry->value;
2771 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002772 if (btf_equal_array(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002773 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002774 break;
2775 }
2776 }
2777 break;
2778 }
2779
2780 case BTF_KIND_FUNC_PROTO: {
2781 struct btf_param *param;
2782 __u16 vlen;
2783 int i;
2784
2785 ref_type_id = btf_dedup_ref_type(d, t->type);
2786 if (ref_type_id < 0)
2787 return ref_type_id;
2788 t->type = ref_type_id;
2789
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002790 vlen = btf_vlen(t);
2791 param = btf_params(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002792 for (i = 0; i < vlen; i++) {
2793 ref_type_id = btf_dedup_ref_type(d, param->type);
2794 if (ref_type_id < 0)
2795 return ref_type_id;
2796 param->type = ref_type_id;
2797 param++;
2798 }
2799
2800 h = btf_hash_fnproto(t);
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002801 for_each_dedup_cand(d, hash_entry, h) {
2802 cand_id = (__u32)(long)hash_entry->value;
2803 cand = d->btf->types[cand_id];
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002804 if (btf_equal_fnproto(t, cand)) {
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002805 new_id = cand_id;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002806 break;
2807 }
2808 }
2809 break;
2810 }
2811
2812 default:
2813 return -EINVAL;
2814 }
2815
2816 d->map[type_id] = new_id;
2817 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2818 return -ENOMEM;
2819
2820 return new_id;
2821}
2822
2823static int btf_dedup_ref_types(struct btf_dedup *d)
2824{
2825 int i, err;
2826
2827 for (i = 1; i <= d->btf->nr_types; i++) {
2828 err = btf_dedup_ref_type(d, i);
2829 if (err < 0)
2830 return err;
2831 }
Andrii Nakryiko2fc3fc02019-05-24 11:59:02 -07002832 /* we won't need d->dedup_table anymore */
2833 hashmap__free(d->dedup_table);
2834 d->dedup_table = NULL;
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002835 return 0;
2836}
2837
2838/*
2839 * Compact types.
2840 *
2841 * After we established for each type its corresponding canonical representative
2842 * type, we now can eliminate types that are not canonical and leave only
2843 * canonical ones layed out sequentially in memory by copying them over
2844 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
2845 * a map from original type ID to a new compacted type ID, which will be used
2846 * during next phase to "fix up" type IDs, referenced from struct/union and
2847 * reference types.
2848 */
2849static int btf_dedup_compact_types(struct btf_dedup *d)
2850{
2851 struct btf_type **new_types;
2852 __u32 next_type_id = 1;
2853 char *types_start, *p;
2854 int i, len;
2855
2856 /* we are going to reuse hypot_map to store compaction remapping */
2857 d->hypot_map[0] = 0;
2858 for (i = 1; i <= d->btf->nr_types; i++)
2859 d->hypot_map[i] = BTF_UNPROCESSED_ID;
2860
2861 types_start = d->btf->nohdr_data + d->btf->hdr->type_off;
2862 p = types_start;
2863
2864 for (i = 1; i <= d->btf->nr_types; i++) {
2865 if (d->map[i] != i)
2866 continue;
2867
2868 len = btf_type_size(d->btf->types[i]);
2869 if (len < 0)
2870 return len;
2871
2872 memmove(p, d->btf->types[i], len);
2873 d->hypot_map[i] = next_type_id;
2874 d->btf->types[next_type_id] = (struct btf_type *)p;
2875 p += len;
2876 next_type_id++;
2877 }
2878
2879 /* shrink struct btf's internal types index and update btf_header */
2880 d->btf->nr_types = next_type_id - 1;
2881 d->btf->types_size = d->btf->nr_types;
2882 d->btf->hdr->type_len = p - types_start;
2883 new_types = realloc(d->btf->types,
2884 (1 + d->btf->nr_types) * sizeof(struct btf_type *));
2885 if (!new_types)
2886 return -ENOMEM;
2887 d->btf->types = new_types;
2888
2889 /* make sure string section follows type information without gaps */
2890 d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data;
2891 memmove(p, d->btf->strings, d->btf->hdr->str_len);
2892 d->btf->strings = p;
2893 p += d->btf->hdr->str_len;
2894
2895 d->btf->data_size = p - (char *)d->btf->data;
2896 return 0;
2897}
2898
2899/*
2900 * Figure out final (deduplicated and compacted) type ID for provided original
2901 * `type_id` by first resolving it into corresponding canonical type ID and
2902 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
2903 * which is populated during compaction phase.
2904 */
2905static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
2906{
2907 __u32 resolved_type_id, new_type_id;
2908
2909 resolved_type_id = resolve_type_id(d, type_id);
2910 new_type_id = d->hypot_map[resolved_type_id];
Andrii Nakryiko5aab3922019-02-15 19:52:18 -08002911 if (new_type_id > BTF_MAX_NR_TYPES)
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002912 return -EINVAL;
2913 return new_type_id;
2914}
2915
2916/*
2917 * Remap referenced type IDs into deduped type IDs.
2918 *
2919 * After BTF types are deduplicated and compacted, their final type IDs may
2920 * differ from original ones. The map from original to a corresponding
2921 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
2922 * compaction phase. During remapping phase we are rewriting all type IDs
2923 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
2924 * their final deduped type IDs.
2925 */
2926static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
2927{
2928 struct btf_type *t = d->btf->types[type_id];
2929 int i, r;
2930
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002931 switch (btf_kind(t)) {
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002932 case BTF_KIND_INT:
2933 case BTF_KIND_ENUM:
2934 break;
2935
2936 case BTF_KIND_FWD:
2937 case BTF_KIND_CONST:
2938 case BTF_KIND_VOLATILE:
2939 case BTF_KIND_RESTRICT:
2940 case BTF_KIND_PTR:
2941 case BTF_KIND_TYPEDEF:
2942 case BTF_KIND_FUNC:
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07002943 case BTF_KIND_VAR:
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002944 r = btf_dedup_remap_type_id(d, t->type);
2945 if (r < 0)
2946 return r;
2947 t->type = r;
2948 break;
2949
2950 case BTF_KIND_ARRAY: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002951 struct btf_array *arr_info = btf_array(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002952
2953 r = btf_dedup_remap_type_id(d, arr_info->type);
2954 if (r < 0)
2955 return r;
2956 arr_info->type = r;
2957 r = btf_dedup_remap_type_id(d, arr_info->index_type);
2958 if (r < 0)
2959 return r;
2960 arr_info->index_type = r;
2961 break;
2962 }
2963
2964 case BTF_KIND_STRUCT:
2965 case BTF_KIND_UNION: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002966 struct btf_member *member = btf_members(t);
2967 __u16 vlen = btf_vlen(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002968
2969 for (i = 0; i < vlen; i++) {
2970 r = btf_dedup_remap_type_id(d, member->type);
2971 if (r < 0)
2972 return r;
2973 member->type = r;
2974 member++;
2975 }
2976 break;
2977 }
2978
2979 case BTF_KIND_FUNC_PROTO: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002980 struct btf_param *param = btf_params(t);
2981 __u16 vlen = btf_vlen(t);
Andrii Nakryikod5caef52019-02-04 17:29:45 -08002982
2983 r = btf_dedup_remap_type_id(d, t->type);
2984 if (r < 0)
2985 return r;
2986 t->type = r;
2987
2988 for (i = 0; i < vlen; i++) {
2989 r = btf_dedup_remap_type_id(d, param->type);
2990 if (r < 0)
2991 return r;
2992 param->type = r;
2993 param++;
2994 }
2995 break;
2996 }
2997
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07002998 case BTF_KIND_DATASEC: {
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002999 struct btf_var_secinfo *var = btf_var_secinfos(t);
3000 __u16 vlen = btf_vlen(t);
Andrii Nakryiko189cf5a2019-04-15 16:48:07 -07003001
3002 for (i = 0; i < vlen; i++) {
3003 r = btf_dedup_remap_type_id(d, var->type);
3004 if (r < 0)
3005 return r;
3006 var->type = r;
3007 var++;
3008 }
3009 break;
3010 }
3011
Andrii Nakryikod5caef52019-02-04 17:29:45 -08003012 default:
3013 return -EINVAL;
3014 }
3015
3016 return 0;
3017}
3018
3019static int btf_dedup_remap_types(struct btf_dedup *d)
3020{
3021 int i, r;
3022
3023 for (i = 1; i <= d->btf->nr_types; i++) {
3024 r = btf_dedup_remap_type(d, i);
3025 if (r < 0)
3026 return r;
3027 }
3028 return 0;
3029}
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08003030
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08003031/*
3032 * Probe few well-known locations for vmlinux kernel image and try to load BTF
3033 * data out of it to use for target BTF.
3034 */
3035struct btf *libbpf_find_kernel_btf(void)
3036{
3037 struct {
3038 const char *path_fmt;
3039 bool raw_btf;
3040 } locations[] = {
3041 /* try canonical vmlinux BTF through sysfs first */
3042 { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
3043 /* fall back to trying to find vmlinux ELF on disk otherwise */
3044 { "/boot/vmlinux-%1$s" },
3045 { "/lib/modules/%1$s/vmlinux-%1$s" },
3046 { "/lib/modules/%1$s/build/vmlinux" },
3047 { "/usr/lib/modules/%1$s/kernel/vmlinux" },
3048 { "/usr/lib/debug/boot/vmlinux-%1$s" },
3049 { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
3050 { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
3051 };
3052 char path[PATH_MAX + 1];
3053 struct utsname buf;
3054 struct btf *btf;
3055 int i;
3056
3057 uname(&buf);
3058
3059 for (i = 0; i < ARRAY_SIZE(locations); i++) {
3060 snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
3061
3062 if (access(path, R_OK))
3063 continue;
3064
3065 if (locations[i].raw_btf)
Andrii Nakryiko94a1fedd62020-08-01 18:32:17 -07003066 btf = btf__parse_raw(path);
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08003067 else
3068 btf = btf__parse_elf(path, NULL);
3069
3070 pr_debug("loading kernel BTF '%s': %ld\n",
3071 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
3072 if (IS_ERR(btf))
3073 continue;
3074
3075 return btf;
3076 }
3077
3078 pr_warn("failed to find valid kernel BTF\n");
3079 return ERR_PTR(-ESRCH);
3080}