coredump: fix section name parsing in python utility

elf.py assumed every section header name had its own string in
shstrtab, but multiple sections may reuse the same substring with
different offsets.
This commit is contained in:
Marius Vikhammer
2021-08-02 12:15:23 +08:00
parent a5a131825f
commit 8b259d15f6
2 changed files with 11 additions and 21 deletions

View File

@@ -121,7 +121,6 @@ class ElfFile(object):
self._struct = None # type: Optional[Struct] self._struct = None # type: Optional[Struct]
self._model = None # type: Optional[Container] self._model = None # type: Optional[Container]
self._section_names = {} # type: dict[int, str]
self.sections = [] # type: list[ElfSection] self.sections = [] # type: list[ElfSection]
self.load_segments = [] # type: list[ElfSegment] self.load_segments = [] # type: list[ElfSegment]
@@ -146,36 +145,28 @@ class ElfFile(object):
self._struct = self._generate_struct_from_headers(header_tables) self._struct = self._generate_struct_from_headers(header_tables)
self._model = self._struct.parse(elf_bytes) self._model = self._struct.parse(elf_bytes)
if 'string_table' in self._model:
self._section_names = self._parse_string_table(self._model.string_table)
self.load_segments = [ElfSegment(seg.ph.p_vaddr, self.load_segments = [ElfSegment(seg.ph.p_vaddr,
seg.data, seg.data,
seg.ph.p_flags) for seg in self._model.load_segments] seg.ph.p_flags) for seg in self._model.load_segments]
self.note_segments = [ElfNoteSegment(seg.ph.p_vaddr, self.note_segments = [ElfNoteSegment(seg.ph.p_vaddr,
seg.data, seg.data,
seg.ph.p_flags) for seg in self._model.note_segments] seg.ph.p_flags) for seg in self._model.note_segments]
self.sections = [ElfSection(self._section_names[sec.sh.sh_name], self.sections = [ElfSection(self._parse_string_table(self._model.string_table, sec.sh.sh_name),
sec.sh.sh_addr, sec.sh.sh_addr,
sec.data, sec.data,
sec.sh.sh_flags) for sec in self._model.sections] sec.sh.sh_flags) for sec in self._model.sections]
@staticmethod @staticmethod
def _parse_string_table(byte_str): # type: (bytes) -> dict def _parse_string_table(byte_str, offset): # type: (bytes, int) -> str
name = '' section_name_str = byte_str[offset:]
index = 0 string_end = section_name_str.find(0x00)
res = {}
for i, c in enumerate(byte_str): if (string_end == -1):
if c in [0x00, '\x00']: # a workaround for python 2 bytes is actually string raise ValueError('Unable to get section name from section header string table')
res[index] = name
name = '' name = section_name_str[:string_end].decode('utf-8')
index = i + 1
continue return name
if isinstance(c, int):
name += chr(c)
else:
name += c
return res
def _generate_struct_from_headers(self, header_tables): # type: (Container) -> Struct def _generate_struct_from_headers(self, header_tables): # type: (Container) -> Struct
""" """

View File

@@ -3,7 +3,6 @@ components/efuse/efuse_table_gen.py
components/efuse/test_efuse_host/efuse_tests.py components/efuse/test_efuse_host/efuse_tests.py
components/esp_local_ctrl/python/esp_local_ctrl_pb2.py components/esp_local_ctrl/python/esp_local_ctrl_pb2.py
components/esp_netif/test_apps/component_ut_test.py components/esp_netif/test_apps/component_ut_test.py
components/espcoredump/corefile/elf.py
components/espcoredump/corefile/gdb.py components/espcoredump/corefile/gdb.py
components/espcoredump/test/test_espcoredump.py components/espcoredump/test/test_espcoredump.py
components/lwip/weekend_test/net_suite_test.py components/lwip/weekend_test/net_suite_test.py