From 0193fb8e36ba7e44863119834ef3817f91f3f95b Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 26 Nov 2011 16:16:16 +0200 Subject: [PATCH] cleanup + add profiling top-level function to readelf --- elftools/dwarf/dwarfrelocationmanager.py | 61 ------------------------ scripts/readelf.py | 21 ++++++-- 2 files changed, 18 insertions(+), 64 deletions(-) delete mode 100644 elftools/dwarf/dwarfrelocationmanager.py diff --git a/elftools/dwarf/dwarfrelocationmanager.py b/elftools/dwarf/dwarfrelocationmanager.py deleted file mode 100644 index 048e1c6..0000000 --- a/elftools/dwarf/dwarfrelocationmanager.py +++ /dev/null @@ -1,61 +0,0 @@ -#------------------------------------------------------------------------------- -# elftools: dwarf/dwarfrelocationmanager.py -# -# DWARFRelocationManager - handles relocations of DWARF data -# -# Eli Bendersky (eliben@gmail.com) -# This code is in the public domain -#------------------------------------------------------------------------------- -from ..elf.sections import RelocationSection - - -class DWARFRelocationManager(object): - """ Manages relocations for some DWARF section - """ - def __init__(self, elffile, section_name): - """ - """ - self.elffile = elffile - self.section_name = section_name - self._section = self.elffile.get_section_by_name(section_name) - - # _relocs maps an offset in the section to an index in the relocation - # table. - # _reloc_section is the relocation section object - # ... both are loaded by _load_relocations - self._relocs = {} - self._reloc_section = None - self._load_relocations() - - def has_relocation(self, offset): - """ Does the given offset have a relocation registered for it? - The offset is relative to its section. - """ - return offset in self._relocs - - def apply_relocation(self, offset, value): - """ Apply the relocation registered for the given offset. value is - the original value at that offset. Return the relocated value. - """ - reloc_index = self._relocs[offset] - return self.elffile.apply_relocation( - reloc_section=self._reloc_section, - reloc_index=reloc_index, - offset=offset, - value=value) - - def _load_relocations(self): - # Currently assume that only a single relocation section will exist - # for our section, and that it's either a .rel or a .rela - reloc_section_names = ( - '.rel' + self.section_name, - '.rela' + self.section_name) - for section in self.elffile.iter_sections(): - if ( isinstance(section, RelocationSection) and - section.name in reloc_section_names): - self._reloc_section = section - for i, reloc in enumerate( - self._reloc_section.iter_relocations()): - self._relocs[reloc['r_offset']] = i - break - diff --git a/scripts/readelf.py b/scripts/readelf.py index 8a76c6f..79fb174 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -553,7 +553,7 @@ SCRIPT_DESCRIPTION = 'Display information about the contents of ELF format files VERSION_STRING = '%%prog: based on pyelftools %s' % __version__ -def main(): +def main(stream=None): # parse the command-line arguments and invoke ReadElf optparser = OptionParser( usage='usage: %prog [options] ', @@ -607,7 +607,7 @@ def main(): with open(args[0], 'rb') as file: try: - readelf = ReadElf(file, sys.stdout) + readelf = ReadElf(file, stream or sys.stdout) if do_file_header: readelf.display_file_header() if do_section_header: @@ -631,7 +631,22 @@ def main(): sys.exit(1) +def profile_main(): + # Run 'main' redirecting its output to readelfout.txt + # Saves profiling information in readelf.profile + PROFFILE = 'readelf.profile' + import cProfile + cProfile.run('main(open("readelfout.txt", "w"))', PROFFILE) + + # Dig in some profiling stats + import pstats + p = pstats.Stats(PROFFILE) + p.sort_stats('cumulative').print_stats(25) + + #------------------------------------------------------------------------------- if __name__ == '__main__': - main() + #main() + profile_main() + -- 2.30.2