是否有可能为C转换为ASM没有在Linux上链接的libc? [英] Is it possible to convert C to asm without link libc on Linux?

查看:196
本文介绍了是否有可能为C转换为ASM没有在Linux上链接的libc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试平台是Linux 32位。 (但在Windows的某些解决方案,32位也欢迎)

Test platform is on Linux 32 bit. (But certain solution on windows 32 bit is also welcome)

下面是一个C code片断:

Here is a c code snippet:

int a = 0;
printf("%d\n", a);

如果我使用gcc来生成汇编code

And if I use gcc to generate assembly code

gcc -S test.c

然后我将获得:

      movl    $0, 28(%esp)
      movl    28(%esp), %eax
      movl    %eax, 4(%esp)
      movl    $.LC0, (%esp)
      call    printf
      leave
      ret

和本次大会code需要连接到libc工作(因为调用的printf)

And this assembly code needs linking to libc to work(because of the call printf)

我的问题是:

是否有可能把C的,只有明确使用的系统调用自动以ASM,不使用的libc?

Is it possible to convert C to asm with only explicit using system call automatically, without using libc?

这样的:

    pop ecx        
    add ecx,host_msg-host_reloc
    mov eax,4
    mov ebx,1
     mov edx,host_msg_len
    int 80h
    mov eax,1
     xor ebx,ebx
     int 80h

直接调用INT 80H软件中断。

Directly call the int 80h software interrupt.

这可能吗?如果是这样,是否有任何关于此问题的工具?

Is it possible? If so, is there any tool on this issue?

感谢您!

推荐答案

无法从源头code。调用到的printf()不能被编译器转换为在系统调用, - 对的printf()库函数包含逻辑的显著量这是不是在系统调用present(如处理格式字符串和转换整数和浮点数为字符串)。

Not from that source code. A call to printf() cannot be converted by the compiler to a call to the write system call - the printf() library function contains a significant amount of logic which is not present in the system call (such as processing the format string and converting integer and floating-point numbers to strings).

有可能生成系统直接调用,而只能通过使用内联组件。例如,生成对调用_exit(0)(不太一样退出()!),你可以这样写:

It is possible to generate system calls directly, but only by using inline assembly. For instance, to generate a call to _exit(0) (not quite the same as exit()!), you would write:

#include <asm/unistd.h>
...
int retval;
asm("int $0x80" : "=a" (retval) : "a" (__NR_exit), "b" (0));

有关GCC内联汇编的更多信息,特别是在我使用这里变量映射到寄存器的制约,请阅读的 GCC内联汇编HOWTO 。这是比较旧,但仍然完全相关的。

For more information on GCC inline assembly, particularly on the constraints I'm using here to map variables to registers, please read the GCC Inline Assembly HOWTO. It's rather old, but still perfectly relevant.

请注意,这样做就是不建议即可。系统调用的确切调用约定(如,哪些寄存器用于呼叫号码和参数,如何返回的错误,的的)都在不同的体系结构不同,操作系统,甚至在32位和64位x86。写作code这种方式将使其很难保持。

Note that doing this is not recommended. The exact calling conventions for system calls (e.g, which registers are used for the call number and arguments, how errors are returned, etc) are different on different architectures, operating systems, and even between 32-bit and 64-bit x86. Writing code this way will make it very difficult to maintain.

这篇关于是否有可能为C转换为ASM没有在Linux上链接的libc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