Module: Evoasm::X64

Defined in:
lib/evoasm/x64.rb,
lib/evoasm/x64/operand.rb,
lib/evoasm/x64/cpu_state.rb,
lib/evoasm/x64/parameters.rb,
lib/evoasm/x64/instruction.rb

Defined Under Namespace

Classes: BasicParameters, CPUState, Instruction, Operand, Parameters

Class Method Summary collapse

Class Method Details

.disassemble(assembly, address = nil) ⇒ String

Disassembles x86-64 machine code

Parameters:

  • assembly (String)

    assembly

  • address (Integer) (defaults to: nil)

    optional address to show in the disassembly

Returns:

  • (String)

    disassembly



18
19
20
# File 'lib/evoasm/x64.rb', line 18

def disassemble(assembly, address = nil)
  Evoasm::Capstone.disassemble_x64 assembly, address
end

.emit_stack_frame(abi = :sysv, buffer) ⇒ void

This method returns an undefined value.

Emits a stack frame for the given ABI

Parameters:

  • abi (Symbol) (defaults to: :sysv)

    the ABI to use

  • buffer (Buffer)

    the buffer to emit to



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/evoasm/x64.rb', line 64

def emit_stack_frame(abi = :sysv, buffer)
  unless Libevoasm.x64_emit_func_prolog abi, buffer
    raise Error.last
  end

  yield

  unless Libevoasm.x64_emit_func_epilog abi, buffer
    raise Error.last
  end
end

.encode(instruction_name, parameters, buffer = nil, basic: false) ⇒ String?

Encodes a x86-64 machine instruction

Parameters:

  • instruction_name (Symbol)

    the name of the instruction

  • parameters (X64::Parameters, Hash)

    the instruction parameters

  • buffer (Buffer) (defaults to: nil)

    the buffer to emit to, if omitted, the encoded instruction is retured as string

  • basic (Boolean)

    whether to use the basic encoder

Returns:

  • (String, nil)

    the encoded machine code as string, or nil if a buffer was provided



28
29
30
# File 'lib/evoasm/x64.rb', line 28

def encode(instruction_name, parameters, buffer = nil, basic: false)
  instruction(instruction_name).encode parameters, buffer, basic: basic
end

.featuresArray<Symbol>

Returns the list of supported CPU features (obtained via CPUID)

Returns:

  • (Array<Symbol>)

    the list of supported CPU features (obtained via CPUID)



38
39
40
41
42
43
44
45
46
47
# File 'lib/evoasm/x64.rb', line 38

def features
  feature_enum_type = Libevoasm.enum_type(:x64_feature)
  arch_info = Libevoasm.get_arch_info :x64
  features_as_flags = Libevoasm.arch_info_get_features arch_info
  feature_enum_type.symbol_map.each_with_object({}) do |(k, v), features|
    next if k == :none
    supported = (features_as_flags & (1 << v)) != 0
    features[k] = supported
  end
end

.idObject



49
50
51
# File 'lib/evoasm/x64.rb', line 49

def id
  :x64
end

.instruction(instruction_name) ⇒ Instruction

Gives an Instruction object for the given instruction name.

Parameters:

  • instruction_name (Symbol)

    instruction name

Returns:



56
57
58
# File 'lib/evoasm/x64.rb', line 56

def instruction(instruction_name)
  Instruction.new Libevoasm.x64_get_inst(instruction_name), instruction_name
end

.instruction_names(*reg_types, operand_types: [:reg, :rm, :imm], useless: false, basic: true, features: nil) ⇒ Array<Symbol>

Gives a list of instructions

Parameters:

  • reg_types (Array)

    restrict to given register types (e.g. :gp for general-purpose registers)

  • operand_types (Array<Symbol>)

    restrict to instructions whose operands are of the specified types

  • useless (Bool)

    whether to include useless instructions

  • basic (Bool)

    restrict to instructions supported by the basic encoder

  • features (Array<Symbol>)

    only give instructions covered by the specified feature set

Returns:

  • (Array<Symbol>)

    array of instruction names



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/evoasm/x64.rb', line 83

def instruction_names(*reg_types, operand_types: [:reg, :rm, :imm], useless: false, basic: true, features: nil)
  inst_id_enum_type = Libevoasm.enum_type(:x64_inst_id)
  feature_enum_type = Libevoasm.enum_type(:x64_feature)
  insts_flags_enum_type = Libevoasm.enum_type(:x64_insts_flags)
  op_type_enum_type = Libevoasm.enum_type(:x64_operand_type)
  reg_type_enum_type = Libevoasm.enum_type(:x64_reg_type)

  flags = []

  flags << :include_useless if useless
  flags << :only_basic if basic
  flags_as_flags = insts_flags_enum_type.flags flags, shift: false

  features_as_flags =
    if features.nil?
      arch_info = Libevoasm.get_arch_info :x64
      Libevoasm.arch_info_get_features arch_info
    else
      feature_enum_type.flags features, shift: true
    end

  op_types_as_flags = op_type_enum_type.flags operand_types, shift: true
  reg_types_as_flags = reg_type_enum_type.flags reg_types, shift: true

  n_insts = inst_id_enum_type[:none]
  array = FFI::MemoryPointer.new :int, n_insts
  len = Libevoasm.x64_get_insts(flags_as_flags, features_as_flags,
                             op_types_as_flags, reg_types_as_flags, array)

  instruction_ids = array.read_array_of_type(:int, :read_int, len)

  instruction_ids.map { |e| inst_id_enum_type[e] }
end

.registersArray<Symbol>

Returns a list of available registers

Returns:

  • (Array<Symbol>)

    a list of available registers



33
34
35
# File 'lib/evoasm/x64.rb', line 33

def registers
  Libevoasm.enum_type(:x64_reg_id).symbols[0..-2]
end