








			 The Linux GCC HOWTOV0.2

		: Daniel Barlow <dan@detached.demon.co.uk>

		 : ½ѫ(Frank J.S. Chen)<frank63@ms5.hinet.net>

			    v1.17, 28 February 1996



				   Abstract

     ĲװGNU
     CͳʽݵķͬʱŹ۵˵ʽı롢ᡢִСĹԼٵ⡣дĲο̩Mitch
     D'SouzaռGCC-FAQһԴELF-
     HOWTO˷HOWTO˵ѴGCC-FAQҼҪELF-HOW
     TOˡ GCC-
     HOWTOһݹеİ汾汾ţRCSĽκָ뽨ģ˶ܻӭ



1.  Զǣ

1.1  ߵĻ

    ΪLinux document pro
     jects(LDP)ķƻϵ֮һĿǰַ֮Ϊhttp://www.linux.org.tw/CLDP/ӭλӻԾͶһƻ

    Ҳûȫԭַ롣ΪͨɶһԵһݵ֣ףǾԭģĻֲӢļ﷨ṹĲԣԹͨް

    һЩؼרҵʻȣḽԭĵ֡

    תѣΨԭĳֻbugsshadow pass
     wordpaddingimage֮ģԭĲ䡣¶Щֻʵǵģ벻ָ̡

    "[ע:**]"֮ǣΪ˶֮ע⡣

    ƪκνʵģemailfrank63@ms5.hinet.net

    WWW Home Page http://linux.ntcic.edu.tw/~jsfrank/

    ļ֮ȨȡӢ֮ԭDaniel Barlow
     ֮ͬ⣻½ѫд˷ļеȨĿԸýɢļΨ˽ڲ˵ԭⲻϣҲġ

    v0.1൱ֲڣľ﷨ṹ̫ɢv0.2ĵϰһĲ׵ĵطei
     ther֣Ӣ˳ei
     therãָһHOWTOͳݷӦϰԣлϵĲ졣

    м仰ûһǿֱҲ벻ֻñԭˣҪǸµģҡ

    v0.1淭ʼΪ11/7/97ֹΪ11/19/97



The Linux GCC HOWTOV0.2						      1





The Linux GCC HOWTOV0.2						      2



    v0.2ʼڣ5/13/98ֹΪ6/3/98

1.2  뾲


 "

ĿǰLinuxķչӿĽһ㽲Lin
uxִеĸʽãȡϵͳģLin
uxӦһְɣĶļ԰ִе



Ҫأִйóʽfile磬file
/bin/bashͶˡELFĳʽԣөĻʾѶϢẬELFۣ˵a.outģѶϢڻ
Linux/i386

ELFa.outʽĲ֮½ۣܹ㷺รELFǱȽµĸʽһԣܵĳ̶Ƚϼѡ

1.3  ߵ˽

 "

 Ȩ˵Ϸɹ涨Ͱļβˡ֮⣬һһЩòĻҪû¸ɣҲҪUsenet϶һЩʵ⣻УҪΪԼCԵĹרŷһЩbugsbugsۣⲻ͵춸߱㲻ѧڹүǰˣ˵ΪǵӢǵòʧġ

1.4  ӡˢŰ

 ڶǭ
Postscriptdvihtmlʽͱ仯ͻָֻʽ˶һЩرǵơժ¼ԭʼȣͳͳǴֻ͡ĻĳЩҪǿġЩûй̶ġԣͿԴﵽǿЧˡ

ļͬʱҲõһõdvi
postscript֮İ汾־½(sec
tion)ıţHTMLĻЩֻᰴ˳УûȥԵݣ㿴Ǵְ汾Ļ,
־ֻ֣ûĺ⣻ϿΪ

õshellBourne shellC shellٵȻBourne
shell﷨õC shellĻ趨﷨

     % setenv FOO bar

ҪBourne shellĻһд

     $ FOO=bar; export FOO

ʾʾǾַ#Ǯַ
$ĻпֻrootѡȻҪЩŪϵͳֱ䣬ҿһҲḺรףˣ:-)

11/8/97.  5/13/98޶


2.  Ķ











The Linux GCC HOWTOV0.2						      3



2.1  GCC-HOWTOĶ

 ļLinux HOWTOϵ֮һ仰˵дLinux HOW
TOļվҵķ٣<URL:http://sun
site.unc.edu/pub/linux/docs/HOWTO/>HTMLİ汾ܻǽµİ汾Դ<URL:http://ftp.linux.org.uk/~bar
low/howto/gcc-howto.html>ץ

2.2  GCCصĶ

 "

׼gcc˵ļ渽ڷеԭʼ(source distribu
tion)ڣ¿ˣͷtextin
fo.infoֵҪ·ʹ죬һƬcdromȻи߶ȵҲɣԼun
tarȻٰӦλԪһһ/usr/infoĿ¼¡Ĳ
tsx-111 վȥι۲ιۡ룬ûбҪǵµİ汾ɣ



 libcļ˵ԴһGNU libc.infoĸʽ棬st
dio֮⣬Linux
libc˵൱꾡ȷһֿLinuxarchivemanpages2
ҵϵͳУsystem call2ڣlibcfunc
tion3ڣļ˵

2.3  GCC

ж:

(a)<URL:ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/>վҵ
ʽLinux GCCϵͳ(distribu
tion)ѾõĿִедļʱ2.7.2(gcc-2.7.2.bin.tar.gz)µİ汾

(b)ᣨFree Software FoundationGCCԭʼԴվGNU
archives3
ȡáûбҪǵİ汾һ²У汾ȷĿǰµġLinux
GCCά(maintainers)Ժɵбµİ汾configure(script)Զ趨и顣пղtsx-114
˵İ汾Ҫõġ

ҪдһЩõ壨вûɶ;·ĴܡһС̸ҲҪģ

2.4  Cʽͷ

 "


____________________

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

2. <URL:ftp://sunsite.unc.edu/pub/Linux/docs/>

3. <URL:ftp://prep.ai.mit.edu/pub/gnu/>

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







The Linux GCC HOWTOV0.2						      4



ѡһ׳ʽȡ(i)ϵͳELFĻa.outģ(ii)ϣϵͳһ֣Ǵlibc
4libc
5һĵĽ飬ȥELF-HOWTO˵һʣELFļĶأ٣٣ƫУͲļͬλáվtsx-115
ҵҪġ

      libc-5.2.18.bin.tar.gz
	    --- ELFʽݣELF shared library images̬ʽݣstatic
	    librariesͷCѧʽݣ

      libc-5.2.18.tar.gz
	    ---libc-5.2.18.bin.tar.gzԭʼ롣ҲҪΪ.bin.׼(pack
	    age)бıͷʱԥºֱCʽȽϺãֱ˼ұõĶλ(bina
	    ries)ͿˡŵˣҵΣ˼ұõĶλͽֻҪNYSshadow
	    password£ҪԼƶҡ

      libc-4.7.5.bin.tar.gz
	    --- a.outĹʽ(shared library
	    images)뾲̬ʽ⣬;Ϊǰlibc
	    5׼湲ٶƵģҪʹa.outĳʽǼչa.outĳʽȻĻǲҪġ

2.5  йĹ (as, ld, ar, strings etc)




 "

ĿǰΪֹ֮ǰ̸Ķһվtsx-116
ϣͿҵЩ߳ʽĿǰİ汾binutils-2.6.0.2.bin.tar.gz

Ҫעbinu
tilsֻELFĿǰlibcİ汾ҲELFģȻϰa.outиELFlibca.outlibcһʹãٺòˡɷϵģCʽݵķչԼĽŲELFĸʽкܺõɣҪa.outĶȻҶͻƣ񲻿ɵĴ

11/9/97


3.  GCCİװGCC趨

3.1  GCCİ汾



 "

shellʾ¼gcc
-vөĻϾͻʾĿǰʹõGCCİ汾ͬʱҲһ൱ɿķȷõELFa.outҵϵͳϣִgcc
-vĽǣ



____________________

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

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







The Linux GCC HOWTOV0.2						      5



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

