From dd6dede6ef62abaeb1c0dc11a090a058adc0d3b9 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 16 Dec 2016 16:18:14 +0200 Subject: [PATCH] refactor object file somewhat --- lib/elf/object_file.rb | 92 +++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/elf/object_file.rb b/lib/elf/object_file.rb index 1776ca79..b14b642b 100644 --- a/lib/elf/object_file.rb +++ b/lib/elf/object_file.rb @@ -18,55 +18,42 @@ module Elf end def write(io) - io << "\x7fELF" - io.write_uint8 @target[0] - io.write_uint8 @target[1] - io.write_uint8 EV_CURRENT - io.write_uint8 @target[2] - io << "\x00" * 8 # pad + write_preamble(io) - io.write_uint16 ET_REL - io.write_uint16 @target[3] - io.write_uint32 EV_CURRENT - io.write_uint32 0 # entry point - io.write_uint32 0 # no program header table sh_offset_pos = io.tell - io.write_uint32 0 # section header table offset - io.write_uint32 0 # no flags - io.write_uint16 52 # header length - io.write_uint16 0 # program header length - io.write_uint16 0 # program header count - io.write_uint16 40 # section header length - shstrtab = StringTableSection.new(".shstrtab") - @sections << shstrtab - @sections.each { |section| - shstrtab.add_string section.name - } + write_header(io) + + string_table = write_string_table(io) io.write_uint16 @sections.length # section header count io.write_uint16 @sections.length-1 # section name string table index - # write sections - - section_data = [] - @sections.each { |section| - offset = io.tell - section.write(io) - size = io.tell - offset - section_data << {:section => section, :offset => offset, - :size => size} - } - - # write section headers + section_data = write_sections(io) sh_offset = io.tell + write_section_data(section_data, string_table , io) + + io.seek sh_offset_pos + io.write_uint32 sh_offset + end + + def write_string_table(io) + string_table = StringTableSection.new(".shstrtab") + @sections << string_table + @sections.each { |section| + string_table.add_string section.name + } + string_table + end + + def write_section_data(section_data, string_table,io) section_data.each { |data| section, offset, size = data[:section], data[:offset], data[:size] # write header first - io.write_uint32 shstrtab.index_for(section.name) + io.write_uint32 string_table.index_for(section.name) io.write_uint32 section.type io.write_uint32 section.flags io.write_uint32 section.addr @@ -84,9 +71,40 @@ module Elf io.write_uint32 section.alignment io.write_uint32 section.ent_size } + end - io.seek sh_offset_pos - io.write_uint32 sh_offset + def write_sections(io) + section_data = [] + @sections.each { |section| + offset = io.tell + section.write(io) + size = io.tell - offset + section_data << {:section => section, :offset => offset, :size => size} + } + section_data + end + + def write_header(io) + io.write_uint32 0 # section header table offset + io.write_uint32 0 # no flags + io.write_uint16 52 # header length + io.write_uint16 0 # program header length + io.write_uint16 0 # program header count + io.write_uint16 40 # section header length + end + + def write_preamble(io) + io << "\x7fELF" + io.write_uint8 @target[0] + io.write_uint8 @target[1] + io.write_uint8 EV_CURRENT + io.write_uint8 @target[2] + io << "\x00" * 8 # pad + io.write_uint16 ET_REL + io.write_uint16 @target[3] + io.write_uint32 EV_CURRENT + io.write_uint32 0 # entry point + io.write_uint32 0 # no program header table end end end