Issue Details
- Number
- 31843
- Title
- build: `-static-pie` builds no-longer working with CMake
- Description
- Using 28.x, it's easy to produce a `-static-pie` binary doing:
```bash
# aarch64 alpine, 28.x (32efe850438ef22e2de39e562af557872a402c31)
make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_USDT=1 NO_UPNP=1 NO_NATPMP=1 LDFLAGS="-static-pie"
./autogen.sh
CONFIG_SITE=/bitcoin/depends/aarch64-unknown-linux-musl/share/config.site ./configure
make src/bitcoind
# link line
libtool: link: /usr/bin/ccache g++ -std=c++20 <snip> -fPIE <snip> -pie -static-pie -o bitcoind bitcoind-bitcoind.o ....
# file src/bitcoind
src/bitcoind: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), static-pie linked, BuildID[sha1]=b2b5020870722780c89654169ae9e8d21fde955b, with debug_info, not stripped
```
This no-longer works with master (86528937e5c4da2e12c46085fc41e87ed759258e):
```bash
# aarch64 alpine, master (86528937e5c4da2e12c46085fc41e87ed759258e)
make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_USDT=1 LDFLAGS="-static-pie"
cmake -B build --toolchain /bitcoin/depends/aarch64-unknown-linux-musl/toolchain.cmake
cmake --build build --target bitcoind
# link line
/usr/bin/g++ <snip> -static-pie <snip> -fPIE -pie <snip> CMakeFiles/bitcoind.dir/init/bitcoind.cpp.o -o bitcoind ....
# file build/src/bitcoind
build/src/bitcoind: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-aarch64.so.1, BuildID[sha1]=fc952a9ebce3b0627f2b93f0a6011323071ce27a, with debug_info, not stripped
```
I assumed using something like [`-DCMAKE_EXE_LINKER_FLAGS`](https://cmake.org/cmake/help/latest/variable/CMAKE_EXE_LINKER_FLAGS.html) would work, but it also does not:
```bash
# aarch64 alpine, master (86528937e5c4da2e12c46085fc41e87ed759258e)
make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_USDT=1
cmake -B build --toolchain /bitcoin/depends/aarch64-unknown-linux-musl/toolchain.cmake -DCMAKE_EXE_LINKER_FLAGS="-static-pie"
cmake --build build --target bitcoind
# link line
/usr/bin/g++ <snip> -static-pie <snip> -fPIE -pie <snip> CMakeFiles/bitcoind.dir/init/bitcoind.cpp.o -o bitcoind …
# file build/src/bitcoind
build/src/bitcoind: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-aarch64.so.1, BuildID[sha1]=fc952a9ebce3b0627f2b93f0a6011323071ce27a, with debug_info, not stripped
```
Seems this is due to the linker flag ordering, where `-pie` is now appearing later on the link line, and nullifying the `-static-pie`.
You can make things work using `-DAPPEND_LDFLAGS`, but using this (non-standard workaround) shouldn't be necessary.
- URL
-
https://github.com/bitcoin/bitcoin/issue/31843
- Closed by
-
Back to List