From 1acd231a3301446395a1d536c94ab1d6f71d04e6 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 30 Apr 2018 13:28:55 +0300 Subject: [PATCH] debugging binaries, initial jump issues --- lib/arm/translator.rb | 7 ------- lib/elf/object_writer.rb | 2 ++ lib/risc/machine.rb | 2 +- lib/risc/positioned.rb | 2 +- lib/risc/text_writer.rb | 18 +++++++++++++++--- test/parfait/test_binary_code.rb | 7 +++++++ test/risc/test_machine.rb | 13 +++++++++++-- 7 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/arm/translator.rb b/lib/arm/translator.rb index 842cf3bf..7c688d43 100644 --- a/lib/arm/translator.rb +++ b/lib/arm/translator.rb @@ -24,13 +24,6 @@ module Arm raise "index error 0" if index == 0 index * 4 end - # Arm stores the return address in a register (not on the stack) - # The register is called link , or lr for short . - # Maybe because it provides the "link" back to the caller - # the vm defines a register for the location, so we store it there. - def translate_SaveReturn( code ) - ArmMachine.str( :lr , code.register , arm_index(code) ) - end def translate_Transfer( code ) # Risc machine convention is from => to diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index 58603446..0053563a 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -9,6 +9,8 @@ module Elf class ObjectWriter def initialize( machine ) @machine = machine + @machine.position_all + @machine.create_binary target = Elf::Constants::TARGET_ARM @object = Elf::ObjectFile.new(target) sym_strtab = Elf::StringTableSection.new(".strtab") diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index 8d923f94..71c3fea4 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -57,7 +57,7 @@ module Risc # add a constant (which get created during compilatio and need to be linked) def add_constant(const) - raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object) + raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object) @constants << const end # To create binaries, objects (and labels) need to have a position diff --git a/lib/risc/positioned.rb b/lib/risc/positioned.rb index 70773d5c..c6243cdb 100644 --- a/lib/risc/positioned.rb +++ b/lib/risc/positioned.rb @@ -11,7 +11,7 @@ module Positioned if pos == nil str = "position accessed but not set, " str += "0x#{object.object_id.to_s(16)}\n" - str += "for #{object.class} byte_length #{object.byte_length if object.respond_to?(:byte_length)} for #{object.inspect[0...100]}" + str += "for #{object.class} byte_length #{object.byte_length if object.respond_to?(:byte_length)} for #{object.inspect[0...130]}" raise str end pos diff --git a/lib/risc/text_writer.rb b/lib/risc/text_writer.rb index 0098c0af..0ab3d291 100644 --- a/lib/risc/text_writer.rb +++ b/lib/risc/text_writer.rb @@ -9,9 +9,9 @@ module Risc class TextWriter include Logging - log_level :info + log_level :debug - MARKER = 0xA51AF00D + MARKER = 0xBAD4C0DE def initialize( machine) @machine = machine @@ -25,7 +25,7 @@ module Risc # - all BinaryCode def write_as_string @stream = StringIO.new - write_any(@machine.binary_init) + write_init(@machine.binary_init) write_debug write_objects write_code @@ -146,6 +146,18 @@ module Risc log.debug "Data4 witten stream 0x#{@stream.length.to_s(16)}" end + # first jump, treated as a binary code, but this one needs + # the actual jump as the first + def write_init( code ) + code.each_word do |word| + @stream.write_unsigned_int_32( word || 0 ) + end + write_ref_for( code.next ) + write_ref_for( code.get_type ) + @stream.write_signed_int_32( MARKER ) + log.debug "Code16 witten stream 0x#{@stream.length.to_s(16)}" + end + def write_BinaryCode( code ) @stream.write_signed_int_32( MARKER ) write_ref_for( code.get_type ) diff --git a/test/parfait/test_binary_code.rb b/test/parfait/test_binary_code.rb index be6e4602..082ed0bc 100644 --- a/test/parfait/test_binary_code.rb +++ b/test/parfait/test_binary_code.rb @@ -85,6 +85,13 @@ module Parfait @code.each_word{ len += 1} assert_equal 13 , len end + def test_each_set + (1..13).each{|i| @code.set_word(i,i)} + all = [] + @code.each_word{ |w| all << w} + assert_equal 1 , all.first + assert_equal 13 , all.last + end def test_set_word assert_equal 1 , @code.set_word(1 , 1) end diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index f9aa0e40..98084ce4 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -28,9 +28,18 @@ module Risc assert Positioned.position(obj) end end - - def test_binary + end + class TestMachineInit < MiniTest::Test + def setup + @machine = Risc.machine.boot + @machine.position_all @machine.create_binary end + def test_has_binary + assert_equal Parfait::BinaryCode , @machine.binary_init.class + end + def test_has_jump + assert_equal "1" , @machine.binary_init.get_word(1).to_s(16) + end end end