source: chapter05/toolchaintechnotes.xml@ c863747

6.0
Last change on this file since c863747 was c863747, checked in by Manuel Canales Esparcia <manuel@…>, 20 years ago

Wrapped the commands until Chapter 5 to 60 chars.
Testing new look for 'cat > .... << EOF' blocks.

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

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