When i have local data defined inside a function, it gets placed into .rodata section. This means that .text section will contain relative reference (//1) to an absolute address (//2) inside the .text, which in turn points to the data (//3) inside .rodata.
int function() { int a[] = {97, 98, 99, 100, 101, 103}; }
// Undesirable:
00000000 <.text.function>:
/---/
6: 4b07 ldr r3, [pc, #28] ; (24 <function+0x24>) //1
/---/
24: 00000000 .word 0x00000000 //2 absolute address of .rodata
00000000 <.rodata>: // absolute address will be replaced by linker
0: 00000061 .word 0x00000061 //3
4: 00000062 .word 0x00000062
8: 00000063 .word 0x00000063
c: 00000064 .word 0x00000064
10: 00000065 .word 0x00000065
14: 00000067 .word 0x00000067
I understand that keeping data and code separate is admirable in general, but in the case of my embedded system I want to have local data and function code entangled. I want that the [pc, #28] relative pointer would point directly to the data location and the array values should be directly in the .text section.
I can do something similar using linkerscript but the absolute address will still be in there, and I don't want the function .text section to contain any absolute addresses.
// Undesirable:
.text.function : {
*(.text.function)
filename.o(.rodata)
}
00000000 <.text.function>:
/---/
6: 4b07 ldr r3, [pc, #28] ; (24 <function+0x24>)
/---/
24: 00000000 .word 0x00000028 // address of next line
28: 00000061 .word 0x00000061
/---/
Are there any compiler flags or other methods to force gcc to include data alongside the code and skip this additional indirection?
edit:
To further clarify what I am asking, I'd like to point out some solutions, that won't work. Adding static const
to the array will force it to be stored inside .rodata section and is therefore the exact opposite of what I want. Adding __attribute__((section(".text")))
won't work because this question is about local variables and local variables can't have section attributes. Unfortunately -fpic
or -fpie
don't seem to work either, instead those depend on runtime linker to replace absolute addresses.
User contributions licensed under CC BY-SA 3.0