Compile following code with options -mthumb -fpic -Os, int foo(int j) { static int i; int t = i; i = j; return t; }
GCC generates: foo: ldr r3, .L2 // A ldr r2, .L2+4 // B .LPIC0: add r3, pc // C add r3, r3, r2 // D ldr r2, [r3] @ sp needed for prologue str r0, [r3] mov r0, r2 bx lr .L3: .align 2 .L2: .word _GLOBAL_OFFSET_TABLE_-(.LPIC0+4) .word .LANCHOR0(GOTOFF) ".word _GLOBAL_OFFSET_TABLE_-(.LPIC0+4)" is the GOT table address relative to instruction C, ".word .LANCHOR0(GOTOFF)" is offset of symbol .LANCHOR0 relative to GOT table. So firstly instruction AC computes the GOT table address, then instruciton BD add the offset of variable i relative to GOT table. Actually it is not necessary to compute the GOT address. The better code can be: foo: ldr r3, .L2 // E .LPIC0: add r3, pc // F ldr r2, [r3] @ sp needed for prologue str r0, [r3] mov r0, r2 bx lr .L3: .align 2 .L2: .word .LANCHOR0-(.LPIC0+4) //offset of i relative to instruction F Now it has two less instructions, 1 less memory access and smaller constant pool. -- Summary: Simplify code to address function static variables with option -fpic Product: gcc Version: 4.5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: carrot at google dot com GCC build triplet: i686-linux GCC host triplet: i686-linux GCC target triplet: arm-eabi http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42601