pyelftools icon indicating copy to clipboard operation
pyelftools copied to clipboard

Check for SHN_UNDEF during symbol dumping

Open sevaa opened this issue 1 year ago • 2 comments

Supersedes #566

Now, I don't know what was the original motivation for that one, but this fix should be equivalent.

sevaa avatar Oct 23 '24 19:10 sevaa

I like the change more but agree that the motivation remains mysterious, especially without any test case. Is this similar to what readelf itself is doing in a comparable code path?

eliben avatar Oct 23 '24 22:10 eliben

There is a similar check in readelf when it retrieves the symbol name for STT_SECTION type symbols. In readelf's print_symbol() there is the following:

/* Get the symbol's name.  For section symbols without a
     specific name use the (already computed) section name.  */
  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
      && section_index_real (filedata, psym->st_shndx)
      && psym->st_name == 0)
    {
      ;
    }
  else
    {
      bool is_valid;

      is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
      sstr = is_valid  ? strtab + psym->st_name : _("<corrupt>");
    }

Where section_index_real() checks, among other things, if st_shndx>0. So as far as understand, the logic is:

  • if st_shndx is nonzero, treat it as a section index, retrieve that section's name
  • if not, retrieve the name from the string table at st_name
  • if that is not available either, use <corrupt>

So this tells me that st_shndx being zero (SHN_UNDEF) is a condition readelf handles gracefully. So pyelftools shouldn't crash on that either :)

sevaa avatar Oct 26 '24 16:10 sevaa