Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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