Now to the niddy-gritty:
I'm working on a project that would involve dynamic recompilation, but I have hit an early snag that I hope someone can help me with. I want to be able to load machine code from a file, load it in ram, jump to that memory address, then jump back. Sounds simple. I start in C to open the file and copy it to ram, then use its pointer address as an argument (addr) in the below inline asm.
Code: Select all
asm(
"la $a0, skip\n"
"or %0, $a0, $zero\n" //load the address of skip: into output variable b
"j cont\n"
"skip:\n"
"jr $ra\n"
"cont:\n"
"jalr $a0\n" //option A
// "jalr %3\n" //option B
"lw %1, 0(%3)\n" //load the value at addr into output variable c
"lw %2, 0($a0)\n" //load the value at skip: into output variable d
:"=r" (b), "=r" (c), "=r" (d)
:"r" (addr)
:"%ra", "%sp", "%fp", "%at", "%a0", "%t0", "%t1", "%s0" //got lazy with the clobber list
);
Code: Select all
jr $ra
nop
skip address: 0x08900440 value: 0x03E00008
file address: 0x08918fd0 value: 0x03E00008
(So pretty much the same thing except the address.)
However, when I try option B where it tries to jump to the ram content of the file, it locks up. Why does this happen and how can I get it to run what i'm trying to inject?