Bug 16470

Summary: violation of `-ffreestanding' rules?
Product: gcc Reporter: Pawel Sikora <pawel_sikora>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: enhancement CC: ebotcazou, gcc-bugs, malitzke, qboosh
Priority: P2    
Version: 3.3.4   
Target Milestone: ---   
Host: sparc-pld-linux Target: sparc-pld-linux
Build: sparc-pld-linux Known to work:
Known to fail: 3.3.4 3.3.5 3.4.1 3.4.2 4.0.0 Last reconfirmed: 2004-07-11 20:22:25

Description Pawel Sikora 2004-07-10 21:32:55 UTC
 -ffreestanding 
 
   Assert that compilation takes place in a freestanding environment. 
   This implies -fno-builtin.  A freestanding environment is one in 
   which the standard library may not exist, and program startup may 
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
   not necessarily be at "main".  The most obvious example is an OS 
   kernel.  This is equivalent to -fno-hosted. 
 
$ cat switch64.c 
 
#include "/usr/src/linux/include/linux/types.h" 
__u64 test(__u64 val) 
{ 
    switch (val) 
    { 
        case 0: 
            return 0; 
            break; 
        case 1: 
            return 1; 
            break; 
        default: 
            return -1; 
    } 
} 
 
$ gcc switch64.c -c -ffreestanding 
$ objdump -t switch64.o 
 
switch64.o:     file format elf32-sparc 
 
SYMBOL TABLE: 
00000000 l    df *ABS*  00000000 switch64.c 
00000000 l    d  .text  00000000 
00000000 l    d  .data  00000000 
00000000 l    d  .bss   00000000 
00000000 l    d  .note.GNU-stack        00000000 
00000000 l    d  .comment       00000000 
00000000         *UND*  00000000 __ucmpdi2 
                                 ^^^^^^^^^ 
00000000 g     F .text  0000009c test 
 
$ objdump -T /lib/libgcc_s.so|grep __uc 
0000181c g    DF .text  00000038  GCC_3.0     __ucmpdi2 
 
$ ldd /lib/libgcc_s.so 
        libc.so.6 => /lib/libc.so.6 (0x70028000) 
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x08000000) 
 
In the kernel sources such situations happen very often. 
In the case of __ucmpdi2, it appears to be a combination of kernel 
and compiler stupidity - there's no reason why __ucmpdi2 should 
not be done inline by the compiler, but at the same time there 
is probably also little reason to use a slow "long long" comparison 
in the kernel. Of course I can force a static linking with libgcc.a 
but in the above case gcc should report an error or do inline and warn user.
Comment 1 Eric Botcazou 2004-07-11 20:22:25 UTC
While I confirm that the undefined symbol __ucmpdi2 is emitted by the compiler,
I'm a bit skeptical about your interpretation of '-ffreestanding': __ucmpdi2 is
provided by libgcc.a, not by the standard library.  So it is possible to build a
standalone executable containing the chunk of code with the compiler only.
Comment 2 Pawel Sikora 2004-07-11 20:51:51 UTC
(In reply to comment #1) 
> While I confirm that the undefined symbol __ucmpdi2 is emitted by the 
compiler, 
> I'm a bit skeptical about your interpretation of '-ffreestanding': __ucmpdi2 
is 
> provided by libgcc.a, not by the standard library. 
 
by libgcc_s.so (which is linked with standard library) too. 
 
> So it is possible to build a standalone executable (...) 
 
with libgcc.a - yes. 
with libgcc_s.so - no. 
 
 
Comment 3 Andreas Schwab 2004-07-11 22:41:56 UTC
A freestanding implementation won't have a shared libgcc.  Mind you that 
GNU/Linux is _not_ a freestanding implementation. 
Comment 4 Andrew Pinski 2004-07-12 04:12:51 UTC
Sorry but no these are not a violation of `-ffreestanding' at all, since otherwise there is no way to 
support 64bit in 32bit targets at all or some other builtins which are done like __builtin_ctlz and such.
So this is invalid.
Comment 5 Andrew Pinski 2004-07-12 04:13:42 UTC
Note you can use -static-libgcc to get the static libgcc.
Comment 6 Andrew Pinski 2007-06-25 16:45:08 UTC
*** Bug 32501 has been marked as a duplicate of this bug. ***
Comment 7 Andrew Pinski 2007-06-25 16:54:10 UTC
*** Bug 32501 has been marked as a duplicate of this bug. ***