<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/default.xsl"?>
<fr:tree xmlns:fr="http://www.forester-notes.org" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xml="http://www.w3.org/XML/1998/namespace" root="false" base-url="/">
  <fr:frontmatter>
    <fr:authors>
      <fr:author>
        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
      </fr:author>
    </fr:authors>
    <fr:date>
      <fr:year>2019</fr:year>
      <fr:month>11</fr:month>
      <fr:day>10</fr:day>
    </fr:date>
    <fr:date>
      <fr:year>2025</fr:year>
      <fr:month>8</fr:month>
      <fr:day>11</fr:day>
    </fr:date>
    <fr:uri>https://xw.is/gcc-sysroot-build/</fr:uri>
    <fr:display-uri>gcc-sysroot-build</fr:display-uri>
    <fr:route>/gcc-sysroot-build/</fr:route>
    <fr:title text="Toolchains › Build GCC targeting user space"><fr:link href="/toolchain-index/" title="Toolchains" uri="https://xw.is/toolchain-index/" display-uri="toolchain-index" type="local">Toolchains</fr:link> › Build <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> targeting user space</fr:title>
  </fr:frontmatter>
  <fr:mainmatter>
    <fr:tree show-metadata="false">
      <fr:frontmatter>
        <fr:authors>
          <fr:author>
            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
          </fr:author>
        </fr:authors>
        <fr:date>
          <fr:year>2019</fr:year>
          <fr:month>11</fr:month>
          <fr:day>10</fr:day>
        </fr:date>
        <fr:date>
          <fr:year>2025</fr:year>
          <fr:month>8</fr:month>
          <fr:day>11</fr:day>
        </fr:date>
        <fr:title text="Create source and build directories">Create source and build directories</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <html:p>We use a suffix for the build directories so we can build for multiple targets at the same time. The name is arbitrary, we just choose the target name for convenience.</html:p>
        <html:pre>
mkdir -p ${HOME}/src
mkdir -p ${HOME}/obj/binutils-2.45-s390x-linux-gnu
mkdir -p ${HOME}/obj/gcc-15.2.0-s390x-linux-gnu</html:pre>
      </fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false">
      <fr:frontmatter>
        <fr:authors>
          <fr:author>
            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
          </fr:author>
        </fr:authors>
        <fr:date>
          <fr:year>2019</fr:year>
          <fr:month>11</fr:month>
          <fr:day>10</fr:day>
        </fr:date>
        <fr:date>
          <fr:year>2025</fr:year>
          <fr:month>8</fr:month>
          <fr:day>11</fr:day>
        </fr:date>
        <fr:title text="Download the source code">Download the source code</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <html:pre>
curl https://sourceware.org/pub/binutils/releases/binutils-2.45.tar.xz | tar -C $HOME/src -xJf -
curl https://sourceware.org/pub/gcc/releases/gcc-15.2.0/gcc-15.2.0.tar.xz | tar -C $HOME/src -xJf -</html:pre>
      </fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false">
      <fr:frontmatter>
        <fr:authors>
          <fr:author>
            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
          </fr:author>
        </fr:authors>
        <fr:date>
          <fr:year>2019</fr:year>
          <fr:month>11</fr:month>
          <fr:day>10</fr:day>
        </fr:date>
        <fr:date>
          <fr:year>2025</fr:year>
          <fr:month>8</fr:month>
          <fr:day>11</fr:day>
        </fr:date>
        <fr:title text="Download GCC prerequisites">Download <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> prerequisites</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
  
  <html:pre>
