Class: ECUTools::Disassembler

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/disassembler.rb

Instance Method Summary collapse

Methods included from Helpers

#absolute_address, #address_descriptions, #base_address, #from_hex, #instruction_at, #load_rom_xml, #read_bytes, #rom_id, #rom_xml, #subroutine_descriptions

Constructor Details

#initialize(input = nil, options = {}) ⇒ Disassembler

Returns a new instance of Disassembler.



10
11
12
13
14
15
16
# File 'lib/disassembler.rb', line 10

def initialize(input = nil, options = {})
  @options = options
  @reference_addresses = {}
  if(!input.nil?)
    open input
  end
end

Instance Method Details

#analyzeObject



51
52
53
54
55
56
57
# File 'lib/disassembler.rb', line 51

def analyze
  $stderr.puts "Analyzing assembly..." if verbose
  annotate_tables
  annotate_subroutines
  annotate_code
  $stderr.puts "Analyzation complete." if verbose
end

#annotate_codeObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/disassembler.rb', line 116

def annotate_code
  $stderr.puts "Annotating code..." if verbose
  count = 0
  found_rom_addresses = {}
  found_ram_addresses = {}
  @assembly.each do |instruction|
    # annotate subroute prologue/epilogue
    if instruction.assembly =~ /push lr/
      c = 'begin subroutine'
      c << " #{subroutine_descriptions[instruction.address]}" if subroutine_descriptions.include? instruction.address
      instruction.comments << c
    end
    if instruction.assembly =~ /jmp lr/ 
      instruction.comments << 'return'
    end

    # annotate subroutine calls
    match = /bl 0x(\w+)/.match(instruction.assembly)
    if match
      address = match[1]
      if subroutine_descriptions.include? address
        instruction.comments << "Call #{subroutine_descriptions[address]}"
      end
    end
    
    # annotate table address references
    address = /0x([\d|[a-f]|[A-F]]+)/.match(instruction.assembly)
    if address and @reference_addresses.include? address[1]
      instruction.comments << "contains possible reference to '#{@reference_addresses[address[1]]}'"
      $stderr.puts "Annotated reference to: #{@reference_addresses[address[1]]} at #{instruction.address}" if verbose
      count = count + 1
      found_rom_addresses[@reference_addresses[address[1]]] = true 
    end
    
    # annotate absolute RAM addressing ld24 r4,0x800700
    match = /(\w+)\s+\w\w,0x(8\w\w\w\w\w)/.match(instruction.assembly)
    if match
      address = match[2]
      display = address_descriptions[address]
      
      if match[1] == "ld24"
        op = "Assign pointer to"
      else
        op = "Unknown op on"
      end
        
      if !display.nil? and verbose
        $stderr.puts "Found reference to absolute RAM address #{address} (#{display})"
      end
      instruction.comments << "#{op} RAM address 0x#{address}" + (display.nil? ? '' : " (#{display})")
      found_ram_addresses[display] = true if !display.nil?
      count = count + 1
    end

    # annotate relative RAM addressing
    match = /(\w+)\s+.+?@\((-?\d+),fp\)/.match(instruction.assembly)
    if match
      address = absolute_address match[2].to_i
      display = address_descriptions[address]

      case match[1]
      when "lduh"
        op = "Load unsigned halfword from"
      when "ldub"
        op = "Load unsigned byte from"
      when "ld"
        op = "Load from"
      when "ldb"
        op = "Load byte from"
      when "st" 
        op = "Store at"
      when "stb"
        op = "Store byte at"
      when "sth"
        op = "Store half word at"
      when "bclr"
        op = "Clear bit in"
      else
        op = "Unknown op on"
      end
      if !display.nil? and verbose
        $stderr.puts "Found reference to relative RAM address #{address} (#{display})"
      end
      instruction.comments << "#{op} RAM address 0x#{address}" + (display.nil? ? '' : " (#{display})")
      found_ram_addresses[display] = true if !display.nil?
      count = count + 1
    end
  
  end

  $stderr.puts "#{count} lines of code annotated." if verbose
  if verbose
    @reference_addresses.each_key do |key|
      $stderr.puts "Unable to find any reference to table #{@reference_addresses[key]}" if !found_rom_addresses.include? @reference_addresses[key]
      found_rom_addresses[@reference_addresses[key]] = true # stop multiple reports
    end
    address_descriptions.each_key do |key|
      $stderr.puts "Unable to find any reference to RAM address #{address_descriptions[key]} (#{key})" if !found_ram_addresses.include? address_descriptions[key]
    end
  end
