How to disassemble a memory range with GDB?

前端 未结 8 644
故里飘歌
故里飘歌 2020-12-04 08:03

I\'m trying to disassemble a program to see a syscall assembly instruction (the INT instruction, I believe) and the handler with GDB and have written a little program (see b

相关标签:
8条回答
  • 2020-12-04 08:39

    fopen() is a C library function and so you won't see any syscall instructions in your code, just a regular function call. At some point, it does call open(2), but it does that via a trampoline. There is simply a jump to the VDSO page, which is provided by the kernel to every process. The VDSO then provides code to make the system call. On modern processors, the SYSCALL or SYSENTER instructions will be used, but you can also use INT 80h on x86 processors.

    0 讨论(0)
  • 2020-12-04 08:45

    gdb disassemble has a /m to include source code alongside the instructions. This is equivalent of objdump -S, with the extra benefit of confining to just the one function (or address-range) of interest.

    0 讨论(0)
  • 2020-12-04 08:46

    You don't have to use gdb. GCC will do it.

     gcc -S foo.c
    

    This will create foo.s which is the assembly.

    gcc -m32 -c -g -Wa,-a,-ad foo.c > foo.lst
    

    The above version will create a listing file that has both the C and the assembly generated by it. GCC FAQ

    0 讨论(0)
  • 2020-12-04 08:47

    Do you only want to disassemble your actual main? If so try this:

    (gdb) info line main 
    (gdb) disas STARTADDRESS ENDADDRESS
    

    Like so:

    USER@MACHINE /cygdrive/c/prog/dsa
    $ gcc-3.exe -g main.c
    
    USER@MACHINE /cygdrive/c/prog/dsa
    $ gdb a.exe
    GNU gdb 6.8.0.20080328-cvs (cygwin-special)
    ...
    (gdb) info line main
    Line 3 of "main.c" starts at address 0x401050 <main> and ends at 0x401075 <main+
    (gdb) disas 0x401050 0x401075
    Dump of assembler code from 0x401050 to 0x401075:
    0x00401050 <main+0>:    push   %ebp
    0x00401051 <main+1>:    mov    %esp,%ebp
    0x00401053 <main+3>:    sub    $0x18,%esp
    0x00401056 <main+6>:    and    $0xfffffff0,%esp
    0x00401059 <main+9>:    mov    $0x0,%eax
    0x0040105e <main+14>:   add    $0xf,%eax
    0x00401061 <main+17>:   add    $0xf,%eax
    0x00401064 <main+20>:   shr    $0x4,%eax
    0x00401067 <main+23>:   shl    $0x4,%eax
    0x0040106a <main+26>:   mov    %eax,-0xc(%ebp)
    0x0040106d <main+29>:   mov    -0xc(%ebp),%eax
    0x00401070 <main+32>:   call   0x4010c4 <_alloca>
    End of assembler dump.
    

    I don't see your system interrupt call however. (its been a while since I last tried to make a system call in assembly. INT 21h though, last I recall

    0 讨论(0)
  • 2020-12-04 08:47

    Yeah, disassemble is not the best command to use here. The command you want is "x/i" (examine as instructions):

    (gdb) x/i 0xdeadbeef
    
    0 讨论(0)
  • 2020-12-04 08:51

    You can force gcc to output directly to assembly code by adding the -S switch

    gcc -S hello.c
    
    0 讨论(0)
提交回复
热议问题