ronin-code-asm
ronin-code-asm copied to clipboard
Add a pure-Ruby assembler backend
Add a pure-Ruby assembler backend, instead of relying on YASM to produce shellcode.
This would likely take the form of having each architecture module define it's own Assembler class. These class may be initialized with additional configuration (ARM supports being switched to big-endian or little-endian mode) or could also track state while processing instructions. These classes should implement the "Emitter Pattern", and provide an entry point method called emit_instruction(inst) which would accept an Instruction object and output a binary String of opcodes (or possibly append the opcodes directly to an output String/File object that's also given). This method could be as simple as one giant case/when statement which inspects the instruction's name, or a method which dynamically invokes other emit_instruction_#{inst.name} methods using public_send(). Additional helper emit_* methods will probably also need to be defined to emit the operands for the instructions. An emit_program(program) method may also be necessary to output a fully assembled program, instead of a series of opcodes for shellcode.
References
- https://github.com/tenderlove/aarch64 - pure-Ruby ARM64 toy assembler.
- https://github.com/tenderlove/fisk - pure-Ruby x86-64 toy assembler.
- https://github.com/yasm/yasm/blob/master/modules/arch/x86/gen_x86_insn.py - YASM code generator which outputs C code to convert instructions into opcodes.
- https://github.com/jjyg/metasm/tree/main/metasm/cpu - Metasm assembler source code, very messy... Look specifically at
opcodes.rbin each directory. - https://github.com/llvm/llvm-project/tree/main/llvm/lib/Target - LLVM target definitions. Pay special attention to the
.tdfiles.espcially*InstrFormats.tdand*InstrInfo.td. - https://raw.githubusercontent.com/Maratyszcza/Opcodes/refs/heads/master/opcodes/x86.xml - All x86 assembly instructions documented as XML.
- https://raw.githubusercontent.com/Maratyszcza/Opcodes/refs/heads/master/opcodes/x86_64.xml - All x86-64 assembly instructions documented as XML.
- https://github.com/tenderlove/fisk/blob/bb30ecb2c672acb6983030e7982346ba61f04e1b/bin/build-machine.rb - Ruby code that converts the
x86.xmlISA file into Ruby files for each instruction. - https://github.com/tenderlove/aarch64/blob/19844261026b8a7099d30a6384324e7337128fa8/Rakefile#L6-L88 - Ruby code to download and extract XML files from the AArch64 ISA
.tar.gzfile.