Skip to content

Commit 815250b

Browse files
bdashMark Rowe
andauthored
[compiler-rt] Don't rely on automatic codesigning with Apple's linker (#91681)
In #88323, I changed the logic within `add_compiler_rt_runtime` to only explicitly code sign the resulting library if an older version of Apple's ld64 was in use. This was based on the assumption that newer versions of ld64 and the new Apple linker always ad-hoc sign their output binaries. This is true in most cases, but not when using Apple's new linker with the `-darwin-target-variant` flag to build Mac binaries that are compatible with Catalyst. Rather than adding increasingly complicated logic to detect the exact scenarios that require explicit code signing, I've opted to always explicitly code sign when using any Apple linker. We instead detect and use the 'linker-signed' codesigning option when possible to match the signatures that the linker would otherwise create. This avoids having non-'linker-signed' ad-hoc signatures which was the underlying problem that #88323 was intended to address. Co-authored-by: Mark Rowe <[email protected]>
1 parent d38d0a0 commit 815250b

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -387,35 +387,35 @@ function(add_compiler_rt_runtime name type)
387387
set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
388388
endif()
389389
if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
390-
# Ad-hoc sign the dylibs when using Xcode versions older than 12.
391-
# Xcode 12 shipped with ld64-609.
392-
# FIXME: Remove whole conditional block once everything uses Xcode 12+.
393-
set(LD_V_OUTPUT)
390+
# Apple's linker signs the resulting dylib with an ad-hoc code signature in
391+
# most situations, except:
392+
# 1. Versions of ld64 prior to ld64-609 in Xcode 12 predate this behavior.
393+
# 2. Apple's new linker does not when building with `-darwin-target-variant`
394+
# to support macOS Catalyst.
395+
#
396+
# Explicitly re-signing the dylib works around both of these issues. The
397+
# signature is marked as `linker-signed` when that is supported so that it
398+
# behaves as expected when processed by subsequent tooling.
399+
#
400+
# Detect whether `codesign` supports `-o linker-signed` by passing it as an
401+
# argument and looking for `invalid argument "linker-signed"` in its output.
402+
# FIXME: Remove this once all supported toolchains support `-o linker-signed`.
394403
execute_process(
395-
COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
396-
RESULT_VARIABLE HAD_ERROR
397-
OUTPUT_VARIABLE LD_V_OUTPUT
404+
COMMAND sh -c "codesign -f -s - -o linker-signed this-does-not-exist 2>&1 | grep -q linker-signed"
405+
RESULT_VARIABLE CODESIGN_SUPPORTS_LINKER_SIGNED
398406
)
399-
if (HAD_ERROR)
400-
message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
401-
endif()
402-
set(NEED_EXPLICIT_ADHOC_CODESIGN 0)
403-
# Apple introduced a new linker by default in Xcode 15. This linker reports itself as ld
404-
# rather than ld64 and does not match this version regex. That's ok since it never needs
405-
# the explicit ad-hoc code signature.
406-
if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
407-
string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
408-
if (HOST_LINK_VERSION VERSION_LESS 609)
409-
set(NEED_EXPLICIT_ADHOC_CODESIGN 1)
410-
endif()
411-
endif()
412-
if (NEED_EXPLICIT_ADHOC_CODESIGN)
413-
add_custom_command(TARGET ${libname}
414-
POST_BUILD
415-
COMMAND codesign --sign - $<TARGET_FILE:${libname}>
416-
WORKING_DIRECTORY ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
417-
)
407+
408+
set(EXTRA_CODESIGN_ARGUMENTS)
409+
if (CODESIGN_SUPPORTS_LINKER_SIGNED)
410+
list(APPEND EXTRA_CODESIGN_ARGUMENTS -o linker-signed)
418411
endif()
412+
413+
add_custom_command(TARGET ${libname}
414+
POST_BUILD
415+
COMMAND codesign --sign - ${EXTRA_CODESIGN_ARGUMENTS} $<TARGET_FILE:${libname}>
416+
WORKING_DIRECTORY ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
417+
COMMAND_EXPAND_LISTS
418+
)
419419
endif()
420420
endif()
421421

0 commit comments

Comments
 (0)