2015-10-10 12:24:43 +03:00
|
|
|
require_relative "stream_writer"
|
|
|
|
require_relative 'object_file'
|
|
|
|
require_relative 'symbol_table_section'
|
|
|
|
require_relative 'text_section'
|
|
|
|
require_relative 'string_table_section'
|
2014-04-14 18:09:56 +03:00
|
|
|
|
2014-04-23 13:57:34 +03:00
|
|
|
module Elf
|
2014-04-14 18:09:56 +03:00
|
|
|
|
|
|
|
class ObjectWriter
|
2018-03-29 20:37:25 +03:00
|
|
|
def initialize( machine )
|
2016-12-31 18:46:17 +02:00
|
|
|
@machine = machine
|
|
|
|
target = Elf::Constants::TARGET_ARM
|
2014-04-19 23:25:46 +03:00
|
|
|
@object = Elf::ObjectFile.new(target)
|
|
|
|
sym_strtab = Elf::StringTableSection.new(".strtab")
|
2014-04-14 18:09:56 +03:00
|
|
|
@object.add_section sym_strtab
|
2014-04-19 23:25:46 +03:00
|
|
|
@symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab)
|
2014-04-14 18:09:56 +03:00
|
|
|
@object.add_section @symbol_table
|
|
|
|
|
2014-04-19 23:25:46 +03:00
|
|
|
@text = Elf::TextSection.new(".text")
|
2014-04-14 18:09:56 +03:00
|
|
|
@object.add_section @text
|
2015-05-12 15:36:44 +03:00
|
|
|
|
2018-03-29 20:37:25 +03:00
|
|
|
assembler = Risc::TextWriter.new(@machine)
|
2015-06-09 11:37:32 +02:00
|
|
|
set_text assembler.write_as_string
|
2014-08-30 14:17:00 +03:00
|
|
|
|
2016-12-30 13:15:08 +02:00
|
|
|
# for debug add labels for labels
|
2016-12-31 15:08:32 +02:00
|
|
|
Parfait.object_space.each_type do |type|
|
2018-05-14 12:38:44 +03:00
|
|
|
type.each_method do |meth|
|
|
|
|
meth.cpu_instructions.each do |label|
|
2018-03-29 20:37:25 +03:00
|
|
|
next unless label.is_a?(Risc::Label)
|
2018-05-14 12:38:44 +03:00
|
|
|
add_symbol "#{type.name}@#{meth.name}:Label=#{label.name}" , Risc::Position.get(label).at
|
2018-05-13 17:21:48 +03:00
|
|
|
end
|
2018-05-28 11:45:04 +03:00
|
|
|
meth.binary.each_block do |code|
|
2018-05-13 17:21:48 +03:00
|
|
|
label = "BinaryCode@#{Risc::Position.get(code).method.name}"
|
|
|
|
add_symbol label , Risc::Position.get(code).at
|
2016-12-30 13:15:08 +02:00
|
|
|
end
|
2014-05-31 17:02:55 +03:00
|
|
|
end
|
2014-05-31 14:35:33 +03:00
|
|
|
end
|
2015-10-25 12:03:31 +02:00
|
|
|
|
2018-03-29 20:37:25 +03:00
|
|
|
@machine.objects.each do |id,slot|
|
2015-11-14 15:04:04 +02:00
|
|
|
next if slot.is_a?(Parfait::BinaryCode)
|
2018-05-14 12:38:44 +03:00
|
|
|
if( slot.respond_to? :rxf_reference_name )
|
|
|
|
label = "#{slot.rxf_reference_name}"
|
2015-06-27 20:08:07 +03:00
|
|
|
else
|
2018-05-06 20:04:02 +03:00
|
|
|
label = "#{slot.class.name}::#{Risc::Position.get(slot)}"
|
2015-06-27 20:08:07 +03:00
|
|
|
end
|
2014-10-02 16:06:05 +03:00
|
|
|
label += "=#{slot}" if slot.is_a?(Symbol) or slot.is_a?(String)
|
2018-05-06 20:04:02 +03:00
|
|
|
add_symbol label , Risc::Position.get(slot).at
|
2014-08-30 20:55:22 +03:00
|
|
|
end
|
2014-04-14 18:09:56 +03:00
|
|
|
end
|
2016-12-15 18:08:55 +02:00
|
|
|
|
2014-05-20 10:28:34 +03:00
|
|
|
attr_reader :text
|
2016-12-15 18:08:55 +02:00
|
|
|
|
2014-04-14 18:09:56 +03:00
|
|
|
def set_text(text)
|
|
|
|
@text.text = text
|
|
|
|
add_symbol "_start", 0
|
|
|
|
end
|
2016-12-30 13:15:08 +02:00
|
|
|
|
2014-04-14 18:09:56 +03:00
|
|
|
def add_symbol(name, offset, linkage = Elf::Constants::STB_GLOBAL)
|
2015-11-14 22:53:01 +02:00
|
|
|
return add_symbol( name + "_" , offset ) if @symbol_table.has_name(name)
|
2014-04-14 18:09:56 +03:00
|
|
|
@symbol_table.add_func_symbol name, offset, @text, linkage
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(filename)
|
2015-05-12 15:36:44 +03:00
|
|
|
to = File.open(filename, 'wb')
|
2014-04-21 21:38:39 +03:00
|
|
|
@object.write to
|
|
|
|
to.close
|
2014-04-14 18:09:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-05-12 15:36:44 +03:00
|
|
|
end
|