Class: Unarm::Symbols

Inherits:
Object
  • Object
show all
Defined in:
lib/unarm/unarm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Symbols

Returns a new instance of Symbols.



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/unarm/unarm.rb', line 523

def initialize(args = {})
  if args.has_key? :file_path
    @map, @locs = Symbols.load(args[:file_path])
  elsif args.has_key? :syms
    @map, @locs = args[:syms].map, args[:syms].locs
  else
    raise ArgumentError, 'Symbols must be initialized through a file or a hash'
  end

  if args.has_key? :locs
    all_syms = @map
    @map = {}
    args[:locs].each do |loc|
      @locs[loc].each { |sym| @map[sym] = all_syms[sym] }
    end
  end

  @count = @map.length

  @demangled_map = {}
  @ambig_demangled = []

  @map.each do |sym, addr|
    demangled = Unarm.shitty_demangle(sym)
    if @demangled_map[demangled].nil?
      @demangled_map[demangled] = sym
    else
      @ambig_demangled << [demangled, addr]
    end
  end
end

Instance Attribute Details

#ambig_demangledObject (readonly)

Returns the value of attribute ambig_demangled.



483
484
485
# File 'lib/unarm/unarm.rb', line 483

def ambig_demangled
  @ambig_demangled
end

#countObject (readonly)

Returns the value of attribute count.



483
484
485
# File 'lib/unarm/unarm.rb', line 483

def count
  @count
end

#demangled_mapObject (readonly)

Returns the value of attribute demangled_map.



483
484
485
# File 'lib/unarm/unarm.rb', line 483

def demangled_map
  @demangled_map
end

#locsObject (readonly)

Returns the value of attribute locs.



483
484
485
# File 'lib/unarm/unarm.rb', line 483

def locs
  @locs
end

#mapObject (readonly)

Returns the value of attribute map.



483
484
485
# File 'lib/unarm/unarm.rb', line 483

def map
  @map
end

Class Method Details

.load(file_path) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/unarm/unarm.rb', line 485

def self.load(file_path)
  syms = {} # maps symbol names to their addresses
  locs = {} # maps symbol names to their code locations (e.g. arm9, ov0, ov10)
  dest = nil # current symbol location
  File.open(file_path) do |f|
    f.each_line do |line|
      parts = line.split

      next if parts.length < 3
      
      is_comment = line.strip.start_with?('/')
      if is_comment
        new_dest = parts[1].split('_')
        dest = new_dest[new_dest.length == 1 ? 0 : 1] if new_dest[0].include?('arm')
        next
      end

      next if is_comment || (line.length < 4)

      parts.delete_at(1) # removes '='
      parts = parts[0..1] # keep symbol and name

      parts[1] = parts[1].chomp(';') if parts[1].end_with?(';')

      addr = parts[1].hex
      next if addr <= 0
      parts[1] = addr # - (addr & 1)
      syms[parts[0]] = parts[1]
      if dest
        locs[dest] = Array.new unless locs[dest]
        locs[dest] << parts[0]
      end

    end
  end
  return syms, locs
end