cd ${HOME}/src/gcc-15.2.0
./contrib/download_prerequisites
</html:pre>
</fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false">
      <fr:frontmatter>
        <fr:authors>
          <fr:author>
            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
          </fr:author>
        </fr:authors>
        <fr:date>
          <fr:year>2019</fr:year>
          <fr:month>11</fr:month>
          <fr:day>10</fr:day>
        </fr:date>
        <fr:date>
          <fr:year>2025</fr:year>
          <fr:month>8</fr:month>
          <fr:day>11</fr:day>
        </fr:date>
        <fr:title text="Prepare the sysroot">Prepare the sysroot</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <html:p>You will need header files and libraries for the target system (here <html:code>s390x-linux-gnu</html:code>). You can either obtain those files from an existing system, or <fr:link href="/debian-sysroot/" title="Toolchains › Create Debian sysroots" uri="https://xw.is/debian-sysroot/" display-uri="debian-sysroot" type="local">you can prepare a sysroot</fr:link> on an unrelated system. We will assume the sysroot is installed in <html:code>${HOME}/cross/s390x-linux-gnu/sys-root</html:code>, which is the <fr:link href="https://gcc.gnu.org/install/configure.html#Cross-Compiler-Specific-Options" type="external">default location</fr:link> for <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> sysroots.</html:p>
      </fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false">
      <fr:frontmatter>
        <fr:authors>
          <fr:author>
            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
          </fr:author>
        </fr:authors>
        <fr:date>
          <fr:year>2019</fr:year>
          <fr:month>11</fr:month>
          <fr:day>10</fr:day>
        </fr:date>
        <fr:date>
          <fr:year>2025</fr:year>
          <fr:month>8</fr:month>
          <fr:day>11</fr:day>
        </fr:date>
        <fr:title text="Build and install">Build and install</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <fr:tree show-metadata="false">
          <fr:frontmatter>
            <fr:authors>
              <fr:author>
                <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
              </fr:author>
            </fr:authors>
            <fr:date>
              <fr:year>2019</fr:year>
              <fr:month>11</fr:month>
              <fr:day>10</fr:day>
            </fr:date>
            <fr:date>
              <fr:year>2025</fr:year>
              <fr:month>8</fr:month>
              <fr:day>11</fr:day>
            </fr:date>
            <fr:title text="Binutils">
              <fr:link href="/binutils/" title="GNU Binutils" uri="https://xw.is/binutils/" display-uri="binutils" type="local">Binutils</fr:link>
            </fr:title>
          </fr:frontmatter>
          <fr:mainmatter>
            <html:p>Build and install <fr:link href="/binutils/" title="GNU Binutils" uri="https://xw.is/binutils/" display-uri="binutils" type="local">GNU Binutils</fr:link>.</html:p>
            <html:pre>
cd ${HOME}/obj/binutils-2.45-s390x-linux-gnu
../../src/binutils-2.45/configure --prefix ${HOME}/cross --with-system-zlib --target s390x-linux-gnu --enable-multiarch --disable-multilib --with-sysroot
make -j12 &amp;&amp; make install</html:pre>
          </fr:mainmatter>
        </fr:tree>
        <fr:tree show-metadata="false">
          <fr:frontmatter>
            <fr:authors>
              <fr:author>
                <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
              </fr:author>
            </fr:authors>
            <fr:date>
              <fr:year>2019</fr:year>
              <fr:month>11</fr:month>
              <fr:day>10</fr:day>
            </fr:date>
            <fr:date>
              <fr:year>2025</fr:year>
              <fr:month>8</fr:month>
              <fr:day>11</fr:day>
            </fr:date>
            <fr:title text="GCC">
              <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link>
            </fr:title>
          </fr:frontmatter>
          <fr:mainmatter>
            <html:p>Build and install <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GNU GCC</fr:link>. If we use <fr:link href="https://wiki.debian.org/Multiarch" type="external">multiarch</fr:link> sysroot (such as from Debian), we must pass <html:code>--enable-multiarch</html:code> otherwise <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> will not find the target headers and libraries. Additionally, if the sysroot doesn't make use of multilib, we must <html:em>explicitly</html:em> disable via <html:code>--disable-multilib</html:code> as otherwise <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> might default to enabling it. <html:mark>Failure to set <html:code>--enable-multiarch --disable-multilib</html:code> will result in a failed build.</html:mark></html:p>
            <html:pre>
