arm_ssp_per_task_plugin.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "gcc-common.h"
  3. __visible int plugin_is_GPL_compatible;
  4. static unsigned int sp_mask, canary_offset;
  5. static unsigned int arm_pertask_ssp_rtl_execute(void)
  6. {
  7. rtx_insn *insn;
  8. for (insn = get_insns(); insn; insn = NEXT_INSN(insn)) {
  9. const char *sym;
  10. rtx body;
  11. rtx mask, masked_sp;
  12. /*
  13. * Find a SET insn involving a SYMBOL_REF to __stack_chk_guard
  14. */
  15. if (!INSN_P(insn))
  16. continue;
  17. body = PATTERN(insn);
  18. if (GET_CODE(body) != SET ||
  19. GET_CODE(SET_SRC(body)) != SYMBOL_REF)
  20. continue;
  21. sym = XSTR(SET_SRC(body), 0);
  22. if (strcmp(sym, "__stack_chk_guard"))
  23. continue;
  24. /*
  25. * Replace the source of the SET insn with an expression that
  26. * produces the address of the copy of the stack canary value
  27. * stored in struct thread_info
  28. */
  29. mask = GEN_INT(sext_hwi(sp_mask, GET_MODE_PRECISION(Pmode)));
  30. masked_sp = gen_reg_rtx(Pmode);
  31. emit_insn_before(gen_rtx_set(masked_sp,
  32. gen_rtx_AND(Pmode,
  33. stack_pointer_rtx,
  34. mask)),
  35. insn);
  36. SET_SRC(body) = gen_rtx_PLUS(Pmode, masked_sp,
  37. GEN_INT(canary_offset));
  38. }
  39. return 0;
  40. }
  41. #define PASS_NAME arm_pertask_ssp_rtl
  42. #define NO_GATE
  43. #include "gcc-generate-rtl-pass.h"
  44. #if BUILDING_GCC_VERSION >= 9000
  45. static bool no(void)
  46. {
  47. return false;
  48. }
  49. static void arm_pertask_ssp_start_unit(void *gcc_data, void *user_data)
  50. {
  51. targetm.have_stack_protect_combined_set = no;
  52. targetm.have_stack_protect_combined_test = no;
  53. }
  54. #endif
  55. __visible int plugin_init(struct plugin_name_args *plugin_info,
  56. struct plugin_gcc_version *version)
  57. {
  58. const char * const plugin_name = plugin_info->base_name;
  59. const int argc = plugin_info->argc;
  60. const struct plugin_argument *argv = plugin_info->argv;
  61. int tso = 0;
  62. int i;
  63. if (!plugin_default_version_check(version, &gcc_version)) {
  64. error(G_("incompatible gcc/plugin versions"));
  65. return 1;
  66. }
  67. for (i = 0; i < argc; ++i) {
  68. if (!strcmp(argv[i].key, "disable"))
  69. return 0;
  70. /* all remaining options require a value */
  71. if (!argv[i].value) {
  72. error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
  73. plugin_name, argv[i].key);
  74. return 1;
  75. }
  76. if (!strcmp(argv[i].key, "tso")) {
  77. tso = atoi(argv[i].value);
  78. continue;
  79. }
  80. if (!strcmp(argv[i].key, "offset")) {
  81. canary_offset = atoi(argv[i].value);
  82. continue;
  83. }
  84. error(G_("unknown option '-fplugin-arg-%s-%s'"),
  85. plugin_name, argv[i].key);
  86. return 1;
  87. }
  88. /* create the mask that produces the base of the stack */
  89. sp_mask = ~((1U << (12 + tso)) - 1);
  90. PASS_INFO(arm_pertask_ssp_rtl, "expand", 1, PASS_POS_INSERT_AFTER);
  91. register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP,
  92. NULL, &arm_pertask_ssp_rtl_pass_info);
  93. #if BUILDING_GCC_VERSION >= 9000
  94. register_callback(plugin_info->base_name, PLUGIN_START_UNIT,
  95. arm_pertask_ssp_start_unit, NULL);
  96. #endif
  97. return 0;
  98. }