* added another test file (real-life object file borrowed from libelf)
authorEli Bendersky <eliben@gmail.com>
Fri, 23 Sep 2011 12:23:41 +0000 (15:23 +0300)
committerEli Bendersky <eliben@gmail.com>
Fri, 23 Sep 2011 12:23:41 +0000 (15:23 +0300)
* print out notification about sections having relocations against them in the file
* munge run_tests to ignore case when comparing lines

elftools/elf/sections.py
scripts/readelf.py
tests/run_tests.py
tests/testfiles/update32.o.elf [new file with mode: 0644]

index 10b99f9cfe248d85bcad3c107e895b6afeeff3c1..d545ec2a220e42361186333ae7201968dc836ffd 100644 (file)
@@ -39,6 +39,9 @@ class Section(object):
         """
         return self.header[name]
 
+    def __eq__(self, other):
+        return self.header == other.header
+
 
 class NullSection(Section):
     """ ELF NULL section
index 8f1c27f074503f46cfc4ce277fe2d71b781229a8..d90b5bf116f7450d1bba35e0b8ea038c90897083 100755 (executable)
@@ -302,12 +302,19 @@ class ReadElf(object):
                     continue
 
                 symbol = symtable.get_symbol(rel['r_info_sym'])
+                # Some symbols have zero 'st_name', so instead what's used is
+                # the name of the section they point at
+                if symbol['st_name'] == 0:
+                    symsec = self.elffile.get_section(symbol['st_shndx'])
+                    symbol_name = symsec.name
+                else:
+                    symbol_name = symbol.name
                 self._emit(' %s %s%s' % (
                     self._format_hex(
                         symbol['st_value'],
                         fullhex=True, lead0x=False),
                     '  ' if self.elffile.elfclass == 32 else '',
-                    symbol.name))
+                    symbol_name))
                 if section.is_RELA():
                     self._emit(' %s %x' % (
                         '+' if rel['r_addend'] >= 0 else '-',
@@ -328,7 +335,7 @@ class ReadElf(object):
             return
 
         self._emitline("\nHex dump of section '%s':" % section.name)
-
+        self._note_relocs_for_section(section)
         addr = section['sh_addr']
         data = section.data()
         dataptr = 0
@@ -438,6 +445,17 @@ class ReadElf(object):
             # Not a number. Must be a name then
             return self.elffile.get_section_by_name(spec)
 
+    def _note_relocs_for_section(self, section):
+        """ If there are relocation sections pointing to the givne section,
+            emit a note about it.
+        """
+        for relsec in self.elffile.iter_sections():
+            if isinstance(relsec, RelocationSection):
+                info_idx = relsec['sh_info']
+                if self.elffile.get_section(info_idx) == section:
+                    self._emitline('  Note: This section has relocations against it, but these have NOT been applied to this dump.')
+                    return
+
     def _emit(self, s=''):
         """ Emit an object to output
         """
index fda237175c24508e6a860bea53a2ce1fd1d0412c..b0b24a08e1c8141cb9539feaecfffd985d4b3957 100755 (executable)
@@ -84,10 +84,12 @@ def compare_output(s1, s2):
         Note: this function contains some rather horrible hacks to ignore
         differences which are not important for the verification of pyelftools.
         This is due to some intricacies of binutils's readelf which pyelftools
-        doesn't currently implement. Read the documentation for more details.
+        doesn't currently implement, or silly inconsistencies in the output of
+        readelf, which I was reluctant to replicate.
+        Read the documentation for more details.
     """
-    lines1 = s1.splitlines()
-    lines2 = s2.splitlines()
+    lines1 = s1.lower().splitlines()
+    lines2 = s2.lower().splitlines()
     if len(lines1) != len(lines2):
         return False, 'Number of lines different: %s vs %s' % (
                 len(lines1), len(lines2))
diff --git a/tests/testfiles/update32.o.elf b/tests/testfiles/update32.o.elf
new file mode 100644 (file)
index 0000000..ae68ef9
Binary files /dev/null and b/tests/testfiles/update32.o.elf differ