cd ${HOME}/obj/gcc-15.2.0-s390x-linux-gnu
../../src/gcc-15.2.0/configure --prefix ${HOME}/cross --with-system-zlib --target s390x-linux-gnu --enable-languages=c,c++ --enable-multiarch --disable-multilib --with-sysroot
make -j12 &amp;&amp; make install</html:pre>
            <html:p>Note that don't pass a specific directory to <html:code>--with-sysroot</html:code> because we placed out sysroot in the default location where <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> expects it.</html:p>
          </fr:mainmatter>
        </fr:tree>
      </fr:mainmatter>
    </fr:tree>
  </fr:mainmatter>
  <fr:backmatter>
    <fr:tree show-metadata="false" hidden-when-empty="true">
      <fr:frontmatter>
        <fr:authors />
        <fr:title text="References">References</fr:title>
      </fr:frontmatter>
      <fr:mainmatter />
    </fr:tree>
    <fr:tree show-metadata="false" hidden-when-empty="true">
      <fr:frontmatter>
        <fr:authors />
        <fr:title text="Context">Context</fr:title>
      </fr:frontmatter>
      <fr:mainmatter />
    </fr:tree>
    <fr:tree show-metadata="false" hidden-when-empty="true">
      <fr:frontmatter>
        <fr:authors />
        <fr:title text="Backlinks">Backlinks</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <fr:tree show-metadata="true" expanded="false" toc="false" numbered="false">
          <fr:frontmatter>
            <fr:authors>
              <fr:author>
                <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
              </fr:author>
            </fr:authors>
            <fr:date>
              <fr:year>2025</fr:year>
              <fr:month>8</fr:month>
              <fr:day>12</fr:day>
            </fr:date>
            <fr:uri>https://xw.is/gdb-build/</fr:uri>
            <fr:display-uri>gdb-build</fr:display-uri>
            <fr:route>/gdb-build/</fr:route>
            <fr:title text="Toolchains › Build GDB and gdbserver(1)"><fr:link href="/toolchain-index/" title="Toolchains" uri="https://xw.is/toolchain-index/" display-uri="toolchain-index" type="local">Toolchains</fr:link> › Build <fr:link href="/gdb/" title="GNU Project Debugger" uri="https://xw.is/gdb/" display-uri="gdb" type="local">GDB</fr:link> and <fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link></fr:title>
          </fr:frontmatter>
          <fr:mainmatter>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>12</fr:day>
                </fr:date>
                <fr:title text="Create source and build directories">Create source and build directories</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <html:p>We use a suffix for the build directories so we can build for multiple targets at the same time. The name is arbitrary, we just choose the target name for convenience.</html:p>
                <html:pre>
mkdir -p ${HOME}/src
mkdir -p ${HOME}/obj/gdb-16.3
mkdir -p ${HOME}/obj/gdb-16.3-riscv64-unknown-elf
mkdir -p ${HOME}/obj/gdbserver-16.3-riscv64-linux-gnu</html:pre>
              </fr:mainmatter>
            </fr:tree>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>12</fr:day>
                </fr:date>
                <fr:title text="Download the source code">Download the source code</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <html:pre>curl https://sourceware.org/pub/gdb/releases/gdb-16.3.tar.xz | tar -C $HOME/src -xJf -</html:pre>
              </fr:mainmatter>
            </fr:tree>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>12</fr:day>
                </fr:date>
                <fr:title text="Build and install">Build and install</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>12</fr:day>
                    </fr:date>
                    <fr:title text="GDB">
                      <fr:link href="/gdb/" title="GNU Project Debugger" uri="https://xw.is/gdb/" display-uri="gdb" type="local">GDB</fr:link>
                    </fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p>Make sure you have <fr:link href="https://gmplib.org/" type="external">GMP</fr:link> 4.2+ and <fr:link href="https://www.mpfr.org/" type="external">GNU MPFR</fr:link> 3.1.0+ installed. Set the path in <html:code>--with-gmp</html:code> and/or <html:code>--with-mpfr</html:code>, if needed.</html:p>
                    <fr:tree show-metadata="false">
                      <fr:frontmatter>
                        <fr:authors>
                          <fr:author>
                            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                          </fr:author>
                        </fr:authors>
                        <fr:date>
                          <fr:year>2025</fr:year>
                          <fr:month>8</fr:month>
                          <fr:day>12</fr:day>
                        </fr:date>
                        <fr:title text="Native debugging">Native debugging</fr:title>
                      </fr:frontmatter>
                      <fr:mainmatter>
                        <html:p>As we are building, running, and debugging binaries for the same architecture, we do not need to set <html:em>host</html:em> or <html:em>target</html:em>. Also note that we install it in a different prefix than the cross-debugging case below (<html:code>${HOME}</html:code> vs. <html:code>${HOME}/xgcc</html:code>).</html:p>
                        <html:pre>
