Method: Voodoo::NasmELFGenerator#write

Defined in:
lib/voodoo/generators/nasm_elf_generator.rb

#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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/voodoo/generators/nasm_elf_generator.rb', line 25

def write io
  # Create temporary file to write NASM code to
  if io.respond_to? :path
    base = File.basename(io.path).sub(/([^.]*).*/, "\\1")
  else
    base = self.class.name
  end
  
  Tempfile.open(base + '.asm') do |nasmfile|
    begin
      Tempfile.open(base + '.o') do |elffile|
        begin
          elffile.close
          # Write NASM code to nsamfile
          @nasmgenerator.write nasmfile
          nasmfile.close
          # Find out the name of the nasm executable
          if ENV.has_key? 'NASM'
            nasm = ENV['NASM']
          elsif ENV.has_key? 'YASM'
            nasm = ENV['YASM']
          else
            nasm = Voodoo::Config.nasm_command
          end
          # Invoke nasm on nasmfile
          command = "#{nasm} #{@nasm_extra_args}" +
            " -o #{shell_encode elffile.path}" +
            " #{shell_encode nasmfile.path}"
          if system(command)
            write_file_to_io elffile.path, io
          else
            raise "Command (#{command}) failed " +
              " with status #{$?.exitstatus}"
          end
        ensure
          elffile.unlink
        end
      end
    ensure
      nasmfile.unlink
    end
  end
end