Skip to content

Commit 7e151ed

Browse files
committed
configure: Add PIE detection logic for BOLT optimizations compatibility
BOLT requires the --relocs flag if a binary is built as PIE (Position Independent Executable). Test if a build system enforces PIE during compilation, via the preprocessor macros, or via linkage via creating a test binary. If so, -fno-pie and -no-pie flags will not be enforced when --enable-bolt is used and --relocs will be added to the BOLT flags.
1 parent f673f0e commit 7e151ed

File tree

2 files changed

+118
-8
lines changed

2 files changed

+118
-8
lines changed

configure

Lines changed: 70 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,10 +2116,49 @@ if test "$Py_BOLT" = 'true' ; then
21162116
# These flags are required for bolt to work:
21172117
LDFLAGS_NODIST="$LDFLAGS_NODIST -Wl,--emit-relocs"
21182118

2119-
# These flags are required to get good performance from bolt:
2120-
CFLAGS_NODIST="$CFLAGS_NODIST -fno-pie"
2121-
# We want to add these no-pie flags to linking executables but not shared libraries:
2122-
LINKCC="$LINKCC -fno-pie -no-pie"
2119+
# Detect if PIE is enabled
2120+
# Compile-time PIE: -fPIE in CFLAGS (defines __PIE__)
2121+
# Link-time PIE: -pie for link (no __PIE__ defined)
2122+
# We need to check CFLAGS and LDFLAGS.
2123+
AC_MSG_CHECKING([whether PIE is enabled by default])
2124+
2125+
pie_enabled=no
2126+
2127+
# Check for compile-time PIE (-fPIE defines __PIE__/__pie__)
2128+
# Save original CFLAGS and temporarily add CFLAGS_NODIST to test
2129+
save_CFLAGS="$CFLAGS"
2130+
CFLAGS="$CFLAGS $CFLAGS_NODIST"
2131+
AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
2132+
#if !defined(__PIE__) && !defined(__pie__)
2133+
#error PIE not enabled
2134+
#endif
2135+
]])],
2136+
[pie_enabled=yes],
2137+
[])
2138+
CFLAGS="$save_CFLAGS"
2139+
2140+
# Check for link-time PIE (test if linker produces PIE by default)
2141+
if test "$pie_enabled" = "no"; then
2142+
save_LDFLAGS="$LDFLAGS"
2143+
LDFLAGS="$LDFLAGS $LDFLAGS_NODIST"
2144+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],[[]])],
2145+
[if file conftest$EXEEXT 2>/dev/null | grep -q -E 'pie executable|shared object'; then
2146+
pie_enabled=yes
2147+
fi],
2148+
[])
2149+
LDFLAGS="$save_LDFLAGS"
2150+
fi
2151+
2152+
AC_MSG_RESULT([$pie_enabled])
2153+
2154+
# Only add -fno-pie flags if PIE is not enabled by default.
2155+
# If PIE is enabled, these flags would conflict with the build system's PIE enforcement.
2156+
if test "$pie_enabled" = "no"; then
2157+
# These flags are required to get good performance from bolt:
2158+
CFLAGS_NODIST="$CFLAGS_NODIST -fno-pie"
2159+
# We want to add these no-pie flags to linking executables but not shared libraries:
2160+
LINKCC="$LINKCC -fno-pie -no-pie"
2161+
fi
21232162
AC_SUBST([LLVM_BOLT])
21242163
AC_PATH_TOOL([LLVM_BOLT], [llvm-bolt], [''], [${llvm_path}])
21252164
if test -n "${LLVM_BOLT}" -a -x "${LLVM_BOLT}"
@@ -2165,6 +2204,11 @@ then
21652204
[-skip-funcs=_PyEval_EvalFrameDefault,sre_ucs1_match/1,sre_ucs2_match/1,sre_ucs4_match/1]
21662205
")]
21672206
)
2207+
2208+
# Add --relocs flag if PIE is enabled, as BOLT requires it for PIE binaries
2209+
if test "${pie_enabled:-no}" = "yes"; then
2210+
BOLT_COMMON_FLAGS="${BOLT_COMMON_FLAGS} --relocs"
2211+
fi
21682212
fi
21692213

21702214
AC_ARG_VAR(

0 commit comments

Comments
 (0)