diff --git a/elftools/elf/descriptions.py b/elftools/elf/descriptions.py index a86027f2..69422c17 100644 --- a/elftools/elf/descriptions.py +++ b/elftools/elf/descriptions.py @@ -27,7 +27,7 @@ def describe_ei_data(x): def describe_ei_version(x): - s = '%d' % ENUM_E_VERSION[x] + s = str(ENUM_E_VERSION.get(x, f"{x} ")) if x == 'EV_CURRENT': s += ' (current)' return s @@ -41,9 +41,10 @@ def describe_e_type(x, elffile=None): if elffile is not None and x == 'ET_DYN': # Detect whether this is a normal SO or a PIE executable dynamic = elffile.get_section_by_name('.dynamic') - for t in dynamic.iter_tags('DT_FLAGS_1'): - if t.entry.d_val & ENUM_DT_FLAGS_1['DF_1_PIE']: - return 'DYN (Position-Independent Executable file)' + if dynamic: + for t in dynamic.iter_tags('DT_FLAGS_1'): + if t.entry.d_val & ENUM_DT_FLAGS_1['DF_1_PIE']: + return 'DYN (Position-Independent Executable file)' return _DESCR_E_TYPE.get(x, _unknown) @@ -52,7 +53,7 @@ def describe_e_machine(x): def describe_e_version_numeric(x): - return '0x%x' % ENUM_E_VERSION[x] + return f"{ENUM_E_VERSION.get(x, x):#x}" def describe_p_type(x): diff --git a/elftools/elf/elffile.py b/elftools/elf/elffile.py index 6efc27ca..a5953814 100644 --- a/elftools/elf/elffile.py +++ b/elftools/elf/elffile.py @@ -84,8 +84,7 @@ def __init__(self, stream, stream_loader=None): self.stream.seek(0) self.e_ident_raw = self.stream.read(16) - self._section_header_stringtable = \ - self._get_section_header_stringtable() + self._section_header_stringtable = None # Lazy load self._section_name_map = None self.stream_loader = stream_loader @@ -693,8 +692,10 @@ def _get_section_name(self, section_header): """ Given a section header, find this section's name in the file's string table """ - if self._section_header_stringtable is None: - raise ELFParseError("String Table not found") + if self._section_header_stringtable is None: # Try to lazy load + self._section_header_stringtable = self._get_section_header_stringtable() + if self._section_header_stringtable is None: # If absent (or badly malformed) + raise ELFParseError("String Table not found") name_offset = section_header['sh_name'] return self._section_header_stringtable.get_string(name_offset)