Daniel Kamil Kozar
2014-10-05 18:13:46 UTC
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
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