  The Linux GCC HOWTO

  Daniel Barlow <dan@detached.demon.co.uk>
  v1.17, 28 February 1996

    GNU C Ϸ  ̺귯  󿡼 
  ¾ϴ  ٷ ִ. ׸  󿡼 , ŷ,
  ,   ϴ Ͽ   Ѵ.
  κ  Mitch D'Souza GCC-FAQκ ؿ ̸ (
  κ üߴ.)  ELF-HOWTOκ͵  ؿ ̴. (̰͵
   κ ٲ  ̴.)   ù°  ̴. (
  ȣ RCS  峭 ̴.)  ǰ ȯѴ.

  1.  ϴ 

  1.1.  ELF vs. a.out

     翡 Ӿ ȭ   ִ. 
  ؼ,  鿡  ؾ ϴ ˰ ִ ̳ʸ
  ٷ  2   ִ.  ý  Ǿ ִ
       ִ.

  2   ϴ°?  file̶ ϴ ƿƼ ϸ ȴ.
  ELFα׷ ؼ ELF  ¼ ¼  ̸, a.out
  α׷ ؼ Linux/i386̶ ܾ   
  ̴.

    ̴  Ĺݺο  ̴. ELF  ο ȭ
  ̸, Ϲ  پٰ  ִ.

  1.2.  å(Admistrata)

  ۱ǿ õ     ϶. , Ĺݺο
    а, Usenet ٺ ( ʴ GCC ׸
  ǥϴ ) ø      ̴.

  2.  ʿ  𿡼   ִ°?

  2.1.    

     Ͽ   ø ϳ̴.   
  Ͽ  Ǿ ִ ̶  ִ.  
  <http://sunsite.unc.edu/pub/linux/docs/HOWTO/>   ٷ
  װ̴.  HTML   <http://ftp.linux.org.uk/~barlow/howto/gcc-
  howto.html> ã   ణ    𸥴.

  2.2.  ٸ 

  gcc     ҽ  ȭϿ ִ. texinfo ȭ,
  .infoȭ  ִ. Ʈũ ӵ ٰų, õҿ
   ְų, Ǵ γ  ٰ   װ untar 
  Ŀ ش ȭ /usr/info丮 īϵ .  ٸ
  tsx-11 <ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/>  ڷḦ
  ãƺ. ׻ ֽ  ִ  ƴ ̴.

  libc    2  ִ. GNU  libc  쿡 info ȭϵ
   ִµ stdio κ   ڼ  libc  ؼ
  ˷ְ ִ.  <ftp://sunsite.unc.edu/pub/Linux/docs/> 
   ִµ ý ȣ(system call  2),  libc Լ( 3)
     ϰ ִ.

  2.3.  GCC

     ִ.

  (a)  GCC   
  <ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/> ̳ʸ ·
    ִ.  ̹ ϵǾ ִ  Ѵ.    ִ
    ֽ  2.7.2 μ ȭϸ gcc-2.7.2.bin.tar.gz̴.
  (:   ۼñⰡ 96⵵ ϶!)

  (b) FSFκ ֽ ҽ  GNU α׷  GNU archives
  <ftp://prep.ai.mit.edu/pub/gnu/>   ִ. ҽ  ׻
  Ĺ ̳ʸ    ƴϴ.  configure ũƮ
  ̿ؼ     ִ.  tsx-11
  <ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/>  캸
  . ġȭ ʿ  𸣱 ̴.

   ̵ ̶  ϱ ؼ  ʿϴ.

  2.4.  C ̺귯  ȭϵ

  ⼭ п ʿ  ϴ (1) ý a.out ΰ?
  (2) ƴϸ   ִ 쿡  ߿  ϰ ?  
  ޶.   libc 4   libc 5  ׷̵Ϸ Ѵٸ
  켱 ELF-HOWTO  ̴.

  tsx-11 <ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/>  
  ִ.

     libc-5.2.18.bin.tar.gz
        --- ELF  ̺귯 ̹,  ̺귯 ׸ C
        ̺귯  ̺귯  ȭϵ

     libc-5.2.18.tar.gz
        ---  ̺귯  ҽ.   ȭ ؾ ϱ
          ִ ̳ʸǵ ʿϴ. ռ  
         ƴϸ ׳ ̳ʸ    
        ϴ. ̳ʸ ϶!   NYS ε н
           ռ ϴ  ۿ .

     libc-4.7.5.bin.tar.gz
        --- a.out  ̺귯 ̹,  ̺귯(C Լ,  
        Լ),  ִ libc 5    ְԲ εǾ ִ.
          a.out α׷   ְų Ϸ
          ʿϴ.

  2.5.  õ  (as, ld, ar, strings )

  tsx-11 <ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/>  
  ,   binutils-2.6.x.x.bin.tar.gz̴.

  ̳ʸ ƿƼ  ELF  ִٴ ǿ . 
  ̺귯 ELF θ ߵǰ  a.out ̺귯 ELF  
    ǹִٰ Ѵ. C ̺귯  ELF θ ǰ
  , a.out ؾ Ŀٶ   ٸ ׿  
  .

  3.  GCCġ 

  3.1.  GCC 

     gcc   ˰  쿡 gcc -v 
  Ʈ Ű ȴ.  ̷   
  ý ELF õǾ ִ ƴϸ a.out  Ǿ ִ Ȯϰ
  ˾Ƴ  ִ.  ýۿ   ´.

       $ gcc -v
       Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
       gcc version 2.7.2

  ⼭ ˾Ƶξ  ٽ   .

  o  i486.  ̴  486 μ  ϵ gcc ϰ
     ִٴ ̴.  κ ٸ  ִµ   386, 586 
      ٸ  ִ.   3  Ĩ ϵ ͵
        ȴ.  ̶ Ѵٸ 486 ڵ尡 𿣰
     ν 486     ȴٴ ̴. 386 
     ϴµ ذ ȴٰų  ʴ´.  ణ ̳ʸ Ŀ
     ̴.

  o  box.  ̰  ߿ κ ƴϴ.   box  ſ
     slackware  debian  ܾ ü  ְ ƿ  κ
       ִ.  i486-linux ̷  ̴.   gcc 
     ؼ Ѵٸ   i486-box-linux  ߵ
     gcc     ִ.

  o  linux.   ܾ ſ linuxelf 簡 linuxaout̶ ܾ
       ִ. Ǵ  Ŀ     ִ.
     ư   Ÿ ִ.   ڸ, 
     ܾ   GCC   ٸ ؼȴ.

  o  2.7.0 ̻  ׳ linux̸ ELF  ǹϰ a.out
     linuxaout   ̸ ´.

  o   ELF  ư鼭 ̸ linux зٰ  
     ִ.   2.7.0  Ͽ linuxaout ̶  ãƺ 
      ̴.

  o  linuxelf ̸  ̴. gcc  2.6.3  ELF
     ȭ  ؼ  ̸̴. gcc 2.6.3  ELF
     ȭ µ װ ִٰ ˷ ִ. ׷̵ϱ
     ٶ.

  o  2.7.2 ̰  ȣ̴.

   غ ڴ  ELF ڵ带 Ű gcc 2.7.2 
   ִٴ ̴.

  3.2.  ü  gcc   ִ°ǰ?

  ׳ ƹ  gcc  ġ߰ų  ġ  ڵ
  ġϰ ߴٸ, ü  ȭ ý 󿡼  ġϴ
  ˰  ̴.   ̷.

  o  /usr/lib/gcc-lib/target/version/ (׸   丮)
     Ϸ κ ġϴ ̴.   ϴ ȭ
      ü gcc    ̺귯 ȭϵ ִ.

  o  /usr/bin/gcc Ϸ (Compiler Driver) Ѵ.  Ŀǵ
     󿡼 gcc  Ѵ.    Ϸ 
     ִٸ   Բ   ִ. gcc  ϰ  Ʈ
      Ϸ ˾Ƴ ؼ gcc -v غ ȴ. ٸ
       ϰ Ϸ gcc -V version ̷ 
     ϸ ȴ.  ...

       # gcc -v
       Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
       gcc version 2.7.2
       # gcc -V 2.6.3 -v
       Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.6.3/specs
       gcc driver version 2.7.2 executing gcc version 2.6.3

  o  /usr/target/(bin|lib|include)/.    ǥ 
      ִٸ (ϴ ELFΰ a.out ΰ Ǵ   ũν
     Ϸ ) Ʈ ǥ Ŀ ƴ ̺귯, ̳ʸ
     ƿƼ (as, ld ...),  ȭϵ鵵 ãƺ   ̴.
        gcc   ִ ϴ ſ  ͵ 
     丮  Ȯ  ִ. ׷ ʴٸ Ƹ
     /usr/(bin|lib|include)  ̴.

  o  /lib/,/usr/lib ׸ Ÿ ̺귯 丮 ⺻ ý
      ̺귯 丮̴.     α׷
     Ͽ /lib/cpp  ־ Ѵ. (X    ϰ
     ִ.) /usr/lib/gcc-lib/target/version/ ִ cpp  īس?
     ƴϸ ɺ ũ ش.

  3.3.   ȭϵ  ִ°?

   ռ /usr/local/include ġ ͵   3 
  ߿  丮 ִ.

  o  κ/usr/include/  Ϻ 丮  H J Lu  libc
     ̳ʸ ǿ ؼ ȴ. ⼭  "κ"̶
     ǥ µ,   ٸ ҽ (  curses,  dbm
     ̺귯)  ȭϵ鵵 ֱ ̴. Ư ֱ libc
       ׷ ȭϵ . (  ޷
     )

  o  /usr/include/linux /usr/include/asm(<linux/*.h>ȭϰ <asm/*.h>
      Ǵ ȭϵ ִ )  Ŀ ҽ
     linux/include/linux linux/include/asm Ű ɺ ũ
     Ѵ.  ̶ ū ۾ Ϸ Ѵٸ и ġؾ Ѵ.
     Ŀ  ϱ ؼ ִ  ƴϴ.

      Ŀ ҽ Ǯ  make config ۾ ־ 
     ̴.   ȭϵ   ؼ ܳ
     <linux/autoconf.h> ȭϿ ϱ ̴. ׸  
     Ŀο asm ̶ ϴ  ɺ ũ , make config 
       찡 ִ.

     asm   asm-i386 ũǾ ִ.    ӽſ
     ȭϸ ־  asm  ־  
     ǻϰ Ƽ÷ ü ư ֱ ̴. asm-
     i386 asm-alpha, asm-generic, asm-m68k, asm-mips, asm-ppc,
     asm-sparc  ȭ 丮 ִ  ߰  ִ.

      /usr/src/linux ϴ 丮 ̹ ҽ
     ǮҴٸ...

       $ cd /usr/src/linux
       $ su
       # make config
       [answer the questions.  Unless you're going to go on and build the kernel
       it doesn't matter _too_ much what you say]
       # cd /usr/include
       # ln -s ../src/linux/include/linux .
       # ln -s ../src/linux/include/asm .

  o  <float.h>, <limits.h>, <varargs.h>, <stdarg.h> ׸ <stddef.h>
      ȭϵ Ϸ  ٸ ̴. ׸ ׵
     /usr/lib/gcc-lib/i486-box-linux/2.7.2/include/ ġϰ ִ.

  3.4.  ũν Ϸ(Cross Compiler) 

  3.4.1.  ǥ ÷μ 

    gcc ҽ ڵ带  ִٰ ϰڴ.  GCC 
   INSTALL ȭϿ ϴ   ȴ.  configure
  --target=i486-linux --host=XXX ̷  ִµ, XXX ÷
  Ѵ.  make  ġ ȴ.  ȭ, Ŀ
  ȭ ʿϸ, ũν Ϸ ũν Ŀ  ؼ
  ʿϴ.  <ftp://tsx-11.mit.edu/pub/linux/packages/GCC/>  
  ִ.

  3.4.2.  ҽ ÷μ , ǥ ÷μ MSDOS

  . ҽ  ۼ ڿ  ư α׷
  ϱ ؼ emx Ű go extender  ʿ Ѵ.
  <ftp://sunsite.unc.edu/pub/Linux/devel/msdos>   ȭ
  ãƺ ٶ.

  μ ׽Ʈغ  , ϴٰ ܾϱ .

  4.  ð ϸ

  4.1.  ڵ ǵǴ ɺ

     ִ  gcc -v ɼ ν 
  ɺ ڵ ϴ ˾Ƴ  ִ.    
   .

       $ echo 'main(){printf("hello world\n");}' | gcc -E -v -
       Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
       gcc version 2.7.2
        /usr/lib/gcc-lib/i486-box-linux/2.7.2/cpp -lang-c -v -undef
       -D__GNUC__=2 -D__GNUC_MINOR__=7 -D__ELF__ -Dunix -Di386 -Dlinux
       -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__i386
       -D__linux -Asystem(unix) -Asystem(posix) -Acpu(i386)
       -Amachine(i386) -D__i486__ -

    ڵ尡  Ǵ ڵ,   ִ
   .

       #ifdef __linux__
       /* ... funky stuff ... */
       #endif /* linux */

  __linux__ ̸ ϶. linux ƴϴ. ڰ ǵǾ ֱ
   POSIX ԰ݿ  ʱ ̴.

  4.2.  Ϸ θ

  Ϸ ġ鿡   gcc info   ȴ. (
  Emacs ϰ ִٸ C-h i׸  gcc ɼ ϶) 
   ִ    gcc info  ־ ʾ 
  ְ, Ǵ       ִ.   
  <ftp://prep.ai.mit.edu/pub/gnu> Ǵ ̷ Ʈ  gcc ҽ
  ڵ带 ޾ƿ ̴.  ׸  ҽ ȿ īؿ´.

  gcc   (gcc.1) Ϲ ô뿡 ڶ ִٰ 
   ִ.   ϸ ׷     ִ.

  4.2.1.  Ϸ ÷(flag)

  gcc  , -On(⼭ n   , ص ȴ)
  Ŀǵ  ɼ ־ָ  ڵ尡 ȭȴ. ⼭ Ǵ
  n  ߿  ǹ̸   gcc   ٸ,
  Ϲ 0 (ȭ ) ؼ 2(  ȭ),
  3(־  ȭ) δ.
   gcc  ɼ -f -m ̶ ɼǵ ٲپ óϰ
  ȴ. -O Ư   ǹ̸  ؼ gcc ÿ -v
  -Q(ȭ ʾ)÷׸ ٿν Ȯ  ִ.   
  -O2   Ÿ. (鸶  ٸ  ִ)

       enabled: -fdefer-pop -fcse-follow-jumps -fcse-skip-blocks
       -fexpensive-optimizations
                -fthread-jumps -fpeephole -fforce-mem -ffunction-cse -finline
                -fcaller-saves -fpcc-struct-return -frerun-cse-after-loop
                -fcommon -fgnu-linker -m80387 -mhard-float -mno-soft-float
                -mno-386 -m486 -mieee-fp -mfp-ret-in-387

   Ϸ ϰ ִ ȭ  ū ڸ Ѵٸ
  (  -O6),  Ϸ ϴ   ȭش.
  ̷  ϵǵ õǾ ִ ڵ带 ϴ   
   ƴ  .   ȭ   gcc  ٸ,
  ߸ϸ  ҽ ڵ尡 ϰ ϵǴ  ִ.

     -O3 ְ ̶ Ͽ -O6 ߴٰ
  ġ.   (  2.7.3.?) -O8 ϰ
  ȴٸ -O6   ǹ̸   ִ.

  gcc  2.7.0  2.7.2  ڵ -O2 ȭ ÷׿ װ
  ִٴ   ˾Ƶα ٶ. Strength Reduction̶ ϴ 
   ۵ ʴ´.   ذ  ִ ġ ְ ٽ gcc
   ؾ Ұ̴. Ǵ  -fno-strength-reduce  ɼ
  ְ ϱ ٶ.

  4.2.1.1.  μ ɼ

  -O ɼ ־ ڵ ۵ ʴ -m ÷׵ ִ.  
  ̵  ϴ. ߿ δ -m386 -m486 ִ.  
  ÷׵ gcc  386, 486  Ϳ  缭 
   ˷ִ ̴. -m486 Ͽٰ ϴ 386 
  Ǵµ . ׷  ʿ. 486 ڵ尡  
  ũ 386  ų  ʴ´.

   -mpentium̳ -m586   .  (Linus) 486
  ڵƼ ڵ带 鼭 Ƽ  ʴ Ĺİ
  Ŀٶ   ڵ带  ؼ, -m486 -malign-loops=2
  -malign-jumps=2 -malign-functions=2   ϰ ִ.
  Michael Meissner(Cygnus ִ)   ϰ ִ.

        δ -mno-strength-reduce    x86 
         ڵ带   ִٴ ̴. (!  
       strength reduction ׿ ؼ ϰ ִ  ƴϴ.
       װ  ٸ ̴) ֳϸ x86 ټ 
       ڰ  ̴. (׸ ٸ Ϳ Ͽ
       ͵ ׷  spill   óϴ
       GCC  ó    ʴ´) StrengthReduc
       tion    üϱ Ͽ ٸ
       ͵ ϰ ȴ. -fcaller-saves  ̷
         ʳ ϰ ִ.

   ٸ  ̷. -fomit-frame-pointer   
  ְ, ׷   ִٴ ̴.  δ  ٸ
  Ͱ Ҵ簡ϴٴ  ǹ  ְ, ٸ 
  δ x86  (instruction) Ͽ ڵϴ
  μ,   ּҰ   ּҺٵ 
    Ѵٴ  ǹϱ⵵ Ѵ. ̷ Ǹ
  α׷   ִ Icache ణ پ. 
  -fomit-frame-pointer Ϸ  ȣ Ŀ
   ͸ ؾ Ѵٴ  Ѵ.  
   ,   ȣ⸸ε    ȴ.

      ϰ ִ.

         ȭ ȿ Ѵٸ,   .
        ׽Ʈ غ Ѵ. gcc Ϸ ɼ 
       . ׸   Ư    ȭ ̷
       ̴.

  4.2.2.  Internal compiler error: cc1 got fatal signal 11

  ñ׳ 11 SIGSEGV,  ׸̼ ݿ  ñ׳̴.
  Ϲ α׷ ͸ ߸ ٴ ̰ų ڱⰡ ϰ
    ޸𸮿  ۾ Ϸ   ߻Ѵ. ׷ ̴
  gcc   ִ.

   gcc κ ۾ ſ ̰ ׽  ģ
  Ʈ  ϶. gcc   ڷ  ͸
  û  ϰ ִ.  ڸ  Ʈ
  ߿  پ  ׽ α׷(RAM Tester)̶  
  ִ.  Ź   ߴ ġ ٸٸ ̴ 
  κ  ϵ   ȴ. (CPU, ޸, 峪
  ĳ)  ǻͰ Ŀ  üŷ ļ  õǾ ׸
    ͵  ưٰ ؼ װ gcc ׷ 
  . ̷  ǹϴ. ׸ Ŀ ϸ鼭 make
  zImage  ٰ ؼ gcc ׶   . make
  zImage  200 ̻ ȭ ϰ ִ. װͺٴ  
  츦 ãƺ .

    װ Ȱ Ÿ ڱ׸ α׷ Ͽ
  ׷ϴٸ, FSFٰ  Ʈ ص ǰ, Ǵ linux-gcc ϸ
  Ʈ  ÷ ȴ. ׷ ؼ 켱 gcc  о
    ʿ   ϱ ٶ.

  4.3.  (Portability)

     Ʈ  õ  ٸ  Ʈ
  ġ  α׷̶ Ѵ. :-)

  ϰ ڸ, Ϲ  100% POSIX ȣȯ ̷
  ؼ  ణ ۾ ʿϴ.   make  ϸ
  ȭ   ֵ ϱ Ͽ ڵ ڿ 
  ڵ带  ̾߸ ġִ ̴.

  4.3.1.  BSDisms ( bsd_ioctl , daemon  ׸ <sgtty.h> )

    α׷ -I/usr/include/bsd ־  ,
  -lbsd ɼ ְ ũ  ִ. ( Makefile ȿ
  -I/usr/include/bsd CFLAGS  ְ, -lbsd LDFLAGS ν)
   BSD Ÿ ñ׳ ൿ  ؼ -D__USE_BSD_SIGNAL
   ʿ䰡 . ֳϸ -I/usr/include/bsd ְ
  <signal.h> ҽ ȿ ϸ    ̷.

  4.3.2.   ñ׳ε ( SIGBUS , SIGEMT , SIGIOT , SIGTRAP , SIGSYS
  )

   POSIX ؼϰ ִ. ̷ ñ׳ε POSIX 
  ñ׳ε ƴϴ. ̴ ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990),
  paragraph B.3.3.1.1    ϰ ִ ̴.

       SIGBUS, SIGEMT, SIGIOT, SIGTRAP, ׸ SIGSYS 
       ñ׳ε POSIX.1κ ܵǾ. ֳϸ ׵
       ൿ ̰  θĿ  ٸ 
       ϰ ȭų  . ̷ ñ׳ε ֹ
        Ծ ؼ  ,   ñ׳ε
       عȴ ؼ ȭؾ Ѵ. ׸ 
       ñ׳ε  ó ΰ ؼ ƹ 
        .

    ذ  ִ    ̷ ñ׳ε 
  SIGUNUSED ϴ ̴. ٸ  ̷ ñ׳
  óϴ κ #ifdef  Ἥ óϵ ϴ ̴.

       #ifdef SIGSYS
       /* ... POSIX  ƴ SIGSYS ڵ尡 ⿡ ´ .... */
       #endif

  4.3.3.  K & R ڵ

  GCC ANSI Ϸ̴.    ڵ ANSI ƴϴ. ̷
   Ϸ ÷׿ -traditional ̶ ٿָ ȴٰ  
  ִ.   Ӱ ۾  ϴ κе  ִ. gcc info
   캸 ٶ.

  -traditional ɼ gcc  ̿Ϸ ϴ C   ٲٴ 
   ٸ ȿ ϰ ִ.    ɼ -fwritable-
  strings ۵Űµ, ڿ  Ÿ   
  Ѵ. (ؽƮ ,  ׵     Ѵ) ̷ 
  α׷ ޸ (footprint) ϰ ȴ.

  4.3.4.  ó ɺ ڵ Ÿ԰ 浹 

   ߻ϴ  ߿ ϳ ٷ  Լ ̹ 
  ȭϵ鿡 ũη ǵǾ ְ óⰡ ڵ  
  ŸԿ Ͽ ó źθ ϴ ̴.  atoi() atol()
  찡 .

  4.3.5.  sprintf()

  sprintf(string, fmt, ...)  н ýۿ ڿ 
  ͸ ȯϴ ݸ鿡 ANSI   ڿ Ե 
   ȯѴ. ̴ Ư SunOS  κ ϴ 쿡
   ؾ Ѵ.

  4.3.6.  FD_*   ͵?  fcntl    ༮. ü Ǻκ
   ִ°?

  <sys/time.h> ִ.  fcntl ̿ϰ Ѵٸ  Ÿ
  Ͽ <unistd.h>  ԽŰ  ̴.

  Ϲ ڸ  Լ    SYNOPSYS κп
   ȭ #include ؾϴ ڼ Ÿְ  װ
  ϱ ٶ.

  4.3.7.  select()  ŸӾƿ ɸ α׷  ٸ⸸
  Ѵ.

   select()  ŸӾƿ ĶͰ бθ
  Ǿ. ׸       ־.

       select() Ƹ   ִ ð ν
       ࿡ ׷  ߻Ѵٸ  ŸӾƿ 
       ð ȯؾ  ̴.     
        ̴.  Ÿ ƿ Ͱ select() ȣ⿡
       Ͽ   ̶ ϴ  ٶ
       ϴ.

  ٷ   Դ! ּ װ ̷ ִ. select()ȣκ
  ƿ , ŸӾƿ μ Ͱ  ʴ´ٸ ٸ ߴ
  ܷ ð õȴ.  ƹ ͵  ʾҾٸ  
  0(zero) Ǿ ̴. ׸  ŸӾƿ ü  ȣ
  ϰ Ǹ ȣ  ǵƿ ̴.

    ذϱ ؼ ŸӾƿ  Ź select() ȣ
    ü ־ Ѵ.   ڵ尡 ִٸ,

             struct timeval timeout;
             timeout.tv_sec = 1; timeout.tv_usec = 0;
             while (some_condition)
                   select(n,readfds,writefds,exceptfds,&timeout);

  Ʒ  ٲٵ ϶.

             struct timeval timeout;
             while (some_condition) {
                   timeout.tv_sec = 1; timeout.tv_usec = 0;
                   select(n,readfds,writefds,exceptfds,&timeout);
             }

  (Mosaic)     ̷  ߾.
  ȸϴ  ִϸ̼ ӵ Ʈũ   ڷ
  ӵ ݺϴ   ̴!

  4.3.8.  ý ȣ ͷƮ 

  4.3.8.1.  :

  α׷ Ctrl+Z ǰ ٽ ۵Ǿ . Ǵ ٸ
   Ctrl+C  ñ׳ ߻Ű ڽ μ δ
  ... "interrupted system calls" Ǵ "write: unknown error" Ǵ
  ׷    .

  4.3.8.2.  :

  POSIX ý ٸ  н ü ణ   ñ׳ο
  ؼ üŷ Ѵ.  ñ׳ ڵ鷯(signal handler)
  ų ̴.

  o  Ÿ̸Ӱ °  񵿱.

  o   ý ȣ ȯÿ.

  o  ׸   ý ȣ ȿ ׷ϴ: select(),
     pause(), connect(), accept(), ͹̳ 󿡼 read(), ,
       , FIFO  open(), PTY ø ,
     ͹̳ο  ioctl(), F_SETLKW   fcntl(), wait4(),
     syslog(),  TCP Ǵ NFS ۾

  ٸ ü 쿡   ý ȣ⿡ ؼ üũ
  ̴.     ̿ܿ   ý ȣ: creat(),
  close(), getmsg(), putmsg(), msgrcv(), msgsnd(), recv(), send(),
  wait(), waitpid(), wait3(), tcdrain(), sigpause(), semop()

   ñ׳(α׷ ڵ鷯 ν ) ý ȣ ߿
  ߻Ѵٸ, ׿  ڵ鷯 ȣȴ. ׸ ڵ鷯 ȯǸ
  (ý ȣ), ý ȣ ߰ ä⸦ ߴ 캸
   -1   ȯȴ. ׸errno  EINTR  Ѵ.
  α׷ ׷   ̶  ϰ ״ ̴.

    2  ذå ߿ ϳ  ȴ.

  (1)  ġ  ñ׳ ڵ鷯 Ͽ SA_RESTART sigaction
  ÷׿ ÷Ѵ.    ִٸ,

         signal (sig_nr, my_signal_handler);

     ٲ۴.

         signal (sig_nr, my_signal_handler);
         { struct sigaction sa;
           sigaction (sig_nr, (struct sigaction *)0, &sa);
       #ifdef SA_RESTART
           sa.sa_flags |= SA_RESTART;
       #endif
       #ifdef SA_INTERRUPT
           sa.sa_flags &= ~ SA_INTERRUPT;
       #endif
           sigaction (sig_nr, &sa, (struct sigaction *)0);
         }

    κ ý ȣ⿡ Ǳ , read(), write(),
  ioctl(), select(), pause(), connect() ؼ   EINTR
  üũ־ Ѵ.  캸.

  (2)    EINTR üũش.

  read() ϴ ڵ尡  ̷ Ǿ ִٰ ġ.

       int result;
       while (len > 0) {
         result = read(fd,buffer,len);
         if (result < 0) break;
         buffer += result; len -= result;
       }

   ڵ带   ٲپָ ȴ.

       int result;
       while (len > 0) {
         result = read(fd,buffer,len);
         if (result < 0) { if (errno != EINTR) break; }
         else { buffer += result; len -= result; }
       }

  ̹ ̷ ڵ尡 ִٸ,

       int result;
       result = ioctl(fd,cmd,addr);

  װ    ٲ Ѵ.

       int result;
       do { result = ioctl(fd,cmd,addr); }
       while ((result == -1) && (errno == EINTR));

  BSD н   ý ȣ 簳ϴ  ⺻ ൿ
  Ǿ ִ 쵵 Ƿ . ý ȣ ä⸦ ϱ
  ؼ SV_INTERRUPT Ǵ SA_INTERRUPT ÷׸ ϵ .

  4.3.9.    ڿ (α׷ ϰ Ʈ )

  GCC gcc ϴ  ڿ  Ͽ Ȯ μ
    ̶ ϰ ִ  ϴ.   ڿ 
  α׷ ؽƮ  ִ´. ̷ ν  
  ϴ  ƴ϶ α׷ ũ ̹κ   & ƿ
    ֵ ش.  ׷Ƿ ڿ  Ͽ ٽ  ۾
  ϰ Ǹ ׸̼ Ʈ Ű Ǵ ̴.

    ڿ  μ Ͽ mktemp() ȣϴ 
  α׷鿡  ߻ ̴. mktemp() ־ μ ٽ
   ϱ ̴.

    ġ  ؼ (a) -fwritable-strings ̶ ɼ ־
  Ѵ. ̷ ָ gcc ڿ  Ÿ  ְ ȴ.
  Ǵ (b)  Ǵ κ ؼ  ƴ϶  ־
   ȣ  strcpy  Ͽ ͸ װ īش.

  4.3.10.   execl() ȣ ϴ°?

   ϴ.  ȣ  ʾұ ̴. execl 
  ù° μ ϰ ϴ α׷̴. ׸ ι°ʹ
  ȣϴ α׷  argv迭̴. ϶! argv[0]
   ƹ μ  Ǵ  ȴٴ ! 
    ڵ带 Ѵ.

       execl("/bin/ls","ls",NULL);

      ȵȴ.

       execl("/bin/ls", NULL);

  ƹ μ  Ű 쿡  ڽ 
  ̺귯  Ÿ  ִ    ¶
  Ѵ. ּѵ a.out  ׷ϴ. ELF  ٸ 
  ۵Ѵ.

  ( ̷ ̺귯  Ѵٸ   ̽ ִ.
   εDynamic Loading   ų ldd  
  ϶)

  5.   Profiling

  5.1.   (lint)

   ߻ϰ  ذϴ ͺٴ  ̿ ϴ 
  ߿ ?  θ ̴ lint . Ƹ κ
   gcc  ڼ  ޼ ϰ ֱ  
  . Ƹ  Ͼ̴  -Wall ġ ̴. ̰
  ǹϴ ٴ "Warnings, all"μ   ޼ ߻Ű
  ̴.   ڼϰ ´.

  Public Domain lint  <ftp://larch.lcs.mit.edu/pub/Larch/lclint>
    ִ.  󸶳   𸥴.

  5.2.  

  5.2.1.   ϸ α׷   ˾Ƴ  ִ°?

  ׷ ؼ -g ɼ ְ /ũؾ Ѵ. ׸ -fomit-
  frame-pointer ġ ־ Ѵ.   κ ٽ 
  ʿ ,    ִ κи ׷ ָ ȴ.

  a.out ־ ̺귯  -fomit-frame-pointer ġ
   ϵǾٸ gdb    ̴. -g ɼ ִ
   ٷ  ũ ϶  ϰ ȴ.

   Ŀ libg.a ã  ٰ ϸ鼭 ϰ ȴٸ, 
  /usr/lib/libg.a   ʱ  ̴.  ȭ Ư
  ̺귯μ   C ̺귯̴. libc Ű ԵǾ
  ְų Ǵ libc ҽ ڵ带 ޾Ƽ ϸ .  ׷
  ʿ  ƴϰ  /usr/lib/libc.a /usr/lib/libg.a
  ũѹ κ  ̴.

  5.2.1.1.     ϸ ٽ   ִ°?

    GNU Ʈ -g ɼ  ϵǾ Ƿ ȭ
  ũⰡ ſ ũ. (  ũǾ ) ׷   
   ʴ.

   α׷ autoconf   configure  ִٸ,
    Makefile ǵ帲ν    ʰ  
  ִ.  ELF ϰ ִٸ, α׷ -g ð  
  ũǸ, ׳  strip(  ȭϿ 
  )ų  ִ.

  5.2.2.   Ʈ

  κ  gdb ϰ ִ. gdb GNU archive sites
  <ftp://prep.ai.mit.edu/pub/gnu> ҽ ·, ƴϸ tsx-11
  <ftp://tsx-11.mit.edu/pub/linux/packages/GCC>̳ Ʈ
  ̳ʸ ·   ִ. xxgdb gdb  X 
  ̴. , 켱 gdb ̹ ġ߾ Ѵٴ ̴. 
  ҽ  <ftp://ftp.x.org/contrib/xxgdb-1.08.tar.gz> ã  ִ.

   UPS Ű Rick Sladkey  õǾ. X 쿡 
  ư.   xxgdb  ؽƮ  gdb Ϳ ϴ
  ´ ƴϴ.  Ǹ ɵ   ִ.  
  뿡  ð Ҿϰ ִٸ, 켱 UPS Ÿ Ѵ.
   ϵ ̳ʸ ҽ ġȭ
  <ftp://sunsite.unc.edu/pub/Linux/devel/debuggers/>   ְ
   ҽ  <ftp://ftp.x.org/contrib/ups-2.45.2.tar.Z> ã
   ִ.

  뿡 ̴  ٸ  ϳ ڸ strace   ִ. strace
  α׷  ý ȣ ȭ鿡 ǥش. ̰ 
  ٹ 밡ѵ,    н ҽڵ带 
    ̳ʸ ȭ ȿ ϵǾ ִ, и ̳ʸ
  ȿ ִ ǵ ߰ϰ  , Ϲ Ϲ  
  ۵ϰ ִ ˾Ƴ   Ѵ. ֽ strace (
  3.0.8)   <ftp://ftp.std.com/pub/jrs/>   ִ.

  5.2.3.  ׶ () α׷

   α׷  fork()  ϰ , θ μ
   . ̴  ǿ Ͽ   иϴ.

  ̷     fork Ͽ (breakpoint)
  ִ ̰ α׷ ߸ ٽñ װ 0  ִ
  ̴.

       (gdb) list
       1       #include <stdio.h>
       2
       3       main()
       4       {
       5         if(fork()==0) printf("child\n");
       6         else printf("parent\n");
       7       }
       (gdb) break fork
       Breakpoint 1 at 0x80003b8
       (gdb) run
       Starting program: /home/dan/src/hello/./fork
       Breakpoint 1 at 0x400177c4

       Breakpoint 1, 0x400177c4 in fork ()
       (gdb) return 0
       Make selected stack frame return now? (y or n) y
       #0  0x80004a8 in main ()
           at fork.c:5
       5         if(fork()==0) printf("child\n");
       (gdb) next
       Single stepping until exit from function fork,
       which has no line number information.
       child
       7       }

  5.2.4.  ھ ȭ(Core file)

    ýÿ ھ ȭ  ʵ õǾ ִ. 
  ھȭ   Ϸ Ѵٸ װ ٽ  ϴ  
   Ѵ.

   ȣȯ (. tcsh)  ִٸ    .

       % limit core unlimited

   з(sh, bash, zsh, pdksh) ϰ ִٸ,

       $ ulimit -c unlimited

   ھ ȭ ̸ Ͽ 뼺  ʹٸ, Ŀ ҽ
  ణ ָ ȴ. , fs/binfmt_aout.c fs/binfmt_elf.c 
  ȭ ãƺ.

               memcpy(corefile,"core.",5);
       #if 0
               memcpy(corefile+5,current->comm,sizeof(current->comm));
       #else
               corefile[4] = '\0';
       #endif

  grep    ̷ κ  ã Ŀ 0̶ Ǿ ִ
   1̶  ش.

  5.3.  Profiling

  Profiling̶ ϴ  α׷  κ   ȣǰ
  ִ Ǵ  ð ҿϰ ִ ϴ ̴. ڵ带
  ȭŰ ð   ҺǴ  ִ  ̴.
  ̷ ϱ ؼ -p ɼ ־ ð  Ʈ ȭϵ
    ֵ ٽ ־ Ѵ.  binutil Ű ִ
  gprof   ʿ Ѵ. ڼ  gprof  ϱ
  ٶ.

  6.  ũ

  ȣȯ ʴ   ̳ʸ ,  ̺귯 
  ̺귯 ,   Ŀ Ͼ ۾ ̹  ģ
   α׷   Ͼ ۾  ٿ Ͽ "ũ" 
   Ͽ  ȥ( ε(load)Ѵٶ  
  ϶   ִ), ̷  Ϳ Ͽ ٷǷ ̹ 
    ̴.   ̹Ƿ ũ  ʿ .

  ̷ ȥ ȭϱ ؼ, 츮 (runtime) Ͼ Ͽ
  Ͽ  ε(Dynamic Loading)̶ ܾ ϰڴ. ׸ 
  ǿ  ٷ Ѵ. Ǵ  ŷ(Dynamic Linking)̶
  ܾ ǥǱ⵵ Ѵ.  ̹ ǿ    ٷ
  Ŀ  ũ ۾ ؼ ٷ Ѵ.

  6.1.   ̺귯 vs  ̺귯

  α׷   ۾ ٷ ũ(Link) ̴. ʿ
    ų  κ  ִ ˾ƺ  ̴.
  и α׷ ؾ  .      ¥ִ
   ƴϴ.   ȭ ٵ ϴ ε ׷ ϵ ̹
  п ̺귯 · ־ ִ.  
  ýۿ /lib /usr/lib/ ׷ ̺귯 ã  ִ.

   ̺귯(Static Library)  , Ŀ α׷ ʿ
  ϴ κ ̺귯 ãƼ ׳ ȭϿ īع. 
  ̺귯(Ǵ  ̺귯) 쿡 ̷ ϴ  ƴ϶
  ȭϿٰ  "  켱  ̺귯 εų "̶
  ޼ ܳ´. 翬  ̺귯 ϸ ȭ
  ũⰡ ۾. ׵ ޸𸮵   ϸ, ϵ ũ
  뷮  Ѵ.  ⺻ ൿ ϴ  ̺귯
   װͰ ũ Ű, ׷   ̺귯 
  ũ ۾ Ѵ.  ̺귯  ȭ  ߴµ,
  쿬  ȭ ٸ 켱  ̺귯 
  ִ(a.out *.sa, ELF *.so)캸 б ۹̼ ־ ִ
  ˾ƺ.

    ̺귯 libname.a    ̸ ´.
  ׿   ̺귯 libname.so.x.y.z   ̸ µ
  x.y.z  Ѵ.   ̺귯  ũǾ ִ.
  ( ߿) libname.so.x ׸ libname.so  ũ ´.
  ǥ ̺귯     ִ.

   ldd  ν Ư α׷  
  ̺귯 ϴ   ִ. (ldd = List Dynamic Dependencies)

       $ ldd /usr/bin/lynx
               libncurses.so.1 => /usr/lib/libncurses.so.1.9.6
               libc.so.5 => /lib/libc.so.5.2.18

     ýۿ ؽƮ   ϰ ִ lynx
   α׷ Ͽ  üũ غ ̴. libc.so.5 (C
  ̺귯) libncurses.so.1 (͹̳  Ǵ ̺귯)
  ʿ ϰ ִٰ ϰ ִ. ƹ  ̺귯 ʿ
  ׳ `statically linked' Ǵ `statically linked (ELF)' 
  Ѵ.

  6.2.  ̺귯 鿩ٺ (ü sin()   ִ°?)

  nm libraryname ̶ Ű ̺귯   ɺ
  ش. ̴  ̺귯  ̺귯   ȴ.
   tcgetattr()̶ Լ ã ʹٸ   ָ ȴ.

       $ nm libncurses.so.1 |grep tcget
                U tcgetattr

  U ϴ ٴ "undefined"  ncurses ̺귯 ϰ 
   Ǵ  ʰ ִٴ ̴.

  ̷Ե   ִ.

       $ nm libc.so.5 | grep tcget
       00010fe8 T __tcgetattr
       00010fe8 W tcgetattr
       00068718 T tcgetpgrp

  `W' "weak"  ɺ Ǵ Ǿ ٸ ̺귯 
  ǵ  ִ ¶ ǹ̴. Ϲ  쿡
  `T' .

  sin()  ִ°    ª  libm.(so|a)̴.
  <math.h> ǵǾ ִ  Լ ٷ   ̺귯
  ִ. װ ϱ ؼ ũÿ  -lm ɼ ־ Ѵ.
  using any of them.

  6.3.  ȭ ã

  ld: Output file requires shared library `libfoo.so.1`

   ϴٺ   ޼      
  ̴. ld ׸  α׷ ȭ ã   
  ٸ ⺻ /usr/lib ã ȴ.    ٸ 
  ̺귯  ְ װ ld  ˷ֱ ؼ gcc  ld
   ̺귯 մ 丮 -L ɼ ༭ ˸.

  -L ɼ ־ ȵȴٸ, ld  ϴ ȭ  ҿ  ִ
  Ȯغ. a.out  ؼ -lfoo  ϸ ld libfoo.sa (
  ̺귯) ã ȴ.  װ ãµ ϸ libfoo.a (
  ̺귯) ȭ ã´. ELF ؼ libfoo.so ã 
  libfoo.a ã´. libfoo.so libfoo.so.x  ũ̴.

  6.4.  и ̺귯 

  6.4.1.   

  ٸ  α׷  ̺귯   ׸
  ư Ѵ. Ǵ ο  ϰų  ִ  
  ȿ  üѴٵ ׸ ʿ  ֹٵ ϴ
   ʿϴ. ̷  ȭϴ ̺귯  α׷ϴ
    ƴ  .    ɿ ϴ
  α׷̶?

  ׷ 츮 ̺귯 ̶ ϴ  Ѵ. ׸
  ̺귯 ȭ ̳ Ǵ  ȭ ̷ зϰ ̳
  ׷̵  α׷ 浹  ȭ Īϰ Ѵ.
  ̺귯  ȭϸ    ִ. (  ڸ,
  ELF ؼ ̴.  ׷  о  ̴)
  libfoo.so.1.2   1 ̰ ̳ 2 ̴. ̳ 
  ټ ߿    ִ. libc 쿡 ̳ʹ
  ġ ִ´.  libc.so.5.2.18  ̸ .
    , ھ(_), Ǵ Ʈ  ڸ ־
  .

  ELF a.out  Ŀٶ  ߿ ϳ ٷ  ̺귯
   Ŀ ִ. 켱 ELF ˾ƺ . ֳϸ  
  ̴.

  6.4.2.  ELF? ü װ ΰ?

  ELF (Executable and Linking Format)̶ ϴ   USL(UNIX
  System Laboratories) ϴ   ̳ʸ ̴. ׸
   ֶ󸮽 SVR4  ̴.  ؿԴ 
  a.out      GCC C ̺귯 ڵ
     ǥ ̳ʸ İ  ELF ̵ϱ
  Ͽ.

  6.4.2.1.  ٽ   ?

  ̹  '/news-archives/comp.sys.sun.misc' κ 
  ̴.

       ELF ("Executable Linking Format) ϴ  "Ӱ
       " Ʈ ȭ μ SVR4  ԵǾ. ELF
       ׳ COFF ĺ  ϴ. ֳϸ  Ȯ强
       ֱ ̴. ELF Ʈ ȭ  ̸ 
       ǵ Ʈ Ѵ. װ  ũ ü
        迭 ٸ. ̷  COFFʹ ޸ Ư
       ġ  ʿ䵵 ,  Ư  
       ʿ䵵 . ڵ Ѵٸ ο  ÷ 
       ִ. ELF  DWARF(Debugging With Attribute Record For
       mat) ϴ       ִ. -
         Ϻ ǰ  ʴ.  ۾
        ̴ DWARF DIE(Ǵ Debugging Information Entries)
       ELF  .debug  Ѵ.  ũ  
       ſ DWARF DIE   ̸   Ӽ
       ϰ   α׷ Ÿ Ʈξ
       ִ. DIE COFF .debug Ǻ    Ƴ 
       ִ.(COFF 쿡 C++  ׷  ͵ Ƴ
        .)

       ELF ȭϵ SVR4(ֶ󸮽 2.0 ?) ELF  ̺귯
       ؼ   ִ.  ̺귯 ELF Ͽ 
        ̽ ϰ ִ. ELF  ̺귯
       鼭  ߿  ϳ ELF ȭ н
       ȭϷμ  ʿ䰡  ٴ ̴. װ  Elf *
       μ ٰϴ.  elf_open() ȣ ϸ  
       ϴ.  Ŀ elf_foobar()  ۾ Ѵ. ̴
        COFF Ŀ  ũ  ̹ 
       ۾ߴ Ͱ  ٸ ̴.

  ELF  /ݴ, ׸  a.out ý ELF  ý
  ׷̵ؾ  ʿ伺 ELFϿ  ٷ  
  װ ⿡   ʴ´.

  6.4.2.2.  ELF  ̺귯

  libfoo.so  ̺񸣷   ⺻  
  .

       $ gcc -fPIC -c *.c
       $ gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 *.o
       $ ln -s libfoo.so.1.0 libfoo.so.1
       $ ln -s libfoo.so.1 libfoo.so
       $ LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH ; export LD_LIBRARY_PATH

  ̷ ϸ libfoo.so.1.0̶  ̺귯  ̴.
  ׸ ld (libfoo.so ʿ)  Ŀ(libfoo.so.1 ʿ) ʿ
   ũ . װ ׽Ʈغ ؼ 츮
  LD_LIBRARY_PATH  丮 ÷Ѵ.

   ̺귯  ۵Ѵٴ  Ȯϸ,  ̺귯
  /usr/local/lib ̵Ų.  ׸ ٽ ũ ش.
  libfoo.so.1κ libfoo.so.1.0 ̸ ũ ldconfig ϴ
  α׷  ׻ ֽ  ȴ.  ð ˾Ƽ
  ش.  libfoo.so  ־ Ѵ.  ѹ 
  ̺귯  κе(  ȭϵ ش) Ĳ
  ׷̵ַ Ѵٸ libfoo.so -> libfoo.so.1̶ ũ 
  ָ ȴ. ׷ Ǹ ldconfig ˾Ƽ ũ ش. ࿡
  ̷ ͱ     Ϸ Ѵٸ ߿  
   ִ. и صξ.

       $ su
       # cp libfoo.so.1.0 /usr/local/lib
       # /sbin/ldconfig
       # ( cd /usr/local/lib ; ln -s libfoo.so.1 libfoo.so )

  6.4.2.3.   ȣ ̱, soname ׸ ɺ ũ

   ̺귯 soname̶   ִ. Ŀ ã ִ
  ̺귯 ȿ ̷ ̸ ߰ϰ Ǹ,  ȭϸ(libfoo.so
   ̸) ƴ϶ soname̶ ϴ   ̳ʸ ǥصд.
  ÿ  δ soname  ȭ ã ȴ.  
  ȭϸ ƴϴ. ̴  ǹϴ°? ϸ libfoo.so ȭϸ 
  ̺귯 libbar.so soname   ְ װ ũ 
  α׷ ᱹ libbar.so ã´ٴ ̴.

  ̰  ǹ ó ̴µ  ̰̾߸ 
  ̺귯  ٸ    ýۿ   ִ°
  ϴµ ־ ٽ κ̴.  ̺귯 ̸
  ǻ ǥ ̺귯 libfoo.so.1.2 ̷  θ
  libfoo.so.1̶ soname οϴ ̴.   ǥ ̺귯
  丮( /usr/lib) ߰Ǹ ldconfig libfoo.so.1 ->
  libfoo.so.1.2 ũ   ̴. ׷ ν ÿ
   ̹ õǵ ش.   libfoo.so ->
  libfoo.so.1̶ ɺ ũ ʿϴ. ֳϸ ld  ũ 
  Ȯ soname  ã ϱ ؼ̴.

   ̺귯 ׸ ĥ  Ǵ ο  ÷ (
  α׷ ǿ  ʴ ȭ), ٽ ̺귯  
  soname ְ ȭϸ ٲٵ Ѵ.   ̺귯
  ũǾ ִ  α׷ 浹ϰ Ǵ ̺귯 ȭ
    soname ڸ ϳ ø ȴ. ̷  ο 
  ̺귯 libfoo.so.2.0 װ, soname libfoo.so.2  ̴.
  ׸ ̹ libfoo.so ο  ̺귯̿ ɺ
  ũŰ .

    ̷  ̺귯 ̸  ʿ . 
  װ  ̴. ELF п ̺귯 ̸⿡ ־
   ְ  ׷ٰ ؼ  ׷Ը ϶  ƴϴ.

  ڸ,  ȣȯ    ׷̵̰ ׷
    ̳ ׷̵  ؼѴٸ   ϶.

       gcc -shared -Wl,-soname,libfoo.so.major -o libfoo.so.major.minor

      ̴.

  6.4.3.  a.out  

   ̺귯   ELF ׷̵忡  ߿
  ̴.  a.out ϱ ϴ.
  <ftp://tsx-11.mit.edu/pub/linux/packages/GCC/src/tools-2.17.tar.gz>
  ޾ƿ.  ׸  ȭ Ǯ  20 ¥ 
  о. 鿡  ̴ ڰ ǰ  ʴ. 
    ڽ  ϰ  ʴ. :-)

  6.4.3.1.  ZMAGIC vs QMAGIC

  QMAGIC ̶ ϴ   a.out(ZMAGIC ̶ ˷ ִ)
    ȭ ̴.  ù°   ʴ
  ̳ʸ̴. 0-4096   ε  ʱ  ̷
  ν NULL ۷ Ʈ(deference trapping)    
  ִ.  ȿμ  ȭ  1K  ۾ ȴ.

   Ŀ  ZMAGIC  Ѵ. ణ   Ŀ 
   ϸ, ֽ   QMAGIC  ϰ ִ. ̰
   ߿ ʴ. ֳϸ Ŀ ü    ų 
  ֱ ̴.

  file  ָ װ QMAGIC Ǻ   ̴.

  6.4.3.2.  ȭ ġ(File Placement)

  a.out(DLL)  ̺귯 2   ȭ ׸ ϳ ũ
   Ǿ ִ.   ü ؼ  ؿ ̸ foo 
  ̺귯 Ͽ   ˾ƺ. foo  Ͽ libfoo.sa,
  libfoo.so.1.2 ׸ libfoo.so.1 ̶ ũ Ǿ ִ. ũ
  libfoo.so.1.2 Ų. ̰͵  ΰ?

    ld libfoo.sa ã´. ̰̾߸ ̺귯 
  ׷ͱ ȭ ȴ. ׸ ũ   ܺ Ÿ Լ
   ͸ ϰ ִ.

   ÿ  δ libfoo.so.1 ã´. ̴  ȭ
  ƴ϶ ɺ ũ̴.    ռ  ̺귯
   ø̼ǰ 浹,  ο, װ  ο
   ü  ֵ ϱ ؼ̴. ο  (
   libfoo.so.1.3)̶ . ldconfig Ű ڵ
  libfoo.so.1 --> libfoo.so.1.3 ũ ۾   ̴.  
  α׷ ƹ ̻  ̴.

  DLL ̺귯(ݺ̶  ˰ ִ.   :DLL  ̹
  ̺귯  ִ)   ̺귯 ũ. DLL
  ̷ Ȯ强 ؼ  Ը  · ڸ صд. 
   ڸ ũ   ʵ   ִ.  cp
  makehole̶ α׷ ̷ ϴ  ϴ. ̹ 
  ġ ּҵ Ƿ ̺귯  Ŀ strip   ִ. 
  ELF ̺귯 ؼ strip .

  6.4.3.3.  libc-lite  ΰ?

  libc-lite  ϴ  libc   ұԸ ̶   ִ.
  ϳ ÷ ȿ  н   鿡 
    ̺귯̴. װ curses  dbm, termcap 
  ڵ带 ϰ  ʴ.    /lib/libc.so.4 lite 
  ̺귯 ũǾ ִٸ   libc  üϱ
  ٶ.
    Ʈ  Ʈغ  lite  C
  ̺귯     ̴. ġ غ ġ ʿ
  ŭ  C ̺귯̴.

  6.4.4.  ũϱ : Ϲ 

   ũ   ޶! ׷ װͿ ؼ  ƹ
  ϵ   ̴.   ̴  ؼ  ڴ.

      ̺귯 ũǱ ٶµ  ̺귯 ũǰ
        ִ.

        켱 ld ̺귯  ã  ֵ ũ ˸°
        Ǿ ִ Ѵ. ELF ؼ ̰ libfoo.so ɺ
        ũ ϸ a.out 쿡 libfoo.saȭ ϴ ̴.
        ELF binutil 2.5  2.6  ׷̵  
        ް ִ ̴.    ̺귯 Ͽ  
        ȶϰ ãƳ´µ,    ũ  
        ʾҴ ̴.  ൿ ٸ  İ ȣȯ
        ؼ Ź ŵǾ.  ൿ ߸  
        ǰ       ׷  ̴.

     DLL  mkimage  libgcc ãµ Ѵ.

        libc.so.4.5.x  ̻  Ͽ libgcc  ̻ 
        ̺귯 ƴϴ.   `-lgcc'   
        `gcc -print-libgcc-file-name` ٲ־ Ѵ. ( 
        ٷ Ʈ(`) ̴.   ڸ ϶.)

          /usr/lib/libgcc* ȭϵ ϶. ̰ ߿ϴ.

     __NEEDS_SHRLIB_libc_4  ̴.

     DLL ÿ ``Assertion failure'' ޽
         ޽   ִ jump table  
        jump.varsȭϿ ʹ   ۿ  ʾұ 
        ÷ο   ̴.   tools-2.17.tar.gz
        Ű  ִ `getsize'  Ͽ   ãƳ 
        ִ. Ƹ  ذå  ȣ  ۿ  
        .    ȣȯǵ ϸ鼭 ̴.

      ld: output file needs shared library libc.so.4
        ̷   libc ƴ ̺귯 (, X 
        ̺귯...)ϰ ũϷ   ߻Ѵ. -static Բ
         ʰ ũ ÿ -g ɼ ־ ̴.

         ̺귯  .sa ȭ  ǵ
        _NEEDS_SHRLIB_libc_4  ɺ  ִµ ߿
        libc.sa ذȴ.  -g ɼ ְ Ǹ libg.a Ǵ
        libc.a ũǰ ǹǷ  ɺ ذ  ʰ ǰ 
          ޼ ߰ Ǵ ̴.

         -g ÷׷   -static ̶ ɼ Բ
        ֱ ٶ. Ǵ -g   ȴ. ũ  
        ϴ κи -g ɼ ְ ص   
          ִ.

  7.   ε(Dynamic Loading)

  ̹   μ   븸  ִ. ELF Ͽ
   ν    þ  ̴.

  7.1.   

    ̺귯  ִ.   ü д  
  ̷   ͵  ̴.  α׷ ũ 
   ۾ ε   ݴ  ľ Ѵ.

  7.2.   ޼

     can't load library: /lib/libxxx.so, Incompatible version
        a.out  Ͼµ,    ̺귯 
         Ʋٴ ̴. ٸ   ִٰ ؼ
         ɺ ũϴ  ȵȴ. ȴ  ᱹ
        Ʈ ų ̴. ο  .ELF
         ޼ ´.

          ftp: can't load library 'libreadline.so.2'

     warning using incompatible library version xxx
        a.out ̴. α׷    ̳ 
        ̺귯  ֱ  ߻ϴ  ޼̴.
        α׷ Ǳ  ̴.  ׷̵ϴ  ?

  7.3.   δ ۵ ϱ

   ȯ   δ Ѵ. κ Ϲ ںٴ
  ldd ϴ. ldd پ ġ ν    ִ.

  o  LD_BIND_NOW --- Ϲ Լ ȣǱ  ̺귯
     ãƺ ʴ´.  ÷׸ ָ ̺귯 ÿ 
     üũ ϰ ǰ    ȴ. ̰  
     α׷  ͵  ũ Ǿ غ  ϴ.

  o  LD_PRELOAD --- overriding Լ Ǹ  ִ ȭϿ õ 
     ִ.   ޸ Ҵ  ׽Ϸ ϸ, malloc
     üϷ    ϴ ƾ  Ŀ ü 
     ִ. malloc.o  ̸     غ.

       $ LD_PRELOAD=malloc.o; export LD_PRELOAD
       $ some_test_program

  LD_ELF_PRELOAD LD_AOUT_PRELOAD   ϴ.   Ư
  ¿ Ѵ.  LD_ELF_PRELOAD LD_PRELOAD  
  Ǿٸ   ڼ   LD_ELF_PRELOAD ȴ.
  o  LD_LIBRARY_PATH --- ̰  ̺귯 ã  
     丮 ݷ(:) иڷ Ἥ ǥ Ʈ̴. װ ld
       Ѵ.  ÿ Ѵ.  setuid
     setgid  α׷ ؼ ̴. 
     LD_ELF_LIBRARY_PATH LD_AOUT_LIBRARY_PATH  ̳ʸ
     Ŀ ǵ ϰ ִ.  LD_LIBRARY_PATH  
     ׷ ʿ ʴ. ſ /etc/ld.so.conf/ 丮 ߰ϰ
     ldconfig ٽ   Ű° .

  o  LD_NOWARN --- ̴ a.out ȴ.    
     ϸ LD_NOWARN=true; export LD_NOWARN) ̳  ٸٵ
     ϴ, ũ ɰ ʴ  ǥ ʵ Ѵ.

  o  LD_WARN --- ̴ ELF شȴ. õǸ Ϲ ``Can't find
     library''  ɰ   ٲپش.  ʿ
     ɼ̴.

  o  LD_TRACE_LOADED_OBJECTS --- ELF ȴ. α׷ Ͽ
     ldd Ͽ ǰ ִٰ ϰԲ .

       $ LD_TRACE_LOADED_OBJECTS=true /usr/bin/lynx
               libncurses.so.1 => /usr/lib/libncurses.so.1.9.6
               libc.so.5 => /lib/libc.so.5.2.18

  7.4.   ε ϴ α׷ 

  ̴ ֶ󸮽 2.x  ε  ̷ İ ſ ϴ. H
  J Lu ELF α׷  ڼ   dlopen(3) 
     ִ.  ld.so Ű ִ.  α׷
  -ldl ɼ ְ ũ϶.

       #include <dlfcn.h>
       #include <stdio.h>

       main()
       {
         void *libc;
         void (*printf_call)();

         if(libc=dlopen("/lib/libc.so.5",RTLD_LAZY))
         {
           printf_call=dlsym(libc,"printf");
           (*printf_call)("hello, world\n");
         }

       }

  8.  ڿ ϱ(ϴ Ǿ  ʽϴ.)

  8.1.  Bug reports

  Start by narrowing the problem down.  Is it specific to Linux, or does
  it happen with gcc on other systems?  Is it specific to the kernel
  version?  Library version?  Does it go away if you link static?  Can
  you trim the program down to something short that demonstrates the
  bug?

  Having done that, you'll know what program(s) the bug is in.  For GCC,
  the bug reporting procedure is explained in the info file.  For ld.so
  or the C or maths libraries, send mail to linux-gcc@vger.rutgers.edu.
  If possible, include a short and self-contained program that exhibits
  the bug, and a description both of what you want it to do, and what it
  actually does.

  8.2.  Helping with development

  If you want to help with the development effort for GCC or the C
  library, the first thing to do is join the linux-gcc@vger.rutgers.edu
  mailing list.  If you just want to see what the discussion is about,
  there are list archives at  <http://homer.ncm.com/linux-gcc/>.  The
  second and subsequent things depend on what you want to do!

  9.  The Remains

  9.1.  The Credits

       Only presidents, editors, and people with tapeworms have the
       right to use the editorial ``we''.

  (Mark Twain)

  This HOWTO is based very closely on Mitchum DSouza's GCC-FAQ; most of
  the information (not to mention a reasonable amount of the text) in it
  comes directly from that document.  Instances of the first person
  pronoun in this HOWTO could refer to either of us; generally the ones
  that say ``I have not tested this; don't blame me if it toasts your
  hard disk/system/spouse'' apply to both of us.

  Contributors to this document have included (in ASCII ordering by
  first name) Andrew Tefft, Axel Boldt, Bill Metzenthen, Bruce Evans,
  Bruno Haible, Daniel Barlow, Daniel Quinlan, David Engel, Dirk
  Hohndel, Eric Youngdale, Fergus Henderson, H.J. Lu, Jens Schweikhardt,
  Kai Petzke, Michael Meissner, Mitchum DSouza, Olaf Flebbe, Paul
  Gortmaker, Rik Faith, Steven S. Dick, Tuomas J Lukka, and of course
  Linus Torvalds, without whom the whole exercise would have been
  pointless, let alone impossible :-)

  Please do not feel offended if your name has not appeared here and you
  have contributed to this document (either as HOWTO or as FAQ).  Email
  me and I will rectify it.

  9.2.  Translations

  At this time, there are no known translations of this work.  If you
  wish to produce one, please go right ahead, but do tell me about it!
  The chances are (sadly) several hundred to one against that I speak
  the language you wish to translate to, but that aside I am happy to
  help in whatever way I can.

  9.3.  Feedback is welcomed.  Mail me atdan@detached.demon.co.uk.  My
  PGP public key (ID 5F263625) is available from myweb pages
  <http://ftp.linux.org.uk/~barlow/>, if you feel the need to be secre
  tive about things.

  9.4.  Legalese

  All trademarks used in this document are acknowledged as being owned
  by their respective owners.

  This document is copyright (C) 1996 Daniel Barlow
  <dan@detached.demon.co.uk> It may be reproduced and distributed in
  whole or in part, in any medium physical or electronic, as long as
  this copyright notice is retained on all copies. Commercial
  redistribution is allowed and encouraged; however, the author would
  like to be notified of any such distributions.

  All translations, derivative works, or aggregate works incorporating
  any Linux HOWTO documents must be covered under this copyright notice.
  That is, you may not produce a derivative work from a HOWTO and impose
  additional restrictions on its distribution. Exceptions to these rules
  may be granted under certain conditions; please contact the Linux
  HOWTO coordinator at the address given below.

  In short, we wish to promote dissemination of this information through
  as many channels as possible. However, we do wish to retain copyright
  on the HOWTO documents, and would like to be notified of any plans to
  redistribute the HOWTOs.

  If you have questions, please contact Tim Bynum, the Linux HOWTO
  coordinator, at linux-howto@sunsite.unc.edu via email.

  10.  Index

  Entries starting with a non-alphabetical character are listed in ASCII
  order.

  o  -fwritable-strings ``39'' ``56''

  o  /lib/cpp ``16''

  o  a.out ``1''

  o  ar ``10''

  o  as ``8''

  o  <asm/*.h> ``19''

  o  atoi() ``40''

  o  atol() ``41''

  o  binaries too big ``63'' ``65'' ``77''

  o  chewing gum ``3''

  o  cos() ``68''

  o  debugging ``59''

  o  dlopen() ``82''

  o  dlsym() ``83''

  o  documentation ``4''

  o  EINTR ``52''

  o  elf ``0'' ``71''

  o  execl() ``57''

  o  fcntl ``47''

  o  FD_CLR ``44''

  o  FD_ISSET ``45''

  o  FD_SET ``43''

  o  FD_ZERO ``46''

  o  file ``2''

  o  <float.h> ``20''

  o  gcc ``6''

  o  gcc -fomit-frame-pointer ``61''

  o  gcc -g ``60''

  o  gcc -v ``14''

  o  gcc, bugs ``15'' ``28'' ``29'' ``84''

  o  gcc, flags ``13'' ``25'' ``26''

  o  gdb ``64''

  o  header files ``17''

  o  interrupted system calls ``51''

  o  ld ``9''

  o  LD_* environment variables ``80''

  o  ldd ``81''

  o  libc ``7''

  o  libg.a ``62''

  o  libgcc ``79''

  o  <limits.h> ``21''

  o  lint ``58''

  o  <linux/*.h> ``18''

  o  manual pages ``5''

  o  <math.h> ``70''

  o  maths ``69''

  o  mktemp() ``55''

  o  optimisation ``27''

  o  QMAGIC ``76''

  o  segmentation fault ``30'' ``54''

  o  segmentation fault, in GCC ``33''

  o  select() ``50''

  o  SIGBUS ``34''

  o  SIGEMT ``35''

  o  SIGIOT ``36''

  o  SIGSEGV ``31'' ``53''

  o  SIGSEGV, in gcc ``32''

  o  SIGSYS ``38''

  o  SIGTRAP ``37''

  o  sin() ``67''

  o  soname ``73''

  o  sprintf() ``42''

  o  statically linked binaries, unexpected ``66'' ``78''

  o  <stdarg.h> ``23''

  o  <stddef.h> ``24''

  o  strings ``11''

  o  <sys/time.h> ``48''

  o  <unistd.h> ``49''

  o  <varargs.h> ``22''

  o  version numbers ``12'' ``74''

  o  weird things ``72''

  o  ZMAGIC ``75''

