gperftools/cmake/FindObjcopyWithWeaken.cmake
Aliaksey Kandratsenka a39073886a unbreak symbol weakening
It is kinda minor feature, and apparently we never had it working. But
is a nice to have. Allows our users to override malloc/free/etc while
still being able to link to us (for tc_malloc for example). With
broken weakening we had this use-case broken for static library
case. And it should now work.
2023-07-02 22:30:00 -04:00

63 lines
2.8 KiB
CMake

function(find_objcopy_with_weaken)
find_program(OBJCOPY_EXECUTABLE "objcopy")
message(STATUS "Looking for objcopy that supports weaken - ${OBJCOPY_EXECUTABLE}")
if(NOT OBJCOPY_EXECUTABLE)
return()
endif()
set(objcopy_test_src "${CMAKE_CURRENT_BINARY_DIR}/objcopy_test.c")
set(objcopy_test_exe "${CMAKE_CURRENT_BINARY_DIR}/objcopy_test")
file(WRITE ${objcopy_test_src} "void foo() {} int main() { return 0; }")
try_compile(objcopy_test_compiled
${CMAKE_CURRENT_BINARY_DIR} ${objcopy_test_src}
COPY_FILE ${objcopy_test_exe})
if(objcopy_test_compiled AND EXISTS ${objcopy_test_exe})
execute_process(
COMMAND ${OBJCOPY_EXECUTABLE} -W foo ${objcopy_test_exe}
RESULT_VARIABLE objcopy_result)
file(REMOVE ${objcopy_test_exe})
endif()
if(objcopy_result EQUAL 0)
set(objcopy_weaken ON)
endif()
file(REMOVE ${objcopy_test_src})
if(objcopy_weaken)
set(objcopy_has_weaken "Success")
set(HAVE_OBJCOPY_WEAKEN TRUE PARENT_SCOPE)
set(OBJCOPY_EXECUTABLE "${OBJCOPY_EXECUTABLE}" PARENT_SCOPE)
else()
set(objcopy_has_weaken "Failed")
endif()
message(STATUS "objcopy has weaken support - ${objcopy_has_weaken}")
endfunction(find_objcopy_with_weaken)
function(weaken_object target)
if(NOT HAVE_OBJCOPY_WEAKEN)
return()
endif()
# If we have objcopy, make malloc/free/etc weak symbols. That way folks
# can override our malloc if they want to (they can still use tc_malloc).
# Note: the weird-looking symbols are the c++ memory functions:
# (in order) new, new(nothrow), new[], new[](nothrow), delete, delete[]
# In theory this will break if mangling changes, but that seems pretty
# unlikely at this point. Just in case, I throw in versions with an
# extra underscore as well, which may help on OS X.
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${OBJCOPY_EXECUTABLE}"
-W malloc -W free -W realloc -W calloc -W cfree
-W memalign -W posix_memalign -W valloc -W pvalloc
-W aligned_alloc
-W malloc_stats -W mallopt -W mallinfo -W nallocx
-W _Znwm -W _ZnwmRKSt9nothrow_t -W _Znam -W _ZnamRKSt9nothrow_t
-W _ZdlPv -W _ZdaPv
-W __Znwm -W __ZnwmRKSt9nothrow_t -W __Znam -W __ZnamRKSt9nothrow_t
-W __ZdlPv -W __ZdaPv
-W _ZdaPvRKSt9nothrow_t -W _ZdaPvSt11align_val_t
-W _ZdaPvSt11align_val_tRKSt9nothrow_t -W _ZdaPvm
-W _ZdaPvmSt11align_val_t -W _ZdlPvRKSt9nothrow_t
-W _ZdlPvSt11align_val_t -W _ZdlPvSt11align_val_tRKSt9nothrow_t
-W _ZdlPvm -W _ZdlPvmSt11align_val_t
-W _ZnamSt11align_val_t -W _ZnamSt11align_val_tRKSt9nothrow_t
-W _ZnwmSt11align_val_t -W _ZnwmSt11align_val_tRKSt9nothrow_t
-W malloc_size -W malloc_usable_size
"$<TARGET_FILE:${target}>")
endfunction()