1fbbb68deSJiri Olsa // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2fbbb68deSJiri Olsa
3fbbb68deSJiri Olsa /*
470e79866SAlexey Dobriyan * resolve_btfids scans ELF object for .BTF_ids section and resolves
5fbbb68deSJiri Olsa * its symbols with BTF ID values.
6fbbb68deSJiri Olsa *
7fbbb68deSJiri Olsa * Each symbol points to 4 bytes data and is expected to have
8fbbb68deSJiri Olsa * following name syntax:
9fbbb68deSJiri Olsa *
10fbbb68deSJiri Olsa * __BTF_ID__<type>__<symbol>[__<id>]
11fbbb68deSJiri Olsa *
12fbbb68deSJiri Olsa * type is:
13fbbb68deSJiri Olsa *
14fbbb68deSJiri Olsa * func - lookup BTF_KIND_FUNC symbol with <symbol> name
15fbbb68deSJiri Olsa * and store its ID into the data:
16fbbb68deSJiri Olsa *
17fbbb68deSJiri Olsa * __BTF_ID__func__vfs_close__1:
18fbbb68deSJiri Olsa * .zero 4
19fbbb68deSJiri Olsa *
20fbbb68deSJiri Olsa * struct - lookup BTF_KIND_STRUCT symbol with <symbol> name
21fbbb68deSJiri Olsa * and store its ID into the data:
22fbbb68deSJiri Olsa *
23fbbb68deSJiri Olsa * __BTF_ID__struct__sk_buff__1:
24fbbb68deSJiri Olsa * .zero 4
25fbbb68deSJiri Olsa *
26fbbb68deSJiri Olsa * union - lookup BTF_KIND_UNION symbol with <symbol> name
27fbbb68deSJiri Olsa * and store its ID into the data:
28fbbb68deSJiri Olsa *
29fbbb68deSJiri Olsa * __BTF_ID__union__thread_union__1:
30fbbb68deSJiri Olsa * .zero 4
31fbbb68deSJiri Olsa *
32fbbb68deSJiri Olsa * typedef - lookup BTF_KIND_TYPEDEF symbol with <symbol> name
33fbbb68deSJiri Olsa * and store its ID into the data:
34fbbb68deSJiri Olsa *
35fbbb68deSJiri Olsa * __BTF_ID__typedef__pid_t__1:
36fbbb68deSJiri Olsa * .zero 4
37fbbb68deSJiri Olsa *
38fbbb68deSJiri Olsa * set - store symbol size into first 4 bytes and sort following
39fbbb68deSJiri Olsa * ID list
40fbbb68deSJiri Olsa *
41fbbb68deSJiri Olsa * __BTF_ID__set__list:
42fbbb68deSJiri Olsa * .zero 4
43fbbb68deSJiri Olsa * list:
44fbbb68deSJiri Olsa * __BTF_ID__func__vfs_getattr__3:
45fbbb68deSJiri Olsa * .zero 4
46fbbb68deSJiri Olsa * __BTF_ID__func__vfs_fallocate__4:
47fbbb68deSJiri Olsa * .zero 4
48ef2c6f37SKumar Kartikeya Dwivedi *
49ef2c6f37SKumar Kartikeya Dwivedi * set8 - store symbol size into first 4 bytes and sort following
50ef2c6f37SKumar Kartikeya Dwivedi * ID list
51ef2c6f37SKumar Kartikeya Dwivedi *
52ef2c6f37SKumar Kartikeya Dwivedi * __BTF_ID__set8__list:
53ef2c6f37SKumar Kartikeya Dwivedi * .zero 8
54ef2c6f37SKumar Kartikeya Dwivedi * list:
55ef2c6f37SKumar Kartikeya Dwivedi * __BTF_ID__func__vfs_getattr__3:
56ef2c6f37SKumar Kartikeya Dwivedi * .zero 4
57ef2c6f37SKumar Kartikeya Dwivedi * .word (1 << 0) | (1 << 2)
58ef2c6f37SKumar Kartikeya Dwivedi * __BTF_ID__func__vfs_fallocate__5:
59ef2c6f37SKumar Kartikeya Dwivedi * .zero 4
60ef2c6f37SKumar Kartikeya Dwivedi * .word (1 << 3) | (1 << 1) | (1 << 2)
61fbbb68deSJiri Olsa */
62fbbb68deSJiri Olsa
63fbbb68deSJiri Olsa #define _GNU_SOURCE
64fbbb68deSJiri Olsa #include <stdio.h>
65fbbb68deSJiri Olsa #include <string.h>
66fbbb68deSJiri Olsa #include <unistd.h>
67fbbb68deSJiri Olsa #include <stdlib.h>
68fbbb68deSJiri Olsa #include <libelf.h>
69fbbb68deSJiri Olsa #include <gelf.h>
70fbbb68deSJiri Olsa #include <sys/stat.h>
71fbbb68deSJiri Olsa #include <fcntl.h>
72fbbb68deSJiri Olsa #include <errno.h>
739707ac4fSViktor Malik #include <linux/btf_ids.h>
74fbbb68deSJiri Olsa #include <linux/rbtree.h>
75fbbb68deSJiri Olsa #include <linux/zalloc.h>
76fbbb68deSJiri Olsa #include <linux/err.h>
771478994aSQuentin Monnet #include <bpf/btf.h>
781478994aSQuentin Monnet #include <bpf/libbpf.h>
79af03299dSIan Rogers #include <subcmd/parse-options.h>
80fbbb68deSJiri Olsa
81fbbb68deSJiri Olsa #define BTF_IDS_SECTION ".BTF_ids"
829707ac4fSViktor Malik #define BTF_ID_PREFIX "__BTF_ID__"
83fbbb68deSJiri Olsa
84fbbb68deSJiri Olsa #define BTF_STRUCT "struct"
85fbbb68deSJiri Olsa #define BTF_UNION "union"
86fbbb68deSJiri Olsa #define BTF_TYPEDEF "typedef"
87fbbb68deSJiri Olsa #define BTF_FUNC "func"
88fbbb68deSJiri Olsa #define BTF_SET "set"
89ef2c6f37SKumar Kartikeya Dwivedi #define BTF_SET8 "set8"
90fbbb68deSJiri Olsa
91fbbb68deSJiri Olsa #define ADDR_CNT 100
92fbbb68deSJiri Olsa
93903fad43SViktor Malik #if __BYTE_ORDER == __LITTLE_ENDIAN
94903fad43SViktor Malik # define ELFDATANATIVE ELFDATA2LSB
95903fad43SViktor Malik #elif __BYTE_ORDER == __BIG_ENDIAN
96903fad43SViktor Malik # define ELFDATANATIVE ELFDATA2MSB
97903fad43SViktor Malik #else
98903fad43SViktor Malik # error "Unknown machine endianness!"
99903fad43SViktor Malik #endif
100903fad43SViktor Malik
101fbbb68deSJiri Olsa struct btf_id {
102fbbb68deSJiri Olsa struct rb_node rb_node;
103fbbb68deSJiri Olsa char *name;
104fbbb68deSJiri Olsa union {
105fbbb68deSJiri Olsa int id;
106fbbb68deSJiri Olsa int cnt;
107fbbb68deSJiri Olsa };
108fbbb68deSJiri Olsa int addr_cnt;
1093345193fSKumar Kartikeya Dwivedi bool is_set;
110ef2c6f37SKumar Kartikeya Dwivedi bool is_set8;
111fbbb68deSJiri Olsa Elf64_Addr addr[ADDR_CNT];
112fbbb68deSJiri Olsa };
113fbbb68deSJiri Olsa
114fbbb68deSJiri Olsa struct object {
115fbbb68deSJiri Olsa const char *path;
116fbbb68deSJiri Olsa const char *btf;
117f614f2c7SKumar Kartikeya Dwivedi const char *base_btf_path;
118fbbb68deSJiri Olsa
119fbbb68deSJiri Olsa struct {
120fbbb68deSJiri Olsa int fd;
121fbbb68deSJiri Olsa Elf *elf;
122fbbb68deSJiri Olsa Elf_Data *symbols;
123fbbb68deSJiri Olsa Elf_Data *idlist;
124fbbb68deSJiri Olsa int symbols_shndx;
125fbbb68deSJiri Olsa int idlist_shndx;
126fbbb68deSJiri Olsa size_t strtabidx;
127fbbb68deSJiri Olsa unsigned long idlist_addr;
128903fad43SViktor Malik int encoding;
129fbbb68deSJiri Olsa } efile;
130fbbb68deSJiri Olsa
131fbbb68deSJiri Olsa struct rb_root sets;
132fbbb68deSJiri Olsa struct rb_root structs;
133fbbb68deSJiri Olsa struct rb_root unions;
134fbbb68deSJiri Olsa struct rb_root typedefs;
135fbbb68deSJiri Olsa struct rb_root funcs;
136fbbb68deSJiri Olsa
137fbbb68deSJiri Olsa int nr_funcs;
138fbbb68deSJiri Olsa int nr_structs;
139fbbb68deSJiri Olsa int nr_unions;
140fbbb68deSJiri Olsa int nr_typedefs;
141fbbb68deSJiri Olsa };
142fbbb68deSJiri Olsa
143fbbb68deSJiri Olsa static int verbose;
144fbbb68deSJiri Olsa
eprintf(int level,int var,const char * fmt,...)145e27bfefbSStanislav Fomichev static int eprintf(int level, int var, const char *fmt, ...)
146fbbb68deSJiri Olsa {
147fbbb68deSJiri Olsa va_list args;
148e27bfefbSStanislav Fomichev int ret = 0;
149fbbb68deSJiri Olsa
150fbbb68deSJiri Olsa if (var >= level) {
151fbbb68deSJiri Olsa va_start(args, fmt);
152fbbb68deSJiri Olsa ret = vfprintf(stderr, fmt, args);
153fbbb68deSJiri Olsa va_end(args);
154fbbb68deSJiri Olsa }
155fbbb68deSJiri Olsa return ret;
156fbbb68deSJiri Olsa }
157fbbb68deSJiri Olsa
158fbbb68deSJiri Olsa #ifndef pr_fmt
159fbbb68deSJiri Olsa #define pr_fmt(fmt) fmt
160fbbb68deSJiri Olsa #endif
161fbbb68deSJiri Olsa
162fbbb68deSJiri Olsa #define pr_debug(fmt, ...) \
163fbbb68deSJiri Olsa eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
164fbbb68deSJiri Olsa #define pr_debugN(n, fmt, ...) \
165fbbb68deSJiri Olsa eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
166fbbb68deSJiri Olsa #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
167fbbb68deSJiri Olsa #define pr_err(fmt, ...) \
168fbbb68deSJiri Olsa eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
16967208692SJiri Olsa #define pr_info(fmt, ...) \
17067208692SJiri Olsa eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
171fbbb68deSJiri Olsa
is_btf_id(const char * name)172fbbb68deSJiri Olsa static bool is_btf_id(const char *name)
173fbbb68deSJiri Olsa {
1749707ac4fSViktor Malik return name && !strncmp(name, BTF_ID_PREFIX, sizeof(BTF_ID_PREFIX) - 1);
175fbbb68deSJiri Olsa }
176fbbb68deSJiri Olsa
btf_id__find(struct rb_root * root,const char * name)177fbbb68deSJiri Olsa static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
178fbbb68deSJiri Olsa {
179fbbb68deSJiri Olsa struct rb_node *p = root->rb_node;
180fbbb68deSJiri Olsa struct btf_id *id;
181fbbb68deSJiri Olsa int cmp;
182fbbb68deSJiri Olsa
183fbbb68deSJiri Olsa while (p) {
184fbbb68deSJiri Olsa id = rb_entry(p, struct btf_id, rb_node);
185fbbb68deSJiri Olsa cmp = strcmp(id->name, name);
186fbbb68deSJiri Olsa if (cmp < 0)
187fbbb68deSJiri Olsa p = p->rb_left;
188fbbb68deSJiri Olsa else if (cmp > 0)
189fbbb68deSJiri Olsa p = p->rb_right;
190fbbb68deSJiri Olsa else
191fbbb68deSJiri Olsa return id;
192fbbb68deSJiri Olsa }
193fbbb68deSJiri Olsa return NULL;
194fbbb68deSJiri Olsa }
195fbbb68deSJiri Olsa
196fbbb68deSJiri Olsa static struct btf_id *
btf_id__add(struct rb_root * root,char * name,bool unique)197fbbb68deSJiri Olsa btf_id__add(struct rb_root *root, char *name, bool unique)
198fbbb68deSJiri Olsa {
199fbbb68deSJiri Olsa struct rb_node **p = &root->rb_node;
200fbbb68deSJiri Olsa struct rb_node *parent = NULL;
201fbbb68deSJiri Olsa struct btf_id *id;
202fbbb68deSJiri Olsa int cmp;
203fbbb68deSJiri Olsa
204fbbb68deSJiri Olsa while (*p != NULL) {
205fbbb68deSJiri Olsa parent = *p;
206fbbb68deSJiri Olsa id = rb_entry(parent, struct btf_id, rb_node);
207fbbb68deSJiri Olsa cmp = strcmp(id->name, name);
208fbbb68deSJiri Olsa if (cmp < 0)
209fbbb68deSJiri Olsa p = &(*p)->rb_left;
210fbbb68deSJiri Olsa else if (cmp > 0)
211fbbb68deSJiri Olsa p = &(*p)->rb_right;
212fbbb68deSJiri Olsa else
213fbbb68deSJiri Olsa return unique ? NULL : id;
214fbbb68deSJiri Olsa }
215fbbb68deSJiri Olsa
216fbbb68deSJiri Olsa id = zalloc(sizeof(*id));
217fbbb68deSJiri Olsa if (id) {
218fbbb68deSJiri Olsa pr_debug("adding symbol %s\n", name);
219fbbb68deSJiri Olsa id->name = name;
220fbbb68deSJiri Olsa rb_link_node(&id->rb_node, parent, p);
221fbbb68deSJiri Olsa rb_insert_color(&id->rb_node, root);
222fbbb68deSJiri Olsa }
223fbbb68deSJiri Olsa return id;
224fbbb68deSJiri Olsa }
225fbbb68deSJiri Olsa
get_id(const char * prefix_end)226fbbb68deSJiri Olsa static char *get_id(const char *prefix_end)
227fbbb68deSJiri Olsa {
228fbbb68deSJiri Olsa /*
229fbbb68deSJiri Olsa * __BTF_ID__func__vfs_truncate__0
230fbbb68deSJiri Olsa * prefix_end = ^
231193a983cSJiri Olsa * pos = ^
232fbbb68deSJiri Olsa */
233193a983cSJiri Olsa int len = strlen(prefix_end);
234193a983cSJiri Olsa int pos = sizeof("__") - 1;
235193a983cSJiri Olsa char *p, *id;
236fbbb68deSJiri Olsa
237193a983cSJiri Olsa if (pos >= len)
238193a983cSJiri Olsa return NULL;
239193a983cSJiri Olsa
240193a983cSJiri Olsa id = strdup(prefix_end + pos);
241fbbb68deSJiri Olsa if (id) {
242fbbb68deSJiri Olsa /*
243fbbb68deSJiri Olsa * __BTF_ID__func__vfs_truncate__0
244fbbb68deSJiri Olsa * id = ^
245fbbb68deSJiri Olsa *
246fbbb68deSJiri Olsa * cut the unique id part
247fbbb68deSJiri Olsa */
248fbbb68deSJiri Olsa p = strrchr(id, '_');
249fbbb68deSJiri Olsa p--;
250fbbb68deSJiri Olsa if (*p != '_') {
251fbbb68deSJiri Olsa free(id);
252fbbb68deSJiri Olsa return NULL;
253fbbb68deSJiri Olsa }
254fbbb68deSJiri Olsa *p = '\0';
255fbbb68deSJiri Olsa }
256fbbb68deSJiri Olsa return id;
257fbbb68deSJiri Olsa }
258fbbb68deSJiri Olsa
add_set(struct object * obj,char * name,bool is_set8)259ef2c6f37SKumar Kartikeya Dwivedi static struct btf_id *add_set(struct object *obj, char *name, bool is_set8)
260a5f53b1dSJiri Olsa {
261a5f53b1dSJiri Olsa /*
262a5f53b1dSJiri Olsa * __BTF_ID__set__name
263a5f53b1dSJiri Olsa * name = ^
264a5f53b1dSJiri Olsa * id = ^
265a5f53b1dSJiri Olsa */
266ef2c6f37SKumar Kartikeya Dwivedi char *id = name + (is_set8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__")) - 1;
267a5f53b1dSJiri Olsa int len = strlen(name);
268a5f53b1dSJiri Olsa
269a5f53b1dSJiri Olsa if (id >= name + len) {
270a5f53b1dSJiri Olsa pr_err("FAILED to parse set name: %s\n", name);
271a5f53b1dSJiri Olsa return NULL;
272a5f53b1dSJiri Olsa }
273a5f53b1dSJiri Olsa
274a5f53b1dSJiri Olsa return btf_id__add(&obj->sets, id, true);
275a5f53b1dSJiri Olsa }
276a5f53b1dSJiri Olsa
add_symbol(struct rb_root * root,char * name,size_t size)277fbbb68deSJiri Olsa static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
278fbbb68deSJiri Olsa {
279fbbb68deSJiri Olsa char *id;
280fbbb68deSJiri Olsa
281fbbb68deSJiri Olsa id = get_id(name + size);
282fbbb68deSJiri Olsa if (!id) {
283fbbb68deSJiri Olsa pr_err("FAILED to parse symbol name: %s\n", name);
284fbbb68deSJiri Olsa return NULL;
285fbbb68deSJiri Olsa }
286fbbb68deSJiri Olsa
287fbbb68deSJiri Olsa return btf_id__add(root, id, false);
288fbbb68deSJiri Olsa }
289fbbb68deSJiri Olsa
29041462c6eSKun-Chuan Hsieh /* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */
29141462c6eSKun-Chuan Hsieh #ifndef SHF_COMPRESSED
29241462c6eSKun-Chuan Hsieh #define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */
29341462c6eSKun-Chuan Hsieh #endif
29441462c6eSKun-Chuan Hsieh
29551f6463aSJiri Olsa /*
29651f6463aSJiri Olsa * The data of compressed section should be aligned to 4
29751f6463aSJiri Olsa * (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
29851f6463aSJiri Olsa * sets sh_addralign to 1, which makes libelf fail with
29951f6463aSJiri Olsa * misaligned section error during the update:
30051f6463aSJiri Olsa * FAILED elf_update(WRITE): invalid section alignment
30151f6463aSJiri Olsa *
30251f6463aSJiri Olsa * While waiting for ld fix, we fix the compressed sections
30351f6463aSJiri Olsa * sh_addralign value manualy.
30451f6463aSJiri Olsa */
compressed_section_fix(Elf * elf,Elf_Scn * scn,GElf_Shdr * sh)30551f6463aSJiri Olsa static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
30651f6463aSJiri Olsa {
30751f6463aSJiri Olsa int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
30851f6463aSJiri Olsa
30951f6463aSJiri Olsa if (!(sh->sh_flags & SHF_COMPRESSED))
31051f6463aSJiri Olsa return 0;
31151f6463aSJiri Olsa
31251f6463aSJiri Olsa if (sh->sh_addralign == expected)
31351f6463aSJiri Olsa return 0;
31451f6463aSJiri Olsa
31551f6463aSJiri Olsa pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
31651f6463aSJiri Olsa sh->sh_addralign, expected);
31751f6463aSJiri Olsa
31851f6463aSJiri Olsa sh->sh_addralign = expected;
31951f6463aSJiri Olsa
32051f6463aSJiri Olsa if (gelf_update_shdr(scn, sh) == 0) {
3215aad0368SHengqi Chen pr_err("FAILED cannot update section header: %s\n",
32251f6463aSJiri Olsa elf_errmsg(-1));
32351f6463aSJiri Olsa return -1;
32451f6463aSJiri Olsa }
32551f6463aSJiri Olsa return 0;
32651f6463aSJiri Olsa }
32751f6463aSJiri Olsa
elf_collect(struct object * obj)328fbbb68deSJiri Olsa static int elf_collect(struct object *obj)
329fbbb68deSJiri Olsa {
330fbbb68deSJiri Olsa Elf_Scn *scn = NULL;
331fbbb68deSJiri Olsa size_t shdrstrndx;
332903fad43SViktor Malik GElf_Ehdr ehdr;
333fbbb68deSJiri Olsa int idx = 0;
334fbbb68deSJiri Olsa Elf *elf;
335fbbb68deSJiri Olsa int fd;
336fbbb68deSJiri Olsa
337fbbb68deSJiri Olsa fd = open(obj->path, O_RDWR, 0666);
338fbbb68deSJiri Olsa if (fd == -1) {
339fbbb68deSJiri Olsa pr_err("FAILED cannot open %s: %s\n",
340fbbb68deSJiri Olsa obj->path, strerror(errno));
341fbbb68deSJiri Olsa return -1;
342fbbb68deSJiri Olsa }
343fbbb68deSJiri Olsa
344fbbb68deSJiri Olsa elf_version(EV_CURRENT);
345fbbb68deSJiri Olsa
346fbbb68deSJiri Olsa elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
347fbbb68deSJiri Olsa if (!elf) {
3485aad0368SHengqi Chen close(fd);
349fbbb68deSJiri Olsa pr_err("FAILED cannot create ELF descriptor: %s\n",
350fbbb68deSJiri Olsa elf_errmsg(-1));
351fbbb68deSJiri Olsa return -1;
352fbbb68deSJiri Olsa }
353fbbb68deSJiri Olsa
354fbbb68deSJiri Olsa obj->efile.fd = fd;
355fbbb68deSJiri Olsa obj->efile.elf = elf;
356fbbb68deSJiri Olsa
357fbbb68deSJiri Olsa elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
358fbbb68deSJiri Olsa
359fbbb68deSJiri Olsa if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
360fbbb68deSJiri Olsa pr_err("FAILED cannot get shdr str ndx\n");
361fbbb68deSJiri Olsa return -1;
362fbbb68deSJiri Olsa }
363fbbb68deSJiri Olsa
364903fad43SViktor Malik if (gelf_getehdr(obj->efile.elf, &ehdr) == NULL) {
365903fad43SViktor Malik pr_err("FAILED cannot get ELF header: %s\n",
366903fad43SViktor Malik elf_errmsg(-1));
367903fad43SViktor Malik return -1;
368903fad43SViktor Malik }
369903fad43SViktor Malik obj->efile.encoding = ehdr.e_ident[EI_DATA];
370903fad43SViktor Malik
371fbbb68deSJiri Olsa /*
372fbbb68deSJiri Olsa * Scan all the elf sections and look for save data
373fbbb68deSJiri Olsa * from .BTF_ids section and symbols.
374fbbb68deSJiri Olsa */
375fbbb68deSJiri Olsa while ((scn = elf_nextscn(elf, scn)) != NULL) {
376fbbb68deSJiri Olsa Elf_Data *data;
377fbbb68deSJiri Olsa GElf_Shdr sh;
378fbbb68deSJiri Olsa char *name;
379fbbb68deSJiri Olsa
380fbbb68deSJiri Olsa idx++;
381fbbb68deSJiri Olsa if (gelf_getshdr(scn, &sh) != &sh) {
382fbbb68deSJiri Olsa pr_err("FAILED get section(%d) header\n", idx);
383fbbb68deSJiri Olsa return -1;
384fbbb68deSJiri Olsa }
385fbbb68deSJiri Olsa
386fbbb68deSJiri Olsa name = elf_strptr(elf, shdrstrndx, sh.sh_name);
387fbbb68deSJiri Olsa if (!name) {
388fbbb68deSJiri Olsa pr_err("FAILED get section(%d) name\n", idx);
389fbbb68deSJiri Olsa return -1;
390fbbb68deSJiri Olsa }
391fbbb68deSJiri Olsa
392fbbb68deSJiri Olsa data = elf_getdata(scn, 0);
393fbbb68deSJiri Olsa if (!data) {
394fbbb68deSJiri Olsa pr_err("FAILED to get section(%d) data from %s\n",
395fbbb68deSJiri Olsa idx, name);
396fbbb68deSJiri Olsa return -1;
397fbbb68deSJiri Olsa }
398fbbb68deSJiri Olsa
399fbbb68deSJiri Olsa pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
400fbbb68deSJiri Olsa idx, name, (unsigned long) data->d_size,
401fbbb68deSJiri Olsa (int) sh.sh_link, (unsigned long) sh.sh_flags,
402fbbb68deSJiri Olsa (int) sh.sh_type);
403fbbb68deSJiri Olsa
404fbbb68deSJiri Olsa if (sh.sh_type == SHT_SYMTAB) {
405fbbb68deSJiri Olsa obj->efile.symbols = data;
406fbbb68deSJiri Olsa obj->efile.symbols_shndx = idx;
407fbbb68deSJiri Olsa obj->efile.strtabidx = sh.sh_link;
408fbbb68deSJiri Olsa } else if (!strcmp(name, BTF_IDS_SECTION)) {
409fbbb68deSJiri Olsa obj->efile.idlist = data;
410fbbb68deSJiri Olsa obj->efile.idlist_shndx = idx;
411fbbb68deSJiri Olsa obj->efile.idlist_addr = sh.sh_addr;
4126ba77385SAlan Maguire } else if (!strcmp(name, BTF_BASE_ELF_SEC)) {
4136ba77385SAlan Maguire /* If a .BTF.base section is found, do not resolve
4146ba77385SAlan Maguire * BTF ids relative to vmlinux; resolve relative
4156ba77385SAlan Maguire * to the .BTF.base section instead. btf__parse_split()
4166ba77385SAlan Maguire * will take care of this once the base BTF it is
4176ba77385SAlan Maguire * passed is NULL.
4186ba77385SAlan Maguire */
4196ba77385SAlan Maguire obj->base_btf_path = NULL;
420fbbb68deSJiri Olsa }
42151f6463aSJiri Olsa
42251f6463aSJiri Olsa if (compressed_section_fix(elf, scn, &sh))
42351f6463aSJiri Olsa return -1;
424fbbb68deSJiri Olsa }
425fbbb68deSJiri Olsa
426fbbb68deSJiri Olsa return 0;
427fbbb68deSJiri Olsa }
428fbbb68deSJiri Olsa
symbols_collect(struct object * obj)429fbbb68deSJiri Olsa static int symbols_collect(struct object *obj)
430fbbb68deSJiri Olsa {
431fbbb68deSJiri Olsa Elf_Scn *scn = NULL;
432e27bfefbSStanislav Fomichev int n, i;
433fbbb68deSJiri Olsa GElf_Shdr sh;
434fbbb68deSJiri Olsa char *name;
435fbbb68deSJiri Olsa
436fbbb68deSJiri Olsa scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
437fbbb68deSJiri Olsa if (!scn)
438fbbb68deSJiri Olsa return -1;
439fbbb68deSJiri Olsa
440fbbb68deSJiri Olsa if (gelf_getshdr(scn, &sh) != &sh)
441fbbb68deSJiri Olsa return -1;
442fbbb68deSJiri Olsa
443fbbb68deSJiri Olsa n = sh.sh_size / sh.sh_entsize;
444fbbb68deSJiri Olsa
445fbbb68deSJiri Olsa /*
446fbbb68deSJiri Olsa * Scan symbols and look for the ones starting with
447fbbb68deSJiri Olsa * __BTF_ID__* over .BTF_ids section.
448fbbb68deSJiri Olsa */
449e27bfefbSStanislav Fomichev for (i = 0; i < n; i++) {
450e27bfefbSStanislav Fomichev char *prefix;
451fbbb68deSJiri Olsa struct btf_id *id;
452fbbb68deSJiri Olsa GElf_Sym sym;
453fbbb68deSJiri Olsa
454fbbb68deSJiri Olsa if (!gelf_getsym(obj->efile.symbols, i, &sym))
455fbbb68deSJiri Olsa return -1;
456fbbb68deSJiri Olsa
457fbbb68deSJiri Olsa if (sym.st_shndx != obj->efile.idlist_shndx)
458fbbb68deSJiri Olsa continue;
459fbbb68deSJiri Olsa
460fbbb68deSJiri Olsa name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
461fbbb68deSJiri Olsa sym.st_name);
462fbbb68deSJiri Olsa
463fbbb68deSJiri Olsa if (!is_btf_id(name))
464fbbb68deSJiri Olsa continue;
465fbbb68deSJiri Olsa
466fbbb68deSJiri Olsa /*
467fbbb68deSJiri Olsa * __BTF_ID__TYPE__vfs_truncate__0
468fbbb68deSJiri Olsa * prefix = ^
469fbbb68deSJiri Olsa */
4709707ac4fSViktor Malik prefix = name + sizeof(BTF_ID_PREFIX) - 1;
471fbbb68deSJiri Olsa
472fbbb68deSJiri Olsa /* struct */
473fbbb68deSJiri Olsa if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
474fbbb68deSJiri Olsa obj->nr_structs++;
475fbbb68deSJiri Olsa id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
476fbbb68deSJiri Olsa /* union */
477fbbb68deSJiri Olsa } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
478fbbb68deSJiri Olsa obj->nr_unions++;
479fbbb68deSJiri Olsa id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
480fbbb68deSJiri Olsa /* typedef */
481fbbb68deSJiri Olsa } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
482fbbb68deSJiri Olsa obj->nr_typedefs++;
483fbbb68deSJiri Olsa id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
484fbbb68deSJiri Olsa /* func */
485fbbb68deSJiri Olsa } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
486fbbb68deSJiri Olsa obj->nr_funcs++;
487fbbb68deSJiri Olsa id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
488ef2c6f37SKumar Kartikeya Dwivedi /* set8 */
489ef2c6f37SKumar Kartikeya Dwivedi } else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
490ef2c6f37SKumar Kartikeya Dwivedi id = add_set(obj, prefix, true);
491ef2c6f37SKumar Kartikeya Dwivedi /*
492ef2c6f37SKumar Kartikeya Dwivedi * SET8 objects store list's count, which is encoded
493ef2c6f37SKumar Kartikeya Dwivedi * in symbol's size, together with 'cnt' field hence
494ef2c6f37SKumar Kartikeya Dwivedi * that - 1.
495ef2c6f37SKumar Kartikeya Dwivedi */
496ef2c6f37SKumar Kartikeya Dwivedi if (id) {
497ef2c6f37SKumar Kartikeya Dwivedi id->cnt = sym.st_size / sizeof(uint64_t) - 1;
498ef2c6f37SKumar Kartikeya Dwivedi id->is_set8 = true;
499ef2c6f37SKumar Kartikeya Dwivedi }
500fbbb68deSJiri Olsa /* set */
501fbbb68deSJiri Olsa } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
502ef2c6f37SKumar Kartikeya Dwivedi id = add_set(obj, prefix, false);
503fbbb68deSJiri Olsa /*
504fbbb68deSJiri Olsa * SET objects store list's count, which is encoded
505fbbb68deSJiri Olsa * in symbol's size, together with 'cnt' field hence
506fbbb68deSJiri Olsa * that - 1.
507fbbb68deSJiri Olsa */
5083345193fSKumar Kartikeya Dwivedi if (id) {
509fbbb68deSJiri Olsa id->cnt = sym.st_size / sizeof(int) - 1;
5103345193fSKumar Kartikeya Dwivedi id->is_set = true;
5113345193fSKumar Kartikeya Dwivedi }
512fbbb68deSJiri Olsa } else {
513fbbb68deSJiri Olsa pr_err("FAILED unsupported prefix %s\n", prefix);
514fbbb68deSJiri Olsa return -1;
515fbbb68deSJiri Olsa }
516fbbb68deSJiri Olsa
517fbbb68deSJiri Olsa if (!id)
518fbbb68deSJiri Olsa return -ENOMEM;
519fbbb68deSJiri Olsa
520fbbb68deSJiri Olsa if (id->addr_cnt >= ADDR_CNT) {
52122e8ebe3SBrendan Jackman pr_err("FAILED symbol %s crossed the number of allowed lists\n",
522fbbb68deSJiri Olsa id->name);
523fbbb68deSJiri Olsa return -1;
524fbbb68deSJiri Olsa }
525fbbb68deSJiri Olsa id->addr[id->addr_cnt++] = sym.st_value;
526fbbb68deSJiri Olsa }
527fbbb68deSJiri Olsa
528fbbb68deSJiri Olsa return 0;
529fbbb68deSJiri Olsa }
530fbbb68deSJiri Olsa
symbols_resolve(struct object * obj)531fbbb68deSJiri Olsa static int symbols_resolve(struct object *obj)
532fbbb68deSJiri Olsa {
533fbbb68deSJiri Olsa int nr_typedefs = obj->nr_typedefs;
534fbbb68deSJiri Olsa int nr_structs = obj->nr_structs;
535fbbb68deSJiri Olsa int nr_unions = obj->nr_unions;
536fbbb68deSJiri Olsa int nr_funcs = obj->nr_funcs;
537f614f2c7SKumar Kartikeya Dwivedi struct btf *base_btf = NULL;
538fbbb68deSJiri Olsa int err, type_id;
539fbbb68deSJiri Olsa struct btf *btf;
54067208692SJiri Olsa __u32 nr_types;
541fbbb68deSJiri Olsa
542f614f2c7SKumar Kartikeya Dwivedi if (obj->base_btf_path) {
543f614f2c7SKumar Kartikeya Dwivedi base_btf = btf__parse(obj->base_btf_path, NULL);
544f614f2c7SKumar Kartikeya Dwivedi err = libbpf_get_error(base_btf);
545f614f2c7SKumar Kartikeya Dwivedi if (err) {
546f614f2c7SKumar Kartikeya Dwivedi pr_err("FAILED: load base BTF from %s: %s\n",
547f614f2c7SKumar Kartikeya Dwivedi obj->base_btf_path, strerror(-err));
548f614f2c7SKumar Kartikeya Dwivedi return -1;
549f614f2c7SKumar Kartikeya Dwivedi }
550f614f2c7SKumar Kartikeya Dwivedi }
551f614f2c7SKumar Kartikeya Dwivedi
552f614f2c7SKumar Kartikeya Dwivedi btf = btf__parse_split(obj->btf ?: obj->path, base_btf);
553fbbb68deSJiri Olsa err = libbpf_get_error(btf);
554fbbb68deSJiri Olsa if (err) {
55522e8ebe3SBrendan Jackman pr_err("FAILED: load BTF from %s: %s\n",
5565aad0368SHengqi Chen obj->btf ?: obj->path, strerror(-err));
557f614f2c7SKumar Kartikeya Dwivedi goto out;
558fbbb68deSJiri Olsa }
559fbbb68deSJiri Olsa
560fbbb68deSJiri Olsa err = -1;
5612d8f09faSHengqi Chen nr_types = btf__type_cnt(btf);
562fbbb68deSJiri Olsa
563fbbb68deSJiri Olsa /*
564fbbb68deSJiri Olsa * Iterate all the BTF types and search for collected symbol IDs.
565fbbb68deSJiri Olsa */
5662d8f09faSHengqi Chen for (type_id = 1; type_id < nr_types; type_id++) {
567fbbb68deSJiri Olsa const struct btf_type *type;
568fbbb68deSJiri Olsa struct rb_root *root;
569fbbb68deSJiri Olsa struct btf_id *id;
570fbbb68deSJiri Olsa const char *str;
571fbbb68deSJiri Olsa int *nr;
572fbbb68deSJiri Olsa
573fbbb68deSJiri Olsa type = btf__type_by_id(btf, type_id);
574fbbb68deSJiri Olsa if (!type) {
575fbbb68deSJiri Olsa pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
576fbbb68deSJiri Olsa type_id);
577fbbb68deSJiri Olsa goto out;
578fbbb68deSJiri Olsa }
579fbbb68deSJiri Olsa
580fbbb68deSJiri Olsa if (btf_is_func(type) && nr_funcs) {
581fbbb68deSJiri Olsa nr = &nr_funcs;
582fbbb68deSJiri Olsa root = &obj->funcs;
583fbbb68deSJiri Olsa } else if (btf_is_struct(type) && nr_structs) {
584fbbb68deSJiri Olsa nr = &nr_structs;
585fbbb68deSJiri Olsa root = &obj->structs;
586fbbb68deSJiri Olsa } else if (btf_is_union(type) && nr_unions) {
587fbbb68deSJiri Olsa nr = &nr_unions;
588fbbb68deSJiri Olsa root = &obj->unions;
589fbbb68deSJiri Olsa } else if (btf_is_typedef(type) && nr_typedefs) {
590fbbb68deSJiri Olsa nr = &nr_typedefs;
591fbbb68deSJiri Olsa root = &obj->typedefs;
592fbbb68deSJiri Olsa } else
593fbbb68deSJiri Olsa continue;
594fbbb68deSJiri Olsa
595fbbb68deSJiri Olsa str = btf__name_by_offset(btf, type->name_off);
596fbbb68deSJiri Olsa if (!str) {
597fbbb68deSJiri Olsa pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
598fbbb68deSJiri Olsa type_id);
599fbbb68deSJiri Olsa goto out;
600fbbb68deSJiri Olsa }
601fbbb68deSJiri Olsa
602fbbb68deSJiri Olsa id = btf_id__find(root, str);
603fbbb68deSJiri Olsa if (id) {
60467208692SJiri Olsa if (id->id) {
60567208692SJiri Olsa pr_info("WARN: multiple IDs found for '%s': %d, %d - using %d\n",
60667208692SJiri Olsa str, id->id, type_id, id->id);
60767208692SJiri Olsa } else {
608fbbb68deSJiri Olsa id->id = type_id;
609fbbb68deSJiri Olsa (*nr)--;
610fbbb68deSJiri Olsa }
611fbbb68deSJiri Olsa }
61267208692SJiri Olsa }
613fbbb68deSJiri Olsa
614fbbb68deSJiri Olsa err = 0;
615fbbb68deSJiri Olsa out:
616f614f2c7SKumar Kartikeya Dwivedi btf__free(base_btf);
617fbbb68deSJiri Olsa btf__free(btf);
618fbbb68deSJiri Olsa return err;
619fbbb68deSJiri Olsa }
620fbbb68deSJiri Olsa
id_patch(struct object * obj,struct btf_id * id)621fbbb68deSJiri Olsa static int id_patch(struct object *obj, struct btf_id *id)
622fbbb68deSJiri Olsa {
623fbbb68deSJiri Olsa Elf_Data *data = obj->efile.idlist;
624fbbb68deSJiri Olsa int *ptr = data->d_buf;
625fbbb68deSJiri Olsa int i;
626fbbb68deSJiri Olsa
627ef2c6f37SKumar Kartikeya Dwivedi /* For set, set8, id->id may be 0 */
628ef2c6f37SKumar Kartikeya Dwivedi if (!id->id && !id->is_set && !id->is_set8)
6295aad0368SHengqi Chen pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
630fbbb68deSJiri Olsa
631fbbb68deSJiri Olsa for (i = 0; i < id->addr_cnt; i++) {
632fbbb68deSJiri Olsa unsigned long addr = id->addr[i];
633fbbb68deSJiri Olsa unsigned long idx = addr - obj->efile.idlist_addr;
634fbbb68deSJiri Olsa
635fbbb68deSJiri Olsa pr_debug("patching addr %5lu: ID %7d [%s]\n",
636fbbb68deSJiri Olsa idx, id->id, id->name);
637fbbb68deSJiri Olsa
638fbbb68deSJiri Olsa if (idx >= data->d_size) {
639fbbb68deSJiri Olsa pr_err("FAILED patching index %lu out of bounds %lu\n",
640fbbb68deSJiri Olsa idx, data->d_size);
641fbbb68deSJiri Olsa return -1;
642fbbb68deSJiri Olsa }
643fbbb68deSJiri Olsa
644fbbb68deSJiri Olsa idx = idx / sizeof(int);
645fbbb68deSJiri Olsa ptr[idx] = id->id;
646fbbb68deSJiri Olsa }
647fbbb68deSJiri Olsa
648fbbb68deSJiri Olsa return 0;
649fbbb68deSJiri Olsa }
650fbbb68deSJiri Olsa
__symbols_patch(struct object * obj,struct rb_root * root)651fbbb68deSJiri Olsa static int __symbols_patch(struct object *obj, struct rb_root *root)
652fbbb68deSJiri Olsa {
653fbbb68deSJiri Olsa struct rb_node *next;
654fbbb68deSJiri Olsa struct btf_id *id;
655fbbb68deSJiri Olsa
656fbbb68deSJiri Olsa next = rb_first(root);
657fbbb68deSJiri Olsa while (next) {
658fbbb68deSJiri Olsa id = rb_entry(next, struct btf_id, rb_node);
659fbbb68deSJiri Olsa
660fbbb68deSJiri Olsa if (id_patch(obj, id))
661fbbb68deSJiri Olsa return -1;
662fbbb68deSJiri Olsa
663fbbb68deSJiri Olsa next = rb_next(next);
664fbbb68deSJiri Olsa }
665fbbb68deSJiri Olsa return 0;
666fbbb68deSJiri Olsa }
667fbbb68deSJiri Olsa
cmp_id(const void * pa,const void * pb)668fbbb68deSJiri Olsa static int cmp_id(const void *pa, const void *pb)
669fbbb68deSJiri Olsa {
670fbbb68deSJiri Olsa const int *a = pa, *b = pb;
671fbbb68deSJiri Olsa
672fbbb68deSJiri Olsa return *a - *b;
673fbbb68deSJiri Olsa }
674fbbb68deSJiri Olsa
sets_patch(struct object * obj)675fbbb68deSJiri Olsa static int sets_patch(struct object *obj)
676fbbb68deSJiri Olsa {
677fbbb68deSJiri Olsa Elf_Data *data = obj->efile.idlist;
678fbbb68deSJiri Olsa struct rb_node *next;
679fbbb68deSJiri Olsa
680fbbb68deSJiri Olsa next = rb_first(&obj->sets);
681fbbb68deSJiri Olsa while (next) {
6829707ac4fSViktor Malik struct btf_id_set8 *set8;
6839707ac4fSViktor Malik struct btf_id_set *set;
6849707ac4fSViktor Malik unsigned long addr, off;
685fbbb68deSJiri Olsa struct btf_id *id;
686fbbb68deSJiri Olsa
687fbbb68deSJiri Olsa id = rb_entry(next, struct btf_id, rb_node);
688fbbb68deSJiri Olsa addr = id->addr[0];
6899707ac4fSViktor Malik off = addr - obj->efile.idlist_addr;
690fbbb68deSJiri Olsa
691fbbb68deSJiri Olsa /* sets are unique */
692fbbb68deSJiri Olsa if (id->addr_cnt != 1) {
693fbbb68deSJiri Olsa pr_err("FAILED malformed data for set '%s'\n",
694fbbb68deSJiri Olsa id->name);
695fbbb68deSJiri Olsa return -1;
696fbbb68deSJiri Olsa }
697fbbb68deSJiri Olsa
6989707ac4fSViktor Malik if (id->is_set) {
6999707ac4fSViktor Malik set = data->d_buf + off;
7009707ac4fSViktor Malik qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id);
7019707ac4fSViktor Malik } else {
7029707ac4fSViktor Malik set8 = data->d_buf + off;
7039707ac4fSViktor Malik /*
7049707ac4fSViktor Malik * Make sure id is at the beginning of the pairs
7059707ac4fSViktor Malik * struct, otherwise the below qsort would not work.
7069707ac4fSViktor Malik */
707*13c9b702SLiwei Song BUILD_BUG_ON((u32 *)set8->pairs != &set8->pairs[0].id);
7089707ac4fSViktor Malik qsort(set8->pairs, set8->cnt, sizeof(set8->pairs[0]), cmp_id);
709903fad43SViktor Malik
710903fad43SViktor Malik /*
711903fad43SViktor Malik * When ELF endianness does not match endianness of the
712903fad43SViktor Malik * host, libelf will do the translation when updating
713903fad43SViktor Malik * the ELF. This, however, corrupts SET8 flags which are
714903fad43SViktor Malik * already in the target endianness. So, let's bswap
715903fad43SViktor Malik * them to the host endianness and libelf will then
716903fad43SViktor Malik * correctly translate everything.
717903fad43SViktor Malik */
718903fad43SViktor Malik if (obj->efile.encoding != ELFDATANATIVE) {
719903fad43SViktor Malik int i;
720903fad43SViktor Malik
721903fad43SViktor Malik set8->flags = bswap_32(set8->flags);
722903fad43SViktor Malik for (i = 0; i < set8->cnt; i++) {
723903fad43SViktor Malik set8->pairs[i].flags =
724903fad43SViktor Malik bswap_32(set8->pairs[i].flags);
725903fad43SViktor Malik }
726903fad43SViktor Malik }
7279707ac4fSViktor Malik }
728fbbb68deSJiri Olsa
729fbbb68deSJiri Olsa pr_debug("sorting addr %5lu: cnt %6d [%s]\n",
7309707ac4fSViktor Malik off, id->is_set ? set->cnt : set8->cnt, id->name);
731fbbb68deSJiri Olsa
732fbbb68deSJiri Olsa next = rb_next(next);
733fbbb68deSJiri Olsa }
734d48556f4SStanislav Fomichev return 0;
735fbbb68deSJiri Olsa }
736fbbb68deSJiri Olsa
symbols_patch(struct object * obj)737fbbb68deSJiri Olsa static int symbols_patch(struct object *obj)
738fbbb68deSJiri Olsa {
73944382b3eSFriedrich Vock off_t err;
740fbbb68deSJiri Olsa
741fbbb68deSJiri Olsa if (__symbols_patch(obj, &obj->structs) ||
742fbbb68deSJiri Olsa __symbols_patch(obj, &obj->unions) ||
743fbbb68deSJiri Olsa __symbols_patch(obj, &obj->typedefs) ||
744fbbb68deSJiri Olsa __symbols_patch(obj, &obj->funcs) ||
745fbbb68deSJiri Olsa __symbols_patch(obj, &obj->sets))
746fbbb68deSJiri Olsa return -1;
747fbbb68deSJiri Olsa
748fbbb68deSJiri Olsa if (sets_patch(obj))
749fbbb68deSJiri Olsa return -1;
750fbbb68deSJiri Olsa
75161e8aedaSTony Ambardar /* Set type to ensure endian translation occurs. */
75261e8aedaSTony Ambardar obj->efile.idlist->d_type = ELF_T_WORD;
75361e8aedaSTony Ambardar
754fbbb68deSJiri Olsa elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
755fbbb68deSJiri Olsa
756fbbb68deSJiri Olsa err = elf_update(obj->efile.elf, ELF_C_WRITE);
757fbbb68deSJiri Olsa if (err < 0) {
758fbbb68deSJiri Olsa pr_err("FAILED elf_update(WRITE): %s\n",
759fbbb68deSJiri Olsa elf_errmsg(-1));
760fbbb68deSJiri Olsa }
761fbbb68deSJiri Olsa
762fbbb68deSJiri Olsa pr_debug("update %s for %s\n",
763fbbb68deSJiri Olsa err >= 0 ? "ok" : "failed", obj->path);
764fbbb68deSJiri Olsa return err < 0 ? -1 : 0;
765fbbb68deSJiri Olsa }
766fbbb68deSJiri Olsa
767fbbb68deSJiri Olsa static const char * const resolve_btfids_usage[] = {
768fbbb68deSJiri Olsa "resolve_btfids [<options>] <ELF object>",
769fbbb68deSJiri Olsa NULL
770fbbb68deSJiri Olsa };
771fbbb68deSJiri Olsa
main(int argc,const char ** argv)772fbbb68deSJiri Olsa int main(int argc, const char **argv)
773fbbb68deSJiri Olsa {
774fbbb68deSJiri Olsa struct object obj = {
775fbbb68deSJiri Olsa .efile = {
776fbbb68deSJiri Olsa .idlist_shndx = -1,
777fbbb68deSJiri Olsa .symbols_shndx = -1,
778fbbb68deSJiri Olsa },
779fbbb68deSJiri Olsa .structs = RB_ROOT,
780fbbb68deSJiri Olsa .unions = RB_ROOT,
781fbbb68deSJiri Olsa .typedefs = RB_ROOT,
782fbbb68deSJiri Olsa .funcs = RB_ROOT,
783fbbb68deSJiri Olsa .sets = RB_ROOT,
784fbbb68deSJiri Olsa };
785fbbb68deSJiri Olsa struct option btfid_options[] = {
786fbbb68deSJiri Olsa OPT_INCR('v', "verbose", &verbose,
787fbbb68deSJiri Olsa "be more verbose (show errors, etc)"),
788fbbb68deSJiri Olsa OPT_STRING(0, "btf", &obj.btf, "BTF data",
789fbbb68deSJiri Olsa "BTF data"),
790f614f2c7SKumar Kartikeya Dwivedi OPT_STRING('b', "btf_base", &obj.base_btf_path, "file",
791f614f2c7SKumar Kartikeya Dwivedi "path of file providing base BTF"),
792fbbb68deSJiri Olsa OPT_END()
793fbbb68deSJiri Olsa };
794fbbb68deSJiri Olsa int err = -1;
795fbbb68deSJiri Olsa
796fbbb68deSJiri Olsa argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
797fbbb68deSJiri Olsa PARSE_OPT_STOP_AT_NON_OPTION);
798fbbb68deSJiri Olsa if (argc != 1)
799fbbb68deSJiri Olsa usage_with_options(resolve_btfids_usage, btfid_options);
800fbbb68deSJiri Olsa
801fbbb68deSJiri Olsa obj.path = argv[0];
802fbbb68deSJiri Olsa
803fbbb68deSJiri Olsa if (elf_collect(&obj))
804fbbb68deSJiri Olsa goto out;
805fbbb68deSJiri Olsa
806fbbb68deSJiri Olsa /*
807fbbb68deSJiri Olsa * We did not find .BTF_ids section or symbols section,
808fbbb68deSJiri Olsa * nothing to do..
809fbbb68deSJiri Olsa */
810fbbb68deSJiri Olsa if (obj.efile.idlist_shndx == -1 ||
811fbbb68deSJiri Olsa obj.efile.symbols_shndx == -1) {
812f614f2c7SKumar Kartikeya Dwivedi pr_debug("Cannot find .BTF_ids or symbols sections, nothing to do\n");
8131144ab9bSAndrii Nakryiko err = 0;
8141144ab9bSAndrii Nakryiko goto out;
815fbbb68deSJiri Olsa }
816fbbb68deSJiri Olsa
817fbbb68deSJiri Olsa if (symbols_collect(&obj))
818fbbb68deSJiri Olsa goto out;
819fbbb68deSJiri Olsa
820fbbb68deSJiri Olsa if (symbols_resolve(&obj))
821fbbb68deSJiri Olsa goto out;
822fbbb68deSJiri Olsa
823fbbb68deSJiri Olsa if (symbols_patch(&obj))
824fbbb68deSJiri Olsa goto out;
825fbbb68deSJiri Olsa
826fbbb68deSJiri Olsa err = 0;
827fbbb68deSJiri Olsa out:
8285aad0368SHengqi Chen if (obj.efile.elf) {
829fbbb68deSJiri Olsa elf_end(obj.efile.elf);
830fbbb68deSJiri Olsa close(obj.efile.fd);
8315aad0368SHengqi Chen }
832fbbb68deSJiri Olsa return err;
833fbbb68deSJiri Olsa }
834