gcc中为变量和函数指定汇编时名称

在gcc中可以通过 asm() 或者 __asm__() 为变量和函数指定其在汇编代码中的名称

int foo asm ("myfoo") = 2;

int func (int x, int y) asm ("MYFUNC");

int func (int x, int y)
{
/* … */
}

如下所示,如果不指定的话,变量 foo 的汇编时名称是: foo.1724,

$ cat ./main.c
int main(void) {
    static int foo = 5 ;

    return 0;
}
$ gcc -S main.c -o main.S
$ cat main.S
...
foo.1724:
    .long 5
    .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
    .section .note.GNU-stack,"",@progbits
...

如下所示,指定之后,变量 foo 的汇编时名称是:myfoo,

$ cat ./main.c
int main(void) {
    static int foo asm("myfoo") = 5 ;

    return 0;
}

$ gcc -S main.c -o main.S
$ cat main.S
...
myfoo:
    .long 5
    .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
    .section .note.GNU-stack,"",@progbits
...

猜你喜欢

转载自blog.csdn.net/choumin/article/details/112968903