ali hagigat | 4 Jan 06:11 2011
Picon

-ffreestanding option of GCC

What is the effect of -ffreestanding option? Is it like -fno-builtin exactly?

Andi Hellmund | 4 Jan 09:53 2011

Re: -ffreestanding option of GCC

On 01/04/2011 06:11 AM, ali hagigat wrote:
> What is the effect of -ffreestanding option? Is it like -fno-builtin exactly?
>
>    
Hey Ali,

here's a quote from the man page:

"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."

 From the code-level, the -ffreestanding option just sets -fno-hosted 
and the -fno-bulitin option.

Andi

ali hagigat | 5 Jan 05:32 2011
Picon

Re: -ffreestanding option of GCC

Hello Andi,
Please look at the following example and its output:
-----------------------------------------------------------
int func2(int a);
main(int r) {
	func2(4);
}

int func2(int a){
	return a*2;
}
-----------------------------------------------------------
/root/tmp3> gcc -v -ffreestanding -o prog1 prog1.c
Using built-in specs.
Target: i686-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap
--enable-shared --enable-threads=posix --enable-checking=release
--with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada
--enable-java-awt=gtk --disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic
--with-arch=i686 --build=i686-redhat-linux
Thread model: posix
gcc version 4.4.2 20091222 (Red Hat 4.4.2-20) (GCC)
(Continue reading)

Ian Lance Taylor | 5 Jan 17:50 2011
Picon

Re: -ffreestanding option of GCC

ali hagigat <hagigatali <at> gmail.com> writes:

> Although i specified compiling in a free standing environment but
> still libc.so, libgcc.so (-lc and -lgcc) and some other strange object
> files are being linked with my program like ccYT96w6.o, crt1.o,
> crtend.o...
> Why?
> Can i get rid of all of them some how?
> My interpretation of free standing was a program which does not depend
> on any external files, but why many files are here besides my file?

The -ffreestanding option directs the compiler to not assume that
standard functions have their usual definition, and to not assign any
special semantics to the function main.  It does not change the linking
behaviour.  You are looking for the -nostdlib option, or possibly
-nostartfiles or -nodefaultlibs.

Ian

ali hagigat | 16 Jan 09:28 2011
Picon

Re: -ffreestanding option of GCC

Thank you Ian for the reply. If -ffreestanding is not specified ,
compiler may optimize the code based on considerations? without making
sure if a function is built-in or it has been defined by a standard
library?
What are these modifications exactly?

On Wed, Jan 5, 2011 at 8:20 PM, Ian Lance Taylor <iant <at> google.com> wrote:
> ali hagigat <hagigatali <at> gmail.com> writes:
> The -ffreestanding option directs the compiler to not assume that
> standard functions have their usual definition, and to not assign any
> special semantics to the function main.  It does not change the linking
> behaviour.  You are looking for the -nostdlib option, or possibly
> -nostartfiles or -nodefaultlibs.
>
> Ian
>

ali hagigat | 16 Jan 09:39 2011
Picon

Re: -ffreestanding option of GCC

-ffreestanding
A freestanding environment is one in which the standard
library may not exist, and program startup may not necessarily be at main.
--------------------------------------------------------------------------------
I have copied part of gcc manual. The fact that program startup may
not necessarily be at main, is not a special feature which is created
by -ffreestanding option! If we do not specify any option or use
-fhosted, program startup may not necessarily be at main too!!
Why the manual mentions the issue?

> On Wed, Jan 5, 2011 at 8:20 PM, Ian Lance Taylor <iant <at> google.com> wrote:
>> ali hagigat <hagigatali <at> gmail.com> writes:
>> The -ffreestanding option directs the compiler to not assume that
>> standard functions have their usual definition, and to not assign any
>> special semantics to the function main.  It does not change the linking
>> behaviour.  You are looking for the -nostdlib option, or possibly
>> -nostartfiles or -nodefaultlibs.
>>
>> Ian
>>
>

Ian Lance Taylor | 17 Jan 19:28 2011
Picon

Re: -ffreestanding option of GCC

ali hagigat <hagigatali <at> gmail.com> writes:

> Thank you Ian for the reply. If -ffreestanding is not specified ,
> compiler may optimize the code based on considerations? without making
> sure if a function is built-in or it has been defined by a standard
> library?
> What are these modifications exactly?

The optimizations which gcc implements in the default hosted environment
are fairly extensive.  In general, in a hosted environment, gcc can
assume that there is a complete library available that meets the
conditions of the language standard being used (the default for C is
gnu90, the GNU extension to the ISO C90 standard).  That means, for
example, it can optimize printf("hi\n") into puts("hi").  There are many
such optimizations.  When -ffreestanding is used, gcc does not assume
that you are using a standard library environment.

> -ffreestanding
> A freestanding environment is one in which the standard
> library may not exist, and program startup may not necessarily be at main.
> --------------------------------------------------------------------------------
> I have copied part of gcc manual. The fact that program startup may
> not necessarily be at main, is not a special feature which is created
> by -ffreestanding option! If we do not specify any option or use
> -fhosted, program startup may not necessarily be at main too!!
> Why the manual mentions the issue?

Your statement is incorrect.  The C90 standard specifies that a program
starts at main, and on some (but not most) targets gcc does take
advantage of that fact to run certain initialization code at that time.
(Continue reading)


Gmane