home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / DOC / HOWTO / OTHER_FO / SGML / GCC_HOWT.GZ / GCC-HOWTO.sgml
Encoding:
SGML Document  |  1996-03-04  |  69.4 KB  |  1,751 lines

  1.              <!-- Warning to the author: -->
  2. <!-- Automatically generated by ./makeindex.pl: EDIT THE SOURCE INSTEAD -->
  3. <!doctype linuxdoc system>   <!--  Oy!  -*- SGML -*- mode, for this one -->
  4. <!-- $Id: gcc-howto.sgmlp,v 1.17 1996/02/28 19:35:50 dan Exp dan $ -->
  5. <article>
  6.  
  7. <title>The Linux GCC HOWTO
  8. <author>Daniel Barlow <tt><dan@detached.demon.co.uk></tt>
  9. <date>v1.17, 28 February 1996
  10.  
  11. <abstract>
  12. This document covers how to set up the GNU C compiler and development
  13. libraries under Linux, and gives an overview of compiling, linking,
  14. running and debugging programs under it.  Most of the material in it
  15. has been taken from Mitch D'Souza's GCC-FAQ, which it replaces, or the
  16. ELF-HOWTO, which it will eventually largely replace.
  17.  
  18. This is the first publically released version (despite the version
  19. number; that's an artifact of RCS).  Feedback is welcomed.
  20. </abstract>
  21.  
  22. <!-- *********************** SECTION ************************** -->
  23. <!-- Preliminaries                                              -->
  24.  
  25. <sect>Preliminaries
  26.  
  27. <sect1> ELF vs. a.out
  28. <label id="index.0"> <!-- elf -->
  29. <label id="index.1"> <!-- a.out -->
  30.  
  31. <p> Linux development is in a state of flux right now.  Briefly, there
  32. are two formats for the binaries that Linux knows how to execute, and
  33. depending on how your system is put together, you may have either.
  34. When reading this HOWTO, it helps to know which.
  35.  
  36. <label id="index.2"> <!-- <tt/file/ -->
  37.  
  38. How to tell?  Use the `file' utility (eg <tt>file /bin/bash</tt>).
  39. For an ELF program it will say something with ELF in, for an a.out
  40. program it will say something involving <tt>Linux/i386</tt>.
  41.  
  42. The differences between ELF and a.out are covered (extensively) later
  43. in this document.  ELF is the newer format, and generally accepted as
  44. better.
  45.  
  46. <sect1> Administrata
  47.  
  48. <label id="index.3"> <!-- chewing gum -->
  49. <p>The copyright information and like legalese can be found at the
  50. <em/end/ of this document, together with the statutory warnings about
  51. asking dumb questions on Usenet, revealing your ignorance of the C
  52. language by reporting bugs which aren't, and picking your nose while
  53. chewing gum.  
  54.  
  55. <sect1> Typography
  56.  
  57. <p> If you're reading this in Postscipt, dvi, or html format, you get
  58. to see a little more font variation than people with the plain text
  59. version.  In particular, filenames, commands, command output and
  60. source code excerpts are set in some form of <tt/typewriter/ font,
  61. whereas `variables' and random things that need emphasizing are
  62. <em/empasized/.
  63.  
  64. You also get a usable index.  In dvi or postscript, the numbers in the
  65. index are section numbers.  In HTML they're just sequentially assigned
  66. numbers that you can click on.  In the plain text version, they really
  67. are just numbers.  Get an upgrade!
  68.  
  69. The Bourne (rather than C) shell syntax is used in examples.  C shell
  70. users will want to use 
  71. <tscreen><verb>
  72. % setenv FOO bar
  73. </verb></tscreen>
  74. where I have written
  75. <tscreen><verb>
  76. $ FOO=bar; export FOO
  77. </verb></tscreen>
  78.  
  79. If the prompt shown is <tt/#/ rather than <tt/$/, the command shown
  80. will probably only work as root.  Of course, I accept no
  81. responsibility for anything that happens to your system as a result of
  82. trying these examples.  Have a nice day <tt/:-)/
  83.  
  84. <!-- *********************** SECTION ************************** -->
  85. <!-- Where to get things                                        -->
  86.  
  87. <sect>Where to get things
  88.  
  89. <sect1>This document
  90.  
  91. <p> This document is one of the Linux HOWTO series, so is available from all
  92. Linux HOWTO repositories, such as <url
  93. url="http://sunsite.unc.edu/pub/linux/docs/HOWTO/">.  The HTML version
  94. can also be found (possibly in a slightly newer version) from <url
  95. url="http://ftp.linux.org.uk/~barlow/howto/gcc-howto.html">.
  96.  
  97. <sect1> Other documentation
  98. <label id="index.4"> <!-- documentation -->
  99.  
  100. <p> The official documentation for gcc is in the source distribution
  101. (see below) as texinfo files, and as <tt/.info/ files.  If you have a fast
  102. network connection, a cdrom, or a reasonable amount of patience, you
  103. can just untar it and copy the relevant bits into <tt>/usr/info</tt>.  If not,
  104. you may find them at <url url=
  105. "ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/" name="tsx-11">, but
  106. not necessarily always the latest version.
  107.  
  108. <label id="index.5"> <!-- manual pages -->
  109.  
  110. <p> There are two source of documentation for libc.  GNU libc comes
  111. with info files which describe Linux libc fairly accurately except for
  112. stdio.  Also, the <url name="manpages"
  113. url="ftp://sunsite.unc.edu/pub/Linux/docs/"> archive are written for
  114. Linux and describe a lot of system calls (section 2) and libc
  115. functions (section 3).
  116.  
  117. <sect1>GCC 
  118. <label id="index.6"> <!-- gcc -->
  119.  
  120. <p> There are two answers.  
  121.  
  122. (a) The official Linux GCC distribution can always be found in binary
  123. (ready-compiled) form at <url url=
  124. "ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/">.  At the time of
  125. writing, 2.7.2 (<tt/gcc-2.7.2.bin.tar.gz/) is the latest version.
  126.  
  127. (b) The latest source distribution of GCC from the Free Software
  128. Foundation can be had from <url name="GNU archives"
  129. url="ftp://prep.ai.mit.edu/pub/gnu/">.  This is not necessarily always
  130. the same version as above, though it is just now.  The Linux GCC
  131. maintainer(s) have made it easy for you to compile the latest version
  132. available yourself --- the <tt/configure/ script should set it all up
  133. for you.  Check <url name="tsx-11" url=
  134. "ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/"> as well, for patches
  135. which you may want to apply.
  136.  
  137. To compile anything non-trivial (and quite a few trivial things also)
  138. you will also need the 
  139.  
  140. <sect1> C library and header files
  141. <label id="index.7"> <!-- libc -->
  142.  
  143. <p>What you want here depends on (i) whether your system is ELF or
  144. a.out, and (ii) which you want it to be.  If you're upgrading from
  145. libc 4 to libc 5, you are recommended to look at the ELF-HOWTO from
  146. approximately the same place as you found this document.
  147.  
  148. These are available from <url name ="tsx-11" url=
  149. "ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/"> as above:
  150.  
  151. <descrip>
  152.  
  153. <tag/<tt/libc-5.2.18.bin.tar.gz// --- ELF shared library images, static
  154. libraries and include files for the C and maths libraries.
  155.  
  156. <tag/<tt/libc-5.2.18.tar.gz// --- Source for the above.  You will also
  157. need the <tt>.bin.</tt> package for the header files.  If you are
  158. deliberating whether to compile the C library yourself or use the
  159. binaries, the right answer in nearly all cases is to use the binaries.
  160. You will however need to roll your own if you want NYS or shadow
  161. password support.
  162.  
  163. <tag/<tt/libc-4.7.5.bin.tar.gz// --- a.out shared library images and static
  164. libraries for version 4.7.5 of the C library and friends.  This is
  165. designed to coexist with the libc 5 package above, but is only really
  166. necessary if you wish to keep using/developing a.out format programs.
  167. </descrip>
  168.  
  169. <sect1>Associated tools (as, ld, ar, strings etc)
  170.  
  171. <label id="index.8"> <!-- <tt/as/ -->
  172. <label id="index.9"> <!-- <tt/ld/ -->
  173. <label id="index.10"> <!-- <tt/ar/ -->
  174. <label id="index.11"> <!-- <tt/strings/ -->
  175.  
  176. <p> From <url name ="tsx-11" url=
  177. "ftp://tsx-11.mit.edu:/pub/linux/packages/GCC/">, just like everything
  178. else so far.  The current version is <tt/binutils-2.6.0.2.bin.tar.gz/.
  179.  
  180. <p> Note that the binutils are only available in ELF, the current libc
  181. version is in ELF and the a.out libc is happiest when used in
  182. conjunction with an ELF libc.  C library development is moving
  183. emphatically ELFwards, and unless you have really good reasons for
  184. needing a.out things you're encouraged to follow suit.
  185.  
  186. <!-- to insert: 29) Where can I get Objective C for Linux ?-->
  187.  
  188. <!-- *********************** SECTION ************************** -->
  189. <!-- installation and setup                                     -->
  190.  
  191. <sect>GCC installation and setup
  192.  
  193. <sect1>GCC versions
  194.  
  195. <label id="index.12"> <!-- version numbers -->
  196. <label id="index.13"> <!-- gcc, flags -->
  197. <label id="index.14"> <!-- gcc -v -->
  198.  
  199. <p> You can find out what GCC version you're running by typing <tt>gcc
  200. -v</tt> at the shell prompt.  This is also a fairly reliable way to
  201. find out whether you are set up for ELF or a.out.  On my system it does
  202.  
  203. <tscreen><verb>
  204. $ gcc -v
  205. Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
  206. gcc version 2.7.2
  207. </verb></tscreen>
  208.  
  209. <p> The key things to note here are
  210. <itemize>
  211. <item> <tt/i486/.  This indicates that the gcc you are using was built
  212. for a 486 processor --- you might have 386 or 586 instead.  All of
  213. these chips can run code compiled for each of the others; the
  214. difference is that the 486 code has added padding in some places so
  215. runs faster on a 486.  This has no detrimental performance effect on a
  216. 386, but does make the binaries slightly larger.
  217.  
  218. <item> <tt/box/.  This is <em/not/ at all important, and may say
  219. something else (such as <tt/slackware/ or <tt/debian/) or nothing at
  220. all (so that the complete directory name is <tt/i486-linux/).  If you
  221. build your own gcc, you can set this at build time for cosmetic
  222. effect.  Just like I did <tt/:-)/
  223.  
  224. <item> <tt/linux/.  This may instead say <tt/linuxelf/ or
  225. <tt/linuxaout/, and, confusingly, the meaning of each varies according
  226. to the version that you are using.
  227.  
  228. <itemize>
  229. <item> <tt/linux/ means ELF if the version is 2.7.0 or newer, a.out
  230. otherwise.
  231.  
  232. <item><tt/linuxaout/ means a.out.  It was introduced as a target
  233. when the definition of <tt/linux/ was changed from a.out to ELF, so
  234. you won't see any <tt/linuxaout/ gcc older than 2.7.0.
  235.  
  236. <label id="index.15"> <!-- gcc, bugs -->
  237. <item><tt/linuxelf/ is obsolete.  It is generally a version of gcc
  238. 2.6.3 set to produce ELF executables.  Note that gcc 2.6.3 has known
  239. bugs when producing code for ELF --- an upgrade is advisable.
  240.  
  241. </itemize>
  242. <item> <tt/2.7.2/ is the version number.
  243. </itemize>
  244.  
  245. So, in summary, I have gcc 2.7.2 producing ELF code.  Quelle surprise.
  246.  
  247. <sect1> Where did it go?
  248.  
  249. <p>If you installed gcc without watching, or if you got it as part of
  250. a distribution, you may like to find out where it lives in the
  251. filesystem.  The key bits are
  252.  
  253. <itemize>
  254. <item> <tt>/usr/lib/gcc-lib/</tt><em>target</em><tt>/</tt><em/version/<tt>/</tt> (and
  255. subdirectories) is where most of the compiler lives.  This includes
  256. the executable programs that do actual compiling, and some
  257. version-specific libraries and include files.
  258.  
  259. <item> <tt>/usr/bin/gcc</tt> is the compiler driver --- the bit that you
  260. can actually run from the command line.  This can be used with
  261. multiple versions of gcc provided that you have multiple compiler
  262. directories (as above) installed.  To find out the default version it
  263. will use, type <tt/gcc -v/.  To force it to another version, type
  264. <tt/gcc -V /<em/version/.  For example
  265.  
  266. <tscreen><verb>
  267. # gcc -v
  268. Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
  269. gcc version 2.7.2
  270. # gcc -V 2.6.3 -v
  271. Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.6.3/specs
  272. gcc driver version 2.7.2 executing gcc version 2.6.3
  273. </verb></tscreen>
  274.  
  275. <item> <tt>/usr/</tt><em>target</em><tt>/(bin|lib|include)/</tt>.  If you have
  276. multiple targets installed (for example, a.out and elf, or a
  277. cross-compiler of some sort, the libraries, binutils (<tt/as/, <tt/ld/
  278. and so on) and header files for the non-native target(s) can be found
  279. here.  Even if you only have one kind of gcc installed you might find
  280. anyway that various bits for it are kept here.  If not, they're in
  281. <tt>/usr/(bin|lib|include)</tt>.
  282.  
  283. <item> <tt>/lib/</tt>,<tt>/usr/lib</tt> and others are library directories for
  284. the native system.  You will also need <tt>/lib/cpp</tt> for many
  285. applications (X makes quite a lot of use of it) --- either copy it
  286. from <tt>/usr/lib/gcc-lib/</tt><em>target</em><tt>/</tt><em/version/<tt>/</tt> or
  287. make a symlink pointing there.
  288.  
  289. <label id="index.16"> <!-- /lib/cpp -->
  290. </itemize>
  291.  
  292. <sect1> Where are the header files?
  293. <label id="index.17"> <!-- header files -->
  294.  
  295. <p>Apart from whatever you install yourself under
  296. <tt>/usr/local/include</tt>, there are three main sources of header
  297. files in Linux:
  298.  
  299. <itemize>
  300.  
  301. <item> Most of <tt>/usr/include/</tt> and its subdirectories are
  302. supplied with the libc binary package from H J Lu.  I say `most'
  303. because you may also have files from other sources (<tt/curses/ and
  304. <tt/dbm/ libraries, for example) in here, especially if you are using
  305. the newest libc distribution (which doesn't come with curses or dbm,
  306. unlike the older ones).
  307.  
  308. <label id="index.18"> <!-- <linux/*.h> -->
  309. <label id="index.19"> <!-- <asm/*.h> -->
  310.  
  311. <item> <tt>/usr/include/linux</tt> and <tt>/usr/include/asm</tt> (for
  312. the files <tt><linux/*.h></tt> and <tt><asm/*.h></tt>)
  313. should be symbolic links to the directories
  314. <tt>linux/include/linux</tt> and <tt>linux/include/asm</tt> in the
  315. kernel source distribution.  You need to install these if you plan to
  316. do <em/any/ non-trivial development; they are not just there for
  317. compiling the kernel.
  318.  
  319. You might find also that you need to do <tt>make config</tt> in the
  320. kernel directory after unpacking the sources.  Many files depend on
  321. <tt><linux/autoconf.h></tt> which otherwise may not exist, and in
  322. some kernel versions <tt/asm/ is a symbolic link itself and only
  323. created at <tt/make config/ time.
  324.  
  325. So, if you unpack your kernel sources under <tt>/usr/src/linux</tt>, that's
  326. <tscreen><verb>
  327. $ cd /usr/src/linux
  328. $ su
  329. # make config
  330. [answer the questions.  Unless you're going to go on and build the kernel
  331. it doesn't matter _too_ much what you say]
  332. # cd /usr/include
  333. # ln -s ../src/linux/include/linux .
  334. # ln -s ../src/linux/include/asm .
  335. </verb></tscreen>
  336.  
  337. <label id="index.20"> <!-- <float.h> -->
  338. <label id="index.21"> <!-- <limits.h> -->
  339. <label id="index.22"> <!-- <varargs.h> -->
  340. <label id="index.23"> <!-- <stdarg.h> -->
  341. <label id="index.24"> <!-- <stddef.h> -->
  342.  
  343. <item> Files such as <tt/<float.h>/, <tt/<limits.h>/,
  344. <tt/<varargs.h>/, <tt/<stdarg.h>/ and
  345. <tt/<stddef.h>/ vary according to the compiler version, so are
  346. found in <tt>/usr/lib/gcc-lib/i486-box-linux/2.7.2/include/</tt> and
  347. places of that ilk.
  348.  
  349. </itemize>
  350.  
  351. <sect1> Building cross compilers
  352.  
  353. <sect2> Linux as the target platform
  354.  
  355. <p> Assuming you have obtained the source code to gcc, usually you can
  356. just follow the instructions given in the INSTALL file for GCC.  A
  357. <tt/configure --target=i486-linux --host=XXX/ on platform <tt/XXX/
  358. followed by a <tt/make/ should do the trick.  Note that you will need
  359. the Linux includes, the kernel includes, and also to build the cross
  360. assembler and cross linker from the sources in <url
  361. url="ftp://tsx-11.mit.edu/pub/linux/packages/GCC/">.
  362.  
  363. <sect2> Linux as the source platform, MSDOS as the target
  364.  
  365. <p> Ugh.  Apparently this is somewhat possible by using the "emx"
  366. package or the "go" extender.  Please look at <url
  367. url="ftp://sunsite.unc.edu/pub/Linux/devel/msdos">.
  368.  
  369. I have not tested this and cannot vouch for its abilities.
  370.  
  371. <!-- *********************** SECTION ************************** -->
  372. <!-- * Porting and Compiling                                    -->
  373.  
  374. <sect>Porting and Compiling
  375.  
  376. <sect1> Automatically defined symbols
  377. <label id="index.25"> <!-- gcc, flags -->
  378.  
  379. <p>You can find out what symbols your version of gcc defines
  380. automatically by running it with the <tt/-v/ switch.  For example,
  381. mine does:
  382.  
  383. <tscreen><verb>
  384. $ echo 'main(){printf("hello world\n");}' | gcc -E -v -
  385. Reading specs from /usr/lib/gcc-lib/i486-box-linux/2.7.2/specs
  386. gcc version 2.7.2
  387.  /usr/lib/gcc-lib/i486-box-linux/2.7.2/cpp -lang-c -v -undef
  388. -D__GNUC__=2 -D__GNUC_MINOR__=7 -D__ELF__ -Dunix -Di386 -Dlinux
  389. -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__i386
  390. -D__linux -Asystem(unix) -Asystem(posix) -Acpu(i386)
  391. -Amachine(i386) -D__i486__ -
  392. </verb></tscreen>
  393.  
  394. If you are writing code that uses Linux-specific features, it is a
  395. good idea to enclose the nonportable bits in
  396.  
  397. <tscreen><verb>
  398. #ifdef __linux__
  399. /* ... funky stuff ... */
  400. #endif /* linux */
  401. </verb></tscreen>
  402.  
  403. Use <tt>__linux__</tt> for this purpose, <em/not/ <tt/linux/.
  404. Although the latter is defined, it is not POSIX compliant.
  405.  
  406. <!-- what does this mean?  are we talking namespace issues here? -->
  407.  
  408. <sect1> Compiler invocation
  409.  
  410. <p> The documentation for compiler switches is the gcc info page (in
  411. Emacs, use <tt>C-h i</tt> then select the `gcc' option).  Your distributor
  412. may not have packed this with your system, or you may have an old
  413. version; the best thing to do in this case is to download the gcc
  414. source archive from <url url="ftp://prep.ai.mit.edu/pub/gnu"> or one
  415. of its mirrors, and copy them out of it.  
  416.  
  417. The gcc manual page (<tt/gcc.1/) is, generally speaking, out of date.
  418. It will warn you of this when you try to look at it.
  419.  
  420. <sect2> Compiler flags
  421. <label id="index.26"> <!-- gcc, flags -->
  422. <label id="index.27"> <!-- optimisation -->
  423.  
  424. <p> gcc can be made to optimize its output code by adding
  425. <tt/-O/<em/n/ to its command line, where <em/n/ is an optional small
  426. integer.  Meaningful values of <em/n/, and their exact effect, vary
  427. according to the exact version, but typically it ranges from 0 (no
  428. optimization) to 2 (lots) or 3 (lots and lots).  
  429.  
  430. Internally, gcc translates these to a series of <tt/-f/ and <tt/-m/
  431. options.  You can see exactly which <tt/-O/ levels map to which
  432. options by running gcc with the <tt/-v/ flag and the (undocumented)
  433. <tt/-Q/ flag.  For example, for <tt/-O2/, mine says
  434.  
  435. <tscreen><verb>
  436. enabled: -fdefer-pop -fcse-follow-jumps -fcse-skip-blocks
  437. -fexpensive-optimizations
  438.          -fthread-jumps -fpeephole -fforce-mem -ffunction-cse -finline
  439.          -fcaller-saves -fpcc-struct-return -frerun-cse-after-loop
  440.          -fcommon -fgnu-linker -m80387 -mhard-float -mno-soft-float
  441.          -mno-386 -m486 -mieee-fp -mfp-ret-in-387
  442. </verb></tscreen>
  443.  
  444. Using an optimization level higher than your compiler supports
  445. (e.g. <tt/-O6/) will have exactly the same effect as using the highest
  446. level that it <em/does/ support.  Distributing code which is set to
  447. compile this way is a poor idea though --- if further optimisations
  448. are incorporated into future versions, you (or your users) may find
  449. that they break your code.
  450.  
  451. <label id="index.28"> <!-- gcc, bugs -->
  452. Users of gcc 2.7.0 thru 2.7.2 should note that there is a bug in
  453. <tt/-O2/ on these.  Specifically, strength reduction doesn't work.  A
  454. patch can be had to fix this if you feel like recompiling gcc,
  455. otherwise make sure that you always compile with <tt/-fno-strength-reduce/
  456.  
  457. <!-- I need a source for this patch -->
  458.  
  459. <sect3>Processor-specific
  460.  
  461. <p> There are other <tt/-m/ flags which aren't turned on by any variety of
  462. <tt/-O/ but are nevertheless useful.  Chief among these are <tt/-m386/
  463. and <tt/-m486/, which tell gcc to favour the 386 or 486 respectively.
  464. Code compiled with one of these will still work on the other; 486 code
  465. is bigger, but otherwise not slower on the 386.
  466.  
  467. There is currently no <tt/-mpentium/ or <tt/-m586/.  Linus suggests
  468. using <tt/-m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2/,
  469. to get 486 code optimisations but without the big gaps for alignment
  470. (which the pentium doesn't need).  Michael Meissner (of Cygnus) says
  471.  
  472. <quote>
  473. My hunch is that <tt/-mno-strength-reduce/ also results in faster code on
  474. the x86 (note, I'm not talking about the strength reduction bug, which
  475. is another issue).  This is because the x86 is rather register starved
  476. (and GCC's method of grouping registers into spill registers vs. other
  477. registers doesn't help either).  Strength reduction typically results
  478. in using additional registers to replace multiplications with
  479. addition.  I also suspect <tt/-fcaller-saves/ may also be a loss.
  480. </quote>
  481. <quote>
  482. Another hunch is that <tt/-fomit-frame-pointer/ might or might not be a
  483. win.  On the one hand, it can mean that another register is available
  484. for allocation.  On the other hand, the way the x86 encodes its
  485. instruction set, means that stack relative addresses take more space
  486. instead of frame relative addresses, which means slightly less Icache
  487. availble to the program.  Also, <tt/-fomit-frame-pointer/, means that the
  488. compiler has to constantly adjust the stack pointer after calls, while
  489. with a frame, it can let the stack accumulate for a few calls.
  490. </quote>
  491.  
  492. The final word on this subject is from Linus again:
  493.  
  494. <quote>
  495. Note that if you want to get optimal performance, don't believe me:
  496. test.  There are lots of gcc compiler switches, and it may be that a
  497. particular set gives the best optimizations for you. 
  498. </quote>
  499.  
  500. <!-- TODO: url for pentium gcc-->
  501.  
  502. <sect2> <tt/Internal compiler error: cc1 got fatal signal 11/
  503.  
  504. <label id="index.29"> <!-- gcc, bugs -->
  505. <label id="index.30"> <!-- segmentation fault -->
  506. <label id="index.31"> <!-- SIGSEGV -->
  507. <label id="index.32"> <!-- SIGSEGV, in gcc -->
  508. <label id="index.33"> <!-- segmentation fault, in GCC -->
  509.  
  510. <p> Signal 11 is SIGSEGV, or `segmentation violation'.  Usually it
  511. means that the program got its pointers confused and tried to write to
  512. memory it didn't own.  So, it could be a gcc bug.
  513.  
  514. gcc is however, a well tested and reliable piece of software, for
  515. the most part.  It also uses a large number of complex data
  516. structures, and an awful lot of pointers.  In short, it's the pickiest
  517. RAM tester commonly available.  If you <em/can't duplicate the bug/
  518. --- if it doesn't stop in the same place when you restart the
  519. compilation --- it's almost certainly a problem with your hardware
  520. (CPU, memory, motherboard or cache).  <bf/Don't/ claim it as a bug
  521. because your computer passes the power-on checks or runs Windows ok or
  522. whatever; these `tests' are commonly and rightly held to be worthless.
  523. And don't claim it's a bug because a kernel compile always stops
  524. during `<tt/make zImage/' --- of course it will!  `<tt/make zImage/'
  525. is probably compiling over 200 files; we're looking for a slightly
  526. <em/smaller/ place than that.
  527.  
  528. <p> If you can duplicate the bug, and (better) can produce a short
  529. program that exhibits it, you can submit it as a bug report to the
  530. FSF, or to the linux-gcc mailing list.  See the gcc documentation for
  531. details of exactly what information they need.
  532.  
  533. <!-- pointer to the sig11 faq -->
  534.  
  535. <sect1>Portability
  536.  
  537. <!-- 25) How do I port program XXX to Linux ? -->
  538.  
  539. <p> It has been said that, these days, if something hasn't been ported
  540. to Linux then it is not worth having :-)
  541.  
  542. Seriously though, in general only minor changes are needed to the
  543. sources to get over Linux's 100% POSIX compliance. It is also
  544. worthwhile passing back any changes to authors of the code such that
  545. in the future only `make' need be called to provide a working
  546. executable.
  547.  
  548. <sect2> BSDisms (including <tt/bsd_ioctl/, <tt/daemon/ and <tt/<sgtty.h>/)
  549.  
  550. <p> You can compile your program with <tt>-I/usr/include/bsd</tt> and link
  551. it with <tt>-lbsd</tt> (i.e. add <tt>-I/usr/include/bsd</tt> to <tt>CFLAGS</tt>
  552. and <tt>-lbsd</tt> to the <tt>LDFLAGS</tt> line in your Makefile). There is
  553. <em/no/ need to add <tt>-D__USE_BSD_SIGNAL</tt> any more if you want BSD
  554. type signal behavior, as you get this automatically when you have
  555. <tt>-I/usr/include/bsd</tt> and include <tt/<signal.h>/.
  556.  
  557. <sect2> `Missing' signals 
  558. (<tt/SIGBUS/, <tt/SIGEMT/, <tt/SIGIOT/, <tt/SIGTRAP/, <tt/SIGSYS/ etc)
  559. <label id="index.34"> <!-- <tt/SIGBUS/ -->
  560. <label id="index.35"> <!-- <tt/SIGEMT/ -->
  561. <label id="index.36"> <!-- <tt/SIGIOT/ -->
  562. <label id="index.37"> <!-- <tt/SIGTRAP/ -->
  563. <label id="index.38"> <!-- <tt/SIGSYS/ -->
  564.  
  565. <p> Linux is POSIX compliant.  These are not POSIX-defined signals ---
  566. ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
  567.  
  568. <quote>
  569. ``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS were omitted
  570. from POSIX.1 because their behavior is implementation dependent and
  571. could not be adequately categorized.  Conforming implementations may
  572. deliver these signals, but must document the circumstances under which
  573. they are delivered and note any restrictions concerning their
  574. delivery.''
  575. </quote>
  576.  
  577. <p>The cheap and cheesy way to fix this is to redefine these signals
  578. to <tt/SIGUNUSED/.  The <em/correct/ way is to bracket the code that
  579. handles them with appropriate <tt/#ifdef/s:
  580.  
  581. <tscreen><verb>
  582. #ifdef SIGSYS
  583. /* ... non-posix SIGSYS code here .... */
  584. #endif
  585. </verb></tscreen>
  586.  
  587. <sect2> K & R Code
  588. <label id="index.39"> <!-- <tt/-fwritable-strings/ -->
  589.  
  590. <p> GCC is an ANSI compiler; much existing code is not ANSI.  There's
  591. really not much that can be done about this, except to add
  592. <tt/-traditional/ to the compiler flags.  There is a certain amount of
  593. finer-grained control over which varieties of brain damage to emulate;
  594. consult the gcc info page.
  595.  
  596. Note that <tt/-traditional/ has effects beyond just changing the
  597. language that gcc accepts.  For example, it turns on
  598. <tt>-fwritable-strings</tt>, which moves string constants into data space
  599. (from text space, where they cannot be written to).  This increases
  600. the memory footprint of the program.
  601.  
  602. <sect2> Preprocessor symbols conflict with prototypes in the code
  603. <label id="index.40"> <!-- <tt/atoi()/ -->
  604. <label id="index.41"> <!-- <tt/atol()/ -->
  605.  
  606. <p> One of the most frequent problems is that some common functions
  607. are defined as macros in Linux's header files and the preprocessor
  608. will refuse to parse similar prototype definitions in the code. Common
  609. ones are <tt/atoi()/ and <tt/atol()/.
  610.  
  611. <sect2> <tt>sprintf()</tt>
  612. <label id="index.42"> <!-- <tt/sprintf()/ -->
  613.  
  614. <p> Something to be aware of, especially when porting from SunOS, is
  615. that <tt/sprintf(string, fmt, ...)/ returns a pointer to <tt/string/
  616. on many unices, whereas Linux (following ANSI) returns the number of
  617. characters which were put into the string.
  618.  
  619. <sect2> <tt/fcntl/ and friends.  Where are the definitions of
  620. <tt/FD_*/ stuff ?
  621. <label id="index.43"> <!-- <tt/FD_SET/ -->
  622. <label id="index.44"> <!-- <tt/FD_CLR/ -->
  623. <label id="index.45"> <!-- <tt/FD_ISSET/ -->
  624. <label id="index.46"> <!-- <tt/FD_ZERO/ -->
  625. <label id="index.47"> <!-- <tt/fcntl/ -->
  626. <label id="index.48"> <!-- <tt><sys/time.h></tt> -->
  627. <label id="index.49"> <!-- <tt><unistd.h></tt> -->
  628.  
  629. <p> In <tt><sys/time.h></tt>.  If you are using <tt/fcntl/ you
  630. probably want to include <tt/<unistd.h>/ too, for the actual
  631. prototype.
  632.  
  633. Generally speaking, the manual page for a function lists the necessary
  634. <tt/#include/s in its SYNOPSIS section.
  635.  
  636. <!-- These courtesy of Bruno Haible-->
  637.  
  638. <sect2> The <tt>select()</tt> timeout.  Programs start busy-waiting.
  639. <label id="index.50"> <!-- select() -->
  640.  
  641. <P> Once upon a time, the timeout parameter to <tt>select()</tt> was used
  642. read-only.  Even then, manual pages warned:
  643.  
  644. <quote>
  645. select() should probably return the time remaining from the original
  646. timeout, if any, by modifying the time value in place.  This may be
  647. implemented in future versions of the system.  Thus, it is unwise to
  648. assume that the timeout pointer will be unmodified by the select()
  649. call.
  650. </quote>
  651.  
  652. The future has arrived!  At least, it has here.  On return from a
  653. <tt/select()/, the timeout argument will be set to the remaining time
  654. that it would have waited had data not arrived.  If no data had
  655. arrived, this will be zero, and future calls using the same timeout
  656. structure will immediately return.
  657.  
  658. To fix, put the timeout value into that structure every time you call
  659. <tt/select()/.  Change code like
  660. <tscreen><verb>
  661.       struct timeval timeout;
  662.       timeout.tv_sec = 1; timeout.tv_usec = 0;
  663.       while (some_condition)
  664.             select(n,readfds,writefds,exceptfds,&ero;timeout); 
  665. </verb></tscreen>
  666. to, say,
  667. <tscreen><verb>
  668.       struct timeval timeout;
  669.       while (some_condition) {
  670.             timeout.tv_sec = 1; timeout.tv_usec = 0;
  671.             select(n,readfds,writefds,exceptfds,&ero;timeout);
  672.       }
  673. </verb></tscreen>
  674.  
  675. Some versions of Mosaic were at one time notable for this problem.
  676. The speed of the spinning globe animation was inversely related to the
  677. speed that the data was coming in from the network at!
  678.  
  679. <sect2> Interrupted system calls.
  680. <label id="index.51"> <!-- interrupted system calls -->
  681. <label id="index.52"> <!-- EINTR -->
  682.  
  683. <sect3>Symptom:
  684.  
  685. <p>When a program is stopped using Ctrl-Z and then restarted - or in
  686. other situations that generate signals: Ctrl-C interruption,
  687. termination of a child process etc. - it complains about "interrupted
  688. system call" or "write: unknown error" or things like that.
  689.  
  690. <sect3>Problem:
  691.  
  692. <p>POSIX systems check for signals a bit more often than some older
  693. unices.  Linux may execute signal handlers ---
  694.  
  695. <itemize>
  696. <item> asynchronously (at a timer tick)
  697. <item> on return from any system call
  698. <item> during the execution of the following system calls:
  699. <tt>select()</tt>, <tt>pause()</tt>, <tt>connect()</tt>,
  700. <tt>accept()</tt>, <tt>read()</tt> on terminals, sockets, pipes or
  701. files in <tt>/proc</tt>, <tt>write()</tt> on terminals, sockets, pipes or
  702. the line printer, <tt>open()</tt> on FIFOs, PTYs or serial lines,
  703. <tt>ioctl()</tt> on terminals, <tt>fcntl()</tt> with command
  704. <tt/F_SETLKW/, <tt>wait4()</tt>, <tt>syslog()</tt>, any TCP or NFS
  705. operations.  
  706. </itemize>
  707.  
  708. For other operating systems you may have to include the system calls
  709. <tt>creat()</tt>, <tt>close()</tt>, <tt>getmsg()</tt>, <tt>putmsg()</tt>,
  710. <tt>msgrcv()</tt>, <tt>msgsnd()</tt>, <tt>recv()</tt>, <tt>send()</tt>,
  711. <tt>wait()</tt>, <tt>waitpid()</tt>, <tt>wait3()</tt>, <tt>tcdrain()</tt>,
  712. <tt>sigpause()</tt>, <tt>semop()</tt> to this list.
  713. <!-- What does this MEAN? -->
  714.  
  715. <p>If a signal (that the program has installed a handler for) occurs
  716. during a system call, the handler is called.  When the handler returns
  717. (to the system call) it detects that it was interrupted, and
  718. immediately returns with -1 and <tt/errno = EINTR/.  The program is
  719. not expecting that to happen, so bottles out.
  720.  
  721. You may choose between two fixes.
  722.  
  723. (1) For every signal handler that you install, add <tt/SA_RESTART/ to the
  724. sigaction flags. For example, change
  725.  
  726. <tscreen><verb>
  727.   signal (sig_nr, my_signal_handler);
  728. </verb></tscreen>
  729. to
  730. <tscreen><verb>
  731.   signal (sig_nr, my_signal_handler);
  732.   { struct sigaction sa;
  733.     sigaction (sig_nr, (struct sigaction *)0, &ero;sa);
  734. #ifdef SA_RESTART
  735.     sa.sa_flags |= SA_RESTART;
  736. #endif
  737. #ifdef SA_INTERRUPT
  738.     sa.sa_flags &ero;= ~ SA_INTERRUPT;
  739. #endif
  740.     sigaction (sig_nr, &ero;sa, (struct sigaction *)0);
  741.   }
  742. </verb></tscreen>
  743.  
  744. Note that while this applies to most system calls, you must still
  745. check for <tt/EINTR/ yourself on <tt/read()/, <tt/write()/,
  746. <tt/ioctl()/, <tt/select()/, <tt/pause()/ and <tt/connect()/.  See
  747. below.
  748.  
  749. (2) Check for <tt/EINTR/ explicitly, yourself:
  750.  
  751. Here are two examples for <tt/read()/ and <tt/ioctl()/,
  752.  
  753. Original piece of code using <tt/read()/
  754.  
  755. <tscreen><verb>
  756. int result;
  757. while (len > 0) { 
  758.   result = read(fd,buffer,len);
  759.   if (result < 0) break;
  760.   buffer += result; len -= result;
  761. }
  762. </verb></tscreen>
  763. becomes
  764.  
  765. <tscreen><verb>
  766. int result;
  767. while (len > 0) { 
  768.   result = read(fd,buffer,len);
  769.   if (result < 0) { if (errno != EINTR) break; }
  770.   else { buffer += result; len -= result; }
  771. }
  772.  
  773. </verb></tscreen>
  774. and a piece of code using <tt/ioctl()/
  775.  
  776. <tscreen><verb>
  777. int result;
  778. result = ioctl(fd,cmd,addr);
  779. </verb></tscreen>
  780. becomes
  781. <tscreen><verb>
  782. int result;
  783. do { result = ioctl(fd,cmd,addr); }
  784. while ((result == -1) && (errno == EINTR));
  785. </verb></tscreen>
  786.  
  787. Note that in some versions of BSD Unix the default behaviour is to
  788. restart system calls. To get system calls interrupted you have to use
  789. the <tt/SV_INTERRUPT/ or <tt/SA_INTERRUPT/ flag.
  790.  
  791. <!-- according to some comments in /usr/include this may no longer be -->
  792. <!-- the case.  Should check -->
  793.  
  794. <sect2> Writable strings (program seg faults randomly)
  795. <label id="index.53"> <!-- SIGSEGV -->
  796. <label id="index.54"> <!-- segmentation fault -->
  797. <label id="index.55"> <!-- <tt/mktemp()/ -->
  798. <label id="index.56"> <!-- <tt/-fwritable-strings/ -->
  799.  
  800. <p> GCC has an optimistic view of its users, believing that they
  801. intend string constants to be exactly that --- constant.  Thus, it
  802. stores them in the text (code) area of the program, where they can be
  803. paged in and out from the program's disk image (instead of taking up
  804. swapspace), and any attempt to rewrite them will cause a segmentation
  805. fault.  This is a feature!
  806.  
  807. It may cause a problem for old programs that, for example, call
  808. <tt>mktemp()</tt> with a string constant as argument.  <tt/mktemp()/
  809. attempts to rewrite its argument in place.
  810.  
  811. To fix, either (a) compile with <tt/-fwritable-strings/, to get gcc to
  812. put constants in data space, or (b) rewrite the offending parts to
  813. allocate a non-constant string and strcpy the data into it before
  814. calling.
  815.  
  816. <sect2> Why does the <tt>execl()</tt> call fail?
  817. <label id="index.57"> <!-- <tt/execl()/ -->
  818.  
  819. <p> Because you're calling it wrong.  The first argument to <tt/execl/
  820. is the program that you want to run.  The second and subsequent
  821. arguments become the <tt/argv/ array of the program you're calling.
  822. Remember: <tt/argv[0]/ is traditionally set even when a program is run
  823. with `no' arguments.  So, you should be writing
  824.  
  825. <tscreen><verb>
  826. execl("/bin/ls","ls",NULL);
  827. </verb></tscreen>
  828. not just
  829. <tscreen><verb>
  830. execl("/bin/ls", NULL);
  831. </verb></tscreen>
  832.  
  833. <p> Executing the program with no arguments at all is construed as an
  834. invitation to print out its dynamic library dependencies, at least
  835. using a.out.  ELF does things differently.
  836.  
  837. <p>(If you want this library information, there are simpler interfaces;
  838. see the section on dynamic loading, or the manual page for <tt/ldd/).
  839.  
  840. <!-- *********************** SECTION ************************** -->
  841. <!-- * Debugging                                                -->
  842.  
  843. <sect> Debugging and Profiling
  844.  
  845. <sect1> Preventative maintenance (lint)
  846. <label id="index.58"> <!-- lint -->
  847.  
  848. <p> There is no widely-used lint for Linux, as most people are
  849. satisfied with the warnings that gcc can generate. 
  850. Probably the most useful is the <tt/-Wall/ switch --- this stands for
  851. `Warnings, all' but probably has more mnemonic value if thought of as
  852. the thing you bang your head against.
  853.  
  854. There is a public domain lint available from <url
  855. url="ftp://larch.lcs.mit.edu/pub/Larch/lclint">.  I don't know how
  856. good it is.
  857.  
  858. <sect1> Debugging
  859. <label id="index.59"> <!-- debugging -->
  860.  
  861. <sect2> How do I get debugging information into a program ?
  862. <label id="index.60"> <!-- <tt/gcc -g/ -->
  863. <label id="index.61"> <!-- <tt/gcc -fomit-frame-pointer/ -->
  864. <label id="index.62"> <!-- <tt/libg.a/ -->
  865.  
  866. <p> You need to compile and link all its bits with the <tt/-g/ switch,
  867. and without the <tt/-fomit-frame-pointer/ switch.  Actually, you don't
  868. need to recompile all of it, just the bits you're interested in debugging.
  869.  
  870. <p> On a.out configurations the shared libraries are compiled with
  871. <tt/-fomit-frame-pointer/, which gdb won't get on with.  Giving the
  872. <tt/-g/ option when you link should imply static linking; this is why.
  873.  
  874. <p> If the linker fails with a message about not finding libg.a, you
  875. don't have <tt>/usr/lib/libg.a</tt>, which is the special
  876. debugging-enabled C library.  It may be supplied in the libc binary
  877. package, or (in newer C library versions) you may need to get the libc
  878. source code and build it yourself.  You don't actually <em/need/ it
  879. though; you can get enough information for most purposes simply by
  880. symlinking it to <tt>/usr/lib/libc.a</tt>
  881.  
  882. <sect3> How do I get it out again?
  883. <label id="index.63"> <!-- binaries too big -->
  884.  
  885. <p> A lot of GNU software comes set up to compile and link with
  886. <tt/-g/, causing it to make very big (and often static) executables.
  887. This is not really such a hot idea.
  888.  
  889. <p> If the program has an autoconf generated <tt/configure/ script,
  890. you can usually turn off debugging information by doing
  891. <tt>./configure CFLAGS=</tt> or <tt>./configure CFLAGS=-O2</tt>.  Otherwise,
  892. check the Makefile.  Of course, if you're using ELF, the program is
  893. dynamically linked regardless of the <tt/-g/ setting, so you can just
  894. <tt/strip/ it.
  895.  
  896. <!--- gdb; where on sunsite?   xxgdb almost certainly has wrong url-->
  897.  
  898. <sect2> Available software
  899. <label id="index.64"> <!-- gdb -->
  900.  
  901. <p> Most people use <bf/gdb/, which you can get in source form from
  902. <url url="ftp://prep.ai.mit.edu/pub/gnu" name="GNU archive sites">, or
  903. as a binary from <url
  904. url="ftp://tsx-11.mit.edu/pub/linux/packages/GCC" name="tsx-11"> or
  905. sunsite.  <bf/xxgdb/ is an X debugger based on this (i.e. you need gdb
  906. installed first). The source may be found at <url
  907. url="ftp://ftp.x.org/contrib/xxgdb-1.08.tar.gz">
  908.  
  909. Also, the <bf/UPS/ debugger has been ported by Rick Sladkey. It runs
  910. under X as well, but unlike xxgdb, it is not merely an X front end for
  911. a text based debugger. It has quite a number of nice features, and if
  912. you spend any time debugging stuff, you probably should check it
  913. out. The Linux precompiled version and patches for the stock UPS
  914. sources can be found in <url
  915. url="ftp://sunsite.unc.edu/pub/Linux/devel/debuggers/">, and the
  916. original source at <url
  917. url="ftp://ftp.x.org/contrib/ups-2.45.2.tar.Z">.
  918.  
  919. Another tool you might find useful for debugging is `<bf/strace/', which
  920. displays the system calls that a process makes.  It has a multiplicity
  921. of other uses too, including figuring out what pathnames were compiled
  922. into binaries that you don't have the source for, exacerbating race
  923. conditions in programs that you suspect contain them, and generally
  924. learning how things work.  The latest version of strace (currently
  925. 3.0.8) can be found at <url url="ftp://ftp.std.com/pub/jrs/">.
  926.  
  927. <sect2> Background (daemon) programs
  928.  
  929. <p> Daemon programs typically execute <tt/fork()/ early, and terminate
  930. the parent.  This makes for a short debugging session.
  931.  
  932. <p> The simplest way to get around this is to set a breakpoint for
  933. <tt/fork/, and when the program stops, force it to return 0.
  934.  
  935. <tscreen><verb>
  936. (gdb) list 
  937. 1       #include <stdio.h>
  938. 2
  939. 3       main()
  940. 4       {
  941. 5         if(fork()==0) printf("child\n");
  942. 6         else printf("parent\n");
  943. 7       }
  944. (gdb) break fork
  945. Breakpoint 1 at 0x80003b8
  946. (gdb) run
  947. Starting program: /home/dan/src/hello/./fork 
  948. Breakpoint 1 at 0x400177c4
  949.  
  950. Breakpoint 1, 0x400177c4 in fork ()
  951. (gdb) return 0
  952. Make selected stack frame return now? (y or n) y
  953. #0  0x80004a8 in main ()
  954.     at fork.c:5
  955. 5         if(fork()==0) printf("child\n");
  956. (gdb) next
  957. Single stepping until exit from function fork, 
  958. which has no line number information.
  959. child
  960. 7       }
  961. </verb></tscreen>
  962.  
  963. <sect2> Core files
  964.  
  965. <p> When Linux boots it is usually configured not to produce core
  966. files.  If you like them, use your shell's builtin command to re-enable them:
  967. for C-shell compatibles (e.g. tcsh) this is 
  968. <tscreen><verb>
  969. % limit core unlimited
  970. </verb></tscreen>
  971. while Bourne-like shells (sh, bash, zsh, pdksh) use
  972. <tscreen><verb>
  973. $ ulimit -c unlimited
  974. </verb></tscreen>
  975.  
  976. If you want a bit more versatility in your core file naming (for
  977. example, if you're trying to conduct a post-mortem using a debugger
  978. that's buggy itself) you can make a simple mod to your kernel.  Look
  979. for the code in <tt>fs/binfmt_aout.c</tt> and <tt>fs/binfmt_elf.c</tt> (in
  980. newer kernels, you'll have to grep around a little in older ones) that
  981. says
  982.  
  983. <tscreen><verb>
  984.         memcpy(corefile,"core.",5);
  985. #if 0
  986.         memcpy(corefile+5,current->comm,sizeof(current->comm));
  987. #else
  988.         corefile[4] = '\0';
  989. #endif
  990. </verb></tscreen>
  991.  
  992. and change the <tt/0/s to <tt/1/s.
  993.  
  994. <sect1>Profiling
  995.  
  996. <p>Profiling is a way to examine which bits of a program are called most
  997. often or run for longest. It is a good way to optimize code and look
  998. at where time is being wasted.  You must compile all object files that
  999. you require timing information for with <tt/-p/, and to make sense of
  1000. the output file you will also need <tt/gprof/ (from the binutils
  1001. package).  See the <tt/gprof/ manual page for details.
  1002.  
  1003. <!-- **************************** SECTION ********************* -->
  1004. <!-- * Linking                                                  -->
  1005.  
  1006. <sect>Linking
  1007.  
  1008. <p> Between the two incompatible binary formats, the static vs shared
  1009. library distinction, and the overloading of the verb `link' to mean
  1010. both `what happens after compilation' and `what happens when a
  1011. compiled program is invoked' (and, actually, the overloading of
  1012. the word `load' in a comparable but opposite sense), this section is
  1013. complicated.  Little of it is much more complicated than that
  1014. sentence, though, so don't worry too much about it.
  1015.  
  1016. <p> To alleviate the confusion somewhat, we refer to what happens at
  1017. runtime as `dynamic loading' and cover it in the next section.  You
  1018. will also see it described as `dynamic linking', but not here.  This
  1019. section, then, is exclusively concerned with the kind of linking
  1020. that happens at the end of a compilation.
  1021.  
  1022. <sect1> Shared vs static libraries
  1023.  
  1024. <p> The last stage of building a program is to `link' it; to join all
  1025. the pieces of it together and see what is missing.  Obviously there
  1026. are some things that many programs will want to do --- open files, for
  1027. example, and the pieces that do these things are provided for you in
  1028. the form of libraries.  On the average Linux system these can be found
  1029. in <tt>/lib</tt> and <tt>/usr/lib/</tt>, among other places.
  1030.  
  1031. <label id="index.65"> <!-- binaries too big -->
  1032. <label id="index.66"> <!-- statically linked binaries, unexpected -->
  1033.  
  1034. When using a static library, the linker finds the bits that the
  1035. program modules need, and physically copies them into the executable
  1036. output file that it generates.  For shared libraries, it doesn't ---
  1037. instead it leaves a note in the output saying `when this program is
  1038. run, it will first have to load this library'.  Obviously shared
  1039. libraries tend to make for smaller executables; they also use less 
  1040. memory and mean that less disk space is used.  The default behaviour
  1041. of Linux is to link shared if it can find the shared libraries, static
  1042. otherwise.  If you're getting static binaries when you want shared,
  1043. check that the shared library files (<tt/*.sa/ for a.out, <tt/*.so/
  1044. for ELF) are where they should be, and are readable.
  1045.  
  1046. On Linux, static libraries have names like <tt/libname.a/, while
  1047. shared libraries are called <tt/libname.so.x.y.z/ where <tt/x.y.z/ is
  1048. some form of version number.  Shared libraries often also have links
  1049. pointing to them, which are important, and (on a.out configurations)
  1050. associated <tt/.sa/ files.  The standard libraries come in both shared
  1051. and static formats.
  1052.  
  1053. You can find out what shared libraries a program requires by using
  1054. <tt/ldd/ (List Dynamic Dependencies)
  1055. <tscreen><verb>
  1056. $ ldd /usr/bin/lynx
  1057.         libncurses.so.1 => /usr/lib/libncurses.so.1.9.6
  1058.         libc.so.5 => /lib/libc.so.5.2.18
  1059. </verb></tscreen>
  1060.  
  1061. This shows that on my system the WWW browser `lynx' depends on the
  1062. presence of <tt/libc.so.5/ (the C library) and <tt/libncurses.so.1/
  1063. (used for terminal control).  If a program has no dependencies,
  1064. <tt/ldd/ will say `<tt/statically linked/' or `<tt/statically linked (ELF)/'.
  1065.  
  1066. <sect1>Interrogating libraries (`which library is <tt/sin()/ in?')
  1067. <label id="index.67"> <!-- <tt/sin()/ -->
  1068. <label id="index.68"> <!-- <tt/cos()/ -->
  1069. <label id="index.69"> <!-- maths -->
  1070.  
  1071. <p> <tt/nm /<em/libraryname/ should list all the symbols that
  1072. <em/libraryname/ has references to.  It works on both static and shared
  1073. libraries.  Suppose that you want to know where <tt/tcgetattr()/ is defined:
  1074. you might do
  1075.  
  1076. <tscreen><verb>
  1077. $ nm libncurses.so.1 |grep tcget
  1078.          U tcgetattr
  1079. </verb></tscreen>
  1080.  
  1081. The <tt/U/ stands for `undefined' --- it shows that the ncurses
  1082. library uses but does not define it.  You could also do
  1083.  
  1084. <tscreen><verb>
  1085. $ nm libc.so.5 | grep tcget
  1086. 00010fe8 T __tcgetattr
  1087. 00010fe8 W tcgetattr
  1088. 00068718 T tcgetpgrp
  1089. </verb></tscreen>
  1090.  
  1091. The `<tt/W/' stands for `weak', which means that the symbol is
  1092. defined, but in such a way that it can be overridden by another
  1093. definition in a different library.  A straightforward `normal'
  1094. definition (such as the one for <tt/tcgetpgrp/) is marked by a
  1095. `<tt/T/'
  1096.  
  1097. <label id="index.70"> <!-- <tt/<math.h>/ -->
  1098.  
  1099. The short answer to the question in the title, by the way, is
  1100. <tt/libm.(so|a)/.  All the functions defined in <tt><math.h></tt> are
  1101. kept in the maths library; thus you need to link with <tt/-lm/ when
  1102. using any of them.
  1103.  
  1104. <!-- 13) Why are my binaries so huge and how do I reduce them ? -->
  1105. <!-- covered static/shared, need to look at -g, -N -->
  1106.  
  1107. <sect1>Finding files
  1108.  
  1109. <p><tt/ld: Output file requires shared library `libfoo.so.1`/
  1110.  
  1111. <p> The file search strategy of ld and friends varies according to
  1112. version, but the only default you can reasonably assume is
  1113. <tt>/usr/lib</tt>.  If you want libraries elsewhere to be searched,
  1114. specify their directories with the <tt/-L/ option to gcc or ld.
  1115.  
  1116. <p> If that doesn't help, check that you have the right file in that
  1117. place.  For a.out, linking with <tt/-lfoo/ makes ld look for
  1118. <tt/libfoo.sa/ (shared stubs), and if unsuccessful then for
  1119. <tt/libfoo.a/ (static).  For ELF, it looks for <tt/libfoo.so/ then
  1120. <tt/libfoo.a/.  <tt/libfoo.so/ is usually a symbolic link to
  1121. <tt/libfoo.so.x/.
  1122.  
  1123. <!-- n+1) How/when to recompile libc -->
  1124.  
  1125. <sect1> Building your own libraries
  1126.  
  1127. <sect2> Version control
  1128.  
  1129. <p> As any other program, libraries tend to have bugs which get fixed
  1130. over time.  They also may introduce new features, change the effect of
  1131. existing ones, or remove old ones.  This could be a problem for
  1132. programs using them; what if it was depending on that old feature?
  1133.  
  1134. So, we introduce library versioning.  We categorise the changes that
  1135. might be made to a library as `minor' or `major', and we rule that a
  1136. `minor' change is not allowed to break old programs that are using the
  1137. library.  You can tell the version of a library by looking at its
  1138. filename (actually, this is, strictly speaking, a lie for
  1139. ELF; keep reading to find out why) : <tt/libfoo.so.1.2/ has
  1140. major version 1, minor version 2.  The minor version number can be
  1141. more or less anything --- libc puts a `patchlevel' in it, giving 
  1142. library names like <tt/libc.so.5.2.18/, and it's also reasonable to
  1143. put letters, underscores, or more or less any printable ASCII in it.
  1144.  
  1145. One of the major differences between ELF and a.out format is in
  1146. building shared libraries.  We look at ELF first, because it's
  1147. simpler.
  1148.  
  1149. <sect2> ELF?  What is it then, anyway?
  1150. <label id="index.71"> <!-- elf -->
  1151.  
  1152. <p> ELF (Executable and Linking Format) is a binary format originally
  1153. developed by USL (UNIX System Laboratories) and currently used in
  1154. Solaris and System V Release 4.  Because of its increased flexibility
  1155. over the older a.out format that Linux was using, the GCC and C
  1156. library developers decided last year to move to using ELF as the Linux
  1157. standard binary format also.
  1158.  
  1159. <sect3> Come again?
  1160.  
  1161. <p> This section is from the document '/news-archives/comp.sys.sun.misc'.
  1162.  
  1163. <quote>
  1164. ELF ("Executable Linking Format) is the "new, improved" object file
  1165. format introduced in SVR4. ELF is much more powerful than straight 
  1166. COFF, in that it *is* user-extensible. ELF views an object-file as
  1167. an arbitarily long list of sections (rather than an array of fixed
  1168. size entities), these sections, unlike in COFF, do not HAVE to be in
  1169. a certain place and do not HAVE to come in any specific order etc.
  1170. Users can add new sections to object-files if they wish to
  1171. capture new data. ELF also has a far more powerful debugging format
  1172. called DWARF (Debugging With Attribute Record Format) - not currently
  1173. fully supported on linux (but work is underway). A linked list
  1174. of DWARF DIEs (or Debugging Information Entries) forms the .debug
  1175. section in ELF. Instead of being a collection of small, fixed-size
  1176. information records, DWARF DIEs each contain an arbitrarily long
  1177. list of complex attributes and are written out as a scope-based tree
  1178. of program data. DIEs can capture a large amount of information that
  1179. the COFF .debug section simply couldn't (like C++ inheritance graphs
  1180. etc.).
  1181. </quote>
  1182. <quote>
  1183. ELF files are accessed via the SVR4 (Solaris 2.0 ?) ELF access
  1184. library, which provides an easy and fast interface to the more gory
  1185. parts of ELF. One of the major boons in using the ELF access library
  1186. is that you will never need to look at an ELF file qua. UNIX file, it
  1187. is accessed as an Elf *, after an elf_open() call and from then on,
  1188. you perform elf_foobar() calls on its components instead of messing
  1189. about with its actual on-disk image (something many COFFers did with
  1190. impunity). 
  1191. </quote>
  1192.  
  1193. The case for/against ELF, and the necessary contortions to upgrade an
  1194. a.out system to support it, are covered in the ELF-HOWTO and I don't
  1195. propose to cut/paste them here.  The HOWTO should be available in the
  1196. same place as you found this one.
  1197.  
  1198. <sect3> ELF shared libraries
  1199.  
  1200. <p> To build <tt/libfoo.so/ as a shared library, the basic steps look
  1201. like this:
  1202.  
  1203. <tscreen><verb>
  1204. $ gcc -fPIC -c *.c
  1205. $ gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 *.o
  1206. $ ln -s libfoo.so.1.0 libfoo.so.1
  1207. $ ln -s libfoo.so.1 libfoo.so
  1208. $ LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH ; export LD_LIBRARY_PATH
  1209. </verb></tscreen>
  1210.  
  1211. This will generate a shared library called <tt>libfoo.so.1.0</tt>, and
  1212. the appropriate links for ld (<tt>libfoo.so</tt>) and the dynamic
  1213. loader (<tt>libfoo.so.1</tt>) to find it.  To test, we add the current
  1214. directory to <tt/LD_LIBRARY_PATH/.
  1215.  
  1216. <label id="index.72"> <!-- weird things -->
  1217. When you're happpy that the library works, you'll have to move it to,
  1218. say, <tt>/usr/local/lib</tt>, and recreate the appropriate links.  The
  1219. link from <tt/libfoo.so.1/ to <tt/libfoo.so.1.0/ is kept up to date by
  1220. <tt>ldconfig</tt>, which on most systems is run as part of the boot
  1221. process.  The <tt>libfoo.so</tt> link must be updated manually.  If you are
  1222. scrupulous about upgrading all the parts of a library (e.g. the header
  1223. files) at the same time, the simplest thing to do is make
  1224. <tt>libfoo.so -> libfoo.so.1</tt>, so that ldconfig will keep both
  1225. links current for you.  If you <em/aren't/, you're setting yourself up
  1226. to have <em/all kinds of weird things/ happen at a later date.  Don't say
  1227. you weren't warned.
  1228.  
  1229. <tscreen><verb>
  1230. $ su
  1231. # cp libfoo.so.1.0 /usr/local/lib
  1232. # /sbin/ldconfig
  1233. # ( cd /usr/local/lib ; ln -s libfoo.so.1 libfoo.so )
  1234. </verb></tscreen>
  1235.  
  1236. <sect3> Version numbering, sonames and symlinks
  1237. <label id="index.73"> <!-- soname -->
  1238. <label id="index.74"> <!-- version numbers -->
  1239.  
  1240. <p> Each library has a <em>soname</em>.  When the linker finds one of
  1241. these in a library it is searching, it embeds the soname into the
  1242. binary instead of the actual filename it is looking at.  At runtime,
  1243. the dynamic loader will then search for a file with the name of the
  1244. soname, not the library filename.  Thus a library called
  1245. <tt/libfoo.so/ could have a soname <tt/libbar.so/, and all programs
  1246. linked to it would look for <tt/libbar.so/ instead when they started.
  1247.  
  1248. <p>This sounds like a pointless feature, but it is key to
  1249. understanding how multiple versions of the same library can coexist on
  1250. a system.  The de facto naming standard for libraries in Linux is to
  1251. call the library, say, <tt>libfoo.so.1.2</tt>, and give it a soname of
  1252. <tt>libfoo.so.1</tt>.  If it's added to a `standard' library directory
  1253. (e.g. <tt>/usr/lib</tt>), <tt>ldconfig</tt> will create a symlink
  1254. <tt>libfoo.so.1 -> libfoo.so.1.2</tt> so that the appropriate image
  1255. is found at runtime.  You also need a link <tt>libfoo.so ->
  1256. libfoo.so.1</tt> so that ld will find the right soname to use at link
  1257. time.</p>
  1258.  
  1259. <p> So, when you fix bugs in the library, or add new functions (any
  1260. changes that won't adversely affect existing programs), you rebuild
  1261. it, keeping the soname as it was, and changing the filename.  When you
  1262. make changes to the library that would break existing binaries, you
  1263. simply increment the number in the soname --- in this case, call the
  1264. new version <tt>libfoo.so.2.0</tt>, and give it a soname of
  1265. <tt>libfoo.so.2</tt>.  Now switch the <tt>libfoo.so</tt> link to point
  1266. to the new version and all's well with the world again.  
  1267.  
  1268. Note that you don't <em/have/ to name libraries this way, but it's a
  1269. good convention.  ELF gives you the flexibility to name libraries in
  1270. ways that will confuse the pants off people, but that doesn't mean you
  1271. have to use it.
  1272.  
  1273. Executive summary: supposing that you observe the tradition that major
  1274. upgrades may break compatibility, minor upgrades may not, then link
  1275. with
  1276.  
  1277. <tscreen><verb>
  1278. gcc -shared -Wl,-soname,libfoo.so.major -o libfoo.so.major.minor
  1279. </verb></tscreen>
  1280.  
  1281. and everything will be all right.
  1282.  
  1283. <sect2> a.out.  Ye olde traditional format
  1284.  
  1285. <p> The ease of building shared libraries is a major reason for
  1286. upgrading to ELF.  That said, it's still possible in a.out.  Get
  1287. <url url=
  1288. "ftp://tsx-11.mit.edu/pub/linux/packages/GCC/src/tools-2.17.tar.gz">
  1289. and read the 20 page document that you will find after unpacking it.
  1290. I hate to be so transparently partisan, but it should be clear from
  1291. context that I never bothered myself :-)
  1292.  
  1293. <sect3> ZMAGIC vs QMAGIC
  1294. <label id="index.75"> <!-- ZMAGIC -->
  1295. <label id="index.76"> <!-- QMAGIC -->
  1296.  
  1297. <p> QMAGIC is an executable format just like the old a.out (also known
  1298. as ZMAGIC) binaries, but which leaves the first page unmapped. This
  1299. allows for easier NULL dereference trapping as no mapping exists in
  1300. the range 0-4096. As a side effect your binaries are nominally smaller
  1301. as well (by about 1K).
  1302.  
  1303. Obsolescent linkers support ZMAGIC only, semi-obsolescent support both
  1304. formats, and current versions support QMAGIC only.  This doesn't
  1305. actually matter, though, as the kernel can still run both formats.
  1306.  
  1307. Your `file' command should be able to identify whether a program is
  1308. QMAGIC.
  1309.  
  1310. <sect3>File Placement
  1311.  
  1312. <p> An a.out (DLL) shared library consists of two real files and a
  1313. symlink.  For the `foo' library used throughout this document as an
  1314. example, these files would be <tt/libfoo.sa/ and <tt/libfoo.so.1.2/;
  1315. the symlink would be <tt/libfoo.so.1/ and would point at the latter of
  1316. the files.  What are these for?
  1317.  
  1318. At compile time, <tt/ld/ looks for <tt/libfoo.sa/.  This is the `stub'
  1319. file for the library, and contains all exported data and pointers to
  1320. the functions required for run time linking.
  1321.  
  1322. At run time, the dynamic loader looks for <tt/libfoo.so.1/.  This is a
  1323. symlink rather than a real file so that libraries can be updated with
  1324. newer, bugfixed versions without crashing any application that was
  1325. using the library at the time.  After the new version --- say,
  1326. <tt/libfoo.so.1.3/ --- is completely there, running ldconfig will
  1327. switch the link to point to it in one atomic operation, leaving any
  1328. program which had the old version still perfectly happy.
  1329.  
  1330. DLL libraries (I know that's a tautology --- so sue me) often appear
  1331. bigger than their static counterparts.  They reserve space for future
  1332. expansion in the form of `holes' which can be made to take no disk
  1333. space. A simple <tt/cp/ call or using the program <tt/makehole/ will
  1334. achieve this.  You can also strip them after building, as the
  1335. addresses are in fixed locations. <bf/Do not attempt to strip ELF
  1336. libraries/.
  1337.  
  1338. <sect3> ``libc-lite''?
  1339.  
  1340. <p> A libc-lite is a light-weight version of the libc library built
  1341. such that it will fit on a floppy and suffice for all of the most
  1342. menial of UNIX tasks. It does <em/not/ include curses, dbm, termcap
  1343. etc code. If your <tt>/lib/libc.so.4</tt> is linked to a lite lib, you are
  1344. advised to replace it with a full version.
  1345.  
  1346. <sect2> Linking: common problems
  1347.  
  1348. <p> Send me your linking problems!  I probably won't do anything about
  1349. them, but I will write them up if I get enough ...
  1350.  
  1351. <descrip>
  1352.  
  1353. <tag> Programs link static when you wanted them shared</tag>
  1354. <label id="index.77"> <!-- binaries too big -->
  1355. <label id="index.78"> <!-- statically linked binaries, unexpected -->
  1356.  
  1357. Check that you have the right links for <tt/ld/ to find each shared
  1358. library.  For ELF this means a <tt>libfoo.so</tt> symlink to the image,
  1359. for a.out a <tt>libfoo.sa</tt> file.  A lot of people had this problem
  1360. after moving from ELF binutils 2.5 to 2.6 --- the earlier version
  1361. searched more `intelligently' for shared libraries, so they hadn't
  1362. created all the links.  The intelligent behaviour was removed for
  1363. compatibility with other architectures, and because quite often it got
  1364. its assumptions wrong and caused more trouble than it solved.
  1365.  
  1366. <tag> The DLL tool `mkimage' fails to find libgcc, or </tag> 
  1367. <label id="index.79"> <!-- libgcc -->
  1368.  
  1369. As of <tt/libc.so.4.5.x/ and above, libgcc is no longer shared. Hence
  1370. you must replace occurrences of `<tt/-lgcc/' on the offending line with
  1371. <tt/`gcc -print-libgcc-file-name`/ (complete with the backquotes).
  1372.  
  1373. Also, delete all <tt>/usr/lib/libgcc*</tt> files.  This is important.
  1374.  
  1375. <tag> <tt>__NEEDS_SHRLIB_libc_4 multiply defined</tt> messages </tag> 
  1376.  
  1377. are another consequence of the same problem.
  1378.  
  1379. <tag> ``Assertion failure'' message when rebuilding a DLL ?</tag>
  1380.  
  1381. This cryptic message most probably means that one of your jump table
  1382. slots has overflowed because too little space has been reserved in the
  1383. original <tt/jump.vars/ file.  You can locate the culprit(s) by
  1384. running the `<tt/getsize/' command provided in the tools-2.17.tar.gz
  1385. package. Probably the only solution, though, is to bump the major
  1386. version number of the library, forcing it to be backward incompatible.
  1387.  
  1388. <tag> <tt/ld: output file needs shared library libc.so.4/ </tag>
  1389.  
  1390. This usually happens when you are linking with libraries other than
  1391. libc (e.g. X libraries), and use the <tt/-g/ switch on the link line
  1392. without also using <tt/-static/.
  1393.  
  1394. The <tt/.sa/ stubs for the shared libraries usually have an undefined
  1395. symbol <tt/_NEEDS_SHRLIB_libc_4/ which gets resolved from the
  1396. <tt/libc.sa/ stub.  However with <tt/-g/ you end up linking with
  1397. <tt/libg.a/ or <tt/libc.a/ and thus this symbol never gets resolved,
  1398. leading to the above error message.
  1399.  
  1400. In conclusion, add <tt/-static/ when compiling with the <tt/-g/ flag,
  1401. or don't link with <tt/-g/.  Quite often you can get enough debugging
  1402. information by compiling the individual files with <tt/-g/, and
  1403. linking <em/without/ it.
  1404.  
  1405. </descrip>
  1406.  
  1407. <!-- *********************** SECTION ************************** -->
  1408. <!-- * Dynamic Loading                                          -->
  1409.  
  1410. <sect>Dynamic Loading
  1411.  
  1412. <p> <em>This section is a tad short right now; it will be expanded
  1413. over time as I gut the ELF howto </em>
  1414.  
  1415. <sect1>Concepts
  1416.  
  1417. <p> Linux has shared libraries, as you will by now be sick of hearing
  1418. if you read the whole of the last section at a sitting.  Some of the
  1419. matching-names-to-places work which was traditionally done at link
  1420. time must be deferred to load time.
  1421.  
  1422. <sect1> Error messages
  1423.  
  1424. <p> Send me your link errors!  I won't do anything about them, but I
  1425. might write them up ...
  1426.  
  1427. <descrip>
  1428.  
  1429. <tag> <tt>can't load library: /lib/libxxx.so, Incompatible version</tt></tag>
  1430.  
  1431. (a.out only) This means that you don't have the correct major version
  1432. of the xxx library.  No, you can't just make a symlink to another
  1433. version that you do have; if you are lucky this will cause your
  1434. program to segfault.  Get the new version.  A similar situation with
  1435. ELF will result in a message like 
  1436.  
  1437. <tscreen><verb>
  1438. ftp: can't load library 'libreadline.so.2'
  1439. </verb></tscreen>
  1440.  
  1441. <tag><tt/warning using incompatible library version xxx/</tag>
  1442.  
  1443. (a.out only) You have an older minor version of the library than the
  1444. person who compiled the program used.  The program will still run.
  1445. Probably.  An upgrade wouldn't hurt, though.
  1446.  
  1447. </descrip>
  1448.  
  1449. <sect1> Controlling the operation of the dynamic loader
  1450. <label id="index.80"> <!-- <tt/LD_*/ environment variables -->
  1451. <label id="index.81"> <!-- ldd -->
  1452.  
  1453. <p> There are a range of environment variables that the dynamic loader
  1454. will respond to.  Most of these are more use to <tt/ldd/ than they are
  1455. to the average user, and can most conveniently be set by running ldd
  1456. with various switches.  They include
  1457.  
  1458. <itemize>
  1459.  
  1460. <item> <tt/LD_BIND_NOW/ --- normally, functions are not `looked up' in
  1461. libraries until they are called.  Setting this flag causes all the
  1462. lookups to happen when the library is loaded, giving a slower startup
  1463. time.  It's useful when you want to test a program to make sure that
  1464. everything is linked.
  1465.  
  1466. <item> <tt/LD_PRELOAD/ can be set to a file containing `overriding'
  1467. function definitions.  For example, if you were testing memory
  1468. allocation strategies, and wanted to replace `malloc', you could write
  1469. your replacement routine, compile it into <tt/malloc.o/ and then
  1470. <tscreen><verb>
  1471. $ LD_PRELOAD=malloc.o; export LD_PRELOAD
  1472. $ some_test_program
  1473. </verb></tscreen>
  1474.  
  1475. <tt/LD_ELF_PRELOAD/ and <tt/LD_AOUT_PRELOAD/ are similar, but only
  1476. apply to the appropriate type of binary.  If
  1477. <tt/LD_/<em/something/<tt/_PRELOAD/ and <tt/LD_PRELOAD/ are set, the
  1478. more specific one is used.
  1479.  
  1480. <item> <tt/LD_LIBRARY_PATH/ is a colon-separated list of directories
  1481. in which to look for shared libraries.  It does <em/not/ affect ld; it
  1482. only has effect at runtime.  Also, it is disabled for programs that
  1483. run setuid or setgid.  Again, <tt/LD_ELF_LIBRARY_PATH/ and
  1484. <tt/LD_AOUT_LIBRARY_PATH/ can also be used to direct the search
  1485. differently for different flavours of binary.  <tt/LD_LIBRARY_PATH/
  1486. shouldn't be necessary in normal operation; add the directories to
  1487. <tt>/etc/ld.so.conf/</tt> and rerun ldconfig instead.
  1488.  
  1489. <item> <tt/LD_NOWARN/ applies to a.out only.  When set (e.g. with
  1490. <tt/LD_NOWARN=true; export LD_NOWARN/) it stops the loader from
  1491. issuing non-fatal warnings (such as minor version incompatibility
  1492. messages).
  1493.  
  1494. <item> <tt/LD_WARN/ applies to ELF only.  When set, it turns the
  1495. usually fatal ``Can't find library'' messages into warnings.  It's not
  1496. much use in normal operation, but important for ldd.
  1497.  
  1498. <item> <tt/LD_TRACE_LOADED_OBJECTS/ applies to ELF only, and causes
  1499. programs to think they're being run under <tt/ldd/:
  1500.  
  1501. <tscreen><verb>
  1502. $ LD_TRACE_LOADED_OBJECTS=true /usr/bin/lynx
  1503.         libncurses.so.1 => /usr/lib/libncurses.so.1.9.6
  1504.         libc.so.5 => /lib/libc.so.5.2.18
  1505. </verb></tscreen>
  1506.  
  1507. </itemize>
  1508.  
  1509. <sect1> Writing programs with dynamic loading
  1510. <label id="index.82"> <!-- <tt/dlopen()/ -->
  1511. <label id="index.83"> <!-- <tt/dlsym()/ -->
  1512.  
  1513. <p> This is very close to the way that Solaris 2.x dynamic loading
  1514. support works, if you're familiar with that.  It is covered
  1515. extensively in H J Lu's ELF programming document, and the
  1516. <tt>dlopen(3)</tt> manual page, which can be found in the ld.so
  1517. package.  Here's a nice simple example though: link it with
  1518. <tt>-ldl</tt>
  1519.  
  1520. <tscreen><verb>
  1521. #include <dlfcn.h>
  1522. #include <stdio.h>
  1523.  
  1524. main()
  1525. {
  1526.   void *libc;
  1527.   void (*printf_call)();
  1528.  
  1529.   if(libc=dlopen("/lib/libc.so.5",RTLD_LAZY))
  1530.   {
  1531.     printf_call=dlsym(libc,"printf");
  1532.     (*printf_call)("hello, world\n");
  1533.   }
  1534.  
  1535. }
  1536. </verb></tscreen>
  1537.  
  1538. <!-- *********************** SECTION ************************** -->
  1539. <!-- * The developers                        -->
  1540.  
  1541. <sect> Contacting the developers
  1542.  
  1543. <sect1> Bug reports
  1544. <label id="index.84"> <!-- gcc, bugs -->
  1545.  
  1546. <p> Start by <bf>narrowing the problem down</bf>.  Is it specific to
  1547. Linux, or does it happen with gcc on other systems?  Is it specific to
  1548. the kernel version?  Library version?  Does it go away if you link
  1549. static?  Can you trim the program down to something <bf/short/ that
  1550. demonstrates the bug?
  1551.  
  1552. Having done that, you'll know what program(s) the bug is in.  For
  1553. GCC, the bug reporting procedure is explained in the info file.  For
  1554. ld.so or the C or maths libraries, send mail to
  1555. <tt/linux-gcc@vger.rutgers.edu/.  If possible, include a short and
  1556. self-contained program that exhibits the bug, and a description both
  1557. of what you want it to do, and what it actually does. 
  1558.  
  1559. <sect1> Helping with development
  1560.  
  1561. <p> If you want to help with the development effort for GCC or the C
  1562. library, the first thing to do is join the
  1563. <tt/linux-gcc@vger.rutgers.edu/ mailing list.  If you just want to see
  1564. what the discussion is about, there are list archives at <url
  1565. url="http://homer.ncm.com/linux-gcc/">.  The second and subsequent
  1566. things depend on what you want to do!
  1567.  
  1568. <!-- Maybe there should be a TODO list for gcc/libc/whatever, for -->
  1569. <!-- people who want to play with it?  Moot the idea on the list -->
  1570.  
  1571. <!-- *********************** SECTION ************************** -->
  1572. <!-- * Clearing up                        -->
  1573.  
  1574. <sect> The Remains
  1575.  
  1576. <sect1> The Credits
  1577.  
  1578. <p><quote> 
  1579. Only presidents, editors, and people with tapeworms have the right to
  1580. use the editorial ``we''.
  1581. </quote>
  1582. (Mark Twain)
  1583.  
  1584. <p> This HOWTO is based very closely on Mitchum DSouza's GCC-FAQ; most
  1585. of the information (not to mention a reasonable amount of the text) in
  1586. it comes directly from that document.  Instances of the first person
  1587. pronoun in this HOWTO could refer to either of us; generally the ones
  1588. that say ``I have not tested this; don't blame me if it toasts your
  1589. hard disk/system/spouse'' apply to both of us.
  1590.  
  1591. Contributors to this document have included (in ASCII ordering by
  1592. first name) 
  1593. Andrew Tefft,
  1594. Axel Boldt,
  1595. Bill Metzenthen,
  1596. Bruce Evans,
  1597. Bruno Haible,
  1598. Daniel Barlow,
  1599. Daniel Quinlan,
  1600. David Engel,
  1601. Dirk Hohndel,
  1602. Eric Youngdale,
  1603. Fergus Henderson,
  1604. H.J. Lu,
  1605. Jens Schweikhardt,
  1606. Kai Petzke,
  1607. Michael Meissner,
  1608. Mitchum DSouza,
  1609. Olaf Flebbe,
  1610. Paul Gortmaker,
  1611. Rik Faith,
  1612. Steven S. Dick,
  1613. Tuomas J Lukka,
  1614. and of course Linus Torvalds, without whom the whole exercise would
  1615. have been pointless, let alone impossible :-)
  1616.  
  1617. Please do not feel offended if your name has not appeared here and you
  1618. have contributed to this document (either as HOWTO or as FAQ).  Email
  1619. me and I will rectify it.
  1620.  
  1621. <!-- of course, lots of this is due to other people; the copyright had -->
  1622. <!-- better be looked at -->
  1623.  
  1624. <sect1> Translations
  1625.  
  1626. <p> At this time, there are no known translations of this work.  If
  1627. you wish to produce one, please go right ahead, but do tell me about
  1628. it!  The chances are (sadly) several hundred to one against that I
  1629. speak the language you wish to translate to, but that aside I am happy
  1630. to help in whatever way I can.
  1631.  
  1632. <sect1> Feedback
  1633.  
  1634. is welcomed.  Mail me at <htmlurl
  1635. url="mailto:dan@detached.demon.co.uk"
  1636. name="dan@detached.demon.co.uk">.  My PGP public key (ID 5F263625) is
  1637. available from my <url url="http://ftp.linux.org.uk/~barlow/"
  1638. name="web pages">, if you feel the need to be secretive about things.
  1639.  
  1640. <sect1> Legalese
  1641.  
  1642. <p> All trademarks used in this document are acknowledged as being
  1643. owned by their respective owners.
  1644.  
  1645. This document is copyright (C) 1996 Daniel Barlow
  1646. <tt/<dan@detached.demon.co.uk>/ It may be reproduced and
  1647. distributed in whole or in part, in any medium physical or electronic,
  1648. as long as this copyright notice is retained on all copies. Commercial
  1649. redistribution is allowed and encouraged; however, the author would
  1650. like to be notified of any such distributions.  
  1651.  
  1652. All translations, derivative works, or aggregate works incorporating
  1653. any Linux HOWTO documents must be covered under this copyright notice.
  1654. That is, you may not produce a derivative work from a HOWTO and impose
  1655. additional restrictions on its distribution. Exceptions to these rules
  1656. may be granted under certain conditions; please contact the Linux
  1657. HOWTO coordinator at the address given below.
  1658.  
  1659. In short, we wish to promote dissemination of this information through
  1660. as many channels as possible. However, we do wish to retain copyright
  1661. on the HOWTO documents, and would like to be notified of any plans to
  1662. redistribute the HOWTOs.
  1663.  
  1664. If you have questions, please contact Greg Hankins, the Linux HOWTO
  1665. coordinator, at <tt/gregh@sunsite.unc.edu/ via email.
  1666.  
  1667. <sect>Index
  1668.  
  1669. <p>Entries starting with a non-alphabetical character are listed in ASCII 
  1670. order.  
  1671.  
  1672. <itemize>
  1673.  
  1674.  
  1675. <item> <tt/-fwritable-strings/ <ref id="index.39" name="39"> <ref id="index.56" name="56"> 
  1676. <item> /lib/cpp <ref id="index.16" name="16"> 
  1677. <item> a.out <ref id="index.1" name="1"> 
  1678. <item> <tt/ar/ <ref id="index.10" name="10"> 
  1679. <item> <tt/as/ <ref id="index.8" name="8"> 
  1680. <item> <asm/*.h> <ref id="index.19" name="19"> 
  1681. <item> <tt/atoi()/ <ref id="index.40" name="40"> 
  1682. <item> <tt/atol()/ <ref id="index.41" name="41"> 
  1683. <item> binaries too big <ref id="index.63" name="63"> <ref id="index.65" name="65"> <ref id="index.77" name="77"> 
  1684. <item> chewing gum <ref id="index.3" name="3"> 
  1685. <item> <tt/cos()/ <ref id="index.68" name="68"> 
  1686. <item> debugging <ref id="index.59" name="59"> 
  1687. <item> <tt/dlopen()/ <ref id="index.82" name="82"> 
  1688. <item> <tt/dlsym()/ <ref id="index.83" name="83"> 
  1689. <item> documentation <ref id="index.4" name="4"> 
  1690. <item> EINTR <ref id="index.52" name="52"> 
  1691. <item> elf <ref id="index.0" name="0"> <ref id="index.71" name="71"> 
  1692. <item> <tt/execl()/ <ref id="index.57" name="57"> 
  1693. <item> <tt/fcntl/ <ref id="index.47" name="47"> 
  1694. <item> <tt/FD_CLR/ <ref id="index.44" name="44"> 
  1695. <item> <tt/FD_ISSET/ <ref id="index.45" name="45"> 
  1696. <item> <tt/FD_SET/ <ref id="index.43" name="43"> 
  1697. <item> <tt/FD_ZERO/ <ref id="index.46" name="46"> 
  1698. <item> <tt/file/ <ref id="index.2" name="2"> 
  1699. <item> <float.h> <ref id="index.20" name="20"> 
  1700. <item> gcc <ref id="index.6" name="6"> 
  1701. <item> <tt/gcc -fomit-frame-pointer/ <ref id="index.61" name="61"> 
  1702. <item> <tt/gcc -g/ <ref id="index.60" name="60"> 
  1703. <item> gcc -v <ref id="index.14" name="14"> 
  1704. <item> gcc, bugs <ref id="index.15" name="15"> <ref id="index.28" name="28"> <ref id="index.29" name="29"> <ref id="index.84" name="84"> 
  1705. <item> gcc, flags <ref id="index.13" name="13"> <ref id="index.25" name="25"> <ref id="index.26" name="26"> 
  1706. <item> gdb <ref id="index.64" name="64"> 
  1707. <item> header files <ref id="index.17" name="17"> 
  1708. <item> interrupted system calls <ref id="index.51" name="51"> 
  1709. <item> <tt/ld/ <ref id="index.9" name="9"> 
  1710. <item> <tt/LD_*/ environment variables <ref id="index.80" name="80"> 
  1711. <item> ldd <ref id="index.81" name="81"> 
  1712. <item> libc <ref id="index.7" name="7"> 
  1713. <item> <tt/libg.a/ <ref id="index.62" name="62"> 
  1714. <item> libgcc <ref id="index.79" name="79"> 
  1715. <item> <limits.h> <ref id="index.21" name="21"> 
  1716. <item> lint <ref id="index.58" name="58"> 
  1717. <item> <linux/*.h> <ref id="index.18" name="18"> 
  1718. <item> manual pages <ref id="index.5" name="5"> 
  1719. <item> <tt/<math.h>/ <ref id="index.70" name="70"> 
  1720. <item> maths <ref id="index.69" name="69"> 
  1721. <item> <tt/mktemp()/ <ref id="index.55" name="55"> 
  1722. <item> optimisation <ref id="index.27" name="27"> 
  1723. <item> QMAGIC <ref id="index.76" name="76"> 
  1724. <item> segmentation fault <ref id="index.30" name="30"> <ref id="index.54" name="54"> 
  1725. <item> segmentation fault, in GCC <ref id="index.33" name="33"> 
  1726. <item> select() <ref id="index.50" name="50"> 
  1727. <item> <tt/SIGBUS/ <ref id="index.34" name="34"> 
  1728. <item> <tt/SIGEMT/ <ref id="index.35" name="35"> 
  1729. <item> <tt/SIGIOT/ <ref id="index.36" name="36"> 
  1730. <item> SIGSEGV <ref id="index.31" name="31"> <ref id="index.53" name="53"> 
  1731. <item> SIGSEGV, in gcc <ref id="index.32" name="32"> 
  1732. <item> <tt/SIGSYS/ <ref id="index.38" name="38"> 
  1733. <item> <tt/SIGTRAP/ <ref id="index.37" name="37"> 
  1734. <item> <tt/sin()/ <ref id="index.67" name="67"> 
  1735. <item> soname <ref id="index.73" name="73"> 
  1736. <item> <tt/sprintf()/ <ref id="index.42" name="42"> 
  1737. <item> statically linked binaries, unexpected <ref id="index.66" name="66"> <ref id="index.78" name="78"> 
  1738. <item> <stdarg.h> <ref id="index.23" name="23"> 
  1739. <item> <stddef.h> <ref id="index.24" name="24"> 
  1740. <item> <tt/strings/ <ref id="index.11" name="11"> 
  1741. <item> <tt><sys/time.h></tt> <ref id="index.48" name="48"> 
  1742. <item> <tt><unistd.h></tt> <ref id="index.49" name="49"> 
  1743. <item> <varargs.h> <ref id="index.22" name="22"> 
  1744. <item> version numbers <ref id="index.12" name="12"> <ref id="index.74" name="74"> 
  1745. <item> weird things <ref id="index.72" name="72"> 
  1746. <item> ZMAGIC <ref id="index.75" name="75"> 
  1747. </itemize>
  1748.  
  1749. <!-- ************** E N D   O F   G C C - H O W T O **************** -->
  1750. </article>
  1751.