Skip to content

Commit c36f800

Browse files
jgmelberclaude
andcommitted
Force PIE on bootgen with explicit compiler/linker flags, not just the CMake property
Same fix as #3333 (against main), carried forward here since this branch replaces bootgen's CMakeLists.txt with the 2026.1 upstream-source layout. The POSITION_INDEPENDENT_CODE property set on bootgen-lib/bootgen doesn't actually force PIE: this project never calls check_pie_supported(), which CMake's CMP0083 needs to turn that property into real -fPIE/-pie flags for an executable target. Confirmed via readelf + strace that the wheel built from #3327's own commit (main's earlier attempt at this) still ships a non-PIE `bootgen` and segfaults (SIGSEGV/SEGV_ACCERR jumping into its own non-executable DYNAMIC segment) once auditwheel repair patches in an RPATH for vendored OpenSSL -- manylinux_2_28's gcc-toolset doesn't default to PIE the way a distro's system gcc does. Pass -fPIC/-fPIE/-pie explicitly, last, so they win over any inherited -fno-pie/-fno-PIC via ordinary last-flag-wins compiler semantics. Verified locally under a forced -fno-pie -fno-PIC -no-pie global configuration that this reliably produces a true PIE (ET_DYN) bootgen, unlike the property-only approach. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 980215d commit c36f800

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

tools/bootgen/CMakeLists.txt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
135135
-Wno-deprecated-declarations
136136
-Wno-sign-compare
137137
-Wno-type-limits
138-
-Wno-unused)
138+
-Wno-unused
139+
# See bootgen-lib below for why -fPIC is also passed explicitly here.
140+
-fPIC)
139141
endif()
140142

141143
add_library(bootgen-lib STATIC ${libsources})
142144
# bootgen must be compiled with PIE/PIC, irrespective of the remainder of
143-
# the project, because auditwheel repair's patchelf-based RPATH rewrite
144-
# corrupts the non-PIE binary
145+
# the project, because auditwheel repair's patchelf-based RPATH rewrite
146+
# corrupts the non-PIE binary.
145147
set_target_properties(bootgen-lib PROPERTIES POSITION_INDEPENDENT_CODE ON)
146148
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
147149
set(bootgen_warning_ignores
@@ -179,6 +181,18 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
179181
target_compile_options(bootgen-lib PRIVATE /wd4996 /wd4840 /wd4005 /wd5033)
180182
endif()
181183
target_compile_options(bootgen-lib PRIVATE ${bootgen_warning_ignores})
184+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
185+
# Explicit -fPIC, not just the POSITION_INDEPENDENT_CODE property above:
186+
# that property relies on CMake's CMP0083 + the CheckPIESupported module
187+
# to inject the right compiler/linker flags, and this project never calls
188+
# check_pie_supported(), so the property silently does nothing on some
189+
# toolchains (observed on the manylinux_2_28 gcc-toolset used to build
190+
# release wheels, which -- unlike a distro's system gcc -- does not
191+
# default to PIE/PIC either). Passed last so it wins (compilers apply
192+
# "last flag wins" for -fPIC/-fno-PIC) over anything inherited from the
193+
# linked MLIR/LLVM package's own build configuration.
194+
target_compile_options(bootgen-lib PRIVATE -fPIC)
195+
endif()
182196
target_include_directories(bootgen-lib PRIVATE ${bootgen_include_dirs} ${OPENSSL_INCLUDE_DIR})
183197
# Make C API header available publicly
184198
target_include_directories(bootgen-lib PUBLIC
@@ -203,6 +217,15 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
203217
target_compile_options(bootgen PRIVATE /EHsc)
204218
target_compile_options(bootgen PRIVATE /wd4996 /wd4840 /wd4005 /wd5033)
205219
endif()
220+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
221+
# Explicit -fPIE/-pie (see bootgen-lib above for why the
222+
# POSITION_INDEPENDENT_CODE property alone can't be trusted). Passed last
223+
# so it wins -- compilers apply "last flag wins" for -fPIE/-fno-PIE and
224+
# -pie/-no-pie -- over anything inherited from the linked MLIR/LLVM
225+
# package's own build configuration.
226+
target_compile_options(bootgen PRIVATE -fPIE)
227+
target_link_options(bootgen PRIVATE -fPIE -pie)
228+
endif()
206229
target_compile_definitions(bootgen PRIVATE OPENSSL_USE_APPLINK)
207230
target_link_libraries(bootgen PRIVATE bootgen-lib OpenSSL::SSL OpenSSL::applink)
208231
install(TARGETS bootgen)

0 commit comments

Comments
 (0)