Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Support linking bpf objects of either endianness

Allow static linking object files of either endianness, checking that input
files have consistent byte-order, and setting output endianness from input.

Linking requires in-memory processing of programs, relocations, sections,
etc. in native endianness, and output conversion to target byte-order. This
is enabled by built-in ELF translation and recent BTF/BTF.ext endianness
functions. Further add local functions for swapping byte-order of sections
containing BPF insns.

Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/b47ca686d02664843fc99b96262fe3259650bc43.1726475448.git.tony.ambardar@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Tony Ambardar and committed by
Alexei Starovoitov
0aed726c bcc60abd

+62 -16
+62 -16
tools/lib/bpf/linker.c
··· 135 135 int fd; 136 136 Elf *elf; 137 137 Elf64_Ehdr *elf_hdr; 138 + bool swapped_endian; 138 139 139 140 /* Output sections metadata */ 140 141 struct dst_sec *secs; ··· 325 324 326 325 linker->elf_hdr->e_machine = EM_BPF; 327 326 linker->elf_hdr->e_type = ET_REL; 328 - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 329 - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB; 330 - #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 331 - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB; 332 - #else 333 - #error "Unknown __BYTE_ORDER__" 334 - #endif 327 + /* Set unknown ELF endianness, assign later from input files */ 328 + linker->elf_hdr->e_ident[EI_DATA] = ELFDATANONE; 335 329 336 330 /* STRTAB */ 337 331 /* initialize strset with an empty string to conform to ELF */ ··· 537 541 const struct bpf_linker_file_opts *opts, 538 542 struct src_obj *obj) 539 543 { 540 - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 541 - const int host_endianness = ELFDATA2LSB; 542 - #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 543 - const int host_endianness = ELFDATA2MSB; 544 - #else 545 - #error "Unknown __BYTE_ORDER__" 546 - #endif 547 544 int err = 0; 548 545 Elf_Scn *scn; 549 546 Elf_Data *data; 550 547 Elf64_Ehdr *ehdr; 551 548 Elf64_Shdr *shdr; 552 549 struct src_sec *sec; 550 + unsigned char obj_byteorder; 551 + unsigned char link_byteorder = linker->elf_hdr->e_ident[EI_DATA]; 552 + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 553 + const unsigned char host_byteorder = ELFDATA2LSB; 554 + #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 555 + const unsigned char host_byteorder = ELFDATA2MSB; 556 + #else 557 + #error "Unknown __BYTE_ORDER__" 558 + #endif 553 559 554 560 pr_debug("linker: adding object file '%s'...\n", filename); 555 561 ··· 577 579 pr_warn_elf("failed to get ELF header for %s", filename); 578 580 return err; 579 581 } 580 - if (ehdr->e_ident[EI_DATA] != host_endianness) { 582 + 583 + /* Linker output endianness set by first input object */ 584 + obj_byteorder = ehdr->e_ident[EI_DATA]; 585 + if (obj_byteorder != ELFDATA2LSB && obj_byteorder != ELFDATA2MSB) { 581 586 err = -EOPNOTSUPP; 582 - pr_warn_elf("unsupported byte order of ELF file %s", filename); 587 + pr_warn("unknown byte order of ELF file %s\n", filename); 583 588 return err; 584 589 } 590 + if (link_byteorder == ELFDATANONE) { 591 + linker->elf_hdr->e_ident[EI_DATA] = obj_byteorder; 592 + linker->swapped_endian = obj_byteorder != host_byteorder; 593 + pr_debug("linker: set %s-endian output byte order\n", 594 + obj_byteorder == ELFDATA2MSB ? "big" : "little"); 595 + } else if (link_byteorder != obj_byteorder) { 596 + err = -EOPNOTSUPP; 597 + pr_warn("byte order mismatch with ELF file %s\n", filename); 598 + return err; 599 + } 600 + 585 601 if (ehdr->e_type != ET_REL 586 602 || ehdr->e_machine != EM_BPF 587 603 || ehdr->e_ident[EI_CLASS] != ELFCLASS64) { ··· 1123 1111 return true; 1124 1112 } 1125 1113 1114 + static bool is_exec_sec(struct dst_sec *sec) 1115 + { 1116 + if (!sec || sec->ephemeral) 1117 + return false; 1118 + return (sec->shdr->sh_type == SHT_PROGBITS) && 1119 + (sec->shdr->sh_flags & SHF_EXECINSTR); 1120 + } 1121 + 1122 + static void exec_sec_bswap(void *raw_data, int size) 1123 + { 1124 + const int insn_cnt = size / sizeof(struct bpf_insn); 1125 + struct bpf_insn *insn = raw_data; 1126 + int i; 1127 + 1128 + for (i = 0; i < insn_cnt; i++, insn++) 1129 + bpf_insn_bswap(insn); 1130 + } 1131 + 1126 1132 static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src) 1127 1133 { 1128 1134 void *tmp; ··· 1200 1170 memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz); 1201 1171 /* now copy src data at a properly aligned offset */ 1202 1172 memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size); 1173 + 1174 + /* convert added bpf insns to native byte-order */ 1175 + if (linker->swapped_endian && is_exec_sec(dst)) 1176 + exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size); 1203 1177 } 1204 1178 1205 1179 dst->sec_sz = dst_final_sz; ··· 2664 2630 if (!sec->scn) 2665 2631 continue; 2666 2632 2633 + /* restore sections with bpf insns to target byte-order */ 2634 + if (linker->swapped_endian && is_exec_sec(sec)) 2635 + exec_sec_bswap(sec->raw_data, sec->sec_sz); 2636 + 2667 2637 sec->data->d_buf = sec->raw_data; 2668 2638 } 2669 2639 ··· 2736 2698 2737 2699 static int finalize_btf(struct bpf_linker *linker) 2738 2700 { 2701 + enum btf_endianness link_endianness; 2739 2702 LIBBPF_OPTS(btf_dedup_opts, opts); 2740 2703 struct btf *btf = linker->btf; 2741 2704 const void *raw_data; ··· 2780 2741 pr_warn("BTF dedup failed: %d\n", err); 2781 2742 return err; 2782 2743 } 2744 + 2745 + /* Set .BTF and .BTF.ext output byte order */ 2746 + link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ? 2747 + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; 2748 + btf__set_endianness(linker->btf, link_endianness); 2749 + if (linker->btf_ext) 2750 + btf_ext__set_endianness(linker->btf_ext, link_endianness); 2783 2751 2784 2752 /* Emit .BTF section */ 2785 2753 raw_data = btf__raw_data(linker->btf, &raw_sz);