public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* linking error in gcc 4.1.0 when address of static function is assigned  to function pointer and shared library link is attempted
@ 2006-07-18  8:27 Vladimir Vassilev
  2006-07-18  8:49 ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir Vassilev @ 2006-07-18  8:27 UTC (permalink / raw)
  To: gcc-help

Hello,

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


Thanks,
Vladimir Vassilev

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: linking error in gcc 4.1.0 when address of static function is assigned  to function pointer and shared library link is attempted
  2006-07-18  8:27 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
@ 2006-07-18  8:49 ` Andrew Haley
  2006-07-18  9:52   ` Vladimir Vassilev
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Haley @ 2006-07-18  8:49 UTC (permalink / raw)
  To: Vladimir Vassilev; +Cc: gcc-help

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)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: linking error in gcc 4.1.0 when address of static function is  assigned  to function pointer and shared library link is attempted
  2006-07-18  8:49 ` Andrew Haley
@ 2006-07-18  9:52   ` Vladimir Vassilev
  2006-07-19 14:05     ` Vladimir Vassilev
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir Vassilev @ 2006-07-18  9:52 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: linking error in gcc 4.1.0 when address of static function is   assigned  to function pointer and shared library link is attempted
  2006-07-18  9:52   ` Vladimir Vassilev
@ 2006-07-19 14:05     ` Vladimir Vassilev
  0 siblings, 0 replies; 4+ messages in thread
From: Vladimir Vassilev @ 2006-07-19 14:05 UTC (permalink / raw)
  To: gcc-help

Hello,

The problem is still a valid issue. After looking around I figured out 
-fno-unit-at-a-time is needed to solve some problem with building linux 
modules. But this flag is only used for the gcc versions which support 
the option, but not for (and newer) version than gcc 4.0.0 although they 
also support it.

The excerpt below is from ./arch/i386/Makefile:

CFLAGS                          += $(shell if [ $(call cc-version) -lt 
0400 ] ;
then echo $(call cc-option,-fno-unit-at-a-time); fi ;)

So I guess there has to be a related cause for linux maintainers to stop 
using this option since 4.0.0 ... and probably it is the same with my 
case ?  ... Can you give me some clues?

Regards,
Vladimir

Vladimir Vassilev wrote:
> 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
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-07-19 14:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-18  8:27 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
2006-07-18  8:49 ` Andrew Haley
2006-07-18  9:52   ` Vladimir Vassilev
2006-07-19 14:05     ` Vladimir Vassilev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).