Thread (18 messages) 18 messages, 4 authors, 2021-01-24

Re: [PATCH 2/3] bpf_encoder: Translate SHN_XINDEX in symbol's st_shndx values

From: Andrii Nakryiko <hidden>
Date: 2021-01-21 23:33:35
Also in: bpf

On Thu, Jan 21, 2021 at 12:25 PM Jiri Olsa [off-list ref] 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_symbols function.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
You missed fixing up collect_function() as well, which is using
elf_sym__section(), which doesn't know about extended numbering.
quoted hunk ↗ jump to hunk
 btf_encoder.c | 36 ++++++++++++++++++++++++++++++++----
 elf_symtab.c  | 39 ++++++++++++++++++++++++++++++++++++++-
 elf_symtab.h  |  2 ++
 3 files changed, 72 insertions(+), 5 deletions(-)
diff --git a/btf_encoder.c b/btf_encoder.c
index 5557c9efd365..6e6f22c438ce 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -585,12 +585,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 +599,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)
+{
+       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))

gelf_getsymshndx() is supposed to work even for cases that don't use
extended numbering, so this should work, right?

if (!gelf_getsymshndx(syms, syms_sec_idx_table, id, sym, sym_sec_idx))
    return false;

if (sym->st_shndx == SHN_XINDEX)
  *sym_sec_idx = sym->st_shndx;

return true;

?
+                       return false;
+       }
+
+       return true;
+}
+
+#define elf_symtab__for_each_symbol_index(symtab, id, sym, sym_sec_idx)                \
+       for (id = 0, elf_sym__get(symtab->syms, symtab->syms_sec_idx_table,     \
+                                 id, &sym, &sym_sec_idx);                      \
+            id < symtab->nr_syms;                                              \
+            id++, elf_sym__get(symtab->syms, symtab->syms_sec_idx_table,       \
+                               id, &sym, &sym_sec_idx))
what do we want to do if elf_sym__get() returns error (false)? We can
either stop or ignore that symbol, right? But currently you are
returning invalid symbol data.

so either

for (id = 0; id < symtab->nr_syms && elf_sym__get(symtab->syms,
symtab->syms_sec_idx_table, d, &sym, &sym_sec_idx); id++)

or

for (id = 0; id < symtab->nr_syms; id++)
  if (elf_sym__get(symtab->syms, symtab->syms_sec_idx_table, d, &sym,
&sym_sec_idx))


But the current variant looks broken. Oh, and
elf_symtab__for_each_symbol() is similarly broken, can you please fix
that as well?

And this new macro should probably be in elf_symtab.h, along the
elf_symtab__for_each_symbol.

+
 static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)
 {
        struct funcs_layout fl = { };
+       Elf32_Word sym_sec_idx;
        uint32_t core_id;
        GElf_Sym sym;
[...]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help