From 091a93f368313a6674de795c140c2a88ec6b1489 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 19 Apr 2014 23:25:46 +0300 Subject: [PATCH] fix names and requires to get some elf out again (still crashes though) --- lib/asm/README.markdown | 2 +- lib/asm/assembler.rb | 2 ++ lib/asm/object_writer.rb | 16 ++++++++++------ lib/crystal.rb | 5 ----- lib/elf/constants.rb | 12 ++++++------ lib/elf/null_section.rb | 2 ++ lib/elf/object_file.rb | 5 ++++- lib/stream_reader.rb | 6 ++++++ unused/code_generator.rb | 6 +++--- unused/elf_object.rb | 10 +++++----- 10 files changed, 39 insertions(+), 27 deletions(-) diff --git a/lib/asm/README.markdown b/lib/asm/README.markdown index c64135f9..a42b0252 100644 --- a/lib/asm/README.markdown +++ b/lib/asm/README.markdown @@ -3,7 +3,7 @@ Assembler in Ruby Supporting arm, but aimed quite specifically at raspberry pi, arm v7, floating point included -Outputs ELF object files, with relocation support. +Outputs Elf object files, with relocation support. Constant table support exists but isn't very good. Some addressing modes are not supported or only partially supported. diff --git a/lib/asm/assembler.rb b/lib/asm/assembler.rb index bf40e2c3..2c949428 100644 --- a/lib/asm/assembler.rb +++ b/lib/asm/assembler.rb @@ -1,3 +1,5 @@ +require_relative "relocation" + module Asm ERRSTR_NUMERIC_TOO_LARGE = 'cannot fit numeric literal argument in operand' ERRSTR_INVALID_ARG = 'invalid operand argument' diff --git a/lib/asm/object_writer.rb b/lib/asm/object_writer.rb index 9743c3ca..7cfd4df9 100644 --- a/lib/asm/object_writer.rb +++ b/lib/asm/object_writer.rb @@ -1,20 +1,24 @@ -require_relative 'elfobject' +require 'elf/object_file' +require 'elf/symbol_table_section' +require 'elf/text_section' +require 'elf/string_table_section' +require 'elf/relocation_table_section' module Asm class ObjectWriter def initialize(target) - @object = ELF::ObjectFile.new(target) + @object = Elf::ObjectFile.new(target) - sym_strtab = ELF::StringTableSection.new(".strtab") + sym_strtab = Elf::StringTableSection.new(".strtab") @object.add_section sym_strtab - @symbol_table = ELF::SymbolTableSection.new(".symtab", sym_strtab) + @symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab) @object.add_section @symbol_table - @text = ELF::TextSection.new(".text") + @text = Elf::TextSection.new(".text") @object.add_section @text - @reloc_table = ELF::RelocationTableSection.new(".text.rel", @symbol_table, @text) + @reloc_table = Elf::RelocationTableSection.new(".text.rel", @symbol_table, @text) @object.add_section @reloc_table end diff --git a/lib/crystal.rb b/lib/crystal.rb index f6e1850e..e69de29b 100644 --- a/lib/crystal.rb +++ b/lib/crystal.rb @@ -1,5 +0,0 @@ -class Numeric - def fits_u8? - self >= 0 and self <= 255 - end -end diff --git a/lib/elf/constants.rb b/lib/elf/constants.rb index 93e2ea50..ce4276b8 100644 --- a/lib/elf/constants.rb +++ b/lib/elf/constants.rb @@ -1,4 +1,4 @@ -module ELF +module Elf module Constants ET_NONE = 0 ET_REL = 1 @@ -23,9 +23,9 @@ module ELF ELFCLASS32 = 1 ELFCLASS64 = 2 - ELFDATANONE = 0 - ELFDATA2LSB = 1 - ELFDATA2MSB = 2 + Elf::DATANONE = 0 + Elf::DATA2LSB = 1 + Elf::DATA2MSB = 2 SHT_NULL = 0 SHT_PROGBITS = 1 @@ -53,7 +53,7 @@ module ELF ARM_INFLOOP = "\x08\xf0\x4f\xe2" - TARGET_ARM = [ELFCLASS32, ELFDATA2LSB, ABI_ARM, EM_ARM] - TARGET_X86 = [ELFCLASS32, ELFDATA2LSB, ABI_SYSTEMV, EM_386] + TARGET_ARM = [ELFCLASS32, Elf::DATA2LSB, ABI_ARM, EM_ARM] + TARGET_X86 = [ELFCLASS32, Elf::DATA2LSB, ABI_SYSTEMV, EM_386] end end \ No newline at end of file diff --git a/lib/elf/null_section.rb b/lib/elf/null_section.rb index 27f341fa..2d61d1bc 100644 --- a/lib/elf/null_section.rb +++ b/lib/elf/null_section.rb @@ -1,3 +1,5 @@ +require_relative "section" + module Elf class NullSection < Section def initialize diff --git a/lib/elf/object_file.rb b/lib/elf/object_file.rb index f221ad7f..1776ca79 100644 --- a/lib/elf/object_file.rb +++ b/lib/elf/object_file.rb @@ -1,6 +1,9 @@ +require_relative "constants" +require_relative "null_section" + module Elf class ObjectFile - include ELF + include Constants def initialize(target) @target = target diff --git a/lib/stream_reader.rb b/lib/stream_reader.rb index d8173e61..081ff159 100644 --- a/lib/stream_reader.rb +++ b/lib/stream_reader.rb @@ -1,3 +1,9 @@ +class Numeric + def fits_u8? + self >= 0 and self <= 255 + end +end + module StreamReader def read_binary(size, count, type) d = __sr_read(size*count) diff --git a/unused/code_generator.rb b/unused/code_generator.rb index 602b8a91..49f4b871 100644 --- a/unused/code_generator.rb +++ b/unused/code_generator.rb @@ -1,6 +1,7 @@ +require "asm/arm/code_generator" if (__FILE__ == $0) - gen = Asm::ArmCodeGenerator.new + gen = Asm::Arm::CodeGenerator.new gen.instance_eval { mov r0, 5 @@ -11,8 +12,7 @@ if (__FILE__ == $0) bx lr } - require 'objectwriter' - require 'tempfile' + require 'asm/object_writer' writer = Asm::ObjectWriter.new(Elf::Constants::TARGET_ARM) writer.set_text gen.assemble diff --git a/unused/elf_object.rb b/unused/elf_object.rb index 404342f8..13b44219 100644 --- a/unused/elf_object.rb +++ b/unused/elf_object.rb @@ -1,15 +1,15 @@ if (__FILE__ == $0) - obj = ELF::ObjectFile.new ELF::TARGET_ARM + obj = Elf::ObjectFile.new Elf::TARGET_ARM - sym_strtab = ELF::StringTableSection.new(".strtab") + sym_strtab = Elf::StringTableSection.new(".strtab") obj.add_section sym_strtab - symtab = ELF::SymbolTableSection.new(".symtab", sym_strtab) + symtab = Elf::SymbolTableSection.new(".symtab", sym_strtab) obj.add_section symtab - text_section = ELF::TextSection.new(".text") + text_section = Elf::TextSection.new(".text") obj.add_section text_section - symtab.add_func_symbol "_start", 0, text_section, ELF::STB_GLOBAL + symtab.add_func_symbol "_start", 0, text_section, Elf::STB_GLOBAL fp = File.open("test.o", "wb") obj.write fp