I do not have extensive experience with gdb, so I am not sure if what I am asking is even possible, but is it possible to edit the code live with gdb?
When running (after hitting a breakpoint), the disas looks like so:
0x080487d8 <+9>: movl $0x80485e4,0x1c(%esp)
0x080487e0 <+17>: movl $0x8048640,0x20(%esp)
0x080487e8 <+25>: movl $0x804869c,0x24(%esp)
0x080487f0 <+33>: movl $0x8048719,0x28(%esp)
In an attempt to change the address in one of those instructions, I did this:
set (*0x080487e1)=0x5b870408
But instead of simply changing the address as I expected, the new disas looked like this:
0x080487d8 <+9>: movl $0x80485e4,0x1c(%esp)
0x080487e0 <+17>: (bad)
0x080487e1 <+18>: or %al,(%edi,%eax,4)
0x080487e4 <+21>: pop %ebx
0x080487e5 <+22>: xchg %al,(%eax,%ecx,1)
0x080487e8 <+25>: movl $0x804869c,0x24(%esp)
0x080487f0 <+33>: movl $0x8048719,0x28(%esp)
So I have 3 questions: Is what I am trying to do possible? If so, am I doing something wrong? If so, what am I doing wrong and how can I fix it?
Yes, you can change .text of a binary.
Note that this change will only affect current execution; upon
runyour change will “evaporate” (if you wanted to permanently patch the binary, that’s possible as well, but the procedure is different).Likely. You didn’t tell us what you are trying to change the instruction to.
Using
(gdb) disas/rwill show you actual raw instruction bytes, and will likely make it easier to see what you did wrong. When I use it, I see this:That is, the address (which you apparently wanted to overwrite) for the instruction above [1] does not begin at
&instruction+1, it begins at&instruction+4. Also, you shouldn’t reverse the bytes when you ask GDB to write a word (I am guessing you wanted the new address to be0x0804785band not0x5b870408):[1] It is very likely that your instruction:
has the same encoding as my instruction:
as they are the “same”, and have the same 8-byte length, but as FrankH pointed out, there might exist a different encoding of the same instruction. In any case,
disas/rwill show you all you need to know.