Fix an md5sum
[linux_from_scratch.git] / chapter06 / adjusting.xml
blob64767b25f24bd0cee80bd0af3290315412585cbe
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 ]>
8 <sect1 id="ch-system-adjusting">
9   <?dbhtml filename="adjusting.html"?>
11   <title>Adjusting the Toolchain</title>
13   <para>Now that the final C libraries have been installed, it is time to adjust
14   the toolchain so that it will link any
15   newly compiled program against these new libraries.</para>
17   <para>First, backup the <filename class="directory">/tools</filename> linker,
18   and replace it with the adjusted linker we made in chapter 5. We'll also create
19   a link to its counterpart in
20   <filename class="directory">/tools/$(uname -m)-pc-linux-gnu/bin</filename>:</para>
22 <screen><userinput>mv -v /tools/bin/{ld,ld-old}
23 mv -v /tools/$(uname -m)-pc-linux-gnu/bin/{ld,ld-old}
24 mv -v /tools/bin/{ld-new,ld}
25 ln -sv /tools/bin/ld /tools/$(uname -m)-pc-linux-gnu/bin/ld</userinput></screen>
27   <para>Next, amend the GCC specs file so that it points to the new
28   dynamic linker. Simply deleting all instances of <quote>/tools</quote> should
29   leave us with the correct path to the dynamic linker. Also adjust the specs file
30   so that GCC knows where to find the correct headers and Glibc start files.
31   A <command>sed</command> command accomplishes this:</para>
33 <screen><userinput>gcc -dumpspecs | sed -e 's@/tools@@g'                   \
34     -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
35     -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' &gt;      \
36     `dirname $(gcc --print-libgcc-file-name)`/specs</userinput></screen>
38   <para>It is a good idea to visually inspect the specs file to verify the
39   intended change was actually made.</para>
41   <para>It is imperative at this point to ensure that the basic
42   functions (compiling and linking) of the adjusted toolchain are working
43   as expected. To do this, perform the following sanity checks:</para>
45 <screen os="a"><userinput>echo 'int main(){}' &gt; dummy.c
46 cc dummy.c -v -Wl,--verbose &amp;&gt; dummy.log
47 readelf -l a.out | grep ': /lib'</userinput></screen>
49   <para os="b">There should be no errors,
50   and the output of the last command will be (allowing for
51   platform-specific differences in dynamic linker name):</para>
53 <screen os="c"><computeroutput>[Requesting program interpreter: /lib/ld-linux.so.2]</computeroutput></screen>
55   <para>Note that <filename class="directory">/lib</filename> is now
56   the prefix of our dynamic linker.</para>
58   <note><para>On 64-bit systems the interpreter should be
59   /lib64/ld-linux-x86-64.so.2.</para></note>
61   <para os="d">Now make sure that we're setup to use the correct start files:</para>
63 <screen os="e"><userinput>grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log</userinput></screen>
65   <para os="f">The output of the last command should be:</para>
67 <screen><computeroutput>/usr/lib/crt1.o succeeded
68 /usr/lib/crti.o succeeded
69 /usr/lib/crtn.o succeeded</computeroutput></screen>
71    <note><para>On 64-bit systems, the path above will be
72    /usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../lib64/.  This
73    reduces to /usr/lib64 and /usr/lib64 is a symlink that points to
74    /usr/lib.</para></note>
76   <para os="g">Verify that the compiler is searching for the correct header
77   files:</para>
79 <screen><userinput>grep -B1 '^ /usr/include' dummy.log</userinput></screen>
81   <para os="h">This command should return the following output:</para>
83 <screen><computeroutput>#include &lt;...&gt; search starts here:
84  /usr/include</computeroutput></screen>
86   <para os="i">Next, verify that the new linker is being used with the correct search paths:</para>
88 <screen os="j"><userinput>grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'</userinput></screen>
90   <para os="k">References to paths that have components with '-linux-gnu' should
91   be ignored, but otherwise the output of the last command should be:</para>
93 <screen><computeroutput>SEARCH_DIR("/usr/lib")
94 SEARCH_DIR("/lib");</computeroutput></screen>
96   <para os="l">Next make sure that we're using the correct libc:</para>
98 <screen os="m"><userinput>grep "/lib.*/libc.so.6 " dummy.log</userinput></screen>
100   <para os="n">The output of the last command (allowing for a lib64 directory 
101   on 64-bit hosts) should be:</para>
103 <screen os="o"><computeroutput>attempt to open /lib/libc.so.6 succeeded</computeroutput></screen>
105   <para os="p">Lastly, make sure GCC is using the correct dynamic linker:</para>
107 <screen os="q"><userinput>grep found dummy.log</userinput></screen>
109   <para os="r"> The output of the last command should be (allowing for
110   platform-specific differences in dynamic linker name and a lib64 directory on
111   64-bit hosts):</para>
113 <screen os="s"><computeroutput>found ld-linux.so.2 at /lib/ld-linux.so.2</computeroutput></screen>
115   <para os="t">If the output does not appear as shown above or is not received
116   at all, then something is seriously wrong. Investigate and retrace the
117   steps to find out where the problem is and correct it. The most likely
118   reason is that something went wrong with the specs file adjustment. Any
119   issues will need to be resolved before continuing on with the process.</para>
121   <para os="u">Once everything is working correctly, clean up the test files:</para>
123 <screen os="v"><userinput>rm -v dummy.c a.out dummy.log</userinput></screen>
125 </sect1>