1189af465SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2189af465SArd Biesheuvel 
3189af465SArd Biesheuvel #include "gcc-common.h"
4189af465SArd Biesheuvel 
5189af465SArd Biesheuvel __visible int plugin_is_GPL_compatible;
6189af465SArd Biesheuvel 
7*dfbdcda2SArd Biesheuvel static unsigned int canary_offset;
8189af465SArd Biesheuvel 
arm_pertask_ssp_rtl_execute(void)9189af465SArd Biesheuvel static unsigned int arm_pertask_ssp_rtl_execute(void)
10189af465SArd Biesheuvel {
11189af465SArd Biesheuvel 	rtx_insn *insn;
12189af465SArd Biesheuvel 
13189af465SArd Biesheuvel 	for (insn = get_insns(); insn; insn = NEXT_INSN(insn)) {
14189af465SArd Biesheuvel 		const char *sym;
15189af465SArd Biesheuvel 		rtx body;
16*dfbdcda2SArd Biesheuvel 		rtx current;
17189af465SArd Biesheuvel 
18189af465SArd Biesheuvel 		/*
19189af465SArd Biesheuvel 		 * Find a SET insn involving a SYMBOL_REF to __stack_chk_guard
20189af465SArd Biesheuvel 		 */
21189af465SArd Biesheuvel 		if (!INSN_P(insn))
22189af465SArd Biesheuvel 			continue;
23189af465SArd Biesheuvel 		body = PATTERN(insn);
24189af465SArd Biesheuvel 		if (GET_CODE(body) != SET ||
25189af465SArd Biesheuvel 		    GET_CODE(SET_SRC(body)) != SYMBOL_REF)
26189af465SArd Biesheuvel 			continue;
27189af465SArd Biesheuvel 		sym = XSTR(SET_SRC(body), 0);
28189af465SArd Biesheuvel 		if (strcmp(sym, "__stack_chk_guard"))
29189af465SArd Biesheuvel 			continue;
30189af465SArd Biesheuvel 
31189af465SArd Biesheuvel 		/*
32189af465SArd Biesheuvel 		 * Replace the source of the SET insn with an expression that
33*dfbdcda2SArd Biesheuvel 		 * produces the address of the current task's stack canary value
34189af465SArd Biesheuvel 		 */
35*dfbdcda2SArd Biesheuvel 		current = gen_reg_rtx(Pmode);
36189af465SArd Biesheuvel 
37*dfbdcda2SArd Biesheuvel 		emit_insn_before(gen_load_tp_hard(current), insn);
38189af465SArd Biesheuvel 
39*dfbdcda2SArd Biesheuvel 		SET_SRC(body) = gen_rtx_PLUS(Pmode, current,
40189af465SArd Biesheuvel 					     GEN_INT(canary_offset));
41189af465SArd Biesheuvel 	}
42189af465SArd Biesheuvel 	return 0;
43189af465SArd Biesheuvel }
44189af465SArd Biesheuvel 
45189af465SArd Biesheuvel #define PASS_NAME arm_pertask_ssp_rtl
46189af465SArd Biesheuvel 
47189af465SArd Biesheuvel #define NO_GATE
48189af465SArd Biesheuvel #include "gcc-generate-rtl-pass.h"
49189af465SArd Biesheuvel 
502c88c742SArd Biesheuvel #if BUILDING_GCC_VERSION >= 9000
no(void)512c88c742SArd Biesheuvel static bool no(void)
522c88c742SArd Biesheuvel {
532c88c742SArd Biesheuvel 	return false;
542c88c742SArd Biesheuvel }
552c88c742SArd Biesheuvel 
arm_pertask_ssp_start_unit(void * gcc_data,void * user_data)562c88c742SArd Biesheuvel static void arm_pertask_ssp_start_unit(void *gcc_data, void *user_data)
572c88c742SArd Biesheuvel {
582c88c742SArd Biesheuvel 	targetm.have_stack_protect_combined_set = no;
592c88c742SArd Biesheuvel 	targetm.have_stack_protect_combined_test = no;
602c88c742SArd Biesheuvel }
612c88c742SArd Biesheuvel #endif
622c88c742SArd Biesheuvel 
plugin_init(struct plugin_name_args * plugin_info,struct plugin_gcc_version * version)63189af465SArd Biesheuvel __visible int plugin_init(struct plugin_name_args *plugin_info,
64189af465SArd Biesheuvel 			  struct plugin_gcc_version *version)
65189af465SArd Biesheuvel {
66189af465SArd Biesheuvel 	const char * const plugin_name = plugin_info->base_name;
67189af465SArd Biesheuvel 	const int argc = plugin_info->argc;
68189af465SArd Biesheuvel 	const struct plugin_argument *argv = plugin_info->argv;
69189af465SArd Biesheuvel 	int i;
70189af465SArd Biesheuvel 
71189af465SArd Biesheuvel 	if (!plugin_default_version_check(version, &gcc_version)) {
72189af465SArd Biesheuvel 		error(G_("incompatible gcc/plugin versions"));
73189af465SArd Biesheuvel 		return 1;
74189af465SArd Biesheuvel 	}
75189af465SArd Biesheuvel 
76189af465SArd Biesheuvel 	for (i = 0; i < argc; ++i) {
77189af465SArd Biesheuvel 		if (!strcmp(argv[i].key, "disable"))
78189af465SArd Biesheuvel 			return 0;
79189af465SArd Biesheuvel 
80189af465SArd Biesheuvel 		/* all remaining options require a value */
81189af465SArd Biesheuvel 		if (!argv[i].value) {
82189af465SArd Biesheuvel 			error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
83189af465SArd Biesheuvel 			      plugin_name, argv[i].key);
84189af465SArd Biesheuvel 			return 1;
85189af465SArd Biesheuvel 		}
86189af465SArd Biesheuvel 
87189af465SArd Biesheuvel 		if (!strcmp(argv[i].key, "offset")) {
88189af465SArd Biesheuvel 			canary_offset = atoi(argv[i].value);
89189af465SArd Biesheuvel 			continue;
90189af465SArd Biesheuvel 		}
91189af465SArd Biesheuvel 		error(G_("unknown option '-fplugin-arg-%s-%s'"),
92189af465SArd Biesheuvel 		      plugin_name, argv[i].key);
93189af465SArd Biesheuvel 		return 1;
94189af465SArd Biesheuvel 	}
95189af465SArd Biesheuvel 
96189af465SArd Biesheuvel 	PASS_INFO(arm_pertask_ssp_rtl, "expand", 1, PASS_POS_INSERT_AFTER);
97189af465SArd Biesheuvel 
98189af465SArd Biesheuvel 	register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP,
99189af465SArd Biesheuvel 			  NULL, &arm_pertask_ssp_rtl_pass_info);
100189af465SArd Biesheuvel 
1012c88c742SArd Biesheuvel #if BUILDING_GCC_VERSION >= 9000
1022c88c742SArd Biesheuvel 	register_callback(plugin_info->base_name, PLUGIN_START_UNIT,
1032c88c742SArd Biesheuvel 			  arm_pertask_ssp_start_unit, NULL);
1042c88c742SArd Biesheuvel #endif
1052c88c742SArd Biesheuvel 
106189af465SArd Biesheuvel 	return 0;
107189af465SArd Biesheuvel }
108