rubyx/lib/elf/object_writer.rb

67 lines
1.9 KiB
Ruby
Raw Normal View History

require_relative "stream_writer"
require_relative 'object_file'
require_relative 'symbol_table_section'
require_relative 'text_section'
require_relative 'string_table_section'
2014-04-23 12:57:34 +02:00
module Elf
class ObjectWriter
2015-11-12 19:02:44 +01:00
def initialize(target = Elf::Constants::TARGET_ARM )
@object = Elf::ObjectFile.new(target)
sym_strtab = Elf::StringTableSection.new(".strtab")
@object.add_section sym_strtab
@symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab)
@object.add_section @symbol_table
@text = Elf::TextSection.new(".text")
@object.add_section @text
2015-11-12 19:02:44 +01:00
assembler = Register::Assembler.new(Register.machine)
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
Register.machine.space.types.values.each do |type|
type.instance_methods.each do |f|
f.instructions.each_label do |label|
2016-12-30 12:15:08 +01:00
add_symbol "#{clazz.name}::#{f.name}:#{label.name}" , label.position
end
end
end
2015-10-25 11:03:31 +01:00
2015-11-12 19:02:44 +01:00
Register.machine.objects.each do |id,slot|
next if slot.is_a?(Parfait::BinaryCode)
2015-06-27 19:08:07 +02:00
if( slot.respond_to? :sof_reference_name )
label = "#{slot.sof_reference_name}"
else
label = "#{slot.class.name}::#{slot.position.to_s(16)}"
end
2014-10-02 15:06:05 +02:00
label += "=#{slot}" if slot.is_a?(Symbol) or slot.is_a?(String)
2016-12-30 12:15:08 +01:00
add_symbol label , slot.position
2016-12-12 22:38:55 +01:00
if slot.is_a?(Parfait::TypedMethod)
2016-12-30 12:15:08 +01:00
add_symbol slot.name.to_s , slot.binary.position
end
end
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
def set_text(text)
@text.text = text
add_symbol "_start", 0
end
2016-12-30 12:15:08 +01: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)
@symbol_table.add_func_symbol name, offset, @text, linkage
end
def save(filename)
to = File.open(filename, 'wb')
2014-04-21 20:38:39 +02:00
@object.write to
to.close
end
end
end