Hi!

On Thu, 2026-01-15 at 04:09:41 +0100, Guillem Jover wrote:
> On Thu, 2026-01-15 at 03:54:32 +0100, Guillem Jover wrote:
> > Checking now criu, I think my position is now that given that there's
> > no standardized variable to use for direct ld(1) calls (I mean we
> > could just declare one as such, but…), and such projects might be mixing
> > them with other linker calls done via the compiler, so passing these
> > might be tricky or imply modifying upstream code anyway, there's
> > currently no point in providing pre-cooked flags for such use, so I'll
> > go for now with changing the documentation to ban their use, and
> > explain what can be done on the upstream codebase instead.
> 
> Actually let me re-backtrack :D, after noticing now the optee-os, and
> how it is a pure direct ld(1) caller user, I guess providing this
> variable makes more sense, instead of having to off-load that to each
> potential package, and at worse, they will just need to be patched
> upstream in case of mixed usage anyway. So, I guess I'll keep the
> current dpkg change to introduce a new variable for direct calls.

Having slept over it, I think upstream use of LDFLAGS for direct ld
calls is just unsafe, as the general expectation is that this is to
be used via the compiler, so IMO any such instance in upstream should
be fixed anyway, at which point those upstreams will accept getting
LDFLAGS (and filtering that themselves) or a direct ld(1) variable
which we can pre-populate.

I'm thinking to hold off a bit on introducing the new variable, and
I've asked autotools upstream for an agreed name for it, so that we
can at least try to unify its use. (Mails should be appearing on
the autoconf/automake/dpkg lists RSN.)

For optee-os, I've prepared the attached patch, but I'm not tested it
and I'm not sure the placement or the chosen variable name would be
acceptable to upstream. Also, perhaps waiting for any conclusion from
the autotools discussion might be of interest as well?

Thanks,
Guillem
From c854c1afa09354452a882912769817b342238c8d Mon Sep 17 00:00:00 2001
From: Guillem Jover <[email protected]>
Date: Thu, 15 Jan 2026 12:44:05 +0100
Subject: [PATCH] Filter LDFLAGS for direct ld calls

We should not be using flags intended for linking via the compiler
on direct ld calls, as these take a different form. Accept LDFLAGS
from the environment, but filter it so that we only allow a set of
known options (-l and -L), and then remap the options intended for
the linker passed as -Wl.
---
 core/arch/arm/kernel/link.mk     |  2 +-
 core/arch/riscv/kernel/link.mk   |  2 +-
 lib/libmbedtls/mbedtls/README.md |  2 +-
 mk/lib.mk                        | 11 +++++++++++
 ta/link_shlib.mk                 |  2 +-
 5 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/core/arch/arm/kernel/link.mk b/core/arch/arm/kernel/link.mk
index 2051977..27ca772 100644
--- a/core/arch/arm/kernel/link.mk
+++ b/core/arch/arm/kernel/link.mk
@@ -14,7 +14,7 @@ ifeq ($(CFG_ARM32_core),y)
 link-ldflags-common += $(call ld-option,--no-warn-execstack)
 endif
 
-link-ldflags  = $(LDFLAGS)
+link-ldflags  = $(LD_LDFLAGS)
 ifeq ($(call cfg-one-enabled, CFG_CORE_ASLR CFG_CORE_PHYS_RELOCATABLE),y)
 link-ldflags += -pie -Bsymbolic -z norelro $(ldflag-apply-dynamic-relocs)
 ifeq ($(CFG_ARM64_core),y)
diff --git a/core/arch/riscv/kernel/link.mk b/core/arch/riscv/kernel/link.mk
index bdfaf9e..8a14441 100644
--- a/core/arch/riscv/kernel/link.mk
+++ b/core/arch/riscv/kernel/link.mk
@@ -8,7 +8,7 @@ link-script-dep = $(link-out-dir)/.kern.ld.d
 
 link-ldflags-common += $(call ld-option,--no-warn-rwx-segments)
 
