为什么glibc库使用汇编 [英] Why does glibc library use assembly

查看:67
本文介绍了为什么glibc库使用汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此页面: https://sys.readthedocs.io/en/latest/doc/01_introduction.html
这将解释glibc如何进行系统调用.
在其中一个示例中,对代码进行了检查,结果表明,汇编语言编写了glibc要做的最后一条指令实际上做系统调用(意味着对CPU的中断)...那么为什么在汇编中加入glibc?通过在装配中编写一小部分零件是否有某种优势?
另外,在运行时共享库​​已经被编译为机器代码正确了吗?
那么,为什么在编译之前使用两种不同的语言会有什么优势呢?谢谢.

I am looking at this page: https://sys.readthedocs.io/en/latest/doc/01_introduction.html
that goes into explanation about how glibc does system calls.
In one of the examples the code is examined and it is shown, that the last instruction glibc does to actually do a system call (meaning the interrupt to the cpu) is written in assembly.... So why is part of glibc in assembly? Is there some sort of advantage by writing that small part in assembly?
Also, the shared libraries during runtime are already compiled to machine code correct?
So why would there be any advantage using two different languages before compilation? Thank you.

推荐答案

答案非常简单-因为C不涵盖系统调用(因为它通常不涵盖任何物理硬件,并且更喜欢用对于抽象机器而言),没有C构造 glibc 可以用来执行系统调用.

The answer is super simple - since C doesn't cover system calls (because it doesn't cover any physical hardware in general, and prefers to express itself in terms of abstract machine), there is no C construct glibc can use to perform system call.

有人认为编译器可以提供某种内在的功能,但是由于在Linux中glibc实际上是编译器工具套件的一部分(也包含CRT),因此glibc确实不需要它工作.

One could argue that compiler could provide a sort of intrinsic to do that, but since in Linux glibc is actually part of the compiler suit of tools (in contains CRT as well) there is really no need for it, glibc can do the job.

最后,但并非最不重要的一点,在现代CPU中,系统调用通常不是中断.而是一条特定的指令(x86_64中的 syscall ).

Also, last, but not the least, in modern CPUs syscall is usually not an interrupt. Instead, it's a specific instruction (syscall in x86_64).

这篇关于为什么glibc库使用汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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