eliben / pyelftools

Parsing ELF and DWARF in Python
Other
1.99k stars 507 forks source link

Cannot hash Section #539

Open Dragorn421 opened 7 months ago

Dragorn421 commented 7 months ago

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

4. observe error

Traceback (most recent call last): File ".../test_pyelftools_section_hash.py", line 5, in 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)