Andrew Haley wrote:
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)
Hello Andrew,
My bad. I have been too fast in my conclusion and overlooked some
parameters. Actually the problem requires three command line parameters
'-O2', '-fno-unit-at-a-time' and '-finline-functions'. If either of
those is not added everything works fine.:
gcc -c -O2 -fno-unit-at-a-time -finline-functions -fPIC dummy.c
Then linking fails with:
gcc -O2 -shared -o dummy.so -fPIC dummy.o
I am looking into why '-fno-unit-at-a-time' was needed and if I can
remove its use in case this option is no longer needed.
Regards,
Vladimir