xref: /linux/tools/bpf/resolve_btfids/main.c (revision eb71ab2bf72260054677e348498ba995a057c463)
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  *   set8    - store symbol size into first 4 bytes and sort following
50  *             ID list
51  *
52  *             __BTF_ID__set8__list:
53  *             .zero 8
54  *             list:
55  *             __BTF_ID__func__vfs_getattr__3:
56  *             .zero 4
57  *	       .word (1 << 0) | (1 << 2)
58  *             __BTF_ID__func__vfs_fallocate__5:
59  *             .zero 4
60  *	       .word (1 << 3) | (1 << 1) | (1 << 2)
61  */
62 
63 #define  _GNU_SOURCE
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <libelf.h>
69 #include <gelf.h>
70 #include <sys/stat.h>
71 #include <fcntl.h>
72 #include <errno.h>
73 #include <linux/btf_ids.h>
74 #include <linux/kallsyms.h>
75 #include <linux/rbtree.h>
76 #include <linux/zalloc.h>
77 #include <linux/err.h>
78 #include <linux/limits.h>
79 #include <bpf/btf.h>
80 #include <bpf/libbpf.h>
81 #include <subcmd/parse-options.h>
82 
83 #define BTF_IDS_SECTION	".BTF_ids"
84 #define BTF_ID_PREFIX	"__BTF_ID__"
85 
86 #define BTF_STRUCT	"struct"
87 #define BTF_UNION	"union"
88 #define BTF_TYPEDEF	"typedef"
89 #define BTF_FUNC	"func"
90 #define BTF_SET		"set"
91 #define BTF_SET8	"set8"
92 
93 #define ADDR_CNT	100
94 
95 #if __BYTE_ORDER == __LITTLE_ENDIAN
96 # define ELFDATANATIVE	ELFDATA2LSB
97 #elif __BYTE_ORDER == __BIG_ENDIAN
98 # define ELFDATANATIVE	ELFDATA2MSB
99 #else
100 # error "Unknown machine endianness!"
101 #endif
102 
103 enum btf_id_kind {
104 	BTF_ID_KIND_NONE,
105 	BTF_ID_KIND_SYM,
106 	BTF_ID_KIND_SET,
107 	BTF_ID_KIND_SET8
108 };
109 
110 struct btf_id {
111 	struct rb_node	 rb_node;
112 	char		*name;
113 	union {
114 		int	 id;
115 		int	 cnt;
116 	};
117 	enum btf_id_kind kind;
118 	int		 addr_cnt;
119 	Elf64_Addr	 addr[ADDR_CNT];
120 };
121 
122 struct object {
123 	const char *path;
124 	const char *btf_path;
125 	const char *base_btf_path;
126 
127 	struct btf *btf;
128 	struct btf *base_btf;
129 	bool distill_base;
130 
131 	struct {
132 		int		 fd;
133 		Elf		*elf;
134 		Elf_Data	*symbols;
135 		Elf_Data	*idlist;
136 		int		 symbols_shndx;
137 		int		 idlist_shndx;
138 		size_t		 strtabidx;
139 		unsigned long	 idlist_addr;
140 		int		 encoding;
141 	} efile;
142 
143 	struct rb_root	sets;
144 	struct rb_root	structs;
145 	struct rb_root	unions;
146 	struct rb_root	typedefs;
147 	struct rb_root	funcs;
148 
149 	int nr_funcs;
150 	int nr_structs;
151 	int nr_unions;
152 	int nr_typedefs;
153 };
154 
155 #define KF_IMPLICIT_ARGS (1 << 16)
156 #define KF_IMPL_SUFFIX "_impl"
157 
158 struct kfunc {
159 	const char *name;
160 	u32 btf_id;
161 	u32 flags;
162 };
163 
164 struct btf2btf_context {
165 	struct btf *btf;
166 	u32 *decl_tags;
167 	u32 nr_decl_tags;
168 	u32 max_decl_tags;
169 	struct kfunc *kfuncs;
170 	u32 nr_kfuncs;
171 	u32 max_kfuncs;
172 };
173 
174 static int verbose;
175 static int warnings;
176 
eprintf(int level,int var,const char * fmt,...)177 static int eprintf(int level, int var, const char *fmt, ...)
178 {
179 	va_list args;
180 	int ret = 0;
181 
182 	if (var >= level) {
183 		va_start(args, fmt);
184 		ret = vfprintf(stderr, fmt, args);
185 		va_end(args);
186 	}
187 	return ret;
188 }
189 
190 #ifndef pr_fmt
191 #define pr_fmt(fmt) fmt
192 #endif
193 
194 #define pr_debug(fmt, ...) \
195 	eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
196 #define pr_debugN(n, fmt, ...) \
197 	eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
198 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
199 #define pr_err(fmt, ...) \
200 	eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
201 #define pr_info(fmt, ...) \
202 	eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
203 
is_btf_id(const char * name)204 static bool is_btf_id(const char *name)
205 {
206 	return name && !strncmp(name, BTF_ID_PREFIX, sizeof(BTF_ID_PREFIX) - 1);
207 }
208 
btf_id__find(struct rb_root * root,const char * name)209 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
210 {
211 	struct rb_node *p = root->rb_node;
212 	struct btf_id *id;
213 	int cmp;
214 
215 	while (p) {
216 		id = rb_entry(p, struct btf_id, rb_node);
217 		cmp = strcmp(id->name, name);
218 		if (cmp < 0)
219 			p = p->rb_left;
220 		else if (cmp > 0)
221 			p = p->rb_right;
222 		else
223 			return id;
224 	}
225 	return NULL;
226 }
227 
__btf_id__add(struct rb_root * root,const char * name,enum btf_id_kind kind,bool unique)228 static struct btf_id *__btf_id__add(struct rb_root *root,
229 				    const char *name,
230 				    enum btf_id_kind kind,
231 				    bool unique)
232 {
233 	struct rb_node **p = &root->rb_node;
234 	struct rb_node *parent = NULL;
235 	struct btf_id *id;
236 	int cmp;
237 
238 	while (*p != NULL) {
239 		parent = *p;
240 		id = rb_entry(parent, struct btf_id, rb_node);
241 		cmp = strcmp(id->name, name);
242 		if (cmp < 0)
243 			p = &(*p)->rb_left;
244 		else if (cmp > 0)
245 			p = &(*p)->rb_right;
246 		else
247 			return unique ? NULL : id;
248 	}
249 
250 	id = zalloc(sizeof(*id));
251 	if (id) {
252 		pr_debug("adding symbol %s\n", name);
253 		id->name = strdup(name);
254 		if (!id->name) {
255 			free(id);
256 			return NULL;
257 		}
258 		id->kind = kind;
259 		rb_link_node(&id->rb_node, parent, p);
260 		rb_insert_color(&id->rb_node, root);
261 	}
262 	return id;
263 }
264 
btf_id__add(struct rb_root * root,const char * name,enum btf_id_kind kind)265 static inline struct btf_id *btf_id__add(struct rb_root *root,
266 					 const char *name,
267 					 enum btf_id_kind kind)
268 {
269 	return __btf_id__add(root, name, kind, false);
270 }
271 
btf_id__add_unique(struct rb_root * root,const char * name,enum btf_id_kind kind)272 static inline struct btf_id *btf_id__add_unique(struct rb_root *root,
273 						const char *name,
274 						enum btf_id_kind kind)
275 {
276 	return __btf_id__add(root, name, kind, true);
277 }
278 
get_id(const char * prefix_end,char * buf,size_t buf_sz)279 static int get_id(const char *prefix_end, char *buf, size_t buf_sz)
280 {
281 	/*
282 	 * __BTF_ID__func__vfs_truncate__0
283 	 * prefix_end =  ^
284 	 * pos        =    ^
285 	 */
286 	int len = strlen(prefix_end);
287 	int pos = sizeof("__") - 1;
288 	char *p;
289 
290 	if (pos >= len)
291 		return -1;
292 
293 	if (len - pos >= buf_sz)
294 		return -1;
295 
296 	strcpy(buf, prefix_end + pos);
297 	/*
298 	 * __BTF_ID__func__vfs_truncate__0
299 	 * buf =           ^
300 	 *
301 	 * cut the unique id part
302 	 */
303 	p = strrchr(buf, '_');
304 	p--;
305 	if (*p != '_')
306 		return -1;
307 	*p = '\0';
308 
309 	return 0;
310 }
311 
add_set(struct object * obj,char * name,enum btf_id_kind kind)312 static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind kind)
313 {
314 	int len = strlen(name);
315 	int prefixlen;
316 	char *id;
317 
318 	/*
319 	 * __BTF_ID__set__name
320 	 * name =    ^
321 	 * id   =         ^
322 	 */
323 	switch (kind) {
324 	case BTF_ID_KIND_SET:
325 		prefixlen = sizeof(BTF_SET "__") - 1;
326 		break;
327 	case BTF_ID_KIND_SET8:
328 		prefixlen = sizeof(BTF_SET8 "__") - 1;
329 		break;
330 	default:
331 		pr_err("Unexpected kind %d passed to %s() for symbol %s\n", kind, __func__, name);
332 		return NULL;
333 	}
334 
335 	id = name + prefixlen;
336 	if (id >= name + len) {
337 		pr_err("FAILED to parse set name: %s\n", name);
338 		return NULL;
339 	}
340 
341 	return btf_id__add_unique(&obj->sets, id, kind);
342 }
343 
add_symbol(struct rb_root * root,char * name,size_t size)344 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
345 {
346 	char id[KSYM_NAME_LEN];
347 
348 	if (get_id(name + size, id, sizeof(id))) {
349 		pr_err("FAILED to parse symbol name: %s\n", name);
350 		return NULL;
351 	}
352 
353 	return btf_id__add(root, id, BTF_ID_KIND_SYM);
354 }
355 
btf_id__free_all(struct rb_root * root)356 static void btf_id__free_all(struct rb_root *root)
357 {
358 	struct rb_node *next;
359 	struct btf_id *id;
360 
361 	next = rb_first(root);
362 	while (next) {
363 		id = rb_entry(next, struct btf_id, rb_node);
364 		next = rb_next(&id->rb_node);
365 		rb_erase(&id->rb_node, root);
366 		free(id->name);
367 		free(id);
368 	}
369 }
370 
bswap_32_data(void * data,u32 nr_bytes)371 static void bswap_32_data(void *data, u32 nr_bytes)
372 {
373 	u32 cnt, i;
374 	u32 *ptr;
375 
376 	cnt = nr_bytes / sizeof(u32);
377 	ptr = data;
378 
379 	for (i = 0; i < cnt; i++)
380 		ptr[i] = bswap_32(ptr[i]);
381 }
382 
elf_collect(struct object * obj)383 static int elf_collect(struct object *obj)
384 {
385 	Elf_Scn *scn = NULL;
386 	size_t shdrstrndx;
387 	GElf_Ehdr ehdr;
388 	int idx = 0;
389 	Elf *elf;
390 	int fd;
391 
392 	fd = open(obj->path, O_RDWR, 0666);
393 	if (fd == -1) {
394 		pr_err("FAILED cannot open %s: %s\n",
395 			obj->path, strerror(errno));
396 		return -1;
397 	}
398 
399 	elf_version(EV_CURRENT);
400 
401 	elf = elf_begin(fd, ELF_C_READ_MMAP_PRIVATE, NULL);
402 	if (!elf) {
403 		close(fd);
404 		pr_err("FAILED cannot create ELF descriptor: %s\n",
405 			elf_errmsg(-1));
406 		return -1;
407 	}
408 
409 	obj->efile.fd  = fd;
410 	obj->efile.elf = elf;
411 
412 	elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
413 
414 	if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
415 		pr_err("FAILED cannot get shdr str ndx\n");
416 		return -1;
417 	}
418 
419 	if (gelf_getehdr(obj->efile.elf, &ehdr) == NULL) {
420 		pr_err("FAILED cannot get ELF header: %s\n",
421 			elf_errmsg(-1));
422 		return -1;
423 	}
424 	obj->efile.encoding = ehdr.e_ident[EI_DATA];
425 
426 	/*
427 	 * Scan all the elf sections and look for save data
428 	 * from .BTF_ids section and symbols.
429 	 */
430 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
431 		Elf_Data *data;
432 		GElf_Shdr sh;
433 		char *name;
434 
435 		idx++;
436 		if (gelf_getshdr(scn, &sh) != &sh) {
437 			pr_err("FAILED get section(%d) header\n", idx);
438 			return -1;
439 		}
440 
441 		name = elf_strptr(elf, shdrstrndx, sh.sh_name);
442 		if (!name) {
443 			pr_err("FAILED get section(%d) name\n", idx);
444 			return -1;
445 		}
446 
447 		data = elf_getdata(scn, 0);
448 		if (!data) {
449 			pr_err("FAILED to get section(%d) data from %s\n",
450 				idx, name);
451 			return -1;
452 		}
453 
454 		pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
455 			  idx, name, (unsigned long) data->d_size,
456 			  (int) sh.sh_link, (unsigned long) sh.sh_flags,
457 			  (int) sh.sh_type);
458 
459 		if (sh.sh_type == SHT_SYMTAB) {
460 			obj->efile.symbols       = data;
461 			obj->efile.symbols_shndx = idx;
462 			obj->efile.strtabidx     = sh.sh_link;
463 		} else if (!strcmp(name, BTF_IDS_SECTION)) {
464 			/*
465 			 * If target endianness differs from host, we need to bswap32
466 			 * the .BTF_ids section data on load, because .BTF_ids has
467 			 * Elf_Type = ELF_T_BYTE, and so libelf returns data buffer in
468 			 * the target endianness. We repeat this on dump.
469 			 */
470 			if (obj->efile.encoding != ELFDATANATIVE) {
471 				pr_debug("bswap_32 .BTF_ids data from target to host endianness\n");
472 				bswap_32_data(data->d_buf, data->d_size);
473 			}
474 			obj->efile.idlist       = data;
475 			obj->efile.idlist_shndx = idx;
476 			obj->efile.idlist_addr  = sh.sh_addr;
477 		}
478 	}
479 
480 	return 0;
481 }
482 
symbols_collect(struct object * obj)483 static int symbols_collect(struct object *obj)
484 {
485 	Elf_Scn *scn = NULL;
486 	int n, i;
487 	GElf_Shdr sh;
488 	char *name;
489 
490 	scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
491 	if (!scn)
492 		return -1;
493 
494 	if (gelf_getshdr(scn, &sh) != &sh)
495 		return -1;
496 
497 	n = sh.sh_size / sh.sh_entsize;
498 
499 	/*
500 	 * Scan symbols and look for the ones starting with
501 	 * __BTF_ID__* over .BTF_ids section.
502 	 */
503 	for (i = 0; i < n; i++) {
504 		char *prefix;
505 		struct btf_id *id;
506 		GElf_Sym sym;
507 
508 		if (!gelf_getsym(obj->efile.symbols, i, &sym))
509 			return -1;
510 
511 		if (sym.st_shndx != obj->efile.idlist_shndx)
512 			continue;
513 
514 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
515 				  sym.st_name);
516 
517 		if (!is_btf_id(name))
518 			continue;
519 
520 		/*
521 		 * __BTF_ID__TYPE__vfs_truncate__0
522 		 * prefix =  ^
523 		 */
524 		prefix = name + sizeof(BTF_ID_PREFIX) - 1;
525 
526 		/* struct */
527 		if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
528 			obj->nr_structs++;
529 			id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
530 		/* union  */
531 		} else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
532 			obj->nr_unions++;
533 			id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
534 		/* typedef */
535 		} else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
536 			obj->nr_typedefs++;
537 			id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
538 		/* func */
539 		} else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
540 			obj->nr_funcs++;
541 			id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
542 		/* set8 */
543 		} else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
544 			id = add_set(obj, prefix, BTF_ID_KIND_SET8);
545 			/*
546 			 * SET8 objects store list's count, which is encoded
547 			 * in symbol's size, together with 'cnt' field hence
548 			 * that - 1.
549 			 */
550 			if (id)
551 				id->cnt = sym.st_size / sizeof(uint64_t) - 1;
552 		/* set */
553 		} else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
554 			id = add_set(obj, prefix, BTF_ID_KIND_SET);
555 			/*
556 			 * SET objects store list's count, which is encoded
557 			 * in symbol's size, together with 'cnt' field hence
558 			 * that - 1.
559 			 */
560 			if (id)
561 				id->cnt = sym.st_size / sizeof(int) - 1;
562 		} else {
563 			pr_err("FAILED unsupported prefix %s\n", prefix);
564 			return -1;
565 		}
566 
567 		if (!id)
568 			return -EINVAL;
569 
570 		if (id->addr_cnt >= ADDR_CNT) {
571 			pr_err("FAILED symbol %s crossed the number of allowed lists\n",
572 				id->name);
573 			return -1;
574 		}
575 		id->addr[id->addr_cnt++] = sym.st_value;
576 	}
577 
578 	return 0;
579 }
580 
load_btf(struct object * obj)581 static int load_btf(struct object *obj)
582 {
583 	struct btf *base_btf = NULL, *btf = NULL;
584 	int err;
585 
586 	if (obj->base_btf_path) {
587 		base_btf = btf__parse(obj->base_btf_path, NULL);
588 		err = libbpf_get_error(base_btf);
589 		if (err) {
590 			pr_err("FAILED: load base BTF from %s: %s\n",
591 			       obj->base_btf_path, strerror(-err));
592 			goto out_err;
593 		}
594 	}
595 
596 	btf = btf__parse_split(obj->btf_path ?: obj->path, base_btf);
597 	err = libbpf_get_error(btf);
598 	if (err) {
599 		pr_err("FAILED: load BTF from %s: %s\n",
600 			obj->btf_path ?: obj->path, strerror(-err));
601 		goto out_err;
602 	}
603 
604 	obj->base_btf = base_btf;
605 	obj->btf = btf;
606 
607 	return 0;
608 
609 out_err:
610 	btf__free(base_btf);
611 	btf__free(btf);
612 	obj->base_btf = NULL;
613 	obj->btf = NULL;
614 	return err;
615 }
616 
symbols_resolve(struct object * obj)617 static int symbols_resolve(struct object *obj)
618 {
619 	int nr_typedefs = obj->nr_typedefs;
620 	int nr_structs  = obj->nr_structs;
621 	int nr_unions   = obj->nr_unions;
622 	int nr_funcs    = obj->nr_funcs;
623 	struct btf *btf = obj->btf;
624 	int err, type_id;
625 	__u32 nr_types;
626 
627 	err = -1;
628 	nr_types = btf__type_cnt(btf);
629 
630 	/*
631 	 * Iterate all the BTF types and search for collected symbol IDs.
632 	 */
633 	for (type_id = 1; type_id < nr_types; type_id++) {
634 		const struct btf_type *type;
635 		struct rb_root *root;
636 		struct btf_id *id;
637 		const char *str;
638 		int *nr;
639 
640 		type = btf__type_by_id(btf, type_id);
641 		if (!type) {
642 			pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
643 				type_id);
644 			goto out;
645 		}
646 
647 		if (btf_is_func(type) && nr_funcs) {
648 			nr   = &nr_funcs;
649 			root = &obj->funcs;
650 		} else if (btf_is_struct(type) && nr_structs) {
651 			nr   = &nr_structs;
652 			root = &obj->structs;
653 		} else if (btf_is_union(type) && nr_unions) {
654 			nr   = &nr_unions;
655 			root = &obj->unions;
656 		} else if (btf_is_typedef(type) && nr_typedefs) {
657 			nr   = &nr_typedefs;
658 			root = &obj->typedefs;
659 		} else
660 			continue;
661 
662 		str = btf__name_by_offset(btf, type->name_off);
663 		if (!str) {
664 			pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
665 				type_id);
666 			goto out;
667 		}
668 
669 		id = btf_id__find(root, str);
670 		if (id) {
671 			if (id->id) {
672 				pr_info("WARN: multiple IDs found for '%s': %d, %d - using %d\n",
673 					str, id->id, type_id, id->id);
674 				warnings++;
675 			} else {
676 				id->id = type_id;
677 				(*nr)--;
678 			}
679 		}
680 	}
681 
682 	err = 0;
683 out:
684 	return err;
685 }
686 
id_patch(struct object * obj,struct btf_id * id)687 static int id_patch(struct object *obj, struct btf_id *id)
688 {
689 	Elf_Data *data = obj->efile.idlist;
690 	int *ptr = data->d_buf;
691 	int i;
692 
693 	/* For set, set8, id->id may be 0 */
694 	if (!id->id && id->kind != BTF_ID_KIND_SET && id->kind != BTF_ID_KIND_SET8) {
695 		pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
696 		warnings++;
697 	}
698 
699 	for (i = 0; i < id->addr_cnt; i++) {
700 		unsigned long addr = id->addr[i];
701 		unsigned long idx = addr - obj->efile.idlist_addr;
702 
703 		pr_debug("patching addr %5lu: ID %7d [%s]\n",
704 			 idx, id->id, id->name);
705 
706 		if (idx >= data->d_size) {
707 			pr_err("FAILED patching index %lu out of bounds %lu\n",
708 				idx, data->d_size);
709 			return -1;
710 		}
711 
712 		idx = idx / sizeof(int);
713 		ptr[idx] = id->id;
714 	}
715 
716 	return 0;
717 }
718 
__symbols_patch(struct object * obj,struct rb_root * root)719 static int __symbols_patch(struct object *obj, struct rb_root *root)
720 {
721 	struct rb_node *next;
722 	struct btf_id *id;
723 
724 	next = rb_first(root);
725 	while (next) {
726 		id = rb_entry(next, struct btf_id, rb_node);
727 
728 		if (id_patch(obj, id))
729 			return -1;
730 
731 		next = rb_next(next);
732 	}
733 	return 0;
734 }
735 
cmp_id(const void * pa,const void * pb)736 static int cmp_id(const void *pa, const void *pb)
737 {
738 	const int *a = pa, *b = pb;
739 
740 	return *a - *b;
741 }
742 
sets_patch(struct object * obj)743 static int sets_patch(struct object *obj)
744 {
745 	Elf_Data *data = obj->efile.idlist;
746 	struct rb_node *next;
747 	int cnt;
748 
749 	next = rb_first(&obj->sets);
750 	while (next) {
751 		struct btf_id_set8 *set8 = NULL;
752 		struct btf_id_set *set = NULL;
753 		unsigned long addr, off;
754 		struct btf_id *id;
755 
756 		id   = rb_entry(next, struct btf_id, rb_node);
757 		addr = id->addr[0];
758 		off = addr - obj->efile.idlist_addr;
759 
760 		/* sets are unique */
761 		if (id->addr_cnt != 1) {
762 			pr_err("FAILED malformed data for set '%s'\n",
763 				id->name);
764 			return -1;
765 		}
766 
767 		switch (id->kind) {
768 		case BTF_ID_KIND_SET:
769 			set = data->d_buf + off;
770 			cnt = set->cnt;
771 			qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id);
772 			break;
773 		case BTF_ID_KIND_SET8:
774 			set8 = data->d_buf + off;
775 			cnt = set8->cnt;
776 			/*
777 			 * Make sure id is at the beginning of the pairs
778 			 * struct, otherwise the below qsort would not work.
779 			 */
780 			BUILD_BUG_ON((u32 *)set8->pairs != &set8->pairs[0].id);
781 			qsort(set8->pairs, set8->cnt, sizeof(set8->pairs[0]), cmp_id);
782 			break;
783 		default:
784 			pr_err("Unexpected btf_id_kind %d for set '%s'\n", id->kind, id->name);
785 			return -1;
786 		}
787 
788 		pr_debug("sorting  addr %5lu: cnt %6d [%s]\n", off, cnt, id->name);
789 
790 		next = rb_next(next);
791 	}
792 	return 0;
793 }
794 
symbols_patch(struct object * obj)795 static int symbols_patch(struct object *obj)
796 {
797 	if (__symbols_patch(obj, &obj->structs)  ||
798 	    __symbols_patch(obj, &obj->unions)   ||
799 	    __symbols_patch(obj, &obj->typedefs) ||
800 	    __symbols_patch(obj, &obj->funcs)    ||
801 	    __symbols_patch(obj, &obj->sets))
802 		return -1;
803 
804 	if (sets_patch(obj))
805 		return -1;
806 
807 	return 0;
808 }
809 
dump_raw_data(const char * out_path,const void * data,u32 size)810 static int dump_raw_data(const char *out_path, const void *data, u32 size)
811 {
812 	size_t written;
813 	FILE *file;
814 
815 	file = fopen(out_path, "wb");
816 	if (!file) {
817 		pr_err("Couldn't open %s for writing\n", out_path);
818 		return -1;
819 	}
820 
821 	written = fwrite(data, 1, size, file);
822 	if (written != size) {
823 		pr_err("Failed to write data to %s\n", out_path);
824 		fclose(file);
825 		unlink(out_path);
826 		return -1;
827 	}
828 
829 	fclose(file);
830 	pr_debug("Dumped %lu bytes of data to %s\n", size, out_path);
831 
832 	return 0;
833 }
834 
dump_raw_btf_ids(struct object * obj,const char * out_path)835 static int dump_raw_btf_ids(struct object *obj, const char *out_path)
836 {
837 	Elf_Data *data = obj->efile.idlist;
838 	int err;
839 
840 	if (!data || !data->d_buf) {
841 		pr_debug("%s has no BTF_ids data to dump\n", obj->path);
842 		return 0;
843 	}
844 
845 	/*
846 	 * If target endianness differs from host, we need to bswap32 the
847 	 * .BTF_ids section data before dumping so that the output is in
848 	 * target endianness.
849 	 */
850 	if (obj->efile.encoding != ELFDATANATIVE) {
851 		pr_debug("bswap_32 .BTF_ids data from host to target endianness\n");
852 		bswap_32_data(data->d_buf, data->d_size);
853 	}
854 
855 	err = dump_raw_data(out_path, data->d_buf, data->d_size);
856 	if (err)
857 		return -1;
858 
859 	return 0;
860 }
861 
dump_raw_btf(struct btf * btf,const char * out_path)862 static int dump_raw_btf(struct btf *btf, const char *out_path)
863 {
864 	const void *raw_btf_data;
865 	u32 raw_btf_size;
866 	int err;
867 
868 	raw_btf_data = btf__raw_data(btf, &raw_btf_size);
869 	if (!raw_btf_data) {
870 		pr_err("btf__raw_data() failed\n");
871 		return -1;
872 	}
873 
874 	err = dump_raw_data(out_path, raw_btf_data, raw_btf_size);
875 	if (err)
876 		return -1;
877 
878 	return 0;
879 }
880 
btf_type_skip_qualifiers(const struct btf * btf,s32 type_id)881 static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, s32 type_id)
882 {
883 	const struct btf_type *t = btf__type_by_id(btf, type_id);
884 
885 	while (btf_is_mod(t))
886 		t = btf__type_by_id(btf, t->type);
887 
888 	return t;
889 }
890 
push_decl_tag_id(struct btf2btf_context * ctx,u32 decl_tag_id)891 static int push_decl_tag_id(struct btf2btf_context *ctx, u32 decl_tag_id)
892 {
893 	u32 *arr = ctx->decl_tags;
894 	u32 cap = ctx->max_decl_tags;
895 
896 	if (ctx->nr_decl_tags + 1 > cap) {
897 		cap = max(cap + 256, cap * 2);
898 		arr = realloc(arr, sizeof(u32) * cap);
899 		if (!arr)
900 			return -ENOMEM;
901 		ctx->max_decl_tags = cap;
902 		ctx->decl_tags = arr;
903 	}
904 
905 	ctx->decl_tags[ctx->nr_decl_tags++] = decl_tag_id;
906 
907 	return 0;
908 }
909 
push_kfunc(struct btf2btf_context * ctx,struct kfunc * kfunc)910 static int push_kfunc(struct btf2btf_context *ctx, struct kfunc *kfunc)
911 {
912 	struct kfunc *arr = ctx->kfuncs;
913 	u32 cap = ctx->max_kfuncs;
914 
915 	if (ctx->nr_kfuncs + 1 > cap) {
916 		cap = max(cap + 256, cap * 2);
917 		arr = realloc(arr, sizeof(struct kfunc) * cap);
918 		if (!arr)
919 			return -ENOMEM;
920 		ctx->max_kfuncs = cap;
921 		ctx->kfuncs = arr;
922 	}
923 
924 	ctx->kfuncs[ctx->nr_kfuncs++] = *kfunc;
925 
926 	return 0;
927 }
928 
collect_decl_tags(struct btf2btf_context * ctx)929 static int collect_decl_tags(struct btf2btf_context *ctx)
930 {
931 	const u32 type_cnt = btf__type_cnt(ctx->btf);
932 	struct btf *btf = ctx->btf;
933 	const struct btf_type *t;
934 	int err;
935 
936 	for (u32 id = 1; id < type_cnt; id++) {
937 		t = btf__type_by_id(btf, id);
938 		if (!btf_is_decl_tag(t))
939 			continue;
940 		err = push_decl_tag_id(ctx, id);
941 		if (err)
942 			return err;
943 	}
944 
945 	return 0;
946 }
947 
948 /*
949  * To find the kfunc flags having its struct btf_id (with ELF addresses)
950  * we need to find the address that is in range of a set8.
951  * If a set8 is found, then the flags are located at addr + 4 bytes.
952  * Return 0 (no flags!) if not found.
953  */
find_kfunc_flags(struct object * obj,struct btf_id * kfunc_id)954 static u32 find_kfunc_flags(struct object *obj, struct btf_id *kfunc_id)
955 {
956 	const u32 *elf_data_ptr = obj->efile.idlist->d_buf;
957 	u64 set_lower_addr, set_upper_addr, addr;
958 	struct btf_id *set_id;
959 	struct rb_node *next;
960 	u32 flags;
961 	u64 idx;
962 
963 	for (next = rb_first(&obj->sets); next; next = rb_next(next)) {
964 		set_id = rb_entry(next, struct btf_id, rb_node);
965 		if (set_id->kind != BTF_ID_KIND_SET8 || set_id->addr_cnt != 1)
966 			continue;
967 
968 		set_lower_addr = set_id->addr[0];
969 		set_upper_addr = set_lower_addr + set_id->cnt * sizeof(u64);
970 
971 		for (u32 i = 0; i < kfunc_id->addr_cnt; i++) {
972 			addr = kfunc_id->addr[i];
973 			/*
974 			 * Lower bound is exclusive to skip the 8-byte header of the set.
975 			 * Upper bound is inclusive to capture the last entry at offset 8*cnt.
976 			 */
977 			if (set_lower_addr < addr && addr <= set_upper_addr) {
978 				pr_debug("found kfunc %s in BTF_ID_FLAGS %s\n",
979 					 kfunc_id->name, set_id->name);
980 				idx = addr - obj->efile.idlist_addr;
981 				idx = idx / sizeof(u32) + 1;
982 				flags = elf_data_ptr[idx];
983 
984 				return flags;
985 			}
986 		}
987 	}
988 
989 	return 0;
990 }
991 
collect_kfuncs(struct object * obj,struct btf2btf_context * ctx)992 static int collect_kfuncs(struct object *obj, struct btf2btf_context *ctx)
993 {
994 	const char *tag_name, *func_name;
995 	struct btf *btf = ctx->btf;
996 	const struct btf_type *t;
997 	u32 flags, func_id;
998 	struct kfunc kfunc;
999 	struct btf_id *id;
1000 	int err;
1001 
1002 	if (ctx->nr_decl_tags == 0)
1003 		return 0;
1004 
1005 	for (u32 i = 0; i < ctx->nr_decl_tags; i++) {
1006 		t = btf__type_by_id(btf, ctx->decl_tags[i]);
1007 		if (btf_kflag(t) || btf_decl_tag(t)->component_idx != -1)
1008 			continue;
1009 
1010 		tag_name = btf__name_by_offset(btf, t->name_off);
1011 		if (strcmp(tag_name, "bpf_kfunc") != 0)
1012 			continue;
1013 
1014 		func_id = t->type;
1015 		t = btf__type_by_id(btf, func_id);
1016 		if (!btf_is_func(t))
1017 			continue;
1018 
1019 		func_name = btf__name_by_offset(btf, t->name_off);
1020 		if (!func_name)
1021 			continue;
1022 
1023 		id = btf_id__find(&obj->funcs, func_name);
1024 		if (!id || id->kind != BTF_ID_KIND_SYM)
1025 			continue;
1026 
1027 		flags = find_kfunc_flags(obj, id);
1028 
1029 		kfunc.name = id->name;
1030 		kfunc.btf_id = func_id;
1031 		kfunc.flags = flags;
1032 
1033 		err = push_kfunc(ctx, &kfunc);
1034 		if (err)
1035 			return err;
1036 	}
1037 
1038 	return 0;
1039 }
1040 
build_btf2btf_context(struct object * obj,struct btf2btf_context * ctx)1041 static int build_btf2btf_context(struct object *obj, struct btf2btf_context *ctx)
1042 {
1043 	int err;
1044 
1045 	ctx->btf = obj->btf;
1046 
1047 	err = collect_decl_tags(ctx);
1048 	if (err) {
1049 		pr_err("ERROR: resolve_btfids: failed to collect decl tags from BTF\n");
1050 		return err;
1051 	}
1052 
1053 	err = collect_kfuncs(obj, ctx);
1054 	if (err) {
1055 		pr_err("ERROR: resolve_btfids: failed to collect kfuncs from BTF\n");
1056 		return err;
1057 	}
1058 
1059 	return 0;
1060 }
1061 
1062 
1063 /* Implicit BPF kfunc arguments can only be of particular types */
is_kf_implicit_arg(const struct btf * btf,const struct btf_param * p)1064 static bool is_kf_implicit_arg(const struct btf *btf, const struct btf_param *p)
1065 {
1066 	static const char *const kf_implicit_arg_types[] = {
1067 		"bpf_prog_aux",
1068 	};
1069 	const struct btf_type *t;
1070 	const char *name;
1071 
1072 	t = btf_type_skip_qualifiers(btf, p->type);
1073 	if (!btf_is_ptr(t))
1074 		return false;
1075 
1076 	t = btf_type_skip_qualifiers(btf, t->type);
1077 	if (!btf_is_struct(t))
1078 		return false;
1079 
1080 	name = btf__name_by_offset(btf, t->name_off);
1081 	if (!name)
1082 		return false;
1083 
1084 	for (int i = 0; i < ARRAY_SIZE(kf_implicit_arg_types); i++)
1085 		if (strcmp(name, kf_implicit_arg_types[i]) == 0)
1086 			return true;
1087 
1088 	return false;
1089 }
1090 
1091 /*
1092  * For a kfunc with KF_IMPLICIT_ARGS we do the following:
1093  *   1. Add a new function with _impl suffix in the name, with the prototype
1094  *      of the original kfunc.
1095  *   2. Add all decl tags except "bpf_kfunc" for the _impl func.
1096  *   3. Add a new function prototype with modified list of arguments:
1097  *      omitting implicit args.
1098  *   4. Change the prototype of the original kfunc to the new one.
1099  *
1100  * This way we transform the BTF associated with the kfunc from
1101  *	__bpf_kfunc bpf_foo(int arg1, void *implicit_arg);
1102  * into
1103  *	bpf_foo_impl(int arg1, void *implicit_arg);
1104  *	__bpf_kfunc bpf_foo(int arg1);
1105  *
1106  * If a kfunc with KF_IMPLICIT_ARGS already has an _impl counterpart
1107  * in BTF, then it's a legacy case: an _impl function is declared in the
1108  * source code. In this case, we can skip adding an _impl function, but we
1109  * still have to add a func prototype that omits implicit args.
1110  */
process_kfunc_with_implicit_args(struct btf2btf_context * ctx,struct kfunc * kfunc)1111 static int process_kfunc_with_implicit_args(struct btf2btf_context *ctx, struct kfunc *kfunc)
1112 {
1113 	s32 idx, new_proto_id, new_func_id, proto_id;
1114 	const char *param_name, *tag_name;
1115 	const struct btf_param *params;
1116 	enum btf_func_linkage linkage;
1117 	char tmp_name[KSYM_NAME_LEN];
1118 	struct btf *btf = ctx->btf;
1119 	int err, len, nr_params;
1120 	struct btf_type *t;
1121 
1122 	t = (struct btf_type *)btf__type_by_id(btf, kfunc->btf_id);
1123 	if (!t || !btf_is_func(t)) {
1124 		pr_err("ERROR: resolve_btfids: btf id %d is not a function\n", kfunc->btf_id);
1125 		return -EINVAL;
1126 	}
1127 
1128 	linkage = btf_vlen(t);
1129 
1130 	proto_id = t->type;
1131 	t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1132 	if (!t || !btf_is_func_proto(t)) {
1133 		pr_err("ERROR: resolve_btfids: btf id %d is not a function prototype\n", proto_id);
1134 		return -EINVAL;
1135 	}
1136 
1137 	len = snprintf(tmp_name, sizeof(tmp_name), "%s%s", kfunc->name, KF_IMPL_SUFFIX);
1138 	if (len < 0 || len >= sizeof(tmp_name)) {
1139 		pr_err("ERROR: function name is too long: %s%s\n", kfunc->name, KF_IMPL_SUFFIX);
1140 		return -E2BIG;
1141 	}
1142 
1143 	if (btf__find_by_name_kind(btf, tmp_name, BTF_KIND_FUNC) > 0) {
1144 		pr_debug("resolve_btfids: function %s already exists in BTF\n", tmp_name);
1145 		goto add_new_proto;
1146 	}
1147 
1148 	/* Add a new function with _impl suffix and original prototype */
1149 	new_func_id = btf__add_func(btf, tmp_name, linkage, proto_id);
1150 	if (new_func_id < 0) {
1151 		pr_err("ERROR: resolve_btfids: failed to add func %s to BTF\n", tmp_name);
1152 		return new_func_id;
1153 	}
1154 
1155 	/* Copy all decl tags except "bpf_kfunc" from the original kfunc to the new one */
1156 	for (int i = 0; i < ctx->nr_decl_tags; i++) {
1157 		t = (struct btf_type *)btf__type_by_id(btf, ctx->decl_tags[i]);
1158 		if (t->type != kfunc->btf_id)
1159 			continue;
1160 
1161 		tag_name = btf__name_by_offset(btf, t->name_off);
1162 		if (strcmp(tag_name, "bpf_kfunc") == 0)
1163 			continue;
1164 
1165 		idx = btf_decl_tag(t)->component_idx;
1166 
1167 		if (btf_kflag(t))
1168 			err = btf__add_decl_attr(btf, tag_name, new_func_id, idx);
1169 		else
1170 			err = btf__add_decl_tag(btf, tag_name, new_func_id, idx);
1171 
1172 		if (err < 0) {
1173 			pr_err("ERROR: resolve_btfids: failed to add decl tag %s for %s\n",
1174 			       tag_name, tmp_name);
1175 			return -EINVAL;
1176 		}
1177 	}
1178 
1179 add_new_proto:
1180 	t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1181 	new_proto_id = btf__add_func_proto(btf, t->type);
1182 	if (new_proto_id < 0) {
1183 		pr_err("ERROR: resolve_btfids: failed to add func proto for %s\n", kfunc->name);
1184 		return new_proto_id;
1185 	}
1186 
1187 	/* Add non-implicit args to the new prototype */
1188 	t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1189 	nr_params = btf_vlen(t);
1190 	for (int i = 0; i < nr_params; i++) {
1191 		params = btf_params(t);
1192 		if (is_kf_implicit_arg(btf, &params[i]))
1193 			break;
1194 		param_name = btf__name_by_offset(btf, params[i].name_off);
1195 		err = btf__add_func_param(btf, param_name, params[i].type);
1196 		if (err < 0) {
1197 			pr_err("ERROR: resolve_btfids: failed to add param %s for %s\n",
1198 			       param_name, kfunc->name);
1199 			return err;
1200 		}
1201 		t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1202 	}
1203 
1204 	/* Finally change the prototype of the original kfunc to the new one */
1205 	t = (struct btf_type *)btf__type_by_id(btf, kfunc->btf_id);
1206 	t->type = new_proto_id;
1207 
1208 	pr_debug("resolve_btfids: updated BTF for kfunc with implicit args %s\n", kfunc->name);
1209 
1210 	return 0;
1211 }
1212 
btf2btf(struct object * obj)1213 static int btf2btf(struct object *obj)
1214 {
1215 	struct btf2btf_context ctx = {};
1216 	int err;
1217 
1218 	err = build_btf2btf_context(obj, &ctx);
1219 	if (err)
1220 		goto out;
1221 
1222 	for (u32 i = 0; i < ctx.nr_kfuncs; i++) {
1223 		struct kfunc *kfunc = &ctx.kfuncs[i];
1224 
1225 		if (!(kfunc->flags & KF_IMPLICIT_ARGS))
1226 			continue;
1227 
1228 		err = process_kfunc_with_implicit_args(&ctx, kfunc);
1229 		if (err)
1230 			goto out;
1231 	}
1232 
1233 	err = 0;
1234 out:
1235 	free(ctx.decl_tags);
1236 	free(ctx.kfuncs);
1237 
1238 	return err;
1239 }
1240 
1241 /*
1242  * Sort types by name in ascending order resulting in all
1243  * anonymous types being placed before named types.
1244  */
cmp_type_names(const void * a,const void * b,void * priv)1245 static int cmp_type_names(const void *a, const void *b, void *priv)
1246 {
1247 	struct btf *btf = (struct btf *)priv;
1248 	const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
1249 	const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
1250 	const char *na, *nb;
1251 	int r;
1252 
1253 	na = btf__str_by_offset(btf, ta->name_off);
1254 	nb = btf__str_by_offset(btf, tb->name_off);
1255 	r = strcmp(na, nb);
1256 	if (r != 0)
1257 		return r;
1258 
1259 	/* preserve original relative order of anonymous or same-named types */
1260 	return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
1261 }
1262 
sort_btf_by_name(struct btf * btf)1263 static int sort_btf_by_name(struct btf *btf)
1264 {
1265 	__u32 *permute_ids = NULL, *id_map = NULL;
1266 	int nr_types, i, err = 0;
1267 	__u32 start_id = 0, id;
1268 
1269 	if (btf__base_btf(btf))
1270 		start_id = btf__type_cnt(btf__base_btf(btf));
1271 	nr_types = btf__type_cnt(btf) - start_id;
1272 
1273 	permute_ids = calloc(nr_types, sizeof(*permute_ids));
1274 	if (!permute_ids) {
1275 		err = -ENOMEM;
1276 		goto out;
1277 	}
1278 
1279 	id_map = calloc(nr_types, sizeof(*id_map));
1280 	if (!id_map) {
1281 		err = -ENOMEM;
1282 		goto out;
1283 	}
1284 
1285 	for (i = 0, id = start_id; i < nr_types; i++, id++)
1286 		permute_ids[i] = id;
1287 
1288 	qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
1289 		btf);
1290 
1291 	for (i = 0; i < nr_types; i++) {
1292 		id = permute_ids[i] - start_id;
1293 		id_map[id] = i + start_id;
1294 	}
1295 
1296 	err = btf__permute(btf, id_map, nr_types, NULL);
1297 	if (err)
1298 		pr_err("FAILED: btf permute: %s\n", strerror(-err));
1299 
1300 out:
1301 	free(permute_ids);
1302 	free(id_map);
1303 	return err;
1304 }
1305 
finalize_btf(struct object * obj)1306 static int finalize_btf(struct object *obj)
1307 {
1308 	struct btf *base_btf = obj->base_btf, *btf = obj->btf;
1309 	int err;
1310 
1311 	if (obj->base_btf && obj->distill_base) {
1312 		err = btf__distill_base(obj->btf, &base_btf, &btf);
1313 		if (err) {
1314 			pr_err("FAILED to distill base BTF: %s\n", strerror(errno));
1315 			goto out_err;
1316 		}
1317 
1318 		btf__free(obj->base_btf);
1319 		btf__free(obj->btf);
1320 		obj->base_btf = base_btf;
1321 		obj->btf = btf;
1322 	}
1323 
1324 	err = sort_btf_by_name(obj->btf);
1325 	if (err) {
1326 		pr_err("FAILED to sort BTF: %s\n", strerror(errno));
1327 		goto out_err;
1328 	}
1329 
1330 	return 0;
1331 
1332 out_err:
1333 	btf__free(base_btf);
1334 	btf__free(btf);
1335 	obj->base_btf = NULL;
1336 	obj->btf = NULL;
1337 
1338 	return err;
1339 }
1340 
make_out_path(char * buf,u32 buf_sz,const char * in_path,const char * suffix)1341 static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, const char *suffix)
1342 {
1343 	int len = snprintf(buf, buf_sz, "%s%s", in_path, suffix);
1344 
1345 	if (len < 0 || len >= buf_sz) {
1346 		pr_err("Output path is too long: %s%s\n", in_path, suffix);
1347 		return -E2BIG;
1348 	}
1349 
1350 	return 0;
1351 }
1352 
1353 /*
1354  * Patch the .BTF_ids section of an ELF file with data from provided file.
1355  * Equivalent to: objcopy --update-section .BTF_ids=<btfids> <elf>
1356  *
1357  * 1. Find .BTF_ids section in the ELF
1358  * 2. Verify that blob file size matches section size
1359  * 3. Update section data buffer with blob data
1360  * 4. Write the ELF file
1361  */
patch_btfids(const char * btfids_path,const char * elf_path)1362 static int patch_btfids(const char *btfids_path, const char *elf_path)
1363 {
1364 	Elf_Scn *scn = NULL;
1365 	FILE *btfids_file;
1366 	size_t shdrstrndx;
1367 	int fd, err = -1;
1368 	Elf_Data *data;
1369 	struct stat st;
1370 	GElf_Shdr sh;
1371 	char *name;
1372 	Elf *elf;
1373 
1374 	elf_version(EV_CURRENT);
1375 
1376 	fd = open(elf_path, O_RDWR, 0666);
1377 	if (fd < 0) {
1378 		pr_err("FAILED to open %s: %s\n", elf_path, strerror(errno));
1379 		return -1;
1380 	}
1381 
1382 	elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
1383 	if (!elf) {
1384 		close(fd);
1385 		pr_err("FAILED cannot create ELF descriptor: %s\n", elf_errmsg(-1));
1386 		return -1;
1387 	}
1388 
1389 	elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
1390 
1391 	if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
1392 		pr_err("FAILED cannot get shdr str ndx\n");
1393 		goto out;
1394 	}
1395 
1396 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
1397 
1398 		if (gelf_getshdr(scn, &sh) != &sh) {
1399 			pr_err("FAILED to get section header\n");
1400 			goto out;
1401 		}
1402 
1403 		name = elf_strptr(elf, shdrstrndx, sh.sh_name);
1404 		if (!name)
1405 			continue;
1406 
1407 		if (strcmp(name, BTF_IDS_SECTION) == 0)
1408 			break;
1409 	}
1410 
1411 	if (!scn) {
1412 		pr_err("FAILED: section %s not found in %s\n", BTF_IDS_SECTION, elf_path);
1413 		goto out;
1414 	}
1415 
1416 	data = elf_getdata(scn, NULL);
1417 	if (!data) {
1418 		pr_err("FAILED to get %s section data from %s\n", BTF_IDS_SECTION, elf_path);
1419 		goto out;
1420 	}
1421 
1422 	if (stat(btfids_path, &st) < 0) {
1423 		pr_err("FAILED to stat %s: %s\n", btfids_path, strerror(errno));
1424 		goto out;
1425 	}
1426 
1427 	if ((size_t)st.st_size != data->d_size) {
1428 		pr_err("FAILED: size mismatch - %s section in %s is %zu bytes, %s is %zu bytes\n",
1429 		       BTF_IDS_SECTION, elf_path, data->d_size, btfids_path, (size_t)st.st_size);
1430 		goto out;
1431 	}
1432 
1433 	btfids_file = fopen(btfids_path, "rb");
1434 	if (!btfids_file) {
1435 		pr_err("FAILED to open %s: %s\n", btfids_path, strerror(errno));
1436 		goto out;
1437 	}
1438 
1439 	pr_debug("Copying data from %s to %s section of %s (%zu bytes)\n",
1440 		 btfids_path, BTF_IDS_SECTION, elf_path, data->d_size);
1441 
1442 	if (fread(data->d_buf, data->d_size, 1, btfids_file) != 1) {
1443 		pr_err("FAILED to read %s\n", btfids_path);
1444 		fclose(btfids_file);
1445 		goto out;
1446 	}
1447 	fclose(btfids_file);
1448 
1449 	elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1450 	if (elf_update(elf, ELF_C_WRITE) < 0) {
1451 		pr_err("FAILED to update ELF file %s\n", elf_path);
1452 		goto out;
1453 	}
1454 
1455 	err = 0;
1456 out:
1457 	elf_end(elf);
1458 	close(fd);
1459 
1460 	return err;
1461 }
1462 
1463 static const char * const resolve_btfids_usage[] = {
1464 	"resolve_btfids [<options>] <ELF object>",
1465 	"resolve_btfids --patch_btfids <.BTF_ids file> <ELF object>",
1466 	NULL
1467 };
1468 
main(int argc,const char ** argv)1469 int main(int argc, const char **argv)
1470 {
1471 	struct object obj = {
1472 		.efile = {
1473 			.idlist_shndx  = -1,
1474 			.symbols_shndx = -1,
1475 		},
1476 		.structs  = RB_ROOT,
1477 		.unions   = RB_ROOT,
1478 		.typedefs = RB_ROOT,
1479 		.funcs    = RB_ROOT,
1480 		.sets     = RB_ROOT,
1481 	};
1482 	const char *btfids_path = NULL;
1483 	bool fatal_warnings = false;
1484 	bool resolve_btfids = true;
1485 	char out_path[PATH_MAX];
1486 
1487 	struct option btfid_options[] = {
1488 		OPT_INCR('v', "verbose", &verbose,
1489 			 "be more verbose (show errors, etc)"),
1490 		OPT_STRING(0, "btf", &obj.btf_path, "file",
1491 			   "path to a file with input BTF data"),
1492 		OPT_STRING('b', "btf_base", &obj.base_btf_path, "file",
1493 			   "path of file providing base BTF"),
1494 		OPT_BOOLEAN(0, "fatal_warnings", &fatal_warnings,
1495 			    "turn warnings into errors"),
1496 		OPT_BOOLEAN(0, "distill_base", &obj.distill_base,
1497 			    "distill --btf_base and emit .BTF.base section data"),
1498 		OPT_STRING(0, "patch_btfids", &btfids_path, "file",
1499 			   "path to .BTF_ids section data blob to patch into ELF file"),
1500 		OPT_END()
1501 	};
1502 	int err = -1;
1503 
1504 	argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
1505 			     PARSE_OPT_STOP_AT_NON_OPTION);
1506 	if (argc != 1)
1507 		usage_with_options(resolve_btfids_usage, btfid_options);
1508 
1509 	obj.path = argv[0];
1510 
1511 	if (btfids_path)
1512 		return patch_btfids(btfids_path, obj.path);
1513 
1514 	if (elf_collect(&obj))
1515 		goto out;
1516 
1517 	/*
1518 	 * We did not find .BTF_ids section or symbols section,
1519 	 * nothing to do..
1520 	 */
1521 	if (obj.efile.idlist_shndx == -1 ||
1522 	    obj.efile.symbols_shndx == -1) {
1523 		pr_debug("Cannot find .BTF_ids or symbols sections, skip symbols resolution\n");
1524 		resolve_btfids = false;
1525 	}
1526 
1527 	if (resolve_btfids)
1528 		if (symbols_collect(&obj))
1529 			goto out;
1530 
1531 	if (load_btf(&obj))
1532 		goto out;
1533 
1534 	if (btf2btf(&obj))
1535 		goto out;
1536 
1537 	if (finalize_btf(&obj))
1538 		goto out;
1539 
1540 	if (!resolve_btfids)
1541 		goto dump_btf;
1542 
1543 	if (symbols_resolve(&obj))
1544 		goto out;
1545 
1546 	if (symbols_patch(&obj))
1547 		goto out;
1548 
1549 	err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_IDS_SECTION);
1550 	err = err ?: dump_raw_btf_ids(&obj, out_path);
1551 	if (err)
1552 		goto out;
1553 
1554 dump_btf:
1555 	err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_ELF_SEC);
1556 	err = err ?: dump_raw_btf(obj.btf, out_path);
1557 	if (err)
1558 		goto out;
1559 
1560 	if (obj.base_btf && obj.distill_base) {
1561 		err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_BASE_ELF_SEC);
1562 		err = err ?: dump_raw_btf(obj.base_btf, out_path);
1563 		if (err)
1564 			goto out;
1565 	}
1566 
1567 	if (!(fatal_warnings && warnings))
1568 		err = 0;
1569 out:
1570 	btf__free(obj.base_btf);
1571 	btf__free(obj.btf);
1572 	btf_id__free_all(&obj.structs);
1573 	btf_id__free_all(&obj.unions);
1574 	btf_id__free_all(&obj.typedefs);
1575 	btf_id__free_all(&obj.funcs);
1576 	btf_id__free_all(&obj.sets);
1577 	if (obj.efile.elf) {
1578 		elf_end(obj.efile.elf);
1579 		close(obj.efile.fd);
1580 	}
1581 	return err;
1582 }
1583