cd ${HOME}/obj/gdb-16.3
../../src/gdb-16.3/configure --prefix ${HOME} 
make -j12 &amp;&amp; make install
</html:pre>
                      </fr:mainmatter>
                    </fr:tree>
                    <fr:tree show-metadata="false">
                      <fr:frontmatter>
                        <fr:authors>
                          <fr:author>
                            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                          </fr:author>
                        </fr:authors>
                        <fr:date>
                          <fr:year>2025</fr:year>
                          <fr:month>8</fr:month>
                          <fr:day>12</fr:day>
                        </fr:date>
                        <fr:title text="Cross-debugging">Cross-debugging</fr:title>
                      </fr:frontmatter>
                      <fr:mainmatter>
                        <html:p>Set <html:code>--target</html:code> to your desired platform, here we use <html:code>riscv64-unknown-elf</html:code> representing bare-metal 64-bit <fr:link href="https://riscv.org/" type="external">RISC-V</fr:link>. It is fine for multiple cross toolchains to share the same <html:code>--prefix</html:code> and <html:code>--bindir</html:code>.</html:p>
                        <html:pre>
cd ${HOME}/obj/gdb-16.3-riscv64-unknown-elf
../../src/gdb-16.3/configure --prefix ${HOME}/cross --with-system-zlib --target riscv64-unknown-elf --with-gmp=/opt/homebrew
make -j12 &amp;&amp; make install</html:pre>
                      </fr:mainmatter>
                    </fr:tree>
                  </fr:mainmatter>
                </fr:tree>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>12</fr:day>
                    </fr:date>
                    <fr:title text="gdbserver(1)">
                      <fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link>
                    </fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p><fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link> is a program that runs on the same machine as the binaries you are debugging (the <html:em>target</html:em> machine above). You have two options, either compile <fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link> <html:em>natively</html:em> on the target machine, or cross-compile it on the <html:em>build</html:em> machine and move it to the <html:em>host</html:em> machine (which for <fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link> means the <html:em>target</html:em> machine). Of course if you can compile it natively you might as well compile <fr:link href="/gdb/" title="GNU Project Debugger" uri="https://xw.is/gdb/" display-uri="gdb" type="local">GDB</fr:link> itself on the target machine and use that directly. Consult the <fr:link href="https://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html" type="external">GCC documentation</fr:link> if you are confused by this paragraph.</html:p>
                    <fr:tree show-metadata="false">
                      <fr:frontmatter>
                        <fr:authors>
                          <fr:author>
                            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                          </fr:author>
                        </fr:authors>
                        <fr:date>
                          <fr:year>2025</fr:year>
                          <fr:month>8</fr:month>
                          <fr:day>12</fr:day>
                        </fr:date>
                        <fr:title text="Native compilation">Native compilation</fr:title>
                      </fr:frontmatter>
                      <fr:mainmatter>
                        <html:p><fr:link href="/gdbserver.1" title="gdbserver(1)" uri="https://xw.is/gdbserver.1" display-uri="gdbserver.1" type="local">gdbserver(1)</fr:link> is part of <fr:link href="/gdb/" title="GNU Project Debugger" uri="https://xw.is/gdb/" display-uri="gdb" type="local">GDB</fr:link>, so just install <fr:link href="/gdb/" title="GNU Project Debugger" uri="https://xw.is/gdb/" display-uri="gdb" type="local">GDB</fr:link> on the <html:em>host</html:em> as described above.</html:p>
                      </fr:mainmatter>
                    </fr:tree>
                    <fr:tree show-metadata="false">
                      <fr:frontmatter>
                        <fr:authors>
                          <fr:author>
                            <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                          </fr:author>
                        </fr:authors>
                        <fr:date>
                          <fr:year>2025</fr:year>
                          <fr:month>8</fr:month>
                          <fr:day>12</fr:day>
                        </fr:date>
                        <fr:title text="Cross compilation">Cross compilation</fr:title>
                      </fr:frontmatter>
                      <fr:mainmatter>
                        <html:p>You need a <fr:link href="/gcc-sysroot-build/" title="Toolchains › Build GCC targeting user space" uri="https://xw.is/gcc-sysroot-build/" display-uri="gcc-sysroot-build" type="local">GCC cross compiler</fr:link> targetting the <html:em>host</html:em> system (the one running <html:em>gdbserver</html:em>, here <html:code>riscv64-linux-gnu</html:code>).</html:p>
                        <html:pre>
