toolchaintechnotes.xml 12 KB

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