Creating% pc relative persistent data address

Is there a way for gcc to generate %pc relative constant addresses? Even when a line appears in the text segment, arm-elf-gcc generates a constant pointer to the data, loads the pointer address through the relative address %pc , and then plays it out. For various reasons, I need to skip the middle step. For example, this simple function:

 const char * filename(void) { static const char _filename[] __attribute__((section(".text"))) = "logfile"; return _filename; } 

generates (when compiling with 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 

I would expect it to generate something more:

 filename: add r0, pc, #0 bx lr _filename.1175: .ascii "logfile\000" 

This code should be partially position-independent, as it will be moved to memory at boot time, but also integrated with code that was not compiled by -fPIC , so there is no global offset table.

My current job is to call the non-inline function (which will be done using the relative address %pc ) to find the offset from the compiled location in a method similar to how -fPIC code works:

 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; } 

But this method requires that the links to all data be fixed manually, so the filename() function in the above example will become:

 const char * filename(void) { static const char _filename[] __attribute__((section(".text"))) = "logfile"; return _filename + find_offset(); } 
+6
gcc arm fpic
source share
1 answer

Hmmm, maybe you need to compile it as -fPIC to get the pic. Or just write it in assembler, assembler is much simpler than the C you write.

  00000000:
    0: e59f300c ldr r3, [pc, # 12];  14 
    4: e59f000c ldr r0, [pc, # 12];  eighteen 
    8: e08f3003 add r3, pc, r3
    c: e0830000 add r0, r3, r0
   10: e12fff1e bx lr
   14: 00000004 andeq r0, r0, r4
   18: 00000000 andeq r0, r0, r0

 0000001c:
   1c: 66676f6c strbtvs r6, [r7], -ip, ror # 30
   20: 00656c69 rsbeq r6, r5, r9, ror # 24

Are you getting the same warning as me?

  /tmp/ccySyaUE.s: Assembler messages:
 /tmp/ccySyaUE.s:35: Warning: ignoring changed section attributes for .text
+1
source share

All Articles