The ARM back-end avoids marking a function as needing PIC in require_pic_register(). The code there checks if the current IR-type is IR_GIMPLE or not. The logic does not work when both -fPIC and -fstack-protector are given. The stack protector prologue is generated very early in RTL expansion when the IR-type is still IR_GIMPLE. Hence, the current code ends up using a PIC regiser never gets initialized properly.
--- extern int sprintf (char *, const char*, ...); void test (void) { char buf[10]; sprintf(buf, "aaaa"); } --- compiled using top-of-trunk on 6/9/2008 ../install/bin/arm-elf-gcc -O2 -S -fdump-rtl-expand -fpic -fstack-protector bug.c Note that vreg 133 is used in insn 4 but never assigned a value. ;; ;; Full RTL generated for this function: ;; (note 1 0 7 NOTE_INSN_DELETED) (note 7 1 10 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (insn 10 7 11 2 bug.c:7 (set (reg:SI 139) (unspec:SI [ (const:SI (minus:SI (const (symbol_ref:SI ("_GLOBAL_OFFSET_TABLE_"))) (const:SI (plus:SI (unspec:SI [ (const_int 0 [0x0]) ] 21) (const_int 8 [0x8]))))) ] 3)) -1 (nil)) (insn 11 10 12 2 bug.c:7 (set (reg:SI 139) (unspec:SI [ (plus:SI (reg:SI 139) (const (plus:SI (pc) (const_int 8 [0x8])))) (const_int 0 [0x0]) ] 4)) -1 (nil)) (insn 12 11 2 2 bug.c:7 (use (reg:SI 139)) -1 (nil)) (note 2 12 3 2 NOTE_INSN_FUNCTION_BEG) (insn 3 2 4 2 bug.c:5 (set (reg:SI 135) (unspec:SI [ (symbol_ref:SI ("__stack_chk_guard") [flags 0x40] <var_decl 0xf7ec6f78 __stack_chk_guard>) ] 3)) -1 (nil)) (insn 4 3 5 2 bug.c:5 (set (reg/f:SI 134) (mem/u/c:SI (plus:SI (reg:SI 133) (reg:SI 135)) [0 S4 A32])) -1 (expr_list:REG_EQUAL (symbol_ref:SI ("__stack_chk_guard") [flags 0x40] <var_decl 0xf7ec6f78 __stack_chk_guard>) (nil))) -- Summary: stack-protector causes bad ARM PIC code generated Product: gcc Version: 4.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: dougkwan at google dot com GCC build triplet: i686-unknown-linux-gnu GCC host triplet: i686-unknown-linux-gnu GCC target triplet: arm-unknown-elf http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36480