From: Diego Nieto Cid <[email protected]>

---
 toolchain.mdwn                        |   1 +
 toolchain/cross-gnu-from-scratch.mdwn | 397 ++++++++++++++++++++++++++
 2 files changed, 398 insertions(+)
 create mode 100644 toolchain/cross-gnu-from-scratch.mdwn

diff --git a/toolchain.mdwn b/toolchain.mdwn
index 26144cc3..01628fd6 100644
--- a/toolchain.mdwn
+++ b/toolchain.mdwn
@@ -31,3 +31,4 @@ for GNU/Linux.
 ---
 
   * [[cross-gnu]]
+  * [[Building a Cross Toolchain From Scratch|cross-gnu-from-scratch]]
diff --git a/toolchain/cross-gnu-from-scratch.mdwn 
b/toolchain/cross-gnu-from-scratch.mdwn
new file mode 100644
index 00000000..dd15dc91
--- /dev/null
+++ b/toolchain/cross-gnu-from-scratch.mdwn
@@ -0,0 +1,397 @@
+[[!meta copyright="Copyright © 2026 Free Software Foundation,
+Inc."]]
+
+[[!meta license="""[[!toggle id="license" text="GFDL 1.2+"]][[!toggleable
+id="license" text="Permission is granted to copy, distribute and/or modify this
+document under the terms of the GNU Free Documentation License, Version 1.2 or
+any later version published by the Free Software Foundation; with no Invariant
+Sections, no Front-Cover Texts, and no Back-Cover Texts.  A copy of the license
+is included in the section entitled [[GNU Free Documentation
+License|/fdl]]."]]"""]]
+
+[[!tag stable_URL]]
+
+# Building a Cross Toolchain from Scratch
+
+## Install Dependencies
+
+To start building a toolchain that allows to cross compile the
+GNU/Hurd some system libraries must be installed in the host OS.
+
+  * __bison__
+  * __flex__
+  * the three libraries GCC needs.
+      * __libgmp-dev__
+      * __libmpfr-dev__
+      * __libmpc-dev__
+  * __texinfo__ (for makeinfo command)
+  * packages for making bootable images
+      * __grub-pc-bin__
+      * __grub2-common__
+      * __parted__
+
+## Setup Environment
+
+To support the build process a few environment variables need to be defined.
+They are used in the commands that have to be run during the compilation
+process.
+
+    $ export DEV_DIR=~/dev/hurd/cross-gnu
+    $ export TARGET=i686-gnu
+    $ export PREFIX=$DEV_DIR/gnu
+    $ export SYSROOT=$DEV_DIR/sysroot
+    $ export PATH="$PREFIX/bin:$PATH"
+
+The following directories must be created before the process can be started.
+
+    $ mkdir -p "$DEV_DIR/src"
+    $ mkdir -p "$PREFIX"
+    $ mkdir -p "$SYSROOT/include"
+
+The `TARGET` variable is the system architecture we are building a cross
+toolchain for.
+
+The `PREFIX` variable points to a directory which holds all the installed
+host tools for building the cross-toolchain: like the linker, assembler or
+compiler.
+
+And the `SYSROOT` variable holds the path to the directory where all the
+libraries and programs for the target system are installed.
+
+Finally, some version variables are defined that will be used to download
+the required sources.
+
+    $ export BINUTILS_VERSION=2.39
+    $ export GCC_VERSION=12.2.0
+
+## Building the Tool Chain
+
+In order to build a cross toolchain some cyclic dependencies must
+be dealt with and it's thus an inherently multi step process.
+
+### Compiling binutils
+
+The binutils package provides utilities to work with the target system
+binaries and assembling and linking for it.
+
+    $ cd $DEV_DIR/src
+    $ wget https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VERSION.tar.gz
+    $ tar xf binutils-$BINUTILS_VESION.tar.gz
+    $ cd binutils-$BINUTILS_VERSION
+    $ mkdir build && cd build
+    $ ../configure --target=$TARGET \
+                   --prefix=$PREFIX \
+                   --with-sysroot=$SYSROOT \
+                   --disable-nls \
+                   --disable-werror
+    $ make -j$(nproc)
+    $ make install
+
+The `--with-sysroot` parameter sets the prefix for target system
+paths, for instance, `i686-gnu-ld` will look `/lib/libc.so` under
+`$SYSROOT`.
+
+Verify that e.g. `$TARGET-nm --version` works.
+
+### GCC and GLIBC
+
+Building a cross-compiler is multi stage process since there is no
+C library available for the target system yet.
+
+    GCC Stage 1 -> glibc -> GCC Stage 2
+
+#### GCC Stage 1
+
+For the first stage of `GCC` compilation, a minimal compiler is needed
+that is just capable of hanlding the bootstraping process by compiling
+`glibc`.
+
+    $ cd $DEV_DIR/src
+    $ wget https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.gz
+    $ tar xf gcc-$GCC_VERSION.tar.gz
+    $ cd gcc-$GCC_VERSION
+    $ mkdir build-stage1 && cd build-stage1
+    $ ../configure \
+            --target=$TARGET \
+            --prefix=$PREFIX \
+            --with-sysroot=$SYSROOT \
+            --with-newlib \
+            --without-headers \
+            --enable-initfini-array \
+            --disable-nls \
+            --disable-shared \
+            --disable-multilib \
+            --disable-decimal-float \
+            --disable-threads \
+            --disable-libatocim \
+            --disable-libgomp \
+            --disable-libquadmath \
+            --disable-libssp \
+            --disable-libvtv \
+            --disable-libstdcxx \
+            --enable-languages=c
+    $ make -j$(nproc) all-gcc all-target-libgcc
+    $ make install-gcc install-target-libgcc
+
+With the `--with-newlib` and `--without-headers` we tell the GCC
+buildsystem to avoid including headers of the target system that
+don't yet exist. `--disable-shared` and `--disable-threads` prevents
+the `libgcc_s.so` from being built.
+
+#### GLIBC prerequisites on GNU/Hurd
+
+To ultimately build `glibc` under _GNU/Hurd_ some header files
+and tools need to be installed before compilation succeeds.
+
+  1. `gnumach` headers are required for Hurd and MIG to be able
+     to refer to RPC provided by the kernel.
+  2. `mig`, the Mach Interface Generator, needs to be compiled
+     as a host tool that produces output for `$TARGET` getting
+     _.defs_ files as input.
+  3. `Hurd` headers must be installed before glibc can be built.
+     `mig` is used to generate headers for the IPC interfaces of
+     the system.
+
+##### gnumach
+
+    $ cd $DEV_DIR/src
+    $ git clone git://git.savannah.gnu.org/hurd/gnumach.git
+    $ cd gnumach
+    $ autoreconf -i
+    $ mkdir build && cd build
+    $ ../configure --host=$TARGET --prefix=
+    $ make DESTDIR=$SYSROOT install-data
+
+##### mig
+
+    $ cd $DEV_DIR/src
+    $ git clone git://git.savannah.gnu.org/hurd/mig.git
+    $ cd mig
+    $ autoreconf -i
+    $ mkdir build && cd build
+    $ ../configure --target=$TARGET --prefix=$PREFIX
+    $ make -j$(nproc)
+    $ make install
+
+##### Hurd
+
+    $ cd $DEV_DIR/src
+    $ git clone git://git.savannah.gnu.org/hurd/hurd.git
+    $ cd hurd
+    $ autoreconf -i
+    $ mkdir build && cd build
+    $ ../configure --host=$TARGET --prefix= --without-parted \
+        --without-libbz2 --without-libz --without-rump \
+        --disable-profile \
+        ac_cv_func_file_exec_paths=no \
+        ac_cv_func_exec_exec_paths=no \
+        ac_cv_func__hurd_exec_paths=no \
+        ac_cv_func__hurd_libc_proc_init=no \
+        ac_cv_func_mach_port_set_ktype=no \
+        ac_cv_func_file_utimens=no \
+        ac_cv_lib_crypt_crypt=no \
+        ac_cv_lib_ncursesw_initscr=no
+    $ make DESTDIR=$SYSROOT no_deps=t install-headers
+
+The few `ac_cv_*` arguments disable the production of some
+test executables that will inevitably fail as there's no glibc
+for the target system yet.
+
+#### glibc
+
+Finally, `glibc` can be configured and built against the produced
+headers.
+
+    $ cd $DEV_DIR/src/glibc
+    $ git clone https://sourceware.org/git/glibc.git
+    $ cd glibc
+    $ mkdir build && cd build
+    $ ../configure \
+          --host=$TARGET \
+          --build=$(../scripts/config.guess) \
+          --prefix= \
+          --with-headers=$SYSROOT/include \
+          --disable-werror \
+          libc_cv_forced_unwind=yes \
+          libc_cv_c_cleanup=yes \
+          CXX=false
+    $ make install-bootstrap-headers=yes install-headers DESTDIR=$SYSROOT
+
+`--host=$TARGET` indicates on what architecture the built code will run.
+`CXX=false` prevents the build process from using the host's g++ compiler
+to build test programs like `links-dso-program`.
+
+Now the full `glibc` is built and installed at the `$SYSROOT`.
+
+    $ make -j$(nproc)
+    $ make install DESTDIR=$SYSROOT
+
+#### GCC Stage 2
+
+Once `glibc` is built, the process can continue with the preparation of
+a fully capable `GCC` that can be used for building programs for the target
+system or even building `gnumach` and `Hurd`.
+
+    $ cd $DEV_DIR/src/gcc-$GCC_VERSION
+    $ mkdir build-stage2 && cd build-stage2
+    $ ../configure \
+            --target=$TARGET \
+            --prefix=$PREFIX \
+            --with-sysroot=$SYSROOT \
+            --enable-initfini-array \
+            --disable-nls \
+            --disable-multilib \
+            --enable-shared \
+            --enable-threads=posix \
+            --enable-languages=c,c++
+    $ make -j$(nproc)
+    $ make install
+
+At this point it should be possible to cross-compile simple C
+programs for the Hurd with no ceremony:
+
+    $ $TARGET-gcc hello.c -o hello
+    $ file hello
+    hello: ELF 32-bit LSB executable, Intel i386, version 1 (SYSV),
+    dynamically linked, interpreter /lib/ld.so, for GNU/Hurd 0.0.0,
+    with debug_info, not stripped
+
+### (BONUS) Making a bootable image
+
+To make a bootable image first a full build of `gnumach` and
+`Hurd` need to be done.
+
+For `Hurd`, the linker must be told about the libmachuser.so and
+libhurduser.so libraries.
+
+    $ cd $SYSROOT/lib
+    $ cat libc.so
+    /* GNU ld script
+       Use the shared library, but some functions are only in
+       the static library, so try that secondarily.  */
+    OUTPUT_FORMAT(elf32-i386)
+    GROUP ( /lib/libc.so.0.3 /lib/libcrt_nonshared.a /lib/libhurduser.so 
/lib/libmachuser.so AS_NEEDED ( /lib/ld.so.1 ) )
+
+Make sure /lib/libhurduser.so and /lib/libmachuser.so is listed
+inthe GROUP section.
+
+#### gnumach
+
+    $ cd $DEV_DIR/src/gnumach/build
+    $ make distclean
+    $ ../configure --prefix= --host=$TARGET
+    $ make -j$(nproc)
+    $ make DESTDIR=$SYSROOT install
+
+#### Hurd
+
+    $ cd $DEV_DIR/src/hurd/build
+    $ make distclean
+    $ ../configure --prefix= \
+                   --host=$TARGET \
+                   --without-parted \
+                   --without-libcrypt \
+                   --without-libbz2 \
+                   --without-libz \
+                   --without-rump
+    $ make -j$(nproc)
+    $ fakeroot make install DESTDIR=$SYSROOT
+
+`fakeroot` is required for constructing the `$SYSROOT`/dev and `$SYSROOT`/lib
+directories where the install Makefile target tries to put root owned files.
+
+#### Making the image
+
+For programs to be able to run in the target system, the `$SYSROOT` libr
+directory must contain a link namead ld.so.
+
+    $ ln -s ld.so.1 "$SYSROOT/lib/ld.so"
+
+Since a full fledged init system with core utilities is not available, the
+image will be tested using a simple userspace program that prints a banner
+and loops infinitely.
+
+    $ cd $SYSROOT
+    $ mv libexec/runsystem libexec/runsystem.bak
+    $ mv libexec/runsystem.hurd libexec/runsystem.hurd.bak
+    $ cat << 'EOF' > /tmp/banner.c
+    #include <stdio.h>
+    #include <unistd.h>
+
+    int main(void) {
+        printf("================================\n");
+        printf("Hello from usermode on GNU/Hurd!\n");
+        printf("PID %d\n", getpid());
+        printf("================================\n");
+        fflush(stdout);
+
+        while (1) {
+            sleep(10);
+        }
+        return 0;
+    }
+    EOF
+    $ "$TARGET-gcc" /tmp/banner.c -o libexec/runsystem
+
+Next, some directories and files that must exist in a
+bootable system are created.
+
+    $ mkdir -p boot/grub root servers servers/socket tmp usr
+    $ chmod 1777 tmp
+    $ touch 
$SYSROOT/servers/{exec,crash-kill,default-pager,password,socket,startup,proc,auth}
+
+And finally the bootable image can be produced. First, a very
+basic raw disk image is created.
+
+    $ dd if=/dev/zero of=$DEV_DIR/hurd.img bs=1M count=256
+    $ parted -s "$DEV_DIR/hurd.img" mklabel msdos
+    $ parted -s "$DEV_DIR/hurd.img" mkpart primary ext2 1MiB "100%"
+    $ parted -s "$DEV_DIR/hurd.img" set 1 boot on
+
+Then the grub configuration is created.
+
+    $ cat << 'EOF' > $SYSROOT/boot/grub/grub.cfg
+    set timeout=5
+    set default=0
+
+    menuentry "GNU/Hurd Minimal" {
+        insmod ext2
+        set root=(hd0,msdos1)
+        multiboot /boot/gnumach root=part:1:device:hd0 console=com0
+        module /hurd/ext2fs.static ext2fs \
+            --multiboot-command-line='${kernel-command-line}' \
+            --host-priv-port='${host-port}' \
+            --device-master-port='${device-port}' \
+            --exec-server-task='${exec-task}' \
+            -T typed '${root}' \
+            '$(task-create)' '$(task-resume)'
+        module /lib/ld.so.1 exec /hurd/exec '$(exec-task=task-create)'
+    }
+    EOF
+
+Then, the data partition is formatted and initialized with the contents
+of the `$SYSROOT` directory.
+
+    $ fakeroot mke2fs -t ext2 -o hurd -d "$SYSROOT" -E offset=1048576 
"$DEV_DIR/hurd.img" 255M
+
+Finally, grub is installed into the image MBR. Use losetup to mount the
+filesystem and access the raw disk.
+
+    (NOTE check the loop device assigned to your image)
+
+    $ cd "$DEV_DIR"
+    $ sudo losetup -Pf hurd.img
+    $ mkdir mnt
+    $ sudo mount /dev/loop0p1 mnt
+    $ sudo grub-install --target=i386-pc \
+                        --boot-directory=./mnt/boot \
+                        --modules="part_msdos ext2 multiboot" \
+                        --force \
+                        /dev/loop0
+    $ sudo umount mnt
+    $ sudo losetup -d /dev/loop0
+
+The image is ready for being run with, for example, QEMU.
+
+    $ qemu-system-i386 -m 512M -drive 
file="$DEV_DIR/hurd.img",format=raw,index=0,media=disk -nographic
+
-- 
2.47.3


Reply via email to