eliben / pyelftools

Parsing ELF and DWARF in Python
Other
2.04k stars 512 forks source link

Check for SHN_UNDEF during symbol dumping #574

Closed sevaa closed 1 month ago

sevaa commented 1 month ago

Supersedes #566

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

eliben commented 1 month ago

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?

sevaa commented 1 month ago

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:

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 :)