[英]Inline assembly in C program on x86_64 linux
I've built a short program written on C and inline assembly on my linux x86_64. 我在Linux x86_64上构建了一个用C和内联汇编编写的简短程序。 It is supposed to write a string to stdout.
应该将一个字符串写入stdout。 I found it in an article on the internet:
我在互联网上的一篇文章中找到了它:
int write_call( int fd, const char * str, int len ){
long __res;
__asm__ volatile ( "int $0x80":
"=a" (__res):"0"(__NR_write),"b"((long)(fd)),"c"((long)(str)),"d"((long)(len)) );
return (int) __res;
}
void do_write( void ){
char * str = "Paragon output string.\n";
int len = strlen( str ), n;
printf( "string for write length = %d\n", len );
n = write_call( 1, str, len );
printf( "write return : %d\n", n );
}
int main( int argc, char * argv[] ){
do_write();
return EXIT_SUCCESS;
}
But as I run it, it works incorrectly, making output 但是当我运行它时,它无法正常工作,导致输出
"write return : -14"
“写返回:-14”
If I build and run it on 32-bit linux it does what is expected. 如果我在32位linux上构建并运行它,它会完成预期的工作。
After some research I fount out that instruction "int $0x80" is a x86 instruction and truncates arguments in registers if called on x86_64. 经过一些研究,我发现指令“ int $ 0x80”是x86指令,如果在x86_64上调用,则会截断寄存器中的参数。
But I couldn't find a proper substitution of "int $0x80" for x86_64 architecture. 但是我找不到x86_64体系结构的“ int $ 0x80”的适当替代。 I have zero experience in assembly.
我的组装经验为零。
What should I put instead of "int $0x80" to receive expected result? 我应该用什么代替“ int $ 0x80”来获得预期的结果?
For amd64, you need to use "syscall" - and use different registers - instead of "int 0x80": 对于amd64,您需要使用“ syscall”-并使用不同的寄存器-而不是“ int 0x80”:
http://cs.lmu.edu/~ray/notes/linuxsyscalls/ http://cs.lmu.edu/~ray/notes/linuxsyscalls/
http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64 http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64
http://crypto.stanford.edu/~blynn/rop/ http://crypto.stanford.edu/~blynn/rop/
Here's a good example: 这是一个很好的例子:
How to invoke a system call via sysenter in inline assembly (x86/amd64 linux)? 如何通过内联汇编(x86 / amd64 linux)中的sysenter调用系统调用?
#include <unistd.h>
int main(void)
{
const char hello[] = "Hello World!\n";
const size_t hello_size = sizeof(hello);
ssize_t ret;
asm volatile
(
"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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.