source: chapter05/toolchaintechnotes.xml@ af9063d

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since af9063d was af9063d, checked in by Matthew Burgess <matthew@…>, 15 years ago

Correct and clarify toolchain explanatory notes. Fixes #2461.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@9021 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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