Blame SOURCES/gcc48-pr78875.patch

25c7f1
2017-01-17  Segher Boessenkool  <segher@kernel.crashing.org>
25c7f1
25c7f1
	PR target/78875
25c7f1
	* config/rs6000/rs6000-opts.h (stack_protector_guard): New enum.
25c7f1
	* config/rs6000/rs6000.c (rs6000_option_override_internal): Handle
25c7f1
	the new options.
25c7f1
	* config/rs6000/rs6000.md (stack_protect_set): Handle the new more
25c7f1
	flexible settings.
25c7f1
	(stack_protect_test): Ditto.
25c7f1
	* config/rs6000/rs6000.opt (mstack-protector-guard=,
25c7f1
	mstack-protector-guard-reg=, mstack-protector-guard-offset=): New
25c7f1
	options.
25c7f1
	* doc/invoke.texi (Option Summary) [RS/6000 and PowerPC Options]:
25c7f1
	Add -mstack-protector-guard=, -mstack-protector-guard-reg=, and
25c7f1
	-mstack-protector-guard-offset=.
25c7f1
	(RS/6000 and PowerPC Options): Ditto.
25c7f1
25c7f1
	* gcc.target/powerpc/ssp-1.c: New testcase.
25c7f1
	* gcc.target/powerpc/ssp-2.c: New testcase.
25c7f1
25c7f1
--- gcc/config/rs6000/rs6000.opt	(revision 244555)
25c7f1
+++ gcc/config/rs6000/rs6000.opt	(revision 244556)
25c7f1
@@ -593,3 +593,31 @@ Allow float variables in upper registers
25c7f1
 moptimize-swaps
25c7f1
 Target Undocumented Var(rs6000_optimize_swaps) Init(1) Save
25c7f1
 Analyze and remove doubleword swaps from VSX computations.
25c7f1
+
25c7f1
+mstack-protector-guard=
25c7f1
+Target RejectNegative Joined Enum(stack_protector_guard) Var(rs6000_stack_protector_guard) Init(SSP_TLS)
25c7f1
+Use given stack-protector guard.
25c7f1
+
25c7f1
+Enum
25c7f1
+Name(stack_protector_guard) Type(enum stack_protector_guard)
25c7f1
+Valid arguments to -mstack-protector-guard=:
25c7f1
+
25c7f1
+EnumValue
25c7f1
+Enum(stack_protector_guard) String(tls) Value(SSP_TLS)
25c7f1
+
25c7f1
+EnumValue
25c7f1
+Enum(stack_protector_guard) String(global) Value(SSP_GLOBAL)
25c7f1
+
25c7f1
+mstack-protector-guard-reg=
25c7f1
+Target RejectNegative Joined Var(rs6000_stack_protector_guard_reg_str)
25c7f1
+Use the given base register for addressing the stack-protector guard.
25c7f1
+
25c7f1
+TargetVariable
25c7f1
+int rs6000_stack_protector_guard_reg = 0
25c7f1
+
25c7f1
+mstack-protector-guard-offset=
25c7f1
+Target RejectNegative Joined Integer Var(rs6000_stack_protector_guard_offset_str)
25c7f1
+Use the given offset for addressing the stack-protector guard.
25c7f1
+
25c7f1
+TargetVariable
25c7f1
+long rs6000_stack_protector_guard_offset = 0
25c7f1
--- gcc/config/rs6000/rs6000.c	(revision 244555)
25c7f1
+++ gcc/config/rs6000/rs6000.c	(revision 244556)
25c7f1
@@ -3727,6 +3727,54 @@ rs6000_option_override_internal (bool gl
25c7f1
 				    atoi (rs6000_sched_insert_nops_str));
25c7f1
     }
25c7f1
 
