Pregunta

I try to define string out of .text section. It's compiled with no error but when I disassemble with gdb , I get bad instruction. Here , it's code in c :

void main(){
    __asm__(
            "jmp .+0x35;"
            "pop %rsi;"
            "mov %rsi, -0x10(%rsi);"
            "movq $0x0,-0x8(%rsi);"
            "mov -0x10(%rsi), %rax;"
            "lea -0x10(%rsi), %rcx;"
            "mov $0x0, %edx;"
            "mov %rcx, %rsi;"
            "mov %rax, %rdi;"
            "mov $0x3b,%eax;"
            "syscall;"
            "mov $0x0,%ebx;"
            "mov $0x1,%eax;"
            "syscall;"
            "call .-0x33;"
            ".string \"/bin/bash\";"
    );

}

disassemble :

   0x0000000000400494 <+0>:     push   %rbp
   0x0000000000400495 <+1>:     mov    %rsp,%rbp
   0x0000000000400498 <+4>:     jmp    0x4004cd <main+57>
   0x000000000040049a <+6>:     pop    %rsi
   0x000000000040049b <+7>:     mov    %rsi,-0x10(%rsi)
   0x000000000040049f <+11>:    movq   $0x0,-0x8(%rsi)
   0x00000000004004a7 <+19>:    mov    -0x10(%rsi),%rax
   0x00000000004004ab <+23>:    lea    -0x10(%rsi),%rcx
   0x00000000004004af <+27>:    mov    $0x0,%edx
   0x00000000004004b4 <+32>:    mov    %rcx,%rsi
   0x00000000004004b7 <+35>:    mov    %rax,%rdi
   0x00000000004004ba <+38>:    mov    $0x3b,%eax
   0x00000000004004bf <+43>:    syscall
   0x00000000004004c1 <+45>:    mov    $0x0,%ebx
   0x00000000004004c6 <+50>:    mov    $0x1,%eax
   0x00000000004004cb <+55>:    syscall
   0x00000000004004cd <+57>:    callq  0x40049a <main+6>
   0x00000000004004d2 <+62>:    (bad) ( **here is define string** )
   0x00000000004004d3 <+63>:    (bad)
   0x00000000004004d4 <+64>:    imul   $0x68736162,0x2f(%rsi),%ebp
   0x00000000004004db <+71>:    add    %cl,%cl
   0x00000000004004dd <+73>:    retq

How can I avoid this error?

¿Fue útil?

Solución

   0x00000000004004cd <+57>:    callq  0x40049a <main+6>
   0x00000000004004d2 <+62>:    (bad) ( **here is define string** )
   0x00000000004004d3 <+63>:    (bad)

How can I avoid this error?

If you don't want the string data appearing directly in the code (.text) section then you can use the .section directive to switch to the .data section, store the string, and then (optionally) switch back to .text again if you need to use the address of the data in more inline asm code:

        "call .-0x33;"
        ".section .data;"
        "1: .string \"/bin/bash\";"

This is described well by the accepted answer to Defining Bytes in GCC Inline Assembly in Dev-C++(.ascii in AT&T syntax on Windows)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top