1 #-------------------------------------------------------------------------------
2 # elftools: dwarf/abbrevtable.py
4 # DWARF abbreviation table
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from ..common
.utils
import struct_parse
, dwarf_assert
12 class AbbrevTable(object):
13 """ Represents a DWARF abbreviation table.
15 def __init__(self
, structs
, stream
, offset
):
16 """ Create new abbreviation table. Parses the actual table from the
17 stream and stores it internally.
20 A DWARFStructs instance for parsing the data
23 The stream and offset into the stream where this abbreviation
26 self
.structs
= structs
30 self
._abbrev
_map
= self
._parse
_abbrev
_table
()
32 def get_abbrev(self
, code
):
33 """ Get the AbbrevDecl for a given code. Raise KeyError if no
34 declaration for this code exists.
36 return AbbrevDecl(code
, self
._abbrev
_map
[code
])
38 def _parse_abbrev_table(self
):
39 """ Parse the abbrev table from the stream
42 self
.stream
.seek(self
.offset
)
44 decl_code
= struct_parse(
45 struct
=self
.structs
.Dwarf_uleb128(''),
49 declaration
= struct_parse(
50 struct
=self
.structs
.Dwarf_abbrev_declaration
,
52 map[decl_code
] = declaration
56 class AbbrevDecl(object):
57 """ Wraps a parsed abbreviation declaration, exposing its fields with
58 dict-like access, and adding some convenience methods.
60 The abbreviation declaration represents an "entry" that points to it.
62 def __init__(self
, code
, decl
):
66 def has_children(self
):
67 """ Does the entry have children?
69 return self
['children_flag'] == 'DW_CHILDREN_yes'
71 def iter_attr_specs(self
):
72 """ Iterate over the attribute specifications for the entry. Yield
75 for attr_spec
in self
['attr_spec']:
76 yield attr_spec
.name
, attr_spec
.form
78 def __getitem__(self
, entry
):
79 return self
.decl
[entry
]