ѶϢָ˼Ҫ飺

     i486 ָõgc
     cΪ486΢д---ĵԿ3865863΢ľƬɵĳʽ룬˴˼ǿʹõġ֮486ĳʽĳЩطмϭ
     paddingĹܣԿ486ܵñȽϿ졣386ĻԣִгʽЧܲʲ᲻Ӱ죬ֻĻóʽԵĴһЩ

     box ˵һҲҪҲָslackwarede
     bian߸ʲҲǣޣĿ¼i486-linuxʵɵٮٮߣԶֽԼgccڽĹ趨һװ档һ:-)

     linux ʵָlinuxelflin
     uxaoutһ𲻱Ҫ󣬾ָһֻõİ汾졣


	  linux
	  ָELF汾2.7.0.Ǹµİ汾Ļa.outˡ

	 linuxaout ָa.outĸʽlinuxĶa.outELFʱlin
	  uxaoutͻ˳ˮۣҡһ䣬һĿˣ㲻ῴκΰ汾2.7.0.gc
	  clinuxaoutġ



	 linuxelf
	  Ѿʱˡָͨ2.6.3gcc汾ҲELFĿִеҪעǣgcc
	  2.6.3ڲELFʽʱbugsĿǰõǡ汾Ͽ

     2.7.2 汾š

ԣܽ2.7.2gccԲELFʽĳʽ롣򵥣Ȱɣeh

3.2  װᶼĶȥ?

װgccʱûϸĿөĻǴһķϵͳgccץװĻҲ֪ЩװסϵͳЩطص£

    /usr/lib/gcc-lib/target/version/
     Ŀ¼󲿷ݵıסطġпִеĳʽʵĹ⣬һЩض汾ĳʽͷҲᴢڴˡ

     /usr/bin/gcc ָǱʽ---Ҳʵ(command
     line)ִеĳʽĿ¼ɹְ汾gccʹãֻҪòͬıĿ¼װͿˡҪ֪ڶİ汾һshel
     lʾ´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










The Linux GCC HOWTOV0.2						      6



     /usr/target/(bin|lib|include)/ װֵĿa.out
     elfĳһֵĽ(cross-com
     piler)ȵȣЩ춷Ŀnon-native target(s)ĳʽ⣬binu
     tilsasldȵȣͷȶҵʹֻװһgccǿҵЩԭ׼ĶӦ/usr/(bin|lib|include)ˡ


     /lib//usr/lib Ŀ¼ȶϵͳnative-sys
     temĳʽĿ¼Ӧóʽõ/lib/cppҲҪ---ϣǴ/usr/lib/gcc-
     lib/target/version/ Ŀ¼�ŪᣨsymlinkָǶ
     [ע:νna
     tiveָĿǰϵͳa.outelfĸʽΪڶgc
     cһְ汾ȵȡnative˼ǡġġ롮ġȵȣõһƬCD-
     ROMͷβLinuxװɣLinuxΪɫŨҵƽ̨ᣬټװһЩһĿȻС롮ʡ
     ޹Σ롮롮ΪȵȵͬʱҲڶdefault˼ڡٸ˵ļֵжϺϲãnativenon-
     nativeӦûǡ]



3.3  ͷ˨?ͷ˨?

 "

аװ/usr/local/includeĿ¼µıͷųĻLinux3Ҫıͷ

   
     /usr/include/Ŀ¼µıͷ󲿷ݶH.J.Luչlibc׼(libc
     binary pack
     age)ṩġһֻ˵󲿷ݡԭΪԴıͷcurs
     esdbmʽȵȣǣõµlibcϵͳĻ°汾ɰѾ֧Ԯcursesdb
     mˡǶ֮Ϊ֮զģ [ע:libc binary pack
     ageָԶλʽ(machine
     code)֮׼ԭʼ루textҪȫɡlibcλ׼֮ӣȥbinarylibc׼ͨơ]




     ںԭʼķϵͳ(kernel source distribution) /usr/include/linux
      /usr/include/asm ͷЩ<linux/*.h> 
     <asm/*.h>Ӧзᣨsymbolic
     linksĿ¼linux/include/linux 
     linux/include/asmк֮־ĻװЩᣬͲӦֻģker
     nelѡ

     ԭʼѹunpackingᣬҲ֣ᷢҪںĵĿ¼kernel
     directorymake configĶܶĵ<linux/auto
     conf.h>İæȴп汾ͬڡɺİ汾asmֻԼһᣬڭ
     make configʱŽѡ [ע:ԭἰautoconf.hʱǡMany files
     depend on <linux/autoconf.h>,which otherwise may not exist,*˴֮oth
     er
     wise֮ӦΪݴ(adj)ָһһ֡ͬġ֮⣬ԭݴӾӦΪ(i)
     Many files depend on <linux/autoconf.h>.  (ii)<linux/autoconf.h> of other
     condition may not exist.
     һ以ȶԣ˴Ӧָͬڲͬ汾֮¡]









The Linux GCC HOWTOV0.2						      7



     ԣĿ¼/usr/src/linux£⿪ĵĳʽʱָʾɣ


	  $ cd /usr/src/linux
	  $ su
	  # make config
	  (ش⡣ͨشȷҪġ)
	  # cd /usr/include
	  # ln -s ../src/linux/include/linux .
	  # ln -s ../src/linux/include/asm .









     <float.h><limits.h><varargs.h><stdarg.h> <std
     def.h>֮ĵͬı汾죬㡮˵ġ
     /usr/lib/gcc-lib/i486-box-
     linux/2.7.2/include/ƣͬĿ¼Ƶĵطҵ

     11/11/97 5/14/98

3.4  (Building cross compilers)

3.4.1  Linuxҵƽ̨(target platform)

 Ѿõgccԭʼ룬ֻͨҪѭINSTALLָʾһе趨
makeٽconfigure --target=i486-linux --host=XXX on platform
XXXܰϷˡҪעǣҪLinuxкĵıͷͬʱҲҪ(cross
assembler)뽻(cross
linker)Դ<URL:ftp://tsx-11.mit.edu/pub/linux/packages/GCC/>

3.4.2  Linuxԭʼҵƽ̨(source platform)MSDOSΪҵƽ̨

 UghԵģҪõ׼emx׼goȥ<URL:ftp://sun
site.unc.edu/pub/Linux/devel/msdos>ҲûвԹЩûа취֤ʲᡣ


4.  ֲʽʽ

4.1  gccжķ

 "

ֻҪִgccʱ
-vҳõgccԶ㶨ʲš磬ҵĻ












The Linux GCC HOWTOV0.2						      8



     $ 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__ -

дĳʽõһЩLinuxеԣЩ޷ֲĳʽ룬ʽǰǸأʾ

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

__linux__ͿԴĿģϸһ㣬linuxมlin
uxҲж壬ϾȻPOSIXı׼

4.2  ˵

 gcc˵ļgcc info pageEmacsڣC-h
iȻѡgccѡҪŪCD-
ROMûѹ㣬ȻõǾɰġõķƶεarchive<URL:ftp://prep.ai.mit.edu/pub/gnu>mir
rorsվ̨ȥgccԭʼץؼңһ

gcc manual pagegcc.1
˵ѾʱˣҪԱ˳û¸Ӳ뿴ͻ˵ˡ

4.2.1  ƮƮ~


 "

 ִgccʱֻҪƨ-
OnѡgccԹԵѱĻ롣nһп޵Сͬ汾gccnȷĹЧһ͵ġΧǴ0ҪţҲҪѱ롣仯2ѱҪһ㡣3ѱҪٶһ㣬һ㣩

gccڲὫЩתһϵе-f-mѡִgccʱ-v-
QܺĿÿһֵȼ-OǶӦЩѡñ˵-
O2ҵgcc߻˵

     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

Ҫõѱȼı֧Ԯģe.g.
-O6Ч͸ıṩߵȼЧһġ˵ʵڵģгȥgccʽ룬ڱʱ˴⣬ĲʲõĹ롣иѱ뷽ϵµİ汾㣨usersĻܾͻᷢgccжĳʽˡ


gcc 2.7.02.7.2usersӦעһ㣬ʹ-
O2ʱһbugǣǿۼ(strength reduc
tion)ȻûãҪϲ±gccĻһİ汾Ը󣻲ȻĻһҪȷÿαʱм-








The Linux GCC HOWTOV0.2						      9



fno-strength-reduceร

11/12/97

4.2.1.1  иԵ΢

 һЩ-mʮôȴ޷ɸֵȼ-
Oʹá֮Ҫ-m386-
m486֣gccðڱĳʽרΪ386486дġһ-
mʽ룬ڱ˴˵ĻִУ-m486Ƚϴ󣬲386ĻҲȽˡ

Ŀǰ-mpentium-m586šLinusǿ-m486 -malign-loops=2
-malign-jumps=2 -malign-func
tions=2õѱ486ʽ룬þͿԱalignmentPen
tiumҪйgapsMichael Meissner˵

     ҵĵиң-mno-strength-
     reduce٣Ҫҿɲ̸ǿۼbugѽѾһ۵սˡһҲx86ĻϲϿĳʽ룬Ϊx86ĻݴĥġڣGCC's
     method of grouping registers into spill registers vs. other registers
     doesn't help
     eitherͳϣǿۼĽʹñȥüӷݴԼӷȡ˷㡣ʵϣڻ-
     fcaller-savesҲֻǸ©Ҳ˵



     ҵĵ߸ٶȵĸ˵-fomit-frame-
     pointerܻҲܲκε׬ͷνһݴ⡣棬x86ĻתָΪķνѵõļռҪframeõĻҪöࣻ仰˵IcacheԳʽԲûʵϵİǸ-
     fomit-frame-
     pointerĻͬʱҲǸ߱ÿκк֮ᣬͱѵָꣻȻframeеĴĻѵʱѻ

йⷽһλLinus

     ҪעǣҪõ״ִЧܣǧҵĻΣһҪвԡgccĲãпܾһرϣԸѱĽ

11/14/97 5/15/98

4.2.2  Internal compiler error: cc1 got fatal signal 11





 "

 Signal 11ָ SIGSEGV segmentation violationָͨ
˵gccԼõָе󣬶һд벻ļԣһgccbug
Ȼԣgccһ֧ܲҿɿõҲ˴ӵϽṹ뾪˵ָ֮Ҫѡһ˿RAMԳʽgccԿһժڡ޷¸ֻbug---¿ʼʱѶϢûһֱͬһط---Ǽȷ,Ӳ屾(CPU,,ǿȡ).ǧҪΪĵԿͨĲԡWin
dowsܵú˳ʲģͻعͷ˵gccһbugЩԶͨûʲʵϵļֵǺܺĽۡ⣬ҲҪΪʱͣڡmake
zImageĽ׶ΣҪgccbug---ȻͣǶmake zIm
ageʱҪĵܾͳ200һķ

 ظbugңдһСĳʽչʾֻbugĻͿ԰bug棬Ȼe
mailFSFlinux-
gcc̳ͨȥοgcc˵ļʲϸѶ,Ҫġ








The Linux GCC HOWTOV0.2						     10



4.3  ֲ

 ݱϢָĳڶûֲLin
uxȥԿ϶ǣһһֵҲûС:-)

ţһ㡣һԣԭʼֻҪһЩֲ޸ģͿԿ˷Linux
100%POSIXݵʡκε޸ģ˲ݴظԭߣǺнԵľٶֻҪõmakeܵõһִеĵˡ

4.3.1  BSDͽ ( bsd_ioctldaemon  <sgtty.h>)

 ʽʱ-I/usr/include/bsd-lbsdĳʽ⡣磺Make
fileڣ-I/usr/include/bsdӵCFLAGSһУ-
lbsdӵLDFLAGSһУҪBSD̬źΪҲҪټ-
D__USE_BSD_SIGNALˡΪ-I/usr/include/bsd뺬˱ͷ<sig
nal.h>֮ᣬmakeʱͻԶˡ

4.3.2  ʧķӡ

(SIGBUS, SIGEMT, SIGIOT, SIGTRAP, SIGSYS etc)




 "

 LinuxPOSIXȫݵġЩźŲPOSIX---ISO/IEC
9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:

     POSIX.1ʡSIGBUSSIGEMTSIGIOTSIG
     TRAPSIGSYSźţΪǵΪʵķʽϢϢأҲ޷ʵķࡣȷʵʽᣬԷЩźţǱļ˵ʲĻ·ͳģԼָκǵķչصơ

Ҫ⣬ҲķSI
GUNUSED¶ЩźšȷķӦʽı#ifdefЩŶԣ

     #ifdef SIGSYS
     /* ... non-posix SIGSYS code here .... */
     #endif

