gpt4 book ai didi

linux - glibc 中这段代码段的目的是什么

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:46 24 4
gpt4 key购买 nike

我试图从 tls.h in glibc 中理解以下代码段的内容正在做以及为什么:

/* Macros to load from and store into segment registers.  */
# define TLS_GET_FS() \
({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })

我想我理解了将存储在 fs 寄存器中的值移动到 __seg 的基本操作。但是,我有一些问题:

  1. 我的理解是 fs 只有 16 位。它是否正确?当值移动到四字内存位置时会发生什么?这是否意味着高位被设置为 0?

  2. 更重要的是,我认为在段开头声明的变量 __seg 的范围仅限于该段。那么 __seg 有什么用呢?我确信 glibc 的作者这样做有充分的理由,但我无法通过查看源代码来弄清楚它是什么。

我尝试为此代码生成程序集并得到以下结果?

 #APP
# 13 "fs-test.cpp" 1
movl %fs, %eax
# 0 "" 2
#NO_APP

因此在我的例子中,eax 似乎用于 __seg。但我不知道这是否总是会发生,或者这是否只是我编译的小测试文件中发生的事情。如果它总是要使用 eax 为什么不以这种方式编写程序集?如果编译器可能会选择其他寄存器,那么由于 __seg 在宏结束时超出范围,程序员将如何知道要访问哪个寄存器?最后,当我在 glibc 源代码中 grepping 时,我没有看到这个宏在任何地方被使用,所以这进一步增加了我对它的目的是什么的困惑。对代码的作用和原因的任何解释表示赞赏。

最佳答案

My understanding is the fs is only 16-bits. Is this correct? What happens when the value gets moved to a quadword memory location? Does this mean the upper bits get set to 0?

是的。

the variable __seg that gets declared at the start of the segment is limited to this segment. So how is __seg useful?

您必须阅读 GCC statement-expression extension .语句表达式的值是其中最后一个表达式的值。末尾的 __seg; 将毫无用处,除非有人将它分配给其他东西,如下所示:

int foo = TLS_GET_FS();

Finally I did not see this macro used anywhere when I grepped for it in the glibc source code

TLS_{GET,SET}_FS 实际上似乎没有被使用。它们可能在某些版本中使用过,然后在删除引用它们的代码时意外遗留下来。

关于linux - glibc 中这段代码段的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10092738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com