This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: linking error in gcc 4.1.0 when address of static function is assigned to function pointer and shared library link is attempted


Vladimir Vassilev writes:

 > In gcc 4.1.0 something changed so when the -O2 option is present 
 > addresses of static functions can not be assigned to pointer variables 
 > when compiling position independent code (-fPIC). Check the example 
 > below and tell me if this behavior is a feature or a bug.
 > 
 > So the code below  generates no error when built into  dynamic library 
 > when  the "static" specifier is commented.
 > With the "static" specifier uncommented there is no error only when the 
 > -O2 option is not present.
 > 
 > #include <stdio.h>
 > /*static*/ void dummy_func(void)
 > {
 >   printf("dummy_func\n");
 > }
 > void (*my_func_ptr)(void)=NULL;
 > int main(int argc, char** argv)
 > {
 >    my_func_ptr = dummy_func;
 >    my_func_ptr();
 >    return 0;
 > }
 > 
 > Simplified command line used for the test:
 > #compile
 > gcc -c -O2 -fPIC dummy.c
 > #link
 > gcc -shared -o dummy.so -fPIC dummy.o
 > 
 > #output if the above conditions are not met
 > /usr/bin/ld: dummy.o: relocation R_X86_64_PC32 against `dummy_func' can 
 > not be used when making a shared object; recompile with -fPIC
 > /usr/bin/ld: final link failed: Bad value
 > collect2: ld returned 1 exit status

WORKSFORME gcc version 4.1.1 x86_64-redhat-linux.

Try a more recent version; if that doesn't work, tell us more about your setup.

Andrew.

zorro:~ $ cat dummy.c
#include <stdio.h>
static void dummy_func(void)
{
  printf("dummy_func\n");
}
void (*my_func_ptr)(void)=NULL;
int main(int argc, char** argv)
{
   my_func_ptr = dummy_func;
   my_func_ptr();
   return 0;
}
zorro:~ $ /usr/bin/gcc -c -O2 -fPIC dummy.c
zorro:~ $ /usr/bin/gcc -shared -o dummy.so -fPIC dummy.o
zorro:~ $ /usr/bin/gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]