cd ${HOME}/obj/gdbserver-16.3-riscv64-linux-gnu
../../src/gdbserver-16.3/configure --prefix=/ --disable-gdb --disable-nls --host riscv64-linux-gnu LDFLAGS="-static"
make -j12 all-gdbserver
make DESTDIR=/tmp install-gdbserver
</html:pre>
                        <html:p>We're building a statically-linked <html:em>gdbserver</html:em> so that it works on every system. For example, we might run <html:em>gdbserver</html:em> on a <fr:link href="https://www.musl-libc.org/" type="external">musl</fr:link>-based system, even though our Debian sysroot is based on <fr:link href="https://www.gnu.org/software/libc/" type="external">GNU libc</fr:link>. The binary is available in <html:code>/tmp/bin/gdbserver</html:code> and can be moved to the <html:em>host</html:em> system.</html:p>
                      </fr:mainmatter>
                    </fr:tree>
                  </fr:mainmatter>
                </fr:tree>
              </fr:mainmatter>
            </fr:tree>
          </fr:mainmatter>
        </fr:tree>
      </fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false" hidden-when-empty="true">
      <fr:frontmatter>
        <fr:authors />
        <fr:title text="Related">Related</fr:title>
      </fr:frontmatter>
      <fr:mainmatter>
        <fr:tree show-metadata="true" expanded="false" toc="false" numbered="false">
          <fr:frontmatter>
            <fr:authors />
            <fr:uri>https://xw.is/binutils/</fr:uri>
            <fr:display-uri>binutils</fr:display-uri>
            <fr:route>/binutils/</fr:route>
            <fr:title text="GNU Binutils">GNU Binutils</fr:title>
            <fr:taxon>Software</fr:taxon>
            <fr:meta name="external">https://www.gnu.org/software/binutils/</fr:meta>
          </fr:frontmatter>
          <fr:mainmatter>
            <html:p>Collection of low-level binary tools such as linkers and assemblers.</html:p>
          </fr:mainmatter>
        </fr:tree>
        <fr:tree show-metadata="true" expanded="false" toc="false" numbered="false">
          <fr:frontmatter>
            <fr:authors />
            <fr:uri>https://xw.is/gcc/</fr:uri>
            <fr:display-uri>gcc</fr:display-uri>
            <fr:route>/gcc/</fr:route>
            <fr:title text="GNU Compiler Collection">GNU Compiler Collection</fr:title>
            <fr:taxon>Software</fr:taxon>
            <fr:meta name="external">https://gcc.gnu.org</fr:meta>
          </fr:frontmatter>
          <fr:mainmatter>
            <html:p><fr:link href="https://www.gnu.org/" type="external">GNU</fr:link> compiler suite.</html:p>
          </fr:mainmatter>
        </fr:tree>
        <fr:tree show-metadata="true" expanded="false" toc="false" numbered="false">
          <fr:frontmatter>
            <fr:authors>
              <fr:author>
                <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
              </fr:author>
            </fr:authors>
            <fr:date>
              <fr:year>2019</fr:year>
              <fr:month>11</fr:month>
              <fr:day>28</fr:day>
            </fr:date>
            <fr:date>
              <fr:year>2025</fr:year>
              <fr:month>8</fr:month>
              <fr:day>13</fr:day>
            </fr:date>
            <fr:uri>https://xw.is/debian-sysroot/</fr:uri>
            <fr:display-uri>debian-sysroot</fr:display-uri>
            <fr:route>/debian-sysroot/</fr:route>
            <fr:title text="Toolchains › Create Debian sysroots"><fr:link href="/toolchain-index/" title="Toolchains" uri="https://xw.is/toolchain-index/" display-uri="toolchain-index" type="local">Toolchains</fr:link> › Create Debian sysroots</fr:title>
          </fr:frontmatter>
          <fr:mainmatter>
            <html:p>We'll create Debian sysroots suitable for a GCC cross compiler.</html:p>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2019</fr:year>
                  <fr:month>11</fr:month>
                  <fr:day>28</fr:day>
                </fr:date>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>13</fr:day>
                </fr:date>
                <fr:title text="Debootstrap machine">Debootstrap machine</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <html:p>Everything below runs on the <html:em>debootstrap machine</html:em> as root:</html:p>
                <html:pre>sudo -i</html:pre>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Prerequisites">Prerequisites</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p>Debian or Ubuntu or any system that can run <fr:link href="/mmdebstrap.1" title="mmdebstrap(1)" uri="https://xw.is/mmdebstrap.1" display-uri="mmdebstrap.1" type="local">mmdebstrap(1)</fr:link> (modern alternative to <fr:link href="/debootstrap.8" title="debootstrap(8)" uri="https://xw.is/debootstrap.8" display-uri="debootstrap.8" type="local">debootstrap(8)</fr:link>), with a configured QEMU user emulator.</html:p>
                  </fr:mainmatter>
                </fr:tree>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Install debootstrap and associated tools">Install debootstrap and associated tools</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:pre>apt install -y binfmt-support qemu-user-static mmdebstrap</html:pre>
                  </fr:mainmatter>
                </fr:tree>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Run debootstrap">Run debootstrap</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p>We're going to create sysroots for <html:em>riscv64</html:em> and <html:em>s390x</html:em> (see the list of <fr:link href="https://www.debian.org/ports/" type="external">supported Debian ports</fr:link>.). Add more packages to <html:code>--include</html:code>, if needed.</html:p>
                    <html:pre>archs="riscv64 s390x"
