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