11/15/97 5/22/98

4.3.3  K & R

gccһANSIݵıֵǣĿǰĳʽ붼AN
SIı׼ȰANSIϲANSIṩı׼׫дCʽƺ˼-
traditional֮⣬ûʲԶ̸ˡThere is a certain amount of
finer-grained control over which varieties of brain damage to emu
late;вgcc info page

Ҫעǣ-tradition
alıԵԣЧҲgccܹܵġΧ, -tradi
tional-fwritable-
stringsʹִϼռ(ӳʽռƳطǲд)óʽļռӵġ










The Linux GCC HOWTOV0.2						     11



4.3.4  ǰôķîϺԭ


 "

ǣ֪Lin
uxೣõĺɾ޼ڱͷڣʱƵĺԭڳʽڣǰôܾ﷨ǰҵa
toi()atol()

4.3.5  sprintf()

 "

ڴ󲿷ݵUnixϵͳϣsprintf(string, fmt,
...)صstringָ꣬ȻⷽLinuxѭANSIصȴǷstringڵԪĿ.ֲʱSunOSоġ

4.3.6  fcntl صĺFD_*Ķ嵽װ?







 "

<sys/time.h>ͷ Ϊԭ棬fc
ntlҲ뺬ͷ<unistd.h>

һԣmanual pageSYNOPSIS½гҪıͷ

4.3.7  select()ļʱ---ʽִʱᴦæµ-ȴ״̬

 "

ܾúܾǰ,select()ļʱֻΨԶѡʹmanual
pagesȻεľ棺

     select()ӦǽʱֵеĻٴԭʼʱʼʣŵʱ䡣δİ汾ܻʹʵ֡ˣĿǰԣΪse
     lect()֮ᣬʱָȻᱻһַǳǵ뷨ร

δǵǰˣ٣ԿԿse
lect()صģǿ۳ȴδķѵʱᣬʣŵʱֵڼʱʱûϴͽʱΪ0κεse
lect()ͬļʱstructureУselect()̽

Ҫ⣬ֻҪÿκselect()ǰѼʱֵŵʱ struc
tureڣûˡĳʽ룬

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










The Linux GCC HOWTOV0.2						     12



ĳɣ

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

⣬Щ汾Mo
saic൱ģֻҪһεĵȴMosaic͹ˡMosaicөĻϽǣǲиԲԲġתĵ򶯻ǿת죬ͱʾϴ·ϴ͹

4.3.8  жϵϵͳ


 "

4.3.8.1  磺

һ֧ʽCtrl-ZֹȻִʱԲCtrl-
CжźŵӳսȡϵͳͻᱧԹ˵"interrupted system
call""write: unknown error"ѶϢ

4.3.8.2  㣺

POSIXϵͳźŵĴһЩɰUnixҪһ㡣Linuxܾͻִsig
nal handlersˡ

     ͬ(ʱĵδ)

     ϵͳеĴֵ

     ϵͳеִڼ select(), pause(), connect(),accept(), read()
     on terminals, sockets, pipes or files in /proc, write() on terminals,
     sockets, pipes or the line printer, open() on FIFOs, PTYs or serial
     lines,ioctl() on terminals, fcntl() with command F_SETLKW, wait4(), sys
     log(), any TCP or NFS operations.