25c7f1
+  /* Handle stack protector */
25c7f1
+  if (!global_options_set.x_rs6000_stack_protector_guard)
25c7f1
+#ifdef TARGET_THREAD_SSP_OFFSET
25c7f1
+    rs6000_stack_protector_guard = SSP_TLS;
25c7f1
+#else
25c7f1
+    rs6000_stack_protector_guard = SSP_GLOBAL;
25c7f1
+#endif
25c7f1
+
25c7f1
+#ifdef TARGET_THREAD_SSP_OFFSET
25c7f1
+  rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
25c7f1
+  rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
25c7f1
+#endif
25c7f1
+
25c7f1
+  if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
25c7f1
+    {
25c7f1
+      char *endp;
25c7f1
+      const char *str = rs6000_stack_protector_guard_offset_str;
25c7f1
+
25c7f1
+      errno = 0;
25c7f1
+      long offset = strtol (str, &endp, 0);
25c7f1
+      if (!*str || *endp || errno)
25c7f1
+	error ("%qs is not a valid number "
25c7f1
+	       "in -mstack-protector-guard-offset=", str);
25c7f1
+
25c7f1
+      if (!IN_RANGE (offset, -0x8000, 0x7fff)
25c7f1
+	  || (TARGET_64BIT && (offset & 3)))
25c7f1
+	error ("%qs is not a valid offset "
25c7f1
+	       "in -mstack-protector-guard-offset=", str);
25c7f1
+
25c7f1
+      rs6000_stack_protector_guard_offset = offset;
25c7f1
+    }
25c7f1
+
25c7f1
+  if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
25c7f1
+    {
25c7f1
+      const char *str = rs6000_stack_protector_guard_reg_str;
25c7f1
+      int reg = decode_reg_name (str);
25c7f1
+
25c7f1
+      if (!IN_RANGE (reg, 1, 31))
25c7f1
+	error ("%qs is not a valid base register "
25c7f1
+	       "in -mstack-protector-guard-reg=", str);
25c7f1
+
25c7f1
+      rs6000_stack_protector_guard_reg = reg;
25c7f1
+    }
25c7f1
+
25c7f1
+  if (rs6000_stack_protector_guard == SSP_TLS
25c7f1
+      && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
25c7f1
+    error ("-mstack-protector-guard=tls needs a valid base register");
25c7f1
+
25c7f1
   if (global_init_p)
25c7f1
     {
25c7f1
 #ifdef TARGET_REGNAMES
25c7f1
--- gcc/config/rs6000/rs6000.md	(revision 244555)
25c7f1
+++ gcc/config/rs6000/rs6000.md	(revision 244556)
25c7f1
@@ -13092,19 +13092,23 @@
25c7f1
 
25c7f1
 
25c7f1
 (define_expand "stack_protect_set"
25c7f1
-  [(match_operand 0 "memory_operand" "")
25c7f1
-   (match_operand 1 "memory_operand" "")]
25c7f1
+  [(match_operand 0 "memory_operand")
25c7f1
+   (match_operand 1 "memory_operand")]
25c7f1
   ""
25c7f1
 {
25c7f1
-#ifdef TARGET_THREAD_SSP_OFFSET
25c7f1
-  rtx tlsreg = gen_rtx_REG (Pmode, TARGET_64BIT ? 13 : 2);
25c7f1
-  rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET));
25c7f1
-  operands[1] = gen_rtx_MEM (Pmode, addr);
25c7f1
-#endif
25c7f1
+  if (rs6000_stack_protector_guard == SSP_TLS)
25c7f1
+    {
25c7f1
+      rtx reg = gen_rtx_REG (Pmode, rs6000_stack_protector_guard_reg);
25c7f1
+      rtx offset = GEN_INT (rs6000_stack_protector_guard_offset);
25c7f1
+      rtx addr = gen_rtx_PLUS (Pmode, reg, offset);
25c7f1
+      operands[1] = gen_rtx_MEM (Pmode, addr);
25c7f1
+    }
25c7f1
+
25c7f1
   if (TARGET_64BIT)
25c7f1
     emit_insn (gen_stack_protect_setdi (operands[0], operands[1]));
25c7f1
   else
25c7f1
     emit_insn (gen_stack_protect_setsi (operands[0], operands[1]));
25c7f1
+
25c7f1
   DONE;
25c7f1
 })
25c7f1
 
25c7f1
@@ -13127,21 +13131,26 @@
25c7f1
    (set_attr "length" "12")])
25c7f1
 
25c7f1
 (define_expand "stack_protect_test"
25c7f1
-  [(match_operand 0 "memory_operand" "")
25c7f1
-   (match_operand 1 "memory_operand" "")
25c7f1
-   (match_operand 2 "" "")]
25c7f1
+  [(match_operand 0 "memory_operand")
25c7f1
+   (match_operand 1 "memory_operand")
25c7f1
+   (match_operand 2 "")]
25c7f1
   ""
25c7f1
 {
25c7f1
-  rtx test, op0, op1;
25c7f1
-#ifdef TARGET_THREAD_SSP_OFFSET
25c7f1
-  rtx tlsreg = gen_rtx_REG (Pmode, TARGET_64BIT ? 13 : 2);
25c7f1
-  rtx addr = gen_rtx_PLUS (Pmode, tlsreg, GEN_INT (TARGET_THREAD_SSP_OFFSET));
25c7f1
-  operands[1] = gen_rtx_MEM (Pmode, addr);
25c7f1
-#endif
25c7f1
-  op0 = operands[0];
25c7f1
-  op1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, operands[1]), UNSPEC_SP_TEST);
25c7f1
-  test = gen_rtx_EQ (VOIDmode, op0, op1);
25c7f1
-  emit_jump_insn (gen_cbranchsi4 (test, op0, op1, operands[2]));
25c7f1
+  rtx guard = operands[1];
25c7f1
+
25c7f1
+  if (rs6000_stack_protector_guard == SSP_TLS)
25c7f1
+    {
25c7f1
+      rtx reg = gen_rtx_REG (Pmode, rs6000_stack_protector_guard_reg);
25c7f1
+      rtx offset = GEN_INT (rs6000_stack_protector_guard_offset);
25c7f1
+      rtx addr = gen_rtx_PLUS (Pmode, reg, offset);
25c7f1
+      guard = gen_rtx_MEM (Pmode, addr);
25c7f1
+    }
25c7f1
+
25c7f1
+  operands[1] = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, guard), UNSPEC_SP_TEST);
25c7f1
+  rtx test = gen_rtx_EQ (VOIDmode, operands[0], operands[1]);
25c7f1
+  rtx jump = gen_cbranchsi4 (test, operands[0], operands[1], operands[2]);
25c7f1
+  emit_jump_insn (jump);
25c7f1
+
25c7f1
   DONE;
