Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
basic DWARF4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj committed Oct 28, 2016
1 parent 64b8ca5 commit 285369e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
20 changes: 13 additions & 7 deletions src/bintools/dwarf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,13 @@
0x66: 'elemental',
0x67: 'pure',
0x68: 'recursive',

# DWARF 4 values
0x69: 'signature',
0x6a: 'main_subprogram',
0x6b: 'data_bit_offset',
0x6c: 'const_expr',
0x6d: 'enum_class',
0x6e: 'linkage_name',

0x2000: 'lo_user',
0x2007: 'MIPS_linkage_name',
Expand Down Expand Up @@ -416,16 +421,17 @@ class DW_OP(object):
stack_value = 0x9f
lo_user = 0xe0

# https://fedorahosted.org/elfutils/wiki/DwarfExtensions
GNU_uninit = 0xf0
GNU_encoded_addr = 0xf1
GNU_implicit_pointer = 0xf2
GNU_entry_value = 0xf3
GNU_const_type = 0xf4
GNU_regval_type = 0xf5
GNU_deref_type = 0xf6
GNU_convert = 0xf7
GNU_entry_value = 0xf3 # http://www.dwarfstd.org/ShowIssue.php?issue=100909.1
GNU_const_type = 0xf4 # http://www.dwarfstd.org/doc/040408.1.html
GNU_regval_type = 0xf5 # http://www.dwarfstd.org/doc/040408.1.html
GNU_deref_type = 0xf6 # http://www.dwarfstd.org/doc/040408.1.html
GNU_convert = 0xf7 # http://www.dwarfstd.org/doc/040408.1.html
GNU_reinterpret = 0xf9
GNU_parameter_ref = 0xfa
GNU_parameter_ref = 0xfa # https://gcc.gnu.org/ml/gcc-patches/2011-06/msg00649.html
GNU_addr_index = 0xfb
GNU_const_index = 0xfc

Expand Down
15 changes: 11 additions & 4 deletions src/bintools/dwarf/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def __str__(self):
DW_OP.const4s: ('sdata4', None),
DW_OP.const8u: ('data8', None),
DW_OP.const8s: ('sdata8', None),
DW_OP.constu : ('read_udata', None),
DW_OP.consts : ('read_sdata', None),
DW_OP.constu : ('udata', None),
DW_OP.consts : ('sdata', None),

# Register Based Addressing
DW_OP.fbreg : ('sdata', None),
Expand Down Expand Up @@ -89,6 +89,12 @@ def __str__(self):

# GNU
DW_OP.GNU_implicit_pointer: ('addr', 'sdata'),
DW_OP.GNU_entry_value: ('exprloc', None),
DW_OP.GNU_const_type: ('udata', 'block1'),
DW_OP.GNU_regval_type: ('udata', 'udata'),
DW_OP.GNU_deref_type: ('data1', 'udata'),
DW_OP.GNU_convert: ('udata', None),
DW_OP.GNU_parameter_ref: ('data4', None),
}


Expand All @@ -106,8 +112,9 @@ def __init__(self, dwarf, length):

opcode = dwarf.u08()
if opcode not in DW_OP:
raise ParseError("Unknown DW_OP code: %d" % opcode)

previnst = ','.join(str(x) for x in self.instructions)
raise ParseError("Unknown DW_OP code: %d (after %s, offset 0x%x)" % (opcode, previnst, dwarf.io.tell()))

operand_1 = operand_2 = None
if opcode in DW_OP_OPERANDS:
type_1, type_2 = DW_OP_OPERANDS[opcode]
Expand Down
2 changes: 1 addition & 1 deletion src/bintools/dwarf/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self, dwarf, overall_offset):
length = dwarf.u32()
stop = dwarf.io.tell() + length

ver = dwarf.check_version(handled=[2, 3])
ver = dwarf.check_version(handled=[2, 3, 4])

abbrev_offset = dwarf.u32()
self.pointer_size = dwarf.u08()
Expand Down
9 changes: 8 additions & 1 deletion src/bintools/dwarf/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ def __init__(self, addr_size=4):
elif addr_size == 4:
self.read_addr = self.u32
self.max_addr = 0xFFFFFFFF

elif addr_size == 8:
self.read_addr = self.u64
self.max_addr = 0xFFFFFFFFFFFFFFFF

if self.bits == ELFCLASS.ELFCLASS32:
self.CIE_ID = 0xFFFFFFFF
self.read_sec_offset = self.u32
elif self.bits == ELFCLASS.ELFCLASS64:
self.CIE_ID = 0xFFFFFFFFFFFFFFFF
self.read_sec_offset = self.u64

# Read methods aliases
self.read_data1 = self.read_ref1 = self.u08
Expand Down Expand Up @@ -104,6 +106,9 @@ def read_strp(self):
def read_flag(self):
return (self.u08() != 0)

def read_flag_present(self): # the attribute is implicitly indicated as present
return True

def read_indirect(self):
return self.read_form(self.ULEB128())

Expand Down Expand Up @@ -135,6 +140,8 @@ def read_expr_block(self, form):
def read_expr(self):
return Expression(self, self.u16())

def read_exprloc(self):
return Expression(self, self.ULEB128())

class SectionLoader(object):
def __init__(self, dwarf, section_name, Entry):
Expand Down
2 changes: 1 addition & 1 deletion src/dwarfhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def expect_ref(attr):
return attr.value

def expect_flag(attr):
assert(attr.form in ['flag'])
assert(attr.form in ['flag','flag_present'])
return attr.value

def expect_addr(attr):
Expand Down

0 comments on commit 285369e

Please sign in to comment.