Class: Ronin::ASM::Program
- Inherits:
-
Object
- Object
- Ronin::ASM::Program
- Defined in:
- lib/ronin/asm/program.rb
Overview
Represents a full Assembly program.
Direct Known Subclasses
Constant Summary collapse
- SYNTAX =
Supported Assembly Syntaxs
{ att: Syntax::ATT, intel: Syntax::Intel }
- PARSERS =
The Assembly Parsers
{ att: :gas, intel: :nasm }
Instance Attribute Summary collapse
-
#allocated_registers ⇒ Object
readonly
The registers used by the program.
-
#arch ⇒ Object
readonly
The targeted architecture.
-
#instructions ⇒ Object
readonly
The instructions of the program.
-
#os ⇒ Object
readonly
The targeted Operating System.
-
#registers ⇒ Hash{Symbol => Register}
readonly
The registers available to the program.
-
#syscalls ⇒ Hash{Symbol => Integer}
readonly
The syscalls available to the program.
-
#word_size ⇒ Object
readonly
The default word size.
Instance Method Summary collapse
-
#assemble(output, options = {}) ⇒ String
Assembles the program.
-
#byte(op) ⇒ MemoryOperand, ImmediateOperand
Creates an operand of size 1 (byte).
-
#critical(*regs) { ... } ⇒ Object
Defines a critical region, where the specified Registers should be saved and then reloaded.
-
#dword(op) ⇒ ImmediateOperand
Creates a operand of size 4 (bytes).
-
#eval { ... } ⇒ Object
Evaluates code within the Program.
-
#initialize(options = {}) { ... } ⇒ Program
constructor
Initializes a new Assembly Program.
-
#instruction(name, *operands) ⇒ Instruction
Adds a new instruction to the program.
-
#interrupt(number) ⇒ Object
abstract
Generic method for generating the instruction for causing an interrupt.
-
#label(name) { ... } ⇒ Symbol
Adds a label to the program.
-
#method_missing(name, *arguments, &block) ⇒ Object
protected
Allows adding unknown instructions to the program.
-
#qword(op) ⇒ MemoryOperand, ImmediateOperand
Creates a operand of size 8 (bytes).
-
#register(name) ⇒ Register
Accesses a register.
-
#register?(name) ⇒ Boolean
Determines if a register exists.
-
#register_clear(name) ⇒ Object
abstract
Generic method for clearing a register.
-
#register_load(name) ⇒ Object
abstract
Generic method for loading a register.
-
#register_save(name) ⇒ Object
abstract
Generic method for saving a register.
-
#register_set(name, value) ⇒ Object
abstract
Generic method for setting a register.
-
#stack_pop(name) ⇒ Object
abstract
Generic method for popping off the stack.
-
#stack_push(value) ⇒ Object
abstract
Generic method for pushing onto the stack.
-
#syscall ⇒ Object
abstract
Generic method for generating the instruction for invoking a syscall.
-
#to_asm(syntax = :intel) ⇒ Object
Converts the program to Assembly Source Code.
- #to_s ⇒ Object
-
#word(op) ⇒ MemoryOperand, ImmediateOperand
Creates a operand of size 2 (bytes).
Constructor Details
#initialize(options = {}) { ... } ⇒ Program
Initializes a new Assembly Program.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ronin/asm/program.rb', line 105 def initialize(={},&block) @arch = .fetch(:arch,:x86).to_sym arch = Archs.const_get(@arch.to_s.upcase) @word_size = arch::WORD_SIZE @registers = arch::REGISTERS extend Archs.const_get(@arch.to_s.upcase) @syscalls = {} if .has_key?(:os) @os = [:os].to_s @syscalls = OS::SYSCALLS[@os][@arch] extend OS.const_get(@os) end if [:define] [:define].each do |name,value| instance_variable_set("@#{name}",value) end end @allocated_registers = [] @instructions = [] instance_eval(&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, &block) ⇒ Object (protected)
Allows adding unknown instructions to the program.
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/ronin/asm/program.rb', line 487 def method_missing(name,*arguments,&block) if (block && arguments.empty?) label(name,&block) elsif block.nil? if (arguments.empty? && register?(name)) register(name) else instruction(name,*arguments) end else super(name,*arguments,&block) end end |
Instance Attribute Details
#allocated_registers ⇒ Object (readonly)
The registers used by the program
73 74 75 |
# File 'lib/ronin/asm/program.rb', line 73 def allocated_registers @allocated_registers end |
#arch ⇒ Object (readonly)
The targeted architecture
52 53 54 |
# File 'lib/ronin/asm/program.rb', line 52 def arch @arch end |
#instructions ⇒ Object (readonly)
The instructions of the program
76 77 78 |
# File 'lib/ronin/asm/program.rb', line 76 def instructions @instructions end |
#os ⇒ Object (readonly)
The targeted Operating System
55 56 57 |
# File 'lib/ronin/asm/program.rb', line 55 def os @os end |
#registers ⇒ Hash{Symbol => Register} (readonly)
The registers available to the program
64 65 66 |
# File 'lib/ronin/asm/program.rb', line 64 def registers @registers end |
#syscalls ⇒ Hash{Symbol => Integer} (readonly)
The syscalls available to the program
70 71 72 |
# File 'lib/ronin/asm/program.rb', line 70 def syscalls @syscalls end |
#word_size ⇒ Object (readonly)
The default word size
58 59 60 |
# File 'lib/ronin/asm/program.rb', line 58 def word_size @word_size end |
Instance Method Details
#assemble(output, options = {}) ⇒ String
Assembles the program.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/ronin/asm/program.rb', line 453 def assemble(output,={}) syntax = .fetch(:syntax,:intel) format = .fetch(:format,:bin) parser = PARSERS[syntax] source = Tempfile.new(['ronin-asm', '.s']) source.write(to_asm(syntax)) source.close YASM::Program.assemble( file: source.path, parser: PARSERS[syntax], target: @arch, output_format: format, output: output ) return output end |
#byte(op) ⇒ MemoryOperand, ImmediateOperand
Creates an operand of size 1 (byte).
202 203 204 205 206 207 208 209 |
# File 'lib/ronin/asm/program.rb', line 202 def byte(op) case op when MemoryOperand MemoryOperand.new(op.base,op.offset,op.index,op.scale,1) else ImmediateOperand.new(op,1) end end |
#critical(*regs) { ... } ⇒ Object
Defines a critical region, where the specified Registers should be saved and then reloaded.
385 386 387 388 389 390 391 |
# File 'lib/ronin/asm/program.rb', line 385 def critical(*regs,&block) regs.each { |name| register_save(name) } instance_eval(&block) regs.reverse_each { |name| register_load(name) } end |
#dword(op) ⇒ ImmediateOperand
Creates a operand of size 4 (bytes).
238 239 240 241 242 243 244 245 |
# File 'lib/ronin/asm/program.rb', line 238 def dword(op) case op when MemoryOperand MemoryOperand.new(op.base,op.offset,op.index,op.scale,4) else ImmediateOperand.new(op,4) end end |
#eval { ... } ⇒ Object
Evaluates code within the Program.
399 400 401 |
# File 'lib/ronin/asm/program.rb', line 399 def eval(&block) instance_eval(&block) end |
#instruction(name, *operands) ⇒ Instruction
Adds a new instruction to the program.
186 187 188 189 190 191 |
# File 'lib/ronin/asm/program.rb', line 186 def instruction(name,*operands) insn = Instruction.new(name.to_sym,operands) @instructions << insn return insn end |
#interrupt(number) ⇒ Object
Generic method for generating the instruction for causing an interrupt.
294 295 |
# File 'lib/ronin/asm/program.rb', line 294 def interrupt(number) end |
#label(name) { ... } ⇒ Symbol
Adds a label to the program.
278 279 280 281 282 283 284 |
# File 'lib/ronin/asm/program.rb', line 278 def label(name,&block) name = name.to_sym @instructions << name instance_eval(&block) return name end |
#qword(op) ⇒ MemoryOperand, ImmediateOperand
Creates a operand of size 8 (bytes).
256 257 258 259 260 261 262 263 |
# File 'lib/ronin/asm/program.rb', line 256 def qword(op) case op when MemoryOperand MemoryOperand.new(op.base,op.offset,op.index,op.scale,8) else ImmediateOperand.new(op,8) end end |
#register(name) ⇒ Register
Accesses a register.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ronin/asm/program.rb', line 161 def register(name) name = name.to_sym unless register?(name) raise(ArgumentError,"unknown register: #{name}") end unless @allocated_registers.include?(name) # mark the register as being used, when it was first accessed @allocated_registers << name end return @registers[name] end |
#register?(name) ⇒ Boolean
Determines if a register exists.
145 146 147 |
# File 'lib/ronin/asm/program.rb', line 145 def register?(name) @registers.has_key?(name.to_sym) end |
#register_clear(name) ⇒ Object
Generic method for clearing a register.
335 336 |
# File 'lib/ronin/asm/program.rb', line 335 def register_clear(name) end |
#register_load(name) ⇒ Object
Generic method for loading a register.
371 372 |
# File 'lib/ronin/asm/program.rb', line 371 def register_load(name) end |
#register_save(name) ⇒ Object
Generic method for saving a register.
360 361 |
# File 'lib/ronin/asm/program.rb', line 360 def register_save(name) end |
#register_set(name, value) ⇒ Object
Generic method for setting a register.
349 350 |
# File 'lib/ronin/asm/program.rb', line 349 def register_set(name,value) end |
#stack_pop(name) ⇒ Object
Generic method for popping off the stack.
324 325 |
# File 'lib/ronin/asm/program.rb', line 324 def stack_pop(name) end |
#stack_push(value) ⇒ Object
Generic method for pushing onto the stack.
313 314 |
# File 'lib/ronin/asm/program.rb', line 313 def stack_push(value) end |
#syscall ⇒ Object
Generic method for generating the instruction for invoking a syscall.
302 303 |
# File 'lib/ronin/asm/program.rb', line 302 def syscall end |
#to_asm(syntax = :intel) ⇒ Object
Converts the program to Assembly Source Code.
409 410 411 |
# File 'lib/ronin/asm/program.rb', line 409 def to_asm(syntax=:intel) SYNTAX[syntax].emit_program(self) end |
#to_s ⇒ Object
416 417 418 |
# File 'lib/ronin/asm/program.rb', line 416 def to_s to_asm end |
#word(op) ⇒ MemoryOperand, ImmediateOperand
Creates a operand of size 2 (bytes).
220 221 222 223 224 225 226 227 |
# File 'lib/ronin/asm/program.rb', line 220 def word(op) case op when MemoryOperand MemoryOperand.new(op.base,op.offset,op.index,op.scale,2) else ImmediateOperand.new(op,2) end end |