toolchaintechnotes.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?xml version="1.0" encoding="UTF-8"?>
  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>工具链技术说明</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. to at any time during the process.</para-->
  15. <para>本节综合地解释构建方法中的逻辑和技术细节。
  16. 您现在并不需要立刻理解本节的所有内容,在实际进行构建的过程中,
  17. 可以更清晰地理解本节的信息。在整个构建过程中,您随时可以回来翻阅本节。
  18. </para>
  19. <!--para>The overall goal of <xref linkend="chapter-temporary-tools"/> is to
  20. produce a temporary area that contains a known-good set of tools that can be
  21. isolated from the host system. By using <command>chroot</command>, the
  22. commands in the remaining chapters will be contained within that environment,
  23. ensuring a clean, trouble-free build of the target LFS system. The build
  24. process has been designed to minimize the risks for new readers and to provide
  25. the most educational value at the same time.</para-->
  26. <para>
  27. <xref linkend="chapter-temporary-tools"/>
  28. 的总目标是构造一个临时环境,它包含一组可靠的,
  29. 能够与宿主系统完全分离的工具。这样,通过使用 <command>chroot</command>
  30. 命令,其余各章中执行的命令就被限制在这个临时环境中。
  31. 这确保我们能够干净、顺利地构建 LFS 系统。整个构建过程被精心设计,
  32. 以尽量降低新读者可能面临的风险,同时提供尽可能多的教育价值。</para>
  33. <note>
  34. <!--para>Before continuing, be aware of the name of the working platform,
  35. often referred to as the target triplet. A simple way to determine the
  36. name of the target triplet is to run the <command>config.guess</command>
  37. script that comes with the source for many packages. Unpack the Binutils
  38. sources and run the script: <userinput>./config.guess</userinput> and note
  39. the output. For example, for a 32-bit Intel processor the
  40. output will be <emphasis>i686-pc-linux-gnu</emphasis>. On a 64-bit
  41. system it will be <emphasis>x86_64-pc-linux-gnu</emphasis>.</para-->
  42. <para>在继续阅读之前,请注意往往被称为目标三元组的工作平台名称。
  43. 获得它的一种简单方法是运行许多软件包附带的
  44. <command>config.guess</command> 脚本。解压缩 Binutils 源码,
  45. 然后运行脚本:<userinput>./config.guess</userinput>,观察输出。
  46. 例如,对于 32 位 Intel 处理器,输出应该是
  47. <emphasis>i686-pc-linux-gnu</emphasis>,而对于 64 位系统输出应该是
  48. <emphasis>x86_64-pc-linux-gnu</emphasis>。</para>
  49. <!--para>Also be aware of the name of the platform's dynamic linker, often
  50. referred to as the dynamic loader (not to be confused with the standard
  51. linker <command>ld</command> that is part of Binutils). The dynamic linker
  52. provided by Glibc finds and loads the shared libraries needed by a program,
  53. prepares the program to run, and then runs it. The name of the dynamic
  54. linker for a 32-bit Intel machine will be <filename
  55. class="libraryfile">ld-linux.so.2</filename> (<filename
  56. class="libraryfile">ld-linux-x86-64.so.2</filename> for 64-bit systems). A
  57. sure-fire way to determine the name of the dynamic linker is to inspect a
  58. random binary from the host system by running: <userinput>readelf -l
  59. &lt;name of binary&gt; | grep interpreter</userinput> and noting the
  60. output. The authoritative reference covering all platforms is in the
  61. <filename>shlib-versions</filename> file in the root of the Glibc source
  62. tree.</para-->
  63. <para>另外注意平台的动态链接器的名称,它又被称为动态加载器
  64. (不要和 Binutils 中的普通链接器 <command>ld</command> 混淆)。
  65. 动态链接器由 Glibc 提供,它寻找并加载程序所需的共享库,
  66. 为程序运行做好准备,然后运行程序。
  67. 在 32 位 Intel 机器上动态链接器的名称是
  68. <filename class="libraryfile">ld-linux.so.2</filename>
  69. (在 64 位系统上是
  70. <filename class="libraryfile">ld-linux-x86-64.so.2</filename>)。
  71. 一个确定动态链接器名称的准确方法是从宿主系统找一个二进制可执行文件,
  72. 然后执行:
  73. <userinput>readelf -l &lt;二进制文件名&gt; | grep interpreter</userinput>
  74. 并观察输出。包含所有平台的权威参考可以在 Glibc 源码树根目录的
  75. <filename>shlib-versions</filename> 文件中找到。</para>
  76. </note>
  77. <!--para>Some key technical points of how the <xref
  78. linkend="chapter-temporary-tools"/> build method works:</para-->
  79. <para><xref linkend="chapter-temporary-tools"/> 中构建方法的技术重点在于:
  80. </para>
  81. <itemizedlist>
  82. <listitem>
  83. <!--para>Slightly adjusting the name of the working platform, by changing the
  84. &quot;vendor&quot; field target triplet by way of the
  85. <envar>LFS_TGT</envar> variable, ensures that the first build of Binutils
  86. and GCC produces a compatible cross-linker and cross-compiler. Instead of
  87. producing binaries for another architecture, the cross-linker and
  88. cross-compiler will produce binaries compatible with the current
  89. hardware.</para-->
  90. <para>微调工作平台的名称,将目标三元组中的 <quote>供应商</quote>
  91. (vendor) 字段修改,得到 <envar>LFS_TGT</envar> 中的三元组。
  92. 这保证我们第一次构建 Binutils 和 GCC 时,
  93. 能够产生与宿主系统兼容的交叉链接器和交叉编译器。
  94. 它们不为其他架构生成二进制代码,
  95. 而是生成与当前硬件兼容的二进制代码。</para>
  96. </listitem>
  97. <listitem>
  98. <!--para> The temporary libraries are cross-compiled. Because a
  99. cross-compiler by its nature cannot rely on anything from its host
  100. system, this method removes potential contamination of the target
  101. system by lessening the chance of headers or libraries from the host
  102. being incorporated into the new tools. Cross-compilation also allows for
  103. the possibility of building both 32-bit and 64-bit libraries on 64-bit
  104. capable hardware.</para-->
  105. <para>交叉编译临时环境中的库。
  106. 由于交叉编译器从本质上不可能依赖于宿主环境中的任何东西,
  107. 我们可以降低宿主环境中头文件或库被嵌入新编译的工具的概率,
  108. 从而防止新编译的系统被污染。另外,交叉编译使得我们可能在
  109. 64 位硬件上同时编译出 32 位和 64 位的库。</para>
  110. </listitem>
  111. <listitem>
  112. <!--para>Careful manipulation of the GCC source tells the compiler which target
  113. dynamic linker will be used.</para-->
  114. <para>小心地修改 GCC 源代码,
  115. 从而使得编译器使用我们指定的目标系统动态链接器。</para>
  116. </listitem>
  117. </itemizedlist>
  118. <!--para>Binutils is installed first because the <command>configure</command>
  119. runs of both GCC and Glibc perform various feature tests on the assembler
  120. and linker to determine which software features to enable or disable. This
  121. is more important than one might first realize. An incorrectly configured
  122. GCC or Glibc can result in a subtly broken toolchain, where the impact of
  123. such breakage might not show up until near the end of the build of an
  124. entire distribution. A test suite failure will usually highlight this error
  125. before too much additional work is performed.</para-->
  126. <para>我们首先安装 Binutils 。这是由于 GCC 和 Glibc 的
  127. <command>configure</command> 脚本首先测试汇编器和链接器的一些特性,
  128. 以决定启用或禁用一些软件特性。初看起来这并不重要,
  129. 但没有正确配置的 GCC 或者 Glibc 可以导致工具链中潜伏的故障。
  130. 这些故障可能到整个构建过程快要结束时才突然爆发,
  131. 不过在花费大量无用功之前,测试套件的失败可以将这类错误凸显出来。
  132. </para>
  133. <!--para>Binutils installs its assembler and linker in two locations,
  134. <filename class="directory">/tools/bin</filename> and <filename
  135. class="directory">/tools/$LFS_TGT/bin</filename>. The tools in one
  136. location are hard linked to the other. An important facet of the linker is
  137. its library search order. Detailed information can be obtained from
  138. <command>ld</command> by passing it the <parameter>verbose</parameter>
  139. flag. For example, an <userinput>ld verbose | grep SEARCH</userinput>
  140. will illustrate the current search paths and their order. It shows which
  141. files are linked by <command>ld</command> by compiling a dummy program and
  142. passing the <parameter>verbose</parameter> switch to the linker. For example,
  143. <userinput>gcc dummy.c -Wl,verbose 2&gt;&amp;1 | grep succeeded</userinput>
  144. will show all the files successfully opened during the linking.</para-->
  145. <para>Binutils 将汇编器和链接器安装在两个位置,一个是
  146. <filename class="directory">/tools/bin</filename>,
  147. 另一个是 <filename class="directory">/tools/$LFS_TGT/bin</filename>。
  148. 这两个位置中的工具互为硬链接。链接器的一个重要方面是它搜索库的顺序,
  149. 通过向 <command>ld</command> 命令加入 <parameter>--verbose</parameter>
  150. 参数,可以得到关于搜索路径的详细信息。例如,
  151. <userinput>ld --verbose | grep SEARCH</userinput>
  152. 会输出当前的搜索路径及其顺序。此外,通过编译一个样品 (dummy)
  153. 程序并向链接器传递 <parameter>--verbose</parameter> 参数,
  154. 可以知道哪些文件被链接。例如,
  155. <userinput>gcc dummy.c -Wl,--verbose 2&gt;&amp;1 | grep succeeded
  156. </userinput> 将显示所有在链接过程中被成功打开的文件。</para>
  157. <!--para>The next package installed is GCC. An example of what can be
  158. seen during its run of <command>configure</command> is:</para-->
  159. <para>下一步安装 GCC。在执行它的 <command>configure</command> 脚本时,
  160. 您会看到类似下面这样的输出:</para>
  161. <screen><computeroutput>checking what assembler to use... /tools/i686-lfs-linux-gnu/bin/as
  162. checking what linker to use... /tools/i686-lfs-linux-gnu/bin/ld</computeroutput></screen>
  163. <!--para>This is important for the reasons mentioned above. It also demonstrates
  164. that GCC's configure script does not search the PATH directories to find which
  165. tools to use. However, during the actual operation of <command>gcc</command>
  166. itself, the same search paths are not necessarily used. To find out which
  167. standard linker <command>gcc</command> will use, run:
  168. <userinput>gcc -print-prog-name=ld</userinput>.</para-->
  169. <para>基于我们上面论述的原因,这些输出非常重要。
  170. 这说明 GCC 的配置脚本没有在 PATH 变量指定的目录中搜索工具。
  171. 然而,在 <command>gcc</command> 的实际运行中,
  172. 未必会使用同样的搜索路径。为了查询 <command>gcc</command>
  173. 会使用哪个链接器,需要执行以下命令:
  174. <userinput>gcc -print-prog-name=ld</userinput>。</para>
  175. <!--para>Detailed information can be obtained from <command>gcc</command> by
  176. passing it the <parameter>-v</parameter> command line option while compiling
  177. a dummy program. For example, <userinput>gcc -v dummy.c</userinput> will show
  178. detailed information about the preprocessor, compilation, and assembly stages,
  179. including <command>gcc</command>'s included search paths and their order.</para-->
  180. <para> 通过向 <command>gcc</command> 传递 <parameter>-v</parameter>
  181. 参数,可以知道在编译样品程序时发生的细节。例如,
  182. <userinput>gcc -v dummy.c</userinput>
  183. 会输出预处理、编译和汇编阶段中的详细信息,包括 <command>gcc</command>
  184. 的包含文件搜索路径和顺序。</para>
  185. <!--para>Next installed are sanitized Linux API headers. These allow the standard
  186. C library (Glibc) to interface with features that the Linux kernel will
  187. provide.</para-->
  188. <para>下一步安装<quote>净化的</quote> (sanitized) Linux API 头文件。
  189. 这允许 C 标准库 (Glibc) 与 Linux 内核提供的各种特性交互。</para>
  190. <note><title>译注</title>
  191. <para>关于<quote>净化的</quote>一词的含义,见科普文章
  192. <ulink url="http://www.jinbuguo.com/kernel/header_story.html"/>。
  193. </para>
  194. </note>
  195. <!--para>The next package installed is Glibc. The most important considerations
  196. for building Glibc are the compiler, binary tools, and kernel headers. The
  197. compiler is generally not an issue since Glibc will always use the compiler
  198. relating to the <parameter>host</parameter> parameter passed to its
  199. configure script; e.g. in our case, the compiler will be
  200. <command>i686-lfs-linux-gnu-gcc</command>. The binary tools and kernel
  201. headers can be a bit more complicated. Therefore, take no risks and use the
  202. available configure switches to enforce the correct selections. After the run
  203. of <command>configure</command>, check the contents of the
  204. <filename>config.make</filename> file in the <filename
  205. class="directory">glibc-build</filename> directory for all important details.
  206. Note the use of <parameter>CC="i686-lfs-gnu-gcc"</parameter> to control which
  207. binary tools are used and the use of the <parameter>-nostdinc</parameter> and
  208. <parameter>-isystem</parameter> flags to control the compiler's include
  209. search path. These items highlight an important aspect of the Glibc
  210. package&mdash;it is very self-sufficient in terms of its build machinery and
  211. generally does not rely on toolchain defaults.</para-->
  212. <para>下一步安装 Glibc 。在构建 Glibc 时最重要的考虑是编译器、
  213. 二进制工具和内核头文件。编译器一般不成问题,
  214. Glibc 总是使用传递给配置脚本的
  215. <parameter>--host</parameter> 参数相关的编译器。
  216. 例如,在我们的例子中,使用的编译器是
  217. <command>i686-lfs-linux-gnu-gcc</command>。
  218. 但二进制工具和内核头文件的问题比较复杂,安全起见,
  219. 我们使用配置脚本提供的开关,保证正确的选择。
  220. 在 <command>configure</command> 脚本运行完成后,可以检查
  221. <filename class="directory">glibc-build</filename> 目录中的
  222. <filename>config.make</filename> 文件,了解全部重要的细节。
  223. 注意参数 <parameter>CC=i686-lfs-linux-gnu-gcc</parameter>
  224. 控制构建系统使用正确的二进制工具,而参数
  225. <parameter>-nostdinc</parameter> 和
  226. <parameter>-isystem</parameter> 控制编译器的头文件搜索路径。
  227. 这些事项凸显了 Glibc 软件包的一个重要性质 ——
  228. 它的构建机制是相当自给自足的,不依赖于工具链默认值。</para>
  229. <!--para>During the second pass of Binutils, we are able to utilize the
  230. <parameter>with-lib-path</parameter> configure switch to control
  231. <command>ld</command>'s library search path.</para-->
  232. <para>在第二次构建 Binutils 时,我们在配置时使用开关
  233. <parameter>--with-lib-path</parameter> 来控制 <command>ld</command>
  234. 的库文件搜索路径。</para>
  235. <!--para>For the second pass of GCC, its sources also need to be modified to
  236. tell GCC to use the new dynamic linker. Failure to do so will result in the
  237. GCC programs themselves having the name of the dynamic linker from the host
  238. system's <filename class="directory">/lib</filename> directory embedded into
  239. them, which would defeat the goal of getting away from the host. From this
  240. point onwards, the core toolchain is self-contained and self-hosted. The
  241. remainder of the <xref linkend="chapter-temporary-tools"/> packages all build
  242. against the new Glibc in <filename
  243. class="directory">/tools</filename>.</para-->
  244. <para>在第二次构建 GCC 时,需要修改其源码,使 GCC 使用新的动态链接器。
  245. 如果不这么做,GCC 程序中就会嵌入宿主
  246. <filename class="directory">/lib</filename> 目录中的动态链接器名称,
  247. 从而破坏我们脱离宿主环境的目标。之后,核心工具链就是自包含、
  248. 不依赖宿主的。<xref linkend="chapter-temporary-tools"/>
  249. 中的其他软件包都将在 <filename class="directory">/tools</filename>
  250. 中新的 Glibc 基础上构建。</para>
  251. <!--para>Upon entering the chroot environment in <xref
  252. linkend="chapter-building-system"/>, the first major package to be
  253. installed is Glibc, due to its self-sufficient nature mentioned above.
  254. Once this Glibc is installed into <filename
  255. class="directory">/usr</filename>, we will perform a quick changeover of the
  256. toolchain defaults, and then proceed in building the rest of the target
  257. LFS system.</para-->
  258. <para>在进入 <xref linkend="chapter-building-system"/> 中的 chroot
  259. 环境后,由于我们之前提到的自给性质,在全部重要软件包中首先安装 Glibc。
  260. 一旦 Glibc 被安装在 <filename class="directory">/usr</filename>
  261. 目录中,我们将会简便地修改工具链默认值,然后继续构建目标 LFS
  262. 系统的剩余部分。</para>
  263. </sect1>