Description of problem: When we run gcc with "-O2", the following message is shown. ------------------------------------ /tmp/ccQQ3lnD.o(.text+0x10): In function `geta': : undefined reference to `a' collect2: ld returned 1 exit status ------------------------------------ Our test program is as follows.(smp.c) ------------------------------------ <smp.c> #include<stdio.h> typedef unsigned int UINT32; UINT32 geta(void); static UINT32 a=3; /* UINT32 a=5; */ UINT32 geta(void){ __asm ( "{alloc r19 = ar.pfs, 0,0,0,0;;\n\t" "nop.m 0\n\t" "nop.i 0};;\n\t" "{addl r18 = @ltoff(a#), r1\n\t" "nop.m 0\n\t" "nop.i 0};;\n\t" "{ld8 r18 = [r18]\n\t" "nop.m 0\n\t" "nop.i 0};;\n\t" "{ld4 r8 = [r18]\n\t" "nop.m 0\n\t" "nop.i 0};;\n\t" "br.ret.sptk.many b0;;\n\t" ); } main(){ UINT32 out; out = geta(); printf("%08X\n",out); } ------------------------------------ This compile error is caused by gcc's bug ? Or it's feature of option "-O2" ? Temporarily, we run "# gcc -O2 -fno-unit-at-a-time smp.c", in order to avoid the error. Our act is right ? [Having tried] (1) When we run gcc without option, there is no error. (# gcc smp.c) (2) We changed "static UINT32 a=3" in test program(smp.c) into "UINT32 a=5". There is no error. Version-Release number of selected component (if applicable): gcc-3.4.2-6.fc3 How reproducible: Always Steps to Reproduce: 1. # gcc -O2 smp.c Actual results: # gcc -O2 smp.c /tmp/ccQQ3lnD.o(.text+0x10): In function `geta': : undefined reference to `a' collect2: ld returned 1 exit status Expected results: (1) This compile error is caused by gcc's bug ? Or it's feature of option "-O2" ? ("a" is referred to only at inline assembler.) (2) When is corrected gcc released ? (if gcc is bug) (3) Our act is right ? (avoid error by option "-fno-unit-at-a-time") Additional info:
This is not a bug, but a new feature of GCC 3.4.x+. GCC without -fno-unit-at-a-time discards unused static variables and functions. The compiler doesn't know what inline assembly is doing, so you need to tell the compiler about it. There is a new attribute, __attribute__((used)) documented in info gcc: `used' This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly. and the same applies to variables.