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'
|
|
|
|
require 'elf/relocation_table_section'
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
module Asm
|
|
|
|
|
|
|
|
class ObjectWriter
|
|
|
|
def initialize(target)
|
2014-04-19 22:25:46 +02:00
|
|
|
@object = Elf::ObjectFile.new(target)
|
2014-04-14 17:09:56 +02:00
|
|
|
|
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-04-21 20:38:39 +02:00
|
|
|
# @reloc_table = Elf::RelocationTableSection.new(".text.rel", @symbol_table, @text)
|
|
|
|
# @object.add_section @reloc_table
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-04-21 20:38:39 +02:00
|
|
|
# def add_reloc_symbol(name)
|
|
|
|
# @symbol_table.add_func_symbol name, 0, nil, Elf::Constants::STB_GLOBAL
|
|
|
|
# end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-21 20:38:39 +02:00
|
|
|
# def add_reloc(offset, label, type)
|
|
|
|
# @reloc_table.add_reloc offset, label, type
|
|
|
|
# end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
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
|