gpt4 book ai didi

c - 为什么GAS汇编语言可以不声明直接调用C语言的全局变量?

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:03 36 4
gpt4 key购买 nike

这几天在学习linux启动代码,有个小问题困扰着我。

setup.c :

unsigned int __atags_pointer __initdata;

head-common.S

    .align  2
.type __mmap_switched_data, %object
__mmap_switched_data:
.long __data_loc @ r4
.long _sdata @ r5
.long __bss_start @ r6
.long _end @ r7
.long processor_id @ r4
.long __machine_arch_type @ r5
.long __atags_pointer @ r6
.long cr_alignment @ r7
.long init_thread_union + THREAD_START_SP @ sp
.size __mmap_switched_data, . - __mmap_switched_data

head-common.S中的.long __atags_pointer@r6行调用了setup.c中的全局变量,没有任何声明,例如 .global __atags_pointer。为什么它有效?

最佳答案

我不这么认为:

.long  __atags_pointer

是一个电话。这是一个伪表达式。在这种情况下,我认为它会创建一个 4 字节的空间,并为链接器留下一个标记,以便在链接时用值填充该空间。来自气体手册:

'.long' is the same as '.int'. *Note '.int': Int.

7.44 '.int EXPRESSIONS'

Expect zero or more EXPRESSIONS, of any section, separated by commas. For each expression, emit a number that, at run time, is the value of that expression. The byte order and bit size of the number depends on what kind of target the assembly is for.

如果你单独编译文件得到一个.o,然后在上面运行objdump -D,我相信你会看到空格全是0。但是,在您链接之后,同一空间中将有一个实际数字。

关于c - 为什么GAS汇编语言可以不声明直接调用C语言的全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38431864/

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