unlinking the objects collection from the machine
passing it around instead
This commit is contained in:
parent
b094bcc64f
commit
113b349af5
@ -7,7 +7,10 @@ require_relative 'string_table_section'
|
|||||||
module Elf
|
module Elf
|
||||||
|
|
||||||
class ObjectWriter
|
class ObjectWriter
|
||||||
def initialize(target = Elf::Constants::TARGET_ARM )
|
def initialize( machine , objects )
|
||||||
|
@machine = machine
|
||||||
|
@objects = objects
|
||||||
|
target = Elf::Constants::TARGET_ARM
|
||||||
@object = Elf::ObjectFile.new(target)
|
@object = Elf::ObjectFile.new(target)
|
||||||
sym_strtab = Elf::StringTableSection.new(".strtab")
|
sym_strtab = Elf::StringTableSection.new(".strtab")
|
||||||
@object.add_section sym_strtab
|
@object.add_section sym_strtab
|
||||||
@ -17,7 +20,7 @@ module Elf
|
|||||||
@text = Elf::TextSection.new(".text")
|
@text = Elf::TextSection.new(".text")
|
||||||
@object.add_section @text
|
@object.add_section @text
|
||||||
|
|
||||||
assembler = Register::Assembler.new(Register.machine)
|
assembler = Register::Assembler.new(@machine , @objects)
|
||||||
set_text assembler.write_as_string
|
set_text assembler.write_as_string
|
||||||
|
|
||||||
# for debug add labels for labels
|
# for debug add labels for labels
|
||||||
@ -29,7 +32,7 @@ module Elf
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Register.machine.objects.each do |id,slot|
|
@objects.each do |id,slot|
|
||||||
next if slot.is_a?(Parfait::BinaryCode)
|
next if slot.is_a?(Parfait::BinaryCode)
|
||||||
if( slot.respond_to? :sof_reference_name )
|
if( slot.respond_to? :sof_reference_name )
|
||||||
label = "#{slot.sof_reference_name}"
|
label = "#{slot.sof_reference_name}"
|
||||||
|
@ -15,8 +15,9 @@ module Register
|
|||||||
|
|
||||||
MARKER = 0xA51AF00D
|
MARKER = 0xA51AF00D
|
||||||
|
|
||||||
def initialize machine
|
def initialize( machine , objects)
|
||||||
@machine = machine
|
@machine = machine
|
||||||
|
@objects = objects
|
||||||
@load_at = 0x8054 # this is linux/arm
|
@load_at = 0x8054 # this is linux/arm
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,13 +25,14 @@ module Register
|
|||||||
at = 0
|
at = 0
|
||||||
#need the initial jump at 0 and then functions
|
#need the initial jump at 0 and then functions
|
||||||
@machine.init.set_position(0)
|
@machine.init.set_position(0)
|
||||||
at = assemble_objects
|
at = @machine.init.byte_length
|
||||||
|
at = assemble_objects( at )
|
||||||
# and then everything code
|
# and then everything code
|
||||||
asseble_code_from( at )
|
asseble_code_from( at )
|
||||||
end
|
end
|
||||||
|
|
||||||
def asseble_code_from( at )
|
def asseble_code_from( at )
|
||||||
@machine.objects.each do |id , objekt|
|
@objects.each do |id , objekt|
|
||||||
next unless objekt.is_a? Parfait::TypedMethod
|
next unless objekt.is_a? Parfait::TypedMethod
|
||||||
log.debug "CODE1 #{objekt.name}"
|
log.debug "CODE1 #{objekt.name}"
|
||||||
binary = objekt.binary
|
binary = objekt.binary
|
||||||
@ -44,11 +46,10 @@ module Register
|
|||||||
at
|
at
|
||||||
end
|
end
|
||||||
|
|
||||||
def assemble_objects
|
def assemble_objects( at)
|
||||||
at = @machine.init.byte_length
|
|
||||||
at += 8 # thats the padding
|
at += 8 # thats the padding
|
||||||
# want to have the objects first in the executable
|
# want to have the objects first in the executable
|
||||||
@machine.objects.each do | id , objekt|
|
@objects.each do | id , objekt|
|
||||||
next if objekt.is_a? Register::Label # will get assembled as method.instructions
|
next if objekt.is_a? Register::Label # will get assembled as method.instructions
|
||||||
next if objekt.is_a? Parfait::BinaryCode
|
next if objekt.is_a? Parfait::BinaryCode
|
||||||
objekt.set_position at
|
objekt.set_position at
|
||||||
@ -81,7 +82,7 @@ module Register
|
|||||||
|
|
||||||
# debugging loop accesses all positions to force an error if it's not set
|
# debugging loop accesses all positions to force an error if it's not set
|
||||||
def try_write_debug
|
def try_write_debug
|
||||||
all = @machine.objects.values.sort{|a,b| a.position <=> b.position}
|
all = @objects.values.sort{|a,b| a.position <=> b.position}
|
||||||
all.each do |objekt|
|
all.each do |objekt|
|
||||||
next if objekt.is_a?(Register::Label)
|
next if objekt.is_a?(Register::Label)
|
||||||
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
|
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
|
||||||
@ -91,19 +92,19 @@ module Register
|
|||||||
|
|
||||||
def try_write_create_binary
|
def try_write_create_binary
|
||||||
# first we need to create the binary code for the methods
|
# first we need to create the binary code for the methods
|
||||||
@machine.objects.each do |id , objekt|
|
@objects.each do |id , objekt|
|
||||||
next unless objekt.is_a? Parfait::TypedMethod
|
next unless objekt.is_a? Parfait::TypedMethod
|
||||||
assemble_binary_method(objekt)
|
assemble_binary_method(objekt)
|
||||||
end
|
end
|
||||||
@stream = StringIO.new
|
@stream = StringIO.new
|
||||||
@machine.init.assemble( @stream )
|
@machine.init.assemble( @stream )
|
||||||
8.times do
|
8.times do
|
||||||
@stream.write_uint8(0)
|
@stream.write_unsigned_int_8(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def try_write_objects
|
def try_write_objects
|
||||||
# then the objects , not code yet
|
# then the objects , not code yet
|
||||||
@machine.objects.each do | id, objekt|
|
@objects.each do | id, objekt|
|
||||||
next if objekt.is_a? Parfait::BinaryCode
|
next if objekt.is_a? Parfait::BinaryCode
|
||||||
next if objekt.is_a? Register::Label # ignore
|
next if objekt.is_a? Register::Label # ignore
|
||||||
write_any( objekt )
|
write_any( objekt )
|
||||||
@ -112,7 +113,7 @@ module Register
|
|||||||
|
|
||||||
def try_write_method
|
def try_write_method
|
||||||
# then write the methods to file
|
# then write the methods to file
|
||||||
@machine.objects.each do |id, objekt|
|
@objects.each do |id, objekt|
|
||||||
next unless objekt.is_a? Parfait::BinaryCode
|
next unless objekt.is_a? Parfait::BinaryCode
|
||||||
write_any( objekt )
|
write_any( objekt )
|
||||||
end
|
end
|
||||||
@ -187,7 +188,7 @@ module Register
|
|||||||
|
|
||||||
def write_object_check(object)
|
def write_object_check(object)
|
||||||
log.debug "Write object #{object.class} #{object.inspect}"
|
log.debug "Write object #{object.class} #{object.inspect}"
|
||||||
unless @machine.objects.has_key? object.object_id
|
unless @objects.has_key? object.object_id
|
||||||
raise "Object(#{object.object_id}) not linked #{object.inspect}"
|
raise "Object(#{object.object_id}) not linked #{object.inspect}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -204,7 +205,7 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write_object_variables(object)
|
def write_object_variables(object)
|
||||||
@stream.write_sint32( MARKER )
|
@stream.write_signed_int_32( MARKER )
|
||||||
written = 0 # compensate for the "secrect" marker
|
written = 0 # compensate for the "secrect" marker
|
||||||
object.get_instance_variables.each do |var|
|
object.get_instance_variables.each do |var|
|
||||||
inst = object.get_instance_variable(var)
|
inst = object.get_instance_variable(var)
|
||||||
@ -230,9 +231,9 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write_checked_string(string, str)
|
def write_checked_string(string, str)
|
||||||
@stream.write_sint32( MARKER )
|
@stream.write_signed_int_32( MARKER )
|
||||||
write_ref_for( string.get_type ) #ref
|
write_ref_for( string.get_type ) #ref
|
||||||
@stream.write_sint32( str.length ) #int
|
@stream.write_signed_int_32( str.length ) #int
|
||||||
@stream.write str
|
@stream.write str
|
||||||
pad_after(str.length + 8 ) # type , length *4 == 12
|
pad_after(str.length + 8 ) # type , length *4 == 12
|
||||||
log.debug "String (#{string.length}) stream #{@stream.length}"
|
log.debug "String (#{string.length}) stream #{@stream.length}"
|
||||||
@ -249,11 +250,11 @@ module Register
|
|||||||
def write_ref_for object
|
def write_ref_for object
|
||||||
case object
|
case object
|
||||||
when nil
|
when nil
|
||||||
@stream.write_sint32(0)
|
@stream.write_signed_int_32(0)
|
||||||
when Fixnum
|
when Fixnum
|
||||||
@stream.write_sint32(object)
|
@stream.write_signed_int_32(object)
|
||||||
else
|
else
|
||||||
@stream.write_sint32(object.position + @load_at)
|
@stream.write_signed_int_32(object.position + @load_at)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -262,7 +263,7 @@ module Register
|
|||||||
before = stream_position
|
before = stream_position
|
||||||
pad = padding_for(length) - 4 # four is for the MARKER we write
|
pad = padding_for(length) - 4 # four is for the MARKER we write
|
||||||
pad.times do
|
pad.times do
|
||||||
@stream.write_uint8(0)
|
@stream.write_unsigned_int_8(0)
|
||||||
end
|
end
|
||||||
after = stream_position
|
after = stream_position
|
||||||
log.debug "padded #{length} with #{pad} stream #{before}/#{after}"
|
log.debug "padded #{length} with #{pad} stream #{before}/#{after}"
|
||||||
|
@ -3,9 +3,10 @@ module Register
|
|||||||
# collect anything that is in the space but and reachable from init
|
# collect anything that is in the space but and reachable from init
|
||||||
module Collector
|
module Collector
|
||||||
def collect_space
|
def collect_space
|
||||||
@objects.clear
|
@objects = {}
|
||||||
keep Parfait.object_space , 0
|
keep Parfait.object_space , 0
|
||||||
constants.each {|obj| keep(obj,0)}
|
constants.each {|obj| keep(obj,0)}
|
||||||
|
@objects
|
||||||
end
|
end
|
||||||
|
|
||||||
def keep( object , depth )
|
def keep( object , depth )
|
||||||
|
@ -17,11 +17,10 @@ module Register
|
|||||||
log_level :info
|
log_level :info
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@objects = {}
|
|
||||||
@booted = false
|
@booted = false
|
||||||
@constants = []
|
@constants = []
|
||||||
end
|
end
|
||||||
attr_reader :constants , :init , :objects , :booted
|
attr_reader :constants , :init , :booted
|
||||||
|
|
||||||
# idea being that later method missing could catch translate_xxx and translate to target xxx
|
# idea being that later method missing could catch translate_xxx and translate to target xxx
|
||||||
# now we just instantiate ArmTranslater and pass instructions
|
# now we just instantiate ArmTranslater and pass instructions
|
||||||
|
@ -6,9 +6,9 @@ class HelloTest < MiniTest::Test
|
|||||||
def check
|
def check
|
||||||
machine = Register.machine.boot
|
machine = Register.machine.boot
|
||||||
Typed.compile( @input )
|
Typed.compile( @input )
|
||||||
machine.collect_space
|
objects = machine.collect_space
|
||||||
machine.translate_arm
|
machine.translate_arm
|
||||||
writer = Elf::ObjectWriter.new
|
writer = Elf::ObjectWriter.new(machine , objects )
|
||||||
writer.save "test/hello.o"
|
writer.save "test/hello.o"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ class TestZeroCode < MiniTest::Test
|
|||||||
type.remove_method(method) unless keeper(method)
|
type.remove_method(method) unless keeper(method)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@machine.collect_space
|
@objects = @machine.collect_space
|
||||||
end
|
end
|
||||||
def keeper name
|
def keeper name
|
||||||
name == :main or name == :__init__
|
name == :main or name == :__init__
|
||||||
@ -19,13 +19,13 @@ class TestZeroCode < MiniTest::Test
|
|||||||
def test_empty_translate
|
def test_empty_translate
|
||||||
assert_equal 2 , @space.collect_methods.length
|
assert_equal 2 , @space.collect_methods.length
|
||||||
@machine.translate_arm
|
@machine.translate_arm
|
||||||
writer = Elf::ObjectWriter.new
|
writer = Elf::ObjectWriter.new(@machine , @objects )
|
||||||
writer.save "test/zero.o"
|
writer.save "test/zero.o"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_methods_match_objects
|
def test_methods_match_objects
|
||||||
assert_equal 2 , @space.collect_methods.length
|
assert_equal 2 , @space.collect_methods.length
|
||||||
@machine.objects.each do |id , objekt|
|
@objects.each do |id , objekt|
|
||||||
next unless objekt.is_a? Parfait::TypedMethod
|
next unless objekt.is_a? Parfait::TypedMethod
|
||||||
assert keeper(objekt.name) , "CODE1 #{objekt.name}"
|
assert keeper(objekt.name) , "CODE1 #{objekt.name}"
|
||||||
end
|
end
|
||||||
|
@ -8,8 +8,8 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_collect_all_types
|
def test_collect_all_types
|
||||||
@machine.collect_space
|
objects = @machine.collect_space
|
||||||
@machine.objects.each do |id, objekt|
|
objects.each do |id, objekt|
|
||||||
next unless objekt.is_a?( Parfait::Type )
|
next unless objekt.is_a?( Parfait::Type )
|
||||||
assert Parfait.object_space.get_type_for( objekt.hash ) , objekt.hash
|
assert Parfait.object_space.get_type_for( objekt.hash ) , objekt.hash
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user