xref: /linux/tools/bpf/resolve_btfids/main.c (revision b828324bba8f575fde487a91fec07303789dda8a)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 /*
4  * resolve_btfids scans Elf object for .BTF_ids section and resolves
5  * its symbols with BTF ID values.
6  *
7  * Each symbol points to 4 bytes data and is expected to have
8  * following name syntax:
9  *
10  * __BTF_ID__<type>__<symbol>[__<id>]
11  *
12  * type is:
13  *
14  *   func    - lookup BTF_KIND_FUNC symbol with <symbol> name
15  *             and store its ID into the data:
16  *
17  *             __BTF_ID__func__vfs_close__1:
18  *             .zero 4
19  *
20  *   struct  - lookup BTF_KIND_STRUCT symbol with <symbol> name
21  *             and store its ID into the data:
22  *
23  *             __BTF_ID__struct__sk_buff__1:
24  *             .zero 4
25  *
26  *   union   - lookup BTF_KIND_UNION symbol with <symbol> name
27  *             and store its ID into the data:
28  *
29  *             __BTF_ID__union__thread_union__1:
30  *             .zero 4
31  *
32  *   typedef - lookup BTF_KIND_TYPEDEF symbol with <symbol> name
33  *             and store its ID into the data:
34  *
35  *             __BTF_ID__typedef__pid_t__1:
36  *             .zero 4
37  *
38  *   set     - store symbol size into first 4 bytes and sort following
39  *             ID list
40  *
41  *             __BTF_ID__set__list:
42  *             .zero 4
43  *             list:
44  *             __BTF_ID__func__vfs_getattr__3:
45  *             .zero 4
46  *             __BTF_ID__func__vfs_fallocate__4:
47  *             .zero 4
48  */
49 
50 #define  _GNU_SOURCE
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <libelf.h>
56 #include <gelf.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <linux/rbtree.h>
61 #include <linux/zalloc.h>
62 #include <linux/err.h>
63 #include <btf.h>
64 #include <libbpf.h>
65 #include <parse-options.h>
66 
67 #define BTF_IDS_SECTION	".BTF_ids"
68 #define BTF_ID		"__BTF_ID__"
69 
70 #define BTF_STRUCT	"struct"
71 #define BTF_UNION	"union"
72 #define BTF_TYPEDEF	"typedef"
73 #define BTF_FUNC	"func"
74 #define BTF_SET		"set"
75 
76 #define ADDR_CNT	100
77 
78 struct btf_id {
79 	struct rb_node	 rb_node;
80 	char		*name;
81 	union {
82 		int	 id;
83 		int	 cnt;
84 	};
85 	int		 addr_cnt;
86 	Elf64_Addr	 addr[ADDR_CNT];
87 };
88 
89 struct object {
90 	const char *path;
91 	const char *btf;
92 
93 	struct {
94 		int		 fd;
95 		Elf		*elf;
96 		Elf_Data	*symbols;
97 		Elf_Data	*idlist;
98 		int		 symbols_shndx;
99 		int		 idlist_shndx;
100 		size_t		 strtabidx;
101 		unsigned long	 idlist_addr;
102 	} efile;
103 
104 	struct rb_root	sets;
105 	struct rb_root	structs;
106 	struct rb_root	unions;
107 	struct rb_root	typedefs;
108 	struct rb_root	funcs;
109 
110 	int nr_funcs;
111 	int nr_structs;
112 	int nr_unions;
113 	int nr_typedefs;
114 };
115 
116 static int verbose;
117 
118 int eprintf(int level, int var, const char *fmt, ...)
119 {
120 	va_list args;
121 	int ret;
122 
123 	if (var >= level) {
124 		va_start(args, fmt);
125 		ret = vfprintf(stderr, fmt, args);
126 		va_end(args);
127 	}
128 	return ret;
129 }
130 
131 #ifndef pr_fmt
132 #define pr_fmt(fmt) fmt
133 #endif
134 
135 #define pr_debug(fmt, ...) \
136 	eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
137 #define pr_debugN(n, fmt, ...) \
138 	eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
139 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
140 #define pr_err(fmt, ...) \
141 	eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
142 #define pr_info(fmt, ...) \
143 	eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
144 
145 static bool is_btf_id(const char *name)
146 {
147 	return name && !strncmp(name, BTF_ID, sizeof(BTF_ID) - 1);
148 }
149 
150 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
151 {
152 	struct rb_node *p = root->rb_node;
153 	struct btf_id *id;
154 	int cmp;
155 
156 	while (p) {
157 		id = rb_entry(p, struct btf_id, rb_node);
158 		cmp = strcmp(id->name, name);
159 		if (cmp < 0)
160 			p = p->rb_left;
161 		else if (cmp > 0)
162 			p = p->rb_right;
163 		else
164 			return id;
165 	}
166 	return NULL;
167 }
168 
169 static struct btf_id*
170 btf_id__add(struct rb_root *root, char *name, bool unique)
171 {
172 	struct rb_node **p = &root->rb_node;
173 	struct rb_node *parent = NULL;
174 	struct btf_id *id;
175 	int cmp;
176 
177 	while (*p != NULL) {
178 		parent = *p;
179 		id = rb_entry(parent, struct btf_id, rb_node);
180 		cmp = strcmp(id->name, name);
181 		if (cmp < 0)
182 			p = &(*p)->rb_left;
183 		else if (cmp > 0)
184 			p = &(*p)->rb_right;
185 		else
186 			return unique ? NULL : id;
187 	}
188 
189 	id = zalloc(sizeof(*id));
190 	if (id) {
191 		pr_debug("adding symbol %s\n", name);
192 		id->name = name;
193 		rb_link_node(&id->rb_node, parent, p);
194 		rb_insert_color(&id->rb_node, root);
195 	}
196 	return id;
197 }
198 
199 static char *get_id(const char *prefix_end)
200 {
201 	/*
202 	 * __BTF_ID__func__vfs_truncate__0
203 	 * prefix_end =  ^
204 	 * pos        =    ^
205 	 */
206 	int len = strlen(prefix_end);
207 	int pos = sizeof("__") - 1;
208 	char *p, *id;
209 
210 	if (pos >= len)
211 		return NULL;
212 
213 	id = strdup(prefix_end + pos);
214 	if (id) {
215 		/*
216 		 * __BTF_ID__func__vfs_truncate__0
217 		 * id =            ^
218 		 *
219 		 * cut the unique id part
220 		 */
221 		p = strrchr(id, '_');
222 		p--;
223 		if (*p != '_') {
224 			free(id);
225 			return NULL;
226 		}
227 		*p = '\0';
228 	}
229 	return id;
230 }
231 
232 static struct btf_id *add_set(struct object *obj, char *name)
233 {
234 	/*
235 	 * __BTF_ID__set__name
236 	 * name =    ^
237 	 * id   =         ^
238 	 */
239 	char *id = name + sizeof(BTF_SET "__") - 1;
240 	int len = strlen(name);
241 
242 	if (id >= name + len) {
243 		pr_err("FAILED to parse set name: %s\n", name);
244 		return NULL;
245 	}
246 
247 	return btf_id__add(&obj->sets, id, true);
248 }
249 
250 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
251 {
252 	char *id;
253 
254 	id = get_id(name + size);
255 	if (!id) {
256 		pr_err("FAILED to parse symbol name: %s\n", name);
257 		return NULL;
258 	}
259 
260 	return btf_id__add(root, id, false);
261 }
262 
263 /* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */
264 #ifndef SHF_COMPRESSED
265 #define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */
266 #endif
267 
268 /*
269  * The data of compressed section should be aligned to 4
270  * (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
271  * sets sh_addralign to 1, which makes libelf fail with
272  * misaligned section error during the update:
273  *    FAILED elf_update(WRITE): invalid section alignment
274  *
275  * While waiting for ld fix, we fix the compressed sections
276  * sh_addralign value manualy.
277  */
278 static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
279 {
280 	int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
281 
282 	if (!(sh->sh_flags & SHF_COMPRESSED))
283 		return 0;
284 
285 	if (sh->sh_addralign == expected)
286 		return 0;
287 
288 	pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
289 		  sh->sh_addralign, expected);
290 
291 	sh->sh_addralign = expected;
292 
293 	if (gelf_update_shdr(scn, sh) == 0) {
294 		printf("FAILED cannot update section header: %s\n",
295 			elf_errmsg(-1));
296 		return -1;
297 	}
298 	return 0;
299 }
300 
301 static int elf_collect(struct object *obj)
302 {
303 	Elf_Scn *scn = NULL;
304 	size_t shdrstrndx;
305 	int idx = 0;
306 	Elf *elf;
307 	int fd;
308 
309 	fd = open(obj->path, O_RDWR, 0666);
310 	if (fd == -1) {
311 		pr_err("FAILED cannot open %s: %s\n",
312 			obj->path, strerror(errno));
313 		return -1;
314 	}
315 
316 	elf_version(EV_CURRENT);
317 
318 	elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
319 	if (!elf) {
320 		pr_err("FAILED cannot create ELF descriptor: %s\n",
321 			elf_errmsg(-1));
322 		return -1;
323 	}
324 
325 	obj->efile.fd  = fd;
326 	obj->efile.elf = elf;
327 
328 	elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
329 
330 	if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
331 		pr_err("FAILED cannot get shdr str ndx\n");
332 		return -1;
333 	}
334 
335 	/*
336 	 * Scan all the elf sections and look for save data
337 	 * from .BTF_ids section and symbols.
338 	 */
339 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
340 		Elf_Data *data;
341 		GElf_Shdr sh;
342 		char *name;
343 
344 		idx++;
345 		if (gelf_getshdr(scn, &sh) != &sh) {
346 			pr_err("FAILED get section(%d) header\n", idx);
347 			return -1;
348 		}
349 
350 		name = elf_strptr(elf, shdrstrndx, sh.sh_name);
351 		if (!name) {
352 			pr_err("FAILED get section(%d) name\n", idx);
353 			return -1;
354 		}
355 
356 		data = elf_getdata(scn, 0);
357 		if (!data) {
358 			pr_err("FAILED to get section(%d) data from %s\n",
359 				idx, name);
360 			return -1;
361 		}
362 
363 		pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
364 			  idx, name, (unsigned long) data->d_size,
365 			  (int) sh.sh_link, (unsigned long) sh.sh_flags,
366 			  (int) sh.sh_type);
367 
368 		if (sh.sh_type == SHT_SYMTAB) {
369 			obj->efile.symbols       = data;
370 			obj->efile.symbols_shndx = idx;
371 			obj->efile.strtabidx     = sh.sh_link;
372 		} else if (!strcmp(name, BTF_IDS_SECTION)) {
373 			obj->efile.idlist       = data;
374 			obj->efile.idlist_shndx = idx;
375 			obj->efile.idlist_addr  = sh.sh_addr;
376 		}
377 
378 		if (compressed_section_fix(elf, scn, &sh))
379 			return -1;
380 	}
381 
382 	return 0;
383 }
384 
385 static int symbols_collect(struct object *obj)
386 {
387 	Elf_Scn *scn = NULL;
388 	int n, i, err = 0;
389 	GElf_Shdr sh;
390 	char *name;
391 
392 	scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
393 	if (!scn)
394 		return -1;
395 
396 	if (gelf_getshdr(scn, &sh) != &sh)
397 		return -1;
398 
399 	n = sh.sh_size / sh.sh_entsize;
400 
401 	/*
402 	 * Scan symbols and look for the ones starting with
403 	 * __BTF_ID__* over .BTF_ids section.
404 	 */
405 	for (i = 0; !err && i < n; i++) {
406 		char *tmp, *prefix;
407 		struct btf_id *id;
408 		GElf_Sym sym;
409 		int err = -1;
410 
411 		if (!gelf_getsym(obj->efile.symbols, i, &sym))
412 			return -1;
413 
414 		if (sym.st_shndx != obj->efile.idlist_shndx)
415 			continue;
416 
417 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
418 				  sym.st_name);
419 
420 		if (!is_btf_id(name))
421 			continue;
422 
423 		/*
424 		 * __BTF_ID__TYPE__vfs_truncate__0
425 		 * prefix =  ^
426 		 */
427 		prefix = name + sizeof(BTF_ID) - 1;
428 
429 		/* struct */
430 		if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
431 			obj->nr_structs++;
432 			id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
433 		/* union  */
434 		} else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
435 			obj->nr_unions++;
436 			id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
437 		/* typedef */
438 		} else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
439 			obj->nr_typedefs++;
440 			id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
441 		/* func */
442 		} else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
443 			obj->nr_funcs++;
444 			id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
445 		/* set */
446 		} else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
447 			id = add_set(obj, prefix);
448 			/*
449 			 * SET objects store list's count, which is encoded
450 			 * in symbol's size, together with 'cnt' field hence
451 			 * that - 1.
452 			 */
453 			if (id)
454 				id->cnt = sym.st_size / sizeof(int) - 1;
455 		} else {
456 			pr_err("FAILED unsupported prefix %s\n", prefix);
457 			return -1;
458 		}
459 
460 		if (!id)
461 			return -ENOMEM;
462 
463 		if (id->addr_cnt >= ADDR_CNT) {
464 			pr_err("FAILED symbol %s crossed the number of allowed lists\n",
465 				id->name);
466 			return -1;
467 		}
468 		id->addr[id->addr_cnt++] = sym.st_value;
469 	}
470 
471 	return 0;
472 }
473 
474 static int symbols_resolve(struct object *obj)
475 {
476 	int nr_typedefs = obj->nr_typedefs;
477 	int nr_structs  = obj->nr_structs;
478 	int nr_unions   = obj->nr_unions;
479 	int nr_funcs    = obj->nr_funcs;
480 	int err, type_id;
481 	struct btf *btf;
482 	__u32 nr_types;
483 
484 	btf = btf__parse(obj->btf ?: obj->path, NULL);
485 	err = libbpf_get_error(btf);
486 	if (err) {
487 		pr_err("FAILED: load BTF from %s: %s\n",
488 			obj->path, strerror(-err));
489 		return -1;
490 	}
491 
492 	err = -1;
493 	nr_types = btf__get_nr_types(btf);
494 
495 	/*
496 	 * Iterate all the BTF types and search for collected symbol IDs.
497 	 */
498 	for (type_id = 1; type_id <= nr_types; type_id++) {
499 		const struct btf_type *type;
500 		struct rb_root *root;
501 		struct btf_id *id;
502 		const char *str;
503 		int *nr;
504 
505 		type = btf__type_by_id(btf, type_id);
506 		if (!type) {
507 			pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
508 				type_id);
509 			goto out;
510 		}
511 
512 		if (btf_is_func(type) && nr_funcs) {
513 			nr   = &nr_funcs;
514 			root = &obj->funcs;
515 		} else if (btf_is_struct(type) && nr_structs) {
516 			nr   = &nr_structs;
517 			root = &obj->structs;
518 		} else if (btf_is_union(type) && nr_unions) {
519 			nr   = &nr_unions;
520 			root = &obj->unions;
521 		} else if (btf_is_typedef(type) && nr_typedefs) {
522 			nr   = &nr_typedefs;
523 			root = &obj->typedefs;
524 		} else
525 			continue;
526 
527 		str = btf__name_by_offset(btf, type->name_off);
528 		if (!str) {
529 			pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
530 				type_id);
531 			goto out;
532 		}
533 
534 		id = btf_id__find(root, str);
535 		if (id) {
536 			if (id->id) {
537 				pr_info("WARN: multiple IDs found for '%s': %d, %d - using %d\n",
538 					str, id->id, type_id, id->id);
539 			} else {
540 				id->id = type_id;
541 				(*nr)--;
542 			}
543 		}
544 	}
545 
546 	err = 0;
547 out:
548 	btf__free(btf);
549 	return err;
550 }
551 
552 static int id_patch(struct object *obj, struct btf_id *id)
553 {
554 	Elf_Data *data = obj->efile.idlist;
555 	int *ptr = data->d_buf;
556 	int i;
557 
558 	if (!id->id) {
559 		pr_err("FAILED unresolved symbol %s\n", id->name);
560 		return -EINVAL;
561 	}
562 
563 	for (i = 0; i < id->addr_cnt; i++) {
564 		unsigned long addr = id->addr[i];
565 		unsigned long idx = addr - obj->efile.idlist_addr;
566 
567 		pr_debug("patching addr %5lu: ID %7d [%s]\n",
568 			 idx, id->id, id->name);
569 
570 		if (idx >= data->d_size) {
571 			pr_err("FAILED patching index %lu out of bounds %lu\n",
572 				idx, data->d_size);
573 			return -1;
574 		}
575 
576 		idx = idx / sizeof(int);
577 		ptr[idx] = id->id;
578 	}
579 
580 	return 0;
581 }
582 
583 static int __symbols_patch(struct object *obj, struct rb_root *root)
584 {
585 	struct rb_node *next;
586 	struct btf_id *id;
587 
588 	next = rb_first(root);
589 	while (next) {
590 		id = rb_entry(next, struct btf_id, rb_node);
591 
592 		if (id_patch(obj, id))
593 			return -1;
594 
595 		next = rb_next(next);
596 	}
597 	return 0;
598 }
599 
600 static int cmp_id(const void *pa, const void *pb)
601 {
602 	const int *a = pa, *b = pb;
603 
604 	return *a - *b;
605 }
606 
607 static int sets_patch(struct object *obj)
608 {
609 	Elf_Data *data = obj->efile.idlist;
610 	int *ptr = data->d_buf;
611 	struct rb_node *next;
612 
613 	next = rb_first(&obj->sets);
614 	while (next) {
615 		unsigned long addr, idx;
616 		struct btf_id *id;
617 		int *base;
618 		int cnt;
619 
620 		id   = rb_entry(next, struct btf_id, rb_node);
621 		addr = id->addr[0];
622 		idx  = addr - obj->efile.idlist_addr;
623 
624 		/* sets are unique */
625 		if (id->addr_cnt != 1) {
626 			pr_err("FAILED malformed data for set '%s'\n",
627 				id->name);
628 			return -1;
629 		}
630 
631 		idx = idx / sizeof(int);
632 		base = &ptr[idx] + 1;
633 		cnt = ptr[idx];
634 
635 		pr_debug("sorting  addr %5lu: cnt %6d [%s]\n",
636 			 (idx + 1) * sizeof(int), cnt, id->name);
637 
638 		qsort(base, cnt, sizeof(int), cmp_id);
639 
640 		next = rb_next(next);
641 	}
642 	return 0;
643 }
644 
645 static int symbols_patch(struct object *obj)
646 {
647 	int err;
648 
649 	if (__symbols_patch(obj, &obj->structs)  ||
650 	    __symbols_patch(obj, &obj->unions)   ||
651 	    __symbols_patch(obj, &obj->typedefs) ||
652 	    __symbols_patch(obj, &obj->funcs)    ||
653 	    __symbols_patch(obj, &obj->sets))
654 		return -1;
655 
656 	if (sets_patch(obj))
657 		return -1;
658 
659 	elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
660 
661 	err = elf_update(obj->efile.elf, ELF_C_WRITE);
662 	if (err < 0) {
663 		pr_err("FAILED elf_update(WRITE): %s\n",
664 			elf_errmsg(-1));
665 	}
666 
667 	pr_debug("update %s for %s\n",
668 		 err >= 0 ? "ok" : "failed", obj->path);
669 	return err < 0 ? -1 : 0;
670 }
671 
672 static const char * const resolve_btfids_usage[] = {
673 	"resolve_btfids [<options>] <ELF object>",
674 	NULL
675 };
676 
677 int main(int argc, const char **argv)
678 {
679 	bool no_fail = false;
680 	struct object obj = {
681 		.efile = {
682 			.idlist_shndx  = -1,
683 			.symbols_shndx = -1,
684 		},
685 		.structs  = RB_ROOT,
686 		.unions   = RB_ROOT,
687 		.typedefs = RB_ROOT,
688 		.funcs    = RB_ROOT,
689 		.sets     = RB_ROOT,
690 	};
691 	struct option btfid_options[] = {
692 		OPT_INCR('v', "verbose", &verbose,
693 			 "be more verbose (show errors, etc)"),
694 		OPT_STRING(0, "btf", &obj.btf, "BTF data",
695 			   "BTF data"),
696 		OPT_BOOLEAN(0, "no-fail", &no_fail,
697 			   "do not fail if " BTF_IDS_SECTION " section is not found"),
698 		OPT_END()
699 	};
700 	int err = -1;
701 
702 	argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
703 			     PARSE_OPT_STOP_AT_NON_OPTION);
704 	if (argc != 1)
705 		usage_with_options(resolve_btfids_usage, btfid_options);
706 
707 	obj.path = argv[0];
708 
709 	if (elf_collect(&obj))
710 		goto out;
711 
712 	/*
713 	 * We did not find .BTF_ids section or symbols section,
714 	 * nothing to do..
715 	 */
716 	if (obj.efile.idlist_shndx == -1 ||
717 	    obj.efile.symbols_shndx == -1) {
718 		if (no_fail)
719 			return 0;
720 		pr_err("FAILED to find needed sections\n");
721 		return -1;
722 	}
723 
724 	if (symbols_collect(&obj))
725 		goto out;
726 
727 	if (symbols_resolve(&obj))
728 		goto out;
729 
730 	if (symbols_patch(&obj))
731 		goto out;
732 
733 	err = 0;
734 out:
735 	if (obj.efile.elf)
736 		elf_end(obj.efile.elf);
737 	close(obj.efile.fd);
738 	return err;
739 }
740