]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/lib/bpf/btf.c
bpf: btf: Clean up btf.h in uapi
[linux.git] / tools / lib / bpf / btf.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <linux/err.h>
10 #include <linux/btf.h>
11 #include "btf.h"
12 #include "bpf.h"
13
14 #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
15 #define max(a, b) ((a) > (b) ? (a) : (b))
16 #define min(a, b) ((a) < (b) ? (a) : (b))
17
18 #define BTF_MAX_NR_TYPES 65535
19
20 static struct btf_type btf_void;
21
22 struct btf {
23         union {
24                 struct btf_header *hdr;
25                 void *data;
26         };
27         struct btf_type **types;
28         const char *strings;
29         void *nohdr_data;
30         uint32_t nr_types;
31         uint32_t types_size;
32         uint32_t data_size;
33         int fd;
34 };
35
36 static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
37 {
38         if (!BTF_STR_TBL_ELF_ID(offset) &&
39             BTF_STR_OFFSET(offset) < btf->hdr->str_len)
40                 return &btf->strings[BTF_STR_OFFSET(offset)];
41         else
42                 return NULL;
43 }
44
45 static int btf_add_type(struct btf *btf, struct btf_type *t)
46 {
47         if (btf->types_size - btf->nr_types < 2) {
48                 struct btf_type **new_types;
49                 u32 expand_by, new_size;
50
51                 if (btf->types_size == BTF_MAX_NR_TYPES)
52                         return -E2BIG;
53
54                 expand_by = max(btf->types_size >> 2, 16);
55                 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
56
57                 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
58                 if (!new_types)
59                         return -ENOMEM;
60
61                 if (btf->nr_types == 0)
62                         new_types[0] = &btf_void;
63
64                 btf->types = new_types;
65                 btf->types_size = new_size;
66         }
67
68         btf->types[++(btf->nr_types)] = t;
69
70         return 0;
71 }
72
73 static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
74 {
75         const struct btf_header *hdr = btf->hdr;
76         u32 meta_left;
77
78         if (btf->data_size < sizeof(struct btf_header)) {
79                 elog("BTF header not found\n");
80                 return -EINVAL;
81         }
82
83         if (hdr->magic != BTF_MAGIC) {
84                 elog("Invalid BTF magic:%x\n", hdr->magic);
85                 return -EINVAL;
86         }
87
88         if (hdr->version != BTF_VERSION) {
89                 elog("Unsupported BTF version:%u\n", hdr->version);
90                 return -ENOTSUP;
91         }
92
93         if (hdr->flags) {
94                 elog("Unsupported BTF flags:%x\n", hdr->flags);
95                 return -ENOTSUP;
96         }
97
98         meta_left = btf->data_size - sizeof(*hdr);
99         if (!meta_left) {
100                 elog("BTF has no data\n");
101                 return -EINVAL;
102         }
103
104         if (meta_left < hdr->type_off) {
105                 elog("Invalid BTF type section offset:%u\n", hdr->type_off);
106                 return -EINVAL;
107         }
108
109         if (meta_left < hdr->str_off) {
110                 elog("Invalid BTF string section offset:%u\n", hdr->str_off);
111                 return -EINVAL;
112         }
113
114         if (hdr->type_off >= hdr->str_off) {
115                 elog("BTF type section offset >= string section offset. No type?\n");
116                 return -EINVAL;
117         }
118
119         if (hdr->type_off & 0x02) {
120                 elog("BTF type section is not aligned to 4 bytes\n");
121                 return -EINVAL;
122         }
123
124         btf->nohdr_data = btf->hdr + 1;
125
126         return 0;
127 }
128
129 static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
130 {
131         const struct btf_header *hdr = btf->hdr;
132         const char *start = btf->nohdr_data + hdr->str_off;
133         const char *end = start + btf->hdr->str_len;
134
135         if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
136             start[0] || end[-1]) {
137                 elog("Invalid BTF string section\n");
138                 return -EINVAL;
139         }
140
141         btf->strings = start;
142
143         return 0;
144 }
145
146 static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
147 {
148         struct btf_header *hdr = btf->hdr;
149         void *nohdr_data = btf->nohdr_data;
150         void *next_type = nohdr_data + hdr->type_off;
151         void *end_type = nohdr_data + hdr->str_off;
152
153         while (next_type < end_type) {
154                 struct btf_type *t = next_type;
155                 uint16_t vlen = BTF_INFO_VLEN(t->info);
156                 int err;
157
158                 next_type += sizeof(*t);
159                 switch (BTF_INFO_KIND(t->info)) {
160                 case BTF_KIND_INT:
161                         next_type += sizeof(int);
162                         break;
163                 case BTF_KIND_ARRAY:
164                         next_type += sizeof(struct btf_array);
165                         break;
166                 case BTF_KIND_STRUCT:
167                 case BTF_KIND_UNION:
168                         next_type += vlen * sizeof(struct btf_member);
169                         break;
170                 case BTF_KIND_ENUM:
171                         next_type += vlen * sizeof(struct btf_enum);
172                         break;
173                 case BTF_KIND_TYPEDEF:
174                 case BTF_KIND_PTR:
175                 case BTF_KIND_FWD:
176                 case BTF_KIND_VOLATILE:
177                 case BTF_KIND_CONST:
178                 case BTF_KIND_RESTRICT:
179                         break;
180                 default:
181                         elog("Unsupported BTF_KIND:%u\n",
182                              BTF_INFO_KIND(t->info));
183                         return -EINVAL;
184                 }
185
186                 err = btf_add_type(btf, t);
187                 if (err)
188                         return err;
189         }
190
191         return 0;
192 }
193
194 static const struct btf_type *btf_type_by_id(const struct btf *btf,
195                                              uint32_t type_id)
196 {
197         if (type_id > btf->nr_types)
198                 return NULL;
199
200         return btf->types[type_id];
201 }
202
203 static bool btf_type_is_void(const struct btf_type *t)
204 {
205         return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
206 }
207
208 static bool btf_type_is_void_or_null(const struct btf_type *t)
209 {
210         return !t || btf_type_is_void(t);
211 }
212
213 static int64_t btf_type_size(const struct btf_type *t)
214 {
215         switch (BTF_INFO_KIND(t->info)) {
216         case BTF_KIND_INT:
217         case BTF_KIND_STRUCT:
218         case BTF_KIND_UNION:
219         case BTF_KIND_ENUM:
220                 return t->size;
221         case BTF_KIND_PTR:
222                 return sizeof(void *);
223         default:
224                 return -EINVAL;
225         }
226 }
227
228 #define MAX_RESOLVE_DEPTH 32
229
230 int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)
231 {
232         const struct btf_array *array;
233         const struct btf_type *t;
234         uint32_t nelems = 1;
235         int64_t size = -1;
236         int i;
237
238         t = btf_type_by_id(btf, type_id);
239         for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
240              i++) {
241                 size = btf_type_size(t);
242                 if (size >= 0)
243                         break;
244
245                 switch (BTF_INFO_KIND(t->info)) {
246                 case BTF_KIND_TYPEDEF:
247                 case BTF_KIND_VOLATILE:
248                 case BTF_KIND_CONST:
249                 case BTF_KIND_RESTRICT:
250                         type_id = t->type;
251                         break;
252                 case BTF_KIND_ARRAY:
253                         array = (const struct btf_array *)(t + 1);
254                         if (nelems && array->nelems > UINT32_MAX / nelems)
255                                 return -E2BIG;
256                         nelems *= array->nelems;
257                         type_id = array->type;
258                         break;
259                 default:
260                         return -EINVAL;
261                 }
262
263                 t = btf_type_by_id(btf, type_id);
264         }
265
266         if (size < 0)
267                 return -EINVAL;
268
269         if (nelems && size > UINT32_MAX / nelems)
270                 return -E2BIG;
271
272         return nelems * size;
273 }
274
275 int32_t btf__find_by_name(const struct btf *btf, const char *type_name)
276 {
277         uint32_t i;
278
279         if (!strcmp(type_name, "void"))
280                 return 0;
281
282         for (i = 1; i <= btf->nr_types; i++) {
283                 const struct btf_type *t = btf->types[i];
284                 const char *name = btf_name_by_offset(btf, t->name_off);
285
286                 if (name && !strcmp(type_name, name))
287                         return i;
288         }
289
290         return -ENOENT;
291 }
292
293 void btf__free(struct btf *btf)
294 {
295         if (!btf)
296                 return;
297
298         if (btf->fd != -1)
299                 close(btf->fd);
300
301         free(btf->data);
302         free(btf->types);
303         free(btf);
304 }
305
306 struct btf *btf__new(uint8_t *data, uint32_t size,
307                      btf_print_fn_t err_log)
308 {
309         uint32_t log_buf_size = 0;
310         char *log_buf = NULL;
311         struct btf *btf;
312         int err;
313
314         btf = calloc(1, sizeof(struct btf));
315         if (!btf)
316                 return ERR_PTR(-ENOMEM);
317
318         btf->fd = -1;
319
320         if (err_log) {
321                 log_buf = malloc(BPF_LOG_BUF_SIZE);
322                 if (!log_buf) {
323                         err = -ENOMEM;
324                         goto done;
325                 }
326                 *log_buf = 0;
327                 log_buf_size = BPF_LOG_BUF_SIZE;
328         }
329
330         btf->data = malloc(size);
331         if (!btf->data) {
332                 err = -ENOMEM;
333                 goto done;
334         }
335
336         memcpy(btf->data, data, size);
337         btf->data_size = size;
338
339         btf->fd = bpf_load_btf(btf->data, btf->data_size,
340                                log_buf, log_buf_size, false);
341
342         if (btf->fd == -1) {
343                 err = -errno;
344                 elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
345                 if (log_buf && *log_buf)
346                         elog("%s\n", log_buf);
347                 goto done;
348         }
349
350         err = btf_parse_hdr(btf, err_log);
351         if (err)
352                 goto done;
353
354         err = btf_parse_str_sec(btf, err_log);
355         if (err)
356                 goto done;
357
358         err = btf_parse_type_sec(btf, err_log);
359
360 done:
361         free(log_buf);
362
363         if (err) {
364                 btf__free(btf);
365                 return ERR_PTR(err);
366         }
367
368         return btf;
369 }
370
371 int btf__fd(const struct btf *btf)
372 {
373         return btf->fd;
374 }