Class: ElfUtils::Section::DebugInfo::Die::Base

Inherits:
Object
  • Object
show all
Includes:
CTypes::Helpers
Defined in:
lib/elf_utils/section/debug_info/die/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cu:, offset:, attrs:, has_children:) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 5

def initialize(cu:, offset:, attrs:, has_children:)
  @cu = cu
  @offset = offset
  @attrs = attrs
  @children = [] if has_children
end

Instance Attribute Details

#cuObject (readonly)

Returns the value of attribute cu.



11
12
13
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 11

def cu
  @cu
end

#offsetObject (readonly)

Returns the value of attribute offset.



11
12
13
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 11

def offset
  @offset
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 11

def tag
  @tag
end

Instance Method Details

#<<(child) ⇒ Object

Raises:



13
14
15
16
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 13

def <<(child)
  raise Error, "This DIE cannot have children" unless @children
  @children << child
end

#childrenObject



22
23
24
25
26
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 22

def children
  # to make it easier to walk a tree of DIEs, we're going to return an
  # empty array for DIEs that don't have children
  @children || []
end

#children?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 18

def children?
  !!@children
end

#ctypeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 45

def ctype
  return unless type?
  return @ctype if @ctype

  name, type = case tag
  when :base_type
    unsigned = @attrs[:encoding].to_s.include?("unsigned")
    type = case @attrs[:byte_size]
    when 1
      unsigned ? uint8 : int8
    when 2
      unsigned ? uint16 : int16
    when 4
      unsigned ? uint32 : int32
    when 8
      unsigned ? uint64 : int64
    else
      raise "unsupported base_type size: %p" % die
    end
    [@attrs[:name].deref, type.with_endian(@cu.file.endian)]
  when :volatile_type
    name, type = @attrs[:type].deref.ctype
    ["volatile #{name}", type]
  when :typedef
    _, type = @attrs[:type].deref.ctype
    [@attrs[:name].deref, type]
  when :array_type
    name, inner_type = @attrs[:type].deref.ctype

    subrange = children.find { |c| c.tag == :subrange_type }
    size = if subrange.has_attr?(:count)
      subrange.count
    elsif subrange.has_attr?(:upper_bound)
      subrange.upper_bound
    end

    # XXX need flexible array member test

    # if we have a signed char array, we'll use that
    if inner_type.is_a?(CTypes::Int) &&
        inner_type.size == 1 &&
        inner_type.signed?
      ["#{name}[#{size}]", string(size)]
    else
      ["#{name}[#{size}]", array(inner_type, size)]
    end
  when :pointer_type
    type = @attrs[:type].deref
    name, = type.ctype
    [(type.tag == :pointer_type) ? "#{name}*" : "#{name} *", @cu.addr_type]
  when :structure_type
    fields, = @children.inject([{}, 0]) do |(o, offset), child|
      next [o, offset] unless child.tag == :member

      pad = child.data_member_location - offset
      o[:"__pad_#{offset}"] = string(pad, trim: false) if pad != 0

      _, type = child.type.deref.ctype
      o[child.name.deref.to_sym] = type
      [o, child.data_member_location + type.size]
    end

    name = @attrs[:name]&.deref || ("anon_%x" % offset)

    ["struct #{name}", struct(**fields)]
  when :union_type
    layout = @children.each_with_object({}) do |child, o|
      next o unless child.tag == :member

      _, type = child.type.deref.ctype
      o[child.name.deref.to_sym] = type
    end

    name = @attrs[:name]&.deref || ("anon_%x" % offset)

    ["union #{name}", union(**layout)]
  when :subrange_type, :subroutine_type
    return
  else
    raise Error, "unsupported DIE: %p" % [die]
  end

  @ctype = [name, type]
end

#has_attr?(key) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 37

def has_attr?(key)
  @attrs.has_key?(key)
end

#inspectObject



28
29
30
31
32
33
34
35
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 28

def inspect
  buf = "<%p\n    0x%08x: DW_TAG_%s (children=%p)\n" %
    [self.class, @offset, tag, @children ? @children.size : false]
  @attrs.each do |name, value|
    buf << "            DW_AT_%-15s %p\n" % [name, value]
  end
  buf.chomp! << ">"
end

#type?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/elf_utils/section/debug_info/die/base.rb', line 41

def type?
  tag == :typedef || tag.to_s.end_with?("_type")
end