public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Using GCC to produce small executables (OSX)
@ 2010-02-14  4:46 Adrian Boeing
  2010-02-14 11:35 ` John Graham
  0 siblings, 1 reply; 4+ messages in thread
From: Adrian Boeing @ 2010-02-14  4:46 UTC (permalink / raw)
  To: gcc-help

Hi,

I am trying to compile a small program with the aim of reducing the
output file size on OSX:

int main(void) {
return 99;
}

I compile with:
gcc tiny.c
and run :
./a.out
echo $?
99

But:
ls -al a.out
Reveals a file of 8664 bytes. (A bit much! Strip reduces this only by
~100 bytes!)

Windows/MSVC does something similar, but if you disable the standard
libraries, you get a executable of 200 or so bytes.

I tried the same with GCC on OSX, but I get:
gcc tiny.c -nostdlib
ld: could not find entry point "start" (perhaps missing crt1.o)
collect2: ld returned 1 exit status

If I try and pass the entry point to LD, I get:
gcc tiny.c -c -nostdlib -m32
ld tiny.o -e main
ld: could not find entry point "main" (perhaps missing crt1.o) for
inferred architecture i386

How can you tell the linker what the entry point function is?
If anyone has any tips on how I can bring the file size down I would
be keen to hear it.

Thanks,
-Adrian

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

* Re: Using GCC to produce small executables (OSX)
  2010-02-14  4:46 Using GCC to produce small executables (OSX) Adrian Boeing
@ 2010-02-14 11:35 ` John Graham
  2010-02-14 15:44   ` Adrian Boeing
  0 siblings, 1 reply; 4+ messages in thread
From: John Graham @ 2010-02-14 11:35 UTC (permalink / raw)
  To: Adrian Boeing; +Cc: gcc-help

> If I try and pass the entry point to LD, I get:
> gcc tiny.c -c -nostdlib -m32
> ld tiny.o -e main
> ld: could not find entry point "main" (perhaps missing crt1.o) for
> inferred architecture i386

On my machine, that would work just fine - maybe gcc mangles function
names slightly on OSX, like prefixing with an '_'? Try running nm on
tiny.o and you'll see all its symbols - from your source, you should
only see one, the symbol marking the start of main(). Use the name of
that symbol as the -e argument to ld.

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

* Re: Using GCC to produce small executables (OSX)
  2010-02-14 11:35 ` John Graham
@ 2010-02-14 15:44   ` Adrian Boeing
  2010-02-25 17:07     ` Miles Bader
  0 siblings, 1 reply; 4+ messages in thread
From: Adrian Boeing @ 2010-02-14 15:44 UTC (permalink / raw)
  To: John Graham; +Cc: gcc-help

Hi John,

Thanks! You were infact correct, it does prefix with an '_'. This
produces an executable of only 4kb now. (a 50% saving)

When I try to execute the program I get a 'bus error', presumably
because the cleanup/startup code is not run.
I know this is getting a bit off the topic of GCC, but does anyone
know what the stdlib does / what I need to do to make the program
startup/cleanup nicely?

Thanks,
-Adrian

On Sun, Feb 14, 2010 at 4:59 PM, John Graham
<johngavingraham@googlemail.com> wrote:
>> If I try and pass the entry point to LD, I get:
>> gcc tiny.c -c -nostdlib -m32
>> ld tiny.o -e main
>> ld: could not find entry point "main" (perhaps missing crt1.o) for
>> inferred architecture i386
>
> On my machine, that would work just fine - maybe gcc mangles function
> names slightly on OSX, like prefixing with an '_'? Try running nm on
> tiny.o and you'll see all its symbols - from your source, you should
> only see one, the symbol marking the start of main(). Use the name of
> that symbol as the -e argument to ld.
>

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

* Re: Using GCC to produce small executables (OSX)
  2010-02-14 15:44   ` Adrian Boeing
@ 2010-02-25 17:07     ` Miles Bader
  0 siblings, 0 replies; 4+ messages in thread
From: Miles Bader @ 2010-02-25 17:07 UTC (permalink / raw)
  To: Adrian Boeing; +Cc: John Graham, gcc-help

Adrian Boeing <aboeing@gmail.com> writes:
> Thanks! You were infact correct, it does prefix with an '_'. This
> produces an executable of only 4kb now. (a 50% saving)
>
> When I try to execute the program I get a 'bus error', presumably
> because the cleanup/startup code is not run.
> I know this is getting a bit off the topic of GCC, but does anyone
> know what the stdlib does / what I need to do to make the program
> startup/cleanup nicely?

On debian, I can get it to work pretty well by doing:

  (1) Call "_exit" instead of returning from main

  (2) Use "-nostdlib" to avoid standard crt0 etc

  (3) Use "-Wl,-emain" to tell the linker to use "main" as the entry point

  (4) Explicitly link against libc (-nostdlib turns off the implicit
      link against it), to at least get the definition of _exit

  (5) Link statically with "-static"


Source file "small.c":

   #include <unistd.h>
   int main () { _exit (99); }


Compiling:

   $ gcc -O2 -static -o small small.c -Wl,-emain -nostdlib -lc
   $ ./small; echo $?
   99
   $ size small
      text    data     bss     dec     hex filename
       204       0       4     208      d0 small


-Miles

-- 
"Don't just question authority,
Don't forget to question me."
-- Jello Biafra

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

end of thread, other threads:[~2010-02-25 11:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-14  4:46 Using GCC to produce small executables (OSX) Adrian Boeing
2010-02-14 11:35 ` John Graham
2010-02-14 15:44   ` Adrian Boeing
2010-02-25 17:07     ` Miles Bader

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