【发布时间】:2011-08-19 23:55:05
【问题描述】:
当我看到一个 C 应用程序的汇编代码时,像这样:
emacs hello.c
clang -S -O hello.c -o hello.s
cat hello.s
函数名称以下划线为前缀(例如callq _printf)。为什么要这样做,有什么好处?
例子:
hello.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *myString = malloc(strlen("Hello, World!") + 1);
memcpy(myString, "Hello, World!", strlen("Hello, World!") + 1);
printf("%s", myString);
return 0;
}
hello.s
_main: ; Here
Leh_func_begin0:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
movl $14, %edi
callq _malloc ; Here
movabsq $6278066737626506568, %rcx
movq %rcx, (%rax)
movw $33, 12(%rax)
movl $1684828783, 8(%rax)
leaq L_.str1(%rip), %rdi
movq %rax, %rsi
xorb %al, %al
callq _printf ; Here
xorl %eax, %eax
popq %rbp
ret
Leh_func_end0:
【问题讨论】:
-
我很想知道这是哪个操作系统。我以为这种愚蠢的做法早就被抛弃了。也许是 Mac?
-
OSX 和其他一些 BSD 衍生产品是我所知道的唯一可能仍然这样做的类 unix 操作系统。 Linux 在大约 12 到 15 年前肯定放弃了它。
-
@R..:你是说a.out做了那个?
标签: c function assembly naming compilation