home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!warwick!dcs.warwick.ac.uk!rince
- From: rince@dcs.warwick.ac.uk (James Bonfield)
- Subject: Re: Followup on size of executables.
- Message-ID: <1993Jan4.142121.29666@dcs.warwick.ac.uk>
- Sender: news@dcs.warwick.ac.uk (Network News)
- Nntp-Posting-Host: stone
- Organization: Department of Computer Science, Warwick University, England
- References: <93004.051358RVK@psuvm.psu.edu>
- Date: Mon, 4 Jan 1993 14:21:21 GMT
- Lines: 66
-
- In <93004.051358RVK@psuvm.psu.edu> <RVK@psuvm.psu.edu> writes:
-
- > This brings me to my final question: Is there no way other than
- > programming in assembly, to produce a really small executable?
- > I would like to use C, but I don't know how (if at all possible)
- > to do that.
-
- This all depends on your system architecture and the flags on your compiler
- and linker. It is perfectly possible (on a Sun anyway) to produce very small
- neat binaries from a C compiler, although you may lose some wanted things
- (such as read-only text segment and page aligned segments).
-
- $ cat hw.s
- .seg "text"
- .global _main
- ping:
- .ascii "Hello, World!\n"
- .align 4
- _main:
- mov 1,%o0 ! 1st arg of write
- sethi %hi(ping),%o1 ! 2nd arg
- mov 14,%o2 ! 3rd
- mov 0x4, %g1
- ta %g0 ! call write
- mov 0x1, %g1
- ta %g0 ! call exit
- $ as hw.s -o hw.o
- $ ld -s -N -e _main hw.o -o a.out
- $ ./a.out
- Hello, World!
- $ ls -l a.out
- -rwxr-xr-x 1 jkb staff 80 Jan 4 11:47 a.out
-
- (Astute readers will notice that changing the exec header and lopping off the
- last few bytes can result in a workable executable of size 74 - but this
- requires some intervention with a text editor and so is really a side issue.)
-
- In C it is also possible to create small binaries.
-
- $ cat b.c
- main() {
- write(1, "Hello, World!\n", 14);
- _exit(0);
- }
- $ cc -O4 -c b.c
- $ ld -s -n -e _main b.o -o a.out -lc
- $ ./a.out
- Hello, World!
- $ ls -l a.out
- -rwxr-xr-x 1 jkb staff 160 Jan 4 14:11 a.out
-
- Ok - so it's twice the size of the original program. This is of course a
- significant improvement on the 16K a normal compiled (and stripped) executable
- would be.
-
- This is all quite likely irrelevant to your case: as I said these things are
- system dependant. The reason this works here is that by using ld explicitely
- we can remove the need for dynamic linking and the inclusion of the crt0.o
- code. This of course will make binaries larger for big programs, and also has
- the side effect of not setting up our command line arguments (and environment
- pointer) correctly.
-
- --
- James Bonfield (jkb@mrc-lmba.cam.ac.uk / rince@dcs.warwick.ac.uk)
- Medical Research Council Laboratory of Molecular Biology, Hills Road,
- Cambridge, CB2 2QH, England. Tel: 0223 402499 Fax: 0223 412282
-