为什么不能在多个函数中定义相同的本地标签? [英] why cannot define same local label in multiple functions?
问题描述
想在多个功能中定义相同的本地标签:
Want to define same local label in multiple functions:
.text
.globl main
func:
push %rbp
mov %rsp, %rbp
.a:
leave
ret
main:
push %rbp
mov %rsp, %rbp
.a:
leave
ret
奇怪地得到错误:
$ clang -c main.s
main.s:13:1: error: invalid symbol redefinition
.a:
^
当我使用yasm时,它允许多个功能使用相同的本地标签. 你有什么线索吗?
When I was using yasm it allowed same local labels in multiple functions. Do you have any clues?
推荐答案
与NASM不同,.label
在气体语法中不是该函数(实际上在非.
标签之前)的局部变量.
Unlike NASM, .label
isn't local to the function (actually preceding non-.
label) in gas syntax.
.Llabel
是本地"符号名称,表示它不在符号表中.在整个文件中仍然可见,因此 GNU as
手册不会称它为本地标签.
.Llabel
is a "local" symbol name, meaning it doesn't go in the symbol table. It's still visible throughout the file, so the GNU as
manual doesn't call it a local label.
gas语法中有 个本地标签,但它们不在功能范围内. (请参见上面的链接).您必须使用前进/后退注释来引用它们,否则它们是数字常量而不是标签. (例如mov $1, %eax
将立即数1放入eax,而不是最新的1:
的地址.)
There are local labels in gas syntax, but they're not function scoped. (See the above link). You have to use forward/back annotations to references them, otherwise they're numeric constants instead of labels. (e.g. mov $1, %eax
puts a literal 1 into eax, not the address of the most recent 1:
).
更重要的是,您不能给它们提供有意义的名称,例如.Lcopy_loop
或.Linput_non_zero
.它们在宏定义内或在可能内联到多个地方或由优化程序以其他方式复制的内联汇编中很有用.否则,有意义的名称应该是首选.
More importantly, you can't give them meaningful names, like .Lcopy_loop
or .Linput_non_zero
. They're useful inside macro definitions, or in inline asm that might be inlined into multiple places or otherwise duplicated by the optimizer. Otherwise meaningful names should be preferred.
func1:
test
jcc 1f # you need the forward/back annotation, otherwise it's an absolute address to jump to.
...
1:
...
ret
func2:
test
# jcc 1b # BAD!!! jumps to 1: in func1, which is still in scope. This could bite you after moving some blocks around but missing the f/b annotations.
jcc 1f # good: will jump forward to the next definition of 1:
...
1:
...
ret
最好只写func1.a
或func2.a
.
在某些目标(不包括x86-64和i386)上,有有限范围的局部标签,可以避免意外跳转到标签的错误定义,但仍不能使用有意义的标签名称:请参阅<手册同一页上的strong>美元本地标签(上面的链接).
On some targets (not including x86-64 and i386), there are restricted-scope local labels that let you avoid accidentally jumping to the wrong definition of a label, but you still can't use meaningful label names: See Dollar Local Labels on the same page of the manual (link above).
1$:
是针对x86目标的gas和clang语法错误.
1$:
is a syntax error in gas and clang, for x86 targets.
这是不幸的,因为除非您在函数中使用任何带有有意义名称的标签(例如.Lmain_loop:
),否则它将是功能范围的.
That's unfortunate, because it would be function-scoped, unless you use any labels with meaningful names inside your functions (like .Lmain_loop:
).
这篇关于为什么不能在多个函数中定义相同的本地标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!