Tweak dynamic section handling to decipher DT_SONAME + small cleanups
authorEli Bendersky <eliben@gmail.com>
Sun, 31 Mar 2013 13:56:56 +0000 (06:56 -0700)
committerEli Bendersky <eliben@gmail.com>
Sun, 31 Mar 2013 13:56:56 +0000 (06:56 -0700)
elftools/elf/dynamic.py
scripts/readelf.py

index 9d7dfaa955aefa2ab3e539410b2f40d3c7703257..a62ef169dc894717f33087a882549546908fb1cc 100644 (file)
@@ -19,11 +19,13 @@ class DynamicTag(object):
     """ Dynamic Tag object - representing a single dynamic tag entry from a
         dynamic section.
 
-        Similarly to Section objects, allows dictionary-like access to the
-        dynamic tag.
+        Allows dictionary-like access to the dynamic structure. For special
+        tags (those listed in the _HANDLED_TAGS set below), creates additional
+        attributes for convenience. For example, .soname will contain the actual
+        value of DT_SONAME (fetched from the dynamic symbol table).
     """
-
-    _HANDLED_TAGS = frozenset(['DT_NEEDED', 'DT_RPATH', 'DT_RUNPATH'])
+    _HANDLED_TAGS = frozenset(
+        ['DT_NEEDED', 'DT_RPATH', 'DT_RUNPATH', 'DT_SONAME'])
 
     def __init__(self, entry, elffile):
         self.entry = entry
@@ -49,11 +51,13 @@ class DynamicTag(object):
 
 
 class Dynamic(object):
+    """ Shared functionality between dynamic sections and segments.
+    """
     def __init__(self, stream, elffile, position):
         self._stream = stream
         self._elffile = elffile
         self._elfstructs = elffile.structs
-        self._num_tags = -1;
+        self._num_tags = -1
         self._offset = position
         self._tagsize = self._elfstructs.Elf_Dyn.sizeof()
 
index 8c1b0f88370cb05f30b35dbf4cd2d38ec1d00436..91c0e75dd3df5296529e19219b359dd0872d9e73 100755 (executable)
@@ -297,7 +297,6 @@ class ReadElf(object):
                 section.num_tags()))
             self._emitline("  Tag        Type                         Name/Value")
 
-            hexwidth = 8 if self.elffile.elfclass == 32 else 16
             padding = 20 + (8 if self.elffile.elfclass == 32 else 0)
             for tag in section.iter_tags():
                 if tag.entry.d_tag == 'DT_NEEDED':
@@ -306,6 +305,8 @@ class ReadElf(object):
                     parsed = 'Library rpath: [%s]' % tag.rpath
                 elif tag.entry.d_tag == 'DT_RUNPATH':
                     parsed = 'Library runpath: [%s]' % tag.runpath
+                elif tag.entry.d_tag == 'DT_SONAME':
+                    parsed = 'Library soname: [%s]' % tag.soname
                 elif (tag.entry.d_tag.endswith('SZ') or
                       tag.entry.d_tag.endswith('ENT')):
                     parsed = '%i (bytes)' % tag['d_val']
@@ -321,7 +322,7 @@ class ReadElf(object):
 
                 self._emitline(" %s %-*s %s" % (
                     self._format_hex(ENUM_D_TAG.get(tag.entry.d_tag, tag.entry.d_tag),
-                        fieldsize=hexwidth, lead0x=True),
+                        fullhex=True, lead0x=True),
                     padding,
                     '(%s)' % (tag.entry.d_tag[3:],),
                     parsed))