class Metasm::AOut

Constants

FLAGS
MACHINE_TYPE
MAGIC
SYMBOL_TYPE

Attributes

data[RW]
datarel[RW]
endianness[RW]
header[RW]
symbols[RW]
text[RW]
textrel[RW]

Public Class Methods

new(cpu = nil) click to toggle source
Calls superclass method Metasm::ExeFormat::new
# File metasm/exe_format/a_out.rb, line 100
def initialize(cpu = nil)
        @endianness = cpu ? cpu.endianness : :little
        @header = Header.new
        @text = EncodedData.new
        @data = EncodedData.new
        super(cpu)
end

Public Instance Methods

assemble(*a) click to toggle source
# File metasm/exe_format/a_out.rb, line 181
def assemble(*a)
        parse(*a) if not a.empty?
        @text << assemble_sequence(@textsrc, @cpu)
        @textsrc.clear
        @data << assemble_sequence(@datasrc, @cpu)
        @datasrc.clear
        self
end
decode() click to toggle source
# File metasm/exe_format/a_out.rb, line 113
def decode
        decode_header

        tlen = @header.text
        case @header.magic
        when 'ZMAGIC'; @encoded.ptr = 1024
        when 'QMAGIC'; tlen -= 32     # header is included in .text
        end
        @text = EncodedData.new << @encoded.read(tlen)

        @data = EncodedData.new << @encoded.read(@header.data)

        # TODO
        #textrel = @encoded.read @header.trsz
        #datarel = @encoded.read @header.drsz
        #syms    = @encoded.read @header.syms
        #strings = @encoded.read
end
decode_byte(edata = @encoded) click to toggle source
# File metasm/exe_format/a_out.rb, line 90
def decode_byte(edata = @encoded) edata.decode_imm(:u8 , @endianness) end
decode_half(edata = @encoded) click to toggle source
# File metasm/exe_format/a_out.rb, line 91
def decode_half(edata = @encoded) edata.decode_imm(:u16, @endianness) end
decode_header() click to toggle source
# File metasm/exe_format/a_out.rb, line 108
def decode_header
        @encoded.ptr = 0
        @header.decode(self)
end
decode_word(edata = @encoded) click to toggle source
# File metasm/exe_format/a_out.rb, line 92
def decode_word(edata = @encoded) edata.decode_imm(:u32, @endianness) end
each_section() { |text, tva| ... } click to toggle source
# File metasm/exe_format/a_out.rb, line 190
def each_section
        tva = 0
        tva = 4096+32 if @header.magic == 'QMAGIC'
        yield @text, tva
        yield @data, tva + @text.virtsize
end
encode() click to toggle source
# File metasm/exe_format/a_out.rb, line 132
def encode
        # non mmapable on linux anyway
        # could support OMAGIC..
        raise EncodeError, 'cannot encode non-QMAGIC a.out' if @header.magic and @header.magic != 'QMAGIC'

        # data must be 4096-aligned
        # 32 bytes of header included in .text
        @text.virtsize = (@text.virtsize + 32 + 4096 - 1) / 4096 * 4096 - 32
        if @data.rawsize % 4096 != 0
                @data[(@data.rawsize + 4096 - 1) / 4096 * 4096 - 1] = 0
        end

        @header.text = @text.length+32
        @header.data = @data.rawsize
        @header.bss = @data.virtsize - @data.rawsize

        @encoded = EncodedData.new
        @encoded << @header.encode(self)
        binding = @text.binding(4096+32).merge @data.binding(4096 + @header.text)
        @encoded << @text << @data
        @encoded.fixup! binding
        @encoded.data
end
encode_byte(w) click to toggle source
# File metasm/exe_format/a_out.rb, line 93
def encode_byte(w) Expression[w].encode(:u8 , @endianness) end
encode_half(w) click to toggle source
# File metasm/exe_format/a_out.rb, line 94
def encode_half(w) Expression[w].encode(:u16, @endianness) end
encode_word(w) click to toggle source
# File metasm/exe_format/a_out.rb, line 95
def encode_word(w) Expression[w].encode(:u32, @endianness) end
parse_init() click to toggle source
Calls superclass method Metasm::ExeFormat#parse_init
# File metasm/exe_format/a_out.rb, line 156
def parse_init
        @textsrc ||= []
        @datasrc ||= []
        @cursource ||= @textsrc
        super()
end
parse_parser_instruction(instr) click to toggle source
# File metasm/exe_format/a_out.rb, line 163
def parse_parser_instruction(instr)
        case instr.raw.downcase
        when '.text'; @cursource = @textsrc
        when '.data'; @cursource = @datasrc
        when '.entrypoint'
                # ".entrypoint <somelabel/expression>" or ".entrypoint" (here)
                @lexer.skip_space
                if tok = @lexer.nexttok and tok.type == :string
                        raise instr if not entrypoint = Expression.parse(@lexer)
                else
                        entrypoint = new_label('entrypoint')
                        @cursource << Label.new(entrypoint, instr.backtrace.dup)
                end
                @header.entry = entrypoint
        else super(instr)
        end
end
sizeof_byte() click to toggle source
# File metasm/exe_format/a_out.rb, line 96
def sizeof_byte ; 1 ; end
sizeof_half() click to toggle source
# File metasm/exe_format/a_out.rb, line 97
def sizeof_half ; 2 ; end
sizeof_word() click to toggle source
# File metasm/exe_format/a_out.rb, line 98
def sizeof_word ; 4 ; end