toolchaintechnotes.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <sect1 id="ch05-toolchaintechnotes">
  2. <title>Toolchain technical notes</title>
  3. <?dbhtml filename="toolchaintechnotes.html" dir="chapter05"?>
  4. <para>This section attempts to explain some of the rationale and technical
  5. details behind the overall build method. It's not essential that you understand
  6. everything here immediately. Most of it will make sense once you have performed
  7. an actual build. Feel free to refer back here at any time.</para>
  8. <para>The overall goal of <xref linkend="chapter05"/> is to provide a sane,
  9. temporary environment that we can chroot into, and from which we can produce a
  10. clean, trouble-free build of the target LFS system in
  11. <xref linkend="chapter06"/>. Along the way, we attempt to divorce ourselves
  12. from the host system as much as possible, and in so doing build a
  13. self-contained and self-hosted toolchain. It should be noted that the
  14. build process has been designed in such a way so as to minimize the risks for
  15. new readers and provide maximum educational value at the same time. In other
  16. words, more advanced techniques could be used to build the system.</para>
  17. <important>
  18. <para>Before continuing, you really should be aware of the name of your working
  19. platform, often also referred to as the <emphasis>target triplet</emphasis>. For
  20. many folks the target triplet will be, for example:
  21. <emphasis>i686-pc-linux-gnu</emphasis>. A simple way to determine your target
  22. triplet is to run the <filename>config.guess</filename> script that comes with
  23. the source for many packages. Unpack the Binutils sources and run the script:
  24. <userinput>./config.guess</userinput> and note the output.</para>
  25. <para>You'll also need to be aware of the name of your platform's
  26. <emphasis>dynamic linker</emphasis>, often also referred to as the
  27. <emphasis>dynamic loader</emphasis>, not to be confused with the standard linker
  28. <emphasis>ld</emphasis> that is part of Binutils. The dynamic linker is provided
  29. by Glibc and has the job of finding and loading the shared libraries needed by a
  30. program, preparing the program to run and then running it. For most folks, the
  31. name of the dynamic linker will be <emphasis>ld-linux.so.2</emphasis>. On
  32. platforms that are less prevalent, the name might be
  33. <emphasis>ld.so.1</emphasis> and newer 64 bit platforms might even have
  34. something completely different. You should be able to determine the name
  35. of your platform's dynamic linker by looking in the
  36. <filename class="directory">/lib</filename> directory on your host system. A
  37. surefire way is to inspect a random binary from your host system by running:
  38. <userinput>'readelf -l &lt;name of binary&gt; | grep interpreter'</userinput>
  39. and noting the output. The authoritative reference covering all platforms is in
  40. the <filename>shlib-versions</filename> file in the root of the Glibc source
  41. tree.</para>
  42. </important>
  43. <para>Some key technical points of how the <xref linkend="chapter05"/> build
  44. method works:</para>
  45. <itemizedlist>
  46. <listitem><para>Similar in principle to cross compiling whereby tools installed
  47. into the same prefix work in cooperation and thus utilize a little GNU
  48. "magic".</para></listitem>
  49. <listitem><para>Careful manipulation of the standard linker's library search
  50. path to ensure programs are linked only against libraries we
  51. choose.</para></listitem>
  52. <listitem><para>Careful manipulation of <userinput>gcc</userinput>'s
  53. <emphasis>specs</emphasis> file to tell the compiler which target dynamic
  54. linker will be used.</para></listitem>
  55. </itemizedlist>
  56. <para>Binutils is installed first because both GCC and Glibc perform various
  57. feature tests on the assembler and linker during their respective runs of
  58. <userinput>./configure</userinput> to determine which software features to enable
  59. or disable. This is more important than one might first realize. An incorrectly
  60. configured GCC or Glibc can result in a subtly broken toolchain where the impact
  61. of such breakage might not show up until near the end of the build of a whole
  62. distribution. Thankfully, a test suite failure will usually alert us before too
  63. much time is wasted.</para>
  64. <para>Binutils installs its assembler and linker into two locations,
  65. <filename class="directory">/tools/bin</filename> and
  66. <filename class="directory">/tools/$TARGET_TRIPLET/bin</filename>. In reality,
  67. the tools in one location are hard linked to the other. An important facet of
  68. the linker is its library search order. Detailed information can be obtained
  69. from <userinput>ld</userinput> by passing it the <emphasis>--verbose</emphasis>
  70. flag. For example: <userinput>'ld --verbose | grep SEARCH'</userinput> will
  71. show you the current search paths and their order. You can see what files are
  72. actually linked by <userinput>ld</userinput> by compiling a dummy program and
  73. passing the <emphasis>--verbose</emphasis> switch. For example:
  74. <userinput>'gcc dummy.c -Wl,--verbose 2>&amp;1 | grep succeeded'</userinput>
  75. will show you all the files successfully opened during the link.</para>
  76. <para>The next package installed is GCC and during its run of
  77. <userinput>./configure</userinput> you'll see, for example:</para>
  78. <blockquote><screen>checking what assembler to use... /tools/i686-pc-linux-gnu/bin/as
  79. checking what linker to use... /tools/i686-pc-linux-gnu/bin/ld</screen></blockquote>
  80. <para>This is important for the reasons mentioned above. It also demonstrates
  81. that GCC's configure script does not search the $PATH directories to find which
  82. tools to use. However, during the actual operation of <userinput>gcc</userinput>
  83. itself, the same search paths are not necessarily used. You can find out which
  84. standard linker <userinput>gcc</userinput> will use by running:
  85. <userinput>'gcc -print-prog-name=ld'</userinput>.
  86. Detailed information can be obtained from <userinput>gcc</userinput> by passing
  87. it the <emphasis>-v</emphasis> flag while compiling a dummy program. For
  88. example: <userinput>'gcc -v dummy.c'</userinput> will show you detailed
  89. information about the preprocessor, compilation and assembly stages, including
  90. <userinput>gcc</userinput>'s include search paths and their order.</para>
  91. <para>The next package installed is Glibc. The most important considerations for
  92. building Glibc are the compiler, binary tools and kernel headers. The compiler
  93. is generally no problem as Glibc will always use the <userinput>gcc</userinput>
  94. found in a $PATH directory. The binary tools and kernel headers can be a little
  95. more troublesome. Therefore we take no risks and use the available configure
  96. switches to enforce the correct selections. After the run of
  97. <userinput>./configure</userinput> you can check the contents of the
  98. <filename>config.make</filename> file in the
  99. <filename class="directory">glibc-build</filename> directory for all the
  100. important details. You'll note some interesting items like the use of
  101. <userinput>CC="gcc -B/tools/bin/"</userinput> to control which binary tools are
  102. used, and also the use of the <emphasis>-nostdinc</emphasis> and
  103. <emphasis>-isystem</emphasis> flags to control the compiler's include search
  104. path. These items help to highlight an important aspect of the Glibc package:
  105. it is very self-sufficient in terms of its build machinery and generally does
  106. not rely on toolchain defaults.</para>
  107. <para>After the Glibc installation, we make some adjustments to ensure that
  108. searching and linking take place only within our <filename>/tools</filename>
  109. prefix. We install an adjusted <userinput>ld</userinput>, which has a hard-wired
  110. search path limited to <filename class="directory">/tools/lib</filename>. Then
  111. we amend <userinput>gcc</userinput>'s specs file to point to our new dynamic
  112. linker in <filename class="directory">/tools/lib</filename>. This last step is
  113. <emphasis>vital</emphasis> to the whole process. As mentioned above, a
  114. hard-wired path to a dynamic linker is embedded into every ELF shared
  115. executable. You can inspect this by running:
  116. <userinput>'readelf -l &lt;name of binary&gt; | grep interpreter'</userinput>.
  117. By amending <userinput>gcc</userinput>'s specs file, we are ensuring that every
  118. program compiled from here through the end of <xref linkend="chapter05"/> will
  119. use our new dynamic linker in
  120. <filename class="directory">/tools/lib</filename>.</para>
  121. <para>The need to use the new dynamic linker is also the reason why we apply the
  122. Specs patch for the second pass of GCC. Failure to do so will result in the GCC
  123. programs themselves having the name of the dynamic linker from the host system's
  124. <filename class="directory">/lib</filename> directory embedded into them, which
  125. would defeat our goal of getting away from the host.</para>
  126. <para>During the second pass of Binutils, we are able to utilize the
  127. <emphasis>--with-lib-path</emphasis> configure switch to control
  128. <userinput>ld</userinput>'s library search path. From this point onwards, the
  129. core toolchain is self-contained and self-hosted. The remainder of the
  130. <xref linkend="chapter05"/> packages all build against the new Glibc in
  131. <filename class="directory">/tools</filename> and all is well.</para>
  132. <para>Upon entering the chroot environment in <xref linkend="chapter06"/>, the
  133. first major package we install is Glibc, due to its self-sufficient nature that
  134. we mentioned above. Once this Glibc is installed into
  135. <filename class="directory">/usr</filename>, we perform a quick changeover of
  136. the toolchain defaults, then proceed for real in building the rest of the
  137. target <xref linkend="chapter06"/> LFS system.</para>
  138. <sect2>
  139. <title>Notes on static linking</title>
  140. <para>Most programs have to perform, beside their specific task, many rather
  141. common and sometimes trivial operations. These include allocating memory,
  142. searching directories, reading and writing files, string handling, pattern
  143. matching, arithmetic and many other tasks. Instead of obliging each program to
  144. reinvent the wheel, the GNU system provides all these basic functions in
  145. ready-made libraries. The major library on any Linux system is
  146. <emphasis>Glibc</emphasis>.</para>
  147. <para>There are two primary ways of linking the functions from a library to a
  148. program that uses them: statically or dynamically. When a program is linked
  149. statically, the code of the used functions is included in the executable,
  150. resulting in a rather bulky program. When a program is dynamically linked, what
  151. is included is a reference to the dynamic linker, the name of the library, and
  152. the name of the function, resulting in a much smaller executable. (A third way
  153. is to use the programming interface of the dynamic linker. See the
  154. <emphasis>dlopen</emphasis> man page for more information.)</para>
  155. <para>Dynamic linking is the default on Linux and has three major advantages
  156. over static linking. First, you need only one copy of the executable library
  157. code on your hard disk, instead of having many copies of the same code included
  158. into a whole bunch of programs -- thus saving disk space. Second, when several
  159. programs use the same library function at the same time, only one copy of the
  160. function's code is required in core -- thus saving memory space. Third, when a
  161. library function gets a bug fixed or is otherwise improved, you only need to
  162. recompile this one library, instead of having to recompile all the programs that
  163. make use of the improved function.</para>
  164. <para>If dynamic linking has several advantages, why then do we statically link
  165. the first two packages in this chapter? The reasons are threefold: historical,
  166. educational, and technical. Historical, because earlier versions of LFS
  167. statically linked every program in this chapter. Educational, because knowing
  168. the difference is useful. Technical, because we gain an element of independence
  169. from the host in doing so, meaning that those programs can be used
  170. independently of the host system. However, it's worth noting that an overall
  171. successful LFS build can still be achieved when the first two packages are
  172. built dynamically.</para>
  173. </sect2>
  174. </sect1>