netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andriin@fb.com>,
	dwarves@vger.kernel.org, Networking <netdev@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>, Yonghong Song <yhs@fb.com>,
	Hao Luo <haoluo@google.com>, Martin KaFai Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	Mark Wielaard <mjw@redhat.com>
Subject: Re: [PATCH 2/2] bpf_encoder: Translate SHN_XINDEX in symbol's st_shndx values
Date: Fri, 22 Jan 2021 21:37:48 +0100	[thread overview]
Message-ID: <20210122203748.GA70760@krava> (raw)
In-Reply-To: <CAEf4BzbC-s=27vmcJ1KYLVKgGbns2py1bHny3Q_yr4v3Oe49RQ@mail.gmail.com>

On Fri, Jan 22, 2021 at 12:16:42PM -0800, Andrii Nakryiko wrote:
> On Fri, Jan 22, 2021 at 8:46 AM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > For very large ELF objects (with many sections), we could
> > get special value SHN_XINDEX (65535) for symbol's st_shndx.
> >
> > This patch is adding code to detect the optional extended
> > section index table and use it to resolve symbol's section
> > index.
> >
> > Adding elf_symtab__for_each_symbol_index macro that returns
> > symbol's section index and usign it in collect functions.
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  btf_encoder.c | 59 +++++++++++++++++++++++++++++++++++++--------------
> >  elf_symtab.c  | 39 +++++++++++++++++++++++++++++++++-
> >  elf_symtab.h  |  2 ++
> >  3 files changed, 83 insertions(+), 17 deletions(-)
> >
> > diff --git a/btf_encoder.c b/btf_encoder.c
> > index 5557c9efd365..56ee55965093 100644
> > --- a/btf_encoder.c
> > +++ b/btf_encoder.c
> > @@ -63,13 +63,13 @@ static void delete_functions(void)
> >  #define max(x, y) ((x) < (y) ? (y) : (x))
> >  #endif
> >
> > -static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
> > +static int collect_function(struct btf_elf *btfe, GElf_Sym *sym,
> > +                           Elf32_Word sym_sec_idx)
> 
> nit: we use size_t or int for this, no need for libelf types here, imo

ok