25c7f1
 })
25c7f1
 
25c7f1
--- gcc/config/rs6000/rs6000-opts.h	(revision 244555)
25c7f1
+++ gcc/config/rs6000/rs6000-opts.h	(revision 244556)
25c7f1
@@ -154,6 +154,12 @@ enum rs6000_vector {
25c7f1
   VECTOR_OTHER			/* Some other vector unit */
25c7f1
 };
25c7f1
 
25c7f1
+/* Where to get the canary for the stack protector.  */
25c7f1
+enum stack_protector_guard {
25c7f1
+  SSP_TLS,			/* per-thread canary in TLS block */
25c7f1
+  SSP_GLOBAL			/* global canary */
25c7f1
+};
25c7f1
+
25c7f1
 /* No enumeration is defined to index the -mcpu= values (entries in
25c7f1
    processor_target_table), with the type int being used instead, but
25c7f1
    we need to distinguish the special "native" value.  */
25c7f1
--- gcc/doc/invoke.texi	(revision 244555)
25c7f1
+++ gcc/doc/invoke.texi	(revision 244556)
25c7f1
@@ -862,7 +862,9 @@ See RS/6000 and PowerPC Options.
25c7f1
 -mcrypto -mno-crypto -mdirect-move -mno-direct-move @gol
25c7f1
 -mquad-memory -mno-quad-memory @gol
25c7f1
 -mquad-memory-atomic -mno-quad-memory-atomic @gol
25c7f1
--mcompat-align-parm -mno-compat-align-parm}
25c7f1
+-mcompat-align-parm -mno-compat-align-parm @gol
25c7f1
+-mstack-protector-guard=@var{guard} -mstack-protector-guard-reg=@var{reg} @gol
25c7f1
+-mstack-protector-guard-offset=@var{offset}}
25c7f1
 
25c7f1
 @emph{RX Options}
25c7f1
 @gccoptlist{-m64bit-doubles  -m32bit-doubles  -fpu  -nofpu@gol
25c7f1
@@ -18295,6 +18297,23 @@ GCC.
25c7f1
 
25c7f1
 In this version of the compiler, the @option{-mcompat-align-parm}
25c7f1
 is the default, except when using the Linux ELFv2 ABI.
25c7f1
+
25c7f1
+@item -mstack-protector-guard=@var{guard}
25c7f1
+@itemx -mstack-protector-guard-reg=@var{reg}
25c7f1
+@itemx -mstack-protector-guard-offset=@var{offset}
25c7f1
+@opindex mstack-protector-guard
25c7f1
+@opindex mstack-protector-guard-reg
25c7f1
+@opindex mstack-protector-guard-offset
25c7f1
+Generate stack protection code using canary at @var{guard}.  Supported
25c7f1
+locations are @samp{global} for global canary or @samp{tls} for per-thread
25c7f1
+canary in the TLS block (the default with GNU libc version 2.4 or later).
25c7f1
+
25c7f1
+With the latter choice the options
25c7f1
+@option{-mstack-protector-guard-reg=@var{reg}} and
25c7f1
+@option{-mstack-protector-guard-offset=@var{offset}} furthermore specify
25c7f1
+which register to use as base register for reading the canary, and from what
25c7f1
+offset from that base register. The default for those is as specified in the
25c7f1
+relevant ABI.
25c7f1
 @end table
25c7f1
 
25c7f1
 @node RX Options
25c7f1
--- gcc/testsuite/gcc.target/powerpc/ssp-1.c	(nonexistent)
25c7f1
+++ gcc/testsuite/gcc.target/powerpc/ssp-1.c	(revision 244562)
25c7f1
@@ -0,0 +1,6 @@
25c7f1
+/* { dg-do compile } */
25c7f1
+/* { dg-options "-O2 -fstack-protector-all -mstack-protector-guard=global" } */
25c7f1
+
25c7f1
+/* { dg-final { scan-assembler "__stack_chk_guard" } } */
25c7f1
+
25c7f1
+void f(void) { }
25c7f1
--- gcc/testsuite/gcc.target/powerpc/ssp-2.c	(nonexistent)
25c7f1
+++ gcc/testsuite/gcc.target/powerpc/ssp-2.c	(revision 244562)
25c7f1
@@ -0,0 +1,6 @@
25c7f1
+/* { dg-do compile } */
25c7f1
+/* { dg-options "-O2 -fstack-protector-all -mstack-protector-guard=tls -mstack-protector-guard-reg=r18 -mstack-protector-guard-offset=0x3038" } */
25c7f1
+
25c7f1
+/* { dg-final { scan-assembler {\m12344\(r?18\)} } } */
25c7f1
+
25c7f1
+void f(void) { }