end

#annotate_subroutinesObject



112
113
114
# File 'lib/disassembler.rb', line 112

def annotate_subroutines

end

#annotate_tablesObject



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
# File 'lib/disassembler.rb', line 59

def annotate_tables
  $stderr.puts "Annotating tables..." if verbose
  tables = rom_xml.xpath('/rom/table')
  count = 0
  tables.each do |table|
    elements = 1 # all tables start with one element
    element_size = rom_xml.xpath("/rom/scaling[@name='#{table.attr('scaling')}']").attr('storagetype').value().gsub(/[^\d]+/,'').to_i / 8
    address = from_hex table.attr('address')
    is_data = false

    table.xpath('table').each do |subtable|
      elements = elements * subtable.attr('elements').to_i
      is_data = true
    end

    possible_offsets = []

    case elements
    when 1,4
      possible_offsets << 0
    when 3..20
      possible_offsets << 4
      possible_offsets << 6
    when 44
      possible_offsets << 28  # wtf? maf scaling
    else
      possible_offsets << 7
      possible_offsets << 10
      possible_offsets << 16
    end

    possible_offsets.each do |offset|
      offset_hex = (address - offset).to_s(16)
      if verbose and @reference_addresses.include? offset_hex
        $stderr.puts "WARNING: Reference hunt collision at 0x#{offset_hex} (#{@reference_addresses[offset_hex]} vs #{table.attr('name')})! Check code carefully!" 
      end
      @reference_addresses[offset_hex] = table.attr('name')
    end

    storage_size = element_size * elements

    storage_size.times do |n|
      instruction = instruction_at(address + n)
      instruction.comments[0] = table.attr('name') + "(0x#{table.attr('address')} -> 0x#{(address + storage_size - 1).to_s(16)}, #{storage_size} bytes)"
      instruction.data = is_data
    end

    $stderr.puts "Annotated: #{table.attr('name')}" if verbose
    count = count + 1
  end
  $stderr.puts "#{count} tables annotated." if verbose
end

#open(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/disassembler.rb', line 22

def open(file)
  $stderr.puts "Disassembling binary..." if verbose
  h = '[\d|[a-f]|[A-F]]'
  @assembly = []
  io = IO.popen("gobjdump -b binary --architecture=m32r --disassemble-all --disassemble-zeroes -EB #{file}")
  while line = io.gets
    match = /\s+(#{h}+):\s+(#{h}{2}) (#{h}{2}) (#{h}{2}) (#{h}{2})\s+(.+)/.match(line)
    if match
      @assembly << Instruction.new(match[1], [ match[2], match[3], match[4], match[5] ], match[6])
    end
  end

  $stderr.puts "Disassembly complete." if verbose
end

#verboseObject



18
19
20
# File 'lib/disassembler.rb', line 18

def verbose
  @options[:verbose]
end

#write(file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/disassembler.rb', line 37

def write(file)
  f = File.new(file,"w")
  header = "; ecutools v#{ECUTools::VERSION}\n"
  header << "; Generated assembly from ROM ID #{rom_id}\n"
  header << "; Base Address: #{base_address}\n"
  header << "; Disassembly:\n"
  f.write(header)
  $stderr.puts "Writing assembly..." if verbose
  @assembly.each do |instruction|
    f.write("#{instruction}\n")
  end
  $stderr.puts "Done." if verbose
end