Skip to content

Cannot hash Section #539

@Dragorn421

Description

@Dragorn421

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions