Просмотр исходного кода

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.
Marius Vikhammer 4 лет назад
Родитель
Сommit
8b259d15f6
2 измененных файлов с 11 добавлено и 21 удалено
  1. 11 20
      components/espcoredump/corefile/elf.py
  2. 0 1
      tools/ci/mypy_ignore_list.txt

+ 11 - 20
components/espcoredump/corefile/elf.py

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

+ 0 - 1
tools/ci/mypy_ignore_list.txt

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