1 #-------------------------------------------------------------------------------
2 # elftools: elf/dynamic.py
6 # Mike Frysinger (vapier@gentoo.org)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
11 from .sections import Section
12 from .segments import Segment
13 from ..common.utils import struct_parse
15 from enums import ENUM_D_TAG
18 class DynamicTag(object):
19 """ Dynamic Tag object - representing a single dynamic tag entry from a
22 Similarly to Section objects, allows dictionary-like access to the
26 _HANDLED_TAGS = frozenset(['DT_NEEDED', 'DT_RPATH', 'DT_RUNPATH'])
28 def __init__(self, entry, elffile):
30 if entry.d_tag in self._HANDLED_TAGS:
31 dynstr = elffile.get_section_by_name('.dynstr')
32 setattr(self, entry.d_tag[3:].lower(), dynstr.get_string(self.entry.d_val))
34 def __getitem__(self, name):
35 """ Implement dict-like access to entries
37 return self.entry[name]
40 return '<DynamicTag (%s): %r>' % (self.entry.d_tag, self.entry)
43 if self.entry.d_tag in self._HANDLED_TAGS:
44 s = '"%s"' % getattr(self, self.entry.d_tag[3:].lower())
46 s = '%#x' % self.entry.d_ptr
47 return '<DynamicTag (%s) %s>' % (self.entry.d_tag, s)
50 class Dynamic(object):
51 def __init__(self, stream, elffile, position):
53 self._elffile = elffile
54 self._elfstructs = elffile.structs
56 self._offset = position
57 self._tagsize = self._elfstructs.Elf_Dyn.sizeof()
59 def iter_tags(self, type=None):
60 """ Yield all tags (limit to |type| if specified)
62 for n in itertools.count():
64 if type is None or tag.entry.d_tag == type:
66 if tag.entry.d_tag == 'DT_NULL':
70 """ Get the tag at index #n from the file (DynamicTag object)
72 offset = self._offset + n * self._tagsize
74 self._elfstructs.Elf_Dyn,
77 return DynamicTag(entry, self._elffile)
81 """ Number of dynamic tags in the file
83 if self._num_tags != -1:
86 for n in itertools.count():
88 if tag.entry.d_tag == 'DT_NULL':
93 class DynamicSection(Section, Dynamic):
94 """ ELF dynamic table section. Knows how to process the list of tags.
96 def __init__(self, header, name, stream, elffile):
97 Section.__init__(self, header, name, stream)
98 Dynamic.__init__(self, stream, elffile, self['sh_offset'])
101 class DynamicSegment(Segment, Dynamic):
102 """ ELF dynamic table segment. Knows how to process the list of tags.
104 def __init__(self, header, stream, elffile):
105 Segment.__init__(self, header, stream)
106 Dynamic.__init__(self, stream, elffile, self['p_offset'])