minimal-bootstrap.mes: Support x86_64-linux

Some changes made here are specific to x86_64, while others are applied
only to support an upgraded tinycc version later on.

x86_64-related:
- We use ${arch} over "x86" in various places.
- gen-sources.sh changed to support x86_64; sources.nix regenerated.
- nyacc was downgraded. Newer nyacc introduces a known regression that
  causes incompatibility with mes, specifically on x86_64.
  This is honestly a mes bug, but it is hard to patch.
- The x86_64 longjmp/setjmp infrastructure from mes-libc is replaced with a
  custom implementation. The original implementation handles stack-related
  registers incorrectly, and it's hard to fix without resorting to asm.
- Assorted bugfixes for mes-libc; see mes/default.nix for explanation.

To upgrade tinycc:
- We download an implementation of ldexpl from an upstream development version.
This commit is contained in:
Aleksi Hannula
2025-12-13 11:27:13 +02:00
parent e5fb916e66
commit 839e2de5d8
6 changed files with 1366 additions and 31 deletions
@@ -4,6 +4,7 @@
callPackage,
kaem,
mescc-tools,
buildPlatform,
}:
# Maintenance note:
@@ -22,12 +23,41 @@ let
nyacc = callPackage ./nyacc.nix { inherit nyacc; };
intptr =
{
i686-linux = "int";
x86_64-linux = "long";
}
.${buildPlatform.system};
uintptr = "unsigned ${intptr}";
config_h = builtins.toFile "config.h" ''
#ifndef _MES_CONFIG_H
#undef SYSTEM_LIBC
#define MES_VERSION "${version}"
#ifndef __M2__
typedef ${uintptr} uintptr_t;
typedef ${uintptr} size_t;
typedef ${intptr} ssize_t;
typedef ${intptr} intptr_t;
typedef ${intptr} ptrdiff_t;
#define __MES_SIZE_T
#define __MES_SSIZE_T
#define __MES_INTPTR_T
#define __MES_UINTPTR_T
#define __MES_PTRDIFF_T
#endif
#endif
'';
sources = (import ./sources.nix).x86.linux.mescc;
arch =
{
i686-linux = "x86";
x86_64-linux = "x86_64";
}
.${buildPlatform.system};
sources = (import ./sources.nix).${arch}.linux.mescc;
inherit (sources)
libc_mini_SOURCES
@@ -38,6 +68,7 @@ let
# add symlink() to libc+tcc so we can use it in ln-boot
libc_tcc_SOURCES = sources.libc_tcc_SOURCES ++ [ "lib/linux/symlink.c" ];
setjmp_x86_64 = ./setjmp_x86_64.c;
meta = {
description = "Scheme interpreter and C compiler for bootstrapping";
@@ -46,6 +77,7 @@ let
teams = [ lib.teams.minimal-bootstrap ];
platforms = [
"i686-linux"
"x86_64-linux"
];
};
@@ -71,10 +103,91 @@ let
cp ${config_h} include/mes/config.h
# rax is used to indicate the syscall; we need to inform the assembler that rax should not be used to
# pass the exit code as it would be overwritten
exit_c=lib/linux/x86_64-mes-gcc/_exit.c
replace --file ''${exit_c} --output ''${exit_c} --match-on "(code)" --replace-with "(code) : \"rax\", \"rdi\""
# Replace broken implementation of setjmp & longjmp with asm.
cp ${setjmp_x86_64} lib/x86_64-mes-gcc/setjmp.c
# wrong number of arguments for linkat() syscall
link_c=lib/linux/link.c
replace --file ''${link_c} --output ''${link_c} --match-on "_sys_call4" --replace-with "_sys_call5"
replace --file ''${link_c} --output ''${link_c} --match-on "AT_FDCWD, (long) new_name" --replace-with "AT_FDCWD, (long) new_name, 0"
# wrong syscall number used for nanosleep.
amd64_syscall_h=include/linux/x86_64/syscall.h
replace --file ''${amd64_syscall_h} --output ''${amd64_syscall_h} --match-on "SYS_nanosleep 0x33" --replace-with "SYS_nanosleep 0x23"
# strpbrk should return NULL when there is no match.
# The order of these `replace` commands is significant.
strpbrk_c=lib/string/strpbrk.c
replace --file ''${strpbrk_c} --output ''${strpbrk_c} --match-on "return p;" --replace-with "return 0;"
replace --file ''${strpbrk_c} --output ''${strpbrk_c} --match-on "break;" --replace-with "return p;"
# Wrong type used; fix.
# ntoab.c: __measbi_uldiv should use unsigned long as per "ul", not size_t
# ioctl3.c: unsigned long used to align with ioctl.c
# lib.h: change ioctl3 signature, per above
ntoab=lib/mes/ntoab.c
replace --file ''${ntoab} --output ''${ntoab} --match-on "size_t" --replace-with "unsigned long"
ioctl=lib/linux/ioctl3.c
replace --file ''${ioctl} --output ''${ioctl} --match-on "size_t" --replace-with "unsigned long"
lib_h=include/mes/lib.h
replace --file ''${lib_h} --output ''${lib_h} --match-on "size_t command" --replace-with "unsigned long command"
# vfprintf assumes %d arguments can be accessed as `long`. This
# is true on sign-extending platforms like RV64, but not on i686 or x86_64.
# Let's use the caller-specified width for better portability. Also account for
# potential need to zero-extend.
vfprintf_c=lib/stdio/vfprintf.c
replace --file ''${vfprintf_c} --output ''${vfprintf_c} --match-on "int count = 0;" --replace-with "int count = 0; int has_l = 0;"
replace --file ''${vfprintf_c} --output ''${vfprintf_c} --match-on "long d = va_arg (ap, long);" --replace-with "
long d;
if (has_l) {
has_l = 0;
d = va_arg (ap, long);
} else if (c != 'd' && c != 'i') {
d = (long) (va_arg (ap, unsigned int));
} else {
d = (long) (va_arg (ap, int));
}
"
replace --file ''${vfprintf_c} --output ''${vfprintf_c} --match-on "if (c == 'l')" --replace-with "
if (c == 'l') {
/* this is annoying to patch... */
has_l = 1;
c = *++p;
} else if (0)"
# Also, get rid of va_arg8
replace --file ''${vfprintf_c} --output ''${vfprintf_c} --match-on "va_arg8" --replace-with "va_arg"
# Same thing for vsnprintf.
vsnprintf_c=lib/stdio/vsnprintf.c
replace --file ''${vsnprintf_c} --output ''${vsnprintf_c} --match-on "int count = 0;" --replace-with "int count = 0; int has_l = 0;"
replace --file ''${vsnprintf_c} --output ''${vsnprintf_c} --match-on "long d = va_arg (ap, long);" --replace-with "
long d;
if (has_l) {
has_l = 0;
d = va_arg (ap, long);
} else if (c != 'd' && c != 'i') {
d = (long) (va_arg (ap, unsigned int));
} else {
d = (long) (va_arg (ap, int));
}
"
replace --file ''${vsnprintf_c} --output ''${vsnprintf_c} --match-on "if (c == 'l')" --replace-with "
if (c == 'l') {
/* this is annoying to patch... */
has_l = 1;
c = *++p;
} else if (0)"
replace --file ''${vsnprintf_c} --output ''${vsnprintf_c} --match-on "va_arg8" --replace-with "va_arg"
mkdir include/arch
cp include/linux/x86/kernel-stat.h include/arch/kernel-stat.h
cp include/linux/x86/signal.h include/arch/signal.h
cp include/linux/x86/syscall.h include/arch/syscall.h
cp include/linux/${arch}/kernel-stat.h include/arch/kernel-stat.h
cp include/linux/${arch}/signal.h include/arch/signal.h
cp include/linux/${arch}/syscall.h include/arch/syscall.h
# Remove pregenerated files
rm mes/module/mes/psyntax.pp mes/module/mes/psyntax.pp.header
@@ -113,13 +226,13 @@ let
replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"libdir\")" --replace-with "\"''${MES_PREFIX}/lib\""
replace --file ''${mescc_in} --output ''${mescc_in} --match-on @prefix@ --replace-with ''${MES_PREFIX}
replace --file ''${mescc_in} --output ''${mescc_in} --match-on @VERSION@ --replace-with ${version}
replace --file ''${mescc_in} --output ''${mescc_in} --match-on @mes_cpu@ --replace-with x86
replace --file ''${mescc_in} --output ''${mescc_in} --match-on @mes_cpu@ --replace-with ${arch}
replace --file ''${mescc_in} --output ''${mescc_in} --match-on @mes_kernel@ --replace-with linux
mkdir -p ''${bin}/bin
cp ''${mescc_in} ''${bin}/bin/mescc.scm
# Build mes-m2
kaem --verbose --strict --file kaem.x86
kaem --verbose --strict --file kaem.${arch}
cp bin/mes-m2 ''${bin}/bin/mes-m2
chmod 555 ''${bin}/bin/mes-m2
'';
@@ -136,7 +249,7 @@ let
"-I"
"${srcPrefix}/include"
"-I"
"${srcPrefix}/include/linux/x86"
"${srcPrefix}/include/linux/${arch}"
];
CC = toString ([ cc ] ++ ccArgs);
@@ -151,7 +264,7 @@ let
${CC} -c ${srcPrefix}/${source}
'';
crt1 = compile "/lib/linux/x86-mes-mescc/crt1.c";
crt1 = compile "/lib/linux/${arch}-mes-mescc/crt1.c";
getRes = suffix: res: "${res}/${res.name}${suffix}";
@@ -200,27 +313,27 @@ let
LIBDIR=''${out}/lib
mkdir -p ''${out} ''${LIBDIR}
mkdir -p ''${LIBDIR}/x86-mes
mkdir -p ''${LIBDIR}/${arch}-mes
# crt1.o
cp ${crt1}/crt1.o ''${LIBDIR}/x86-mes
cp ${crt1}/crt1.s ''${LIBDIR}/x86-mes
cp ${crt1}/crt1.o ''${LIBDIR}/${arch}-mes
cp ${crt1}/crt1.s ''${LIBDIR}/${arch}-mes
# libc-mini.a
cp ${libc-mini}/lib/libc-mini.a ''${LIBDIR}/x86-mes
cp ${libc-mini}/lib/libc-mini.s ''${LIBDIR}/x86-mes
cp ${libc-mini}/lib/libc-mini.a ''${LIBDIR}/${arch}-mes
cp ${libc-mini}/lib/libc-mini.s ''${LIBDIR}/${arch}-mes
# libmescc.a
cp ${libmescc}/lib/libmescc.a ''${LIBDIR}/x86-mes
cp ${libmescc}/lib/libmescc.s ''${LIBDIR}/x86-mes
cp ${libmescc}/lib/libmescc.a ''${LIBDIR}/${arch}-mes
cp ${libmescc}/lib/libmescc.s ''${LIBDIR}/${arch}-mes
# libc.a
cp ${libc}/lib/libc.a ''${LIBDIR}/x86-mes
cp ${libc}/lib/libc.s ''${LIBDIR}/x86-mes
cp ${libc}/lib/libc.a ''${LIBDIR}/${arch}-mes
cp ${libc}/lib/libc.s ''${LIBDIR}/${arch}-mes
# libc+tcc.a
cp ${libc_tcc}/lib/libc+tcc.a ''${LIBDIR}/x86-mes
cp ${libc_tcc}/lib/libc+tcc.s ''${LIBDIR}/x86-mes
cp ${libc_tcc}/lib/libc+tcc.a ''${LIBDIR}/${arch}-mes
cp ${libc_tcc}/lib/libc+tcc.s ''${LIBDIR}/${arch}-mes
'';
# Build mes itself
@@ -248,7 +361,7 @@ let
-lmescc \
-nostdlib \
-o ''${out}/bin/mes \
${libs}/lib/x86-mes/crt1.o \
${libs}/lib/${arch}-mes/crt1.o \
${lib.concatMapStringsSep " " (getRes ".o") (map compile mes_SOURCES)}
'';
in
@@ -13,7 +13,7 @@
set -eu
# Supported platforms
ARCHS="x86"
ARCHS="x86 x86_64"
KERNELS="linux"
COMPILERS="mescc gcc"
@@ -4,21 +4,35 @@
ln-boot,
mes,
mes-libc,
buildPlatform,
fetchurl,
}:
let
pname = "mes-libc";
inherit (mes.compiler) version;
sources = (import ./sources.nix).x86.linux.gcc;
arch =
{
i686-linux = "x86";
x86_64-linux = "x86_64";
}
.${buildPlatform.system};
sources = (import ./sources.nix).${arch}.linux.gcc;
inherit (sources) libtcc1_SOURCES libc_gnu_SOURCES;
ldexpl = fetchurl {
url = "https://gitlab.com/janneke/mes/-/raw/c837abed8edb341d4e56913729fbe9803b4de47c/lib/math/ldexpl.c";
hash = "sha256-3QoFZZIqVmlMUosEqOdYIMEHzYgQ7GJ7Hz0Bf/1iIig=";
};
# Concatenate all source files into a convenient bundle
# "gcc" variants of source files (eg. "lib/linux/x86-mes-gcc") can also be
# compiled by tinycc
#
# Passing this many arguments is too much for kaem so we need to split
# the operation in two
firstLibc = lib.take 100 libc_gnu_SOURCES;
firstLibc = (lib.take 100 libc_gnu_SOURCES) ++ [ ldexpl ];
lastLibc = lib.drop 100 libc_gnu_SOURCES;
in
kaem.runCommand "${pname}-${version}"
@@ -27,30 +41,33 @@ kaem.runCommand "${pname}-${version}"
nativeBuildInputs = [ ln-boot ];
passthru.CFLAGS = "-DHAVE_CONFIG_H=1 -I${mes-libc}/include -I${mes-libc}/include/linux/x86";
passthru.CFLAGS = "-std=c11";
meta = {
description = "Mes C Library";
homepage = "https://www.gnu.org/software/mes";
license = lib.licenses.gpl3Plus;
teams = [ lib.teams.minimal-bootstrap ];
platforms = [ "i686-linux" ];
platforms = [
"i686-linux"
"x86_64-linux"
];
};
}
''
cd ${mes.srcPrefix}
# mescc compiled libc.a
mkdir -p ''${out}/lib/x86-mes
mkdir -p ''${out}/lib/${arch}-mes
# libc.c
catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc}
catm ''${out}/lib/libc.c ''${TMPDIR}/first.c ${lib.concatStringsSep " " lastLibc}
# crt{1,n,i}.c
cp lib/linux/x86-mes-gcc/crt1.c ''${out}/lib
cp lib/linux/x86-mes-gcc/crtn.c ''${out}/lib
cp lib/linux/x86-mes-gcc/crti.c ''${out}/lib
cp lib/linux/${arch}-mes-gcc/crt1.c ''${out}/lib
cp lib/linux/${arch}-mes-gcc/crtn.c ''${out}/lib
cp lib/linux/${arch}-mes-gcc/crti.c ''${out}/lib
# libtcc1.c
catm ''${out}/lib/libtcc1.c ${lib.concatStringsSep " " libtcc1_SOURCES}
@@ -9,11 +9,16 @@ let
# NYACC is a tightly coupled dependency of mes. This version is known to work
# with mes 0.27.1.
# https://git.savannah.gnu.org/cgit/mes.git/tree/INSTALL?h=v0.27.1#n31
version = "2.02.2";
#
# Note: mes has issues with NYACC 1.09.2 and up. The mes interpreter cannot
# parse block comments outside top level. This seems to be fixed in development
# versions of mes.
# https://gitlab.com/janneke/commencement.scm/-/blob/fdc718f050171f12da58524f60c4d52c03e83df3/gcc-bootstrap.scm#L621
version = "1.09.1";
src = fetchurl {
url = "mirror://savannah/nyacc/nyacc-${version}.tar.gz";
hash = "sha256-aRpTcKU6c6Lc+onZL4EbPlZIOmf5IZnNAGGUTQRLAgU=";
hash = "sha256-DsmuU34NlReBpQ3jx5KayXqFwdS16F5dUVQuN1ECJxc=";
};
in
kaem.runCommand "${pname}-${version}"
@@ -0,0 +1,48 @@
/*
* setjmp() & longjmp() implementation for x86_64.
* Replaces a buggy C implementation.
*/
#include <setjmp.h>
#include <stdlib.h>
asm (".global __longjmp\n\t"
".global _longjmp\n\t"
".global longjmp\n\t"
".type __longjmp, %function\n\t"
".type _longjmp, %function\n\t"
".type longjmp, %function\n\t"
"__longjmp:\n\t"
"_longjmp:\n\t"
"longjmp:\n\t"
/* ensure return value is non-zero */
"mov %rsi, %rax\n\t"
"test %rax, %rax\n\t"
"sete %bl\n\t"
"movsbq %bl, %rbx\n\t"
"add %rbx, %rax\n\t"
"movq 0x00(%rdi), %rbp\n\t" /* rbp = env->__bp */
"movq 0x10(%rdi), %rsp\n\t" /* rsp = env->__sp */
"movq 0x08(%rdi), %rbx\n\t" /* rbx = env->__pc */
"jmp *%rbx\n\t"
);
asm (".global __setjmp\n\t"
".global _setjmp \n\t"
".global setjmp\n\t"
".type __setjmp, %function\n\t"
".type _setjmp, %function\n\t"
".type setjmp, %function\n\t"
"__setjmp:\n\t"
"_setjmp:\n\t"
"setjmp:\n\t"
"movq %rbp, 0x00(%rdi)\n\t" /* env->__bp = base pointer from caller */
"movq (%rsp), %rax\n\t" /* rax = return address to caller */
"movq %rax, 0x08(%rdi)\n\t" /* env->__pc = retaddr */
"movq %rsp, %rax\n\t" /* rax = stack pointer */
"add $8, %rax\n\t" /* offset sp to skip return addr */
"movq %rax, 0x10(%rdi)\n\t" /* env->__sp = sp before call */
"movq $0, %rax\n\t"
"ret\n\t");
File diff suppressed because it is too large Load Diff