From 0a85964f91b10dd3f83824fcac626c36354b4945 Mon Sep 17 00:00:00 2001 From: Sebastian Sumpf Date: Mon, 12 Jun 2023 21:50:58 +0200 Subject: [PATCH] libports: add 'compat-libc' Implement FBSD_1.0 versions of libc functions ('stat', 'fstat'). The functions are versioned with @FBSD_1.0, and therefore, will not clash with libc during linking. However, to be called by our dynamic linker, the library must be before libc in the NEEDED section of the binary using it. This requires the lib to be in front of libc in the LIBS variable. The library currently will call libc 'stat' and 'fstat' by looking up the symbols via 'dlsym'. Ref genodelabs/goa#61 --- repos/libports/lib/mk/compat-libc.mk | 15 +++++ repos/libports/lib/symbols/compat-libc | 2 + .../recipes/api/compat-libc/content.mk | 16 +++++ repos/libports/recipes/api/compat-libc/hash | 1 + repos/libports/recipes/src/compat-libc/api | 1 + .../recipes/src/compat-libc/content.mk | 10 +++ repos/libports/recipes/src/compat-libc/hash | 1 + .../recipes/src/compat-libc/used_apis | 2 + repos/libports/src/lib/compat-libc/README | 1 + repos/libports/src/lib/compat-libc/compat.cc | 63 +++++++++++++++++++ repos/libports/src/lib/compat-libc/libc.c | 43 +++++++++++++ repos/libports/src/lib/compat-libc/libc.h | 20 ++++++ repos/libports/src/lib/compat-libc/symbol.map | 8 +++ 13 files changed, 183 insertions(+) create mode 100644 repos/libports/lib/mk/compat-libc.mk create mode 100644 repos/libports/lib/symbols/compat-libc create mode 100644 repos/libports/recipes/api/compat-libc/content.mk create mode 100644 repos/libports/recipes/api/compat-libc/hash create mode 100644 repos/libports/recipes/src/compat-libc/api create mode 100644 repos/libports/recipes/src/compat-libc/content.mk create mode 100644 repos/libports/recipes/src/compat-libc/hash create mode 100644 repos/libports/recipes/src/compat-libc/used_apis create mode 100644 repos/libports/src/lib/compat-libc/README create mode 100644 repos/libports/src/lib/compat-libc/compat.cc create mode 100644 repos/libports/src/lib/compat-libc/libc.c create mode 100644 repos/libports/src/lib/compat-libc/libc.h create mode 100644 repos/libports/src/lib/compat-libc/symbol.map diff --git a/repos/libports/lib/mk/compat-libc.mk b/repos/libports/lib/mk/compat-libc.mk new file mode 100644 index 0000000000..f6e8ec19e3 --- /dev/null +++ b/repos/libports/lib/mk/compat-libc.mk @@ -0,0 +1,15 @@ +SHARED_LIB = yes +LIBS += libc +COMPAT_DIR = $(REP_DIR)/src/lib/compat-libc + +SRC_CC = compat.cc +SRC_C = libc.c + +vpath %.cc $(COMPAT_DIR) +vpath %.c $(COMPAT_DIR) + +LD_OPT += --version-script=$(COMPAT_DIR)/symbol.map + +CC_CXX_WARN_STRICT_CONVERSION = + +# vi: set ft=make : diff --git a/repos/libports/lib/symbols/compat-libc b/repos/libports/lib/symbols/compat-libc new file mode 100644 index 0000000000..b1bae03ee0 --- /dev/null +++ b/repos/libports/lib/symbols/compat-libc @@ -0,0 +1,2 @@ +fstat T +stat T diff --git a/repos/libports/recipes/api/compat-libc/content.mk b/repos/libports/recipes/api/compat-libc/content.mk new file mode 100644 index 0000000000..fd9b3b670f --- /dev/null +++ b/repos/libports/recipes/api/compat-libc/content.mk @@ -0,0 +1,16 @@ +MIRROR_FROM_REP_DIR := lib/symbols/compat-libc + +content: $(MIRROR_FROM_REP_DIR) LICENSE + +$(MIRROR_FROM_REP_DIR): + $(mirror_from_rep_dir) + +content: lib/symbol.map + +lib/symbol.map: + cp $(REP_DIR)/src/lib/compat-libc/symbol.map $@ + + +LICENSE: + cp $(GENODE_DIR)/LICENSE $@ + diff --git a/repos/libports/recipes/api/compat-libc/hash b/repos/libports/recipes/api/compat-libc/hash new file mode 100644 index 0000000000..aed0203b8b --- /dev/null +++ b/repos/libports/recipes/api/compat-libc/hash @@ -0,0 +1 @@ +2023-06-12 1ce2539b566b8d0d1a1f9919b8c7543f32467679 diff --git a/repos/libports/recipes/src/compat-libc/api b/repos/libports/recipes/src/compat-libc/api new file mode 100644 index 0000000000..901f5e03c1 --- /dev/null +++ b/repos/libports/recipes/src/compat-libc/api @@ -0,0 +1 @@ +compat-libc diff --git a/repos/libports/recipes/src/compat-libc/content.mk b/repos/libports/recipes/src/compat-libc/content.mk new file mode 100644 index 0000000000..dda0639372 --- /dev/null +++ b/repos/libports/recipes/src/compat-libc/content.mk @@ -0,0 +1,10 @@ +MIRROR_FROM_REP_DIR := lib/mk/compat-libc.mk src/lib/compat-libc + +content: $(MIRROR_FROM_REP_DIR) LICENSE + +$(MIRROR_FROM_REP_DIR): + $(mirror_from_rep_dir) + +LICENSE: + cp $(GENODE_DIR)/LICENSE $@ + diff --git a/repos/libports/recipes/src/compat-libc/hash b/repos/libports/recipes/src/compat-libc/hash new file mode 100644 index 0000000000..ed29abeacc --- /dev/null +++ b/repos/libports/recipes/src/compat-libc/hash @@ -0,0 +1 @@ +2023-06-12-a b499380d630694fbe520d666320b5bba333fce25 diff --git a/repos/libports/recipes/src/compat-libc/used_apis b/repos/libports/recipes/src/compat-libc/used_apis new file mode 100644 index 0000000000..4011ccc3b3 --- /dev/null +++ b/repos/libports/recipes/src/compat-libc/used_apis @@ -0,0 +1,2 @@ +libc +so diff --git a/repos/libports/src/lib/compat-libc/README b/repos/libports/src/lib/compat-libc/README new file mode 100644 index 0000000000..697b80c2dd --- /dev/null +++ b/repos/libports/src/lib/compat-libc/README @@ -0,0 +1 @@ +This library contains FBSD_1.0 symbol versions not enabled in our libc port. diff --git a/repos/libports/src/lib/compat-libc/compat.cc b/repos/libports/src/lib/compat-libc/compat.cc new file mode 100644 index 0000000000..4c52c14ecc --- /dev/null +++ b/repos/libports/src/lib/compat-libc/compat.cc @@ -0,0 +1,63 @@ +/* + * \brief FreeBSD-11 function versions + * \author Sebastian Sumpf + * \date 2023-06-12 + */ + +/* + * Copyright (C) 2023 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#include +#define _WANT_FREEBSD11_STAT /* enable freebsd11_stat */ +#define _KERNEL /* disable 'stat' */ +#include +#include "libc.h" + + +static void _to_freebsd11(struct stat *libc_buf, struct freebsd11_stat *buf) +{ + buf->st_dev = libc_buf->st_dev; + buf->st_ino = libc_buf->st_ino; + buf->st_mode = libc_buf->st_mode; + buf->st_nlink = libc_buf->st_nlink; + buf->st_uid = libc_buf->st_uid; + buf->st_gid = libc_buf->st_gid; + buf->st_atim = libc_buf->st_atim; + buf->st_mtim = libc_buf->st_mtim; + buf->st_ctim = libc_buf->st_ctim; + buf->st_size = libc_buf->st_size; + buf->st_blksize = libc_buf->st_blksize; + buf->st_flags = libc_buf->st_flags; + buf->st_gen = libc_buf->st_gen; + buf->st_birthtim = libc_buf->st_birthtim; +} + + +extern "C" int stat(const char *path, struct freebsd11_stat *buf) +{ + struct stat libc_buf { }; + + int err = libc_stat(path, &libc_buf); + if (err) return err; + + _to_freebsd11(&libc_buf, buf); + + return 0; +} + + +extern "C" int fstat(int fd, struct freebsd11_stat *buf) +{ + struct stat libc_buf { }; + + int err = libc_fstat(fd, &libc_buf); + if (err) return err; + + _to_freebsd11(&libc_buf, buf); + + return 0; +} diff --git a/repos/libports/src/lib/compat-libc/libc.c b/repos/libports/src/lib/compat-libc/libc.c new file mode 100644 index 0000000000..9c1e9256a3 --- /dev/null +++ b/repos/libports/src/lib/compat-libc/libc.c @@ -0,0 +1,43 @@ +/* + * \brief Interface real libc + * \author Sebastian Sumpf + * \date 2023-06-12 + */ + +/* + * Copyright (C) 2023 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#include +#include + +static void* libc_handle(void) +{ + static void *handle = NULL; + if (!handle) handle = dlopen("libc.lib.so", RTLD_LAZY); + return handle; +} + + +int (*_stat)(char const*, struct stat*); +int (*_fstat)(int, struct stat *); + +int libc_stat(const char *path, struct stat *buf) +{ + if (!_stat) + _stat = dlsym(libc_handle(), "stat"); + + return _stat(path, buf); +} + + +int libc_fstat(int fd, struct stat *buf) +{ + if (!_fstat) + _fstat = dlsym(libc_handle(), "fstat"); + + return _fstat(fd, buf); +} diff --git a/repos/libports/src/lib/compat-libc/libc.h b/repos/libports/src/lib/compat-libc/libc.h new file mode 100644 index 0000000000..ebc71e0a90 --- /dev/null +++ b/repos/libports/src/lib/compat-libc/libc.h @@ -0,0 +1,20 @@ +/* + * \brief Interface real libc + * \author Sebastian Sumpf + * \date 2023-06-12 + */ + +/* + * Copyright (C) 2023 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _COMPAT_LIBC_H_ +#define _COMPAT_LIBC_H_ + +extern "C" int libc_stat(const char *, struct stat*); +extern "C" int libc_fstat(int, struct stat *); + +#endif /* _COMPAT_LIBC_H_ */ diff --git a/repos/libports/src/lib/compat-libc/symbol.map b/repos/libports/src/lib/compat-libc/symbol.map new file mode 100644 index 0000000000..079771d5f8 --- /dev/null +++ b/repos/libports/src/lib/compat-libc/symbol.map @@ -0,0 +1,8 @@ +# compatible old version implemeted in this library +FBSD_1.0 { + global: + stat; + fstat; + local: + *; +};