It looks like there is a way to inject C code into gdb via compile...code
.
Is there a way to inject a single instruction in the command line? For example, something like:
>>> mov %rax, %rbx
# Undefined command: "mov". Try "help".
It looks like there is a way to inject C code into gdb via compile...code
.
Is there a way to inject a single instruction in the command line? For example, something like:
>>> mov %rax, %rbx
# Undefined command: "mov". Try "help".
Not an easy interactive way that I know of; I don't think GDB has an assembler built-in to its command parser, just commands to disassemble code from memory. Probably in theory some kind of external assemble + load somewhere.
You can of course set $rbx = $rax
to replicate the effect of that specific mov
.
(GDB uses $
as a decorator for register names in GDB expressions.)
To play with single instructions and see what they do, your best bet is to put them in a .s
file and build them into a static executable, which you can then single-step with GDB.
$ cat > foo.s
mov %rax, %rbx
(hit control-d here for EOF)
$ gcc -static -nostdlib foo.s
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
(That's fine, the top of the .text section is where you want the entry point)
$ gdb ./a.out
(gdb) starti # stop before the first instruction
(gdb) set $rax = 0x1234 # or whatever to make the effect visible
(gdb) si
(gdb) quit # or starti to restart or whatever
If you let execution continue from there, it will of course segfault as it decodes the 00 00
bytes that follow the text section as add %al, (%rax)
.
Recent GDB seems to crash on starti
with layout reg
in your ~/.gdbinit
if there's no symbol for the entry point, unfortunately, so you might need to use .globl _start
/ _start:
in your .s.