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