-link-ldflags  = $(LDFLAGS)
+link-ldflags  = $(LD_LDFLAGS)
 ifeq ($(CFG_CORE_ASLR),y)
 link-ldflags += -pie -Bsymbolic -z norelro $(ldflag-apply-dynamic-relocs)
 endif
diff --git a/lib/libmbedtls/mbedtls/README.md b/lib/libmbedtls/mbedtls/README.md
index b70c67e..74d0817 100644
--- a/lib/libmbedtls/mbedtls/README.md
+++ b/lib/libmbedtls/mbedtls/README.md
@@ -106,7 +106,7 @@ You'll still be able to run a much smaller set of tests with:
 
 In order to build for a Windows platform, you should use `WINDOWS_BUILD=1` if the target is Windows but the build environment is Unix-like (for instance when cross-compiling, or compiling from an MSYS shell), and `WINDOWS=1` if the build environment is a Windows shell (for instance using mingw32-make) (in that case some targets will not be available).
 
-Setting the variable `SHARED` in your environment will build shared libraries in addition to the static libraries. Setting `DEBUG` gives you a debug build. You can override `CFLAGS` and `LDFLAGS` by setting them in your environment or on the make command line; compiler warning options may be overridden separately using `WARNING_CFLAGS`. Some directory-specific options (for example, `-I` directives) are still preserved.
+Setting the variable `SHARED` in your environment will build shared libraries in addition to the static libraries. Setting `DEBUG` gives you a debug build. You can override `CFLAGS` and `LD_LDFLAGS` by setting them in your environment or on the make command line; compiler warning options may be overridden separately using `WARNING_CFLAGS`. Some directory-specific options (for example, `-I` directives) are still preserved.
 
 Please note that setting `CFLAGS` overrides its default value of `-O2` and setting `WARNING_CFLAGS` overrides its default value (starting with `-Wall -Wextra`), so if you just want to add some warning options to the default ones, you can do so by setting `CFLAGS=-O2 -Werror` for example. Setting `WARNING_CFLAGS` is useful when you want to get rid of its default content (for example because your compiler doesn't accept `-Wall` as an option). Directory-specific options cannot be overridden from the command line.
 
diff --git a/mk/lib.mk b/mk/lib.mk
index 315cae2..469e749 100644
--- a/mk/lib.mk
+++ b/mk/lib.mk
@@ -26,6 +26,17 @@ endif
 endif
 include mk/compile.mk
 
+nullstring»‐‐‐‐:=
+comma»‐»‐‐‐‐‐‐‐:= ,
+space»‐»‐‐‐‐‐‐‐:= $(nullstring) # EOL
+ldflags-remap»‐:= -Wl,%
+ldflags-allow»‐:= -l% -L%
+ldflags-Wl»‐‐‐‐:= $(filter $(ldflags-remap),$(LDFLAGS))
+LD_LDFLAGS»‐‐‐‐?= \
+	$(filter $(ldflags-allow),$(LDFLAGS)) \
+	$(subst $(comma),$(space),$(subst -Wl,,$(ldflags-Wl))) \
+	# EOL
+
 lib-libfile	:= $(out-dir)/$(base-prefix)$(libdir)/lib$(libname).a
 ifeq ($(CFG_ULIBS_SHARED),y)
 lib-shlibfile	:= $(out-dir)/$(base-prefix)$(libdir)/lib$(libname).so
diff --git a/ta/link_shlib.mk b/ta/link_shlib.mk
index 858c46a..662925b 100644
--- a/ta/link_shlib.mk
+++ b/ta/link_shlib.mk
@@ -17,7 +17,7 @@ cleanfiles += $(link-out-dir)/$(shlibname).stripped.so
 cleanfiles += $(link-out-dir)/$(shlibuuid).elf
 cleanfiles += $(link-out-dir)/$(shlibuuid).ta
 
-shlink-ldflags  = $(LDFLAGS)
+shlink-ldflags  = $(LD_LDFLAGS)
 shlink-ldflags += -shared -z max-page-size=4096
 shlink-ldflags += $(call ld-option,-z separate-loadable-segments)
 ifeq ($(sm)-$(CFG_TA_BTI),ta_arm64-y)
-- 
2.51.0

Reply via email to