Class: Ronin::Code::ASM::Program
- Inherits:
-
Object
- Object
- Ronin::Code::ASM::Program
- Defined in:
- lib/ronin/code/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 ⇒ Array<Symbol>
readonly
The registers used by the program.
-
#arch ⇒ Symbol
readonly
The targeted architecture.
-
#instructions ⇒ Array<Instruction>
readonly
The instructions of the program.
-
#os ⇒ Symbol?
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 ⇒ Integer
readonly
The default word size.
Instance Method Summary collapse
-
#assemble(output, syntax: :intel, format: :bin) ⇒ 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(arch: :x86, os: nil, define: {}) { ... } ⇒ 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(arch: :x86, os: nil, define: {}) { ... } ⇒ Program
Initializes a new Assembly Program.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ronin/code/asm/program.rb', line 112 def initialize(arch: :x86, os: nil, define: {}, &block) @arch = arch arch = Archs.const_get(@arch.to_s.upcase) @word_size = arch::WORD_SIZE @registers = arch::REGISTERS extend arch @syscalls = {} if os @os = os @syscalls = OS::SYSCALLS[@os][@arch] extend OS[@os] end define.each do |name,value| instance_variable_set("@#{name}",value) 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.
492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/ronin/code/asm/program.rb', line 492 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 ⇒ Array<Symbol> (readonly)
The registers used by the program
81 82 83 |
# File 'lib/ronin/code/asm/program.rb', line 81 def allocated_registers @allocated_registers end |
#arch ⇒ Symbol (readonly)
The targeted architecture
54 55 56 |
# File 'lib/ronin/code/asm/program.rb', line 54 def arch @arch end |
#instructions ⇒ Array<Instruction> (readonly)
The instructions of the program
86 87 88 |
# File 'lib/ronin/code/asm/program.rb', line 86 def instructions @instructions end |
#os ⇒ Symbol? (readonly)
The targeted Operating System
59 60 61 |
# File 'lib/ronin/code/asm/program.rb', line 59 def os @os end |
#registers ⇒ Hash{Symbol => Register} (readonly)
The registers available to the program
70 71 72 |
# File 'lib/ronin/code/asm/program.rb', line 70 def registers @registers end |
#syscalls ⇒ Hash{Symbol => Integer} (readonly)
The syscalls available to the program
76 77 78 |
# File 'lib/ronin/code/asm/program.rb', line 76 def syscalls @syscalls end |
#word_size ⇒ Integer (readonly)
The default word size
64 65 66 |
# File 'lib/ronin/code/asm/program.rb', line 64 def word_size @word_size end |
Instance Method Details
#assemble(output, syntax: :intel, format: :bin) ⇒ String
Assembles the program.
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/ronin/code/asm/program.rb', line 458 def assemble(output, syntax: :intel, format: :bin) parser = PARSERS.fetch(syntax) do raise(ArgumentError,"unknown ASM syntax: #{syntax.inspect}") end source = Tempfile.new(['ronin-code-asm', '.s']) source.write(to_asm(syntax)) source.close YASM::Command.run( file: source.path, parser: parser, target: @arch, output_format: format, output: output ) return output end |
#byte(op) ⇒ MemoryOperand, ImmediateOperand
Creates an operand of size 1 (byte).
207 208 209 210 211 212 213 214 |
# File 'lib/ronin/code/asm/program.rb', line 207 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.
390 391 392 393 394 395 396 |
# File 'lib/ronin/code/asm/program.rb', line 390 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).
243 244 245 246 247 248 249 250 |
# File 'lib/ronin/code/asm/program.rb', line 243 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.
404 405 406 |
# File 'lib/ronin/code/asm/program.rb', line 404 def eval(&block) instance_eval(&block) end |
#instruction(name, *operands) ⇒ Instruction
Adds a new instruction to the program.
191 192 193 194 195 196 |
# File 'lib/ronin/code/asm/program.rb', line 191 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.
299 300 |
# File 'lib/ronin/code/asm/program.rb', line 299 def interrupt(number) end |
#label(name) { ... } ⇒ Symbol
Adds a label to the program.
283 284 285 286 287 288 289 |
# File 'lib/ronin/code/asm/program.rb', line 283 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).
261 262 263 264 265 266 267 268 |
# File 'lib/ronin/code/asm/program.rb', line 261 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ronin/code/asm/program.rb', line 166 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.
150 151 152 |
# File 'lib/ronin/code/asm/program.rb', line 150 def register?(name) @registers.has_key?(name.to_sym) end |
#register_clear(name) ⇒ Object
Generic method for clearing a register.
340 341 |
# File 'lib/ronin/code/asm/program.rb', line 340 def register_clear(name) end |
#register_load(name) ⇒ Object
Generic method for loading a register.
376 377 |
# File 'lib/ronin/code/asm/program.rb', line 376 def register_load(name) end |
#register_save(name) ⇒ Object
Generic method for saving a register.
365 366 |
# File 'lib/ronin/code/asm/program.rb', line 365 def register_save(name) end |
#register_set(name, value) ⇒ Object
Generic method for setting a register.
354 355 |
# File 'lib/ronin/code/asm/program.rb', line 354 def register_set(name,value) end |
#stack_pop(name) ⇒ Object
Generic method for popping off the stack.
329 330 |
# File 'lib/ronin/code/asm/program.rb', line 329 def stack_pop(name) end |
#stack_push(value) ⇒ Object
Generic method for pushing onto the stack.
318 319 |
# File 'lib/ronin/code/asm/program.rb', line 318 def stack_push(value) end |
#syscall ⇒ Object
Generic method for generating the instruction for invoking a syscall.
307 308 |
# File 'lib/ronin/code/asm/program.rb', line 307 def syscall end |
#to_asm(syntax = :intel) ⇒ Object
Converts the program to Assembly Source Code.
414 415 416 |
# File 'lib/ronin/code/asm/program.rb', line 414 def to_asm(syntax=:intel) SYNTAX[syntax].emit_program(self) end |
#to_s ⇒ Object
421 422 423 |
# File 'lib/ronin/code/asm/program.rb', line 421 def to_s to_asm end |
#word(op) ⇒ MemoryOperand, ImmediateOperand
Creates a operand of size 2 (bytes).
225 226 227 228 229 230 231 232 |
# File 'lib/ronin/code/asm/program.rb', line 225 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 |