【发布时间】:2011-02-25 19:00:27
【问题描述】:
有没有办法让 gcc 生成%pc 常量的相对地址?即使字符串出现在文本段中,arm-elf-gcc 也会生成一个指向数据的常量指针,通过%pc 相对地址加载指针的地址,然后取消引用它。由于各种原因,我需要跳过中间步骤。例如,这个简单的函数:
const char * filename(void)
{
static const char _filename[]
__attribute__((section(".text")))
= "logfile";
return _filename;
}
生成(使用 arm-elf-gcc-4.3.2 -nostdlib -c
-O3 -W -Wall logfile.c 编译时):
00000000 <filename>:
0: e59f0000 ldr r0, [pc, #0] ; 8 <filename+0x8>
4: e12fff1e bx lr
8: 0000000c .word 0x0000000c
0000000c <_filename.1175>:
c: 66676f6c .word 0x66676f6c
10: 00656c69 .word 0x00656c69
我原以为它会产生类似的东西:
filename:
add r0, pc, #0
bx lr
_filename.1175:
.ascii "logfile\000"
有问题的代码需要部分与位置无关,因为它会在加载时重新定位到内存中,但还要与未编译的代码集成-fPIC,因此没有全局偏移表。
我目前的工作是调用非内联函数(将通过%pc 相对地址完成)以使用类似于@ 987654329@代码有效:
static intptr_t
__attribute__((noinline))
find_offset( void )
{
uintptr_t pc;
asm __volatile__ (
"mov %0, %%pc" : "=&r"(pc)
);
return pc - 8 - (uintptr_t) find_offset;
}
但是这种技术需要手动修复所有个数据引用,所以上面例子中的filename()函数会变成:
const char * filename(void)
{
static const char _filename[]
__attribute__((section(".text")))
= "logfile";
return _filename + find_offset();
}
【问题讨论】:
标签: gcc arm fpic relative-addressing