> 
> 
> 
> >  {
> >         struct elf_function *new;
> >         static GElf_Shdr sh;
> > -       static int last_idx;
> > +       static Elf32_Word last_idx;
> >         const char *name;
> > -       int idx;
> >
> >         if (elf_sym__type(sym) != STT_FUNC)
> >                 return 0;
> > @@ -90,12 +90,10 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
> >                 functions = new;
> >         }
> >
> > -       idx = elf_sym__section(sym);
> > -
> > -       if (idx != last_idx) {
> > -               if (!elf_section_by_idx(btfe->elf, &sh, idx))
> > +       if (sym_sec_idx != last_idx) {
> > +               if (!elf_section_by_idx(btfe->elf, &sh, sym_sec_idx))
> >                         return 0;
> > -               last_idx = idx;
> > +               last_idx = sym_sec_idx;
> >         }
> >
> >         functions[functions_cnt].name = name;
> > @@ -542,14 +540,15 @@ static bool percpu_var_exists(uint64_t addr, uint32_t *sz, const char **name)
> >         return true;
> >  }
> >
> > -static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym)
> > +static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym,
> > +                             Elf32_Word sym_sec_idx)
> 
> nit: same, size_t or just int would be fine
> 
> >  {
> >         const char *sym_name;
> >         uint64_t addr;
> >         uint32_t size;
> >
> >         /* compare a symbol's shndx to determine if it's a percpu variable */
> > -       if (elf_sym__section(sym) != btfe->percpu_shndx)
> > +       if (sym_sec_idx != btfe->percpu_shndx)
> >                 return 0;
> >         if (elf_sym__type(sym) != STT_OBJECT)
> >                 return 0;
> > @@ -585,12 +584,13 @@ static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym)
> >         return 0;
> >  }
> >
> > -static void collect_symbol(GElf_Sym *sym, struct funcs_layout *fl)
> > +static void collect_symbol(GElf_Sym *sym, struct funcs_layout *fl,
> > +                          Elf32_Word sym_sec_idx)
> >  {
> >         if (!fl->mcount_start &&
> >             !strcmp("__start_mcount_loc", elf_sym__name(sym, btfe->symtab))) {
> >                 fl->mcount_start = sym->st_value;
> > -               fl->mcount_sec_idx = sym->st_shndx;
> > +               fl->mcount_sec_idx = sym_sec_idx;
> >         }
> >
> >         if (!fl->mcount_stop &&
> > @@ -598,9 +598,36 @@ static void collect_symbol(GElf_Sym *sym, struct funcs_layout *fl)
> >                 fl->mcount_stop = sym->st_value;
> >  }
> >
> > +static bool elf_sym__get(Elf_Data *syms, Elf_Data *syms_sec_idx_table,
> > +                        int id, GElf_Sym *sym, Elf32_Word *sym_sec_idx)
> 
> This is a generic function, why don't you want to move it into elf_symtab.h?
> 
> > +{
> > +       if (!gelf_getsym(syms, id, sym))
> > +               return false;
> > +
> > +       *sym_sec_idx = sym->st_shndx;
> > +
> > +       if (sym->st_shndx == SHN_XINDEX) {
> > +               if (!syms_sec_idx_table)
> > +                       return false;
> > +               if (!gelf_getsymshndx(syms, syms_sec_idx_table,
> > +                                     id, sym, sym_sec_idx))
> > +                       return false;
> 
> You also ignored my feedback about not fetching symbol twice. Why?

ugh, I did not read your last 2 comments.. let me check that, sry

> 
> > +       }
> > +
> > +       return true;
> > +}
> > +
> > +#define elf_symtab__for_each_symbol_index(symtab, id, sym, sym_sec_idx)                \
> > +       for (id = 0;                                                            \
> > +            id < symtab->nr_syms &&                                            \
> > +            elf_sym__get(symtab->syms, symtab->syms_sec_idx_table,             \
> > +                         id, &sym, &sym_sec_idx);                              \
> > +            id++)
> 
> This should be in elf_symtab.h next to elf_symtab__for_each_symbol.
> 
> And thinking a bit more, the variant with just ignoring symbols that
> we failed to get is probably a safer alternative. I.e., currently
> there is no way to communicate that we terminated iteration with
> error, so it's probably better to skip failed symbols and still get
> the rest, no? I was hoping to discuss stuff like this on the previous
> version of the patch...

I was thinking of that, but then I thought it's better to fail,
than have possibly wrong data in BTF, because the ELF data is
possibly damaged? no idea.. so I took the safer way

jirka


  reply	other threads:[~2021-01-22 20:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-22 16:39 [PATCHv3 0/2] libbpf: Add support to use optional extended section index table Jiri Olsa
2021-01-22 16:39 ` [PATCH 1/2] elf_symtab: Add support for SHN_XINDEX index to elf_section_by_name Jiri Olsa
2021-01-22 19:45   ` Arnaldo Carvalho de Melo
2021-01-22 20:05   ` Andrii Nakryiko
2021-01-22 16:39 ` [PATCH 2/2] bpf_encoder: Translate SHN_XINDEX in symbol's st_shndx values Jiri Olsa
2021-01-22 19:52   ` Arnaldo Carvalho de Melo
2021-01-22 20:24     ` Jiri Olsa
2021-01-22 20:33       ` Arnaldo Carvalho de Melo
2021-01-25 16:16     ` Joe Lawrence
2021-01-22 20:16   ` Andrii Nakryiko
2021-01-22 20:37     ` Jiri Olsa [this message]
2021-01-25 17:37       ` Arnaldo Carvalho de Melo
2021-01-25 17:38         ` Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2021-01-24 22:15 [PATCHv4 0/2] libbpf: Add support to use optional extended section index table Jiri Olsa
2021-01-24 22:15 ` [PATCH 2/2] bpf_encoder: Translate SHN_XINDEX in symbol's st_shndx values Jiri Olsa
2021-01-25 23:51   ` Andrii Nakryiko

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=20210122203748.GA70760@krava \
    --to=jolsa@redhat.com \
    --cc=acme@kernel.org \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dwarves@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=joe.lawrence@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=mjw@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).