eliben / pyelftools

Parsing ELF and DWARF in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot hash Section

Dragorn421 opened this issue · comments

Using pyelftools 0.30

The Section type isn't hashable despite defining a __hash__ method
Or more correctly, because of that method it is not hashable. The method raises an error.

  File ".../elftools/elf/sections.py", line 126, in __hash__
    return hash(self.header)
           ^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'Container'

Reproduce

  1. pip install pyelftools==0.30
  2. get an elf file. For example echo | as to produce a a.out elf (assuming elf unix-y system)
  3. run the following script:
from elftools.elf.elffile import ELFFile

with open("a.out", "rb") as f:
    elf = ELFFile(f)
    sections = set(elf.iter_sections())
  1. observe error
Traceback (most recent call last):
  File ".../test_pyelftools_section_hash.py", line 5, in <module>
    sections = set(elf.iter_sections())
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../elftools/elf/sections.py", line 126, in __hash__
    return hash(self.header)
           ^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'Container'

Suggested fix

Just remove Section.__hash__ and use default "id()"-based hashing.

For example Symbol also doesn't have a __hash__ (but neither a __eq__ which Section does define)