ҵϵͳԣҪĿܾЩϵͳˣ creat(), close(),
getmsg(), putmsg(), msgrcv(), msgsnd(), recv(), send(), wait(), waitpid(),
wait3(), tcdrain(), sigpause(), semop() to this list.

ϵͳڼ䣬һźţ֧ʽӦ׼handlerӦˣhan
dlerͻᱻСhan
dlerȨתƻϵͳʱѾжϣҴֵ趨-1er
rno趨EINTRʽû뵽ᷢ£Ծ͹ˡ

ķѡ

(1) ÿаװsignal handlersigac
tionżSA_RESTART磬еĳʽ

       signal (sig_nr, my_signal_handler);










The Linux GCC HOWTOV0.2						     13



ĳɣ

       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()ioctl()ӡ

ԭʼĳʽƬΣʹ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; }
     }


ԭʼĳʽƬΣʹioctl()

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


޸ĳɣ

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

עһ㣬Щ汾BSD








The Linux GCC HOWTOV0.2						     14



UnixڶΪִϵͳСҪϵͳжϣʹ SV_INTER
RUPTSA_INTERRUPTš

4.3.9  дִ




 "

 gccusersֹܻ۵뷨ŵǴĳִʱ---ִֻѡˣִᴢڳʽļڡԭ
pageŵim
ageϣĵswapļռ䣬κγдľٶɷҳĴ(seg
mentation fault)һɫأ

Ͼһĳʽԣܻһ⡣磬mk
temp()ݵ(arguments)ִ
mktemp()᳢*ʵλ*д

ķ(a)-fwritable-
strings룬ʹgcc˳÷ϼռڣ(b)ַȨĲ¸дһΪִںǰstr
cpy()Ͽȥ

4.3.10	Ϊʲexecl()ʧܣ

 "

ΪеķʽԡexeclĵһҪִеĳʽ.ڶеĳʽargvСסͳϣargv[0]ֻеʽûдִʱŻ趨ֵޣӦд

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


ֻУ

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

ִгʽκɽͳһ뺯ĿǰѴ˳ʽĶ̬ʽӡ٣a.out
ġELFԡͲ.

֪˳ʽѶһЩ򵥵Ľãο̬һ½ڣlddman
ual page

11/16/97 6/2/98


5.  

5.1  Ԥƣlint

 "

 lintLinuxԲûкܹ㷺;ҪΪ󲿷ݵ˶gccṩľѶϢõľ-
Wall---;ҪgccеľѶϢֳbut probably has
more mnemonic value if thought of as the thing you bang your head against.








The Linux GCC HOWTOV0.2						     15



·һʵõpublic domain lintλ
<URL:ftp://larch.lcs.mit.edu/pub/Larch/lclint>Ҳ֪վжþˡ

5.2  

 "

5.2.1  ҪܽѶŵһ֧ʽͷ



 "

 Ҫ-gĲʽҲ-fomit-frame-
pointerʵϣ㲻Ҫ±еĳʽֻ±ĿǰڳĲݼɡ

a.out̬ԣʽ-fomit-frame-
pointerɣʱgdbͱӢ֮ˡʱ-gѡӦþ̬ˣΪʲҪ-
gԭˡ

ʧܣҲlibg.aǾ/usr/lib/Ŀ¼£libg.alibg.aһCԺرʽ⡣һlibc׼ھͻṩlibg.aȻĻ°ģҪlibcԭʼԼˣʵӦòҪŶԡʲĿģ󲿷ݵ£ֻ轫libg.aᵽ/usr/lib/libc.aܵõ㹻Ѷˡ

5.2.1.1  ǣܲܰѳѶõ

 "

ܶGNUڱʱ趨-gѡִе⣨ͨǾ̬ᣩʵϣⲢһŵ뷨

ʽautoconfconfigure壬ͨͿ./configure
CFLAGS=./configure CFLAGS=-O2صѶȻĻüMake
fileˡȻõELFʽԶ̬ķʽᣬǷ-
g趨ƽİ-gõ

5.2.2  ʵõ

 "

˽⣬󲿷ݵ˶gdbԴGNU archive sites7
õԭʼʽǵtsx-118
ÿִеxxgdbһXĳʽֲgdbҲ˵Ȱװgdbװxxgdbxxgdbԭʼ<URL:ftp://ftp.x.org/con
trib/xxgdb-1.08.tar.gz>ҵ

⣬UPSʽRick Slad
keyֲɹUPSX»úܺãxxgdb---gdbXǰ˽棨X
front
end֧ʽһص㣬ûʱȥһ֧õĳʽ㿼ǿxxgdbȱõLinuxԭʼ<URL:ftp://sun
site.unc.edu/pub/Linux/devel/debuggers/>ҵԭʼʽ
<URL:ftp://ftp.x.org/contrib/ups-2.45.2.tar.Z>


____________________

7. <URL:ftp://prep.ai.mit.edu/pub/gnu>

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







The Linux GCC HOWTOV0.2						     16



ܻᷢһĹstraceҲ൱áʾɳϵͳУһӵڶ෱ĹܣֱûԭʼĻstrace԰ҳЩ·ƣpath-
namesѱִеڣ exacerbating race conditions in programs that you
suspect contain
them;Уstraceѧϰʽڵִеġµİ汾Ŀǰ3.0.8ҵ<URL:ftp://ftp.std.com/pub/jrs/>

5.2.3  ʽפʽ

ڵ͵ĳפʽ(daemon pro
grams)ִfork()Ȼֹʹóʱˡ

˽򵥵ķfork()һжϵ㣨break
pointʽֹͣʱǿfork()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  ĵ

Linuxʱ̬ͨ趨ɲҪĵҪϲǵĻshellbuilt
inʹЧC-shellݵshelltcshԣ

     % limit core unlimited


Bourne shellshellsh, bash, zsh, pdkshʹ﷨

     $ ulimit -c unlimited

