Re: [PATCH] gdb: x86: fix x32 builds with inline asm

H.J. Lu
2013-01-08T16:54:43Z

On Tue, Jan 8, 2013 at 7:59 AM, Mike Frysinger <vapier@gentoo.org> wrote:
> On Tuesday 08 January 2013 10:01:20 Mike Frysinger wrote:
>> -#if defined __i386__
>> - asm volatile ("pushl %0;"
>> - ".globl linux_ptrace_test_ret_to_nx_instr;"
>> - "linux_ptrace_test_ret_to_nx_instr:"
>> - "ret"
>> - : : "r" (return_address) : "%esp", "memory");
>> -#elif defined __x86_64__
>> - asm volatile ("pushq %0;"
>> + asm volatile ("push %0;"
>> ".globl linux_ptrace_test_ret_to_nx_instr;"
>> "linux_ptrace_test_ret_to_nx_instr:"
>> "ret"
>> - : : "r" (return_address) : "%rsp", "memory");
>> -#else
>> -# error "!__i386__ && !__x86_64__"
>> -#endif
>> + : : "r" (return_address) : "sp", "memory");
>
> hrm, this works for -m32 and -m64, but doesn't actually help with -mx32. this
> doesn't seem to line up with my expectations. can you suggest something here
> H.J. Lu ?
>
> $ cat test.c
> main() { asm volatile ("push %0; ret;" : : "r"(main) : "sp", "memory"); }
>
> $ gcc -m32 test.c
> 8: 50 push %eax
> 9: c3 ret
>
> $ gcc -m64 test.c
> 9: 50 push %rax
> a: c3 retq
>
> $ gcc -mx32 test.c
> test.c: Assembler messages:
> test.c:2: Error: operand type mismatch for `push'
> -mike

Can you try this?

--
H.J.
---
diff --git a/gdb/common/linux-ptrace.c b/gdb/common/linux-ptrace.c
index 761ef59..cc08b6a 100644
--- a/gdb/common/linux-ptrace.c
+++ b/gdb/common/linux-ptrace.c
@@ -103,21 +103,16 @@ linux_ptrace_test_ret_to_nx (void)
strerror (errno));
else
{
-#if defined __i386__
- asm volatile ("pushl %0;"
- ".globl linux_ptrace_test_ret_to_nx_instr;"
- "linux_ptrace_test_ret_to_nx_instr:"
- "ret"
- : : "r" (return_address) : "%esp", "memory");
-#elif defined __x86_64__
- asm volatile ("pushq %0;"
+ asm volatile ("push %0;"
".globl linux_ptrace_test_ret_to_nx_instr;"
"linux_ptrace_test_ret_to_nx_instr:"
"ret"
- : : "r" (return_address) : "%rsp", "memory");
+#ifdef __x86_64__
+ : : "r" ((uint64_t) (uintptr_t) (return_address))
#else
-# error "!__i386__ && !__x86_64__"
+ : : "r" (return_address)
#endif
+ : "sp", "memory");
gdb_assert_not_reached ("asm block did not terminate");
}