编译器以下划线为前缀的函数名称的原因是什么? [英] What is the reason function names are prefixed with an underscore by the compiler?

查看:13
本文介绍了编译器以下划线为前缀的函数名称的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到一个 C 应用程序的汇编代码时,像这样:

emacs hello.c铛 -S -O hello.c -o hello.s猫你好

函数名称以下划线为前缀(例如 callq _printf).为什么要这样做,有什么好处?

<小时>

示例:

hello.c

#include #include #include int main() {char *myString = malloc(strlen("Hello, World!") + 1);memcpy(myString, "Hello, World!", strlen("Hello, World!") + 1);printf("%s", myString);返回0;}

hello.s

_main: ;这里Leh_func_begin0:pushq %rbpLtmp0:movq %rsp, %rbpLtmp1:movl $14, %edicallq _malloc ;这里movabsq $6278066737626506568,%rcxmovq %rcx, (%rax)movw $33, 12(%rax)移动 $1684828783, 8(%rax)leaq L_.str1(%rip), %rdimovq %rax, %rsixorb %al, %alcallq _printf ;这里xorl %eax, %eaxpopq %rbp退Leh_func_end0:

解决方案

来自 链接器和加载器:

<块引用>

大约在 1974 年,UNIX 被 C 重写时,它的作者已经拥有大量的汇编语言库,而且修改新的 C 和 C 兼容代码的名称比返回并修复所有现有代码更容易代码.现在,20 年后,汇编代码都被重写了五次,UNIX C 编译器,尤其是那些创建 COFF 和 ELF 目标文件的编译器,不再在前面加上下划线.

在 C 编译的汇编结果中预置下划线只是作为变通方法出现的名称修改约定.它一直存在(据我所知)没有特别的原因,现在已经进入 Clang.

在汇编之外,C 标准库通常具有以下划线前缀的实现定义的函数,以传达神奇的概念,并且不要将这一点告诉偶然发现它们的普通程序员.>

When I see the assembly code of a C app, like this:

emacs hello.c
clang -S -O hello.c -o hello.s
cat hello.s

Function names are prefixed with an underscore (e.g. callq _printf). Why is this done and what advantages does it have?


Example:

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:

解决方案

From Linkers and Loaders:

At the time that UNIX was rewritten in C in about 1974, its authors already had extensive assember language libraries, and it was easier to mangle the names of new C and C-compatible code than to go back and fix all the existing code. Now, 20 years later, the assembler code has all been rewritten five times, and UNIX C compilers, particularly ones that create COFF and ELF object files, no longer prepend the underscore.

Prepending an underscore in the assembly results of C compilation is just a name-mangling convention that arose as a workaround. It stuck around for (as far as I know) no particular reason, and has now made its way into Clang.

Outside of assembly, the C standard library often has implementation-defined functions prefixed with an underscore to convey notions of magicalness and don't touch this to the ordinary programmers that stumble across them.

这篇关于编译器以下划线为前缀的函数名称的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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