Discussion:
Local variables used inside an asm block are not recognized as used
Daniel Kamil Kozar
2014-10-05 18:13:46 UTC
Permalink
Hello,
Long time ago, I wrote the following code snippet in order to
demonstrate how to call system calls directly from gcc via inline
assembly in amd64 systems, as opposed to using the libc wrapper
functions.

#include <unistd.h>

int main(void)
{
const char hello[] = "Hello World!\n";
const size_t hello_size = sizeof(hello);
ssize_t ret;
asm
(
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movq %1, %%rsi\n\t"
"movl %2, %%edx\n\t"
"syscall"
: "=a"(ret)
: "g"(hello), "g"(hello_size)
: "%rdi", "%rsi", "%rdx", "%rcx", "%r11"
);
return 0;
}

Unfortunately, this snippet does not work anymore with gcc 4.9.1. An
inspection of gcc's result when run with -S shows that the "hello"
variable is not even created. Adding "static" to the variable's
declaration fixes the issue, however I'm still wondering what's wrong
with the original code and why gcc does not seem to see that the local
variable is actually used by the asm block.

Thanks,
-dkk
David Wohlferd
2014-10-05 21:08:39 UTC
Permalink
Well, as written, gcc discards the whole asm statement as unneeded when
using any optimization since none of the outputs (ie ret) get used and
the statement is not volatile.

I believe that adding "m" (hello) as an input will resolve your other
problem. Note that you may not easily see the string in the asm output
since gcc may encode this using something like this:

movabsq $8022916924116329800, %rax
movq %rax, 32(%rsp)
movl $10, %eax
movw %ax, 44(%rsp)

Also, I'd probably write this statement as something more like this
(untested):

asm volatile
(
"syscall"
: "=a" (ret)
: "0" (1), "S" (hello), "d" (hello_size), "D" (1), "m" (hello)
: "rcx", "r11", "memory", "cc"
);

This lets gcc do as much of the work as possible, which generally
produces better code. And are you sure rcx and r11 get clobbered? Seems
odd.

dw
Post by Daniel Kamil Kozar
#include <unistd.h>
int main(void)
{
const char hello[] = "Hello World!\n";
const size_t hello_size = sizeof(hello);
ssize_t ret;
asm
(
"movl $1, %%eax\n\t"
"movl $1, %%edi\n\t"
"movq %1, %%rsi\n\t"
"movl %2, %%edx\n\t"
"syscall"
: "=a"(ret)
: "g"(hello), "g"(hello_size)
: "%rdi", "%rsi", "%rdx", "%rcx", "%r11"
);
return 0;
}
Unfortunately, this snippet does not work anymore with gcc 4.9.1. An
inspection of gcc's result when run with -S shows that the "hello"
variable is not even created. Adding "static" to the variable's
declaration fixes the issue, however I'm still wondering what's wrong
with the original code and why gcc does not seem to see that the local
variable is actually used by the asm block.
Thanks,
-dkk
Daniel Kamil Kozar
2014-10-05 22:16:28 UTC
Permalink
Post by David Wohlferd
Well, as written, gcc discards the whole asm statement as unneeded when
using any optimization since none of the outputs (ie ret) get used and the
statement is not volatile.
I believe that adding "m" (hello) as an input will resolve your other
problem. Note that you may not easily see the string in the asm output
movabsq $8022916924116329800, %rax
movq %rax, 32(%rsp)
movl $10, %eax
movw %ax, 44(%rsp)
Also, I'd probably write this statement as something more like this
asm volatile
(
"syscall"
: "=a" (ret)
: "0" (1), "S" (hello), "d" (hello_size), "D" (1), "m" (hello)
: "rcx", "r11", "memory", "cc"
);
Thanks a lot! This version is certainly much better.
Post by David Wohlferd
This lets gcc do as much of the work as possible, which generally produces
better code. And are you sure rcx and r11 get clobbered? Seems odd.
Agreed. I'm positive about rcx and r11, since the ABI for Linux amd64
systems specifies that "The kernel destroys
registers %rcx and %r11.". Thus, I guess it's safer to put them on the
clobber list.
Post by David Wohlferd
dw
Thanks again,
dkk

Loading...