| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [  <!ENTITY % general-entities SYSTEM "../general.ent">  %general-entities;]><sect1 id="ch-tools-toolchaintechnotes"><title>Toolchain Technical Notes</title><?dbhtml filename="toolchaintechnotes.html"?><para>This section explains some of the rationale and technicaldetails behind the overall build method. It is not essential toimmediately understand everything in this section. Most of thisinformation will be clearer after performing an actual build. Thissection can be referred back to at any time during the process.</para><para>The overall goal of <xref linkend="chapter-temporary-tools"/> is toprovide a temporary environment that can be chrooted into and from which can beproduced a clean, trouble-free build of the target LFS system in <xreflinkend="chapter-building-system"/>. Along the way, we separate the new systemfrom the host system as much as possible, and in doing so, build aself-contained and self-hosted toolchain. It should be noted that the buildprocess has been designed to minimize the risks for new readers and providemaximum educational value at the same time.</para><important><para>Before continuing, be aware of the name of the working platform,often referred to as the target triplet. Many times, the targettriplet will probably be <emphasis>i686-pc-linux-gnu</emphasis>. Asimple way to determine the name of the target triplet is to run the<command>config.guess</command> script that comes with the source formany packages. Unpack the Binutils sources and run the script:<userinput>./config.guess</userinput> and note the output.</para><para>Also be aware of the name of the platform's dynamic linker,often referred to as the dynamic loader (not to be confused with thestandard linker <command>ld</command> that is part of Binutils). Thedynamic linker provided by Glibc finds and loads the shared librariesneeded by a program, prepares the program to run, and then runs it.The name of the dynamic linker will usually be<filename class="libraryfile">ld-linux.so.2</filename>. On platforms that are lessprevalent, the name might be <filename class="libraryfile">ld.so.1</filename>, and newer 64 bit platforms might be named something else entirely. The name ofthe platform's dynamic linker can be determined by looking in the<filename class="directory">/lib</filename> directory on the hostsystem. A sure-fire way to determine the name is to inspect a randombinary from the host system by running: <userinput>readelf -l <nameof binary> | grep interpreter</userinput> and noting the output.The authoritative reference covering all platforms is in the<filename>shlib-versions</filename> file in the root of the Glibcsource tree.</para></important><para>Some key technical points of how the <xref linkend="chapter-temporary-tools"/> buildmethod works:</para><itemizedlist><listitem><para>The process is similar in principle tocross-compiling, whereby tools installed in the same prefix work incooperation, and thus utilize a little GNU<quote>magic</quote></para></listitem><listitem><para>Careful manipulation of the standard linker's librarysearch path ensures programs are linked only against chosenlibraries</para></listitem><listitem><para>Careful manipulation of <command>gcc</command>'s<filename>specs</filename> file tells the compiler which target dynamiclinker will be used</para></listitem></itemizedlist><para>Binutils is installed first because the<command>configure</command> runs of both GCC and Glibc performvarious feature tests on the assembler and linker to determine whichsoftware features to enable or disable. This is more important thanone might first realize. An incorrectly configured GCC or Glibc canresult in a subtly broken toolchain, where the impact of such breakagemight not show up until near the end of the build of an entiredistribution. A test suite failure will usually highlight this errorbefore too much additional work is performed.</para><beginpage/><para>Binutils installs its assembler and linker in two locations,<filename class="directory">/tools/bin</filename> and <filenameclass="directory">/tools/$TARGET_TRIPLET/bin</filename>. The tools inone location are hard linked to the other. An important facet of thelinker is its library search order. Detailed information can beobtained from <command>ld</command> by passing it the<parameter>--verbose</parameter> flag. For example, an <userinput>ld--verbose | grep SEARCH</userinput> will illustrate the current searchpaths and their order. It shows which files are linked by<command>ld</command> by compiling a dummy program and passing the<parameter>--verbose</parameter> switch to the linker. For example,<userinput>gcc dummy.c -Wl,--verbose 2>&1 | grepsucceeded</userinput> will show all the files successfully openedduring the linking.</para><para>The next package installed is GCC. An example of what can beseen during its run of <command>configure</command> is:</para><screen><computeroutput>checking what assembler to use...         /tools/i686-pc-linux-gnu/bin/aschecking what linker to use... /tools/i686-pc-linux-gnu/bin/ld</computeroutput></screen><para>This is important for the reasons mentioned above. It alsodemonstrates that GCC's configure script does not search the PATHdirectories to find which tools to use. However, during the actualoperation of <command>gcc</command> itself, the samesearch paths are not necessarily used. To find out which standardlinker <command>gcc</command> will use, run: <userinput>gcc-print-prog-name=ld</userinput>.</para><para>Detailed information can be obtained from <command>gcc</command>by passing it the <parameter>-v</parameter> command line option whilecompiling a dummy program. For example, <userinput>gcc -vdummy.c</userinput> will show detailed information about thepreprocessor, compilation, and assembly stages, including<command>gcc</command>'s included search paths and their order.</para><para>The next package installed is Glibc. The most importantconsiderations for building Glibc are the compiler, binary tools, andkernel headers. The compiler is generally not an issue since Glibcwill always use the <command>gcc</command> found in a <envar>PATH</envar> directory.The binary tools and kernel headers can be a bit more complicated.Therefore, take no risks and use the available configure switches toenforce the correct selections. After the run of<command>configure</command>, check the contents of the<filename>config.make</filename> file in the <filenameclass="directory">glibc-build</filename> directory for all importantdetails. Note the use of <parameter>CC="gcc -B/tools/bin/"</parameter>to control which binary tools are used and the use of the<parameter>-nostdinc</parameter> and <parameter>-isystem</parameter>flags to control the compiler's include search path. These itemshighlight an important aspect of the Glibc package—it is veryself-sufficient in terms of its build machinery and generally does notrely on toolchain defaults.</para><para>After the Glibc installation, make some adjustments to ensurethat searching and linking take place only within the <filenameclass="directory">/tools</filename> prefix.  Install an adjusted<command>ld</command>, which has a hard-wired search path limited to<filename class="directory">/tools/lib</filename>. Then amend<command>gcc</command>'s specs file to point to the new dynamic linkerin <filename class="directory">/tools/lib</filename>. This last stepis vital to the whole process. As mentioned above, a hard-wired pathto a dynamic linker is embedded into every Executable and Link Format(ELF)-shared executable.  This can be inspected by running:<userinput>readelf -l <name of binary> | grepinterpreter</userinput>. Amending gcc's specs fileensures that every program compiled from here through the end of thischapter will use the new dynamic linker in <filenameclass="directory">/tools/lib</filename>.</para><para>The need to use the new dynamic linker is also the reason whythe Specs patch is applied for the second pass of GCC. Failure to doso will result in the GCC programs themselves having the name of thedynamic linker from the host system's <filenameclass="directory">/lib</filename> directory embedded into them, whichwould defeat the goal of getting away from the host.</para> <para>During the second pass of Binutils, we are able to utilize the<parameter>--with-lib-path</parameter> configure switch to control<command>ld</command>'s library search path.  From this point onwards,the core toolchain is self-contained and self-hosted. The remainder ofthe <xref linkend="chapter-temporary-tools"/> packages all buildagainst the new Glibc in <filenameclass="directory">/tools</filename>.</para><beginpage/><para>Upon entering the chroot environment in <xreflinkend="chapter-building-system"/>, the first major package to beinstalled is Glibc, due to its self-sufficient nature mentioned above.Once this Glibc is installed into <filenameclass="directory">/usr</filename>, perform a quick changeover of thetoolchain defaults, then proceed in building the rest of the targetLFS system.</para><!-- Removed as part of the fix for bug 1061 - we no longer build pass1     packages statically, therefore this explanation isn't required --><!--<sect2><title>Notes on Static Linking</title><para>Besides their specific task, most programs have to perform manycommon and sometimes trivial operations. These include allocatingmemory, searching directories, reading and writing files, stringhandling, pattern matching, arithmetic, and other tasks. Instead ofobliging each program to reinvent the wheel, the GNU system providesall these basic functions in ready-made libraries. The major libraryon any Linux system is Glibc.</para><para>There are two primary ways of linking the functions from alibrary to a program that uses them—statically or dynamically. Whena program is linked statically, the code of the used functions isincluded in the executable, resulting in a rather bulky program. Whena program is dynamically linked, it includes a reference to thedynamic linker, the name of the library, and the name of the function,resulting in a much smaller executable. A third option is to use theprogramming interface of the dynamic linker (see <filename>dlopen(3)</filename>for more information).</para><para>Dynamic linking is the default on Linux and has three majoradvantages over static linking. First, only one copy of the executablelibrary code is needed on the hard disk, instead of having multiplecopies of the same code included in several programs, thus savingdisk space. Second, when several programs use the same libraryfunction at the same time, only one copy of the function's code isrequired in core, thus saving memory space. Third, when a libraryfunction gets a bug fixed or is otherwise improved, only the onelibrary needs to be recompiled instead of recompiling all programsthat make use of the improved function.</para><para>If dynamic linking has several advantages, why then do westatically link the first two packages in this chapter? The reasonsare threefold—historical, educational, and technical. Thehistorical reason is that earlier versions of LFS statically linkedevery program in this chapter. Educationally, knowing the differencebetween static and dynamic linking is useful. The technical benefit isa gained element of independence from the host, meaning that thoseprograms can be used independently of the host system. However, it isworth noting that an overall successful LFS build can still beachieved when the first two packages are built dynamically.</para></sect2>--></sect1>
 |