1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

Ports: Add mold port

The port exposes some dynamic loader and toolchain shortcomings,
namely RTLD_NEXT, RTLD_NOLOAD, and std::filesystem. Hopefully we can
discover a ton of multi-threading bugs in Serenity with this port :^)
This commit is contained in:
Andrew Kaster 2022-01-11 01:46:43 -07:00 committed by Linus Groh
parent b13846e688
commit 4ebcae9f4f
9 changed files with 292 additions and 0 deletions

View file

@ -112,6 +112,7 @@ Please make sure to keep this list up to date when adding and updating ports. :^
| [`mawk`](mawk/) | mawk | 1.3.4-20200120 | https://invisible-island.net/mawk/ | | [`mawk`](mawk/) | mawk | 1.3.4-20200120 | https://invisible-island.net/mawk/ |
| [`mbedtls`](mbedtls/) | Mbed TLS | 2.16.2 | https://tls.mbed.org/ | | [`mbedtls`](mbedtls/) | Mbed TLS | 2.16.2 | https://tls.mbed.org/ |
| [`milkytracker`](milkytracker/) | milkytracker | 1.03.00 | https://github.com/milkytracker/MilkyTracker | | [`milkytracker`](milkytracker/) | milkytracker | 1.03.00 | https://github.com/milkytracker/MilkyTracker |
| [`mold`](mold/) | | 1.0.1 | https://github.com/rui314/mold |
| [`mpc`](mpc/) | GNU Multiple Precision Complex Library (MPC) | 1.2.1 | http://www.multiprecision.org/mpc/ | | [`mpc`](mpc/) | GNU Multiple Precision Complex Library (MPC) | 1.2.1 | http://www.multiprecision.org/mpc/ |
| [`mpfr`](mpfr/) | GNU Multiple Precision Floating-Point Reliable Library (MPFR) | 4.1.0 | https://www.mpfr.org/ | | [`mpfr`](mpfr/) | GNU Multiple Precision Floating-Point Reliable Library (MPFR) | 4.1.0 | https://www.mpfr.org/ |
| [`mruby`](mruby/) | mruby | 3.0.0 | https://mruby.org/ | | [`mruby`](mruby/) | mruby | 3.0.0 | https://mruby.org/ |

8
Ports/mold/package.sh Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env -S bash ../.port_include.sh
port=mold
version=1.0.1
files="https://github.com/rui314/mold/archive/refs/tags/v${version}.tar.gz mold-${version}.tgz b0d54602d1229c26583ee8a0132e53463c4d755f9dbc456475f388fd8a1aa3e4"
auth_type=sha256
depends=("zlib" "openssl")
makeopts=("OS=SerenityOS" "EXTRA_LDFLAGS=-L${DESTDIR}/usr/local/lib -lcore" "-j$(nproc)")
installopts=("OS=SerenityOS")

View file

@ -0,0 +1,40 @@
From e1ceb7155ee680f1fdd83773e9702735e5a46bd6 Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:42:36 -0700
Subject: [PATCH 1/7] Disable mold_preload.so for Serenity
This feature depends on RTLD_NEXT capabilities which are not yet
implemented in the Serenity DynamicLoader.
---
Makefile | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
index 7f134b89..5b4f8576 100644
--- a/Makefile
+++ b/Makefile
@@ -112,7 +112,7 @@ ifneq ($(OS), Darwin)
LIBS += -lcrypto
endif
-all: mold mold-wrapper.so
+all: mold
mold: $(OBJS) $(MIMALLOC_LIB) $(TBB_LIB) $(XXHASH_LIB)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) $(OBJS) -o $@ $(LIBS)
@@ -165,9 +165,9 @@ install: all
install -m 755 mold $D$(BINDIR)
$(STRIP) $D$(BINDIR)/mold
- install -m 755 -d $D$(LIBDIR)/mold
- install -m 644 mold-wrapper.so $D$(LIBDIR)/mold
- $(STRIP) $D$(LIBDIR)/mold/mold-wrapper.so
+ # install -m 755 -d $D$(LIBDIR)/mold
+ # install -m 644 mold-wrapper.so $D$(LIBDIR)/mold
+ # $(STRIP) $D$(LIBDIR)/mold/mold-wrapper.so
install -m 755 -d $D$(LIBEXECDIR)/mold
ln -sf $(BINDIR)/mold $D$(LIBEXECDIR)/mold/ld
--
2.25.1

