it's alive, ALIVEhg sthg st! DWARF relocations seem to be working fine
authorEli Bendersky <eliben@gmail.com>
Thu, 24 Nov 2011 06:12:37 +0000 (08:12 +0200)
committerEli Bendersky <eliben@gmail.com>
Thu, 24 Nov 2011 06:12:37 +0000 (08:12 +0200)
elftools/dwarf/compileunit.py
elftools/dwarf/descriptions.py
elftools/dwarf/die.py
elftools/dwarf/dwarfinfo.py
scripts/readelf.py
z.py

index fda4980d9f437aab889cd1b7c93a23658b0c790a..fbe4a8b302282373cfeeee3024b44543eaf794c5 100644 (file)
@@ -113,7 +113,10 @@ class CompileUnit(object):
         # First pass: parse all DIEs and place them into self._dielist
         die_offset = self.cu_die_offset
         while die_offset < cu_boundary:
-            die = DIE(cu=self, stream=self.dwarfinfo.stream, offset=die_offset)
+            die = DIE(
+                    cu=self,
+                    stream=self.dwarfinfo.debug_info_sec.stream,
+                    offset=die_offset)
             self._dielist.append(die)
             die_offset += die.size
 
index 66768275f61485510640b2bc92e5fb55273f256b..037aa7ed985d85ed8ce12b31c686a6a890d94618 100644 (file)
@@ -35,7 +35,7 @@ def describe_attr_value(attr, die, section_offset):
 #-------------------------------------------------------------------------------
 
 def _describe_attr_ref(attr, die, section_offset):
-    return '<0x%x>' % (attr.value + die.cu.cu_offset - section_offset)
+    return '<0x%x>' % (attr.value + die.cu.cu_offset)
 
 def _describe_attr_value_passthrough(attr, die, section_offset):
     return attr.value
index f438934edc79cc22f2e776170075f807eddbe52c..82d65e98dff3b666a9cdb302b593a3b58bee1df0 100644 (file)
@@ -162,12 +162,7 @@ class DIE(object):
         
         # The offset of the .debug_info section in the stream. Used to compute
         # relative offset of attribute values to the beginning of the section.
-        section_offset = self.dwarfinfo.debug_info_loc.offset
-
-        # Some attribute values need relocations. These are computed with the
-        # help of a DWARFRelocationManager for .debug_info, which is held by
-        # DWARFInfo for this purpose.
-        relocation_manager = self.dwarfinfo.relocation_manager['.debug_info']
+        section_offset = self.dwarfinfo.debug_info_sec.global_offset
 
         # Guided by the attributes listed in the abbreviation declaration, parse
         # values from the stream.
@@ -176,18 +171,6 @@ class DIE(object):
             attr_offset = self.stream.tell()
             raw_value = struct_parse(structs.Dwarf_dw_form[form], self.stream)
 
