adjusting.xml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-system-adjusting">
  8. <?dbhtml filename="adjusting.html"?>
  9. <title>调整工具链</title>
  10. <!--para>Now that the final C libraries have been installed, it is time to adjust
  11. the toolchain so that it will link any
  12. newly compiled program against these new libraries.</para-->
  13. <para>现在最终的 C 运行库已经安装好了,这时就要调整工具链,
  14. 以便将新编译的任何程序都链接到新的 C 运行库。</para>
  15. <!--para>First, backup the <filename class="directory">/tools</filename> linker,
  16. and replace it with the adjusted linker we made in chapter 5. We'll also create
  17. a link to its counterpart in
  18. <filename class="directory">/tools/$(uname -m)-pc-linux-gnu/bin</filename>:</para-->
  19. <para>首先,备份 <filename class="directory">/tools</filename>
  20. 中的链接器,把它替换成第 5 章准备的调整好的链接器。
  21. 我们也会把
  22. <filename class="directory">/tools/$(uname -m)-pc-linux-gnu/bin</filename>
  23. 中对应的链接器替换成一个符号链接:</para>
  24. <screen><userinput>mv -v /tools/bin/{ld,ld-old}
  25. mv -v /tools/$(uname -m)-pc-linux-gnu/bin/{ld,ld-old}
  26. mv -v /tools/bin/{ld-new,ld}
  27. ln -sv /tools/bin/ld /tools/$(uname -m)-pc-linux-gnu/bin/ld</userinput></screen>
  28. <!--para>Next, amend the GCC specs file so that it points to the new
  29. dynamic linker. Simply deleting all instances of <quote>/tools</quote> should
  30. leave us with the correct path to the dynamic linker. Also adjust the specs file
  31. so that GCC knows where to find the correct headers and Glibc start files.
  32. A <command>sed</command> command accomplishes this:</para-->
  33. <para>下面修改 GCC 的 specs 文件,使其指向新的动态链接器。
  34. 把所有的 <quote>/tools</quote> 删除掉就会留下正确的路径。
  35. 另外,调整 specs 文件,使得 GCC 知道去哪里寻找正确的头文件和
  36. Glibc 启动文件。一个 <command>sed</command> 命令即可完成以上工作:
  37. </para>
  38. <screen><userinput>gcc -dumpspecs | sed -e 's@/tools@@g' \
  39. -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
  40. -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' &gt; \
  41. `dirname $(gcc --print-libgcc-file-name)`/specs</userinput></screen>
  42. <!--para>It is a good idea to visually inspect the specs file to verify the
  43. intended change was actually made.</para-->
  44. <para>这时最好浏览一下生成的 specs 文件,以确认确实进行了我们期望的修改。
  45. </para>
  46. <!--para>It is imperative at this point to ensure that the basic
  47. functions (compiling and linking) of the adjusted toolchain are working
  48. as expected. To do this, perform the following sanity checks:</para-->
  49. <para>现在的当务之急是保证调整过的工具链的基本功能(编译和链接)
  50. 能够像我们期望的那样工作。为此,进行下列完整性检查:</para>
  51. <screen os="a"><userinput>echo 'int main(){}' &gt; dummy.c
  52. cc dummy.c -v -Wl,--verbose &amp;&gt; dummy.log
  53. readelf -l a.out | grep ': /lib'</userinput></screen>
  54. <!--para os="b">There should be no errors,
  55. and the output of the last command will be (allowing for
  56. platform-specific differences in dynamic linker name):</para-->
  57. <para os="b">上述命令不应该出现错误,最后一行命令输出的结果应该
  58. (不同平台的动态链接器名称可能不同)是:</para>
  59. <screen os="c"><computeroutput>[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]</computeroutput></screen>
  60. <!--para>Note that on 64-bit systems <filename class="directory">/lib</filename> is
  61. the location of our dynamic linker, but is accessed via a symbolic link
  62. in /lib64.</para-->
  63. <para>注意在 64 位系统上 <filename class="directory">/lib</filename>
  64. 是动态链接器的位置,但通过 /lib64 中的符号链接访问。</para>
  65. <note><title>译注</title>
  66. <para>虽然可以把编译器查找动态链接器的位置修改到 /lib 中,
  67. 使得构建出的 LFS 系统完全不需要 /lib64 目录,
  68. 但动态链接器的位置是硬编码在 ELF 文件中的。
  69. 因此,为了运行其他机器编译出来的二进制文件
  70. (例如 BLFS 中安装 JDK 时),
  71. 必须创建该符号链接以符合通用惯例。</para>
  72. </note>
  73. <note><para>在 32 位系统上,解释器是 /lib/ld-linux.so.2 。</para></note>
  74. <para os="d">下面确认我们的设定能够使用正确的启动文件:</para>
  75. <screen os="e"><userinput>grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log</userinput></screen>
  76. <para os="f">以上命令应该输出:</para>
  77. <screen><computeroutput>/usr/lib/../lib/crt1.o succeeded
  78. /usr/lib/../lib/crti.o succeeded
  79. /usr/lib/../lib/crtn.o succeeded</computeroutput></screen>
  80. <para os="g">确认编译器能正确查找头文件:</para>
  81. <screen><userinput>grep -B1 '^ /usr/include' dummy.log</userinput></screen>
  82. <para os="h">该命令应当输出:</para>
  83. <screen><computeroutput>#include &lt;...&gt; search starts here:
  84. /usr/include</computeroutput></screen>
  85. <para os="i">下一步确认新的链接器使用了正确的搜索路径:</para>
  86. <screen os="j"><userinput>grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'</userinput></screen>
  87. <para os="k">那些包含 '-linux-gnu' 的路径应该忽略,
  88. 除此之外,以上命令应该输出:</para>
  89. <screen><computeroutput>SEARCH_DIR("/usr/lib")
  90. SEARCH_DIR("/lib")</computeroutput></screen>
  91. <para os="l">之后确认我们使用了正确的 libc:</para>
  92. <screen os="m"><userinput>grep "/lib.*/libc.so.6 " dummy.log</userinput></screen>
  93. <para os="n">以上命令应该输出:</para>
  94. <screen os="o"><computeroutput>attempt to open /lib/libc.so.6 succeeded</computeroutput></screen>
  95. <para os="p">最后,确认 GCC 使用了正确的动态链接器:</para>
  96. <screen os="q"><userinput>grep found dummy.log</userinput></screen>
  97. <para os="r">以上命令应该输出(不同平台的动态链接器名称可能不同):</para>
  98. <screen os="s"><computeroutput>found ld-linux-x86-64.so.2 at /lib/ld-linux-x86-64.so.2</computeroutput></screen>
  99. <!--para os="t">If the output does not appear as shown above or is not received
  100. at all, then something is seriously wrong. Investigate and retrace the
  101. steps to find out where the problem is and correct it. The most likely
  102. reason is that something went wrong with the specs file adjustment. Any
  103. issues will need to be resolved before continuing with the process.</para-->
  104. <para os="t">如果输出和以上描述不符,或者根本没有输出,
  105. 那么必然有什么地方出了严重错误。检查并重新跟踪以上步骤,
  106. 找到问题的原因,并修复它。
  107. 最可能的原因是修改 specs 文件的时候出了错误。
  108. 这里出现的任何问题在继续构建前都必须解决。</para>
  109. <para os="u">在确认一切工作良好后,删除测试文件:</para>
  110. <screen os="v"><userinput>rm -v dummy.c a.out dummy.log</userinput></screen>
  111. </sect1>