From: Jiri Olsa <jolsa@redhat.com>
To: Tony Ambardar <tony.ambardar@gmail.com>
Cc: Yonghong Song <yhs@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>, bpf <bpf@vger.kernel.org>,
Networking <netdev@vger.kernel.org>,
Stable <stable@vger.kernel.org>, Jiri Olsa <jolsa@kernel.org>,
Frank Eigler <fche@redhat.com>, Mark Wielaard <mjw@redhat.com>
Subject: Re: [PATCH bpf v1] bpf: fix libelf endian handling in resolv_btfids
Date: Thu, 17 Jun 2021 11:10:03 +0200 [thread overview]
Message-ID: <YMsRa3nT4tlzO6DJ@krava> (raw)
In-Reply-To: <CAPGftE-CqfycuyTRpFvHwe5kR5gG8WGyLSgdLTat5XnxmqQ3GQ@mail.gmail.com>
On Wed, Jun 16, 2021 at 03:09:13PM -0700, Tony Ambardar wrote:
> On Wed, 16 Jun 2021 at 09:38, Jiri Olsa <jolsa@redhat.com> wrote:
> >
> > On Wed, Jun 16, 2021 at 08:56:42AM -0700, Yonghong Song wrote:
> > >
> > > On 6/16/21 2:25 AM, Tony Ambardar wrote:
> > > > While patching the .BTF_ids section in vmlinux, resolve_btfids writes type
> > > > ids using host-native endianness, and relies on libelf for any required
> > > > translation when finally updating vmlinux. However, the default type of the
> > > > .BTF_ids section content is ELF_T_BYTE (i.e. unsigned char), and undergoes
> > > > no translation. This results in incorrect patched values if cross-compiling
> > > > to non-native endianness, and can manifest as kernel Oops and test failures
> > > > which are difficult to debug.
> >
> > nice catch, great libelf can do that ;-)
>
> Funny, I'd actually assumed that was your intention, but I just
> couldn't find where the
> data type was being set, so resorted to this "kludge". While there's a .BTF_ids
> section definition in include/linux/btf_ids.h, there's no means I can
> see to specify
> the data type either (i.e. in the gcc asm .pushsection() options). That approach
> would be cleaner.
>
> >
> > > >
> > > > Explicitly set the type of patched data to ELF_T_WORD, allowing libelf to
> > > > transparently handle the endian conversions.
> > > >
> > > > Fixes: fbbb68de80a4 ("bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object")
> > > > Cc: stable@vger.kernel.org # v5.10+
> > > > Cc: Jiri Olsa <jolsa@kernel.org>
> > > > Cc: Yonghong Song <yhs@fb.com>
> > > > Link: https://lore.kernel.org/bpf/CAPGftE_eY-Zdi3wBcgDfkz_iOr1KF10n=9mJHm1_a_PykcsoeA@mail.gmail.com/
> > > > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> > > > ---
> > > > tools/bpf/resolve_btfids/main.c | 3 +++
> > > > 1 file changed, 3 insertions(+)
> > > >
> > > > diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
> > > > index d636643ddd35..f32c059fbfb4 100644
> > > > --- a/tools/bpf/resolve_btfids/main.c
> > > > +++ b/tools/bpf/resolve_btfids/main.c
> > > > @@ -649,6 +649,9 @@ static int symbols_patch(struct object *obj)
> > > > if (sets_patch(obj))
> > > > return -1;
> > > > + /* Set type to ensure endian translation occurs. */
> > > > + obj->efile.idlist->d_type = ELF_T_WORD;
> > >
> > > The change makes sense to me as .BTF_ids contains just a list of
> > > u32's.
> > >
> > > Jiri, could you double check on this?
> >
> > the comment in ELF_T_WORD declaration suggests the size depends on
> > elf's class?
> >
> > ELF_T_WORD, /* Elf32_Word, Elf64_Word, ... */
> >
> > data in .BTF_ids section are allways u32
> >
>
> I believe the Elf32/Elf64 refer to the arch since some data structures vary
> between the two, but ELF_T_WORD is common to both, and valid as the
> data type of Elf_Data struct holding the .BTF_ids contents. See elf(5):
>
> Basic types
> The following types are used for N-bit architectures (N=32,64, ElfN
> stands for Elf32 or Elf64, uintN_t stands for uint32_t or uint64_t):
> ...
> ElfN_Word uint32_t
>
> Also see the code and comments in "elf.h":
> /* Types for signed and unsigned 32-bit quantities. */
> typedef uint32_t Elf32_Word;
> typedef uint32_t Elf64_Word;
ok
>
> > I have no idea how is this handled in libelf (perhaps it's ok),
> > but just that comment above suggests it could be also 64 bits,
> > cc-ing Frank and Mark for more insight
> >
>
> One other area I'd like to confirm is with section compression. Is it safe
> to ignore this for .BTF_ids? I've done so because include/linux/btf_ids.h
> appears to define the section with SHF_ALLOC flag set, which is
> incompatible with compression based on "libelf.h" comments.
not sure what you mean.. where it wouldn't be safe?
what workflow/processing
thanks,
jirka
next prev parent reply other threads:[~2021-06-17 9:10 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-16 9:25 [PATCH bpf v1] bpf: fix libelf endian handling in resolv_btfids Tony Ambardar
2021-06-16 15:56 ` Yonghong Song
2021-06-16 16:38 ` Jiri Olsa
2021-06-16 22:09 ` Tony Ambardar
2021-06-17 9:10 ` Jiri Olsa [this message]
2021-06-17 10:44 ` Mark Wielaard
2021-06-16 22:28 ` Mark Wielaard
2021-06-17 9:02 ` Jiri Olsa
2021-06-17 11:22 ` Daniel Borkmann
2021-06-18 0:20 ` Tony Ambardar
2021-06-18 6:14 ` [PATCH bpf v2] " Tony Ambardar
2021-06-18 15:10 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YMsRa3nT4tlzO6DJ@krava \
--to=jolsa@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=fche@redhat.com \
--cc=jolsa@kernel.org \
--cc=mjw@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=tony.ambardar@gmail.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.