View file

@ -0,0 +1,27 @@
From 3912ec0acdd0e01bf32b4fe38004194e4f42fa93 Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:43:35 -0700
Subject: [PATCH 2/7] Disable mimalloc for serenity
mimalloc needs some help to compile on serenity. That's one yak too
far for right now.
---
Makefile | 2 ++
1 file changed, 2 insertions(+)
diff --git a/Makefile b/Makefile
index 5b4f8576..e946c6fd 100644
--- a/Makefile
+++ b/Makefile
@@ -59,6 +59,8 @@ endif
USE_MIMALLOC = 1
ifeq ($(OS), Darwin)
USE_MIMALLOC = 0
+else ifeq ($(OS), SerenityOS)
+ USE_MIMALLOC = 0
else ifneq (, $(findstring android, $(shell uname -r)))
USE_MIMALLOC = 0
else ifeq ($(ASAN), 1)
--
2.25.1

View file

@ -0,0 +1,89 @@
From b7141fc8a74705a0a772f5495a9a92ed7768a6af Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:02:44 -0700
Subject: [PATCH 3/7] Replace std::filesystem usage with Core::File
At least the Clang toolchain does not support std::filesystem, so hack
up the two files that use it to use Core::File abstractions instead.
---
elf/subprocess.cc | 11 +++++++++++
filepath.cc | 19 +++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/elf/subprocess.cc b/elf/subprocess.cc
index 3ed66c89..9e2fa9f6 100644
--- a/elf/subprocess.cc
+++ b/elf/subprocess.cc
@@ -1,6 +1,12 @@
#include "mold.h"
+#ifdef __serenity__
+#define AK_DONT_REPLACE_STD
+#include <LibCore/File.h>
+#else
#include <filesystem>
+#endif
+
#include <signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -251,7 +257,12 @@ void daemonize(Context<E> &ctx, std::function<void()> *wait_for_client,
}
static std::string get_self_path() {
+#ifdef __serenity__
+ auto str = Core::File::read_link("/proc/self/exe");
+ return { str.characters(), str.length() };
+#else
return std::filesystem::read_symlink("/proc/self/exe");
+#endif
}
static bool is_regular_file(const std::string &path) {
diff --git a/filepath.cc b/filepath.cc
index e12043bf..0e158da2 100644
--- a/filepath.cc
+++ b/filepath.cc
@@ -1,20 +1,39 @@
#include "mold.h"
+#ifdef __serenity__
+#define AK_DONT_REPLACE_STD
+#include <LibCore/File.h>
+#else
#include <filesystem>
+#endif
#include <sys/stat.h>
namespace mold {
std::string get_current_dir() {
+#ifdef __serenity__
+ auto str = Core::File::current_working_directory();
+ return { str.characters(), str.length() };
+#else
return std::filesystem::current_path();
+#endif
}
std::string get_realpath(std::string_view path) {
+#ifdef __serenity__
+ StringView sv{ path.data(), path.length() };
+ errno = 0;
+ auto str = Core::File::real_path_for(sv);
+ if (str.is_empty() || errno != 0)
+ return std::string(path);
+ return { str.characters(), str.length() };
+#else
std::error_code ec;
std::string ret = std::filesystem::canonical(path, ec);
if (ec)
return std::string(path);
return ret;
+#endif
}
bool path_is_dir(std::string_view path) {
--
2.25.1

View file

@ -0,0 +1,28 @@
From a55b1a4d2ca37c0c4ef2516d71a6555de118cc68 Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:02:59 -0700
Subject: [PATCH 4/7] Add POSIX headers for timeval and select
mold was relying on other libc implementations leaking these definitions
from other headers.
---
elf/subprocess.cc | 2 ++
1 file changed, 2 insertions(+)
diff --git a/elf/subprocess.cc b/elf/subprocess.cc
index 9e2fa9f6..410e568b 100644
--- a/elf/subprocess.cc
+++ b/elf/subprocess.cc
@@ -10,7 +10,9 @@
#include <signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <sys/types.h>
+#include <sys/select.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
--
2.25.1

View file

@ -0,0 +1,29 @@
From 232eff20c3e16160b841b786017ed7c509c3c65a Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:04:05 -0700
Subject: [PATCH 5/7] Tell TBB that SerenityOS does not support weak symbols
Something about the Clang toolchain configuration causes undefined weak
references to scalable_malloc to remain in the mold executable even
though there's no chance we'll be loading the tbbmalloc library at
runtime. So, just lie to TBB that we don't support weak symbols.
---
third-party/tbb/include/oneapi/tbb/detail/_config.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/third-party/tbb/include/oneapi/tbb/detail/_config.h b/third-party/tbb/include/oneapi/tbb/detail/_config.h
index 278beeb3..d2962374 100644
--- a/third-party/tbb/include/oneapi/tbb/detail/_config.h
+++ b/third-party/tbb/include/oneapi/tbb/detail/_config.h
@@ -193,7 +193,7 @@
/** __TBB_WEAK_SYMBOLS_PRESENT denotes that the system supports the weak symbol mechanism **/
#ifndef __TBB_WEAK_SYMBOLS_PRESENT
- #define __TBB_WEAK_SYMBOLS_PRESENT ( !_WIN32 && !__APPLE__ && !__sun && (__TBB_GCC_VERSION >= 40000 || __INTEL_COMPILER ) )
+ #define __TBB_WEAK_SYMBOLS_PRESENT ( !_WIN32 && !__APPLE__ && !__sun && !__serenity__ && (__TBB_GCC_VERSION >= 40000 || __INTEL_COMPILER ) )
#endif
/** Presence of compiler features **/
--
2.25.1

View file

@ -0,0 +1,42 @@
From 271f83f66d67ce88512cfd128241ccff1919972b Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:04:13 -0700
Subject: [PATCH 6/7] Tell TBB that SerenityOS libraries are named like BSD
ones
We won't be loading these libraries when building TBB as a static
library for mold, but the OS detection logic still needs updated.
---
third-party/tbb/src/tbb/allocator.cpp | 2 +-
third-party/tbb/src/tbb/rml_tbb.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/third-party/tbb/src/tbb/allocator.cpp b/third-party/tbb/src/tbb/allocator.cpp
index 6bf5a0be..c779361e 100644
--- a/third-party/tbb/src/tbb/allocator.cpp
+++ b/third-party/tbb/src/tbb/allocator.cpp
@@ -95,7 +95,7 @@ static const dynamic_link_descriptor MallocLinkTable[] = {
#define MALLOCLIB_NAME "tbbmalloc" DEBUG_SUFFIX ".dll"
#elif __APPLE__
#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".dylib"
-#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __ANDROID__
+#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __ANDROID__ || __serenity__
#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".so"
#elif __linux__ // Note that order of these #elif's is important!
#define MALLOCLIB_NAME "libtbbmalloc" DEBUG_SUFFIX ".so.2"
diff --git a/third-party/tbb/src/tbb/rml_tbb.cpp b/third-party/tbb/src/tbb/rml_tbb.cpp
index 122e2709..fc8d7fa8 100644
--- a/third-party/tbb/src/tbb/rml_tbb.cpp
+++ b/third-party/tbb/src/tbb/rml_tbb.cpp
@@ -52,7 +52,7 @@ namespace rml {
#define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".dylib"
#elif __linux__
#define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".so.1"
-#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX
+#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __serenity__
#define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".so"
#else
#error Unknown OS
--
2.25.1

View file

@ -0,0 +1,28 @@
From e48745461894370e3b47c14fe53417dd2bc7515f Mon Sep 17 00:00:00 2001
From: Andrew Kaster <akaster@serenityos.org>
Date: Tue, 11 Jan 2022 01:04:20 -0700
Subject: [PATCH 7/7] Stub out a definition of RTLD_NOLOAD
SerenityOs's DynamicLoader doesn't support this flag. However, we won't
be dynamically loading any tbb extensions for the static library build
mold uses, so we can just define it as a no-op as the code paths that
use it will never be used.
---
third-party/tbb/src/tbb/dynamic_link.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/third-party/tbb/src/tbb/dynamic_link.cpp b/third-party/tbb/src/tbb/dynamic_link.cpp
index f9b25ee1..4bacaae8 100644
--- a/third-party/tbb/src/tbb/dynamic_link.cpp
+++ b/third-party/tbb/src/tbb/dynamic_link.cpp
@@ -53,6 +53,7 @@
#pragma weak dlclose
#endif /* __TBB_WEAK_SYMBOLS_PRESENT && !__TBB_DYNAMIC_LOAD_ENABLED */
+#define RTLD_NOLOAD 0
#define __USE_STATIC_DL_INIT ( !__ANDROID__ )
--
2.25.1