From dfdcaaa269f684264644dc05e13aee8028ed1a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 29 Sep 2026 15:41:17 +0200 Subject: [PATCH] meson: use -static-pie instead of -static in PIE mode If compiling with -Db_pie=true, we should do PIE compilation (position independent executables), even in the static mode. The caveat is that people distributions actually override compilation flags when doing their builds. They'll need to set the static flags properly if they do such builds. (cherry picked from commit 8b289a8c4d9e2f69a2c10d109c1c19f353005047) --- meson.build | 5 ++--- src/core/meson.build | 13 ++++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index 2db5138147..02fe41b32b 100644 --- a/meson.build +++ b/meson.build @@ -1028,9 +1028,8 @@ libm = cc.find_library('m') # On some architectures, libatomic is required. But on some installations, # it is found, but actual linking fails. So let's try to use it opportunistically. # If it is installed, but not needed, it will be dropped because of --as-needed. -if cc.links('''int main(int argc, char **argv) { return 0; }''', - args : '-latomic', - name : 'libatomic') +basic_c_prog = '''int main(int argc, char **argv) { return 0; }''' +if cc.links(basic_c_prog, args : '-latomic', name : 'libatomic') libatomic = declare_dependency(link_args : '-latomic') else libatomic = [] diff --git a/src/core/meson.build b/src/core/meson.build index e8d6563803..80418bfe57 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -219,13 +219,24 @@ if conf.get('SYSTEMD_MULTICALL_BINARY') == 1 ] endif +if get_option('build-static') + if (get_option('b_pie') and + cc.links(basic_c_prog, args : '-static-pie', name : '-static-pie')) + systemd_link_args = ['-static-pie', '-Wl,-static,-pie'] + else + systemd_link_args = ['-static'] + endif +else + systemd_link_args = [] +endif + executables += [ libexec_template + { 'name' : 'systemd', 'dbus' : true, 'public' : true, 'sources' : systemd_sources, - 'link_args_static' : get_option('build-static') ? ['-static'] : [], + 'link_args_static' : systemd_link_args, 'link_with' : [ libcore, libshared,