1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #include <sys/types.h>
15 #include <sys/queue.h>
20 #include <rte_common.h>
22 #include <rte_debug.h>
23 #include <rte_memory.h>
25 #include <rte_byteorder.h>
26 #include <rte_errno.h>
30 /* To overcome compatibility issue */
36 bpf_find_xsym(const char *sn, enum rte_bpf_xtype type,
37 const struct rte_bpf_xsym fp[], uint32_t fn)
41 if (sn == NULL || fp == NULL)
44 for (i = 0; i != fn; i++) {
45 if (fp[i].type == type && strcmp(sn, fp[i].name) == 0)
49 return (i != fn) ? i : UINT32_MAX;
53 * update BPF code at offset *ofs* with a proper address(index) for external
57 resolve_xsym(const char *sn, size_t ofs, struct ebpf_insn *ins, size_t ins_sz,
58 const struct rte_bpf_prm *prm)
61 enum rte_bpf_xtype type;
63 if (ofs % sizeof(ins[0]) != 0 || ofs >= ins_sz)
66 idx = ofs / sizeof(ins[0]);
67 if (ins[idx].code == (BPF_JMP | EBPF_CALL))
68 type = RTE_BPF_XTYPE_FUNC;
69 else if (ins[idx].code == (BPF_LD | BPF_IMM | EBPF_DW) &&
70 ofs < ins_sz - sizeof(ins[idx]))
71 type = RTE_BPF_XTYPE_VAR;
75 fidx = bpf_find_xsym(sn, type, prm->xsym, prm->nb_xsym);
76 if (fidx == UINT32_MAX)
79 /* for function we just need an index in our xsym table */
80 if (type == RTE_BPF_XTYPE_FUNC) {
82 /* we don't support multiple functions per BPF module,
83 * so treat EBPF_PSEUDO_CALL to extrernal function
84 * as an ordinary EBPF_CALL.
86 if (ins[idx].src_reg == EBPF_PSEUDO_CALL) {
87 RTE_BPF_LOG(INFO, "%s(%u): "
88 "EBPF_PSEUDO_CALL to external function: %s\n",
90 ins[idx].src_reg = EBPF_REG_0;
93 /* for variable we need to store its absolute address */
95 ins[idx].imm = (uintptr_t)prm->xsym[fidx].var.val;
97 (uint64_t)(uintptr_t)prm->xsym[fidx].var.val >> 32;
104 check_elf_header(const Elf64_Ehdr *eh)
110 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
111 if (eh->e_ident[EI_DATA] != ELFDATA2LSB)
113 if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
115 err = "not native byte order";
116 else if (eh->e_ident[EI_OSABI] != ELFOSABI_NONE)
117 err = "unexpected OS ABI";
118 else if (eh->e_type != ET_REL)
119 err = "unexpected ELF type";
120 else if (eh->e_machine != EM_NONE && eh->e_machine != EM_BPF)
121 err = "unexpected machine type";
124 RTE_BPF_LOG(ERR, "%s(): %s\n", __func__, err);
132 * helper function, find executable section by name.
135 find_elf_code(Elf *elf, const char *section, Elf_Data **psd, size_t *pidx)
138 const Elf64_Ehdr *eh;
139 const Elf64_Shdr *sh;
144 eh = elf64_getehdr(elf);
147 RTE_BPF_LOG(ERR, "%s(%p, %s) error code: %d(%s)\n",
148 __func__, elf, section, rc, elf_errmsg(rc));
152 if (check_elf_header(eh) != 0)
155 /* find given section by name */
156 for (sc = elf_nextscn(elf, NULL); sc != NULL;
157 sc = elf_nextscn(elf, sc)) {
158 sh = elf64_getshdr(sc);
159 sn = elf_strptr(elf, eh->e_shstrndx, sh->sh_name);
160 if (sn != NULL && strcmp(section, sn) == 0 &&
161 sh->sh_type == SHT_PROGBITS &&
162 sh->sh_flags == (SHF_ALLOC | SHF_EXECINSTR))
166 sd = elf_getdata(sc, NULL);
167 if (sd == NULL || sd->d_size == 0 ||
168 sd->d_size % sizeof(struct ebpf_insn) != 0) {
170 RTE_BPF_LOG(ERR, "%s(%p, %s) error code: %d(%s)\n",
171 __func__, elf, section, rc, elf_errmsg(rc));
176 *pidx = elf_ndxscn(sc);
181 * helper function to process data from relocation table.
184 process_reloc(Elf *elf, size_t sym_idx, Elf64_Rel *re, size_t re_sz,
185 struct ebpf_insn *ins, size_t ins_sz, const struct rte_bpf_prm *prm)
191 const Elf64_Ehdr *eh;
196 eh = elf64_getehdr(elf);
198 /* get symtable by section index */
199 sc = elf_getscn(elf, sym_idx);
200 sd = elf_getdata(sc, NULL);
205 n = re_sz / sizeof(re[0]);
206 for (i = 0; i != n; i++) {
208 ofs = re[i].r_offset;
210 /* retrieve index in the symtable */
211 sym = ELF64_R_SYM(re[i].r_info);
212 if (sym * sizeof(sm[0]) >= sd->d_size)
215 sn = elf_strptr(elf, eh->e_shstrndx, sm[sym].st_name);
217 rc = resolve_xsym(sn, ofs, ins, ins_sz, prm);
220 "resolve_xsym(%s, %zu) error code: %d\n",
230 * helper function, find relocation information (if any)
231 * and update bpf code.
234 elf_reloc_code(Elf *elf, Elf_Data *ed, size_t sidx,
235 const struct rte_bpf_prm *prm)
239 const Elf64_Shdr *sh;
245 /* walk through all sections */
246 for (sc = elf_nextscn(elf, NULL); sc != NULL && rc == 0;
247 sc = elf_nextscn(elf, sc)) {
249 sh = elf64_getshdr(sc);
251 /* relocation data for our code section */
252 if (sh->sh_type == SHT_REL && sh->sh_info == sidx) {
253 sd = elf_getdata(sc, NULL);
254 if (sd == NULL || sd->d_size == 0 ||
255 sd->d_size % sizeof(re[0]) != 0)
257 rc = process_reloc(elf, sh->sh_link,
258 sd->d_buf, sd->d_size, ed->d_buf, ed->d_size,
266 static struct rte_bpf *
267 bpf_load_elf(const struct rte_bpf_prm *prm, int32_t fd, const char *section)
274 struct rte_bpf_prm np;
276 elf_version(EV_CURRENT);
277 elf = elf_begin(fd, ELF_C_READ, NULL);
279 rc = find_elf_code(elf, section, &sd, &sidx);
281 rc = elf_reloc_code(elf, sd, sidx, prm);
286 np.nb_ins = sd->d_size / sizeof(struct ebpf_insn);
287 bpf = rte_bpf_load(&np);
298 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
304 if (prm == NULL || fname == NULL || sname == NULL) {
309 fd = open(fname, O_RDONLY);
312 RTE_BPF_LOG(ERR, "%s(%s) error code: %d(%s)\n",
313 __func__, fname, rc, strerror(rc));
318 bpf = bpf_load_elf(prm, fd, sname);
323 "%s(fname=\"%s\", sname=\"%s\") failed, "
325 __func__, fname, sname, rte_errno);
329 RTE_BPF_LOG(INFO, "%s(fname=\"%s\", sname=\"%s\") "
330 "successfully creates %p(jit={.func=%p,.sz=%zu});\n",
331 __func__, fname, sname, bpf, bpf->jit.func, bpf->jit.sz);