这是什么原因函数名与编译器下划线pfixed $ P $? [英] What is the reason function names are prefixed with an underscore by the compiler?

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

问题描述

当我看到一个C程序的组装code,是这样的:

 的hello.c的emacs
铛-S -O的hello.c -o hello.s
猫hello.s

函数名称以下划线pfixed $ P $(例如 callq _printf )。这是为什么做,它有什么优势呢?


例如:

的hello.c

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
诠释主(){
  字符* MyString的=的malloc(strlen的()+ 1你好,世界!);
  的memcpy(MyString的,你好,世界!,strlen的()+ 1你好,世界!);
  的printf(%S,myString的);
  返回0;
}

hello.s

  _main:;这里
Leh_func_begin0:
    pushq%RBP
Ltmp0:
    MOVQ%RSP,RBP%
Ltmp1:
    MOVL $ 14%EDI
    callq _malloc;这里
    movabsq $六二七八〇六六七三七六二六五〇六五六八,RCX%
    MOVQ%RCX,(RAX%)
    MOVW $ 33 12(%RAX)
    MOVL $十六亿八千四百八十二万八千七百八十三,8(RAX%)
    leaq L_.str1(%RIP),%RDI
    MOVQ%RAX,RSI%
    xorb%人,%人
    callq _printf;这里
    xorl%EAX,EAX%
    popq%RBP
    RET
Leh_func_end0:


解决方案

链接器和加载的:


  

在那个UNIX是在大约1974年重写C中的时候,它的作者已经进行了广泛的装配台语言库,它是更容易裂伤新的C和C兼容code中的名称,而不是回去和修复所有现有的code。如今,20年过去了,汇编code已经全部重写五次,UNIX C编译器,创造COFF和ELF目标文件特别是那些不再prePEND下划线。


$ P $用C编写的组装结果ppending下划线只是一个名字,压延约定出现作为一种解决方法。它坚持周围(据我所知)没有特别的理由,现在已经做了它的方式进入锵。

组装以外,C标准库往往已实现自定义函数$ P $以下划线pfixed传达magicalness的概念和不要碰这个的到他们对面的绊倒普通的程序员

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.

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

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