diff --git a/elftools/dwarf/dwarfinfo.py b/elftools/dwarf/dwarfinfo.py index 55966393..a6aa1ada 100644 --- a/elftools/dwarf/dwarfinfo.py +++ b/elftools/dwarf/dwarfinfo.py @@ -82,7 +82,8 @@ def __init__(self, debug_rnglists_sec, debug_sup_sec, gnu_debugaltlink_sec, - debug_types_sec + debug_types_sec, + max_cu_cache_size = -1 ): """ config: A DwarfConfig object @@ -91,6 +92,9 @@ def __init__(self, DebugSectionDescriptor for a section. Pass None for sections that don't exist. These arguments are best given with keyword syntax. + + max_cu_cache_size: + Enforces a limit on CU cache size, For unlimitted cache size set to -1 """ self.config = config self.debug_info_sec = debug_info_sec @@ -113,6 +117,8 @@ def __init__(self, self.gnu_debugaltlink_sec = gnu_debugaltlink_sec self.debug_types_sec = debug_types_sec + self.max_cu_cache_size = max_cu_cache_size + # Sets the supplementary_dwarfinfo to None. Client code can set this # to something else, typically a DWARFInfo file read from an ELFFile # which path is stored in the debug_sup_sec or gnu_debugaltlink_sec. @@ -547,6 +553,12 @@ def _cached_CU_at_offset(self, offset): # bisect_right search while the parallel indexed ._cu_cache[] holds # the object references. cu = self._parse_CU_at_offset(offset) + + # max_cu_cache_size of -1 makes cache size unlimited + if self.max_cu_cache_size != -1 and len(self._cu_cache) > self.max_cu_cache_size: + self._cu_offsets_map.pop(0) + self._cu_cache.pop(0) + self._cu_offsets_map.insert(i, offset) self._cu_cache.insert(i, cu) return cu diff --git a/elftools/elf/elffile.py b/elftools/elf/elffile.py index fb46daba..3ac34765 100644 --- a/elftools/elf/elffile.py +++ b/elftools/elf/elffile.py @@ -261,7 +261,7 @@ def has_dwarf_info(self, strict=False): self.has_section('.zdebug_info') or (not strict and self.has_section('.eh_frame'))) - def get_dwarf_info(self, relocate_dwarf_sections=True, follow_links=True): + def get_dwarf_info(self, relocate_dwarf_sections=True, follow_links=True, max_cu_cache_size = -1): """ Return a DWARFInfo object representing the debugging information in this file. @@ -355,7 +355,8 @@ def get_dwarf_info(self, relocate_dwarf_sections=True, follow_links=True): debug_rnglists_sec=debug_sections[debug_rnglists_sec_name], debug_sup_sec=debug_sections[debug_sup_name], gnu_debugaltlink_sec=debug_sections[gnu_debugaltlink_name], - debug_types_sec=debug_sections[debug_types_sec_name] + debug_types_sec=debug_sections[debug_types_sec_name], + max_cu_cache_size = max_cu_cache_size ) if follow_links: dwarfinfo.supplementary_dwarfinfo = self.get_supplementary_dwarfinfo(dwarfinfo)