I am very skeptical about patching of existing .cubin files - it requires too much book-keeping. Let's say we want to insert several additional instructions into some function - then we need
- extend section containing code for those function by patching sections table
- patch symbols table/relocs
- disasm whole function and build code-flow graph for all instructions in function
- fix offsets for jumps
- fix attributes like EIATTR_INDIRECT_BRANCH_TARGETS & EIATTR_JUMPTABLE_RELOCS
- and so on
While points 1-2 can be implemented with ELF patching libraries like elftools it is anyway too much tedious labour
For example CuAssembler prefers to create new .cubin files from scratch. In any case we need some engine to generate sass instructions and this task is perfectly achieve-able when you have ready disassembler. So I add to my sass disasm engine some primary features for code generation:
- dictionary of all instructions for given SM - method INV_disasm::get_instrs
- for each instruction add encoders describing how to put values for fields, tables, constant banks & scheduling
As illustration I've implemented interactive sass assembler (with some help of readline for auto-completion)