/*
when using an external function pointer cast as an interrupt in a if (&function
== 0) statement the compiler assume the pointer is non-null and optimize out
the if right away even tho the symbol could indeed be null and optimizations
are disabled.
this is a big problem when values are set as symbol addresses in the linker
script.
gcc and g++ give the same result
.cpp and .c extentions give the same result
all targets I've tested (i386, arm, powerpc) give the same result
Linux shockenhull 2.6.16.13-4-smp #1 SMP Wed May 3 04:53:23 UTC 2006 i686
athlon i386 GNU/Linux
GCC versions I tried:
i686-pc-linux-gnu-gcc (GCC) 4.1.2
gcc (GCC) 4.1.0 (SUSE Linux)
arm-unknown-elf-gcc (GCC) 4.1.1
ppc-unknown-elf-gcc (GCC) 4.1.1
i386-unknown-elf-gcc (GCC) 4.1.1
*/
//------------if-bug-function-pointers.cpp-----------
extern void SomeLinkerScriptDefinedSymbol(void);
#define SOMEVALUE ((int)&SomeLinkerScriptDefinedSymbol)
int main()
{
#ifdef USETEMP
// this works fine
int i = SOMEVALUE;
if(i == 0){
return 0;
} else {
return -1;
}
#else
// this incorrectly assumes SOMEVALUE != 0
if(SOMEVALUE == 0){
return 0;
} else {
return -1;
}
#endif
}
//----------------------------------------
/*
[EMAIL PROTECTED]:~/tmp> i686-pc-linux-gnu-gcc -v -save-temps -O0 -c
if-bug-function-pointers.c -o if-bug-function-pointers.o
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/home/shockenhull/bin/gcc-4.1.2
Thread model: posix
gcc version 4.1.2
/home/shockenhull/bin/gcc-4.1.2/libexec/gcc/i686-pc-linux-gnu/4.1.2/cc1 -E
-quiet -v if-bug-function-pointers.c -mtune=pentiumpro -O0 -fpch-preprocess -o
if-bug-function-pointers.i
ignoring nonexistent directory
"/home/shockenhull/bin/gcc-4.1.2/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/home/shockenhull/bin/gcc-4.1.2/include
/home/shockenhull/bin/gcc-4.1.2/lib/gcc/i686-pc-linux-gnu/4.1.2/include
/usr/include
End of search list.
/home/shockenhull/bin/gcc-4.1.2/libexec/gcc/i686-pc-linux-gnu/4.1.2/cc1
-fpreprocessed if-bug-function-pointers.i -quiet -dumpbase
if-bug-function-pointers.c -mtune=pentiumpro -auxbase-strip
if-bug-function-pointers.o -O0 -version -o if-bug-function-pointers.s
GNU C version 4.1.2 (i686-pc-linux-gnu)
compiled by GNU C version 4.1.0 (SUSE Linux).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ee7023030d57847fe562209522d89579
as -V -Qy -o if-bug-function-pointers.o if-bug-function-pointers.s
GNU assembler version 2.16.91.0.5 (i586-suse-linux) using BFD version
2.16.91.0.5 20051219 (SUSE Linux)
[EMAIL PROTECTED]:~/tmp>
----------------------------------------
*/
--
Summary: incorrect output on external symbol address cast as
integer used in conditional statement
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sh-list at ssc-studios dot com
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: any
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31236