2014-04-19 22:25:46 +02:00
|
|
|
require 'elf/object_file'
|
|
|
|
require 'elf/symbol_table_section'
|
|
|
|
require 'elf/text_section'
|
|
|
|
require 'elf/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
|
2014-08-30 13:17:00 +02:00
|
|
|
def initialize(space , target = Elf::Constants::TARGET_ARM )
|
2014-04-19 22:25:46 +02:00
|
|
|
@object = Elf::ObjectFile.new(target)
|
2014-08-30 13:17:00 +02:00
|
|
|
@object_space = space
|
2014-04-19 22:25:46 +02:00
|
|
|
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
|
2014-05-19 16:32:41 +02:00
|
|
|
|
2014-08-30 13:17:00 +02:00
|
|
|
@object_space.run_passes
|
|
|
|
assembler = Register::Assembler.new(@object_space)
|
|
|
|
set_text assembler.assemble
|
|
|
|
|
|
|
|
# for debug add labels to the block positions
|
2014-05-22 13:56:31 +02:00
|
|
|
blocks = []
|
2014-08-30 13:17:00 +02:00
|
|
|
space.classes.values.each do |clazz|
|
|
|
|
clazz.instance_methods.each do |f|
|
2014-05-31 16:02:55 +02:00
|
|
|
f.blocks.each do |b|
|
2014-09-07 16:31:40 +02:00
|
|
|
add_symbol "#{clazz.name}::#{f.name}@#{b.name}" , b.position
|
2014-05-31 16:02:55 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-31 13:35:33 +02:00
|
|
|
end
|
2014-10-07 14:34:43 +02:00
|
|
|
space.main.blocks.each do |b|
|
|
|
|
add_symbol "main@#{b.name}" , b.position
|
|
|
|
end
|
|
|
|
add_symbol "#register@#{space.init.name}" , space.init.position
|
2014-10-02 15:06:05 +02:00
|
|
|
assembler.objects.values.each do |slot|
|
|
|
|
label = "#{slot.class.name}::#{slot.position.to_s(16)}"
|
|
|
|
label += "=#{slot}" if slot.is_a?(Symbol) or slot.is_a?(String)
|
|
|
|
label += "=#{slot.string}" if slot.is_a?(Virtual::StringConstant)
|
|
|
|
add_symbol label , slot.position
|
2014-08-30 19:55:22 +02:00
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
2014-05-20 09:28:34 +02:00
|
|
|
attr_reader :text
|
2014-04-14 17:09:56 +02:00
|
|
|
def set_text(text)
|
|
|
|
@text.text = text
|
|
|
|
add_symbol "_start", 0
|
|
|
|
end
|
|
|
|
def add_symbol(name, offset, linkage = Elf::Constants::STB_GLOBAL)
|
|
|
|
@symbol_table.add_func_symbol name, offset, @text, linkage
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(filename)
|
2014-04-21 20:38:39 +02:00
|
|
|
to = File.open(filename, 'wb')
|
|
|
|
@object.write to
|
|
|
|
to.close
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|