[diffoscope] 01/01: comparators: elf: adds radare2 command classes

Juliana Oliveira jwnx-guest at moszumanska.debian.org
Fri Mar 2 02:47:58 CET 2018


This is an automated email from the git hooks/post-receive script.

jwnx-guest pushed a commit to branch jwnx_radare2_readelf
in repository diffoscope.

commit 2990ba98e760d300f32338afb64dce8aa2d98da4
Author: Juliana Oliveira <juliana.orod at gmail.com>
Date:   Thu Mar 1 21:44:15 2018 -0300

    comparators: elf: adds radare2 command classes
    
    Signed-off-by: Juliana Oliveira <juliana.orod at gmail.com>
---
 diffoscope/comparators/elf.py | 112 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/diffoscope/comparators/elf.py b/diffoscope/comparators/elf.py
index 181ea6e..f3c4d83 100644
--- a/diffoscope/comparators/elf.py
+++ b/diffoscope/comparators/elf.py
@@ -258,6 +258,61 @@ READELF_COMMANDS = (
 )
 
 
+class Radare2Command(Command):
+
+    @tool_required('radare2')
+    def cmdline(self):
+        return ['radare2', '-e', 'io.cache=true', '-q0', self.path,
+                 '-2', '-c'] + self.options()
+
+    def options(self):
+        return []
+
+    def filter(self, line):
+        if line.startswith(b'invalid'):
+            return b''
+        return line
+
+
+class Radare2FullDump(Radare2Command):
+    def options(self):
+        return ["pi"]
+
+
+class Radare2Sections(Radare2Command):
+    def options(self):
+        return ["S"]
+
+class Rabin2FileProperties(Radare2Command):
+
+    @tool_required('rabin2')
+    def cmdline(self):
+        return ['radare2', '-e', 'io.cache=true', '-q0', self.path,
+                 '-2', '-c'] + self.options()
+
+
+class Radare2SectionDump(Radare2Command):
+    def __init__(self, path=None, section=None, *args, **kwargs):
+        super().__init__(path, *args, **kwargs)
+        self._section = section
+
+    def options(self):
+        return (["s {} ; pi {}".format(self._section._section_start,
+                                       self._section._section_size)])
+
+
+RADARE2_COMMANDS = (
+    Radare2Sections,
+)
+
+
+def _compare_radare2_elf_data(path1, path2):
+    return [
+        Difference.from_command(x, path1, path2)
+        for x in list(RADARE2_COMMANDS)
+    ]
+
+
 def _compare_elf_data(path1, path2):
     return [
         Difference.from_command(x, path1, path2)
@@ -403,6 +458,7 @@ def get_debug_link(path):
 
 class ElfContainer(Container):
     auto_diff_metadata = False
+    required_tool = 'readelf'
 
     SECTION_FLAG_MAPPING = {
         'X': ElfCodeSection,
@@ -557,6 +613,62 @@ class ElfContainer(Container):
         return self._sections[member_name]
 
 
+class Radare2Section(ElfSection):
+
+    def __init__(self, container, name, start, size):
+      super().__init__(elf_container=container, member_name=name)
+      self._section_name = name
+      self._section_start = start
+      self._section_size = size
+      self._section_cmd = Radare2SectionDump
+
+
+    def compare(self, other, source=None):
+      return Difference.from_command(self._section_cmd,
+                                     self.path,
+                                     other.path,
+                                     command_args=[self])
+
+
+
+class Radare2Container(Container):
+
+    required_tool = 'radare2'
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        logger.debug("Creating Radare2Container for %s", self.source.path)
+
+        output = subprocess.check_output(self.__get_sections(),
+                                         shell=False,
+                                         stderr=subprocess.DEVNULL)
+
+        self._sections = self.__format_sections(output)
+
+
+    def __get_sections(self):
+        return (Radare2Sections(path=self.source.path).cmdline())
+
+    def __format_sections(self, output):
+        sections = {}
+        for line in output.splitlines():
+            line = line.decode('utf-8').split(' ')
+
+            if line[7].startswith('.'):
+                sections[line[7]] = Radare2Section(container = self,
+                                                   name = line[7],
+                                                   start = line[4][3:],
+                                                   size = line[5][3:])
+        return sections
+
+
+    def get_member_names(self):
+        return self._sections.keys()
+
+    def get_member(self, member_name):
+        return self._sections[member_name]
+
+
 class ElfFile(File):
     CONTAINER_CLASS = ElfContainer
     FILE_TYPE_RE = re.compile(r'^ELF ')

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list