[llvm] [bazel] Add linker flags to clang equivalent to those added in CMake for plugin support (PR #194897)

Tom Murray via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 08:59:34 PDT 2026


https://github.com/TomMurray created https://github.com/llvm/llvm-project/pull/194897

# Problem Description

Currently the bazel overlay lacks some linker options that are added in the CMake build to some tools & drivers for supporting plugins i.e. dynamic libraries loaded at runtime with the likes of `dlopen`. [Clang plugins](https://clang.llvm.org/docs/ClangPlugins.html) are an example which use this kind of dynamic library loading. Some platforms require specific flags when linking the executable to support this and if they aren't present this usual results in a runtime error (e.g. this change is motivated by trying to use clang plugins on Linux where the dynamic library fails to link symbols in the main executable during `dlopen`).
In the CMake project these linker flags are added through the `export_executable_symbols` CMake function in `llvm/cmake/modules/AddLLVM.cmake` and vary per-platform.

# Implementation
Capture the linker flags added by `export_executable_symbols` in the CMake setup in a variable using `select` per-platform based on the default configuration (`LLVM_ENABLE_PLUGINS=1`) and pass these as `linkopts` to the clang binary in the bazel overlay. 

# Testing
Tested I can now successfully load a clang plugin using `-fplugin` argument to the `@llvm-project//clang:clang` binary on both Mac/Linux.

>From d20b899012f77c506f6ce32588732f2af07d25ed Mon Sep 17 00:00:00 2001
From: Tom Murray <tom.paul.murray at gmail.com>
Date: Wed, 29 Apr 2026 16:19:50 +0100
Subject: [PATCH] Add export_executable_symbols equivalent linker flags to
 clang

---
 utils/bazel/llvm-project-overlay/clang/BUILD.bazel | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index 819b6b9dcfbfd..730d1fb7452a0 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -2334,6 +2334,14 @@ llvm_driver_cc_binary(
     needs_posix_utility_signal_handling = True,
     stamp = 0,
     deps = [":clang-driver"],
+    # Linker options added in CMake by `export_executable_symbols` function
+    # from `llvm/cmake/modules/AddLLVM.cmake` to some tools/drivers for
+    # plugin support.
+    linkopts = select({
+        "@platforms//os:linux": ["-Wl,--export-dynamic"],
+        "@platforms//os:macos": ["-rdynamic"],
+        "//conditions:default": [],
+    }),
 )
 
 cc_binary(



More information about the llvm-commits mailing list