From 18dfeca8ccc5537e3bc0ee62b16005121fa466f2 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Mon, 9 Feb 2026 23:10:37 +0100 Subject: [PATCH 1/5] Enable use of Phobos functions in plugins, by _statically_ linking the standard library into the plugin. --- tests/plugins/plugin_phobos_usage.d | 30 +++++++++++++++++++++++++++++ tools/ldc-build-plugin.d.in | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/plugins/plugin_phobos_usage.d diff --git a/tests/plugins/plugin_phobos_usage.d b/tests/plugins/plugin_phobos_usage.d new file mode 100644 index 00000000000..c426a9fe2eb --- /dev/null +++ b/tests/plugins/plugin_phobos_usage.d @@ -0,0 +1,30 @@ +// Test usage of Phobos (used to result in dlopen missing symbols, because LDC itself did not have that Phobos symbol) + +// REQUIRES: Plugins + +// RUN: split-file %s %t --leading-lines +// RUN: %buildplugin %t/plugin.d -of=%t/plugin%so --buildDir=%t/build +// RUN: %ldc -wi -c -o- --plugin=%t/plugin%so %t/testcase.d 2>&1 | FileCheck %t/testcase.d + +//--- plugin.d +import dmd.dmodule : Module; +import dmd.errors; +import dmd.location; + +import std.experimental.allocator.mallocator; + +extern(C) void runSemanticAnalysis(Module m) { + auto buffer = Mallocator.instance.allocate(1024 * 1024 * 4); + scope(exit) Mallocator.instance.deallocate(buffer); + + if (m.md) { + warning(m.md.loc, "It works!"); + } +} + +//--- testcase.d +// CHECK: testcase.d([[@LINE+1]]): Warning: It works! +module testcase; +int testfunction(int i) { + return i * 2; +} diff --git a/tools/ldc-build-plugin.d.in b/tools/ldc-build-plugin.d.in index 84fd25ff357..eb71d2a3db0 100644 --- a/tools/ldc-build-plugin.d.in +++ b/tools/ldc-build-plugin.d.in @@ -140,7 +140,7 @@ void build() { "--d-version=IN_LLVM", "-J" ~ buildPath(config.ldcSourceDir, "dmd", "res"), "--shared", - "--defaultlib=", + "--link-defaultlib-shared=false", "--od=" ~ config.buildDir ]; From 7ff201299af8cd15ebfae5107db26f5c9cba8526 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Mon, 9 Feb 2026 23:37:42 +0100 Subject: [PATCH 2/5] fixup, keep shared std libs on non-darwin --- tools/ldc-build-plugin.d.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ldc-build-plugin.d.in b/tools/ldc-build-plugin.d.in index eb71d2a3db0..09316158ae7 100644 --- a/tools/ldc-build-plugin.d.in +++ b/tools/ldc-build-plugin.d.in @@ -140,11 +140,11 @@ void build() { "--d-version=IN_LLVM", "-J" ~ buildPath(config.ldcSourceDir, "dmd", "res"), "--shared", - "--link-defaultlib-shared=false", "--od=" ~ config.buildDir ]; version (Darwin) { + args ~= "--link-defaultlib-shared=false"; args ~= "-L-Wl,-undefined,dynamic_lookup"; } From 78e7bb5d8a49576bce4703c7189dd47146ca9b81 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Tue, 10 Feb 2026 22:32:35 +0100 Subject: [PATCH 3/5] test --- tools/ldc-build-plugin.d.in | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/ldc-build-plugin.d.in b/tools/ldc-build-plugin.d.in index 09316158ae7..d7d0245bcbe 100644 --- a/tools/ldc-build-plugin.d.in +++ b/tools/ldc-build-plugin.d.in @@ -144,7 +144,6 @@ void build() { ]; version (Darwin) { - args ~= "--link-defaultlib-shared=false"; args ~= "-L-Wl,-undefined,dynamic_lookup"; } From 357c0a60e9bd4b950f4ed712cbdbe7ae4a5d82d5 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Wed, 11 Feb 2026 23:36:24 +0100 Subject: [PATCH 4/5] fixup --- tools/ldc-build-plugin.d.in | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/ldc-build-plugin.d.in b/tools/ldc-build-plugin.d.in index d7d0245bcbe..a249f5162a7 100644 --- a/tools/ldc-build-plugin.d.in +++ b/tools/ldc-build-plugin.d.in @@ -16,6 +16,9 @@ else version (TVOS) else version (WatchOS) version = Darwin; +version (linux) version = LDCLinkedWithSharedRuntime; +version (FreeBSD) version = LDCLinkedWithSharedRuntime; + struct Config { string ldcExecutable; string buildDir; @@ -147,6 +150,11 @@ void build() { args ~= "-L-Wl,-undefined,dynamic_lookup"; } + // When LDC is linked with shared runtime/phobos, don't link them into the plugin to avoid two runtimes being loaded. + version (LDCLinkedWithSharedRuntime) { + args ~= "--defaultlib="; + } + args ~= config.ldcArgs; exec(args); From 6cead58fdb99f7950563a2c824530c0811a099be Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Sun, 1 Mar 2026 11:10:27 +0100 Subject: [PATCH 5/5] Link with phobos but not druntime. --- tools/ldc-build-plugin.d.in | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/ldc-build-plugin.d.in b/tools/ldc-build-plugin.d.in index a249f5162a7..d580f80c92e 100644 --- a/tools/ldc-build-plugin.d.in +++ b/tools/ldc-build-plugin.d.in @@ -150,9 +150,10 @@ void build() { args ~= "-L-Wl,-undefined,dynamic_lookup"; } - // When LDC is linked with shared runtime/phobos, don't link them into the plugin to avoid two runtimes being loaded. + // When LDC is linked with shared runtime, don't link it again into the plugin to avoid two runtimes being loaded. + // Only link with phobos. version (LDCLinkedWithSharedRuntime) { - args ~= "--defaultlib="; + args ~= "--defaultlib=phobos2-ldc"; } args ~= config.ldcArgs;