Message ID | 895753fe6de5a06dee8ac110f0c05571db73e70c.1724976539.git.tony.ambardar@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | libbpf, selftests/bpf: Support cross-endian usage | expand |
On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > 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> > --- > tools/lib/bpf/linker.c | 90 ++++++++++++++++++++++++++++++++++-------- > 1 file changed, 74 insertions(+), 16 deletions(-) > [...] > > +static bool is_exec_sec(struct dst_sec *sec) > +{ > + if (!sec || sec->ephemeral) > + return false; > + return (sec->shdr->sh_type == SHT_PROGBITS) && > + (sec->shdr->sh_flags & SHF_EXECINSTR); > +} > + > +static int exec_sec_bswap(void *raw_data, int size) > +{ > + const int insn_cnt = size / sizeof(struct bpf_insn); > + struct bpf_insn *insn = raw_data; > + int i; > + > + if (size % sizeof(struct bpf_insn)) > + return -EINVAL; this shouldn't be checked here, it should be assumed this is valid and was ensured by the caller. And make exec_sec_bswap() a void function, please. > + for (i = 0; i < insn_cnt; i++, insn++) > + bpf_insn_bswap(insn); > + return 0; > +} > + > static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src) > { > void *tmp; > @@ -1170,6 +1203,10 @@ static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src > memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz); > /* now copy src data at a properly aligned offset */ > memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size); > + the check for size % sizeof(struct bpf_insn) should be somewhere here (if is_exec_sec()), right? > + /* convert added bpf insns to native byte-order */ > + if (linker->swapped_endian && is_exec_sec(dst)) > + exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size); > } > > dst->sec_sz = dst_final_sz; > @@ -2630,6 +2667,10 @@ int bpf_linker__finalize(struct bpf_linker *linker) > if (!sec->scn) > continue; > but no need to check here, we know it's correct, if we got all the way here > + /* restore sections with bpf insns to target byte-order */ > + if (linker->swapped_endian && is_exec_sec(sec)) > + exec_sec_bswap(sec->raw_data, sec->sec_sz); > + > sec->data->d_buf = sec->raw_data; > } > > @@ -2698,6 +2739,7 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name, > > static int finalize_btf(struct bpf_linker *linker) > { > + enum btf_endianness link_endianness; > LIBBPF_OPTS(btf_dedup_opts, opts); > struct btf *btf = linker->btf; > const void *raw_data; > @@ -2742,6 +2784,22 @@ static int finalize_btf(struct bpf_linker *linker) > return err; > } > > + /* Set .BTF and .BTF.ext output byte order */ > + link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ? > + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; > + err = btf__set_endianness(linker->btf, link_endianness); > + if (err) { > + pr_warn("failed to set .BTF output endianness: %d\n", err); > + return err; > + } link_endianness is always well-formed enum, there is no need to check errors, here and for btf_ext__set_endianness, please drop both > + if (linker->btf_ext) { > + err = btf_ext__set_endianness(linker->btf_ext, link_endianness); > + if (err) { > + pr_warn("failed to set .BTF.ext output endianness: %d\n", err); > + return err; > + } > + } > + > /* Emit .BTF section */ > raw_data = btf__raw_data(linker->btf, &raw_sz); > if (!raw_data) > -- > 2.34.1 >
On Fri, Aug 30, 2024 at 02:25:07PM -0700, Andrii Nakryiko wrote: > On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > > > 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> > > --- > > tools/lib/bpf/linker.c | 90 ++++++++++++++++++++++++++++++++++-------- > > 1 file changed, 74 insertions(+), 16 deletions(-) > > > > [...] > > > > > +static bool is_exec_sec(struct dst_sec *sec) > > +{ > > + if (!sec || sec->ephemeral) > > + return false; > > + return (sec->shdr->sh_type == SHT_PROGBITS) && > > + (sec->shdr->sh_flags & SHF_EXECINSTR); > > +} > > + > > +static int exec_sec_bswap(void *raw_data, int size) > > +{ > > + const int insn_cnt = size / sizeof(struct bpf_insn); > > + struct bpf_insn *insn = raw_data; > > + int i; > > + > > + if (size % sizeof(struct bpf_insn)) > > + return -EINVAL; > > this shouldn't be checked here, it should be assumed this is valid and > was ensured by the caller. And make exec_sec_bswap() a void function, > please. > > > + for (i = 0; i < insn_cnt; i++, insn++) > > + bpf_insn_bswap(insn); > > + return 0; > > +} > > + > > static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src) > > { > > void *tmp; > > @@ -1170,6 +1203,10 @@ static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src > > memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz); > > /* now copy src data at a properly aligned offset */ > > memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size); > > + > > the check for size % sizeof(struct bpf_insn) should be somewhere here > (if is_exec_sec()), right? > > > + /* convert added bpf insns to native byte-order */ > > + if (linker->swapped_endian && is_exec_sec(dst)) > > + exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size); > > } > > > > dst->sec_sz = dst_final_sz; > > @@ -2630,6 +2667,10 @@ int bpf_linker__finalize(struct bpf_linker *linker) > > if (!sec->scn) > > continue; > > > > but no need to check here, we know it's correct, if we got all the way here > I did a pass earlier to reorganize sanity checks and remove redundant ones, and realized linker_sanity_check_elf() already does what we want but overlooked dropping this from exec_sec_bswap(). Will do so now. Thanks for catching! > > + /* restore sections with bpf insns to target byte-order */ > > + if (linker->swapped_endian && is_exec_sec(sec)) > > + exec_sec_bswap(sec->raw_data, sec->sec_sz); > > + > > sec->data->d_buf = sec->raw_data; > > } > > > > @@ -2698,6 +2739,7 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name, > > > > static int finalize_btf(struct bpf_linker *linker) > > { > > + enum btf_endianness link_endianness; > > LIBBPF_OPTS(btf_dedup_opts, opts); > > struct btf *btf = linker->btf; > > const void *raw_data; > > @@ -2742,6 +2784,22 @@ static int finalize_btf(struct bpf_linker *linker) > > return err; > > } > > > > + /* Set .BTF and .BTF.ext output byte order */ > > + link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ? > > + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; > > + err = btf__set_endianness(linker->btf, link_endianness); > > + if (err) { > > + pr_warn("failed to set .BTF output endianness: %d\n", err); > > + return err; > > + } > > link_endianness is always well-formed enum, there is no need to check > errors, here and for btf_ext__set_endianness, please drop both Right, makes sense. > > > + if (linker->btf_ext) { > > + err = btf_ext__set_endianness(linker->btf_ext, link_endianness); > > + if (err) { > > + pr_warn("failed to set .BTF.ext output endianness: %d\n", err); > > + return err; > > + } > > + } > > + > > /* Emit .BTF section */ > > raw_data = btf__raw_data(linker->btf, &raw_sz); > > if (!raw_data) > > -- > > 2.34.1 > >
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 7489306cd6f7..bd97da68eed6 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -135,6 +135,7 @@ struct bpf_linker { int fd; Elf *elf; Elf64_Ehdr *elf_hdr; + bool swapped_endian; /* Output sections metadata */ struct dst_sec *secs; @@ -324,13 +325,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) linker->elf_hdr->e_machine = EM_BPF; linker->elf_hdr->e_type = ET_REL; -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB; -#else -#error "Unknown __BYTE_ORDER__" -#endif + /* Set unknown ELF endianness, assign later from input files */ + linker->elf_hdr->e_ident[EI_DATA] = ELFDATANONE; /* STRTAB */ /* initialize strset with an empty string to conform to ELF */ @@ -541,19 +537,21 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, const struct bpf_linker_file_opts *opts, struct src_obj *obj) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - const int host_endianness = ELFDATA2LSB; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - const int host_endianness = ELFDATA2MSB; -#else -#error "Unknown __BYTE_ORDER__" -#endif int err = 0; Elf_Scn *scn; Elf_Data *data; Elf64_Ehdr *ehdr; Elf64_Shdr *shdr; struct src_sec *sec; + unsigned char obj_byteorder; + unsigned char link_byteorder = linker->elf_hdr->e_ident[EI_DATA]; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + const unsigned char host_byteorder = ELFDATA2LSB; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + const unsigned char host_byteorder = ELFDATA2MSB; +#else +#error "Unknown __BYTE_ORDER__" +#endif pr_debug("linker: adding object file '%s'...\n", filename); @@ -579,11 +577,25 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, pr_warn_elf("failed to get ELF header for %s", filename); return err; } - if (ehdr->e_ident[EI_DATA] != host_endianness) { + + /* Linker output endianness set by first input object */ + obj_byteorder = ehdr->e_ident[EI_DATA]; + if (obj_byteorder != ELFDATA2LSB && obj_byteorder != ELFDATA2MSB) { + err = -EOPNOTSUPP; + pr_warn("unknown byte order of ELF file %s\n", filename); + return err; + } + if (link_byteorder == ELFDATANONE) { + linker->elf_hdr->e_ident[EI_DATA] = obj_byteorder; + linker->swapped_endian = obj_byteorder != host_byteorder; + pr_debug("linker: set %s-endian output byte order\n", + obj_byteorder == ELFDATA2MSB ? "big" : "little"); + } else if (link_byteorder != obj_byteorder) { err = -EOPNOTSUPP; - pr_warn_elf("unsupported byte order of ELF file %s", filename); + pr_warn("byte order mismatch with ELF file %s\n", filename); return err; } + if (ehdr->e_type != ET_REL || ehdr->e_machine != EM_BPF || ehdr->e_ident[EI_CLASS] != ELFCLASS64) { @@ -1111,6 +1123,27 @@ static bool sec_content_is_same(struct dst_sec *dst_sec, struct src_sec *src_sec return true; } +static bool is_exec_sec(struct dst_sec *sec) +{ + if (!sec || sec->ephemeral) + return false; + return (sec->shdr->sh_type == SHT_PROGBITS) && + (sec->shdr->sh_flags & SHF_EXECINSTR); +} + +static int exec_sec_bswap(void *raw_data, int size) +{ + const int insn_cnt = size / sizeof(struct bpf_insn); + struct bpf_insn *insn = raw_data; + int i; + + if (size % sizeof(struct bpf_insn)) + return -EINVAL; + for (i = 0; i < insn_cnt; i++, insn++) + bpf_insn_bswap(insn); + return 0; +} + static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src) { void *tmp; @@ -1170,6 +1203,10 @@ static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz); /* now copy src data at a properly aligned offset */ memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size); + + /* convert added bpf insns to native byte-order */ + if (linker->swapped_endian && is_exec_sec(dst)) + exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size); } dst->sec_sz = dst_final_sz; @@ -2630,6 +2667,10 @@ int bpf_linker__finalize(struct bpf_linker *linker) if (!sec->scn) continue; + /* restore sections with bpf insns to target byte-order */ + if (linker->swapped_endian && is_exec_sec(sec)) + exec_sec_bswap(sec->raw_data, sec->sec_sz); + sec->data->d_buf = sec->raw_data; } @@ -2698,6 +2739,7 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name, static int finalize_btf(struct bpf_linker *linker) { + enum btf_endianness link_endianness; LIBBPF_OPTS(btf_dedup_opts, opts); struct btf *btf = linker->btf; const void *raw_data; @@ -2742,6 +2784,22 @@ static int finalize_btf(struct bpf_linker *linker) return err; } + /* Set .BTF and .BTF.ext output byte order */ + link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ? + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; + err = btf__set_endianness(linker->btf, link_endianness); + if (err) { + pr_warn("failed to set .BTF output endianness: %d\n", err); + return err; + } + if (linker->btf_ext) { + err = btf_ext__set_endianness(linker->btf_ext, link_endianness); + if (err) { + pr_warn("failed to set .BTF.ext output endianness: %d\n", err); + return err; + } + } + /* Emit .BTF section */ raw_data = btf__raw_data(linker->btf, &raw_sz); if (!raw_data)
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> --- tools/lib/bpf/linker.c | 90 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 16 deletions(-)