Skip to content

Commit

Permalink
refactor: remove need for oldBintools
Browse files Browse the repository at this point in the history
This refactors the seL4-test and the seL4-camkes-vm-examples pkgs, so
that the old musllibc is patched to work even with modern bintools!
  • Loading branch information
wucke13 committed Oct 31, 2024
1 parent 8573d5d commit 4c48d0b
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 45 deletions.
26 changes: 26 additions & 0 deletions patches/seL4-compile-musl-on-recent-gcc-1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
From 29d041ae7c9d0afd312f77c11a737fab2a371d2c Mon Sep 17 00:00:00 2001
From: Alwin Joshy <[email protected]>
Date: Thu, 15 Aug 2024 16:32:59 +1000
Subject: [PATCH] hack: provide __getauxval()

Signed-off-by: Alwin Joshy <[email protected]>
---
src/misc/getauxval.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/src/misc/getauxval.c b/src/misc/getauxval.c
index b846c80fd..c1134492b 100644
--- a/src/misc/getauxval.c
+++ b/src/misc/getauxval.c
@@ -2,6 +2,11 @@
#include <errno.h>
#include "libc.h"

+unsigned long __getauxval(unsigned long type) {
+ errno = ENOENT;
+ return 0;
+}
+
unsigned long getauxval(unsigned long item)
{
size_t *auxv = libc.auxv;
185 changes: 185 additions & 0 deletions patches/seL4-compile-musl-on-recent-gcc-2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
From 41e4ef7776c1ab8e3f8d7d597f26f8fbef38e0da Mon Sep 17 00:00:00 2001
From: Alwin Joshy <[email protected]>
Date: Wed, 14 Aug 2024 18:17:35 +1000
Subject: [PATCH] hack: allow musllibc to compile with gcc >= 12

Signed-off-by: Alwin Joshy <[email protected]>
---
include/features.h | 11 +++++++++++
include/stdio.h | 18 ++++++++++++++++++
src/internal/stdio_impl.h | 4 ++++
src/stdio/stderr.c | 27 +++++++++++++++------------
src/stdio/stdin.c | 25 ++++++++++++++-----------
src/stdio/stdout.c | 28 ++++++++++++++++------------
6 files changed, 78 insertions(+), 35 deletions(-)

diff --git a/include/features.h b/include/features.h
index f4d651efc..ac5c2f25b 100644
--- a/include/features.h
+++ b/include/features.h
@@ -36,3 +36,14 @@
#endif

#endif
+
+#ifndef FEATURES_H
+#define FEATURES_H
+
+// #define weak __attribute__((__weak__))
+#define hidden __attribute__((__visibility__("hidden")))
+/*
+#define weak_alias(old, new) \
+ extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
+*/
+#endif
\ No newline at end of file
diff --git a/include/stdio.h b/include/stdio.h
index 884d2e6a7..a92fcc248 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -201,3 +201,21 @@ int fputs_unlocked(const char *, FILE *);
#endif

#endif
+
+
+#ifndef STDIO_H
+#define STDIO_H
+
+#undef stdin
+#undef stdout
+#undef stderr
+
+extern hidden FILE __stdin_FILE;
+extern hidden FILE __stdout_FILE;
+extern hidden FILE __stderr_FILE;
+
+#define stdin (&__stdin_FILE)
+#define stdout (&__stdout_FILE)
+#define stderr (&__stderr_FILE)
+
+#endif
diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h
index 7cdf729de..433a71f72 100644
--- a/src/internal/stdio_impl.h
+++ b/src/internal/stdio_impl.h
@@ -50,6 +50,10 @@ struct _IO_FILE {
struct __locale_struct *locale;
};

+extern hidden FILE *volatile __stdin_used;
+extern hidden FILE *volatile __stdout_used;
+extern hidden FILE *volatile __stderr_used;
+
size_t __stdio_read(FILE *, unsigned char *, size_t);
size_t __stdio_write(FILE *, const unsigned char *, size_t);
size_t __stdout_write(FILE *, const unsigned char *, size_t);
diff --git a/src/stdio/stderr.c b/src/stdio/stderr.c
index 229c8651d..e837cdfa0 100644
--- a/src/stdio/stderr.c
+++ b/src/stdio/stderr.c
@@ -1,16 +1,19 @@
#include "stdio_impl.h"
+#include "features.h"
+
+#undef stderr

static unsigned char buf[UNGET];
-static FILE f = {
- .buf = buf+UNGET,
- .buf_size = 0,
- .fd = 2,
- .flags = F_PERM | F_NORD,
- .lbf = -1,
- .write = __stdio_write,
- .seek = __stdio_seek,
- .close = __stdio_close,
- .lock = -1,
+hidden FILE __stderr_FILE = {
+ .buf = buf+UNGET,
+ .buf_size = 0,
+ .fd = 2,
+ .flags = F_PERM | F_NORD,
+ .lbf = -1,
+ .write = __stdio_write,
+ .seek = __stdio_seek,
+ .close = __stdio_close,
+ .lock = -1,
};
-FILE *const stderr = &f;
-FILE *volatile __stderr_used = &f;
+FILE *const stderr = &__stderr_FILE;
+FILE *volatile __stderr_used = &__stderr_FILE;
\ No newline at end of file
diff --git a/src/stdio/stdin.c b/src/stdio/stdin.c
index 171ff22a9..3655871c7 100644
--- a/src/stdio/stdin.c
+++ b/src/stdio/stdin.c
@@ -1,15 +1,18 @@
#include "stdio_impl.h"
+#include "features.h"
+
+#undef stdin

static unsigned char buf[BUFSIZ+UNGET];
-static FILE f = {
- .buf = buf+UNGET,
- .buf_size = sizeof buf-UNGET,
- .fd = 0,
- .flags = F_PERM | F_NOWR,
- .read = __stdio_read,
- .seek = __stdio_seek,
- .close = __stdio_close,
- .lock = -1,
+hidden FILE __stdin_FILE = {
+ .buf = buf+UNGET,
+ .buf_size = sizeof buf-UNGET,
+ .fd = 0,
+ .flags = F_PERM | F_NOWR,
+ .read = __stdio_read,
+ .seek = __stdio_seek,
+ .close = __stdio_close,
+ .lock = -1,
};
-FILE *const stdin = &f;
-FILE *volatile __stdin_used = &f;
+FILE *const stdin = &__stdin_FILE;
+FILE *volatile __stdin_used = &__stdin_FILE;
\ No newline at end of file
diff --git a/src/stdio/stdout.c b/src/stdio/stdout.c
index 6b188942d..85268d2e4 100644
--- a/src/stdio/stdout.c
+++ b/src/stdio/stdout.c
@@ -1,16 +1,20 @@
#include "stdio_impl.h"
+#include "features.h"
+
+#undef stdout

static unsigned char buf[BUFSIZ+UNGET];
-static FILE f = {
- .buf = buf+UNGET,
- .buf_size = sizeof buf-UNGET,
- .fd = 1,
- .flags = F_PERM | F_NORD,
- .lbf = '\n',
- .write = __stdout_write,
- .seek = __stdio_seek,
- .close = __stdio_close,
- .lock = -1,
+hidden FILE __stdout_FILE = {
+ .buf = buf+UNGET,
+ .buf_size = sizeof buf-UNGET,
+ .fd = 1,
+ .flags = F_PERM | F_NORD,
+ .lbf = '\n',
+ .write = __stdout_write,
+ .seek = __stdio_seek,
+ .close = __stdio_close,
+ .lock = -1,
};
-FILE *const stdout = &f;
-FILE *volatile __stdout_used = &f;
+
+FILE *const stdout = &__stdout_FILE;
+FILE *volatile __stdout_used = &__stdout_FILE;
52 changes: 17 additions & 35 deletions pkgs/seL4-camkes-vm-examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
{ lib
, stdenvNoLibs
, fetchGoogleRepoTool
, fetchurl
, writeShellScriptBin
, writeText
, capDL-tool
, buildPackages
, cmake
, cpio
, dtc
, libxml2
, nanopb
, ninja
, libxml2
, dtc
, cpio
, protobuf
, python3
, qemu
, ubootTools
, extraCmakeFlags ? [ ]
, stack
}:

let
capDL-makefile = writeText "Makefile" ''
.RECIPEPREFIX = >
all:
> ln --symbolic -- ${lib.getExe capDL-tool} ./
> ln --symbolic -- ${lib.getExe buildPackages.capDL-tool} ./
'';
in

Expand All @@ -46,12 +41,11 @@ stdenvNoLibs.mkDerivation rec {
libxml2 # xmllint
nanopb # ser/de
ninja # build tools
protobuf # to generate ser/de stuff
ubootTools # for mkimage
(python3.withPackages (ps: with ps; [ camkes-deps protobuf seL4-deps ]))
(buildPackages.python3.withPackages (ps: with ps; [ camkes-deps seL4-deps protobuf ]))

# fakegit
(writeShellScriptBin "git" ''
(buildPackages.writeShellScriptBin "git" ''
# Taken from https://git.musl-libc.org/cgit/musl/tree/tools/version.sh
if [[ $@ = "git describe --tags --match 'v[0-9]*'" ]]; then
echo "${version}"
Expand All @@ -61,32 +55,10 @@ stdenvNoLibs.mkDerivation rec {
fi
'')
];

depsBuildBuild = [
qemu # qemu-system-aarch64
];

# required because the musllibc fork of seL4 is so old it won't compile with gcc12
# see https://github.com/seL4/musllibc/issues/19#issuecomment-1841713558
# and https://www.mail-archive.com/[email protected]/msg04088.html
patches = [
# introduces base for new hidden macro
(fetchurl {
url = "https://git.musl-libc.org/cgit/musl/patch/src/include/features.h?id=13d1afa46f8098df290008c681816c9eb89ffbdb";
hash = "sha256-2W5iKJP9SdXy+SdcX5dEBLGSjJMrFK1OKHn2BZGJJpc=";
})

# introduces new way to access stdio stuff, relying on hidden symbol macro
(fetchurl {
url = "https://git.musl-libc.org/cgit/musl/patch/?id=d8f2efa708a027132d443f45a8c98a0c7c1b2d77";
hash = "sha256-kt30c6joUxU7BcTyq/TcY9+YgJ5Cw01cOTGn8hQpCOU=";
})

# make sure the new definition of hidden macro is seen by the stdio stuff
../patches/musslibc-add-features.h-to-stdio_impl.h.patch
];
patchFlags = [ "-p1" "--directory=projects/musllibc" ];

postPatch = ''
# fix /bin/bash et al.
patchShebangs .
Expand All @@ -95,6 +67,16 @@ stdenvNoLibs.mkDerivation rec {
# avoid from-scratch compilation of capDL-tool
cp -- ${capDL-makefile} projects/capdl/capDL-tool/Makefile
''

# required because the musllibc fork of seL4 is so old it won't compile with gcc12
# see https://github.com/seL4/musllibc/issues/19#issuecomment-1841713558
# and https://www.mail-archive.com/[email protected]/msg04088.html
+ ''
pushd projects/musllibc
patch -p1 < ${ ../patches/seL4-compile-musl-on-recent-gcc-1.patch }
patch -p1 < ${ ../patches/seL4-compile-musl-on-recent-gcc-2.patch }
popd
'';

hardeningDisable = [ "all" ];
Expand Down
20 changes: 10 additions & 10 deletions pkgs/seL4-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{ lib
, stdenvNoLibs
, fetchGoogleRepoTool
, writeShellScriptBin
, buildPackages
, cmake
, cpio
, dtc
Expand All @@ -25,8 +25,6 @@ stdenvNoLibs.mkDerivation rec {
};

nativeBuildInputs = [
# leak cross compiler into eventual devShell, if one uses this drv with inputsFrom
stdenvNoLibs.cc
cmake # build tools
cpio # cpio archive tool
dtc # device tree compiler
Expand All @@ -37,7 +35,7 @@ stdenvNoLibs.mkDerivation rec {
python3Packages.seL4-deps # python deps for seL4

# fakegit
(writeShellScriptBin "git" ''
(buildPackages.writeShellScriptBin "git" ''
# Taken from https://git.musl-libc.org/cgit/musl/tree/tools/version.sh
if [[ $@ = "git describe --tags --match 'v[0-9]*'" ]]; then
echo "${version}"
Expand All @@ -51,14 +49,16 @@ stdenvNoLibs.mkDerivation rec {
# fix /bin/bash et al.
postPatch = ''
patchShebangs .
''
# Patch old musllibc to work with new bintools
# TODO remove this once https://github.com/seL4/sel4test-manifest/issues/21 is fixed
+ ''
pushd projects/musllibc
patch -p1 < ${ ../patches/seL4-compile-musl-on-recent-gcc-1.patch }
patch -p1 < ${ ../patches/seL4-compile-musl-on-recent-gcc-2.patch }
popd
'';

env.NIX_CFLAGS_COMPILE = builtins.concatStringsSep " " ([ ]
# Hotfix for:
# aarch64-unknown-linux-musl-ld: apps/sel4test-driver/musllibc/build-temp/stage/lib/libc.a(__stdio_exit.o): copy relocation against non-copyable protected symbol `__stdin_used'
++ lib.lists.optional (!stdenvNoLibs.hostPlatform.isx86) "-fpic"
);

# prevent Nix from injecting any flags meant to harden the build
hardeningDisable = [ "all" ];

Expand Down

0 comments on commit 4c48d0b

Please sign in to comment.