arm_ssp_per_task_plugin.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "gcc-common.h"
  3. __visible int plugin_is_GPL_compatible;
  4. static unsigned int 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 current;
  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 current task's stack canary value
  27. */
  28. current = gen_reg_rtx(Pmode);
  29. emit_insn_before(gen_load_tp_hard(current), insn);
  30. SET_SRC(body) = gen_rtx_PLUS(Pmode, current,
  31. GEN_INT(canary_offset));
  32. }
  33. return 0;
  34. }
  35. #define PASS_NAME arm_pertask_ssp_rtl
  36. #define NO_GATE
  37. #include "gcc-generate-rtl-pass.h"
  38. #if BUILDING_GCC_VERSION >= 9000
  39. static bool no(void)
  40. {
  41. return false;
  42. }
  43. static void arm_pertask_ssp_start_unit(void *gcc_data, void *user_data)
  44. {
  45. targetm.have_stack_protect_combined_set = no;
  46. targetm.have_stack_protect_combined_test = no;
  47. }
  48. #endif
  49. __visible int plugin_init(struct plugin_name_args *plugin_info,
  50. struct plugin_gcc_version *version)
  51. {
  52. const char * const plugin_name = plugin_info->base_name;
  53. const int argc = plugin_info->argc;
  54. const struct plugin_argument *argv = plugin_info->argv;
  55. int i;
  56. if (!plugin_default_version_check(version, &gcc_version)) {
  57. error(G_("incompatible gcc/plugin versions"));
  58. return 1;
  59. }
  60. for (i = 0; i < argc; ++i) {
  61. if (!strcmp(argv[i].key, "disable"))
  62. return 0;
  63. /* all remaining options require a value */
  64. if (!argv[i].value) {
  65. error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
  66. plugin_name, argv[i].key);
  67. return 1;
  68. }
  69. if (!strcmp(argv[i].key, "offset")) {
  70. canary_offset = atoi(argv[i].value);
  71. continue;
  72. }
  73. error(G_("unknown option '-fplugin-arg-%s-%s'"),
  74. plugin_name, argv[i].key);
  75. return 1;
  76. }
  77. PASS_INFO(arm_pertask_ssp_rtl, "expand", 1, PASS_POS_INSERT_AFTER);
  78. register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP,
  79. NULL, &arm_pertask_ssp_rtl_pass_info);
  80. #if BUILDING_GCC_VERSION >= 9000
  81. register_callback(plugin_info->base_name, PLUGIN_START_UNIT,
  82. arm_pertask_ssp_start_unit, NULL);
  83. #endif
  84. return 0;
  85. }