From: Diego Nieto Cid <[email protected]>

---
 toolchain.mdwn                        |   1 +
 toolchain/cross-gnu-from-scratch.mdwn | 491 ++++++++++++++++++++++++++
 2 files changed, 492 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..16a50508
--- /dev/null
+++ b/toolchain/cross-gnu-from-scratch.mdwn
@@ -0,0 +1,491 @@
+[[!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]]
+
+# Opening Words
+
+This guide is meant to be executed manually, for educational
+purposes. It sheds some light on what is required to build
+a GCC cross-compiler.
+
+Here, the requirement is using that cross-compiler to create
+executables for the GNU/Hurd or even to build a booting
+GNU/Hurd system, as demonstrated on the last section.
+
+Nonetheless, the procedure can also be applied generally
+to other GLIBC-based systems, by following its
+GLIBC-required headers installation steps.
+
+If you prefer having an executable script that automates
+the work, here is an awk script that extracts the shell
+commands in this guide to standard output.
+
+    BEGIN {
+       print "#!/bin/bash"
+       print "set -e"
+       print ""
+    }
+
+    # Enter printing mode
+    /^[ ]{4}\$/ {
+       p=1
+    }
+
+    p {
+       line = substr($0, delim ? 5 : 7)
+       print line
+
+
+       # 1. Detect here-doc start (e.g., << EOF, << '\''EOF'\'', <<- "EOF")
+       if (!delim && match($0, 
/<<-?\[[:space:]]*['\''"]?[^[:space:]'\''";]+['\''"]?/)) {
+               delim = substr($0, RSTART, RLENGTH)
+               sub(/^<<-?\[[:space:]]*['\''"]?/, "", delim)
+               sub(/['\''"]?$/, "", delim)
+               next
+       }
+
+       # 2. If inside a here-doc, keep printing until the closing delimiter 
line
+       if (delim) {
+               if (line ~ "^\[[:space:]]*" delim "\[[:space:]]*$") {
+                       delim = ""
+                       if (!/\\$/) p = 0
+               }
+               next
+       }
+
+       # 3. Standard trailing-slash check
+       if (!/\\$/) p = 0
+    }
+
+
+# Building a Cross Toolchain from Scratch
+
+## Install Dependencies
+
+To start building a toolchain that allows cross-compiling the
+GNU/Hurd, some system libraries must be installed in the host OS.
+
+  * __bison__ (package _bison_ in Debian)
+  * __flex__ (package _flex_ in Debian)
+  * the three libraries GCC needs.
+      * The __GMP__ library (package _libgmp-dev_ in Debian)
+      * The __GNU MPFR__ library (package _libmpfr-dev_ in Debian)
+      * The __GNU MPC__ library (package _libmpc-dev_ in Debian)
+  * the makeinfo command is required for building GNU Mach
+      * __GNU Texinfo__ (package _texinfo_ in Debian)
+  * packages for making bootable images
+      * __GNU GRUB__ (packages _grub-pc-bin_ and _grub2-common_ in Debian)
+      * __GNU Parted__ (package _parted_ in Debian)
+
+## 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.
+
+    $ DEV_DIR="$HOME/dev/hurd/cross-gnu"
+    $ TARGET=i686-gnu
+    $ PREFIX="$DEV_DIR"/gnu
+    $ 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" "$DEV_DIR/tmp"
+    $ mkdir -p "$PREFIX"
+    $ mkdir -p "$SYSROOT/include" "$SYSROOT/lib"
+
+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.
+
+    $ BINUTILS_VERSION=2.47
+    $ GCC_VERSION=16.2.0
+    $ GLIBC_COMMITTISH=glibc-2.44
+    $ GNUMACH_COMMITTISH=v1.8+git20261028
+    $ MIG_COMMITTISH=master
+    $ HURD_COMMITTISH=v0.9.git20261028
+
+## Building the Toolchain
+
+In order to build a cross-toolchain, some cyclic dependencies must
+be dealt with and it is 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"
+    $ test -f "binutils-$BINUTILS_VERSION.tar.gz" || wget 
"https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VERSION.tar.gz";
+    $ test -d "binutils-$BINUTILS_VERSION" || tar xf 
"binutils-$BINUTILS_VERSION.tar.gz"
+    $ cd "binutils-$BINUTILS_VERSION"
+    $ mkdir -p 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 for `/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 handling the bootstrapping process by compiling
+`glibc`.
+
+    $ cd "$DEV_DIR/src"
+    $ test -f "gcc-$GCC_VERSION.tar.gz" || wget 
"https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.gz";
+    $ test -d "gcc-$GCC_VERSION" || tar xf "gcc-$GCC_VERSION.tar.gz"
+    $ cd "gcc-$GCC_VERSION"
+    $ mkdir -p build && cd build
+    $ ../configure \
+            --target="$TARGET" \
+            --prefix="$PREFIX" \
+            --with-sysroot="$SYSROOT" \
+            --disable-multilib \
+            --disable-libatomic \
+            --enable-languages=c
+    $ make -j$(nproc) all-gcc
+    $ make install-gcc
+
+#### 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 RPCs 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"
+    $ test -d gnumach || git clone -n 
git://git.savannah.gnu.org/hurd/gnumach.git
+    $ cd gnumach
+    $ git checkout "$GNUMACH_COMMITTISH"
+    $ autoreconf -i
+    $ mkdir -p build && cd build
+    $ CC=gcc ../configure --host="$TARGET" --prefix=
+    $ make DESTDIR="$SYSROOT" install-data
+
+`--host=$TARGET` indicates on what architecture the built code will run.
+And with `CC=gcc` the build system is told to use the host's GCC instead
+of the cross-compiler (which doesn't work yet). After all, only the
+headers are needed at this stage.
+
+##### mig
+
+    $ cd "$DEV_DIR/src"
+    $ test -d mig || git clone -n git://git.savannah.gnu.org/hurd/mig.git
+    $ cd mig
+    $ git checkout "$MIG_COMMITTISH"
+    $ autoreconf -i
+    $ mkdir -p build && cd build
+    $ ../configure --target="$TARGET" --prefix="$PREFIX"
+    $ make -j$(nproc)
+    $ make install
+
+##### Hurd
+
+    $ cd "$DEV_DIR/src"
+    $ test -d hurd || git clone -n git://git.savannah.gnu.org/hurd/hurd.git
+    $ cd hurd
+    $ git checkout "$HURD_COMMITTISH"
+    $ autoreconf -i
+    $ mkdir -p build && cd build
+    $ CC=gcc ../configure \
+        --host="$TARGET" \
+        --prefix= \
+        --without-parted \
+        --without-libcrypt \
+        --without-libbz2 \
+        --without-libz \
+        --without-rump \
+        --disable-profile
+    $ make DESTDIR="$SYSROOT" no_deps=t install-headers
+
+
+The same `CC` trick in `gnumach` is used here.
+
+#### glibc Stage 1
+
+With `gnumach` and `hurd` headers, the `glibc` headers can be
+installed in preparation to build GCC fully.
+
+    $ cd "$DEV_DIR/src"
+    $ test -d glibc || git clone -n https://sourceware.org/git/glibc.git
+    $ cd glibc
+    $ git checkout "$GLIBC_COMMITTISH"
+    $ mkdir -p build && cd build
+    $ ../configure \
+          --host="$TARGET" \
+          --build=$(../scripts/config.guess) \
+          --prefix= \
+          --with-headers="$SYSROOT/include" \
+          CXX=false
+    $ make DESTDIR="$SYSROOT" install-headers
+
+`CXX=false` prevents the build process from using the host's g++ compiler
+to build test programs like `links-dso-program`. Otherwise, the following
+error will pop up during the make step:
+
+    /home/demo/dev/hurd/cross-gnu/src/glibc/build/support/links-dso-program.o: 
file not recognized: file format not recognized
+    collect2: error: ld returned 1 exit status
+
+Some prerequisites need to exist before GCC can be fully built.
+
+    $ touch "$SYSROOT/include/gnu/stubs.h"
+    $ touch "$SYSROOT/lib/libc.so"
+    $ make csu/subdir_lib
+    $ install csu/crt1.o csu/crti.o csu/crtn.o "$SYSROOT/lib"
+
+#### GCC Stage 2
+
+Once `glibc` stage 1 is built, the process can continue with the preparation
+of a fully capable `GCC` that can be used for the final build of `glibc` and,
+after that, programs for the target system or even building `gnumach` and 
`Hurd`.
+
+    $ cd "$DEV_DIR/src/gcc-$GCC_VERSION/build"
+    $ make -j$(nproc) configure-target-libgcc
+    $ make -j$(nproc) all-target-libgcc
+    $ make install-target-libgcc
+
+#### glibc Stage 2
+
+    $ cd "$DEV_DIR/src/glibc/build"
+    $ make PARALLELMFLAGS="-j$(nproc)"
+    $ make install DESTDIR="$SYSROOT"
+
+At this point it should be possible to cross-compile simple C
+programs for the Hurd with no ceremony:
+
+    $ cd "$DEV_DIR"
+    $ cat << 'EOF' > hello.c
+    #include <stdio.h>
+    int main(void) {
+        printf("Hello World!\n");
+        return 0;
+    }
+    EOF
+    $ "$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 `GNU/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  AS_NEEDED ( /lib/ld.so.1 
) )
+
+Make sure /lib/libhurduser.so and /lib/libmachuser.so are listed
+in the GROUP section. If it isn't (as above) add it with, for instance, sed:
+
+    $ sed -i 's| AS_NEEDED|/lib/libmachuser.so /lib/libhurduser.so  
AS_NEEDED|' libc.so
+
+Recheck, the libraries should be now listed.
+
+    $ 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/libmachuser.so 
/lib/libhurduser.so  AS_NEEDED ( /lib/ld.so.1 ) )
+
+#### gnumach
+
+    $ cd "$DEV_DIR/src/gnumach/build"
+    $ make distclean
+    $ ../configure --prefix= --host="$TARGET"
+    $ make -j$(nproc)
+    $ make DESTDIR="$SYSROOT" install
+
+#### Hurd
+
+The configure step of GNU Hurd makes use of pkg-config to find some
+libraries that it may require to operate. However, the host's .pc
+files need to be omitted from the lookup path.
+
+    $ cd "$DEV_DIR/src/hurd/build"
+    $ make distclean
+    $ PKG_CONFIG_PATH= \
+      PKG_CONFIG_LIBDIR="$SYSROOT/lib/pkgconfig:$SYSROOT/share/pkgconfig" \
+      PKG_CONFIG_SYSROOT_DIR="$SYSROOT" \
+      ../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
+
+   (NOTE: The following section only applies to 32-bit builds
+          of the GNU/Hurd where in-kernel device drivers are
+          available at gnumach.
+
+          x86_64 builds need user-space device drivers provided
+          by the rump project which have been disabled in the
+          current guide for simplicity's sake.)
+
+For programs to be able to run in the target system, the `$SYSROOT` lib
+directory must contain a link named 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 user-space 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' > "$DEV_DIR/tmp/banner.c"
+    #include <stdio.h>
+    #include <sys/reboot.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);
+
+        int n = 0;
+        while (n++ < 5) {
+            sleep(5);
+            printf(".");
+            fflush(stdout);
+        }
+        printf("\n");
+        reboot(RB_AUTOBOOT);
+        return 0;
+    }
+    EOF
+    $ "$TARGET-gcc" "$DEV_DIR/tmp/banner.c" -o libexec/runsystem
+
+Next, some directories and files that must exist in a
+bootable system are created.
+
+    $ cd "$SYSROOT"
+    $ mkdir -p boot/grub root servers servers/socket tmp usr
+    $ chmod 1777 tmp
+    $ touch 
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.
+
+    $ cd "$DEV_DIR"
+    $ dd if=/dev/zero of=hurd.img bs=1M count=256
+    $ parted -s hurd.img mklabel msdos
+    $ parted -s hurd.img mkpart primary ext2 1MiB "100%"
+    $ parted -s 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=device:hd0s1 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.
+
+    (WARNING: Double-check the loop device assigned to your image.
+              Permanent damage may be done to the system if
+              grub-install is run against the wrong device.)
+
+    $ cd "$DEV_DIR"
+    $ loopdev=$(sudo losetup --find --show --partscan hurd.img)
+    $ mkdir -p mnt
+    $ sudo mount "${loopdev}p1" mnt
+    $ sudo grub-install --target=i386-pc \
+                        --boot-directory=./mnt/boot \
+                        --modules="part_msdos ext2 multiboot" \
+                        --force \
+                        "$loopdev"
+    $ sudo umount mnt
+    $ sudo losetup -d "$loopdev"
+
+The image is ready to be 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