for arch in ${archs}; do
    mmdebstrap --variant=buildd --include="zlib1g-dev libgmp-dev libmpfr-dev" --architectures=${arch} stable /tmp/sysroot-${arch}.tar.xz
done
</html:pre>
                    <html:p>Note that Debian images are <fr:link href="https://wiki.debian.org/Multiarch" type="external">multiarch</fr:link>, which means that libraries and header files <fr:link href="https://wiki.debian.org/Multiarch/LibraryPathOverview" type="external">are in a different location</fr:link> than the usual one. <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> must be made aware of this via <html:code>--enable-multiarch</html:code> when running the top-level <fr:link href="https://gcc.gnu.org/install/configure.html" type="external"><html:em>configure</html:em></fr:link> script. <html:mark>Failure to set <html:code>--enable-multiarch</html:code> will result in a failed build.</html:mark> Additionally, <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> for the <html:em>host</html:em> should be configured just as the the <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> on the <html:em>target</html:em> (in the sysroot). For example, some platforms do not make use of multilib (even though <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> might support it), so <html:mark>in those cases multilib should be <html:em>explicitly</html:em> disabled on the <html:em>build</html:em> machine with <html:code>--disable-multilib</html:code></html:mark>, because otherwise it might default to being enabled leading to build failure.</html:p>
                  </fr:mainmatter>
                </fr:tree>
              </fr:mainmatter>
            </fr:tree>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2019</fr:year>
                  <fr:month>11</fr:month>
                  <fr:day>28</fr:day>
                </fr:date>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>13</fr:day>
                </fr:date>
                <fr:title text="Host machine">Host machine</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <html:p>The following runs on the <html:em>host machine</html:em> (the machine where you'll compile and run your cross compiler). It can be the same machine as the <html:em>debootstrap machine</html:em>.</html:p>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Create directories">Create directories</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:pre>targets="riscv64-linux-gnu s390x-linux-gnu"
for target in ${targets}; do
    mkdir -p $HOME/cross/${target}/sys-root
done</html:pre>
                  </fr:mainmatter>
                </fr:tree>
                <html:p>Note that when we created the sysroots we used the Debian architecture names <html:em>(riscv64, s390x)</html:em>, but here we use the <fr:link href="/gcc/" title="GNU Compiler Collection" uri="https://xw.is/gcc/" display-uri="gcc" type="local">GCC</fr:link> <fr:link href="https://gcc.gnu.org/install/configure.html#Host_002c-Build-and-Target-specification" type="external">target triple</fr:link> <html:em>(riscv64-linux-gnu, s390x-linux-gnu)</html:em>.</html:p>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Copy sysroots">Copy sysroots</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p>Copy the sysroots from the <html:em>debootstrap machine</html:em> (here <html:code>debian@194.182.186.188</html:code>) to the <html:em>host machine</html:em>.</html:p>
                    <html:pre>targets="riscv64-linux-gnu s390x-linux-gnu"
for target in ${targets}; do
    arch=$(echo ${target} | cut -d '-' -f 1)
    ssh debian@194.182.186.230 "cat /tmp/sysroot-${arch}.tar.xz" | tar -C ${HOME}/cross/${target}/sys-root -xJf -
done</html:pre>
                  </fr:mainmatter>
                </fr:tree>
              </fr:mainmatter>
            </fr:tree>
            <fr:tree show-metadata="false">
              <fr:frontmatter>
                <fr:authors>
                  <fr:author>
                    <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                  </fr:author>
                </fr:authors>
                <fr:date>
                  <fr:year>2019</fr:year>
                  <fr:month>11</fr:month>
                  <fr:day>28</fr:day>
                </fr:date>
                <fr:date>
                  <fr:year>2025</fr:year>
                  <fr:month>8</fr:month>
                  <fr:day>13</fr:day>
                </fr:date>
                <fr:title text="Debootstrap machine (again)">Debootstrap machine (again)</fr:title>
              </fr:frontmatter>
              <fr:mainmatter>
                <fr:tree show-metadata="false">
                  <fr:frontmatter>
                    <fr:authors>
                      <fr:author>
                        <fr:link href="/aram/" title="Aram Hăvărneanu" uri="https://xw.is/aram/" display-uri="aram" type="local">Aram Hăvărneanu</fr:link>
                      </fr:author>
                    </fr:authors>
                    <fr:date>
                      <fr:year>2019</fr:year>
                      <fr:month>11</fr:month>
                      <fr:day>28</fr:day>
                    </fr:date>
                    <fr:date>
                      <fr:year>2025</fr:year>
                      <fr:month>8</fr:month>
                      <fr:day>13</fr:day>
                    </fr:date>
                    <fr:title text="Cleanup">Cleanup</fr:title>
                  </fr:frontmatter>
                  <fr:mainmatter>
                    <html:p>Still as <html:em>root</html:em>:</html:p>
                    <html:pre>rm -rf /tmp/sysroot-*.tar.xz</html:pre>
                  </fr:mainmatter>
                </fr:tree>
              </fr:mainmatter>
            </fr:tree>
          </fr:mainmatter>
        </fr:tree>
      </fr:mainmatter>
    </fr:tree>
    <fr:tree show-metadata="false" hidden-when-empty="true">
      <fr:frontmatter>
        <fr:authors />
        <fr:title text="Contributions">Contributions</fr:title>
      </fr:frontmatter>
      <fr:mainmatter />
    </fr:tree>
  </fr:backmatter>
</fr:tree>
