1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /*
3*f7c14bbaSAndroid Build Coastguard Worker * BPF static linker
4*f7c14bbaSAndroid Build Coastguard Worker *
5*f7c14bbaSAndroid Build Coastguard Worker * Copyright (c) 2021 Facebook
6*f7c14bbaSAndroid Build Coastguard Worker */
7*f7c14bbaSAndroid Build Coastguard Worker #include <stdbool.h>
8*f7c14bbaSAndroid Build Coastguard Worker #include <stddef.h>
9*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
10*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
11*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
12*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <linux/err.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <linux/btf.h>
16*f7c14bbaSAndroid Build Coastguard Worker #include <elf.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include <libelf.h>
18*f7c14bbaSAndroid Build Coastguard Worker #include <fcntl.h>
19*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
20*f7c14bbaSAndroid Build Coastguard Worker #include "btf.h"
21*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
22*f7c14bbaSAndroid Build Coastguard Worker #include "strset.h"
23*f7c14bbaSAndroid Build Coastguard Worker
24*f7c14bbaSAndroid Build Coastguard Worker #define BTF_EXTERN_SEC ".extern"
25*f7c14bbaSAndroid Build Coastguard Worker
26*f7c14bbaSAndroid Build Coastguard Worker struct src_sec {
27*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
28*f7c14bbaSAndroid Build Coastguard Worker /* positional (not necessarily ELF) index in an array of sections */
29*f7c14bbaSAndroid Build Coastguard Worker int id;
30*f7c14bbaSAndroid Build Coastguard Worker /* positional (not necessarily ELF) index of a matching section in a final object file */
31*f7c14bbaSAndroid Build Coastguard Worker int dst_id;
32*f7c14bbaSAndroid Build Coastguard Worker /* section data offset in a matching output section */
33*f7c14bbaSAndroid Build Coastguard Worker int dst_off;
34*f7c14bbaSAndroid Build Coastguard Worker /* whether section is omitted from the final ELF file */
35*f7c14bbaSAndroid Build Coastguard Worker bool skipped;
36*f7c14bbaSAndroid Build Coastguard Worker /* whether section is an ephemeral section, not mapped to an ELF section */
37*f7c14bbaSAndroid Build Coastguard Worker bool ephemeral;
38*f7c14bbaSAndroid Build Coastguard Worker
39*f7c14bbaSAndroid Build Coastguard Worker /* ELF info */
40*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx;
41*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
42*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
43*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
44*f7c14bbaSAndroid Build Coastguard Worker
45*f7c14bbaSAndroid Build Coastguard Worker /* corresponding BTF DATASEC type ID */
46*f7c14bbaSAndroid Build Coastguard Worker int sec_type_id;
47*f7c14bbaSAndroid Build Coastguard Worker };
48*f7c14bbaSAndroid Build Coastguard Worker
49*f7c14bbaSAndroid Build Coastguard Worker struct src_obj {
50*f7c14bbaSAndroid Build Coastguard Worker const char *filename;
51*f7c14bbaSAndroid Build Coastguard Worker int fd;
52*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
53*f7c14bbaSAndroid Build Coastguard Worker /* Section header strings section index */
54*f7c14bbaSAndroid Build Coastguard Worker size_t shstrs_sec_idx;
55*f7c14bbaSAndroid Build Coastguard Worker /* SYMTAB section index */
56*f7c14bbaSAndroid Build Coastguard Worker size_t symtab_sec_idx;
57*f7c14bbaSAndroid Build Coastguard Worker
58*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
59*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext *btf_ext;
60*f7c14bbaSAndroid Build Coastguard Worker
61*f7c14bbaSAndroid Build Coastguard Worker /* List of sections (including ephemeral). Slot zero is unused. */
62*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *secs;
63*f7c14bbaSAndroid Build Coastguard Worker int sec_cnt;
64*f7c14bbaSAndroid Build Coastguard Worker
65*f7c14bbaSAndroid Build Coastguard Worker /* mapping of symbol indices from src to dst ELF */
66*f7c14bbaSAndroid Build Coastguard Worker int *sym_map;
67*f7c14bbaSAndroid Build Coastguard Worker /* mapping from the src BTF type IDs to dst ones */
68*f7c14bbaSAndroid Build Coastguard Worker int *btf_type_map;
69*f7c14bbaSAndroid Build Coastguard Worker };
70*f7c14bbaSAndroid Build Coastguard Worker
71*f7c14bbaSAndroid Build Coastguard Worker /* single .BTF.ext data section */
72*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_sec_data {
73*f7c14bbaSAndroid Build Coastguard Worker size_t rec_cnt;
74*f7c14bbaSAndroid Build Coastguard Worker __u32 rec_sz;
75*f7c14bbaSAndroid Build Coastguard Worker void *recs;
76*f7c14bbaSAndroid Build Coastguard Worker };
77*f7c14bbaSAndroid Build Coastguard Worker
78*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym {
79*f7c14bbaSAndroid Build Coastguard Worker /* ELF symbol index */
80*f7c14bbaSAndroid Build Coastguard Worker int sym_idx;
81*f7c14bbaSAndroid Build Coastguard Worker /* associated section id for .ksyms, .kconfig, etc, but not .extern */
82*f7c14bbaSAndroid Build Coastguard Worker int sec_id;
83*f7c14bbaSAndroid Build Coastguard Worker /* extern name offset in STRTAB */
84*f7c14bbaSAndroid Build Coastguard Worker int name_off;
85*f7c14bbaSAndroid Build Coastguard Worker /* optional associated BTF type ID */
86*f7c14bbaSAndroid Build Coastguard Worker int btf_id;
87*f7c14bbaSAndroid Build Coastguard Worker /* BTF type ID to which VAR/FUNC type is pointing to; used for
88*f7c14bbaSAndroid Build Coastguard Worker * rewriting types when extern VAR/FUNC is resolved to a concrete
89*f7c14bbaSAndroid Build Coastguard Worker * definition
90*f7c14bbaSAndroid Build Coastguard Worker */
91*f7c14bbaSAndroid Build Coastguard Worker int underlying_btf_id;
92*f7c14bbaSAndroid Build Coastguard Worker /* sec_var index in the corresponding dst_sec, if exists */
93*f7c14bbaSAndroid Build Coastguard Worker int var_idx;
94*f7c14bbaSAndroid Build Coastguard Worker
95*f7c14bbaSAndroid Build Coastguard Worker /* extern or resolved/global symbol */
96*f7c14bbaSAndroid Build Coastguard Worker bool is_extern;
97*f7c14bbaSAndroid Build Coastguard Worker /* weak or strong symbol, never goes back from strong to weak */
98*f7c14bbaSAndroid Build Coastguard Worker bool is_weak;
99*f7c14bbaSAndroid Build Coastguard Worker };
100*f7c14bbaSAndroid Build Coastguard Worker
101*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec {
102*f7c14bbaSAndroid Build Coastguard Worker char *sec_name;
103*f7c14bbaSAndroid Build Coastguard Worker /* positional (not necessarily ELF) index in an array of sections */
104*f7c14bbaSAndroid Build Coastguard Worker int id;
105*f7c14bbaSAndroid Build Coastguard Worker
106*f7c14bbaSAndroid Build Coastguard Worker bool ephemeral;
107*f7c14bbaSAndroid Build Coastguard Worker
108*f7c14bbaSAndroid Build Coastguard Worker /* ELF info */
109*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx;
110*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
111*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
112*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
113*f7c14bbaSAndroid Build Coastguard Worker
114*f7c14bbaSAndroid Build Coastguard Worker /* final output section size */
115*f7c14bbaSAndroid Build Coastguard Worker int sec_sz;
116*f7c14bbaSAndroid Build Coastguard Worker /* final output contents of the section */
117*f7c14bbaSAndroid Build Coastguard Worker void *raw_data;
118*f7c14bbaSAndroid Build Coastguard Worker
119*f7c14bbaSAndroid Build Coastguard Worker /* corresponding STT_SECTION symbol index in SYMTAB */
120*f7c14bbaSAndroid Build Coastguard Worker int sec_sym_idx;
121*f7c14bbaSAndroid Build Coastguard Worker
122*f7c14bbaSAndroid Build Coastguard Worker /* section's DATASEC variable info, emitted on BTF finalization */
123*f7c14bbaSAndroid Build Coastguard Worker bool has_btf;
124*f7c14bbaSAndroid Build Coastguard Worker int sec_var_cnt;
125*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *sec_vars;
126*f7c14bbaSAndroid Build Coastguard Worker
127*f7c14bbaSAndroid Build Coastguard Worker /* section's .BTF.ext data */
128*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_sec_data func_info;
129*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_sec_data line_info;
130*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_sec_data core_relo_info;
131*f7c14bbaSAndroid Build Coastguard Worker };
132*f7c14bbaSAndroid Build Coastguard Worker
133*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker {
134*f7c14bbaSAndroid Build Coastguard Worker char *filename;
135*f7c14bbaSAndroid Build Coastguard Worker int fd;
136*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
137*f7c14bbaSAndroid Build Coastguard Worker Elf64_Ehdr *elf_hdr;
138*f7c14bbaSAndroid Build Coastguard Worker
139*f7c14bbaSAndroid Build Coastguard Worker /* Output sections metadata */
140*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *secs;
141*f7c14bbaSAndroid Build Coastguard Worker int sec_cnt;
142*f7c14bbaSAndroid Build Coastguard Worker
143*f7c14bbaSAndroid Build Coastguard Worker struct strset *strtab_strs; /* STRTAB unique strings */
144*f7c14bbaSAndroid Build Coastguard Worker size_t strtab_sec_idx; /* STRTAB section index */
145*f7c14bbaSAndroid Build Coastguard Worker size_t symtab_sec_idx; /* SYMTAB section index */
146*f7c14bbaSAndroid Build Coastguard Worker
147*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
148*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext *btf_ext;
149*f7c14bbaSAndroid Build Coastguard Worker
150*f7c14bbaSAndroid Build Coastguard Worker /* global (including extern) ELF symbols */
151*f7c14bbaSAndroid Build Coastguard Worker int glob_sym_cnt;
152*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_syms;
153*f7c14bbaSAndroid Build Coastguard Worker };
154*f7c14bbaSAndroid Build Coastguard Worker
155*f7c14bbaSAndroid Build Coastguard Worker #define pr_warn_elf(fmt, ...) \
156*f7c14bbaSAndroid Build Coastguard Worker libbpf_print(LIBBPF_WARN, "libbpf: " fmt ": %s\n", ##__VA_ARGS__, elf_errmsg(-1))
157*f7c14bbaSAndroid Build Coastguard Worker
158*f7c14bbaSAndroid Build Coastguard Worker static int init_output_elf(struct bpf_linker *linker, const char *file);
159*f7c14bbaSAndroid Build Coastguard Worker
160*f7c14bbaSAndroid Build Coastguard Worker static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
161*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_linker_file_opts *opts,
162*f7c14bbaSAndroid Build Coastguard Worker struct src_obj *obj);
163*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf(struct src_obj *obj);
164*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
165*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
166*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_btf(struct src_obj *obj);
167*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_btf_ext(struct src_obj *obj);
168*f7c14bbaSAndroid Build Coastguard Worker static int linker_fixup_btf(struct src_obj *obj);
169*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
170*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
171*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
172*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym, const char *sym_name, int src_sym_idx);
173*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
174*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
175*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
176*f7c14bbaSAndroid Build Coastguard Worker
177*f7c14bbaSAndroid Build Coastguard Worker static int finalize_btf(struct bpf_linker *linker);
178*f7c14bbaSAndroid Build Coastguard Worker static int finalize_btf_ext(struct bpf_linker *linker);
179*f7c14bbaSAndroid Build Coastguard Worker
bpf_linker__free(struct bpf_linker * linker)180*f7c14bbaSAndroid Build Coastguard Worker void bpf_linker__free(struct bpf_linker *linker)
181*f7c14bbaSAndroid Build Coastguard Worker {
182*f7c14bbaSAndroid Build Coastguard Worker int i;
183*f7c14bbaSAndroid Build Coastguard Worker
184*f7c14bbaSAndroid Build Coastguard Worker if (!linker)
185*f7c14bbaSAndroid Build Coastguard Worker return;
186*f7c14bbaSAndroid Build Coastguard Worker
187*f7c14bbaSAndroid Build Coastguard Worker free(linker->filename);
188*f7c14bbaSAndroid Build Coastguard Worker
189*f7c14bbaSAndroid Build Coastguard Worker if (linker->elf)
190*f7c14bbaSAndroid Build Coastguard Worker elf_end(linker->elf);
191*f7c14bbaSAndroid Build Coastguard Worker
192*f7c14bbaSAndroid Build Coastguard Worker if (linker->fd >= 0)
193*f7c14bbaSAndroid Build Coastguard Worker close(linker->fd);
194*f7c14bbaSAndroid Build Coastguard Worker
195*f7c14bbaSAndroid Build Coastguard Worker strset__free(linker->strtab_strs);
196*f7c14bbaSAndroid Build Coastguard Worker
197*f7c14bbaSAndroid Build Coastguard Worker btf__free(linker->btf);
198*f7c14bbaSAndroid Build Coastguard Worker btf_ext__free(linker->btf_ext);
199*f7c14bbaSAndroid Build Coastguard Worker
200*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
201*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
202*f7c14bbaSAndroid Build Coastguard Worker
203*f7c14bbaSAndroid Build Coastguard Worker free(sec->sec_name);
204*f7c14bbaSAndroid Build Coastguard Worker free(sec->raw_data);
205*f7c14bbaSAndroid Build Coastguard Worker free(sec->sec_vars);
206*f7c14bbaSAndroid Build Coastguard Worker
207*f7c14bbaSAndroid Build Coastguard Worker free(sec->func_info.recs);
208*f7c14bbaSAndroid Build Coastguard Worker free(sec->line_info.recs);
209*f7c14bbaSAndroid Build Coastguard Worker free(sec->core_relo_info.recs);
210*f7c14bbaSAndroid Build Coastguard Worker }
211*f7c14bbaSAndroid Build Coastguard Worker free(linker->secs);
212*f7c14bbaSAndroid Build Coastguard Worker
213*f7c14bbaSAndroid Build Coastguard Worker free(linker->glob_syms);
214*f7c14bbaSAndroid Build Coastguard Worker free(linker);
215*f7c14bbaSAndroid Build Coastguard Worker }
216*f7c14bbaSAndroid Build Coastguard Worker
bpf_linker__new(const char * filename,struct bpf_linker_opts * opts)217*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts)
218*f7c14bbaSAndroid Build Coastguard Worker {
219*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker *linker;
220*f7c14bbaSAndroid Build Coastguard Worker int err;
221*f7c14bbaSAndroid Build Coastguard Worker
222*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_linker_opts))
223*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
224*f7c14bbaSAndroid Build Coastguard Worker
225*f7c14bbaSAndroid Build Coastguard Worker if (elf_version(EV_CURRENT) == EV_NONE) {
226*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("libelf initialization failed");
227*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
228*f7c14bbaSAndroid Build Coastguard Worker }
229*f7c14bbaSAndroid Build Coastguard Worker
230*f7c14bbaSAndroid Build Coastguard Worker linker = calloc(1, sizeof(*linker));
231*f7c14bbaSAndroid Build Coastguard Worker if (!linker)
232*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOMEM, NULL;
233*f7c14bbaSAndroid Build Coastguard Worker
234*f7c14bbaSAndroid Build Coastguard Worker linker->fd = -1;
235*f7c14bbaSAndroid Build Coastguard Worker
236*f7c14bbaSAndroid Build Coastguard Worker err = init_output_elf(linker, filename);
237*f7c14bbaSAndroid Build Coastguard Worker if (err)
238*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
239*f7c14bbaSAndroid Build Coastguard Worker
240*f7c14bbaSAndroid Build Coastguard Worker return linker;
241*f7c14bbaSAndroid Build Coastguard Worker
242*f7c14bbaSAndroid Build Coastguard Worker err_out:
243*f7c14bbaSAndroid Build Coastguard Worker bpf_linker__free(linker);
244*f7c14bbaSAndroid Build Coastguard Worker return errno = -err, NULL;
245*f7c14bbaSAndroid Build Coastguard Worker }
246*f7c14bbaSAndroid Build Coastguard Worker
add_dst_sec(struct bpf_linker * linker,const char * sec_name)247*f7c14bbaSAndroid Build Coastguard Worker static struct dst_sec *add_dst_sec(struct bpf_linker *linker, const char *sec_name)
248*f7c14bbaSAndroid Build Coastguard Worker {
249*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *secs = linker->secs, *sec;
250*f7c14bbaSAndroid Build Coastguard Worker size_t new_cnt = linker->sec_cnt ? linker->sec_cnt + 1 : 2;
251*f7c14bbaSAndroid Build Coastguard Worker
252*f7c14bbaSAndroid Build Coastguard Worker secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
253*f7c14bbaSAndroid Build Coastguard Worker if (!secs)
254*f7c14bbaSAndroid Build Coastguard Worker return NULL;
255*f7c14bbaSAndroid Build Coastguard Worker
256*f7c14bbaSAndroid Build Coastguard Worker /* zero out newly allocated memory */
257*f7c14bbaSAndroid Build Coastguard Worker memset(secs + linker->sec_cnt, 0, (new_cnt - linker->sec_cnt) * sizeof(*secs));
258*f7c14bbaSAndroid Build Coastguard Worker
259*f7c14bbaSAndroid Build Coastguard Worker linker->secs = secs;
260*f7c14bbaSAndroid Build Coastguard Worker linker->sec_cnt = new_cnt;
261*f7c14bbaSAndroid Build Coastguard Worker
262*f7c14bbaSAndroid Build Coastguard Worker sec = &linker->secs[new_cnt - 1];
263*f7c14bbaSAndroid Build Coastguard Worker sec->id = new_cnt - 1;
264*f7c14bbaSAndroid Build Coastguard Worker sec->sec_name = strdup(sec_name);
265*f7c14bbaSAndroid Build Coastguard Worker if (!sec->sec_name)
266*f7c14bbaSAndroid Build Coastguard Worker return NULL;
267*f7c14bbaSAndroid Build Coastguard Worker
268*f7c14bbaSAndroid Build Coastguard Worker return sec;
269*f7c14bbaSAndroid Build Coastguard Worker }
270*f7c14bbaSAndroid Build Coastguard Worker
add_new_sym(struct bpf_linker * linker,size_t * sym_idx)271*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *add_new_sym(struct bpf_linker *linker, size_t *sym_idx)
272*f7c14bbaSAndroid Build Coastguard Worker {
273*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *symtab = &linker->secs[linker->symtab_sec_idx];
274*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *syms, *sym;
275*f7c14bbaSAndroid Build Coastguard Worker size_t sym_cnt = symtab->sec_sz / sizeof(*sym);
276*f7c14bbaSAndroid Build Coastguard Worker
277*f7c14bbaSAndroid Build Coastguard Worker syms = libbpf_reallocarray(symtab->raw_data, sym_cnt + 1, sizeof(*sym));
278*f7c14bbaSAndroid Build Coastguard Worker if (!syms)
279*f7c14bbaSAndroid Build Coastguard Worker return NULL;
280*f7c14bbaSAndroid Build Coastguard Worker
281*f7c14bbaSAndroid Build Coastguard Worker sym = &syms[sym_cnt];
282*f7c14bbaSAndroid Build Coastguard Worker memset(sym, 0, sizeof(*sym));
283*f7c14bbaSAndroid Build Coastguard Worker
284*f7c14bbaSAndroid Build Coastguard Worker symtab->raw_data = syms;
285*f7c14bbaSAndroid Build Coastguard Worker symtab->sec_sz += sizeof(*sym);
286*f7c14bbaSAndroid Build Coastguard Worker symtab->shdr->sh_size += sizeof(*sym);
287*f7c14bbaSAndroid Build Coastguard Worker symtab->data->d_size += sizeof(*sym);
288*f7c14bbaSAndroid Build Coastguard Worker
289*f7c14bbaSAndroid Build Coastguard Worker if (sym_idx)
290*f7c14bbaSAndroid Build Coastguard Worker *sym_idx = sym_cnt;
291*f7c14bbaSAndroid Build Coastguard Worker
292*f7c14bbaSAndroid Build Coastguard Worker return sym;
293*f7c14bbaSAndroid Build Coastguard Worker }
294*f7c14bbaSAndroid Build Coastguard Worker
init_output_elf(struct bpf_linker * linker,const char * file)295*f7c14bbaSAndroid Build Coastguard Worker static int init_output_elf(struct bpf_linker *linker, const char *file)
296*f7c14bbaSAndroid Build Coastguard Worker {
297*f7c14bbaSAndroid Build Coastguard Worker int err, str_off;
298*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *init_sym;
299*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec;
300*f7c14bbaSAndroid Build Coastguard Worker
301*f7c14bbaSAndroid Build Coastguard Worker linker->filename = strdup(file);
302*f7c14bbaSAndroid Build Coastguard Worker if (!linker->filename)
303*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
304*f7c14bbaSAndroid Build Coastguard Worker
305*f7c14bbaSAndroid Build Coastguard Worker linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
306*f7c14bbaSAndroid Build Coastguard Worker if (linker->fd < 0) {
307*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
308*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to create '%s': %d\n", file, err);
309*f7c14bbaSAndroid Build Coastguard Worker return err;
310*f7c14bbaSAndroid Build Coastguard Worker }
311*f7c14bbaSAndroid Build Coastguard Worker
312*f7c14bbaSAndroid Build Coastguard Worker linker->elf = elf_begin(linker->fd, ELF_C_WRITE, NULL);
313*f7c14bbaSAndroid Build Coastguard Worker if (!linker->elf) {
314*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create ELF object");
315*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
316*f7c14bbaSAndroid Build Coastguard Worker }
317*f7c14bbaSAndroid Build Coastguard Worker
318*f7c14bbaSAndroid Build Coastguard Worker /* ELF header */
319*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr = elf64_newehdr(linker->elf);
320*f7c14bbaSAndroid Build Coastguard Worker if (!linker->elf_hdr) {
321*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create ELF header");
322*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
323*f7c14bbaSAndroid Build Coastguard Worker }
324*f7c14bbaSAndroid Build Coastguard Worker
325*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr->e_machine = EM_BPF;
326*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr->e_type = ET_REL;
327*f7c14bbaSAndroid Build Coastguard Worker #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
328*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB;
329*f7c14bbaSAndroid Build Coastguard Worker #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
330*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB;
331*f7c14bbaSAndroid Build Coastguard Worker #else
332*f7c14bbaSAndroid Build Coastguard Worker #error "Unknown __BYTE_ORDER__"
333*f7c14bbaSAndroid Build Coastguard Worker #endif
334*f7c14bbaSAndroid Build Coastguard Worker
335*f7c14bbaSAndroid Build Coastguard Worker /* STRTAB */
336*f7c14bbaSAndroid Build Coastguard Worker /* initialize strset with an empty string to conform to ELF */
337*f7c14bbaSAndroid Build Coastguard Worker linker->strtab_strs = strset__new(INT_MAX, "", sizeof(""));
338*f7c14bbaSAndroid Build Coastguard Worker if (libbpf_get_error(linker->strtab_strs))
339*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(linker->strtab_strs);
340*f7c14bbaSAndroid Build Coastguard Worker
341*f7c14bbaSAndroid Build Coastguard Worker sec = add_dst_sec(linker, ".strtab");
342*f7c14bbaSAndroid Build Coastguard Worker if (!sec)
343*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
344*f7c14bbaSAndroid Build Coastguard Worker
345*f7c14bbaSAndroid Build Coastguard Worker sec->scn = elf_newscn(linker->elf);
346*f7c14bbaSAndroid Build Coastguard Worker if (!sec->scn) {
347*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create STRTAB section");
348*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
349*f7c14bbaSAndroid Build Coastguard Worker }
350*f7c14bbaSAndroid Build Coastguard Worker
351*f7c14bbaSAndroid Build Coastguard Worker sec->shdr = elf64_getshdr(sec->scn);
352*f7c14bbaSAndroid Build Coastguard Worker if (!sec->shdr)
353*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
354*f7c14bbaSAndroid Build Coastguard Worker
355*f7c14bbaSAndroid Build Coastguard Worker sec->data = elf_newdata(sec->scn);
356*f7c14bbaSAndroid Build Coastguard Worker if (!sec->data) {
357*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create STRTAB data");
358*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
359*f7c14bbaSAndroid Build Coastguard Worker }
360*f7c14bbaSAndroid Build Coastguard Worker
361*f7c14bbaSAndroid Build Coastguard Worker str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
362*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
363*f7c14bbaSAndroid Build Coastguard Worker return str_off;
364*f7c14bbaSAndroid Build Coastguard Worker
365*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx = elf_ndxscn(sec->scn);
366*f7c14bbaSAndroid Build Coastguard Worker linker->elf_hdr->e_shstrndx = sec->sec_idx;
367*f7c14bbaSAndroid Build Coastguard Worker linker->strtab_sec_idx = sec->sec_idx;
368*f7c14bbaSAndroid Build Coastguard Worker
369*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_name = str_off;
370*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_type = SHT_STRTAB;
371*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_flags = SHF_STRINGS;
372*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_offset = 0;
373*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_link = 0;
374*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_info = 0;
375*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_addralign = 1;
376*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_size = sec->sec_sz = 0;
377*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_entsize = 0;
378*f7c14bbaSAndroid Build Coastguard Worker
379*f7c14bbaSAndroid Build Coastguard Worker /* SYMTAB */
380*f7c14bbaSAndroid Build Coastguard Worker sec = add_dst_sec(linker, ".symtab");
381*f7c14bbaSAndroid Build Coastguard Worker if (!sec)
382*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
383*f7c14bbaSAndroid Build Coastguard Worker
384*f7c14bbaSAndroid Build Coastguard Worker sec->scn = elf_newscn(linker->elf);
385*f7c14bbaSAndroid Build Coastguard Worker if (!sec->scn) {
386*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create SYMTAB section");
387*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
388*f7c14bbaSAndroid Build Coastguard Worker }
389*f7c14bbaSAndroid Build Coastguard Worker
390*f7c14bbaSAndroid Build Coastguard Worker sec->shdr = elf64_getshdr(sec->scn);
391*f7c14bbaSAndroid Build Coastguard Worker if (!sec->shdr)
392*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
393*f7c14bbaSAndroid Build Coastguard Worker
394*f7c14bbaSAndroid Build Coastguard Worker sec->data = elf_newdata(sec->scn);
395*f7c14bbaSAndroid Build Coastguard Worker if (!sec->data) {
396*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to create SYMTAB data");
397*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
398*f7c14bbaSAndroid Build Coastguard Worker }
399*f7c14bbaSAndroid Build Coastguard Worker
400*f7c14bbaSAndroid Build Coastguard Worker str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
401*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
402*f7c14bbaSAndroid Build Coastguard Worker return str_off;
403*f7c14bbaSAndroid Build Coastguard Worker
404*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx = elf_ndxscn(sec->scn);
405*f7c14bbaSAndroid Build Coastguard Worker linker->symtab_sec_idx = sec->sec_idx;
406*f7c14bbaSAndroid Build Coastguard Worker
407*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_name = str_off;
408*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_type = SHT_SYMTAB;
409*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_flags = 0;
410*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_offset = 0;
411*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_link = linker->strtab_sec_idx;
412*f7c14bbaSAndroid Build Coastguard Worker /* sh_info should be one greater than the index of the last local
413*f7c14bbaSAndroid Build Coastguard Worker * symbol (i.e., binding is STB_LOCAL). But why and who cares?
414*f7c14bbaSAndroid Build Coastguard Worker */
415*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_info = 0;
416*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_addralign = 8;
417*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_entsize = sizeof(Elf64_Sym);
418*f7c14bbaSAndroid Build Coastguard Worker
419*f7c14bbaSAndroid Build Coastguard Worker /* .BTF */
420*f7c14bbaSAndroid Build Coastguard Worker linker->btf = btf__new_empty();
421*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(linker->btf);
422*f7c14bbaSAndroid Build Coastguard Worker if (err)
423*f7c14bbaSAndroid Build Coastguard Worker return err;
424*f7c14bbaSAndroid Build Coastguard Worker
425*f7c14bbaSAndroid Build Coastguard Worker /* add the special all-zero symbol */
426*f7c14bbaSAndroid Build Coastguard Worker init_sym = add_new_sym(linker, NULL);
427*f7c14bbaSAndroid Build Coastguard Worker if (!init_sym)
428*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
429*f7c14bbaSAndroid Build Coastguard Worker
430*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_name = 0;
431*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_info = 0;
432*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_other = 0;
433*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_shndx = SHN_UNDEF;
434*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_value = 0;
435*f7c14bbaSAndroid Build Coastguard Worker init_sym->st_size = 0;
436*f7c14bbaSAndroid Build Coastguard Worker
437*f7c14bbaSAndroid Build Coastguard Worker return 0;
438*f7c14bbaSAndroid Build Coastguard Worker }
439*f7c14bbaSAndroid Build Coastguard Worker
bpf_linker__add_file(struct bpf_linker * linker,const char * filename,const struct bpf_linker_file_opts * opts)440*f7c14bbaSAndroid Build Coastguard Worker int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
441*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_linker_file_opts *opts)
442*f7c14bbaSAndroid Build Coastguard Worker {
443*f7c14bbaSAndroid Build Coastguard Worker struct src_obj obj = {};
444*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
445*f7c14bbaSAndroid Build Coastguard Worker
446*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_linker_file_opts))
447*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
448*f7c14bbaSAndroid Build Coastguard Worker
449*f7c14bbaSAndroid Build Coastguard Worker if (!linker->elf)
450*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
451*f7c14bbaSAndroid Build Coastguard Worker
452*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
453*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_append_sec_data(linker, &obj);
454*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_append_elf_syms(linker, &obj);
455*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_append_elf_relos(linker, &obj);
456*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_append_btf(linker, &obj);
457*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_append_btf_ext(linker, &obj);
458*f7c14bbaSAndroid Build Coastguard Worker
459*f7c14bbaSAndroid Build Coastguard Worker /* free up src_obj resources */
460*f7c14bbaSAndroid Build Coastguard Worker free(obj.btf_type_map);
461*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj.btf);
462*f7c14bbaSAndroid Build Coastguard Worker btf_ext__free(obj.btf_ext);
463*f7c14bbaSAndroid Build Coastguard Worker free(obj.secs);
464*f7c14bbaSAndroid Build Coastguard Worker free(obj.sym_map);
465*f7c14bbaSAndroid Build Coastguard Worker if (obj.elf)
466*f7c14bbaSAndroid Build Coastguard Worker elf_end(obj.elf);
467*f7c14bbaSAndroid Build Coastguard Worker if (obj.fd >= 0)
468*f7c14bbaSAndroid Build Coastguard Worker close(obj.fd);
469*f7c14bbaSAndroid Build Coastguard Worker
470*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
471*f7c14bbaSAndroid Build Coastguard Worker }
472*f7c14bbaSAndroid Build Coastguard Worker
is_dwarf_sec_name(const char * name)473*f7c14bbaSAndroid Build Coastguard Worker static bool is_dwarf_sec_name(const char *name)
474*f7c14bbaSAndroid Build Coastguard Worker {
475*f7c14bbaSAndroid Build Coastguard Worker /* approximation, but the actual list is too long */
476*f7c14bbaSAndroid Build Coastguard Worker return strncmp(name, ".debug_", sizeof(".debug_") - 1) == 0;
477*f7c14bbaSAndroid Build Coastguard Worker }
478*f7c14bbaSAndroid Build Coastguard Worker
is_ignored_sec(struct src_sec * sec)479*f7c14bbaSAndroid Build Coastguard Worker static bool is_ignored_sec(struct src_sec *sec)
480*f7c14bbaSAndroid Build Coastguard Worker {
481*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr = sec->shdr;
482*f7c14bbaSAndroid Build Coastguard Worker const char *name = sec->sec_name;
483*f7c14bbaSAndroid Build Coastguard Worker
484*f7c14bbaSAndroid Build Coastguard Worker /* no special handling of .strtab */
485*f7c14bbaSAndroid Build Coastguard Worker if (shdr->sh_type == SHT_STRTAB)
486*f7c14bbaSAndroid Build Coastguard Worker return true;
487*f7c14bbaSAndroid Build Coastguard Worker
488*f7c14bbaSAndroid Build Coastguard Worker /* ignore .llvm_addrsig section as well */
489*f7c14bbaSAndroid Build Coastguard Worker if (shdr->sh_type == SHT_LLVM_ADDRSIG)
490*f7c14bbaSAndroid Build Coastguard Worker return true;
491*f7c14bbaSAndroid Build Coastguard Worker
492*f7c14bbaSAndroid Build Coastguard Worker /* no subprograms will lead to an empty .text section, ignore it */
493*f7c14bbaSAndroid Build Coastguard Worker if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size == 0 &&
494*f7c14bbaSAndroid Build Coastguard Worker strcmp(sec->sec_name, ".text") == 0)
495*f7c14bbaSAndroid Build Coastguard Worker return true;
496*f7c14bbaSAndroid Build Coastguard Worker
497*f7c14bbaSAndroid Build Coastguard Worker /* DWARF sections */
498*f7c14bbaSAndroid Build Coastguard Worker if (is_dwarf_sec_name(sec->sec_name))
499*f7c14bbaSAndroid Build Coastguard Worker return true;
500*f7c14bbaSAndroid Build Coastguard Worker
501*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(name, ".rel", sizeof(".rel") - 1) == 0) {
502*f7c14bbaSAndroid Build Coastguard Worker name += sizeof(".rel") - 1;
503*f7c14bbaSAndroid Build Coastguard Worker /* DWARF section relocations */
504*f7c14bbaSAndroid Build Coastguard Worker if (is_dwarf_sec_name(name))
505*f7c14bbaSAndroid Build Coastguard Worker return true;
506*f7c14bbaSAndroid Build Coastguard Worker
507*f7c14bbaSAndroid Build Coastguard Worker /* .BTF and .BTF.ext don't need relocations */
508*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, BTF_ELF_SEC) == 0 ||
509*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, BTF_EXT_ELF_SEC) == 0)
510*f7c14bbaSAndroid Build Coastguard Worker return true;
511*f7c14bbaSAndroid Build Coastguard Worker }
512*f7c14bbaSAndroid Build Coastguard Worker
513*f7c14bbaSAndroid Build Coastguard Worker return false;
514*f7c14bbaSAndroid Build Coastguard Worker }
515*f7c14bbaSAndroid Build Coastguard Worker
add_src_sec(struct src_obj * obj,const char * sec_name)516*f7c14bbaSAndroid Build Coastguard Worker static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
517*f7c14bbaSAndroid Build Coastguard Worker {
518*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *secs = obj->secs, *sec;
519*f7c14bbaSAndroid Build Coastguard Worker size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2;
520*f7c14bbaSAndroid Build Coastguard Worker
521*f7c14bbaSAndroid Build Coastguard Worker secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
522*f7c14bbaSAndroid Build Coastguard Worker if (!secs)
523*f7c14bbaSAndroid Build Coastguard Worker return NULL;
524*f7c14bbaSAndroid Build Coastguard Worker
525*f7c14bbaSAndroid Build Coastguard Worker /* zero out newly allocated memory */
526*f7c14bbaSAndroid Build Coastguard Worker memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs));
527*f7c14bbaSAndroid Build Coastguard Worker
528*f7c14bbaSAndroid Build Coastguard Worker obj->secs = secs;
529*f7c14bbaSAndroid Build Coastguard Worker obj->sec_cnt = new_cnt;
530*f7c14bbaSAndroid Build Coastguard Worker
531*f7c14bbaSAndroid Build Coastguard Worker sec = &obj->secs[new_cnt - 1];
532*f7c14bbaSAndroid Build Coastguard Worker sec->id = new_cnt - 1;
533*f7c14bbaSAndroid Build Coastguard Worker sec->sec_name = sec_name;
534*f7c14bbaSAndroid Build Coastguard Worker
535*f7c14bbaSAndroid Build Coastguard Worker return sec;
536*f7c14bbaSAndroid Build Coastguard Worker }
537*f7c14bbaSAndroid Build Coastguard Worker
linker_load_obj_file(struct bpf_linker * linker,const char * filename,const struct bpf_linker_file_opts * opts,struct src_obj * obj)538*f7c14bbaSAndroid Build Coastguard Worker static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
539*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_linker_file_opts *opts,
540*f7c14bbaSAndroid Build Coastguard Worker struct src_obj *obj)
541*f7c14bbaSAndroid Build Coastguard Worker {
542*f7c14bbaSAndroid Build Coastguard Worker #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
543*f7c14bbaSAndroid Build Coastguard Worker const int host_endianness = ELFDATA2LSB;
544*f7c14bbaSAndroid Build Coastguard Worker #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
545*f7c14bbaSAndroid Build Coastguard Worker const int host_endianness = ELFDATA2MSB;
546*f7c14bbaSAndroid Build Coastguard Worker #else
547*f7c14bbaSAndroid Build Coastguard Worker #error "Unknown __BYTE_ORDER__"
548*f7c14bbaSAndroid Build Coastguard Worker #endif
549*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
550*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
551*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
552*f7c14bbaSAndroid Build Coastguard Worker Elf64_Ehdr *ehdr;
553*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
554*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *sec;
555*f7c14bbaSAndroid Build Coastguard Worker
556*f7c14bbaSAndroid Build Coastguard Worker pr_debug("linker: adding object file '%s'...\n", filename);
557*f7c14bbaSAndroid Build Coastguard Worker
558*f7c14bbaSAndroid Build Coastguard Worker obj->filename = filename;
559*f7c14bbaSAndroid Build Coastguard Worker
560*f7c14bbaSAndroid Build Coastguard Worker obj->fd = open(filename, O_RDONLY | O_CLOEXEC);
561*f7c14bbaSAndroid Build Coastguard Worker if (obj->fd < 0) {
562*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
563*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open file '%s': %d\n", filename, err);
564*f7c14bbaSAndroid Build Coastguard Worker return err;
565*f7c14bbaSAndroid Build Coastguard Worker }
566*f7c14bbaSAndroid Build Coastguard Worker obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL);
567*f7c14bbaSAndroid Build Coastguard Worker if (!obj->elf) {
568*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
569*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to parse ELF file '%s'", filename);
570*f7c14bbaSAndroid Build Coastguard Worker return err;
571*f7c14bbaSAndroid Build Coastguard Worker }
572*f7c14bbaSAndroid Build Coastguard Worker
573*f7c14bbaSAndroid Build Coastguard Worker /* Sanity check ELF file high-level properties */
574*f7c14bbaSAndroid Build Coastguard Worker ehdr = elf64_getehdr(obj->elf);
575*f7c14bbaSAndroid Build Coastguard Worker if (!ehdr) {
576*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
577*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to get ELF header for %s", filename);
578*f7c14bbaSAndroid Build Coastguard Worker return err;
579*f7c14bbaSAndroid Build Coastguard Worker }
580*f7c14bbaSAndroid Build Coastguard Worker if (ehdr->e_ident[EI_DATA] != host_endianness) {
581*f7c14bbaSAndroid Build Coastguard Worker err = -EOPNOTSUPP;
582*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("unsupported byte order of ELF file %s", filename);
583*f7c14bbaSAndroid Build Coastguard Worker return err;
584*f7c14bbaSAndroid Build Coastguard Worker }
585*f7c14bbaSAndroid Build Coastguard Worker if (ehdr->e_type != ET_REL
586*f7c14bbaSAndroid Build Coastguard Worker || ehdr->e_machine != EM_BPF
587*f7c14bbaSAndroid Build Coastguard Worker || ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
588*f7c14bbaSAndroid Build Coastguard Worker err = -EOPNOTSUPP;
589*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("unsupported kind of ELF file %s", filename);
590*f7c14bbaSAndroid Build Coastguard Worker return err;
591*f7c14bbaSAndroid Build Coastguard Worker }
592*f7c14bbaSAndroid Build Coastguard Worker
593*f7c14bbaSAndroid Build Coastguard Worker if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) {
594*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
595*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to get SHSTRTAB section index for %s", filename);
596*f7c14bbaSAndroid Build Coastguard Worker return err;
597*f7c14bbaSAndroid Build Coastguard Worker }
598*f7c14bbaSAndroid Build Coastguard Worker
599*f7c14bbaSAndroid Build Coastguard Worker scn = NULL;
600*f7c14bbaSAndroid Build Coastguard Worker while ((scn = elf_nextscn(obj->elf, scn)) != NULL) {
601*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx = elf_ndxscn(scn);
602*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
603*f7c14bbaSAndroid Build Coastguard Worker
604*f7c14bbaSAndroid Build Coastguard Worker shdr = elf64_getshdr(scn);
605*f7c14bbaSAndroid Build Coastguard Worker if (!shdr) {
606*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
607*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to get section #%zu header for %s",
608*f7c14bbaSAndroid Build Coastguard Worker sec_idx, filename);
609*f7c14bbaSAndroid Build Coastguard Worker return err;
610*f7c14bbaSAndroid Build Coastguard Worker }
611*f7c14bbaSAndroid Build Coastguard Worker
612*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name);
613*f7c14bbaSAndroid Build Coastguard Worker if (!sec_name) {
614*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
615*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to get section #%zu name for %s",
616*f7c14bbaSAndroid Build Coastguard Worker sec_idx, filename);
617*f7c14bbaSAndroid Build Coastguard Worker return err;
618*f7c14bbaSAndroid Build Coastguard Worker }
619*f7c14bbaSAndroid Build Coastguard Worker
620*f7c14bbaSAndroid Build Coastguard Worker data = elf_getdata(scn, 0);
621*f7c14bbaSAndroid Build Coastguard Worker if (!data) {
622*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
623*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to get section #%zu (%s) data from %s",
624*f7c14bbaSAndroid Build Coastguard Worker sec_idx, sec_name, filename);
625*f7c14bbaSAndroid Build Coastguard Worker return err;
626*f7c14bbaSAndroid Build Coastguard Worker }
627*f7c14bbaSAndroid Build Coastguard Worker
628*f7c14bbaSAndroid Build Coastguard Worker sec = add_src_sec(obj, sec_name);
629*f7c14bbaSAndroid Build Coastguard Worker if (!sec)
630*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
631*f7c14bbaSAndroid Build Coastguard Worker
632*f7c14bbaSAndroid Build Coastguard Worker sec->scn = scn;
633*f7c14bbaSAndroid Build Coastguard Worker sec->shdr = shdr;
634*f7c14bbaSAndroid Build Coastguard Worker sec->data = data;
635*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx = elf_ndxscn(scn);
636*f7c14bbaSAndroid Build Coastguard Worker
637*f7c14bbaSAndroid Build Coastguard Worker if (is_ignored_sec(sec)) {
638*f7c14bbaSAndroid Build Coastguard Worker sec->skipped = true;
639*f7c14bbaSAndroid Build Coastguard Worker continue;
640*f7c14bbaSAndroid Build Coastguard Worker }
641*f7c14bbaSAndroid Build Coastguard Worker
642*f7c14bbaSAndroid Build Coastguard Worker switch (shdr->sh_type) {
643*f7c14bbaSAndroid Build Coastguard Worker case SHT_SYMTAB:
644*f7c14bbaSAndroid Build Coastguard Worker if (obj->symtab_sec_idx) {
645*f7c14bbaSAndroid Build Coastguard Worker err = -EOPNOTSUPP;
646*f7c14bbaSAndroid Build Coastguard Worker pr_warn("multiple SYMTAB sections found, not supported\n");
647*f7c14bbaSAndroid Build Coastguard Worker return err;
648*f7c14bbaSAndroid Build Coastguard Worker }
649*f7c14bbaSAndroid Build Coastguard Worker obj->symtab_sec_idx = sec_idx;
650*f7c14bbaSAndroid Build Coastguard Worker break;
651*f7c14bbaSAndroid Build Coastguard Worker case SHT_STRTAB:
652*f7c14bbaSAndroid Build Coastguard Worker /* we'll construct our own string table */
653*f7c14bbaSAndroid Build Coastguard Worker break;
654*f7c14bbaSAndroid Build Coastguard Worker case SHT_PROGBITS:
655*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, BTF_ELF_SEC) == 0) {
656*f7c14bbaSAndroid Build Coastguard Worker obj->btf = btf__new(data->d_buf, shdr->sh_size);
657*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf);
658*f7c14bbaSAndroid Build Coastguard Worker if (err) {
659*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse .BTF from %s: %d\n", filename, err);
660*f7c14bbaSAndroid Build Coastguard Worker return err;
661*f7c14bbaSAndroid Build Coastguard Worker }
662*f7c14bbaSAndroid Build Coastguard Worker sec->skipped = true;
663*f7c14bbaSAndroid Build Coastguard Worker continue;
664*f7c14bbaSAndroid Build Coastguard Worker }
665*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, BTF_EXT_ELF_SEC) == 0) {
666*f7c14bbaSAndroid Build Coastguard Worker obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size);
667*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf_ext);
668*f7c14bbaSAndroid Build Coastguard Worker if (err) {
669*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse .BTF.ext from '%s': %d\n", filename, err);
670*f7c14bbaSAndroid Build Coastguard Worker return err;
671*f7c14bbaSAndroid Build Coastguard Worker }
672*f7c14bbaSAndroid Build Coastguard Worker sec->skipped = true;
673*f7c14bbaSAndroid Build Coastguard Worker continue;
674*f7c14bbaSAndroid Build Coastguard Worker }
675*f7c14bbaSAndroid Build Coastguard Worker
676*f7c14bbaSAndroid Build Coastguard Worker /* data & code */
677*f7c14bbaSAndroid Build Coastguard Worker break;
678*f7c14bbaSAndroid Build Coastguard Worker case SHT_NOBITS:
679*f7c14bbaSAndroid Build Coastguard Worker /* BSS */
680*f7c14bbaSAndroid Build Coastguard Worker break;
681*f7c14bbaSAndroid Build Coastguard Worker case SHT_REL:
682*f7c14bbaSAndroid Build Coastguard Worker /* relocations */
683*f7c14bbaSAndroid Build Coastguard Worker break;
684*f7c14bbaSAndroid Build Coastguard Worker default:
685*f7c14bbaSAndroid Build Coastguard Worker pr_warn("unrecognized section #%zu (%s) in %s\n",
686*f7c14bbaSAndroid Build Coastguard Worker sec_idx, sec_name, filename);
687*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
688*f7c14bbaSAndroid Build Coastguard Worker return err;
689*f7c14bbaSAndroid Build Coastguard Worker }
690*f7c14bbaSAndroid Build Coastguard Worker }
691*f7c14bbaSAndroid Build Coastguard Worker
692*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_sanity_check_elf(obj);
693*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_sanity_check_btf(obj);
694*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_sanity_check_btf_ext(obj);
695*f7c14bbaSAndroid Build Coastguard Worker err = err ?: linker_fixup_btf(obj);
696*f7c14bbaSAndroid Build Coastguard Worker
697*f7c14bbaSAndroid Build Coastguard Worker return err;
698*f7c14bbaSAndroid Build Coastguard Worker }
699*f7c14bbaSAndroid Build Coastguard Worker
linker_sanity_check_elf(struct src_obj * obj)700*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf(struct src_obj *obj)
701*f7c14bbaSAndroid Build Coastguard Worker {
702*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *sec;
703*f7c14bbaSAndroid Build Coastguard Worker int i, err;
704*f7c14bbaSAndroid Build Coastguard Worker
705*f7c14bbaSAndroid Build Coastguard Worker if (!obj->symtab_sec_idx) {
706*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename);
707*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
708*f7c14bbaSAndroid Build Coastguard Worker }
709*f7c14bbaSAndroid Build Coastguard Worker if (!obj->shstrs_sec_idx) {
710*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename);
711*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
712*f7c14bbaSAndroid Build Coastguard Worker }
713*f7c14bbaSAndroid Build Coastguard Worker
714*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < obj->sec_cnt; i++) {
715*f7c14bbaSAndroid Build Coastguard Worker sec = &obj->secs[i];
716*f7c14bbaSAndroid Build Coastguard Worker
717*f7c14bbaSAndroid Build Coastguard Worker if (sec->sec_name[0] == '\0') {
718*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename);
719*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
720*f7c14bbaSAndroid Build Coastguard Worker }
721*f7c14bbaSAndroid Build Coastguard Worker
722*f7c14bbaSAndroid Build Coastguard Worker if (is_dwarf_sec_name(sec->sec_name))
723*f7c14bbaSAndroid Build Coastguard Worker continue;
724*f7c14bbaSAndroid Build Coastguard Worker
725*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
726*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu alignment %llu is non pow-of-2 alignment in %s\n",
727*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
728*f7c14bbaSAndroid Build Coastguard Worker obj->filename);
729*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
730*f7c14bbaSAndroid Build Coastguard Worker }
731*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_addralign != sec->data->d_align) {
732*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu has inconsistent alignment addr=%llu != d=%llu in %s\n",
733*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
734*f7c14bbaSAndroid Build Coastguard Worker (long long unsigned)sec->data->d_align, obj->filename);
735*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
736*f7c14bbaSAndroid Build Coastguard Worker }
737*f7c14bbaSAndroid Build Coastguard Worker
738*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_size != sec->data->d_size) {
739*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu has inconsistent section size sh=%llu != d=%llu in %s\n",
740*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
741*f7c14bbaSAndroid Build Coastguard Worker (long long unsigned)sec->data->d_size, obj->filename);
742*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
743*f7c14bbaSAndroid Build Coastguard Worker }
744*f7c14bbaSAndroid Build Coastguard Worker
745*f7c14bbaSAndroid Build Coastguard Worker switch (sec->shdr->sh_type) {
746*f7c14bbaSAndroid Build Coastguard Worker case SHT_SYMTAB:
747*f7c14bbaSAndroid Build Coastguard Worker err = linker_sanity_check_elf_symtab(obj, sec);
748*f7c14bbaSAndroid Build Coastguard Worker if (err)
749*f7c14bbaSAndroid Build Coastguard Worker return err;
750*f7c14bbaSAndroid Build Coastguard Worker break;
751*f7c14bbaSAndroid Build Coastguard Worker case SHT_STRTAB:
752*f7c14bbaSAndroid Build Coastguard Worker break;
753*f7c14bbaSAndroid Build Coastguard Worker case SHT_PROGBITS:
754*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_flags & SHF_EXECINSTR) {
755*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) {
756*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu has unexpected size alignment %llu in %s\n",
757*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
758*f7c14bbaSAndroid Build Coastguard Worker obj->filename);
759*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
760*f7c14bbaSAndroid Build Coastguard Worker }
761*f7c14bbaSAndroid Build Coastguard Worker }
762*f7c14bbaSAndroid Build Coastguard Worker break;
763*f7c14bbaSAndroid Build Coastguard Worker case SHT_NOBITS:
764*f7c14bbaSAndroid Build Coastguard Worker break;
765*f7c14bbaSAndroid Build Coastguard Worker case SHT_REL:
766*f7c14bbaSAndroid Build Coastguard Worker err = linker_sanity_check_elf_relos(obj, sec);
767*f7c14bbaSAndroid Build Coastguard Worker if (err)
768*f7c14bbaSAndroid Build Coastguard Worker return err;
769*f7c14bbaSAndroid Build Coastguard Worker break;
770*f7c14bbaSAndroid Build Coastguard Worker case SHT_LLVM_ADDRSIG:
771*f7c14bbaSAndroid Build Coastguard Worker break;
772*f7c14bbaSAndroid Build Coastguard Worker default:
773*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF section #%zu (%s) has unrecognized type %zu in %s\n",
774*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename);
775*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
776*f7c14bbaSAndroid Build Coastguard Worker }
777*f7c14bbaSAndroid Build Coastguard Worker }
778*f7c14bbaSAndroid Build Coastguard Worker
779*f7c14bbaSAndroid Build Coastguard Worker return 0;
780*f7c14bbaSAndroid Build Coastguard Worker }
781*f7c14bbaSAndroid Build Coastguard Worker
linker_sanity_check_elf_symtab(struct src_obj * obj,struct src_sec * sec)782*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec)
783*f7c14bbaSAndroid Build Coastguard Worker {
784*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *link_sec;
785*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
786*f7c14bbaSAndroid Build Coastguard Worker int i, n;
787*f7c14bbaSAndroid Build Coastguard Worker
788*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_entsize != sizeof(Elf64_Sym))
789*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
790*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
791*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
792*f7c14bbaSAndroid Build Coastguard Worker
793*f7c14bbaSAndroid Build Coastguard Worker if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) {
794*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF SYMTAB section #%zu points to missing STRTAB section #%zu in %s\n",
795*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
796*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
797*f7c14bbaSAndroid Build Coastguard Worker }
798*f7c14bbaSAndroid Build Coastguard Worker link_sec = &obj->secs[sec->shdr->sh_link];
799*f7c14bbaSAndroid Build Coastguard Worker if (link_sec->shdr->sh_type != SHT_STRTAB) {
800*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF SYMTAB section #%zu points to invalid STRTAB section #%zu in %s\n",
801*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
802*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
803*f7c14bbaSAndroid Build Coastguard Worker }
804*f7c14bbaSAndroid Build Coastguard Worker
805*f7c14bbaSAndroid Build Coastguard Worker n = sec->shdr->sh_size / sec->shdr->sh_entsize;
806*f7c14bbaSAndroid Build Coastguard Worker sym = sec->data->d_buf;
807*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, sym++) {
808*f7c14bbaSAndroid Build Coastguard Worker int sym_type = ELF64_ST_TYPE(sym->st_info);
809*f7c14bbaSAndroid Build Coastguard Worker int sym_bind = ELF64_ST_BIND(sym->st_info);
810*f7c14bbaSAndroid Build Coastguard Worker int sym_vis = ELF64_ST_VISIBILITY(sym->st_other);
811*f7c14bbaSAndroid Build Coastguard Worker
812*f7c14bbaSAndroid Build Coastguard Worker if (i == 0) {
813*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_name != 0 || sym->st_info != 0
814*f7c14bbaSAndroid Build Coastguard Worker || sym->st_other != 0 || sym->st_shndx != 0
815*f7c14bbaSAndroid Build Coastguard Worker || sym->st_value != 0 || sym->st_size != 0) {
816*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sym #0 is invalid in %s\n", obj->filename);
817*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
818*f7c14bbaSAndroid Build Coastguard Worker }
819*f7c14bbaSAndroid Build Coastguard Worker continue;
820*f7c14bbaSAndroid Build Coastguard Worker }
821*f7c14bbaSAndroid Build Coastguard Worker if (sym_bind != STB_LOCAL && sym_bind != STB_GLOBAL && sym_bind != STB_WEAK) {
822*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sym #%d in section #%zu has unsupported symbol binding %d\n",
823*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, sym_bind);
824*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
825*f7c14bbaSAndroid Build Coastguard Worker }
826*f7c14bbaSAndroid Build Coastguard Worker if (sym_vis != STV_DEFAULT && sym_vis != STV_HIDDEN) {
827*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sym #%d in section #%zu has unsupported symbol visibility %d\n",
828*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, sym_vis);
829*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
830*f7c14bbaSAndroid Build Coastguard Worker }
831*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx == 0) {
832*f7c14bbaSAndroid Build Coastguard Worker if (sym_type != STT_NOTYPE || sym_bind == STB_LOCAL
833*f7c14bbaSAndroid Build Coastguard Worker || sym->st_value != 0 || sym->st_size != 0) {
834*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sym #%d is invalid extern symbol in %s\n",
835*f7c14bbaSAndroid Build Coastguard Worker i, obj->filename);
836*f7c14bbaSAndroid Build Coastguard Worker
837*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
838*f7c14bbaSAndroid Build Coastguard Worker }
839*f7c14bbaSAndroid Build Coastguard Worker continue;
840*f7c14bbaSAndroid Build Coastguard Worker }
841*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx < SHN_LORESERVE && sym->st_shndx >= obj->sec_cnt) {
842*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sym #%d in section #%zu points to missing section #%zu in %s\n",
843*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename);
844*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
845*f7c14bbaSAndroid Build Coastguard Worker }
846*f7c14bbaSAndroid Build Coastguard Worker if (sym_type == STT_SECTION) {
847*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_value != 0)
848*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
849*f7c14bbaSAndroid Build Coastguard Worker continue;
850*f7c14bbaSAndroid Build Coastguard Worker }
851*f7c14bbaSAndroid Build Coastguard Worker }
852*f7c14bbaSAndroid Build Coastguard Worker
853*f7c14bbaSAndroid Build Coastguard Worker return 0;
854*f7c14bbaSAndroid Build Coastguard Worker }
855*f7c14bbaSAndroid Build Coastguard Worker
linker_sanity_check_elf_relos(struct src_obj * obj,struct src_sec * sec)856*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec)
857*f7c14bbaSAndroid Build Coastguard Worker {
858*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *link_sec, *sym_sec;
859*f7c14bbaSAndroid Build Coastguard Worker Elf64_Rel *relo;
860*f7c14bbaSAndroid Build Coastguard Worker int i, n;
861*f7c14bbaSAndroid Build Coastguard Worker
862*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_entsize != sizeof(Elf64_Rel))
863*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
864*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
865*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
866*f7c14bbaSAndroid Build Coastguard Worker
867*f7c14bbaSAndroid Build Coastguard Worker /* SHT_REL's sh_link should point to SYMTAB */
868*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr->sh_link != obj->symtab_sec_idx) {
869*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo section #%zu points to invalid SYMTAB section #%zu in %s\n",
870*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
871*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
872*f7c14bbaSAndroid Build Coastguard Worker }
873*f7c14bbaSAndroid Build Coastguard Worker
874*f7c14bbaSAndroid Build Coastguard Worker /* SHT_REL's sh_info points to relocated section */
875*f7c14bbaSAndroid Build Coastguard Worker if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) {
876*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo section #%zu points to missing section #%zu in %s\n",
877*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
878*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
879*f7c14bbaSAndroid Build Coastguard Worker }
880*f7c14bbaSAndroid Build Coastguard Worker link_sec = &obj->secs[sec->shdr->sh_info];
881*f7c14bbaSAndroid Build Coastguard Worker
882*f7c14bbaSAndroid Build Coastguard Worker /* .rel<secname> -> <secname> pattern is followed */
883*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(sec->sec_name, ".rel", sizeof(".rel") - 1) != 0
884*f7c14bbaSAndroid Build Coastguard Worker || strcmp(sec->sec_name + sizeof(".rel") - 1, link_sec->sec_name) != 0) {
885*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo section #%zu name has invalid name in %s\n",
886*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, obj->filename);
887*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
888*f7c14bbaSAndroid Build Coastguard Worker }
889*f7c14bbaSAndroid Build Coastguard Worker
890*f7c14bbaSAndroid Build Coastguard Worker /* don't further validate relocations for ignored sections */
891*f7c14bbaSAndroid Build Coastguard Worker if (link_sec->skipped)
892*f7c14bbaSAndroid Build Coastguard Worker return 0;
893*f7c14bbaSAndroid Build Coastguard Worker
894*f7c14bbaSAndroid Build Coastguard Worker /* relocatable section is data or instructions */
895*f7c14bbaSAndroid Build Coastguard Worker if (link_sec->shdr->sh_type != SHT_PROGBITS && link_sec->shdr->sh_type != SHT_NOBITS) {
896*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo section #%zu points to invalid section #%zu in %s\n",
897*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
898*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
899*f7c14bbaSAndroid Build Coastguard Worker }
900*f7c14bbaSAndroid Build Coastguard Worker
901*f7c14bbaSAndroid Build Coastguard Worker /* check sanity of each relocation */
902*f7c14bbaSAndroid Build Coastguard Worker n = sec->shdr->sh_size / sec->shdr->sh_entsize;
903*f7c14bbaSAndroid Build Coastguard Worker relo = sec->data->d_buf;
904*f7c14bbaSAndroid Build Coastguard Worker sym_sec = &obj->secs[obj->symtab_sec_idx];
905*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, relo++) {
906*f7c14bbaSAndroid Build Coastguard Worker size_t sym_idx = ELF64_R_SYM(relo->r_info);
907*f7c14bbaSAndroid Build Coastguard Worker size_t sym_type = ELF64_R_TYPE(relo->r_info);
908*f7c14bbaSAndroid Build Coastguard Worker
909*f7c14bbaSAndroid Build Coastguard Worker if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32 &&
910*f7c14bbaSAndroid Build Coastguard Worker sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32) {
911*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
912*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, sym_type, obj->filename);
913*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
914*f7c14bbaSAndroid Build Coastguard Worker }
915*f7c14bbaSAndroid Build Coastguard Worker
916*f7c14bbaSAndroid Build Coastguard Worker if (!sym_idx || sym_idx * sizeof(Elf64_Sym) >= sym_sec->shdr->sh_size) {
917*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo #%d in section #%zu points to invalid symbol #%zu in %s\n",
918*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, sym_idx, obj->filename);
919*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
920*f7c14bbaSAndroid Build Coastguard Worker }
921*f7c14bbaSAndroid Build Coastguard Worker
922*f7c14bbaSAndroid Build Coastguard Worker if (link_sec->shdr->sh_flags & SHF_EXECINSTR) {
923*f7c14bbaSAndroid Build Coastguard Worker if (relo->r_offset % sizeof(struct bpf_insn) != 0) {
924*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF relo #%d in section #%zu points to missing symbol #%zu in %s\n",
925*f7c14bbaSAndroid Build Coastguard Worker i, sec->sec_idx, sym_idx, obj->filename);
926*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
927*f7c14bbaSAndroid Build Coastguard Worker }
928*f7c14bbaSAndroid Build Coastguard Worker }
929*f7c14bbaSAndroid Build Coastguard Worker }
930*f7c14bbaSAndroid Build Coastguard Worker
931*f7c14bbaSAndroid Build Coastguard Worker return 0;
932*f7c14bbaSAndroid Build Coastguard Worker }
933*f7c14bbaSAndroid Build Coastguard Worker
check_btf_type_id(__u32 * type_id,void * ctx)934*f7c14bbaSAndroid Build Coastguard Worker static int check_btf_type_id(__u32 *type_id, void *ctx)
935*f7c14bbaSAndroid Build Coastguard Worker {
936*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf = ctx;
937*f7c14bbaSAndroid Build Coastguard Worker
938*f7c14bbaSAndroid Build Coastguard Worker if (*type_id >= btf__type_cnt(btf))
939*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
940*f7c14bbaSAndroid Build Coastguard Worker
941*f7c14bbaSAndroid Build Coastguard Worker return 0;
942*f7c14bbaSAndroid Build Coastguard Worker }
943*f7c14bbaSAndroid Build Coastguard Worker
check_btf_str_off(__u32 * str_off,void * ctx)944*f7c14bbaSAndroid Build Coastguard Worker static int check_btf_str_off(__u32 *str_off, void *ctx)
945*f7c14bbaSAndroid Build Coastguard Worker {
946*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf = ctx;
947*f7c14bbaSAndroid Build Coastguard Worker const char *s;
948*f7c14bbaSAndroid Build Coastguard Worker
949*f7c14bbaSAndroid Build Coastguard Worker s = btf__str_by_offset(btf, *str_off);
950*f7c14bbaSAndroid Build Coastguard Worker
951*f7c14bbaSAndroid Build Coastguard Worker if (!s)
952*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
953*f7c14bbaSAndroid Build Coastguard Worker
954*f7c14bbaSAndroid Build Coastguard Worker return 0;
955*f7c14bbaSAndroid Build Coastguard Worker }
956*f7c14bbaSAndroid Build Coastguard Worker
linker_sanity_check_btf(struct src_obj * obj)957*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_btf(struct src_obj *obj)
958*f7c14bbaSAndroid Build Coastguard Worker {
959*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t;
960*f7c14bbaSAndroid Build Coastguard Worker int i, n, err = 0;
961*f7c14bbaSAndroid Build Coastguard Worker
962*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
963*f7c14bbaSAndroid Build Coastguard Worker return 0;
964*f7c14bbaSAndroid Build Coastguard Worker
965*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
966*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
967*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(obj->btf, i);
968*f7c14bbaSAndroid Build Coastguard Worker
969*f7c14bbaSAndroid Build Coastguard Worker err = err ?: btf_type_visit_type_ids(t, check_btf_type_id, obj->btf);
970*f7c14bbaSAndroid Build Coastguard Worker err = err ?: btf_type_visit_str_offs(t, check_btf_str_off, obj->btf);
971*f7c14bbaSAndroid Build Coastguard Worker if (err)
972*f7c14bbaSAndroid Build Coastguard Worker return err;
973*f7c14bbaSAndroid Build Coastguard Worker }
974*f7c14bbaSAndroid Build Coastguard Worker
975*f7c14bbaSAndroid Build Coastguard Worker return 0;
976*f7c14bbaSAndroid Build Coastguard Worker }
977*f7c14bbaSAndroid Build Coastguard Worker
linker_sanity_check_btf_ext(struct src_obj * obj)978*f7c14bbaSAndroid Build Coastguard Worker static int linker_sanity_check_btf_ext(struct src_obj *obj)
979*f7c14bbaSAndroid Build Coastguard Worker {
980*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
981*f7c14bbaSAndroid Build Coastguard Worker
982*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_ext)
983*f7c14bbaSAndroid Build Coastguard Worker return 0;
984*f7c14bbaSAndroid Build Coastguard Worker
985*f7c14bbaSAndroid Build Coastguard Worker /* can't use .BTF.ext without .BTF */
986*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
987*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
988*f7c14bbaSAndroid Build Coastguard Worker
989*f7c14bbaSAndroid Build Coastguard Worker err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf);
990*f7c14bbaSAndroid Build Coastguard Worker err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf);
991*f7c14bbaSAndroid Build Coastguard Worker if (err)
992*f7c14bbaSAndroid Build Coastguard Worker return err;
993*f7c14bbaSAndroid Build Coastguard Worker
994*f7c14bbaSAndroid Build Coastguard Worker return 0;
995*f7c14bbaSAndroid Build Coastguard Worker }
996*f7c14bbaSAndroid Build Coastguard Worker
init_sec(struct bpf_linker * linker,struct dst_sec * dst_sec,struct src_sec * src_sec)997*f7c14bbaSAndroid Build Coastguard Worker static int init_sec(struct bpf_linker *linker, struct dst_sec *dst_sec, struct src_sec *src_sec)
998*f7c14bbaSAndroid Build Coastguard Worker {
999*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
1000*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
1001*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
1002*f7c14bbaSAndroid Build Coastguard Worker int name_off;
1003*f7c14bbaSAndroid Build Coastguard Worker
1004*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_sz = 0;
1005*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_idx = 0;
1006*f7c14bbaSAndroid Build Coastguard Worker dst_sec->ephemeral = src_sec->ephemeral;
1007*f7c14bbaSAndroid Build Coastguard Worker
1008*f7c14bbaSAndroid Build Coastguard Worker /* ephemeral sections are just thin section shells lacking most parts */
1009*f7c14bbaSAndroid Build Coastguard Worker if (src_sec->ephemeral)
1010*f7c14bbaSAndroid Build Coastguard Worker return 0;
1011*f7c14bbaSAndroid Build Coastguard Worker
1012*f7c14bbaSAndroid Build Coastguard Worker scn = elf_newscn(linker->elf);
1013*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
1014*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1015*f7c14bbaSAndroid Build Coastguard Worker data = elf_newdata(scn);
1016*f7c14bbaSAndroid Build Coastguard Worker if (!data)
1017*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1018*f7c14bbaSAndroid Build Coastguard Worker shdr = elf64_getshdr(scn);
1019*f7c14bbaSAndroid Build Coastguard Worker if (!shdr)
1020*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1021*f7c14bbaSAndroid Build Coastguard Worker
1022*f7c14bbaSAndroid Build Coastguard Worker dst_sec->scn = scn;
1023*f7c14bbaSAndroid Build Coastguard Worker dst_sec->shdr = shdr;
1024*f7c14bbaSAndroid Build Coastguard Worker dst_sec->data = data;
1025*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_idx = elf_ndxscn(scn);
1026*f7c14bbaSAndroid Build Coastguard Worker
1027*f7c14bbaSAndroid Build Coastguard Worker name_off = strset__add_str(linker->strtab_strs, src_sec->sec_name);
1028*f7c14bbaSAndroid Build Coastguard Worker if (name_off < 0)
1029*f7c14bbaSAndroid Build Coastguard Worker return name_off;
1030*f7c14bbaSAndroid Build Coastguard Worker
1031*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_name = name_off;
1032*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_type = src_sec->shdr->sh_type;
1033*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_flags = src_sec->shdr->sh_flags;
1034*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_size = 0;
1035*f7c14bbaSAndroid Build Coastguard Worker /* sh_link and sh_info have different meaning for different types of
1036*f7c14bbaSAndroid Build Coastguard Worker * sections, so we leave it up to the caller code to fill them in, if
1037*f7c14bbaSAndroid Build Coastguard Worker * necessary
1038*f7c14bbaSAndroid Build Coastguard Worker */
1039*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_link = 0;
1040*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_info = 0;
1041*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_addralign = src_sec->shdr->sh_addralign;
1042*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_entsize = src_sec->shdr->sh_entsize;
1043*f7c14bbaSAndroid Build Coastguard Worker
1044*f7c14bbaSAndroid Build Coastguard Worker data->d_type = src_sec->data->d_type;
1045*f7c14bbaSAndroid Build Coastguard Worker data->d_size = 0;
1046*f7c14bbaSAndroid Build Coastguard Worker data->d_buf = NULL;
1047*f7c14bbaSAndroid Build Coastguard Worker data->d_align = src_sec->data->d_align;
1048*f7c14bbaSAndroid Build Coastguard Worker data->d_off = 0;
1049*f7c14bbaSAndroid Build Coastguard Worker
1050*f7c14bbaSAndroid Build Coastguard Worker return 0;
1051*f7c14bbaSAndroid Build Coastguard Worker }
1052*f7c14bbaSAndroid Build Coastguard Worker
find_dst_sec_by_name(struct bpf_linker * linker,const char * sec_name)1053*f7c14bbaSAndroid Build Coastguard Worker static struct dst_sec *find_dst_sec_by_name(struct bpf_linker *linker, const char *sec_name)
1054*f7c14bbaSAndroid Build Coastguard Worker {
1055*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec;
1056*f7c14bbaSAndroid Build Coastguard Worker int i;
1057*f7c14bbaSAndroid Build Coastguard Worker
1058*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
1059*f7c14bbaSAndroid Build Coastguard Worker sec = &linker->secs[i];
1060*f7c14bbaSAndroid Build Coastguard Worker
1061*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec->sec_name, sec_name) == 0)
1062*f7c14bbaSAndroid Build Coastguard Worker return sec;
1063*f7c14bbaSAndroid Build Coastguard Worker }
1064*f7c14bbaSAndroid Build Coastguard Worker
1065*f7c14bbaSAndroid Build Coastguard Worker return NULL;
1066*f7c14bbaSAndroid Build Coastguard Worker }
1067*f7c14bbaSAndroid Build Coastguard Worker
secs_match(struct dst_sec * dst,struct src_sec * src)1068*f7c14bbaSAndroid Build Coastguard Worker static bool secs_match(struct dst_sec *dst, struct src_sec *src)
1069*f7c14bbaSAndroid Build Coastguard Worker {
1070*f7c14bbaSAndroid Build Coastguard Worker if (dst->ephemeral || src->ephemeral)
1071*f7c14bbaSAndroid Build Coastguard Worker return true;
1072*f7c14bbaSAndroid Build Coastguard Worker
1073*f7c14bbaSAndroid Build Coastguard Worker if (dst->shdr->sh_type != src->shdr->sh_type) {
1074*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec %s types mismatch\n", dst->sec_name);
1075*f7c14bbaSAndroid Build Coastguard Worker return false;
1076*f7c14bbaSAndroid Build Coastguard Worker }
1077*f7c14bbaSAndroid Build Coastguard Worker if (dst->shdr->sh_flags != src->shdr->sh_flags) {
1078*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec %s flags mismatch\n", dst->sec_name);
1079*f7c14bbaSAndroid Build Coastguard Worker return false;
1080*f7c14bbaSAndroid Build Coastguard Worker }
1081*f7c14bbaSAndroid Build Coastguard Worker if (dst->shdr->sh_entsize != src->shdr->sh_entsize) {
1082*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec %s entsize mismatch\n", dst->sec_name);
1083*f7c14bbaSAndroid Build Coastguard Worker return false;
1084*f7c14bbaSAndroid Build Coastguard Worker }
1085*f7c14bbaSAndroid Build Coastguard Worker
1086*f7c14bbaSAndroid Build Coastguard Worker return true;
1087*f7c14bbaSAndroid Build Coastguard Worker }
1088*f7c14bbaSAndroid Build Coastguard Worker
sec_content_is_same(struct dst_sec * dst_sec,struct src_sec * src_sec)1089*f7c14bbaSAndroid Build Coastguard Worker static bool sec_content_is_same(struct dst_sec *dst_sec, struct src_sec *src_sec)
1090*f7c14bbaSAndroid Build Coastguard Worker {
1091*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->sec_sz != src_sec->shdr->sh_size)
1092*f7c14bbaSAndroid Build Coastguard Worker return false;
1093*f7c14bbaSAndroid Build Coastguard Worker if (memcmp(dst_sec->raw_data, src_sec->data->d_buf, dst_sec->sec_sz) != 0)
1094*f7c14bbaSAndroid Build Coastguard Worker return false;
1095*f7c14bbaSAndroid Build Coastguard Worker return true;
1096*f7c14bbaSAndroid Build Coastguard Worker }
1097*f7c14bbaSAndroid Build Coastguard Worker
extend_sec(struct bpf_linker * linker,struct dst_sec * dst,struct src_sec * src)1098*f7c14bbaSAndroid Build Coastguard Worker static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src)
1099*f7c14bbaSAndroid Build Coastguard Worker {
1100*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
1101*f7c14bbaSAndroid Build Coastguard Worker size_t dst_align, src_align;
1102*f7c14bbaSAndroid Build Coastguard Worker size_t dst_align_sz, dst_final_sz;
1103*f7c14bbaSAndroid Build Coastguard Worker int err;
1104*f7c14bbaSAndroid Build Coastguard Worker
1105*f7c14bbaSAndroid Build Coastguard Worker /* Ephemeral source section doesn't contribute anything to ELF
1106*f7c14bbaSAndroid Build Coastguard Worker * section data.
1107*f7c14bbaSAndroid Build Coastguard Worker */
1108*f7c14bbaSAndroid Build Coastguard Worker if (src->ephemeral)
1109*f7c14bbaSAndroid Build Coastguard Worker return 0;
1110*f7c14bbaSAndroid Build Coastguard Worker
1111*f7c14bbaSAndroid Build Coastguard Worker /* Some sections (like .maps) can contain both externs (and thus be
1112*f7c14bbaSAndroid Build Coastguard Worker * ephemeral) and non-externs (map definitions). So it's possible that
1113*f7c14bbaSAndroid Build Coastguard Worker * it has to be "upgraded" from ephemeral to non-ephemeral when the
1114*f7c14bbaSAndroid Build Coastguard Worker * first non-ephemeral entity appears. In such case, we add ELF
1115*f7c14bbaSAndroid Build Coastguard Worker * section, data, etc.
1116*f7c14bbaSAndroid Build Coastguard Worker */
1117*f7c14bbaSAndroid Build Coastguard Worker if (dst->ephemeral) {
1118*f7c14bbaSAndroid Build Coastguard Worker err = init_sec(linker, dst, src);
1119*f7c14bbaSAndroid Build Coastguard Worker if (err)
1120*f7c14bbaSAndroid Build Coastguard Worker return err;
1121*f7c14bbaSAndroid Build Coastguard Worker }
1122*f7c14bbaSAndroid Build Coastguard Worker
1123*f7c14bbaSAndroid Build Coastguard Worker dst_align = dst->shdr->sh_addralign;
1124*f7c14bbaSAndroid Build Coastguard Worker src_align = src->shdr->sh_addralign;
1125*f7c14bbaSAndroid Build Coastguard Worker if (dst_align == 0)
1126*f7c14bbaSAndroid Build Coastguard Worker dst_align = 1;
1127*f7c14bbaSAndroid Build Coastguard Worker if (dst_align < src_align)
1128*f7c14bbaSAndroid Build Coastguard Worker dst_align = src_align;
1129*f7c14bbaSAndroid Build Coastguard Worker
1130*f7c14bbaSAndroid Build Coastguard Worker dst_align_sz = (dst->sec_sz + dst_align - 1) / dst_align * dst_align;
1131*f7c14bbaSAndroid Build Coastguard Worker
1132*f7c14bbaSAndroid Build Coastguard Worker /* no need to re-align final size */
1133*f7c14bbaSAndroid Build Coastguard Worker dst_final_sz = dst_align_sz + src->shdr->sh_size;
1134*f7c14bbaSAndroid Build Coastguard Worker
1135*f7c14bbaSAndroid Build Coastguard Worker if (src->shdr->sh_type != SHT_NOBITS) {
1136*f7c14bbaSAndroid Build Coastguard Worker tmp = realloc(dst->raw_data, dst_final_sz);
1137*f7c14bbaSAndroid Build Coastguard Worker /* If dst_align_sz == 0, realloc() behaves in a special way:
1138*f7c14bbaSAndroid Build Coastguard Worker * 1. When dst->raw_data is NULL it returns:
1139*f7c14bbaSAndroid Build Coastguard Worker * "either NULL or a pointer suitable to be passed to free()" [1].
1140*f7c14bbaSAndroid Build Coastguard Worker * 2. When dst->raw_data is not-NULL it frees dst->raw_data and returns NULL,
1141*f7c14bbaSAndroid Build Coastguard Worker * thus invalidating any "pointer suitable to be passed to free()" obtained
1142*f7c14bbaSAndroid Build Coastguard Worker * at step (1).
1143*f7c14bbaSAndroid Build Coastguard Worker *
1144*f7c14bbaSAndroid Build Coastguard Worker * The dst_align_sz > 0 check avoids error exit after (2), otherwise
1145*f7c14bbaSAndroid Build Coastguard Worker * dst->raw_data would be freed again in bpf_linker__free().
1146*f7c14bbaSAndroid Build Coastguard Worker *
1147*f7c14bbaSAndroid Build Coastguard Worker * [1] man 3 realloc
1148*f7c14bbaSAndroid Build Coastguard Worker */
1149*f7c14bbaSAndroid Build Coastguard Worker if (!tmp && dst_align_sz > 0)
1150*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1151*f7c14bbaSAndroid Build Coastguard Worker dst->raw_data = tmp;
1152*f7c14bbaSAndroid Build Coastguard Worker
1153*f7c14bbaSAndroid Build Coastguard Worker /* pad dst section, if it's alignment forced size increase */
1154*f7c14bbaSAndroid Build Coastguard Worker memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz);
1155*f7c14bbaSAndroid Build Coastguard Worker /* now copy src data at a properly aligned offset */
1156*f7c14bbaSAndroid Build Coastguard Worker memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size);
1157*f7c14bbaSAndroid Build Coastguard Worker }
1158*f7c14bbaSAndroid Build Coastguard Worker
1159*f7c14bbaSAndroid Build Coastguard Worker dst->sec_sz = dst_final_sz;
1160*f7c14bbaSAndroid Build Coastguard Worker dst->shdr->sh_size = dst_final_sz;
1161*f7c14bbaSAndroid Build Coastguard Worker dst->data->d_size = dst_final_sz;
1162*f7c14bbaSAndroid Build Coastguard Worker
1163*f7c14bbaSAndroid Build Coastguard Worker dst->shdr->sh_addralign = dst_align;
1164*f7c14bbaSAndroid Build Coastguard Worker dst->data->d_align = dst_align;
1165*f7c14bbaSAndroid Build Coastguard Worker
1166*f7c14bbaSAndroid Build Coastguard Worker src->dst_off = dst_align_sz;
1167*f7c14bbaSAndroid Build Coastguard Worker
1168*f7c14bbaSAndroid Build Coastguard Worker return 0;
1169*f7c14bbaSAndroid Build Coastguard Worker }
1170*f7c14bbaSAndroid Build Coastguard Worker
is_data_sec(struct src_sec * sec)1171*f7c14bbaSAndroid Build Coastguard Worker static bool is_data_sec(struct src_sec *sec)
1172*f7c14bbaSAndroid Build Coastguard Worker {
1173*f7c14bbaSAndroid Build Coastguard Worker if (!sec || sec->skipped)
1174*f7c14bbaSAndroid Build Coastguard Worker return false;
1175*f7c14bbaSAndroid Build Coastguard Worker /* ephemeral sections are data sections, e.g., .kconfig, .ksyms */
1176*f7c14bbaSAndroid Build Coastguard Worker if (sec->ephemeral)
1177*f7c14bbaSAndroid Build Coastguard Worker return true;
1178*f7c14bbaSAndroid Build Coastguard Worker return sec->shdr->sh_type == SHT_PROGBITS || sec->shdr->sh_type == SHT_NOBITS;
1179*f7c14bbaSAndroid Build Coastguard Worker }
1180*f7c14bbaSAndroid Build Coastguard Worker
is_relo_sec(struct src_sec * sec)1181*f7c14bbaSAndroid Build Coastguard Worker static bool is_relo_sec(struct src_sec *sec)
1182*f7c14bbaSAndroid Build Coastguard Worker {
1183*f7c14bbaSAndroid Build Coastguard Worker if (!sec || sec->skipped || sec->ephemeral)
1184*f7c14bbaSAndroid Build Coastguard Worker return false;
1185*f7c14bbaSAndroid Build Coastguard Worker return sec->shdr->sh_type == SHT_REL;
1186*f7c14bbaSAndroid Build Coastguard Worker }
1187*f7c14bbaSAndroid Build Coastguard Worker
linker_append_sec_data(struct bpf_linker * linker,struct src_obj * obj)1188*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj)
1189*f7c14bbaSAndroid Build Coastguard Worker {
1190*f7c14bbaSAndroid Build Coastguard Worker int i, err;
1191*f7c14bbaSAndroid Build Coastguard Worker
1192*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < obj->sec_cnt; i++) {
1193*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_sec;
1194*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *dst_sec;
1195*f7c14bbaSAndroid Build Coastguard Worker
1196*f7c14bbaSAndroid Build Coastguard Worker src_sec = &obj->secs[i];
1197*f7c14bbaSAndroid Build Coastguard Worker if (!is_data_sec(src_sec))
1198*f7c14bbaSAndroid Build Coastguard Worker continue;
1199*f7c14bbaSAndroid Build Coastguard Worker
1200*f7c14bbaSAndroid Build Coastguard Worker dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
1201*f7c14bbaSAndroid Build Coastguard Worker if (!dst_sec) {
1202*f7c14bbaSAndroid Build Coastguard Worker dst_sec = add_dst_sec(linker, src_sec->sec_name);
1203*f7c14bbaSAndroid Build Coastguard Worker if (!dst_sec)
1204*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1205*f7c14bbaSAndroid Build Coastguard Worker err = init_sec(linker, dst_sec, src_sec);
1206*f7c14bbaSAndroid Build Coastguard Worker if (err) {
1207*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to init section '%s'\n", src_sec->sec_name);
1208*f7c14bbaSAndroid Build Coastguard Worker return err;
1209*f7c14bbaSAndroid Build Coastguard Worker }
1210*f7c14bbaSAndroid Build Coastguard Worker } else {
1211*f7c14bbaSAndroid Build Coastguard Worker if (!secs_match(dst_sec, src_sec)) {
1212*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ELF sections %s are incompatible\n", src_sec->sec_name);
1213*f7c14bbaSAndroid Build Coastguard Worker return -1;
1214*f7c14bbaSAndroid Build Coastguard Worker }
1215*f7c14bbaSAndroid Build Coastguard Worker
1216*f7c14bbaSAndroid Build Coastguard Worker /* "license" and "version" sections are deduped */
1217*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(src_sec->sec_name, "license") == 0
1218*f7c14bbaSAndroid Build Coastguard Worker || strcmp(src_sec->sec_name, "version") == 0) {
1219*f7c14bbaSAndroid Build Coastguard Worker if (!sec_content_is_same(dst_sec, src_sec)) {
1220*f7c14bbaSAndroid Build Coastguard Worker pr_warn("non-identical contents of section '%s' are not supported\n", src_sec->sec_name);
1221*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1222*f7c14bbaSAndroid Build Coastguard Worker }
1223*f7c14bbaSAndroid Build Coastguard Worker src_sec->skipped = true;
1224*f7c14bbaSAndroid Build Coastguard Worker src_sec->dst_id = dst_sec->id;
1225*f7c14bbaSAndroid Build Coastguard Worker continue;
1226*f7c14bbaSAndroid Build Coastguard Worker }
1227*f7c14bbaSAndroid Build Coastguard Worker }
1228*f7c14bbaSAndroid Build Coastguard Worker
1229*f7c14bbaSAndroid Build Coastguard Worker /* record mapped section index */
1230*f7c14bbaSAndroid Build Coastguard Worker src_sec->dst_id = dst_sec->id;
1231*f7c14bbaSAndroid Build Coastguard Worker
1232*f7c14bbaSAndroid Build Coastguard Worker err = extend_sec(linker, dst_sec, src_sec);
1233*f7c14bbaSAndroid Build Coastguard Worker if (err)
1234*f7c14bbaSAndroid Build Coastguard Worker return err;
1235*f7c14bbaSAndroid Build Coastguard Worker }
1236*f7c14bbaSAndroid Build Coastguard Worker
1237*f7c14bbaSAndroid Build Coastguard Worker return 0;
1238*f7c14bbaSAndroid Build Coastguard Worker }
1239*f7c14bbaSAndroid Build Coastguard Worker
linker_append_elf_syms(struct bpf_linker * linker,struct src_obj * obj)1240*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj)
1241*f7c14bbaSAndroid Build Coastguard Worker {
1242*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
1243*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym = symtab->data->d_buf;
1244*f7c14bbaSAndroid Build Coastguard Worker int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize, err;
1245*f7c14bbaSAndroid Build Coastguard Worker int str_sec_idx = symtab->shdr->sh_link;
1246*f7c14bbaSAndroid Build Coastguard Worker const char *sym_name;
1247*f7c14bbaSAndroid Build Coastguard Worker
1248*f7c14bbaSAndroid Build Coastguard Worker obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map));
1249*f7c14bbaSAndroid Build Coastguard Worker if (!obj->sym_map)
1250*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1251*f7c14bbaSAndroid Build Coastguard Worker
1252*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, sym++) {
1253*f7c14bbaSAndroid Build Coastguard Worker /* We already validated all-zero symbol #0 and we already
1254*f7c14bbaSAndroid Build Coastguard Worker * appended it preventively to the final SYMTAB, so skip it.
1255*f7c14bbaSAndroid Build Coastguard Worker */
1256*f7c14bbaSAndroid Build Coastguard Worker if (i == 0)
1257*f7c14bbaSAndroid Build Coastguard Worker continue;
1258*f7c14bbaSAndroid Build Coastguard Worker
1259*f7c14bbaSAndroid Build Coastguard Worker sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
1260*f7c14bbaSAndroid Build Coastguard Worker if (!sym_name) {
1261*f7c14bbaSAndroid Build Coastguard Worker pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename);
1262*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1263*f7c14bbaSAndroid Build Coastguard Worker }
1264*f7c14bbaSAndroid Build Coastguard Worker
1265*f7c14bbaSAndroid Build Coastguard Worker err = linker_append_elf_sym(linker, obj, sym, sym_name, i);
1266*f7c14bbaSAndroid Build Coastguard Worker if (err)
1267*f7c14bbaSAndroid Build Coastguard Worker return err;
1268*f7c14bbaSAndroid Build Coastguard Worker }
1269*f7c14bbaSAndroid Build Coastguard Worker
1270*f7c14bbaSAndroid Build Coastguard Worker return 0;
1271*f7c14bbaSAndroid Build Coastguard Worker }
1272*f7c14bbaSAndroid Build Coastguard Worker
get_sym_by_idx(struct bpf_linker * linker,size_t sym_idx)1273*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *get_sym_by_idx(struct bpf_linker *linker, size_t sym_idx)
1274*f7c14bbaSAndroid Build Coastguard Worker {
1275*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *symtab = &linker->secs[linker->symtab_sec_idx];
1276*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *syms = symtab->raw_data;
1277*f7c14bbaSAndroid Build Coastguard Worker
1278*f7c14bbaSAndroid Build Coastguard Worker return &syms[sym_idx];
1279*f7c14bbaSAndroid Build Coastguard Worker }
1280*f7c14bbaSAndroid Build Coastguard Worker
find_glob_sym(struct bpf_linker * linker,const char * sym_name)1281*f7c14bbaSAndroid Build Coastguard Worker static struct glob_sym *find_glob_sym(struct bpf_linker *linker, const char *sym_name)
1282*f7c14bbaSAndroid Build Coastguard Worker {
1283*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_sym;
1284*f7c14bbaSAndroid Build Coastguard Worker const char *name;
1285*f7c14bbaSAndroid Build Coastguard Worker int i;
1286*f7c14bbaSAndroid Build Coastguard Worker
1287*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < linker->glob_sym_cnt; i++) {
1288*f7c14bbaSAndroid Build Coastguard Worker glob_sym = &linker->glob_syms[i];
1289*f7c14bbaSAndroid Build Coastguard Worker name = strset__data(linker->strtab_strs) + glob_sym->name_off;
1290*f7c14bbaSAndroid Build Coastguard Worker
1291*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, sym_name) == 0)
1292*f7c14bbaSAndroid Build Coastguard Worker return glob_sym;
1293*f7c14bbaSAndroid Build Coastguard Worker }
1294*f7c14bbaSAndroid Build Coastguard Worker
1295*f7c14bbaSAndroid Build Coastguard Worker return NULL;
1296*f7c14bbaSAndroid Build Coastguard Worker }
1297*f7c14bbaSAndroid Build Coastguard Worker
add_glob_sym(struct bpf_linker * linker)1298*f7c14bbaSAndroid Build Coastguard Worker static struct glob_sym *add_glob_sym(struct bpf_linker *linker)
1299*f7c14bbaSAndroid Build Coastguard Worker {
1300*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *syms, *sym;
1301*f7c14bbaSAndroid Build Coastguard Worker
1302*f7c14bbaSAndroid Build Coastguard Worker syms = libbpf_reallocarray(linker->glob_syms, linker->glob_sym_cnt + 1,
1303*f7c14bbaSAndroid Build Coastguard Worker sizeof(*linker->glob_syms));
1304*f7c14bbaSAndroid Build Coastguard Worker if (!syms)
1305*f7c14bbaSAndroid Build Coastguard Worker return NULL;
1306*f7c14bbaSAndroid Build Coastguard Worker
1307*f7c14bbaSAndroid Build Coastguard Worker sym = &syms[linker->glob_sym_cnt];
1308*f7c14bbaSAndroid Build Coastguard Worker memset(sym, 0, sizeof(*sym));
1309*f7c14bbaSAndroid Build Coastguard Worker sym->var_idx = -1;
1310*f7c14bbaSAndroid Build Coastguard Worker
1311*f7c14bbaSAndroid Build Coastguard Worker linker->glob_syms = syms;
1312*f7c14bbaSAndroid Build Coastguard Worker linker->glob_sym_cnt++;
1313*f7c14bbaSAndroid Build Coastguard Worker
1314*f7c14bbaSAndroid Build Coastguard Worker return sym;
1315*f7c14bbaSAndroid Build Coastguard Worker }
1316*f7c14bbaSAndroid Build Coastguard Worker
glob_sym_btf_matches(const char * sym_name,bool exact,const struct btf * btf1,__u32 id1,const struct btf * btf2,__u32 id2)1317*f7c14bbaSAndroid Build Coastguard Worker static bool glob_sym_btf_matches(const char *sym_name, bool exact,
1318*f7c14bbaSAndroid Build Coastguard Worker const struct btf *btf1, __u32 id1,
1319*f7c14bbaSAndroid Build Coastguard Worker const struct btf *btf2, __u32 id2)
1320*f7c14bbaSAndroid Build Coastguard Worker {
1321*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t1, *t2;
1322*f7c14bbaSAndroid Build Coastguard Worker bool is_static1, is_static2;
1323*f7c14bbaSAndroid Build Coastguard Worker const char *n1, *n2;
1324*f7c14bbaSAndroid Build Coastguard Worker int i, n;
1325*f7c14bbaSAndroid Build Coastguard Worker
1326*f7c14bbaSAndroid Build Coastguard Worker recur:
1327*f7c14bbaSAndroid Build Coastguard Worker n1 = n2 = NULL;
1328*f7c14bbaSAndroid Build Coastguard Worker t1 = skip_mods_and_typedefs(btf1, id1, &id1);
1329*f7c14bbaSAndroid Build Coastguard Worker t2 = skip_mods_and_typedefs(btf2, id2, &id2);
1330*f7c14bbaSAndroid Build Coastguard Worker
1331*f7c14bbaSAndroid Build Coastguard Worker /* check if only one side is FWD, otherwise handle with common logic */
1332*f7c14bbaSAndroid Build Coastguard Worker if (!exact && btf_is_fwd(t1) != btf_is_fwd(t2)) {
1333*f7c14bbaSAndroid Build Coastguard Worker n1 = btf__str_by_offset(btf1, t1->name_off);
1334*f7c14bbaSAndroid Build Coastguard Worker n2 = btf__str_by_offset(btf2, t2->name_off);
1335*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(n1, n2) != 0) {
1336*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible forward declaration names '%s' and '%s'\n",
1337*f7c14bbaSAndroid Build Coastguard Worker sym_name, n1, n2);
1338*f7c14bbaSAndroid Build Coastguard Worker return false;
1339*f7c14bbaSAndroid Build Coastguard Worker }
1340*f7c14bbaSAndroid Build Coastguard Worker /* validate if FWD kind matches concrete kind */
1341*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_fwd(t1)) {
1342*f7c14bbaSAndroid Build Coastguard Worker if (btf_kflag(t1) && btf_is_union(t2))
1343*f7c14bbaSAndroid Build Coastguard Worker return true;
1344*f7c14bbaSAndroid Build Coastguard Worker if (!btf_kflag(t1) && btf_is_struct(t2))
1345*f7c14bbaSAndroid Build Coastguard Worker return true;
1346*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible %s forward declaration and concrete kind %s\n",
1347*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kflag(t1) ? "union" : "struct", btf_kind_str(t2));
1348*f7c14bbaSAndroid Build Coastguard Worker } else {
1349*f7c14bbaSAndroid Build Coastguard Worker if (btf_kflag(t2) && btf_is_union(t1))
1350*f7c14bbaSAndroid Build Coastguard Worker return true;
1351*f7c14bbaSAndroid Build Coastguard Worker if (!btf_kflag(t2) && btf_is_struct(t1))
1352*f7c14bbaSAndroid Build Coastguard Worker return true;
1353*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible %s forward declaration and concrete kind %s\n",
1354*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kflag(t2) ? "union" : "struct", btf_kind_str(t1));
1355*f7c14bbaSAndroid Build Coastguard Worker }
1356*f7c14bbaSAndroid Build Coastguard Worker return false;
1357*f7c14bbaSAndroid Build Coastguard Worker }
1358*f7c14bbaSAndroid Build Coastguard Worker
1359*f7c14bbaSAndroid Build Coastguard Worker if (btf_kind(t1) != btf_kind(t2)) {
1360*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible BTF kinds %s and %s\n",
1361*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1), btf_kind_str(t2));
1362*f7c14bbaSAndroid Build Coastguard Worker return false;
1363*f7c14bbaSAndroid Build Coastguard Worker }
1364*f7c14bbaSAndroid Build Coastguard Worker
1365*f7c14bbaSAndroid Build Coastguard Worker switch (btf_kind(t1)) {
1366*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_STRUCT:
1367*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_UNION:
1368*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM:
1369*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM64:
1370*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FWD:
1371*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FUNC:
1372*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_VAR:
1373*f7c14bbaSAndroid Build Coastguard Worker n1 = btf__str_by_offset(btf1, t1->name_off);
1374*f7c14bbaSAndroid Build Coastguard Worker n2 = btf__str_by_offset(btf2, t2->name_off);
1375*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(n1, n2) != 0) {
1376*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible %s names '%s' and '%s'\n",
1377*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1), n1, n2);
1378*f7c14bbaSAndroid Build Coastguard Worker return false;
1379*f7c14bbaSAndroid Build Coastguard Worker }
1380*f7c14bbaSAndroid Build Coastguard Worker break;
1381*f7c14bbaSAndroid Build Coastguard Worker default:
1382*f7c14bbaSAndroid Build Coastguard Worker break;
1383*f7c14bbaSAndroid Build Coastguard Worker }
1384*f7c14bbaSAndroid Build Coastguard Worker
1385*f7c14bbaSAndroid Build Coastguard Worker switch (btf_kind(t1)) {
1386*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_UNKN: /* void */
1387*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FWD:
1388*f7c14bbaSAndroid Build Coastguard Worker return true;
1389*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_INT:
1390*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FLOAT:
1391*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM:
1392*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM64:
1393*f7c14bbaSAndroid Build Coastguard Worker /* ignore encoding for int and enum values for enum */
1394*f7c14bbaSAndroid Build Coastguard Worker if (t1->size != t2->size) {
1395*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible %s '%s' size %u and %u\n",
1396*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1), n1, t1->size, t2->size);
1397*f7c14bbaSAndroid Build Coastguard Worker return false;
1398*f7c14bbaSAndroid Build Coastguard Worker }
1399*f7c14bbaSAndroid Build Coastguard Worker return true;
1400*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_PTR:
1401*f7c14bbaSAndroid Build Coastguard Worker /* just validate overall shape of the referenced type, so no
1402*f7c14bbaSAndroid Build Coastguard Worker * contents comparison for struct/union, and allowd fwd vs
1403*f7c14bbaSAndroid Build Coastguard Worker * struct/union
1404*f7c14bbaSAndroid Build Coastguard Worker */
1405*f7c14bbaSAndroid Build Coastguard Worker exact = false;
1406*f7c14bbaSAndroid Build Coastguard Worker id1 = t1->type;
1407*f7c14bbaSAndroid Build Coastguard Worker id2 = t2->type;
1408*f7c14bbaSAndroid Build Coastguard Worker goto recur;
1409*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ARRAY:
1410*f7c14bbaSAndroid Build Coastguard Worker /* ignore index type and array size */
1411*f7c14bbaSAndroid Build Coastguard Worker id1 = btf_array(t1)->type;
1412*f7c14bbaSAndroid Build Coastguard Worker id2 = btf_array(t2)->type;
1413*f7c14bbaSAndroid Build Coastguard Worker goto recur;
1414*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FUNC:
1415*f7c14bbaSAndroid Build Coastguard Worker /* extern and global linkages are compatible */
1416*f7c14bbaSAndroid Build Coastguard Worker is_static1 = btf_func_linkage(t1) == BTF_FUNC_STATIC;
1417*f7c14bbaSAndroid Build Coastguard Worker is_static2 = btf_func_linkage(t2) == BTF_FUNC_STATIC;
1418*f7c14bbaSAndroid Build Coastguard Worker if (is_static1 != is_static2) {
1419*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible func '%s' linkage\n", sym_name, n1);
1420*f7c14bbaSAndroid Build Coastguard Worker return false;
1421*f7c14bbaSAndroid Build Coastguard Worker }
1422*f7c14bbaSAndroid Build Coastguard Worker
1423*f7c14bbaSAndroid Build Coastguard Worker id1 = t1->type;
1424*f7c14bbaSAndroid Build Coastguard Worker id2 = t2->type;
1425*f7c14bbaSAndroid Build Coastguard Worker goto recur;
1426*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_VAR:
1427*f7c14bbaSAndroid Build Coastguard Worker /* extern and global linkages are compatible */
1428*f7c14bbaSAndroid Build Coastguard Worker is_static1 = btf_var(t1)->linkage == BTF_VAR_STATIC;
1429*f7c14bbaSAndroid Build Coastguard Worker is_static2 = btf_var(t2)->linkage == BTF_VAR_STATIC;
1430*f7c14bbaSAndroid Build Coastguard Worker if (is_static1 != is_static2) {
1431*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible var '%s' linkage\n", sym_name, n1);
1432*f7c14bbaSAndroid Build Coastguard Worker return false;
1433*f7c14bbaSAndroid Build Coastguard Worker }
1434*f7c14bbaSAndroid Build Coastguard Worker
1435*f7c14bbaSAndroid Build Coastguard Worker id1 = t1->type;
1436*f7c14bbaSAndroid Build Coastguard Worker id2 = t2->type;
1437*f7c14bbaSAndroid Build Coastguard Worker goto recur;
1438*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_STRUCT:
1439*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_UNION: {
1440*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *m1, *m2;
1441*f7c14bbaSAndroid Build Coastguard Worker
1442*f7c14bbaSAndroid Build Coastguard Worker if (!exact)
1443*f7c14bbaSAndroid Build Coastguard Worker return true;
1444*f7c14bbaSAndroid Build Coastguard Worker
1445*f7c14bbaSAndroid Build Coastguard Worker if (btf_vlen(t1) != btf_vlen(t2)) {
1446*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible number of %s fields %u and %u\n",
1447*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1), btf_vlen(t1), btf_vlen(t2));
1448*f7c14bbaSAndroid Build Coastguard Worker return false;
1449*f7c14bbaSAndroid Build Coastguard Worker }
1450*f7c14bbaSAndroid Build Coastguard Worker
1451*f7c14bbaSAndroid Build Coastguard Worker n = btf_vlen(t1);
1452*f7c14bbaSAndroid Build Coastguard Worker m1 = btf_members(t1);
1453*f7c14bbaSAndroid Build Coastguard Worker m2 = btf_members(t2);
1454*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, m1++, m2++) {
1455*f7c14bbaSAndroid Build Coastguard Worker n1 = btf__str_by_offset(btf1, m1->name_off);
1456*f7c14bbaSAndroid Build Coastguard Worker n2 = btf__str_by_offset(btf2, m2->name_off);
1457*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(n1, n2) != 0) {
1458*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible field #%d names '%s' and '%s'\n",
1459*f7c14bbaSAndroid Build Coastguard Worker sym_name, i, n1, n2);
1460*f7c14bbaSAndroid Build Coastguard Worker return false;
1461*f7c14bbaSAndroid Build Coastguard Worker }
1462*f7c14bbaSAndroid Build Coastguard Worker if (m1->offset != m2->offset) {
1463*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible field #%d ('%s') offsets\n",
1464*f7c14bbaSAndroid Build Coastguard Worker sym_name, i, n1);
1465*f7c14bbaSAndroid Build Coastguard Worker return false;
1466*f7c14bbaSAndroid Build Coastguard Worker }
1467*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym_btf_matches(sym_name, exact, btf1, m1->type, btf2, m2->type))
1468*f7c14bbaSAndroid Build Coastguard Worker return false;
1469*f7c14bbaSAndroid Build Coastguard Worker }
1470*f7c14bbaSAndroid Build Coastguard Worker
1471*f7c14bbaSAndroid Build Coastguard Worker return true;
1472*f7c14bbaSAndroid Build Coastguard Worker }
1473*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FUNC_PROTO: {
1474*f7c14bbaSAndroid Build Coastguard Worker const struct btf_param *m1, *m2;
1475*f7c14bbaSAndroid Build Coastguard Worker
1476*f7c14bbaSAndroid Build Coastguard Worker if (btf_vlen(t1) != btf_vlen(t2)) {
1477*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': incompatible number of %s params %u and %u\n",
1478*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1), btf_vlen(t1), btf_vlen(t2));
1479*f7c14bbaSAndroid Build Coastguard Worker return false;
1480*f7c14bbaSAndroid Build Coastguard Worker }
1481*f7c14bbaSAndroid Build Coastguard Worker
1482*f7c14bbaSAndroid Build Coastguard Worker n = btf_vlen(t1);
1483*f7c14bbaSAndroid Build Coastguard Worker m1 = btf_params(t1);
1484*f7c14bbaSAndroid Build Coastguard Worker m2 = btf_params(t2);
1485*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, m1++, m2++) {
1486*f7c14bbaSAndroid Build Coastguard Worker /* ignore func arg names */
1487*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym_btf_matches(sym_name, exact, btf1, m1->type, btf2, m2->type))
1488*f7c14bbaSAndroid Build Coastguard Worker return false;
1489*f7c14bbaSAndroid Build Coastguard Worker }
1490*f7c14bbaSAndroid Build Coastguard Worker
1491*f7c14bbaSAndroid Build Coastguard Worker /* now check return type as well */
1492*f7c14bbaSAndroid Build Coastguard Worker id1 = t1->type;
1493*f7c14bbaSAndroid Build Coastguard Worker id2 = t2->type;
1494*f7c14bbaSAndroid Build Coastguard Worker goto recur;
1495*f7c14bbaSAndroid Build Coastguard Worker }
1496*f7c14bbaSAndroid Build Coastguard Worker
1497*f7c14bbaSAndroid Build Coastguard Worker /* skip_mods_and_typedefs() make this impossible */
1498*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_TYPEDEF:
1499*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_VOLATILE:
1500*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_CONST:
1501*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_RESTRICT:
1502*f7c14bbaSAndroid Build Coastguard Worker /* DATASECs are never compared with each other */
1503*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_DATASEC:
1504*f7c14bbaSAndroid Build Coastguard Worker default:
1505*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': unsupported BTF kind %s\n",
1506*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_kind_str(t1));
1507*f7c14bbaSAndroid Build Coastguard Worker return false;
1508*f7c14bbaSAndroid Build Coastguard Worker }
1509*f7c14bbaSAndroid Build Coastguard Worker }
1510*f7c14bbaSAndroid Build Coastguard Worker
map_defs_match(const char * sym_name,const struct btf * main_btf,const struct btf_map_def * main_def,const struct btf_map_def * main_inner_def,const struct btf * extra_btf,const struct btf_map_def * extra_def,const struct btf_map_def * extra_inner_def)1511*f7c14bbaSAndroid Build Coastguard Worker static bool map_defs_match(const char *sym_name,
1512*f7c14bbaSAndroid Build Coastguard Worker const struct btf *main_btf,
1513*f7c14bbaSAndroid Build Coastguard Worker const struct btf_map_def *main_def,
1514*f7c14bbaSAndroid Build Coastguard Worker const struct btf_map_def *main_inner_def,
1515*f7c14bbaSAndroid Build Coastguard Worker const struct btf *extra_btf,
1516*f7c14bbaSAndroid Build Coastguard Worker const struct btf_map_def *extra_def,
1517*f7c14bbaSAndroid Build Coastguard Worker const struct btf_map_def *extra_inner_def)
1518*f7c14bbaSAndroid Build Coastguard Worker {
1519*f7c14bbaSAndroid Build Coastguard Worker const char *reason;
1520*f7c14bbaSAndroid Build Coastguard Worker
1521*f7c14bbaSAndroid Build Coastguard Worker if (main_def->map_type != extra_def->map_type) {
1522*f7c14bbaSAndroid Build Coastguard Worker reason = "type";
1523*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1524*f7c14bbaSAndroid Build Coastguard Worker }
1525*f7c14bbaSAndroid Build Coastguard Worker
1526*f7c14bbaSAndroid Build Coastguard Worker /* check key type/size match */
1527*f7c14bbaSAndroid Build Coastguard Worker if (main_def->key_size != extra_def->key_size) {
1528*f7c14bbaSAndroid Build Coastguard Worker reason = "key_size";
1529*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1530*f7c14bbaSAndroid Build Coastguard Worker }
1531*f7c14bbaSAndroid Build Coastguard Worker if (!!main_def->key_type_id != !!extra_def->key_type_id) {
1532*f7c14bbaSAndroid Build Coastguard Worker reason = "key type";
1533*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1534*f7c14bbaSAndroid Build Coastguard Worker }
1535*f7c14bbaSAndroid Build Coastguard Worker if ((main_def->parts & MAP_DEF_KEY_TYPE)
1536*f7c14bbaSAndroid Build Coastguard Worker && !glob_sym_btf_matches(sym_name, true /*exact*/,
1537*f7c14bbaSAndroid Build Coastguard Worker main_btf, main_def->key_type_id,
1538*f7c14bbaSAndroid Build Coastguard Worker extra_btf, extra_def->key_type_id)) {
1539*f7c14bbaSAndroid Build Coastguard Worker reason = "key type";
1540*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1541*f7c14bbaSAndroid Build Coastguard Worker }
1542*f7c14bbaSAndroid Build Coastguard Worker
1543*f7c14bbaSAndroid Build Coastguard Worker /* validate value type/size match */
1544*f7c14bbaSAndroid Build Coastguard Worker if (main_def->value_size != extra_def->value_size) {
1545*f7c14bbaSAndroid Build Coastguard Worker reason = "value_size";
1546*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1547*f7c14bbaSAndroid Build Coastguard Worker }
1548*f7c14bbaSAndroid Build Coastguard Worker if (!!main_def->value_type_id != !!extra_def->value_type_id) {
1549*f7c14bbaSAndroid Build Coastguard Worker reason = "value type";
1550*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1551*f7c14bbaSAndroid Build Coastguard Worker }
1552*f7c14bbaSAndroid Build Coastguard Worker if ((main_def->parts & MAP_DEF_VALUE_TYPE)
1553*f7c14bbaSAndroid Build Coastguard Worker && !glob_sym_btf_matches(sym_name, true /*exact*/,
1554*f7c14bbaSAndroid Build Coastguard Worker main_btf, main_def->value_type_id,
1555*f7c14bbaSAndroid Build Coastguard Worker extra_btf, extra_def->value_type_id)) {
1556*f7c14bbaSAndroid Build Coastguard Worker reason = "key type";
1557*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1558*f7c14bbaSAndroid Build Coastguard Worker }
1559*f7c14bbaSAndroid Build Coastguard Worker
1560*f7c14bbaSAndroid Build Coastguard Worker if (main_def->max_entries != extra_def->max_entries) {
1561*f7c14bbaSAndroid Build Coastguard Worker reason = "max_entries";
1562*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1563*f7c14bbaSAndroid Build Coastguard Worker }
1564*f7c14bbaSAndroid Build Coastguard Worker if (main_def->map_flags != extra_def->map_flags) {
1565*f7c14bbaSAndroid Build Coastguard Worker reason = "map_flags";
1566*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1567*f7c14bbaSAndroid Build Coastguard Worker }
1568*f7c14bbaSAndroid Build Coastguard Worker if (main_def->numa_node != extra_def->numa_node) {
1569*f7c14bbaSAndroid Build Coastguard Worker reason = "numa_node";
1570*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1571*f7c14bbaSAndroid Build Coastguard Worker }
1572*f7c14bbaSAndroid Build Coastguard Worker if (main_def->pinning != extra_def->pinning) {
1573*f7c14bbaSAndroid Build Coastguard Worker reason = "pinning";
1574*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1575*f7c14bbaSAndroid Build Coastguard Worker }
1576*f7c14bbaSAndroid Build Coastguard Worker
1577*f7c14bbaSAndroid Build Coastguard Worker if ((main_def->parts & MAP_DEF_INNER_MAP) != (extra_def->parts & MAP_DEF_INNER_MAP)) {
1578*f7c14bbaSAndroid Build Coastguard Worker reason = "inner map";
1579*f7c14bbaSAndroid Build Coastguard Worker goto mismatch;
1580*f7c14bbaSAndroid Build Coastguard Worker }
1581*f7c14bbaSAndroid Build Coastguard Worker
1582*f7c14bbaSAndroid Build Coastguard Worker if (main_def->parts & MAP_DEF_INNER_MAP) {
1583*f7c14bbaSAndroid Build Coastguard Worker char inner_map_name[128];
1584*f7c14bbaSAndroid Build Coastguard Worker
1585*f7c14bbaSAndroid Build Coastguard Worker snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", sym_name);
1586*f7c14bbaSAndroid Build Coastguard Worker
1587*f7c14bbaSAndroid Build Coastguard Worker return map_defs_match(inner_map_name,
1588*f7c14bbaSAndroid Build Coastguard Worker main_btf, main_inner_def, NULL,
1589*f7c14bbaSAndroid Build Coastguard Worker extra_btf, extra_inner_def, NULL);
1590*f7c14bbaSAndroid Build Coastguard Worker }
1591*f7c14bbaSAndroid Build Coastguard Worker
1592*f7c14bbaSAndroid Build Coastguard Worker return true;
1593*f7c14bbaSAndroid Build Coastguard Worker
1594*f7c14bbaSAndroid Build Coastguard Worker mismatch:
1595*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': map %s mismatch\n", sym_name, reason);
1596*f7c14bbaSAndroid Build Coastguard Worker return false;
1597*f7c14bbaSAndroid Build Coastguard Worker }
1598*f7c14bbaSAndroid Build Coastguard Worker
glob_map_defs_match(const char * sym_name,struct bpf_linker * linker,struct glob_sym * glob_sym,struct src_obj * obj,Elf64_Sym * sym,int btf_id)1599*f7c14bbaSAndroid Build Coastguard Worker static bool glob_map_defs_match(const char *sym_name,
1600*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker *linker, struct glob_sym *glob_sym,
1601*f7c14bbaSAndroid Build Coastguard Worker struct src_obj *obj, Elf64_Sym *sym, int btf_id)
1602*f7c14bbaSAndroid Build Coastguard Worker {
1603*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def dst_def = {}, dst_inner_def = {};
1604*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def src_def = {}, src_inner_def = {};
1605*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
1606*f7c14bbaSAndroid Build Coastguard Worker int err;
1607*f7c14bbaSAndroid Build Coastguard Worker
1608*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, btf_id);
1609*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(t)) {
1610*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': invalid map definition type [%d]\n", sym_name, btf_id);
1611*f7c14bbaSAndroid Build Coastguard Worker return false;
1612*f7c14bbaSAndroid Build Coastguard Worker }
1613*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
1614*f7c14bbaSAndroid Build Coastguard Worker
1615*f7c14bbaSAndroid Build Coastguard Worker err = parse_btf_map_def(sym_name, obj->btf, t, true /*strict*/, &src_def, &src_inner_def);
1616*f7c14bbaSAndroid Build Coastguard Worker if (err) {
1617*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': invalid map definition\n", sym_name);
1618*f7c14bbaSAndroid Build Coastguard Worker return false;
1619*f7c14bbaSAndroid Build Coastguard Worker }
1620*f7c14bbaSAndroid Build Coastguard Worker
1621*f7c14bbaSAndroid Build Coastguard Worker /* re-parse existing map definition */
1622*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(linker->btf, glob_sym->btf_id);
1623*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(linker->btf, t->type, NULL);
1624*f7c14bbaSAndroid Build Coastguard Worker err = parse_btf_map_def(sym_name, linker->btf, t, true /*strict*/, &dst_def, &dst_inner_def);
1625*f7c14bbaSAndroid Build Coastguard Worker if (err) {
1626*f7c14bbaSAndroid Build Coastguard Worker /* this should not happen, because we already validated it */
1627*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': invalid dst map definition\n", sym_name);
1628*f7c14bbaSAndroid Build Coastguard Worker return false;
1629*f7c14bbaSAndroid Build Coastguard Worker }
1630*f7c14bbaSAndroid Build Coastguard Worker
1631*f7c14bbaSAndroid Build Coastguard Worker /* Currently extern map definition has to be complete and match
1632*f7c14bbaSAndroid Build Coastguard Worker * concrete map definition exactly. This restriction might be lifted
1633*f7c14bbaSAndroid Build Coastguard Worker * in the future.
1634*f7c14bbaSAndroid Build Coastguard Worker */
1635*f7c14bbaSAndroid Build Coastguard Worker return map_defs_match(sym_name, linker->btf, &dst_def, &dst_inner_def,
1636*f7c14bbaSAndroid Build Coastguard Worker obj->btf, &src_def, &src_inner_def);
1637*f7c14bbaSAndroid Build Coastguard Worker }
1638*f7c14bbaSAndroid Build Coastguard Worker
glob_syms_match(const char * sym_name,struct bpf_linker * linker,struct glob_sym * glob_sym,struct src_obj * obj,Elf64_Sym * sym,size_t sym_idx,int btf_id)1639*f7c14bbaSAndroid Build Coastguard Worker static bool glob_syms_match(const char *sym_name,
1640*f7c14bbaSAndroid Build Coastguard Worker struct bpf_linker *linker, struct glob_sym *glob_sym,
1641*f7c14bbaSAndroid Build Coastguard Worker struct src_obj *obj, Elf64_Sym *sym, size_t sym_idx, int btf_id)
1642*f7c14bbaSAndroid Build Coastguard Worker {
1643*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *src_t;
1644*f7c14bbaSAndroid Build Coastguard Worker
1645*f7c14bbaSAndroid Build Coastguard Worker /* if we are dealing with externs, BTF types describing both global
1646*f7c14bbaSAndroid Build Coastguard Worker * and extern VARs/FUNCs should be completely present in all files
1647*f7c14bbaSAndroid Build Coastguard Worker */
1648*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym->btf_id || !btf_id) {
1649*f7c14bbaSAndroid Build Coastguard Worker pr_warn("BTF info is missing for global symbol '%s'\n", sym_name);
1650*f7c14bbaSAndroid Build Coastguard Worker return false;
1651*f7c14bbaSAndroid Build Coastguard Worker }
1652*f7c14bbaSAndroid Build Coastguard Worker
1653*f7c14bbaSAndroid Build Coastguard Worker src_t = btf__type_by_id(obj->btf, btf_id);
1654*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(src_t) && !btf_is_func(src_t)) {
1655*f7c14bbaSAndroid Build Coastguard Worker pr_warn("only extern variables and functions are supported, but got '%s' for '%s'\n",
1656*f7c14bbaSAndroid Build Coastguard Worker btf_kind_str(src_t), sym_name);
1657*f7c14bbaSAndroid Build Coastguard Worker return false;
1658*f7c14bbaSAndroid Build Coastguard Worker }
1659*f7c14bbaSAndroid Build Coastguard Worker
1660*f7c14bbaSAndroid Build Coastguard Worker /* deal with .maps definitions specially */
1661*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym->sec_id && strcmp(linker->secs[glob_sym->sec_id].sec_name, MAPS_ELF_SEC) == 0)
1662*f7c14bbaSAndroid Build Coastguard Worker return glob_map_defs_match(sym_name, linker, glob_sym, obj, sym, btf_id);
1663*f7c14bbaSAndroid Build Coastguard Worker
1664*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym_btf_matches(sym_name, true /*exact*/,
1665*f7c14bbaSAndroid Build Coastguard Worker linker->btf, glob_sym->btf_id, obj->btf, btf_id))
1666*f7c14bbaSAndroid Build Coastguard Worker return false;
1667*f7c14bbaSAndroid Build Coastguard Worker
1668*f7c14bbaSAndroid Build Coastguard Worker return true;
1669*f7c14bbaSAndroid Build Coastguard Worker }
1670*f7c14bbaSAndroid Build Coastguard Worker
btf_is_non_static(const struct btf_type * t)1671*f7c14bbaSAndroid Build Coastguard Worker static bool btf_is_non_static(const struct btf_type *t)
1672*f7c14bbaSAndroid Build Coastguard Worker {
1673*f7c14bbaSAndroid Build Coastguard Worker return (btf_is_var(t) && btf_var(t)->linkage != BTF_VAR_STATIC)
1674*f7c14bbaSAndroid Build Coastguard Worker || (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_STATIC);
1675*f7c14bbaSAndroid Build Coastguard Worker }
1676*f7c14bbaSAndroid Build Coastguard Worker
find_glob_sym_btf(struct src_obj * obj,Elf64_Sym * sym,const char * sym_name,int * out_btf_sec_id,int * out_btf_id)1677*f7c14bbaSAndroid Build Coastguard Worker static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name,
1678*f7c14bbaSAndroid Build Coastguard Worker int *out_btf_sec_id, int *out_btf_id)
1679*f7c14bbaSAndroid Build Coastguard Worker {
1680*f7c14bbaSAndroid Build Coastguard Worker int i, j, n, m, btf_id = 0;
1681*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
1682*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vi;
1683*f7c14bbaSAndroid Build Coastguard Worker const char *name;
1684*f7c14bbaSAndroid Build Coastguard Worker
1685*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf) {
1686*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find BTF info for object '%s'\n", obj->filename);
1687*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1688*f7c14bbaSAndroid Build Coastguard Worker }
1689*f7c14bbaSAndroid Build Coastguard Worker
1690*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
1691*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
1692*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, i);
1693*f7c14bbaSAndroid Build Coastguard Worker
1694*f7c14bbaSAndroid Build Coastguard Worker /* some global and extern FUNCs and VARs might not be associated with any
1695*f7c14bbaSAndroid Build Coastguard Worker * DATASEC, so try to detect them in the same pass
1696*f7c14bbaSAndroid Build Coastguard Worker */
1697*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_non_static(t)) {
1698*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(obj->btf, t->name_off);
1699*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, sym_name) != 0)
1700*f7c14bbaSAndroid Build Coastguard Worker continue;
1701*f7c14bbaSAndroid Build Coastguard Worker
1702*f7c14bbaSAndroid Build Coastguard Worker /* remember and still try to find DATASEC */
1703*f7c14bbaSAndroid Build Coastguard Worker btf_id = i;
1704*f7c14bbaSAndroid Build Coastguard Worker continue;
1705*f7c14bbaSAndroid Build Coastguard Worker }
1706*f7c14bbaSAndroid Build Coastguard Worker
1707*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(t))
1708*f7c14bbaSAndroid Build Coastguard Worker continue;
1709*f7c14bbaSAndroid Build Coastguard Worker
1710*f7c14bbaSAndroid Build Coastguard Worker vi = btf_var_secinfos(t);
1711*f7c14bbaSAndroid Build Coastguard Worker for (j = 0, m = btf_vlen(t); j < m; j++, vi++) {
1712*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, vi->type);
1713*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(obj->btf, t->name_off);
1714*f7c14bbaSAndroid Build Coastguard Worker
1715*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, sym_name) != 0)
1716*f7c14bbaSAndroid Build Coastguard Worker continue;
1717*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_var(t) && btf_var(t)->linkage == BTF_VAR_STATIC)
1718*f7c14bbaSAndroid Build Coastguard Worker continue;
1719*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_func(t) && btf_func_linkage(t) == BTF_FUNC_STATIC)
1720*f7c14bbaSAndroid Build Coastguard Worker continue;
1721*f7c14bbaSAndroid Build Coastguard Worker
1722*f7c14bbaSAndroid Build Coastguard Worker if (btf_id && btf_id != vi->type) {
1723*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global/extern '%s' BTF is ambiguous: both types #%d and #%u match\n",
1724*f7c14bbaSAndroid Build Coastguard Worker sym_name, btf_id, vi->type);
1725*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1726*f7c14bbaSAndroid Build Coastguard Worker }
1727*f7c14bbaSAndroid Build Coastguard Worker
1728*f7c14bbaSAndroid Build Coastguard Worker *out_btf_sec_id = i;
1729*f7c14bbaSAndroid Build Coastguard Worker *out_btf_id = vi->type;
1730*f7c14bbaSAndroid Build Coastguard Worker
1731*f7c14bbaSAndroid Build Coastguard Worker return 0;
1732*f7c14bbaSAndroid Build Coastguard Worker }
1733*f7c14bbaSAndroid Build Coastguard Worker }
1734*f7c14bbaSAndroid Build Coastguard Worker
1735*f7c14bbaSAndroid Build Coastguard Worker /* free-floating extern or global FUNC */
1736*f7c14bbaSAndroid Build Coastguard Worker if (btf_id) {
1737*f7c14bbaSAndroid Build Coastguard Worker *out_btf_sec_id = 0;
1738*f7c14bbaSAndroid Build Coastguard Worker *out_btf_id = btf_id;
1739*f7c14bbaSAndroid Build Coastguard Worker return 0;
1740*f7c14bbaSAndroid Build Coastguard Worker }
1741*f7c14bbaSAndroid Build Coastguard Worker
1742*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find BTF info for global/extern symbol '%s'\n", sym_name);
1743*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1744*f7c14bbaSAndroid Build Coastguard Worker }
1745*f7c14bbaSAndroid Build Coastguard Worker
find_src_sec_by_name(struct src_obj * obj,const char * sec_name)1746*f7c14bbaSAndroid Build Coastguard Worker static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name)
1747*f7c14bbaSAndroid Build Coastguard Worker {
1748*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *sec;
1749*f7c14bbaSAndroid Build Coastguard Worker int i;
1750*f7c14bbaSAndroid Build Coastguard Worker
1751*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < obj->sec_cnt; i++) {
1752*f7c14bbaSAndroid Build Coastguard Worker sec = &obj->secs[i];
1753*f7c14bbaSAndroid Build Coastguard Worker
1754*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec->sec_name, sec_name) == 0)
1755*f7c14bbaSAndroid Build Coastguard Worker return sec;
1756*f7c14bbaSAndroid Build Coastguard Worker }
1757*f7c14bbaSAndroid Build Coastguard Worker
1758*f7c14bbaSAndroid Build Coastguard Worker return NULL;
1759*f7c14bbaSAndroid Build Coastguard Worker }
1760*f7c14bbaSAndroid Build Coastguard Worker
complete_extern_btf_info(struct btf * dst_btf,int dst_id,struct btf * src_btf,int src_id)1761*f7c14bbaSAndroid Build Coastguard Worker static int complete_extern_btf_info(struct btf *dst_btf, int dst_id,
1762*f7c14bbaSAndroid Build Coastguard Worker struct btf *src_btf, int src_id)
1763*f7c14bbaSAndroid Build Coastguard Worker {
1764*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *dst_t = btf_type_by_id(dst_btf, dst_id);
1765*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *src_t = btf_type_by_id(src_btf, src_id);
1766*f7c14bbaSAndroid Build Coastguard Worker struct btf_param *src_p, *dst_p;
1767*f7c14bbaSAndroid Build Coastguard Worker const char *s;
1768*f7c14bbaSAndroid Build Coastguard Worker int i, n, off;
1769*f7c14bbaSAndroid Build Coastguard Worker
1770*f7c14bbaSAndroid Build Coastguard Worker /* We already made sure that source and destination types (FUNC or
1771*f7c14bbaSAndroid Build Coastguard Worker * VAR) match in terms of types and argument names.
1772*f7c14bbaSAndroid Build Coastguard Worker */
1773*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_var(dst_t)) {
1774*f7c14bbaSAndroid Build Coastguard Worker btf_var(dst_t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
1775*f7c14bbaSAndroid Build Coastguard Worker return 0;
1776*f7c14bbaSAndroid Build Coastguard Worker }
1777*f7c14bbaSAndroid Build Coastguard Worker
1778*f7c14bbaSAndroid Build Coastguard Worker dst_t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_GLOBAL, 0);
1779*f7c14bbaSAndroid Build Coastguard Worker
1780*f7c14bbaSAndroid Build Coastguard Worker /* now onto FUNC_PROTO types */
1781*f7c14bbaSAndroid Build Coastguard Worker src_t = btf_type_by_id(src_btf, src_t->type);
1782*f7c14bbaSAndroid Build Coastguard Worker dst_t = btf_type_by_id(dst_btf, dst_t->type);
1783*f7c14bbaSAndroid Build Coastguard Worker
1784*f7c14bbaSAndroid Build Coastguard Worker /* Fill in all the argument names, which for extern FUNCs are missing.
1785*f7c14bbaSAndroid Build Coastguard Worker * We'll end up with two copies of FUNCs/VARs for externs, but that
1786*f7c14bbaSAndroid Build Coastguard Worker * will be taken care of by BTF dedup at the very end.
1787*f7c14bbaSAndroid Build Coastguard Worker * It might be that BTF types for extern in one file has less/more BTF
1788*f7c14bbaSAndroid Build Coastguard Worker * information (e.g., FWD instead of full STRUCT/UNION information),
1789*f7c14bbaSAndroid Build Coastguard Worker * but that should be (in most cases, subject to BTF dedup rules)
1790*f7c14bbaSAndroid Build Coastguard Worker * handled and resolved by BTF dedup algorithm as well, so we won't
1791*f7c14bbaSAndroid Build Coastguard Worker * worry about it. Our only job is to make sure that argument names
1792*f7c14bbaSAndroid Build Coastguard Worker * are populated on both sides, otherwise BTF dedup will pedantically
1793*f7c14bbaSAndroid Build Coastguard Worker * consider them different.
1794*f7c14bbaSAndroid Build Coastguard Worker */
1795*f7c14bbaSAndroid Build Coastguard Worker src_p = btf_params(src_t);
1796*f7c14bbaSAndroid Build Coastguard Worker dst_p = btf_params(dst_t);
1797*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, n = btf_vlen(dst_t); i < n; i++, src_p++, dst_p++) {
1798*f7c14bbaSAndroid Build Coastguard Worker if (!src_p->name_off)
1799*f7c14bbaSAndroid Build Coastguard Worker continue;
1800*f7c14bbaSAndroid Build Coastguard Worker
1801*f7c14bbaSAndroid Build Coastguard Worker /* src_btf has more complete info, so add name to dst_btf */
1802*f7c14bbaSAndroid Build Coastguard Worker s = btf__str_by_offset(src_btf, src_p->name_off);
1803*f7c14bbaSAndroid Build Coastguard Worker off = btf__add_str(dst_btf, s);
1804*f7c14bbaSAndroid Build Coastguard Worker if (off < 0)
1805*f7c14bbaSAndroid Build Coastguard Worker return off;
1806*f7c14bbaSAndroid Build Coastguard Worker dst_p->name_off = off;
1807*f7c14bbaSAndroid Build Coastguard Worker }
1808*f7c14bbaSAndroid Build Coastguard Worker return 0;
1809*f7c14bbaSAndroid Build Coastguard Worker }
1810*f7c14bbaSAndroid Build Coastguard Worker
sym_update_bind(Elf64_Sym * sym,int sym_bind)1811*f7c14bbaSAndroid Build Coastguard Worker static void sym_update_bind(Elf64_Sym *sym, int sym_bind)
1812*f7c14bbaSAndroid Build Coastguard Worker {
1813*f7c14bbaSAndroid Build Coastguard Worker sym->st_info = ELF64_ST_INFO(sym_bind, ELF64_ST_TYPE(sym->st_info));
1814*f7c14bbaSAndroid Build Coastguard Worker }
1815*f7c14bbaSAndroid Build Coastguard Worker
sym_update_type(Elf64_Sym * sym,int sym_type)1816*f7c14bbaSAndroid Build Coastguard Worker static void sym_update_type(Elf64_Sym *sym, int sym_type)
1817*f7c14bbaSAndroid Build Coastguard Worker {
1818*f7c14bbaSAndroid Build Coastguard Worker sym->st_info = ELF64_ST_INFO(ELF64_ST_BIND(sym->st_info), sym_type);
1819*f7c14bbaSAndroid Build Coastguard Worker }
1820*f7c14bbaSAndroid Build Coastguard Worker
sym_update_visibility(Elf64_Sym * sym,int sym_vis)1821*f7c14bbaSAndroid Build Coastguard Worker static void sym_update_visibility(Elf64_Sym *sym, int sym_vis)
1822*f7c14bbaSAndroid Build Coastguard Worker {
1823*f7c14bbaSAndroid Build Coastguard Worker /* libelf doesn't provide setters for ST_VISIBILITY,
1824*f7c14bbaSAndroid Build Coastguard Worker * but it is stored in the lower 2 bits of st_other
1825*f7c14bbaSAndroid Build Coastguard Worker */
1826*f7c14bbaSAndroid Build Coastguard Worker sym->st_other &= ~0x03;
1827*f7c14bbaSAndroid Build Coastguard Worker sym->st_other |= sym_vis;
1828*f7c14bbaSAndroid Build Coastguard Worker }
1829*f7c14bbaSAndroid Build Coastguard Worker
linker_append_elf_sym(struct bpf_linker * linker,struct src_obj * obj,Elf64_Sym * sym,const char * sym_name,int src_sym_idx)1830*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
1831*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym, const char *sym_name, int src_sym_idx)
1832*f7c14bbaSAndroid Build Coastguard Worker {
1833*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_sec = NULL;
1834*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *dst_sec = NULL;
1835*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_sym = NULL;
1836*f7c14bbaSAndroid Build Coastguard Worker int name_off, sym_type, sym_bind, sym_vis, err;
1837*f7c14bbaSAndroid Build Coastguard Worker int btf_sec_id = 0, btf_id = 0;
1838*f7c14bbaSAndroid Build Coastguard Worker size_t dst_sym_idx;
1839*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *dst_sym;
1840*f7c14bbaSAndroid Build Coastguard Worker bool sym_is_extern;
1841*f7c14bbaSAndroid Build Coastguard Worker
1842*f7c14bbaSAndroid Build Coastguard Worker sym_type = ELF64_ST_TYPE(sym->st_info);
1843*f7c14bbaSAndroid Build Coastguard Worker sym_bind = ELF64_ST_BIND(sym->st_info);
1844*f7c14bbaSAndroid Build Coastguard Worker sym_vis = ELF64_ST_VISIBILITY(sym->st_other);
1845*f7c14bbaSAndroid Build Coastguard Worker sym_is_extern = sym->st_shndx == SHN_UNDEF;
1846*f7c14bbaSAndroid Build Coastguard Worker
1847*f7c14bbaSAndroid Build Coastguard Worker if (sym_is_extern) {
1848*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf) {
1849*f7c14bbaSAndroid Build Coastguard Worker pr_warn("externs without BTF info are not supported\n");
1850*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1851*f7c14bbaSAndroid Build Coastguard Worker }
1852*f7c14bbaSAndroid Build Coastguard Worker } else if (sym->st_shndx < SHN_LORESERVE) {
1853*f7c14bbaSAndroid Build Coastguard Worker src_sec = &obj->secs[sym->st_shndx];
1854*f7c14bbaSAndroid Build Coastguard Worker if (src_sec->skipped)
1855*f7c14bbaSAndroid Build Coastguard Worker return 0;
1856*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
1857*f7c14bbaSAndroid Build Coastguard Worker
1858*f7c14bbaSAndroid Build Coastguard Worker /* allow only one STT_SECTION symbol per section */
1859*f7c14bbaSAndroid Build Coastguard Worker if (sym_type == STT_SECTION && dst_sec->sec_sym_idx) {
1860*f7c14bbaSAndroid Build Coastguard Worker obj->sym_map[src_sym_idx] = dst_sec->sec_sym_idx;
1861*f7c14bbaSAndroid Build Coastguard Worker return 0;
1862*f7c14bbaSAndroid Build Coastguard Worker }
1863*f7c14bbaSAndroid Build Coastguard Worker }
1864*f7c14bbaSAndroid Build Coastguard Worker
1865*f7c14bbaSAndroid Build Coastguard Worker if (sym_bind == STB_LOCAL)
1866*f7c14bbaSAndroid Build Coastguard Worker goto add_sym;
1867*f7c14bbaSAndroid Build Coastguard Worker
1868*f7c14bbaSAndroid Build Coastguard Worker /* find matching BTF info */
1869*f7c14bbaSAndroid Build Coastguard Worker err = find_glob_sym_btf(obj, sym, sym_name, &btf_sec_id, &btf_id);
1870*f7c14bbaSAndroid Build Coastguard Worker if (err)
1871*f7c14bbaSAndroid Build Coastguard Worker return err;
1872*f7c14bbaSAndroid Build Coastguard Worker
1873*f7c14bbaSAndroid Build Coastguard Worker if (sym_is_extern && btf_sec_id) {
1874*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name = NULL;
1875*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
1876*f7c14bbaSAndroid Build Coastguard Worker
1877*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, btf_sec_id);
1878*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__str_by_offset(obj->btf, t->name_off);
1879*f7c14bbaSAndroid Build Coastguard Worker
1880*f7c14bbaSAndroid Build Coastguard Worker /* Clang puts unannotated extern vars into
1881*f7c14bbaSAndroid Build Coastguard Worker * '.extern' BTF DATASEC. Treat them the same
1882*f7c14bbaSAndroid Build Coastguard Worker * as unannotated extern funcs (which are
1883*f7c14bbaSAndroid Build Coastguard Worker * currently not put into any DATASECs).
1884*f7c14bbaSAndroid Build Coastguard Worker * Those don't have associated src_sec/dst_sec.
1885*f7c14bbaSAndroid Build Coastguard Worker */
1886*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, BTF_EXTERN_SEC) != 0) {
1887*f7c14bbaSAndroid Build Coastguard Worker src_sec = find_src_sec_by_name(obj, sec_name);
1888*f7c14bbaSAndroid Build Coastguard Worker if (!src_sec) {
1889*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find matching ELF sec '%s'\n", sec_name);
1890*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1891*f7c14bbaSAndroid Build Coastguard Worker }
1892*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
1893*f7c14bbaSAndroid Build Coastguard Worker }
1894*f7c14bbaSAndroid Build Coastguard Worker }
1895*f7c14bbaSAndroid Build Coastguard Worker
1896*f7c14bbaSAndroid Build Coastguard Worker glob_sym = find_glob_sym(linker, sym_name);
1897*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym) {
1898*f7c14bbaSAndroid Build Coastguard Worker /* Preventively resolve to existing symbol. This is
1899*f7c14bbaSAndroid Build Coastguard Worker * needed for further relocation symbol remapping in
1900*f7c14bbaSAndroid Build Coastguard Worker * the next step of linking.
1901*f7c14bbaSAndroid Build Coastguard Worker */
1902*f7c14bbaSAndroid Build Coastguard Worker obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
1903*f7c14bbaSAndroid Build Coastguard Worker
1904*f7c14bbaSAndroid Build Coastguard Worker /* If both symbols are non-externs, at least one of
1905*f7c14bbaSAndroid Build Coastguard Worker * them has to be STB_WEAK, otherwise they are in
1906*f7c14bbaSAndroid Build Coastguard Worker * a conflict with each other.
1907*f7c14bbaSAndroid Build Coastguard Worker */
1908*f7c14bbaSAndroid Build Coastguard Worker if (!sym_is_extern && !glob_sym->is_extern
1909*f7c14bbaSAndroid Build Coastguard Worker && !glob_sym->is_weak && sym_bind != STB_WEAK) {
1910*f7c14bbaSAndroid Build Coastguard Worker pr_warn("conflicting non-weak symbol #%d (%s) definition in '%s'\n",
1911*f7c14bbaSAndroid Build Coastguard Worker src_sym_idx, sym_name, obj->filename);
1912*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1913*f7c14bbaSAndroid Build Coastguard Worker }
1914*f7c14bbaSAndroid Build Coastguard Worker
1915*f7c14bbaSAndroid Build Coastguard Worker if (!glob_syms_match(sym_name, linker, glob_sym, obj, sym, src_sym_idx, btf_id))
1916*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1917*f7c14bbaSAndroid Build Coastguard Worker
1918*f7c14bbaSAndroid Build Coastguard Worker dst_sym = get_sym_by_idx(linker, glob_sym->sym_idx);
1919*f7c14bbaSAndroid Build Coastguard Worker
1920*f7c14bbaSAndroid Build Coastguard Worker /* If new symbol is strong, then force dst_sym to be strong as
1921*f7c14bbaSAndroid Build Coastguard Worker * well; this way a mix of weak and non-weak extern
1922*f7c14bbaSAndroid Build Coastguard Worker * definitions will end up being strong.
1923*f7c14bbaSAndroid Build Coastguard Worker */
1924*f7c14bbaSAndroid Build Coastguard Worker if (sym_bind == STB_GLOBAL) {
1925*f7c14bbaSAndroid Build Coastguard Worker /* We still need to preserve type (NOTYPE or
1926*f7c14bbaSAndroid Build Coastguard Worker * OBJECT/FUNC, depending on whether the symbol is
1927*f7c14bbaSAndroid Build Coastguard Worker * extern or not)
1928*f7c14bbaSAndroid Build Coastguard Worker */
1929*f7c14bbaSAndroid Build Coastguard Worker sym_update_bind(dst_sym, STB_GLOBAL);
1930*f7c14bbaSAndroid Build Coastguard Worker glob_sym->is_weak = false;
1931*f7c14bbaSAndroid Build Coastguard Worker }
1932*f7c14bbaSAndroid Build Coastguard Worker
1933*f7c14bbaSAndroid Build Coastguard Worker /* Non-default visibility is "contaminating", with stricter
1934*f7c14bbaSAndroid Build Coastguard Worker * visibility overwriting more permissive ones, even if more
1935*f7c14bbaSAndroid Build Coastguard Worker * permissive visibility comes from just an extern definition.
1936*f7c14bbaSAndroid Build Coastguard Worker * Currently only STV_DEFAULT and STV_HIDDEN are allowed and
1937*f7c14bbaSAndroid Build Coastguard Worker * ensured by ELF symbol sanity checks above.
1938*f7c14bbaSAndroid Build Coastguard Worker */
1939*f7c14bbaSAndroid Build Coastguard Worker if (sym_vis > ELF64_ST_VISIBILITY(dst_sym->st_other))
1940*f7c14bbaSAndroid Build Coastguard Worker sym_update_visibility(dst_sym, sym_vis);
1941*f7c14bbaSAndroid Build Coastguard Worker
1942*f7c14bbaSAndroid Build Coastguard Worker /* If the new symbol is extern, then regardless if
1943*f7c14bbaSAndroid Build Coastguard Worker * existing symbol is extern or resolved global, just
1944*f7c14bbaSAndroid Build Coastguard Worker * keep the existing one untouched.
1945*f7c14bbaSAndroid Build Coastguard Worker */
1946*f7c14bbaSAndroid Build Coastguard Worker if (sym_is_extern)
1947*f7c14bbaSAndroid Build Coastguard Worker return 0;
1948*f7c14bbaSAndroid Build Coastguard Worker
1949*f7c14bbaSAndroid Build Coastguard Worker /* If existing symbol is a strong resolved symbol, bail out,
1950*f7c14bbaSAndroid Build Coastguard Worker * because we lost resolution battle have nothing to
1951*f7c14bbaSAndroid Build Coastguard Worker * contribute. We already checked abover that there is no
1952*f7c14bbaSAndroid Build Coastguard Worker * strong-strong conflict. We also already tightened binding
1953*f7c14bbaSAndroid Build Coastguard Worker * and visibility, so nothing else to contribute at that point.
1954*f7c14bbaSAndroid Build Coastguard Worker */
1955*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym->is_extern && sym_bind == STB_WEAK)
1956*f7c14bbaSAndroid Build Coastguard Worker return 0;
1957*f7c14bbaSAndroid Build Coastguard Worker
1958*f7c14bbaSAndroid Build Coastguard Worker /* At this point, new symbol is strong non-extern,
1959*f7c14bbaSAndroid Build Coastguard Worker * so overwrite glob_sym with new symbol information.
1960*f7c14bbaSAndroid Build Coastguard Worker * Preserve binding and visibility.
1961*f7c14bbaSAndroid Build Coastguard Worker */
1962*f7c14bbaSAndroid Build Coastguard Worker sym_update_type(dst_sym, sym_type);
1963*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_shndx = dst_sec->sec_idx;
1964*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_value = src_sec->dst_off + sym->st_value;
1965*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_size = sym->st_size;
1966*f7c14bbaSAndroid Build Coastguard Worker
1967*f7c14bbaSAndroid Build Coastguard Worker /* see comment below about dst_sec->id vs dst_sec->sec_idx */
1968*f7c14bbaSAndroid Build Coastguard Worker glob_sym->sec_id = dst_sec->id;
1969*f7c14bbaSAndroid Build Coastguard Worker glob_sym->is_extern = false;
1970*f7c14bbaSAndroid Build Coastguard Worker
1971*f7c14bbaSAndroid Build Coastguard Worker if (complete_extern_btf_info(linker->btf, glob_sym->btf_id,
1972*f7c14bbaSAndroid Build Coastguard Worker obj->btf, btf_id))
1973*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1974*f7c14bbaSAndroid Build Coastguard Worker
1975*f7c14bbaSAndroid Build Coastguard Worker /* request updating VAR's/FUNC's underlying BTF type when appending BTF type */
1976*f7c14bbaSAndroid Build Coastguard Worker glob_sym->underlying_btf_id = 0;
1977*f7c14bbaSAndroid Build Coastguard Worker
1978*f7c14bbaSAndroid Build Coastguard Worker obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
1979*f7c14bbaSAndroid Build Coastguard Worker return 0;
1980*f7c14bbaSAndroid Build Coastguard Worker }
1981*f7c14bbaSAndroid Build Coastguard Worker
1982*f7c14bbaSAndroid Build Coastguard Worker add_sym:
1983*f7c14bbaSAndroid Build Coastguard Worker name_off = strset__add_str(linker->strtab_strs, sym_name);
1984*f7c14bbaSAndroid Build Coastguard Worker if (name_off < 0)
1985*f7c14bbaSAndroid Build Coastguard Worker return name_off;
1986*f7c14bbaSAndroid Build Coastguard Worker
1987*f7c14bbaSAndroid Build Coastguard Worker dst_sym = add_new_sym(linker, &dst_sym_idx);
1988*f7c14bbaSAndroid Build Coastguard Worker if (!dst_sym)
1989*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1990*f7c14bbaSAndroid Build Coastguard Worker
1991*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_name = name_off;
1992*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_info = sym->st_info;
1993*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_other = sym->st_other;
1994*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_shndx = dst_sec ? dst_sec->sec_idx : sym->st_shndx;
1995*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_value = (src_sec ? src_sec->dst_off : 0) + sym->st_value;
1996*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_size = sym->st_size;
1997*f7c14bbaSAndroid Build Coastguard Worker
1998*f7c14bbaSAndroid Build Coastguard Worker obj->sym_map[src_sym_idx] = dst_sym_idx;
1999*f7c14bbaSAndroid Build Coastguard Worker
2000*f7c14bbaSAndroid Build Coastguard Worker if (sym_type == STT_SECTION && dst_sym) {
2001*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_sym_idx = dst_sym_idx;
2002*f7c14bbaSAndroid Build Coastguard Worker dst_sym->st_value = 0;
2003*f7c14bbaSAndroid Build Coastguard Worker }
2004*f7c14bbaSAndroid Build Coastguard Worker
2005*f7c14bbaSAndroid Build Coastguard Worker if (sym_bind != STB_LOCAL) {
2006*f7c14bbaSAndroid Build Coastguard Worker glob_sym = add_glob_sym(linker);
2007*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym)
2008*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2009*f7c14bbaSAndroid Build Coastguard Worker
2010*f7c14bbaSAndroid Build Coastguard Worker glob_sym->sym_idx = dst_sym_idx;
2011*f7c14bbaSAndroid Build Coastguard Worker /* we use dst_sec->id (and not dst_sec->sec_idx), because
2012*f7c14bbaSAndroid Build Coastguard Worker * ephemeral sections (.kconfig, .ksyms, etc) don't have
2013*f7c14bbaSAndroid Build Coastguard Worker * sec_idx (as they don't have corresponding ELF section), but
2014*f7c14bbaSAndroid Build Coastguard Worker * still have id. .extern doesn't have even ephemeral section
2015*f7c14bbaSAndroid Build Coastguard Worker * associated with it, so dst_sec->id == dst_sec->sec_idx == 0.
2016*f7c14bbaSAndroid Build Coastguard Worker */
2017*f7c14bbaSAndroid Build Coastguard Worker glob_sym->sec_id = dst_sec ? dst_sec->id : 0;
2018*f7c14bbaSAndroid Build Coastguard Worker glob_sym->name_off = name_off;
2019*f7c14bbaSAndroid Build Coastguard Worker /* we will fill btf_id in during BTF merging step */
2020*f7c14bbaSAndroid Build Coastguard Worker glob_sym->btf_id = 0;
2021*f7c14bbaSAndroid Build Coastguard Worker glob_sym->is_extern = sym_is_extern;
2022*f7c14bbaSAndroid Build Coastguard Worker glob_sym->is_weak = sym_bind == STB_WEAK;
2023*f7c14bbaSAndroid Build Coastguard Worker }
2024*f7c14bbaSAndroid Build Coastguard Worker
2025*f7c14bbaSAndroid Build Coastguard Worker return 0;
2026*f7c14bbaSAndroid Build Coastguard Worker }
2027*f7c14bbaSAndroid Build Coastguard Worker
linker_append_elf_relos(struct bpf_linker * linker,struct src_obj * obj)2028*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj)
2029*f7c14bbaSAndroid Build Coastguard Worker {
2030*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx];
2031*f7c14bbaSAndroid Build Coastguard Worker int i, err;
2032*f7c14bbaSAndroid Build Coastguard Worker
2033*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < obj->sec_cnt; i++) {
2034*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_sec, *src_linked_sec;
2035*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *dst_sec, *dst_linked_sec;
2036*f7c14bbaSAndroid Build Coastguard Worker Elf64_Rel *src_rel, *dst_rel;
2037*f7c14bbaSAndroid Build Coastguard Worker int j, n;
2038*f7c14bbaSAndroid Build Coastguard Worker
2039*f7c14bbaSAndroid Build Coastguard Worker src_sec = &obj->secs[i];
2040*f7c14bbaSAndroid Build Coastguard Worker if (!is_relo_sec(src_sec))
2041*f7c14bbaSAndroid Build Coastguard Worker continue;
2042*f7c14bbaSAndroid Build Coastguard Worker
2043*f7c14bbaSAndroid Build Coastguard Worker /* shdr->sh_info points to relocatable section */
2044*f7c14bbaSAndroid Build Coastguard Worker src_linked_sec = &obj->secs[src_sec->shdr->sh_info];
2045*f7c14bbaSAndroid Build Coastguard Worker if (src_linked_sec->skipped)
2046*f7c14bbaSAndroid Build Coastguard Worker continue;
2047*f7c14bbaSAndroid Build Coastguard Worker
2048*f7c14bbaSAndroid Build Coastguard Worker dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
2049*f7c14bbaSAndroid Build Coastguard Worker if (!dst_sec) {
2050*f7c14bbaSAndroid Build Coastguard Worker dst_sec = add_dst_sec(linker, src_sec->sec_name);
2051*f7c14bbaSAndroid Build Coastguard Worker if (!dst_sec)
2052*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2053*f7c14bbaSAndroid Build Coastguard Worker err = init_sec(linker, dst_sec, src_sec);
2054*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2055*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to init section '%s'\n", src_sec->sec_name);
2056*f7c14bbaSAndroid Build Coastguard Worker return err;
2057*f7c14bbaSAndroid Build Coastguard Worker }
2058*f7c14bbaSAndroid Build Coastguard Worker } else if (!secs_match(dst_sec, src_sec)) {
2059*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sections %s are not compatible\n", src_sec->sec_name);
2060*f7c14bbaSAndroid Build Coastguard Worker return -1;
2061*f7c14bbaSAndroid Build Coastguard Worker }
2062*f7c14bbaSAndroid Build Coastguard Worker
2063*f7c14bbaSAndroid Build Coastguard Worker /* shdr->sh_link points to SYMTAB */
2064*f7c14bbaSAndroid Build Coastguard Worker dst_sec->shdr->sh_link = linker->symtab_sec_idx;
2065*f7c14bbaSAndroid Build Coastguard Worker
2066*f7c14bbaSAndroid Build Coastguard Worker /* shdr->sh_info points to relocated section */
2067*f7c14bbaSAndroid Build Coastguard Worker dst_linked_sec = &linker->secs[src_linked_sec->dst_id];
2068*f7c14bbaSAndroid Build Coastguard Worker dst_sec->shdr->sh_info = dst_linked_sec->sec_idx;
2069*f7c14bbaSAndroid Build Coastguard Worker
2070*f7c14bbaSAndroid Build Coastguard Worker src_sec->dst_id = dst_sec->id;
2071*f7c14bbaSAndroid Build Coastguard Worker err = extend_sec(linker, dst_sec, src_sec);
2072*f7c14bbaSAndroid Build Coastguard Worker if (err)
2073*f7c14bbaSAndroid Build Coastguard Worker return err;
2074*f7c14bbaSAndroid Build Coastguard Worker
2075*f7c14bbaSAndroid Build Coastguard Worker src_rel = src_sec->data->d_buf;
2076*f7c14bbaSAndroid Build Coastguard Worker dst_rel = dst_sec->raw_data + src_sec->dst_off;
2077*f7c14bbaSAndroid Build Coastguard Worker n = src_sec->shdr->sh_size / src_sec->shdr->sh_entsize;
2078*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < n; j++, src_rel++, dst_rel++) {
2079*f7c14bbaSAndroid Build Coastguard Worker size_t src_sym_idx, dst_sym_idx, sym_type;
2080*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *src_sym;
2081*f7c14bbaSAndroid Build Coastguard Worker
2082*f7c14bbaSAndroid Build Coastguard Worker src_sym_idx = ELF64_R_SYM(src_rel->r_info);
2083*f7c14bbaSAndroid Build Coastguard Worker src_sym = src_symtab->data->d_buf + sizeof(*src_sym) * src_sym_idx;
2084*f7c14bbaSAndroid Build Coastguard Worker
2085*f7c14bbaSAndroid Build Coastguard Worker dst_sym_idx = obj->sym_map[src_sym_idx];
2086*f7c14bbaSAndroid Build Coastguard Worker dst_rel->r_offset += src_linked_sec->dst_off;
2087*f7c14bbaSAndroid Build Coastguard Worker sym_type = ELF64_R_TYPE(src_rel->r_info);
2088*f7c14bbaSAndroid Build Coastguard Worker dst_rel->r_info = ELF64_R_INFO(dst_sym_idx, sym_type);
2089*f7c14bbaSAndroid Build Coastguard Worker
2090*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
2091*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *sec = &obj->secs[src_sym->st_shndx];
2092*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn;
2093*f7c14bbaSAndroid Build Coastguard Worker
2094*f7c14bbaSAndroid Build Coastguard Worker if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
2095*f7c14bbaSAndroid Build Coastguard Worker /* calls to the very first static function inside
2096*f7c14bbaSAndroid Build Coastguard Worker * .text section at offset 0 will
2097*f7c14bbaSAndroid Build Coastguard Worker * reference section symbol, not the
2098*f7c14bbaSAndroid Build Coastguard Worker * function symbol. Fix that up,
2099*f7c14bbaSAndroid Build Coastguard Worker * otherwise it won't be possible to
2100*f7c14bbaSAndroid Build Coastguard Worker * relocate calls to two different
2101*f7c14bbaSAndroid Build Coastguard Worker * static functions with the same name
2102*f7c14bbaSAndroid Build Coastguard Worker * (rom two different object files)
2103*f7c14bbaSAndroid Build Coastguard Worker */
2104*f7c14bbaSAndroid Build Coastguard Worker insn = dst_linked_sec->raw_data + dst_rel->r_offset;
2105*f7c14bbaSAndroid Build Coastguard Worker if (insn->code == (BPF_JMP | BPF_CALL))
2106*f7c14bbaSAndroid Build Coastguard Worker insn->imm += sec->dst_off / sizeof(struct bpf_insn);
2107*f7c14bbaSAndroid Build Coastguard Worker else
2108*f7c14bbaSAndroid Build Coastguard Worker insn->imm += sec->dst_off;
2109*f7c14bbaSAndroid Build Coastguard Worker } else {
2110*f7c14bbaSAndroid Build Coastguard Worker pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
2111*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2112*f7c14bbaSAndroid Build Coastguard Worker }
2113*f7c14bbaSAndroid Build Coastguard Worker }
2114*f7c14bbaSAndroid Build Coastguard Worker
2115*f7c14bbaSAndroid Build Coastguard Worker }
2116*f7c14bbaSAndroid Build Coastguard Worker }
2117*f7c14bbaSAndroid Build Coastguard Worker
2118*f7c14bbaSAndroid Build Coastguard Worker return 0;
2119*f7c14bbaSAndroid Build Coastguard Worker }
2120*f7c14bbaSAndroid Build Coastguard Worker
find_sym_by_name(struct src_obj * obj,size_t sec_idx,int sym_type,const char * sym_name)2121*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx,
2122*f7c14bbaSAndroid Build Coastguard Worker int sym_type, const char *sym_name)
2123*f7c14bbaSAndroid Build Coastguard Worker {
2124*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
2125*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym = symtab->data->d_buf;
2126*f7c14bbaSAndroid Build Coastguard Worker int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize;
2127*f7c14bbaSAndroid Build Coastguard Worker int str_sec_idx = symtab->shdr->sh_link;
2128*f7c14bbaSAndroid Build Coastguard Worker const char *name;
2129*f7c14bbaSAndroid Build Coastguard Worker
2130*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++, sym++) {
2131*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx != sec_idx)
2132*f7c14bbaSAndroid Build Coastguard Worker continue;
2133*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_TYPE(sym->st_info) != sym_type)
2134*f7c14bbaSAndroid Build Coastguard Worker continue;
2135*f7c14bbaSAndroid Build Coastguard Worker
2136*f7c14bbaSAndroid Build Coastguard Worker name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
2137*f7c14bbaSAndroid Build Coastguard Worker if (!name)
2138*f7c14bbaSAndroid Build Coastguard Worker return NULL;
2139*f7c14bbaSAndroid Build Coastguard Worker
2140*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sym_name, name) != 0)
2141*f7c14bbaSAndroid Build Coastguard Worker continue;
2142*f7c14bbaSAndroid Build Coastguard Worker
2143*f7c14bbaSAndroid Build Coastguard Worker return sym;
2144*f7c14bbaSAndroid Build Coastguard Worker }
2145*f7c14bbaSAndroid Build Coastguard Worker
2146*f7c14bbaSAndroid Build Coastguard Worker return NULL;
2147*f7c14bbaSAndroid Build Coastguard Worker }
2148*f7c14bbaSAndroid Build Coastguard Worker
linker_fixup_btf(struct src_obj * obj)2149*f7c14bbaSAndroid Build Coastguard Worker static int linker_fixup_btf(struct src_obj *obj)
2150*f7c14bbaSAndroid Build Coastguard Worker {
2151*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
2152*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *sec;
2153*f7c14bbaSAndroid Build Coastguard Worker int i, j, n, m;
2154*f7c14bbaSAndroid Build Coastguard Worker
2155*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
2156*f7c14bbaSAndroid Build Coastguard Worker return 0;
2157*f7c14bbaSAndroid Build Coastguard Worker
2158*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
2159*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
2160*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vi;
2161*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t;
2162*f7c14bbaSAndroid Build Coastguard Worker
2163*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(obj->btf, i);
2164*f7c14bbaSAndroid Build Coastguard Worker if (btf_kind(t) != BTF_KIND_DATASEC)
2165*f7c14bbaSAndroid Build Coastguard Worker continue;
2166*f7c14bbaSAndroid Build Coastguard Worker
2167*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__str_by_offset(obj->btf, t->name_off);
2168*f7c14bbaSAndroid Build Coastguard Worker sec = find_src_sec_by_name(obj, sec_name);
2169*f7c14bbaSAndroid Build Coastguard Worker if (sec) {
2170*f7c14bbaSAndroid Build Coastguard Worker /* record actual section size, unless ephemeral */
2171*f7c14bbaSAndroid Build Coastguard Worker if (sec->shdr)
2172*f7c14bbaSAndroid Build Coastguard Worker t->size = sec->shdr->sh_size;
2173*f7c14bbaSAndroid Build Coastguard Worker } else {
2174*f7c14bbaSAndroid Build Coastguard Worker /* BTF can have some sections that are not represented
2175*f7c14bbaSAndroid Build Coastguard Worker * in ELF, e.g., .kconfig, .ksyms, .extern, which are used
2176*f7c14bbaSAndroid Build Coastguard Worker * for special extern variables.
2177*f7c14bbaSAndroid Build Coastguard Worker *
2178*f7c14bbaSAndroid Build Coastguard Worker * For all but one such special (ephemeral)
2179*f7c14bbaSAndroid Build Coastguard Worker * sections, we pre-create "section shells" to be able
2180*f7c14bbaSAndroid Build Coastguard Worker * to keep track of extra per-section metadata later
2181*f7c14bbaSAndroid Build Coastguard Worker * (e.g., those BTF extern variables).
2182*f7c14bbaSAndroid Build Coastguard Worker *
2183*f7c14bbaSAndroid Build Coastguard Worker * .extern is even more special, though, because it
2184*f7c14bbaSAndroid Build Coastguard Worker * contains extern variables that need to be resolved
2185*f7c14bbaSAndroid Build Coastguard Worker * by static linker, not libbpf and kernel. When such
2186*f7c14bbaSAndroid Build Coastguard Worker * externs are resolved, we are going to remove them
2187*f7c14bbaSAndroid Build Coastguard Worker * from .extern BTF section and might end up not
2188*f7c14bbaSAndroid Build Coastguard Worker * needing it at all. Each resolved extern should have
2189*f7c14bbaSAndroid Build Coastguard Worker * matching non-extern VAR/FUNC in other sections.
2190*f7c14bbaSAndroid Build Coastguard Worker *
2191*f7c14bbaSAndroid Build Coastguard Worker * We do support leaving some of the externs
2192*f7c14bbaSAndroid Build Coastguard Worker * unresolved, though, to support cases of building
2193*f7c14bbaSAndroid Build Coastguard Worker * libraries, which will later be linked against final
2194*f7c14bbaSAndroid Build Coastguard Worker * BPF applications. So if at finalization we still
2195*f7c14bbaSAndroid Build Coastguard Worker * see unresolved externs, we'll create .extern
2196*f7c14bbaSAndroid Build Coastguard Worker * section on our own.
2197*f7c14bbaSAndroid Build Coastguard Worker */
2198*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, BTF_EXTERN_SEC) == 0)
2199*f7c14bbaSAndroid Build Coastguard Worker continue;
2200*f7c14bbaSAndroid Build Coastguard Worker
2201*f7c14bbaSAndroid Build Coastguard Worker sec = add_src_sec(obj, sec_name);
2202*f7c14bbaSAndroid Build Coastguard Worker if (!sec)
2203*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2204*f7c14bbaSAndroid Build Coastguard Worker
2205*f7c14bbaSAndroid Build Coastguard Worker sec->ephemeral = true;
2206*f7c14bbaSAndroid Build Coastguard Worker sec->sec_idx = 0; /* will match UNDEF shndx in ELF */
2207*f7c14bbaSAndroid Build Coastguard Worker }
2208*f7c14bbaSAndroid Build Coastguard Worker
2209*f7c14bbaSAndroid Build Coastguard Worker /* remember ELF section and its BTF type ID match */
2210*f7c14bbaSAndroid Build Coastguard Worker sec->sec_type_id = i;
2211*f7c14bbaSAndroid Build Coastguard Worker
2212*f7c14bbaSAndroid Build Coastguard Worker /* fix up variable offsets */
2213*f7c14bbaSAndroid Build Coastguard Worker vi = btf_var_secinfos(t);
2214*f7c14bbaSAndroid Build Coastguard Worker for (j = 0, m = btf_vlen(t); j < m; j++, vi++) {
2215*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type);
2216*f7c14bbaSAndroid Build Coastguard Worker const char *var_name = btf__str_by_offset(obj->btf, vt->name_off);
2217*f7c14bbaSAndroid Build Coastguard Worker int var_linkage = btf_var(vt)->linkage;
2218*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
2219*f7c14bbaSAndroid Build Coastguard Worker
2220*f7c14bbaSAndroid Build Coastguard Worker /* no need to patch up static or extern vars */
2221*f7c14bbaSAndroid Build Coastguard Worker if (var_linkage != BTF_VAR_GLOBAL_ALLOCATED)
2222*f7c14bbaSAndroid Build Coastguard Worker continue;
2223*f7c14bbaSAndroid Build Coastguard Worker
2224*f7c14bbaSAndroid Build Coastguard Worker sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name);
2225*f7c14bbaSAndroid Build Coastguard Worker if (!sym) {
2226*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find symbol for variable '%s' in section '%s'\n", var_name, sec_name);
2227*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
2228*f7c14bbaSAndroid Build Coastguard Worker }
2229*f7c14bbaSAndroid Build Coastguard Worker
2230*f7c14bbaSAndroid Build Coastguard Worker vi->offset = sym->st_value;
2231*f7c14bbaSAndroid Build Coastguard Worker }
2232*f7c14bbaSAndroid Build Coastguard Worker }
2233*f7c14bbaSAndroid Build Coastguard Worker
2234*f7c14bbaSAndroid Build Coastguard Worker return 0;
2235*f7c14bbaSAndroid Build Coastguard Worker }
2236*f7c14bbaSAndroid Build Coastguard Worker
remap_type_id(__u32 * type_id,void * ctx)2237*f7c14bbaSAndroid Build Coastguard Worker static int remap_type_id(__u32 *type_id, void *ctx)
2238*f7c14bbaSAndroid Build Coastguard Worker {
2239*f7c14bbaSAndroid Build Coastguard Worker int *id_map = ctx;
2240*f7c14bbaSAndroid Build Coastguard Worker int new_id = id_map[*type_id];
2241*f7c14bbaSAndroid Build Coastguard Worker
2242*f7c14bbaSAndroid Build Coastguard Worker /* Error out if the type wasn't remapped. Ignore VOID which stays VOID. */
2243*f7c14bbaSAndroid Build Coastguard Worker if (new_id == 0 && *type_id != 0) {
2244*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find new ID mapping for original BTF type ID %u\n", *type_id);
2245*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2246*f7c14bbaSAndroid Build Coastguard Worker }
2247*f7c14bbaSAndroid Build Coastguard Worker
2248*f7c14bbaSAndroid Build Coastguard Worker *type_id = id_map[*type_id];
2249*f7c14bbaSAndroid Build Coastguard Worker
2250*f7c14bbaSAndroid Build Coastguard Worker return 0;
2251*f7c14bbaSAndroid Build Coastguard Worker }
2252*f7c14bbaSAndroid Build Coastguard Worker
linker_append_btf(struct bpf_linker * linker,struct src_obj * obj)2253*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
2254*f7c14bbaSAndroid Build Coastguard Worker {
2255*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
2256*f7c14bbaSAndroid Build Coastguard Worker int i, j, n, start_id, id;
2257*f7c14bbaSAndroid Build Coastguard Worker const char *name;
2258*f7c14bbaSAndroid Build Coastguard Worker
2259*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
2260*f7c14bbaSAndroid Build Coastguard Worker return 0;
2261*f7c14bbaSAndroid Build Coastguard Worker
2262*f7c14bbaSAndroid Build Coastguard Worker start_id = btf__type_cnt(linker->btf);
2263*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
2264*f7c14bbaSAndroid Build Coastguard Worker
2265*f7c14bbaSAndroid Build Coastguard Worker obj->btf_type_map = calloc(n + 1, sizeof(int));
2266*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_type_map)
2267*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2268*f7c14bbaSAndroid Build Coastguard Worker
2269*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
2270*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_sym = NULL;
2271*f7c14bbaSAndroid Build Coastguard Worker
2272*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, i);
2273*f7c14bbaSAndroid Build Coastguard Worker
2274*f7c14bbaSAndroid Build Coastguard Worker /* DATASECs are handled specially below */
2275*f7c14bbaSAndroid Build Coastguard Worker if (btf_kind(t) == BTF_KIND_DATASEC)
2276*f7c14bbaSAndroid Build Coastguard Worker continue;
2277*f7c14bbaSAndroid Build Coastguard Worker
2278*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_non_static(t)) {
2279*f7c14bbaSAndroid Build Coastguard Worker /* there should be glob_sym already */
2280*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(obj->btf, t->name_off);
2281*f7c14bbaSAndroid Build Coastguard Worker glob_sym = find_glob_sym(linker, name);
2282*f7c14bbaSAndroid Build Coastguard Worker
2283*f7c14bbaSAndroid Build Coastguard Worker /* VARs without corresponding glob_sym are those that
2284*f7c14bbaSAndroid Build Coastguard Worker * belong to skipped/deduplicated sections (i.e.,
2285*f7c14bbaSAndroid Build Coastguard Worker * license and version), so just skip them
2286*f7c14bbaSAndroid Build Coastguard Worker */
2287*f7c14bbaSAndroid Build Coastguard Worker if (!glob_sym)
2288*f7c14bbaSAndroid Build Coastguard Worker continue;
2289*f7c14bbaSAndroid Build Coastguard Worker
2290*f7c14bbaSAndroid Build Coastguard Worker /* linker_append_elf_sym() might have requested
2291*f7c14bbaSAndroid Build Coastguard Worker * updating underlying type ID, if extern was resolved
2292*f7c14bbaSAndroid Build Coastguard Worker * to strong symbol or weak got upgraded to non-weak
2293*f7c14bbaSAndroid Build Coastguard Worker */
2294*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym->underlying_btf_id == 0)
2295*f7c14bbaSAndroid Build Coastguard Worker glob_sym->underlying_btf_id = -t->type;
2296*f7c14bbaSAndroid Build Coastguard Worker
2297*f7c14bbaSAndroid Build Coastguard Worker /* globals from previous object files that match our
2298*f7c14bbaSAndroid Build Coastguard Worker * VAR/FUNC already have a corresponding associated
2299*f7c14bbaSAndroid Build Coastguard Worker * BTF type, so just make sure to use it
2300*f7c14bbaSAndroid Build Coastguard Worker */
2301*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym->btf_id) {
2302*f7c14bbaSAndroid Build Coastguard Worker /* reuse existing BTF type for global var/func */
2303*f7c14bbaSAndroid Build Coastguard Worker obj->btf_type_map[i] = glob_sym->btf_id;
2304*f7c14bbaSAndroid Build Coastguard Worker continue;
2305*f7c14bbaSAndroid Build Coastguard Worker }
2306*f7c14bbaSAndroid Build Coastguard Worker }
2307*f7c14bbaSAndroid Build Coastguard Worker
2308*f7c14bbaSAndroid Build Coastguard Worker id = btf__add_type(linker->btf, obj->btf, t);
2309*f7c14bbaSAndroid Build Coastguard Worker if (id < 0) {
2310*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename);
2311*f7c14bbaSAndroid Build Coastguard Worker return id;
2312*f7c14bbaSAndroid Build Coastguard Worker }
2313*f7c14bbaSAndroid Build Coastguard Worker
2314*f7c14bbaSAndroid Build Coastguard Worker obj->btf_type_map[i] = id;
2315*f7c14bbaSAndroid Build Coastguard Worker
2316*f7c14bbaSAndroid Build Coastguard Worker /* record just appended BTF type for var/func */
2317*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym) {
2318*f7c14bbaSAndroid Build Coastguard Worker glob_sym->btf_id = id;
2319*f7c14bbaSAndroid Build Coastguard Worker glob_sym->underlying_btf_id = -t->type;
2320*f7c14bbaSAndroid Build Coastguard Worker }
2321*f7c14bbaSAndroid Build Coastguard Worker }
2322*f7c14bbaSAndroid Build Coastguard Worker
2323*f7c14bbaSAndroid Build Coastguard Worker /* remap all the types except DATASECs */
2324*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(linker->btf);
2325*f7c14bbaSAndroid Build Coastguard Worker for (i = start_id; i < n; i++) {
2326*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *dst_t = btf_type_by_id(linker->btf, i);
2327*f7c14bbaSAndroid Build Coastguard Worker
2328*f7c14bbaSAndroid Build Coastguard Worker if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map))
2329*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2330*f7c14bbaSAndroid Build Coastguard Worker }
2331*f7c14bbaSAndroid Build Coastguard Worker
2332*f7c14bbaSAndroid Build Coastguard Worker /* Rewrite VAR/FUNC underlying types (i.e., FUNC's FUNC_PROTO and VAR's
2333*f7c14bbaSAndroid Build Coastguard Worker * actual type), if necessary
2334*f7c14bbaSAndroid Build Coastguard Worker */
2335*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < linker->glob_sym_cnt; i++) {
2336*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_sym = &linker->glob_syms[i];
2337*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *glob_t;
2338*f7c14bbaSAndroid Build Coastguard Worker
2339*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym->underlying_btf_id >= 0)
2340*f7c14bbaSAndroid Build Coastguard Worker continue;
2341*f7c14bbaSAndroid Build Coastguard Worker
2342*f7c14bbaSAndroid Build Coastguard Worker glob_sym->underlying_btf_id = obj->btf_type_map[-glob_sym->underlying_btf_id];
2343*f7c14bbaSAndroid Build Coastguard Worker
2344*f7c14bbaSAndroid Build Coastguard Worker glob_t = btf_type_by_id(linker->btf, glob_sym->btf_id);
2345*f7c14bbaSAndroid Build Coastguard Worker glob_t->type = glob_sym->underlying_btf_id;
2346*f7c14bbaSAndroid Build Coastguard Worker }
2347*f7c14bbaSAndroid Build Coastguard Worker
2348*f7c14bbaSAndroid Build Coastguard Worker /* append DATASEC info */
2349*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < obj->sec_cnt; i++) {
2350*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_sec;
2351*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *dst_sec;
2352*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *src_var;
2353*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *dst_var;
2354*f7c14bbaSAndroid Build Coastguard Worker
2355*f7c14bbaSAndroid Build Coastguard Worker src_sec = &obj->secs[i];
2356*f7c14bbaSAndroid Build Coastguard Worker if (!src_sec->sec_type_id || src_sec->skipped)
2357*f7c14bbaSAndroid Build Coastguard Worker continue;
2358*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
2359*f7c14bbaSAndroid Build Coastguard Worker
2360*f7c14bbaSAndroid Build Coastguard Worker /* Mark section as having BTF regardless of the presence of
2361*f7c14bbaSAndroid Build Coastguard Worker * variables. In some cases compiler might generate empty BTF
2362*f7c14bbaSAndroid Build Coastguard Worker * with no variables information. E.g., when promoting local
2363*f7c14bbaSAndroid Build Coastguard Worker * array/structure variable initial values and BPF object
2364*f7c14bbaSAndroid Build Coastguard Worker * file otherwise has no read-only static variables in
2365*f7c14bbaSAndroid Build Coastguard Worker * .rodata. We need to preserve such empty BTF and just set
2366*f7c14bbaSAndroid Build Coastguard Worker * correct section size.
2367*f7c14bbaSAndroid Build Coastguard Worker */
2368*f7c14bbaSAndroid Build Coastguard Worker dst_sec->has_btf = true;
2369*f7c14bbaSAndroid Build Coastguard Worker
2370*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, src_sec->sec_type_id);
2371*f7c14bbaSAndroid Build Coastguard Worker src_var = btf_var_secinfos(t);
2372*f7c14bbaSAndroid Build Coastguard Worker n = btf_vlen(t);
2373*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < n; j++, src_var++) {
2374*f7c14bbaSAndroid Build Coastguard Worker void *sec_vars = dst_sec->sec_vars;
2375*f7c14bbaSAndroid Build Coastguard Worker int new_id = obj->btf_type_map[src_var->type];
2376*f7c14bbaSAndroid Build Coastguard Worker struct glob_sym *glob_sym = NULL;
2377*f7c14bbaSAndroid Build Coastguard Worker
2378*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(linker->btf, new_id);
2379*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_non_static(t)) {
2380*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(linker->btf, t->name_off);
2381*f7c14bbaSAndroid Build Coastguard Worker glob_sym = find_glob_sym(linker, name);
2382*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym->sec_id != dst_sec->id) {
2383*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': section mismatch %d vs %d\n",
2384*f7c14bbaSAndroid Build Coastguard Worker name, glob_sym->sec_id, dst_sec->id);
2385*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2386*f7c14bbaSAndroid Build Coastguard Worker }
2387*f7c14bbaSAndroid Build Coastguard Worker }
2388*f7c14bbaSAndroid Build Coastguard Worker
2389*f7c14bbaSAndroid Build Coastguard Worker /* If there is already a member (VAR or FUNC) mapped
2390*f7c14bbaSAndroid Build Coastguard Worker * to the same type, don't add a duplicate entry.
2391*f7c14bbaSAndroid Build Coastguard Worker * This will happen when multiple object files define
2392*f7c14bbaSAndroid Build Coastguard Worker * the same extern VARs/FUNCs.
2393*f7c14bbaSAndroid Build Coastguard Worker */
2394*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym && glob_sym->var_idx >= 0) {
2395*f7c14bbaSAndroid Build Coastguard Worker __s64 sz;
2396*f7c14bbaSAndroid Build Coastguard Worker
2397*f7c14bbaSAndroid Build Coastguard Worker dst_var = &dst_sec->sec_vars[glob_sym->var_idx];
2398*f7c14bbaSAndroid Build Coastguard Worker /* Because underlying BTF type might have
2399*f7c14bbaSAndroid Build Coastguard Worker * changed, so might its size have changed, so
2400*f7c14bbaSAndroid Build Coastguard Worker * re-calculate and update it in sec_var.
2401*f7c14bbaSAndroid Build Coastguard Worker */
2402*f7c14bbaSAndroid Build Coastguard Worker sz = btf__resolve_size(linker->btf, glob_sym->underlying_btf_id);
2403*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2404*f7c14bbaSAndroid Build Coastguard Worker pr_warn("global '%s': failed to resolve size of underlying type: %d\n",
2405*f7c14bbaSAndroid Build Coastguard Worker name, (int)sz);
2406*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2407*f7c14bbaSAndroid Build Coastguard Worker }
2408*f7c14bbaSAndroid Build Coastguard Worker dst_var->size = sz;
2409*f7c14bbaSAndroid Build Coastguard Worker continue;
2410*f7c14bbaSAndroid Build Coastguard Worker }
2411*f7c14bbaSAndroid Build Coastguard Worker
2412*f7c14bbaSAndroid Build Coastguard Worker sec_vars = libbpf_reallocarray(sec_vars,
2413*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_var_cnt + 1,
2414*f7c14bbaSAndroid Build Coastguard Worker sizeof(*dst_sec->sec_vars));
2415*f7c14bbaSAndroid Build Coastguard Worker if (!sec_vars)
2416*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2417*f7c14bbaSAndroid Build Coastguard Worker
2418*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_vars = sec_vars;
2419*f7c14bbaSAndroid Build Coastguard Worker dst_sec->sec_var_cnt++;
2420*f7c14bbaSAndroid Build Coastguard Worker
2421*f7c14bbaSAndroid Build Coastguard Worker dst_var = &dst_sec->sec_vars[dst_sec->sec_var_cnt - 1];
2422*f7c14bbaSAndroid Build Coastguard Worker dst_var->type = obj->btf_type_map[src_var->type];
2423*f7c14bbaSAndroid Build Coastguard Worker dst_var->size = src_var->size;
2424*f7c14bbaSAndroid Build Coastguard Worker dst_var->offset = src_sec->dst_off + src_var->offset;
2425*f7c14bbaSAndroid Build Coastguard Worker
2426*f7c14bbaSAndroid Build Coastguard Worker if (glob_sym)
2427*f7c14bbaSAndroid Build Coastguard Worker glob_sym->var_idx = dst_sec->sec_var_cnt - 1;
2428*f7c14bbaSAndroid Build Coastguard Worker }
2429*f7c14bbaSAndroid Build Coastguard Worker }
2430*f7c14bbaSAndroid Build Coastguard Worker
2431*f7c14bbaSAndroid Build Coastguard Worker return 0;
2432*f7c14bbaSAndroid Build Coastguard Worker }
2433*f7c14bbaSAndroid Build Coastguard Worker
add_btf_ext_rec(struct btf_ext_sec_data * ext_data,const void * src_rec)2434*f7c14bbaSAndroid Build Coastguard Worker static void *add_btf_ext_rec(struct btf_ext_sec_data *ext_data, const void *src_rec)
2435*f7c14bbaSAndroid Build Coastguard Worker {
2436*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
2437*f7c14bbaSAndroid Build Coastguard Worker
2438*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(ext_data->recs, ext_data->rec_cnt + 1, ext_data->rec_sz);
2439*f7c14bbaSAndroid Build Coastguard Worker if (!tmp)
2440*f7c14bbaSAndroid Build Coastguard Worker return NULL;
2441*f7c14bbaSAndroid Build Coastguard Worker ext_data->recs = tmp;
2442*f7c14bbaSAndroid Build Coastguard Worker
2443*f7c14bbaSAndroid Build Coastguard Worker tmp += ext_data->rec_cnt * ext_data->rec_sz;
2444*f7c14bbaSAndroid Build Coastguard Worker memcpy(tmp, src_rec, ext_data->rec_sz);
2445*f7c14bbaSAndroid Build Coastguard Worker
2446*f7c14bbaSAndroid Build Coastguard Worker ext_data->rec_cnt++;
2447*f7c14bbaSAndroid Build Coastguard Worker
2448*f7c14bbaSAndroid Build Coastguard Worker return tmp;
2449*f7c14bbaSAndroid Build Coastguard Worker }
2450*f7c14bbaSAndroid Build Coastguard Worker
linker_append_btf_ext(struct bpf_linker * linker,struct src_obj * obj)2451*f7c14bbaSAndroid Build Coastguard Worker static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj)
2452*f7c14bbaSAndroid Build Coastguard Worker {
2453*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info_sec *ext_sec;
2454*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name, *s;
2455*f7c14bbaSAndroid Build Coastguard Worker struct src_sec *src_sec;
2456*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *dst_sec;
2457*f7c14bbaSAndroid Build Coastguard Worker int rec_sz, str_off, i;
2458*f7c14bbaSAndroid Build Coastguard Worker
2459*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_ext)
2460*f7c14bbaSAndroid Build Coastguard Worker return 0;
2461*f7c14bbaSAndroid Build Coastguard Worker
2462*f7c14bbaSAndroid Build Coastguard Worker rec_sz = obj->btf_ext->func_info.rec_size;
2463*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) {
2464*f7c14bbaSAndroid Build Coastguard Worker struct bpf_func_info_min *src_rec, *dst_rec;
2465*f7c14bbaSAndroid Build Coastguard Worker
2466*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2467*f7c14bbaSAndroid Build Coastguard Worker src_sec = find_src_sec_by_name(obj, sec_name);
2468*f7c14bbaSAndroid Build Coastguard Worker if (!src_sec) {
2469*f7c14bbaSAndroid Build Coastguard Worker pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
2470*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2471*f7c14bbaSAndroid Build Coastguard Worker }
2472*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
2473*f7c14bbaSAndroid Build Coastguard Worker
2474*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->func_info.rec_sz == 0)
2475*f7c14bbaSAndroid Build Coastguard Worker dst_sec->func_info.rec_sz = rec_sz;
2476*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->func_info.rec_sz != rec_sz) {
2477*f7c14bbaSAndroid Build Coastguard Worker pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
2478*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2479*f7c14bbaSAndroid Build Coastguard Worker }
2480*f7c14bbaSAndroid Build Coastguard Worker
2481*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) {
2482*f7c14bbaSAndroid Build Coastguard Worker dst_rec = add_btf_ext_rec(&dst_sec->func_info, src_rec);
2483*f7c14bbaSAndroid Build Coastguard Worker if (!dst_rec)
2484*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2485*f7c14bbaSAndroid Build Coastguard Worker
2486*f7c14bbaSAndroid Build Coastguard Worker dst_rec->insn_off += src_sec->dst_off;
2487*f7c14bbaSAndroid Build Coastguard Worker dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
2488*f7c14bbaSAndroid Build Coastguard Worker }
2489*f7c14bbaSAndroid Build Coastguard Worker }
2490*f7c14bbaSAndroid Build Coastguard Worker
2491*f7c14bbaSAndroid Build Coastguard Worker rec_sz = obj->btf_ext->line_info.rec_size;
2492*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) {
2493*f7c14bbaSAndroid Build Coastguard Worker struct bpf_line_info_min *src_rec, *dst_rec;
2494*f7c14bbaSAndroid Build Coastguard Worker
2495*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2496*f7c14bbaSAndroid Build Coastguard Worker src_sec = find_src_sec_by_name(obj, sec_name);
2497*f7c14bbaSAndroid Build Coastguard Worker if (!src_sec) {
2498*f7c14bbaSAndroid Build Coastguard Worker pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
2499*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2500*f7c14bbaSAndroid Build Coastguard Worker }
2501*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
2502*f7c14bbaSAndroid Build Coastguard Worker
2503*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->line_info.rec_sz == 0)
2504*f7c14bbaSAndroid Build Coastguard Worker dst_sec->line_info.rec_sz = rec_sz;
2505*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->line_info.rec_sz != rec_sz) {
2506*f7c14bbaSAndroid Build Coastguard Worker pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
2507*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2508*f7c14bbaSAndroid Build Coastguard Worker }
2509*f7c14bbaSAndroid Build Coastguard Worker
2510*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) {
2511*f7c14bbaSAndroid Build Coastguard Worker dst_rec = add_btf_ext_rec(&dst_sec->line_info, src_rec);
2512*f7c14bbaSAndroid Build Coastguard Worker if (!dst_rec)
2513*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2514*f7c14bbaSAndroid Build Coastguard Worker
2515*f7c14bbaSAndroid Build Coastguard Worker dst_rec->insn_off += src_sec->dst_off;
2516*f7c14bbaSAndroid Build Coastguard Worker
2517*f7c14bbaSAndroid Build Coastguard Worker s = btf__str_by_offset(obj->btf, src_rec->file_name_off);
2518*f7c14bbaSAndroid Build Coastguard Worker str_off = btf__add_str(linker->btf, s);
2519*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
2520*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2521*f7c14bbaSAndroid Build Coastguard Worker dst_rec->file_name_off = str_off;
2522*f7c14bbaSAndroid Build Coastguard Worker
2523*f7c14bbaSAndroid Build Coastguard Worker s = btf__str_by_offset(obj->btf, src_rec->line_off);
2524*f7c14bbaSAndroid Build Coastguard Worker str_off = btf__add_str(linker->btf, s);
2525*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
2526*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2527*f7c14bbaSAndroid Build Coastguard Worker dst_rec->line_off = str_off;
2528*f7c14bbaSAndroid Build Coastguard Worker
2529*f7c14bbaSAndroid Build Coastguard Worker /* dst_rec->line_col is fine */
2530*f7c14bbaSAndroid Build Coastguard Worker }
2531*f7c14bbaSAndroid Build Coastguard Worker }
2532*f7c14bbaSAndroid Build Coastguard Worker
2533*f7c14bbaSAndroid Build Coastguard Worker rec_sz = obj->btf_ext->core_relo_info.rec_size;
2534*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) {
2535*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_relo *src_rec, *dst_rec;
2536*f7c14bbaSAndroid Build Coastguard Worker
2537*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2538*f7c14bbaSAndroid Build Coastguard Worker src_sec = find_src_sec_by_name(obj, sec_name);
2539*f7c14bbaSAndroid Build Coastguard Worker if (!src_sec) {
2540*f7c14bbaSAndroid Build Coastguard Worker pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
2541*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2542*f7c14bbaSAndroid Build Coastguard Worker }
2543*f7c14bbaSAndroid Build Coastguard Worker dst_sec = &linker->secs[src_sec->dst_id];
2544*f7c14bbaSAndroid Build Coastguard Worker
2545*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->core_relo_info.rec_sz == 0)
2546*f7c14bbaSAndroid Build Coastguard Worker dst_sec->core_relo_info.rec_sz = rec_sz;
2547*f7c14bbaSAndroid Build Coastguard Worker if (dst_sec->core_relo_info.rec_sz != rec_sz) {
2548*f7c14bbaSAndroid Build Coastguard Worker pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
2549*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2550*f7c14bbaSAndroid Build Coastguard Worker }
2551*f7c14bbaSAndroid Build Coastguard Worker
2552*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) {
2553*f7c14bbaSAndroid Build Coastguard Worker dst_rec = add_btf_ext_rec(&dst_sec->core_relo_info, src_rec);
2554*f7c14bbaSAndroid Build Coastguard Worker if (!dst_rec)
2555*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2556*f7c14bbaSAndroid Build Coastguard Worker
2557*f7c14bbaSAndroid Build Coastguard Worker dst_rec->insn_off += src_sec->dst_off;
2558*f7c14bbaSAndroid Build Coastguard Worker dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
2559*f7c14bbaSAndroid Build Coastguard Worker
2560*f7c14bbaSAndroid Build Coastguard Worker s = btf__str_by_offset(obj->btf, src_rec->access_str_off);
2561*f7c14bbaSAndroid Build Coastguard Worker str_off = btf__add_str(linker->btf, s);
2562*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
2563*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2564*f7c14bbaSAndroid Build Coastguard Worker dst_rec->access_str_off = str_off;
2565*f7c14bbaSAndroid Build Coastguard Worker
2566*f7c14bbaSAndroid Build Coastguard Worker /* dst_rec->kind is fine */
2567*f7c14bbaSAndroid Build Coastguard Worker }
2568*f7c14bbaSAndroid Build Coastguard Worker }
2569*f7c14bbaSAndroid Build Coastguard Worker
2570*f7c14bbaSAndroid Build Coastguard Worker return 0;
2571*f7c14bbaSAndroid Build Coastguard Worker }
2572*f7c14bbaSAndroid Build Coastguard Worker
bpf_linker__finalize(struct bpf_linker * linker)2573*f7c14bbaSAndroid Build Coastguard Worker int bpf_linker__finalize(struct bpf_linker *linker)
2574*f7c14bbaSAndroid Build Coastguard Worker {
2575*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec;
2576*f7c14bbaSAndroid Build Coastguard Worker size_t strs_sz;
2577*f7c14bbaSAndroid Build Coastguard Worker const void *strs;
2578*f7c14bbaSAndroid Build Coastguard Worker int err, i;
2579*f7c14bbaSAndroid Build Coastguard Worker
2580*f7c14bbaSAndroid Build Coastguard Worker if (!linker->elf)
2581*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
2582*f7c14bbaSAndroid Build Coastguard Worker
2583*f7c14bbaSAndroid Build Coastguard Worker err = finalize_btf(linker);
2584*f7c14bbaSAndroid Build Coastguard Worker if (err)
2585*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
2586*f7c14bbaSAndroid Build Coastguard Worker
2587*f7c14bbaSAndroid Build Coastguard Worker /* Finalize strings */
2588*f7c14bbaSAndroid Build Coastguard Worker strs_sz = strset__data_size(linker->strtab_strs);
2589*f7c14bbaSAndroid Build Coastguard Worker strs = strset__data(linker->strtab_strs);
2590*f7c14bbaSAndroid Build Coastguard Worker
2591*f7c14bbaSAndroid Build Coastguard Worker sec = &linker->secs[linker->strtab_sec_idx];
2592*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_align = 1;
2593*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_off = 0LL;
2594*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_buf = (void *)strs;
2595*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_type = ELF_T_BYTE;
2596*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_size = strs_sz;
2597*f7c14bbaSAndroid Build Coastguard Worker sec->shdr->sh_size = strs_sz;
2598*f7c14bbaSAndroid Build Coastguard Worker
2599*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2600*f7c14bbaSAndroid Build Coastguard Worker sec = &linker->secs[i];
2601*f7c14bbaSAndroid Build Coastguard Worker
2602*f7c14bbaSAndroid Build Coastguard Worker /* STRTAB is handled specially above */
2603*f7c14bbaSAndroid Build Coastguard Worker if (sec->sec_idx == linker->strtab_sec_idx)
2604*f7c14bbaSAndroid Build Coastguard Worker continue;
2605*f7c14bbaSAndroid Build Coastguard Worker
2606*f7c14bbaSAndroid Build Coastguard Worker /* special ephemeral sections (.ksyms, .kconfig, etc) */
2607*f7c14bbaSAndroid Build Coastguard Worker if (!sec->scn)
2608*f7c14bbaSAndroid Build Coastguard Worker continue;
2609*f7c14bbaSAndroid Build Coastguard Worker
2610*f7c14bbaSAndroid Build Coastguard Worker sec->data->d_buf = sec->raw_data;
2611*f7c14bbaSAndroid Build Coastguard Worker }
2612*f7c14bbaSAndroid Build Coastguard Worker
2613*f7c14bbaSAndroid Build Coastguard Worker /* Finalize ELF layout */
2614*f7c14bbaSAndroid Build Coastguard Worker if (elf_update(linker->elf, ELF_C_NULL) < 0) {
2615*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
2616*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to finalize ELF layout");
2617*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
2618*f7c14bbaSAndroid Build Coastguard Worker }
2619*f7c14bbaSAndroid Build Coastguard Worker
2620*f7c14bbaSAndroid Build Coastguard Worker /* Write out final ELF contents */
2621*f7c14bbaSAndroid Build Coastguard Worker if (elf_update(linker->elf, ELF_C_WRITE) < 0) {
2622*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
2623*f7c14bbaSAndroid Build Coastguard Worker pr_warn_elf("failed to write ELF contents");
2624*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
2625*f7c14bbaSAndroid Build Coastguard Worker }
2626*f7c14bbaSAndroid Build Coastguard Worker
2627*f7c14bbaSAndroid Build Coastguard Worker elf_end(linker->elf);
2628*f7c14bbaSAndroid Build Coastguard Worker close(linker->fd);
2629*f7c14bbaSAndroid Build Coastguard Worker
2630*f7c14bbaSAndroid Build Coastguard Worker linker->elf = NULL;
2631*f7c14bbaSAndroid Build Coastguard Worker linker->fd = -1;
2632*f7c14bbaSAndroid Build Coastguard Worker
2633*f7c14bbaSAndroid Build Coastguard Worker return 0;
2634*f7c14bbaSAndroid Build Coastguard Worker }
2635*f7c14bbaSAndroid Build Coastguard Worker
emit_elf_data_sec(struct bpf_linker * linker,const char * sec_name,size_t align,const void * raw_data,size_t raw_sz)2636*f7c14bbaSAndroid Build Coastguard Worker static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name,
2637*f7c14bbaSAndroid Build Coastguard Worker size_t align, const void *raw_data, size_t raw_sz)
2638*f7c14bbaSAndroid Build Coastguard Worker {
2639*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
2640*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
2641*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
2642*f7c14bbaSAndroid Build Coastguard Worker int name_off;
2643*f7c14bbaSAndroid Build Coastguard Worker
2644*f7c14bbaSAndroid Build Coastguard Worker name_off = strset__add_str(linker->strtab_strs, sec_name);
2645*f7c14bbaSAndroid Build Coastguard Worker if (name_off < 0)
2646*f7c14bbaSAndroid Build Coastguard Worker return name_off;
2647*f7c14bbaSAndroid Build Coastguard Worker
2648*f7c14bbaSAndroid Build Coastguard Worker scn = elf_newscn(linker->elf);
2649*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
2650*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2651*f7c14bbaSAndroid Build Coastguard Worker data = elf_newdata(scn);
2652*f7c14bbaSAndroid Build Coastguard Worker if (!data)
2653*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2654*f7c14bbaSAndroid Build Coastguard Worker shdr = elf64_getshdr(scn);
2655*f7c14bbaSAndroid Build Coastguard Worker if (!shdr)
2656*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2657*f7c14bbaSAndroid Build Coastguard Worker
2658*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_name = name_off;
2659*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_type = SHT_PROGBITS;
2660*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_flags = 0;
2661*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_size = raw_sz;
2662*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_link = 0;
2663*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_info = 0;
2664*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_addralign = align;
2665*f7c14bbaSAndroid Build Coastguard Worker shdr->sh_entsize = 0;
2666*f7c14bbaSAndroid Build Coastguard Worker
2667*f7c14bbaSAndroid Build Coastguard Worker data->d_type = ELF_T_BYTE;
2668*f7c14bbaSAndroid Build Coastguard Worker data->d_size = raw_sz;
2669*f7c14bbaSAndroid Build Coastguard Worker data->d_buf = (void *)raw_data;
2670*f7c14bbaSAndroid Build Coastguard Worker data->d_align = align;
2671*f7c14bbaSAndroid Build Coastguard Worker data->d_off = 0;
2672*f7c14bbaSAndroid Build Coastguard Worker
2673*f7c14bbaSAndroid Build Coastguard Worker return 0;
2674*f7c14bbaSAndroid Build Coastguard Worker }
2675*f7c14bbaSAndroid Build Coastguard Worker
finalize_btf(struct bpf_linker * linker)2676*f7c14bbaSAndroid Build Coastguard Worker static int finalize_btf(struct bpf_linker *linker)
2677*f7c14bbaSAndroid Build Coastguard Worker {
2678*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(btf_dedup_opts, opts);
2679*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf = linker->btf;
2680*f7c14bbaSAndroid Build Coastguard Worker const void *raw_data;
2681*f7c14bbaSAndroid Build Coastguard Worker int i, j, id, err;
2682*f7c14bbaSAndroid Build Coastguard Worker __u32 raw_sz;
2683*f7c14bbaSAndroid Build Coastguard Worker
2684*f7c14bbaSAndroid Build Coastguard Worker /* bail out if no BTF data was produced */
2685*f7c14bbaSAndroid Build Coastguard Worker if (btf__type_cnt(linker->btf) == 1)
2686*f7c14bbaSAndroid Build Coastguard Worker return 0;
2687*f7c14bbaSAndroid Build Coastguard Worker
2688*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2689*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
2690*f7c14bbaSAndroid Build Coastguard Worker
2691*f7c14bbaSAndroid Build Coastguard Worker if (!sec->has_btf)
2692*f7c14bbaSAndroid Build Coastguard Worker continue;
2693*f7c14bbaSAndroid Build Coastguard Worker
2694*f7c14bbaSAndroid Build Coastguard Worker id = btf__add_datasec(btf, sec->sec_name, sec->sec_sz);
2695*f7c14bbaSAndroid Build Coastguard Worker if (id < 0) {
2696*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to add consolidated BTF type for datasec '%s': %d\n",
2697*f7c14bbaSAndroid Build Coastguard Worker sec->sec_name, id);
2698*f7c14bbaSAndroid Build Coastguard Worker return id;
2699*f7c14bbaSAndroid Build Coastguard Worker }
2700*f7c14bbaSAndroid Build Coastguard Worker
2701*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < sec->sec_var_cnt; j++) {
2702*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vi = &sec->sec_vars[j];
2703*f7c14bbaSAndroid Build Coastguard Worker
2704*f7c14bbaSAndroid Build Coastguard Worker if (btf__add_datasec_var_info(btf, vi->type, vi->offset, vi->size))
2705*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2706*f7c14bbaSAndroid Build Coastguard Worker }
2707*f7c14bbaSAndroid Build Coastguard Worker }
2708*f7c14bbaSAndroid Build Coastguard Worker
2709*f7c14bbaSAndroid Build Coastguard Worker err = finalize_btf_ext(linker);
2710*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2711*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".BTF.ext generation failed: %d\n", err);
2712*f7c14bbaSAndroid Build Coastguard Worker return err;
2713*f7c14bbaSAndroid Build Coastguard Worker }
2714*f7c14bbaSAndroid Build Coastguard Worker
2715*f7c14bbaSAndroid Build Coastguard Worker opts.btf_ext = linker->btf_ext;
2716*f7c14bbaSAndroid Build Coastguard Worker err = btf__dedup(linker->btf, &opts);
2717*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2718*f7c14bbaSAndroid Build Coastguard Worker pr_warn("BTF dedup failed: %d\n", err);
2719*f7c14bbaSAndroid Build Coastguard Worker return err;
2720*f7c14bbaSAndroid Build Coastguard Worker }
2721*f7c14bbaSAndroid Build Coastguard Worker
2722*f7c14bbaSAndroid Build Coastguard Worker /* Emit .BTF section */
2723*f7c14bbaSAndroid Build Coastguard Worker raw_data = btf__raw_data(linker->btf, &raw_sz);
2724*f7c14bbaSAndroid Build Coastguard Worker if (!raw_data)
2725*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2726*f7c14bbaSAndroid Build Coastguard Worker
2727*f7c14bbaSAndroid Build Coastguard Worker err = emit_elf_data_sec(linker, BTF_ELF_SEC, 8, raw_data, raw_sz);
2728*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2729*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to write out .BTF ELF section: %d\n", err);
2730*f7c14bbaSAndroid Build Coastguard Worker return err;
2731*f7c14bbaSAndroid Build Coastguard Worker }
2732*f7c14bbaSAndroid Build Coastguard Worker
2733*f7c14bbaSAndroid Build Coastguard Worker /* Emit .BTF.ext section */
2734*f7c14bbaSAndroid Build Coastguard Worker if (linker->btf_ext) {
2735*f7c14bbaSAndroid Build Coastguard Worker raw_data = btf_ext__raw_data(linker->btf_ext, &raw_sz);
2736*f7c14bbaSAndroid Build Coastguard Worker if (!raw_data)
2737*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2738*f7c14bbaSAndroid Build Coastguard Worker
2739*f7c14bbaSAndroid Build Coastguard Worker err = emit_elf_data_sec(linker, BTF_EXT_ELF_SEC, 8, raw_data, raw_sz);
2740*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2741*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to write out .BTF.ext ELF section: %d\n", err);
2742*f7c14bbaSAndroid Build Coastguard Worker return err;
2743*f7c14bbaSAndroid Build Coastguard Worker }
2744*f7c14bbaSAndroid Build Coastguard Worker }
2745*f7c14bbaSAndroid Build Coastguard Worker
2746*f7c14bbaSAndroid Build Coastguard Worker return 0;
2747*f7c14bbaSAndroid Build Coastguard Worker }
2748*f7c14bbaSAndroid Build Coastguard Worker
emit_btf_ext_data(struct bpf_linker * linker,void * output,const char * sec_name,struct btf_ext_sec_data * sec_data)2749*f7c14bbaSAndroid Build Coastguard Worker static int emit_btf_ext_data(struct bpf_linker *linker, void *output,
2750*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name, struct btf_ext_sec_data *sec_data)
2751*f7c14bbaSAndroid Build Coastguard Worker {
2752*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info_sec *sec_info;
2753*f7c14bbaSAndroid Build Coastguard Worker void *cur = output;
2754*f7c14bbaSAndroid Build Coastguard Worker int str_off;
2755*f7c14bbaSAndroid Build Coastguard Worker size_t sz;
2756*f7c14bbaSAndroid Build Coastguard Worker
2757*f7c14bbaSAndroid Build Coastguard Worker if (!sec_data->rec_cnt)
2758*f7c14bbaSAndroid Build Coastguard Worker return 0;
2759*f7c14bbaSAndroid Build Coastguard Worker
2760*f7c14bbaSAndroid Build Coastguard Worker str_off = btf__add_str(linker->btf, sec_name);
2761*f7c14bbaSAndroid Build Coastguard Worker if (str_off < 0)
2762*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2763*f7c14bbaSAndroid Build Coastguard Worker
2764*f7c14bbaSAndroid Build Coastguard Worker sec_info = cur;
2765*f7c14bbaSAndroid Build Coastguard Worker sec_info->sec_name_off = str_off;
2766*f7c14bbaSAndroid Build Coastguard Worker sec_info->num_info = sec_data->rec_cnt;
2767*f7c14bbaSAndroid Build Coastguard Worker cur += sizeof(struct btf_ext_info_sec);
2768*f7c14bbaSAndroid Build Coastguard Worker
2769*f7c14bbaSAndroid Build Coastguard Worker sz = sec_data->rec_cnt * sec_data->rec_sz;
2770*f7c14bbaSAndroid Build Coastguard Worker memcpy(cur, sec_data->recs, sz);
2771*f7c14bbaSAndroid Build Coastguard Worker cur += sz;
2772*f7c14bbaSAndroid Build Coastguard Worker
2773*f7c14bbaSAndroid Build Coastguard Worker return cur - output;
2774*f7c14bbaSAndroid Build Coastguard Worker }
2775*f7c14bbaSAndroid Build Coastguard Worker
finalize_btf_ext(struct bpf_linker * linker)2776*f7c14bbaSAndroid Build Coastguard Worker static int finalize_btf_ext(struct bpf_linker *linker)
2777*f7c14bbaSAndroid Build Coastguard Worker {
2778*f7c14bbaSAndroid Build Coastguard Worker size_t funcs_sz = 0, lines_sz = 0, core_relos_sz = 0, total_sz = 0;
2779*f7c14bbaSAndroid Build Coastguard Worker size_t func_rec_sz = 0, line_rec_sz = 0, core_relo_rec_sz = 0;
2780*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_header *hdr;
2781*f7c14bbaSAndroid Build Coastguard Worker void *data, *cur;
2782*f7c14bbaSAndroid Build Coastguard Worker int i, err, sz;
2783*f7c14bbaSAndroid Build Coastguard Worker
2784*f7c14bbaSAndroid Build Coastguard Worker /* validate that all sections have the same .BTF.ext record sizes
2785*f7c14bbaSAndroid Build Coastguard Worker * and calculate total data size for each type of data (func info,
2786*f7c14bbaSAndroid Build Coastguard Worker * line info, core relos)
2787*f7c14bbaSAndroid Build Coastguard Worker */
2788*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2789*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
2790*f7c14bbaSAndroid Build Coastguard Worker
2791*f7c14bbaSAndroid Build Coastguard Worker if (sec->func_info.rec_cnt) {
2792*f7c14bbaSAndroid Build Coastguard Worker if (func_rec_sz == 0)
2793*f7c14bbaSAndroid Build Coastguard Worker func_rec_sz = sec->func_info.rec_sz;
2794*f7c14bbaSAndroid Build Coastguard Worker if (func_rec_sz != sec->func_info.rec_sz) {
2795*f7c14bbaSAndroid Build Coastguard Worker pr_warn("mismatch in func_info record size %zu != %u\n",
2796*f7c14bbaSAndroid Build Coastguard Worker func_rec_sz, sec->func_info.rec_sz);
2797*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2798*f7c14bbaSAndroid Build Coastguard Worker }
2799*f7c14bbaSAndroid Build Coastguard Worker
2800*f7c14bbaSAndroid Build Coastguard Worker funcs_sz += sizeof(struct btf_ext_info_sec) + func_rec_sz * sec->func_info.rec_cnt;
2801*f7c14bbaSAndroid Build Coastguard Worker }
2802*f7c14bbaSAndroid Build Coastguard Worker if (sec->line_info.rec_cnt) {
2803*f7c14bbaSAndroid Build Coastguard Worker if (line_rec_sz == 0)
2804*f7c14bbaSAndroid Build Coastguard Worker line_rec_sz = sec->line_info.rec_sz;
2805*f7c14bbaSAndroid Build Coastguard Worker if (line_rec_sz != sec->line_info.rec_sz) {
2806*f7c14bbaSAndroid Build Coastguard Worker pr_warn("mismatch in line_info record size %zu != %u\n",
2807*f7c14bbaSAndroid Build Coastguard Worker line_rec_sz, sec->line_info.rec_sz);
2808*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2809*f7c14bbaSAndroid Build Coastguard Worker }
2810*f7c14bbaSAndroid Build Coastguard Worker
2811*f7c14bbaSAndroid Build Coastguard Worker lines_sz += sizeof(struct btf_ext_info_sec) + line_rec_sz * sec->line_info.rec_cnt;
2812*f7c14bbaSAndroid Build Coastguard Worker }
2813*f7c14bbaSAndroid Build Coastguard Worker if (sec->core_relo_info.rec_cnt) {
2814*f7c14bbaSAndroid Build Coastguard Worker if (core_relo_rec_sz == 0)
2815*f7c14bbaSAndroid Build Coastguard Worker core_relo_rec_sz = sec->core_relo_info.rec_sz;
2816*f7c14bbaSAndroid Build Coastguard Worker if (core_relo_rec_sz != sec->core_relo_info.rec_sz) {
2817*f7c14bbaSAndroid Build Coastguard Worker pr_warn("mismatch in core_relo_info record size %zu != %u\n",
2818*f7c14bbaSAndroid Build Coastguard Worker core_relo_rec_sz, sec->core_relo_info.rec_sz);
2819*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2820*f7c14bbaSAndroid Build Coastguard Worker }
2821*f7c14bbaSAndroid Build Coastguard Worker
2822*f7c14bbaSAndroid Build Coastguard Worker core_relos_sz += sizeof(struct btf_ext_info_sec) + core_relo_rec_sz * sec->core_relo_info.rec_cnt;
2823*f7c14bbaSAndroid Build Coastguard Worker }
2824*f7c14bbaSAndroid Build Coastguard Worker }
2825*f7c14bbaSAndroid Build Coastguard Worker
2826*f7c14bbaSAndroid Build Coastguard Worker if (!funcs_sz && !lines_sz && !core_relos_sz)
2827*f7c14bbaSAndroid Build Coastguard Worker return 0;
2828*f7c14bbaSAndroid Build Coastguard Worker
2829*f7c14bbaSAndroid Build Coastguard Worker total_sz += sizeof(struct btf_ext_header);
2830*f7c14bbaSAndroid Build Coastguard Worker if (funcs_sz) {
2831*f7c14bbaSAndroid Build Coastguard Worker funcs_sz += sizeof(__u32); /* record size prefix */
2832*f7c14bbaSAndroid Build Coastguard Worker total_sz += funcs_sz;
2833*f7c14bbaSAndroid Build Coastguard Worker }
2834*f7c14bbaSAndroid Build Coastguard Worker if (lines_sz) {
2835*f7c14bbaSAndroid Build Coastguard Worker lines_sz += sizeof(__u32); /* record size prefix */
2836*f7c14bbaSAndroid Build Coastguard Worker total_sz += lines_sz;
2837*f7c14bbaSAndroid Build Coastguard Worker }
2838*f7c14bbaSAndroid Build Coastguard Worker if (core_relos_sz) {
2839*f7c14bbaSAndroid Build Coastguard Worker core_relos_sz += sizeof(__u32); /* record size prefix */
2840*f7c14bbaSAndroid Build Coastguard Worker total_sz += core_relos_sz;
2841*f7c14bbaSAndroid Build Coastguard Worker }
2842*f7c14bbaSAndroid Build Coastguard Worker
2843*f7c14bbaSAndroid Build Coastguard Worker cur = data = calloc(1, total_sz);
2844*f7c14bbaSAndroid Build Coastguard Worker if (!data)
2845*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2846*f7c14bbaSAndroid Build Coastguard Worker
2847*f7c14bbaSAndroid Build Coastguard Worker hdr = cur;
2848*f7c14bbaSAndroid Build Coastguard Worker hdr->magic = BTF_MAGIC;
2849*f7c14bbaSAndroid Build Coastguard Worker hdr->version = BTF_VERSION;
2850*f7c14bbaSAndroid Build Coastguard Worker hdr->flags = 0;
2851*f7c14bbaSAndroid Build Coastguard Worker hdr->hdr_len = sizeof(struct btf_ext_header);
2852*f7c14bbaSAndroid Build Coastguard Worker cur += sizeof(struct btf_ext_header);
2853*f7c14bbaSAndroid Build Coastguard Worker
2854*f7c14bbaSAndroid Build Coastguard Worker /* All offsets are in bytes relative to the end of this header */
2855*f7c14bbaSAndroid Build Coastguard Worker hdr->func_info_off = 0;
2856*f7c14bbaSAndroid Build Coastguard Worker hdr->func_info_len = funcs_sz;
2857*f7c14bbaSAndroid Build Coastguard Worker hdr->line_info_off = funcs_sz;
2858*f7c14bbaSAndroid Build Coastguard Worker hdr->line_info_len = lines_sz;
2859*f7c14bbaSAndroid Build Coastguard Worker hdr->core_relo_off = funcs_sz + lines_sz;
2860*f7c14bbaSAndroid Build Coastguard Worker hdr->core_relo_len = core_relos_sz;
2861*f7c14bbaSAndroid Build Coastguard Worker
2862*f7c14bbaSAndroid Build Coastguard Worker if (funcs_sz) {
2863*f7c14bbaSAndroid Build Coastguard Worker *(__u32 *)cur = func_rec_sz;
2864*f7c14bbaSAndroid Build Coastguard Worker cur += sizeof(__u32);
2865*f7c14bbaSAndroid Build Coastguard Worker
2866*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2867*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
2868*f7c14bbaSAndroid Build Coastguard Worker
2869*f7c14bbaSAndroid Build Coastguard Worker sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
2870*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2871*f7c14bbaSAndroid Build Coastguard Worker err = sz;
2872*f7c14bbaSAndroid Build Coastguard Worker goto out;
2873*f7c14bbaSAndroid Build Coastguard Worker }
2874*f7c14bbaSAndroid Build Coastguard Worker
2875*f7c14bbaSAndroid Build Coastguard Worker cur += sz;
2876*f7c14bbaSAndroid Build Coastguard Worker }
2877*f7c14bbaSAndroid Build Coastguard Worker }
2878*f7c14bbaSAndroid Build Coastguard Worker
2879*f7c14bbaSAndroid Build Coastguard Worker if (lines_sz) {
2880*f7c14bbaSAndroid Build Coastguard Worker *(__u32 *)cur = line_rec_sz;
2881*f7c14bbaSAndroid Build Coastguard Worker cur += sizeof(__u32);
2882*f7c14bbaSAndroid Build Coastguard Worker
2883*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2884*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
2885*f7c14bbaSAndroid Build Coastguard Worker
2886*f7c14bbaSAndroid Build Coastguard Worker sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
2887*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2888*f7c14bbaSAndroid Build Coastguard Worker err = sz;
2889*f7c14bbaSAndroid Build Coastguard Worker goto out;
2890*f7c14bbaSAndroid Build Coastguard Worker }
2891*f7c14bbaSAndroid Build Coastguard Worker
2892*f7c14bbaSAndroid Build Coastguard Worker cur += sz;
2893*f7c14bbaSAndroid Build Coastguard Worker }
2894*f7c14bbaSAndroid Build Coastguard Worker }
2895*f7c14bbaSAndroid Build Coastguard Worker
2896*f7c14bbaSAndroid Build Coastguard Worker if (core_relos_sz) {
2897*f7c14bbaSAndroid Build Coastguard Worker *(__u32 *)cur = core_relo_rec_sz;
2898*f7c14bbaSAndroid Build Coastguard Worker cur += sizeof(__u32);
2899*f7c14bbaSAndroid Build Coastguard Worker
2900*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < linker->sec_cnt; i++) {
2901*f7c14bbaSAndroid Build Coastguard Worker struct dst_sec *sec = &linker->secs[i];
2902*f7c14bbaSAndroid Build Coastguard Worker
2903*f7c14bbaSAndroid Build Coastguard Worker sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
2904*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2905*f7c14bbaSAndroid Build Coastguard Worker err = sz;
2906*f7c14bbaSAndroid Build Coastguard Worker goto out;
2907*f7c14bbaSAndroid Build Coastguard Worker }
2908*f7c14bbaSAndroid Build Coastguard Worker
2909*f7c14bbaSAndroid Build Coastguard Worker cur += sz;
2910*f7c14bbaSAndroid Build Coastguard Worker }
2911*f7c14bbaSAndroid Build Coastguard Worker }
2912*f7c14bbaSAndroid Build Coastguard Worker
2913*f7c14bbaSAndroid Build Coastguard Worker linker->btf_ext = btf_ext__new(data, total_sz);
2914*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(linker->btf_ext);
2915*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2916*f7c14bbaSAndroid Build Coastguard Worker linker->btf_ext = NULL;
2917*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse final .BTF.ext data: %d\n", err);
2918*f7c14bbaSAndroid Build Coastguard Worker goto out;
2919*f7c14bbaSAndroid Build Coastguard Worker }
2920*f7c14bbaSAndroid Build Coastguard Worker
2921*f7c14bbaSAndroid Build Coastguard Worker out:
2922*f7c14bbaSAndroid Build Coastguard Worker free(data);
2923*f7c14bbaSAndroid Build Coastguard Worker return err;
2924*f7c14bbaSAndroid Build Coastguard Worker }
2925