Message ID | 5672557ebf511ff2231853fa306a36d280313165.1724976539.git.tony.ambardar@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | libbpf, selftests/bpf: Support cross-endian usage | expand |
On Fri, 2024-08-30 at 00:29 -0700, Tony Ambardar wrote: > Object linking output data uses the default ELF_T_BYTE type for '.symtab' > section data, which disables any libelf-based translation. Explicitly set > the ELF_T_SYM type for output to restore libelf's byte-order conversion, > noting that input '.symtab' data is already correctly translated. > > Fixes: faf6ed321cf6 ("libbpf: Add BPF static linker APIs") > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> > --- > tools/lib/bpf/linker.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c > index 9cd3d4109788..7489306cd6f7 100644 > --- a/tools/lib/bpf/linker.c > +++ b/tools/lib/bpf/linker.c > @@ -396,6 +396,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) > pr_warn_elf("failed to create SYMTAB data"); > return -EINVAL; > } > + /* Ensure libelf translates byte-order of symbol records */ > + sec->data->d_type = ELF_T_SYM; I tried grepping through libelf to find out how this affects things, and identified that it is primarily used by elfutils/libelf/gelf_xlatetof.c:gelf_xlatetof(), which is an interface function and we don't seem to use it. It is also used by dwfl_* functions while applying relocations, but we don't use that either. Could you please elaborate a bit on effects of this change? > > str_off = strset__add_str(linker->strtab_strs, sec->sec_name); > if (str_off < 0)
On Fri, Aug 30, 2024 at 03:15:10PM -0700, Eduard Zingerman wrote: > On Fri, 2024-08-30 at 00:29 -0700, Tony Ambardar wrote: > > Object linking output data uses the default ELF_T_BYTE type for '.symtab' > > section data, which disables any libelf-based translation. Explicitly set > > the ELF_T_SYM type for output to restore libelf's byte-order conversion, > > noting that input '.symtab' data is already correctly translated. > > > > Fixes: faf6ed321cf6 ("libbpf: Add BPF static linker APIs") > > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> > > --- > > tools/lib/bpf/linker.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c > > index 9cd3d4109788..7489306cd6f7 100644 > > --- a/tools/lib/bpf/linker.c > > +++ b/tools/lib/bpf/linker.c > > @@ -396,6 +396,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) > > pr_warn_elf("failed to create SYMTAB data"); > > return -EINVAL; > > } > > + /* Ensure libelf translates byte-order of symbol records */ > > + sec->data->d_type = ELF_T_SYM; > > I tried grepping through libelf to find out how this affects things, > and identified that it is primarily used by elfutils/libelf/gelf_xlatetof.c:gelf_xlatetof(), > which is an interface function and we don't seem to use it. > It is also used by dwfl_* functions while applying relocations, > but we don't use that either. > Right, gelf_xlatetof() is exposed for _explicit_ user conversions, but libelf still does translations implicitly for known section record types, based on the ELF file's byte-order metadata. The idea is that ELF data loaded in memory will be native-endianness for accessibility, but output in the original endianness at rest/in a file, all transparently. We try to follow the same idea in libbpf when opening and writing .BTF and .BTF.ext data (e.g. see the *_raw_data() funcs). > Could you please elaborate a bit on effects of this change? > When linking objects of either endianness, libelf can translate the input files based on ELF headers (endianness and type ELF_T_SYM) and allows us to process .symtab data. When writing out the linked file however, we create a new .symtab section in init_output_elf() but leave it as default ELT_T_BYTE type, which undergoes no translation and leaves .symtab always in native byte-order regardless of target endianness. See also 61e8aeda9398 ("bpf: Fix libelf endian handling in resolv_btfids") and related links for a similar example and explanations. Hope that helps. Cheers, Tony > > > > str_off = strset__add_str(linker->strtab_strs, sec->sec_name); > > if (str_off < 0) >
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 9cd3d4109788..7489306cd6f7 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -396,6 +396,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) pr_warn_elf("failed to create SYMTAB data"); return -EINVAL; } + /* Ensure libelf translates byte-order of symbol records */ + sec->data->d_type = ELF_T_SYM; str_off = strset__add_str(linker->strtab_strs, sec->sec_name); if (str_off < 0)
Object linking output data uses the default ELF_T_BYTE type for '.symtab' section data, which disables any libelf-based translation. Explicitly set the ELF_T_SYM type for output to restore libelf's byte-order conversion, noting that input '.symtab' data is already correctly translated. Fixes: faf6ed321cf6 ("libbpf: Add BPF static linker APIs") Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> --- tools/lib/bpf/linker.c | 2 ++ 1 file changed, 2 insertions(+)