gcc 内联 asm 嵌入指向 .text 中的 .rodata 的指针,x86
我正在尝试使用内联汇编器在代码部分嵌入指向字符串的指针。但是 gcc 在符号名称的开头添加 $,导致链接错误。
这是一个最小的示例,
static const char str[] = "bar";
int main()
{
__asm__ __volatile__
(
"jmp 0f\n\t"
".long %0\n\t"
"0:"
:
: "i" ( str )
);
return 0;
}
使用 构建
gcc -Wall -save-temps test.c -o test
出现错误
test.o: In function `main':
test.c:(.text+0x6): undefined reference to `$str'
会在查看 .s 临时文件时 ,可以看到附加的 $ 前缀to str
.file "test.c"
.section .rodata
.type str, @object
.size str, 4
str:
.string "bar"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
#APP
# 4 "test.c" 1
jmp 0f
.long $str
0:
# 0 "" 2
#NO_APP
movl $0, %eax
leave
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits
认为我正在以正确的方式这样做,因为同样的方法适用于 ppc gcc,
<clip>
b 0f
.long str
0:
</clip>
那么,也许这只是“运气”,它适用于 ppc。问题是因为使用 AT&T 语法时 $ 用作立即数的前缀吗?
在这个简单的示例中,我可以通过在内联汇编器中硬编码符号名称“str”来解决该问题,但实际上需要它作为内联汇编器的输入约束。
有谁知道如何在 x86 目标上实现此功能吗?
谢谢,
——卢克
I'm trying to embed a pointer to a string in the code section using inline assembler. But gcc is adding a $ to the start of the symbol name, causing a link error.
Here is a minimal example,
static const char str[] = "bar";
int main()
{
__asm__ __volatile__
(
"jmp 0f\n\t"
".long %0\n\t"
"0:"
:
: "i" ( str )
);
return 0;
}
building with
gcc -Wall -save-temps test.c -o test
gives the error
test.o: In function `main':
test.c:(.text+0x6): undefined reference to `$str'
looking at the .s temp file, can see the additional $ prepended to str
.file "test.c"
.section .rodata
.type str, @object
.size str, 4
str:
.string "bar"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
#APP
# 4 "test.c" 1
jmp 0f
.long $str
0:
# 0 "" 2
#NO_APP
movl $0, %eax
leave
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits
Think i am doing this the correct way, as the same approach works on ppc gcc,
<clip>
b 0f
.long str
0:
</clip>
Then again, maybe it is just "luck" it works for ppc. Is the issue because $ is used as a prefix for immediates when using the AT&T synax ?
In this simple example, i can work around the issue by hardcoding the symbol name, "str", in the inline assembler, but really need it to be an input constraint to the inline assembler.
Does anyone have any ideas on how to get this working on x86 targets ?
Thanks,
- Luke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 clang 也会发生同样的情况,可能是因为代码生成器不知道操作数是在 .long 中使用的,而不是作为立即指令操作数。您的代码尝试如下:(
我必须删除 str 上的“静态”,因为编译器将其优化为不被引用。)
The same thing happens using clang, probably because the code generator doesn't know the operand is bing used in a .long rather than as an immediate instruction operand. You code try something like:
(I had to remove the "static" on str because the compiler optimized it out as not being referenced.)