2015-10-10 11:24:43 +02: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 17:09:56 +02:00
|
|
|
|
2014-04-23 12:57:34 +02:00
|
|
|
module Elf
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
class ObjectWriter
|
2018-07-04 08:18:55 +02:00
|
|
|
def initialize( linker )
|
|
|
|
@linker = linker
|
2016-12-31 17:46:17 +01:00
|
|
|
target = Elf::Constants::TARGET_ARM
|
2014-04-19 22:25:46 +02:00
|
|
|
@object = Elf::ObjectFile.new(target)
|
|
|
|
sym_strtab = Elf::StringTableSection.new(".strtab")
|
2014-04-14 17:09:56 +02:00
|
|
|
@object.add_section sym_strtab
|
2014-04-19 22:25:46 +02:00
|
|
|
@symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab)
|
2014-04-14 17:09:56 +02:00
|
|
|
@object.add_section @symbol_table
|
|
|
|
|
2014-04-19 22:25:46 +02:00
|
|
|
@text = Elf::TextSection.new(".text")
|
2014-04-14 17:09:56 +02:00
|
|
|
@object.add_section @text
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2018-07-04 08:18:55 +02:00
|
|
|
assembler = Risc::TextWriter.new(@linker)
|
2015-06-09 11:37:32 +02:00
|
|
|
set_text assembler.write_as_string
|
2014-08-30 13:17:00 +02:00
|
|
|
|
2016-12-30 12:15:08 +01:00
|
|
|
# for debug add labels for labels
|
2018-07-04 08:18:55 +02:00
|
|
|
@linker.assemblers.each do |asm|
|
2018-07-30 09:23:42 +02:00
|
|
|
meth = asm.callable
|
2018-07-04 08:18:55 +02:00
|
|
|
asm.instructions.each do |label|
|
|
|
|
next unless label.is_a?(Risc::Label)
|
2018-07-06 19:01:17 +02:00
|
|
|
add_symbol "#{meth.self_type.name}@#{meth.name}:Label=#{label.name}" , Risc::Position.get(label).at
|
2018-07-04 08:18:55 +02:00
|
|
|
end
|
|
|
|
meth.binary.each_block do |code|
|
|
|
|
label = "BinaryCode@#{meth.name}"
|
|
|
|
add_symbol label , Risc::Position.get(code).at
|
2014-05-31 16:02:55 +02:00
|
|
|
end
|
2014-05-31 13:35:33 +02:00
|
|
|
end
|
2015-10-25 11:03:31 +01:00
|
|
|
|
2018-07-04 08:18:55 +02:00
|
|
|
@linker.object_positions.each do |slot , position|
|
2015-11-14 14:04:04 +01:00
|
|
|
next if slot.is_a?(Parfait::BinaryCode)
|
2018-06-19 09:51:03 +02:00
|
|
|
next if slot.class.name.include?("Arm")
|
2018-05-14 11:38:44 +02:00
|
|
|
if( slot.respond_to? :rxf_reference_name )
|
|
|
|
label = "#{slot.rxf_reference_name}"
|
2015-06-27 19:08:07 +02:00
|
|
|
else
|
2018-05-06 19:04:02 +02:00
|
|
|
label = "#{slot.class.name}::#{Risc::Position.get(slot)}"
|
2015-06-27 19:08:07 +02:00
|
|
|
end
|
2014-10-02 15:06:05 +02:00
|
|
|
label += "=#{slot}" if slot.is_a?(Symbol) or slot.is_a?(String)
|
2018-05-06 19:04:02 +02:00
|
|
|
add_symbol label , Risc::Position.get(slot).at
|
2014-08-30 19:55:22 +02:00
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
2016-12-15 17:08:55 +01:00
|
|
|
|
2014-05-20 09:28:34 +02:00
|
|
|
attr_reader :text
|
2016-12-15 17:08:55 +01:00
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
def set_text(text)
|
|
|
|
@text.text = text
|
|
|
|
add_symbol "_start", 0
|
|
|
|
end
|
2016-12-30 12:15:08 +01:00
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
def add_symbol(name, offset, linkage = Elf::Constants::STB_GLOBAL)
|
2015-11-14 21:53:01 +01:00
|
|
|
return add_symbol( name + "_" , offset ) if @symbol_table.has_name(name)
|
2014-04-14 17:09:56 +02:00
|
|
|
@symbol_table.add_func_symbol name, offset, @text, linkage
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(filename)
|
2015-05-12 14:36:44 +02:00
|
|
|
to = File.open(filename, 'wb')
|
2014-04-21 20:38:39 +02:00
|
|
|
@object.write to
|
|
|
|
to.close
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|