eliben / pyelftools

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

Helper to check type in ELFFile.get_section_by_name()? #570

Closed rossburton closed 1 month ago

rossburton commented 1 month ago

When a library with debug symbols is split into code and debug info, the debug info has all of the original segments but without any content. This means that if you do get_section_by_name(".dynamic") you'll get a stub Section instance.

For code which wants to poke around an ELF, I wonder if it would be useful to have an optional arguments to save boilerplate. Maybe:

# Check the type is as expected
def get_section_by_name(self, name, type=None)
...
get_section_by_name(".dynamic", "SHT_DYNAMIC")

Or:

# Check the type isn't NOBITS
def get_section_by_name(self, name, content_only=False)
get_section_by_name(".dynamic", True)
sevaa commented 1 month ago

@eliben I don't know, an optional argument that will save the OP a one line check...

eliben commented 1 month ago

@rossburton can you elaborate a bit more on the boilerplate saved?

rossburton commented 1 month ago

It is pretty minimal, but I've a lot of code that does this:

dynamic = elf.get_section_by_name('.dynamic')
if not dynamic or dynamic["sh_type"] != "SHT_DYNAMIC":
  return

I can have a convenience wrapper in my code if you prefer.

eliben commented 1 month ago

Understood, thanks. I'd like to keep it minimal for now; will be on the lookout for similar requests