why cannot define same local label in multiple functions?

前端 未结 1 384
清酒与你
清酒与你 2020-12-21 18:28

Want to define same local label in multiple functions:

    .text
    .globl main
func:
    push %rbp
    mov %rsp, %rbp
.a:
    leave
    ret

main:
    push         


        
相关标签:
1条回答
  • 2020-12-21 19:08

    Unlike NASM, .label isn't local to the function (actually preceding non-. label) in gas syntax.

    .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.


    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:).

    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
    

    It may be better to just write func1.a or func2.a.


    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$: is a syntax error in gas and clang, for x86 targets.

    That's unfortunate, because it would be function-scoped, unless you use any labels with meaningful names inside your functions (like .Lmain_loop:).

    0 讨论(0)
提交回复
热议问题