-            # raw_value may need to be relocated, if there's a relocation
-            # registered for this offset in the relocation manager.
-            # Relocations are listed by offset relative to the beginning of
-            # the section.
-            offset_from_section = attr_offset - section_offset
-            if relocation_manager.has_relocation(offset_from_section):
-                # Applying the relocation may change the stream, so preserve it
-                with preserve_stream_pos(self.stream):
-                    raw_value = relocation_manager.apply_relocation(
-                            offset=offset_from_section,
-                            value=raw_value)
-
             value = self._translate_attr_value(form, raw_value)            
             self.attributes[name] = AttributeValue(
                 name=name,
index 3a66871b8b1942fcdd80f0b2c460b60e96cf451c..bfbba8d175fd737584aff6dd1f4fddb39a936d12 100644 (file)
@@ -107,7 +107,7 @@ class DWARFInfo(object):
         if offset not in self._abbrevtable_cache:
             self._abbrevtable_cache[offset] = AbbrevTable(
                 structs=self.structs,
-                stream=self.stream,
+                stream=self.debug_abbrev_sec.stream,
                 offset=offset)
         return self._abbrevtable_cache[offset]
     
@@ -117,7 +117,7 @@ class DWARFInfo(object):
         """
         return struct_parse(
             CString(''),
-            self.stream,
+            self.debug_str_sec.stream,
             stream_pos=offset)
     
     #------ PRIVATE ------#
@@ -138,7 +138,7 @@ class DWARFInfo(object):
             # instance suitable for this CU and use it to parse the rest.
             #
             initial_length = struct_parse(
-                self.structs.Dwarf_uint32(''), self.stream, offset)
+                self.structs.Dwarf_uint32(''), self.debug_info_sec.stream, offset)
             dwarf_format = 64 if initial_length == 0xFFFFFFFF else 32
             
             # At this point we still haven't read the whole header, so we don't
@@ -153,14 +153,14 @@ class DWARFInfo(object):
                 address_size=4)
             
             cu_header = struct_parse(
-                cu_structs.Dwarf_CU_header, self.stream, offset)
+                cu_structs.Dwarf_CU_header, self.debug_info_sec.stream, offset)
             if cu_header['address_size'] == 8:
                 cu_structs = DWARFStructs(
                     little_endian=self.little_endian,
                     dwarf_format=dwarf_format,
                      address_size=8)
             
-            cu_die_offset = self.stream.tell()
+            cu_die_offset = self.debug_info_sec.stream.tell()
             dwarf_assert(
                 self._is_supported_version(cu_header['version']),
                 "Expected supported DWARF version. Got '%s'" % cu_header['version'])
index 85b8f0b1da530e59f62be5d9746c90df5fc0676e..2de7cb65e3ed97a60fe0ccea03043c489a07079c 100755 (executable)
@@ -24,7 +24,8 @@ from elftools import __version__
 from elftools.common.exceptions import ELFError
 from elftools.elf.elffile import ELFFile
 from elftools.elf.segments import InterpSegment
-from elftools.elf.sections import SymbolTableSection, RelocationSection
+from elftools.elf.sections import SymbolTableSection
+from elftools.elf.relocation import RelocationSection
 from elftools.elf.descriptions import (
     describe_ei_class, describe_ei_data, describe_ei_version,
     describe_ei_osabi, describe_e_type, describe_e_machine,
@@ -33,7 +34,7 @@ from elftools.elf.descriptions import (
     describe_symbol_type, describe_symbol_bind, describe_symbol_visibility,
     describe_symbol_shndx, describe_reloc_type,
     )
-from elftools.dwarf.dwarfinfo import DWARFInfo, DebugSectionLocator
+from elftools.dwarf.dwarfinfo import DWARFInfo
 from elftools.dwarf.descriptions import describe_attr_value
 
 
@@ -496,13 +497,11 @@ class ReadElf(object):
         self._emitline('Contents of the .debug_info section:\n')
         
         # Offset of the .debug_info section in the stream
-        section_offset = self._dwarfinfo.debug_info_loc.offset
+        section_offset = self._dwarfinfo.debug_info_sec.global_offset
 
-        print '&&& section_offset', section_offset
-        
         for cu in self._dwarfinfo.iter_CUs():
             self._emitline('  Compilation Unit @ offset %s:' %
-                self._format_hex(cu.cu_offset - section_offset))
+                self._format_hex(cu.cu_offset))
             self._emitline('   Length:        %s (%s)' % (
                 self._format_hex(cu['unit_length']),
                 '%s-bit' % cu.dwarf_format()))
@@ -523,13 +522,13 @@ class ReadElf(object):
                     continue
                 self._emitline(' <%s><%x>: Abbrev Number: %s (%s)' % (
                     die_depth,
-                    die.offset - section_offset,
+                    die.offset,
                     die.abbrev_code,
                     die.tag))
                 
                 for attr in die.attributes.itervalues():
                     self._emitline('    <%2x>   %-18s: %s' % (
-                        attr.offset - section_offset,
+                        attr.offset,
                         attr.name,
                         describe_attr_value(
                             attr, die, section_offset)))
diff --git a/z.py b/z.py
index b0cdfb273cd715d18ee709bd5d429a4a193852ef..865d75b0e9cf416bc2558df2729b35f800d8cd72 100644 (file)
--- a/z.py
+++ b/z.py
@@ -31,8 +31,4 @@ topdie = cu.get_top_DIE()
 
 print topdie
 dinfo_sec = efile.get_section_by_name('.debug_info')
-relman = DWARFRelocationManager(efile, dinfo_sec.name)
-
-print relman._reloc_section.name, relman._reloc_section['sh_offset']
-#pprint.pprint(relman._relocs)