Hi Mark,

On Wed, 13 Nov 2013 12:30:17 +0100, Mark Wielaard wrote:
> I am not sure this is fixable, even if we would remove the aux_file
> support. Should we just add a comment to the function that the
> st_shndx/shndxp cannot be relied upon? Or should we provide a new dwfl
> interface that provides the Elf file given a symbol index (somewhat like
> this i_to_symfile function)?

left for the next mail [patch 2/4].


> This looks correct.

Checked in.


Thanks,
Jan
commit 5dca2b62db78163535aa5c7ee5fbda32f1087d6f
Author: Jan Kratochvil <[email protected]>
Date:   Wed Nov 13 19:58:06 2013 +0100

    Fix dwfl_module_addrsym for minidebuginfo
    
    Signed-off-by: Jan Kratochvil <[email protected]>

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 467b486..282f011 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,12 @@
+2013-11-13  Jan Kratochvil  <[email protected]>
+
+       Fix dwfl_module_addrsym for minidebuginfo.
+       * dwfl_module_addrsym.c (dwfl_module_addrsym): New variable
+       addr_symfile.
+       (dwfl_module_addrsym) (same_section): Use it.
+       (dwfl_module_addrsym) (i_to_symfile): New function.
+       (dwfl_module_addrsym) (search_table): Use it.
+
 2013-11-07  Jan Kratochvil  <[email protected]>
            Mark Wielaard  <[email protected]>
 
diff --git a/libdwfl/dwfl_module_addrsym.c b/libdwfl/dwfl_module_addrsym.c
index 732b698..3d19943 100644
--- a/libdwfl/dwfl_module_addrsym.c
+++ b/libdwfl/dwfl_module_addrsym.c
@@ -41,6 +41,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
 
   /* Return true iff we consider ADDR to lie in the same section as SYM.  */
   GElf_Word addr_shndx = SHN_UNDEF;
+  struct dwfl_file *addr_symfile = NULL;
   inline bool same_section (const GElf_Sym *sym, struct dwfl_file *symfile,
                            GElf_Word shndx)
     {
@@ -49,11 +50,12 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
        return sym->st_value == addr;
 
       /* Figure out what section ADDR lies in.  */
-      if (addr_shndx == SHN_UNDEF)
+      if (addr_shndx == SHN_UNDEF || addr_symfile != symfile)
        {
          GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symfile, addr);
          Elf_Scn *scn = NULL;
          addr_shndx = SHN_ABS;
+         addr_symfile = symfile;
          while ((scn = elf_nextscn (symfile->elf, scn)) != NULL)
            {
              GElf_Shdr shdr_mem;
@@ -68,7 +70,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
            }
        }
 
-      return shndx == addr_shndx;
+      return shndx == addr_shndx && addr_symfile == symfile;
     }
 
   /* Keep track of the closest symbol we have seen so far.
@@ -84,6 +86,20 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
   /* Keep track of the lowest address a relevant sizeless symbol could have.  
*/
   GElf_Addr min_label = 0;
 
+  inline struct dwfl_file *i_to_symfile (int ndx)
+    {
+      int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
+      if (ndx < mod->first_global)
+       return mod->symfile;    // main symbol table (locals).
+      else if (ndx < mod->first_global + mod->aux_first_global - skip_aux_zero)
+       return &mod->aux_sym;   // aux symbol table (locals).
+      else if ((size_t) ndx
+              < mod->syments + mod->aux_first_global - skip_aux_zero)
+       return mod->symfile;    // main symbol table (globals).
+      else
+       return &mod->aux_sym;   // aux symbol table (globals).
+    }
+
   /* Look through the symbol table for a matching symbol.  */
   inline void search_table (int start, int end)
     {
@@ -136,11 +152,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
                        }
                      else if (closest_name == NULL
                               && sym.st_value >= min_label
-                              && same_section (&sym,
-                                               ((size_t) i < mod->syments
-                                                ? mod->symfile
-                                                : &mod->aux_sym),
-                                               shndx))
+                              && same_section (&sym, i_to_symfile (i), shndx))
                        {
                          /* Handwritten assembly symbols sometimes have no
                             st_size.  If no symbol with proper size includes

Reply via email to