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