ҪиŶյĺĵcore file namingfor example, if you're
trying to conduct a post-mortem using a debugger that's buggy
itselfԶĺĳʽһССĸһfs/binfmt_aout.cfs/binfmt_elf.cĳʽƬ(in








The Linux GCC HOWTOV0.2						     17



newer kernels, you'll have to grep around a little in older ones)

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

01.

5.3  

 ܣProfil
ingһ֧ʽЩлִеʱõķԳʽѻҳʱʱ˷ѵĶԣ൱õķʽҪʱѶtim
ing informationĿĵ-
p룬Ҫĵ壬ҲҪgprofbinu
tils׼gprofmanual pageɵ֪ϸڡ

11/18/97


6.  

춾̬빲ʽ߼䲻ݵĸʽĲ붯*link*ʹָ***õĳʽʹʱ*ͷʹһ½ڱøࡣ
and, actually, the overloading of the word `load' in a comparable but opposite
senseٸҲˣԸ²ع춵ġ

Ϊ΢ߵǳִڼΪ*̬*һһ½̸ҲڱĵطҰѶ̬*̬*һ½С仰˵һ½̸ģȫָڱᡣ

6.1  ʽ vs̬ʽ

ʽһ᣻ҲǽзɢСʽǷ©ЩʲᡣȻһЩǺܶʽ---磬뿪йصСʽͻὫʽصṩĳʽʹáһLin
uxϵͳϣЩСʽ/lib/usr/lib/Ŀ¼ҵ




õǾ̬ĳʽʱҳʽģ飬ȻʵʽǿִеڡȻԹʽԣͲˡʽִеһǺţָ*ʽִʱȱʽ*ȻʽͼʹִеøСͬʹøٵļŵռ䡣Lin
uxڶΪṲʽ⣬ֻҪLin
uxҵЩʽĻûʲ⣻Ȼ,Linuxͻᾲ̬ˡҪʽĻЩʽ⣨*.sa
for a.out, *.so for ELFǷסǸڵĵطǿɶȡġ

Linuxϣ̬ʽlibname.aƣʽΪlib
name.so.x.y.z˴x.y.zָ汾ŵʽʽָ̬ͨʽ⣨Ҫģ.sa׼ĳʽ뾲̬ʽָʽ

lddList 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

˵ҵϵͳϣWWW*lynx*libc.so.5 (the C library)libn
curses.so.1ն˻өĻĿƣĴڡĳ֧ʽȱԣ








The Linux GCC HOWTOV0.2						     18



lddͻ˵statically linkedǡstatically linked (ELF)

6.2  ռУsin() ĸʽ



 "

nm
ʽӦûг˳ʽοзšָӦھ̬빲ʽϡ֪tcge
tattr()Ķģ

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

*U*ָ*δ*---Ҳ˵ncursesʽõtege
tattr()ǲûжҲ

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

*W*˵*̬(weak)*ָѶ壬ɲͬʽеһ򵥵**壨tcgetp
grp*T*ʾ



̸⣬Ĵ𰸱libm.(so|a)ˡж<math.h>ĺڭ
mathsʽڣˣõκһʱҪ-
lmĲ˳ʽ⡣

6.3  X

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

 ldƵѰĲϣݰ汾ĲͬΨһһԺڶĿ¼/usr/libˡϣĳʽҲѰУͱ-
Lѡ֪gccld

Ҫ㷢һЧҲûУ͸Ͻ쿴ǵǲǻԹԵԭءa.outԣ-
lfooᣬʹldȥѰlibfoo.sashared
stubsûгɹͻỻѰlibfoo.astaticELFԣ ldlib
foo.soȻlibfoo.alibfoo.soͨһţlibfoo.so.x

6.4  Լĳʽ

6.4.1  ư汾

κεĳʽһʽҲbugsڡҲܲһЩµص㣬ĿǰڵģĹЧǽɵƳʹǵĳʽԣܻһ⡣һ֧ʽǸЩɵصִеĻ죿

ԣ˳ʽ汾ŵĹǽʽ*Ҫ**Ҫ*ıű࣬ͬʱ涨*Ҫ*ıǲõʽľɳʽжϵԴӳʽĵֱİ汾ʵϣϸELFԽһĻԣȥΪʲˣ
lib
foo.so.1.2Ҫ汾1Ҫ汾2Ҫ汾ıſ£Ҳʲᶼû---libcһ*̶*Ĺlibc.so.5.2.18ĳʽơҪ汾ıǷһЩĸߡκοӡASCIIԪҲǺܺġ









The Linux GCC HOWTOV0.2						     19



ELFa.outʽҪĲ֮һùʽϣȿELFΪȽϼһЩ

6.4.2  ELFʲᶫ˨ߣ

 "

 ELFExecutable and Linking FormatUSLUNIX System Laborato
riesչɵĶλʽĿǰӦSolarisSystem V Release
4ϡELFǵĵԶԶLinuxȥõa.outʽGC
CCʽķչʿ1995ELFΪLinux׼Ķλʽ

6.4.2.1  ˣ

 һ춡/news-archives/comp.sys.sun.miscļ

     ELFExecutable Linking For
     matSVR4ʽĿĵʽELF
     COFFǶ˲ٵĹܡELFԣ**ʹġELFһĿĵΪsec
     tions紮аϣҴ˴пΪĳȣһ̶СУЩ
     COFFĲһҪ̶ĳطҲҪĳ˳Сʹϣ׽µϣԼµĽĿĵڡELFҲһǿĳʽΪDWARFDebug
     ging With Attribute Record FormatĿǰLinuxȫ֧ԮDWARF
     DIEsDebugging Information EntriesᴮлELFγ
     .debugĽDWARF DIEsÿһ
     .debugһЩҹ̶СѶ¼ļϣһⳤȵĴУӵиӵԣҳʽϻСΧƵ״ϽṹдDIEsܲ׽ĴѶCOFF
     .debug޷ġC++ļ̳ͼ

     ELFǴSVR4Solaris 2.0 ELFȡʽ⣨ELF access
     libraryڴȡġ˳ʽṩһٵĽELFʹELFȡʽҪĶ֮һǣ㲻Ҫȥ쿴һELFquaˡUNIXĵԣElf*ʽȡelf_open()֮ᣬӴʱʼֻelf_foo
     bar()ĳһݼɣҪѵʵڴŵϵim
     ageһҡ

ELFȱELFȼ辭ʹ࣬ELF-HOW
TOۼҲͿELF HOWTOӦļͬǡ

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ʵᣨlib
foo.soʹö̬ʽdynamic loaderҵlib
foo.so.1Ϊ˽вԣǽĿǰĿ¼ӵLD_LIBRARY_PATH



ֵ춳ʽɹ֮ʱ˰Ƶ/usr/local/libĿ¼£趨ȷ·lib
foo.so.1libfoo.so.1.0ldcon
figڲϵĸ£ʹ󲿷ݵϵͳ˵ldconfigڿִСlib
foo.soֶʽ¡ԳʽɷӣͷȣǱһ˿̬ȣ򵥵ķlib








The Linux GCC HOWTOV0.2						     20



foo.so -> libfoo.so.1һldcon
figͬʱµᡣҪû趨Ķͻǧٹֵ֡ʱ򣬿ɱ˵û㰡

     $ 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


 "

ÿһʽⶼһson
ameѰĳʽһƣὫson
ameеĶλڣʵʵĵڳʽִڼ䣬̬ʽѰӵson
ameĵĵǳʽĵˣһΪlib
foo.soĳʽ⣬Ϳһlibbar.sosonameˡᵽlib
bar.soĳʽʽʼִʱѰҵılibbar.soˡ

һҲûУһ㣬˽ͬ汾ͬһʽڵһϵͳϹԭȴǹؼ֮ԿLinuxʽ׼ʽ˵lib
foo.so.1.2Ҹʽһlibfoo.so.1son
ame˳ʽǼӵ׼ʽĿ¼£e.g. /usr/libldcon
figὨlibfoo.so.1 -> libfoo.so.1.2ʹȷim
ageִڼҵҲҪlibfoo.so -> lib
foo.so.1ʹldڼҵȷsoname

ޣʽڵbugsµĺȥκβִĳʽɲӰĸı䣩ؽ˳ʽ⣬ԭеson
ameȻĳʽ⵵Գʽıʹеĳʽжϣֻson
ameеı---У°汾Ϊlibfoo.so.2.0sonamelib
foo.so.2ٽlibfoo.soתµİ汾ˣٶȻָ˺ƽ

ʵ㲻ҪԴַʽʽȷǸõĴͳELFڳʽϵĵԣʹĸ㲻״ĵڣҲʾ͵ȥ

ELFܽ᣺辭ǵĹ۲췢и˵ʽҪƻԣҪܲ᣻ķʽᣬеһоͶలˡ

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

6.4.3  a.out---ɾɵĸʽ

ʽıELFҪԭ֮һҲ˵,a.outܻôڵġftpվȥץ<URL:ftp://tsx-11.mit.edu/pub/linux/pack
ages/GCC/src/tools-2.17.tar.gz>ѹᷢ20ҳļĶҺܲϲԼɵƫֵ£Ǵļ䣬ӦҲԺҴʯͷԼĽŵƢɣ:-)

6.4.3.1  ZMAGIC vs QMAGIC


 "

QMAGICһƾɸʽa.outΪZMAGICĿִе
ʽָʽʹõһҳ޷map0-4096ġΧûmap
pingʱNULL dereference trap
pingӵסı߽ЧӦִеȽСԼ1Kң

ֻмϵ֧ԮZMAGICһײĵ֧ԮָʽĿǰİ汾֧ԮQMAGICѡʵϣⲢûжӰ죬ΪĿǰĺָʽִС








The Linux GCC HOWTOV0.2						     21



*file*ӦÿȷϳʽǲQMAGICĸʽġ

6.4.3.2  

һa.out(DLL)Ĺʽʵĵһš*foo*ļΪĳʽԣЩlib
foo.salibfoo.so.1.2Żlibfoo.so.1һָlib
foo.so.1.2Щʲõģ

ڱʱldѰlib
foo.saǳʽ*stub*Һִڼex
portedָָꡣ

ִڼ䣬̬ʽѰlib
foo.so.1һᣬʵĵʳʽɸ³ɽµİ汾κδʱʹô˳ʽӦóʽ°---˵lib
foo.so.1.3---ʱldcon
figһ΢СĲָµİ汾ʹκԭʹþɰĳʽе˿Ĳá

DLLʽ⣨֪νķ---Զϰɣͨǵľ̬Ҫôࡣ*holes*ʽռԱ䡣**ԲռκεĴŵռ䡣һ򵥵cpУʹmake
holeʽͿԴﵽЧΪǵλַǹ̶ͬһλϣڽʽᣬ԰õǧҪõELFĳʽ⡣

6.4.3.3  ``libc-lite''?

libc-
litelibc汾ڴŵƬϣҲ󲿷ݵ΢UNIXβûаcurses,
dbm, term
capȵȵĳʽ롣/lib/libc.so.4ᵽһlitelibcὨİ汾ȡ

6.4.4  ᣺

ʱĸңҿʲҲֻҪۻ㹻һд*

      빲ƫƫʽȴɾ̬ģ



	    ṩldǷȷʹldҵÿһӦĹʽ⣬ELFԣָһlib
	    foo.soimagea.outԣlibfoo.saˡܶ˽ELF
	    binutils
	    2.52.6֮ᣬͲ---ڵİ汾ѰʽʱǻۣԲûнеὨΪļܹݣǻ۵Ϊ˸ɾˣ⣬*ǻ*жϴĻ൱ߣɵ鷳⻹࣬ҲǺ˾ȥ⣡

      DLLĹ߳ʽmkimageҲlibgcc


	    libc.so.4.5.x֮ᣬlibgc
	    cѲǹĸʽˣ*-lgcc*֮`gcc -print-
	    libgcc-file-name`ĵţback-
	    quotes⣬ɾ/usr/lib/libgcc*ĵҪ

      __NEEDS_SHRLIB_libc_4 multiply defined messages
	    ͬɵһֽ

      ``Assertion failure'' message when rebuilding a DLL ?
	    һصѶϢпܵԭǣԭʼjump.varsڣ춱Ŀռ̫٣һjump
	    table








The Linux GCC HOWTOV0.2						     22



	    slotsִй߳ʽ2.17.tar.gz׼ṩġget
	    sizeɷټΨһĽǣ˳ʽҪİ汾ţǿصݵ

      ld: output file needs shared library libc.so.4
	    ͨǷڵĳʽⲻlibcXʽ⣩-
	    gĲȴûһʹ-staticĴѶϢ

	    ʽ.sa
	    stubsͨһδķ_NEEDS_SHRLIB_libc_4һɽlibc.sa
	    stubȻ-
	    gʱʹlibg.alibc.aһֱûнҲͻᵼĴѶϢˡ

	    ֮-gűʱ˼-staticȻͱ-
	    gᡣͨ-
	    gĵʱõĳѶѾ㹻ʱͿԲҪˡ


7.  ̬

һ½ĿǰǼһ㣻ӾELF HOWTOʱⲿٶչʱˡ

7.1  

 Lin
uxйʽ⣬֮ǰһ½ڣһ˵ʣ̸еͷ衣һЩչʱڱɵĹӳٵʱڲɡ

7.2  ѶϢ

ĴĸңҲκε£ҿ԰д**

      can't load library: /lib/libxxx.so, Incompatible version
	    a. out only ָûxxxʽȷҪ汾ɱΪ
	    ŪᵽĿǰӵеİ汾Ϳˣ˵Ļֻĳʽҳѡȥץµİ汾.ELFƵѶϢ

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

      warning using incompatible library version xxx
	    a. out
	    onlyĳʽĴҪ汾֧ʽĻҪɡʽȻִСֻǿ룬Ӧûʲ˺ɣ

7.3  ƶ̬


 "

һ黷ö̬Ӧ󲿷ݵĻldd;ҪһusersĻҪøࡣҿԺܷ趨ld
dϸֲִСЩ

    LD_BIND_NOW ---
     ں֮ǰǲóʽѰҵġ趨Żʹóʽһ룬еѰұᷢͬʱҲʼʱԳʽȷеᶼûʱžͱúá

    LD_PRELOAD趨һʹ**磬ҪԼķԣһû*mal
     loc*д׼滻ĸʽmallolc.Ȼ᣺









The Linux GCC HOWTOV0.2						     23



	  $ LD_PRELOAD=malloc.o; export LD_PRELOAD
	  $ some_test_program



     LD_ELF_PRELOAD  LD_AOUT_PRELOAD
     ƣǽȷĶλ̬趨 LD_something_PRELOAD 
     LD_PRELOAD Ƚȷһᱻõ

   
     LD_LIBRARY_PATHһԷֺŸĿ¼ƣѰʽ⡣ldԣûκεӰ죻ִֻڼӰ졣⣬ִse
     tuidset
     gidĳʽԣһЧġLD_ELF_LIBRARY_PATHLD_AOUT_LIBRARY_PATHſɸݸĶλʽֱͬѰ·һ£ӦûõLD_LIBRARY_PATHҪѰĿ¼ӵ/etc/ld.so.conf/Ȼִld
     config

     LD_NOWARN a.outһ趨һLD_NOWARN=true; export
     LD_NOWARN봦fatal-warn
     ingsǴҪ汾ݵȣľѶϢ

    LD_WARNELF趨һʱὫͨѶϢġCan*t find
     libraryתɾѶϢĲԣⲢûжôǶlddͺҪˡ

    LD_TRACE_LOADED_OBJECTSELFһʹóʽΪld
     dִеģ


	  $ 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  Զ̬׫дʽ


 "

ϤSolaris 2.x֧ԮĶ̬ĹĻᷢLin
uxǳһH.J.LuELFʽļdlopen(3)man
ual pageld.so׼ҵй㷺ۡиļ򵥡-
ldlᡣ





















The Linux GCC HOWTOV0.2						     24



     #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

 "

 дLinuxģgccϵͳ⡣ker
nelİ汾𣿻ǳʽİ汾þ̬ʽᣬǲǾʧˣԽ¼һСγʽչʾֻbug

Щ֮ᣬ㽫֪ʽڵbugsʲᡣgccԣbuginfo˵ġld.soCmathsʽ⣬emailĵlinux-
gcc@vger.rut
gers.eduܵĻһ֧ԼСʽչʾbugҸ˵Ҫ֧ʽЩʲʵЩʲᡣ

8.2  Эչ

ҪæչgccCʽ⣬һ¾Ǽlinux-gcc@vger.rut
gers.eduֻ̳ͨ뿴̳ͨЩʲᣬһ̳archivesλ<URL:http://homer.ncm.com/linux-
gcc/>£Ϳʲˡ


9.  

9.1  ˰


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


(Mark Twain)

HOWTOļȫֲMitchum DsouzaGCC-FAQ
ļд󲿷ݵѶֱGCC-
FAQļõĵһ˳ƴʣΪһ˵Ҫһ˵һûвԹЩӲϵͳǸɢģɱ˵û㰡"Ļϡ

ļй׵ʿУASCII˳г Andrew Tefft, Axel
Boldt, Bill Metzenthen, Bruce Evans, Bruno Haible, Daniel Barlow, Daniel








The Linux GCC HOWTOV0.2						     25



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, ȻLinux Tor
valdsû˶ͱһҲûˣԲµġ:-)

벻Ҫκεð֮ûгļHOWTOǭ
FAQй׵Ļemailңһġ

9.2  

ĿǰΪֹļû֪ķ汾֡ϣһ뾡ȥһøصˡҽԻҪԣǻʣźֻкüٷ֮һȰѱ˰һ԰ɣʲ⣬Ҷæġ

9.3  ӭκεĻ

Ÿdan@detached.demon.co.ukҵPGP public key (ID 5F263625)
ҵĺ༦web pages9 ʹãбҪܵĻ

9.4  Ϸɹ涨

Ĳù̱춸ĳߡ

HOWTOļİȨDaniel Barlow <dan@detached.demon.co.uk>
κʽý壬εġӵķʽظݻݵGCC-
HOWTOֻҪݰȨеĸƷСҵĽģҹҺϣ֪ͨйصϸڡ

ʲᣬǷ롢ļļصļLinux
HOWTOļȣѭݰȨҲ˵HOW
TOļд˵ļǲԶļ϶ƣӰļķȨġȻЩرǿģ㣬LinuxЭ磬ʼĵַͷ

֮ܶϣеHOW
TOļܹ͸ֲͬĹܵܵĴȥȻͬҲϣݰȨԴļͷκηHOW
TOsļļƻǶԸӵ֪ͨ

κ⣬Linux HOWTOЭTim Bynumϵemail addressgregh@sun
site.unc.edu

ԭת£ 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. Com
mercial 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.

____________________

9. <URL:http://ftp.linux.org.uk/~barlow/>







The Linux GCC HOWTOV0.2						     26



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 doc
uments, 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.

6/3/98


10.  

ͷֵԪоͰASCII˳

     -fwritable-strings 39 (section .39-hn .tm "GETHN:index.39 not defined" ,
     page .39-pn .tm "GETPN:index.39 not defined" ) 56 (section .56-hn .tm
     "GETHN:index.56 not defined" , page .56-pn .tm "GETPN:index.56 not
     defined" )

     /lib/cpp 16 (section .16-hn .tm "GETHN:index.16 not defined" , page
     .16-pn .tm "GETPN:index.16 not defined" )

     a.out 1 (section .1-hn .tm "GETHN:index.1 not defined" , page .1-pn .tm
     "GETPN:index.1 not defined" )

     ar 10 (section .10-hn .tm "GETHN:index.10 not defined" , page .10-pn .tm
     "GETPN:index.10 not defined" )

     as 8 (section .8-hn .tm "GETHN:index.8 not defined" , page .8-pn .tm
     "GETPN:index.8 not defined" )

     <asm/*.h> 19 (section .19-hn .tm "GETHN:index.19 not defined" , page
     .19-pn .tm "GETPN:index.19 not defined" )

     atoi() 40 (section .40-hn .tm "GETHN:index.40 not defined" , page .40-pn
     .tm "GETPN:index.40 not defined" )

     atol() 41 (section .41-hn .tm "GETHN:index.41 not defined" , page .41-pn
     .tm "GETPN:index.41 not defined" )

     binaries too big 63 (section .63-hn .tm "GETHN:index.63 not defined" ,
     page .63-pn .tm "GETPN:index.63 not defined" ) 65 (section .65-hn .tm
     "GETHN:index.65 not defined" , page .65-pn .tm "GETPN:index.65 not
     defined" ) 77 (section .77-hn .tm "GETHN:index.77 not defined" , page
     .77-pn .tm "GETPN:index.77 not defined" )

     chewing gum 3 (section .3-hn .tm "GETHN:index.3 not defined" , page .3-pn
     .tm "GETPN:index.3 not defined" )

     cos() 68 (section .68-hn .tm "GETHN:index.68 not defined" , page .68-pn
     .tm "GETPN:index.68 not defined" )

     debugging 59 (section .59-hn .tm "GETHN:index.59 not defined" , page
     .59-pn .tm "GETPN:index.59 not defined" )








The Linux GCC HOWTOV0.2						     27



     dlopen() 82 (section .82-hn .tm "GETHN:index.82 not defined" , page
     .82-pn .tm "GETPN:index.82 not defined" )

     dlsym() 83 (section .83-hn .tm "GETHN:index.83 not defined" , page .83-pn
     .tm "GETPN:index.83 not defined" )

     documentation 4 (section .4-hn .tm "GETHN:index.4 not defined" , page
     .4-pn .tm "GETPN:index.4 not defined" )

     EINTR 52 (section .52-hn .tm "GETHN:index.52 not defined" , page .52-pn
     .tm "GETPN:index.52 not defined" )

     elf 0 (section .0-hn .tm "GETHN:index.0 not defined" , page .0-pn .tm
     "GETPN:index.0 not defined" ) 71 (section .71-hn .tm "GETHN:index.71 not
     defined" , page .71-pn .tm "GETPN:index.71 not defined" )

     execl() 57 (section .57-hn .tm "GETHN:index.57 not defined" , page .57-pn
     .tm "GETPN:index.57 not defined" )

     fcntl 47 (section .47-hn .tm "GETHN:index.47 not defined" , page .47-pn
     .tm "GETPN:index.47 not defined" )

     FD_CLR 44 (section .44-hn .tm "GETHN:index.44 not defined" , page .44-pn
     .tm "GETPN:index.44 not defined" )

     FD_ISSET 45 (section .45-hn .tm "GETHN:index.45 not defined" , page
     .45-pn .tm "GETPN:index.45 not defined" )

     FD_SET 43 (section .43-hn .tm "GETHN:index.43 not defined" , page .43-pn
     .tm "GETPN:index.43 not defined" )

     FD_ZERO 46 (section .46-hn .tm "GETHN:index.46 not defined" , page .46-pn
     .tm "GETPN:index.46 not defined" )

     file 2 (section .2-hn .tm "GETHN:index.2 not defined" , page .2-pn .tm
     "GETPN:index.2 not defined" )

     <float.h> 20 (section .20-hn .tm "GETHN:index.20 not defined" , page
     .20-pn .tm "GETPN:index.20 not defined" )

     gcc 6 (section .6-hn .tm "GETHN:index.6 not defined" , page .6-pn .tm
     "GETPN:index.6 not defined" )

     gcc -fomit-frame-pointer 61 (section .61-hn .tm "GETHN:index.61 not
     defined" , page .61-pn .tm "GETPN:index.61 not defined" )

     gcc -g 60 (section .60-hn .tm "GETHN:index.60 not defined" , page .60-pn
     .tm "GETPN:index.60 not defined" )

     gcc -v 14 (section .14-hn .tm "GETHN:index.14 not defined" , page .14-pn
     .tm "GETPN:index.14 not defined" )

     gcc, bugs 15 (section .15-hn .tm "GETHN:index.15 not defined" , page
     .15-pn .tm "GETPN:index.15 not defined" ) 28 (section .28-hn .tm








The Linux GCC HOWTOV0.2						     28



     "GETHN:index.28 not defined" , page .28-pn .tm "GETPN:index.28 not
     defined" ) 29 (section .29-hn .tm "GETHN:index.29 not defined" , page
     .29-pn .tm "GETPN:index.29 not defined" ) 84 (section .84-hn .tm
     "GETHN:index.84 not defined" , page .84-pn .tm "GETPN:index.84 not
     defined" )

     gcc, flags 13 (section .13-hn .tm "GETHN:index.13 not defined" , page
     .13-pn .tm "GETPN:index.13 not defined" ) 25 (section .25-hn .tm
     "GETHN:index.25 not defined" , page .25-pn .tm "GETPN:index.25 not
     defined" ) 26 (section .26-hn .tm "GETHN:index.26 not defined" , page
     .26-pn .tm "GETPN:index.26 not defined" )

     gdb 64 (section .64-hn .tm "GETHN:index.64 not defined" , page .64-pn .tm
     "GETPN:index.64 not defined" )

     header files 17 (section .17-hn .tm "GETHN:index.17 not defined" , page
     .17-pn .tm "GETPN:index.17 not defined" )

     interrupted system calls 51 (section .51-hn .tm "GETHN:index.51 not
     defined" , page .51-pn .tm "GETPN:index.51 not defined" )

     ld 9 (section .9-hn .tm "GETHN:index.9 not defined" , page .9-pn .tm
     "GETPN:index.9 not defined" )

     LD_* environment variables 80 (section .80-hn .tm "GETHN:index.80 not
     defined" , page .80-pn .tm "GETPN:index.80 not defined" )

     ldd 81 (section .81-hn .tm "GETHN:index.81 not defined" , page .81-pn .tm
     "GETPN:index.81 not defined" )

     libc 7 (section .7-hn .tm "GETHN:index.7 not defined" , page .7-pn .tm
     "GETPN:index.7 not defined" )

     libg.a 62 (section .62-hn .tm "GETHN:index.62 not defined" , page .62-pn
     .tm "GETPN:index.62 not defined" )

     libgcc 79 (section .79-hn .tm "GETHN:index.79 not defined" , page .79-pn
     .tm "GETPN:index.79 not defined" )

     <limits.h> 21 (section .21-hn .tm "GETHN:index.21 not defined" , page
     .21-pn .tm "GETPN:index.21 not defined" )

     lint 58 (section .58-hn .tm "GETHN:index.58 not defined" , page .58-pn
     .tm "GETPN:index.58 not defined" )

     <linux/*.h> 18 (section .18-hn .tm "GETHN:index.18 not defined" , page
     .18-pn .tm "GETPN:index.18 not defined" )

     manual pages 5 (section .5-hn .tm "GETHN:index.5 not defined" , page
     .5-pn .tm "GETPN:index.5 not defined" )

     <math.h> 70 (section .70-hn .tm "GETHN:index.70 not defined" , page
     .70-pn .tm "GETPN:index.70 not defined" )









The Linux GCC HOWTOV0.2						     29



     maths 69 (section .69-hn .tm "GETHN:index.69 not defined" , page .69-pn
     .tm "GETPN:index.69 not defined" )

     mktemp() 55 (section .55-hn .tm "GETHN:index.55 not defined" , page
     .55-pn .tm "GETPN:index.55 not defined" )

     optimisation 27 (section .27-hn .tm "GETHN:index.27 not defined" , page
     .27-pn .tm "GETPN:index.27 not defined" )

     QMAGIC 76 (section .76-hn .tm "GETHN:index.76 not defined" , page .76-pn
     .tm "GETPN:index.76 not defined" )

     segmentation fault 30 (section .30-hn .tm "GETHN:index.30 not defined" ,
     page .30-pn .tm "GETPN:index.30 not defined" ) 54 (section .54-hn .tm
     "GETHN:index.54 not defined" , page .54-pn .tm "GETPN:index.54 not
     defined" )

     segmentation fault, in GCC 33 (section .33-hn .tm "GETHN:index.33 not
     defined" , page .33-pn .tm "GETPN:index.33 not defined" )

     select() 50 (section .50-hn .tm "GETHN:index.50 not defined" , page
     .50-pn .tm "GETPN:index.50 not defined" )

     SIGBUS 34 (section .34-hn .tm "GETHN:index.34 not defined" , page .34-pn
     .tm "GETPN:index.34 not defined" )

     SIGEMT 35 (section .35-hn .tm "GETHN:index.35 not defined" , page .35-pn
     .tm "GETPN:index.35 not defined" )

     SIGIOT 36 (section .36-hn .tm "GETHN:index.36 not defined" , page .36-pn
     .tm "GETPN:index.36 not defined" )

     SIGSEGV 31 (section .31-hn .tm "GETHN:index.31 not defined" , page .31-pn
     .tm "GETPN:index.31 not defined" ) 53 (section .53-hn .tm "GETHN:index.53
     not defined" , page .53-pn .tm "GETPN:index.53 not defined" )

     SIGSEGV, in gcc 32 (section .32-hn .tm "GETHN:index.32 not defined" ,
     page .32-pn .tm "GETPN:index.32 not defined" )

     SIGSYS 38 (section .38-hn .tm "GETHN:index.38 not defined" , page .38-pn
     .tm "GETPN:index.38 not defined" )

     SIGTRAP 37 (section .37-hn .tm "GETHN:index.37 not defined" , page .37-pn
     .tm "GETPN:index.37 not defined" )

     sin() 67 (section .67-hn .tm "GETHN:index.67 not defined" , page .67-pn
     .tm "GETPN:index.67 not defined" )

     soname 73 (section .73-hn .tm "GETHN:index.73 not defined" , page .73-pn
     .tm "GETPN:index.73 not defined" )

     sprintf() 42 (section .42-hn .tm "GETHN:index.42 not defined" , page
     .42-pn .tm "GETPN:index.42 not defined" )









The Linux GCC HOWTOV0.2						     30



     statically linked binaries, unexpected 66 (section .66-hn .tm
     "GETHN:index.66 not defined" , page .66-pn .tm "GETPN:index.66 not
     defined" ) 78 (section .78-hn .tm "GETHN:index.78 not defined" , page
     .78-pn .tm "GETPN:index.78 not defined" )

     <stdarg.h> 23 (section .23-hn .tm "GETHN:index.23 not defined" , page
     .23-pn .tm "GETPN:index.23 not defined" )

     <stddef.h> 24 (section .24-hn .tm "GETHN:index.24 not defined" , page
     .24-pn .tm "GETPN:index.24 not defined" )

     strings 11 (section .11-hn .tm "GETHN:index.11 not defined" , page .11-pn
     .tm "GETPN:index.11 not defined" )

     <sys/time.h> 48 (section .48-hn .tm "GETHN:index.48 not defined" , page
     .48-pn .tm "GETPN:index.48 not defined" )

     <unistd.h> 49 (section .49-hn .tm "GETHN:index.49 not defined" , page
     .49-pn .tm "GETPN:index.49 not defined" )

     <varargs.h> 22 (section .22-hn .tm "GETHN:index.22 not defined" , page
     .22-pn .tm "GETPN:index.22 not defined" )

     version numbers 12 (section .12-hn .tm "GETHN:index.12 not defined" ,
     page .12-pn .tm "GETPN:index.12 not defined" ) 74 (section .74-hn .tm
     "GETHN:index.74 not defined" , page .74-pn .tm "GETPN:index.74 not
     defined" )

     weird things 72 (section .72-hn .tm "GETHN:index.72 not defined" , page
     .72-pn .tm "GETPN:index.72 not defined" )

     ZMAGIC 75 (section .75-hn .tm "GETHN:index.75 not defined" , page .75-pn
     .tm "GETPN:index.75 not defined" )





























The Linux GCC HOWTOV0.2						     31





































































				   CONTENTS



1.  Զǣ ............................................................ 1
    1.1 ߵĻ ............................................................ 1
    1.2 뾲 .............................................................. 2
    1.3 ߵ˽ .......................................................... 2
    1.4 ӡˢŰ .......................................................... 2

2.  Ķ ............................................................ 2
    2.1 GCC-HOWTOĶ ................................................... 3
    2.2 GCCصĶ ............................................. 3
    2.3 GCC   ............................................................... 3
    2.4 Cʽͷ ..................................................... 3
    2.5 йĹ (as, ld, ar, strings etc) .............................. 4

3.  GCCİװGCC趨 .................................................... 4
    3.1 GCCİ汾 ........................................................... 4
    3.2 װᶼĶȥ? ............................................. 5
    3.3 ͷ˨?ͷ˨? .............................................. 6
    3.4 (Building cross compilers) ............................ 7

4.  ֲʽʽ ...................................................... 7
    4.1 gccжķ ................................................... 7
    4.2 ˵ ........................................................ 8
    4.3 ֲ ........................................................... 10

5.   ............................................................. 14
    5.1 Ԥƣlint ............................................... 14
    5.2  ............................................................... 15
    5.3  ............................................................... 17

6.   ................................................................... 17
    6.1 ʽ vs̬ʽ ............................................ 17
    6.2 ռУsin() ĸʽ ............................. 18
    6.3 X ............................................................ 18
    6.4 Լĳʽ ................................................. 18

7.  ̬ ............................................................... 22
    7.1  ........................................................... 22
    7.2 ѶϢ ........................................................... 22
    7.3 ƶ̬ ............................................... 22
    7.4 Զ̬׫дʽ ................................................. 23

8.  뷢չʿ ......................................................... 24
    8.1 Bug ............................................................ 24
    8.2 Эչ ........................................................... 24

9.   ................................................................... 24
    9.1 ˰ ............................................................. 24
    9.2  ............................................................... 25
    9.3 ӭκεĻ ..................................................... 25
    9.4


				       i









	Ϸɹ涨 ..................................................... 25

10.  ................................................................... 26





















































				       ii


