From f8d2cf7a48d5b71a88432e3e97914b8876386281 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sun, 31 Mar 2013 06:56:56 -0700 Subject: [PATCH] Tweak dynamic section handling to decipher DT_SONAME + small cleanups --- elftools/elf/dynamic.py | 14 +++++++++----- scripts/readelf.py | 5 +++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/elftools/elf/dynamic.py b/elftools/elf/dynamic.py index 9d7dfaa..a62ef16 100644 --- a/elftools/elf/dynamic.py +++ b/elftools/elf/dynamic.py @@ -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() diff --git a/scripts/readelf.py b/scripts/readelf.py index 8c1b0f8..91c0e75 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -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)) -- 2.30.2