home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19180 < prev    next >
Encoding:
Text File  |  1993-01-04  |  2.8 KB  |  79 lines

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