Class: Voodoo::GasELFGenerator

Inherits:
Object
  • Object
show all
Includes:
CommandPostProcessor
Defined in:
lib/voodoo/generators/gas_elf_generator.rb

Overview

Class that generates ELF object files by invoking the GNU assembler on the output of a code generator that generates GNU assembly.

Instance Method Summary collapse

Methods included from CommandPostProcessor

shell_encode, tempfile, write_file_to_io

Constructor Details

#initialize(asmgenerator, extra_args = "") ⇒ GasELFGenerator

Returns a new instance of GasELFGenerator.



8
9
10
11
12
# File 'lib/voodoo/generators/gas_elf_generator.rb', line 8

def initialize asmgenerator, extra_args = ""
  @asmgenerator = asmgenerator
  @extra_args = extra_args
  @output_file_suffix = '.o'
end

Instance Method Details

#output_file_name(input_name) ⇒ Object



16
17
18
# File 'lib/voodoo/generators/gas_elf_generator.rb', line 16

def output_file_name input_name
  input_name.sub(/\.voo$/, '') + @output_file_suffix
end

#output_file_suffixObject



20
21
22
# File 'lib/voodoo/generators/gas_elf_generator.rb', line 20

def output_file_suffix
  @output_file_suffix
end

#write(io) ⇒ Object

Writes the generated code to the given IO handle



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/voodoo/generators/gas_elf_generator.rb', line 25

def write io
  # Create temporary file to write assembly code to
  if io.respond_to? :path
    base = File.basename(io.path).sub(/([^.]*).*/, "\\1")
  else
    base = self.class.name
  end
  
  Tempfile.open(base + '.s') do |asmfile|
    Tempfile.open(base + '.o') do |elffile|
      elffile.close
      # Write assembly code to asmfile
      @asmgenerator.write asmfile
      asmfile.close
      # Find out the name of the GNU assembler
      gas = Voodoo::Config.gas_command
      # Invoke gas on asmfile
      command = "#{gas} #{@extra_args}" +
        " -o #{shell_encode elffile.path}" +
        " #{shell_encode asmfile.path}"
      if system(command)
        write_file_to_io elffile.path, io
      end
      elffile.unlink
    end
    asmfile.unlink
  end
end