fix-compile-on-lower-version-of-gcc.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. From 24c79d845bcc1d3de35ea9052665a68f89ff6a02 Mon Sep 17 00:00:00 2001
  2. From: Redbeanw44602 <[email protected]>
  3. Date: Sat, 31 Aug 2024 01:33:04 +0800
  4. Subject: [PATCH] fix compile on lower version of gcc.
  5. ---
  6. .../ImportTableReplace/dobby_import_replace.cc | 4 ++--
  7. examples/socket_example.cc | 4 +---
  8. .../ClosureTrampolineBridge/common_bridge_handler.cc | 4 +---
  9. source/dobby/pac_kit.h | 8 +++++---
  10. source/dobby/platform_features.h | 11 ++++++++++-
  11. 5 files changed, 19 insertions(+), 12 deletions(-)
  12. diff --git a/builtin-plugin/ImportTableReplace/dobby_import_replace.cc b/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
  13. index eab1af9..1b7b573 100644
  14. --- a/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
  15. +++ b/builtin-plugin/ImportTableReplace/dobby_import_replace.cc
  16. @@ -172,13 +172,13 @@ PUBLIC int DobbyImportTableReplace(char *image_name, char *symbol_name, void *fa
  17. if (stub) {
  18. void *orig_func;
  19. orig_func = *(void **)stub;
  20. -#if __has_feature(ptrauth_calls)
  21. +#if __has_ptrauth_calls
  22. orig_func = ptrauth_strip(orig_func, ptrauth_key_asia);
  23. orig_func = ptrauth_sign_unauthenticated(orig_func, ptrauth_key_asia, 0);
  24. #endif
  25. *orig_func_ptr = orig_func;
  26. -#if __has_feature(ptrauth_calls)
  27. +#if __has_ptrauth_calls
  28. fake_func = (void *)ptrauth_strip(fake_func, ptrauth_key_asia);
  29. fake_func = ptrauth_sign_unauthenticated(fake_func, ptrauth_key_asia, stub);
  30. #endif
  31. diff --git a/examples/socket_example.cc b/examples/socket_example.cc
  32. index 1377fa3..07e2dcc 100644
  33. --- a/examples/socket_example.cc
  34. +++ b/examples/socket_example.cc
  35. @@ -43,12 +43,10 @@ const char *func_short_array[] = {
  36. // clang-format on
  37. #define pac_strip(symbol)
  38. -#if defined(__APPLE__) && __arm64e__
  39. -#if __has_feature(ptrauth_calls)
  40. +#if __has_ptrauth_calls
  41. #define pac_strip(symbol)
  42. //#define pac_strip(symbol) *(void **)&symbol = (void *)ptrauth_sign_unauthenticated((void *)symbol, ptrauth_key_asia, 0)
  43. #endif
  44. -#endif
  45. #define install_hook(name, fn_ret_t, fn_args_t...) \
  46. fn_ret_t (*orig_##name)(fn_args_t); \
  47. diff --git a/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc b/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
  48. index b1be02a..b7757e0 100644
  49. --- a/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
  50. +++ b/source/TrampolineBridge/ClosureTrampolineBridge/common_bridge_handler.cc
  51. @@ -9,13 +9,11 @@ PUBLIC void common_closure_bridge_handler(DobbyRegisterContext *ctx, ClosureTram
  52. typedef void (*routing_handler_t)(InterceptEntry *, DobbyRegisterContext *);
  53. auto routing_handler = (routing_handler_t)entry->carry_handler;
  54. -#if defined(__APPLE__) && __arm64e__
  55. -#if __has_feature(ptrauth_calls)
  56. +#if __has_ptrauth_calls
  57. uint64_t discriminator = 0;
  58. // discriminator = __builtin_ptrauth_type_discriminator(__typeof(routing_handler));
  59. routing_handler = (__typeof(routing_handler))__builtin_ptrauth_sign_unauthenticated((void *)routing_handler,
  60. ptrauth_key_asia, discriminator);
  61. -#endif
  62. #endif
  63. routing_handler((InterceptEntry *)entry->carry_data, ctx);
  64. diff --git a/source/dobby/pac_kit.h b/source/dobby/pac_kit.h
  65. index 12bf097..c8c3dbb 100644
  66. --- a/source/dobby/pac_kit.h
  67. +++ b/source/dobby/pac_kit.h
  68. @@ -2,9 +2,11 @@
  69. #include <stdint.h>
  70. +#include "platform_features.h"
  71. +
  72. #ifndef PAC_KIT
  73. #define PAC_KIT
  74. -#if defined(__arm64e__) && __has_feature(ptrauth_calls)
  75. +#if __has_ptrauth_calls
  76. #include <ptrauth.h>
  77. #endif
  78. @@ -12,7 +14,7 @@ static inline void *pac_strip(void *addr) {
  79. if (addr == NULL) {
  80. return NULL;
  81. }
  82. -#if __has_feature(ptrauth_calls)
  83. +#if __has_ptrauth_calls
  84. addr = ptrauth_strip(addr, ptrauth_key_asia);
  85. #endif
  86. return addr;
  87. @@ -22,7 +24,7 @@ static inline void *pac_sign(void *addr) {
  88. if (addr == NULL) {
  89. return NULL;
  90. }
  91. -#if __has_feature(ptrauth_calls)
  92. +#if __has_ptrauth_calls
  93. addr = ptrauth_sign_unauthenticated((void *)addr, ptrauth_key_asia, 0);
  94. #endif
  95. return addr;
  96. diff --git a/source/dobby/platform_features.h b/source/dobby/platform_features.h
  97. index fb9076a..8e9a5c5 100644
  98. --- a/source/dobby/platform_features.h
  99. +++ b/source/dobby/platform_features.h
  100. @@ -1,7 +1,16 @@
  101. #pragma once
  102. +// __has_feature is only supported in clang or gcc >= 14.0. To avoid compilation failure in lower versions
  103. +// of gcc, please should not use this extension directly in project.
  104. +#if defined(__has_feature)
  105. +// ptrauth_calls is only implemented in Apple Clang.
  106. +#if __has_feature(ptrauth_calls) // Do not use && directly, it will cause gcc < 14.0 to fail to compile
  107. +#define __has_ptrauth_calls (1)
  108. +#endif
  109. +#endif
  110. +
  111. #if defined(__APPLE__) && __arm64e__
  112. -#if __has_feature(ptrauth_calls)
  113. +#if __has_ptrauth_calls
  114. #include <ptrauth.h>
  115. #endif
  116. #endif
  117. --
  118. 2.47.1