refactor object file somewhat

This commit is contained in:
Torsten Ruger 2016-12-16 16:18:14 +02:00
parent e10fc3eedd
commit dd6dede6ef

View File

@ -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