How to disable placing local constants to .rodata for ARM

Mere Loom mereloom@gmail.com
Sat Mar 3 21:28:00 GMT 2018


Hi,

I want to compile (and statically pre-link) a single function so that
it would be position independent without requiring run-time
modifications by dynamic linker. I have mostly achieved it:
-mlong-calls forces outgoing function calls to use absolute addresses,
while local branches like loops and conditionals use pc-relative
branches.

However, local constants get placed to .rodata section and referenced
by absolute memory addresses. Even when using -fpic, code uses
pc-relative expressions to access .got, but .got still contains
absolute memory addresses to .rodata. I want code to access local
constants directly using pc-relative expressions.


Are there any compiler flags or other methods to force gcc to access
local constants using pc-relative expressions (/ disable .rodata /
include local data in .text)?


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 so that I could move some functions
around in memory unchanged and still call them. Local data can be
either directly in the code where it is used (like gcc currently does
with very short local constants, char-s for example), or it can be
after the code (like .got).

Example: When i have local data defined inside a function, the .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. This structure remains even when I tell linker to put
.text.function and corresponding .rodata into single output section.

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, currently not linked
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 am using arm-none-eabi-gcc to cross-compile for cortex-m7 thumb.



More information about the Gcc-help mailing list