PIE_validator.cmake 896 B

1234567891011121314151617181920212223242526272829303132
  1. include_guard(GLOBAL)
  2. function (CHECK_EXECUTABLE executable result)
  3. set (${result} "UNKNOWN" PARENT_SCOPE)
  4. if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  5. set (tool otool -hv)
  6. else()
  7. set (tool "${CMAKE_COMMAND}" -E env LANG=C LC_ALL=C readelf -lW)
  8. endif()
  9. execute_process(COMMAND ${tool} "${executable}"
  10. OUTPUT_VARIABLE output
  11. ERROR_VARIABLE output)
  12. if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  13. if (output MATCHES "( |\t)PIE( |\n|$)")
  14. set (${result} "PIE" PARENT_SCOPE)
  15. else()
  16. set (${result} "NO_PIE" PARENT_SCOPE)
  17. endif()
  18. else()
  19. if (output MATCHES "Elf file type is DYN")
  20. set (${result} "PIE" PARENT_SCOPE)
  21. elseif (output MATCHES "Elf file type is EXEC")
  22. set (${result} "NO_PIE" PARENT_SCOPE)
  23. else()
  24. message(SEND_ERROR "Did not find a known file type")
  25. endif()
  26. endif()
  27. endfunction()