toolchaintechnotes.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  3. <!ENTITY % general-entities SYSTEM "../general.ent">
  4. %general-entities;
  5. ]>
  6. <sect1 id="ch-tools-toolchaintechnotes">
  7. <title>Toolchain Technical Notes</title>
  8. <?dbhtml filename="toolchaintechnotes.html"?>
  9. <para>This section explains some of the rationale and technical
  10. details behind the overall build method. It is not essential to
  11. immediately understand everything in this section. Most of this
  12. information will be clearer after performing an actual build. This
  13. section can be referred back to at any time during the process.</para>
  14. <para>The overall goal of <xref linkend="chapter-temporary-tools"/> is
  15. to provide a temporary environment that can be chrooted into and from
  16. which can be produced a clean, trouble-free build of the target LFS
  17. system in <xref linkend="chapter-building-system"/>. Along the way, we
  18. separate from the host system as much as possible, and in doing so,
  19. build a self-contained and self-hosted toolchain. It should be noted
  20. that the build process has been designed to minimize the risks for new
  21. readers and provide maximum educational value at the same time. In
  22. other words, more advanced techniques could be used to build the
  23. system.</para>
  24. <important>
  25. <para>Before continuing, be aware of the name of the working platform,
  26. often referred to as the target triplet. Many times, the target
  27. triplet will probably be <emphasis>i686-pc-linux-gnu</emphasis>. A
  28. simple way to determine the name of the target triplet is to run the
  29. <command>config.guess</command> script that comes with the source for
  30. many packages. Unpack the Binutils sources and run the script:
  31. <userinput>./config.guess</userinput> and note the output.</para>
  32. <para>Also be aware of the name of the platform's dynamic linker,
  33. often referred to as the dynamic loader (not to be confused with the
  34. standard linker <command>ld</command> that is part of Binutils). The
  35. dynamic linker provided by Glibc finds and loads the shared libraries
  36. needed by a program, prepares the program to run, and then runs it.
  37. The name of the dynamic linker will usually be
  38. <filename class="libraryfile">ld-linux.so.2</filename>. On platforms that are less
  39. prevalent, the name might be <filename class="libraryfile">ld.so.1</filename>,
  40. and newer 64 bit platforms might be named something else entirely. The name of
  41. the platform's dynamic linker can be determined by looking in the
  42. <filename class="directory">/lib</filename> directory on the host
  43. system. A sure-fire way to determine the name is to inspect a random
  44. binary from the host system by running: <userinput>readelf -l &lt;name
  45. of binary&gt; | grep interpreter</userinput> and noting the output.
  46. The authoritative reference covering all platforms is in the
  47. <filename>shlib-versions</filename> file in the root of the Glibc
  48. source tree.</para>
  49. </important>
  50. <para>Some key technical points of how the <xref linkend="chapter-temporary-tools"/> build
  51. method works:</para>
  52. <itemizedlist>
  53. <listitem><para>The process is similar in principle to
  54. cross-compiling, whereby tools installed in the same prefix work in
  55. cooperation, and thus utilize a little GNU
  56. <quote>magic</quote></para></listitem>
  57. <listitem><para>Careful manipulation of the standard linker's library
  58. search path ensures programs are linked only against chosen
  59. libraries</para></listitem>
  60. <listitem><para>Careful manipulation of <command>gcc</command>'s
  61. <filename>specs</filename> file tells the compiler which target dynamic
  62. linker will be used</para></listitem>
  63. </itemizedlist>
  64. <para>Binutils is installed first because the
  65. <command>./configure</command> runs of both GCC and Glibc perform
  66. various feature tests on the assembler and linker to determine which
  67. software features to enable or disable. This is more important than
  68. one might first realize. An incorrectly configured GCC or Glibc can
  69. result in a subtly broken toolchain, where the impact of such breakage
  70. might not show up until near the end of the build of an entire
  71. distribution. A test suite failure will usually highlight this error
  72. before too much additional work is performed.</para>
  73. <beginpage/>
  74. <para>Binutils installs its assembler and linker in two locations,
  75. <filename class="directory">/tools/bin</filename> and <filename
  76. class="directory">/tools/$TARGET_TRIPLET/bin</filename>. The tools in
  77. one location are hard linked to the other. An important facet of the
  78. linker is its library search order. Detailed information can be
  79. obtained from <command>ld</command> by passing it the
  80. <parameter>--verbose</parameter> flag. For example, an <userinput>ld
  81. --verbose | grep SEARCH</userinput> will illustrate the current search
  82. paths and their order. It shows which files are linked by
  83. <command>ld</command> by compiling a dummy program and passing the
  84. <parameter>--verbose</parameter> switch to the linker. For example,
  85. <userinput>gcc dummy.c -Wl,--verbose 2&gt;&amp;1 | grep
  86. succeeded</userinput> will show all the files successfully opened
  87. during the linking.</para>
  88. <para>The next package installed is GCC. An example of what can be
  89. seen during its run of <command>./configure</command> is:</para>
  90. <screen><computeroutput>checking what assembler to use...
  91. /tools/i686-pc-linux-gnu/bin/as
  92. checking what linker to use... /tools/i686-pc-linux-gnu/bin/ld</computeroutput></screen>
  93. <para>This is important for the reasons mentioned above. It also
  94. demonstrates that GCC's configure script does not search the PATH
  95. directories to find which tools to use. However, during the actual
  96. operation of <command>gcc</command> itself, the same
  97. search paths are not necessarily used. To find out which standard
  98. linker <command>gcc</command> will use, run: <userinput>gcc
  99. -print-prog-name=ld</userinput>.</para>
  100. <para>Detailed information can be obtained from <command>gcc</command>
  101. by passing it the <parameter>-v</parameter> command line option while
  102. compiling a dummy program. For example, <userinput>gcc -v
  103. dummy.c</userinput> will show detailed information about the
  104. preprocessor, compilation, and assembly stages, including
  105. <command>gcc</command>'s included search paths and their order.</para>
  106. <para>The next package installed is Glibc. The most important
  107. considerations for building Glibc are the compiler, binary tools, and
  108. kernel headers. The compiler is generally not an issue since Glibc
  109. will always use the <command>gcc</command> found in a
  110. <envar>PATH</envar> directory.
  111. The binary tools and kernel headers can be a bit more complicated.
  112. Therefore, take no risks and use the available configure switches to
  113. enforce the correct selections. After the run of
  114. <command>./configure</command>, check the contents of the
  115. <filename>config.make</filename> file in the <filename
  116. class="directory">glibc-build</filename> directory for all important
  117. details. Note the use of <parameter>CC="gcc -B/tools/bin/"</parameter>
  118. to control which binary tools are used and the use of the
  119. <parameter>-nostdinc</parameter> and <parameter>-isystem</parameter>
  120. flags to control the compiler's include search path. These items
  121. highlight an important aspect of the Glibc package&mdash;it is very
  122. self-sufficient in terms of its build machinery and generally does not
  123. rely on toolchain defaults.</para>
  124. <para>After the Glibc installation, make some adjustments to ensure
  125. that searching and linking take place only within the <filename
  126. class="directory">/tools</filename> prefix. Install an adjusted
  127. <command>ld</command>, which has a hard-wired search path limited to
  128. <filename class="directory">/tools/lib</filename>. Then amend
  129. <command>gcc</command>'s specs file to point to the new dynamic linker
  130. in <filename class="directory">/tools/lib</filename>. This last step
  131. is vital to the whole process. As mentioned above, a hard-wired path
  132. to a dynamic linker is embedded into every Executable and Link Format
  133. (ELF)-shared executable. This can be inspected by running:
  134. <userinput>readelf -l &lt;name of binary&gt; | grep
  135. interpreter</userinput>. Amending gcc's specs file
  136. ensures that every program compiled from here through the end of this
  137. chapter will use the new dynamic linker in <filename
  138. class="directory">/tools/lib</filename>.</para>
  139. <para>The need to use the new dynamic linker is also the reason why
  140. the Specs patch is applied for the second pass of GCC. Failure to do
  141. so will result in the GCC programs themselves having the name of the
  142. dynamic linker from the host system's <filename
  143. class="directory">/lib</filename> directory embedded into them, which
  144. would defeat the goal of getting away from the host.</para>
  145. <para>During the second pass of Binutils, we are able to utilize the
  146. <parameter>--with-lib-path</parameter> configure switch to control
  147. <command>ld</command>'s library search path. From this point onwards,
  148. the core toolchain is self-contained and self-hosted. The remainder of
  149. the <xref linkend="chapter-temporary-tools"/> packages all build
  150. against the new Glibc in <filename
  151. class="directory">/tools</filename>.</para>
  152. <beginpage/>
  153. <para>Upon entering the chroot environment in <xref
  154. linkend="chapter-building-system"/>, the first major package to be
  155. installed is Glibc, due to its self-sufficient nature mentioned above.
  156. Once this Glibc is installed into <filename
  157. class="directory">/usr</filename>, perform a quick changeover of the
  158. toolchain defaults, then proceed in building the rest of the target
  159. LFS system.</para>
  160. <!-- Removed as part of the fix for bug 1061 - we no longer build pass1
  161. packages statically, therefore this explanation isn't required -->
  162. <!--<sect2>
  163. <title>Notes on Static Linking</title>
  164. <para>Besides their specific task, most programs have to perform many
  165. common and sometimes trivial operations. These include allocating
  166. memory, searching directories, reading and writing files, string
  167. handling, pattern matching, arithmetic, and other tasks. Instead of
  168. obliging each program to reinvent the wheel, the GNU system provides
  169. all these basic functions in ready-made libraries. The major library
  170. on any Linux system is Glibc.</para>
  171. <para>There are two primary ways of linking the functions from a
  172. library to a program that uses them&mdash;statically or dynamically. When
  173. a program is linked statically, the code of the used functions is
  174. included in the executable, resulting in a rather bulky program. When
  175. a program is dynamically linked, it includes a reference to the
  176. dynamic linker, the name of the library, and the name of the function,
  177. resulting in a much smaller executable. A third option is to use the
  178. programming interface of the dynamic linker (see the
  179. <emphasis>dlopen</emphasis> man page for more information).</para>
  180. <para>Dynamic linking is the default on Linux and has three major
  181. advantages over static linking. First, only one copy of the executable
  182. library code is needed on the hard disk, instead of having multiple
  183. copies of the same code included in several programs, thus saving
  184. disk space. Second, when several programs use the same library
  185. function at the same time, only one copy of the function's code is
  186. required in core, thus saving memory space. Third, when a library
  187. function gets a bug fixed or is otherwise improved, only the one
  188. library needs to be recompiled instead of recompiling all programs
  189. that make use of the improved function.</para>
  190. <para>If dynamic linking has several advantages, why then do we
  191. statically link the first two packages in this chapter? The reasons
  192. are threefold&mdash;historical, educational, and technical. The
  193. historical reason is that earlier versions of LFS statically linked
  194. every program in this chapter. Educationally, knowing the difference
  195. between static and dynamic linking is useful. The technical benefit is
  196. a gained element of independence from the host, meaning that those
  197. programs can be used independently of the host system. However, it is
  198. worth noting that an overall successful LFS build can still be
  199. achieved when the first two packages are built dynamically.</para>
  200. </sect2>-->
  201. </sect1>